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