2 * Copyright (C) 2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
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
31 #include <gnutls/gnutls.h>
34 /* This will test whether the default public key storage backend
35 * is operating properly */
38 tls_log_func (int level
, const char *str
)
40 fprintf (stderr
, "|<%d>| %s", level
, str
);
43 static unsigned char server_cert_pem
[] =
44 "-----BEGIN CERTIFICATE-----\n"
45 "MIICVjCCAcGgAwIBAgIERiYdMTALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
46 "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTIxWhcNMDgwNDE3MTMyOTIxWjA3MRsw\n"
47 "GQYDVQQKExJHbnVUTFMgdGVzdCBzZXJ2ZXIxGDAWBgNVBAMTD3Rlc3QuZ251dGxz\n"
48 "Lm9yZzCBnDALBgkqhkiG9w0BAQEDgYwAMIGIAoGA17pcr6MM8C6pJ1aqU46o63+B\n"
49 "dUxrmL5K6rce+EvDasTaDQC46kwTHzYWk95y78akXrJutsoKiFV1kJbtple8DDt2\n"
50 "DZcevensf9Op7PuFZKBroEjOd35znDET/z3IrqVgbtm2jFqab7a+n2q9p/CgMyf1\n"
51 "tx2S5Zacc1LWn9bIjrECAwEAAaOBkzCBkDAMBgNVHRMBAf8EAjAAMBoGA1UdEQQT\n"
52 "MBGCD3Rlc3QuZ251dGxzLm9yZzATBgNVHSUEDDAKBggrBgEFBQcDATAPBgNVHQ8B\n"
53 "Af8EBQMDB6AAMB0GA1UdDgQWBBTrx0Vu5fglyoyNgw106YbU3VW0dTAfBgNVHSME\n"
54 "GDAWgBTpPBz7rZJu5gakViyi4cBTJ8jylTALBgkqhkiG9w0BAQUDgYEAaFEPTt+7\n"
55 "bzvBuOf7+QmeQcn29kT6Bsyh1RHJXf8KTk5QRfwp6ogbp94JQWcNQ/S7YDFHglD1\n"
56 "AwUNBRXwd3riUsMnsxgeSDxYBfJYbDLeohNBsqaPDJb7XailWbMQKfAbFQ8cnOxg\n"
57 "rOKLUQRWJ0K3HyXRMhbqjdLIaQiCvQLuizo=\n" "-----END CERTIFICATE-----\n";
59 const gnutls_datum_t server_cert
= { server_cert_pem
,
60 sizeof (server_cert_pem
)
63 static char client_pem
[] =
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";
77 const gnutls_datum_t client_cert
= { (void*)client_pem
, sizeof (client_pem
) };
79 #define TMP_FILE "mini-tdb-tmp"
81 #define SHA1_HASH "\x53\x4b\x3b\xdc\x5e\xc8\x44\x4c\x02\x20\xbf\x39\x48\x6f\x4c\xfe\xcd\x25\x52\x10"
85 gnutls_datum_t der_cert
, der_cert2
;
89 /* the sha1 hash of the server's pubkey */
90 hash
.data
= (void*)SHA1_HASH
;
91 hash
.size
= sizeof(SHA1_HASH
)-1;
94 gnutls_global_init ();
95 gnutls_global_set_log_function (tls_log_func
);
97 gnutls_global_set_log_level (2);
99 ret
= gnutls_pem_base64_decode_alloc("CERTIFICATE", &server_cert
, &der_cert
);
102 fail("base64 decoding\n");
106 ret
= gnutls_pem_base64_decode_alloc("CERTIFICATE", &client_cert
, &der_cert2
);
109 fail("base64 decoding\n");
115 /* verify whether the stored hash verification succeeeds */
116 ret
= gnutls_store_commitment(TMP_FILE
, NULL
, "localhost", "https",
117 GNUTLS_DIG_SHA1
, &hash
, 0, 0);
120 fail("commitment storage: %s\n", gnutls_strerror(ret
));
125 success("Commitment storage: passed\n");
127 ret
= gnutls_verify_stored_pubkey(TMP_FILE
, NULL
, "localhost", "https",
128 GNUTLS_CRT_X509
, &der_cert
, 0);
133 fail("commitment verification: %s\n", gnutls_strerror(ret
));
138 success("Commitment verification: passed\n");
140 /* verify whether the stored pubkey verification succeeeds */
141 ret
= gnutls_store_pubkey(TMP_FILE
, NULL
, "localhost", "https",
142 GNUTLS_CRT_X509
, &der_cert
, 0, 0);
145 fail("storage: %s\n", gnutls_strerror(ret
));
150 success("Public key storage: passed\n");
152 ret
= gnutls_verify_stored_pubkey(TMP_FILE
, NULL
, "localhost", "https",
153 GNUTLS_CRT_X509
, &der_cert
, 0);
156 fail("pubkey verification: %s\n", gnutls_strerror(ret
));
160 ret
= gnutls_verify_stored_pubkey(TMP_FILE
, NULL
, "localhost", "https",
161 GNUTLS_CRT_X509
, &der_cert2
, 0);
165 fail("verification succeed when shouldn't!\n");
168 if (ret
!= GNUTLS_E_CERTIFICATE_KEY_MISMATCH
)
170 fail("Wrong error code returned: %s!\n", gnutls_strerror(ret
));
175 success("Public key verification: passed\n");
178 gnutls_global_deinit();
179 gnutls_free(der_cert
.data
);
180 gnutls_free(der_cert2
.data
);