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