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