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