2 * Copyright (C) 2005-2012 Free Software Foundation, Inc.
3 * Copyright (C) 2012 Nikos Mavrogiannopoulos
5 * Author: Simon Josefsson
7 * This file is part of GnuTLS.
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.
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.
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
29 #include <read-file.h>
30 #include <gnutls/pkcs12.h>
31 #include <gnutls/x509.h>
35 tls_log_func (int level
, const char *str
)
37 fprintf (stderr
, "<%d>| %s", level
, str
);
43 const char *filename
, *password
= "1234";
44 gnutls_pkcs12_t pkcs12
;
45 unsigned char* file_data
;
48 gnutls_x509_crt_t
* chain
, * extras
;
49 unsigned int chain_size
, extras_size
, i
;
50 gnutls_x509_privkey_t pkey
;
53 ret
= gnutls_global_init ();
55 fail ("gnutls_global_init failed %d\n", ret
);
57 gnutls_global_set_log_function (tls_log_func
);
59 gnutls_global_set_log_level (2);
61 ret
= gnutls_pkcs12_init(&pkcs12
);
63 fail ("initialization failed: %s\n", gnutls_strerror(ret
));
65 filename
= getenv ("PKCS12_MANY_CERTS_FILE");
68 filename
= "pkcs12-decode/pkcs12_5certs.p12";
71 success ("Reading PKCS#12 blob from `%s' using password `%s'.\n",
74 file_data
= (void*)read_binary_file( filename
, &file_size
);
75 if (file_data
== NULL
)
76 fail("cannot open file");
78 data
.data
= file_data
;
79 data
.size
= file_size
;
80 ret
= gnutls_pkcs12_import(pkcs12
, &data
, GNUTLS_X509_FMT_DER
, 0);
82 fail ("pkcs12_import failed %d: %s\n", ret
, gnutls_strerror (ret
));
85 success ("Read file OK\n");
87 ret
= gnutls_pkcs12_simple_parse (pkcs12
, password
, &pkey
, &chain
, &chain_size
,
88 &extras
, &extras_size
, NULL
, 0);
90 fail ("pkcs12_simple_parse failed %d: %s\n", ret
, gnutls_strerror (ret
));
93 fail("chain size (%u) should have been 1\n", chain_size
);
96 fail("extras size (%u) should have been 4\n", extras_size
);
103 dn_size
= sizeof(dn
);
104 ret
= gnutls_x509_crt_get_dn(chain
[0], dn
, &dn_size
);
106 fail ("crt_get_dn failed %d: %s\n", ret
, gnutls_strerror (ret
));
108 success("dn: %s\n", dn
);
110 dn_size
= sizeof(dn
);
111 ret
= gnutls_x509_crt_get_issuer_dn(chain
[0], dn
, &dn_size
);
113 fail ("crt_get_dn failed %d: %s\n", ret
, gnutls_strerror (ret
));
115 success("issuer dn: %s\n", dn
);
118 gnutls_pkcs12_deinit(pkcs12
);
119 gnutls_x509_privkey_deinit(pkey
);
121 for (i
=0;i
<chain_size
;i
++)
122 gnutls_x509_crt_deinit(chain
[i
]);
125 for (i
=0;i
<extras_size
;i
++)
126 gnutls_x509_crt_deinit(extras
[i
]);
129 /* Try gnutls_x509_privkey_import2() */
130 ret
= gnutls_x509_privkey_init(&pkey
);
132 fail ("gnutls_x509_privkey_init failed %d: %s\n", ret
, gnutls_strerror (ret
));
134 ret
= gnutls_x509_privkey_import2(pkey
, &data
, GNUTLS_X509_FMT_DER
, password
, 0);
136 fail ("gnutls_x509_privkey_import2 failed %d: %s\n", ret
, gnutls_strerror (ret
));
137 gnutls_x509_privkey_deinit(pkey
);
141 gnutls_global_deinit ();