Tizen 2.0 Release
[external/libgnutls26.git] / tests / crq_key_id.c
1 /*
2  * Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
3  *
4  * Author: David Marín Carreño
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 <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <gnutls/gnutls.h>
31 #include <gnutls/x509.h>
32 #include <gnutls/abstract.h>
33
34 #include "utils.h"
35
36 static void
37 tls_log_func (int level, const char *str)
38 {
39   fprintf (stderr, "%s |<%d>| %s", "crq_key_id", level, str);
40 }
41
42 void
43 doit (void)
44 {
45   gnutls_x509_privkey_t pkey;
46   gnutls_privkey_t abs_pkey;
47   gnutls_x509_crq_t crq;
48
49   size_t pkey_key_id_len;
50   unsigned char *pkey_key_id = NULL;
51
52   size_t crq_key_id_len;
53   unsigned char *crq_key_id = NULL;
54
55   gnutls_pk_algorithm_t algorithm;
56
57   int ret;
58
59   ret = gnutls_global_init ();
60   if (ret < 0)
61     fail ("gnutls_global_init: %d\n", ret);
62
63   gnutls_global_set_log_function (tls_log_func);
64   if (debug)
65     gnutls_global_set_log_level (4711);
66
67   for (algorithm = GNUTLS_PK_RSA; algorithm <= GNUTLS_PK_DSA; algorithm++)
68     {
69       ret = gnutls_x509_crq_init (&crq);
70       if (ret < 0)
71         fail ("gnutls_x509_crq_init: %d\n", ret);
72
73       ret = gnutls_x509_privkey_init (&pkey);
74       if (ret < 0)
75         {
76           fail ("gnutls_x509_privkey_init: %d\n", ret);
77         }
78
79       ret = gnutls_privkey_init (&abs_pkey);
80       if (ret < 0)
81         {
82           fail ("gnutls_privkey_init: %d\n", ret);
83         }
84
85       ret = gnutls_x509_privkey_generate (pkey, algorithm, 1024, 0);
86       if (ret < 0)
87         {
88           fail ("gnutls_x509_privkey_generate (rsa): %d\n", ret);
89         }
90       else if (debug)
91         {
92           success ("Key[%s] generation ok: %d\n",
93                    gnutls_pk_algorithm_get_name (algorithm), ret);
94         }
95
96       pkey_key_id_len = 0;
97       ret = gnutls_x509_privkey_get_key_id (pkey, 0, pkey_key_id,
98                                             &pkey_key_id_len);
99       if (ret != GNUTLS_E_SHORT_MEMORY_BUFFER)
100         {
101           fail ("gnutls_x509_privkey_get_key_id incorrectly returns %d\n",
102                 ret);
103         }
104
105       pkey_key_id = malloc (sizeof (unsigned char) * pkey_key_id_len);
106       ret = gnutls_x509_privkey_get_key_id (pkey, 0, pkey_key_id,
107                                             &pkey_key_id_len);
108       if (ret != GNUTLS_E_SUCCESS)
109         {
110           fail ("gnutls_x509_privkey_get_key_id incorrectly returns %d\n",
111                 ret);
112         }
113
114       ret = gnutls_x509_crq_set_version (crq, 1);
115       if (ret < 0)
116         {
117           fail ("gnutls_x509_crq_set_version: %d\n", ret);
118         }
119
120       ret = gnutls_x509_crq_set_key (crq, pkey);
121       if (ret < 0)
122         {
123           fail ("gnutls_x509_crq_set_key: %d\n", ret);
124         }
125
126       ret = gnutls_x509_crq_set_dn_by_oid (crq, GNUTLS_OID_X520_COMMON_NAME,
127                                            0, "CN-Test", 7);
128       if (ret < 0)
129         {
130           fail ("gnutls_x509_crq_set_dn_by_oid: %d\n", ret);
131         }
132
133       ret = gnutls_privkey_import_x509( abs_pkey, pkey, 0);
134       if (ret < 0)
135         {
136           fail ("gnutls_privkey_import_x509: %d\n", ret);
137         }
138
139       ret = gnutls_x509_crq_privkey_sign (crq, abs_pkey, GNUTLS_DIG_SHA1, 0);
140       if (ret < 0)
141         {
142           fail ("gnutls_x509_crq_sign: %d\n", ret);
143         }
144
145       ret = gnutls_x509_crq_verify (crq, 0);
146       if (ret < 0)
147         {
148           fail ("gnutls_x509_crq_verify: %d\n", ret);
149         }
150
151       crq_key_id_len = 0;
152       ret = gnutls_x509_crq_get_key_id (crq, 0, crq_key_id, &crq_key_id_len);
153       if (ret != GNUTLS_E_SHORT_MEMORY_BUFFER)
154         {
155           fail ("gnutls_x509_crq_get_key_id incorrectly returns %d\n", ret);
156         }
157
158       crq_key_id = malloc (sizeof (unsigned char) * crq_key_id_len);
159       ret = gnutls_x509_crq_get_key_id (crq, 0, crq_key_id, &crq_key_id_len);
160       if (ret != GNUTLS_E_SUCCESS)
161         {
162           fail ("gnutls_x509_crq_get_key_id incorrectly returns %d\n", ret);
163         }
164
165       if (crq_key_id_len == pkey_key_id_len)
166         {
167           ret = memcmp (crq_key_id, pkey_key_id, crq_key_id_len);
168           if (ret == 0)
169             {
170               if (debug)
171                 success ("Key ids are identical. OK.\n");
172             }
173           else
174             {
175               fail ("Key ids differ incorrectly: %d\n", ret);
176             }
177         }
178       else
179         {
180           fail ("Key_id lengths differ incorrectly: %d - %d\n",
181                 (int) crq_key_id_len, (int) pkey_key_id_len);
182         }
183
184
185       if (pkey_key_id)
186         {
187           free (pkey_key_id);
188           pkey_key_id = NULL;
189         }
190
191       if (crq_key_id)
192         {
193           free (crq_key_id);
194           crq_key_id = NULL;
195         }
196
197       gnutls_x509_crq_deinit (crq);
198       gnutls_x509_privkey_deinit (pkey);
199       gnutls_privkey_deinit (abs_pkey);
200     }
201
202   gnutls_global_deinit ();
203 }