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. */
32 #include <sys/types.h>
33 #include <netinet/in.h>
34 #include <sys/socket.h>
36 # include <arpa/inet.h>
39 #include <gnutls/gnutls.h>
40 #include <gnutls/x509.h>
41 #include <gnutls/abstract.h>
45 /* sha1 hash of "hello" string */
46 const gnutls_datum_t hash_data = {
48 "\xaa\xf4\xc6\x1d\xdc\xc5\xe8\xa2\xda\xbe"
49 "\xde\x0f\x3b\x48\x2c\xd9\xae\xa9\x43\x4d",
53 const gnutls_datum_t raw_data = {
54 (void *) "hello there",
58 static char pem1_cert[] =
59 "-----BEGIN CERTIFICATE-----\n"
60 "MIICHjCCAYmgAwIBAgIERiYdNzALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
61 "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTI3WhcNMDgwNDE3MTMyOTI3WjAdMRsw\n"
62 "GQYDVQQDExJHbnVUTFMgdGVzdCBjbGllbnQwgZwwCwYJKoZIhvcNAQEBA4GMADCB\n"
63 "iAKBgLtmQ/Xyxde2jMzF3/WIO7HJS2oOoa0gUEAIgKFPXKPQ+GzP5jz37AR2ExeL\n"
64 "ZIkiW8DdU3w77XwEu4C5KL6Om8aOoKUSy/VXHqLnu7czSZ/ju0quak1o/8kR4jKN\n"
65 "zj2AC41179gAgY8oBAOgIo1hBAf6tjd9IQdJ0glhaZiQo1ipAgMBAAGjdjB0MAwG\n"
66 "A1UdEwEB/wQCMAAwEwYDVR0lBAwwCgYIKwYBBQUHAwIwDwYDVR0PAQH/BAUDAweg\n"
67 "ADAdBgNVHQ4EFgQUTLkKm/odNON+3svSBxX+odrLaJEwHwYDVR0jBBgwFoAU6Twc\n"
68 "+62SbuYGpFYsouHAUyfI8pUwCwYJKoZIhvcNAQEFA4GBALujmBJVZnvaTXr9cFRJ\n"
69 "jpfc/3X7sLUsMvumcDE01ls/cG5mIatmiyEU9qI3jbgUf82z23ON/acwJf875D3/\n"
70 "U7jyOsBJ44SEQITbin2yUeJMIm1tievvdNXBDfW95AM507ShzP12sfiJkJfjjdhy\n"
71 "dc8Siq5JojruiMizAf0pA7in\n" "-----END CERTIFICATE-----\n";
73 static char pem1_key[] =
74 "-----BEGIN RSA PRIVATE KEY-----\n"
75 "MIICXAIBAAKBgQC7ZkP18sXXtozMxd/1iDuxyUtqDqGtIFBACIChT1yj0Phsz+Y8\n"
76 "9+wEdhMXi2SJIlvA3VN8O+18BLuAuSi+jpvGjqClEsv1Vx6i57u3M0mf47tKrmpN\n"
77 "aP/JEeIyjc49gAuNde/YAIGPKAQDoCKNYQQH+rY3fSEHSdIJYWmYkKNYqQIDAQAB\n"
78 "AoGADpmARG5CQxS+AesNkGmpauepiCz1JBF/JwnyiX6vEzUh0Ypd39SZztwrDxvF\n"
79 "PJjQaKVljml1zkJpIDVsqvHdyVdse8M+Qn6hw4x2p5rogdvhhIL1mdWo7jWeVJTF\n"
80 "RKB7zLdMPs3ySdtcIQaF9nUAQ2KJEvldkO3m/bRJFEp54k0CQQDYy+RlTmwRD6hy\n"
81 "7UtMjR0H3CSZJeQ8svMCxHLmOluG9H1UKk55ZBYfRTsXniqUkJBZ5wuV1L+pR9EK\n"
82 "ca89a+1VAkEA3UmBelwEv2u9cAU1QjKjmwju1JgXbrjEohK+3B5y0ESEXPAwNQT9\n"
83 "TrDM1m9AyxYTWLxX93dI5QwNFJtmbtjeBQJARSCWXhsoaDRG8QZrCSjBxfzTCqZD\n"
84 "ZXtl807ymCipgJm60LiAt0JLr4LiucAsMZz6+j+quQbSakbFCACB8SLV1QJBAKZQ\n"
85 "YKf+EPNtnmta/rRKKvySsi3GQZZN+Dt3q0r094XgeTsAqrqujVNfPhTMeP4qEVBX\n"
86 "/iVX2cmMTSh3w3z8MaECQEp0XJWDVKOwcTW6Ajp9SowtmiZ3YDYo1LF9igb4iaLv\n"
87 "sWZGfbnU3ryjvkb6YuFjgtzbZDZHWQCo8/cOtOBmPdk=\n"
88 "-----END RSA PRIVATE KEY-----\n";
90 const gnutls_datum_t cert_dat[] = {
91 {(void *) pem1_cert, sizeof(pem1_cert)}
94 const gnutls_datum_t key_dat[] = {
95 {(void *) pem1_key, sizeof(pem1_key)}
100 gnutls_x509_privkey_t key;
101 gnutls_x509_crt_t crt;
102 gnutls_pubkey_t pubkey;
103 gnutls_privkey_t privkey;
104 gnutls_datum_t out, out2;
110 for (i = 0; i < sizeof(key_dat) / sizeof(key_dat[0]); i++) {
112 success("loop %d\n", (int) i);
114 ret = gnutls_x509_privkey_init(&key);
116 fail("gnutls_x509_privkey_init\n");
119 gnutls_x509_privkey_import(key, &key_dat[i],
120 GNUTLS_X509_FMT_PEM);
122 fail("gnutls_x509_privkey_import\n");
124 ret = gnutls_pubkey_init(&pubkey);
126 fail("gnutls_privkey_init\n");
128 ret = gnutls_privkey_init(&privkey);
130 fail("gnutls_pubkey_init\n");
132 ret = gnutls_privkey_import_x509(privkey, key, 0);
134 fail("gnutls_privkey_import_x509\n");
136 ret = gnutls_x509_crt_init(&crt);
138 fail("gnutls_x509_crt_init\n");
141 gnutls_x509_crt_import(crt, &cert_dat[i],
142 GNUTLS_X509_FMT_PEM);
144 fail("gnutls_x509_crt_import\n");
146 ret = gnutls_pubkey_import_x509(pubkey, crt, 0);
148 fail("gnutls_x509_pubkey_import\n");
152 gnutls_pubkey_encrypt_data(pubkey, 0, &hash_data,
155 fail("gnutls_pubkey_encrypt_data\n");
158 ret = gnutls_privkey_decrypt_data(privkey, 0, &out, &out2);
160 fail("gnutls_privkey_decrypt_data\n");
162 if (out2.size != hash_data.size)
163 fail("Decrypted data don't match original (1)\n");
165 if (memcmp(out2.data, hash_data.data, hash_data.size) != 0)
166 fail("Decrypted data don't match original (2)\n");
168 gnutls_free(out.data);
169 gnutls_free(out2.data);
172 gnutls_pubkey_encrypt_data(pubkey, 0, &raw_data, &out);
174 fail("gnutls_pubkey_encrypt_data\n");
176 ret = gnutls_privkey_decrypt_data(privkey, 0, &out, &out2);
178 fail("gnutls_privkey_decrypt_data\n");
180 if (out2.size != raw_data.size)
181 fail("Decrypted data don't match original (3)\n");
183 if (memcmp(out2.data, raw_data.data, raw_data.size) != 0)
184 fail("Decrypted data don't match original (4)\n");
189 gnutls_free(out.data);
190 gnutls_free(out2.data);
191 gnutls_x509_privkey_deinit(key);
192 gnutls_x509_crt_deinit(crt);
193 gnutls_privkey_deinit(privkey);
194 gnutls_pubkey_deinit(pubkey);
197 gnutls_global_deinit();