2 * Copyright (C) 2009, 2010 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
27 #include <gnutls/gnutls.h>
28 #include <gnutls/x509.h>
29 #include <gnutls/pkcs12.h>
35 static char client_pem
[] =
36 "-----BEGIN CERTIFICATE-----\n"
37 "MIICHjCCAYmgAwIBAgIERiYdNzALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
38 "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTI3WhcNMDgwNDE3MTMyOTI3WjAdMRsw\n"
39 "GQYDVQQDExJHbnVUTFMgdGVzdCBjbGllbnQwgZwwCwYJKoZIhvcNAQEBA4GMADCB\n"
40 "iAKBgLtmQ/Xyxde2jMzF3/WIO7HJS2oOoa0gUEAIgKFPXKPQ+GzP5jz37AR2ExeL\n"
41 "ZIkiW8DdU3w77XwEu4C5KL6Om8aOoKUSy/VXHqLnu7czSZ/ju0quak1o/8kR4jKN\n"
42 "zj2AC41179gAgY8oBAOgIo1hBAf6tjd9IQdJ0glhaZiQo1ipAgMBAAGjdjB0MAwG\n"
43 "A1UdEwEB/wQCMAAwEwYDVR0lBAwwCgYIKwYBBQUHAwIwDwYDVR0PAQH/BAUDAweg\n"
44 "ADAdBgNVHQ4EFgQUTLkKm/odNON+3svSBxX+odrLaJEwHwYDVR0jBBgwFoAU6Twc\n"
45 "+62SbuYGpFYsouHAUyfI8pUwCwYJKoZIhvcNAQEFA4GBALujmBJVZnvaTXr9cFRJ\n"
46 "jpfc/3X7sLUsMvumcDE01ls/cG5mIatmiyEU9qI3jbgUf82z23ON/acwJf875D3/\n"
47 "U7jyOsBJ44SEQITbin2yUeJMIm1tievvdNXBDfW95AM507ShzP12sfiJkJfjjdhy\n"
48 "dc8Siq5JojruiMizAf0pA7in\n" "-----END CERTIFICATE-----\n";
49 const gnutls_datum_t client_dat
= { client_pem
, sizeof (client_pem
) };
51 static char ca_pem
[] =
52 "-----BEGIN CERTIFICATE-----\n"
53 "MIIB5zCCAVKgAwIBAgIERiYdJzALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
54 "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTExWhcNMDgwNDE3MTMyOTExWjAZMRcw\n"
55 "FQYDVQQDEw5HbnVUTFMgdGVzdCBDQTCBnDALBgkqhkiG9w0BAQEDgYwAMIGIAoGA\n"
56 "vuyYeh1vfmslnuggeEKgZAVmQ5ltSdUY7H25WGSygKMUYZ0KT74v8C780qtcNt9T\n"
57 "7EPH/N6RvB4BprdssgcQLsthR3XKA84jbjjxNCcaGs33lvOz8A1nf8p3hD+cKfRi\n"
58 "kfYSW2JazLrtCC4yRCas/SPOUxu78of+3HiTfFm/oXUCAwEAAaNDMEEwDwYDVR0T\n"
59 "AQH/BAUwAwEB/zAPBgNVHQ8BAf8EBQMDBwQAMB0GA1UdDgQWBBTpPBz7rZJu5gak\n"
60 "Viyi4cBTJ8jylTALBgkqhkiG9w0BAQUDgYEAiaIRqGfp1jPpNeVhABK60SU0KIAy\n"
61 "njuu7kHq5peUgYn8Jd9zNzExBOEp1VOipGsf6G66oQAhDFp2o8zkz7ZH71zR4HEW\n"
62 "KoX6n5Emn6DvcEH/9pAhnGxNHJAoS7czTKv/JDZJhkqHxyrE1fuLsg5Qv25DTw7+\n"
63 "PfqUpIhz5Bbm7J4=\n" "-----END CERTIFICATE-----\n";
64 const gnutls_datum_t ca_dat
= { ca_pem
, sizeof (ca_pem
) };
69 gnutls_pkcs12_t pkcs12
;
70 gnutls_x509_crt_t client
;
72 gnutls_pkcs12_bag_t bag
;
74 gnutls_datum_t key_id
;
80 ret
= gnutls_global_init ();
82 error (EXIT_FAILURE
, 0, "gnutls_global_init %d", ret
);
85 ret
= gnutls_x509_crt_init (&client
);
87 error (EXIT_FAILURE
, 0, "crt_init: %d", ret
);
89 ret
= gnutls_x509_crt_import (client
, &client_dat
, GNUTLS_X509_FMT_PEM
);
91 error (EXIT_FAILURE
, 0, "crt_import: %d", ret
);
93 ret
= gnutls_x509_crt_init (&ca
);
95 error (EXIT_FAILURE
, 0, "ca_init: %d", ret
);
97 ret
= gnutls_x509_crt_import (ca
, &ca_dat
, GNUTLS_X509_FMT_PEM
);
99 error (EXIT_FAILURE
, 0, "ca_import: %d", ret
);
101 /* Create PKCS#12 structure. */
102 ret
= gnutls_pkcs12_init (&pkcs12
);
104 error (EXIT_FAILURE
, 0, "pkcs12_init: %d", ret
);
106 /* Generate and add PKCS#12 cert bags. */
107 for (i
= 0; i
< 2; i
++)
109 ret
= gnutls_pkcs12_bag_init (&bag
);
111 error (EXIT_FAILURE
, 0, "bag_init: %d", ret
);
113 ret
= gnutls_pkcs12_bag_set_crt (bag
, i
== 0 ? client
: ca
);
115 error (EXIT_FAILURE
, 0, "set_crt: %d", ret
);
119 ret
= gnutls_pkcs12_bag_set_friendly_name (bag
, indx
,
120 i
== 0 ? "client" : "ca");
122 error (EXIT_FAILURE
, 0, "set_friendly_name: %d", ret
);
124 size
= sizeof (key_id_buf
);
125 ret
= gnutls_x509_crt_get_key_id (i
== 0 ? client
: ca
, 0,
128 error (EXIT_FAILURE
, 0, "get_key_id: %d", ret
);
130 key_id
.data
= key_id_buf
;
133 ret
= gnutls_pkcs12_bag_set_key_id (bag
, indx
, &key_id
);
135 error (EXIT_FAILURE
, 0, "bag_set_key_id: %d", ret
);
137 ret
= gnutls_pkcs12_bag_encrypt (bag
, "pass",
138 i
== 0 ? GNUTLS_PKCS8_USE_PKCS12_3DES
139 : GNUTLS_PKCS_USE_PKCS12_RC2_40
);
141 error (EXIT_FAILURE
, 0, "bag_encrypt: %d", ret
);
143 ret
= gnutls_pkcs12_set_bag (pkcs12
, bag
);
145 error (EXIT_FAILURE
, 0, "set_bag: %d", ret
);
147 gnutls_pkcs12_bag_deinit (bag
);
150 /* MAC the structure, export and print. */
151 ret
= gnutls_pkcs12_generate_mac (pkcs12
, "pass");
153 error (EXIT_FAILURE
, 0, "generate_mac: %d", ret
);
155 size
= sizeof (outbuf
);
156 ret
= gnutls_pkcs12_export (pkcs12
, GNUTLS_X509_FMT_PEM
, outbuf
, &size
);
158 error (EXIT_FAILURE
, 0, "pkcs12_export: %d", ret
);
160 fwrite (outbuf
, size
, 1, stdout
);
163 gnutls_pkcs12_deinit (pkcs12
);
164 gnutls_x509_crt_deinit (client
);
165 gnutls_x509_crt_deinit (ca
);
166 gnutls_global_deinit ();