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