Imported Upstream version 3.4.11
[platform/upstream/gnutls.git] / tests / pkcs12_simple.c
1 /*
2  * Copyright (C) 2005-2012 Free Software Foundation, Inc.
3  * Copyright (C) 2012 Nikos Mavrogiannopoulos
4  *
5  * Author: Simon Josefsson
6  *
7  * This file is part of GnuTLS.
8  *
9  * GnuTLS is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * GnuTLS is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with GnuTLS; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <gnutls/pkcs12.h>
31 #include <gnutls/x509.h>
32 #include "utils.h"
33
34 static void tls_log_func(int level, const char *str)
35 {
36         fprintf(stderr, "<%d>| %s", level, str);
37 }
38
39 void doit(void)
40 {
41 #ifdef ENABLE_NON_SUITEB_CURVES
42         const char *filename, *password = "1234";
43         gnutls_pkcs12_t pkcs12;
44         gnutls_datum_t data;
45         gnutls_x509_crt_t *chain, *extras;
46         unsigned int chain_size = 0, extras_size = 0, i;
47         gnutls_x509_privkey_t pkey;
48         int ret;
49
50         ret = global_init();
51         if (ret < 0)
52                 fail("global_init failed %d\n", ret);
53
54         gnutls_global_set_log_function(tls_log_func);
55         if (debug)
56                 gnutls_global_set_log_level(2);
57
58         ret = gnutls_pkcs12_init(&pkcs12);
59         if (ret < 0)
60                 fail("initialization failed: %s\n", gnutls_strerror(ret));
61
62         filename = getenv("PKCS12_MANY_CERTS_FILE");
63
64         if (!filename)
65                 filename = "pkcs12-decode/pkcs12_5certs.p12";
66
67         if (debug)
68                 success
69                     ("Reading PKCS#12 blob from `%s' using password `%s'.\n",
70                      filename, password);
71
72         ret = gnutls_load_file(filename, &data);
73         if (ret < 0)
74                 fail("cannot open file");
75
76         ret = gnutls_pkcs12_import(pkcs12, &data, GNUTLS_X509_FMT_DER, 0);
77         if (ret < 0)
78                 fail("pkcs12_import failed %d: %s\n", ret,
79                      gnutls_strerror(ret));
80
81         if (debug)
82                 success("Read file OK\n");
83
84         ret =
85             gnutls_pkcs12_simple_parse(pkcs12, password, &pkey, &chain,
86                                        &chain_size, &extras, &extras_size,
87                                        NULL, 0);
88         if (ret < 0)
89                 fail("pkcs12_simple_parse failed %d: %s\n", ret,
90                      gnutls_strerror(ret));
91
92         if (chain_size != 1)
93                 fail("chain size (%u) should have been 1\n", chain_size);
94
95         if (extras_size != 4)
96                 fail("extras size (%u) should have been 4\n", extras_size);
97
98         if (debug) {
99                 char dn[512];
100                 size_t dn_size;
101
102                 dn_size = sizeof(dn);
103                 ret = gnutls_x509_crt_get_dn(chain[0], dn, &dn_size);
104                 if (ret < 0)
105                         fail("crt_get_dn failed %d: %s\n", ret,
106                              gnutls_strerror(ret));
107
108                 success("dn: %s\n", dn);
109
110                 dn_size = sizeof(dn);
111                 ret =
112                     gnutls_x509_crt_get_issuer_dn(chain[0], dn, &dn_size);
113                 if (ret < 0)
114                         fail("crt_get_dn failed %d: %s\n", ret,
115                              gnutls_strerror(ret));
116
117                 success("issuer dn: %s\n", dn);
118         }
119
120         gnutls_pkcs12_deinit(pkcs12);
121         gnutls_x509_privkey_deinit(pkey);
122
123         for (i = 0; i < chain_size; i++)
124                 gnutls_x509_crt_deinit(chain[i]);
125         gnutls_free(chain);
126
127         for (i = 0; i < extras_size; i++)
128                 gnutls_x509_crt_deinit(extras[i]);
129         gnutls_free(extras);
130
131         /* Try gnutls_x509_privkey_import2() */
132         ret = gnutls_x509_privkey_init(&pkey);
133         if (ret < 0)
134                 fail("gnutls_x509_privkey_init failed %d: %s\n", ret,
135                      gnutls_strerror(ret));
136
137         ret =
138             gnutls_x509_privkey_import2(pkey, &data, GNUTLS_X509_FMT_DER,
139                                         password, 0);
140         if (ret < 0)
141                 fail("gnutls_x509_privkey_import2 failed %d: %s\n", ret,
142                      gnutls_strerror(ret));
143         gnutls_x509_privkey_deinit(pkey);
144
145         free(data.data);
146
147         gnutls_global_deinit();
148 #else
149         exit(77);
150 #endif
151 }