blob: 3c84f1561afac93f517774a4ebf3f41f7dfc3bba [file] [log] [blame]
Damien Millerdd8b1dd2014-07-02 17:38:31 +10001/* $OpenBSD: test_file.c,v 1.1 2014/06/24 01:14:18 djm Exp $ */
2/*
3 * Regress test for sshkey.h key management API
4 *
5 * Placed in the public domain
6 */
7
8#include "includes.h"
9
10#include <sys/types.h>
11#include <sys/param.h>
12#include <sys/stat.h>
13#include <fcntl.h>
14#include <stdio.h>
15#include <stdint.h>
16#include <stdlib.h>
17#include <string.h>
18#include <unistd.h>
19
20#include <openssl/bn.h>
21#include <openssl/ec.h>
22#include <openssl/rsa.h>
23#include <openssl/dsa.h>
24#include <openssl/objects.h>
25
26#include "../test_helper/test_helper.h"
27
28#include "ssherr.h"
29#include "authfile.h"
30#include "sshkey.h"
31#include "sshbuf.h"
32
33#include "common.h"
34
35void sshkey_file_tests(void);
36
37void
38sshkey_file_tests(void)
39{
40 struct sshkey *k1, *k2;
41 struct sshbuf *buf, *pw;
42 BIGNUM *a, *b, *c;
43 char *cp;
44
45 TEST_START("load passphrase");
46 pw = load_text_file("pw");
47 TEST_DONE();
48
49 TEST_START("parse RSA1 from private");
50 buf = load_file("rsa1_1");
51 ASSERT_INT_EQ(sshkey_parse_private_fileblob(buf, "", "rsa1_1",
52 &k1, NULL), 0);
53 sshbuf_free(buf);
54 ASSERT_PTR_NE(k1, NULL);
55 a = load_bignum("rsa1_1.param.n");
56 ASSERT_BIGNUM_EQ(k1->rsa->n, a);
57 BN_free(a);
58 TEST_DONE();
59
60 TEST_START("parse RSA1 from private w/ passphrase");
61 buf = load_file("rsa1_1_pw");
62 ASSERT_INT_EQ(sshkey_parse_private_fileblob(buf,
63 (const char *)sshbuf_ptr(pw), "rsa1_1_pw", &k2, NULL), 0);
64 sshbuf_free(buf);
65 ASSERT_PTR_NE(k2, NULL);
66 ASSERT_INT_EQ(sshkey_equal(k1, k2), 1);
67 sshkey_free(k2);
68 TEST_DONE();
69
70 TEST_START("load RSA1 from public");
71 ASSERT_INT_EQ(sshkey_load_public(test_data_file("rsa1_1.pub"), &k2,
72 NULL), 0);
73 ASSERT_PTR_NE(k2, NULL);
74 ASSERT_INT_EQ(sshkey_equal(k1, k2), 1);
75 sshkey_free(k2);
76 TEST_DONE();
77
78 TEST_START("RSA1 key hex fingerprint");
79 buf = load_text_file("rsa1_1.fp");
80 cp = sshkey_fingerprint(k1, SSH_FP_MD5, SSH_FP_HEX);
81 ASSERT_PTR_NE(cp, NULL);
82 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
83 sshbuf_free(buf);
84 free(cp);
85 TEST_DONE();
86
87 TEST_START("RSA1 key bubblebabble fingerprint");
88 buf = load_text_file("rsa1_1.fp.bb");
89 cp = sshkey_fingerprint(k1, SSH_FP_SHA1, SSH_FP_BUBBLEBABBLE);
90 ASSERT_PTR_NE(cp, NULL);
91 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
92 sshbuf_free(buf);
93 free(cp);
94 TEST_DONE();
95
96 sshkey_free(k1);
97
98 TEST_START("parse RSA from private");
99 buf = load_file("rsa_1");
100 ASSERT_INT_EQ(sshkey_parse_private_fileblob(buf, "", "rsa_1",
101 &k1, NULL), 0);
102 sshbuf_free(buf);
103 ASSERT_PTR_NE(k1, NULL);
104 a = load_bignum("rsa_1.param.n");
105 b = load_bignum("rsa_1.param.p");
106 c = load_bignum("rsa_1.param.q");
107 ASSERT_BIGNUM_EQ(k1->rsa->n, a);
108 ASSERT_BIGNUM_EQ(k1->rsa->p, b);
109 ASSERT_BIGNUM_EQ(k1->rsa->q, c);
110 BN_free(a);
111 BN_free(b);
112 BN_free(c);
113 TEST_DONE();
114
115 TEST_START("parse RSA from private w/ passphrase");
116 buf = load_file("rsa_1_pw");
117 ASSERT_INT_EQ(sshkey_parse_private_fileblob(buf,
118 (const char *)sshbuf_ptr(pw), "rsa_1_pw", &k2, NULL), 0);
119 sshbuf_free(buf);
120 ASSERT_PTR_NE(k2, NULL);
121 ASSERT_INT_EQ(sshkey_equal(k1, k2), 1);
122 sshkey_free(k2);
123 TEST_DONE();
124
125 TEST_START("parse RSA from new-format");
126 buf = load_file("rsa_n");
127 ASSERT_INT_EQ(sshkey_parse_private_fileblob(buf,
128 "", "rsa_n", &k2, NULL), 0);
129 sshbuf_free(buf);
130 ASSERT_PTR_NE(k2, NULL);
131 ASSERT_INT_EQ(sshkey_equal(k1, k2), 1);
132 sshkey_free(k2);
133 TEST_DONE();
134
135 TEST_START("parse RSA from new-format w/ passphrase");
136 buf = load_file("rsa_n_pw");
137 ASSERT_INT_EQ(sshkey_parse_private_fileblob(buf,
138 (const char *)sshbuf_ptr(pw), "rsa_n_pw", &k2, NULL), 0);
139 sshbuf_free(buf);
140 ASSERT_PTR_NE(k2, NULL);
141 ASSERT_INT_EQ(sshkey_equal(k1, k2), 1);
142 sshkey_free(k2);
143 TEST_DONE();
144
145 TEST_START("load RSA from public");
146 ASSERT_INT_EQ(sshkey_load_public(test_data_file("rsa_1.pub"), &k2,
147 NULL), 0);
148 ASSERT_PTR_NE(k2, NULL);
149 ASSERT_INT_EQ(sshkey_equal(k1, k2), 1);
150 sshkey_free(k2);
151 TEST_DONE();
152
153 TEST_START("load RSA cert");
154 ASSERT_INT_EQ(sshkey_load_cert(test_data_file("rsa_1"), &k2), 0);
155 ASSERT_PTR_NE(k2, NULL);
156 ASSERT_INT_EQ(k2->type, KEY_RSA_CERT);
157 ASSERT_INT_EQ(sshkey_equal(k1, k2), 0);
158 ASSERT_INT_EQ(sshkey_equal_public(k1, k2), 1);
159 TEST_DONE();
160
161 TEST_START("RSA key hex fingerprint");
162 buf = load_text_file("rsa_1.fp");
163 cp = sshkey_fingerprint(k1, SSH_FP_MD5, SSH_FP_HEX);
164 ASSERT_PTR_NE(cp, NULL);
165 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
166 sshbuf_free(buf);
167 free(cp);
168 TEST_DONE();
169
170 TEST_START("RSA cert hex fingerprint");
171 buf = load_text_file("rsa_1-cert.fp");
172 cp = sshkey_fingerprint(k2, SSH_FP_MD5, SSH_FP_HEX);
173 ASSERT_PTR_NE(cp, NULL);
174 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
175 sshbuf_free(buf);
176 free(cp);
177 sshkey_free(k2);
178 TEST_DONE();
179
180 TEST_START("RSA key bubblebabble fingerprint");
181 buf = load_text_file("rsa_1.fp.bb");
182 cp = sshkey_fingerprint(k1, SSH_FP_SHA1, SSH_FP_BUBBLEBABBLE);
183 ASSERT_PTR_NE(cp, NULL);
184 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
185 sshbuf_free(buf);
186 free(cp);
187 TEST_DONE();
188
189 sshkey_free(k1);
190
191 TEST_START("parse DSA from private");
192 buf = load_file("dsa_1");
193 ASSERT_INT_EQ(sshkey_parse_private_fileblob(buf, "", "dsa_1",
194 &k1, NULL), 0);
195 sshbuf_free(buf);
196 ASSERT_PTR_NE(k1, NULL);
197 a = load_bignum("dsa_1.param.g");
198 b = load_bignum("dsa_1.param.priv");
199 c = load_bignum("dsa_1.param.pub");
200 ASSERT_BIGNUM_EQ(k1->dsa->g, a);
201 ASSERT_BIGNUM_EQ(k1->dsa->priv_key, b);
202 ASSERT_BIGNUM_EQ(k1->dsa->pub_key, c);
203 BN_free(a);
204 BN_free(b);
205 BN_free(c);
206 TEST_DONE();
207
208 TEST_START("parse DSA from private w/ passphrase");
209 buf = load_file("dsa_1_pw");
210 ASSERT_INT_EQ(sshkey_parse_private_fileblob(buf,
211 (const char *)sshbuf_ptr(pw), "dsa_1_pw", &k2, NULL), 0);
212 sshbuf_free(buf);
213 ASSERT_PTR_NE(k2, NULL);
214 ASSERT_INT_EQ(sshkey_equal(k1, k2), 1);
215 sshkey_free(k2);
216 TEST_DONE();
217
218 TEST_START("parse DSA from new-format");
219 buf = load_file("dsa_n");
220 ASSERT_INT_EQ(sshkey_parse_private_fileblob(buf,
221 "", "dsa_n", &k2, NULL), 0);
222 sshbuf_free(buf);
223 ASSERT_PTR_NE(k2, NULL);
224 ASSERT_INT_EQ(sshkey_equal(k1, k2), 1);
225 sshkey_free(k2);
226 TEST_DONE();
227
228 TEST_START("parse DSA from new-format w/ passphrase");
229 buf = load_file("dsa_n_pw");
230 ASSERT_INT_EQ(sshkey_parse_private_fileblob(buf,
231 (const char *)sshbuf_ptr(pw), "dsa_n_pw", &k2, NULL), 0);
232 sshbuf_free(buf);
233 ASSERT_PTR_NE(k2, NULL);
234 ASSERT_INT_EQ(sshkey_equal(k1, k2), 1);
235 sshkey_free(k2);
236 TEST_DONE();
237
238 TEST_START("load DSA from public");
239 ASSERT_INT_EQ(sshkey_load_public(test_data_file("dsa_1.pub"), &k2,
240 NULL), 0);
241 ASSERT_PTR_NE(k2, NULL);
242 ASSERT_INT_EQ(sshkey_equal(k1, k2), 1);
243 sshkey_free(k2);
244 TEST_DONE();
245
246 TEST_START("load DSA cert");
247 ASSERT_INT_EQ(sshkey_load_cert(test_data_file("dsa_1"), &k2), 0);
248 ASSERT_PTR_NE(k2, NULL);
249 ASSERT_INT_EQ(k2->type, KEY_DSA_CERT);
250 ASSERT_INT_EQ(sshkey_equal(k1, k2), 0);
251 ASSERT_INT_EQ(sshkey_equal_public(k1, k2), 1);
252 TEST_DONE();
253
254 TEST_START("DSA key hex fingerprint");
255 buf = load_text_file("dsa_1.fp");
256 cp = sshkey_fingerprint(k1, SSH_FP_MD5, SSH_FP_HEX);
257 ASSERT_PTR_NE(cp, NULL);
258 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
259 sshbuf_free(buf);
260 free(cp);
261 TEST_DONE();
262
263 TEST_START("DSA cert hex fingerprint");
264 buf = load_text_file("dsa_1-cert.fp");
265 cp = sshkey_fingerprint(k2, SSH_FP_MD5, SSH_FP_HEX);
266 ASSERT_PTR_NE(cp, NULL);
267 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
268 sshbuf_free(buf);
269 free(cp);
270 sshkey_free(k2);
271 TEST_DONE();
272
273 TEST_START("DSA key bubblebabble fingerprint");
274 buf = load_text_file("dsa_1.fp.bb");
275 cp = sshkey_fingerprint(k1, SSH_FP_SHA1, SSH_FP_BUBBLEBABBLE);
276 ASSERT_PTR_NE(cp, NULL);
277 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
278 sshbuf_free(buf);
279 free(cp);
280 TEST_DONE();
281
282 sshkey_free(k1);
283
284 TEST_START("parse ECDSA from private");
285 buf = load_file("ecdsa_1");
286 ASSERT_INT_EQ(sshkey_parse_private_fileblob(buf, "", "ecdsa_1",
287 &k1, NULL), 0);
288 sshbuf_free(buf);
289 ASSERT_PTR_NE(k1, NULL);
290 buf = load_text_file("ecdsa_1.param.curve");
291 ASSERT_STRING_EQ((const char *)sshbuf_ptr(buf),
292 OBJ_nid2sn(k1->ecdsa_nid));
293 sshbuf_free(buf);
294 a = load_bignum("ecdsa_1.param.priv");
295 b = load_bignum("ecdsa_1.param.pub");
296 c = EC_POINT_point2bn(EC_KEY_get0_group(k1->ecdsa),
297 EC_KEY_get0_public_key(k1->ecdsa), POINT_CONVERSION_UNCOMPRESSED,
298 NULL, NULL);
299 ASSERT_PTR_NE(c, NULL);
300 ASSERT_BIGNUM_EQ(EC_KEY_get0_private_key(k1->ecdsa), a);
301 ASSERT_BIGNUM_EQ(b, c);
302 BN_free(a);
303 BN_free(b);
304 BN_free(c);
305 TEST_DONE();
306
307 TEST_START("parse ECDSA from private w/ passphrase");
308 buf = load_file("ecdsa_1_pw");
309 ASSERT_INT_EQ(sshkey_parse_private_fileblob(buf,
310 (const char *)sshbuf_ptr(pw), "ecdsa_1_pw", &k2, NULL), 0);
311 sshbuf_free(buf);
312 ASSERT_PTR_NE(k2, NULL);
313 ASSERT_INT_EQ(sshkey_equal(k1, k2), 1);
314 sshkey_free(k2);
315 TEST_DONE();
316
317 TEST_START("parse ECDSA from new-format");
318 buf = load_file("ecdsa_n");
319 ASSERT_INT_EQ(sshkey_parse_private_fileblob(buf,
320 "", "ecdsa_n", &k2, NULL), 0);
321 sshbuf_free(buf);
322 ASSERT_PTR_NE(k2, NULL);
323 ASSERT_INT_EQ(sshkey_equal(k1, k2), 1);
324 sshkey_free(k2);
325 TEST_DONE();
326
327 TEST_START("parse ECDSA from new-format w/ passphrase");
328 buf = load_file("ecdsa_n_pw");
329 ASSERT_INT_EQ(sshkey_parse_private_fileblob(buf,
330 (const char *)sshbuf_ptr(pw), "ecdsa_n_pw", &k2, NULL), 0);
331 sshbuf_free(buf);
332 ASSERT_PTR_NE(k2, NULL);
333 ASSERT_INT_EQ(sshkey_equal(k1, k2), 1);
334 sshkey_free(k2);
335 TEST_DONE();
336
337 TEST_START("load ECDSA from public");
338 ASSERT_INT_EQ(sshkey_load_public(test_data_file("ecdsa_1.pub"), &k2,
339 NULL), 0);
340 ASSERT_PTR_NE(k2, NULL);
341 ASSERT_INT_EQ(sshkey_equal(k1, k2), 1);
342 sshkey_free(k2);
343 TEST_DONE();
344
345 TEST_START("load ECDSA cert");
346 ASSERT_INT_EQ(sshkey_load_cert(test_data_file("ecdsa_1"), &k2), 0);
347 ASSERT_PTR_NE(k2, NULL);
348 ASSERT_INT_EQ(k2->type, KEY_ECDSA_CERT);
349 ASSERT_INT_EQ(sshkey_equal(k1, k2), 0);
350 ASSERT_INT_EQ(sshkey_equal_public(k1, k2), 1);
351 TEST_DONE();
352
353 TEST_START("ECDSA key hex fingerprint");
354 buf = load_text_file("ecdsa_1.fp");
355 cp = sshkey_fingerprint(k1, SSH_FP_MD5, SSH_FP_HEX);
356 ASSERT_PTR_NE(cp, NULL);
357 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
358 sshbuf_free(buf);
359 free(cp);
360 TEST_DONE();
361
362 TEST_START("ECDSA cert hex fingerprint");
363 buf = load_text_file("ecdsa_1-cert.fp");
364 cp = sshkey_fingerprint(k2, SSH_FP_MD5, SSH_FP_HEX);
365 ASSERT_PTR_NE(cp, NULL);
366 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
367 sshbuf_free(buf);
368 free(cp);
369 sshkey_free(k2);
370 TEST_DONE();
371
372 TEST_START("ECDSA key bubblebabble fingerprint");
373 buf = load_text_file("ecdsa_1.fp.bb");
374 cp = sshkey_fingerprint(k1, SSH_FP_SHA1, SSH_FP_BUBBLEBABBLE);
375 ASSERT_PTR_NE(cp, NULL);
376 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
377 sshbuf_free(buf);
378 free(cp);
379 TEST_DONE();
380
381 sshkey_free(k1);
382
383 TEST_START("parse Ed25519 from private");
384 buf = load_file("ed25519_1");
385 ASSERT_INT_EQ(sshkey_parse_private_fileblob(buf, "", "ed25519_1",
386 &k1, NULL), 0);
387 sshbuf_free(buf);
388 ASSERT_PTR_NE(k1, NULL);
389 ASSERT_INT_EQ(k1->type, KEY_ED25519);
390 /* XXX check key contents */
391 TEST_DONE();
392
393 TEST_START("parse Ed25519 from private w/ passphrase");
394 buf = load_file("ed25519_1_pw");
395 ASSERT_INT_EQ(sshkey_parse_private_fileblob(buf,
396 (const char *)sshbuf_ptr(pw), "ed25519_1_pw", &k2, NULL), 0);
397 sshbuf_free(buf);
398 ASSERT_PTR_NE(k2, NULL);
399 ASSERT_INT_EQ(sshkey_equal(k1, k2), 1);
400 sshkey_free(k2);
401 TEST_DONE();
402
403 TEST_START("load Ed25519 from public");
404 ASSERT_INT_EQ(sshkey_load_public(test_data_file("ed25519_1.pub"), &k2,
405 NULL), 0);
406 ASSERT_PTR_NE(k2, NULL);
407 ASSERT_INT_EQ(sshkey_equal(k1, k2), 1);
408 sshkey_free(k2);
409 TEST_DONE();
410
411 TEST_START("load Ed25519 cert");
412 ASSERT_INT_EQ(sshkey_load_cert(test_data_file("ed25519_1"), &k2), 0);
413 ASSERT_PTR_NE(k2, NULL);
414 ASSERT_INT_EQ(k2->type, KEY_ED25519_CERT);
415 ASSERT_INT_EQ(sshkey_equal(k1, k2), 0);
416 ASSERT_INT_EQ(sshkey_equal_public(k1, k2), 1);
417 TEST_DONE();
418
419 TEST_START("Ed25519 key hex fingerprint");
420 buf = load_text_file("ed25519_1.fp");
421 cp = sshkey_fingerprint(k1, SSH_FP_MD5, SSH_FP_HEX);
422 ASSERT_PTR_NE(cp, NULL);
423 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
424 sshbuf_free(buf);
425 free(cp);
426 TEST_DONE();
427
428 TEST_START("Ed25519 cert hex fingerprint");
429 buf = load_text_file("ed25519_1-cert.fp");
430 cp = sshkey_fingerprint(k2, SSH_FP_MD5, SSH_FP_HEX);
431 ASSERT_PTR_NE(cp, NULL);
432 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
433 sshbuf_free(buf);
434 free(cp);
435 sshkey_free(k2);
436 TEST_DONE();
437
438 TEST_START("Ed25519 key bubblebabble fingerprint");
439 buf = load_text_file("ed25519_1.fp.bb");
440 cp = sshkey_fingerprint(k1, SSH_FP_SHA1, SSH_FP_BUBBLEBABBLE);
441 ASSERT_PTR_NE(cp, NULL);
442 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
443 sshbuf_free(buf);
444 free(cp);
445 TEST_DONE();
446
447 sshkey_free(k1);
448
449 sshbuf_free(pw);
450
451}