1 /* 	$OpenBSD: test_sshkey.c,v 1.21 2020/08/27 03:55:22 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 <stdio.h>
13 #ifdef HAVE_STDINT_H
14 #include <stdint.h>
15 #endif
16 #include <stdlib.h>
17 #include <string.h>
18 
19 #include <openssl/bn.h>
20 #include <openssl/rsa.h>
21 #include <openssl/dsa.h>
22 #if defined(OPENSSL_HAS_ECC) && defined(OPENSSL_HAS_NISTP256)
23 # include <openssl/ec.h>
24 #endif
25 
26 #include "../test_helper/test_helper.h"
27 
28 #include "ssherr.h"
29 #include "sshbuf.h"
30 #define SSHBUF_INTERNAL 1	/* access internals for testing */
31 #include "sshkey.h"
32 
33 #include "authfile.h"
34 #include "common.h"
35 #include "ssh2.h"
36 
37 void sshkey_tests(void);
38 
39 static void
put_opt(struct sshbuf * b,const char * name,const char * value)40 put_opt(struct sshbuf *b, const char *name, const char *value)
41 {
42 	struct sshbuf *sect;
43 
44 	sect = sshbuf_new();
45 	ASSERT_PTR_NE(sect, NULL);
46 	ASSERT_INT_EQ(sshbuf_put_cstring(b, name), 0);
47 	if (value != NULL)
48 		ASSERT_INT_EQ(sshbuf_put_cstring(sect, value), 0);
49 	ASSERT_INT_EQ(sshbuf_put_stringb(b, sect), 0);
50 	sshbuf_free(sect);
51 }
52 
53 #ifdef WITH_OPENSSL
54 static void
build_cert(struct sshbuf * b,struct sshkey * k,const char * type,struct sshkey * sign_key,struct sshkey * ca_key,const char * sig_alg)55 build_cert(struct sshbuf *b, struct sshkey *k, const char *type,
56     struct sshkey *sign_key, struct sshkey *ca_key,
57     const char *sig_alg)
58 {
59 	struct sshbuf *ca_buf, *pk, *principals, *critopts, *exts;
60 	u_char *sigblob;
61 	size_t siglen;
62 
63 	ca_buf = sshbuf_new();
64 	ASSERT_PTR_NE(ca_buf, NULL);
65 	ASSERT_INT_EQ(sshkey_putb(ca_key, ca_buf), 0);
66 
67 	/*
68 	 * Get the public key serialisation by rendering the key and skipping
69 	 * the type string. This is a bit of a hack :/
70 	 */
71 	pk = sshbuf_new();
72 	ASSERT_PTR_NE(pk, NULL);
73 	ASSERT_INT_EQ(sshkey_putb_plain(k, pk), 0);
74 	ASSERT_INT_EQ(sshbuf_skip_string(pk), 0);
75 
76 	principals = sshbuf_new();
77 	ASSERT_PTR_NE(principals, NULL);
78 	ASSERT_INT_EQ(sshbuf_put_cstring(principals, "gsamsa"), 0);
79 	ASSERT_INT_EQ(sshbuf_put_cstring(principals, "gregor"), 0);
80 
81 	critopts = sshbuf_new();
82 	ASSERT_PTR_NE(critopts, NULL);
83 	put_opt(critopts, "force-command", "/usr/local/bin/nethack");
84 	put_opt(critopts, "source-address", "192.168.0.0/24,127.0.0.1,::1");
85 
86 	exts = sshbuf_new();
87 	ASSERT_PTR_NE(exts, NULL);
88 	put_opt(critopts, "permit-X11-forwarding", NULL);
89 
90 	ASSERT_INT_EQ(sshbuf_put_cstring(b, type), 0);
91 	ASSERT_INT_EQ(sshbuf_put_cstring(b, "noncenoncenonce!"), 0); /* nonce */
92 	ASSERT_INT_EQ(sshbuf_putb(b, pk), 0); /* public key serialisation */
93 	ASSERT_INT_EQ(sshbuf_put_u64(b, 1234), 0); /* serial */
94 	ASSERT_INT_EQ(sshbuf_put_u32(b, SSH2_CERT_TYPE_USER), 0); /* type */
95 	ASSERT_INT_EQ(sshbuf_put_cstring(b, "gregor"), 0); /* key ID */
96 	ASSERT_INT_EQ(sshbuf_put_stringb(b, principals), 0); /* principals */
97 	ASSERT_INT_EQ(sshbuf_put_u64(b, 0), 0); /* start */
98 	ASSERT_INT_EQ(sshbuf_put_u64(b, 0xffffffffffffffffULL), 0); /* end */
99 	ASSERT_INT_EQ(sshbuf_put_stringb(b, critopts), 0); /* options */
100 	ASSERT_INT_EQ(sshbuf_put_stringb(b, exts), 0); /* extensions */
101 	ASSERT_INT_EQ(sshbuf_put_string(b, NULL, 0), 0); /* reserved */
102 	ASSERT_INT_EQ(sshbuf_put_stringb(b, ca_buf), 0); /* signature key */
103 	ASSERT_INT_EQ(sshkey_sign(sign_key, &sigblob, &siglen,
104 	    sshbuf_ptr(b), sshbuf_len(b), sig_alg, NULL, NULL, 0), 0);
105 	ASSERT_INT_EQ(sshbuf_put_string(b, sigblob, siglen), 0); /* signature */
106 
107 	free(sigblob);
108 	sshbuf_free(ca_buf);
109 	sshbuf_free(exts);
110 	sshbuf_free(critopts);
111 	sshbuf_free(principals);
112 	sshbuf_free(pk);
113 }
114 #endif /* WITH_OPENSSL */
115 
116 static void
signature_test(struct sshkey * k,struct sshkey * bad,const char * sig_alg,const u_char * d,size_t l)117 signature_test(struct sshkey *k, struct sshkey *bad, const char *sig_alg,
118     const u_char *d, size_t l)
119 {
120 	size_t len;
121 	u_char *sig;
122 
123 	ASSERT_INT_EQ(sshkey_sign(k, &sig, &len, d, l, sig_alg,
124 	    NULL, NULL, 0), 0);
125 	ASSERT_SIZE_T_GT(len, 8);
126 	ASSERT_PTR_NE(sig, NULL);
127 	ASSERT_INT_EQ(sshkey_verify(k, sig, len, d, l, NULL, 0, NULL), 0);
128 	ASSERT_INT_NE(sshkey_verify(bad, sig, len, d, l, NULL, 0, NULL), 0);
129 	/* Fuzz test is more comprehensive, this is just a smoke test */
130 	sig[len - 5] ^= 0x10;
131 	ASSERT_INT_NE(sshkey_verify(k, sig, len, d, l, NULL, 0, NULL), 0);
132 	free(sig);
133 }
134 
135 static void
banana(u_char * s,size_t l)136 banana(u_char *s, size_t l)
137 {
138 	size_t o;
139 	const u_char the_banana[] = { 'b', 'a', 'n', 'a', 'n', 'a' };
140 
141 	for (o = 0; o < l; o += sizeof(the_banana)) {
142 		if (l - o < sizeof(the_banana)) {
143 			memcpy(s + o, "nanananana", l - o);
144 			break;
145 		}
146 		memcpy(s + o, banana, sizeof(the_banana));
147 	}
148 }
149 
150 static void
signature_tests(struct sshkey * k,struct sshkey * bad,const char * sig_alg)151 signature_tests(struct sshkey *k, struct sshkey *bad, const char *sig_alg)
152 {
153 	u_char i, buf[2049];
154 	size_t lens[] = {
155 		1, 2, 7, 8, 9, 15, 16, 17, 31, 32, 33, 127, 128, 129,
156 		255, 256, 257, 1023, 1024, 1025, 2047, 2048, 2049
157 	};
158 
159 	for (i = 0; i < (sizeof(lens)/sizeof(lens[0])); i++) {
160 		test_subtest_info("%s key, banana length %zu",
161 		    sshkey_type(k), lens[i]);
162 		banana(buf, lens[i]);
163 		signature_test(k, bad, sig_alg, buf, lens[i]);
164 	}
165 }
166 
167 static struct sshkey *
get_private(const char * n)168 get_private(const char *n)
169 {
170 	struct sshbuf *b;
171 	struct sshkey *ret;
172 
173 	b = load_file(n);
174 	ASSERT_INT_EQ(sshkey_parse_private_fileblob(b, "", &ret, NULL), 0);
175 	sshbuf_free(b);
176 	return ret;
177 }
178 
179 void
sshkey_tests(void)180 sshkey_tests(void)
181 {
182 	struct sshkey *k1, *k2, *k3, *kf;
183 #ifdef WITH_OPENSSL
184 	struct sshkey *k4, *kr, *kd;
185 #ifdef OPENSSL_HAS_ECC
186 	struct sshkey *ke;
187 #endif /* OPENSSL_HAS_ECC */
188 #endif /* WITH_OPENSSL */
189 	struct sshbuf *b;
190 
191 	TEST_START("new invalid");
192 	k1 = sshkey_new(-42);
193 	ASSERT_PTR_EQ(k1, NULL);
194 	TEST_DONE();
195 
196 	TEST_START("new/free KEY_UNSPEC");
197 	k1 = sshkey_new(KEY_UNSPEC);
198 	ASSERT_PTR_NE(k1, NULL);
199 	sshkey_free(k1);
200 	TEST_DONE();
201 
202 #ifdef WITH_OPENSSL
203 	TEST_START("new/free KEY_RSA");
204 	k1 = sshkey_new(KEY_RSA);
205 	ASSERT_PTR_NE(k1, NULL);
206 	ASSERT_PTR_NE(k1->rsa, NULL);
207 	sshkey_free(k1);
208 	TEST_DONE();
209 
210 	TEST_START("new/free KEY_DSA");
211 	k1 = sshkey_new(KEY_DSA);
212 	ASSERT_PTR_NE(k1, NULL);
213 	ASSERT_PTR_NE(k1->dsa, NULL);
214 	sshkey_free(k1);
215 	TEST_DONE();
216 
217 #ifdef OPENSSL_HAS_ECC
218 	TEST_START("new/free KEY_ECDSA");
219 	k1 = sshkey_new(KEY_ECDSA);
220 	ASSERT_PTR_NE(k1, NULL);
221 	ASSERT_PTR_EQ(k1->ecdsa, NULL);  /* Can't allocate without NID */
222 	sshkey_free(k1);
223 	TEST_DONE();
224 #endif
225 
226 	TEST_START("new/free KEY_ED25519");
227 	k1 = sshkey_new(KEY_ED25519);
228 	ASSERT_PTR_NE(k1, NULL);
229 	/* These should be blank until key loaded or generated */
230 	ASSERT_PTR_EQ(k1->ed25519_sk, NULL);
231 	ASSERT_PTR_EQ(k1->ed25519_pk, NULL);
232 	sshkey_free(k1);
233 	TEST_DONE();
234 
235 	TEST_START("generate KEY_RSA too small modulus");
236 	ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 128, &k1),
237 	    SSH_ERR_KEY_LENGTH);
238 	ASSERT_PTR_EQ(k1, NULL);
239 	TEST_DONE();
240 
241 	TEST_START("generate KEY_RSA too large modulus");
242 	ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 1 << 20, &k1),
243 	    SSH_ERR_KEY_LENGTH);
244 	ASSERT_PTR_EQ(k1, NULL);
245 	TEST_DONE();
246 
247 	TEST_START("generate KEY_DSA wrong bits");
248 	ASSERT_INT_EQ(sshkey_generate(KEY_DSA, 2048, &k1),
249 	    SSH_ERR_KEY_LENGTH);
250 	ASSERT_PTR_EQ(k1, NULL);
251 	sshkey_free(k1);
252 	TEST_DONE();
253 
254 #ifdef OPENSSL_HAS_ECC
255 	TEST_START("generate KEY_ECDSA wrong bits");
256 	ASSERT_INT_EQ(sshkey_generate(KEY_ECDSA, 42, &k1),
257 	    SSH_ERR_KEY_LENGTH);
258 	ASSERT_PTR_EQ(k1, NULL);
259 	sshkey_free(k1);
260 	TEST_DONE();
261 #endif
262 
263 	TEST_START("generate KEY_RSA");
264 	ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 767, &kr),
265 	    SSH_ERR_KEY_LENGTH);
266 	ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 1024, &kr), 0);
267 	ASSERT_PTR_NE(kr, NULL);
268 	ASSERT_PTR_NE(kr->rsa, NULL);
269 	ASSERT_PTR_NE(rsa_n(kr), NULL);
270 	ASSERT_PTR_NE(rsa_e(kr), NULL);
271 	ASSERT_PTR_NE(rsa_p(kr), NULL);
272 	ASSERT_INT_EQ(BN_num_bits(rsa_n(kr)), 1024);
273 	TEST_DONE();
274 
275 	TEST_START("generate KEY_DSA");
276 	ASSERT_INT_EQ(sshkey_generate(KEY_DSA, 1024, &kd), 0);
277 	ASSERT_PTR_NE(kd, NULL);
278 	ASSERT_PTR_NE(kd->dsa, NULL);
279 	ASSERT_PTR_NE(dsa_g(kd), NULL);
280 	ASSERT_PTR_NE(dsa_priv_key(kd), NULL);
281 	TEST_DONE();
282 
283 #ifdef OPENSSL_HAS_ECC
284 	TEST_START("generate KEY_ECDSA");
285 	ASSERT_INT_EQ(sshkey_generate(KEY_ECDSA, 256, &ke), 0);
286 	ASSERT_PTR_NE(ke, NULL);
287 	ASSERT_PTR_NE(ke->ecdsa, NULL);
288 	ASSERT_PTR_NE(EC_KEY_get0_public_key(ke->ecdsa), NULL);
289 	ASSERT_PTR_NE(EC_KEY_get0_private_key(ke->ecdsa), NULL);
290 	TEST_DONE();
291 #endif /* OPENSSL_HAS_ECC */
292 #endif /* WITH_OPENSSL */
293 
294 	TEST_START("generate KEY_ED25519");
295 	ASSERT_INT_EQ(sshkey_generate(KEY_ED25519, 256, &kf), 0);
296 	ASSERT_PTR_NE(kf, NULL);
297 	ASSERT_INT_EQ(kf->type, KEY_ED25519);
298 	ASSERT_PTR_NE(kf->ed25519_pk, NULL);
299 	ASSERT_PTR_NE(kf->ed25519_sk, NULL);
300 	TEST_DONE();
301 
302 #ifdef WITH_OPENSSL
303 	TEST_START("demote KEY_RSA");
304 	ASSERT_INT_EQ(sshkey_from_private(kr, &k1), 0);
305 	ASSERT_PTR_NE(k1, NULL);
306 	ASSERT_PTR_NE(kr, k1);
307 	ASSERT_INT_EQ(k1->type, KEY_RSA);
308 	ASSERT_PTR_NE(k1->rsa, NULL);
309 	ASSERT_PTR_NE(rsa_n(k1), NULL);
310 	ASSERT_PTR_NE(rsa_e(k1), NULL);
311 	ASSERT_PTR_EQ(rsa_p(k1), NULL);
312 	TEST_DONE();
313 
314 	TEST_START("equal KEY_RSA/demoted KEY_RSA");
315 	ASSERT_INT_EQ(sshkey_equal(kr, k1), 1);
316 	sshkey_free(k1);
317 	TEST_DONE();
318 
319 	TEST_START("demote KEY_DSA");
320 	ASSERT_INT_EQ(sshkey_from_private(kd, &k1), 0);
321 	ASSERT_PTR_NE(k1, NULL);
322 	ASSERT_PTR_NE(kd, k1);
323 	ASSERT_INT_EQ(k1->type, KEY_DSA);
324 	ASSERT_PTR_NE(k1->dsa, NULL);
325 	ASSERT_PTR_NE(dsa_g(k1), NULL);
326 	ASSERT_PTR_EQ(dsa_priv_key(k1), NULL);
327 	TEST_DONE();
328 
329 	TEST_START("equal KEY_DSA/demoted KEY_DSA");
330 	ASSERT_INT_EQ(sshkey_equal(kd, k1), 1);
331 	sshkey_free(k1);
332 	TEST_DONE();
333 
334 #ifdef OPENSSL_HAS_ECC
335 	TEST_START("demote KEY_ECDSA");
336 	ASSERT_INT_EQ(sshkey_from_private(ke, &k1), 0);
337 	ASSERT_PTR_NE(k1, NULL);
338 	ASSERT_PTR_NE(ke, k1);
339 	ASSERT_INT_EQ(k1->type, KEY_ECDSA);
340 	ASSERT_PTR_NE(k1->ecdsa, NULL);
341 	ASSERT_INT_EQ(k1->ecdsa_nid, ke->ecdsa_nid);
342 	ASSERT_PTR_NE(EC_KEY_get0_public_key(ke->ecdsa), NULL);
343 	ASSERT_PTR_EQ(EC_KEY_get0_private_key(k1->ecdsa), NULL);
344 	TEST_DONE();
345 
346 	TEST_START("equal KEY_ECDSA/demoted KEY_ECDSA");
347 	ASSERT_INT_EQ(sshkey_equal(ke, k1), 1);
348 	sshkey_free(k1);
349 	TEST_DONE();
350 #endif /* OPENSSL_HAS_ECC */
351 #endif /* WITH_OPENSSL */
352 
353 	TEST_START("demote KEY_ED25519");
354 	ASSERT_INT_EQ(sshkey_from_private(kf, &k1), 0);
355 	ASSERT_PTR_NE(k1, NULL);
356 	ASSERT_PTR_NE(kf, k1);
357 	ASSERT_INT_EQ(k1->type, KEY_ED25519);
358 	ASSERT_PTR_NE(k1->ed25519_pk, NULL);
359 	ASSERT_PTR_EQ(k1->ed25519_sk, NULL);
360 	TEST_DONE();
361 
362 	TEST_START("equal KEY_ED25519/demoted KEY_ED25519");
363 	ASSERT_INT_EQ(sshkey_equal(kf, k1), 1);
364 	sshkey_free(k1);
365 	TEST_DONE();
366 
367 #ifdef WITH_OPENSSL
368 	TEST_START("equal mismatched key types");
369 	ASSERT_INT_EQ(sshkey_equal(kd, kr), 0);
370 #ifdef OPENSSL_HAS_ECC
371 	ASSERT_INT_EQ(sshkey_equal(kd, ke), 0);
372 	ASSERT_INT_EQ(sshkey_equal(kr, ke), 0);
373 	ASSERT_INT_EQ(sshkey_equal(ke, kf), 0);
374 #endif /* OPENSSL_HAS_ECC */
375 	ASSERT_INT_EQ(sshkey_equal(kd, kf), 0);
376 	TEST_DONE();
377 #endif /* WITH_OPENSSL */
378 
379 	TEST_START("equal different keys");
380 #ifdef WITH_OPENSSL
381 	ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 1024, &k1), 0);
382 	ASSERT_INT_EQ(sshkey_equal(kr, k1), 0);
383 	sshkey_free(k1);
384 	ASSERT_INT_EQ(sshkey_generate(KEY_DSA, 1024, &k1), 0);
385 	ASSERT_INT_EQ(sshkey_equal(kd, k1), 0);
386 	sshkey_free(k1);
387 #ifdef OPENSSL_HAS_ECC
388 	ASSERT_INT_EQ(sshkey_generate(KEY_ECDSA, 256, &k1), 0);
389 	ASSERT_INT_EQ(sshkey_equal(ke, k1), 0);
390 	sshkey_free(k1);
391 #endif /* OPENSSL_HAS_ECC */
392 #endif /* WITH_OPENSSL */
393 	ASSERT_INT_EQ(sshkey_generate(KEY_ED25519, 256, &k1), 0);
394 	ASSERT_INT_EQ(sshkey_equal(kf, k1), 0);
395 	sshkey_free(k1);
396 	TEST_DONE();
397 
398 #ifdef WITH_OPENSSL
399 	sshkey_free(kr);
400 	sshkey_free(kd);
401 #ifdef OPENSSL_HAS_ECC
402 	sshkey_free(ke);
403 #endif /* OPENSSL_HAS_ECC */
404 #endif /* WITH_OPENSSL */
405 	sshkey_free(kf);
406 
407 	TEST_START("certify key");
408 	ASSERT_INT_EQ(sshkey_load_public(test_data_file("ed25519_1.pub"),
409 	    &k1, NULL), 0);
410 	k2 = get_private("ed25519_2");
411 	ASSERT_INT_EQ(sshkey_to_certified(k1), 0);
412 	ASSERT_PTR_NE(k1->cert, NULL);
413 	k1->cert->type = SSH2_CERT_TYPE_USER;
414 	k1->cert->serial = 1234;
415 	k1->cert->key_id = strdup("estragon");
416 	ASSERT_PTR_NE(k1->cert->key_id, NULL);
417 	k1->cert->principals = calloc(4, sizeof(*k1->cert->principals));
418 	ASSERT_PTR_NE(k1->cert->principals, NULL);
419 	k1->cert->principals[0] = strdup("estragon");
420 	k1->cert->principals[1] = strdup("vladimir");
421 	k1->cert->principals[2] = strdup("pozzo");
422 	k1->cert->principals[3] = strdup("lucky");
423 	ASSERT_PTR_NE(k1->cert->principals[0], NULL);
424 	ASSERT_PTR_NE(k1->cert->principals[1], NULL);
425 	ASSERT_PTR_NE(k1->cert->principals[2], NULL);
426 	ASSERT_PTR_NE(k1->cert->principals[3], NULL);
427 	k1->cert->nprincipals = 4;
428 	k1->cert->valid_after = 0;
429 	k1->cert->valid_before = (u_int64_t)-1;
430 	sshbuf_free(k1->cert->critical);
431 	k1->cert->critical = sshbuf_new();
432 	ASSERT_PTR_NE(k1->cert->critical, NULL);
433 	sshbuf_free(k1->cert->extensions);
434 	k1->cert->extensions = sshbuf_new();
435 	ASSERT_PTR_NE(k1->cert->extensions, NULL);
436 	put_opt(k1->cert->critical, "force-command", "/usr/bin/true");
437 	put_opt(k1->cert->critical, "source-address", "127.0.0.1");
438 	put_opt(k1->cert->extensions, "permit-X11-forwarding", NULL);
439 	put_opt(k1->cert->extensions, "permit-agent-forwarding", NULL);
440 	ASSERT_INT_EQ(sshkey_from_private(k2, &k1->cert->signature_key), 0);
441 	ASSERT_INT_EQ(sshkey_certify(k1, k2, NULL, NULL, NULL), 0);
442 	b = sshbuf_new();
443 	ASSERT_PTR_NE(b, NULL);
444 	ASSERT_INT_EQ(sshkey_putb(k1, b), 0);
445 	ASSERT_INT_EQ(sshkey_from_blob(sshbuf_ptr(b), sshbuf_len(b), &k3), 0);
446 
447 	sshkey_free(k1);
448 	sshkey_free(k2);
449 	sshkey_free(k3);
450 	sshbuf_reset(b);
451 	TEST_DONE();
452 
453 #ifdef WITH_OPENSSL
454 	TEST_START("sign and verify RSA");
455 	k1 = get_private("rsa_1");
456 	ASSERT_INT_EQ(sshkey_load_public(test_data_file("rsa_2.pub"), &k2,
457 	    NULL), 0);
458 	signature_tests(k1, k2, "ssh-rsa");
459 	sshkey_free(k1);
460 	sshkey_free(k2);
461 	TEST_DONE();
462 
463 	TEST_START("sign and verify RSA-SHA256");
464 	k1 = get_private("rsa_1");
465 	ASSERT_INT_EQ(sshkey_load_public(test_data_file("rsa_2.pub"), &k2,
466 	    NULL), 0);
467 	signature_tests(k1, k2, "rsa-sha2-256");
468 	sshkey_free(k1);
469 	sshkey_free(k2);
470 	TEST_DONE();
471 
472 	TEST_START("sign and verify RSA-SHA512");
473 	k1 = get_private("rsa_1");
474 	ASSERT_INT_EQ(sshkey_load_public(test_data_file("rsa_2.pub"), &k2,
475 	    NULL), 0);
476 	signature_tests(k1, k2, "rsa-sha2-512");
477 	sshkey_free(k1);
478 	sshkey_free(k2);
479 	TEST_DONE();
480 
481 	TEST_START("sign and verify DSA");
482 	k1 = get_private("dsa_1");
483 	ASSERT_INT_EQ(sshkey_load_public(test_data_file("dsa_2.pub"), &k2,
484 	    NULL), 0);
485 	signature_tests(k1, k2, NULL);
486 	sshkey_free(k1);
487 	sshkey_free(k2);
488 	TEST_DONE();
489 
490 #ifdef OPENSSL_HAS_ECC
491 	TEST_START("sign and verify ECDSA");
492 	k1 = get_private("ecdsa_1");
493 	ASSERT_INT_EQ(sshkey_load_public(test_data_file("ecdsa_2.pub"), &k2,
494 	    NULL), 0);
495 	signature_tests(k1, k2, NULL);
496 	sshkey_free(k1);
497 	sshkey_free(k2);
498 	TEST_DONE();
499 #endif /* OPENSSL_HAS_ECC */
500 #endif /* WITH_OPENSSL */
501 
502 	TEST_START("sign and verify ED25519");
503 	k1 = get_private("ed25519_1");
504 	ASSERT_INT_EQ(sshkey_load_public(test_data_file("ed25519_2.pub"), &k2,
505 	    NULL), 0);
506 	signature_tests(k1, k2, NULL);
507 	sshkey_free(k1);
508 	sshkey_free(k2);
509 	TEST_DONE();
510 
511 #ifdef WITH_OPENSSL
512 	TEST_START("nested certificate");
513 	ASSERT_INT_EQ(sshkey_load_cert(test_data_file("rsa_1"), &k1), 0);
514 	ASSERT_INT_EQ(sshkey_load_public(test_data_file("rsa_1.pub"), &k2,
515 	    NULL), 0);
516 	k3 = get_private("rsa_1");
517 	build_cert(b, k2, "ssh-rsa-cert-v01@openssh.com", k3, k1, NULL);
518 	ASSERT_INT_EQ(sshkey_from_blob(sshbuf_ptr(b), sshbuf_len(b), &k4),
519 	    SSH_ERR_KEY_CERT_INVALID_SIGN_KEY);
520 	ASSERT_PTR_EQ(k4, NULL);
521 	sshkey_free(k1);
522 	sshkey_free(k2);
523 	sshkey_free(k3);
524 	sshbuf_free(b);
525 	TEST_DONE();
526 #endif /* WITH_OPENSSL */
527 }
528