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