47539a57cd631e4ac2af1cca4247986545d34e59
[openssl.git] / ssl / t1_trce.c
1 /* ssl/t1_trce.c */
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         {
63         int num;
64         const char *name;
65         } ssl_trace_tbl;
66
67 #define ssl_trace_str(val, tbl) \
68         do_ssl_trace_str(val, tbl, sizeof(tbl)/sizeof(ssl_trace_tbl))
69
70 #define ssl_trace_list(bio, indent, msg, msglen, value, table) \
71         do_ssl_trace_list(bio, indent, msg, msglen, value, \
72          table, sizeof(table)/sizeof(ssl_trace_tbl))
73  
74
75 static const char *do_ssl_trace_str(int val, ssl_trace_tbl *tbl, size_t ntbl)
76         {
77         size_t i;
78         for (i = 0; i < ntbl; i++, tbl++)
79                 {
80                 if (tbl->num == val)
81                         return tbl->name;
82                 }
83         return "UNKNOWN";
84         }
85
86 static int do_ssl_trace_list(BIO *bio, int indent,
87                                 const unsigned char *msg, size_t msglen,
88                                 size_t vlen, ssl_trace_tbl *tbl, size_t ntbl)
89         {
90         int val;
91         if (msglen%vlen)
92                 return 0;
93         while(msglen)
94                 {
95                 val = msg[0];
96                 if (vlen == 2)
97                         val = (val << 8) | msg[1];
98                 BIO_indent(bio, indent, 80);
99                 BIO_printf(bio, "%s (%d)\n",
100                                         do_ssl_trace_str(val, tbl, ntbl), val);
101                 msg += vlen;
102                 msglen -= vlen;
103                 }
104         return 1;
105         }
106
107 /* Version number */
108
109 static ssl_trace_tbl ssl_version_tbl[] = {
110         {SSL2_VERSION,          "SSL 2.0"},
111         {SSL3_VERSION,          "SSL 3.0"},
112         {TLS1_VERSION,          "TLS 1.0"},
113         {TLS1_1_VERSION,        "TLS 1.1"},
114         {TLS1_2_VERSION,        "TLS 1.2"},
115         {DTLS1_VERSION,         "DTLS 1.0"},
116         {DTLS1_BAD_VER,         "DTLS 1.0 (bad)"}
117 };
118
119 static ssl_trace_tbl ssl_content_tbl[] = {
120         {SSL3_RT_CHANGE_CIPHER_SPEC,    "ChangeCipherSpec"},
121         {SSL3_RT_ALERT,                 "Alert"},
122         {SSL3_RT_HANDSHAKE,             "Handshake"},
123         {SSL3_RT_APPLICATION_DATA,      "ApplicationData"},
124         {TLS1_RT_HEARTBEAT,             "HeartBeat"}
125 };
126 /* Handshake types */
127 static ssl_trace_tbl ssl_handshake_tbl[] = {
128         {SSL3_MT_HELLO_REQUEST,         "HelloRequest"},
129         {SSL3_MT_CLIENT_HELLO,          "ClientHello"},
130         {SSL3_MT_SERVER_HELLO,          "ServerHello"},
131         {DTLS1_MT_HELLO_VERIFY_REQUEST, "HelloVerifyRequest"},
132         {SSL3_MT_NEWSESSION_TICKET,     "NewSessionTicket"},
133         {SSL3_MT_CERTIFICATE,           "Certificate"},
134         {SSL3_MT_SERVER_KEY_EXCHANGE,   "ServerKeyExchange"},
135         {SSL3_MT_CERTIFICATE_REQUEST,   "CertificateRequest"},
136         {SSL3_MT_CLIENT_KEY_EXCHANGE,   "ClientKeyExchange"},
137         {SSL3_MT_CERTIFICATE_STATUS,    "CertificateStatus"},
138         {SSL3_MT_SERVER_DONE,           "ServerHelloDone"},
139         {SSL3_MT_CERTIFICATE_VERIFY,    "CertificateVerify"},
140         {SSL3_MT_CLIENT_KEY_EXCHANGE,   "ClientKeyExchange"},
141         {SSL3_MT_FINISHED,              "Finished"},
142         {SSL3_MT_CERTIFICATE_STATUS,    "CertificateStatus"}
143 };
144 /* Cipher suites */
145 static ssl_trace_tbl ssl_ciphers_tbl[] = {
146         {0x0000, "SSL_NULL_WITH_NULL_NULL"},
147         {0x0001, "SSL_RSA_WITH_NULL_MD5"},
148         {0x0002, "SSL_RSA_WITH_NULL_SHA"},
149         {0x0003, "SSL_RSA_EXPORT_WITH_RC4_40_MD5"},
150         {0x0004, "SSL_RSA_WITH_RC4_128_MD5"},
151         {0x0005, "SSL_RSA_WITH_RC4_128_SHA"},
152         {0x0006, "SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5"},
153         {0x0007, "SSL_RSA_WITH_IDEA_CBC_SHA"},
154         {0x0008, "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA"},
155         {0x0009, "SSL_RSA_WITH_DES_CBC_SHA"},
156         {0x000A, "SSL_RSA_WITH_3DES_EDE_CBC_SHA"},
157         {0x000B, "SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA"},
158         {0x000C, "SSL_DH_DSS_WITH_DES_CBC_SHA"},
159         {0x000D, "SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA"},
160         {0x000E, "SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA"},
161         {0x000F, "SSL_DH_RSA_WITH_DES_CBC_SHA"},
162         {0x0010, "SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA"},
163         {0x0011, "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA"},
164         {0x0012, "SSL_DHE_DSS_WITH_DES_CBC_SHA"},
165         {0x0013, "SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA"},
166         {0x0014, "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA"},
167         {0x0015, "SSL_DHE_RSA_WITH_DES_CBC_SHA"},
168         {0x0016, "SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA"},
169         {0x0017, "SSL_DH_anon_EXPORT_WITH_RC4_40_MD5"},
170         {0x0018, "SSL_DH_anon_WITH_RC4_128_MD5"},
171         {0x0019, "SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA"},
172         {0x001A, "SSL_DH_anon_WITH_DES_CBC_SHA"},
173         {0x001B, "SSL_DH_anon_WITH_3DES_EDE_CBC_SHA"},
174         {0x001D, "SSL_FORTEZZA_KEA_WITH_FORTEZZA_CBC_SHA"},
175         {0x001E, "SSL_FORTEZZA_KEA_WITH_RC4_128_SHA"},
176         {0x001F, "TLS_KRB5_WITH_3DES_EDE_CBC_SHA"},
177         {0x0020, "TLS_KRB5_WITH_RC4_128_SHA"},
178         {0x0021, "TLS_KRB5_WITH_IDEA_CBC_SHA"},
179         {0x0022, "TLS_KRB5_WITH_DES_CBC_MD5"},
180         {0x0023, "TLS_KRB5_WITH_3DES_EDE_CBC_MD5"},
181         {0x0024, "TLS_KRB5_WITH_RC4_128_MD5"},
182         {0x0025, "TLS_KRB5_WITH_IDEA_CBC_MD5"},
183         {0x0026, "TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA"},
184         {0x0027, "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_SHA"},
185         {0x0028, "TLS_KRB5_EXPORT_WITH_RC4_40_SHA"},
186         {0x0029, "TLS_KRB5_EXPORT_WITH_DES_CBC_40_MD5"},
187         {0x002A, "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_MD5"},
188         {0x002B, "TLS_KRB5_EXPORT_WITH_RC4_40_MD5"},
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         {0xC001, "TLS_ECDH_ECDSA_WITH_NULL_SHA"},
288         {0xC002, "TLS_ECDH_ECDSA_WITH_RC4_128_SHA"},
289         {0xC003, "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA"},
290         {0xC004, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA"},
291         {0xC005, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA"},
292         {0xC006, "TLS_ECDHE_ECDSA_WITH_NULL_SHA"},
293         {0xC007, "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA"},
294         {0xC008, "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA"},
295         {0xC009, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA"},
296         {0xC00A, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA"},
297         {0xC00B, "TLS_ECDH_RSA_WITH_NULL_SHA"},
298         {0xC00C, "TLS_ECDH_RSA_WITH_RC4_128_SHA"},
299         {0xC00D, "TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA"},
300         {0xC00E, "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA"},
301         {0xC00F, "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA"},
302         {0xC010, "TLS_ECDHE_RSA_WITH_NULL_SHA"},
303         {0xC011, "TLS_ECDHE_RSA_WITH_RC4_128_SHA"},
304         {0xC012, "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA"},
305         {0xC013, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA"},
306         {0xC014, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA"},
307         {0xC015, "TLS_ECDH_anon_WITH_NULL_SHA"},
308         {0xC016, "TLS_ECDH_anon_WITH_RC4_128_SHA"},
309         {0xC017, "TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA"},
310         {0xC018, "TLS_ECDH_anon_WITH_AES_128_CBC_SHA"},
311         {0xC019, "TLS_ECDH_anon_WITH_AES_256_CBC_SHA"},
312         {0xC01A, "TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA"},
313         {0xC01B, "TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA"},
314         {0xC01C, "TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA"},
315         {0xC01D, "TLS_SRP_SHA_WITH_AES_128_CBC_SHA"},
316         {0xC01E, "TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA"},
317         {0xC01F, "TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA"},
318         {0xC020, "TLS_SRP_SHA_WITH_AES_256_CBC_SHA"},
319         {0xC021, "TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA"},
320         {0xC022, "TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA"},
321         {0xC023, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256"},
322         {0xC024, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384"},
323         {0xC025, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256"},
324         {0xC026, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384"},
325         {0xC027, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256"},
326         {0xC028, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384"},
327         {0xC029, "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256"},
328         {0xC02A, "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384"},
329         {0xC02B, "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256"},
330         {0xC02C, "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384"},
331         {0xC02D, "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256"},
332         {0xC02E, "TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384"},
333         {0xC02F, "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256"},
334         {0xC030, "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"},
335         {0xC031, "TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256"},
336         {0xC032, "TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384"},
337         {0xFEFE, "SSL_RSA_FIPS_WITH_DES_CBC_SHA"},
338         {0xFEFF, "SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA"},
339 };
340 /* Compression methods */
341 static ssl_trace_tbl ssl_comp_tbl[] = {
342         {0x0000, "No Compression"},
343         {0x0001, "Zlib Compression"}
344 };
345 /* Extensions */
346 static ssl_trace_tbl ssl_exts_tbl[] = {
347         {TLSEXT_TYPE_server_name, "server_name"},
348         {TLSEXT_TYPE_max_fragment_length, "max_fragment_length"},
349         {TLSEXT_TYPE_client_certificate_url, "client_certificate_url"},
350         {TLSEXT_TYPE_trusted_ca_keys, "trusted_ca_keys"},
351         {TLSEXT_TYPE_truncated_hmac, "truncated_hmac"},
352         {TLSEXT_TYPE_status_request, "status_request"},
353         {TLSEXT_TYPE_user_mapping, "user_mapping"},
354         {TLSEXT_TYPE_client_authz, "client_authz"},
355         {TLSEXT_TYPE_server_authz, "server_authz"},
356         {TLSEXT_TYPE_cert_type, "cert_type"},
357         {TLSEXT_TYPE_elliptic_curves, "elliptic_curves"},
358         {TLSEXT_TYPE_ec_point_formats, "ec_point_formats"},
359         {TLSEXT_TYPE_srp, "srp"},
360         {TLSEXT_TYPE_signature_algorithms, "signature_algorithms"},
361         {TLSEXT_TYPE_use_srtp, "use_srtp"},
362         {TLSEXT_TYPE_heartbeat, "heartbeat"},
363         {TLSEXT_TYPE_session_ticket, "session_ticket"},
364 #ifdef TLSEXT_TYPE_opaque_prf_input
365         {TLSEXT_TYPE_opaque_prf_input, "opaque_prf_input"},
366 #endif
367         {TLSEXT_TYPE_renegotiate, "renegotiate"},
368         {TLSEXT_TYPE_next_proto_neg, "next_proto_neg"}
369 };
370
371 static ssl_trace_tbl ssl_curve_tbl[] = {
372         {1, "sect163k1 (K-163)"},
373         {2, "sect163r1"},
374         {3, "sect163r2 (B-163)"},
375         {4, "sect193r1"},
376         {5, "sect193r2"},
377         {6, "sect233k1 (K-233)"},
378         {7, "sect233r1 (B-233)"},
379         {8, "sect239k1"},
380         {9, "sect283k1 (K-283)"},
381         {10, "sect283r1 (B-283)"},
382         {11, "sect409k1 (K-409)"},
383         {12, "sect409r1 (B-409)"},
384         {13, "sect571k1 (K-571)"},
385         {14, "sect571r1 (B-571)"},
386         {15, "secp160k1"},
387         {16, "secp160r1"},
388         {17, "secp160r2"},
389         {18, "secp192k1"},
390         {19, "secp192r1 (P-192)"},
391         {20, "secp224k1"},
392         {21, "secp224r1 (P-224)"},
393         {22, "secp256k1"},
394         {23, "secp256r1 (P-256)"},
395         {24, "secp384r1 (P-384)"},
396         {25, "secp521r1 (P-521)"},
397         {0xFF01, "arbitrary_explicit_prime_curves"},
398         {0xFF02, "arbitrary_explicit_char2_curves"}
399 };
400
401 static ssl_trace_tbl ssl_point_tbl[] = {
402         {0, "uncompressed"},
403         {1, "ansiX962_compressed_prime"},
404         {2, "ansiX962_compressed_char2"}
405 };
406
407 static ssl_trace_tbl ssl_md_tbl[] = {
408         {0, "none"},
409         {1, "md5"},
410         {2, "sha1"},
411         {3, "sha224"},
412         {4, "sha256"},
413         {5, "sha384"},
414         {6, "sha512"}
415 };
416
417 static ssl_trace_tbl ssl_sig_tbl[] = {
418         {0, "anonymous"},
419         {1, "rsa"},
420         {2, "dsa"},
421         {3, "ecdsa"}
422 };
423
424 static ssl_trace_tbl ssl_hb_tbl[] = {
425         {1, "peer_allowed_to_send"},
426         {2, "peer_not_allowed_to_send"}
427 };
428
429 static ssl_trace_tbl ssl_hb_type_tbl[] = {
430         {1, "heartbeat_request"},
431         {2, "heartbeat_response"}
432 };
433
434 static ssl_trace_tbl ssl_ctype_tbl[] = {
435         {1, "rsa_sign"},
436         {2, "dss_sign"},
437         {3, "rsa_fixed_dh"},
438         {4, "dss_fixed_dh"},
439         {5, "rsa_ephemeral_dh"},
440         {6, "dss_ephemeral_dh"},
441         {20, "fortezza_dms"},
442         {64, "ecdsa_sign"},
443         {65, "rsa_fixed_ecdh"},
444         {66, "ecdsa_fixed_ecdh"}
445 };
446
447 static void ssl_print_hex(BIO *bio, int indent, const char *name,
448                                 const unsigned char *msg, size_t msglen)
449         {
450         size_t i;
451         BIO_indent(bio, indent, 80);
452         BIO_printf(bio, "%s (len=%d): ", name, (int)msglen);
453         for (i = 0; i < msglen; i++)
454                 BIO_printf(bio, "%02X", msg[i]);
455         BIO_puts(bio, "\n");
456         }
457
458 static int ssl_print_hexbuf(BIO *bio, int indent,
459                                 const char *name, size_t nlen,
460                                 const unsigned char **pmsg, size_t *pmsglen)
461         {
462         size_t blen;
463         const unsigned char *p = *pmsg;
464         if (*pmsglen < nlen)
465                 return 0;
466         blen = p[0];
467         if (nlen > 1)
468                 blen = (blen << 8)|p[1];
469         if (*pmsglen < nlen + blen)
470                 return 0;
471         p += nlen;
472         ssl_print_hex(bio, indent, name, p, blen);
473         *pmsg += blen + nlen;
474         *pmsglen -= blen + nlen;
475         return 1;
476         }
477
478         
479
480 static int ssl_print_version(BIO *bio, int indent, const char *name,
481                                 const unsigned char **pmsg, size_t *pmsglen)
482         {
483         int vers;
484         if (*pmsglen < 2)
485                 return 0;
486         vers = ((*pmsg)[0] << 8) | (*pmsg)[1];
487         BIO_indent(bio, indent, 80);
488         BIO_printf(bio, "%s=0x%x (%s)\n",
489                                 name, vers,
490                                 ssl_trace_str(vers, ssl_version_tbl));
491         *pmsg += 2;
492         *pmsglen -= 2;
493         return 1;
494         }
495
496 static int ssl_print_random(BIO *bio, int indent,
497                                 const unsigned char **pmsg, size_t *pmsglen)
498         {
499         unsigned int tm;
500         const unsigned char *p = *pmsg;
501         if (*pmsglen < 32)
502                 return 0;
503         tm = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
504         p += 4;
505         BIO_indent(bio, indent, 80);
506         BIO_puts(bio, "Random:\n");
507         BIO_indent(bio, indent + 2, 80);
508         BIO_printf(bio, "gmt_unix_time=0x%08X\n", tm);
509         ssl_print_hex(bio, indent + 2, "random_bytes", p, 28);
510         *pmsg += 32;
511         *pmsglen -= 32;
512         return 1;
513         }
514
515 static int ssl_print_signature(BIO *bio, int indent, SSL *s,
516                                 const unsigned char **pmsg, size_t *pmsglen)
517         {
518         if (*pmsglen < 2)
519                 return 0;
520         if (TLS1_get_version(s) >= TLS1_2_VERSION)
521                 {
522                 const unsigned char *p = *pmsg;
523                 BIO_indent(bio, indent, 80);
524                 BIO_printf(bio, "Signature Algorithm %s+%s (%d+%d)\n",
525                                 ssl_trace_str(p[0], ssl_md_tbl),
526                                 ssl_trace_str(p[1], ssl_sig_tbl),
527                                 p[0], p[1]);
528                 *pmsg += 2;
529                 *pmsglen -= 2;
530                 }
531         return ssl_print_hexbuf(bio, indent, "Signature", 2, pmsg, pmsglen);
532         }
533
534 static int ssl_print_extension(BIO *bio, int indent, int server, int extype,
535                                 const unsigned char *ext, size_t extlen)
536         {
537         size_t xlen;
538         BIO_indent(bio, indent, 80);
539         BIO_printf(bio, "extension_type=%s(%d), length=%d\n",
540                                 ssl_trace_str(extype, ssl_exts_tbl),
541                                 extype, (int)extlen);
542         switch(extype)
543                 {
544         case TLSEXT_TYPE_ec_point_formats:
545                 if (extlen < 1)
546                         return 0;
547                 xlen = ext[0];
548                 if (extlen != xlen + 1)
549                         return 0;
550                 return ssl_trace_list(bio, indent + 2,
551                                         ext + 1, xlen, 1, ssl_point_tbl);
552
553         case TLSEXT_TYPE_elliptic_curves:
554                 if (extlen < 2)
555                         return 0;
556                 xlen = (ext[0] << 8) | ext[1];
557                 if (extlen != xlen + 2)
558                         return 0;
559                 return ssl_trace_list(bio, indent + 2,
560                                         ext + 2, xlen, 2, ssl_curve_tbl);
561
562         case TLSEXT_TYPE_signature_algorithms:
563
564                 if (extlen < 2)
565                         return 0;
566                 xlen = (ext[0] << 8) | ext[1];
567                 if (extlen != xlen + 2)
568                         return 0;
569                 if (xlen & 1)
570                         return 0;
571                 ext += 2;
572                 while(xlen > 0)
573                         {
574                         BIO_indent(bio, indent + 2, 80);
575                         BIO_printf(bio, "%s+%s (%d+%d)\n",
576                                 ssl_trace_str(ext[0], ssl_md_tbl),
577                                 ssl_trace_str(ext[1], ssl_sig_tbl),
578                                 ext[0], ext[1]);
579                         xlen-= 2;
580                         ext+= 2;
581                         }
582                 break;
583         
584         case TLSEXT_TYPE_renegotiate:
585                 if (extlen < 1)
586                         return 0;
587                 xlen = ext[0];
588                 if (xlen + 1 != extlen)
589                         return 0;
590                 ext++;
591                 if (xlen)
592                         {
593                         if (server)
594                                 {
595                                 if (xlen & 1)
596                                         return 0;
597                                 xlen >>= 1;
598                                 }
599                         ssl_print_hex(bio, indent + 4, "client_verify_data",
600                                                                 ext, xlen);
601                         if (server)
602                                 {
603                                 ext += xlen;
604                                 ssl_print_hex(bio, indent + 4,
605                                                         "server_verify_data",
606                                                         ext, xlen);
607                                 }
608                         }
609                 else
610                         {
611                         BIO_indent(bio, indent + 4, 80);
612                         BIO_puts(bio, "<EMPTY>\n");
613                         }
614                 break;
615
616         case TLSEXT_TYPE_heartbeat:
617                 if (extlen != 1)
618                         return 0;
619                 BIO_indent(bio, indent + 2, 80);
620                 BIO_printf(bio, "HeartbeatMode: %s\n",
621                                 ssl_trace_str(ext[0], ssl_hb_tbl));
622                 break;
623
624                 case TLSEXT_TYPE_session_ticket:
625                 if (extlen != 0)
626                         ssl_print_hex(bio, indent + 4, "ticket", ext, extlen);
627                 break;
628
629         default:
630                 BIO_dump_indent(bio, (char *)ext, extlen, indent + 2);
631                 }
632         return 1;
633         }
634
635 static int ssl_print_extensions(BIO *bio, int indent, int server,
636                                 const unsigned char *msg, size_t msglen)
637         {
638         size_t extslen;
639         BIO_indent(bio, indent, 80);
640         if (msglen == 0)
641                 {
642                 BIO_puts(bio, "No Extensions\n");
643                 return 1;
644                 }
645         extslen = (msg[0] << 8) | msg[1];
646         if (extslen != msglen - 2)
647                 return 0;
648         msg += 2;
649         msglen = extslen;
650         BIO_printf(bio, "extensions, length = %d\n", (int)msglen);
651         while (msglen > 0)
652                 {
653                 int extype;
654                 size_t extlen;
655                 if (msglen < 4)
656                         return 0;
657                 extype = (msg[0] << 8) | msg[1];
658                 extlen = (msg[2] << 8) | msg[3];
659                 if (msglen < extlen + 4)
660                         return 0;
661                 msg += 4;
662                 if (!ssl_print_extension(bio, indent + 2, server,
663                                                         extype, msg, extlen))
664                         return 0;
665                 msg += extlen;
666                 msglen -= extlen + 4;
667                 }
668         return 1;
669         }
670
671 static int ssl_print_client_hello(BIO *bio, int indent,
672                                 const unsigned char *msg, size_t msglen)
673         {
674         size_t len;
675         unsigned int cs;
676         if (!ssl_print_version(bio, indent, "client_version", &msg, &msglen))
677                 return 0;
678         if (!ssl_print_random(bio, indent, &msg, &msglen))
679                 return 0;
680         if (!ssl_print_hexbuf(bio, indent, "session_id", 1, &msg, &msglen))
681                 return 0;
682         if (msglen < 2)
683                 return 0;
684         len = (msg[0] << 8) | msg[1];
685         msg += 2;
686         msglen -= 2;
687         BIO_indent(bio, indent, 80);
688         BIO_printf(bio, "cipher_suites (len=%d)\n", (int)len);
689         if (msglen < len || len & 1)
690                 return 0;
691         while(len > 0)
692                 {
693                 cs = (msg[0] << 8) | msg[1];
694                 BIO_indent(bio, indent + 2, 80);
695                 BIO_printf(bio, "{0x%02X, 0x%02X} %s\n",
696                                 msg[0], msg[1],
697                                 ssl_trace_str(cs, ssl_ciphers_tbl));
698                 msg += 2;
699                 msglen -= 2;
700                 len -= 2;
701                 }
702         if (msglen < 1)
703                 return 0;
704         len = msg[0];
705         msg++;
706         msglen--;
707         if (msglen < len)
708                 return 0;
709         BIO_indent(bio, indent, 80);
710         BIO_printf(bio, "compression_methods (len=%d)\n", (int)len);
711         while(len > 0)
712                 {
713                 BIO_indent(bio, indent + 2, 80);
714                 BIO_printf(bio, "%s (0x%02X)\n",
715                                 ssl_trace_str(msg[0], ssl_comp_tbl),
716                                 msg[0]);
717                 msg++;
718                 msglen--;
719                 len--;
720                 }
721         if (!ssl_print_extensions(bio, indent, 0, msg, msglen))
722                 return 0;
723         return 1;
724         }
725
726 static int ssl_print_server_hello(BIO *bio, int indent,
727                                 const unsigned char *msg, size_t msglen)
728         {
729         unsigned int cs;
730         if (!ssl_print_version(bio, indent, "server_version", &msg, &msglen))
731                 return 0;
732         if (!ssl_print_random(bio, indent, &msg, &msglen))
733                 return 0;
734         if (!ssl_print_hexbuf(bio, indent, "session_id", 1, &msg, &msglen))
735                 return 0;
736         if (msglen < 2)
737                 return 0;
738         cs = (msg[0] << 8) | msg[1];
739         BIO_indent(bio, indent, 80);
740         BIO_printf(bio, "cipher_suite {0x%02X, 0x%02X} %s\n",
741                                 msg[0], msg[1],
742                                 ssl_trace_str(cs, ssl_ciphers_tbl));
743         msg += 2;
744         msglen -= 2;
745         if (msglen < 1)
746                 return 0;
747         BIO_indent(bio, indent, 80);
748         BIO_printf(bio, "compression_method: %s (0x%02X)\n",
749                         ssl_trace_str(msg[0], ssl_comp_tbl), msg[0]);
750         msg++;
751         msglen--;
752         if (!ssl_print_extensions(bio, indent, 1, msg, msglen))
753                 return 0;
754         return 1;
755         }
756
757 static int ssl_get_keyex(const char **pname, SSL *ssl)
758         {
759         unsigned long alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
760         if (alg_k & SSL_kRSA)
761                 {
762                 *pname = "rsa";
763                 return SSL_kRSA;
764                 }
765         if (alg_k & SSL_kDHr)
766                 {
767                 *pname = "dh_rsa";
768                 return SSL_kDHr;
769                 }
770         if (alg_k & SSL_kDHd)
771                 {
772                 *pname = "dh_dss";
773                 return SSL_kDHd;
774                 }
775         if (alg_k & SSL_kKRB5)
776                 {
777                 *pname = "krb5";
778                 return SSL_kKRB5;
779                 }
780         if (alg_k & SSL_kEDH)
781                 {
782                 *pname = "edh";
783                 return SSL_kEDH;
784                 }
785         if (alg_k & SSL_kEECDH)
786                 {
787                 *pname = "EECDH";
788                 return SSL_kEECDH;
789                 }
790         if (alg_k & SSL_kECDHr)
791                 {
792                 *pname = "ECDH RSA";
793                 return SSL_kECDHr;
794                 }
795         if (alg_k & SSL_kECDHe)
796                 {
797                 *pname = "ECDH ECDSA";
798                 return SSL_kECDHe;
799                 }
800         if (alg_k & SSL_kPSK)
801                 {
802                 *pname = "PSK";
803                 return SSL_kPSK;
804                 }
805         if (alg_k & SSL_kSRP)
806                 {
807                 *pname = "SRP";
808                 return SSL_kSRP;
809                 }
810         if (alg_k & SSL_kGOST)
811                 {
812                 *pname = "GOST";
813                 return SSL_kGOST;
814                 }
815         *pname = "UNKNOWN";
816         return 0;
817         }
818
819 static int ssl_print_client_keyex(BIO *bio, int indent, SSL *ssl,
820                                 const unsigned char *msg, size_t msglen)
821         {
822         const char *algname;
823         int id;
824         id = ssl_get_keyex(&algname, ssl);
825         BIO_indent(bio, indent, 80);
826         BIO_printf(bio, "KeyExchangeAlgorithm=%s\n", algname);
827         switch(id)
828                 {
829
830         case SSL_kRSA:
831                 if (!ssl_print_hexbuf(bio, indent + 2,
832                                                 "EncyptedPreMasterSecret", 2,
833                                                 &msg, &msglen))
834                                 return 0;
835                 break;
836
837                 /* Implicit parameters only allowed for static DH */
838         case SSL_kDHd:
839         case SSL_kDHr:
840                 if (msglen == 0)
841                         {
842                         BIO_indent(bio, indent + 2, 80);
843                         BIO_puts(bio, "implicit\n");
844                         break;
845                         }
846         case SSL_kEDH:
847                 if (!ssl_print_hexbuf(bio, indent + 2, "dh_Yc", 2,
848                                                                 &msg, &msglen))
849                         return 0;
850                 break;
851
852         case SSL_kECDHr:
853         case SSL_kECDHe:
854                 if (msglen == 0)
855                         {
856                         BIO_indent(bio, indent + 2, 80);
857                         BIO_puts(bio, "implicit\n");
858                         break;
859                         }
860         case SSL_kEECDH:
861                 if (!ssl_print_hexbuf(bio, indent + 2, "ecdh_Yc", 1,
862                                                         &msg, &msglen))
863                         return 0;
864                 break;
865                 }
866
867         return 1;
868         }
869
870 static int ssl_print_server_keyex(BIO *bio, int indent, SSL *ssl,
871                                 const unsigned char *msg, size_t msglen)
872         {
873         const char *algname;
874         int id;
875         id = ssl_get_keyex(&algname, ssl);
876         BIO_indent(bio, indent, 80);
877         BIO_printf(bio, "KeyExchangeAlgorithm=%s\n", algname);
878         switch(id)
879                 {
880                 /* Should never happen */
881         case SSL_kDHd:
882         case SSL_kDHr:
883         case SSL_kECDHr:
884         case SSL_kECDHe:
885                 BIO_indent(bio, indent + 2, 80);
886                 BIO_printf(bio, "Unexpected Message\n");
887                 break;
888
889         case SSL_kRSA:
890
891                 if (!ssl_print_hexbuf(bio, indent + 2, "rsa_modulus", 2,
892                                                 &msg, &msglen))
893                         return 0;
894                 if (!ssl_print_hexbuf(bio, indent + 2, "rsa_exponent", 2,
895                                                 &msg, &msglen))
896                         return 0;
897                 break;
898
899         case SSL_kEDH:
900                 if (!ssl_print_hexbuf(bio, indent + 2, "dh_p", 2,
901                                                 &msg, &msglen))
902                         return 0;
903                 if (!ssl_print_hexbuf(bio, indent + 2, "dh_g", 2,
904                                                 &msg, &msglen))
905                         return 0;
906                 if (!ssl_print_hexbuf(bio, indent + 2, "dh_Ys", 2,
907                                                 &msg, &msglen))
908                         return 0;
909                 break;
910
911         case SSL_kEECDH:
912                 if (msglen < 1)
913                         return 0;
914                 BIO_indent(bio, indent + 2, 80);
915                 if (msg[0] == EXPLICIT_PRIME_CURVE_TYPE)
916                         BIO_puts(bio, "explicit_prime\n");
917                 else if (msg[0] == EXPLICIT_CHAR2_CURVE_TYPE)
918                         BIO_puts(bio, "explicit_char2\n");
919                 else if (msg[0] == NAMED_CURVE_TYPE)
920                         {
921                         int curve;
922                         if (msglen < 3)
923                                 return 0;
924                         curve = (msg[1] << 8) | msg[2];
925                         BIO_printf(bio, "named_curve: %s (%d)\n",
926                                         ssl_trace_str(curve, ssl_curve_tbl),
927                                         curve);
928                         msg += 3;
929                         msglen -= 3;
930                         if (!ssl_print_hexbuf(bio, indent + 2, "point", 1,
931                                                         &msg, &msglen))
932                                 return 0;
933                         }
934                 break;
935                 }
936         return ssl_print_signature(bio, indent, ssl, &msg, &msglen);
937         }
938
939 static int ssl_print_certificate(BIO *bio, int indent, 
940                                 const unsigned char **pmsg, size_t *pmsglen)
941         {
942         size_t msglen = *pmsglen;
943         size_t clen;
944         X509 *x;
945         const unsigned char *p = *pmsg, *q;
946         if (msglen < 3)
947                 return 0;
948         clen = (p[0] << 16) | (p[1] << 8) | p[2];
949         if (msglen < clen + 3)
950                 return 0;
951         q = p + 3;
952         BIO_indent(bio, indent, 80);
953         BIO_printf(bio, "ASN.1Cert, length=%d", (int)clen);
954         x = d2i_X509(NULL, &q, clen);
955         if (!x)
956                 BIO_puts(bio, "<UNPARSEABLE CERTIFICATE>\n");
957         else
958                 {
959                 BIO_puts(bio, "\n------details-----\n");
960                 X509_print_ex(bio, x, XN_FLAG_ONELINE, 0);
961                 PEM_write_bio_X509(bio, x);
962                 /* Print certificate stuff */
963                 BIO_puts(bio, "------------------\n");
964                 X509_free(x);
965                 }
966         if (q != p + 3 + clen)
967                 {
968                 BIO_puts(bio, "<TRAILING GARBAGE AFTER CERTIFICATE>\n");
969                 }
970         *pmsg += clen + 3;
971         *pmsglen -= clen + 3;
972         return 1;
973         }
974
975 static int ssl_print_certificates(BIO *bio, int indent, 
976                                 const unsigned char *msg, size_t msglen)
977         {
978         size_t clen;
979         if (msglen < 3)
980                 return 0;
981         clen = (msg[0] << 16) | (msg[1] << 8) | msg[2];
982         if (msglen != clen + 3)
983                 return 0;
984         msg += 3;
985         BIO_indent(bio, indent, 80);
986         BIO_printf(bio, "certificate_list, length=%d\n", (int)clen);
987         while (clen > 0)
988                 {
989                 if (!ssl_print_certificate(bio, indent + 2, &msg, &clen))
990                         return 0;
991                 }
992         return 1;
993         }
994
995 static int ssl_print_cert_request(BIO *bio, int indent, SSL *s,
996                                 const unsigned char *msg, size_t msglen)
997         {
998         size_t xlen;
999         if (msglen < 1)
1000                 return 0;
1001         xlen = msg[0];
1002         if (msglen < xlen + 1)
1003                 return 0;
1004         msg++;
1005         BIO_indent(bio, indent, 80);
1006         BIO_printf(bio, "certificate_types (len=%d)\n", (int)xlen);
1007         if (!ssl_trace_list(bio, indent + 2, msg, xlen, 1, ssl_ctype_tbl))
1008                 return 0;
1009         msg += xlen;
1010         msglen -= xlen + 1;
1011         if (TLS1_get_version(s) < TLS1_2_VERSION)
1012                 goto skip_sig;
1013         if (msglen < 2)
1014                 return 0;
1015         xlen = (msg[0] << 8) | msg[1];
1016         if (msglen < xlen + 2 || (xlen & 1))
1017                 return 0;
1018         msg += 2;
1019         BIO_indent(bio, indent, 80);
1020         BIO_printf(bio, "signature_algorithms (len=%d)\n", (int)xlen);
1021         while(xlen > 0)
1022                 {
1023                 BIO_indent(bio, indent + 2, 80);
1024                 BIO_printf(bio, "%s+%s (%d+%d)\n",
1025                                 ssl_trace_str(msg[0], ssl_md_tbl),
1026                                 ssl_trace_str(msg[1], ssl_sig_tbl),
1027                                 msg[0], msg[1]);
1028                 xlen -= 2;
1029                 msg += 2;
1030                 }
1031         msg += xlen;
1032         msglen -= xlen + 2;
1033
1034         skip_sig:
1035         xlen = (msg[0] << 8) | msg[1];
1036         BIO_indent(bio, indent, 80);
1037         if (msglen < xlen + 2)
1038                 return 0;
1039         msg += 2;
1040         msglen -= 2;
1041         BIO_printf(bio, "certificate_authorities (len=%d)\n", (int)xlen);
1042         while (xlen > 0)
1043                 {
1044                 size_t dlen;
1045                 X509_NAME *nm;
1046                 const unsigned char *p;
1047                 if (xlen < 2)
1048                         return 0;
1049                 dlen = (msg[0] << 8) | msg[1];
1050                 if (xlen < dlen + 2)
1051                         return 0;
1052                 msg += 2;
1053                 BIO_indent(bio, indent + 2, 80);
1054                 BIO_printf(bio, "DistinguishedName (len=%d): ", (int)dlen);
1055                 p = msg;
1056                 nm = d2i_X509_NAME(NULL, &p, dlen);
1057                 if (!nm)
1058                         {
1059                         BIO_puts(bio, "<UNPARESABLE DN>\n");
1060                         }
1061                 else
1062                         {
1063                         X509_NAME_print_ex(bio, nm, 0, XN_FLAG_ONELINE);
1064                         BIO_puts(bio, "\n");
1065                         X509_NAME_free(nm);
1066                         }
1067                 xlen -= dlen + 2;
1068                 msg += dlen;
1069                 }
1070         return 1;
1071         }
1072
1073 static int ssl_print_ticket(BIO *bio, int indent,
1074                                 const unsigned char *msg, size_t msglen)
1075         {
1076         unsigned int tick_life;
1077         if (msglen == 0)
1078                 {
1079                 BIO_indent(bio, indent + 2, 80);
1080                 BIO_puts(bio, "No Ticket\n");
1081                 return 1;
1082                 }
1083         if (msglen < 4)
1084                 return 0;
1085         tick_life = (msg[0] << 24) | (msg[1] << 16) | (msg[2] << 8) | msg[3];
1086         msglen -= 4;
1087         msg += 4;       
1088         BIO_indent(bio, indent + 2, 80);
1089         BIO_printf(bio, "ticket_lifetime_hint=%u\n", tick_life);
1090         if (!ssl_print_hexbuf(bio, indent + 2, "ticket", 2, &msg, &msglen))
1091                 return 0;
1092         if (msglen)
1093                 return 0;
1094         return 1;
1095         }
1096
1097 static int ssl_print_handshake(BIO *bio, SSL *ssl,
1098                                 const unsigned char *msg, size_t msglen,
1099                                 int indent)
1100         {
1101         size_t hlen;
1102         unsigned char htype;
1103         if (msglen < 4)
1104                 return 0;
1105         htype = msg[0];
1106         hlen = (msg[1] << 16) | (msg[2] << 8) | msg[3];
1107         BIO_indent(bio, indent, 80);
1108         BIO_printf(bio, "%s, Length=%d\n",
1109                                 ssl_trace_str(htype, ssl_handshake_tbl),
1110                                 (int)hlen);
1111         msg += 4;
1112         msglen -= 4;
1113         if (msglen < hlen)
1114                 return 0;
1115         switch(htype)
1116                 {
1117         case SSL3_MT_CLIENT_HELLO:
1118                 if (!ssl_print_client_hello(bio, indent + 2, msg, msglen))
1119                         return 0;
1120                 break;
1121
1122         case SSL3_MT_SERVER_HELLO:
1123                 if (!ssl_print_server_hello(bio, indent + 2, msg, msglen))
1124                         return 0;
1125                 break;
1126
1127         case SSL3_MT_SERVER_KEY_EXCHANGE:
1128                 if (!ssl_print_server_keyex(bio, indent + 2, ssl, msg, msglen))
1129                         return 0;
1130                 break;
1131
1132         case SSL3_MT_CLIENT_KEY_EXCHANGE:
1133                 if (!ssl_print_client_keyex(bio, indent + 2, ssl, msg, msglen))
1134                         return 0;
1135                 break;
1136
1137         case SSL3_MT_CERTIFICATE:
1138                 if (!ssl_print_certificates(bio, indent + 2,  msg, msglen))
1139                         return 0;
1140                 break;
1141
1142         case SSL3_MT_CERTIFICATE_VERIFY:
1143                 if (!ssl_print_signature(bio, indent + 2, ssl, &msg, &msglen))
1144                         return 0;
1145                 break;
1146
1147         case SSL3_MT_CERTIFICATE_REQUEST:
1148                 if (!ssl_print_cert_request(bio, indent + 2,  ssl, msg, msglen))
1149                         return 0;
1150                 break;
1151
1152         case SSL3_MT_FINISHED:
1153                 ssl_print_hex(bio, indent + 2, "verify_data", msg, msglen);
1154                 break;
1155
1156         case SSL3_MT_SERVER_DONE:
1157                 if (msglen != 0)
1158                         ssl_print_hex(bio, indent + 2, "unexpected value",
1159                                                                 msg, msglen);
1160                 break;
1161
1162         case SSL3_MT_NEWSESSION_TICKET:
1163                 if (!ssl_print_ticket(bio, indent + 2,  msg, msglen))
1164                         return 0;
1165                 break;
1166                 
1167         default:
1168                 BIO_indent(bio, indent + 2, 80);
1169                 BIO_puts(bio, "Unsupported, hex dump follows:\n");
1170                 BIO_dump_indent(bio, (char *)msg, msglen, indent + 4);
1171                 }
1172         return 1;
1173         }
1174
1175 static int ssl_print_heartbeat(BIO *bio, int indent,
1176                                 const unsigned char *msg, size_t msglen)
1177         {
1178         if (msglen < 3)
1179                 return 0;
1180         BIO_indent(bio, indent, 80);
1181         BIO_printf(bio, "HeartBeatMessageType: %s\n",
1182                                 ssl_trace_str(msg[0], ssl_hb_type_tbl));
1183         msg++;
1184         msglen--;
1185         if (!ssl_print_hexbuf(bio, indent, "payload", 2, &msg, &msglen))
1186                 return 0;
1187         ssl_print_hex(bio, indent, "padding", msg, msglen);
1188         return 1;
1189         }
1190
1191 void SSL_trace(int write_p, int version, int content_type,
1192                 const void *buf, size_t msglen, SSL *ssl, void *arg)
1193         {
1194         const unsigned char *msg = buf;
1195         BIO *bio = arg;
1196         
1197         BIO_printf(bio, "%s Record: Version = %s (0x%x)",
1198                                 write_p ? "Sent" : "Received",
1199                                 ssl_trace_str(version, ssl_version_tbl),
1200                                 version);
1201         BIO_printf(bio, " Length=%d\n", (int)msglen);
1202         BIO_printf(bio, "  Content Type = %s (%d)\n",
1203                                 ssl_trace_str(content_type, ssl_content_tbl),
1204                                 content_type);
1205
1206         switch (content_type)
1207                 {
1208         case SSL3_RT_HANDSHAKE:
1209                 if (!ssl_print_handshake(bio, ssl, msg, msglen, 4))
1210                         BIO_printf(bio, "Message length parse error!\n");
1211                 break;
1212
1213         case SSL3_RT_CHANGE_CIPHER_SPEC:
1214                 if (msglen == 1 && msg[0] == 1)
1215                         BIO_puts(bio, "    change_cipher_spec (1)\n");
1216                 else
1217                         ssl_print_hex(bio, 4, "unknown value", msg, msglen);
1218                 break;
1219
1220         case SSL3_RT_ALERT:
1221                 if (msglen != 2)
1222                         BIO_puts(bio, "    Illegal Alert Length\n");
1223                 else
1224                         {
1225                         BIO_printf(bio,"    Level=%s(%d), description=%s(%d)\n",
1226                                         SSL_alert_type_string_long(msg[0] << 8),
1227                                         msg[0],
1228                                         SSL_alert_desc_string_long(msg[1]),
1229                                         msg[1]);
1230                         }
1231         case TLS1_RT_HEARTBEAT:
1232                 ssl_print_heartbeat(bio, 4, msg, msglen);
1233                 break;
1234
1235                 }
1236
1237         BIO_puts(bio, "\n");
1238         }
1239
1240 #endif