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