summaryrefslogtreecommitdiff
path: root/regress/unittests/sshkey
diff options
context:
space:
mode:
Diffstat (limited to 'regress/unittests/sshkey')
-rw-r--r--regress/unittests/sshkey/test_sshkey.c105
1 files changed, 67 insertions, 38 deletions
diff --git a/regress/unittests/sshkey/test_sshkey.c b/regress/unittests/sshkey/test_sshkey.c
index 7e03b7e52..8e35f4417 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.15 2018/09/12 01:22:43 djm Exp $ */ 1/* $OpenBSD: test_sshkey.c,v 1.16 2018/09/13 05:06:51 djm Exp $ */
2/* 2/*
3 * Regress test for sshkey.h key management API 3 * Regress test for sshkey.h key management API
4 * 4 *
@@ -173,6 +173,61 @@ get_private(const char *n)
173 return ret; 173 return ret;
174} 174}
175 175
176static const BIGNUM *
177rsa_n(struct sshkey *k)
178{
179 const BIGNUM *n = NULL;
180
181 ASSERT_PTR_NE(k, NULL);
182 ASSERT_PTR_NE(k->rsa, NULL);
183 RSA_get0_key(k->rsa, &n, NULL, NULL);
184 return n;
185}
186
187static const BIGNUM *
188rsa_e(struct sshkey *k)
189{
190 const BIGNUM *e = NULL;
191
192 ASSERT_PTR_NE(k, NULL);
193 ASSERT_PTR_NE(k->rsa, NULL);
194 RSA_get0_key(k->rsa, NULL, &e, NULL);
195 return e;
196}
197
198static const BIGNUM *
199rsa_p(struct sshkey *k)
200{
201 const BIGNUM *p = NULL;
202
203 ASSERT_PTR_NE(k, NULL);
204 ASSERT_PTR_NE(k->rsa, NULL);
205 RSA_get0_factors(k->rsa, &p, NULL);
206 return p;
207}
208
209static const BIGNUM *
210dsa_g(struct sshkey *k)
211{
212 const BIGNUM *g = NULL;
213
214 ASSERT_PTR_NE(k, NULL);
215 ASSERT_PTR_NE(k->dsa, NULL);
216 DSA_get0_pqg(k->dsa, NULL, NULL, &g);
217 return g;
218}
219
220static const BIGNUM *
221dsa_priv_key(struct sshkey *k)
222{
223 const BIGNUM *priv_key = NULL;
224
225 ASSERT_PTR_NE(k, NULL);
226 ASSERT_PTR_NE(k->dsa, NULL);
227 DSA_get0_key(k->dsa, NULL, &priv_key);
228 return priv_key;
229}
230
176void 231void
177sshkey_tests(void) 232sshkey_tests(void)
178{ 233{
@@ -197,9 +252,6 @@ sshkey_tests(void)
197 k1 = sshkey_new(KEY_RSA); 252 k1 = sshkey_new(KEY_RSA);
198 ASSERT_PTR_NE(k1, NULL); 253 ASSERT_PTR_NE(k1, NULL);
199 ASSERT_PTR_NE(k1->rsa, NULL); 254 ASSERT_PTR_NE(k1->rsa, NULL);
200 ASSERT_PTR_NE(k1->rsa->n, NULL);
201 ASSERT_PTR_NE(k1->rsa->e, NULL);
202 ASSERT_PTR_EQ(k1->rsa->p, NULL);
203 sshkey_free(k1); 255 sshkey_free(k1);
204 TEST_DONE(); 256 TEST_DONE();
205 257
@@ -207,8 +259,6 @@ sshkey_tests(void)
207 k1 = sshkey_new(KEY_DSA); 259 k1 = sshkey_new(KEY_DSA);
208 ASSERT_PTR_NE(k1, NULL); 260 ASSERT_PTR_NE(k1, NULL);
209 ASSERT_PTR_NE(k1->dsa, NULL); 261 ASSERT_PTR_NE(k1->dsa, NULL);
210 ASSERT_PTR_NE(k1->dsa->g, NULL);
211 ASSERT_PTR_EQ(k1->dsa->priv_key, NULL);
212 sshkey_free(k1); 262 sshkey_free(k1);
213 TEST_DONE(); 263 TEST_DONE();
214 264
@@ -230,27 +280,6 @@ sshkey_tests(void)
230 sshkey_free(k1); 280 sshkey_free(k1);
231 TEST_DONE(); 281 TEST_DONE();
232 282
233 TEST_START("new_private KEY_RSA");
234 k1 = sshkey_new_private(KEY_RSA);
235 ASSERT_PTR_NE(k1, NULL);
236 ASSERT_PTR_NE(k1->rsa, NULL);
237 ASSERT_PTR_NE(k1->rsa->n, NULL);
238 ASSERT_PTR_NE(k1->rsa->e, NULL);
239 ASSERT_PTR_NE(k1->rsa->p, NULL);
240 ASSERT_INT_EQ(sshkey_add_private(k1), 0);
241 sshkey_free(k1);
242 TEST_DONE();
243
244 TEST_START("new_private KEY_DSA");
245 k1 = sshkey_new_private(KEY_DSA);
246 ASSERT_PTR_NE(k1, NULL);
247 ASSERT_PTR_NE(k1->dsa, NULL);
248 ASSERT_PTR_NE(k1->dsa->g, NULL);
249 ASSERT_PTR_NE(k1->dsa->priv_key, NULL);
250 ASSERT_INT_EQ(sshkey_add_private(k1), 0);
251 sshkey_free(k1);
252 TEST_DONE();
253
254 TEST_START("generate KEY_RSA too small modulus"); 283 TEST_START("generate KEY_RSA too small modulus");
255 ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 128, &k1), 284 ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 128, &k1),
256 SSH_ERR_KEY_LENGTH); 285 SSH_ERR_KEY_LENGTH);
@@ -285,18 +314,18 @@ sshkey_tests(void)
285 ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 1024, &kr), 0); 314 ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 1024, &kr), 0);
286 ASSERT_PTR_NE(kr, NULL); 315 ASSERT_PTR_NE(kr, NULL);
287 ASSERT_PTR_NE(kr->rsa, NULL); 316 ASSERT_PTR_NE(kr->rsa, NULL);
288 ASSERT_PTR_NE(kr->rsa->n, NULL); 317 ASSERT_PTR_NE(rsa_n(kr), NULL);
289 ASSERT_PTR_NE(kr->rsa->e, NULL); 318 ASSERT_PTR_NE(rsa_e(kr), NULL);
290 ASSERT_PTR_NE(kr->rsa->p, NULL); 319 ASSERT_PTR_NE(rsa_p(kr), NULL);
291 ASSERT_INT_EQ(BN_num_bits(kr->rsa->n), 1024); 320 ASSERT_INT_EQ(BN_num_bits(rsa_n(kr)), 1024);
292 TEST_DONE(); 321 TEST_DONE();
293 322
294 TEST_START("generate KEY_DSA"); 323 TEST_START("generate KEY_DSA");
295 ASSERT_INT_EQ(sshkey_generate(KEY_DSA, 1024, &kd), 0); 324 ASSERT_INT_EQ(sshkey_generate(KEY_DSA, 1024, &kd), 0);
296 ASSERT_PTR_NE(kd, NULL); 325 ASSERT_PTR_NE(kd, NULL);
297 ASSERT_PTR_NE(kd->dsa, NULL); 326 ASSERT_PTR_NE(kd->dsa, NULL);
298 ASSERT_PTR_NE(kd->dsa->g, NULL); 327 ASSERT_PTR_NE(dsa_g(kd), NULL);
299 ASSERT_PTR_NE(kd->dsa->priv_key, NULL); 328 ASSERT_PTR_NE(dsa_priv_key(kd), NULL);
300 TEST_DONE(); 329 TEST_DONE();
301 330
302#ifdef OPENSSL_HAS_ECC 331#ifdef OPENSSL_HAS_ECC
@@ -323,9 +352,9 @@ sshkey_tests(void)
323 ASSERT_PTR_NE(kr, k1); 352 ASSERT_PTR_NE(kr, k1);
324 ASSERT_INT_EQ(k1->type, KEY_RSA); 353 ASSERT_INT_EQ(k1->type, KEY_RSA);
325 ASSERT_PTR_NE(k1->rsa, NULL); 354 ASSERT_PTR_NE(k1->rsa, NULL);
326 ASSERT_PTR_NE(k1->rsa->n, NULL); 355 ASSERT_PTR_NE(rsa_n(k1), NULL);
327 ASSERT_PTR_NE(k1->rsa->e, NULL); 356 ASSERT_PTR_NE(rsa_e(k1), NULL);
328 ASSERT_PTR_EQ(k1->rsa->p, NULL); 357 ASSERT_PTR_EQ(rsa_p(k1), NULL);
329 TEST_DONE(); 358 TEST_DONE();
330 359
331 TEST_START("equal KEY_RSA/demoted KEY_RSA"); 360 TEST_START("equal KEY_RSA/demoted KEY_RSA");
@@ -339,8 +368,8 @@ sshkey_tests(void)
339 ASSERT_PTR_NE(kd, k1); 368 ASSERT_PTR_NE(kd, k1);
340 ASSERT_INT_EQ(k1->type, KEY_DSA); 369 ASSERT_INT_EQ(k1->type, KEY_DSA);
341 ASSERT_PTR_NE(k1->dsa, NULL); 370 ASSERT_PTR_NE(k1->dsa, NULL);
342 ASSERT_PTR_NE(k1->dsa->g, NULL); 371 ASSERT_PTR_NE(dsa_g(k1), NULL);
343 ASSERT_PTR_EQ(k1->dsa->priv_key, NULL); 372 ASSERT_PTR_EQ(dsa_priv_key(k1), NULL);
344 TEST_DONE(); 373 TEST_DONE();
345 374
346 TEST_START("equal KEY_DSA/demoted KEY_DSA"); 375 TEST_START("equal KEY_DSA/demoted KEY_DSA");