]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
fecb3aae | 2 | * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved. |
a661b653 | 3 | * |
dffa7520 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
5 | * this file except in compliance with the License. You can obtain a copy |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
a661b653 | 8 | */ |
d02b48c6 | 9 | |
af5e63e1 FG |
10 | /* |
11 | * callback functions used by s_client, s_server, and s_time, | |
12 | * as well as other common logic for those apps | |
13 | */ | |
d02b48c6 RE |
14 | #include <stdio.h> |
15 | #include <stdlib.h> | |
8f744cce | 16 | #include <string.h> /* for memcpy() and strcmp() */ |
d02b48c6 | 17 | #include "apps.h" |
dbde4726 P |
18 | #include <openssl/core_names.h> |
19 | #include <openssl/params.h> | |
ec577822 | 20 | #include <openssl/err.h> |
07a9d1a2 | 21 | #include <openssl/rand.h> |
ec577822 BM |
22 | #include <openssl/x509.h> |
23 | #include <openssl/ssl.h> | |
e03c5b59 DSH |
24 | #include <openssl/bn.h> |
25 | #ifndef OPENSSL_NO_DH | |
0f113f3e | 26 | # include <openssl/dh.h> |
e03c5b59 | 27 | #endif |
d02b48c6 RE |
28 | #include "s_apps.h" |
29 | ||
0f113f3e | 30 | #define COOKIE_SECRET_LENGTH 16 |
07a9d1a2 | 31 | |
78021171 | 32 | VERIFY_CB_ARGS verify_args = { -1, 0, X509_V_OK, 0 }; |
acc00492 | 33 | |
f9e55034 | 34 | #ifndef OPENSSL_NO_SOCK |
df2ee0e2 BL |
35 | static unsigned char cookie_secret[COOKIE_SECRET_LENGTH]; |
36 | static int cookie_initialized = 0; | |
f9e55034 | 37 | #endif |
4bf73e9f | 38 | static BIO *bio_keylog = NULL; |
d02b48c6 | 39 | |
3e8e688f RS |
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 | ||
6d23cf97 | 48 | int verify_callback(int ok, X509_STORE_CTX *ctx) |
0f113f3e MC |
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 | ||
acc00492 | 57 | if (!verify_args.quiet || !ok) { |
0f113f3e | 58 | BIO_printf(bio_err, "depth=%d ", depth); |
2234212c | 59 | if (err_cert != NULL) { |
0f113f3e MC |
60 | X509_NAME_print_ex(bio_err, |
61 | X509_get_subject_name(err_cert), | |
b5c4209b | 62 | 0, get_nameopt()); |
0f113f3e | 63 | BIO_puts(bio_err, "\n"); |
2234212c | 64 | } else { |
0f113f3e | 65 | BIO_puts(bio_err, "<no cert>\n"); |
2234212c | 66 | } |
0f113f3e MC |
67 | } |
68 | if (!ok) { | |
69 | BIO_printf(bio_err, "verify error:num=%d:%s\n", err, | |
70 | X509_verify_cert_error_string(err)); | |
78021171 | 71 | if (verify_args.depth < 0 || verify_args.depth >= depth) { |
acc00492 | 72 | if (!verify_args.return_error) |
0f113f3e | 73 | ok = 1; |
acc00492 | 74 | verify_args.error = err; |
0f113f3e MC |
75 | } else { |
76 | ok = 0; | |
acc00492 | 77 | verify_args.error = X509_V_ERR_CERT_CHAIN_TOO_LONG; |
0f113f3e MC |
78 | } |
79 | } | |
80 | switch (err) { | |
81 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT: | |
fad0f80e DB |
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 | } | |
0f113f3e MC |
88 | break; |
89 | case X509_V_ERR_CERT_NOT_YET_VALID: | |
90 | case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD: | |
fad0f80e DB |
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 | } | |
0f113f3e MC |
96 | break; |
97 | case X509_V_ERR_CERT_HAS_EXPIRED: | |
98 | case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD: | |
fad0f80e DB |
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 | } | |
0f113f3e MC |
104 | break; |
105 | case X509_V_ERR_NO_EXPLICIT_POLICY: | |
acc00492 | 106 | if (!verify_args.quiet) |
ecf3a1fb | 107 | policies_print(ctx); |
0f113f3e MC |
108 | break; |
109 | } | |
acc00492 | 110 | if (err == X509_V_OK && ok == 2 && !verify_args.quiet) |
ecf3a1fb | 111 | policies_print(ctx); |
acc00492 | 112 | if (ok && !verify_args.quiet) |
0f113f3e | 113 | BIO_printf(bio_err, "verify return:%d\n", ok); |
26a7d938 | 114 | return ok; |
0f113f3e | 115 | } |
d02b48c6 | 116 | |
6b691a5c | 117 | int set_cert_stuff(SSL_CTX *ctx, char *cert_file, char *key_file) |
0f113f3e MC |
118 | { |
119 | if (cert_file != NULL) { | |
0f113f3e MC |
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); | |
26a7d938 | 125 | return 0; |
0f113f3e MC |
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); | |
26a7d938 | 133 | return 0; |
0f113f3e MC |
134 | } |
135 | ||
0f113f3e MC |
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"); | |
26a7d938 | 148 | return 0; |
0f113f3e MC |
149 | } |
150 | } | |
208fb891 | 151 | return 1; |
0f113f3e | 152 | } |
d02b48c6 | 153 | |
fc6fc7ff | 154 | int set_cert_key_stuff(SSL_CTX *ctx, X509 *cert, EVP_PKEY *key, |
0f113f3e MC |
155 | STACK_OF(X509) *chain, int build_chain) |
156 | { | |
157 | int chflags = chain ? SSL_BUILD_CHAIN_FLAG_CHECK : 0; | |
5fae09f3 | 158 | |
0f113f3e MC |
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 | } | |
826a42a0 | 193 | |
3e8e688f RS |
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}, | |
3e8e688f | 202 | {"GOST01 Sign", TLS_CT_GOST01_SIGN}, |
7a417606 | 203 | {"GOST12 Sign", TLS_CT_GOST12_IANA_SIGN}, |
3e8e688f RS |
204 | {NULL} |
205 | }; | |
206 | ||
9f27b1ee | 207 | static void ssl_print_client_cert_types(BIO *bio, SSL *s) |
0f113f3e MC |
208 | { |
209 | const unsigned char *p; | |
210 | int i; | |
211 | int cert_type_num = SSL_get0_certificate_types(s, &p); | |
5fae09f3 | 212 | |
0f113f3e MC |
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]; | |
3e8e688f | 218 | const char *cname = lookup((int)cert_type, cert_type_list, NULL); |
0f113f3e MC |
219 | |
220 | if (i) | |
221 | BIO_puts(bio, ", "); | |
2234212c | 222 | if (cname != NULL) |
0f113f3e MC |
223 | BIO_puts(bio, cname); |
224 | else | |
225 | BIO_printf(bio, "UNKNOWN (%d),", cert_type); | |
226 | } | |
227 | BIO_puts(bio, "\n"); | |
228 | } | |
9f27b1ee | 229 | |
42ef7aea DSH |
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 | ||
5fae09f3 | 242 | case EVP_PKEY_EC: |
42ef7aea DSH |
243 | return "ECDSA"; |
244 | ||
5fae09f3 | 245 | case NID_ED25519: |
03327c8b DSH |
246 | return "Ed25519"; |
247 | ||
5fae09f3 | 248 | case NID_ED448: |
0e1d6ecf MC |
249 | return "Ed448"; |
250 | ||
5fae09f3 | 251 | case NID_id_GostR3410_2001: |
f3a246c6 DB |
252 | return "gost2001"; |
253 | ||
5fae09f3 | 254 | case NID_id_GostR3410_2012_256: |
f3a246c6 DB |
255 | return "gost2012_256"; |
256 | ||
5fae09f3 | 257 | case NID_id_GostR3410_2012_512: |
f3a246c6 DB |
258 | return "gost2012_512"; |
259 | ||
42ef7aea DSH |
260 | default: |
261 | return NULL; | |
262 | } | |
263 | } | |
264 | ||
9f27b1ee | 265 | static int do_print_sigalgs(BIO *out, SSL *s, int shared) |
0f113f3e MC |
266 | { |
267 | int i, nsig, client; | |
5fae09f3 | 268 | |
0f113f3e MC |
269 | client = SSL_is_server(s) ? 0 : 1; |
270 | if (shared) | |
6d047e06 | 271 | nsig = SSL_get_shared_sigalgs(s, 0, NULL, NULL, NULL, NULL, NULL); |
0f113f3e MC |
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, ":"); | |
91410d40 | 294 | sstr = get_sigtype(sign_nid); |
0f113f3e | 295 | if (sstr) |
03327c8b | 296 | BIO_printf(out, "%s", sstr); |
0f113f3e | 297 | else |
03327c8b | 298 | BIO_printf(out, "0x%02X", (int)rsign); |
0f113f3e | 299 | if (hash_nid != NID_undef) |
03327c8b DSH |
300 | BIO_printf(out, "+%s", OBJ_nid2sn(hash_nid)); |
301 | else if (sstr == NULL) | |
302 | BIO_printf(out, "+0x%02X", (int)rhash); | |
0f113f3e MC |
303 | } |
304 | BIO_puts(out, "\n"); | |
305 | return 1; | |
306 | } | |
e7f8ff43 | 307 | |
9f27b1ee | 308 | int ssl_print_sigalgs(BIO *out, SSL *s) |
0f113f3e | 309 | { |
42ef7aea | 310 | int nid; |
5fae09f3 | 311 | |
0f113f3e MC |
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); | |
03327c8b | 316 | if (SSL_get_peer_signature_nid(s, &nid) && nid != NID_undef) |
42ef7aea DSH |
317 | BIO_printf(out, "Peer signing digest: %s\n", OBJ_nid2sn(nid)); |
318 | if (SSL_get_peer_signature_type_nid(s, &nid)) | |
395f7c42 | 319 | BIO_printf(out, "Peer signature type: %s\n", get_sigtype(nid)); |
0f113f3e MC |
320 | return 1; |
321 | } | |
322 | ||
14536c8c | 323 | #ifndef OPENSSL_NO_EC |
20b431e3 | 324 | int ssl_print_point_formats(BIO *out, SSL *s) |
0f113f3e MC |
325 | { |
326 | int i, nformats; | |
327 | const char *pformats; | |
5fae09f3 | 328 | |
0f113f3e MC |
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 | } | |
0f113f3e MC |
355 | BIO_puts(out, "\n"); |
356 | return 1; | |
357 | } | |
20b431e3 | 358 | |
de4d764e | 359 | int ssl_print_groups(BIO *out, SSL *s, int noshared) |
0f113f3e | 360 | { |
de4d764e | 361 | int i, ngroups, *groups, nid; |
7e1b7485 | 362 | |
de4d764e MC |
363 | ngroups = SSL_get1_groups(s, NULL); |
364 | if (ngroups <= 0) | |
0f113f3e | 365 | return 1; |
de4d764e MC |
366 | groups = app_malloc(ngroups * sizeof(int), "groups to print"); |
367 | SSL_get1_groups(s, groups); | |
0f113f3e | 368 | |
becbacd7 | 369 | BIO_puts(out, "Supported groups: "); |
de4d764e | 370 | for (i = 0; i < ngroups; i++) { |
0f113f3e MC |
371 | if (i) |
372 | BIO_puts(out, ":"); | |
de4d764e | 373 | nid = groups[i]; |
becbacd7 | 374 | BIO_printf(out, "%s", SSL_group_to_name(s, nid)); |
0f113f3e | 375 | } |
de4d764e | 376 | OPENSSL_free(groups); |
0f113f3e MC |
377 | if (noshared) { |
378 | BIO_puts(out, "\n"); | |
379 | return 1; | |
380 | } | |
becbacd7 | 381 | BIO_puts(out, "\nShared groups: "); |
de4d764e MC |
382 | ngroups = SSL_get_shared_group(s, -1); |
383 | for (i = 0; i < ngroups; i++) { | |
0f113f3e MC |
384 | if (i) |
385 | BIO_puts(out, ":"); | |
de4d764e | 386 | nid = SSL_get_shared_group(s, i); |
becbacd7 | 387 | BIO_printf(out, "%s", SSL_group_to_name(s, nid)); |
0f113f3e | 388 | } |
de4d764e | 389 | if (ngroups == 0) |
0f113f3e MC |
390 | BIO_puts(out, "NONE"); |
391 | BIO_puts(out, "\n"); | |
392 | return 1; | |
393 | } | |
14536c8c | 394 | #endif |
2234212c | 395 | |
33a8de69 | 396 | int ssl_print_tmp_key(BIO *out, SSL *s) |
0f113f3e MC |
397 | { |
398 | EVP_PKEY *key; | |
a51c9f63 VD |
399 | |
400 | if (!SSL_get_peer_tmp_key(s, &key)) | |
0f113f3e MC |
401 | return 1; |
402 | BIO_puts(out, "Server Temp Key: "); | |
ed576acd | 403 | switch (EVP_PKEY_get_id(key)) { |
0f113f3e | 404 | case EVP_PKEY_RSA: |
ed576acd | 405 | BIO_printf(out, "RSA, %d bits\n", EVP_PKEY_get_bits(key)); |
0f113f3e MC |
406 | break; |
407 | ||
408 | case EVP_PKEY_DH: | |
ed576acd | 409 | BIO_printf(out, "DH, %d bits\n", EVP_PKEY_get_bits(key)); |
0f113f3e | 410 | break; |
10bf4fc2 | 411 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
412 | case EVP_PKEY_EC: |
413 | { | |
5b5eea4b SL |
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, "?"); | |
ed576acd | 420 | BIO_printf(out, "ECDH, %s, %d bits\n", name, EVP_PKEY_get_bits(key)); |
0f113f3e | 421 | } |
23143e4d | 422 | break; |
14536c8c | 423 | #endif |
23143e4d | 424 | default: |
ed576acd TM |
425 | BIO_printf(out, "%s, %d bits\n", OBJ_nid2sn(EVP_PKEY_get_id(key)), |
426 | EVP_PKEY_get_bits(key)); | |
0f113f3e MC |
427 | } |
428 | EVP_PKEY_free(key); | |
429 | return 1; | |
430 | } | |
e7f8ff43 | 431 | |
0800318a TM |
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) | |
0f113f3e MC |
434 | { |
435 | BIO *out; | |
436 | ||
437 | out = (BIO *)BIO_get_callback_arg(bio); | |
438 | if (out == NULL) | |
26a7d938 | 439 | return ret; |
0f113f3e MC |
440 | |
441 | if (cmd == (BIO_CB_READ | BIO_CB_RETURN)) { | |
0800318a TM |
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 | } | |
0f113f3e | 450 | } else if (cmd == (BIO_CB_WRITE | BIO_CB_RETURN)) { |
0800318a TM |
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 | } | |
0f113f3e | 459 | } |
26a7d938 | 460 | return ret; |
0f113f3e | 461 | } |
d02b48c6 | 462 | |
6d23cf97 | 463 | void apps_ssl_info_callback(const SSL *s, int where, int ret) |
0f113f3e MC |
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)); | |
2234212c | 489 | else if (ret < 0) |
0f113f3e MC |
490 | BIO_printf(bio_err, "%s:error in %s\n", |
491 | str, SSL_state_string_long(s)); | |
0f113f3e MC |
492 | } |
493 | } | |
d02b48c6 | 494 | |
3e8e688f RS |
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}, | |
582a17d6 | 500 | {"TLS 1.3", TLS1_3_VERSION}, |
3e8e688f RS |
501 | {"DTLS 1.0", DTLS1_VERSION}, |
502 | {"DTLS 1.0 (bad)", DTLS1_BAD_VER}, | |
503 | {NULL} | |
504 | }; | |
2234212c | 505 | |
3e8e688f RS |
506 | static STRINT_PAIR alert_types[] = { |
507 | {" close_notify", 0}, | |
b35fb005 | 508 | {" end_of_early_data", 1}, |
3e8e688f RS |
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}, | |
b35fb005 | 529 | {" inappropriate_fallback", 86}, |
3e8e688f RS |
530 | {" user_canceled", 90}, |
531 | {" no_renegotiation", 100}, | |
b35fb005 | 532 | {" missing_extension", 109}, |
3e8e688f RS |
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}, | |
b35fb005 | 539 | {" certificate_required", 116}, |
3e8e688f RS |
540 | {NULL} |
541 | }; | |
542 | ||
543 | static STRINT_PAIR handshakes[] = { | |
07518cfb TS |
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}, | |
07518cfb TS |
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}, | |
d420729b | 558 | {", CertificateUrl", SSL3_MT_CERTIFICATE_URL}, |
07518cfb | 559 | {", CertificateStatus", SSL3_MT_CERTIFICATE_STATUS}, |
d420729b | 560 | {", SupplementalData", SSL3_MT_SUPPLEMENTAL_DATA}, |
07518cfb | 561 | {", KeyUpdate", SSL3_MT_KEY_UPDATE}, |
b67cb09f | 562 | {", CompressedCertificate", SSL3_MT_COMPRESSED_CERTIFICATE}, |
07518cfb TS |
563 | #ifndef OPENSSL_NO_NEXTPROTONEG |
564 | {", NextProto", SSL3_MT_NEXT_PROTO}, | |
565 | #endif | |
566 | {", MessageHash", SSL3_MT_MESSAGE_HASH}, | |
3e8e688f RS |
567 | {NULL} |
568 | }; | |
0f113f3e MC |
569 | |
570 | void msg_cb(int write_p, int version, int content_type, const void *buf, | |
571 | size_t len, SSL *ssl, void *arg) | |
572 | { | |
573 | BIO *bio = arg; | |
3e8e688f | 574 | const char *str_write_p = write_p ? ">>>" : "<<<"; |
50c911b0 M |
575 | char tmpbuf[128]; |
576 | const char *str_version, *str_content_type = "", *str_details1 = "", *str_details2 = ""; | |
3e8e688f | 577 | const unsigned char* bp = buf; |
0f113f3e MC |
578 | |
579 | if (version == SSL3_VERSION || | |
580 | version == TLS1_VERSION || | |
581 | version == TLS1_1_VERSION || | |
582 | version == TLS1_2_VERSION || | |
582a17d6 | 583 | version == TLS1_3_VERSION || |
0f113f3e | 584 | version == DTLS1_VERSION || version == DTLS1_BAD_VER) { |
50c911b0 | 585 | str_version = lookup(version, ssl_versions, "???"); |
0f113f3e | 586 | switch (content_type) { |
50c911b0 M |
587 | case SSL3_RT_CHANGE_CIPHER_SPEC: |
588 | /* type 20 */ | |
b35fb005 | 589 | str_content_type = ", ChangeCipherSpec"; |
0f113f3e | 590 | break; |
50c911b0 M |
591 | case SSL3_RT_ALERT: |
592 | /* type 21 */ | |
b35fb005 | 593 | str_content_type = ", Alert"; |
0f113f3e | 594 | str_details1 = ", ???"; |
0f113f3e | 595 | if (len == 2) { |
3e8e688f | 596 | switch (bp[0]) { |
0f113f3e MC |
597 | case 1: |
598 | str_details1 = ", warning"; | |
599 | break; | |
600 | case 2: | |
601 | str_details1 = ", fatal"; | |
602 | break; | |
603 | } | |
3e8e688f | 604 | str_details2 = lookup((int)bp[1], alert_types, " ???"); |
0f113f3e | 605 | } |
3e8e688f | 606 | break; |
50c911b0 M |
607 | case SSL3_RT_HANDSHAKE: |
608 | /* type 22 */ | |
b35fb005 | 609 | str_content_type = ", Handshake"; |
0f113f3e | 610 | str_details1 = "???"; |
3e8e688f RS |
611 | if (len > 0) |
612 | str_details1 = lookup((int)bp[0], handshakes, "???"); | |
613 | break; | |
50c911b0 M |
614 | case SSL3_RT_APPLICATION_DATA: |
615 | /* type 23 */ | |
b35fb005 | 616 | str_content_type = ", ApplicationData"; |
7429b398 | 617 | break; |
50c911b0 M |
618 | case SSL3_RT_HEADER: |
619 | /* type 256 */ | |
620 | str_content_type = ", RecordHeader"; | |
621 | break; | |
622 | case SSL3_RT_INNER_CONTENT_TYPE: | |
623 | /* type 257 */ | |
624 | str_content_type = ", InnerContent"; | |
625 | break; | |
626 | default: | |
627 | BIO_snprintf(tmpbuf, sizeof(tmpbuf)-1, ", Unknown (content_type=%d)", content_type); | |
628 | str_content_type = tmpbuf; | |
3e8e688f | 629 | } |
50c911b0 M |
630 | } else { |
631 | BIO_snprintf(tmpbuf, sizeof(tmpbuf)-1, "Not TLS data or unknown version (version=%d, content_type=%d)", version, content_type); | |
632 | str_version = tmpbuf; | |
0f113f3e | 633 | } |
a661b653 | 634 | |
0f113f3e MC |
635 | BIO_printf(bio, "%s %s%s [length %04lx]%s%s\n", str_write_p, str_version, |
636 | str_content_type, (unsigned long)len, str_details1, | |
637 | str_details2); | |
a661b653 | 638 | |
0f113f3e MC |
639 | if (len > 0) { |
640 | size_t num, i; | |
641 | ||
642 | BIO_printf(bio, " "); | |
643 | num = len; | |
0f113f3e MC |
644 | for (i = 0; i < num; i++) { |
645 | if (i % 16 == 0 && i > 0) | |
646 | BIO_printf(bio, "\n "); | |
647 | BIO_printf(bio, " %02x", ((const unsigned char *)buf)[i]); | |
648 | } | |
649 | if (i < len) | |
650 | BIO_printf(bio, " ..."); | |
651 | BIO_printf(bio, "\n"); | |
652 | } | |
653 | (void)BIO_flush(bio); | |
654 | } | |
6434abbf | 655 | |
3e8e688f RS |
656 | static STRINT_PAIR tlsext_types[] = { |
657 | {"server name", TLSEXT_TYPE_server_name}, | |
658 | {"max fragment length", TLSEXT_TYPE_max_fragment_length}, | |
659 | {"client certificate URL", TLSEXT_TYPE_client_certificate_url}, | |
660 | {"trusted CA keys", TLSEXT_TYPE_trusted_ca_keys}, | |
661 | {"truncated HMAC", TLSEXT_TYPE_truncated_hmac}, | |
662 | {"status request", TLSEXT_TYPE_status_request}, | |
663 | {"user mapping", TLSEXT_TYPE_user_mapping}, | |
664 | {"client authz", TLSEXT_TYPE_client_authz}, | |
665 | {"server authz", TLSEXT_TYPE_server_authz}, | |
666 | {"cert type", TLSEXT_TYPE_cert_type}, | |
de4d764e | 667 | {"supported_groups", TLSEXT_TYPE_supported_groups}, |
3e8e688f RS |
668 | {"EC point formats", TLSEXT_TYPE_ec_point_formats}, |
669 | {"SRP", TLSEXT_TYPE_srp}, | |
670 | {"signature algorithms", TLSEXT_TYPE_signature_algorithms}, | |
671 | {"use SRTP", TLSEXT_TYPE_use_srtp}, | |
3e8e688f RS |
672 | {"session ticket", TLSEXT_TYPE_session_ticket}, |
673 | {"renegotiation info", TLSEXT_TYPE_renegotiate}, | |
dd696a55 | 674 | {"signed certificate timestamps", TLSEXT_TYPE_signed_certificate_timestamp}, |
3e8e688f | 675 | {"TLS padding", TLSEXT_TYPE_padding}, |
15a40af2 | 676 | #ifdef TLSEXT_TYPE_next_proto_neg |
3e8e688f | 677 | {"next protocol", TLSEXT_TYPE_next_proto_neg}, |
15a40af2 | 678 | #endif |
5e3ff62c | 679 | #ifdef TLSEXT_TYPE_encrypt_then_mac |
3e8e688f | 680 | {"encrypt-then-mac", TLSEXT_TYPE_encrypt_then_mac}, |
5e3ff62c | 681 | #endif |
b48357d9 AG |
682 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
683 | {"application layer protocol negotiation", | |
684 | TLSEXT_TYPE_application_layer_protocol_negotiation}, | |
fecd04e9 AG |
685 | #endif |
686 | #ifdef TLSEXT_TYPE_extended_master_secret | |
687 | {"extended master secret", TLSEXT_TYPE_extended_master_secret}, | |
b48357d9 | 688 | #endif |
b67cb09f | 689 | {"compress certificate", TLSEXT_TYPE_compress_certificate}, |
3578020b DSH |
690 | {"key share", TLSEXT_TYPE_key_share}, |
691 | {"supported versions", TLSEXT_TYPE_supported_versions}, | |
692 | {"psk", TLSEXT_TYPE_psk}, | |
693 | {"psk kex modes", TLSEXT_TYPE_psk_kex_modes}, | |
694 | {"certificate authorities", TLSEXT_TYPE_certificate_authorities}, | |
9d75dce3 | 695 | {"post handshake auth", TLSEXT_TYPE_post_handshake_auth}, |
3e8e688f RS |
696 | {NULL} |
697 | }; | |
0f113f3e | 698 | |
861e4562 LZ |
699 | /* from rfc8446 4.2.3. + gost (https://tools.ietf.org/id/draft-smyshlyaev-tls12-gost-suites-04.html) */ |
700 | static STRINT_PAIR signature_tls13_scheme_list[] = { | |
701 | {"rsa_pkcs1_sha1", 0x0201 /* TLSEXT_SIGALG_rsa_pkcs1_sha1 */}, | |
702 | {"ecdsa_sha1", 0x0203 /* TLSEXT_SIGALG_ecdsa_sha1 */}, | |
703 | /* {"rsa_pkcs1_sha224", 0x0301 TLSEXT_SIGALG_rsa_pkcs1_sha224}, not in rfc8446 */ | |
704 | /* {"ecdsa_sha224", 0x0303 TLSEXT_SIGALG_ecdsa_sha224} not in rfc8446 */ | |
705 | {"rsa_pkcs1_sha256", 0x0401 /* TLSEXT_SIGALG_rsa_pkcs1_sha256 */}, | |
706 | {"ecdsa_secp256r1_sha256", 0x0403 /* TLSEXT_SIGALG_ecdsa_secp256r1_sha256 */}, | |
707 | {"rsa_pkcs1_sha384", 0x0501 /* TLSEXT_SIGALG_rsa_pkcs1_sha384 */}, | |
708 | {"ecdsa_secp384r1_sha384", 0x0503 /* TLSEXT_SIGALG_ecdsa_secp384r1_sha384 */}, | |
709 | {"rsa_pkcs1_sha512", 0x0601 /* TLSEXT_SIGALG_rsa_pkcs1_sha512 */}, | |
710 | {"ecdsa_secp521r1_sha512", 0x0603 /* TLSEXT_SIGALG_ecdsa_secp521r1_sha512 */}, | |
711 | {"rsa_pss_rsae_sha256", 0x0804 /* TLSEXT_SIGALG_rsa_pss_rsae_sha256 */}, | |
712 | {"rsa_pss_rsae_sha384", 0x0805 /* TLSEXT_SIGALG_rsa_pss_rsae_sha384 */}, | |
713 | {"rsa_pss_rsae_sha512", 0x0806 /* TLSEXT_SIGALG_rsa_pss_rsae_sha512 */}, | |
714 | {"ed25519", 0x0807 /* TLSEXT_SIGALG_ed25519 */}, | |
715 | {"ed448", 0x0808 /* TLSEXT_SIGALG_ed448 */}, | |
716 | {"rsa_pss_pss_sha256", 0x0809 /* TLSEXT_SIGALG_rsa_pss_pss_sha256 */}, | |
717 | {"rsa_pss_pss_sha384", 0x080a /* TLSEXT_SIGALG_rsa_pss_pss_sha384 */}, | |
718 | {"rsa_pss_pss_sha512", 0x080b /* TLSEXT_SIGALG_rsa_pss_pss_sha512 */}, | |
719 | {"gostr34102001", 0xeded /* TLSEXT_SIGALG_gostr34102001_gostr3411 */}, | |
720 | {"gostr34102012_256", 0xeeee /* TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256 */}, | |
721 | {"gostr34102012_512", 0xefef /* TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512 */}, | |
722 | {NULL} | |
723 | }; | |
724 | ||
725 | /* from rfc5246 7.4.1.4.1. */ | |
726 | static STRINT_PAIR signature_tls12_alg_list[] = { | |
727 | {"anonymous", TLSEXT_signature_anonymous /* 0 */}, | |
728 | {"RSA", TLSEXT_signature_rsa /* 1 */}, | |
729 | {"DSA", TLSEXT_signature_dsa /* 2 */}, | |
730 | {"ECDSA", TLSEXT_signature_ecdsa /* 3 */}, | |
731 | {NULL} | |
732 | }; | |
733 | ||
734 | /* from rfc5246 7.4.1.4.1. */ | |
735 | static STRINT_PAIR signature_tls12_hash_list[] = { | |
736 | {"none", TLSEXT_hash_none /* 0 */}, | |
737 | {"MD5", TLSEXT_hash_md5 /* 1 */}, | |
738 | {"SHA1", TLSEXT_hash_sha1 /* 2 */}, | |
739 | {"SHA224", TLSEXT_hash_sha224 /* 3 */}, | |
740 | {"SHA256", TLSEXT_hash_sha256 /* 4 */}, | |
741 | {"SHA384", TLSEXT_hash_sha384 /* 5 */}, | |
742 | {"SHA512", TLSEXT_hash_sha512 /* 6 */}, | |
743 | {NULL} | |
744 | }; | |
745 | ||
3e8e688f | 746 | void tlsext_cb(SSL *s, int client_server, int type, |
b6981744 | 747 | const unsigned char *data, int len, void *arg) |
3e8e688f RS |
748 | { |
749 | BIO *bio = arg; | |
750 | const char *extname = lookup(type, tlsext_types, "unknown"); | |
0f113f3e MC |
751 | |
752 | BIO_printf(bio, "TLS %s extension \"%s\" (id=%d), len=%d\n", | |
753 | client_server ? "server" : "client", extname, type, len); | |
b6981744 | 754 | BIO_dump(bio, (const char *)data, len); |
0f113f3e MC |
755 | (void)BIO_flush(bio); |
756 | } | |
757 | ||
f9e55034 | 758 | #ifndef OPENSSL_NO_SOCK |
21dfdbef RL |
759 | int generate_stateless_cookie_callback(SSL *ssl, unsigned char *cookie, |
760 | size_t *cookie_len) | |
0f113f3e | 761 | { |
dbde4726 | 762 | unsigned char *buffer = NULL; |
10ee7246 | 763 | size_t length = 0; |
d858c876 | 764 | unsigned short port; |
10ee7246 | 765 | BIO_ADDR *lpeer = NULL, *peer = NULL; |
dbde4726 | 766 | int res = 0; |
0f113f3e MC |
767 | |
768 | /* Initialize a random secret */ | |
769 | if (!cookie_initialized) { | |
266483d2 | 770 | if (RAND_bytes(cookie_secret, COOKIE_SECRET_LENGTH) <= 0) { |
0f113f3e MC |
771 | BIO_printf(bio_err, "error setting random cookie secret\n"); |
772 | return 0; | |
773 | } | |
774 | cookie_initialized = 1; | |
775 | } | |
776 | ||
10ee7246 MC |
777 | if (SSL_is_dtls(ssl)) { |
778 | lpeer = peer = BIO_ADDR_new(); | |
779 | if (peer == NULL) { | |
780 | BIO_printf(bio_err, "memory full\n"); | |
781 | return 0; | |
782 | } | |
d858c876 | 783 | |
10ee7246 MC |
784 | /* Read peer information */ |
785 | (void)BIO_dgram_get_peer(SSL_get_rbio(ssl), peer); | |
786 | } else { | |
787 | peer = ourpeer; | |
788 | } | |
0f113f3e MC |
789 | |
790 | /* Create buffer with peer's address and port */ | |
10ee7246 MC |
791 | if (!BIO_ADDR_rawaddress(peer, NULL, &length)) { |
792 | BIO_printf(bio_err, "Failed getting peer address\n"); | |
87188185 | 793 | BIO_ADDR_free(lpeer); |
10ee7246 MC |
794 | return 0; |
795 | } | |
d858c876 RL |
796 | OPENSSL_assert(length != 0); |
797 | port = BIO_ADDR_rawport(peer); | |
798 | length += sizeof(port); | |
68dc6824 | 799 | buffer = app_malloc(length, "cookie generate buffer"); |
0f113f3e | 800 | |
d858c876 RL |
801 | memcpy(buffer, &port, sizeof(port)); |
802 | BIO_ADDR_rawaddress(peer, buffer + sizeof(port), NULL); | |
0f113f3e | 803 | |
0a8a6afd DDO |
804 | if (EVP_Q_mac(NULL, "HMAC", NULL, "SHA1", NULL, |
805 | cookie_secret, COOKIE_SECRET_LENGTH, buffer, length, | |
806 | cookie, DTLS1_COOKIE_LENGTH, cookie_len) == NULL) { | |
807 | BIO_printf(bio_err, | |
808 | "Error calculating HMAC-SHA1 of buffer with secret\n"); | |
809 | goto end; | |
dbde4726 P |
810 | } |
811 | res = 1; | |
812 | end: | |
0f113f3e | 813 | OPENSSL_free(buffer); |
10ee7246 | 814 | BIO_ADDR_free(lpeer); |
0f113f3e | 815 | |
dbde4726 | 816 | return res; |
0f113f3e MC |
817 | } |
818 | ||
21dfdbef RL |
819 | int verify_stateless_cookie_callback(SSL *ssl, const unsigned char *cookie, |
820 | size_t cookie_len) | |
0f113f3e | 821 | { |
87a595e5 | 822 | unsigned char result[EVP_MAX_MD_SIZE]; |
21dfdbef | 823 | size_t resultlength; |
87a595e5 RL |
824 | |
825 | /* Note: we check cookie_initialized because if it's not, | |
826 | * it cannot be valid */ | |
827 | if (cookie_initialized | |
21dfdbef | 828 | && generate_stateless_cookie_callback(ssl, result, &resultlength) |
87a595e5 | 829 | && cookie_len == resultlength |
0f113f3e MC |
830 | && memcmp(result, cookie, resultlength) == 0) |
831 | return 1; | |
832 | ||
833 | return 0; | |
834 | } | |
3fa2812f | 835 | |
21dfdbef RL |
836 | int generate_cookie_callback(SSL *ssl, unsigned char *cookie, |
837 | unsigned int *cookie_len) | |
3fa2812f | 838 | { |
21dfdbef RL |
839 | size_t temp = 0; |
840 | int res = generate_stateless_cookie_callback(ssl, cookie, &temp); | |
f36c3885 | 841 | |
06447b58 BE |
842 | if (res != 0) |
843 | *cookie_len = (unsigned int)temp; | |
3fa2812f BS |
844 | return res; |
845 | } | |
846 | ||
21dfdbef RL |
847 | int verify_cookie_callback(SSL *ssl, const unsigned char *cookie, |
848 | unsigned int cookie_len) | |
3fa2812f | 849 | { |
21dfdbef | 850 | return verify_stateless_cookie_callback(ssl, cookie, cookie_len); |
3fa2812f BS |
851 | } |
852 | ||
f9e55034 | 853 | #endif |
0f113f3e MC |
854 | |
855 | /* | |
856 | * Example of extended certificate handling. Where the standard support of | |
857 | * one certificate per algorithm is not sufficient an application can decide | |
858 | * which certificate(s) to use at runtime based on whatever criteria it deems | |
859 | * appropriate. | |
18d71588 DSH |
860 | */ |
861 | ||
862 | /* Linked list of certificates, keys and chains */ | |
0f113f3e MC |
863 | struct ssl_excert_st { |
864 | int certform; | |
865 | const char *certfile; | |
866 | int keyform; | |
867 | const char *keyfile; | |
868 | const char *chainfile; | |
869 | X509 *cert; | |
870 | EVP_PKEY *key; | |
871 | STACK_OF(X509) *chain; | |
872 | int build_chain; | |
873 | struct ssl_excert_st *next, *prev; | |
874 | }; | |
875 | ||
3e8e688f RS |
876 | static STRINT_PAIR chain_flags[] = { |
877 | {"Overall Validity", CERT_PKEY_VALID}, | |
878 | {"Sign with EE key", CERT_PKEY_SIGN}, | |
879 | {"EE signature", CERT_PKEY_EE_SIGNATURE}, | |
880 | {"CA signature", CERT_PKEY_CA_SIGNATURE}, | |
881 | {"EE key parameters", CERT_PKEY_EE_PARAM}, | |
882 | {"CA key parameters", CERT_PKEY_CA_PARAM}, | |
0d4fb843 | 883 | {"Explicitly sign with EE key", CERT_PKEY_EXPLICIT_SIGN}, |
3e8e688f RS |
884 | {"Issuer Name", CERT_PKEY_ISSUER_NAME}, |
885 | {"Certificate Type", CERT_PKEY_CERT_TYPE}, | |
886 | {NULL} | |
0f113f3e | 887 | }; |
6dbb6219 | 888 | |
ecf3a1fb | 889 | static void print_chain_flags(SSL *s, int flags) |
0f113f3e | 890 | { |
3e8e688f | 891 | STRINT_PAIR *pp; |
ecf3a1fb | 892 | |
3e8e688f RS |
893 | for (pp = chain_flags; pp->name; ++pp) |
894 | BIO_printf(bio_err, "\t%s: %s\n", | |
895 | pp->name, | |
896 | (flags & pp->retval) ? "OK" : "NOT OK"); | |
ecf3a1fb | 897 | BIO_printf(bio_err, "\tSuite B: "); |
0f113f3e | 898 | if (SSL_set_cert_flags(s, 0) & SSL_CERT_FLAG_SUITEB_128_LOS) |
ecf3a1fb | 899 | BIO_puts(bio_err, flags & CERT_PKEY_SUITEB ? "OK\n" : "NOT OK\n"); |
0f113f3e | 900 | else |
ecf3a1fb | 901 | BIO_printf(bio_err, "not tested\n"); |
0f113f3e MC |
902 | } |
903 | ||
904 | /* | |
905 | * Very basic selection callback: just use any certificate chain reported as | |
906 | * valid. More sophisticated could prioritise according to local policy. | |
18d71588 DSH |
907 | */ |
908 | static int set_cert_cb(SSL *ssl, void *arg) | |
0f113f3e MC |
909 | { |
910 | int i, rv; | |
911 | SSL_EXCERT *exc = arg; | |
3323314f | 912 | #ifdef CERT_CB_TEST_RETRY |
0f113f3e | 913 | static int retry_cnt; |
5fae09f3 | 914 | |
0f113f3e MC |
915 | if (retry_cnt < 5) { |
916 | retry_cnt++; | |
7768e116 RS |
917 | BIO_printf(bio_err, |
918 | "Certificate callback retry test: count %d\n", | |
919 | retry_cnt); | |
0f113f3e MC |
920 | return -1; |
921 | } | |
3323314f | 922 | #endif |
0f113f3e MC |
923 | SSL_certs_clear(ssl); |
924 | ||
2234212c | 925 | if (exc == NULL) |
0f113f3e MC |
926 | return 1; |
927 | ||
928 | /* | |
929 | * Go to end of list and traverse backwards since we prepend newer | |
930 | * entries this retains the original order. | |
931 | */ | |
2234212c | 932 | while (exc->next != NULL) |
0f113f3e MC |
933 | exc = exc->next; |
934 | ||
935 | i = 0; | |
936 | ||
2234212c | 937 | while (exc != NULL) { |
0f113f3e MC |
938 | i++; |
939 | rv = SSL_check_chain(ssl, exc->cert, exc->key, exc->chain); | |
940 | BIO_printf(bio_err, "Checking cert chain %d:\nSubject: ", i); | |
941 | X509_NAME_print_ex(bio_err, X509_get_subject_name(exc->cert), 0, | |
b5c4209b | 942 | get_nameopt()); |
0f113f3e | 943 | BIO_puts(bio_err, "\n"); |
ecf3a1fb | 944 | print_chain_flags(ssl, rv); |
0f113f3e | 945 | if (rv & CERT_PKEY_VALID) { |
61986d32 | 946 | if (!SSL_use_certificate(ssl, exc->cert) |
7e1b7485 | 947 | || !SSL_use_PrivateKey(ssl, exc->key)) { |
ac59d705 MC |
948 | return 0; |
949 | } | |
0f113f3e MC |
950 | /* |
951 | * NB: we wouldn't normally do this as it is not efficient | |
952 | * building chains on each connection better to cache the chain | |
953 | * in advance. | |
954 | */ | |
955 | if (exc->build_chain) { | |
956 | if (!SSL_build_cert_chain(ssl, 0)) | |
957 | return 0; | |
2234212c | 958 | } else if (exc->chain != NULL) { |
1aa7ecd0 | 959 | if (!SSL_set1_chain(ssl, exc->chain)) |
960 | return 0; | |
2234212c | 961 | } |
0f113f3e MC |
962 | } |
963 | exc = exc->prev; | |
964 | } | |
965 | return 1; | |
966 | } | |
18d71588 DSH |
967 | |
968 | void ssl_ctx_set_excert(SSL_CTX *ctx, SSL_EXCERT *exc) | |
0f113f3e MC |
969 | { |
970 | SSL_CTX_set_cert_cb(ctx, set_cert_cb, exc); | |
971 | } | |
18d71588 DSH |
972 | |
973 | static int ssl_excert_prepend(SSL_EXCERT **pexc) | |
0f113f3e | 974 | { |
b4faea50 | 975 | SSL_EXCERT *exc = app_malloc(sizeof(*exc), "prepend cert"); |
68dc6824 | 976 | |
64b25758 | 977 | memset(exc, 0, sizeof(*exc)); |
0f113f3e MC |
978 | |
979 | exc->next = *pexc; | |
980 | *pexc = exc; | |
981 | ||
982 | if (exc->next) { | |
983 | exc->certform = exc->next->certform; | |
984 | exc->keyform = exc->next->keyform; | |
985 | exc->next->prev = exc; | |
986 | } else { | |
987 | exc->certform = FORMAT_PEM; | |
988 | exc->keyform = FORMAT_PEM; | |
989 | } | |
990 | return 1; | |
991 | ||
992 | } | |
18d71588 DSH |
993 | |
994 | void ssl_excert_free(SSL_EXCERT *exc) | |
0f113f3e MC |
995 | { |
996 | SSL_EXCERT *curr; | |
25aaa98a | 997 | |
2234212c | 998 | if (exc == NULL) |
25aaa98a | 999 | return; |
0f113f3e | 1000 | while (exc) { |
222561fe | 1001 | X509_free(exc->cert); |
c5ba2d99 | 1002 | EVP_PKEY_free(exc->key); |
79b2a2f2 | 1003 | OSSL_STACK_OF_X509_free(exc->chain); |
0f113f3e MC |
1004 | curr = exc; |
1005 | exc = exc->next; | |
1006 | OPENSSL_free(curr); | |
1007 | } | |
1008 | } | |
18d71588 | 1009 | |
7e1b7485 | 1010 | int load_excert(SSL_EXCERT **pexc) |
0f113f3e MC |
1011 | { |
1012 | SSL_EXCERT *exc = *pexc; | |
5fae09f3 | 1013 | |
2234212c | 1014 | if (exc == NULL) |
0f113f3e MC |
1015 | return 1; |
1016 | /* If nothing in list, free and set to NULL */ | |
2234212c | 1017 | if (exc->certfile == NULL && exc->next == NULL) { |
0f113f3e MC |
1018 | ssl_excert_free(exc); |
1019 | *pexc = NULL; | |
1020 | return 1; | |
1021 | } | |
1022 | for (; exc; exc = exc->next) { | |
2234212c | 1023 | if (exc->certfile == NULL) { |
7e1b7485 | 1024 | BIO_printf(bio_err, "Missing filename\n"); |
0f113f3e MC |
1025 | return 0; |
1026 | } | |
d382e796 TM |
1027 | exc->cert = load_cert(exc->certfile, exc->certform, |
1028 | "Server Certificate"); | |
2234212c | 1029 | if (exc->cert == NULL) |
0f113f3e | 1030 | return 0; |
2234212c | 1031 | if (exc->keyfile != NULL) { |
7e1b7485 | 1032 | exc->key = load_key(exc->keyfile, exc->keyform, |
50eb2a50 | 1033 | 0, NULL, NULL, "server key"); |
0f113f3e | 1034 | } else { |
7e1b7485 | 1035 | exc->key = load_key(exc->certfile, exc->certform, |
50eb2a50 | 1036 | 0, NULL, NULL, "server key"); |
0f113f3e | 1037 | } |
2234212c | 1038 | if (exc->key == NULL) |
0f113f3e | 1039 | return 0; |
2234212c | 1040 | if (exc->chainfile != NULL) { |
ea51096e | 1041 | if (!load_certs(exc->chainfile, 0, &exc->chain, NULL, "server chain")) |
0f113f3e MC |
1042 | return 0; |
1043 | } | |
1044 | } | |
1045 | return 1; | |
1046 | } | |
18d71588 | 1047 | |
7e1b7485 RS |
1048 | enum range { OPT_X_ENUM }; |
1049 | ||
1050 | int args_excert(int opt, SSL_EXCERT **pexc) | |
0f113f3e | 1051 | { |
0f113f3e | 1052 | SSL_EXCERT *exc = *pexc; |
7e1b7485 RS |
1053 | |
1054 | assert(opt > OPT_X__FIRST); | |
1055 | assert(opt < OPT_X__LAST); | |
1056 | ||
1057 | if (exc == NULL) { | |
1058 | if (!ssl_excert_prepend(&exc)) { | |
1059 | BIO_printf(bio_err, " %s: Error initialising xcert\n", | |
1060 | opt_getprog()); | |
0f113f3e MC |
1061 | goto err; |
1062 | } | |
7e1b7485 | 1063 | *pexc = exc; |
0f113f3e | 1064 | } |
7e1b7485 RS |
1065 | |
1066 | switch ((enum range)opt) { | |
1067 | case OPT_X__FIRST: | |
1068 | case OPT_X__LAST: | |
1069 | return 0; | |
1070 | case OPT_X_CERT: | |
2234212c | 1071 | if (exc->certfile != NULL && !ssl_excert_prepend(&exc)) { |
7e1b7485 | 1072 | BIO_printf(bio_err, "%s: Error adding xcert\n", opt_getprog()); |
0f113f3e MC |
1073 | goto err; |
1074 | } | |
52f4840c | 1075 | *pexc = exc; |
7e1b7485 RS |
1076 | exc->certfile = opt_arg(); |
1077 | break; | |
1078 | case OPT_X_KEY: | |
2234212c | 1079 | if (exc->keyfile != NULL) { |
7e1b7485 | 1080 | BIO_printf(bio_err, "%s: Key already specified\n", opt_getprog()); |
0f113f3e MC |
1081 | goto err; |
1082 | } | |
7e1b7485 RS |
1083 | exc->keyfile = opt_arg(); |
1084 | break; | |
1085 | case OPT_X_CHAIN: | |
2234212c | 1086 | if (exc->chainfile != NULL) { |
7e1b7485 RS |
1087 | BIO_printf(bio_err, "%s: Chain already specified\n", |
1088 | opt_getprog()); | |
0f113f3e MC |
1089 | goto err; |
1090 | } | |
7e1b7485 RS |
1091 | exc->chainfile = opt_arg(); |
1092 | break; | |
1093 | case OPT_X_CHAIN_BUILD: | |
1094 | exc->build_chain = 1; | |
1095 | break; | |
1096 | case OPT_X_CERTFORM: | |
6d382c74 | 1097 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &exc->certform)) |
7e1b7485 RS |
1098 | return 0; |
1099 | break; | |
1100 | case OPT_X_KEYFORM: | |
6d382c74 | 1101 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &exc->keyform)) |
7e1b7485 RS |
1102 | return 0; |
1103 | break; | |
1104 | } | |
0f113f3e MC |
1105 | return 1; |
1106 | ||
1107 | err: | |
7e1b7485 | 1108 | ERR_print_errors(bio_err); |
25aaa98a | 1109 | ssl_excert_free(exc); |
0f113f3e | 1110 | *pexc = NULL; |
7e1b7485 | 1111 | return 0; |
0f113f3e | 1112 | } |
18d71588 | 1113 | |
ecf3a1fb | 1114 | static void print_raw_cipherlist(SSL *s) |
0f113f3e MC |
1115 | { |
1116 | const unsigned char *rlist; | |
800fe8e3 | 1117 | static const unsigned char scsv_id[] = { 0, 0xFF }; |
0f113f3e | 1118 | size_t i, rlistlen, num; |
5fae09f3 | 1119 | |
0f113f3e MC |
1120 | if (!SSL_is_server(s)) |
1121 | return; | |
1122 | num = SSL_get0_raw_cipherlist(s, NULL); | |
800fe8e3 | 1123 | OPENSSL_assert(num == 2); |
0f113f3e | 1124 | rlistlen = SSL_get0_raw_cipherlist(s, &rlist); |
ecf3a1fb | 1125 | BIO_puts(bio_err, "Client cipher list: "); |
0f113f3e MC |
1126 | for (i = 0; i < rlistlen; i += num, rlist += num) { |
1127 | const SSL_CIPHER *c = SSL_CIPHER_find(s, rlist); | |
1128 | if (i) | |
ecf3a1fb | 1129 | BIO_puts(bio_err, ":"); |
2234212c | 1130 | if (c != NULL) { |
ecf3a1fb | 1131 | BIO_puts(bio_err, SSL_CIPHER_get_name(c)); |
2234212c | 1132 | } else if (memcmp(rlist, scsv_id, num) == 0) { |
ecf3a1fb | 1133 | BIO_puts(bio_err, "SCSV"); |
2234212c | 1134 | } else { |
0f113f3e | 1135 | size_t j; |
ecf3a1fb | 1136 | BIO_puts(bio_err, "0x"); |
0f113f3e | 1137 | for (j = 0; j < num; j++) |
ecf3a1fb | 1138 | BIO_printf(bio_err, "%02X", rlist[j]); |
0f113f3e MC |
1139 | } |
1140 | } | |
ecf3a1fb | 1141 | BIO_puts(bio_err, "\n"); |
0f113f3e | 1142 | } |
2a7cbe77 | 1143 | |
c0a445a9 VD |
1144 | /* |
1145 | * Hex encoder for TLSA RRdata, not ':' delimited. | |
1146 | */ | |
1147 | static char *hexencode(const unsigned char *data, size_t len) | |
1148 | { | |
1149 | static const char *hex = "0123456789abcdef"; | |
1150 | char *out; | |
1151 | char *cp; | |
1152 | size_t outlen = 2 * len + 1; | |
1153 | int ilen = (int) outlen; | |
1154 | ||
1155 | if (outlen < len || ilen < 0 || outlen != (size_t)ilen) { | |
7d672984 AP |
1156 | BIO_printf(bio_err, "%s: %zu-byte buffer too large to hexencode\n", |
1157 | opt_getprog(), len); | |
c0a445a9 VD |
1158 | exit(1); |
1159 | } | |
1160 | cp = out = app_malloc(ilen, "TLSA hex data buffer"); | |
1161 | ||
b5f40eb2 | 1162 | while (len-- > 0) { |
c0a445a9 VD |
1163 | *cp++ = hex[(*data >> 4) & 0x0f]; |
1164 | *cp++ = hex[*data++ & 0x0f]; | |
1165 | } | |
1166 | *cp = '\0'; | |
1167 | return out; | |
1168 | } | |
1169 | ||
1170 | void print_verify_detail(SSL *s, BIO *bio) | |
1171 | { | |
1172 | int mdpth; | |
1173 | EVP_PKEY *mspki; | |
1174 | long verify_err = SSL_get_verify_result(s); | |
1175 | ||
1176 | if (verify_err == X509_V_OK) { | |
1177 | const char *peername = SSL_get0_peername(s); | |
1178 | ||
1179 | BIO_printf(bio, "Verification: OK\n"); | |
1180 | if (peername != NULL) | |
1181 | BIO_printf(bio, "Verified peername: %s\n", peername); | |
1182 | } else { | |
1183 | const char *reason = X509_verify_cert_error_string(verify_err); | |
1184 | ||
1185 | BIO_printf(bio, "Verification error: %s\n", reason); | |
1186 | } | |
1187 | ||
1188 | if ((mdpth = SSL_get0_dane_authority(s, NULL, &mspki)) >= 0) { | |
1189 | uint8_t usage, selector, mtype; | |
1190 | const unsigned char *data = NULL; | |
1191 | size_t dlen = 0; | |
1192 | char *hexdata; | |
1193 | ||
1194 | mdpth = SSL_get0_dane_tlsa(s, &usage, &selector, &mtype, &data, &dlen); | |
1195 | ||
1196 | /* | |
1197 | * The TLSA data field can be quite long when it is a certificate, | |
1198 | * public key or even a SHA2-512 digest. Because the initial octets of | |
1199 | * ASN.1 certificates and public keys contain mostly boilerplate OIDs | |
1200 | * and lengths, we show the last 12 bytes of the data instead, as these | |
1201 | * are more likely to distinguish distinct TLSA records. | |
1202 | */ | |
1203 | #define TLSA_TAIL_SIZE 12 | |
1204 | if (dlen > TLSA_TAIL_SIZE) | |
1205 | hexdata = hexencode(data + dlen - TLSA_TAIL_SIZE, TLSA_TAIL_SIZE); | |
1206 | else | |
1207 | hexdata = hexencode(data, dlen); | |
1208 | BIO_printf(bio, "DANE TLSA %d %d %d %s%s %s at depth %d\n", | |
1209 | usage, selector, mtype, | |
1210 | (dlen > TLSA_TAIL_SIZE) ? "..." : "", hexdata, | |
1211 | (mspki != NULL) ? "signed the certificate" : | |
1212 | mdpth ? "matched TA certificate" : "matched EE certificate", | |
1213 | mdpth); | |
1214 | OPENSSL_free(hexdata); | |
1215 | } | |
1216 | } | |
1217 | ||
ecf3a1fb | 1218 | void print_ssl_summary(SSL *s) |
0f113f3e MC |
1219 | { |
1220 | const SSL_CIPHER *c; | |
1221 | X509 *peer; | |
ecf3a1fb RS |
1222 | |
1223 | BIO_printf(bio_err, "Protocol version: %s\n", SSL_get_version(s)); | |
1224 | print_raw_cipherlist(s); | |
0f113f3e | 1225 | c = SSL_get_current_cipher(s); |
ecf3a1fb RS |
1226 | BIO_printf(bio_err, "Ciphersuite: %s\n", SSL_CIPHER_get_name(c)); |
1227 | do_print_sigalgs(bio_err, s, 0); | |
8c2bfd25 | 1228 | peer = SSL_get0_peer_certificate(s); |
2234212c | 1229 | if (peer != NULL) { |
0f113f3e | 1230 | int nid; |
c0a445a9 | 1231 | |
ecf3a1fb RS |
1232 | BIO_puts(bio_err, "Peer certificate: "); |
1233 | X509_NAME_print_ex(bio_err, X509_get_subject_name(peer), | |
b5c4209b | 1234 | 0, get_nameopt()); |
ecf3a1fb | 1235 | BIO_puts(bio_err, "\n"); |
0f113f3e | 1236 | if (SSL_get_peer_signature_nid(s, &nid)) |
ecf3a1fb | 1237 | BIO_printf(bio_err, "Hash used: %s\n", OBJ_nid2sn(nid)); |
42ef7aea DSH |
1238 | if (SSL_get_peer_signature_type_nid(s, &nid)) |
1239 | BIO_printf(bio_err, "Signature type: %s\n", get_sigtype(nid)); | |
c0a445a9 | 1240 | print_verify_detail(s, bio_err); |
2234212c | 1241 | } else { |
ecf3a1fb | 1242 | BIO_puts(bio_err, "No peer certificate\n"); |
2234212c | 1243 | } |
14536c8c | 1244 | #ifndef OPENSSL_NO_EC |
ecf3a1fb | 1245 | ssl_print_point_formats(bio_err, s); |
0f113f3e | 1246 | if (SSL_is_server(s)) |
de4d764e | 1247 | ssl_print_groups(bio_err, s, 1); |
0f113f3e | 1248 | else |
ecf3a1fb | 1249 | ssl_print_tmp_key(bio_err, s); |
14536c8c | 1250 | #else |
0f113f3e | 1251 | if (!SSL_is_server(s)) |
ecf3a1fb | 1252 | ssl_print_tmp_key(bio_err, s); |
14536c8c | 1253 | #endif |
0f113f3e | 1254 | } |
2a7cbe77 | 1255 | |
7e1b7485 | 1256 | int config_ctx(SSL_CONF_CTX *cctx, STACK_OF(OPENSSL_STRING) *str, |
dba31777 | 1257 | SSL_CTX *ctx) |
0f113f3e MC |
1258 | { |
1259 | int i; | |
7e1b7485 | 1260 | |
0f113f3e MC |
1261 | SSL_CONF_CTX_set_ssl_ctx(cctx, ctx); |
1262 | for (i = 0; i < sk_OPENSSL_STRING_num(str); i += 2) { | |
7e1b7485 RS |
1263 | const char *flag = sk_OPENSSL_STRING_value(str, i); |
1264 | const char *arg = sk_OPENSSL_STRING_value(str, i + 1); | |
55373bfd | 1265 | |
7e1b7485 | 1266 | if (SSL_CONF_cmd(cctx, flag, arg) <= 0) { |
55373bfd RS |
1267 | BIO_printf(bio_err, "Call to SSL_CONF_cmd(%s, %s) failed\n", |
1268 | flag, arg == NULL ? "<NULL>" : arg); | |
7e1b7485 | 1269 | ERR_print_errors(bio_err); |
0f113f3e MC |
1270 | return 0; |
1271 | } | |
1272 | } | |
0f113f3e | 1273 | if (!SSL_CONF_CTX_finish(cctx)) { |
7e1b7485 RS |
1274 | BIO_puts(bio_err, "Error finishing context\n"); |
1275 | ERR_print_errors(bio_err); | |
0f113f3e MC |
1276 | return 0; |
1277 | } | |
1278 | return 1; | |
1279 | } | |
a5afc0a8 | 1280 | |
fdb78f3d | 1281 | static int add_crls_store(X509_STORE *st, STACK_OF(X509_CRL) *crls) |
0f113f3e MC |
1282 | { |
1283 | X509_CRL *crl; | |
8cdcb63f P |
1284 | int i, ret = 1; |
1285 | ||
0f113f3e MC |
1286 | for (i = 0; i < sk_X509_CRL_num(crls); i++) { |
1287 | crl = sk_X509_CRL_value(crls, i); | |
8cdcb63f P |
1288 | if (!X509_STORE_add_crl(st, crl)) |
1289 | ret = 0; | |
0f113f3e | 1290 | } |
8cdcb63f | 1291 | return ret; |
0f113f3e | 1292 | } |
fdb78f3d | 1293 | |
0090a686 | 1294 | int ssl_ctx_add_crls(SSL_CTX *ctx, STACK_OF(X509_CRL) *crls, int crl_download) |
0f113f3e MC |
1295 | { |
1296 | X509_STORE *st; | |
5fae09f3 | 1297 | |
0f113f3e MC |
1298 | st = SSL_CTX_get_cert_store(ctx); |
1299 | add_crls_store(st, crls); | |
1300 | if (crl_download) | |
1301 | store_setup_crl_download(st); | |
1302 | return 1; | |
1303 | } | |
fdb78f3d | 1304 | |
a5afc0a8 | 1305 | int ssl_load_stores(SSL_CTX *ctx, |
0f113f3e | 1306 | const char *vfyCApath, const char *vfyCAfile, |
fd3397fc | 1307 | const char *vfyCAstore, |
0f113f3e | 1308 | const char *chCApath, const char *chCAfile, |
fd3397fc | 1309 | const char *chCAstore, |
0f113f3e MC |
1310 | STACK_OF(X509_CRL) *crls, int crl_download) |
1311 | { | |
1312 | X509_STORE *vfy = NULL, *ch = NULL; | |
1313 | int rv = 0; | |
5fae09f3 | 1314 | |
fd3397fc | 1315 | if (vfyCApath != NULL || vfyCAfile != NULL || vfyCAstore != NULL) { |
0f113f3e | 1316 | vfy = X509_STORE_new(); |
96487cdd MC |
1317 | if (vfy == NULL) |
1318 | goto err; | |
fd3397fc RL |
1319 | if (vfyCAfile != NULL && !X509_STORE_load_file(vfy, vfyCAfile)) |
1320 | goto err; | |
1321 | if (vfyCApath != NULL && !X509_STORE_load_path(vfy, vfyCApath)) | |
1322 | goto err; | |
1323 | if (vfyCAstore != NULL && !X509_STORE_load_store(vfy, vfyCAstore)) | |
0f113f3e MC |
1324 | goto err; |
1325 | add_crls_store(vfy, crls); | |
1326 | SSL_CTX_set1_verify_cert_store(ctx, vfy); | |
1327 | if (crl_download) | |
1328 | store_setup_crl_download(vfy); | |
1329 | } | |
fd3397fc | 1330 | if (chCApath != NULL || chCAfile != NULL || chCAstore != NULL) { |
0f113f3e | 1331 | ch = X509_STORE_new(); |
96487cdd MC |
1332 | if (ch == NULL) |
1333 | goto err; | |
fd3397fc RL |
1334 | if (chCAfile != NULL && !X509_STORE_load_file(ch, chCAfile)) |
1335 | goto err; | |
1336 | if (chCApath != NULL && !X509_STORE_load_path(ch, chCApath)) | |
1337 | goto err; | |
1338 | if (chCAstore != NULL && !X509_STORE_load_store(ch, chCAstore)) | |
0f113f3e MC |
1339 | goto err; |
1340 | SSL_CTX_set1_chain_cert_store(ctx, ch); | |
1341 | } | |
1342 | rv = 1; | |
1343 | err: | |
222561fe RS |
1344 | X509_STORE_free(vfy); |
1345 | X509_STORE_free(ch); | |
0f113f3e MC |
1346 | return rv; |
1347 | } | |
e03c5b59 DSH |
1348 | |
1349 | /* Verbose print out of security callback */ | |
1350 | ||
0f113f3e MC |
1351 | typedef struct { |
1352 | BIO *out; | |
1353 | int verbose; | |
e4646a89 | 1354 | int (*old_cb) (const SSL *s, const SSL_CTX *ctx, int op, int bits, int nid, |
0f113f3e MC |
1355 | void *other, void *ex); |
1356 | } security_debug_ex; | |
e03c5b59 | 1357 | |
3e8e688f RS |
1358 | static STRINT_PAIR callback_types[] = { |
1359 | {"Supported Ciphersuite", SSL_SECOP_CIPHER_SUPPORTED}, | |
1360 | {"Shared Ciphersuite", SSL_SECOP_CIPHER_SHARED}, | |
1361 | {"Check Ciphersuite", SSL_SECOP_CIPHER_CHECK}, | |
1362 | #ifndef OPENSSL_NO_DH | |
1363 | {"Temp DH key bits", SSL_SECOP_TMP_DH}, | |
1364 | #endif | |
1365 | {"Supported Curve", SSL_SECOP_CURVE_SUPPORTED}, | |
1366 | {"Shared Curve", SSL_SECOP_CURVE_SHARED}, | |
1367 | {"Check Curve", SSL_SECOP_CURVE_CHECK}, | |
861e4562 LZ |
1368 | {"Supported Signature Algorithm", SSL_SECOP_SIGALG_SUPPORTED}, |
1369 | {"Shared Signature Algorithm", SSL_SECOP_SIGALG_SHARED}, | |
1370 | {"Check Signature Algorithm", SSL_SECOP_SIGALG_CHECK}, | |
3e8e688f RS |
1371 | {"Signature Algorithm mask", SSL_SECOP_SIGALG_MASK}, |
1372 | {"Certificate chain EE key", SSL_SECOP_EE_KEY}, | |
1373 | {"Certificate chain CA key", SSL_SECOP_CA_KEY}, | |
1374 | {"Peer Chain EE key", SSL_SECOP_PEER_EE_KEY}, | |
1375 | {"Peer Chain CA key", SSL_SECOP_PEER_CA_KEY}, | |
1376 | {"Certificate chain CA digest", SSL_SECOP_CA_MD}, | |
1377 | {"Peer chain CA digest", SSL_SECOP_PEER_CA_MD}, | |
1378 | {"SSL compression", SSL_SECOP_COMPRESSION}, | |
1379 | {"Session ticket", SSL_SECOP_TICKET}, | |
1380 | {NULL} | |
1381 | }; | |
1382 | ||
e4646a89 | 1383 | static int security_callback_debug(const SSL *s, const SSL_CTX *ctx, |
0f113f3e MC |
1384 | int op, int bits, int nid, |
1385 | void *other, void *ex) | |
1386 | { | |
1387 | security_debug_ex *sdb = ex; | |
1388 | int rv, show_bits = 1, cert_md = 0; | |
1389 | const char *nm; | |
861e4562 | 1390 | int show_nm; |
5fae09f3 | 1391 | |
0f113f3e MC |
1392 | rv = sdb->old_cb(s, ctx, op, bits, nid, other, ex); |
1393 | if (rv == 1 && sdb->verbose < 2) | |
1394 | return 1; | |
1395 | BIO_puts(sdb->out, "Security callback: "); | |
1396 | ||
3e8e688f | 1397 | nm = lookup(op, callback_types, NULL); |
861e4562 | 1398 | show_nm = nm != NULL; |
0f113f3e | 1399 | switch (op) { |
0f113f3e | 1400 | case SSL_SECOP_TICKET: |
0f113f3e | 1401 | case SSL_SECOP_COMPRESSION: |
0f113f3e | 1402 | show_bits = 0; |
861e4562 | 1403 | show_nm = 0; |
0f113f3e | 1404 | break; |
0f113f3e | 1405 | case SSL_SECOP_VERSION: |
3e8e688f | 1406 | BIO_printf(sdb->out, "Version=%s", lookup(nid, ssl_versions, "???")); |
0f113f3e | 1407 | show_bits = 0; |
861e4562 | 1408 | show_nm = 0; |
0f113f3e | 1409 | break; |
0f113f3e | 1410 | case SSL_SECOP_CA_MD: |
0f113f3e MC |
1411 | case SSL_SECOP_PEER_CA_MD: |
1412 | cert_md = 1; | |
0f113f3e | 1413 | break; |
861e4562 LZ |
1414 | case SSL_SECOP_SIGALG_SUPPORTED: |
1415 | case SSL_SECOP_SIGALG_SHARED: | |
1416 | case SSL_SECOP_SIGALG_CHECK: | |
1417 | case SSL_SECOP_SIGALG_MASK: | |
1418 | show_nm = 0; | |
1419 | break; | |
0f113f3e | 1420 | } |
861e4562 | 1421 | if (show_nm) |
0f113f3e MC |
1422 | BIO_printf(sdb->out, "%s=", nm); |
1423 | ||
1424 | switch (op & SSL_SECOP_OTHER_TYPE) { | |
1425 | ||
1426 | case SSL_SECOP_OTHER_CIPHER: | |
1427 | BIO_puts(sdb->out, SSL_CIPHER_get_name(other)); | |
1428 | break; | |
e03c5b59 | 1429 | |
fd86c2b1 | 1430 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
1431 | case SSL_SECOP_OTHER_CURVE: |
1432 | { | |
1433 | const char *cname; | |
1434 | cname = EC_curve_nid2nist(nid); | |
1435 | if (cname == NULL) | |
1436 | cname = OBJ_nid2sn(nid); | |
1437 | BIO_puts(sdb->out, cname); | |
1438 | } | |
1439 | break; | |
37f3a3b3 | 1440 | #endif |
0f113f3e MC |
1441 | case SSL_SECOP_OTHER_CERT: |
1442 | { | |
1443 | if (cert_md) { | |
1444 | int sig_nid = X509_get_signature_nid(other); | |
5fae09f3 | 1445 | |
0f113f3e MC |
1446 | BIO_puts(sdb->out, OBJ_nid2sn(sig_nid)); |
1447 | } else { | |
c01ff880 | 1448 | EVP_PKEY *pkey = X509_get0_pubkey(other); |
5fae09f3 | 1449 | |
1450 | if (pkey == NULL) { | |
1451 | BIO_printf(sdb->out, "Public key missing"); | |
1452 | } else { | |
1453 | const char *algname = ""; | |
1454 | ||
1455 | EVP_PKEY_asn1_get0_info(NULL, NULL, NULL, NULL, | |
1456 | &algname, EVP_PKEY_get0_asn1(pkey)); | |
1457 | BIO_printf(sdb->out, "%s, bits=%d", | |
1458 | algname, EVP_PKEY_get_bits(pkey)); | |
1459 | } | |
0f113f3e MC |
1460 | } |
1461 | break; | |
1462 | } | |
1463 | case SSL_SECOP_OTHER_SIGALG: | |
1464 | { | |
1465 | const unsigned char *salg = other; | |
1466 | const char *sname = NULL; | |
861e4562 LZ |
1467 | int raw_sig_code = (salg[0] << 8) + salg[1]; /* always big endian (msb, lsb) */ |
1468 | /* raw_sig_code: signature_scheme from tls1.3, or signature_and_hash from tls1.2 */ | |
0f113f3e | 1469 | |
861e4562 LZ |
1470 | if (nm != NULL) |
1471 | BIO_printf(sdb->out, "%s", nm); | |
0f113f3e | 1472 | else |
861e4562 LZ |
1473 | BIO_printf(sdb->out, "s_cb.c:security_callback_debug op=0x%x", op); |
1474 | ||
1475 | sname = lookup(raw_sig_code, signature_tls13_scheme_list, NULL); | |
1476 | if (sname != NULL) { | |
1477 | BIO_printf(sdb->out, " scheme=%s", sname); | |
1478 | } else { | |
1479 | int alg_code = salg[1]; | |
1480 | int hash_code = salg[0]; | |
1481 | const char *alg_str = lookup(alg_code, signature_tls12_alg_list, NULL); | |
1482 | const char *hash_str = lookup(hash_code, signature_tls12_hash_list, NULL); | |
1483 | ||
1484 | if (alg_str != NULL && hash_str != NULL) | |
1485 | BIO_printf(sdb->out, " digest=%s, algorithm=%s", hash_str, alg_str); | |
1486 | else | |
1487 | BIO_printf(sdb->out, " scheme=unknown(0x%04x)", raw_sig_code); | |
1488 | } | |
0f113f3e MC |
1489 | } |
1490 | ||
1491 | } | |
1492 | ||
1493 | if (show_bits) | |
1494 | BIO_printf(sdb->out, ", security bits=%d", bits); | |
1495 | BIO_printf(sdb->out, ": %s\n", rv ? "yes" : "no"); | |
1496 | return rv; | |
1497 | } | |
e03c5b59 | 1498 | |
ecf3a1fb | 1499 | void ssl_ctx_security_debug(SSL_CTX *ctx, int verbose) |
0f113f3e MC |
1500 | { |
1501 | static security_debug_ex sdb; | |
ecf3a1fb RS |
1502 | |
1503 | sdb.out = bio_err; | |
0f113f3e MC |
1504 | sdb.verbose = verbose; |
1505 | sdb.old_cb = SSL_CTX_get_security_callback(ctx); | |
1506 | SSL_CTX_set_security_callback(ctx, security_callback_debug); | |
1507 | SSL_CTX_set0_security_ex_data(ctx, &sdb); | |
1508 | } | |
4bf73e9f PW |
1509 | |
1510 | static void keylog_callback(const SSL *ssl, const char *line) | |
1511 | { | |
1512 | if (bio_keylog == NULL) { | |
1513 | BIO_printf(bio_err, "Keylog callback is invoked without valid file!\n"); | |
1514 | return; | |
1515 | } | |
1516 | ||
1517 | /* | |
1518 | * There might be concurrent writers to the keylog file, so we must ensure | |
1519 | * that the given line is written at once. | |
1520 | */ | |
1521 | BIO_printf(bio_keylog, "%s\n", line); | |
1522 | (void)BIO_flush(bio_keylog); | |
1523 | } | |
1524 | ||
1525 | int set_keylog_file(SSL_CTX *ctx, const char *keylog_file) | |
1526 | { | |
1527 | /* Close any open files */ | |
1528 | BIO_free_all(bio_keylog); | |
1529 | bio_keylog = NULL; | |
1530 | ||
1531 | if (ctx == NULL || keylog_file == NULL) { | |
1532 | /* Keylogging is disabled, OK. */ | |
1533 | return 0; | |
1534 | } | |
1535 | ||
1536 | /* | |
1537 | * Append rather than write in order to allow concurrent modification. | |
1538 | * Furthermore, this preserves existing keylog files which is useful when | |
1539 | * the tool is run multiple times. | |
1540 | */ | |
1541 | bio_keylog = BIO_new_file(keylog_file, "a"); | |
1542 | if (bio_keylog == NULL) { | |
1543 | BIO_printf(bio_err, "Error writing keylog file %s\n", keylog_file); | |
1544 | return 1; | |
1545 | } | |
1546 | ||
1547 | /* Write a header for seekable, empty files (this excludes pipes). */ | |
1548 | if (BIO_tell(bio_keylog) == 0) { | |
1549 | BIO_puts(bio_keylog, | |
1550 | "# SSL/TLS secrets log file, generated by OpenSSL\n"); | |
1551 | (void)BIO_flush(bio_keylog); | |
1552 | } | |
1553 | SSL_CTX_set_keylog_callback(ctx, keylog_callback); | |
1554 | return 0; | |
1555 | } | |
5969a2dd DSH |
1556 | |
1557 | void print_ca_names(BIO *bio, SSL *s) | |
1558 | { | |
1559 | const char *cs = SSL_is_server(s) ? "server" : "client"; | |
1560 | const STACK_OF(X509_NAME) *sk = SSL_get0_peer_CA_list(s); | |
1561 | int i; | |
1562 | ||
1563 | if (sk == NULL || sk_X509_NAME_num(sk) == 0) { | |
1e8e75d1 BB |
1564 | if (!SSL_is_server(s)) |
1565 | BIO_printf(bio, "---\nNo %s certificate CA names sent\n", cs); | |
5969a2dd DSH |
1566 | return; |
1567 | } | |
1568 | ||
1287dabd | 1569 | BIO_printf(bio, "---\nAcceptable %s certificate CA names\n", cs); |
5969a2dd | 1570 | for (i = 0; i < sk_X509_NAME_num(sk); i++) { |
b5c4209b | 1571 | X509_NAME_print_ex(bio, sk_X509_NAME_value(sk, i), 0, get_nameopt()); |
5969a2dd DSH |
1572 | BIO_write(bio, "\n", 1); |
1573 | } | |
1574 | } | |
af5e63e1 FG |
1575 | |
1576 | void ssl_print_secure_renegotiation_notes(BIO *bio, SSL *s) | |
1577 | { | |
1578 | if (SSL_VERSION_ALLOWS_RENEGOTIATION(s)) { | |
1579 | BIO_printf(bio, "Secure Renegotiation IS%s supported\n", | |
1580 | SSL_get_secure_renegotiation_support(s) ? "" : " NOT"); | |
1581 | } else { | |
1582 | BIO_printf(bio, "This TLS version forbids renegotiation.\n"); | |
1583 | } | |
1584 | } | |
e1cd94f2 PP |
1585 | |
1586 | int progress_cb(EVP_PKEY_CTX *ctx) | |
1587 | { | |
1588 | BIO *b = EVP_PKEY_CTX_get_app_data(ctx); | |
1589 | int p = EVP_PKEY_CTX_get_keygen_info(ctx, 0); | |
1590 | static const char symbols[] = ".+*\n"; | |
1591 | char c = (p >= 0 && (size_t)p <= sizeof(symbols) - 1) ? symbols[p] : '?'; | |
1592 | ||
1593 | BIO_write(b, &c, 1); | |
1594 | (void)BIO_flush(b); | |
1595 | return 1; | |
1596 | } | |
1597 |