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