Add `gnutls/dtls.h' to the distribution.
[gnutls.git] / tests / crq_key_id.c
blob2d7a9c455f7070c91d345e5a92f97dfe69f7990d
1 /*
2 * Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
4 * Author: David Marín Carreño
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 <stdio.h>
29 #include <string.h>
30 #include <gcrypt.h>
31 #include <gnutls/gnutls.h>
32 #include <gnutls/x509.h>
33 #include <gnutls/abstract.h>
35 #include "utils.h"
37 static void
38 tls_log_func (int level, const char *str)
40 fprintf (stderr, "%s |<%d>| %s", "crq_key_id", level, str);
43 void
44 doit (void)
46 gnutls_x509_privkey_t pkey;
47 gnutls_privkey_t abs_pkey;
48 gnutls_x509_crq_t crq;
50 size_t pkey_key_id_len;
51 unsigned char *pkey_key_id = NULL;
53 size_t crq_key_id_len;
54 unsigned char *crq_key_id = NULL;
56 gnutls_pk_algorithm_t algorithm;
58 int ret;
60 ret = gnutls_global_init ();
61 if (ret < 0)
62 fail ("gnutls_global_init: %d\n", ret);
64 gnutls_global_set_log_function (tls_log_func);
65 if (debug)
66 gnutls_global_set_log_level (4711);
68 for (algorithm = GNUTLS_PK_RSA; algorithm <= GNUTLS_PK_DSA; algorithm++)
70 ret = gnutls_x509_crq_init (&crq);
71 if (ret < 0)
72 fail ("gnutls_x509_crq_init: %d\n", ret);
74 ret = gnutls_x509_privkey_init (&pkey);
75 if (ret < 0)
77 fail ("gnutls_x509_privkey_init: %d\n", ret);
80 ret = gnutls_privkey_init (&abs_pkey);
81 if (ret < 0)
83 fail ("gnutls_privkey_init: %d\n", ret);
86 ret = gnutls_x509_privkey_generate (pkey, algorithm, 1024, 0);
87 if (ret < 0)
89 fail ("gnutls_x509_privkey_generate (rsa): %d\n", ret);
91 else if (debug)
93 success ("Key[%s] generation ok: %d\n",
94 gnutls_pk_algorithm_get_name (algorithm), ret);
97 pkey_key_id_len = 0;
98 ret = gnutls_x509_privkey_get_key_id (pkey, 0, pkey_key_id,
99 &pkey_key_id_len);
100 if (ret != GNUTLS_E_SHORT_MEMORY_BUFFER)
102 fail ("gnutls_x509_privkey_get_key_id incorrectly returns %d\n",
103 ret);
106 pkey_key_id = malloc (sizeof (unsigned char) * pkey_key_id_len);
107 ret = gnutls_x509_privkey_get_key_id (pkey, 0, pkey_key_id,
108 &pkey_key_id_len);
109 if (ret != GNUTLS_E_SUCCESS)
111 fail ("gnutls_x509_privkey_get_key_id incorrectly returns %d\n",
112 ret);
115 ret = gnutls_x509_crq_set_version (crq, 1);
116 if (ret < 0)
118 fail ("gnutls_x509_crq_set_version: %d\n", ret);
121 ret = gnutls_x509_crq_set_key (crq, pkey);
122 if (ret < 0)
124 fail ("gnutls_x509_crq_set_key: %d\n", ret);
127 ret = gnutls_x509_crq_set_dn_by_oid (crq, GNUTLS_OID_X520_COMMON_NAME,
128 0, "CN-Test", 7);
129 if (ret < 0)
131 fail ("gnutls_x509_crq_set_dn_by_oid: %d\n", ret);
134 ret = gnutls_privkey_import_x509( abs_pkey, pkey, 0);
135 if (ret < 0)
137 fail ("gnutls_privkey_import_x509: %d\n", ret);
140 ret = gnutls_x509_crq_privkey_sign (crq, abs_pkey, GNUTLS_DIG_SHA1, 0);
141 if (ret)
143 fail ("gnutls_x509_crq_sign: %d\n", ret);
146 crq_key_id_len = 0;
147 ret = gnutls_x509_crq_get_key_id (crq, 0, crq_key_id, &crq_key_id_len);
148 if (ret != GNUTLS_E_SHORT_MEMORY_BUFFER)
150 fail ("gnutls_x509_crq_get_key_id incorrectly returns %d\n", ret);
153 crq_key_id = malloc (sizeof (unsigned char) * crq_key_id_len);
154 ret = gnutls_x509_crq_get_key_id (crq, 0, crq_key_id, &crq_key_id_len);
155 if (ret != GNUTLS_E_SUCCESS)
157 fail ("gnutls_x509_crq_get_key_id incorrectly returns %d\n", ret);
160 if (crq_key_id_len == pkey_key_id_len)
162 ret = memcmp (crq_key_id, pkey_key_id, crq_key_id_len);
163 if (ret == 0)
165 if (debug)
166 success ("Key ids are identical. OK.\n");
168 else
170 fail ("Key ids differ incorrectly: %d\n", ret);
173 else
175 fail ("Key_id lengths differ incorrectly: %d - %d\n",
176 (int) crq_key_id_len, (int) pkey_key_id_len);
180 if (pkey_key_id)
182 free (pkey_key_id);
183 pkey_key_id = NULL;
186 if (crq_key_id)
188 free (crq_key_id);
189 crq_key_id = NULL;
192 gnutls_x509_crq_deinit (crq);
193 gnutls_x509_privkey_deinit (pkey);
194 gnutls_privkey_deinit (abs_pkey);
197 gnutls_global_deinit ();