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