Tizen 2.0 Release
[external/libgnutls26.git] / tests / set_pkcs12_cred.c
1 /*
2  * Copyright (C) 2005, 2006, 2008, 2010 Free Software Foundation, Inc.
3  *
4  * Author: Simon Josefsson
5  *
6  * This file is part of GnuTLS.
7  *
8  * GnuTLS is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * GnuTLS is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with GnuTLS; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdlib.h>
28
29 #include "utils.h"
30
31 void
32 doit (void)
33 {
34   gnutls_certificate_credentials_t x509cred;
35   const char *file, *password;
36   int ret;
37
38   ret = gnutls_global_init ();
39   if (ret < 0)
40     fail ("gnutls_global_init failed %d\n", ret);
41
42   ret = gnutls_certificate_allocate_credentials (&x509cred);
43   if (ret < 0)
44     fail ("gnutls_certificate_allocate_credentials failed %d\n", ret);
45
46   file = getenv ("PKCS12FILE");
47   password = getenv ("PKCS12PASSWORD");
48
49   if (!file)
50     file = "pkcs12-decode/client.p12";
51   if (!password)
52     password = "foobar";
53
54   if (debug)
55     success ("Reading PKCS#12 blob from `%s' using password `%s'.\n",
56              file, password);
57   ret = gnutls_certificate_set_x509_simple_pkcs12_file (x509cred,
58                                                         file,
59                                                         GNUTLS_X509_FMT_DER,
60                                                         password);
61   if (ret < 0)
62     fail ("x509_pkcs12 failed %d: %s\n", ret, gnutls_strerror (ret));
63
64   if (debug)
65     success ("Read file OK\n");
66
67   gnutls_certificate_free_credentials (x509cred);
68
69   /* try now if we can read correctly from a pkcs12 file that
70    * contains two certificates (one unrelated with key)
71    */
72   ret = gnutls_certificate_allocate_credentials (&x509cred);
73   if (ret < 0)
74     fail ("gnutls_certificate_allocate_credentials failed %d\n", ret);
75
76   file = getenv ("PKCS12FILE_2");
77   password = getenv ("PKCS12PASSWORD_2");
78
79   if (!file)
80     file = "pkcs12-decode/pkcs12_2certs.p12";
81   if (!password)
82     password = "";
83
84   if (debug)
85     success ("Reading PKCS#12 blob from `%s' using password `%s'.\n",
86              file, password);
87   ret = gnutls_certificate_set_x509_simple_pkcs12_file (x509cred,
88                                                         file,
89                                                         GNUTLS_X509_FMT_DER,
90                                                         password);
91   if (ret < 0)
92     fail ("x509_pkcs12 failed %d: %s\n", ret, gnutls_strerror (ret));
93
94   if (debug)
95     success ("Read file OK\n");
96
97   gnutls_certificate_free_credentials (x509cred);
98
99   gnutls_global_deinit ();
100 }