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