added missing file
[gnutls.git] / tests / x509sign-verify.c
blob7e0727cf1fd125e1af164bd87b04edd96b4fb541
1 /*
2 * Copyright (C) 2004-2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos, 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
23 /* Parts copied from GnuTLS example programs. */
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <netinet/in.h>
34 #include <sys/socket.h>
35 #include <arpa/inet.h>
36 #include <unistd.h>
37 #include <gnutls/gnutls.h>
38 #include <gnutls/x509.h>
39 #include <gnutls/abstract.h>
41 #include "utils.h"
43 static void
44 tls_log_func (int level, const char *str)
46 fprintf (stderr, "<%d> %s", level, str);
49 /* sha1 hash of "hello" string */
50 const gnutls_datum_t hash_data = {
51 (void *)
52 "\xaa\xf4\xc6\x1d\xdc\xc5\xe8\xa2\xda\xbe"
53 "\xde\x0f\x3b\x48\x2c\xd9\xae\xa9\x43\x4d",
57 const gnutls_datum_t invalid_hash_data = {
58 (void *)
59 "\xaa\xf4\xc6\x1d\xdc\xca\xe8\xa2\xda\xbe"
60 "\xde\x0f\x3b\x48\x2c\xb9\xae\xa9\x43\x4d",
64 const gnutls_datum_t raw_data = {
65 (void *) "hello",
69 static char pem1_cert[] =
70 "-----BEGIN CERTIFICATE-----\n"
71 "MIICHjCCAYmgAwIBAg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-----\n"
125 "MIIBugIBAAKBgQC5hPVagb4aDcWKc48Mmy+btg5Lw3Qaf2StnfMoxaBHvJtXVvGX\n"
126 "1X43A+nyTPTji38wo10vu6GiN8LqNY8fsV+mol8B8SM2K+RPLy3dndU6pjmvelF8\n"
127 "0iWOl3TPHsV7S3ZDgQcfBhS4blgS4ZDiN2/SG+xoxVji5jDgal4sY3jsBwIVAJ9W\n"
128 "jEhkL/6NqnptltsEXRbvCKVxAoGAYgZ+5Fx2CLdGGl3Xl9QqIfsfMcnS9Po52CfR\n"
129 "m/wnXacKpxr8U8EvQ8I3yIV/PUyrXYEy+x1eHlQRFiDGgFrZjJtD8N1roPTD8oqc\n"
130 "OdIcew/v+iiTj9KhIuvc4IqLrSgOz+8Jhek2vYt6UNV79yUNbGARxO9wkM/WG+u7\n"
131 "jsY+OpcCgYAPiodX8tHC3KzfS4sPi7op9+ED5FX6spgH1v0SsYC89bq0UNR/oA5D\n"
132 "55/JeBFf5eQMLGtqpDXcvVTlYDaaMdGKWW5rHLq9LrrrfIfv2sjdoeukg+aLrfr6\n"
133 "jlvXN8gyPpbCPvRD2n2RAg+3vPjvj/dBAF6W3w8IltzqsukGgq/SLwIUS5/r/2ya\n"
134 "AoNBXjeBjgCGMei2m8E=\n" "-----END DSA PRIVATE KEY-----\n";
136 const gnutls_datum_t cert_dat[] = {
137 {(void*)pem1_cert, sizeof (pem1_cert)}
139 {(void*)pem2_cert, sizeof (pem2_cert)}
142 const gnutls_datum_t key_dat[] = {
143 {(void*)pem1_key, sizeof (pem1_key)}
145 {(void*)pem2_key, sizeof (pem2_key)}
148 void
149 doit (void)
151 gnutls_x509_privkey_t key;
152 gnutls_x509_crt_t crt;
153 gnutls_pubkey_t pubkey;
154 gnutls_privkey_t privkey;
155 gnutls_digest_algorithm_t hash_algo;
156 gnutls_sign_algorithm_t sign_algo;
157 gnutls_datum_t signature;
158 gnutls_datum_t signature2;
159 int ret;
160 size_t i;
162 gnutls_global_init ();
164 gnutls_global_set_log_function (tls_log_func);
165 if (debug)
166 gnutls_global_set_log_level (6);
168 for (i = 0; i < sizeof (key_dat) / sizeof (key_dat[0]); i++)
170 if (debug)
171 success ("loop %d\n", (int) i);
173 ret = gnutls_x509_privkey_init (&key);
174 if (ret < 0)
175 fail ("gnutls_x509_privkey_init\n");
177 ret =
178 gnutls_x509_privkey_import (key, &key_dat[i], GNUTLS_X509_FMT_PEM);
179 if (ret < 0)
180 fail ("gnutls_x509_privkey_import\n");
182 ret = gnutls_pubkey_init (&pubkey);
183 if (ret < 0)
184 fail ("gnutls_privkey_init\n");
186 ret = gnutls_privkey_init (&privkey);
187 if (ret < 0)
188 fail ("gnutls_pubkey_init\n");
190 ret = gnutls_privkey_import_x509 (privkey, key, 0);
191 if (ret < 0)
192 fail ("gnutls_privkey_import_x509\n");
194 ret = gnutls_privkey_sign_hash (privkey, GNUTLS_DIG_SHA1, 0,
195 &hash_data, &signature2);
196 if (ret < 0)
197 fail ("gnutls_privkey_sign_hash\n");
199 ret = gnutls_privkey_sign_data (privkey, GNUTLS_DIG_SHA1, 0,
200 &raw_data, &signature);
201 if (ret < 0)
202 fail ("gnutls_x509_privkey_sign_hash\n");
204 ret = gnutls_x509_crt_init (&crt);
205 if (ret < 0)
206 fail ("gnutls_x509_crt_init\n");
208 ret = gnutls_x509_crt_import (crt, &cert_dat[i], GNUTLS_X509_FMT_PEM);
209 if (ret < 0)
210 fail ("gnutls_x509_crt_import\n");
212 ret =
213 gnutls_pubkey_import_x509 (pubkey, crt, 0);
214 if (ret < 0)
215 fail ("gnutls_x509_pubkey_import\n");
217 ret =
218 gnutls_pubkey_get_verify_algorithm (pubkey, &signature, &hash_algo);
219 if (ret < 0 || hash_algo != GNUTLS_DIG_SHA1)
220 fail ("gnutls_x509_crt_get_verify_algorithm\n");
222 ret = gnutls_pubkey_verify_hash (pubkey, 0, &hash_data, &signature);
223 if (ret < 0)
224 fail ("gnutls_x509_pubkey_verify_hash\n");
226 ret =
227 gnutls_pubkey_get_verify_algorithm (pubkey, &signature2, &hash_algo);
228 if (ret < 0 || hash_algo != GNUTLS_DIG_SHA1)
229 fail ("gnutls_x509_crt_get_verify_algorithm (hashed data)\n");
231 ret = gnutls_pubkey_verify_hash (pubkey, 0, &hash_data, &signature2);
232 if (ret < 0)
233 fail ("gnutls_x509_pubkey_verify_hash-1 (hashed data)\n");
235 /* should fail */
236 ret = gnutls_pubkey_verify_hash (pubkey, 0, &invalid_hash_data, &signature2);
237 if (ret != GNUTLS_E_PK_SIG_VERIFY_FAILED)
238 fail ("gnutls_x509_pubkey_verify_hash-2 (hashed data)\n");
240 sign_algo = gnutls_pk_to_sign(gnutls_pubkey_get_pk_algorithm(pubkey, NULL),
241 GNUTLS_DIG_SHA1);
243 ret = gnutls_pubkey_verify_hash2 (pubkey, sign_algo, 0, &hash_data, &signature2);
244 if (ret < 0)
245 fail ("gnutls_x509_pubkey_verify_hash2-1 (hashed data)\n");
247 /* should fail */
248 ret = gnutls_pubkey_verify_hash2 (pubkey, sign_algo, 0, &invalid_hash_data, &signature2);
249 if (ret != GNUTLS_E_PK_SIG_VERIFY_FAILED)
250 fail ("gnutls_x509_pubkey_verify_hash2-2 (hashed data)\n");
253 gnutls_free(signature.data);
254 gnutls_free(signature2.data);
255 gnutls_x509_privkey_deinit (key);
256 gnutls_x509_crt_deinit (crt);
257 gnutls_privkey_deinit (privkey);
258 gnutls_pubkey_deinit (pubkey);
261 gnutls_global_deinit ();