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