Tizen 2.0 Release
[external/libgnutls26.git] / tests / pkcs12_encode.c
1 /*
2  * Copyright (C) 2009, 2010 Free Software Foundation, Inc.
3  *
4  * Author: Simon Josefsson
5  *
6  * This file is part of GnuTLS.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <gnutls/gnutls.h>
28 #include <gnutls/x509.h>
29 #include <gnutls/pkcs12.h>
30
31 #include "utils.h"
32
33 #include <error.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 static char client_pem[] =
38   "-----BEGIN CERTIFICATE-----\n"
39   "MIICHjCCAYmgAwIBAgIERiYdNzALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
40   "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTI3WhcNMDgwNDE3MTMyOTI3WjAdMRsw\n"
41   "GQYDVQQDExJHbnVUTFMgdGVzdCBjbGllbnQwgZwwCwYJKoZIhvcNAQEBA4GMADCB\n"
42   "iAKBgLtmQ/Xyxde2jMzF3/WIO7HJS2oOoa0gUEAIgKFPXKPQ+GzP5jz37AR2ExeL\n"
43   "ZIkiW8DdU3w77XwEu4C5KL6Om8aOoKUSy/VXHqLnu7czSZ/ju0quak1o/8kR4jKN\n"
44   "zj2AC41179gAgY8oBAOgIo1hBAf6tjd9IQdJ0glhaZiQo1ipAgMBAAGjdjB0MAwG\n"
45   "A1UdEwEB/wQCMAAwEwYDVR0lBAwwCgYIKwYBBQUHAwIwDwYDVR0PAQH/BAUDAweg\n"
46   "ADAdBgNVHQ4EFgQUTLkKm/odNON+3svSBxX+odrLaJEwHwYDVR0jBBgwFoAU6Twc\n"
47   "+62SbuYGpFYsouHAUyfI8pUwCwYJKoZIhvcNAQEFA4GBALujmBJVZnvaTXr9cFRJ\n"
48   "jpfc/3X7sLUsMvumcDE01ls/cG5mIatmiyEU9qI3jbgUf82z23ON/acwJf875D3/\n"
49   "U7jyOsBJ44SEQITbin2yUeJMIm1tievvdNXBDfW95AM507ShzP12sfiJkJfjjdhy\n"
50   "dc8Siq5JojruiMizAf0pA7in\n" "-----END CERTIFICATE-----\n";
51 const gnutls_datum_t client_dat = { client_pem, sizeof (client_pem) };
52
53 static char ca_pem[] =
54   "-----BEGIN CERTIFICATE-----\n"
55   "MIIB5zCCAVKgAwIBAgIERiYdJzALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
56   "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTExWhcNMDgwNDE3MTMyOTExWjAZMRcw\n"
57   "FQYDVQQDEw5HbnVUTFMgdGVzdCBDQTCBnDALBgkqhkiG9w0BAQEDgYwAMIGIAoGA\n"
58   "vuyYeh1vfmslnuggeEKgZAVmQ5ltSdUY7H25WGSygKMUYZ0KT74v8C780qtcNt9T\n"
59   "7EPH/N6RvB4BprdssgcQLsthR3XKA84jbjjxNCcaGs33lvOz8A1nf8p3hD+cKfRi\n"
60   "kfYSW2JazLrtCC4yRCas/SPOUxu78of+3HiTfFm/oXUCAwEAAaNDMEEwDwYDVR0T\n"
61   "AQH/BAUwAwEB/zAPBgNVHQ8BAf8EBQMDBwQAMB0GA1UdDgQWBBTpPBz7rZJu5gak\n"
62   "Viyi4cBTJ8jylTALBgkqhkiG9w0BAQUDgYEAiaIRqGfp1jPpNeVhABK60SU0KIAy\n"
63   "njuu7kHq5peUgYn8Jd9zNzExBOEp1VOipGsf6G66oQAhDFp2o8zkz7ZH71zR4HEW\n"
64   "KoX6n5Emn6DvcEH/9pAhnGxNHJAoS7czTKv/JDZJhkqHxyrE1fuLsg5Qv25DTw7+\n"
65   "PfqUpIhz5Bbm7J4=\n" "-----END CERTIFICATE-----\n";
66 const gnutls_datum_t ca_dat = { ca_pem, sizeof (ca_pem) };
67
68 static void
69 tls_log_func (int level, const char *str)
70 {
71   fprintf (stderr, "|<%d>| %s", level, str);
72 }
73
74 void
75 doit (void)
76 {
77   gnutls_pkcs12_t pkcs12;
78   gnutls_x509_crt_t client;
79   gnutls_x509_crt_t ca;
80   gnutls_pkcs12_bag_t bag;
81   char key_id_buf[20];
82   gnutls_datum_t key_id;
83   int ret, indx;
84   char outbuf[10240];
85   size_t size;
86   int i;
87
88   ret = gnutls_global_init ();
89   if (ret < 0)
90     error (EXIT_FAILURE, 0, "gnutls_global_init %d", ret);
91
92   gnutls_global_init ();
93   gnutls_global_set_log_function (tls_log_func);
94   if (debug)
95     gnutls_global_set_log_level (4711);
96
97   /* Read certs. */
98   ret = gnutls_x509_crt_init (&client);
99   if (ret < 0)
100     error (EXIT_FAILURE, 0, "crt_init: %d", ret);
101
102   ret = gnutls_x509_crt_import (client, &client_dat, GNUTLS_X509_FMT_PEM);
103   if (ret < 0)
104     error (EXIT_FAILURE, 0, "crt_import: %d", ret);
105
106   ret = gnutls_x509_crt_init (&ca);
107   if (ret < 0)
108     error (EXIT_FAILURE, 0, "ca_init: %d", ret);
109
110   ret = gnutls_x509_crt_import (ca, &ca_dat, GNUTLS_X509_FMT_PEM);
111   if (ret < 0)
112     error (EXIT_FAILURE, 0, "ca_import: %d", ret);
113
114   /* Create PKCS#12 structure. */
115   ret = gnutls_pkcs12_init (&pkcs12);
116   if (ret < 0)
117     error (EXIT_FAILURE, 0, "pkcs12_init: %d", ret);
118
119   /* Generate and add PKCS#12 cert bags. */
120   for (i = 0; i < 2; i++)
121     {
122       ret = gnutls_pkcs12_bag_init (&bag);
123       if (ret < 0)
124         error (EXIT_FAILURE, 0, "bag_init: %d", ret);
125
126       ret = gnutls_pkcs12_bag_set_crt (bag, i == 0 ? client : ca);
127       if (ret < 0)
128         error (EXIT_FAILURE, 0, "set_crt: %d", ret);
129
130       indx = ret;
131
132       ret = gnutls_pkcs12_bag_set_friendly_name (bag, indx,
133                                                  i == 0 ? "client" : "ca");
134       if (ret < 0)
135         error (EXIT_FAILURE, 0, "set_friendly_name: %d", ret);
136
137       size = sizeof (key_id_buf);
138       ret = gnutls_x509_crt_get_key_id (i == 0 ? client : ca, 0,
139                                         key_id_buf, &size);
140       if (ret < 0)
141         error (EXIT_FAILURE, 0, "get_key_id: %d", ret);
142
143       key_id.data = key_id_buf;
144       key_id.size = size;
145
146       ret = gnutls_pkcs12_bag_set_key_id (bag, indx, &key_id);
147       if (ret < 0)
148         error (EXIT_FAILURE, 0, "bag_set_key_id: %d", ret);
149
150       ret = gnutls_pkcs12_bag_encrypt (bag, "pass",
151                                        i == 0 ? GNUTLS_PKCS8_USE_PKCS12_3DES
152                                        : GNUTLS_PKCS_USE_PKCS12_RC2_40);
153       if (ret < 0)
154         error (EXIT_FAILURE, 0, "bag_encrypt: %d: %s", ret,
155                i == 0 ? "3DES" : "RC2-40");
156
157       ret = gnutls_pkcs12_set_bag (pkcs12, bag);
158       if (ret < 0)
159         error (EXIT_FAILURE, 0, "set_bag: %d", ret);
160
161       gnutls_pkcs12_bag_deinit (bag);
162     }
163
164   /* MAC the structure, export and print. */
165   ret = gnutls_pkcs12_generate_mac (pkcs12, "pass");
166   if (ret < 0)
167     error (EXIT_FAILURE, 0, "generate_mac: %d", ret);
168
169   size = sizeof (outbuf);
170   ret = gnutls_pkcs12_export (pkcs12, GNUTLS_X509_FMT_PEM, outbuf, &size);
171   if (ret < 0)
172     error (EXIT_FAILURE, 0, "pkcs12_export: %d", ret);
173
174   if (debug)
175     fwrite (outbuf, size, 1, stdout);
176
177   /* Cleanup. */
178   gnutls_pkcs12_deinit (pkcs12);
179   gnutls_x509_crt_deinit (client);
180   gnutls_x509_crt_deinit (ca);
181   gnutls_global_deinit ();
182
183 }