1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
/*
* Copyright (C) 2012 Free Software Foundation, Inc.
*
* Author: Nikos Mavrogiannopoulos
*
* This file is part of GnuTLS.
*
* GnuTLS is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* GnuTLS is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GnuTLS; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <gnutls/gnutls.h>
#include <unistd.h>
#include "utils.h"
#include "cert-common.h"
/* This will test whether the default public key storage backend
* is operating properly */
static void tls_log_func(int level, const char *str)
{
fprintf(stderr, "|<%d>| %s", level, str);
}
static unsigned char tofu_server_cert_pem[] =
"-----BEGIN CERTIFICATE-----\n"
"MIICVjCCAcGgAwIBAgIERiYdMTALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
"VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTIxWhcNMDgwNDE3MTMyOTIxWjA3MRsw\n"
"GQYDVQQKExJHbnVUTFMgdGVzdCBzZXJ2ZXIxGDAWBgNVBAMTD3Rlc3QuZ251dGxz\n"
"Lm9yZzCBnDALBgkqhkiG9w0BAQEDgYwAMIGIAoGA17pcr6MM8C6pJ1aqU46o63+B\n"
"dUxrmL5K6rce+EvDasTaDQC46kwTHzYWk95y78akXrJutsoKiFV1kJbtple8DDt2\n"
"DZcevensf9Op7PuFZKBroEjOd35znDET/z3IrqVgbtm2jFqab7a+n2q9p/CgMyf1\n"
"tx2S5Zacc1LWn9bIjrECAwEAAaOBkzCBkDAMBgNVHRMBAf8EAjAAMBoGA1UdEQQT\n"
"MBGCD3Rlc3QuZ251dGxzLm9yZzATBgNVHSUEDDAKBggrBgEFBQcDATAPBgNVHQ8B\n"
"Af8EBQMDB6AAMB0GA1UdDgQWBBTrx0Vu5fglyoyNgw106YbU3VW0dTAfBgNVHSME\n"
"GDAWgBTpPBz7rZJu5gakViyi4cBTJ8jylTALBgkqhkiG9w0BAQUDgYEAaFEPTt+7\n"
"bzvBuOf7+QmeQcn29kT6Bsyh1RHJXf8KTk5QRfwp6ogbp94JQWcNQ/S7YDFHglD1\n"
"AwUNBRXwd3riUsMnsxgeSDxYBfJYbDLeohNBsqaPDJb7XailWbMQKfAbFQ8cnOxg\n"
"rOKLUQRWJ0K3HyXRMhbqjdLIaQiCvQLuizo=\n" "-----END CERTIFICATE-----\n";
const gnutls_datum_t tofu_server_cert = { tofu_server_cert_pem,
sizeof(tofu_server_cert_pem)
};
static char client_pem[] =
"-----BEGIN CERTIFICATE-----\n"
"MIICHjCCAYmgAwIBAgIERiYdNzALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
"VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTI3WhcNMDgwNDE3MTMyOTI3WjAdMRsw\n"
"GQYDVQQDExJHbnVUTFMgdGVzdCBjbGllbnQwgZwwCwYJKoZIhvcNAQEBA4GMADCB\n"
"iAKBgLtmQ/Xyxde2jMzF3/WIO7HJS2oOoa0gUEAIgKFPXKPQ+GzP5jz37AR2ExeL\n"
"ZIkiW8DdU3w77XwEu4C5KL6Om8aOoKUSy/VXHqLnu7czSZ/ju0quak1o/8kR4jKN\n"
"zj2AC41179gAgY8oBAOgIo1hBAf6tjd9IQdJ0glhaZiQo1ipAgMBAAGjdjB0MAwG\n"
"A1UdEwEB/wQCMAAwEwYDVR0lBAwwCgYIKwYBBQUHAwIwDwYDVR0PAQH/BAUDAweg\n"
"ADAdBgNVHQ4EFgQUTLkKm/odNON+3svSBxX+odrLaJEwHwYDVR0jBBgwFoAU6Twc\n"
"+62SbuYGpFYsouHAUyfI8pUwCwYJKoZIhvcNAQEFA4GBALujmBJVZnvaTXr9cFRJ\n"
"jpfc/3X7sLUsMvumcDE01ls/cG5mIatmiyEU9qI3jbgUf82z23ON/acwJf875D3/\n"
"U7jyOsBJ44SEQITbin2yUeJMIm1tievvdNXBDfW95AM507ShzP12sfiJkJfjjdhy\n"
"dc8Siq5JojruiMizAf0pA7in\n" "-----END CERTIFICATE-----\n";
const gnutls_datum_t client_cert =
{ (void *) client_pem, sizeof(client_pem) };
#define TMP_FILE "mini-tdb.tmp"
#define HOSTS_DIR ".gnutls/"
#define HOSTS_FILE HOSTS_DIR"known_hosts"
#define SHA1_HASH "\x53\x4b\x3b\xdc\x5e\xc8\x44\x4c\x02\x20\xbf\x39\x48\x6f\x4c\xfe\xcd\x25\x52\x10"
void doit(void)
{
gnutls_datum_t der_cert, der_cert2;
gnutls_datum_t der_rawpk, der_rawpk2;
int ret;
gnutls_datum_t hash;
char path[512];
/* the sha1 hash of the server's pubkey */
hash.data = (void *) SHA1_HASH;
hash.size = sizeof(SHA1_HASH) - 1;
/* General init. */
global_init();
gnutls_global_set_log_function(tls_log_func);
if (debug)
gnutls_global_set_log_level(2);
// X.509 certificates
ret =
gnutls_pem_base64_decode_alloc("CERTIFICATE", &tofu_server_cert,
&der_cert);
if (ret < 0) {
fail("base64 decoding\n");
goto fail;
}
ret =
gnutls_pem_base64_decode_alloc("CERTIFICATE", &client_cert,
&der_cert2);
if (ret < 0) {
fail("base64 decoding\n");
goto fail;
}
// Raw public keys
ret =
gnutls_pem_base64_decode_alloc("PUBLIC KEY", &rawpk_public_key1,
&der_rawpk);
if (ret < 0) {
fail("base64 decoding\n");
goto fail;
}
ret =
gnutls_pem_base64_decode_alloc("PUBLIC KEY", &rawpk_public_key2,
&der_rawpk2);
if (ret < 0) {
fail("base64 decoding\n");
goto fail;
}
remove(HOSTS_FILE);
remove(TMP_FILE);
/* verify whether the stored hash verification succeeds */
ret = gnutls_store_commitment(TMP_FILE, NULL, "localhost", "https",
GNUTLS_DIG_SHA1, &hash, 0, GNUTLS_SCOMMIT_FLAG_ALLOW_BROKEN);
if (ret != 0) {
fail("commitment storage: %s\n", gnutls_strerror(ret));
goto fail;
}
if (debug)
success("Commitment storage: passed\n");
ret =
gnutls_verify_stored_pubkey(TMP_FILE, NULL, "localhost",
"https", GNUTLS_CRT_X509,
&der_cert, 0);
remove(TMP_FILE);
if (ret != 0) {
fail("commitment verification: %s\n",
gnutls_strerror(ret));
goto fail;
}
if (debug)
success("Commitment verification: passed\n");
/* Verify access to home dir */
#ifndef _WIN32
setenv("HOME", getcwd(path, sizeof(path)), 1);
/* verify whether the stored hash verification succeeeds */
ret = gnutls_store_commitment(NULL, NULL, "localhost", "https",
GNUTLS_DIG_SHA1, &hash, 0, GNUTLS_SCOMMIT_FLAG_ALLOW_BROKEN);
if (ret != 0) {
fail("commitment storage: %s\n", gnutls_strerror(ret));
goto fail;
}
if (debug)
success("Commitment storage: passed\n");
ret =
gnutls_verify_stored_pubkey(NULL, NULL, "localhost",
"https", GNUTLS_CRT_X509,
&der_cert, 0);
if (ret != 0) {
fail("commitment verification: %s\n",
gnutls_strerror(ret));
goto fail;
}
if (debug)
success("Commitment from homedir verification: passed\n");
#endif
/* verify whether the stored pubkey verification succeeds */
// First we test regular X.509 certs
ret = gnutls_store_pubkey(TMP_FILE, NULL, "localhost", "https",
GNUTLS_CRT_X509, &der_cert, 0, 0);
if (ret != 0) {
fail("storage: %s\n", gnutls_strerror(ret));
goto fail;
}
if (debug)
success("Public key storage (from cert): passed\n");
ret =
gnutls_verify_stored_pubkey(TMP_FILE, NULL, "localhost",
"https", GNUTLS_CRT_X509,
&der_cert, 0);
if (ret != 0) {
fail("pubkey verification (from cert): %s\n", gnutls_strerror(ret));
goto fail;
}
ret =
gnutls_verify_stored_pubkey(TMP_FILE, NULL, "localhost",
"https", GNUTLS_CRT_X509,
&der_cert2, 0);
if (ret == 0) {
fail("verification succeeded when shouldn't!\n");
goto fail;
}
if (ret != GNUTLS_E_CERTIFICATE_KEY_MISMATCH) {
fail("Wrong error code returned: %s!\n",
gnutls_strerror(ret));
goto fail;
}
if (debug)
success("Public key verification (from cert): passed\n");
// Secondly we test raw public keys
ret = gnutls_store_pubkey(TMP_FILE, NULL, "localhost", "https",
GNUTLS_CRT_RAWPK, &der_rawpk, 0, 0);
if (ret != 0) {
fail("storage: %s\n", gnutls_strerror(ret));
goto fail;
}
if (debug)
success("Public key storage (from raw pk): passed\n");
ret =
gnutls_verify_stored_pubkey(TMP_FILE, NULL, "localhost",
"https", GNUTLS_CRT_RAWPK,
&der_rawpk, 0);
if (ret != 0) {
fail("pubkey verification (from raw pk): %s\n", gnutls_strerror(ret));
goto fail;
}
ret =
gnutls_verify_stored_pubkey(TMP_FILE, NULL, "localhost",
"https", GNUTLS_CRT_RAWPK,
&der_rawpk2, 0);
if (ret == 0) {
fail("verification succeeded when shouldn't!\n");
goto fail;
}
if (ret != GNUTLS_E_CERTIFICATE_KEY_MISMATCH) {
fail("Wrong error code returned: %s!\n",
gnutls_strerror(ret));
goto fail;
}
if (debug)
success("Public key verification (from raw pk): passed\n");
remove(HOSTS_FILE);
remove(TMP_FILE);
rmdir(HOSTS_DIR);
gnutls_global_deinit();
gnutls_free(der_cert.data);
gnutls_free(der_cert2.data);
gnutls_free(der_rawpk.data);
gnutls_free(der_rawpk2.data);
return;
fail:
remove(HOSTS_FILE);
remove(TMP_FILE);
rmdir(HOSTS_DIR);
exit(1);
}
|