Revert "Avoid duplication."
[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     {0, "none"},
541     {1, "md5"},
542     {2, "sha1"},
543     {3, "sha224"},
544     {4, "sha256"},
545     {5, "sha384"},
546     {6, "sha512"}
547 };
548
549 static ssl_trace_tbl ssl_sig_tbl[] = {
550     {0, "anonymous"},
551     {1, "rsa"},
552     {2, "dsa"},
553     {3, "ecdsa"}
554 };
555
556 static ssl_trace_tbl ssl_hb_tbl[] = {
557     {1, "peer_allowed_to_send"},
558     {2, "peer_not_allowed_to_send"}
559 };
560
561 static ssl_trace_tbl ssl_hb_type_tbl[] = {
562     {1, "heartbeat_request"},
563     {2, "heartbeat_response"}
564 };
565
566 static ssl_trace_tbl ssl_ctype_tbl[] = {
567     {1, "rsa_sign"},
568     {2, "dss_sign"},
569     {3, "rsa_fixed_dh"},
570     {4, "dss_fixed_dh"},
571     {5, "rsa_ephemeral_dh"},
572     {6, "dss_ephemeral_dh"},
573     {20, "fortezza_dms"},
574     {64, "ecdsa_sign"},
575     {65, "rsa_fixed_ecdh"},
576     {66, "ecdsa_fixed_ecdh"}
577 };
578
579 static ssl_trace_tbl ssl_crypto_tbl[] = {
580     {TLS1_RT_CRYPTO_PREMASTER, "Premaster Secret"},
581     {TLS1_RT_CRYPTO_CLIENT_RANDOM, "Client Random"},
582     {TLS1_RT_CRYPTO_SERVER_RANDOM, "Server Random"},
583     {TLS1_RT_CRYPTO_MASTER, "Master Secret"},
584     {TLS1_RT_CRYPTO_MAC | TLS1_RT_CRYPTO_WRITE, "Write Mac Secret"},
585     {TLS1_RT_CRYPTO_MAC | TLS1_RT_CRYPTO_READ, "Read Mac Secret"},
586     {TLS1_RT_CRYPTO_KEY | TLS1_RT_CRYPTO_WRITE, "Write Key"},
587     {TLS1_RT_CRYPTO_KEY | TLS1_RT_CRYPTO_READ, "Read Key"},
588     {TLS1_RT_CRYPTO_IV | TLS1_RT_CRYPTO_WRITE, "Write IV"},
589     {TLS1_RT_CRYPTO_IV | TLS1_RT_CRYPTO_READ, "Read IV"},
590     {TLS1_RT_CRYPTO_FIXED_IV | TLS1_RT_CRYPTO_WRITE, "Write IV (fixed part)"},
591     {TLS1_RT_CRYPTO_FIXED_IV | TLS1_RT_CRYPTO_READ, "Read IV (fixed part)"}
592 };
593
594 static void ssl_print_hex(BIO *bio, int indent, const char *name,
595                           const unsigned char *msg, size_t msglen)
596 {
597     size_t i;
598     BIO_indent(bio, indent, 80);
599     BIO_printf(bio, "%s (len=%d): ", name, (int)msglen);
600     for (i = 0; i < msglen; i++)
601         BIO_printf(bio, "%02X", msg[i]);
602     BIO_puts(bio, "\n");
603 }
604
605 static int ssl_print_hexbuf(BIO *bio, int indent,
606                             const char *name, size_t nlen,
607                             const unsigned char **pmsg, size_t *pmsglen)
608 {
609     size_t blen;
610     const unsigned char *p = *pmsg;
611     if (*pmsglen < nlen)
612         return 0;
613     blen = p[0];
614     if (nlen > 1)
615         blen = (blen << 8) | p[1];
616     if (*pmsglen < nlen + blen)
617         return 0;
618     p += nlen;
619     ssl_print_hex(bio, indent, name, p, blen);
620     *pmsg += blen + nlen;
621     *pmsglen -= blen + nlen;
622     return 1;
623 }
624
625 static int ssl_print_version(BIO *bio, int indent, const char *name,
626                              const unsigned char **pmsg, size_t *pmsglen)
627 {
628     int vers;
629     if (*pmsglen < 2)
630         return 0;
631     vers = ((*pmsg)[0] << 8) | (*pmsg)[1];
632     BIO_indent(bio, indent, 80);
633     BIO_printf(bio, "%s=0x%x (%s)\n",
634                name, vers, ssl_trace_str(vers, ssl_version_tbl));
635     *pmsg += 2;
636     *pmsglen -= 2;
637     return 1;
638 }
639
640 static int ssl_print_random(BIO *bio, int indent,
641                             const unsigned char **pmsg, size_t *pmsglen)
642 {
643     unsigned int tm;
644     const unsigned char *p = *pmsg;
645     if (*pmsglen < 32)
646         return 0;
647     tm = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
648     p += 4;
649     BIO_indent(bio, indent, 80);
650     BIO_puts(bio, "Random:\n");
651     BIO_indent(bio, indent + 2, 80);
652     BIO_printf(bio, "gmt_unix_time=0x%08X\n", tm);
653     ssl_print_hex(bio, indent + 2, "random_bytes", p, 28);
654     *pmsg += 32;
655     *pmsglen -= 32;
656     return 1;
657 }
658
659 static int ssl_print_signature(BIO *bio, int indent, SSL *s,
660                                const unsigned char **pmsg, size_t *pmsglen)
661 {
662     if (*pmsglen < 2)
663         return 0;
664     if (SSL_USE_SIGALGS(s)) {
665         const unsigned char *p = *pmsg;
666         BIO_indent(bio, indent, 80);
667         BIO_printf(bio, "Signature Algorithm %s+%s (%d+%d)\n",
668                    ssl_trace_str(p[0], ssl_md_tbl),
669                    ssl_trace_str(p[1], ssl_sig_tbl), p[0], p[1]);
670         *pmsg += 2;
671         *pmsglen -= 2;
672     }
673     return ssl_print_hexbuf(bio, indent, "Signature", 2, pmsg, pmsglen);
674 }
675
676 static int ssl_print_extension(BIO *bio, int indent, int server, int extype,
677                                const unsigned char *ext, size_t extlen)
678 {
679     size_t xlen;
680     BIO_indent(bio, indent, 80);
681     BIO_printf(bio, "extension_type=%s(%d), length=%d\n",
682                ssl_trace_str(extype, ssl_exts_tbl), extype, (int)extlen);
683     switch (extype) {
684     case TLSEXT_TYPE_ec_point_formats:
685         if (extlen < 1)
686             return 0;
687         xlen = ext[0];
688         if (extlen != xlen + 1)
689             return 0;
690         return ssl_trace_list(bio, indent + 2,
691                               ext + 1, xlen, 1, ssl_point_tbl);
692
693     case TLSEXT_TYPE_elliptic_curves:
694         if (extlen < 2)
695             return 0;
696         xlen = (ext[0] << 8) | ext[1];
697         if (extlen != xlen + 2)
698             return 0;
699         return ssl_trace_list(bio, indent + 2,
700                               ext + 2, xlen, 2, ssl_curve_tbl);
701
702     case TLSEXT_TYPE_signature_algorithms:
703
704         if (extlen < 2)
705             return 0;
706         xlen = (ext[0] << 8) | ext[1];
707         if (extlen != xlen + 2)
708             return 0;
709         if (xlen & 1)
710             return 0;
711         ext += 2;
712         while (xlen > 0) {
713             BIO_indent(bio, indent + 2, 80);
714             BIO_printf(bio, "%s+%s (%d+%d)\n",
715                        ssl_trace_str(ext[0], ssl_md_tbl),
716                        ssl_trace_str(ext[1], ssl_sig_tbl), ext[0], ext[1]);
717             xlen -= 2;
718             ext += 2;
719         }
720         break;
721
722     case TLSEXT_TYPE_renegotiate:
723         if (extlen < 1)
724             return 0;
725         xlen = ext[0];
726         if (xlen + 1 != extlen)
727             return 0;
728         ext++;
729         if (xlen) {
730             if (server) {
731                 if (xlen & 1)
732                     return 0;
733                 xlen >>= 1;
734             }
735             ssl_print_hex(bio, indent + 4, "client_verify_data", ext, xlen);
736             if (server) {
737                 ext += xlen;
738                 ssl_print_hex(bio, indent + 4,
739                               "server_verify_data", ext, xlen);
740             }
741         } else {
742             BIO_indent(bio, indent + 4, 80);
743             BIO_puts(bio, "<EMPTY>\n");
744         }
745         break;
746
747     case TLSEXT_TYPE_heartbeat:
748         if (extlen != 1)
749             return 0;
750         BIO_indent(bio, indent + 2, 80);
751         BIO_printf(bio, "HeartbeatMode: %s\n",
752                    ssl_trace_str(ext[0], ssl_hb_tbl));
753         break;
754
755     case TLSEXT_TYPE_session_ticket:
756         if (extlen != 0)
757             ssl_print_hex(bio, indent + 4, "ticket", ext, extlen);
758         break;
759
760     default:
761         BIO_dump_indent(bio, (char *)ext, extlen, indent + 2);
762     }
763     return 1;
764 }
765
766 static int ssl_print_extensions(BIO *bio, int indent, int server,
767                                 const unsigned char *msg, size_t msglen)
768 {
769     size_t extslen;
770     BIO_indent(bio, indent, 80);
771     if (msglen == 0) {
772         BIO_puts(bio, "No Extensions\n");
773         return 1;
774     }
775     extslen = (msg[0] << 8) | msg[1];
776     if (extslen != msglen - 2)
777         return 0;
778     msg += 2;
779     msglen = extslen;
780     BIO_printf(bio, "extensions, length = %d\n", (int)msglen);
781     while (msglen > 0) {
782         int extype;
783         size_t extlen;
784         if (msglen < 4)
785             return 0;
786         extype = (msg[0] << 8) | msg[1];
787         extlen = (msg[2] << 8) | msg[3];
788         if (msglen < extlen + 4)
789             return 0;
790         msg += 4;
791         if (!ssl_print_extension(bio, indent + 2, server,
792                                  extype, msg, extlen))
793             return 0;
794         msg += extlen;
795         msglen -= extlen + 4;
796     }
797     return 1;
798 }
799
800 static int ssl_print_client_hello(BIO *bio, SSL *ssl, int indent,
801                                   const unsigned char *msg, size_t msglen)
802 {
803     size_t len;
804     unsigned int cs;
805     if (!ssl_print_version(bio, indent, "client_version", &msg, &msglen))
806         return 0;
807     if (!ssl_print_random(bio, indent, &msg, &msglen))
808         return 0;
809     if (!ssl_print_hexbuf(bio, indent, "session_id", 1, &msg, &msglen))
810         return 0;
811     if (SSL_IS_DTLS(ssl)) {
812         if (!ssl_print_hexbuf(bio, indent, "cookie", 1, &msg, &msglen))
813             return 0;
814     }
815     if (msglen < 2)
816         return 0;
817     len = (msg[0] << 8) | msg[1];
818     msg += 2;
819     msglen -= 2;
820     BIO_indent(bio, indent, 80);
821     BIO_printf(bio, "cipher_suites (len=%d)\n", (int)len);
822     if (msglen < len || len & 1)
823         return 0;
824     while (len > 0) {
825         cs = (msg[0] << 8) | msg[1];
826         BIO_indent(bio, indent + 2, 80);
827         BIO_printf(bio, "{0x%02X, 0x%02X} %s\n",
828                    msg[0], msg[1], ssl_trace_str(cs, ssl_ciphers_tbl));
829         msg += 2;
830         msglen -= 2;
831         len -= 2;
832     }
833     if (msglen < 1)
834         return 0;
835     len = msg[0];
836     msg++;
837     msglen--;
838     if (msglen < len)
839         return 0;
840     BIO_indent(bio, indent, 80);
841     BIO_printf(bio, "compression_methods (len=%d)\n", (int)len);
842     while (len > 0) {
843         BIO_indent(bio, indent + 2, 80);
844         BIO_printf(bio, "%s (0x%02X)\n",
845                    ssl_trace_str(msg[0], ssl_comp_tbl), msg[0]);
846         msg++;
847         msglen--;
848         len--;
849     }
850     if (!ssl_print_extensions(bio, indent, 0, msg, msglen))
851         return 0;
852     return 1;
853 }
854
855 static int dtls_print_hello_vfyrequest(BIO *bio, int indent,
856                                        const unsigned char *msg,
857                                        size_t msglen)
858 {
859     if (!ssl_print_version(bio, indent, "server_version", &msg, &msglen))
860         return 0;
861     if (!ssl_print_hexbuf(bio, indent, "cookie", 1, &msg, &msglen))
862         return 0;
863     return 1;
864 }
865
866 static int ssl_print_server_hello(BIO *bio, int indent,
867                                   const unsigned char *msg, size_t msglen)
868 {
869     unsigned int cs;
870     if (!ssl_print_version(bio, indent, "server_version", &msg, &msglen))
871         return 0;
872     if (!ssl_print_random(bio, indent, &msg, &msglen))
873         return 0;
874     if (!ssl_print_hexbuf(bio, indent, "session_id", 1, &msg, &msglen))
875         return 0;
876     if (msglen < 2)
877         return 0;
878     cs = (msg[0] << 8) | msg[1];
879     BIO_indent(bio, indent, 80);
880     BIO_printf(bio, "cipher_suite {0x%02X, 0x%02X} %s\n",
881                msg[0], msg[1], ssl_trace_str(cs, ssl_ciphers_tbl));
882     msg += 2;
883     msglen -= 2;
884     if (msglen < 1)
885         return 0;
886     BIO_indent(bio, indent, 80);
887     BIO_printf(bio, "compression_method: %s (0x%02X)\n",
888                ssl_trace_str(msg[0], ssl_comp_tbl), msg[0]);
889     msg++;
890     msglen--;
891     if (!ssl_print_extensions(bio, indent, 1, msg, msglen))
892         return 0;
893     return 1;
894 }
895
896 static int ssl_get_keyex(const char **pname, SSL *ssl)
897 {
898     unsigned long alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
899     if (alg_k & SSL_kRSA) {
900         *pname = "rsa";
901         return SSL_kRSA;
902     }
903     if (alg_k & SSL_kDHr) {
904         *pname = "dh_rsa";
905         return SSL_kDHr;
906     }
907     if (alg_k & SSL_kDHd) {
908         *pname = "dh_dss";
909         return SSL_kDHd;
910     }
911     if (alg_k & SSL_kDHE) {
912         *pname = "DHE";
913         return SSL_kDHE;
914     }
915     if (alg_k & SSL_kECDHE) {
916         *pname = "ECDHE";
917         return SSL_kECDHE;
918     }
919     if (alg_k & SSL_kECDHr) {
920         *pname = "ECDH RSA";
921         return SSL_kECDHr;
922     }
923     if (alg_k & SSL_kECDHe) {
924         *pname = "ECDH ECDSA";
925         return SSL_kECDHe;
926     }
927     if (alg_k & SSL_kPSK) {
928         *pname = "PSK";
929         return SSL_kPSK;
930     }
931     if (alg_k & SSL_kSRP) {
932         *pname = "SRP";
933         return SSL_kSRP;
934     }
935     if (alg_k & SSL_kGOST) {
936         *pname = "GOST";
937         return SSL_kGOST;
938     }
939     *pname = "UNKNOWN";
940     return 0;
941 }
942
943 static int ssl_print_client_keyex(BIO *bio, int indent, SSL *ssl,
944                                   const unsigned char *msg, size_t msglen)
945 {
946     const char *algname;
947     int id;
948     id = ssl_get_keyex(&algname, ssl);
949     BIO_indent(bio, indent, 80);
950     BIO_printf(bio, "KeyExchangeAlgorithm=%s\n", algname);
951     switch (id) {
952
953     case SSL_kRSA:
954         if (TLS1_get_version(ssl) == SSL3_VERSION) {
955             ssl_print_hex(bio, indent + 2,
956                           "EncyptedPreMasterSecret", msg, msglen);
957         } else {
958             if (!ssl_print_hexbuf(bio, indent + 2,
959                                   "EncyptedPreMasterSecret", 2,
960                                   &msg, &msglen))
961                 return 0;
962         }
963         break;
964
965         /* Implicit parameters only allowed for static DH */
966     case SSL_kDHd:
967     case SSL_kDHr:
968         if (msglen == 0) {
969             BIO_indent(bio, indent + 2, 80);
970             BIO_puts(bio, "implicit\n");
971             break;
972         }
973     case SSL_kDHE:
974         if (!ssl_print_hexbuf(bio, indent + 2, "dh_Yc", 2, &msg, &msglen))
975             return 0;
976         break;
977
978     case SSL_kECDHr:
979     case SSL_kECDHe:
980         if (msglen == 0) {
981             BIO_indent(bio, indent + 2, 80);
982             BIO_puts(bio, "implicit\n");
983             break;
984         }
985     case SSL_kECDHE:
986         if (!ssl_print_hexbuf(bio, indent + 2, "ecdh_Yc", 1, &msg, &msglen))
987             return 0;
988         break;
989     }
990
991     return 1;
992 }
993
994 static int ssl_print_server_keyex(BIO *bio, int indent, SSL *ssl,
995                                   const unsigned char *msg, size_t msglen)
996 {
997     const char *algname;
998     int id;
999     id = ssl_get_keyex(&algname, ssl);
1000     BIO_indent(bio, indent, 80);
1001     BIO_printf(bio, "KeyExchangeAlgorithm=%s\n", algname);
1002     switch (id) {
1003         /* Should never happen */
1004     case SSL_kDHd:
1005     case SSL_kDHr:
1006     case SSL_kECDHr:
1007     case SSL_kECDHe:
1008         BIO_indent(bio, indent + 2, 80);
1009         BIO_printf(bio, "Unexpected Message\n");
1010         break;
1011
1012     case SSL_kRSA:
1013
1014         if (!ssl_print_hexbuf(bio, indent + 2, "rsa_modulus", 2,
1015                               &msg, &msglen))
1016             return 0;
1017         if (!ssl_print_hexbuf(bio, indent + 2, "rsa_exponent", 2,
1018                               &msg, &msglen))
1019             return 0;
1020         break;
1021
1022     case SSL_kDHE:
1023         if (!ssl_print_hexbuf(bio, indent + 2, "dh_p", 2, &msg, &msglen))
1024             return 0;
1025         if (!ssl_print_hexbuf(bio, indent + 2, "dh_g", 2, &msg, &msglen))
1026             return 0;
1027         if (!ssl_print_hexbuf(bio, indent + 2, "dh_Ys", 2, &msg, &msglen))
1028             return 0;
1029         break;
1030
1031     case SSL_kECDHE:
1032         if (msglen < 1)
1033             return 0;
1034         BIO_indent(bio, indent + 2, 80);
1035         if (msg[0] == EXPLICIT_PRIME_CURVE_TYPE)
1036             BIO_puts(bio, "explicit_prime\n");
1037         else if (msg[0] == EXPLICIT_CHAR2_CURVE_TYPE)
1038             BIO_puts(bio, "explicit_char2\n");
1039         else if (msg[0] == NAMED_CURVE_TYPE) {
1040             int curve;
1041             if (msglen < 3)
1042                 return 0;
1043             curve = (msg[1] << 8) | msg[2];
1044             BIO_printf(bio, "named_curve: %s (%d)\n",
1045                        ssl_trace_str(curve, ssl_curve_tbl), curve);
1046             msg += 3;
1047             msglen -= 3;
1048             if (!ssl_print_hexbuf(bio, indent + 2, "point", 1, &msg, &msglen))
1049                 return 0;
1050         }
1051         break;
1052     }
1053     return ssl_print_signature(bio, indent, ssl, &msg, &msglen);
1054 }
1055
1056 static int ssl_print_certificate(BIO *bio, int indent,
1057                                  const unsigned char **pmsg, size_t *pmsglen)
1058 {
1059     size_t msglen = *pmsglen;
1060     size_t clen;
1061     X509 *x;
1062     const unsigned char *p = *pmsg, *q;
1063     if (msglen < 3)
1064         return 0;
1065     clen = (p[0] << 16) | (p[1] << 8) | p[2];
1066     if (msglen < clen + 3)
1067         return 0;
1068     q = p + 3;
1069     BIO_indent(bio, indent, 80);
1070     BIO_printf(bio, "ASN.1Cert, length=%d", (int)clen);
1071     x = d2i_X509(NULL, &q, clen);
1072     if (!x)
1073         BIO_puts(bio, "<UNPARSEABLE CERTIFICATE>\n");
1074     else {
1075         BIO_puts(bio, "\n------details-----\n");
1076         X509_print_ex(bio, x, XN_FLAG_ONELINE, 0);
1077         PEM_write_bio_X509(bio, x);
1078         /* Print certificate stuff */
1079         BIO_puts(bio, "------------------\n");
1080         X509_free(x);
1081     }
1082     if (q != p + 3 + clen) {
1083         BIO_puts(bio, "<TRAILING GARBAGE AFTER CERTIFICATE>\n");
1084     }
1085     *pmsg += clen + 3;
1086     *pmsglen -= clen + 3;
1087     return 1;
1088 }
1089
1090 static int ssl_print_certificates(BIO *bio, int indent,
1091                                   const unsigned char *msg, size_t msglen)
1092 {
1093     size_t clen;
1094     if (msglen < 3)
1095         return 0;
1096     clen = (msg[0] << 16) | (msg[1] << 8) | msg[2];
1097     if (msglen != clen + 3)
1098         return 0;
1099     msg += 3;
1100     BIO_indent(bio, indent, 80);
1101     BIO_printf(bio, "certificate_list, length=%d\n", (int)clen);
1102     while (clen > 0) {
1103         if (!ssl_print_certificate(bio, indent + 2, &msg, &clen))
1104             return 0;
1105     }
1106     return 1;
1107 }
1108
1109 static int ssl_print_cert_request(BIO *bio, int indent, SSL *s,
1110                                   const unsigned char *msg, size_t msglen)
1111 {
1112     size_t xlen;
1113     if (msglen < 1)
1114         return 0;
1115     xlen = msg[0];
1116     if (msglen < xlen + 1)
1117         return 0;
1118     msg++;
1119     BIO_indent(bio, indent, 80);
1120     BIO_printf(bio, "certificate_types (len=%d)\n", (int)xlen);
1121     if (!ssl_trace_list(bio, indent + 2, msg, xlen, 1, ssl_ctype_tbl))
1122         return 0;
1123     msg += xlen;
1124     msglen -= xlen + 1;
1125     if (!SSL_USE_SIGALGS(s))
1126         goto skip_sig;
1127     if (msglen < 2)
1128         return 0;
1129     xlen = (msg[0] << 8) | msg[1];
1130     if (msglen < xlen + 2 || (xlen & 1))
1131         return 0;
1132     msg += 2;
1133     BIO_indent(bio, indent, 80);
1134     BIO_printf(bio, "signature_algorithms (len=%d)\n", (int)xlen);
1135     while (xlen > 0) {
1136         BIO_indent(bio, indent + 2, 80);
1137         BIO_printf(bio, "%s+%s (%d+%d)\n",
1138                    ssl_trace_str(msg[0], ssl_md_tbl),
1139                    ssl_trace_str(msg[1], ssl_sig_tbl), msg[0], msg[1]);
1140         xlen -= 2;
1141         msg += 2;
1142     }
1143     msg += xlen;
1144     msglen -= xlen + 2;
1145
1146  skip_sig:
1147     xlen = (msg[0] << 8) | msg[1];
1148     BIO_indent(bio, indent, 80);
1149     if (msglen < xlen + 2)
1150         return 0;
1151     msg += 2;
1152     msglen -= 2;
1153     BIO_printf(bio, "certificate_authorities (len=%d)\n", (int)xlen);
1154     while (xlen > 0) {
1155         size_t dlen;
1156         X509_NAME *nm;
1157         const unsigned char *p;
1158         if (xlen < 2)
1159             return 0;
1160         dlen = (msg[0] << 8) | msg[1];
1161         if (xlen < dlen + 2)
1162             return 0;
1163         msg += 2;
1164         BIO_indent(bio, indent + 2, 80);
1165         BIO_printf(bio, "DistinguishedName (len=%d): ", (int)dlen);
1166         p = msg;
1167         nm = d2i_X509_NAME(NULL, &p, dlen);
1168         if (!nm) {
1169             BIO_puts(bio, "<UNPARSEABLE DN>\n");
1170         } else {
1171             X509_NAME_print_ex(bio, nm, 0, XN_FLAG_ONELINE);
1172             BIO_puts(bio, "\n");
1173             X509_NAME_free(nm);
1174         }
1175         xlen -= dlen + 2;
1176         msg += dlen;
1177     }
1178     return 1;
1179 }
1180
1181 static int ssl_print_ticket(BIO *bio, int indent,
1182                             const unsigned char *msg, size_t msglen)
1183 {
1184     unsigned int tick_life;
1185     if (msglen == 0) {
1186         BIO_indent(bio, indent + 2, 80);
1187         BIO_puts(bio, "No Ticket\n");
1188         return 1;
1189     }
1190     if (msglen < 4)
1191         return 0;
1192     tick_life = (msg[0] << 24) | (msg[1] << 16) | (msg[2] << 8) | msg[3];
1193     msglen -= 4;
1194     msg += 4;
1195     BIO_indent(bio, indent + 2, 80);
1196     BIO_printf(bio, "ticket_lifetime_hint=%u\n", tick_life);
1197     if (!ssl_print_hexbuf(bio, indent + 2, "ticket", 2, &msg, &msglen))
1198         return 0;
1199     if (msglen)
1200         return 0;
1201     return 1;
1202 }
1203
1204 static int ssl_print_handshake(BIO *bio, SSL *ssl,
1205                                const unsigned char *msg, size_t msglen,
1206                                int indent)
1207 {
1208     size_t hlen;
1209     unsigned char htype;
1210     if (msglen < 4)
1211         return 0;
1212     htype = msg[0];
1213     hlen = (msg[1] << 16) | (msg[2] << 8) | msg[3];
1214     BIO_indent(bio, indent, 80);
1215     BIO_printf(bio, "%s, Length=%d\n",
1216                ssl_trace_str(htype, ssl_handshake_tbl), (int)hlen);
1217     msg += 4;
1218     msglen -= 4;
1219     if (SSL_IS_DTLS(ssl)) {
1220         if (msglen < 8)
1221             return 0;
1222         BIO_indent(bio, indent, 80);
1223         BIO_printf(bio, "message_seq=%d, fragment_offset=%d, "
1224                    "fragment_length=%d\n",
1225                    (msg[0] << 8) | msg[1],
1226                    (msg[2] << 16) | (msg[3] << 8) | msg[4],
1227                    (msg[5] << 16) | (msg[6] << 8) | msg[7]);
1228         msg += 8;
1229         msglen -= 8;
1230     }
1231     if (msglen < hlen)
1232         return 0;
1233     switch (htype) {
1234     case SSL3_MT_CLIENT_HELLO:
1235         if (!ssl_print_client_hello(bio, ssl, indent + 2, msg, msglen))
1236             return 0;
1237         break;
1238
1239     case DTLS1_MT_HELLO_VERIFY_REQUEST:
1240         if (!dtls_print_hello_vfyrequest(bio, indent + 2, msg, msglen))
1241             return 0;
1242         break;
1243
1244     case SSL3_MT_SERVER_HELLO:
1245         if (!ssl_print_server_hello(bio, indent + 2, msg, msglen))
1246             return 0;
1247         break;
1248
1249     case SSL3_MT_SERVER_KEY_EXCHANGE:
1250         if (!ssl_print_server_keyex(bio, indent + 2, ssl, msg, msglen))
1251             return 0;
1252         break;
1253
1254     case SSL3_MT_CLIENT_KEY_EXCHANGE:
1255         if (!ssl_print_client_keyex(bio, indent + 2, ssl, msg, msglen))
1256             return 0;
1257         break;
1258
1259     case SSL3_MT_CERTIFICATE:
1260         if (!ssl_print_certificates(bio, indent + 2, msg, msglen))
1261             return 0;
1262         break;
1263
1264     case SSL3_MT_CERTIFICATE_VERIFY:
1265         if (!ssl_print_signature(bio, indent + 2, ssl, &msg, &msglen))
1266             return 0;
1267         break;
1268
1269     case SSL3_MT_CERTIFICATE_REQUEST:
1270         if (!ssl_print_cert_request(bio, indent + 2, ssl, msg, msglen))
1271             return 0;
1272         break;
1273
1274     case SSL3_MT_FINISHED:
1275         ssl_print_hex(bio, indent + 2, "verify_data", msg, msglen);
1276         break;
1277
1278     case SSL3_MT_SERVER_DONE:
1279         if (msglen != 0)
1280             ssl_print_hex(bio, indent + 2, "unexpected value", msg, msglen);
1281         break;
1282
1283     case SSL3_MT_NEWSESSION_TICKET:
1284         if (!ssl_print_ticket(bio, indent + 2, msg, msglen))
1285             return 0;
1286         break;
1287
1288     default:
1289         BIO_indent(bio, indent + 2, 80);
1290         BIO_puts(bio, "Unsupported, hex dump follows:\n");
1291         BIO_dump_indent(bio, (char *)msg, msglen, indent + 4);
1292     }
1293     return 1;
1294 }
1295
1296 static int ssl_print_heartbeat(BIO *bio, int indent,
1297                                const unsigned char *msg, size_t msglen)
1298 {
1299     if (msglen < 3)
1300         return 0;
1301     BIO_indent(bio, indent, 80);
1302     BIO_printf(bio, "HeartBeatMessageType: %s\n",
1303                ssl_trace_str(msg[0], ssl_hb_type_tbl));
1304     msg++;
1305     msglen--;
1306     if (!ssl_print_hexbuf(bio, indent, "payload", 2, &msg, &msglen))
1307         return 0;
1308     ssl_print_hex(bio, indent, "padding", msg, msglen);
1309     return 1;
1310 }
1311
1312 const char *SSL_CIPHER_standard_name(const SSL_CIPHER *c)
1313 {
1314     return ssl_trace_str(c->id & 0xFFFF, ssl_ciphers_tbl);
1315 }
1316
1317 void SSL_trace(int write_p, int version, int content_type,
1318                const void *buf, size_t msglen, SSL *ssl, void *arg)
1319 {
1320     const unsigned char *msg = buf;
1321     BIO *bio = arg;
1322
1323     if (write_p == 2) {
1324         BIO_puts(bio, "Session ");
1325         ssl_print_hex(bio, 0,
1326                       ssl_trace_str(content_type, ssl_crypto_tbl),
1327                       msg, msglen);
1328         return;
1329     }
1330     switch (content_type) {
1331     case SSL3_RT_HEADER:
1332         {
1333             int hvers = msg[1] << 8 | msg[2];
1334             BIO_puts(bio, write_p ? "Sent" : "Received");
1335             BIO_printf(bio, " Record\nHeader:\n  Version = %s (0x%x)\n",
1336                        ssl_trace_str(hvers, ssl_version_tbl), hvers);
1337             if (SSL_IS_DTLS(ssl)) {
1338                 BIO_printf(bio,
1339                            "  epoch=%d, sequence_number=%04x%04x%04x\n",
1340                            (msg[3] << 8 | msg[4]),
1341                            (msg[5] << 8 | msg[6]),
1342                            (msg[7] << 8 | msg[8]), (msg[9] << 8 | msg[10]));
1343             }
1344
1345             BIO_printf(bio, "  Content Type = %s (%d)\n  Length = %d",
1346                        ssl_trace_str(msg[0], ssl_content_tbl), msg[0],
1347                        msg[msglen - 2] << 8 | msg[msglen - 1]);
1348         }
1349         break;
1350     case SSL3_RT_HANDSHAKE:
1351         if (!ssl_print_handshake(bio, ssl, msg, msglen, 4))
1352             BIO_printf(bio, "Message length parse error!\n");
1353         break;
1354
1355     case SSL3_RT_CHANGE_CIPHER_SPEC:
1356         if (msglen == 1 && msg[0] == 1)
1357             BIO_puts(bio, "    change_cipher_spec (1)\n");
1358         else
1359             ssl_print_hex(bio, 4, "unknown value", msg, msglen);
1360         break;
1361
1362     case SSL3_RT_ALERT:
1363         if (msglen != 2)
1364             BIO_puts(bio, "    Illegal Alert Length\n");
1365         else {
1366             BIO_printf(bio, "    Level=%s(%d), description=%s(%d)\n",
1367                        SSL_alert_type_string_long(msg[0] << 8),
1368                        msg[0], SSL_alert_desc_string_long(msg[1]), msg[1]);
1369         }
1370     case TLS1_RT_HEARTBEAT:
1371         ssl_print_heartbeat(bio, 4, msg, msglen);
1372         break;
1373
1374     }
1375
1376     BIO_puts(bio, "\n");
1377 }
1378
1379 #endif