1 /* 2 * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the Apache License 2.0 (the "License"). You may not use 5 * this file except in compliance with the License. You can obtain a copy 6 * in the file LICENSE in the source distribution or at 7 * https://www.openssl.org/source/license.html 8 */ 9 10 /* callback functions used by s_client, s_server, and s_time */ 11 #include <stdio.h> 12 #include <stdlib.h> 13 #include <string.h> /* for memcpy() and strcmp() */ 14 #include "apps.h" 15 #include <openssl/core_names.h> 16 #include <openssl/params.h> 17 #include <openssl/err.h> 18 #include <openssl/rand.h> 19 #include <openssl/x509.h> 20 #include <openssl/ssl.h> 21 #include <openssl/bn.h> 22 #ifndef OPENSSL_NO_DH 23 # include <openssl/dh.h> 24 #endif 25 #include "s_apps.h" 26 27 #define COOKIE_SECRET_LENGTH 16 28 29 VERIFY_CB_ARGS verify_args = { -1, 0, X509_V_OK, 0 }; 30 31 #ifndef OPENSSL_NO_SOCK 32 static unsigned char cookie_secret[COOKIE_SECRET_LENGTH]; 33 static int cookie_initialized = 0; 34 #endif 35 static BIO *bio_keylog = NULL; 36 37 static const char *lookup(int val, const STRINT_PAIR* list, const char* def) 38 { 39 for ( ; list->name; ++list) 40 if (list->retval == val) 41 return list->name; 42 return def; 43 } 44 45 int verify_callback(int ok, X509_STORE_CTX *ctx) 46 { 47 X509 *err_cert; 48 int err, depth; 49 50 err_cert = X509_STORE_CTX_get_current_cert(ctx); 51 err = X509_STORE_CTX_get_error(ctx); 52 depth = X509_STORE_CTX_get_error_depth(ctx); 53 54 if (!verify_args.quiet || !ok) { 55 BIO_printf(bio_err, "depth=%d ", depth); 56 if (err_cert != NULL) { 57 X509_NAME_print_ex(bio_err, 58 X509_get_subject_name(err_cert), 59 0, get_nameopt()); 60 BIO_puts(bio_err, "\n"); 61 } else { 62 BIO_puts(bio_err, "<no cert>\n"); 63 } 64 } 65 if (!ok) { 66 BIO_printf(bio_err, "verify error:num=%d:%s\n", err, 67 X509_verify_cert_error_string(err)); 68 if (verify_args.depth < 0 || verify_args.depth >= depth) { 69 if (!verify_args.return_error) 70 ok = 1; 71 verify_args.error = err; 72 } else { 73 ok = 0; 74 verify_args.error = X509_V_ERR_CERT_CHAIN_TOO_LONG; 75 } 76 } 77 switch (err) { 78 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT: 79 if (err_cert != NULL) { 80 BIO_puts(bio_err, "issuer= "); 81 X509_NAME_print_ex(bio_err, X509_get_issuer_name(err_cert), 82 0, get_nameopt()); 83 BIO_puts(bio_err, "\n"); 84 } 85 break; 86 case X509_V_ERR_CERT_NOT_YET_VALID: 87 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD: 88 if (err_cert != NULL) { 89 BIO_printf(bio_err, "notBefore="); 90 ASN1_TIME_print(bio_err, X509_get0_notBefore(err_cert)); 91 BIO_printf(bio_err, "\n"); 92 } 93 break; 94 case X509_V_ERR_CERT_HAS_EXPIRED: 95 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD: 96 if (err_cert != NULL) { 97 BIO_printf(bio_err, "notAfter="); 98 ASN1_TIME_print(bio_err, X509_get0_notAfter(err_cert)); 99 BIO_printf(bio_err, "\n"); 100 } 101 break; 102 case X509_V_ERR_NO_EXPLICIT_POLICY: 103 if (!verify_args.quiet) 104 policies_print(ctx); 105 break; 106 } 107 if (err == X509_V_OK && ok == 2 && !verify_args.quiet) 108 policies_print(ctx); 109 if (ok && !verify_args.quiet) 110 BIO_printf(bio_err, "verify return:%d\n", ok); 111 return ok; 112 } 113 114 int set_cert_stuff(SSL_CTX *ctx, char *cert_file, char *key_file) 115 { 116 if (cert_file != NULL) { 117 if (SSL_CTX_use_certificate_file(ctx, cert_file, 118 SSL_FILETYPE_PEM) <= 0) { 119 BIO_printf(bio_err, "unable to get certificate from '%s'\n", 120 cert_file); 121 ERR_print_errors(bio_err); 122 return 0; 123 } 124 if (key_file == NULL) 125 key_file = cert_file; 126 if (SSL_CTX_use_PrivateKey_file(ctx, key_file, SSL_FILETYPE_PEM) <= 0) { 127 BIO_printf(bio_err, "unable to get private key from '%s'\n", 128 key_file); 129 ERR_print_errors(bio_err); 130 return 0; 131 } 132 133 /* 134 * If we are using DSA, we can copy the parameters from the private 135 * key 136 */ 137 138 /* 139 * Now we know that a key and cert have been set against the SSL 140 * context 141 */ 142 if (!SSL_CTX_check_private_key(ctx)) { 143 BIO_printf(bio_err, 144 "Private key does not match the certificate public key\n"); 145 return 0; 146 } 147 } 148 return 1; 149 } 150 151 int set_cert_key_stuff(SSL_CTX *ctx, X509 *cert, EVP_PKEY *key, 152 STACK_OF(X509) *chain, int build_chain) 153 { 154 int chflags = chain ? SSL_BUILD_CHAIN_FLAG_CHECK : 0; 155 156 if (cert == NULL) 157 return 1; 158 if (SSL_CTX_use_certificate(ctx, cert) <= 0) { 159 BIO_printf(bio_err, "error setting certificate\n"); 160 ERR_print_errors(bio_err); 161 return 0; 162 } 163 164 if (SSL_CTX_use_PrivateKey(ctx, key) <= 0) { 165 BIO_printf(bio_err, "error setting private key\n"); 166 ERR_print_errors(bio_err); 167 return 0; 168 } 169 170 /* 171 * Now we know that a key and cert have been set against the SSL context 172 */ 173 if (!SSL_CTX_check_private_key(ctx)) { 174 BIO_printf(bio_err, 175 "Private key does not match the certificate public key\n"); 176 return 0; 177 } 178 if (chain && !SSL_CTX_set1_chain(ctx, chain)) { 179 BIO_printf(bio_err, "error setting certificate chain\n"); 180 ERR_print_errors(bio_err); 181 return 0; 182 } 183 if (build_chain && !SSL_CTX_build_cert_chain(ctx, chflags)) { 184 BIO_printf(bio_err, "error building certificate chain\n"); 185 ERR_print_errors(bio_err); 186 return 0; 187 } 188 return 1; 189 } 190 191 static STRINT_PAIR cert_type_list[] = { 192 {"RSA sign", TLS_CT_RSA_SIGN}, 193 {"DSA sign", TLS_CT_DSS_SIGN}, 194 {"RSA fixed DH", TLS_CT_RSA_FIXED_DH}, 195 {"DSS fixed DH", TLS_CT_DSS_FIXED_DH}, 196 {"ECDSA sign", TLS_CT_ECDSA_SIGN}, 197 {"RSA fixed ECDH", TLS_CT_RSA_FIXED_ECDH}, 198 {"ECDSA fixed ECDH", TLS_CT_ECDSA_FIXED_ECDH}, 199 {"GOST01 Sign", TLS_CT_GOST01_SIGN}, 200 {"GOST12 Sign", TLS_CT_GOST12_IANA_SIGN}, 201 {NULL} 202 }; 203 204 static void ssl_print_client_cert_types(BIO *bio, SSL *s) 205 { 206 const unsigned char *p; 207 int i; 208 int cert_type_num = SSL_get0_certificate_types(s, &p); 209 210 if (!cert_type_num) 211 return; 212 BIO_puts(bio, "Client Certificate Types: "); 213 for (i = 0; i < cert_type_num; i++) { 214 unsigned char cert_type = p[i]; 215 const char *cname = lookup((int)cert_type, cert_type_list, NULL); 216 217 if (i) 218 BIO_puts(bio, ", "); 219 if (cname != NULL) 220 BIO_puts(bio, cname); 221 else 222 BIO_printf(bio, "UNKNOWN (%d),", cert_type); 223 } 224 BIO_puts(bio, "\n"); 225 } 226 227 static const char *get_sigtype(int nid) 228 { 229 switch (nid) { 230 case EVP_PKEY_RSA: 231 return "RSA"; 232 233 case EVP_PKEY_RSA_PSS: 234 return "RSA-PSS"; 235 236 case EVP_PKEY_DSA: 237 return "DSA"; 238 239 case EVP_PKEY_EC: 240 return "ECDSA"; 241 242 case NID_ED25519: 243 return "ed25519"; 244 245 case NID_ED448: 246 return "ed448"; 247 248 case NID_id_GostR3410_2001: 249 return "gost2001"; 250 251 case NID_id_GostR3410_2012_256: 252 return "gost2012_256"; 253 254 case NID_id_GostR3410_2012_512: 255 return "gost2012_512"; 256 257 default: 258 return NULL; 259 } 260 } 261 262 static int do_print_sigalgs(BIO *out, SSL *s, int shared) 263 { 264 int i, nsig, client; 265 266 client = SSL_is_server(s) ? 0 : 1; 267 if (shared) 268 nsig = SSL_get_shared_sigalgs(s, 0, NULL, NULL, NULL, NULL, NULL); 269 else 270 nsig = SSL_get_sigalgs(s, -1, NULL, NULL, NULL, NULL, NULL); 271 if (nsig == 0) 272 return 1; 273 274 if (shared) 275 BIO_puts(out, "Shared "); 276 277 if (client) 278 BIO_puts(out, "Requested "); 279 BIO_puts(out, "Signature Algorithms: "); 280 for (i = 0; i < nsig; i++) { 281 int hash_nid, sign_nid; 282 unsigned char rhash, rsign; 283 const char *sstr = NULL; 284 if (shared) 285 SSL_get_shared_sigalgs(s, i, &sign_nid, &hash_nid, NULL, 286 &rsign, &rhash); 287 else 288 SSL_get_sigalgs(s, i, &sign_nid, &hash_nid, NULL, &rsign, &rhash); 289 if (i) 290 BIO_puts(out, ":"); 291 switch (rsign | rhash << 8) { 292 case 0x0809: 293 BIO_puts(out, "rsa_pss_pss_sha256"); 294 continue; 295 case 0x080a: 296 BIO_puts(out, "rsa_pss_pss_sha384"); 297 continue; 298 case 0x080b: 299 BIO_puts(out, "rsa_pss_pss_sha512"); 300 continue; 301 case 0x081a: 302 BIO_puts(out, "ecdsa_brainpoolP256r1_sha256"); 303 continue; 304 case 0x081b: 305 BIO_puts(out, "ecdsa_brainpoolP384r1_sha384"); 306 continue; 307 case 0x081c: 308 BIO_puts(out, "ecdsa_brainpoolP512r1_sha512"); 309 continue; 310 } 311 sstr = get_sigtype(sign_nid); 312 if (sstr) 313 BIO_printf(out, "%s", sstr); 314 else 315 BIO_printf(out, "0x%02X", (int)rsign); 316 if (hash_nid != NID_undef) 317 BIO_printf(out, "+%s", OBJ_nid2sn(hash_nid)); 318 else if (sstr == NULL) 319 BIO_printf(out, "+0x%02X", (int)rhash); 320 } 321 BIO_puts(out, "\n"); 322 return 1; 323 } 324 325 int ssl_print_sigalgs(BIO *out, SSL *s) 326 { 327 int nid; 328 329 if (!SSL_is_server(s)) 330 ssl_print_client_cert_types(out, s); 331 do_print_sigalgs(out, s, 0); 332 do_print_sigalgs(out, s, 1); 333 if (SSL_get_peer_signature_nid(s, &nid) && nid != NID_undef) 334 BIO_printf(out, "Peer signing digest: %s\n", OBJ_nid2sn(nid)); 335 if (SSL_get_peer_signature_type_nid(s, &nid)) 336 BIO_printf(out, "Peer signature type: %s\n", get_sigtype(nid)); 337 return 1; 338 } 339 340 #ifndef OPENSSL_NO_EC 341 int ssl_print_point_formats(BIO *out, SSL *s) 342 { 343 int i, nformats; 344 const char *pformats; 345 346 nformats = SSL_get0_ec_point_formats(s, &pformats); 347 if (nformats <= 0) 348 return 1; 349 BIO_puts(out, "Supported Elliptic Curve Point Formats: "); 350 for (i = 0; i < nformats; i++, pformats++) { 351 if (i) 352 BIO_puts(out, ":"); 353 switch (*pformats) { 354 case TLSEXT_ECPOINTFORMAT_uncompressed: 355 BIO_puts(out, "uncompressed"); 356 break; 357 358 case TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime: 359 BIO_puts(out, "ansiX962_compressed_prime"); 360 break; 361 362 case TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2: 363 BIO_puts(out, "ansiX962_compressed_char2"); 364 break; 365 366 default: 367 BIO_printf(out, "unknown(%d)", (int)*pformats); 368 break; 369 370 } 371 } 372 BIO_puts(out, "\n"); 373 return 1; 374 } 375 376 int ssl_print_groups(BIO *out, SSL *s, int noshared) 377 { 378 int i, ngroups, *groups, nid; 379 380 ngroups = SSL_get1_groups(s, NULL); 381 if (ngroups <= 0) 382 return 1; 383 groups = app_malloc(ngroups * sizeof(int), "groups to print"); 384 SSL_get1_groups(s, groups); 385 386 BIO_puts(out, "Supported groups: "); 387 for (i = 0; i < ngroups; i++) { 388 if (i) 389 BIO_puts(out, ":"); 390 nid = groups[i]; 391 BIO_printf(out, "%s", SSL_group_to_name(s, nid)); 392 } 393 OPENSSL_free(groups); 394 if (noshared) { 395 BIO_puts(out, "\n"); 396 return 1; 397 } 398 BIO_puts(out, "\nShared groups: "); 399 ngroups = SSL_get_shared_group(s, -1); 400 for (i = 0; i < ngroups; i++) { 401 if (i) 402 BIO_puts(out, ":"); 403 nid = SSL_get_shared_group(s, i); 404 BIO_printf(out, "%s", SSL_group_to_name(s, nid)); 405 } 406 if (ngroups == 0) 407 BIO_puts(out, "NONE"); 408 BIO_puts(out, "\n"); 409 return 1; 410 } 411 #endif 412 413 int ssl_print_tmp_key(BIO *out, SSL *s) 414 { 415 EVP_PKEY *key; 416 417 if (!SSL_get_peer_tmp_key(s, &key)) 418 return 1; 419 BIO_puts(out, "Server Temp Key: "); 420 switch (EVP_PKEY_get_id(key)) { 421 case EVP_PKEY_RSA: 422 BIO_printf(out, "RSA, %d bits\n", EVP_PKEY_get_bits(key)); 423 break; 424 425 case EVP_PKEY_DH: 426 BIO_printf(out, "DH, %d bits\n", EVP_PKEY_get_bits(key)); 427 break; 428 #ifndef OPENSSL_NO_EC 429 case EVP_PKEY_EC: 430 { 431 char name[80]; 432 size_t name_len; 433 434 if (!EVP_PKEY_get_utf8_string_param(key, OSSL_PKEY_PARAM_GROUP_NAME, 435 name, sizeof(name), &name_len)) 436 strcpy(name, "?"); 437 BIO_printf(out, "ECDH, %s, %d bits\n", name, EVP_PKEY_get_bits(key)); 438 } 439 break; 440 #endif 441 default: 442 BIO_printf(out, "%s, %d bits\n", OBJ_nid2sn(EVP_PKEY_get_id(key)), 443 EVP_PKEY_get_bits(key)); 444 } 445 EVP_PKEY_free(key); 446 return 1; 447 } 448 449 long bio_dump_callback(BIO *bio, int cmd, const char *argp, size_t len, 450 int argi, long argl, int ret, size_t *processed) 451 { 452 BIO *out; 453 454 out = (BIO *)BIO_get_callback_arg(bio); 455 if (out == NULL) 456 return ret; 457 458 if (cmd == (BIO_CB_READ | BIO_CB_RETURN)) { 459 if (ret > 0 && processed != NULL) { 460 BIO_printf(out, "read from %p [%p] (%zu bytes => %zu (0x%zX))\n", 461 (void *)bio, (void *)argp, len, *processed, *processed); 462 BIO_dump(out, argp, (int)*processed); 463 } else { 464 BIO_printf(out, "read from %p [%p] (%zu bytes => %d)\n", 465 (void *)bio, (void *)argp, len, ret); 466 } 467 } else if (cmd == (BIO_CB_WRITE | BIO_CB_RETURN)) { 468 if (ret > 0 && processed != NULL) { 469 BIO_printf(out, "write to %p [%p] (%zu bytes => %zu (0x%zX))\n", 470 (void *)bio, (void *)argp, len, *processed, *processed); 471 BIO_dump(out, argp, (int)*processed); 472 } else { 473 BIO_printf(out, "write to %p [%p] (%zu bytes => %d)\n", 474 (void *)bio, (void *)argp, len, ret); 475 } 476 } 477 return ret; 478 } 479 480 void apps_ssl_info_callback(const SSL *s, int where, int ret) 481 { 482 const char *str; 483 int w; 484 485 w = where & ~SSL_ST_MASK; 486 487 if (w & SSL_ST_CONNECT) 488 str = "SSL_connect"; 489 else if (w & SSL_ST_ACCEPT) 490 str = "SSL_accept"; 491 else 492 str = "undefined"; 493 494 if (where & SSL_CB_LOOP) { 495 BIO_printf(bio_err, "%s:%s\n", str, SSL_state_string_long(s)); 496 } else if (where & SSL_CB_ALERT) { 497 str = (where & SSL_CB_READ) ? "read" : "write"; 498 BIO_printf(bio_err, "SSL3 alert %s:%s:%s\n", 499 str, 500 SSL_alert_type_string_long(ret), 501 SSL_alert_desc_string_long(ret)); 502 } else if (where & SSL_CB_EXIT) { 503 if (ret == 0) 504 BIO_printf(bio_err, "%s:failed in %s\n", 505 str, SSL_state_string_long(s)); 506 else if (ret < 0) 507 BIO_printf(bio_err, "%s:error in %s\n", 508 str, SSL_state_string_long(s)); 509 } 510 } 511 512 static STRINT_PAIR ssl_versions[] = { 513 {"SSL 3.0", SSL3_VERSION}, 514 {"TLS 1.0", TLS1_VERSION}, 515 {"TLS 1.1", TLS1_1_VERSION}, 516 {"TLS 1.2", TLS1_2_VERSION}, 517 {"TLS 1.3", TLS1_3_VERSION}, 518 {"DTLS 1.0", DTLS1_VERSION}, 519 {"DTLS 1.0 (bad)", DTLS1_BAD_VER}, 520 {NULL} 521 }; 522 523 static STRINT_PAIR alert_types[] = { 524 {" close_notify", 0}, 525 {" end_of_early_data", 1}, 526 {" unexpected_message", 10}, 527 {" bad_record_mac", 20}, 528 {" decryption_failed", 21}, 529 {" record_overflow", 22}, 530 {" decompression_failure", 30}, 531 {" handshake_failure", 40}, 532 {" bad_certificate", 42}, 533 {" unsupported_certificate", 43}, 534 {" certificate_revoked", 44}, 535 {" certificate_expired", 45}, 536 {" certificate_unknown", 46}, 537 {" illegal_parameter", 47}, 538 {" unknown_ca", 48}, 539 {" access_denied", 49}, 540 {" decode_error", 50}, 541 {" decrypt_error", 51}, 542 {" export_restriction", 60}, 543 {" protocol_version", 70}, 544 {" insufficient_security", 71}, 545 {" internal_error", 80}, 546 {" inappropriate_fallback", 86}, 547 {" user_canceled", 90}, 548 {" no_renegotiation", 100}, 549 {" missing_extension", 109}, 550 {" unsupported_extension", 110}, 551 {" certificate_unobtainable", 111}, 552 {" unrecognized_name", 112}, 553 {" bad_certificate_status_response", 113}, 554 {" bad_certificate_hash_value", 114}, 555 {" unknown_psk_identity", 115}, 556 {" certificate_required", 116}, 557 {NULL} 558 }; 559 560 static STRINT_PAIR handshakes[] = { 561 {", HelloRequest", SSL3_MT_HELLO_REQUEST}, 562 {", ClientHello", SSL3_MT_CLIENT_HELLO}, 563 {", ServerHello", SSL3_MT_SERVER_HELLO}, 564 {", HelloVerifyRequest", DTLS1_MT_HELLO_VERIFY_REQUEST}, 565 {", NewSessionTicket", SSL3_MT_NEWSESSION_TICKET}, 566 {", EndOfEarlyData", SSL3_MT_END_OF_EARLY_DATA}, 567 {", EncryptedExtensions", SSL3_MT_ENCRYPTED_EXTENSIONS}, 568 {", Certificate", SSL3_MT_CERTIFICATE}, 569 {", ServerKeyExchange", SSL3_MT_SERVER_KEY_EXCHANGE}, 570 {", CertificateRequest", SSL3_MT_CERTIFICATE_REQUEST}, 571 {", ServerHelloDone", SSL3_MT_SERVER_DONE}, 572 {", CertificateVerify", SSL3_MT_CERTIFICATE_VERIFY}, 573 {", ClientKeyExchange", SSL3_MT_CLIENT_KEY_EXCHANGE}, 574 {", Finished", SSL3_MT_FINISHED}, 575 {", CertificateUrl", SSL3_MT_CERTIFICATE_URL}, 576 {", CertificateStatus", SSL3_MT_CERTIFICATE_STATUS}, 577 {", SupplementalData", SSL3_MT_SUPPLEMENTAL_DATA}, 578 {", KeyUpdate", SSL3_MT_KEY_UPDATE}, 579 #ifndef OPENSSL_NO_NEXTPROTONEG 580 {", NextProto", SSL3_MT_NEXT_PROTO}, 581 #endif 582 {", MessageHash", SSL3_MT_MESSAGE_HASH}, 583 {NULL} 584 }; 585 586 void msg_cb(int write_p, int version, int content_type, const void *buf, 587 size_t len, SSL *ssl, void *arg) 588 { 589 BIO *bio = arg; 590 const char *str_write_p = write_p ? ">>>" : "<<<"; 591 char tmpbuf[128]; 592 const char *str_version, *str_content_type = "", *str_details1 = "", *str_details2 = ""; 593 const unsigned char* bp = buf; 594 595 if (version == SSL3_VERSION || 596 version == TLS1_VERSION || 597 version == TLS1_1_VERSION || 598 version == TLS1_2_VERSION || 599 version == TLS1_3_VERSION || 600 version == DTLS1_VERSION || version == DTLS1_BAD_VER) { 601 str_version = lookup(version, ssl_versions, "???"); 602 switch (content_type) { 603 case SSL3_RT_CHANGE_CIPHER_SPEC: 604 /* type 20 */ 605 str_content_type = ", ChangeCipherSpec"; 606 break; 607 case SSL3_RT_ALERT: 608 /* type 21 */ 609 str_content_type = ", Alert"; 610 str_details1 = ", ???"; 611 if (len == 2) { 612 switch (bp[0]) { 613 case 1: 614 str_details1 = ", warning"; 615 break; 616 case 2: 617 str_details1 = ", fatal"; 618 break; 619 } 620 str_details2 = lookup((int)bp[1], alert_types, " ???"); 621 } 622 break; 623 case SSL3_RT_HANDSHAKE: 624 /* type 22 */ 625 str_content_type = ", Handshake"; 626 str_details1 = "???"; 627 if (len > 0) 628 str_details1 = lookup((int)bp[0], handshakes, "???"); 629 break; 630 case SSL3_RT_APPLICATION_DATA: 631 /* type 23 */ 632 str_content_type = ", ApplicationData"; 633 break; 634 case SSL3_RT_HEADER: 635 /* type 256 */ 636 str_content_type = ", RecordHeader"; 637 break; 638 case SSL3_RT_INNER_CONTENT_TYPE: 639 /* type 257 */ 640 str_content_type = ", InnerContent"; 641 break; 642 default: 643 BIO_snprintf(tmpbuf, sizeof(tmpbuf)-1, ", Unknown (content_type=%d)", content_type); 644 str_content_type = tmpbuf; 645 } 646 } else { 647 BIO_snprintf(tmpbuf, sizeof(tmpbuf)-1, "Not TLS data or unknown version (version=%d, content_type=%d)", version, content_type); 648 str_version = tmpbuf; 649 } 650 651 BIO_printf(bio, "%s %s%s [length %04lx]%s%s\n", str_write_p, str_version, 652 str_content_type, (unsigned long)len, str_details1, 653 str_details2); 654 655 if (len > 0) { 656 size_t num, i; 657 658 BIO_printf(bio, " "); 659 num = len; 660 for (i = 0; i < num; i++) { 661 if (i % 16 == 0 && i > 0) 662 BIO_printf(bio, "\n "); 663 BIO_printf(bio, " %02x", ((const unsigned char *)buf)[i]); 664 } 665 if (i < len) 666 BIO_printf(bio, " ..."); 667 BIO_printf(bio, "\n"); 668 } 669 (void)BIO_flush(bio); 670 } 671 672 static const STRINT_PAIR tlsext_types[] = { 673 {"server name", TLSEXT_TYPE_server_name}, 674 {"max fragment length", TLSEXT_TYPE_max_fragment_length}, 675 {"client certificate URL", TLSEXT_TYPE_client_certificate_url}, 676 {"trusted CA keys", TLSEXT_TYPE_trusted_ca_keys}, 677 {"truncated HMAC", TLSEXT_TYPE_truncated_hmac}, 678 {"status request", TLSEXT_TYPE_status_request}, 679 {"user mapping", TLSEXT_TYPE_user_mapping}, 680 {"client authz", TLSEXT_TYPE_client_authz}, 681 {"server authz", TLSEXT_TYPE_server_authz}, 682 {"cert type", TLSEXT_TYPE_cert_type}, 683 {"supported_groups", TLSEXT_TYPE_supported_groups}, 684 {"EC point formats", TLSEXT_TYPE_ec_point_formats}, 685 {"SRP", TLSEXT_TYPE_srp}, 686 {"signature algorithms", TLSEXT_TYPE_signature_algorithms}, 687 {"use SRTP", TLSEXT_TYPE_use_srtp}, 688 {"session ticket", TLSEXT_TYPE_session_ticket}, 689 {"renegotiation info", TLSEXT_TYPE_renegotiate}, 690 {"signed certificate timestamps", TLSEXT_TYPE_signed_certificate_timestamp}, 691 {"TLS padding", TLSEXT_TYPE_padding}, 692 #ifdef TLSEXT_TYPE_next_proto_neg 693 {"next protocol", TLSEXT_TYPE_next_proto_neg}, 694 #endif 695 #ifdef TLSEXT_TYPE_encrypt_then_mac 696 {"encrypt-then-mac", TLSEXT_TYPE_encrypt_then_mac}, 697 #endif 698 #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation 699 {"application layer protocol negotiation", 700 TLSEXT_TYPE_application_layer_protocol_negotiation}, 701 #endif 702 #ifdef TLSEXT_TYPE_extended_master_secret 703 {"extended master secret", TLSEXT_TYPE_extended_master_secret}, 704 #endif 705 {"key share", TLSEXT_TYPE_key_share}, 706 {"supported versions", TLSEXT_TYPE_supported_versions}, 707 {"psk", TLSEXT_TYPE_psk}, 708 {"psk kex modes", TLSEXT_TYPE_psk_kex_modes}, 709 {"certificate authorities", TLSEXT_TYPE_certificate_authorities}, 710 {"post handshake auth", TLSEXT_TYPE_post_handshake_auth}, 711 {"early_data", TLSEXT_TYPE_early_data}, 712 {NULL} 713 }; 714 715 /* from rfc8446 4.2.3. + gost (https://tools.ietf.org/id/draft-smyshlyaev-tls12-gost-suites-04.html) */ 716 static STRINT_PAIR signature_tls13_scheme_list[] = { 717 {"rsa_pkcs1_sha1", 0x0201 /* TLSEXT_SIGALG_rsa_pkcs1_sha1 */}, 718 {"ecdsa_sha1", 0x0203 /* TLSEXT_SIGALG_ecdsa_sha1 */}, 719 /* {"rsa_pkcs1_sha224", 0x0301 TLSEXT_SIGALG_rsa_pkcs1_sha224}, not in rfc8446 */ 720 /* {"ecdsa_sha224", 0x0303 TLSEXT_SIGALG_ecdsa_sha224} not in rfc8446 */ 721 {"rsa_pkcs1_sha256", 0x0401 /* TLSEXT_SIGALG_rsa_pkcs1_sha256 */}, 722 {"ecdsa_secp256r1_sha256", 0x0403 /* TLSEXT_SIGALG_ecdsa_secp256r1_sha256 */}, 723 {"rsa_pkcs1_sha384", 0x0501 /* TLSEXT_SIGALG_rsa_pkcs1_sha384 */}, 724 {"ecdsa_secp384r1_sha384", 0x0503 /* TLSEXT_SIGALG_ecdsa_secp384r1_sha384 */}, 725 {"rsa_pkcs1_sha512", 0x0601 /* TLSEXT_SIGALG_rsa_pkcs1_sha512 */}, 726 {"ecdsa_secp521r1_sha512", 0x0603 /* TLSEXT_SIGALG_ecdsa_secp521r1_sha512 */}, 727 {"rsa_pss_rsae_sha256", 0x0804 /* TLSEXT_SIGALG_rsa_pss_rsae_sha256 */}, 728 {"rsa_pss_rsae_sha384", 0x0805 /* TLSEXT_SIGALG_rsa_pss_rsae_sha384 */}, 729 {"rsa_pss_rsae_sha512", 0x0806 /* TLSEXT_SIGALG_rsa_pss_rsae_sha512 */}, 730 {"ed25519", 0x0807 /* TLSEXT_SIGALG_ed25519 */}, 731 {"ed448", 0x0808 /* TLSEXT_SIGALG_ed448 */}, 732 {"rsa_pss_pss_sha256", 0x0809 /* TLSEXT_SIGALG_rsa_pss_pss_sha256 */}, 733 {"rsa_pss_pss_sha384", 0x080a /* TLSEXT_SIGALG_rsa_pss_pss_sha384 */}, 734 {"rsa_pss_pss_sha512", 0x080b /* TLSEXT_SIGALG_rsa_pss_pss_sha512 */}, 735 {"gostr34102001", 0xeded /* TLSEXT_SIGALG_gostr34102001_gostr3411 */}, 736 {"gostr34102012_256", 0xeeee /* TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256 */}, 737 {"gostr34102012_512", 0xefef /* TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512 */}, 738 {NULL} 739 }; 740 741 /* from rfc5246 7.4.1.4.1. */ 742 static STRINT_PAIR signature_tls12_alg_list[] = { 743 {"anonymous", TLSEXT_signature_anonymous /* 0 */}, 744 {"RSA", TLSEXT_signature_rsa /* 1 */}, 745 {"DSA", TLSEXT_signature_dsa /* 2 */}, 746 {"ECDSA", TLSEXT_signature_ecdsa /* 3 */}, 747 {NULL} 748 }; 749 750 /* from rfc5246 7.4.1.4.1. */ 751 static STRINT_PAIR signature_tls12_hash_list[] = { 752 {"none", TLSEXT_hash_none /* 0 */}, 753 {"MD5", TLSEXT_hash_md5 /* 1 */}, 754 {"SHA1", TLSEXT_hash_sha1 /* 2 */}, 755 {"SHA224", TLSEXT_hash_sha224 /* 3 */}, 756 {"SHA256", TLSEXT_hash_sha256 /* 4 */}, 757 {"SHA384", TLSEXT_hash_sha384 /* 5 */}, 758 {"SHA512", TLSEXT_hash_sha512 /* 6 */}, 759 {NULL} 760 }; 761 762 void tlsext_cb(SSL *s, int client_server, int type, 763 const unsigned char *data, int len, void *arg) 764 { 765 BIO *bio = arg; 766 const char *extname = lookup(type, tlsext_types, "unknown"); 767 768 BIO_printf(bio, "TLS %s extension \"%s\" (id=%d), len=%d\n", 769 client_server ? "server" : "client", extname, type, len); 770 BIO_dump(bio, (const char *)data, len); 771 (void)BIO_flush(bio); 772 } 773 774 #ifndef OPENSSL_NO_SOCK 775 int generate_stateless_cookie_callback(SSL *ssl, unsigned char *cookie, 776 size_t *cookie_len) 777 { 778 unsigned char *buffer = NULL; 779 size_t length = 0; 780 unsigned short port; 781 BIO_ADDR *lpeer = NULL, *peer = NULL; 782 int res = 0; 783 784 /* Initialize a random secret */ 785 if (!cookie_initialized) { 786 if (RAND_bytes(cookie_secret, COOKIE_SECRET_LENGTH) <= 0) { 787 BIO_printf(bio_err, "error setting random cookie secret\n"); 788 return 0; 789 } 790 cookie_initialized = 1; 791 } 792 793 if (SSL_is_dtls(ssl)) { 794 lpeer = peer = BIO_ADDR_new(); 795 if (peer == NULL) { 796 BIO_printf(bio_err, "memory full\n"); 797 return 0; 798 } 799 800 /* Read peer information */ 801 (void)BIO_dgram_get_peer(SSL_get_rbio(ssl), peer); 802 } else { 803 peer = ourpeer; 804 } 805 806 /* Create buffer with peer's address and port */ 807 if (!BIO_ADDR_rawaddress(peer, NULL, &length)) { 808 BIO_printf(bio_err, "Failed getting peer address\n"); 809 BIO_ADDR_free(lpeer); 810 return 0; 811 } 812 OPENSSL_assert(length != 0); 813 port = BIO_ADDR_rawport(peer); 814 length += sizeof(port); 815 buffer = app_malloc(length, "cookie generate buffer"); 816 817 memcpy(buffer, &port, sizeof(port)); 818 BIO_ADDR_rawaddress(peer, buffer + sizeof(port), NULL); 819 820 if (EVP_Q_mac(NULL, "HMAC", NULL, "SHA1", NULL, 821 cookie_secret, COOKIE_SECRET_LENGTH, buffer, length, 822 cookie, DTLS1_COOKIE_LENGTH, cookie_len) == NULL) { 823 BIO_printf(bio_err, 824 "Error calculating HMAC-SHA1 of buffer with secret\n"); 825 goto end; 826 } 827 res = 1; 828 end: 829 OPENSSL_free(buffer); 830 BIO_ADDR_free(lpeer); 831 832 return res; 833 } 834 835 int verify_stateless_cookie_callback(SSL *ssl, const unsigned char *cookie, 836 size_t cookie_len) 837 { 838 unsigned char result[EVP_MAX_MD_SIZE]; 839 size_t resultlength; 840 841 /* Note: we check cookie_initialized because if it's not, 842 * it cannot be valid */ 843 if (cookie_initialized 844 && generate_stateless_cookie_callback(ssl, result, &resultlength) 845 && cookie_len == resultlength 846 && memcmp(result, cookie, resultlength) == 0) 847 return 1; 848 849 return 0; 850 } 851 852 int generate_cookie_callback(SSL *ssl, unsigned char *cookie, 853 unsigned int *cookie_len) 854 { 855 size_t temp = 0; 856 int res = generate_stateless_cookie_callback(ssl, cookie, &temp); 857 858 if (res != 0) 859 *cookie_len = (unsigned int)temp; 860 return res; 861 } 862 863 int verify_cookie_callback(SSL *ssl, const unsigned char *cookie, 864 unsigned int cookie_len) 865 { 866 return verify_stateless_cookie_callback(ssl, cookie, cookie_len); 867 } 868 869 #endif 870 871 /* 872 * Example of extended certificate handling. Where the standard support of 873 * one certificate per algorithm is not sufficient an application can decide 874 * which certificate(s) to use at runtime based on whatever criteria it deems 875 * appropriate. 876 */ 877 878 /* Linked list of certificates, keys and chains */ 879 struct ssl_excert_st { 880 int certform; 881 const char *certfile; 882 int keyform; 883 const char *keyfile; 884 const char *chainfile; 885 X509 *cert; 886 EVP_PKEY *key; 887 STACK_OF(X509) *chain; 888 int build_chain; 889 struct ssl_excert_st *next, *prev; 890 }; 891 892 static STRINT_PAIR chain_flags[] = { 893 {"Overall Validity", CERT_PKEY_VALID}, 894 {"Sign with EE key", CERT_PKEY_SIGN}, 895 {"EE signature", CERT_PKEY_EE_SIGNATURE}, 896 {"CA signature", CERT_PKEY_CA_SIGNATURE}, 897 {"EE key parameters", CERT_PKEY_EE_PARAM}, 898 {"CA key parameters", CERT_PKEY_CA_PARAM}, 899 {"Explicitly sign with EE key", CERT_PKEY_EXPLICIT_SIGN}, 900 {"Issuer Name", CERT_PKEY_ISSUER_NAME}, 901 {"Certificate Type", CERT_PKEY_CERT_TYPE}, 902 {NULL} 903 }; 904 905 static void print_chain_flags(SSL *s, int flags) 906 { 907 STRINT_PAIR *pp; 908 909 for (pp = chain_flags; pp->name; ++pp) 910 BIO_printf(bio_err, "\t%s: %s\n", 911 pp->name, 912 (flags & pp->retval) ? "OK" : "NOT OK"); 913 BIO_printf(bio_err, "\tSuite B: "); 914 if (SSL_set_cert_flags(s, 0) & SSL_CERT_FLAG_SUITEB_128_LOS) 915 BIO_puts(bio_err, flags & CERT_PKEY_SUITEB ? "OK\n" : "NOT OK\n"); 916 else 917 BIO_printf(bio_err, "not tested\n"); 918 } 919 920 /* 921 * Very basic selection callback: just use any certificate chain reported as 922 * valid. More sophisticated could prioritise according to local policy. 923 */ 924 static int set_cert_cb(SSL *ssl, void *arg) 925 { 926 int i, rv; 927 SSL_EXCERT *exc = arg; 928 #ifdef CERT_CB_TEST_RETRY 929 static int retry_cnt; 930 931 if (retry_cnt < 5) { 932 retry_cnt++; 933 BIO_printf(bio_err, 934 "Certificate callback retry test: count %d\n", 935 retry_cnt); 936 return -1; 937 } 938 #endif 939 SSL_certs_clear(ssl); 940 941 if (exc == NULL) 942 return 1; 943 944 /* 945 * Go to end of list and traverse backwards since we prepend newer 946 * entries this retains the original order. 947 */ 948 while (exc->next != NULL) 949 exc = exc->next; 950 951 i = 0; 952 953 while (exc != NULL) { 954 i++; 955 rv = SSL_check_chain(ssl, exc->cert, exc->key, exc->chain); 956 BIO_printf(bio_err, "Checking cert chain %d:\nSubject: ", i); 957 X509_NAME_print_ex(bio_err, X509_get_subject_name(exc->cert), 0, 958 get_nameopt()); 959 BIO_puts(bio_err, "\n"); 960 print_chain_flags(ssl, rv); 961 if (rv & CERT_PKEY_VALID) { 962 if (!SSL_use_certificate(ssl, exc->cert) 963 || !SSL_use_PrivateKey(ssl, exc->key)) { 964 return 0; 965 } 966 /* 967 * NB: we wouldn't normally do this as it is not efficient 968 * building chains on each connection better to cache the chain 969 * in advance. 970 */ 971 if (exc->build_chain) { 972 if (!SSL_build_cert_chain(ssl, 0)) 973 return 0; 974 } else if (exc->chain != NULL) { 975 if (!SSL_set1_chain(ssl, exc->chain)) 976 return 0; 977 } 978 } 979 exc = exc->prev; 980 } 981 return 1; 982 } 983 984 void ssl_ctx_set_excert(SSL_CTX *ctx, SSL_EXCERT *exc) 985 { 986 SSL_CTX_set_cert_cb(ctx, set_cert_cb, exc); 987 } 988 989 static int ssl_excert_prepend(SSL_EXCERT **pexc) 990 { 991 SSL_EXCERT *exc = app_malloc(sizeof(*exc), "prepend cert"); 992 993 memset(exc, 0, sizeof(*exc)); 994 995 exc->next = *pexc; 996 *pexc = exc; 997 998 if (exc->next) { 999 exc->certform = exc->next->certform; 1000 exc->keyform = exc->next->keyform; 1001 exc->next->prev = exc; 1002 } else { 1003 exc->certform = FORMAT_PEM; 1004 exc->keyform = FORMAT_PEM; 1005 } 1006 return 1; 1007 1008 } 1009 1010 void ssl_excert_free(SSL_EXCERT *exc) 1011 { 1012 SSL_EXCERT *curr; 1013 1014 if (exc == NULL) 1015 return; 1016 while (exc) { 1017 X509_free(exc->cert); 1018 EVP_PKEY_free(exc->key); 1019 sk_X509_pop_free(exc->chain, X509_free); 1020 curr = exc; 1021 exc = exc->next; 1022 OPENSSL_free(curr); 1023 } 1024 } 1025 1026 int load_excert(SSL_EXCERT **pexc) 1027 { 1028 SSL_EXCERT *exc = *pexc; 1029 1030 if (exc == NULL) 1031 return 1; 1032 /* If nothing in list, free and set to NULL */ 1033 if (exc->certfile == NULL && exc->next == NULL) { 1034 ssl_excert_free(exc); 1035 *pexc = NULL; 1036 return 1; 1037 } 1038 for (; exc; exc = exc->next) { 1039 if (exc->certfile == NULL) { 1040 BIO_printf(bio_err, "Missing filename\n"); 1041 return 0; 1042 } 1043 exc->cert = load_cert(exc->certfile, exc->certform, 1044 "Server Certificate"); 1045 if (exc->cert == NULL) 1046 return 0; 1047 if (exc->keyfile != NULL) { 1048 exc->key = load_key(exc->keyfile, exc->keyform, 1049 0, NULL, NULL, "server key"); 1050 } else { 1051 exc->key = load_key(exc->certfile, exc->certform, 1052 0, NULL, NULL, "server key"); 1053 } 1054 if (exc->key == NULL) 1055 return 0; 1056 if (exc->chainfile != NULL) { 1057 if (!load_certs(exc->chainfile, 0, &exc->chain, NULL, "server chain")) 1058 return 0; 1059 } 1060 } 1061 return 1; 1062 } 1063 1064 enum range { OPT_X_ENUM }; 1065 1066 int args_excert(int opt, SSL_EXCERT **pexc) 1067 { 1068 SSL_EXCERT *exc = *pexc; 1069 1070 assert(opt > OPT_X__FIRST); 1071 assert(opt < OPT_X__LAST); 1072 1073 if (exc == NULL) { 1074 if (!ssl_excert_prepend(&exc)) { 1075 BIO_printf(bio_err, " %s: Error initialising xcert\n", 1076 opt_getprog()); 1077 goto err; 1078 } 1079 *pexc = exc; 1080 } 1081 1082 switch ((enum range)opt) { 1083 case OPT_X__FIRST: 1084 case OPT_X__LAST: 1085 return 0; 1086 case OPT_X_CERT: 1087 if (exc->certfile != NULL && !ssl_excert_prepend(&exc)) { 1088 BIO_printf(bio_err, "%s: Error adding xcert\n", opt_getprog()); 1089 goto err; 1090 } 1091 *pexc = exc; 1092 exc->certfile = opt_arg(); 1093 break; 1094 case OPT_X_KEY: 1095 if (exc->keyfile != NULL) { 1096 BIO_printf(bio_err, "%s: Key already specified\n", opt_getprog()); 1097 goto err; 1098 } 1099 exc->keyfile = opt_arg(); 1100 break; 1101 case OPT_X_CHAIN: 1102 if (exc->chainfile != NULL) { 1103 BIO_printf(bio_err, "%s: Chain already specified\n", 1104 opt_getprog()); 1105 goto err; 1106 } 1107 exc->chainfile = opt_arg(); 1108 break; 1109 case OPT_X_CHAIN_BUILD: 1110 exc->build_chain = 1; 1111 break; 1112 case OPT_X_CERTFORM: 1113 if (!opt_format(opt_arg(), OPT_FMT_ANY, &exc->certform)) 1114 return 0; 1115 break; 1116 case OPT_X_KEYFORM: 1117 if (!opt_format(opt_arg(), OPT_FMT_ANY, &exc->keyform)) 1118 return 0; 1119 break; 1120 } 1121 return 1; 1122 1123 err: 1124 ERR_print_errors(bio_err); 1125 ssl_excert_free(exc); 1126 *pexc = NULL; 1127 return 0; 1128 } 1129 1130 static void print_raw_cipherlist(SSL *s) 1131 { 1132 const unsigned char *rlist; 1133 static const unsigned char scsv_id[] = { 0, 0xFF }; 1134 size_t i, rlistlen, num; 1135 1136 if (!SSL_is_server(s)) 1137 return; 1138 num = SSL_get0_raw_cipherlist(s, NULL); 1139 OPENSSL_assert(num == 2); 1140 rlistlen = SSL_get0_raw_cipherlist(s, &rlist); 1141 BIO_puts(bio_err, "Client cipher list: "); 1142 for (i = 0; i < rlistlen; i += num, rlist += num) { 1143 const SSL_CIPHER *c = SSL_CIPHER_find(s, rlist); 1144 if (i) 1145 BIO_puts(bio_err, ":"); 1146 if (c != NULL) { 1147 BIO_puts(bio_err, SSL_CIPHER_get_name(c)); 1148 } else if (memcmp(rlist, scsv_id, num) == 0) { 1149 BIO_puts(bio_err, "SCSV"); 1150 } else { 1151 size_t j; 1152 BIO_puts(bio_err, "0x"); 1153 for (j = 0; j < num; j++) 1154 BIO_printf(bio_err, "%02X", rlist[j]); 1155 } 1156 } 1157 BIO_puts(bio_err, "\n"); 1158 } 1159 1160 /* 1161 * Hex encoder for TLSA RRdata, not ':' delimited. 1162 */ 1163 static char *hexencode(const unsigned char *data, size_t len) 1164 { 1165 static const char *hex = "0123456789abcdef"; 1166 char *out; 1167 char *cp; 1168 size_t outlen = 2 * len + 1; 1169 int ilen = (int) outlen; 1170 1171 if (outlen < len || ilen < 0 || outlen != (size_t)ilen) { 1172 BIO_printf(bio_err, "%s: %zu-byte buffer too large to hexencode\n", 1173 opt_getprog(), len); 1174 exit(1); 1175 } 1176 cp = out = app_malloc(ilen, "TLSA hex data buffer"); 1177 1178 while (len-- > 0) { 1179 *cp++ = hex[(*data >> 4) & 0x0f]; 1180 *cp++ = hex[*data++ & 0x0f]; 1181 } 1182 *cp = '\0'; 1183 return out; 1184 } 1185 1186 void print_verify_detail(SSL *s, BIO *bio) 1187 { 1188 int mdpth; 1189 EVP_PKEY *mspki; 1190 long verify_err = SSL_get_verify_result(s); 1191 1192 if (verify_err == X509_V_OK) { 1193 const char *peername = SSL_get0_peername(s); 1194 1195 BIO_printf(bio, "Verification: OK\n"); 1196 if (peername != NULL) 1197 BIO_printf(bio, "Verified peername: %s\n", peername); 1198 } else { 1199 const char *reason = X509_verify_cert_error_string(verify_err); 1200 1201 BIO_printf(bio, "Verification error: %s\n", reason); 1202 } 1203 1204 if ((mdpth = SSL_get0_dane_authority(s, NULL, &mspki)) >= 0) { 1205 uint8_t usage, selector, mtype; 1206 const unsigned char *data = NULL; 1207 size_t dlen = 0; 1208 char *hexdata; 1209 1210 mdpth = SSL_get0_dane_tlsa(s, &usage, &selector, &mtype, &data, &dlen); 1211 1212 /* 1213 * The TLSA data field can be quite long when it is a certificate, 1214 * public key or even a SHA2-512 digest. Because the initial octets of 1215 * ASN.1 certificates and public keys contain mostly boilerplate OIDs 1216 * and lengths, we show the last 12 bytes of the data instead, as these 1217 * are more likely to distinguish distinct TLSA records. 1218 */ 1219 #define TLSA_TAIL_SIZE 12 1220 if (dlen > TLSA_TAIL_SIZE) 1221 hexdata = hexencode(data + dlen - TLSA_TAIL_SIZE, TLSA_TAIL_SIZE); 1222 else 1223 hexdata = hexencode(data, dlen); 1224 BIO_printf(bio, "DANE TLSA %d %d %d %s%s %s at depth %d\n", 1225 usage, selector, mtype, 1226 (dlen > TLSA_TAIL_SIZE) ? "..." : "", hexdata, 1227 (mspki != NULL) ? "signed the certificate" : 1228 mdpth ? "matched TA certificate" : "matched EE certificate", 1229 mdpth); 1230 OPENSSL_free(hexdata); 1231 } 1232 } 1233 1234 void print_ssl_summary(SSL *s) 1235 { 1236 const SSL_CIPHER *c; 1237 X509 *peer; 1238 1239 BIO_printf(bio_err, "Protocol version: %s\n", SSL_get_version(s)); 1240 print_raw_cipherlist(s); 1241 c = SSL_get_current_cipher(s); 1242 BIO_printf(bio_err, "Ciphersuite: %s\n", SSL_CIPHER_get_name(c)); 1243 do_print_sigalgs(bio_err, s, 0); 1244 peer = SSL_get0_peer_certificate(s); 1245 if (peer != NULL) { 1246 int nid; 1247 1248 BIO_puts(bio_err, "Peer certificate: "); 1249 X509_NAME_print_ex(bio_err, X509_get_subject_name(peer), 1250 0, get_nameopt()); 1251 BIO_puts(bio_err, "\n"); 1252 if (SSL_get_peer_signature_nid(s, &nid)) 1253 BIO_printf(bio_err, "Hash used: %s\n", OBJ_nid2sn(nid)); 1254 if (SSL_get_peer_signature_type_nid(s, &nid)) 1255 BIO_printf(bio_err, "Signature type: %s\n", get_sigtype(nid)); 1256 print_verify_detail(s, bio_err); 1257 } else { 1258 BIO_puts(bio_err, "No peer certificate\n"); 1259 } 1260 #ifndef OPENSSL_NO_EC 1261 ssl_print_point_formats(bio_err, s); 1262 if (SSL_is_server(s)) 1263 ssl_print_groups(bio_err, s, 1); 1264 else 1265 ssl_print_tmp_key(bio_err, s); 1266 #else 1267 if (!SSL_is_server(s)) 1268 ssl_print_tmp_key(bio_err, s); 1269 #endif 1270 } 1271 1272 int config_ctx(SSL_CONF_CTX *cctx, STACK_OF(OPENSSL_STRING) *str, 1273 SSL_CTX *ctx) 1274 { 1275 int i; 1276 1277 SSL_CONF_CTX_set_ssl_ctx(cctx, ctx); 1278 for (i = 0; i < sk_OPENSSL_STRING_num(str); i += 2) { 1279 const char *flag = sk_OPENSSL_STRING_value(str, i); 1280 const char *arg = sk_OPENSSL_STRING_value(str, i + 1); 1281 1282 if (SSL_CONF_cmd(cctx, flag, arg) <= 0) { 1283 BIO_printf(bio_err, "Call to SSL_CONF_cmd(%s, %s) failed\n", 1284 flag, arg == NULL ? "<NULL>" : arg); 1285 ERR_print_errors(bio_err); 1286 return 0; 1287 } 1288 } 1289 if (!SSL_CONF_CTX_finish(cctx)) { 1290 BIO_puts(bio_err, "Error finishing context\n"); 1291 ERR_print_errors(bio_err); 1292 return 0; 1293 } 1294 return 1; 1295 } 1296 1297 static int add_crls_store(X509_STORE *st, STACK_OF(X509_CRL) *crls) 1298 { 1299 X509_CRL *crl; 1300 int i, ret = 1; 1301 1302 for (i = 0; i < sk_X509_CRL_num(crls); i++) { 1303 crl = sk_X509_CRL_value(crls, i); 1304 if (!X509_STORE_add_crl(st, crl)) 1305 ret = 0; 1306 } 1307 return ret; 1308 } 1309 1310 int ssl_ctx_add_crls(SSL_CTX *ctx, STACK_OF(X509_CRL) *crls, int crl_download) 1311 { 1312 X509_STORE *st; 1313 1314 st = SSL_CTX_get_cert_store(ctx); 1315 add_crls_store(st, crls); 1316 if (crl_download) 1317 store_setup_crl_download(st); 1318 return 1; 1319 } 1320 1321 int ssl_load_stores(SSL_CTX *ctx, 1322 const char *vfyCApath, const char *vfyCAfile, 1323 const char *vfyCAstore, 1324 const char *chCApath, const char *chCAfile, 1325 const char *chCAstore, 1326 STACK_OF(X509_CRL) *crls, int crl_download) 1327 { 1328 X509_STORE *vfy = NULL, *ch = NULL; 1329 int rv = 0; 1330 1331 if (vfyCApath != NULL || vfyCAfile != NULL || vfyCAstore != NULL) { 1332 vfy = X509_STORE_new(); 1333 if (vfy == NULL) 1334 goto err; 1335 if (vfyCAfile != NULL && !X509_STORE_load_file(vfy, vfyCAfile)) 1336 goto err; 1337 if (vfyCApath != NULL && !X509_STORE_load_path(vfy, vfyCApath)) 1338 goto err; 1339 if (vfyCAstore != NULL && !X509_STORE_load_store(vfy, vfyCAstore)) 1340 goto err; 1341 add_crls_store(vfy, crls); 1342 if (SSL_CTX_set1_verify_cert_store(ctx, vfy) == 0) 1343 goto err; 1344 if (crl_download) 1345 store_setup_crl_download(vfy); 1346 } 1347 if (chCApath != NULL || chCAfile != NULL || chCAstore != NULL) { 1348 ch = X509_STORE_new(); 1349 if (ch == NULL) 1350 goto err; 1351 if (chCAfile != NULL && !X509_STORE_load_file(ch, chCAfile)) 1352 goto err; 1353 if (chCApath != NULL && !X509_STORE_load_path(ch, chCApath)) 1354 goto err; 1355 if (chCAstore != NULL && !X509_STORE_load_store(ch, chCAstore)) 1356 goto err; 1357 if (SSL_CTX_set1_chain_cert_store(ctx, ch) == 0) 1358 goto err; 1359 } 1360 rv = 1; 1361 err: 1362 X509_STORE_free(vfy); 1363 X509_STORE_free(ch); 1364 return rv; 1365 } 1366 1367 /* Verbose print out of security callback */ 1368 1369 typedef struct { 1370 BIO *out; 1371 int verbose; 1372 int (*old_cb) (const SSL *s, const SSL_CTX *ctx, int op, int bits, int nid, 1373 void *other, void *ex); 1374 } security_debug_ex; 1375 1376 static STRINT_PAIR callback_types[] = { 1377 {"Supported Ciphersuite", SSL_SECOP_CIPHER_SUPPORTED}, 1378 {"Shared Ciphersuite", SSL_SECOP_CIPHER_SHARED}, 1379 {"Check Ciphersuite", SSL_SECOP_CIPHER_CHECK}, 1380 #ifndef OPENSSL_NO_DH 1381 {"Temp DH key bits", SSL_SECOP_TMP_DH}, 1382 #endif 1383 {"Supported Curve", SSL_SECOP_CURVE_SUPPORTED}, 1384 {"Shared Curve", SSL_SECOP_CURVE_SHARED}, 1385 {"Check Curve", SSL_SECOP_CURVE_CHECK}, 1386 {"Supported Signature Algorithm", SSL_SECOP_SIGALG_SUPPORTED}, 1387 {"Shared Signature Algorithm", SSL_SECOP_SIGALG_SHARED}, 1388 {"Check Signature Algorithm", SSL_SECOP_SIGALG_CHECK}, 1389 {"Signature Algorithm mask", SSL_SECOP_SIGALG_MASK}, 1390 {"Certificate chain EE key", SSL_SECOP_EE_KEY}, 1391 {"Certificate chain CA key", SSL_SECOP_CA_KEY}, 1392 {"Peer Chain EE key", SSL_SECOP_PEER_EE_KEY}, 1393 {"Peer Chain CA key", SSL_SECOP_PEER_CA_KEY}, 1394 {"Certificate chain CA digest", SSL_SECOP_CA_MD}, 1395 {"Peer chain CA digest", SSL_SECOP_PEER_CA_MD}, 1396 {"SSL compression", SSL_SECOP_COMPRESSION}, 1397 {"Session ticket", SSL_SECOP_TICKET}, 1398 {NULL} 1399 }; 1400 1401 static int security_callback_debug(const SSL *s, const SSL_CTX *ctx, 1402 int op, int bits, int nid, 1403 void *other, void *ex) 1404 { 1405 security_debug_ex *sdb = ex; 1406 int rv, show_bits = 1, cert_md = 0; 1407 const char *nm; 1408 int show_nm; 1409 1410 rv = sdb->old_cb(s, ctx, op, bits, nid, other, ex); 1411 if (rv == 1 && sdb->verbose < 2) 1412 return 1; 1413 BIO_puts(sdb->out, "Security callback: "); 1414 1415 nm = lookup(op, callback_types, NULL); 1416 show_nm = nm != NULL; 1417 switch (op) { 1418 case SSL_SECOP_TICKET: 1419 case SSL_SECOP_COMPRESSION: 1420 show_bits = 0; 1421 show_nm = 0; 1422 break; 1423 case SSL_SECOP_VERSION: 1424 BIO_printf(sdb->out, "Version=%s", lookup(nid, ssl_versions, "???")); 1425 show_bits = 0; 1426 show_nm = 0; 1427 break; 1428 case SSL_SECOP_CA_MD: 1429 case SSL_SECOP_PEER_CA_MD: 1430 cert_md = 1; 1431 break; 1432 case SSL_SECOP_SIGALG_SUPPORTED: 1433 case SSL_SECOP_SIGALG_SHARED: 1434 case SSL_SECOP_SIGALG_CHECK: 1435 case SSL_SECOP_SIGALG_MASK: 1436 show_nm = 0; 1437 break; 1438 } 1439 if (show_nm) 1440 BIO_printf(sdb->out, "%s=", nm); 1441 1442 switch (op & SSL_SECOP_OTHER_TYPE) { 1443 1444 case SSL_SECOP_OTHER_CIPHER: 1445 BIO_puts(sdb->out, SSL_CIPHER_get_name(other)); 1446 break; 1447 1448 #ifndef OPENSSL_NO_EC 1449 case SSL_SECOP_OTHER_CURVE: 1450 { 1451 const char *cname; 1452 cname = EC_curve_nid2nist(nid); 1453 if (cname == NULL) 1454 cname = OBJ_nid2sn(nid); 1455 BIO_puts(sdb->out, cname); 1456 } 1457 break; 1458 #endif 1459 case SSL_SECOP_OTHER_CERT: 1460 { 1461 if (cert_md) { 1462 int sig_nid = X509_get_signature_nid(other); 1463 1464 BIO_puts(sdb->out, OBJ_nid2sn(sig_nid)); 1465 } else { 1466 EVP_PKEY *pkey = X509_get0_pubkey(other); 1467 1468 if (pkey == NULL) { 1469 BIO_printf(sdb->out, "Public key missing"); 1470 } else { 1471 const char *algname = ""; 1472 1473 EVP_PKEY_asn1_get0_info(NULL, NULL, NULL, NULL, 1474 &algname, EVP_PKEY_get0_asn1(pkey)); 1475 BIO_printf(sdb->out, "%s, bits=%d", 1476 algname, EVP_PKEY_get_bits(pkey)); 1477 } 1478 } 1479 break; 1480 } 1481 case SSL_SECOP_OTHER_SIGALG: 1482 { 1483 const unsigned char *salg = other; 1484 const char *sname = NULL; 1485 int raw_sig_code = (salg[0] << 8) + salg[1]; /* always big endian (msb, lsb) */ 1486 /* raw_sig_code: signature_scheme from tls1.3, or signature_and_hash from tls1.2 */ 1487 1488 if (nm != NULL) 1489 BIO_printf(sdb->out, "%s", nm); 1490 else 1491 BIO_printf(sdb->out, "s_cb.c:security_callback_debug op=0x%x", op); 1492 1493 sname = lookup(raw_sig_code, signature_tls13_scheme_list, NULL); 1494 if (sname != NULL) { 1495 BIO_printf(sdb->out, " scheme=%s", sname); 1496 } else { 1497 int alg_code = salg[1]; 1498 int hash_code = salg[0]; 1499 const char *alg_str = lookup(alg_code, signature_tls12_alg_list, NULL); 1500 const char *hash_str = lookup(hash_code, signature_tls12_hash_list, NULL); 1501 1502 if (alg_str != NULL && hash_str != NULL) 1503 BIO_printf(sdb->out, " digest=%s, algorithm=%s", hash_str, alg_str); 1504 else 1505 BIO_printf(sdb->out, " scheme=unknown(0x%04x)", raw_sig_code); 1506 } 1507 } 1508 1509 } 1510 1511 if (show_bits) 1512 BIO_printf(sdb->out, ", security bits=%d", bits); 1513 BIO_printf(sdb->out, ": %s\n", rv ? "yes" : "no"); 1514 return rv; 1515 } 1516 1517 void ssl_ctx_security_debug(SSL_CTX *ctx, int verbose) 1518 { 1519 static security_debug_ex sdb; 1520 1521 sdb.out = bio_err; 1522 sdb.verbose = verbose; 1523 sdb.old_cb = SSL_CTX_get_security_callback(ctx); 1524 SSL_CTX_set_security_callback(ctx, security_callback_debug); 1525 SSL_CTX_set0_security_ex_data(ctx, &sdb); 1526 } 1527 1528 static void keylog_callback(const SSL *ssl, const char *line) 1529 { 1530 if (bio_keylog == NULL) { 1531 BIO_printf(bio_err, "Keylog callback is invoked without valid file!\n"); 1532 return; 1533 } 1534 1535 /* 1536 * There might be concurrent writers to the keylog file, so we must ensure 1537 * that the given line is written at once. 1538 */ 1539 BIO_printf(bio_keylog, "%s\n", line); 1540 (void)BIO_flush(bio_keylog); 1541 } 1542 1543 int set_keylog_file(SSL_CTX *ctx, const char *keylog_file) 1544 { 1545 /* Close any open files */ 1546 BIO_free_all(bio_keylog); 1547 bio_keylog = NULL; 1548 1549 if (ctx == NULL || keylog_file == NULL) { 1550 /* Keylogging is disabled, OK. */ 1551 return 0; 1552 } 1553 1554 /* 1555 * Append rather than write in order to allow concurrent modification. 1556 * Furthermore, this preserves existing keylog files which is useful when 1557 * the tool is run multiple times. 1558 */ 1559 bio_keylog = BIO_new_file(keylog_file, "a"); 1560 if (bio_keylog == NULL) { 1561 BIO_printf(bio_err, "Error writing keylog file %s\n", keylog_file); 1562 return 1; 1563 } 1564 1565 /* Write a header for seekable, empty files (this excludes pipes). */ 1566 if (BIO_tell(bio_keylog) == 0) { 1567 BIO_puts(bio_keylog, 1568 "# SSL/TLS secrets log file, generated by OpenSSL\n"); 1569 (void)BIO_flush(bio_keylog); 1570 } 1571 SSL_CTX_set_keylog_callback(ctx, keylog_callback); 1572 return 0; 1573 } 1574 1575 void print_ca_names(BIO *bio, SSL *s) 1576 { 1577 const char *cs = SSL_is_server(s) ? "server" : "client"; 1578 const STACK_OF(X509_NAME) *sk = SSL_get0_peer_CA_list(s); 1579 int i; 1580 1581 if (sk == NULL || sk_X509_NAME_num(sk) == 0) { 1582 if (!SSL_is_server(s)) 1583 BIO_printf(bio, "---\nNo %s certificate CA names sent\n", cs); 1584 return; 1585 } 1586 1587 BIO_printf(bio, "---\nAcceptable %s certificate CA names\n",cs); 1588 for (i = 0; i < sk_X509_NAME_num(sk); i++) { 1589 X509_NAME_print_ex(bio, sk_X509_NAME_value(sk, i), 0, get_nameopt()); 1590 BIO_write(bio, "\n", 1); 1591 } 1592 } 1593