Remove some #if 0 code in ssl, crypto/bio
[openssl.git] / ssl / t1_trce.c
1 /*
2  * Copyright 2012-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include "ssl_locl.h"
11
12 #ifndef OPENSSL_NO_SSL_TRACE
13
14 /* Packet trace support for OpenSSL */
15
16 typedef struct {
17     int num;
18     const char *name;
19 } ssl_trace_tbl;
20
21 # define ssl_trace_str(val, tbl) \
22         do_ssl_trace_str(val, tbl, OSSL_NELEM(tbl))
23
24 # define ssl_trace_list(bio, indent, msg, msglen, value, table) \
25         do_ssl_trace_list(bio, indent, msg, msglen, value, \
26          table, OSSL_NELEM(table))
27
28 static const char *do_ssl_trace_str(int val, ssl_trace_tbl *tbl, size_t ntbl)
29 {
30     size_t i;
31
32     for (i = 0; i < ntbl; i++, tbl++) {
33         if (tbl->num == val)
34             return tbl->name;
35     }
36     return "UNKNOWN";
37 }
38
39 static int do_ssl_trace_list(BIO *bio, int indent,
40                              const unsigned char *msg, size_t msglen,
41                              size_t vlen, ssl_trace_tbl *tbl, size_t ntbl)
42 {
43     int val;
44
45     if (msglen % vlen)
46         return 0;
47     while (msglen) {
48         val = msg[0];
49         if (vlen == 2)
50             val = (val << 8) | msg[1];
51         BIO_indent(bio, indent, 80);
52         BIO_printf(bio, "%s (%d)\n", do_ssl_trace_str(val, tbl, ntbl), val);
53         msg += vlen;
54         msglen -= vlen;
55     }
56     return 1;
57 }
58
59 /* Version number */
60
61 static ssl_trace_tbl ssl_version_tbl[] = {
62     {SSL3_VERSION, "SSL 3.0"},
63     {TLS1_VERSION, "TLS 1.0"},
64     {TLS1_1_VERSION, "TLS 1.1"},
65     {TLS1_2_VERSION, "TLS 1.2"},
66     {TLS1_3_VERSION, "TLS 1.3"},
67     /* TODO(TLS1.3): Remove this line before release */
68     {TLS1_3_VERSION_DRAFT, TLS1_3_VERSION_DRAFT_TXT},
69     {DTLS1_VERSION, "DTLS 1.0"},
70     {DTLS1_2_VERSION, "DTLS 1.2"},
71     {DTLS1_BAD_VER, "DTLS 1.0 (bad)"}
72 };
73
74 static ssl_trace_tbl ssl_content_tbl[] = {
75     {SSL3_RT_CHANGE_CIPHER_SPEC, "ChangeCipherSpec"},
76     {SSL3_RT_ALERT, "Alert"},
77     {SSL3_RT_HANDSHAKE, "Handshake"},
78     {SSL3_RT_APPLICATION_DATA, "ApplicationData"},
79 };
80
81 /* Handshake types */
82 static ssl_trace_tbl ssl_handshake_tbl[] = {
83     {SSL3_MT_HELLO_REQUEST, "HelloRequest"},
84     {SSL3_MT_CLIENT_HELLO, "ClientHello"},
85     {SSL3_MT_SERVER_HELLO, "ServerHello"},
86     {DTLS1_MT_HELLO_VERIFY_REQUEST, "HelloVerifyRequest"},
87     {SSL3_MT_NEWSESSION_TICKET, "NewSessionTicket"},
88     {SSL3_MT_HELLO_RETRY_REQUEST, "HelloRetryRequest"},
89     {SSL3_MT_ENCRYPTED_EXTENSIONS, "EncryptedExtensions"},
90     {SSL3_MT_CERTIFICATE, "Certificate"},
91     {SSL3_MT_SERVER_KEY_EXCHANGE, "ServerKeyExchange"},
92     {SSL3_MT_CERTIFICATE_REQUEST, "CertificateRequest"},
93     {SSL3_MT_SERVER_DONE, "ServerHelloDone"},
94     {SSL3_MT_CERTIFICATE_VERIFY, "CertificateVerify"},
95     {SSL3_MT_CLIENT_KEY_EXCHANGE, "ClientKeyExchange"},
96     {SSL3_MT_CERTIFICATE_STATUS, "CertificateStatus"},
97     {SSL3_MT_CLIENT_KEY_EXCHANGE, "ClientKeyExchange"},
98     {SSL3_MT_FINISHED, "Finished"},
99     {SSL3_MT_CERTIFICATE_STATUS, "CertificateStatus"},
100     {SSL3_MT_KEY_UPDATE, "KeyUpdate"}
101 };
102
103 /* Cipher suites */
104 static ssl_trace_tbl ssl_ciphers_tbl[] = {
105     {0x0000, "SSL_NULL_WITH_NULL_NULL"},
106     {0x0001, "SSL_RSA_WITH_NULL_MD5"},
107     {0x0002, "SSL_RSA_WITH_NULL_SHA"},
108     {0x0003, "SSL_RSA_EXPORT_WITH_RC4_40_MD5"},
109     {0x0004, "SSL_RSA_WITH_RC4_128_MD5"},
110     {0x0005, "SSL_RSA_WITH_RC4_128_SHA"},
111     {0x0006, "SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5"},
112     {0x0007, "SSL_RSA_WITH_IDEA_CBC_SHA"},
113     {0x0008, "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA"},
114     {0x0009, "SSL_RSA_WITH_DES_CBC_SHA"},
115     {0x000A, "SSL_RSA_WITH_3DES_EDE_CBC_SHA"},
116     {0x000B, "SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA"},
117     {0x000C, "SSL_DH_DSS_WITH_DES_CBC_SHA"},
118     {0x000D, "SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA"},
119     {0x000E, "SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA"},
120     {0x000F, "SSL_DH_RSA_WITH_DES_CBC_SHA"},
121     {0x0010, "SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA"},
122     {0x0011, "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA"},
123     {0x0012, "SSL_DHE_DSS_WITH_DES_CBC_SHA"},
124     {0x0013, "SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA"},
125     {0x0014, "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA"},
126     {0x0015, "SSL_DHE_RSA_WITH_DES_CBC_SHA"},
127     {0x0016, "SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA"},
128     {0x0017, "SSL_DH_anon_EXPORT_WITH_RC4_40_MD5"},
129     {0x0018, "SSL_DH_anon_WITH_RC4_128_MD5"},
130     {0x0019, "SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA"},
131     {0x001A, "SSL_DH_anon_WITH_DES_CBC_SHA"},
132     {0x001B, "SSL_DH_anon_WITH_3DES_EDE_CBC_SHA"},
133     {0x001D, "SSL_FORTEZZA_KEA_WITH_FORTEZZA_CBC_SHA"},
134     {0x001E, "SSL_FORTEZZA_KEA_WITH_RC4_128_SHA"},
135     {0x001F, "TLS_KRB5_WITH_3DES_EDE_CBC_SHA"},
136     {0x0020, "TLS_KRB5_WITH_RC4_128_SHA"},
137     {0x0021, "TLS_KRB5_WITH_IDEA_CBC_SHA"},
138     {0x0022, "TLS_KRB5_WITH_DES_CBC_MD5"},
139     {0x0023, "TLS_KRB5_WITH_3DES_EDE_CBC_MD5"},
140     {0x0024, "TLS_KRB5_WITH_RC4_128_MD5"},
141     {0x0025, "TLS_KRB5_WITH_IDEA_CBC_MD5"},
142     {0x0026, "TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA"},
143     {0x0027, "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_SHA"},
144     {0x0028, "TLS_KRB5_EXPORT_WITH_RC4_40_SHA"},
145     {0x0029, "TLS_KRB5_EXPORT_WITH_DES_CBC_40_MD5"},
146     {0x002A, "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_MD5"},
147     {0x002B, "TLS_KRB5_EXPORT_WITH_RC4_40_MD5"},
148     {0x002C, "TLS_PSK_WITH_NULL_SHA"},
149     {0x002D, "TLS_DHE_PSK_WITH_NULL_SHA"},
150     {0x002E, "TLS_RSA_PSK_WITH_NULL_SHA"},
151     {0x002F, "TLS_RSA_WITH_AES_128_CBC_SHA"},
152     {0x0030, "TLS_DH_DSS_WITH_AES_128_CBC_SHA"},
153     {0x0031, "TLS_DH_RSA_WITH_AES_128_CBC_SHA"},
154     {0x0032, "TLS_DHE_DSS_WITH_AES_128_CBC_SHA"},
155     {0x0033, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA"},
156     {0x0034, "TLS_DH_anon_WITH_AES_128_CBC_SHA"},
157     {0x0035, "TLS_RSA_WITH_AES_256_CBC_SHA"},
158     {0x0036, "TLS_DH_DSS_WITH_AES_256_CBC_SHA"},
159     {0x0037, "TLS_DH_RSA_WITH_AES_256_CBC_SHA"},
160     {0x0038, "TLS_DHE_DSS_WITH_AES_256_CBC_SHA"},
161     {0x0039, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA"},
162     {0x003A, "TLS_DH_anon_WITH_AES_256_CBC_SHA"},
163     {0x003B, "TLS_RSA_WITH_NULL_SHA256"},
164     {0x003C, "TLS_RSA_WITH_AES_128_CBC_SHA256"},
165     {0x003D, "TLS_RSA_WITH_AES_256_CBC_SHA256"},
166     {0x003E, "TLS_DH_DSS_WITH_AES_128_CBC_SHA256"},
167     {0x003F, "TLS_DH_RSA_WITH_AES_128_CBC_SHA256"},
168     {0x0040, "TLS_DHE_DSS_WITH_AES_128_CBC_SHA256"},
169     {0x0041, "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA"},
170     {0x0042, "TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA"},
171     {0x0043, "TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA"},
172     {0x0044, "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA"},
173     {0x0045, "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA"},
174     {0x0046, "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA"},
175     {0x0067, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256"},
176     {0x0068, "TLS_DH_DSS_WITH_AES_256_CBC_SHA256"},
177     {0x0069, "TLS_DH_RSA_WITH_AES_256_CBC_SHA256"},
178     {0x006A, "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256"},
179     {0x006B, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256"},
180     {0x006C, "TLS_DH_anon_WITH_AES_128_CBC_SHA256"},
181     {0x006D, "TLS_DH_anon_WITH_AES_256_CBC_SHA256"},
182     {0x0084, "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA"},
183     {0x0085, "TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA"},
184     {0x0086, "TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA"},
185     {0x0087, "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA"},
186     {0x0088, "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA"},
187     {0x0089, "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA"},
188     {0x008A, "TLS_PSK_WITH_RC4_128_SHA"},
189     {0x008B, "TLS_PSK_WITH_3DES_EDE_CBC_SHA"},
190     {0x008C, "TLS_PSK_WITH_AES_128_CBC_SHA"},
191     {0x008D, "TLS_PSK_WITH_AES_256_CBC_SHA"},
192     {0x008E, "TLS_DHE_PSK_WITH_RC4_128_SHA"},
193     {0x008F, "TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA"},
194     {0x0090, "TLS_DHE_PSK_WITH_AES_128_CBC_SHA"},
195     {0x0091, "TLS_DHE_PSK_WITH_AES_256_CBC_SHA"},
196     {0x0092, "TLS_RSA_PSK_WITH_RC4_128_SHA"},
197     {0x0093, "TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA"},
198     {0x0094, "TLS_RSA_PSK_WITH_AES_128_CBC_SHA"},
199     {0x0095, "TLS_RSA_PSK_WITH_AES_256_CBC_SHA"},
200     {0x0096, "TLS_RSA_WITH_SEED_CBC_SHA"},
201     {0x0097, "TLS_DH_DSS_WITH_SEED_CBC_SHA"},
202     {0x0098, "TLS_DH_RSA_WITH_SEED_CBC_SHA"},
203     {0x0099, "TLS_DHE_DSS_WITH_SEED_CBC_SHA"},
204     {0x009A, "TLS_DHE_RSA_WITH_SEED_CBC_SHA"},
205     {0x009B, "TLS_DH_anon_WITH_SEED_CBC_SHA"},
206     {0x009C, "TLS_RSA_WITH_AES_128_GCM_SHA256"},
207     {0x009D, "TLS_RSA_WITH_AES_256_GCM_SHA384"},
208     {0x009E, "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256"},
209     {0x009F, "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384"},
210     {0x00A0, "TLS_DH_RSA_WITH_AES_128_GCM_SHA256"},
211     {0x00A1, "TLS_DH_RSA_WITH_AES_256_GCM_SHA384"},
212     {0x00A2, "TLS_DHE_DSS_WITH_AES_128_GCM_SHA256"},
213     {0x00A3, "TLS_DHE_DSS_WITH_AES_256_GCM_SHA384"},
214     {0x00A4, "TLS_DH_DSS_WITH_AES_128_GCM_SHA256"},
215     {0x00A5, "TLS_DH_DSS_WITH_AES_256_GCM_SHA384"},
216     {0x00A6, "TLS_DH_anon_WITH_AES_128_GCM_SHA256"},
217     {0x00A7, "TLS_DH_anon_WITH_AES_256_GCM_SHA384"},
218     {0x00A8, "TLS_PSK_WITH_AES_128_GCM_SHA256"},
219     {0x00A9, "TLS_PSK_WITH_AES_256_GCM_SHA384"},
220     {0x00AA, "TLS_DHE_PSK_WITH_AES_128_GCM_SHA256"},
221     {0x00AB, "TLS_DHE_PSK_WITH_AES_256_GCM_SHA384"},
222     {0x00AC, "TLS_RSA_PSK_WITH_AES_128_GCM_SHA256"},
223     {0x00AD, "TLS_RSA_PSK_WITH_AES_256_GCM_SHA384"},
224     {0x00AE, "TLS_PSK_WITH_AES_128_CBC_SHA256"},
225     {0x00AF, "TLS_PSK_WITH_AES_256_CBC_SHA384"},
226     {0x00B0, "TLS_PSK_WITH_NULL_SHA256"},
227     {0x00B1, "TLS_PSK_WITH_NULL_SHA384"},
228     {0x00B2, "TLS_DHE_PSK_WITH_AES_128_CBC_SHA256"},
229     {0x00B3, "TLS_DHE_PSK_WITH_AES_256_CBC_SHA384"},
230     {0x00B4, "TLS_DHE_PSK_WITH_NULL_SHA256"},
231     {0x00B5, "TLS_DHE_PSK_WITH_NULL_SHA384"},
232     {0x00B6, "TLS_RSA_PSK_WITH_AES_128_CBC_SHA256"},
233     {0x00B7, "TLS_RSA_PSK_WITH_AES_256_CBC_SHA384"},
234     {0x00B8, "TLS_RSA_PSK_WITH_NULL_SHA256"},
235     {0x00B9, "TLS_RSA_PSK_WITH_NULL_SHA384"},
236     {0x00BA, "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256"},
237     {0x00BB, "TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256"},
238     {0x00BC, "TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256"},
239     {0x00BD, "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256"},
240     {0x00BE, "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256"},
241     {0x00BF, "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA256"},
242     {0x00C0, "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256"},
243     {0x00C1, "TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256"},
244     {0x00C2, "TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256"},
245     {0x00C3, "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256"},
246     {0x00C4, "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256"},
247     {0x00C5, "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA256"},
248     {0x00FF, "TLS_EMPTY_RENEGOTIATION_INFO_SCSV"},
249     {0x5600, "TLS_FALLBACK_SCSV"},
250     {0xC001, "TLS_ECDH_ECDSA_WITH_NULL_SHA"},
251     {0xC002, "TLS_ECDH_ECDSA_WITH_RC4_128_SHA"},
252     {0xC003, "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA"},
253     {0xC004, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA"},
254     {0xC005, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA"},
255     {0xC006, "TLS_ECDHE_ECDSA_WITH_NULL_SHA"},
256     {0xC007, "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA"},
257     {0xC008, "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA"},
258     {0xC009, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA"},
259     {0xC00A, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA"},
260     {0xC00B, "TLS_ECDH_RSA_WITH_NULL_SHA"},
261     {0xC00C, "TLS_ECDH_RSA_WITH_RC4_128_SHA"},
262     {0xC00D, "TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA"},
263     {0xC00E, "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA"},
264     {0xC00F, "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA"},
265     {0xC010, "TLS_ECDHE_RSA_WITH_NULL_SHA"},
266     {0xC011, "TLS_ECDHE_RSA_WITH_RC4_128_SHA"},
267     {0xC012, "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA"},
268     {0xC013, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA"},
269     {0xC014, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA"},
270     {0xC015, "TLS_ECDH_anon_WITH_NULL_SHA"},
271     {0xC016, "TLS_ECDH_anon_WITH_RC4_128_SHA"},
272     {0xC017, "TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA"},
273     {0xC018, "TLS_ECDH_anon_WITH_AES_128_CBC_SHA"},
274     {0xC019, "TLS_ECDH_anon_WITH_AES_256_CBC_SHA"},
275     {0xC01A, "TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA"},
276     {0xC01B, "TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA"},
277     {0xC01C, "TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA"},
278     {0xC01D, "TLS_SRP_SHA_WITH_AES_128_CBC_SHA"},
279     {0xC01E, "TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA"},
280     {0xC01F, "TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA"},
281     {0xC020, "TLS_SRP_SHA_WITH_AES_256_CBC_SHA"},
282     {0xC021, "TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA"},
283     {0xC022, "TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA"},
284     {0xC023, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256"},
285     {0xC024, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384"},
286     {0xC025, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256"},
287     {0xC026, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384"},
288     {0xC027, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256"},
289     {0xC028, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384"},
290     {0xC029, "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256"},
291     {0xC02A, "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384"},
292     {0xC02B, "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256"},
293     {0xC02C, "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384"},
294     {0xC02D, "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256"},
295     {0xC02E, "TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384"},
296     {0xC02F, "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256"},
297     {0xC030, "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"},
298     {0xC031, "TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256"},
299     {0xC032, "TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384"},
300     {0xC033, "TLS_ECDHE_PSK_WITH_RC4_128_SHA"},
301     {0xC034, "TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA"},
302     {0xC035, "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA"},
303     {0xC036, "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA"},
304     {0xC037, "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256"},
305     {0xC038, "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384"},
306     {0xC039, "TLS_ECDHE_PSK_WITH_NULL_SHA"},
307     {0xC03A, "TLS_ECDHE_PSK_WITH_NULL_SHA256"},
308     {0xC03B, "TLS_ECDHE_PSK_WITH_NULL_SHA384"},
309     {0xC03C, "TLS_RSA_WITH_ARIA_128_CBC_SHA256"},
310     {0xC03D, "TLS_RSA_WITH_ARIA_256_CBC_SHA384"},
311     {0xC03E, "TLS_DH_DSS_WITH_ARIA_128_CBC_SHA256"},
312     {0xC03F, "TLS_DH_DSS_WITH_ARIA_256_CBC_SHA384"},
313     {0xC040, "TLS_DH_RSA_WITH_ARIA_128_CBC_SHA256"},
314     {0xC041, "TLS_DH_RSA_WITH_ARIA_256_CBC_SHA384"},
315     {0xC042, "TLS_DHE_DSS_WITH_ARIA_128_CBC_SHA256"},
316     {0xC043, "TLS_DHE_DSS_WITH_ARIA_256_CBC_SHA384"},
317     {0xC044, "TLS_DHE_RSA_WITH_ARIA_128_CBC_SHA256"},
318     {0xC045, "TLS_DHE_RSA_WITH_ARIA_256_CBC_SHA384"},
319     {0xC046, "TLS_DH_anon_WITH_ARIA_128_CBC_SHA256"},
320     {0xC047, "TLS_DH_anon_WITH_ARIA_256_CBC_SHA384"},
321     {0xC048, "TLS_ECDHE_ECDSA_WITH_ARIA_128_CBC_SHA256"},
322     {0xC049, "TLS_ECDHE_ECDSA_WITH_ARIA_256_CBC_SHA384"},
323     {0xC04A, "TLS_ECDH_ECDSA_WITH_ARIA_128_CBC_SHA256"},
324     {0xC04B, "TLS_ECDH_ECDSA_WITH_ARIA_256_CBC_SHA384"},
325     {0xC04C, "TLS_ECDHE_RSA_WITH_ARIA_128_CBC_SHA256"},
326     {0xC04D, "TLS_ECDHE_RSA_WITH_ARIA_256_CBC_SHA384"},
327     {0xC04E, "TLS_ECDH_RSA_WITH_ARIA_128_CBC_SHA256"},
328     {0xC04F, "TLS_ECDH_RSA_WITH_ARIA_256_CBC_SHA384"},
329     {0xC050, "TLS_RSA_WITH_ARIA_128_GCM_SHA256"},
330     {0xC051, "TLS_RSA_WITH_ARIA_256_GCM_SHA384"},
331     {0xC052, "TLS_DHE_RSA_WITH_ARIA_128_GCM_SHA256"},
332     {0xC053, "TLS_DHE_RSA_WITH_ARIA_256_GCM_SHA384"},
333     {0xC054, "TLS_DH_RSA_WITH_ARIA_128_GCM_SHA256"},
334     {0xC055, "TLS_DH_RSA_WITH_ARIA_256_GCM_SHA384"},
335     {0xC056, "TLS_DHE_DSS_WITH_ARIA_128_GCM_SHA256"},
336     {0xC057, "TLS_DHE_DSS_WITH_ARIA_256_GCM_SHA384"},
337     {0xC058, "TLS_DH_DSS_WITH_ARIA_128_GCM_SHA256"},
338     {0xC059, "TLS_DH_DSS_WITH_ARIA_256_GCM_SHA384"},
339     {0xC05A, "TLS_DH_anon_WITH_ARIA_128_GCM_SHA256"},
340     {0xC05B, "TLS_DH_anon_WITH_ARIA_256_GCM_SHA384"},
341     {0xC05C, "TLS_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256"},
342     {0xC05D, "TLS_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384"},
343     {0xC05E, "TLS_ECDH_ECDSA_WITH_ARIA_128_GCM_SHA256"},
344     {0xC05F, "TLS_ECDH_ECDSA_WITH_ARIA_256_GCM_SHA384"},
345     {0xC060, "TLS_ECDHE_RSA_WITH_ARIA_128_GCM_SHA256"},
346     {0xC061, "TLS_ECDHE_RSA_WITH_ARIA_256_GCM_SHA384"},
347     {0xC062, "TLS_ECDH_RSA_WITH_ARIA_128_GCM_SHA256"},
348     {0xC063, "TLS_ECDH_RSA_WITH_ARIA_256_GCM_SHA384"},
349     {0xC064, "TLS_PSK_WITH_ARIA_128_CBC_SHA256"},
350     {0xC065, "TLS_PSK_WITH_ARIA_256_CBC_SHA384"},
351     {0xC066, "TLS_DHE_PSK_WITH_ARIA_128_CBC_SHA256"},
352     {0xC067, "TLS_DHE_PSK_WITH_ARIA_256_CBC_SHA384"},
353     {0xC068, "TLS_RSA_PSK_WITH_ARIA_128_CBC_SHA256"},
354     {0xC069, "TLS_RSA_PSK_WITH_ARIA_256_CBC_SHA384"},
355     {0xC06A, "TLS_PSK_WITH_ARIA_128_GCM_SHA256"},
356     {0xC06B, "TLS_PSK_WITH_ARIA_256_GCM_SHA384"},
357     {0xC06C, "TLS_DHE_PSK_WITH_ARIA_128_GCM_SHA256"},
358     {0xC06D, "TLS_DHE_PSK_WITH_ARIA_256_GCM_SHA384"},
359     {0xC06E, "TLS_RSA_PSK_WITH_ARIA_128_GCM_SHA256"},
360     {0xC06F, "TLS_RSA_PSK_WITH_ARIA_256_GCM_SHA384"},
361     {0xC070, "TLS_ECDHE_PSK_WITH_ARIA_128_CBC_SHA256"},
362     {0xC071, "TLS_ECDHE_PSK_WITH_ARIA_256_CBC_SHA384"},
363     {0xC072, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256"},
364     {0xC073, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384"},
365     {0xC074, "TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256"},
366     {0xC075, "TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384"},
367     {0xC076, "TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256"},
368     {0xC077, "TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384"},
369     {0xC078, "TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256"},
370     {0xC079, "TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384"},
371     {0xC07A, "TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256"},
372     {0xC07B, "TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384"},
373     {0xC07C, "TLS_DHE_RSA_WITH_CAMELLIA_128_GCM_SHA256"},
374     {0xC07D, "TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384"},
375     {0xC07E, "TLS_DH_RSA_WITH_CAMELLIA_128_GCM_SHA256"},
376     {0xC07F, "TLS_DH_RSA_WITH_CAMELLIA_256_GCM_SHA384"},
377     {0xC080, "TLS_DHE_DSS_WITH_CAMELLIA_128_GCM_SHA256"},
378     {0xC081, "TLS_DHE_DSS_WITH_CAMELLIA_256_GCM_SHA384"},
379     {0xC082, "TLS_DH_DSS_WITH_CAMELLIA_128_GCM_SHA256"},
380     {0xC083, "TLS_DH_DSS_WITH_CAMELLIA_256_GCM_SHA384"},
381     {0xC084, "TLS_DH_anon_WITH_CAMELLIA_128_GCM_SHA256"},
382     {0xC085, "TLS_DH_anon_WITH_CAMELLIA_256_GCM_SHA384"},
383     {0xC086, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_GCM_SHA256"},
384     {0xC087, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_GCM_SHA384"},
385     {0xC088, "TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256"},
386     {0xC089, "TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384"},
387     {0xC08A, "TLS_ECDHE_RSA_WITH_CAMELLIA_128_GCM_SHA256"},
388     {0xC08B, "TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384"},
389     {0xC08C, "TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256"},
390     {0xC08D, "TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384"},
391     {0xC08E, "TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256"},
392     {0xC08F, "TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384"},
393     {0xC090, "TLS_DHE_PSK_WITH_CAMELLIA_128_GCM_SHA256"},
394     {0xC091, "TLS_DHE_PSK_WITH_CAMELLIA_256_GCM_SHA384"},
395     {0xC092, "TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256"},
396     {0xC093, "TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384"},
397     {0xC094, "TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256"},
398     {0xC095, "TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384"},
399     {0xC096, "TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256"},
400     {0xC097, "TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384"},
401     {0xC098, "TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256"},
402     {0xC099, "TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384"},
403     {0xC09A, "TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256"},
404     {0xC09B, "TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384"},
405     {0xC09C, "TLS_RSA_WITH_AES_128_CCM"},
406     {0xC09D, "TLS_RSA_WITH_AES_256_CCM"},
407     {0xC09E, "TLS_DHE_RSA_WITH_AES_128_CCM"},
408     {0xC09F, "TLS_DHE_RSA_WITH_AES_256_CCM"},
409     {0xC0A0, "TLS_RSA_WITH_AES_128_CCM_8"},
410     {0xC0A1, "TLS_RSA_WITH_AES_256_CCM_8"},
411     {0xC0A2, "TLS_DHE_RSA_WITH_AES_128_CCM_8"},
412     {0xC0A3, "TLS_DHE_RSA_WITH_AES_256_CCM_8"},
413     {0xC0A4, "TLS_PSK_WITH_AES_128_CCM"},
414     {0xC0A5, "TLS_PSK_WITH_AES_256_CCM"},
415     {0xC0A6, "TLS_DHE_PSK_WITH_AES_128_CCM"},
416     {0xC0A7, "TLS_DHE_PSK_WITH_AES_256_CCM"},
417     {0xC0A8, "TLS_PSK_WITH_AES_128_CCM_8"},
418     {0xC0A9, "TLS_PSK_WITH_AES_256_CCM_8"},
419     {0xC0AA, "TLS_PSK_DHE_WITH_AES_128_CCM_8"},
420     {0xC0AB, "TLS_PSK_DHE_WITH_AES_256_CCM_8"},
421     {0xC0AC, "TLS_ECDHE_ECDSA_WITH_AES_128_CCM"},
422     {0xC0AD, "TLS_ECDHE_ECDSA_WITH_AES_256_CCM"},
423     {0xC0AE, "TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8"},
424     {0xC0AF, "TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8"},
425     {0xCCA8, "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305"},
426     {0xCCA9, "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305"},
427     {0xCCAA, "TLS_DHE_RSA_WITH_CHACHA20_POLY1305"},
428     {0xCCAB, "TLS_PSK_WITH_CHACHA20_POLY1305"},
429     {0xCCAC, "TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305"},
430     {0xCCAD, "TLS_DHE_PSK_WITH_CHACHA20_POLY1305"},
431     {0xCCAE, "TLS_RSA_PSK_WITH_CHACHA20_POLY1305"},
432     {0x1301, "TLS_AES_128_GCM_SHA256"},
433     {0x1302, "TLS_AES_256_GCM_SHA384"},
434     {0x1303, "TLS_CHACHA20_POLY1305_SHA256"},
435     {0x1304, "TLS_AES_128_CCM_SHA256"},
436     {0x1305, "TLS_AES_128_CCM_8_SHA256"},
437     {0xFEFE, "SSL_RSA_FIPS_WITH_DES_CBC_SHA"},
438     {0xFEFF, "SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA"},
439 };
440
441 /* Compression methods */
442 static ssl_trace_tbl ssl_comp_tbl[] = {
443     {0x0000, "No Compression"},
444     {0x0001, "Zlib Compression"}
445 };
446
447 /* Extensions */
448 static ssl_trace_tbl ssl_exts_tbl[] = {
449     {TLSEXT_TYPE_server_name, "server_name"},
450     {TLSEXT_TYPE_max_fragment_length, "max_fragment_length"},
451     {TLSEXT_TYPE_client_certificate_url, "client_certificate_url"},
452     {TLSEXT_TYPE_trusted_ca_keys, "trusted_ca_keys"},
453     {TLSEXT_TYPE_truncated_hmac, "truncated_hmac"},
454     {TLSEXT_TYPE_status_request, "status_request"},
455     {TLSEXT_TYPE_user_mapping, "user_mapping"},
456     {TLSEXT_TYPE_client_authz, "client_authz"},
457     {TLSEXT_TYPE_server_authz, "server_authz"},
458     {TLSEXT_TYPE_cert_type, "cert_type"},
459     {TLSEXT_TYPE_key_share, "key_share"},
460     {TLSEXT_TYPE_psk, "psk"},
461     {TLSEXT_TYPE_psk_kex_modes, "psk_key_exchange_modes"},
462     {TLSEXT_TYPE_supported_groups, "supported_groups"},
463     {TLSEXT_TYPE_ec_point_formats, "ec_point_formats"},
464     {TLSEXT_TYPE_srp, "srp"},
465     {TLSEXT_TYPE_signature_algorithms, "signature_algorithms"},
466     {TLSEXT_TYPE_use_srtp, "use_srtp"},
467     {TLSEXT_TYPE_session_ticket, "session_ticket"},
468     {TLSEXT_TYPE_supported_versions, "supported_versions"},
469     {TLSEXT_TYPE_renegotiate, "renegotiate"},
470 # ifndef OPENSSL_NO_NEXTPROTONEG
471     {TLSEXT_TYPE_next_proto_neg, "next_proto_neg"},
472 # endif
473     {TLSEXT_TYPE_application_layer_protocol_negotiation,
474      "application_layer_protocol_negotiation"},
475     {TLSEXT_TYPE_signed_certificate_timestamp, "signed_certificate_timestamps"},
476     {TLSEXT_TYPE_padding, "padding"},
477     {TLSEXT_TYPE_encrypt_then_mac, "encrypt_then_mac"},
478     {TLSEXT_TYPE_extended_master_secret, "extended_master_secret"}
479 };
480
481 static ssl_trace_tbl ssl_groups_tbl[] = {
482     {1, "sect163k1 (K-163)"},
483     {2, "sect163r1"},
484     {3, "sect163r2 (B-163)"},
485     {4, "sect193r1"},
486     {5, "sect193r2"},
487     {6, "sect233k1 (K-233)"},
488     {7, "sect233r1 (B-233)"},
489     {8, "sect239k1"},
490     {9, "sect283k1 (K-283)"},
491     {10, "sect283r1 (B-283)"},
492     {11, "sect409k1 (K-409)"},
493     {12, "sect409r1 (B-409)"},
494     {13, "sect571k1 (K-571)"},
495     {14, "sect571r1 (B-571)"},
496     {15, "secp160k1"},
497     {16, "secp160r1"},
498     {17, "secp160r2"},
499     {18, "secp192k1"},
500     {19, "secp192r1 (P-192)"},
501     {20, "secp224k1"},
502     {21, "secp224r1 (P-224)"},
503     {22, "secp256k1"},
504     {23, "secp256r1 (P-256)"},
505     {24, "secp384r1 (P-384)"},
506     {25, "secp521r1 (P-521)"},
507     {26, "brainpoolP256r1"},
508     {27, "brainpoolP384r1"},
509     {28, "brainpoolP512r1"},
510     {29, "ecdh_x25519"},
511     {256, "ffdhe2048"},
512     {257, "ffdhe3072"},
513     {258, "ffdhe4096"},
514     {259, "ffdhe6144"},
515     {260, "ffdhe8192"},
516     {0xFF01, "arbitrary_explicit_prime_curves"},
517     {0xFF02, "arbitrary_explicit_char2_curves"}
518 };
519
520 static ssl_trace_tbl ssl_point_tbl[] = {
521     {0, "uncompressed"},
522     {1, "ansiX962_compressed_prime"},
523     {2, "ansiX962_compressed_char2"}
524 };
525
526 static ssl_trace_tbl ssl_sigalg_tbl[] = {
527     {TLSEXT_SIGALG_ecdsa_secp256r1_sha256, "ecdsa_secp256r1_sha256"},
528     {TLSEXT_SIGALG_ecdsa_secp384r1_sha384, "ecdsa_secp384r1_sha384"},
529     {TLSEXT_SIGALG_ecdsa_secp521r1_sha512, "ecdsa_secp521r1_sha512"},
530     {TLSEXT_SIGALG_ecdsa_sha1, "ecdsa_sha1"},
531     {TLSEXT_SIGALG_rsa_pss_sha256, "rsa_pss_sha256"},
532     {TLSEXT_SIGALG_rsa_pss_sha384, "rsa_pss_sha384"},
533     {TLSEXT_SIGALG_rsa_pss_sha512, "rsa_pss_sha512"},
534     {TLSEXT_SIGALG_rsa_pkcs1_sha256, "rsa_pkcs1_sha256"},
535     {TLSEXT_SIGALG_rsa_pkcs1_sha384, "rsa_pkcs1_sha384"},
536     {TLSEXT_SIGALG_rsa_pkcs1_sha512, "rsa_pkcs1_sha512"},
537     {TLSEXT_SIGALG_rsa_pkcs1_sha1, "rsa_pkcs1_sha1"},
538     {TLSEXT_SIGALG_dsa_sha256, "dsa_sha256"},
539     {TLSEXT_SIGALG_dsa_sha384, "dsa_sha384"},
540     {TLSEXT_SIGALG_dsa_sha512, "dsa_sha512"},
541     {TLSEXT_SIGALG_dsa_sha1, "dsa_sha1"},
542     {TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, "gost2012_256"},
543     {TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512, "gost2012_512"},
544     {TLSEXT_SIGALG_gostr34102001_gostr3411, "gost2001_gost94"},
545 };
546
547 static ssl_trace_tbl ssl_ctype_tbl[] = {
548     {1, "rsa_sign"},
549     {2, "dss_sign"},
550     {3, "rsa_fixed_dh"},
551     {4, "dss_fixed_dh"},
552     {5, "rsa_ephemeral_dh"},
553     {6, "dss_ephemeral_dh"},
554     {20, "fortezza_dms"},
555     {64, "ecdsa_sign"},
556     {65, "rsa_fixed_ecdh"},
557     {66, "ecdsa_fixed_ecdh"}
558 };
559
560 static ssl_trace_tbl ssl_psk_kex_modes_tbl[] = {
561     {TLSEXT_KEX_MODE_KE, "psk_ke"},
562     {TLSEXT_KEX_MODE_KE_DHE, "psk_dhe_ke"}
563 };
564
565 static ssl_trace_tbl ssl_crypto_tbl[] = {
566     {TLS1_RT_CRYPTO_PREMASTER, "Premaster Secret"},
567     {TLS1_RT_CRYPTO_CLIENT_RANDOM, "Client Random"},
568     {TLS1_RT_CRYPTO_SERVER_RANDOM, "Server Random"},
569     {TLS1_RT_CRYPTO_MASTER, "Master Secret"},
570     {TLS1_RT_CRYPTO_MAC | TLS1_RT_CRYPTO_WRITE, "Write Mac Secret"},
571     {TLS1_RT_CRYPTO_MAC | TLS1_RT_CRYPTO_READ, "Read Mac Secret"},
572     {TLS1_RT_CRYPTO_KEY | TLS1_RT_CRYPTO_WRITE, "Write Key"},
573     {TLS1_RT_CRYPTO_KEY | TLS1_RT_CRYPTO_READ, "Read Key"},
574     {TLS1_RT_CRYPTO_IV | TLS1_RT_CRYPTO_WRITE, "Write IV"},
575     {TLS1_RT_CRYPTO_IV | TLS1_RT_CRYPTO_READ, "Read IV"},
576     {TLS1_RT_CRYPTO_FIXED_IV | TLS1_RT_CRYPTO_WRITE, "Write IV (fixed part)"},
577     {TLS1_RT_CRYPTO_FIXED_IV | TLS1_RT_CRYPTO_READ, "Read IV (fixed part)"}
578 };
579
580 static ssl_trace_tbl ssl_key_update_tbl[] = {
581     {SSL_KEY_UPDATE_NOT_REQUESTED, "update_not_requested"},
582     {SSL_KEY_UPDATE_REQUESTED, "update_requested"}
583 };
584
585 static void ssl_print_hex(BIO *bio, int indent, const char *name,
586                           const unsigned char *msg, size_t msglen)
587 {
588     size_t i;
589
590     BIO_indent(bio, indent, 80);
591     BIO_printf(bio, "%s (len=%d): ", name, (int)msglen);
592     for (i = 0; i < msglen; i++)
593         BIO_printf(bio, "%02X", msg[i]);
594     BIO_puts(bio, "\n");
595 }
596
597 static int ssl_print_hexbuf(BIO *bio, int indent,
598                             const char *name, size_t nlen,
599                             const unsigned char **pmsg, size_t *pmsglen)
600 {
601     size_t blen;
602     const unsigned char *p = *pmsg;
603
604     if (*pmsglen < nlen)
605         return 0;
606     blen = p[0];
607     if (nlen > 1)
608         blen = (blen << 8) | p[1];
609     if (*pmsglen < nlen + blen)
610         return 0;
611     p += nlen;
612     ssl_print_hex(bio, indent, name, p, blen);
613     *pmsg += blen + nlen;
614     *pmsglen -= blen + nlen;
615     return 1;
616 }
617
618 static int ssl_print_version(BIO *bio, int indent, const char *name,
619                              const unsigned char **pmsg, size_t *pmsglen,
620                              unsigned int *version)
621 {
622     int vers;
623
624     if (*pmsglen < 2)
625         return 0;
626     vers = ((*pmsg)[0] << 8) | (*pmsg)[1];
627     if (version != NULL) {
628         /* TODO(TLS1.3): Remove the draft conditional here before release */
629         *version = (vers == TLS1_3_VERSION_DRAFT) ? TLS1_3_VERSION : vers;
630     }
631     BIO_indent(bio, indent, 80);
632     BIO_printf(bio, "%s=0x%x (%s)\n",
633                name, vers, ssl_trace_str(vers, ssl_version_tbl));
634     *pmsg += 2;
635     *pmsglen -= 2;
636     return 1;
637 }
638
639 static int ssl_print_random(BIO *bio, int indent,
640                             const unsigned char **pmsg, size_t *pmsglen)
641 {
642     unsigned int tm;
643     const unsigned char *p = *pmsg;
644
645     if (*pmsglen < 32)
646         return 0;
647     tm = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
648     p += 4;
649     BIO_indent(bio, indent, 80);
650     BIO_puts(bio, "Random:\n");
651     BIO_indent(bio, indent + 2, 80);
652     BIO_printf(bio, "gmt_unix_time=0x%08X\n", tm);
653     ssl_print_hex(bio, indent + 2, "random_bytes", p, 28);
654     *pmsg += 32;
655     *pmsglen -= 32;
656     return 1;
657 }
658
659 static int ssl_print_signature(BIO *bio, int indent, SSL *s,
660                                const unsigned char **pmsg, size_t *pmsglen)
661 {
662     if (*pmsglen < 2)
663         return 0;
664     if (SSL_USE_SIGALGS(s)) {
665         const unsigned char *p = *pmsg;
666         unsigned int sigalg = (p[0] << 8) | p[1];
667
668         BIO_indent(bio, indent, 80);
669         BIO_printf(bio, "Signature Algorithm: %s (0x%04x)\n",
670                    ssl_trace_str(sigalg, ssl_sigalg_tbl), sigalg);
671         *pmsg += 2;
672         *pmsglen -= 2;
673     }
674     return ssl_print_hexbuf(bio, indent, "Signature", 2, pmsg, pmsglen);
675 }
676
677 static int ssl_print_extension(BIO *bio, int indent, int server,
678                                unsigned char mt, int extype,
679                                const unsigned char *ext, size_t extlen)
680 {
681     size_t xlen, share_len;
682     unsigned int sigalg;
683
684     BIO_indent(bio, indent, 80);
685     BIO_printf(bio, "extension_type=%s(%d), length=%d\n",
686                ssl_trace_str(extype, ssl_exts_tbl), extype, (int)extlen);
687     switch (extype) {
688     case TLSEXT_TYPE_ec_point_formats:
689         if (extlen < 1)
690             return 0;
691         xlen = ext[0];
692         if (extlen != xlen + 1)
693             return 0;
694         return ssl_trace_list(bio, indent + 2, ext + 1, xlen, 1, ssl_point_tbl);
695
696     case TLSEXT_TYPE_supported_groups:
697         if (extlen < 2)
698             return 0;
699         xlen = (ext[0] << 8) | ext[1];
700         if (extlen != xlen + 2)
701             return 0;
702         return ssl_trace_list(bio, indent + 2, ext + 2, xlen, 2, ssl_groups_tbl);
703     case TLSEXT_TYPE_application_layer_protocol_negotiation:
704         if (extlen < 2)
705             return 0;
706         xlen = (ext[0] << 8) | ext[1];
707         if (extlen != xlen + 2)
708             return 0;
709         ext += 2;
710         while (xlen > 0) {
711             size_t plen = *ext++;
712
713             if (plen > xlen + 1)
714                 return 0;
715             BIO_indent(bio, indent + 2, 80);
716             BIO_write(bio, ext, plen);
717             BIO_puts(bio, "\n");
718             ext += plen;
719             xlen -= plen + 1;
720         }
721         return 1;
722
723     case TLSEXT_TYPE_signature_algorithms:
724
725         if (extlen < 2)
726             return 0;
727         xlen = (ext[0] << 8) | ext[1];
728         if (extlen != xlen + 2)
729             return 0;
730         if (xlen & 1)
731             return 0;
732         ext += 2;
733         while (xlen > 0) {
734             BIO_indent(bio, indent + 2, 80);
735             sigalg = (ext[0] << 8) | ext[1];
736             BIO_printf(bio, "%s (0x%04x)\n",
737                        ssl_trace_str(sigalg, ssl_sigalg_tbl), sigalg);
738             xlen -= 2;
739             ext += 2;
740         }
741         break;
742
743     case TLSEXT_TYPE_renegotiate:
744         if (extlen < 1)
745             return 0;
746         xlen = ext[0];
747         if (xlen + 1 != extlen)
748             return 0;
749         ext++;
750         if (xlen) {
751             if (server) {
752                 if (xlen & 1)
753                     return 0;
754                 xlen >>= 1;
755             }
756             ssl_print_hex(bio, indent + 4, "client_verify_data", ext, xlen);
757             if (server) {
758                 ext += xlen;
759                 ssl_print_hex(bio, indent + 4, "server_verify_data", ext, xlen);
760             }
761         } else {
762             BIO_indent(bio, indent + 4, 80);
763             BIO_puts(bio, "<EMPTY>\n");
764         }
765         break;
766
767     case TLSEXT_TYPE_heartbeat:
768         return 0;
769
770     case TLSEXT_TYPE_session_ticket:
771         if (extlen != 0)
772             ssl_print_hex(bio, indent + 4, "ticket", ext, extlen);
773         break;
774
775     case TLSEXT_TYPE_key_share:
776         if (mt == SSL3_MT_HELLO_RETRY_REQUEST) {
777             int group_id;
778
779             if (extlen != 2)
780                 return 0;
781             group_id = (ext[0] << 8) | ext[1];
782             BIO_indent(bio, indent + 4, 80);
783             BIO_printf(bio, "NamedGroup: %s (%d)\n",
784                        ssl_trace_str(group_id, ssl_groups_tbl), group_id);
785             break;
786         }
787         if (extlen < 2)
788             return 0;
789         if (server) {
790             xlen = extlen;
791         } else {
792             xlen = (ext[0] << 8) | ext[1];
793             if (extlen != xlen + 2)
794                 return 0;
795             ext += 2;
796         }
797         for (; xlen > 0; ext += share_len, xlen -= share_len) {
798             int group_id;
799
800             if (xlen < 4)
801                 return 0;
802             group_id = (ext[0] << 8) | ext[1];
803             share_len = (ext[2] << 8) | ext[3];
804             ext += 4;
805             xlen -= 4;
806             if (xlen < share_len)
807                 return 0;
808             BIO_indent(bio, indent + 4, 80);
809             BIO_printf(bio, "NamedGroup: %s (%d)\n",
810                        ssl_trace_str(group_id, ssl_groups_tbl), group_id);
811             ssl_print_hex(bio, indent + 4, "key_exchange: ", ext, share_len);
812         }
813         break;
814
815     case TLSEXT_TYPE_supported_versions:
816         if (extlen < 1)
817             return 0;
818         xlen = ext[0];
819         if (extlen != xlen + 1)
820             return 0;
821         return ssl_trace_list(bio, indent + 2, ext + 1, xlen, 2,
822                               ssl_version_tbl);
823
824     case TLSEXT_TYPE_psk_kex_modes:
825         if (extlen < 1)
826             return 0;
827         xlen = ext[0];
828         if (extlen != xlen + 1)
829             return 0;
830         return ssl_trace_list(bio, indent + 2, ext + 1, xlen, 1,
831                               ssl_psk_kex_modes_tbl);
832
833     default:
834         BIO_dump_indent(bio, (const char *)ext, extlen, indent + 2);
835     }
836     return 1;
837 }
838
839 static int ssl_print_extensions(BIO *bio, int indent, int server,
840                                 unsigned char mt, const unsigned char **msgin,
841                                 size_t *msginlen)
842 {
843     size_t extslen, msglen = *msginlen;
844     const unsigned char *msg = *msgin;
845
846     BIO_indent(bio, indent, 80);
847     if (msglen == 0) {
848         BIO_puts(bio, "No Extensions\n");
849         return 1;
850     }
851     extslen = (msg[0] << 8) | msg[1];
852     if (extslen != msglen - 2)
853         return 0;
854     msg += 2;
855     msglen = extslen;
856     BIO_printf(bio, "extensions, length = %d\n", (int)msglen);
857     while (msglen > 0) {
858         int extype;
859         size_t extlen;
860         if (msglen < 4)
861             return 0;
862         extype = (msg[0] << 8) | msg[1];
863         extlen = (msg[2] << 8) | msg[3];
864         if (msglen < extlen + 4)
865             return 0;
866         msg += 4;
867         if (!ssl_print_extension(bio, indent + 2, server, mt, extype, msg,
868                                  extlen))
869             return 0;
870         msg += extlen;
871         msglen -= extlen + 4;
872     }
873
874     *msgin = msg;
875     *msginlen = msglen;
876     return 1;
877 }
878
879 static int ssl_print_client_hello(BIO *bio, SSL *ssl, int indent,
880                                   const unsigned char *msg, size_t msglen)
881 {
882     size_t len;
883     unsigned int cs;
884
885     if (!ssl_print_version(bio, indent, "client_version", &msg, &msglen, NULL))
886         return 0;
887     if (!ssl_print_random(bio, indent, &msg, &msglen))
888         return 0;
889     if (!ssl_print_hexbuf(bio, indent, "session_id", 1, &msg, &msglen))
890         return 0;
891     if (SSL_IS_DTLS(ssl)) {
892         if (!ssl_print_hexbuf(bio, indent, "cookie", 1, &msg, &msglen))
893             return 0;
894     }
895     if (msglen < 2)
896         return 0;
897     len = (msg[0] << 8) | msg[1];
898     msg += 2;
899     msglen -= 2;
900     BIO_indent(bio, indent, 80);
901     BIO_printf(bio, "cipher_suites (len=%d)\n", (int)len);
902     if (msglen < len || len & 1)
903         return 0;
904     while (len > 0) {
905         cs = (msg[0] << 8) | msg[1];
906         BIO_indent(bio, indent + 2, 80);
907         BIO_printf(bio, "{0x%02X, 0x%02X} %s\n",
908                    msg[0], msg[1], ssl_trace_str(cs, ssl_ciphers_tbl));
909         msg += 2;
910         msglen -= 2;
911         len -= 2;
912     }
913     if (msglen < 1)
914         return 0;
915     len = msg[0];
916     msg++;
917     msglen--;
918     if (msglen < len)
919         return 0;
920     BIO_indent(bio, indent, 80);
921     BIO_printf(bio, "compression_methods (len=%d)\n", (int)len);
922     while (len > 0) {
923         BIO_indent(bio, indent + 2, 80);
924         BIO_printf(bio, "%s (0x%02X)\n",
925                    ssl_trace_str(msg[0], ssl_comp_tbl), msg[0]);
926         msg++;
927         msglen--;
928         len--;
929     }
930     if (!ssl_print_extensions(bio, indent, 0, SSL3_MT_CLIENT_HELLO, &msg,
931                               &msglen))
932         return 0;
933     return 1;
934 }
935
936 static int dtls_print_hello_vfyrequest(BIO *bio, int indent,
937                                        const unsigned char *msg, size_t msglen)
938 {
939     if (!ssl_print_version(bio, indent, "server_version", &msg, &msglen, NULL))
940         return 0;
941     if (!ssl_print_hexbuf(bio, indent, "cookie", 1, &msg, &msglen))
942         return 0;
943     return 1;
944 }
945
946 static int ssl_print_server_hello(BIO *bio, int indent,
947                                   const unsigned char *msg, size_t msglen)
948 {
949     unsigned int cs;
950     unsigned int vers;
951
952     if (!ssl_print_version(bio, indent, "server_version", &msg, &msglen, &vers))
953         return 0;
954     if (!ssl_print_random(bio, indent, &msg, &msglen))
955         return 0;
956     if (vers != TLS1_3_VERSION
957             && !ssl_print_hexbuf(bio, indent, "session_id", 1, &msg, &msglen))
958         return 0;
959     if (msglen < 2)
960         return 0;
961     cs = (msg[0] << 8) | msg[1];
962     BIO_indent(bio, indent, 80);
963     BIO_printf(bio, "cipher_suite {0x%02X, 0x%02X} %s\n",
964                msg[0], msg[1], ssl_trace_str(cs, ssl_ciphers_tbl));
965     msg += 2;
966     msglen -= 2;
967     if (vers != TLS1_3_VERSION) {
968         if (msglen < 1)
969             return 0;
970         BIO_indent(bio, indent, 80);
971         BIO_printf(bio, "compression_method: %s (0x%02X)\n",
972                    ssl_trace_str(msg[0], ssl_comp_tbl), msg[0]);
973         msg++;
974         msglen--;
975     }
976     if (!ssl_print_extensions(bio, indent, 1, SSL3_MT_SERVER_HELLO, &msg,
977                               &msglen))
978         return 0;
979     return 1;
980 }
981
982 static int ssl_get_keyex(const char **pname, SSL *ssl)
983 {
984     unsigned long alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
985
986     if (alg_k & SSL_kRSA) {
987         *pname = "rsa";
988         return SSL_kRSA;
989     }
990     if (alg_k & SSL_kDHE) {
991         *pname = "DHE";
992         return SSL_kDHE;
993     }
994     if (alg_k & SSL_kECDHE) {
995         *pname = "ECDHE";
996         return SSL_kECDHE;
997     }
998     if (alg_k & SSL_kPSK) {
999         *pname = "PSK";
1000         return SSL_kPSK;
1001     }
1002     if (alg_k & SSL_kRSAPSK) {
1003         *pname = "RSAPSK";
1004         return SSL_kRSAPSK;
1005     }
1006     if (alg_k & SSL_kDHEPSK) {
1007         *pname = "DHEPSK";
1008         return SSL_kDHEPSK;
1009     }
1010     if (alg_k & SSL_kECDHEPSK) {
1011         *pname = "ECDHEPSK";
1012         return SSL_kECDHEPSK;
1013     }
1014     if (alg_k & SSL_kSRP) {
1015         *pname = "SRP";
1016         return SSL_kSRP;
1017     }
1018     if (alg_k & SSL_kGOST) {
1019         *pname = "GOST";
1020         return SSL_kGOST;
1021     }
1022     *pname = "UNKNOWN";
1023     return 0;
1024 }
1025
1026 static int ssl_print_client_keyex(BIO *bio, int indent, SSL *ssl,
1027                                   const unsigned char *msg, size_t msglen)
1028 {
1029     const char *algname;
1030     int id = ssl_get_keyex(&algname, ssl);
1031
1032     BIO_indent(bio, indent, 80);
1033     BIO_printf(bio, "KeyExchangeAlgorithm=%s\n", algname);
1034     if (id & SSL_PSK) {
1035         if (!ssl_print_hexbuf(bio, indent + 2,
1036                               "psk_identity", 2, &msg, &msglen))
1037             return 0;
1038     }
1039     switch (id) {
1040
1041     case SSL_kRSA:
1042     case SSL_kRSAPSK:
1043         if (TLS1_get_version(ssl) == SSL3_VERSION) {
1044             ssl_print_hex(bio, indent + 2,
1045                           "EncyptedPreMasterSecret", msg, msglen);
1046         } else {
1047             if (!ssl_print_hexbuf(bio, indent + 2,
1048                                   "EncyptedPreMasterSecret", 2, &msg, &msglen))
1049                 return 0;
1050         }
1051         break;
1052
1053     case SSL_kDHE:
1054     case SSL_kDHEPSK:
1055         if (!ssl_print_hexbuf(bio, indent + 2, "dh_Yc", 2, &msg, &msglen))
1056             return 0;
1057         break;
1058
1059     case SSL_kECDHE:
1060     case SSL_kECDHEPSK:
1061         if (!ssl_print_hexbuf(bio, indent + 2, "ecdh_Yc", 1, &msg, &msglen))
1062             return 0;
1063         break;
1064
1065     }
1066
1067     return !msglen;
1068 }
1069
1070 static int ssl_print_server_keyex(BIO *bio, int indent, SSL *ssl,
1071                                   const unsigned char *msg, size_t msglen)
1072 {
1073     const char *algname;
1074     int id = ssl_get_keyex(&algname, ssl);
1075
1076     BIO_indent(bio, indent, 80);
1077     BIO_printf(bio, "KeyExchangeAlgorithm=%s\n", algname);
1078     if (id & SSL_PSK) {
1079         if (!ssl_print_hexbuf(bio, indent + 2,
1080                               "psk_identity_hint", 2, &msg, &msglen))
1081             return 0;
1082     }
1083     switch (id) {
1084     case SSL_kRSA:
1085
1086         if (!ssl_print_hexbuf(bio, indent + 2, "rsa_modulus", 2, &msg, &msglen))
1087             return 0;
1088         if (!ssl_print_hexbuf(bio, indent + 2, "rsa_exponent", 2,
1089                               &msg, &msglen))
1090             return 0;
1091         break;
1092
1093     case SSL_kDHE:
1094     case SSL_kDHEPSK:
1095         if (!ssl_print_hexbuf(bio, indent + 2, "dh_p", 2, &msg, &msglen))
1096             return 0;
1097         if (!ssl_print_hexbuf(bio, indent + 2, "dh_g", 2, &msg, &msglen))
1098             return 0;
1099         if (!ssl_print_hexbuf(bio, indent + 2, "dh_Ys", 2, &msg, &msglen))
1100             return 0;
1101         break;
1102
1103 # ifndef OPENSSL_NO_EC
1104     case SSL_kECDHE:
1105     case SSL_kECDHEPSK:
1106         if (msglen < 1)
1107             return 0;
1108         BIO_indent(bio, indent + 2, 80);
1109         if (msg[0] == EXPLICIT_PRIME_CURVE_TYPE)
1110             BIO_puts(bio, "explicit_prime\n");
1111         else if (msg[0] == EXPLICIT_CHAR2_CURVE_TYPE)
1112             BIO_puts(bio, "explicit_char2\n");
1113         else if (msg[0] == NAMED_CURVE_TYPE) {
1114             int curve;
1115             if (msglen < 3)
1116                 return 0;
1117             curve = (msg[1] << 8) | msg[2];
1118             BIO_printf(bio, "named_curve: %s (%d)\n",
1119                        ssl_trace_str(curve, ssl_groups_tbl), curve);
1120             msg += 3;
1121             msglen -= 3;
1122             if (!ssl_print_hexbuf(bio, indent + 2, "point", 1, &msg, &msglen))
1123                 return 0;
1124         } else {
1125             BIO_printf(bio, "UNKNOWN CURVE PARAMETER TYPE %d\n", msg[0]);
1126             return 0;
1127         }
1128         break;
1129 # endif
1130
1131     case SSL_kPSK:
1132     case SSL_kRSAPSK:
1133         break;
1134     }
1135     if (!(id & SSL_PSK))
1136         ssl_print_signature(bio, indent, ssl, &msg, &msglen);
1137     return !msglen;
1138 }
1139
1140 static int ssl_print_certificate(BIO *bio, int indent,
1141                                  const unsigned char **pmsg, size_t *pmsglen)
1142 {
1143     size_t msglen = *pmsglen;
1144     size_t clen;
1145     X509 *x;
1146     const unsigned char *p = *pmsg, *q;
1147
1148     if (msglen < 3)
1149         return 0;
1150     clen = (p[0] << 16) | (p[1] << 8) | p[2];
1151     if (msglen < clen + 3)
1152         return 0;
1153     q = p + 3;
1154     BIO_indent(bio, indent, 80);
1155     BIO_printf(bio, "ASN.1Cert, length=%d", (int)clen);
1156     x = d2i_X509(NULL, &q, clen);
1157     if (!x)
1158         BIO_puts(bio, "<UNPARSEABLE CERTIFICATE>\n");
1159     else {
1160         BIO_puts(bio, "\n------details-----\n");
1161         X509_print_ex(bio, x, XN_FLAG_ONELINE, 0);
1162         PEM_write_bio_X509(bio, x);
1163         /* Print certificate stuff */
1164         BIO_puts(bio, "------------------\n");
1165         X509_free(x);
1166     }
1167     if (q != p + 3 + clen) {
1168         BIO_puts(bio, "<TRAILING GARBAGE AFTER CERTIFICATE>\n");
1169     }
1170     *pmsg += clen + 3;
1171     *pmsglen -= clen + 3;
1172     return 1;
1173 }
1174
1175 static int ssl_print_certificates(BIO *bio, SSL *s, int server, int indent,
1176                                   const unsigned char *msg, size_t msglen)
1177 {
1178     size_t clen;
1179
1180     if (SSL_IS_TLS13(s)
1181             && !ssl_print_hexbuf(bio, indent, "context", 1, &msg, &msglen))
1182         return 0;
1183
1184     if (msglen < 3)
1185         return 0;
1186     clen = (msg[0] << 16) | (msg[1] << 8) | msg[2];
1187     if (msglen != clen + 3)
1188         return 0;
1189     msg += 3;
1190     BIO_indent(bio, indent, 80);
1191     BIO_printf(bio, "certificate_list, length=%d\n", (int)clen);
1192     while (clen > 0) {
1193         if (!ssl_print_certificate(bio, indent + 2, &msg, &clen))
1194             return 0;
1195         if (!ssl_print_extensions(bio, indent + 2, server, SSL3_MT_CERTIFICATE,
1196                                   &msg, &clen))
1197             return 0;
1198
1199     }
1200     return 1;
1201 }
1202
1203 static int ssl_print_cert_request(BIO *bio, int indent, SSL *s,
1204                                   const unsigned char *msg, size_t msglen)
1205 {
1206     size_t xlen;
1207     unsigned int sigalg;
1208
1209     if (SSL_IS_TLS13(s)) {
1210         if (!ssl_print_hexbuf(bio, indent, "request_context", 1, &msg, &msglen))
1211             return 0;
1212     } else {
1213         if (msglen < 1)
1214             return 0;
1215         xlen = msg[0];
1216         if (msglen < xlen + 1)
1217             return 0;
1218         msg++;
1219         BIO_indent(bio, indent, 80);
1220         BIO_printf(bio, "certificate_types (len=%d)\n", (int)xlen);
1221         if (!ssl_trace_list(bio, indent + 2, msg, xlen, 1, ssl_ctype_tbl))
1222             return 0;
1223         msg += xlen;
1224         msglen -= xlen + 1;
1225     }
1226     if (SSL_USE_SIGALGS(s)) {
1227         if (msglen < 2)
1228             return 0;
1229         xlen = (msg[0] << 8) | msg[1];
1230         if (msglen < xlen + 2 || (xlen & 1))
1231             return 0;
1232         msg += 2;
1233         msglen -= xlen + 2;
1234         BIO_indent(bio, indent, 80);
1235         BIO_printf(bio, "signature_algorithms (len=%d)\n", (int)xlen);
1236         while (xlen > 0) {
1237             BIO_indent(bio, indent + 2, 80);
1238             sigalg = (msg[0] << 8) | msg[1];
1239             BIO_printf(bio, "%s (0x%04x)\n",
1240                        ssl_trace_str(sigalg, ssl_sigalg_tbl), sigalg);
1241             xlen -= 2;
1242             msg += 2;
1243         }
1244         msg += xlen;
1245     }
1246
1247     xlen = (msg[0] << 8) | msg[1];
1248     BIO_indent(bio, indent, 80);
1249     if (msglen < xlen + 2)
1250         return 0;
1251     msg += 2;
1252     msglen -= 2 + xlen;
1253     BIO_printf(bio, "certificate_authorities (len=%d)\n", (int)xlen);
1254     while (xlen > 0) {
1255         size_t dlen;
1256         X509_NAME *nm;
1257         const unsigned char *p;
1258         if (xlen < 2)
1259             return 0;
1260         dlen = (msg[0] << 8) | msg[1];
1261         if (xlen < dlen + 2)
1262             return 0;
1263         msg += 2;
1264         BIO_indent(bio, indent + 2, 80);
1265         BIO_printf(bio, "DistinguishedName (len=%d): ", (int)dlen);
1266         p = msg;
1267         nm = d2i_X509_NAME(NULL, &p, dlen);
1268         if (!nm) {
1269             BIO_puts(bio, "<UNPARSEABLE DN>\n");
1270         } else {
1271             X509_NAME_print_ex(bio, nm, 0, XN_FLAG_ONELINE);
1272             BIO_puts(bio, "\n");
1273             X509_NAME_free(nm);
1274         }
1275         xlen -= dlen + 2;
1276         msg += dlen;
1277     }
1278     if (SSL_IS_TLS13(s)) {
1279          if (!ssl_print_hexbuf(bio, indent, "request_extensions", 2,
1280                                &msg, &msglen))
1281         return 0;
1282     }
1283     return msglen == 0;
1284 }
1285
1286 static int ssl_print_ticket(BIO *bio, int indent, SSL *s,
1287                             const unsigned char *msg, size_t msglen)
1288 {
1289     unsigned int tick_life;
1290
1291     if (msglen == 0) {
1292         BIO_indent(bio, indent + 2, 80);
1293         BIO_puts(bio, "No Ticket\n");
1294         return 1;
1295     }
1296     if (msglen < 4)
1297         return 0;
1298     tick_life = (msg[0] << 24) | (msg[1] << 16) | (msg[2] << 8) | msg[3];
1299     msglen -= 4;
1300     msg += 4;
1301     BIO_indent(bio, indent + 2, 80);
1302     BIO_printf(bio, "ticket_lifetime_hint=%u\n", tick_life);
1303     if (SSL_IS_TLS13(s)) {
1304         unsigned int ticket_age_add;
1305
1306         if (msglen < 4)
1307             return 0;
1308         ticket_age_add = (msg[0] << 24) | (msg[1] << 16) | (msg[2] << 8)
1309                           | msg[3];
1310         msglen -= 4;
1311         msg += 4;
1312         BIO_indent(bio, indent + 2, 80);
1313         BIO_printf(bio, "ticket_age_add=%u\n", ticket_age_add);
1314     }
1315     if (!ssl_print_hexbuf(bio, indent + 2, "ticket", 2, &msg, &msglen))
1316         return 0;
1317     if (SSL_IS_TLS13(s) && !ssl_print_extensions(bio, indent + 2, 0,
1318                                                  SSL3_MT_NEWSESSION_TICKET,
1319                                                  &msg, &msglen))
1320         return 0;
1321     if (msglen)
1322         return 0;
1323     return 1;
1324 }
1325
1326 static int ssl_print_handshake(BIO *bio, SSL *ssl, int server,
1327                                const unsigned char *msg, size_t msglen,
1328                                int indent)
1329 {
1330     size_t hlen;
1331     unsigned char htype;
1332
1333     if (msglen < 4)
1334         return 0;
1335     htype = msg[0];
1336     hlen = (msg[1] << 16) | (msg[2] << 8) | msg[3];
1337     BIO_indent(bio, indent, 80);
1338     BIO_printf(bio, "%s, Length=%d\n",
1339                ssl_trace_str(htype, ssl_handshake_tbl), (int)hlen);
1340     msg += 4;
1341     msglen -= 4;
1342     if (SSL_IS_DTLS(ssl)) {
1343         if (msglen < 8)
1344             return 0;
1345         BIO_indent(bio, indent, 80);
1346         BIO_printf(bio, "message_seq=%d, fragment_offset=%d, "
1347                    "fragment_length=%d\n",
1348                    (msg[0] << 8) | msg[1],
1349                    (msg[2] << 16) | (msg[3] << 8) | msg[4],
1350                    (msg[5] << 16) | (msg[6] << 8) | msg[7]);
1351         msg += 8;
1352         msglen -= 8;
1353     }
1354     if (msglen < hlen)
1355         return 0;
1356     switch (htype) {
1357     case SSL3_MT_CLIENT_HELLO:
1358         if (!ssl_print_client_hello(bio, ssl, indent + 2, msg, msglen))
1359             return 0;
1360         break;
1361
1362     case DTLS1_MT_HELLO_VERIFY_REQUEST:
1363         if (!dtls_print_hello_vfyrequest(bio, indent + 2, msg, msglen))
1364             return 0;
1365         break;
1366
1367     case SSL3_MT_SERVER_HELLO:
1368         if (!ssl_print_server_hello(bio, indent + 2, msg, msglen))
1369             return 0;
1370         break;
1371
1372     case SSL3_MT_SERVER_KEY_EXCHANGE:
1373         if (!ssl_print_server_keyex(bio, indent + 2, ssl, msg, msglen))
1374             return 0;
1375         break;
1376
1377     case SSL3_MT_CLIENT_KEY_EXCHANGE:
1378         if (!ssl_print_client_keyex(bio, indent + 2, ssl, msg, msglen))
1379             return 0;
1380         break;
1381
1382     case SSL3_MT_CERTIFICATE:
1383         if (!ssl_print_certificates(bio, ssl, server, indent + 2, msg, msglen))
1384             return 0;
1385         break;
1386
1387     case SSL3_MT_CERTIFICATE_VERIFY:
1388         if (!ssl_print_signature(bio, indent + 2, ssl, &msg, &msglen))
1389             return 0;
1390         break;
1391
1392     case SSL3_MT_CERTIFICATE_REQUEST:
1393         if (!ssl_print_cert_request(bio, indent + 2, ssl, msg, msglen))
1394             return 0;
1395         break;
1396
1397     case SSL3_MT_FINISHED:
1398         ssl_print_hex(bio, indent + 2, "verify_data", msg, msglen);
1399         break;
1400
1401     case SSL3_MT_SERVER_DONE:
1402         if (msglen != 0)
1403             ssl_print_hex(bio, indent + 2, "unexpected value", msg, msglen);
1404         break;
1405
1406     case SSL3_MT_NEWSESSION_TICKET:
1407         if (!ssl_print_ticket(bio, indent + 2, ssl, msg, msglen))
1408             return 0;
1409         break;
1410
1411     case SSL3_MT_HELLO_RETRY_REQUEST:
1412         if (!ssl_print_version(bio, indent + 2, "server_version", &msg, &msglen,
1413                                NULL)
1414                 || !ssl_print_extensions(bio, indent + 2, 1,
1415                                          SSL3_MT_HELLO_RETRY_REQUEST, &msg,
1416                                          &msglen))
1417             return 0;
1418         break;
1419
1420     case SSL3_MT_ENCRYPTED_EXTENSIONS:
1421         if (!ssl_print_extensions(bio, indent + 2, 1,
1422                                   SSL3_MT_ENCRYPTED_EXTENSIONS, &msg, &msglen))
1423             return 0;
1424         break;
1425
1426     case SSL3_MT_KEY_UPDATE:
1427         if (msglen != 1) {
1428             ssl_print_hex(bio, indent + 2, "unexpected value", msg, msglen);
1429             return 0;
1430         }
1431         if (!ssl_trace_list(bio, indent + 2, msg, msglen, 1,
1432                             ssl_key_update_tbl))
1433             return 0;
1434         break;
1435
1436     default:
1437         BIO_indent(bio, indent + 2, 80);
1438         BIO_puts(bio, "Unsupported, hex dump follows:\n");
1439         BIO_dump_indent(bio, (const char *)msg, msglen, indent + 4);
1440     }
1441     return 1;
1442 }
1443
1444 const char *SSL_CIPHER_standard_name(const SSL_CIPHER *c)
1445 {
1446     return ssl_trace_str(c->id & 0xFFFF, ssl_ciphers_tbl);
1447 }
1448
1449 void SSL_trace(int write_p, int version, int content_type,
1450                const void *buf, size_t msglen, SSL *ssl, void *arg)
1451 {
1452     const unsigned char *msg = buf;
1453     BIO *bio = arg;
1454
1455     if (write_p == 2) {
1456         BIO_puts(bio, "Session ");
1457         ssl_print_hex(bio, 0,
1458                       ssl_trace_str(content_type, ssl_crypto_tbl), msg, msglen);
1459         return;
1460     }
1461     switch (content_type) {
1462     case SSL3_RT_HEADER:
1463         {
1464             int hvers = msg[1] << 8 | msg[2];
1465             BIO_puts(bio, write_p ? "Sent" : "Received");
1466             BIO_printf(bio, " Record\nHeader:\n  Version = %s (0x%x)\n",
1467                        ssl_trace_str(hvers, ssl_version_tbl), hvers);
1468             if (SSL_IS_DTLS(ssl)) {
1469                 BIO_printf(bio,
1470                            "  epoch=%d, sequence_number=%04x%04x%04x\n",
1471                            (msg[3] << 8 | msg[4]),
1472                            (msg[5] << 8 | msg[6]),
1473                            (msg[7] << 8 | msg[8]), (msg[9] << 8 | msg[10]));
1474             }
1475
1476             BIO_printf(bio, "  Content Type = %s (%d)\n  Length = %d",
1477                        ssl_trace_str(msg[0], ssl_content_tbl), msg[0],
1478                        msg[msglen - 2] << 8 | msg[msglen - 1]);
1479         }
1480         break;
1481     case SSL3_RT_HANDSHAKE:
1482         if (!ssl_print_handshake(bio, ssl, ssl->server ? write_p : !write_p,
1483                                  msg, msglen, 4))
1484             BIO_printf(bio, "Message length parse error!\n");
1485         break;
1486
1487     case SSL3_RT_CHANGE_CIPHER_SPEC:
1488         if (msglen == 1 && msg[0] == 1)
1489             BIO_puts(bio, "    change_cipher_spec (1)\n");
1490         else
1491             ssl_print_hex(bio, 4, "unknown value", msg, msglen);
1492         break;
1493
1494     case SSL3_RT_ALERT:
1495         if (msglen != 2)
1496             BIO_puts(bio, "    Illegal Alert Length\n");
1497         else {
1498             BIO_printf(bio, "    Level=%s(%d), description=%s(%d)\n",
1499                        SSL_alert_type_string_long(msg[0] << 8),
1500                        msg[0], SSL_alert_desc_string_long(msg[1]), msg[1]);
1501         }
1502
1503     }
1504
1505     BIO_puts(bio, "\n");
1506 }
1507
1508 #endif