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