2 * Copyright (C) 2005-2012 Free Software Foundation, Inc.
4 * Author: Simon Josefsson
6 * This file is part of GnuTLS.
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.
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.
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
28 #include <read-file.h>
29 #include <gnutls/pkcs12.h>
30 #include <gnutls/x509.h>
36 const char *filename
, *password
= "1234";
37 gnutls_pkcs12_t pkcs12
;
38 unsigned char* file_data
;
41 gnutls_x509_crt_t
* chain
, * extras
;
42 unsigned int chain_size
, extras_size
, i
;
43 gnutls_x509_privkey_t pkey
;
46 ret
= gnutls_global_init ();
48 fail ("gnutls_global_init failed %d\n", ret
);
50 ret
= gnutls_pkcs12_init(&pkcs12
);
52 fail ("initialization failed: %s\n", gnutls_strerror(ret
));
54 filename
= getenv ("PKCS12_MANY_CERTS_FILE");
57 filename
= "pkcs12-decode/pkcs12_5certs.p12";
60 success ("Reading PKCS#12 blob from `%s' using password `%s'.\n",
63 file_data
= (void*)read_binary_file( filename
, &file_size
);
64 if (file_data
== NULL
)
65 fail("cannot open file");
67 data
.data
= file_data
;
68 data
.size
= file_size
;
69 ret
= gnutls_pkcs12_import(pkcs12
, &data
, GNUTLS_X509_FMT_DER
, 0);
71 fail ("pkcs12_import failed %d: %s\n", ret
, gnutls_strerror (ret
));
76 success ("Read file OK\n");
78 ret
= gnutls_pkcs12_simple_parse (pkcs12
, password
, &pkey
, &chain
, &chain_size
,
79 &extras
, &extras_size
, NULL
, 0);
81 fail ("pkcs12_simple_parse failed %d: %s\n", ret
, gnutls_strerror (ret
));
84 fail("chain size (%u) should have been 1\n", chain_size
);
87 fail("extras size (%u) should have been 4\n", extras_size
);
95 ret
= gnutls_x509_crt_get_dn(chain
[0], dn
, &dn_size
);
97 fail ("crt_get_dn failed %d: %s\n", ret
, gnutls_strerror (ret
));
99 success("dn: %s\n", dn
);
101 dn_size
= sizeof(dn
);
102 ret
= gnutls_x509_crt_get_issuer_dn(chain
[0], dn
, &dn_size
);
104 fail ("crt_get_dn failed %d: %s\n", ret
, gnutls_strerror (ret
));
106 success("issuer dn: %s\n", dn
);
109 gnutls_pkcs12_deinit(pkcs12
);
110 gnutls_x509_privkey_deinit(pkey
);
112 for (i
=0;i
<chain_size
;i
++)
113 gnutls_x509_crt_deinit(chain
[i
]);
116 for (i
=0;i
<extras_size
;i
++)
117 gnutls_x509_crt_deinit(extras
[i
]);
120 gnutls_global_deinit ();