]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/s_cb.c
Use "==0" instead of "!strcmp" etc
[thirdparty/openssl.git] / apps / s_cb.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57 /* ====================================================================
58 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110
111 /* callback functions used by s_client, s_server, and s_time */
112 #include <stdio.h>
113 #include <stdlib.h>
114 #include <assert.h>
115 #define USE_SOCKETS
116 #include "apps.h"
117 #undef USE_SOCKETS
118 #include <openssl/err.h>
119 #include <openssl/rand.h>
120 #include <openssl/x509.h>
121 #include <openssl/ssl.h>
122 #include <openssl/bn.h>
123 #ifndef OPENSSL_NO_DH
124 # include <openssl/dh.h>
125 #endif
126 #include "s_apps.h"
127
128 #define COOKIE_SECRET_LENGTH 16
129
130 int verify_depth = 0;
131 int verify_quiet = 0;
132 int verify_error = X509_V_OK;
133 int verify_return_error = 0;
134 unsigned char cookie_secret[COOKIE_SECRET_LENGTH];
135 int cookie_initialized = 0;
136
137 int verify_callback(int ok, X509_STORE_CTX *ctx)
138 {
139 X509 *err_cert;
140 int err, depth;
141
142 err_cert = X509_STORE_CTX_get_current_cert(ctx);
143 err = X509_STORE_CTX_get_error(ctx);
144 depth = X509_STORE_CTX_get_error_depth(ctx);
145
146 if (!verify_quiet || !ok) {
147 BIO_printf(bio_err, "depth=%d ", depth);
148 if (err_cert) {
149 X509_NAME_print_ex(bio_err,
150 X509_get_subject_name(err_cert),
151 0, XN_FLAG_ONELINE);
152 BIO_puts(bio_err, "\n");
153 } else
154 BIO_puts(bio_err, "<no cert>\n");
155 }
156 if (!ok) {
157 BIO_printf(bio_err, "verify error:num=%d:%s\n", err,
158 X509_verify_cert_error_string(err));
159 if (verify_depth >= depth) {
160 if (!verify_return_error)
161 ok = 1;
162 verify_error = X509_V_OK;
163 } else {
164 ok = 0;
165 verify_error = X509_V_ERR_CERT_CHAIN_TOO_LONG;
166 }
167 }
168 switch (err) {
169 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
170 BIO_puts(bio_err, "issuer= ");
171 X509_NAME_print_ex(bio_err, X509_get_issuer_name(err_cert),
172 0, XN_FLAG_ONELINE);
173 BIO_puts(bio_err, "\n");
174 break;
175 case X509_V_ERR_CERT_NOT_YET_VALID:
176 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
177 BIO_printf(bio_err, "notBefore=");
178 ASN1_TIME_print(bio_err, X509_get_notBefore(err_cert));
179 BIO_printf(bio_err, "\n");
180 break;
181 case X509_V_ERR_CERT_HAS_EXPIRED:
182 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
183 BIO_printf(bio_err, "notAfter=");
184 ASN1_TIME_print(bio_err, X509_get_notAfter(err_cert));
185 BIO_printf(bio_err, "\n");
186 break;
187 case X509_V_ERR_NO_EXPLICIT_POLICY:
188 if (!verify_quiet)
189 policies_print(ctx);
190 break;
191 }
192 if (err == X509_V_OK && ok == 2 && !verify_quiet)
193 policies_print(ctx);
194 if (ok && !verify_quiet)
195 BIO_printf(bio_err, "verify return:%d\n", ok);
196 return (ok);
197 }
198
199 int set_cert_stuff(SSL_CTX *ctx, char *cert_file, char *key_file)
200 {
201 if (cert_file != NULL) {
202 if (SSL_CTX_use_certificate_file(ctx, cert_file,
203 SSL_FILETYPE_PEM) <= 0) {
204 BIO_printf(bio_err, "unable to get certificate from '%s'\n",
205 cert_file);
206 ERR_print_errors(bio_err);
207 return (0);
208 }
209 if (key_file == NULL)
210 key_file = cert_file;
211 if (SSL_CTX_use_PrivateKey_file(ctx, key_file, SSL_FILETYPE_PEM) <= 0) {
212 BIO_printf(bio_err, "unable to get private key from '%s'\n",
213 key_file);
214 ERR_print_errors(bio_err);
215 return (0);
216 }
217
218 /*
219 * If we are using DSA, we can copy the parameters from the private
220 * key
221 */
222
223 /*
224 * Now we know that a key and cert have been set against the SSL
225 * context
226 */
227 if (!SSL_CTX_check_private_key(ctx)) {
228 BIO_printf(bio_err,
229 "Private key does not match the certificate public key\n");
230 return (0);
231 }
232 }
233 return (1);
234 }
235
236 int set_cert_key_stuff(SSL_CTX *ctx, X509 *cert, EVP_PKEY *key,
237 STACK_OF(X509) *chain, int build_chain)
238 {
239 int chflags = chain ? SSL_BUILD_CHAIN_FLAG_CHECK : 0;
240 if (cert == NULL)
241 return 1;
242 if (SSL_CTX_use_certificate(ctx, cert) <= 0) {
243 BIO_printf(bio_err, "error setting certificate\n");
244 ERR_print_errors(bio_err);
245 return 0;
246 }
247
248 if (SSL_CTX_use_PrivateKey(ctx, key) <= 0) {
249 BIO_printf(bio_err, "error setting private key\n");
250 ERR_print_errors(bio_err);
251 return 0;
252 }
253
254 /*
255 * Now we know that a key and cert have been set against the SSL context
256 */
257 if (!SSL_CTX_check_private_key(ctx)) {
258 BIO_printf(bio_err,
259 "Private key does not match the certificate public key\n");
260 return 0;
261 }
262 if (chain && !SSL_CTX_set1_chain(ctx, chain)) {
263 BIO_printf(bio_err, "error setting certificate chain\n");
264 ERR_print_errors(bio_err);
265 return 0;
266 }
267 if (build_chain && !SSL_CTX_build_cert_chain(ctx, chflags)) {
268 BIO_printf(bio_err, "error building certificate chain\n");
269 ERR_print_errors(bio_err);
270 return 0;
271 }
272 return 1;
273 }
274
275 static void ssl_print_client_cert_types(BIO *bio, SSL *s)
276 {
277 const unsigned char *p;
278 int i;
279 int cert_type_num = SSL_get0_certificate_types(s, &p);
280 if (!cert_type_num)
281 return;
282 BIO_puts(bio, "Client Certificate Types: ");
283 for (i = 0; i < cert_type_num; i++) {
284 unsigned char cert_type = p[i];
285 char *cname;
286 switch (cert_type) {
287 case TLS_CT_RSA_SIGN:
288 cname = "RSA sign";
289 break;
290
291 case TLS_CT_DSS_SIGN:
292 cname = "DSA sign";
293 break;
294
295 case TLS_CT_RSA_FIXED_DH:
296 cname = "RSA fixed DH";
297 break;
298
299 case TLS_CT_DSS_FIXED_DH:
300 cname = "DSS fixed DH";
301 break;
302
303 case TLS_CT_ECDSA_SIGN:
304 cname = "ECDSA sign";
305 break;
306
307 case TLS_CT_RSA_FIXED_ECDH:
308 cname = "RSA fixed ECDH";
309 break;
310
311 case TLS_CT_ECDSA_FIXED_ECDH:
312 cname = "ECDSA fixed ECDH";
313 break;
314
315 case TLS_CT_GOST94_SIGN:
316 cname = "GOST94 Sign";
317 break;
318
319 case TLS_CT_GOST01_SIGN:
320 cname = "GOST01 Sign";
321 break;
322
323 default:
324 cname = NULL;
325 }
326
327 if (i)
328 BIO_puts(bio, ", ");
329
330 if (cname)
331 BIO_puts(bio, cname);
332 else
333 BIO_printf(bio, "UNKNOWN (%d),", cert_type);
334 }
335 BIO_puts(bio, "\n");
336 }
337
338 static int do_print_sigalgs(BIO *out, SSL *s, int shared)
339 {
340 int i, nsig, client;
341 client = SSL_is_server(s) ? 0 : 1;
342 if (shared)
343 nsig = SSL_get_shared_sigalgs(s, -1, NULL, NULL, NULL, NULL, NULL);
344 else
345 nsig = SSL_get_sigalgs(s, -1, NULL, NULL, NULL, NULL, NULL);
346 if (nsig == 0)
347 return 1;
348
349 if (shared)
350 BIO_puts(out, "Shared ");
351
352 if (client)
353 BIO_puts(out, "Requested ");
354 BIO_puts(out, "Signature Algorithms: ");
355 for (i = 0; i < nsig; i++) {
356 int hash_nid, sign_nid;
357 unsigned char rhash, rsign;
358 const char *sstr = NULL;
359 if (shared)
360 SSL_get_shared_sigalgs(s, i, &sign_nid, &hash_nid, NULL,
361 &rsign, &rhash);
362 else
363 SSL_get_sigalgs(s, i, &sign_nid, &hash_nid, NULL, &rsign, &rhash);
364 if (i)
365 BIO_puts(out, ":");
366 if (sign_nid == EVP_PKEY_RSA)
367 sstr = "RSA";
368 else if (sign_nid == EVP_PKEY_DSA)
369 sstr = "DSA";
370 else if (sign_nid == EVP_PKEY_EC)
371 sstr = "ECDSA";
372 if (sstr)
373 BIO_printf(out, "%s+", sstr);
374 else
375 BIO_printf(out, "0x%02X+", (int)rsign);
376 if (hash_nid != NID_undef)
377 BIO_printf(out, "%s", OBJ_nid2sn(hash_nid));
378 else
379 BIO_printf(out, "0x%02X", (int)rhash);
380 }
381 BIO_puts(out, "\n");
382 return 1;
383 }
384
385 int ssl_print_sigalgs(BIO *out, SSL *s)
386 {
387 int mdnid;
388 if (!SSL_is_server(s))
389 ssl_print_client_cert_types(out, s);
390 do_print_sigalgs(out, s, 0);
391 do_print_sigalgs(out, s, 1);
392 if (SSL_get_peer_signature_nid(s, &mdnid))
393 BIO_printf(out, "Peer signing digest: %s\n", OBJ_nid2sn(mdnid));
394 return 1;
395 }
396
397 #ifndef OPENSSL_NO_EC
398 int ssl_print_point_formats(BIO *out, SSL *s)
399 {
400 int i, nformats;
401 const char *pformats;
402 nformats = SSL_get0_ec_point_formats(s, &pformats);
403 if (nformats <= 0)
404 return 1;
405 BIO_puts(out, "Supported Elliptic Curve Point Formats: ");
406 for (i = 0; i < nformats; i++, pformats++) {
407 if (i)
408 BIO_puts(out, ":");
409 switch (*pformats) {
410 case TLSEXT_ECPOINTFORMAT_uncompressed:
411 BIO_puts(out, "uncompressed");
412 break;
413
414 case TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime:
415 BIO_puts(out, "ansiX962_compressed_prime");
416 break;
417
418 case TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2:
419 BIO_puts(out, "ansiX962_compressed_char2");
420 break;
421
422 default:
423 BIO_printf(out, "unknown(%d)", (int)*pformats);
424 break;
425
426 }
427 }
428 if (nformats <= 0)
429 BIO_puts(out, "NONE");
430 BIO_puts(out, "\n");
431 return 1;
432 }
433
434 int ssl_print_curves(BIO *out, SSL *s, int noshared)
435 {
436 int i, ncurves, *curves, nid;
437 const char *cname;
438
439 ncurves = SSL_get1_curves(s, NULL);
440 if (ncurves <= 0)
441 return 1;
442 curves = app_malloc(ncurves * sizeof(int), "curves to print");
443 SSL_get1_curves(s, curves);
444
445 BIO_puts(out, "Supported Elliptic Curves: ");
446 for (i = 0; i < ncurves; i++) {
447 if (i)
448 BIO_puts(out, ":");
449 nid = curves[i];
450 /* If unrecognised print out hex version */
451 if (nid & TLSEXT_nid_unknown)
452 BIO_printf(out, "0x%04X", nid & 0xFFFF);
453 else {
454 /* Use NIST name for curve if it exists */
455 cname = EC_curve_nid2nist(nid);
456 if (!cname)
457 cname = OBJ_nid2sn(nid);
458 BIO_printf(out, "%s", cname);
459 }
460 }
461 if (ncurves == 0)
462 BIO_puts(out, "NONE");
463 OPENSSL_free(curves);
464 if (noshared) {
465 BIO_puts(out, "\n");
466 return 1;
467 }
468 BIO_puts(out, "\nShared Elliptic curves: ");
469 ncurves = SSL_get_shared_curve(s, -1);
470 for (i = 0; i < ncurves; i++) {
471 if (i)
472 BIO_puts(out, ":");
473 nid = SSL_get_shared_curve(s, i);
474 cname = EC_curve_nid2nist(nid);
475 if (!cname)
476 cname = OBJ_nid2sn(nid);
477 BIO_printf(out, "%s", cname);
478 }
479 if (ncurves == 0)
480 BIO_puts(out, "NONE");
481 BIO_puts(out, "\n");
482 return 1;
483 }
484 #endif
485 int ssl_print_tmp_key(BIO *out, SSL *s)
486 {
487 EVP_PKEY *key;
488 if (!SSL_get_server_tmp_key(s, &key))
489 return 1;
490 BIO_puts(out, "Server Temp Key: ");
491 switch (EVP_PKEY_id(key)) {
492 case EVP_PKEY_RSA:
493 BIO_printf(out, "RSA, %d bits\n", EVP_PKEY_bits(key));
494 break;
495
496 case EVP_PKEY_DH:
497 BIO_printf(out, "DH, %d bits\n", EVP_PKEY_bits(key));
498 break;
499 #ifndef OPENSSL_NO_EC
500 case EVP_PKEY_EC:
501 {
502 EC_KEY *ec = EVP_PKEY_get1_EC_KEY(key);
503 int nid;
504 const char *cname;
505 nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
506 EC_KEY_free(ec);
507 cname = EC_curve_nid2nist(nid);
508 if (!cname)
509 cname = OBJ_nid2sn(nid);
510 BIO_printf(out, "ECDH, %s, %d bits\n", cname, EVP_PKEY_bits(key));
511 }
512 #endif
513 }
514 EVP_PKEY_free(key);
515 return 1;
516 }
517
518 long bio_dump_callback(BIO *bio, int cmd, const char *argp,
519 int argi, long argl, long ret)
520 {
521 BIO *out;
522
523 out = (BIO *)BIO_get_callback_arg(bio);
524 if (out == NULL)
525 return (ret);
526
527 if (cmd == (BIO_CB_READ | BIO_CB_RETURN)) {
528 BIO_printf(out, "read from %p [%p] (%lu bytes => %ld (0x%lX))\n",
529 (void *)bio, argp, (unsigned long)argi, ret, ret);
530 BIO_dump(out, argp, (int)ret);
531 return (ret);
532 } else if (cmd == (BIO_CB_WRITE | BIO_CB_RETURN)) {
533 BIO_printf(out, "write to %p [%p] (%lu bytes => %ld (0x%lX))\n",
534 (void *)bio, argp, (unsigned long)argi, ret, ret);
535 BIO_dump(out, argp, (int)ret);
536 }
537 return (ret);
538 }
539
540 void apps_ssl_info_callback(const SSL *s, int where, int ret)
541 {
542 const char *str;
543 int w;
544
545 w = where & ~SSL_ST_MASK;
546
547 if (w & SSL_ST_CONNECT)
548 str = "SSL_connect";
549 else if (w & SSL_ST_ACCEPT)
550 str = "SSL_accept";
551 else
552 str = "undefined";
553
554 if (where & SSL_CB_LOOP) {
555 BIO_printf(bio_err, "%s:%s\n", str, SSL_state_string_long(s));
556 } else if (where & SSL_CB_ALERT) {
557 str = (where & SSL_CB_READ) ? "read" : "write";
558 BIO_printf(bio_err, "SSL3 alert %s:%s:%s\n",
559 str,
560 SSL_alert_type_string_long(ret),
561 SSL_alert_desc_string_long(ret));
562 } else if (where & SSL_CB_EXIT) {
563 if (ret == 0)
564 BIO_printf(bio_err, "%s:failed in %s\n",
565 str, SSL_state_string_long(s));
566 else if (ret < 0) {
567 BIO_printf(bio_err, "%s:error in %s\n",
568 str, SSL_state_string_long(s));
569 }
570 }
571 }
572
573 static const char *ssl_version_str(int version)
574 {
575 switch (version) {
576 case SSL3_VERSION:
577 return "SSL 3.0";
578 case TLS1_VERSION:
579 return "TLS 1.0";
580 case TLS1_1_VERSION:
581 return "TLS 1.1";
582 case TLS1_2_VERSION:
583 return "TLS 1.2";
584 case DTLS1_VERSION:
585 return "DTLS 1.0";
586 case DTLS1_BAD_VER:
587 return "DTLS 1.0 (bad)";
588 default:
589 return "???";
590 }
591 }
592
593 void msg_cb(int write_p, int version, int content_type, const void *buf,
594 size_t len, SSL *ssl, void *arg)
595 {
596 BIO *bio = arg;
597 const char *str_write_p, *str_version, *str_content_type =
598 "", *str_details1 = "", *str_details2 = "";
599
600 str_write_p = write_p ? ">>>" : "<<<";
601
602 str_version = ssl_version_str(version);
603
604 if (version == SSL3_VERSION ||
605 version == TLS1_VERSION ||
606 version == TLS1_1_VERSION ||
607 version == TLS1_2_VERSION ||
608 version == DTLS1_VERSION || version == DTLS1_BAD_VER) {
609 switch (content_type) {
610 case 20:
611 str_content_type = "ChangeCipherSpec";
612 break;
613 case 21:
614 str_content_type = "Alert";
615 break;
616 case 22:
617 str_content_type = "Handshake";
618 break;
619 }
620
621 if (content_type == 21) { /* Alert */
622 str_details1 = ", ???";
623
624 if (len == 2) {
625 switch (((const unsigned char *)buf)[0]) {
626 case 1:
627 str_details1 = ", warning";
628 break;
629 case 2:
630 str_details1 = ", fatal";
631 break;
632 }
633
634 str_details2 = " ???";
635 switch (((const unsigned char *)buf)[1]) {
636 case 0:
637 str_details2 = " close_notify";
638 break;
639 case 10:
640 str_details2 = " unexpected_message";
641 break;
642 case 20:
643 str_details2 = " bad_record_mac";
644 break;
645 case 21:
646 str_details2 = " decryption_failed";
647 break;
648 case 22:
649 str_details2 = " record_overflow";
650 break;
651 case 30:
652 str_details2 = " decompression_failure";
653 break;
654 case 40:
655 str_details2 = " handshake_failure";
656 break;
657 case 42:
658 str_details2 = " bad_certificate";
659 break;
660 case 43:
661 str_details2 = " unsupported_certificate";
662 break;
663 case 44:
664 str_details2 = " certificate_revoked";
665 break;
666 case 45:
667 str_details2 = " certificate_expired";
668 break;
669 case 46:
670 str_details2 = " certificate_unknown";
671 break;
672 case 47:
673 str_details2 = " illegal_parameter";
674 break;
675 case 48:
676 str_details2 = " unknown_ca";
677 break;
678 case 49:
679 str_details2 = " access_denied";
680 break;
681 case 50:
682 str_details2 = " decode_error";
683 break;
684 case 51:
685 str_details2 = " decrypt_error";
686 break;
687 case 60:
688 str_details2 = " export_restriction";
689 break;
690 case 70:
691 str_details2 = " protocol_version";
692 break;
693 case 71:
694 str_details2 = " insufficient_security";
695 break;
696 case 80:
697 str_details2 = " internal_error";
698 break;
699 case 90:
700 str_details2 = " user_canceled";
701 break;
702 case 100:
703 str_details2 = " no_renegotiation";
704 break;
705 case 110:
706 str_details2 = " unsupported_extension";
707 break;
708 case 111:
709 str_details2 = " certificate_unobtainable";
710 break;
711 case 112:
712 str_details2 = " unrecognized_name";
713 break;
714 case 113:
715 str_details2 = " bad_certificate_status_response";
716 break;
717 case 114:
718 str_details2 = " bad_certificate_hash_value";
719 break;
720 case 115:
721 str_details2 = " unknown_psk_identity";
722 break;
723 }
724 }
725 }
726
727 if (content_type == 22) { /* Handshake */
728 str_details1 = "???";
729
730 if (len > 0) {
731 switch (((const unsigned char *)buf)[0]) {
732 case 0:
733 str_details1 = ", HelloRequest";
734 break;
735 case 1:
736 str_details1 = ", ClientHello";
737 break;
738 case 2:
739 str_details1 = ", ServerHello";
740 break;
741 case 3:
742 str_details1 = ", HelloVerifyRequest";
743 break;
744 case 11:
745 str_details1 = ", Certificate";
746 break;
747 case 12:
748 str_details1 = ", ServerKeyExchange";
749 break;
750 case 13:
751 str_details1 = ", CertificateRequest";
752 break;
753 case 14:
754 str_details1 = ", ServerHelloDone";
755 break;
756 case 15:
757 str_details1 = ", CertificateVerify";
758 break;
759 case 16:
760 str_details1 = ", ClientKeyExchange";
761 break;
762 case 20:
763 str_details1 = ", Finished";
764 break;
765 }
766 }
767 }
768 #ifndef OPENSSL_NO_HEARTBEATS
769 if (content_type == 24) { /* Heartbeat */
770 str_details1 = ", Heartbeat";
771
772 if (len > 0) {
773 switch (((const unsigned char *)buf)[0]) {
774 case 1:
775 str_details1 = ", HeartbeatRequest";
776 break;
777 case 2:
778 str_details1 = ", HeartbeatResponse";
779 break;
780 }
781 }
782 }
783 #endif
784 }
785
786 BIO_printf(bio, "%s %s%s [length %04lx]%s%s\n", str_write_p, str_version,
787 str_content_type, (unsigned long)len, str_details1,
788 str_details2);
789
790 if (len > 0) {
791 size_t num, i;
792
793 BIO_printf(bio, " ");
794 num = len;
795 for (i = 0; i < num; i++) {
796 if (i % 16 == 0 && i > 0)
797 BIO_printf(bio, "\n ");
798 BIO_printf(bio, " %02x", ((const unsigned char *)buf)[i]);
799 }
800 if (i < len)
801 BIO_printf(bio, " ...");
802 BIO_printf(bio, "\n");
803 }
804 (void)BIO_flush(bio);
805 }
806
807 void tlsext_cb(SSL *s, int client_server, int type,
808 unsigned char *data, int len, void *arg)
809 {
810 BIO *bio = arg;
811 char *extname;
812
813 switch (type) {
814 case TLSEXT_TYPE_server_name:
815 extname = "server name";
816 break;
817
818 case TLSEXT_TYPE_max_fragment_length:
819 extname = "max fragment length";
820 break;
821
822 case TLSEXT_TYPE_client_certificate_url:
823 extname = "client certificate URL";
824 break;
825
826 case TLSEXT_TYPE_trusted_ca_keys:
827 extname = "trusted CA keys";
828 break;
829
830 case TLSEXT_TYPE_truncated_hmac:
831 extname = "truncated HMAC";
832 break;
833
834 case TLSEXT_TYPE_status_request:
835 extname = "status request";
836 break;
837
838 case TLSEXT_TYPE_user_mapping:
839 extname = "user mapping";
840 break;
841
842 case TLSEXT_TYPE_client_authz:
843 extname = "client authz";
844 break;
845
846 case TLSEXT_TYPE_server_authz:
847 extname = "server authz";
848 break;
849
850 case TLSEXT_TYPE_cert_type:
851 extname = "cert type";
852 break;
853
854 case TLSEXT_TYPE_elliptic_curves:
855 extname = "elliptic curves";
856 break;
857
858 case TLSEXT_TYPE_ec_point_formats:
859 extname = "EC point formats";
860 break;
861
862 case TLSEXT_TYPE_srp:
863 extname = "SRP";
864 break;
865
866 case TLSEXT_TYPE_signature_algorithms:
867 extname = "signature algorithms";
868 break;
869
870 case TLSEXT_TYPE_use_srtp:
871 extname = "use SRTP";
872 break;
873
874 case TLSEXT_TYPE_heartbeat:
875 extname = "heartbeat";
876 break;
877
878 case TLSEXT_TYPE_session_ticket:
879 extname = "session ticket";
880 break;
881
882 case TLSEXT_TYPE_renegotiate:
883 extname = "renegotiation info";
884 break;
885
886 #ifdef TLSEXT_TYPE_next_proto_neg
887 case TLSEXT_TYPE_next_proto_neg:
888 extname = "next protocol";
889 break;
890 #endif
891 #ifdef TLSEXT_TYPE_encrypt_then_mac
892 case TLSEXT_TYPE_encrypt_then_mac:
893 extname = "encrypt-then-mac";
894 break;
895 #endif
896 case TLSEXT_TYPE_padding:
897 extname = "TLS padding";
898 break;
899
900 default:
901 extname = "unknown";
902 break;
903
904 }
905
906 BIO_printf(bio, "TLS %s extension \"%s\" (id=%d), len=%d\n",
907 client_server ? "server" : "client", extname, type, len);
908 BIO_dump(bio, (char *)data, len);
909 (void)BIO_flush(bio);
910 }
911
912 int generate_cookie_callback(SSL *ssl, unsigned char *cookie,
913 unsigned int *cookie_len)
914 {
915 unsigned char *buffer, result[EVP_MAX_MD_SIZE];
916 unsigned int length, resultlength;
917 union {
918 struct sockaddr sa;
919 struct sockaddr_in s4;
920 #if OPENSSL_USE_IPV6
921 struct sockaddr_in6 s6;
922 #endif
923 } peer;
924
925 /* Initialize a random secret */
926 if (!cookie_initialized) {
927 if (RAND_bytes(cookie_secret, COOKIE_SECRET_LENGTH) <= 0) {
928 BIO_printf(bio_err, "error setting random cookie secret\n");
929 return 0;
930 }
931 cookie_initialized = 1;
932 }
933
934 /* Read peer information */
935 (void)BIO_dgram_get_peer(SSL_get_rbio(ssl), &peer);
936
937 /* Create buffer with peer's address and port */
938 length = 0;
939 switch (peer.sa.sa_family) {
940 case AF_INET:
941 length += sizeof(struct in_addr);
942 length += sizeof(peer.s4.sin_port);
943 break;
944 #if OPENSSL_USE_IPV6
945 case AF_INET6:
946 length += sizeof(struct in6_addr);
947 length += sizeof(peer.s6.sin6_port);
948 break;
949 #endif
950 default:
951 OPENSSL_assert(0);
952 break;
953 }
954 buffer = app_malloc(length, "cookie generate buffer");
955
956 switch (peer.sa.sa_family) {
957 case AF_INET:
958 memcpy(buffer, &peer.s4.sin_port, sizeof(peer.s4.sin_port));
959 memcpy(buffer + sizeof(peer.s4.sin_port),
960 &peer.s4.sin_addr, sizeof(struct in_addr));
961 break;
962 #if OPENSSL_USE_IPV6
963 case AF_INET6:
964 memcpy(buffer, &peer.s6.sin6_port, sizeof(peer.s6.sin6_port));
965 memcpy(buffer + sizeof(peer.s6.sin6_port),
966 &peer.s6.sin6_addr, sizeof(struct in6_addr));
967 break;
968 #endif
969 default:
970 OPENSSL_assert(0);
971 break;
972 }
973
974 /* Calculate HMAC of buffer using the secret */
975 HMAC(EVP_sha1(), cookie_secret, COOKIE_SECRET_LENGTH,
976 buffer, length, result, &resultlength);
977 OPENSSL_free(buffer);
978
979 memcpy(cookie, result, resultlength);
980 *cookie_len = resultlength;
981
982 return 1;
983 }
984
985 int verify_cookie_callback(SSL *ssl, unsigned char *cookie,
986 unsigned int cookie_len)
987 {
988 unsigned char *buffer, result[EVP_MAX_MD_SIZE];
989 unsigned int length, resultlength;
990 union {
991 struct sockaddr sa;
992 struct sockaddr_in s4;
993 #if OPENSSL_USE_IPV6
994 struct sockaddr_in6 s6;
995 #endif
996 } peer;
997
998 /* If secret isn't initialized yet, the cookie can't be valid */
999 if (!cookie_initialized)
1000 return 0;
1001
1002 /* Read peer information */
1003 (void)BIO_dgram_get_peer(SSL_get_rbio(ssl), &peer);
1004
1005 /* Create buffer with peer's address and port */
1006 length = 0;
1007 switch (peer.sa.sa_family) {
1008 case AF_INET:
1009 length += sizeof(struct in_addr);
1010 length += sizeof(peer.s4.sin_port);
1011 break;
1012 #if OPENSSL_USE_IPV6
1013 case AF_INET6:
1014 length += sizeof(struct in6_addr);
1015 length += sizeof(peer.s6.sin6_port);
1016 break;
1017 #endif
1018 default:
1019 OPENSSL_assert(0);
1020 break;
1021 }
1022 buffer = app_malloc(length, "cookie verify buffer");
1023
1024 switch (peer.sa.sa_family) {
1025 case AF_INET:
1026 memcpy(buffer, &peer.s4.sin_port, sizeof(peer.s4.sin_port));
1027 memcpy(buffer + sizeof(peer.s4.sin_port),
1028 &peer.s4.sin_addr, sizeof(struct in_addr));
1029 break;
1030 #if OPENSSL_USE_IPV6
1031 case AF_INET6:
1032 memcpy(buffer, &peer.s6.sin6_port, sizeof(peer.s6.sin6_port));
1033 memcpy(buffer + sizeof(peer.s6.sin6_port),
1034 &peer.s6.sin6_addr, sizeof(struct in6_addr));
1035 break;
1036 #endif
1037 default:
1038 OPENSSL_assert(0);
1039 break;
1040 }
1041
1042 /* Calculate HMAC of buffer using the secret */
1043 HMAC(EVP_sha1(), cookie_secret, COOKIE_SECRET_LENGTH,
1044 buffer, length, result, &resultlength);
1045 OPENSSL_free(buffer);
1046
1047 if (cookie_len == resultlength
1048 && memcmp(result, cookie, resultlength) == 0)
1049 return 1;
1050
1051 return 0;
1052 }
1053
1054 /*
1055 * Example of extended certificate handling. Where the standard support of
1056 * one certificate per algorithm is not sufficient an application can decide
1057 * which certificate(s) to use at runtime based on whatever criteria it deems
1058 * appropriate.
1059 */
1060
1061 /* Linked list of certificates, keys and chains */
1062 struct ssl_excert_st {
1063 int certform;
1064 const char *certfile;
1065 int keyform;
1066 const char *keyfile;
1067 const char *chainfile;
1068 X509 *cert;
1069 EVP_PKEY *key;
1070 STACK_OF(X509) *chain;
1071 int build_chain;
1072 struct ssl_excert_st *next, *prev;
1073 };
1074
1075 struct chain_flags {
1076 int flag;
1077 const char *name;
1078 };
1079
1080 struct chain_flags chain_flags_list[] = {
1081 {CERT_PKEY_VALID, "Overall Validity"},
1082 {CERT_PKEY_SIGN, "Sign with EE key"},
1083 {CERT_PKEY_EE_SIGNATURE, "EE signature"},
1084 {CERT_PKEY_CA_SIGNATURE, "CA signature"},
1085 {CERT_PKEY_EE_PARAM, "EE key parameters"},
1086 {CERT_PKEY_CA_PARAM, "CA key parameters"},
1087 {CERT_PKEY_EXPLICIT_SIGN, "Explicity sign with EE key"},
1088 {CERT_PKEY_ISSUER_NAME, "Issuer Name"},
1089 {CERT_PKEY_CERT_TYPE, "Certificate Type"},
1090 {0, NULL}
1091 };
1092
1093 static void print_chain_flags(SSL *s, int flags)
1094 {
1095 struct chain_flags *ctmp = chain_flags_list;
1096
1097 while (ctmp->name) {
1098 BIO_printf(bio_err, "\t%s: %s\n", ctmp->name,
1099 flags & ctmp->flag ? "OK" : "NOT OK");
1100 ctmp++;
1101 }
1102 BIO_printf(bio_err, "\tSuite B: ");
1103 if (SSL_set_cert_flags(s, 0) & SSL_CERT_FLAG_SUITEB_128_LOS)
1104 BIO_puts(bio_err, flags & CERT_PKEY_SUITEB ? "OK\n" : "NOT OK\n");
1105 else
1106 BIO_printf(bio_err, "not tested\n");
1107 }
1108
1109 /*
1110 * Very basic selection callback: just use any certificate chain reported as
1111 * valid. More sophisticated could prioritise according to local policy.
1112 */
1113 static int set_cert_cb(SSL *ssl, void *arg)
1114 {
1115 int i, rv;
1116 SSL_EXCERT *exc = arg;
1117 #ifdef CERT_CB_TEST_RETRY
1118 static int retry_cnt;
1119 if (retry_cnt < 5) {
1120 retry_cnt++;
1121 fprintf(stderr, "Certificate callback retry test: count %d\n",
1122 retry_cnt);
1123 return -1;
1124 }
1125 #endif
1126 SSL_certs_clear(ssl);
1127
1128 if (!exc)
1129 return 1;
1130
1131 /*
1132 * Go to end of list and traverse backwards since we prepend newer
1133 * entries this retains the original order.
1134 */
1135 while (exc->next)
1136 exc = exc->next;
1137
1138 i = 0;
1139
1140 while (exc) {
1141 i++;
1142 rv = SSL_check_chain(ssl, exc->cert, exc->key, exc->chain);
1143 BIO_printf(bio_err, "Checking cert chain %d:\nSubject: ", i);
1144 X509_NAME_print_ex(bio_err, X509_get_subject_name(exc->cert), 0,
1145 XN_FLAG_ONELINE);
1146 BIO_puts(bio_err, "\n");
1147 print_chain_flags(ssl, rv);
1148 if (rv & CERT_PKEY_VALID) {
1149 if (!SSL_use_certificate(ssl, exc->cert)
1150 || !SSL_use_PrivateKey(ssl, exc->key)) {
1151 return 0;
1152 }
1153 /*
1154 * NB: we wouldn't normally do this as it is not efficient
1155 * building chains on each connection better to cache the chain
1156 * in advance.
1157 */
1158 if (exc->build_chain) {
1159 if (!SSL_build_cert_chain(ssl, 0))
1160 return 0;
1161 } else if (exc->chain)
1162 SSL_set1_chain(ssl, exc->chain);
1163 }
1164 exc = exc->prev;
1165 }
1166 return 1;
1167 }
1168
1169 void ssl_ctx_set_excert(SSL_CTX *ctx, SSL_EXCERT *exc)
1170 {
1171 SSL_CTX_set_cert_cb(ctx, set_cert_cb, exc);
1172 }
1173
1174 static int ssl_excert_prepend(SSL_EXCERT **pexc)
1175 {
1176 SSL_EXCERT *exc = app_malloc(sizeof(*exc), "prepend cert");
1177
1178 exc->certfile = NULL;
1179 exc->keyfile = NULL;
1180 exc->chainfile = NULL;
1181 exc->cert = NULL;
1182 exc->key = NULL;
1183 exc->chain = NULL;
1184 exc->prev = NULL;
1185 exc->build_chain = 0;
1186
1187 exc->next = *pexc;
1188 *pexc = exc;
1189
1190 if (exc->next) {
1191 exc->certform = exc->next->certform;
1192 exc->keyform = exc->next->keyform;
1193 exc->next->prev = exc;
1194 } else {
1195 exc->certform = FORMAT_PEM;
1196 exc->keyform = FORMAT_PEM;
1197 }
1198 return 1;
1199
1200 }
1201
1202 void ssl_excert_free(SSL_EXCERT *exc)
1203 {
1204 SSL_EXCERT *curr;
1205
1206 if (!exc)
1207 return;
1208 while (exc) {
1209 X509_free(exc->cert);
1210 EVP_PKEY_free(exc->key);
1211 sk_X509_pop_free(exc->chain, X509_free);
1212 curr = exc;
1213 exc = exc->next;
1214 OPENSSL_free(curr);
1215 }
1216 }
1217
1218 int load_excert(SSL_EXCERT **pexc)
1219 {
1220 SSL_EXCERT *exc = *pexc;
1221 if (!exc)
1222 return 1;
1223 /* If nothing in list, free and set to NULL */
1224 if (!exc->certfile && !exc->next) {
1225 ssl_excert_free(exc);
1226 *pexc = NULL;
1227 return 1;
1228 }
1229 for (; exc; exc = exc->next) {
1230 if (!exc->certfile) {
1231 BIO_printf(bio_err, "Missing filename\n");
1232 return 0;
1233 }
1234 exc->cert = load_cert(exc->certfile, exc->certform,
1235 NULL, NULL, "Server Certificate");
1236 if (!exc->cert)
1237 return 0;
1238 if (exc->keyfile) {
1239 exc->key = load_key(exc->keyfile, exc->keyform,
1240 0, NULL, NULL, "Server Key");
1241 } else {
1242 exc->key = load_key(exc->certfile, exc->certform,
1243 0, NULL, NULL, "Server Key");
1244 }
1245 if (!exc->key)
1246 return 0;
1247 if (exc->chainfile) {
1248 exc->chain = load_certs(exc->chainfile, FORMAT_PEM,
1249 NULL, NULL, "Server Chain");
1250 if (!exc->chain)
1251 return 0;
1252 }
1253 }
1254 return 1;
1255 }
1256
1257 enum range { OPT_X_ENUM };
1258
1259 int args_excert(int opt, SSL_EXCERT **pexc)
1260 {
1261 SSL_EXCERT *exc = *pexc;
1262
1263 assert(opt > OPT_X__FIRST);
1264 assert(opt < OPT_X__LAST);
1265
1266 if (exc == NULL) {
1267 if (!ssl_excert_prepend(&exc)) {
1268 BIO_printf(bio_err, " %s: Error initialising xcert\n",
1269 opt_getprog());
1270 goto err;
1271 }
1272 *pexc = exc;
1273 }
1274
1275 switch ((enum range)opt) {
1276 case OPT_X__FIRST:
1277 case OPT_X__LAST:
1278 return 0;
1279 case OPT_X_CERT:
1280 if (exc->certfile && !ssl_excert_prepend(&exc)) {
1281 BIO_printf(bio_err, "%s: Error adding xcert\n", opt_getprog());
1282 goto err;
1283 }
1284 exc->certfile = opt_arg();
1285 break;
1286 case OPT_X_KEY:
1287 if (exc->keyfile) {
1288 BIO_printf(bio_err, "%s: Key already specified\n", opt_getprog());
1289 goto err;
1290 }
1291 exc->keyfile = opt_arg();
1292 break;
1293 case OPT_X_CHAIN:
1294 if (exc->chainfile) {
1295 BIO_printf(bio_err, "%s: Chain already specified\n",
1296 opt_getprog());
1297 goto err;
1298 }
1299 exc->chainfile = opt_arg();
1300 break;
1301 case OPT_X_CHAIN_BUILD:
1302 exc->build_chain = 1;
1303 break;
1304 case OPT_X_CERTFORM:
1305 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &exc->certform))
1306 return 0;
1307 break;
1308 case OPT_X_KEYFORM:
1309 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &exc->keyform))
1310 return 0;
1311 break;
1312 }
1313 return 1;
1314
1315 err:
1316 ERR_print_errors(bio_err);
1317 ssl_excert_free(exc);
1318 *pexc = NULL;
1319 return 0;
1320 }
1321
1322 static void print_raw_cipherlist(SSL *s)
1323 {
1324 const unsigned char *rlist;
1325 static const unsigned char scsv_id[] = { 0, 0, 0xFF };
1326 size_t i, rlistlen, num;
1327 if (!SSL_is_server(s))
1328 return;
1329 num = SSL_get0_raw_cipherlist(s, NULL);
1330 rlistlen = SSL_get0_raw_cipherlist(s, &rlist);
1331 BIO_puts(bio_err, "Client cipher list: ");
1332 for (i = 0; i < rlistlen; i += num, rlist += num) {
1333 const SSL_CIPHER *c = SSL_CIPHER_find(s, rlist);
1334 if (i)
1335 BIO_puts(bio_err, ":");
1336 if (c)
1337 BIO_puts(bio_err, SSL_CIPHER_get_name(c));
1338 else if (!memcmp(rlist, scsv_id - num + 3, num))
1339 BIO_puts(bio_err, "SCSV");
1340 else {
1341 size_t j;
1342 BIO_puts(bio_err, "0x");
1343 for (j = 0; j < num; j++)
1344 BIO_printf(bio_err, "%02X", rlist[j]);
1345 }
1346 }
1347 BIO_puts(bio_err, "\n");
1348 }
1349
1350 void print_ssl_summary(SSL *s)
1351 {
1352 const SSL_CIPHER *c;
1353 X509 *peer;
1354 /* const char *pnam = SSL_is_server(s) ? "client" : "server"; */
1355
1356 BIO_printf(bio_err, "Protocol version: %s\n", SSL_get_version(s));
1357 print_raw_cipherlist(s);
1358 c = SSL_get_current_cipher(s);
1359 BIO_printf(bio_err, "Ciphersuite: %s\n", SSL_CIPHER_get_name(c));
1360 do_print_sigalgs(bio_err, s, 0);
1361 peer = SSL_get_peer_certificate(s);
1362 if (peer) {
1363 int nid;
1364 BIO_puts(bio_err, "Peer certificate: ");
1365 X509_NAME_print_ex(bio_err, X509_get_subject_name(peer),
1366 0, XN_FLAG_ONELINE);
1367 BIO_puts(bio_err, "\n");
1368 if (SSL_get_peer_signature_nid(s, &nid))
1369 BIO_printf(bio_err, "Hash used: %s\n", OBJ_nid2sn(nid));
1370 } else
1371 BIO_puts(bio_err, "No peer certificate\n");
1372 X509_free(peer);
1373 #ifndef OPENSSL_NO_EC
1374 ssl_print_point_formats(bio_err, s);
1375 if (SSL_is_server(s))
1376 ssl_print_curves(bio_err, s, 1);
1377 else
1378 ssl_print_tmp_key(bio_err, s);
1379 #else
1380 if (!SSL_is_server(s))
1381 ssl_print_tmp_key(bio_err, s);
1382 #endif
1383 }
1384
1385 int config_ctx(SSL_CONF_CTX *cctx, STACK_OF(OPENSSL_STRING) *str,
1386 SSL_CTX *ctx, int no_ecdhe, int no_jpake)
1387 {
1388 int i;
1389
1390 SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
1391 for (i = 0; i < sk_OPENSSL_STRING_num(str); i += 2) {
1392 const char *flag = sk_OPENSSL_STRING_value(str, i);
1393 const char *arg = sk_OPENSSL_STRING_value(str, i + 1);
1394 /* If no_ecdhe or named curve already specified don't need a default. */
1395 if (!no_ecdhe && strcmp(flag, "-named_curve") == 0)
1396 no_ecdhe = 1;
1397 #ifndef OPENSSL_NO_JPAKE
1398 if (!no_jpake && (strcmp(flag, "-cipher") == 0)) {
1399 BIO_puts(bio_err, "JPAKE sets cipher to PSK\n");
1400 return 0;
1401 }
1402 #endif
1403 if (SSL_CONF_cmd(cctx, flag, arg) <= 0) {
1404 if (arg)
1405 BIO_printf(bio_err, "Error with command: \"%s %s\"\n",
1406 flag, arg);
1407 else
1408 BIO_printf(bio_err, "Error with command: \"%s\"\n", flag);
1409 ERR_print_errors(bio_err);
1410 return 0;
1411 }
1412 }
1413 /*
1414 * This is a special case to keep existing s_server functionality: if we
1415 * don't have any curve specified *and* we haven't disabled ECDHE then
1416 * use P-256.
1417 */
1418 if (!no_ecdhe) {
1419 if (SSL_CONF_cmd(cctx, "-named_curve", "P-256") <= 0) {
1420 BIO_puts(bio_err, "Error setting EC curve\n");
1421 ERR_print_errors(bio_err);
1422 return 0;
1423 }
1424 }
1425 #ifndef OPENSSL_NO_JPAKE
1426 if (!no_jpake) {
1427 if (SSL_CONF_cmd(cctx, "-cipher", "PSK") <= 0) {
1428 BIO_puts(bio_err, "Error setting cipher to PSK\n");
1429 ERR_print_errors(bio_err);
1430 return 0;
1431 }
1432 }
1433 #endif
1434 if (!SSL_CONF_CTX_finish(cctx)) {
1435 BIO_puts(bio_err, "Error finishing context\n");
1436 ERR_print_errors(bio_err);
1437 return 0;
1438 }
1439 return 1;
1440 }
1441
1442 static int add_crls_store(X509_STORE *st, STACK_OF(X509_CRL) *crls)
1443 {
1444 X509_CRL *crl;
1445 int i;
1446 for (i = 0; i < sk_X509_CRL_num(crls); i++) {
1447 crl = sk_X509_CRL_value(crls, i);
1448 X509_STORE_add_crl(st, crl);
1449 }
1450 return 1;
1451 }
1452
1453 int ssl_ctx_add_crls(SSL_CTX *ctx, STACK_OF(X509_CRL) *crls, int crl_download)
1454 {
1455 X509_STORE *st;
1456 st = SSL_CTX_get_cert_store(ctx);
1457 add_crls_store(st, crls);
1458 if (crl_download)
1459 store_setup_crl_download(st);
1460 return 1;
1461 }
1462
1463 int ssl_load_stores(SSL_CTX *ctx,
1464 const char *vfyCApath, const char *vfyCAfile,
1465 const char *chCApath, const char *chCAfile,
1466 STACK_OF(X509_CRL) *crls, int crl_download)
1467 {
1468 X509_STORE *vfy = NULL, *ch = NULL;
1469 int rv = 0;
1470 if (vfyCApath || vfyCAfile) {
1471 vfy = X509_STORE_new();
1472 if (!X509_STORE_load_locations(vfy, vfyCAfile, vfyCApath))
1473 goto err;
1474 add_crls_store(vfy, crls);
1475 SSL_CTX_set1_verify_cert_store(ctx, vfy);
1476 if (crl_download)
1477 store_setup_crl_download(vfy);
1478 }
1479 if (chCApath || chCAfile) {
1480 ch = X509_STORE_new();
1481 if (!X509_STORE_load_locations(ch, chCAfile, chCApath))
1482 goto err;
1483 SSL_CTX_set1_chain_cert_store(ctx, ch);
1484 }
1485 rv = 1;
1486 err:
1487 X509_STORE_free(vfy);
1488 X509_STORE_free(ch);
1489 return rv;
1490 }
1491
1492 /* Verbose print out of security callback */
1493
1494 typedef struct {
1495 BIO *out;
1496 int verbose;
1497 int (*old_cb) (SSL *s, SSL_CTX *ctx, int op, int bits, int nid,
1498 void *other, void *ex);
1499 } security_debug_ex;
1500
1501 static int security_callback_debug(SSL *s, SSL_CTX *ctx,
1502 int op, int bits, int nid,
1503 void *other, void *ex)
1504 {
1505 security_debug_ex *sdb = ex;
1506 int rv, show_bits = 1, cert_md = 0;
1507 const char *nm;
1508 rv = sdb->old_cb(s, ctx, op, bits, nid, other, ex);
1509 if (rv == 1 && sdb->verbose < 2)
1510 return 1;
1511 BIO_puts(sdb->out, "Security callback: ");
1512
1513 switch (op) {
1514 case SSL_SECOP_CIPHER_SUPPORTED:
1515 nm = "Supported Ciphersuite";
1516 break;
1517 case SSL_SECOP_CIPHER_SHARED:
1518 nm = "Shared Ciphersuite";
1519 break;
1520 case SSL_SECOP_CIPHER_CHECK:
1521 nm = "Check Ciphersuite";
1522 break;
1523 case SSL_SECOP_TICKET:
1524 BIO_puts(sdb->out, "Session ticket");
1525 show_bits = 0;
1526 nm = NULL;
1527 break;
1528 case SSL_SECOP_COMPRESSION:
1529 BIO_puts(sdb->out, "SSL compression");
1530 show_bits = 0;
1531 nm = NULL;
1532 break;
1533 #ifndef OPENSSL_NO_DH
1534 case SSL_SECOP_TMP_DH:
1535 nm = "Temp DH key bits";
1536 break;
1537 #endif
1538 case SSL_SECOP_CURVE_SUPPORTED:
1539 nm = "Supported Curve";
1540 break;
1541 case SSL_SECOP_CURVE_SHARED:
1542 nm = "Shared Curve";
1543 break;
1544 case SSL_SECOP_CURVE_CHECK:
1545 nm = "Check Curve";
1546 break;
1547 case SSL_SECOP_VERSION:
1548 BIO_printf(sdb->out, "Version=%s", ssl_version_str(nid));
1549 show_bits = 0;
1550 nm = NULL;
1551 break;
1552 case SSL_SECOP_SIGALG_SUPPORTED:
1553 nm = "Supported Signature Algorithm digest";
1554 break;
1555 case SSL_SECOP_SIGALG_SHARED:
1556 nm = "Shared Signature Algorithm digest";
1557 break;
1558 case SSL_SECOP_SIGALG_CHECK:
1559 nm = "Check Signature Algorithm digest";
1560 break;
1561 case SSL_SECOP_SIGALG_MASK:
1562 nm = "Signature Algorithm mask";
1563 break;
1564 case SSL_SECOP_EE_KEY:
1565 nm = "Certificate chain EE key";
1566 break;
1567 case SSL_SECOP_CA_KEY:
1568 nm = "Certificate chain CA key";
1569 break;
1570 case SSL_SECOP_CA_MD:
1571 cert_md = 1;
1572 nm = "Certificate chain CA digest";
1573 break;
1574 case SSL_SECOP_PEER_EE_KEY:
1575 nm = "Peer Chain EE key";
1576 break;
1577 case SSL_SECOP_PEER_CA_KEY:
1578 nm = "Peer Chain CA key";
1579 break;
1580 case SSL_SECOP_PEER_CA_MD:
1581 cert_md = 1;
1582 nm = "Peer chain CA digest";
1583 break;
1584 default:
1585 nm = NULL;
1586 }
1587 if (nm)
1588 BIO_printf(sdb->out, "%s=", nm);
1589
1590 switch (op & SSL_SECOP_OTHER_TYPE) {
1591
1592 case SSL_SECOP_OTHER_CIPHER:
1593 BIO_puts(sdb->out, SSL_CIPHER_get_name(other));
1594 break;
1595
1596 #ifndef OPENSSL_NO_EC
1597 case SSL_SECOP_OTHER_CURVE:
1598 {
1599 const char *cname;
1600 cname = EC_curve_nid2nist(nid);
1601 if (cname == NULL)
1602 cname = OBJ_nid2sn(nid);
1603 BIO_puts(sdb->out, cname);
1604 }
1605 break;
1606 #endif
1607
1608 case SSL_SECOP_OTHER_DH:
1609 {
1610 DH *dh = other;
1611 BIO_printf(sdb->out, "%d", BN_num_bits(dh->p));
1612 break;
1613 }
1614 case SSL_SECOP_OTHER_CERT:
1615 {
1616 if (cert_md) {
1617 int sig_nid = X509_get_signature_nid(other);
1618 BIO_puts(sdb->out, OBJ_nid2sn(sig_nid));
1619 } else {
1620 EVP_PKEY *pkey = X509_get_pubkey(other);
1621 const char *algname = "";
1622 EVP_PKEY_asn1_get0_info(NULL, NULL, NULL, NULL,
1623 &algname, EVP_PKEY_get0_asn1(pkey));
1624 BIO_printf(sdb->out, "%s, bits=%d",
1625 algname, EVP_PKEY_bits(pkey));
1626 EVP_PKEY_free(pkey);
1627 }
1628 break;
1629 }
1630 case SSL_SECOP_OTHER_SIGALG:
1631 {
1632 const unsigned char *salg = other;
1633 const char *sname = NULL;
1634 switch (salg[1]) {
1635 case TLSEXT_signature_anonymous:
1636 sname = "anonymous";
1637 break;
1638 case TLSEXT_signature_rsa:
1639 sname = "RSA";
1640 break;
1641 case TLSEXT_signature_dsa:
1642 sname = "DSA";
1643 break;
1644 case TLSEXT_signature_ecdsa:
1645 sname = "ECDSA";
1646 break;
1647 }
1648
1649 BIO_puts(sdb->out, OBJ_nid2sn(nid));
1650 if (sname)
1651 BIO_printf(sdb->out, ", algorithm=%s", sname);
1652 else
1653 BIO_printf(sdb->out, ", algid=%d", salg[1]);
1654 break;
1655 }
1656
1657 }
1658
1659 if (show_bits)
1660 BIO_printf(sdb->out, ", security bits=%d", bits);
1661 BIO_printf(sdb->out, ": %s\n", rv ? "yes" : "no");
1662 return rv;
1663 }
1664
1665 void ssl_ctx_security_debug(SSL_CTX *ctx, int verbose)
1666 {
1667 static security_debug_ex sdb;
1668
1669 sdb.out = bio_err;
1670 sdb.verbose = verbose;
1671 sdb.old_cb = SSL_CTX_get_security_callback(ctx);
1672 SSL_CTX_set_security_callback(ctx, security_callback_debug);
1673 SSL_CTX_set0_security_ex_data(ctx, &sdb);
1674 }