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