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>
35 #include <arpa/inet.h>
37 #include <gnutls/gnutls.h>
38 #include <gnutls/x509.h>
39 #include <gnutls/abstract.h>
43 /* sha1 hash of "hello" string */
44 const gnutls_datum_t hash_data
= {
46 "\xaa\xf4\xc6\x1d\xdc\xc5\xe8\xa2\xda\xbe"
47 "\xde\x0f\x3b\x48\x2c\xd9\xae\xa9\x43\x4d",
51 const gnutls_datum_t invalid_hash_data
= {
53 "\xaa\xf4\xc6\x1d\xdc\xca\xe8\xa2\xda\xbe"
54 "\xde\x0f\x3b\x48\x2c\xb9\xae\xa9\x43\x4d",
58 const gnutls_datum_t raw_data
= {
63 static char pem1_cert
[] =
64 "-----BEGIN CERTIFICATE-----\n"
65 "MIICHjCCAYmgAwIBAgIERiYdNzALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
66 "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTI3WhcNMDgwNDE3MTMyOTI3WjAdMRsw\n"
67 "GQYDVQQDExJHbnVUTFMgdGVzdCBjbGllbnQwgZwwCwYJKoZIhvcNAQEBA4GMADCB\n"
68 "iAKBgLtmQ/Xyxde2jMzF3/WIO7HJS2oOoa0gUEAIgKFPXKPQ+GzP5jz37AR2ExeL\n"
69 "ZIkiW8DdU3w77XwEu4C5KL6Om8aOoKUSy/VXHqLnu7czSZ/ju0quak1o/8kR4jKN\n"
70 "zj2AC41179gAgY8oBAOgIo1hBAf6tjd9IQdJ0glhaZiQo1ipAgMBAAGjdjB0MAwG\n"
71 "A1UdEwEB/wQCMAAwEwYDVR0lBAwwCgYIKwYBBQUHAwIwDwYDVR0PAQH/BAUDAweg\n"
72 "ADAdBgNVHQ4EFgQUTLkKm/odNON+3svSBxX+odrLaJEwHwYDVR0jBBgwFoAU6Twc\n"
73 "+62SbuYGpFYsouHAUyfI8pUwCwYJKoZIhvcNAQEFA4GBALujmBJVZnvaTXr9cFRJ\n"
74 "jpfc/3X7sLUsMvumcDE01ls/cG5mIatmiyEU9qI3jbgUf82z23ON/acwJf875D3/\n"
75 "U7jyOsBJ44SEQITbin2yUeJMIm1tievvdNXBDfW95AM507ShzP12sfiJkJfjjdhy\n"
76 "dc8Siq5JojruiMizAf0pA7in\n" "-----END CERTIFICATE-----\n";
78 static char pem1_key
[] =
79 "-----BEGIN RSA PRIVATE KEY-----\n"
80 "MIICXAIBAAKBgQC7ZkP18sXXtozMxd/1iDuxyUtqDqGtIFBACIChT1yj0Phsz+Y8\n"
81 "9+wEdhMXi2SJIlvA3VN8O+18BLuAuSi+jpvGjqClEsv1Vx6i57u3M0mf47tKrmpN\n"
82 "aP/JEeIyjc49gAuNde/YAIGPKAQDoCKNYQQH+rY3fSEHSdIJYWmYkKNYqQIDAQAB\n"
83 "AoGADpmARG5CQxS+AesNkGmpauepiCz1JBF/JwnyiX6vEzUh0Ypd39SZztwrDxvF\n"
84 "PJjQaKVljml1zkJpIDVsqvHdyVdse8M+Qn6hw4x2p5rogdvhhIL1mdWo7jWeVJTF\n"
85 "RKB7zLdMPs3ySdtcIQaF9nUAQ2KJEvldkO3m/bRJFEp54k0CQQDYy+RlTmwRD6hy\n"
86 "7UtMjR0H3CSZJeQ8svMCxHLmO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L0PCN8iFfz1Mq12BMvsdXh5UERYg\n"
105 "xoBa2YybQ/Dda6D0w/KKnDnSHHsP7/ook4/SoSLr3OCKi60oDs/vCYXpNr2LelDV\n"
106 "e/clDWxgEcTvcJDP1hvru47GPjqXA4GEAAKBgA+Kh1fy0cLcrN9Liw+Luin34QPk\n"
107 "VfqymAfW/RKxgLz1urRQ1H+gDkPnn8l4EV/l5Awsa2qkNdy9VOVgNpox0YpZbmsc\n"
108 "ur0uuut8h+/ayN2h66SD5out+vqOW9c3yDI+lsI+9EPafZECD7e8+O+P90EAXpbf\n"
109 "DwiW3Oqy6QaCr9Ivo4GTMIGQMAwGA1UdEwEB/wQCMAAwGgYDVR0RBBMwEYIPdGVz\n"
110 "dC5nbnV0bHMub3JnMBMGA1UdJQQMMAoGCCsGAQUFBwMBMA8GA1UdDwEB/wQFAwMH\n"
111 "gAAwHQYDVR0OBBYEFL/su87Y6HtwVuzz0SuS1tSZClvzMB8GA1UdIwQYMBaAFOk8\n"
112 "HPutkm7mBqRWLKLhwFMnyPKVMAsGCSqGSIb3DQEBBQOBgQBCsrnfD1xzh8/Eih1f\n"
113 "x+M0lPoX1Re5L2ElHI6DJpHYOBPwf9glwxnet2+avzgUQDUFwUSxOhodpyeaACXD\n"
114 "o0gGVpcH8sOBTQ+aTdM37hGkPxoXjtIkR/LgG5nP2H2JRd5TkW8l13JdM4MJFB4W\n"
115 "QcDzQ8REwidsfh9uKAluk1c/KQ==\n" "-----END CERTIFICATE-----\n";
117 static char pem2_key
[] =
118 "-----BEGIN DSA PRIVATE KEY-----\n"
119 "MIIBugIBAAKBgQC5hPVagb4aDcWKc48Mmy+btg5Lw3Qaf2StnfMoxaBHvJtXVvGX\n"
120 "1X43A+nyTPTji38wo10vu6GiN8LqNY8fsV+mol8B8SM2K+RPLy3dndU6pjmvelF8\n"
121 "0iWOl3TPHsV7S3ZDgQcfBhS4blgS4ZDiN2/SG+xoxVji5jDgal4sY3jsBwIVAJ9W\n"
122 "jEhkL/6NqnptltsEXRbvCKVxAoGAYgZ+5Fx2CLdGGl3Xl9QqIfsfMcnS9Po52CfR\n"
123 "m/wnXacKpxr8U8EvQ8I3yIV/PUyrXYEy+x1eHlQRFiDGgFrZjJtD8N1roPTD8oqc\n"
124 "OdIcew/v+iiTj9KhIuvc4IqLrSgOz+8Jhek2vYt6UNV79yUNbGARxO9wkM/WG+u7\n"
125 "jsY+OpcCgYAPiodX8tHC3KzfS4sPi7op9+ED5FX6spgH1v0SsYC89bq0UNR/oA5D\n"
126 "55/JeBFf5eQMLGtqpDXcvVTlYDaaMdGKWW5rHLq9LrrrfIfv2sjdoeukg+aLrfr6\n"
127 "jlvXN8gyPpbCPvRD2n2RAg+3vPjvj/dBAF6W3w8IltzqsukGgq/SLwIUS5/r/2ya\n"
128 "AoNBXjeBjgCGMei2m8E=\n" "-----END DSA PRIVATE KEY-----\n";
130 const gnutls_datum_t cert_dat
[] = {
131 {(void*)pem1_cert
, sizeof (pem1_cert
)}
133 {(void*)pem2_cert
, sizeof (pem2_cert
)}
136 const gnutls_datum_t key_dat
[] = {
137 {(void*)pem1_key
, sizeof (pem1_key
)}
139 {(void*)pem2_key
, sizeof (pem2_key
)}
145 gnutls_x509_privkey_t key
;
146 gnutls_x509_crt_t crt
;
147 gnutls_pubkey_t pubkey
;
148 gnutls_privkey_t privkey
;
149 gnutls_digest_algorithm_t hash_algo
;
150 gnutls_datum_t signature
;
151 gnutls_datum_t signature2
;
155 gnutls_global_init ();
157 for (i
= 0; i
< sizeof (key_dat
) / sizeof (key_dat
[0]); i
++)
160 success ("loop %d\n", (int) i
);
162 ret
= gnutls_x509_privkey_init (&key
);
164 fail ("gnutls_x509_privkey_init\n");
167 gnutls_x509_privkey_import (key
, &key_dat
[i
], GNUTLS_X509_FMT_PEM
);
169 fail ("gnutls_x509_privkey_import\n");
171 ret
= gnutls_pubkey_init (&pubkey
);
173 fail ("gnutls_privkey_init\n");
175 ret
= gnutls_privkey_init (&privkey
);
177 fail ("gnutls_pubkey_init\n");
179 ret
= gnutls_privkey_import_x509 (privkey
, key
, 0);
181 fail ("gnutls_privkey_import_x509\n");
183 ret
= gnutls_privkey_sign_hash (privkey
, GNUTLS_DIG_SHA1
, 0,
184 &hash_data
, &signature2
);
186 fail ("gnutls_privkey_sign_hash\n");
188 ret
= gnutls_privkey_sign_data (privkey
, GNUTLS_DIG_SHA1
, 0,
189 &raw_data
, &signature
);
191 fail ("gnutls_x509_privkey_sign_hash\n");
193 ret
= gnutls_x509_crt_init (&crt
);
195 fail ("gnutls_x509_crt_init\n");
197 ret
= gnutls_x509_crt_import (crt
, &cert_dat
[i
], GNUTLS_X509_FMT_PEM
);
199 fail ("gnutls_x509_crt_import\n");
202 gnutls_pubkey_import_x509 (pubkey
, crt
, 0);
204 fail ("gnutls_x509_pubkey_import\n");
207 gnutls_pubkey_get_verify_algorithm (pubkey
, &signature
, &hash_algo
);
208 if (ret
< 0 || hash_algo
!= GNUTLS_DIG_SHA1
)
209 fail ("gnutls_x509_crt_get_verify_algorithm\n");
211 ret
= gnutls_pubkey_verify_hash (pubkey
, 0, &hash_data
, &signature
);
213 fail ("gnutls_x509_privkey_verify_hash\n");
216 gnutls_pubkey_get_verify_algorithm (pubkey
, &signature2
, &hash_algo
);
217 if (ret
< 0 || hash_algo
!= GNUTLS_DIG_SHA1
)
218 fail ("gnutls_x509_crt_get_verify_algorithm (hashed data)\n");
220 ret
= gnutls_pubkey_verify_hash (pubkey
, 0, &hash_data
, &signature2
);
222 fail ("gnutls_x509_privkey_verify_hash (hashed data)\n");
225 ret
= gnutls_pubkey_verify_hash (pubkey
, 0, &invalid_hash_data
, &signature2
);
226 if (ret
!= GNUTLS_E_PK_SIG_VERIFY_FAILED
)
227 fail ("gnutls_x509_privkey_verify_hash (hashed data)\n");
230 gnutls_free(signature
.data
);
231 gnutls_free(signature2
.data
);
232 gnutls_x509_privkey_deinit (key
);
233 gnutls_x509_crt_deinit (crt
);
234 gnutls_privkey_deinit (privkey
);
235 gnutls_pubkey_deinit (pubkey
);
238 gnutls_global_deinit ();