Added gnutls_session_get_id2().
[gnutls.git] / tests / pkcs12_simple.c
blobad17c0e41359934d0e6a82979fb8259bb0b880ba
1 /*
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
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
27 #include <stdlib.h>
28 #include <read-file.h>
29 #include <gnutls/pkcs12.h>
30 #include <gnutls/x509.h>
31 #include "utils.h"
33 void
34 doit (void)
36 const char *filename, *password = "1234";
37 gnutls_pkcs12_t pkcs12;
38 unsigned char* file_data;
39 size_t file_size;
40 gnutls_datum_t data;
41 gnutls_x509_crt_t * chain, * extras;
42 unsigned int chain_size, extras_size, i;
43 gnutls_x509_privkey_t pkey;
44 int ret;
46 ret = gnutls_global_init ();
47 if (ret < 0)
48 fail ("gnutls_global_init failed %d\n", ret);
50 ret = gnutls_pkcs12_init(&pkcs12);
51 if (ret < 0)
52 fail ("initialization failed: %s\n", gnutls_strerror(ret));
54 filename = getenv ("PKCS12_MANY_CERTS_FILE");
56 if (!filename)
57 filename = "pkcs12-decode/pkcs12_5certs.p12";
59 if (debug)
60 success ("Reading PKCS#12 blob from `%s' using password `%s'.\n",
61 filename, password);
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);
70 if (ret < 0)
71 fail ("pkcs12_import failed %d: %s\n", ret, gnutls_strerror (ret));
73 free(file_data);
75 if (debug)
76 success ("Read file OK\n");
78 ret = gnutls_pkcs12_simple_parse (pkcs12, password, &pkey, &chain, &chain_size,
79 &extras, &extras_size, NULL, 0);
80 if (ret < 0)
81 fail ("pkcs12_simple_parse failed %d: %s\n", ret, gnutls_strerror (ret));
83 if (chain_size != 1)
84 fail("chain size (%u) should have been 1\n", chain_size);
86 if (extras_size != 4)
87 fail("extras size (%u) should have been 4\n", extras_size);
89 if (debug)
91 char dn[512];
92 size_t dn_size;
94 dn_size = sizeof(dn);
95 ret = gnutls_x509_crt_get_dn(chain[0], dn, &dn_size);
96 if (ret < 0)
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);
103 if (ret < 0)
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]);
114 gnutls_free(chain);
116 for (i=0;i<extras_size;i++)
117 gnutls_x509_crt_deinit(extras[i]);
118 gnutls_free(extras);
120 gnutls_global_deinit ();