1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
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)"
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
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.]
57 /* ====================================================================
58 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
86 * 6. Redistributions of any form whatsoever must retain the following
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
111 /* callback functions used by s_client, s_server, and s_time */
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>
128 #define COOKIE_SECRET_LENGTH 16
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;
137 int verify_callback(int ok
, X509_STORE_CTX
*ctx
)
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
);
146 if (!verify_quiet
|| !ok
) {
147 BIO_printf(bio_err
, "depth=%d ", depth
);
149 X509_NAME_print_ex(bio_err
,
150 X509_get_subject_name(err_cert
),
152 BIO_puts(bio_err
, "\n");
154 BIO_puts(bio_err
, "<no cert>\n");
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
)
162 verify_error
= X509_V_OK
;
165 verify_error
= X509_V_ERR_CERT_CHAIN_TOO_LONG
;
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
),
173 BIO_puts(bio_err
, "\n");
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");
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");
187 case X509_V_ERR_NO_EXPLICIT_POLICY
:
192 if (err
== X509_V_OK
&& ok
== 2 && !verify_quiet
)
194 if (ok
&& !verify_quiet
)
195 BIO_printf(bio_err
, "verify return:%d\n", ok
);
199 int set_cert_stuff(SSL_CTX
*ctx
, char *cert_file
, char *key_file
)
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",
206 ERR_print_errors(bio_err
);
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",
214 ERR_print_errors(bio_err
);
219 * If we are using DSA, we can copy the parameters from the private
224 * Now we know that a key and cert have been set against the SSL
227 if (!SSL_CTX_check_private_key(ctx
)) {
229 "Private key does not match the certificate public key\n");
236 int set_cert_key_stuff(SSL_CTX
*ctx
, X509
*cert
, EVP_PKEY
*key
,
237 STACK_OF(X509
) *chain
, int build_chain
)
239 int chflags
= chain
? SSL_BUILD_CHAIN_FLAG_CHECK
: 0;
242 if (SSL_CTX_use_certificate(ctx
, cert
) <= 0) {
243 BIO_printf(bio_err
, "error setting certificate\n");
244 ERR_print_errors(bio_err
);
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
);
255 * Now we know that a key and cert have been set against the SSL context
257 if (!SSL_CTX_check_private_key(ctx
)) {
259 "Private key does not match the certificate public key\n");
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
);
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
);
275 static void ssl_print_client_cert_types(BIO
*bio
, SSL
*s
)
277 const unsigned char *p
;
279 int cert_type_num
= SSL_get0_certificate_types(s
, &p
);
282 BIO_puts(bio
, "Client Certificate Types: ");
283 for (i
= 0; i
< cert_type_num
; i
++) {
284 unsigned char cert_type
= p
[i
];
287 case TLS_CT_RSA_SIGN
:
291 case TLS_CT_DSS_SIGN
:
295 case TLS_CT_RSA_FIXED_DH
:
296 cname
= "RSA fixed DH";
299 case TLS_CT_DSS_FIXED_DH
:
300 cname
= "DSS fixed DH";
303 case TLS_CT_ECDSA_SIGN
:
304 cname
= "ECDSA sign";
307 case TLS_CT_RSA_FIXED_ECDH
:
308 cname
= "RSA fixed ECDH";
311 case TLS_CT_ECDSA_FIXED_ECDH
:
312 cname
= "ECDSA fixed ECDH";
315 case TLS_CT_GOST94_SIGN
:
316 cname
= "GOST94 Sign";
319 case TLS_CT_GOST01_SIGN
:
320 cname
= "GOST01 Sign";
331 BIO_puts(bio
, cname
);
333 BIO_printf(bio
, "UNKNOWN (%d),", cert_type
);
338 static int do_print_sigalgs(BIO
*out
, SSL
*s
, int shared
)
341 client
= SSL_is_server(s
) ? 0 : 1;
343 nsig
= SSL_get_shared_sigalgs(s
, -1, NULL
, NULL
, NULL
, NULL
, NULL
);
345 nsig
= SSL_get_sigalgs(s
, -1, NULL
, NULL
, NULL
, NULL
, NULL
);
350 BIO_puts(out
, "Shared ");
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
;
360 SSL_get_shared_sigalgs(s
, i
, &sign_nid
, &hash_nid
, NULL
,
363 SSL_get_sigalgs(s
, i
, &sign_nid
, &hash_nid
, NULL
, &rsign
, &rhash
);
366 if (sign_nid
== EVP_PKEY_RSA
)
368 else if (sign_nid
== EVP_PKEY_DSA
)
370 else if (sign_nid
== EVP_PKEY_EC
)
373 BIO_printf(out
, "%s+", sstr
);
375 BIO_printf(out
, "0x%02X+", (int)rsign
);
376 if (hash_nid
!= NID_undef
)
377 BIO_printf(out
, "%s", OBJ_nid2sn(hash_nid
));
379 BIO_printf(out
, "0x%02X", (int)rhash
);
385 int ssl_print_sigalgs(BIO
*out
, SSL
*s
)
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
));
397 #ifndef OPENSSL_NO_EC
398 int ssl_print_point_formats(BIO
*out
, SSL
*s
)
401 const char *pformats
;
402 nformats
= SSL_get0_ec_point_formats(s
, &pformats
);
405 BIO_puts(out
, "Supported Elliptic Curve Point Formats: ");
406 for (i
= 0; i
< nformats
; i
++, pformats
++) {
410 case TLSEXT_ECPOINTFORMAT_uncompressed
:
411 BIO_puts(out
, "uncompressed");
414 case TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime
:
415 BIO_puts(out
, "ansiX962_compressed_prime");
418 case TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
:
419 BIO_puts(out
, "ansiX962_compressed_char2");
423 BIO_printf(out
, "unknown(%d)", (int)*pformats
);
429 BIO_puts(out
, "NONE");
434 int ssl_print_curves(BIO
*out
, SSL
*s
, int noshared
)
436 int i
, ncurves
, *curves
, nid
;
439 ncurves
= SSL_get1_curves(s
, NULL
);
442 curves
= app_malloc(ncurves
* sizeof(int), "curves to print");
443 SSL_get1_curves(s
, curves
);
445 BIO_puts(out
, "Supported Elliptic Curves: ");
446 for (i
= 0; i
< ncurves
; i
++) {
450 /* If unrecognised print out hex version */
451 if (nid
& TLSEXT_nid_unknown
)
452 BIO_printf(out
, "0x%04X", nid
& 0xFFFF);
454 /* Use NIST name for curve if it exists */
455 cname
= EC_curve_nid2nist(nid
);
457 cname
= OBJ_nid2sn(nid
);
458 BIO_printf(out
, "%s", cname
);
462 BIO_puts(out
, "NONE");
463 OPENSSL_free(curves
);
468 BIO_puts(out
, "\nShared Elliptic curves: ");
469 ncurves
= SSL_get_shared_curve(s
, -1);
470 for (i
= 0; i
< ncurves
; i
++) {
473 nid
= SSL_get_shared_curve(s
, i
);
474 cname
= EC_curve_nid2nist(nid
);
476 cname
= OBJ_nid2sn(nid
);
477 BIO_printf(out
, "%s", cname
);
480 BIO_puts(out
, "NONE");
485 int ssl_print_tmp_key(BIO
*out
, SSL
*s
)
488 if (!SSL_get_server_tmp_key(s
, &key
))
490 BIO_puts(out
, "Server Temp Key: ");
491 switch (EVP_PKEY_id(key
)) {
493 BIO_printf(out
, "RSA, %d bits\n", EVP_PKEY_bits(key
));
497 BIO_printf(out
, "DH, %d bits\n", EVP_PKEY_bits(key
));
499 #ifndef OPENSSL_NO_EC
502 EC_KEY
*ec
= EVP_PKEY_get1_EC_KEY(key
);
505 nid
= EC_GROUP_get_curve_name(EC_KEY_get0_group(ec
));
507 cname
= EC_curve_nid2nist(nid
);
509 cname
= OBJ_nid2sn(nid
);
510 BIO_printf(out
, "ECDH, %s, %d bits\n", cname
, EVP_PKEY_bits(key
));
518 long bio_dump_callback(BIO
*bio
, int cmd
, const char *argp
,
519 int argi
, long argl
, long ret
)
523 out
= (BIO
*)BIO_get_callback_arg(bio
);
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
);
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
);
540 void apps_ssl_info_callback(const SSL
*s
, int where
, int ret
)
545 w
= where
& ~SSL_ST_MASK
;
547 if (w
& SSL_ST_CONNECT
)
549 else if (w
& SSL_ST_ACCEPT
)
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",
560 SSL_alert_type_string_long(ret
),
561 SSL_alert_desc_string_long(ret
));
562 } else if (where
& SSL_CB_EXIT
) {
564 BIO_printf(bio_err
, "%s:failed in %s\n",
565 str
, SSL_state_string_long(s
));
567 BIO_printf(bio_err
, "%s:error in %s\n",
568 str
, SSL_state_string_long(s
));
573 static const char *ssl_version_str(int version
)
587 return "DTLS 1.0 (bad)";
593 void msg_cb(int write_p
, int version
, int content_type
, const void *buf
,
594 size_t len
, SSL
*ssl
, void *arg
)
597 const char *str_write_p
, *str_version
, *str_content_type
=
598 "", *str_details1
= "", *str_details2
= "";
600 str_write_p
= write_p
? ">>>" : "<<<";
602 str_version
= ssl_version_str(version
);
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
) {
611 str_content_type
= "ChangeCipherSpec";
614 str_content_type
= "Alert";
617 str_content_type
= "Handshake";
621 if (content_type
== 21) { /* Alert */
622 str_details1
= ", ???";
625 switch (((const unsigned char *)buf
)[0]) {
627 str_details1
= ", warning";
630 str_details1
= ", fatal";
634 str_details2
= " ???";
635 switch (((const unsigned char *)buf
)[1]) {
637 str_details2
= " close_notify";
640 str_details2
= " unexpected_message";
643 str_details2
= " bad_record_mac";
646 str_details2
= " decryption_failed";
649 str_details2
= " record_overflow";
652 str_details2
= " decompression_failure";
655 str_details2
= " handshake_failure";
658 str_details2
= " bad_certificate";
661 str_details2
= " unsupported_certificate";
664 str_details2
= " certificate_revoked";
667 str_details2
= " certificate_expired";
670 str_details2
= " certificate_unknown";
673 str_details2
= " illegal_parameter";
676 str_details2
= " unknown_ca";
679 str_details2
= " access_denied";
682 str_details2
= " decode_error";
685 str_details2
= " decrypt_error";
688 str_details2
= " export_restriction";
691 str_details2
= " protocol_version";
694 str_details2
= " insufficient_security";
697 str_details2
= " internal_error";
700 str_details2
= " user_canceled";
703 str_details2
= " no_renegotiation";
706 str_details2
= " unsupported_extension";
709 str_details2
= " certificate_unobtainable";
712 str_details2
= " unrecognized_name";
715 str_details2
= " bad_certificate_status_response";
718 str_details2
= " bad_certificate_hash_value";
721 str_details2
= " unknown_psk_identity";
727 if (content_type
== 22) { /* Handshake */
728 str_details1
= "???";
731 switch (((const unsigned char *)buf
)[0]) {
733 str_details1
= ", HelloRequest";
736 str_details1
= ", ClientHello";
739 str_details1
= ", ServerHello";
742 str_details1
= ", HelloVerifyRequest";
745 str_details1
= ", Certificate";
748 str_details1
= ", ServerKeyExchange";
751 str_details1
= ", CertificateRequest";
754 str_details1
= ", ServerHelloDone";
757 str_details1
= ", CertificateVerify";
760 str_details1
= ", ClientKeyExchange";
763 str_details1
= ", Finished";
768 #ifndef OPENSSL_NO_HEARTBEATS
769 if (content_type
== 24) { /* Heartbeat */
770 str_details1
= ", Heartbeat";
773 switch (((const unsigned char *)buf
)[0]) {
775 str_details1
= ", HeartbeatRequest";
778 str_details1
= ", HeartbeatResponse";
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
,
793 BIO_printf(bio
, " ");
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
]);
801 BIO_printf(bio
, " ...");
802 BIO_printf(bio
, "\n");
804 (void)BIO_flush(bio
);
807 void tlsext_cb(SSL
*s
, int client_server
, int type
,
808 unsigned char *data
, int len
, void *arg
)
814 case TLSEXT_TYPE_server_name
:
815 extname
= "server name";
818 case TLSEXT_TYPE_max_fragment_length
:
819 extname
= "max fragment length";
822 case TLSEXT_TYPE_client_certificate_url
:
823 extname
= "client certificate URL";
826 case TLSEXT_TYPE_trusted_ca_keys
:
827 extname
= "trusted CA keys";
830 case TLSEXT_TYPE_truncated_hmac
:
831 extname
= "truncated HMAC";
834 case TLSEXT_TYPE_status_request
:
835 extname
= "status request";
838 case TLSEXT_TYPE_user_mapping
:
839 extname
= "user mapping";
842 case TLSEXT_TYPE_client_authz
:
843 extname
= "client authz";
846 case TLSEXT_TYPE_server_authz
:
847 extname
= "server authz";
850 case TLSEXT_TYPE_cert_type
:
851 extname
= "cert type";
854 case TLSEXT_TYPE_elliptic_curves
:
855 extname
= "elliptic curves";
858 case TLSEXT_TYPE_ec_point_formats
:
859 extname
= "EC point formats";
862 case TLSEXT_TYPE_srp
:
866 case TLSEXT_TYPE_signature_algorithms
:
867 extname
= "signature algorithms";
870 case TLSEXT_TYPE_use_srtp
:
871 extname
= "use SRTP";
874 case TLSEXT_TYPE_heartbeat
:
875 extname
= "heartbeat";
878 case TLSEXT_TYPE_session_ticket
:
879 extname
= "session ticket";
882 case TLSEXT_TYPE_renegotiate
:
883 extname
= "renegotiation info";
886 #ifdef TLSEXT_TYPE_next_proto_neg
887 case TLSEXT_TYPE_next_proto_neg
:
888 extname
= "next protocol";
891 #ifdef TLSEXT_TYPE_encrypt_then_mac
892 case TLSEXT_TYPE_encrypt_then_mac
:
893 extname
= "encrypt-then-mac";
896 case TLSEXT_TYPE_padding
:
897 extname
= "TLS padding";
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
);
912 int generate_cookie_callback(SSL
*ssl
, unsigned char *cookie
,
913 unsigned int *cookie_len
)
915 unsigned char *buffer
, result
[EVP_MAX_MD_SIZE
];
916 unsigned int length
, resultlength
;
919 struct sockaddr_in s4
;
921 struct sockaddr_in6 s6
;
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");
931 cookie_initialized
= 1;
934 /* Read peer information */
935 (void)BIO_dgram_get_peer(SSL_get_rbio(ssl
), &peer
);
937 /* Create buffer with peer's address and port */
939 switch (peer
.sa
.sa_family
) {
941 length
+= sizeof(struct in_addr
);
942 length
+= sizeof(peer
.s4
.sin_port
);
946 length
+= sizeof(struct in6_addr
);
947 length
+= sizeof(peer
.s6
.sin6_port
);
954 buffer
= app_malloc(length
, "cookie generate buffer");
956 switch (peer
.sa
.sa_family
) {
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
));
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
));
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
);
979 memcpy(cookie
, result
, resultlength
);
980 *cookie_len
= resultlength
;
985 int verify_cookie_callback(SSL
*ssl
, unsigned char *cookie
,
986 unsigned int cookie_len
)
988 unsigned char *buffer
, result
[EVP_MAX_MD_SIZE
];
989 unsigned int length
, resultlength
;
992 struct sockaddr_in s4
;
994 struct sockaddr_in6 s6
;
998 /* If secret isn't initialized yet, the cookie can't be valid */
999 if (!cookie_initialized
)
1002 /* Read peer information */
1003 (void)BIO_dgram_get_peer(SSL_get_rbio(ssl
), &peer
);
1005 /* Create buffer with peer's address and port */
1007 switch (peer
.sa
.sa_family
) {
1009 length
+= sizeof(struct in_addr
);
1010 length
+= sizeof(peer
.s4
.sin_port
);
1012 #if OPENSSL_USE_IPV6
1014 length
+= sizeof(struct in6_addr
);
1015 length
+= sizeof(peer
.s6
.sin6_port
);
1022 buffer
= app_malloc(length
, "cookie verify buffer");
1024 switch (peer
.sa
.sa_family
) {
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
));
1030 #if OPENSSL_USE_IPV6
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
));
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
);
1047 if (cookie_len
== resultlength
1048 && memcmp(result
, cookie
, resultlength
) == 0)
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
1061 /* Linked list of certificates, keys and chains */
1062 struct ssl_excert_st
{
1064 const char *certfile
;
1066 const char *keyfile
;
1067 const char *chainfile
;
1070 STACK_OF(X509
) *chain
;
1072 struct ssl_excert_st
*next
, *prev
;
1075 struct chain_flags
{
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"},
1093 static void print_chain_flags(SSL
*s
, int flags
)
1095 struct chain_flags
*ctmp
= chain_flags_list
;
1097 while (ctmp
->name
) {
1098 BIO_printf(bio_err
, "\t%s: %s\n", ctmp
->name
,
1099 flags
& ctmp
->flag
? "OK" : "NOT OK");
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");
1106 BIO_printf(bio_err
, "not tested\n");
1110 * Very basic selection callback: just use any certificate chain reported as
1111 * valid. More sophisticated could prioritise according to local policy.
1113 static int set_cert_cb(SSL
*ssl
, void *arg
)
1116 SSL_EXCERT
*exc
= arg
;
1117 #ifdef CERT_CB_TEST_RETRY
1118 static int retry_cnt
;
1119 if (retry_cnt
< 5) {
1121 fprintf(stderr
, "Certificate callback retry test: count %d\n",
1126 SSL_certs_clear(ssl
);
1132 * Go to end of list and traverse backwards since we prepend newer
1133 * entries this retains the original order.
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,
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
)) {
1154 * NB: we wouldn't normally do this as it is not efficient
1155 * building chains on each connection better to cache the chain
1158 if (exc
->build_chain
) {
1159 if (!SSL_build_cert_chain(ssl
, 0))
1161 } else if (exc
->chain
)
1162 SSL_set1_chain(ssl
, exc
->chain
);
1169 void ssl_ctx_set_excert(SSL_CTX
*ctx
, SSL_EXCERT
*exc
)
1171 SSL_CTX_set_cert_cb(ctx
, set_cert_cb
, exc
);
1174 static int ssl_excert_prepend(SSL_EXCERT
**pexc
)
1176 SSL_EXCERT
*exc
= app_malloc(sizeof(*exc
), "prepend cert");
1178 exc
->certfile
= NULL
;
1179 exc
->keyfile
= NULL
;
1180 exc
->chainfile
= NULL
;
1185 exc
->build_chain
= 0;
1191 exc
->certform
= exc
->next
->certform
;
1192 exc
->keyform
= exc
->next
->keyform
;
1193 exc
->next
->prev
= exc
;
1195 exc
->certform
= FORMAT_PEM
;
1196 exc
->keyform
= FORMAT_PEM
;
1202 void ssl_excert_free(SSL_EXCERT
*exc
)
1209 X509_free(exc
->cert
);
1210 EVP_PKEY_free(exc
->key
);
1211 sk_X509_pop_free(exc
->chain
, X509_free
);
1218 int load_excert(SSL_EXCERT
**pexc
)
1220 SSL_EXCERT
*exc
= *pexc
;
1223 /* If nothing in list, free and set to NULL */
1224 if (!exc
->certfile
&& !exc
->next
) {
1225 ssl_excert_free(exc
);
1229 for (; exc
; exc
= exc
->next
) {
1230 if (!exc
->certfile
) {
1231 BIO_printf(bio_err
, "Missing filename\n");
1234 exc
->cert
= load_cert(exc
->certfile
, exc
->certform
,
1235 NULL
, NULL
, "Server Certificate");
1239 exc
->key
= load_key(exc
->keyfile
, exc
->keyform
,
1240 0, NULL
, NULL
, "Server Key");
1242 exc
->key
= load_key(exc
->certfile
, exc
->certform
,
1243 0, NULL
, NULL
, "Server Key");
1247 if (exc
->chainfile
) {
1248 exc
->chain
= load_certs(exc
->chainfile
, FORMAT_PEM
,
1249 NULL
, NULL
, "Server Chain");
1257 enum range
{ OPT_X_ENUM
};
1259 int args_excert(int opt
, SSL_EXCERT
**pexc
)
1261 SSL_EXCERT
*exc
= *pexc
;
1263 assert(opt
> OPT_X__FIRST
);
1264 assert(opt
< OPT_X__LAST
);
1267 if (!ssl_excert_prepend(&exc
)) {
1268 BIO_printf(bio_err
, " %s: Error initialising xcert\n",
1275 switch ((enum range
)opt
) {
1280 if (exc
->certfile
&& !ssl_excert_prepend(&exc
)) {
1281 BIO_printf(bio_err
, "%s: Error adding xcert\n", opt_getprog());
1284 exc
->certfile
= opt_arg();
1288 BIO_printf(bio_err
, "%s: Key already specified\n", opt_getprog());
1291 exc
->keyfile
= opt_arg();
1294 if (exc
->chainfile
) {
1295 BIO_printf(bio_err
, "%s: Chain already specified\n",
1299 exc
->chainfile
= opt_arg();
1301 case OPT_X_CHAIN_BUILD
:
1302 exc
->build_chain
= 1;
1304 case OPT_X_CERTFORM
:
1305 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &exc
->certform
))
1309 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &exc
->keyform
))
1316 ERR_print_errors(bio_err
);
1317 ssl_excert_free(exc
);
1322 static void print_raw_cipherlist(SSL
*s
)
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
))
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
);
1335 BIO_puts(bio_err
, ":");
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");
1342 BIO_puts(bio_err
, "0x");
1343 for (j
= 0; j
< num
; j
++)
1344 BIO_printf(bio_err
, "%02X", rlist
[j
]);
1347 BIO_puts(bio_err
, "\n");
1350 void print_ssl_summary(SSL
*s
)
1352 const SSL_CIPHER
*c
;
1354 /* const char *pnam = SSL_is_server(s) ? "client" : "server"; */
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
);
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
));
1371 BIO_puts(bio_err
, "No peer certificate\n");
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);
1378 ssl_print_tmp_key(bio_err
, s
);
1380 if (!SSL_is_server(s
))
1381 ssl_print_tmp_key(bio_err
, s
);
1385 int config_ctx(SSL_CONF_CTX
*cctx
, STACK_OF(OPENSSL_STRING
) *str
,
1386 SSL_CTX
*ctx
, int no_ecdhe
, int no_jpake
)
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)
1397 #ifndef OPENSSL_NO_JPAKE
1398 if (!no_jpake
&& (strcmp(flag
, "-cipher") == 0)) {
1399 BIO_puts(bio_err
, "JPAKE sets cipher to PSK\n");
1403 if (SSL_CONF_cmd(cctx
, flag
, arg
) <= 0) {
1405 BIO_printf(bio_err
, "Error with command: \"%s %s\"\n",
1408 BIO_printf(bio_err
, "Error with command: \"%s\"\n", flag
);
1409 ERR_print_errors(bio_err
);
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
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
);
1425 #ifndef OPENSSL_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
);
1434 if (!SSL_CONF_CTX_finish(cctx
)) {
1435 BIO_puts(bio_err
, "Error finishing context\n");
1436 ERR_print_errors(bio_err
);
1442 static int add_crls_store(X509_STORE
*st
, STACK_OF(X509_CRL
) *crls
)
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
);
1453 int ssl_ctx_add_crls(SSL_CTX
*ctx
, STACK_OF(X509_CRL
) *crls
, int crl_download
)
1456 st
= SSL_CTX_get_cert_store(ctx
);
1457 add_crls_store(st
, crls
);
1459 store_setup_crl_download(st
);
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
)
1468 X509_STORE
*vfy
= NULL
, *ch
= NULL
;
1470 if (vfyCApath
|| vfyCAfile
) {
1471 vfy
= X509_STORE_new();
1472 if (!X509_STORE_load_locations(vfy
, vfyCAfile
, vfyCApath
))
1474 add_crls_store(vfy
, crls
);
1475 SSL_CTX_set1_verify_cert_store(ctx
, vfy
);
1477 store_setup_crl_download(vfy
);
1479 if (chCApath
|| chCAfile
) {
1480 ch
= X509_STORE_new();
1481 if (!X509_STORE_load_locations(ch
, chCAfile
, chCApath
))
1483 SSL_CTX_set1_chain_cert_store(ctx
, ch
);
1487 X509_STORE_free(vfy
);
1488 X509_STORE_free(ch
);
1492 /* Verbose print out of security callback */
1497 int (*old_cb
) (SSL
*s
, SSL_CTX
*ctx
, int op
, int bits
, int nid
,
1498 void *other
, void *ex
);
1499 } security_debug_ex
;
1501 static int security_callback_debug(SSL
*s
, SSL_CTX
*ctx
,
1502 int op
, int bits
, int nid
,
1503 void *other
, void *ex
)
1505 security_debug_ex
*sdb
= ex
;
1506 int rv
, show_bits
= 1, cert_md
= 0;
1508 rv
= sdb
->old_cb(s
, ctx
, op
, bits
, nid
, other
, ex
);
1509 if (rv
== 1 && sdb
->verbose
< 2)
1511 BIO_puts(sdb
->out
, "Security callback: ");
1514 case SSL_SECOP_CIPHER_SUPPORTED
:
1515 nm
= "Supported Ciphersuite";
1517 case SSL_SECOP_CIPHER_SHARED
:
1518 nm
= "Shared Ciphersuite";
1520 case SSL_SECOP_CIPHER_CHECK
:
1521 nm
= "Check Ciphersuite";
1523 case SSL_SECOP_TICKET
:
1524 BIO_puts(sdb
->out
, "Session ticket");
1528 case SSL_SECOP_COMPRESSION
:
1529 BIO_puts(sdb
->out
, "SSL compression");
1533 #ifndef OPENSSL_NO_DH
1534 case SSL_SECOP_TMP_DH
:
1535 nm
= "Temp DH key bits";
1538 case SSL_SECOP_CURVE_SUPPORTED
:
1539 nm
= "Supported Curve";
1541 case SSL_SECOP_CURVE_SHARED
:
1542 nm
= "Shared Curve";
1544 case SSL_SECOP_CURVE_CHECK
:
1547 case SSL_SECOP_VERSION
:
1548 BIO_printf(sdb
->out
, "Version=%s", ssl_version_str(nid
));
1552 case SSL_SECOP_SIGALG_SUPPORTED
:
1553 nm
= "Supported Signature Algorithm digest";
1555 case SSL_SECOP_SIGALG_SHARED
:
1556 nm
= "Shared Signature Algorithm digest";
1558 case SSL_SECOP_SIGALG_CHECK
:
1559 nm
= "Check Signature Algorithm digest";
1561 case SSL_SECOP_SIGALG_MASK
:
1562 nm
= "Signature Algorithm mask";
1564 case SSL_SECOP_EE_KEY
:
1565 nm
= "Certificate chain EE key";
1567 case SSL_SECOP_CA_KEY
:
1568 nm
= "Certificate chain CA key";
1570 case SSL_SECOP_CA_MD
:
1572 nm
= "Certificate chain CA digest";
1574 case SSL_SECOP_PEER_EE_KEY
:
1575 nm
= "Peer Chain EE key";
1577 case SSL_SECOP_PEER_CA_KEY
:
1578 nm
= "Peer Chain CA key";
1580 case SSL_SECOP_PEER_CA_MD
:
1582 nm
= "Peer chain CA digest";
1588 BIO_printf(sdb
->out
, "%s=", nm
);
1590 switch (op
& SSL_SECOP_OTHER_TYPE
) {
1592 case SSL_SECOP_OTHER_CIPHER
:
1593 BIO_puts(sdb
->out
, SSL_CIPHER_get_name(other
));
1596 #ifndef OPENSSL_NO_EC
1597 case SSL_SECOP_OTHER_CURVE
:
1600 cname
= EC_curve_nid2nist(nid
);
1602 cname
= OBJ_nid2sn(nid
);
1603 BIO_puts(sdb
->out
, cname
);
1608 case SSL_SECOP_OTHER_DH
:
1611 BIO_printf(sdb
->out
, "%d", BN_num_bits(dh
->p
));
1614 case SSL_SECOP_OTHER_CERT
:
1617 int sig_nid
= X509_get_signature_nid(other
);
1618 BIO_puts(sdb
->out
, OBJ_nid2sn(sig_nid
));
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
);
1630 case SSL_SECOP_OTHER_SIGALG
:
1632 const unsigned char *salg
= other
;
1633 const char *sname
= NULL
;
1635 case TLSEXT_signature_anonymous
:
1636 sname
= "anonymous";
1638 case TLSEXT_signature_rsa
:
1641 case TLSEXT_signature_dsa
:
1644 case TLSEXT_signature_ecdsa
:
1649 BIO_puts(sdb
->out
, OBJ_nid2sn(nid
));
1651 BIO_printf(sdb
->out
, ", algorithm=%s", sname
);
1653 BIO_printf(sdb
->out
, ", algid=%d", salg
[1]);
1660 BIO_printf(sdb
->out
, ", security bits=%d", bits
);
1661 BIO_printf(sdb
->out
, ": %s\n", rv
? "yes" : "no");
1665 void ssl_ctx_security_debug(SSL_CTX
*ctx
, int verbose
)
1667 static security_debug_ex sdb
;
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
);