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