summaryrefslogtreecommitdiff
path: root/regress/unittests/sshkey/test_sshkey.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2014-07-02 17:38:31 +1000
committerDamien Miller <djm@mindrot.org>2014-07-02 17:38:31 +1000
commitdd8b1dd7933eb6f5652641b0cdced34a387f2e80 (patch)
tree8c5422101cb6991722ed617d068c1d540847f41d /regress/unittests/sshkey/test_sshkey.c
parentc1dc24b71f087f385b92652b9673f52af64e0428 (diff)
- djm@cvs.openbsd.org 2014/06/24 01:14:17
[Makefile.in regress/Makefile regress/unittests/Makefile] [regress/unittests/sshkey/Makefile] [regress/unittests/sshkey/common.c] [regress/unittests/sshkey/common.h] [regress/unittests/sshkey/mktestdata.sh] [regress/unittests/sshkey/test_file.c] [regress/unittests/sshkey/test_fuzz.c] [regress/unittests/sshkey/test_sshkey.c] [regress/unittests/sshkey/tests.c] [regress/unittests/sshkey/testdata/dsa_1] [regress/unittests/sshkey/testdata/dsa_1-cert.fp] [regress/unittests/sshkey/testdata/dsa_1-cert.pub] [regress/unittests/sshkey/testdata/dsa_1.fp] [regress/unittests/sshkey/testdata/dsa_1.fp.bb] [regress/unittests/sshkey/testdata/dsa_1.param.g] [regress/unittests/sshkey/testdata/dsa_1.param.priv] [regress/unittests/sshkey/testdata/dsa_1.param.pub] [regress/unittests/sshkey/testdata/dsa_1.pub] [regress/unittests/sshkey/testdata/dsa_1_pw] [regress/unittests/sshkey/testdata/dsa_2] [regress/unittests/sshkey/testdata/dsa_2.fp] [regress/unittests/sshkey/testdata/dsa_2.fp.bb] [regress/unittests/sshkey/testdata/dsa_2.pub] [regress/unittests/sshkey/testdata/dsa_n] [regress/unittests/sshkey/testdata/dsa_n_pw] [regress/unittests/sshkey/testdata/ecdsa_1] [regress/unittests/sshkey/testdata/ecdsa_1-cert.fp] [regress/unittests/sshkey/testdata/ecdsa_1-cert.pub] [regress/unittests/sshkey/testdata/ecdsa_1.fp] [regress/unittests/sshkey/testdata/ecdsa_1.fp.bb] [regress/unittests/sshkey/testdata/ecdsa_1.param.curve] [regress/unittests/sshkey/testdata/ecdsa_1.param.priv] [regress/unittests/sshkey/testdata/ecdsa_1.param.pub] [regress/unittests/sshkey/testdata/ecdsa_1.pub] [regress/unittests/sshkey/testdata/ecdsa_1_pw] [regress/unittests/sshkey/testdata/ecdsa_2] [regress/unittests/sshkey/testdata/ecdsa_2.fp] [regress/unittests/sshkey/testdata/ecdsa_2.fp.bb] [regress/unittests/sshkey/testdata/ecdsa_2.param.curve] [regress/unittests/sshkey/testdata/ecdsa_2.param.priv] [regress/unittests/sshkey/testdata/ecdsa_2.param.pub] [regress/unittests/sshkey/testdata/ecdsa_2.pub] [regress/unittests/sshkey/testdata/ecdsa_n] [regress/unittests/sshkey/testdata/ecdsa_n_pw] [regress/unittests/sshkey/testdata/ed25519_1] [regress/unittests/sshkey/testdata/ed25519_1-cert.fp] [regress/unittests/sshkey/testdata/ed25519_1-cert.pub] [regress/unittests/sshkey/testdata/ed25519_1.fp] [regress/unittests/sshkey/testdata/ed25519_1.fp.bb] [regress/unittests/sshkey/testdata/ed25519_1.pub] [regress/unittests/sshkey/testdata/ed25519_1_pw] [regress/unittests/sshkey/testdata/ed25519_2] [regress/unittests/sshkey/testdata/ed25519_2.fp] [regress/unittests/sshkey/testdata/ed25519_2.fp.bb] [regress/unittests/sshkey/testdata/ed25519_2.pub] [regress/unittests/sshkey/testdata/pw] [regress/unittests/sshkey/testdata/rsa1_1] [regress/unittests/sshkey/testdata/rsa1_1.fp] [regress/unittests/sshkey/testdata/rsa1_1.fp.bb] [regress/unittests/sshkey/testdata/rsa1_1.param.n] [regress/unittests/sshkey/testdata/rsa1_1.pub] [regress/unittests/sshkey/testdata/rsa1_1_pw] [regress/unittests/sshkey/testdata/rsa1_2] [regress/unittests/sshkey/testdata/rsa1_2.fp] [regress/unittests/sshkey/testdata/rsa1_2.fp.bb] [regress/unittests/sshkey/testdata/rsa1_2.param.n] [regress/unittests/sshkey/testdata/rsa1_2.pub] [regress/unittests/sshkey/testdata/rsa_1] [regress/unittests/sshkey/testdata/rsa_1-cert.fp] [regress/unittests/sshkey/testdata/rsa_1-cert.pub] [regress/unittests/sshkey/testdata/rsa_1.fp] [regress/unittests/sshkey/testdata/rsa_1.fp.bb] [regress/unittests/sshkey/testdata/rsa_1.param.n] [regress/unittests/sshkey/testdata/rsa_1.param.p] [regress/unittests/sshkey/testdata/rsa_1.param.q] [regress/unittests/sshkey/testdata/rsa_1.pub] [regress/unittests/sshkey/testdata/rsa_1_pw] [regress/unittests/sshkey/testdata/rsa_2] [regress/unittests/sshkey/testdata/rsa_2.fp] [regress/unittests/sshkey/testdata/rsa_2.fp.bb] [regress/unittests/sshkey/testdata/rsa_2.param.n] [regress/unittests/sshkey/testdata/rsa_2.param.p] [regress/unittests/sshkey/testdata/rsa_2.param.q] [regress/unittests/sshkey/testdata/rsa_2.pub] [regress/unittests/sshkey/testdata/rsa_n] [regress/unittests/sshkey/testdata/rsa_n_pw] unit and fuzz tests for new key API
Diffstat (limited to 'regress/unittests/sshkey/test_sshkey.c')
-rw-r--r--regress/unittests/sshkey/test_sshkey.c343
1 files changed, 343 insertions, 0 deletions
diff --git a/regress/unittests/sshkey/test_sshkey.c b/regress/unittests/sshkey/test_sshkey.c
new file mode 100644
index 000000000..2d69b4d0d
--- /dev/null
+++ b/regress/unittests/sshkey/test_sshkey.c
@@ -0,0 +1,343 @@
1/* $OpenBSD: test_sshkey.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 <stdio.h>
13#include <stdint.h>
14#include <stdlib.h>
15#include <string.h>
16
17#include <openssl/bn.h>
18#include <openssl/ec.h>
19#include <openssl/rsa.h>
20#include <openssl/dsa.h>
21
22#include "../test_helper/test_helper.h"
23
24#include "ssherr.h"
25#include "sshbuf.h"
26#define SSHBUF_INTERNAL 1 /* access internals for testing */
27#include "sshkey.h"
28
29#include "authfile.h"
30#include "common.h"
31#include "ssh2.h"
32
33void sshkey_tests(void);
34
35static void
36build_cert(struct sshbuf *b, const struct sshkey *k, const char *type,
37 const struct sshkey *sign_key, const struct sshkey *ca_key)
38{
39 struct sshbuf *ca_buf, *pk, *principals, *critopts, *exts;
40 u_char *sigblob;
41 size_t siglen;
42
43 ca_buf = sshbuf_new();
44 ASSERT_INT_EQ(sshkey_to_blob_buf(ca_key, ca_buf), 0);
45
46 /*
47 * Get the public key serialisation by rendering the key and skipping
48 * the type string. This is a bit of a hack :/
49 */
50 pk = sshbuf_new();
51 ASSERT_INT_EQ(sshkey_plain_to_blob_buf(k, pk), 0);
52 ASSERT_INT_EQ(sshbuf_skip_string(pk), 0);
53
54 principals = sshbuf_new();
55 ASSERT_INT_EQ(sshbuf_put_cstring(principals, "gsamsa"), 0);
56 ASSERT_INT_EQ(sshbuf_put_cstring(principals, "gregor"), 0);
57
58 critopts = sshbuf_new();
59 /* XXX fill this in */
60
61 exts = sshbuf_new();
62 /* XXX fill this in */
63
64 ASSERT_INT_EQ(sshbuf_put_cstring(b, type), 0);
65 ASSERT_INT_EQ(sshbuf_put_cstring(b, "noncenoncenonce!"), 0); /* nonce */
66 ASSERT_INT_EQ(sshbuf_putb(b, pk), 0); /* public key serialisation */
67 ASSERT_INT_EQ(sshbuf_put_u64(b, 1234), 0); /* serial */
68 ASSERT_INT_EQ(sshbuf_put_u32(b, SSH2_CERT_TYPE_USER), 0); /* type */
69 ASSERT_INT_EQ(sshbuf_put_cstring(b, "gregor"), 0); /* key ID */
70 ASSERT_INT_EQ(sshbuf_put_stringb(b, principals), 0); /* principals */
71 ASSERT_INT_EQ(sshbuf_put_u64(b, 0), 0); /* start */
72 ASSERT_INT_EQ(sshbuf_put_u64(b, 0xffffffffffffffffULL), 0); /* end */
73 ASSERT_INT_EQ(sshbuf_put_stringb(b, critopts), 0); /* options */
74 ASSERT_INT_EQ(sshbuf_put_stringb(b, exts), 0); /* extensions */
75 ASSERT_INT_EQ(sshbuf_put_string(b, NULL, 0), 0); /* reserved */
76 ASSERT_INT_EQ(sshbuf_put_stringb(b, ca_buf), 0); /* signature key */
77 ASSERT_INT_EQ(sshkey_sign(sign_key, &sigblob, &siglen,
78 sshbuf_ptr(b), sshbuf_len(b), 0), 0);
79 ASSERT_INT_EQ(sshbuf_put_string(b, sigblob, siglen), 0); /* signature */
80
81 free(sigblob);
82 sshbuf_free(ca_buf);
83 sshbuf_free(exts);
84 sshbuf_free(critopts);
85 sshbuf_free(principals);
86 sshbuf_free(pk);
87}
88
89void
90sshkey_tests(void)
91{
92 struct sshkey *k1, *k2, *k3, *k4, *kr, *kd, *ke, *kf;
93 struct sshbuf *b;
94
95 TEST_START("new invalid");
96 k1 = sshkey_new(-42);
97 ASSERT_PTR_EQ(k1, NULL);
98 TEST_DONE();
99
100 TEST_START("new/free KEY_UNSPEC");
101 k1 = sshkey_new(KEY_UNSPEC);
102 ASSERT_PTR_NE(k1, NULL);
103 sshkey_free(k1);
104 TEST_DONE();
105
106 TEST_START("new/free KEY_RSA1");
107 k1 = sshkey_new(KEY_RSA1);
108 ASSERT_PTR_NE(k1, NULL);
109 ASSERT_PTR_NE(k1->rsa, NULL);
110 ASSERT_PTR_NE(k1->rsa->n, NULL);
111 ASSERT_PTR_NE(k1->rsa->e, NULL);
112 ASSERT_PTR_EQ(k1->rsa->p, NULL);
113 sshkey_free(k1);
114 TEST_DONE();
115
116 TEST_START("new/free KEY_RSA");
117 k1 = sshkey_new(KEY_RSA);
118 ASSERT_PTR_NE(k1, NULL);
119 ASSERT_PTR_NE(k1->rsa, NULL);
120 ASSERT_PTR_NE(k1->rsa->n, NULL);
121 ASSERT_PTR_NE(k1->rsa->e, NULL);
122 ASSERT_PTR_EQ(k1->rsa->p, NULL);
123 sshkey_free(k1);
124 TEST_DONE();
125
126 TEST_START("new/free KEY_DSA");
127 k1 = sshkey_new(KEY_DSA);
128 ASSERT_PTR_NE(k1, NULL);
129 ASSERT_PTR_NE(k1->dsa, NULL);
130 ASSERT_PTR_NE(k1->dsa->g, NULL);
131 ASSERT_PTR_EQ(k1->dsa->priv_key, NULL);
132 sshkey_free(k1);
133 TEST_DONE();
134
135 TEST_START("new/free KEY_ECDSA");
136 k1 = sshkey_new(KEY_ECDSA);
137 ASSERT_PTR_NE(k1, NULL);
138 ASSERT_PTR_EQ(k1->ecdsa, NULL); /* Can't allocate without NID */
139 sshkey_free(k1);
140 TEST_DONE();
141
142 TEST_START("new/free KEY_ED25519");
143 k1 = sshkey_new(KEY_ED25519);
144 ASSERT_PTR_NE(k1, NULL);
145 /* These should be blank until key loaded or generated */
146 ASSERT_PTR_EQ(k1->ed25519_sk, NULL);
147 ASSERT_PTR_EQ(k1->ed25519_pk, NULL);
148 sshkey_free(k1);
149 TEST_DONE();
150
151 TEST_START("new_private KEY_RSA");
152 k1 = sshkey_new_private(KEY_RSA);
153 ASSERT_PTR_NE(k1, NULL);
154 ASSERT_PTR_NE(k1->rsa, NULL);
155 ASSERT_PTR_NE(k1->rsa->n, NULL);
156 ASSERT_PTR_NE(k1->rsa->e, NULL);
157 ASSERT_PTR_NE(k1->rsa->p, NULL);
158 ASSERT_INT_EQ(sshkey_add_private(k1), 0);
159 sshkey_free(k1);
160 TEST_DONE();
161
162 TEST_START("new_private KEY_DSA");
163 k1 = sshkey_new_private(KEY_DSA);
164 ASSERT_PTR_NE(k1, NULL);
165 ASSERT_PTR_NE(k1->dsa, NULL);
166 ASSERT_PTR_NE(k1->dsa->g, NULL);
167 ASSERT_PTR_NE(k1->dsa->priv_key, NULL);
168 ASSERT_INT_EQ(sshkey_add_private(k1), 0);
169 sshkey_free(k1);
170 TEST_DONE();
171
172 TEST_START("generate KEY_RSA too small modulus");
173 ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 128, &k1),
174 SSH_ERR_INVALID_ARGUMENT);
175 ASSERT_PTR_EQ(k1, NULL);
176 TEST_DONE();
177
178 TEST_START("generate KEY_RSA too large modulus");
179 ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 1 << 20, &k1),
180 SSH_ERR_INVALID_ARGUMENT);
181 ASSERT_PTR_EQ(k1, NULL);
182 TEST_DONE();
183
184 TEST_START("generate KEY_DSA wrong bits");
185 ASSERT_INT_EQ(sshkey_generate(KEY_DSA, 2048, &k1),
186 SSH_ERR_INVALID_ARGUMENT);
187 ASSERT_PTR_EQ(k1, NULL);
188 sshkey_free(k1);
189 TEST_DONE();
190
191 TEST_START("generate KEY_ECDSA wrong bits");
192 ASSERT_INT_EQ(sshkey_generate(KEY_ECDSA, 42, &k1),
193 SSH_ERR_INVALID_ARGUMENT);
194 ASSERT_PTR_EQ(k1, NULL);
195 sshkey_free(k1);
196 TEST_DONE();
197
198 TEST_START("generate KEY_RSA");
199 ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 768, &kr), 0);
200 ASSERT_PTR_NE(kr, NULL);
201 ASSERT_PTR_NE(kr->rsa, NULL);
202 ASSERT_PTR_NE(kr->rsa->n, NULL);
203 ASSERT_PTR_NE(kr->rsa->e, NULL);
204 ASSERT_PTR_NE(kr->rsa->p, NULL);
205 ASSERT_INT_EQ(BN_num_bits(kr->rsa->n), 768);
206 TEST_DONE();
207
208 TEST_START("generate KEY_DSA");
209 ASSERT_INT_EQ(sshkey_generate(KEY_DSA, 1024, &kd), 0);
210 ASSERT_PTR_NE(kd, NULL);
211 ASSERT_PTR_NE(kd->dsa, NULL);
212 ASSERT_PTR_NE(kd->dsa->g, NULL);
213 ASSERT_PTR_NE(kd->dsa->priv_key, NULL);
214 TEST_DONE();
215
216 TEST_START("generate KEY_ECDSA");
217 ASSERT_INT_EQ(sshkey_generate(KEY_ECDSA, 256, &ke), 0);
218 ASSERT_PTR_NE(ke, NULL);
219 ASSERT_PTR_NE(ke->ecdsa, NULL);
220 ASSERT_PTR_NE(EC_KEY_get0_public_key(ke->ecdsa), NULL);
221 ASSERT_PTR_NE(EC_KEY_get0_private_key(ke->ecdsa), NULL);
222 TEST_DONE();
223
224 TEST_START("generate KEY_ED25519");
225 ASSERT_INT_EQ(sshkey_generate(KEY_ED25519, 256, &kf), 0);
226 ASSERT_PTR_NE(kf, NULL);
227 ASSERT_INT_EQ(kf->type, KEY_ED25519);
228 ASSERT_PTR_NE(kf->ed25519_pk, NULL);
229 ASSERT_PTR_NE(kf->ed25519_sk, NULL);
230 TEST_DONE();
231
232 TEST_START("demote KEY_RSA");
233 ASSERT_INT_EQ(sshkey_demote(kr, &k1), 0);
234 ASSERT_PTR_NE(k1, NULL);
235 ASSERT_PTR_NE(kr, k1);
236 ASSERT_INT_EQ(k1->type, KEY_RSA);
237 ASSERT_PTR_NE(k1->rsa, NULL);
238 ASSERT_PTR_NE(k1->rsa->n, NULL);
239 ASSERT_PTR_NE(k1->rsa->e, NULL);
240 ASSERT_PTR_EQ(k1->rsa->p, NULL);
241 TEST_DONE();
242
243 TEST_START("equal KEY_RSA/demoted KEY_RSA");
244 ASSERT_INT_EQ(sshkey_equal(kr, k1), 1);
245 sshkey_free(k1);
246 TEST_DONE();
247
248 TEST_START("demote KEY_DSA");
249 ASSERT_INT_EQ(sshkey_demote(kd, &k1), 0);
250 ASSERT_PTR_NE(k1, NULL);
251 ASSERT_PTR_NE(kd, k1);
252 ASSERT_INT_EQ(k1->type, KEY_DSA);
253 ASSERT_PTR_NE(k1->dsa, NULL);
254 ASSERT_PTR_NE(k1->dsa->g, NULL);
255 ASSERT_PTR_EQ(k1->dsa->priv_key, NULL);
256 TEST_DONE();
257
258 TEST_START("equal KEY_DSA/demoted KEY_DSA");
259 ASSERT_INT_EQ(sshkey_equal(kd, k1), 1);
260 sshkey_free(k1);
261 TEST_DONE();
262
263 TEST_START("demote KEY_ECDSA");
264 ASSERT_INT_EQ(sshkey_demote(ke, &k1), 0);
265 ASSERT_PTR_NE(k1, NULL);
266 ASSERT_PTR_NE(ke, k1);
267 ASSERT_INT_EQ(k1->type, KEY_ECDSA);
268 ASSERT_PTR_NE(k1->ecdsa, NULL);
269 ASSERT_INT_EQ(k1->ecdsa_nid, ke->ecdsa_nid);
270 ASSERT_PTR_NE(EC_KEY_get0_public_key(ke->ecdsa), NULL);
271 ASSERT_PTR_EQ(EC_KEY_get0_private_key(k1->ecdsa), NULL);
272 TEST_DONE();
273
274 TEST_START("equal KEY_ECDSA/demoted KEY_ECDSA");
275 ASSERT_INT_EQ(sshkey_equal(ke, k1), 1);
276 sshkey_free(k1);
277 TEST_DONE();
278
279 TEST_START("demote KEY_ED25519");
280 ASSERT_INT_EQ(sshkey_demote(kf, &k1), 0);
281 ASSERT_PTR_NE(k1, NULL);
282 ASSERT_PTR_NE(kf, k1);
283 ASSERT_INT_EQ(k1->type, KEY_ED25519);
284 ASSERT_PTR_NE(k1->ed25519_pk, NULL);
285 ASSERT_PTR_EQ(k1->ed25519_sk, NULL);
286 TEST_DONE();
287
288 TEST_START("equal KEY_ED25519/demoted KEY_ED25519");
289 ASSERT_INT_EQ(sshkey_equal(kf, k1), 1);
290 sshkey_free(k1);
291 TEST_DONE();
292
293 TEST_START("equal mismatched key types");
294 ASSERT_INT_EQ(sshkey_equal(kd, kr), 0);
295 ASSERT_INT_EQ(sshkey_equal(kd, ke), 0);
296 ASSERT_INT_EQ(sshkey_equal(kr, ke), 0);
297 ASSERT_INT_EQ(sshkey_equal(ke, kf), 0);
298 ASSERT_INT_EQ(sshkey_equal(kd, kf), 0);
299 TEST_DONE();
300
301 TEST_START("equal different keys");
302 ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 768, &k1), 0);
303 ASSERT_INT_EQ(sshkey_equal(kr, k1), 0);
304 sshkey_free(k1);
305 ASSERT_INT_EQ(sshkey_generate(KEY_DSA, 1024, &k1), 0);
306 ASSERT_INT_EQ(sshkey_equal(kd, k1), 0);
307 sshkey_free(k1);
308 ASSERT_INT_EQ(sshkey_generate(KEY_ECDSA, 256, &k1), 0);
309 ASSERT_INT_EQ(sshkey_equal(ke, k1), 0);
310 sshkey_free(k1);
311 ASSERT_INT_EQ(sshkey_generate(KEY_ED25519, 256, &k1), 0);
312 ASSERT_INT_EQ(sshkey_equal(kf, k1), 0);
313 sshkey_free(k1);
314 TEST_DONE();
315
316 sshkey_free(kr);
317 sshkey_free(kd);
318 sshkey_free(ke);
319 sshkey_free(kf);
320
321/* XXX certify test */
322/* XXX sign test */
323/* XXX verify test */
324
325 TEST_START("nested certificate");
326 ASSERT_INT_EQ(sshkey_load_cert(test_data_file("rsa_1"), &k1), 0);
327 ASSERT_INT_EQ(sshkey_load_public(test_data_file("rsa_1.pub"), &k2,
328 NULL), 0);
329 b = load_file("rsa_2");
330 ASSERT_INT_EQ(sshkey_parse_private_fileblob(b, "", "rsa_1",
331 &k3, NULL), 0);
332 sshbuf_reset(b);
333 build_cert(b, k2, "ssh-rsa-cert-v01@openssh.com", k3, k1);
334 ASSERT_INT_EQ(sshkey_from_blob(sshbuf_ptr(b), sshbuf_len(b), &k4),
335 SSH_ERR_KEY_CERT_INVALID_SIGN_KEY);
336 ASSERT_PTR_EQ(k4, NULL);
337 sshbuf_free(b);
338 sshkey_free(k1);
339 sshkey_free(k2);
340 sshkey_free(k3);
341 TEST_DONE();
342
343}