summaryrefslogtreecommitdiff
path: root/regress/unittests/sshkey
diff options
context:
space:
mode:
Diffstat (limited to 'regress/unittests/sshkey')
-rw-r--r--regress/unittests/sshkey/common.c4
-rwxr-xr-xregress/unittests/sshkey/mktestdata.sh4
-rw-r--r--regress/unittests/sshkey/test_file.c33
-rw-r--r--regress/unittests/sshkey/test_fuzz.c13
-rw-r--r--regress/unittests/sshkey/test_sshkey.c192
-rw-r--r--regress/unittests/sshkey/testdata/dsa_1-cert.fp2
-rw-r--r--regress/unittests/sshkey/testdata/dsa_1.fp2
-rw-r--r--regress/unittests/sshkey/testdata/dsa_2.fp2
-rw-r--r--regress/unittests/sshkey/testdata/ecdsa_1-cert.fp2
-rw-r--r--regress/unittests/sshkey/testdata/ecdsa_1.fp2
-rw-r--r--regress/unittests/sshkey/testdata/ecdsa_2.fp2
-rw-r--r--regress/unittests/sshkey/testdata/ed25519_1-cert.fp2
-rw-r--r--regress/unittests/sshkey/testdata/ed25519_1.fp2
-rw-r--r--regress/unittests/sshkey/testdata/ed25519_2.fp2
-rw-r--r--regress/unittests/sshkey/testdata/rsa1_1.fp2
-rw-r--r--regress/unittests/sshkey/testdata/rsa1_2.fp2
-rw-r--r--regress/unittests/sshkey/testdata/rsa_1-cert.fp2
-rw-r--r--regress/unittests/sshkey/testdata/rsa_1.fp2
-rw-r--r--regress/unittests/sshkey/testdata/rsa_2.fp2
19 files changed, 223 insertions, 51 deletions
diff --git a/regress/unittests/sshkey/common.c b/regress/unittests/sshkey/common.c
index 0a4b3a90c..b598f05cb 100644
--- a/regress/unittests/sshkey/common.c
+++ b/regress/unittests/sshkey/common.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: common.c,v 1.1 2014/06/24 01:14:18 djm Exp $ */ 1/* $OpenBSD: common.c,v 1.2 2015/01/08 13:10:58 djm Exp $ */
2/* 2/*
3 * Helpers for key API tests 3 * Helpers for key API tests
4 * 4 *
@@ -44,7 +44,7 @@ load_file(const char *name)
44 44
45 ASSERT_PTR_NE(ret = sshbuf_new(), NULL); 45 ASSERT_PTR_NE(ret = sshbuf_new(), NULL);
46 ASSERT_INT_NE(fd = open(test_data_file(name), O_RDONLY), -1); 46 ASSERT_INT_NE(fd = open(test_data_file(name), O_RDONLY), -1);
47 ASSERT_INT_EQ(sshkey_load_file(fd, name, ret), 0); 47 ASSERT_INT_EQ(sshkey_load_file(fd, ret), 0);
48 close(fd); 48 close(fd);
49 return ret; 49 return ret;
50} 50}
diff --git a/regress/unittests/sshkey/mktestdata.sh b/regress/unittests/sshkey/mktestdata.sh
index ee1fe3962..09165af02 100755
--- a/regress/unittests/sshkey/mktestdata.sh
+++ b/regress/unittests/sshkey/mktestdata.sh
@@ -1,5 +1,5 @@
1#!/bin/sh 1#!/bin/sh
2# $OpenBSD: mktestdata.sh,v 1.3 2014/07/22 23:57:40 dtucker Exp $ 2# $OpenBSD: mktestdata.sh,v 1.4 2015/01/18 19:54:46 djm Exp $
3 3
4PW=mekmitasdigoat 4PW=mekmitasdigoat
5 5
@@ -187,4 +187,6 @@ ssh-keygen -Bf dsa_2 | awk '{print $2}' > dsa_2.fp.bb
187ssh-keygen -Bf ecdsa_2 | awk '{print $2}' > ecdsa_2.fp.bb 187ssh-keygen -Bf ecdsa_2 | awk '{print $2}' > ecdsa_2.fp.bb
188ssh-keygen -Bf ed25519_2 | awk '{print $2}' > ed25519_2.fp.bb 188ssh-keygen -Bf ed25519_2 | awk '{print $2}' > ed25519_2.fp.bb
189 189
190# XXX Extend ssh-keygen to do detached signatures (better to test/fuzz against)
191
190echo "$PW" > pw 192echo "$PW" > pw
diff --git a/regress/unittests/sshkey/test_file.c b/regress/unittests/sshkey/test_file.c
index 764f7fb76..fa95212bf 100644
--- a/regress/unittests/sshkey/test_file.c
+++ b/regress/unittests/sshkey/test_file.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: test_file.c,v 1.1 2014/06/24 01:14:18 djm Exp $ */ 1/* $OpenBSD: test_file.c,v 1.3 2015/03/04 23:22:35 djm Exp $ */
2/* 2/*
3 * Regress test for sshkey.h key management API 3 * Regress test for sshkey.h key management API
4 * 4 *
@@ -33,6 +33,7 @@
33#include "authfile.h" 33#include "authfile.h"
34#include "sshkey.h" 34#include "sshkey.h"
35#include "sshbuf.h" 35#include "sshbuf.h"
36#include "digest.h"
36 37
37#include "common.h" 38#include "common.h"
38 39
@@ -50,6 +51,7 @@ sshkey_file_tests(void)
50 pw = load_text_file("pw"); 51 pw = load_text_file("pw");
51 TEST_DONE(); 52 TEST_DONE();
52 53
54#ifdef WITH_SSH1
53 TEST_START("parse RSA1 from private"); 55 TEST_START("parse RSA1 from private");
54 buf = load_file("rsa1_1"); 56 buf = load_file("rsa1_1");
55 ASSERT_INT_EQ(sshkey_parse_private_fileblob(buf, "", "rsa1_1", 57 ASSERT_INT_EQ(sshkey_parse_private_fileblob(buf, "", "rsa1_1",
@@ -81,7 +83,7 @@ sshkey_file_tests(void)
81 83
82 TEST_START("RSA1 key hex fingerprint"); 84 TEST_START("RSA1 key hex fingerprint");
83 buf = load_text_file("rsa1_1.fp"); 85 buf = load_text_file("rsa1_1.fp");
84 cp = sshkey_fingerprint(k1, SSH_FP_MD5, SSH_FP_HEX); 86 cp = sshkey_fingerprint(k1, SSH_DIGEST_MD5, SSH_FP_HEX);
85 ASSERT_PTR_NE(cp, NULL); 87 ASSERT_PTR_NE(cp, NULL);
86 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf)); 88 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
87 sshbuf_free(buf); 89 sshbuf_free(buf);
@@ -90,7 +92,7 @@ sshkey_file_tests(void)
90 92
91 TEST_START("RSA1 key bubblebabble fingerprint"); 93 TEST_START("RSA1 key bubblebabble fingerprint");
92 buf = load_text_file("rsa1_1.fp.bb"); 94 buf = load_text_file("rsa1_1.fp.bb");
93 cp = sshkey_fingerprint(k1, SSH_FP_SHA1, SSH_FP_BUBBLEBABBLE); 95 cp = sshkey_fingerprint(k1, SSH_DIGEST_SHA1, SSH_FP_BUBBLEBABBLE);
94 ASSERT_PTR_NE(cp, NULL); 96 ASSERT_PTR_NE(cp, NULL);
95 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf)); 97 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
96 sshbuf_free(buf); 98 sshbuf_free(buf);
@@ -98,6 +100,7 @@ sshkey_file_tests(void)
98 TEST_DONE(); 100 TEST_DONE();
99 101
100 sshkey_free(k1); 102 sshkey_free(k1);
103#endif
101 104
102 TEST_START("parse RSA from private"); 105 TEST_START("parse RSA from private");
103 buf = load_file("rsa_1"); 106 buf = load_file("rsa_1");
@@ -164,7 +167,7 @@ sshkey_file_tests(void)
164 167
165 TEST_START("RSA key hex fingerprint"); 168 TEST_START("RSA key hex fingerprint");
166 buf = load_text_file("rsa_1.fp"); 169 buf = load_text_file("rsa_1.fp");
167 cp = sshkey_fingerprint(k1, SSH_FP_MD5, SSH_FP_HEX); 170 cp = sshkey_fingerprint(k1, SSH_DIGEST_MD5, SSH_FP_HEX);
168 ASSERT_PTR_NE(cp, NULL); 171 ASSERT_PTR_NE(cp, NULL);
169 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf)); 172 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
170 sshbuf_free(buf); 173 sshbuf_free(buf);
@@ -173,7 +176,7 @@ sshkey_file_tests(void)
173 176
174 TEST_START("RSA cert hex fingerprint"); 177 TEST_START("RSA cert hex fingerprint");
175 buf = load_text_file("rsa_1-cert.fp"); 178 buf = load_text_file("rsa_1-cert.fp");
176 cp = sshkey_fingerprint(k2, SSH_FP_MD5, SSH_FP_HEX); 179 cp = sshkey_fingerprint(k2, SSH_DIGEST_MD5, SSH_FP_HEX);
177 ASSERT_PTR_NE(cp, NULL); 180 ASSERT_PTR_NE(cp, NULL);
178 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf)); 181 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
179 sshbuf_free(buf); 182 sshbuf_free(buf);
@@ -183,7 +186,7 @@ sshkey_file_tests(void)
183 186
184 TEST_START("RSA key bubblebabble fingerprint"); 187 TEST_START("RSA key bubblebabble fingerprint");
185 buf = load_text_file("rsa_1.fp.bb"); 188 buf = load_text_file("rsa_1.fp.bb");
186 cp = sshkey_fingerprint(k1, SSH_FP_SHA1, SSH_FP_BUBBLEBABBLE); 189 cp = sshkey_fingerprint(k1, SSH_DIGEST_SHA1, SSH_FP_BUBBLEBABBLE);
187 ASSERT_PTR_NE(cp, NULL); 190 ASSERT_PTR_NE(cp, NULL);
188 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf)); 191 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
189 sshbuf_free(buf); 192 sshbuf_free(buf);
@@ -257,7 +260,7 @@ sshkey_file_tests(void)
257 260
258 TEST_START("DSA key hex fingerprint"); 261 TEST_START("DSA key hex fingerprint");
259 buf = load_text_file("dsa_1.fp"); 262 buf = load_text_file("dsa_1.fp");
260 cp = sshkey_fingerprint(k1, SSH_FP_MD5, SSH_FP_HEX); 263 cp = sshkey_fingerprint(k1, SSH_DIGEST_MD5, SSH_FP_HEX);
261 ASSERT_PTR_NE(cp, NULL); 264 ASSERT_PTR_NE(cp, NULL);
262 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf)); 265 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
263 sshbuf_free(buf); 266 sshbuf_free(buf);
@@ -266,7 +269,7 @@ sshkey_file_tests(void)
266 269
267 TEST_START("DSA cert hex fingerprint"); 270 TEST_START("DSA cert hex fingerprint");
268 buf = load_text_file("dsa_1-cert.fp"); 271 buf = load_text_file("dsa_1-cert.fp");
269 cp = sshkey_fingerprint(k2, SSH_FP_MD5, SSH_FP_HEX); 272 cp = sshkey_fingerprint(k2, SSH_DIGEST_MD5, SSH_FP_HEX);
270 ASSERT_PTR_NE(cp, NULL); 273 ASSERT_PTR_NE(cp, NULL);
271 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf)); 274 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
272 sshbuf_free(buf); 275 sshbuf_free(buf);
@@ -276,7 +279,7 @@ sshkey_file_tests(void)
276 279
277 TEST_START("DSA key bubblebabble fingerprint"); 280 TEST_START("DSA key bubblebabble fingerprint");
278 buf = load_text_file("dsa_1.fp.bb"); 281 buf = load_text_file("dsa_1.fp.bb");
279 cp = sshkey_fingerprint(k1, SSH_FP_SHA1, SSH_FP_BUBBLEBABBLE); 282 cp = sshkey_fingerprint(k1, SSH_DIGEST_SHA1, SSH_FP_BUBBLEBABBLE);
280 ASSERT_PTR_NE(cp, NULL); 283 ASSERT_PTR_NE(cp, NULL);
281 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf)); 284 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
282 sshbuf_free(buf); 285 sshbuf_free(buf);
@@ -357,7 +360,7 @@ sshkey_file_tests(void)
357 360
358 TEST_START("ECDSA key hex fingerprint"); 361 TEST_START("ECDSA key hex fingerprint");
359 buf = load_text_file("ecdsa_1.fp"); 362 buf = load_text_file("ecdsa_1.fp");
360 cp = sshkey_fingerprint(k1, SSH_FP_MD5, SSH_FP_HEX); 363 cp = sshkey_fingerprint(k1, SSH_DIGEST_MD5, SSH_FP_HEX);
361 ASSERT_PTR_NE(cp, NULL); 364 ASSERT_PTR_NE(cp, NULL);
362 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf)); 365 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
363 sshbuf_free(buf); 366 sshbuf_free(buf);
@@ -366,7 +369,7 @@ sshkey_file_tests(void)
366 369
367 TEST_START("ECDSA cert hex fingerprint"); 370 TEST_START("ECDSA cert hex fingerprint");
368 buf = load_text_file("ecdsa_1-cert.fp"); 371 buf = load_text_file("ecdsa_1-cert.fp");
369 cp = sshkey_fingerprint(k2, SSH_FP_MD5, SSH_FP_HEX); 372 cp = sshkey_fingerprint(k2, SSH_DIGEST_MD5, SSH_FP_HEX);
370 ASSERT_PTR_NE(cp, NULL); 373 ASSERT_PTR_NE(cp, NULL);
371 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf)); 374 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
372 sshbuf_free(buf); 375 sshbuf_free(buf);
@@ -376,7 +379,7 @@ sshkey_file_tests(void)
376 379
377 TEST_START("ECDSA key bubblebabble fingerprint"); 380 TEST_START("ECDSA key bubblebabble fingerprint");
378 buf = load_text_file("ecdsa_1.fp.bb"); 381 buf = load_text_file("ecdsa_1.fp.bb");
379 cp = sshkey_fingerprint(k1, SSH_FP_SHA1, SSH_FP_BUBBLEBABBLE); 382 cp = sshkey_fingerprint(k1, SSH_DIGEST_SHA1, SSH_FP_BUBBLEBABBLE);
380 ASSERT_PTR_NE(cp, NULL); 383 ASSERT_PTR_NE(cp, NULL);
381 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf)); 384 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
382 sshbuf_free(buf); 385 sshbuf_free(buf);
@@ -424,7 +427,7 @@ sshkey_file_tests(void)
424 427
425 TEST_START("Ed25519 key hex fingerprint"); 428 TEST_START("Ed25519 key hex fingerprint");
426 buf = load_text_file("ed25519_1.fp"); 429 buf = load_text_file("ed25519_1.fp");
427 cp = sshkey_fingerprint(k1, SSH_FP_MD5, SSH_FP_HEX); 430 cp = sshkey_fingerprint(k1, SSH_DIGEST_MD5, SSH_FP_HEX);
428 ASSERT_PTR_NE(cp, NULL); 431 ASSERT_PTR_NE(cp, NULL);
429 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf)); 432 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
430 sshbuf_free(buf); 433 sshbuf_free(buf);
@@ -433,7 +436,7 @@ sshkey_file_tests(void)
433 436
434 TEST_START("Ed25519 cert hex fingerprint"); 437 TEST_START("Ed25519 cert hex fingerprint");
435 buf = load_text_file("ed25519_1-cert.fp"); 438 buf = load_text_file("ed25519_1-cert.fp");
436 cp = sshkey_fingerprint(k2, SSH_FP_MD5, SSH_FP_HEX); 439 cp = sshkey_fingerprint(k2, SSH_DIGEST_MD5, SSH_FP_HEX);
437 ASSERT_PTR_NE(cp, NULL); 440 ASSERT_PTR_NE(cp, NULL);
438 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf)); 441 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
439 sshbuf_free(buf); 442 sshbuf_free(buf);
@@ -443,7 +446,7 @@ sshkey_file_tests(void)
443 446
444 TEST_START("Ed25519 key bubblebabble fingerprint"); 447 TEST_START("Ed25519 key bubblebabble fingerprint");
445 buf = load_text_file("ed25519_1.fp.bb"); 448 buf = load_text_file("ed25519_1.fp.bb");
446 cp = sshkey_fingerprint(k1, SSH_FP_SHA1, SSH_FP_BUBBLEBABBLE); 449 cp = sshkey_fingerprint(k1, SSH_DIGEST_SHA1, SSH_FP_BUBBLEBABBLE);
447 ASSERT_PTR_NE(cp, NULL); 450 ASSERT_PTR_NE(cp, NULL);
448 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf)); 451 ASSERT_STRING_EQ(cp, (const char *)sshbuf_ptr(buf));
449 sshbuf_free(buf); 452 sshbuf_free(buf);
diff --git a/regress/unittests/sshkey/test_fuzz.c b/regress/unittests/sshkey/test_fuzz.c
index a3f61a6df..1f08a2e43 100644
--- a/regress/unittests/sshkey/test_fuzz.c
+++ b/regress/unittests/sshkey/test_fuzz.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: test_fuzz.c,v 1.1 2014/06/24 01:14:18 djm Exp $ */ 1/* $OpenBSD: test_fuzz.c,v 1.4 2015/03/04 23:22:35 djm Exp $ */
2/* 2/*
3 * Fuzz tests for key parsing 3 * Fuzz tests for key parsing
4 * 4 *
@@ -53,7 +53,7 @@ public_fuzz(struct sshkey *k)
53 struct fuzz *fuzz; 53 struct fuzz *fuzz;
54 54
55 ASSERT_PTR_NE(buf = sshbuf_new(), NULL); 55 ASSERT_PTR_NE(buf = sshbuf_new(), NULL);
56 ASSERT_INT_EQ(sshkey_to_blob_buf(k, buf), 0); 56 ASSERT_INT_EQ(sshkey_putb(k, buf), 0);
57 /* XXX need a way to run the tests in "slow, but complete" mode */ 57 /* XXX need a way to run the tests in "slow, but complete" mode */
58 fuzz = fuzz_begin(FUZZ_1_BIT_FLIP | /* XXX too slow FUZZ_2_BIT_FLIP | */ 58 fuzz = fuzz_begin(FUZZ_1_BIT_FLIP | /* XXX too slow FUZZ_2_BIT_FLIP | */
59 FUZZ_1_BYTE_FLIP | /* XXX too slow FUZZ_2_BYTE_FLIP | */ 59 FUZZ_1_BYTE_FLIP | /* XXX too slow FUZZ_2_BYTE_FLIP | */
@@ -87,8 +87,11 @@ sig_fuzz(struct sshkey *k)
87 free(sig); 87 free(sig);
88 TEST_ONERROR(onerror, fuzz); 88 TEST_ONERROR(onerror, fuzz);
89 for(; !fuzz_done(fuzz); fuzz_next(fuzz)) { 89 for(; !fuzz_done(fuzz); fuzz_next(fuzz)) {
90 sshkey_verify(k, fuzz_ptr(fuzz), fuzz_len(fuzz), 90 /* Ensure 1-bit difference at least */
91 c, sizeof(c), 0); 91 if (fuzz_matches_original(fuzz))
92 continue;
93 ASSERT_INT_NE(sshkey_verify(k, fuzz_ptr(fuzz), fuzz_len(fuzz),
94 c, sizeof(c), 0), 0);
92 } 95 }
93 fuzz_cleanup(fuzz); 96 fuzz_cleanup(fuzz);
94} 97}
@@ -101,6 +104,7 @@ sshkey_fuzz_tests(void)
101 struct fuzz *fuzz; 104 struct fuzz *fuzz;
102 int r; 105 int r;
103 106
107#ifdef WITH_SSH1
104 TEST_START("fuzz RSA1 private"); 108 TEST_START("fuzz RSA1 private");
105 buf = load_file("rsa1_1"); 109 buf = load_file("rsa1_1");
106 fuzz = fuzz_begin(FUZZ_1_BIT_FLIP | FUZZ_1_BYTE_FLIP | 110 fuzz = fuzz_begin(FUZZ_1_BIT_FLIP | FUZZ_1_BYTE_FLIP |
@@ -144,6 +148,7 @@ sshkey_fuzz_tests(void)
144 sshbuf_free(fuzzed); 148 sshbuf_free(fuzzed);
145 fuzz_cleanup(fuzz); 149 fuzz_cleanup(fuzz);
146 TEST_DONE(); 150 TEST_DONE();
151#endif
147 152
148 TEST_START("fuzz RSA private"); 153 TEST_START("fuzz RSA private");
149 buf = load_file("rsa_1"); 154 buf = load_file("rsa_1");
diff --git a/regress/unittests/sshkey/test_sshkey.c b/regress/unittests/sshkey/test_sshkey.c
index ef0c67956..ad10c9be2 100644
--- a/regress/unittests/sshkey/test_sshkey.c
+++ b/regress/unittests/sshkey/test_sshkey.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: test_sshkey.c,v 1.1 2014/06/24 01:14:18 djm Exp $ */ 1/* $OpenBSD: test_sshkey.c,v 1.3 2015/01/26 06:11:28 djm Exp $ */
2/* 2/*
3 * Regress test for sshkey.h key management API 3 * Regress test for sshkey.h key management API
4 * 4 *
@@ -19,7 +19,7 @@
19#include <openssl/bn.h> 19#include <openssl/bn.h>
20#include <openssl/rsa.h> 20#include <openssl/rsa.h>
21#include <openssl/dsa.h> 21#include <openssl/dsa.h>
22#ifdef OPENSSL_HAS_NISTP256 22#if defined(OPENSSL_HAS_ECC) && defined(OPENSSL_HAS_NISTP256)
23# include <openssl/ec.h> 23# include <openssl/ec.h>
24#endif 24#endif
25 25
@@ -37,6 +37,20 @@
37void sshkey_tests(void); 37void sshkey_tests(void);
38 38
39static void 39static void
40put_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
53static void
40build_cert(struct sshbuf *b, const struct sshkey *k, const char *type, 54build_cert(struct sshbuf *b, const struct sshkey *k, const char *type,
41 const struct sshkey *sign_key, const struct sshkey *ca_key) 55 const struct sshkey *sign_key, const struct sshkey *ca_key)
42{ 56{
@@ -45,25 +59,31 @@ build_cert(struct sshbuf *b, const struct sshkey *k, const char *type,
45 size_t siglen; 59 size_t siglen;
46 60
47 ca_buf = sshbuf_new(); 61 ca_buf = sshbuf_new();
48 ASSERT_INT_EQ(sshkey_to_blob_buf(ca_key, ca_buf), 0); 62 ASSERT_PTR_NE(ca_buf, NULL);
63 ASSERT_INT_EQ(sshkey_putb(ca_key, ca_buf), 0);
49 64
50 /* 65 /*
51 * Get the public key serialisation by rendering the key and skipping 66 * Get the public key serialisation by rendering the key and skipping
52 * the type string. This is a bit of a hack :/ 67 * the type string. This is a bit of a hack :/
53 */ 68 */
54 pk = sshbuf_new(); 69 pk = sshbuf_new();
55 ASSERT_INT_EQ(sshkey_plain_to_blob_buf(k, pk), 0); 70 ASSERT_PTR_NE(pk, NULL);
71 ASSERT_INT_EQ(sshkey_putb_plain(k, pk), 0);
56 ASSERT_INT_EQ(sshbuf_skip_string(pk), 0); 72 ASSERT_INT_EQ(sshbuf_skip_string(pk), 0);
57 73
58 principals = sshbuf_new(); 74 principals = sshbuf_new();
75 ASSERT_PTR_NE(principals, NULL);
59 ASSERT_INT_EQ(sshbuf_put_cstring(principals, "gsamsa"), 0); 76 ASSERT_INT_EQ(sshbuf_put_cstring(principals, "gsamsa"), 0);
60 ASSERT_INT_EQ(sshbuf_put_cstring(principals, "gregor"), 0); 77 ASSERT_INT_EQ(sshbuf_put_cstring(principals, "gregor"), 0);
61 78
62 critopts = sshbuf_new(); 79 critopts = sshbuf_new();
63 /* XXX fill this in */ 80 ASSERT_PTR_NE(critopts, NULL);
81 put_opt(critopts, "force-command", "/usr/local/bin/nethack");
82 put_opt(critopts, "source-address", "192.168.0.0/24,127.0.0.1,::1");
64 83
65 exts = sshbuf_new(); 84 exts = sshbuf_new();
66 /* XXX fill this in */ 85 ASSERT_PTR_NE(exts, NULL);
86 put_opt(critopts, "permit-X11-forwarding", NULL);
67 87
68 ASSERT_INT_EQ(sshbuf_put_cstring(b, type), 0); 88 ASSERT_INT_EQ(sshbuf_put_cstring(b, type), 0);
69 ASSERT_INT_EQ(sshbuf_put_cstring(b, "noncenoncenonce!"), 0); /* nonce */ 89 ASSERT_INT_EQ(sshbuf_put_cstring(b, "noncenoncenonce!"), 0); /* nonce */
@@ -90,10 +110,74 @@ build_cert(struct sshbuf *b, const struct sshkey *k, const char *type,
90 sshbuf_free(pk); 110 sshbuf_free(pk);
91} 111}
92 112
113static void
114signature_test(struct sshkey *k, struct sshkey *bad, const u_char *d, size_t l)
115{
116 size_t len;
117 u_char *sig;
118
119 ASSERT_INT_EQ(sshkey_sign(k, &sig, &len, d, l, 0), 0);
120 ASSERT_SIZE_T_GT(len, 8);
121 ASSERT_PTR_NE(sig, NULL);
122 ASSERT_INT_EQ(sshkey_verify(k, sig, len, d, l, 0), 0);
123 ASSERT_INT_NE(sshkey_verify(bad, sig, len, d, l, 0), 0);
124 /* Fuzz test is more comprehensive, this is just a smoke test */
125 sig[len - 5] ^= 0x10;
126 ASSERT_INT_NE(sshkey_verify(k, sig, len, d, l, 0), 0);
127 free(sig);
128}
129
130static void
131banana(u_char *s, size_t l)
132{
133 size_t o;
134 const u_char the_banana[] = { 'b', 'a', 'n', 'a', 'n', 'a' };
135
136 for (o = 0; o < l; o += sizeof(the_banana)) {
137 if (l - o < sizeof(the_banana)) {
138 memcpy(s + o, "nanananana", l - o);
139 break;
140 }
141 memcpy(s + o, banana, sizeof(the_banana));
142 }
143}
144
145static void
146signature_tests(struct sshkey *k, struct sshkey *bad)
147{
148 u_char i, buf[2049];
149 size_t lens[] = {
150 1, 2, 7, 8, 9, 15, 16, 17, 31, 32, 33, 127, 128, 129,
151 255, 256, 257, 1023, 1024, 1025, 2047, 2048, 2049
152 };
153
154 for (i = 0; i < (sizeof(lens)/sizeof(lens[0])); i++) {
155 test_subtest_info("%s key, banana length %zu",
156 sshkey_type(k), lens[i]);
157 banana(buf, lens[i]);
158 signature_test(k, bad, buf, lens[i]);
159 }
160}
161
162static struct sshkey *
163get_private(const char *n)
164{
165 struct sshbuf *b;
166 struct sshkey *ret;
167
168 b = load_file(n);
169 ASSERT_INT_EQ(sshkey_parse_private_fileblob(b, "", n, &ret, NULL), 0);
170 sshbuf_free(b);
171 return ret;
172}
173
93void 174void
94sshkey_tests(void) 175sshkey_tests(void)
95{ 176{
96 struct sshkey *k1, *k2, *k3, *k4, *kr, *kd, *ke, *kf; 177 struct sshkey *k1, *k2, *k3, *k4, *kr, *kd, *kf;
178#ifdef OPENSSL_HAS_ECC
179 struct sshkey *ke;
180#endif
97 struct sshbuf *b; 181 struct sshbuf *b;
98 182
99 TEST_START("new invalid"); 183 TEST_START("new invalid");
@@ -136,12 +220,14 @@ sshkey_tests(void)
136 sshkey_free(k1); 220 sshkey_free(k1);
137 TEST_DONE(); 221 TEST_DONE();
138 222
223#ifdef OPENSSL_HAS_ECC
139 TEST_START("new/free KEY_ECDSA"); 224 TEST_START("new/free KEY_ECDSA");
140 k1 = sshkey_new(KEY_ECDSA); 225 k1 = sshkey_new(KEY_ECDSA);
141 ASSERT_PTR_NE(k1, NULL); 226 ASSERT_PTR_NE(k1, NULL);
142 ASSERT_PTR_EQ(k1->ecdsa, NULL); /* Can't allocate without NID */ 227 ASSERT_PTR_EQ(k1->ecdsa, NULL); /* Can't allocate without NID */
143 sshkey_free(k1); 228 sshkey_free(k1);
144 TEST_DONE(); 229 TEST_DONE();
230#endif
145 231
146 TEST_START("new/free KEY_ED25519"); 232 TEST_START("new/free KEY_ED25519");
147 k1 = sshkey_new(KEY_ED25519); 233 k1 = sshkey_new(KEY_ED25519);
@@ -192,12 +278,14 @@ sshkey_tests(void)
192 sshkey_free(k1); 278 sshkey_free(k1);
193 TEST_DONE(); 279 TEST_DONE();
194 280
281#ifdef OPENSSL_HAS_ECC
195 TEST_START("generate KEY_ECDSA wrong bits"); 282 TEST_START("generate KEY_ECDSA wrong bits");
196 ASSERT_INT_EQ(sshkey_generate(KEY_ECDSA, 42, &k1), 283 ASSERT_INT_EQ(sshkey_generate(KEY_ECDSA, 42, &k1),
197 SSH_ERR_INVALID_ARGUMENT); 284 SSH_ERR_INVALID_ARGUMENT);
198 ASSERT_PTR_EQ(k1, NULL); 285 ASSERT_PTR_EQ(k1, NULL);
199 sshkey_free(k1); 286 sshkey_free(k1);
200 TEST_DONE(); 287 TEST_DONE();
288#endif
201 289
202 TEST_START("generate KEY_RSA"); 290 TEST_START("generate KEY_RSA");
203 ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 768, &kr), 0); 291 ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 768, &kr), 0);
@@ -332,26 +420,100 @@ sshkey_tests(void)
332#endif 420#endif
333 sshkey_free(kf); 421 sshkey_free(kf);
334 422
335/* XXX certify test */ 423 TEST_START("certify key");
336/* XXX sign test */ 424 ASSERT_INT_EQ(sshkey_load_public(test_data_file("ed25519_1.pub"),
337/* XXX verify test */ 425 &k1, NULL), 0);
426 k2 = get_private("ed25519_2");
427 ASSERT_INT_EQ(sshkey_to_certified(k1, 0), 0);
428 ASSERT_PTR_NE(k1->cert, NULL);
429 k1->cert->type = SSH2_CERT_TYPE_USER;
430 k1->cert->serial = 1234;
431 k1->cert->key_id = strdup("estragon");
432 ASSERT_PTR_NE(k1->cert->key_id, NULL);
433 k1->cert->principals = calloc(4, sizeof(*k1->cert->principals));
434 ASSERT_PTR_NE(k1->cert->principals, NULL);
435 k1->cert->principals[0] = strdup("estragon");
436 k1->cert->principals[1] = strdup("vladimir");
437 k1->cert->principals[2] = strdup("pozzo");
438 k1->cert->principals[3] = strdup("lucky");
439 ASSERT_PTR_NE(k1->cert->principals[0], NULL);
440 ASSERT_PTR_NE(k1->cert->principals[1], NULL);
441 ASSERT_PTR_NE(k1->cert->principals[2], NULL);
442 ASSERT_PTR_NE(k1->cert->principals[3], NULL);
443 k1->cert->valid_after = 0;
444 k1->cert->valid_before = (u_int64_t)-1;
445 k1->cert->critical = sshbuf_new();
446 ASSERT_PTR_NE(k1->cert->critical, NULL);
447 k1->cert->extensions = sshbuf_new();
448 ASSERT_PTR_NE(k1->cert->extensions, NULL);
449 put_opt(k1->cert->critical, "force-command", "/usr/bin/true");
450 put_opt(k1->cert->critical, "source-address", "127.0.0.1");
451 put_opt(k1->cert->extensions, "permit-X11-forwarding", NULL);
452 put_opt(k1->cert->extensions, "permit-agent-forwarding", NULL);
453 ASSERT_INT_EQ(sshkey_from_private(k2, &k1->cert->signature_key), 0);
454 ASSERT_INT_EQ(sshkey_certify(k1, k2), 0);
455 b = sshbuf_new();
456 ASSERT_PTR_NE(b, NULL);
457 ASSERT_INT_EQ(sshkey_putb(k1, b), 0);
458 ASSERT_INT_EQ(sshkey_from_blob(sshbuf_ptr(b), sshbuf_len(b), &k3), 0);
459
460 sshkey_free(k1);
461 sshkey_free(k2);
462 sshkey_free(k3);
463 sshbuf_reset(b);
464 TEST_DONE();
465
466 TEST_START("sign and verify RSA");
467 k1 = get_private("rsa_1");
468 ASSERT_INT_EQ(sshkey_load_public(test_data_file("rsa_2.pub"), &k2,
469 NULL), 0);
470 signature_tests(k1, k2);
471 sshkey_free(k1);
472 sshkey_free(k2);
473 TEST_DONE();
474
475 TEST_START("sign and verify DSA");
476 k1 = get_private("dsa_1");
477 ASSERT_INT_EQ(sshkey_load_public(test_data_file("dsa_2.pub"), &k2,
478 NULL), 0);
479 signature_tests(k1, k2);
480 sshkey_free(k1);
481 sshkey_free(k2);
482 TEST_DONE();
483
484#ifdef OPENSSL_HAS_ECC
485 TEST_START("sign and verify ECDSA");
486 k1 = get_private("ecdsa_1");
487 ASSERT_INT_EQ(sshkey_load_public(test_data_file("ecdsa_2.pub"), &k2,
488 NULL), 0);
489 signature_tests(k1, k2);
490 sshkey_free(k1);
491 sshkey_free(k2);
492 TEST_DONE();
493#endif
494
495 TEST_START("sign and verify ED25519");
496 k1 = get_private("ed25519_1");
497 ASSERT_INT_EQ(sshkey_load_public(test_data_file("ed25519_2.pub"), &k2,
498 NULL), 0);
499 signature_tests(k1, k2);
500 sshkey_free(k1);
501 sshkey_free(k2);
502 TEST_DONE();
338 503
339 TEST_START("nested certificate"); 504 TEST_START("nested certificate");
340 ASSERT_INT_EQ(sshkey_load_cert(test_data_file("rsa_1"), &k1), 0); 505 ASSERT_INT_EQ(sshkey_load_cert(test_data_file("rsa_1"), &k1), 0);
341 ASSERT_INT_EQ(sshkey_load_public(test_data_file("rsa_1.pub"), &k2, 506 ASSERT_INT_EQ(sshkey_load_public(test_data_file("rsa_1.pub"), &k2,
342 NULL), 0); 507 NULL), 0);
343 b = load_file("rsa_2"); 508 k3 = get_private("ed25519_2");
344 ASSERT_INT_EQ(sshkey_parse_private_fileblob(b, "", "rsa_1",
345 &k3, NULL), 0);
346 sshbuf_reset(b);
347 build_cert(b, k2, "ssh-rsa-cert-v01@openssh.com", k3, k1); 509 build_cert(b, k2, "ssh-rsa-cert-v01@openssh.com", k3, k1);
348 ASSERT_INT_EQ(sshkey_from_blob(sshbuf_ptr(b), sshbuf_len(b), &k4), 510 ASSERT_INT_EQ(sshkey_from_blob(sshbuf_ptr(b), sshbuf_len(b), &k4),
349 SSH_ERR_KEY_CERT_INVALID_SIGN_KEY); 511 SSH_ERR_KEY_CERT_INVALID_SIGN_KEY);
350 ASSERT_PTR_EQ(k4, NULL); 512 ASSERT_PTR_EQ(k4, NULL);
351 sshbuf_free(b);
352 sshkey_free(k1); 513 sshkey_free(k1);
353 sshkey_free(k2); 514 sshkey_free(k2);
354 sshkey_free(k3); 515 sshkey_free(k3);
516 sshbuf_free(b);
355 TEST_DONE(); 517 TEST_DONE();
356 518
357} 519}
diff --git a/regress/unittests/sshkey/testdata/dsa_1-cert.fp b/regress/unittests/sshkey/testdata/dsa_1-cert.fp
index 56ee1f89b..b26145b24 100644
--- a/regress/unittests/sshkey/testdata/dsa_1-cert.fp
+++ b/regress/unittests/sshkey/testdata/dsa_1-cert.fp
@@ -1 +1 @@
5a:4a:41:8c:4e:fa:4c:52:19:f9:39:49:31:fb:fd:74 MD5:5a:4a:41:8c:4e:fa:4c:52:19:f9:39:49:31:fb:fd:74
diff --git a/regress/unittests/sshkey/testdata/dsa_1.fp b/regress/unittests/sshkey/testdata/dsa_1.fp
index 56ee1f89b..b26145b24 100644
--- a/regress/unittests/sshkey/testdata/dsa_1.fp
+++ b/regress/unittests/sshkey/testdata/dsa_1.fp
@@ -1 +1 @@
5a:4a:41:8c:4e:fa:4c:52:19:f9:39:49:31:fb:fd:74 MD5:5a:4a:41:8c:4e:fa:4c:52:19:f9:39:49:31:fb:fd:74
diff --git a/regress/unittests/sshkey/testdata/dsa_2.fp b/regress/unittests/sshkey/testdata/dsa_2.fp
index ba9de82a8..822657403 100644
--- a/regress/unittests/sshkey/testdata/dsa_2.fp
+++ b/regress/unittests/sshkey/testdata/dsa_2.fp
@@ -1 +1 @@
72:5f:50:6b:e5:64:c5:62:21:92:3f:8b:10:9b:9f:1a MD5:72:5f:50:6b:e5:64:c5:62:21:92:3f:8b:10:9b:9f:1a
diff --git a/regress/unittests/sshkey/testdata/ecdsa_1-cert.fp b/regress/unittests/sshkey/testdata/ecdsa_1-cert.fp
index a56dbc8d0..c3d747aff 100644
--- a/regress/unittests/sshkey/testdata/ecdsa_1-cert.fp
+++ b/regress/unittests/sshkey/testdata/ecdsa_1-cert.fp
@@ -1 +1 @@
f7:be:4c:02:65:ed:4c:11:af:ab:a8:dd:0a:92:e7:44 MD5:f7:be:4c:02:65:ed:4c:11:af:ab:a8:dd:0a:92:e7:44
diff --git a/regress/unittests/sshkey/testdata/ecdsa_1.fp b/regress/unittests/sshkey/testdata/ecdsa_1.fp
index a56dbc8d0..c3d747aff 100644
--- a/regress/unittests/sshkey/testdata/ecdsa_1.fp
+++ b/regress/unittests/sshkey/testdata/ecdsa_1.fp
@@ -1 +1 @@
f7:be:4c:02:65:ed:4c:11:af:ab:a8:dd:0a:92:e7:44 MD5:f7:be:4c:02:65:ed:4c:11:af:ab:a8:dd:0a:92:e7:44
diff --git a/regress/unittests/sshkey/testdata/ecdsa_2.fp b/regress/unittests/sshkey/testdata/ecdsa_2.fp
index eb4bbdf03..fe7526b92 100644
--- a/regress/unittests/sshkey/testdata/ecdsa_2.fp
+++ b/regress/unittests/sshkey/testdata/ecdsa_2.fp
@@ -1 +1 @@
51:bd:ff:2b:6d:26:9b:90:f9:e1:4a:ca:a0:29:8e:70 MD5:51:bd:ff:2b:6d:26:9b:90:f9:e1:4a:ca:a0:29:8e:70
diff --git a/regress/unittests/sshkey/testdata/ed25519_1-cert.fp b/regress/unittests/sshkey/testdata/ed25519_1-cert.fp
index e6d23d0b8..fbde87af0 100644
--- a/regress/unittests/sshkey/testdata/ed25519_1-cert.fp
+++ b/regress/unittests/sshkey/testdata/ed25519_1-cert.fp
@@ -1 +1 @@
19:08:8e:7e:4d:e5:de:86:2a:09:47:65:eb:0a:51:2f MD5:19:08:8e:7e:4d:e5:de:86:2a:09:47:65:eb:0a:51:2f
diff --git a/regress/unittests/sshkey/testdata/ed25519_1.fp b/regress/unittests/sshkey/testdata/ed25519_1.fp
index e6d23d0b8..fbde87af0 100644
--- a/regress/unittests/sshkey/testdata/ed25519_1.fp
+++ b/regress/unittests/sshkey/testdata/ed25519_1.fp
@@ -1 +1 @@
19:08:8e:7e:4d:e5:de:86:2a:09:47:65:eb:0a:51:2f MD5:19:08:8e:7e:4d:e5:de:86:2a:09:47:65:eb:0a:51:2f
diff --git a/regress/unittests/sshkey/testdata/ed25519_2.fp b/regress/unittests/sshkey/testdata/ed25519_2.fp
index 02c684f36..ec1cdbb94 100644
--- a/regress/unittests/sshkey/testdata/ed25519_2.fp
+++ b/regress/unittests/sshkey/testdata/ed25519_2.fp
@@ -1 +1 @@
5c:c9:ae:a3:0c:aa:28:29:b8:fc:7c:64:ba:6e:e9:c9 MD5:5c:c9:ae:a3:0c:aa:28:29:b8:fc:7c:64:ba:6e:e9:c9
diff --git a/regress/unittests/sshkey/testdata/rsa1_1.fp b/regress/unittests/sshkey/testdata/rsa1_1.fp
index 782ece0db..2e1068c64 100644
--- a/regress/unittests/sshkey/testdata/rsa1_1.fp
+++ b/regress/unittests/sshkey/testdata/rsa1_1.fp
@@ -1 +1 @@
a8:82:9b:98:c5:e6:19:d6:83:39:9f:4d:3a:8f:7c:80 MD5:a8:82:9b:98:c5:e6:19:d6:83:39:9f:4d:3a:8f:7c:80
diff --git a/regress/unittests/sshkey/testdata/rsa1_2.fp b/regress/unittests/sshkey/testdata/rsa1_2.fp
index c3325371d..cd0039306 100644
--- a/regress/unittests/sshkey/testdata/rsa1_2.fp
+++ b/regress/unittests/sshkey/testdata/rsa1_2.fp
@@ -1 +1 @@
c0:83:1c:97:5f:32:77:7e:e4:e3:e9:29:b9:eb:76:9c MD5:c0:83:1c:97:5f:32:77:7e:e4:e3:e9:29:b9:eb:76:9c
diff --git a/regress/unittests/sshkey/testdata/rsa_1-cert.fp b/regress/unittests/sshkey/testdata/rsa_1-cert.fp
index bf9c2e362..1cf780dd9 100644
--- a/regress/unittests/sshkey/testdata/rsa_1-cert.fp
+++ b/regress/unittests/sshkey/testdata/rsa_1-cert.fp
@@ -1 +1 @@
be:27:4c:16:27:f5:04:03:62:a8:b7:91:df:a5:b1:3b MD5:be:27:4c:16:27:f5:04:03:62:a8:b7:91:df:a5:b1:3b
diff --git a/regress/unittests/sshkey/testdata/rsa_1.fp b/regress/unittests/sshkey/testdata/rsa_1.fp
index bf9c2e362..1cf780dd9 100644
--- a/regress/unittests/sshkey/testdata/rsa_1.fp
+++ b/regress/unittests/sshkey/testdata/rsa_1.fp
@@ -1 +1 @@
be:27:4c:16:27:f5:04:03:62:a8:b7:91:df:a5:b1:3b MD5:be:27:4c:16:27:f5:04:03:62:a8:b7:91:df:a5:b1:3b
diff --git a/regress/unittests/sshkey/testdata/rsa_2.fp b/regress/unittests/sshkey/testdata/rsa_2.fp
index 53939f413..8d4367610 100644
--- a/regress/unittests/sshkey/testdata/rsa_2.fp
+++ b/regress/unittests/sshkey/testdata/rsa_2.fp
@@ -1 +1 @@
fb:8f:7b:26:3d:42:40:ef:ed:f1:ed:ee:66:9e:ba:b0 MD5:fb:8f:7b:26:3d:42:40:ef:ed:f1:ed:ee:66:9e:ba:b0