More enum docs.
[gnutls.git] / tests / crq_key_id.c
blob6c246bb8b54a91761ab203c478a825093383730d
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>
34 #include "utils.h"
36 static void
37 tls_log_func (int level, const char *str)
39 fprintf (stderr, "%s |<%d>| %s", "crq_key_id", level, str);
42 void
43 doit (void)
45 gnutls_x509_privkey_t pkey;
46 gnutls_x509_crq_t crq;
48 size_t pkey_key_id_len;
49 unsigned char *pkey_key_id = NULL;
51 size_t crq_key_id_len;
52 unsigned char *crq_key_id = NULL;
54 gnutls_pk_algorithm_t algorithm;
56 int ret;
58 ret = gnutls_global_init ();
59 if (ret < 0)
60 fail ("gnutls_global_init: %d\n", ret);
62 gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
64 gnutls_global_set_log_function (tls_log_func);
65 gnutls_global_set_log_level (4711);
67 for (algorithm = GNUTLS_PK_RSA; algorithm <= GNUTLS_PK_DSA; algorithm++)
69 ret = gnutls_x509_crq_init (&crq);
70 if (ret < 0)
71 fail ("gnutls_x509_crq_init: %d\n", ret);
73 ret = gnutls_x509_privkey_init (&pkey);
74 if (ret < 0)
76 fail ("gnutls_x509_privkey_init: %d\n", ret);
79 ret = gnutls_x509_privkey_generate (pkey, algorithm, 1024, 0);
80 if (ret < 0)
82 fail ("gnutls_x509_privkey_generate (rsa): %d\n", ret);
84 else
86 success ("Key[%s] generation ok: %d\n",
87 gnutls_pk_algorithm_get_name (algorithm), ret);
90 pkey_key_id_len = 0;
91 ret = gnutls_x509_privkey_get_key_id (pkey, 0, pkey_key_id,
92 &pkey_key_id_len);
93 if (ret != GNUTLS_E_SHORT_MEMORY_BUFFER)
95 fail ("gnutls_x509_privkey_get_key_id incorrectly returns %d\n",
96 ret);
99 pkey_key_id = malloc (sizeof (unsigned char) * pkey_key_id_len);
100 ret = gnutls_x509_privkey_get_key_id (pkey, 0, pkey_key_id,
101 &pkey_key_id_len);
102 if (ret != GNUTLS_E_SUCCESS)
104 fail ("gnutls_x509_privkey_get_key_id incorrectly returns %d\n",
105 ret);
108 ret = gnutls_x509_crq_set_version (crq, 1);
109 if (ret < 0)
111 fail ("gnutls_x509_crq_set_version: %d\n", ret);
114 ret = gnutls_x509_crq_set_key (crq, pkey);
115 if (ret < 0)
117 fail ("gnutls_x509_crq_set_key: %d\n", ret);
120 ret = gnutls_x509_crq_set_dn_by_oid (crq, GNUTLS_OID_X520_COMMON_NAME,
121 0, "CN-Test", 7);
122 if (ret < 0)
124 fail ("gnutls_x509_crq_set_dn_by_oid: %d\n", ret);
127 ret = gnutls_x509_crq_sign (crq, pkey);
128 if (ret)
130 fail ("gnutls_x509_crq_sign: %d\n", ret);
133 crq_key_id_len = 0;
134 ret = gnutls_x509_crq_get_key_id (crq, 0, crq_key_id, &crq_key_id_len);
135 if (ret != GNUTLS_E_SHORT_MEMORY_BUFFER)
137 fail ("gnutls_x509_crq_get_key_id incorrectly returns %d\n", ret);
140 crq_key_id = malloc (sizeof (unsigned char) * crq_key_id_len);
141 ret = gnutls_x509_crq_get_key_id (crq, 0, crq_key_id, &crq_key_id_len);
142 if (ret != GNUTLS_E_SUCCESS)
144 fail ("gnutls_x509_crq_get_key_id incorrectly returns %d\n", ret);
147 if (crq_key_id_len == pkey_key_id_len)
149 ret = memcmp (crq_key_id, pkey_key_id, crq_key_id_len);
150 if (ret == 0)
152 success ("Key ids are identical. OK.\n");
154 else
156 fail ("Key ids differ incorrectly: %d\n", ret);
159 else
161 fail ("Key_id lengths differ incorrectly: %d - %d\n",
162 (int)crq_key_id_len, (int)pkey_key_id_len);
166 if (pkey_key_id)
168 free (pkey_key_id);
169 pkey_key_id = NULL;
172 if (crq_key_id)
174 free (crq_key_id);
175 crq_key_id = NULL;
178 gnutls_x509_crq_deinit (crq);
179 gnutls_x509_privkey_deinit (pkey);
182 gnutls_global_deinit ();