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