Extend DTLS method macros.
[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 static const char *do_ssl_trace_str(int val, ssl_trace_tbl *tbl, size_t ntbl)
75         {
76         size_t i;
77         for (i = 0; i < ntbl; i++, tbl++)
78                 {
79                 if (tbl->num == val)
80                         return tbl->name;
81                 }
82         return "UNKNOWN";
83         }
84
85 static int do_ssl_trace_list(BIO *bio, int indent,
86                                 const unsigned char *msg, size_t msglen,
87                                 size_t vlen, ssl_trace_tbl *tbl, size_t ntbl)
88         {
89         int val;
90         if (msglen%vlen)
91                 return 0;
92         while(msglen)
93                 {
94                 val = msg[0];
95                 if (vlen == 2)
96                         val = (val << 8) | msg[1];
97                 BIO_indent(bio, indent, 80);
98                 BIO_printf(bio, "%s (%d)\n",
99                                         do_ssl_trace_str(val, tbl, ntbl), val);
100                 msg += vlen;
101                 msglen -= vlen;
102                 }
103         return 1;
104         }
105
106 /* Version number */
107
108 static ssl_trace_tbl ssl_version_tbl[] = {
109         {SSL2_VERSION,          "SSL 2.0"},
110         {SSL3_VERSION,          "SSL 3.0"},
111         {TLS1_VERSION,          "TLS 1.0"},
112         {TLS1_1_VERSION,        "TLS 1.1"},
113         {TLS1_2_VERSION,        "TLS 1.2"},
114         {DTLS1_VERSION,         "DTLS 1.0"},
115         {DTLS1_BAD_VER,         "DTLS 1.0 (bad)"}
116 };
117
118 static ssl_trace_tbl ssl_content_tbl[] = {
119         {SSL3_RT_CHANGE_CIPHER_SPEC,    "ChangeCipherSpec"},
120         {SSL3_RT_ALERT,                 "Alert"},
121         {SSL3_RT_HANDSHAKE,             "Handshake"},
122         {SSL3_RT_APPLICATION_DATA,      "ApplicationData"},
123         {TLS1_RT_HEARTBEAT,             "HeartBeat"}
124 };
125 /* Handshake types */
126 static ssl_trace_tbl ssl_handshake_tbl[] = {
127         {SSL3_MT_HELLO_REQUEST,         "HelloRequest"},
128         {SSL3_MT_CLIENT_HELLO,          "ClientHello"},
129         {SSL3_MT_SERVER_HELLO,          "ServerHello"},
130         {DTLS1_MT_HELLO_VERIFY_REQUEST, "HelloVerifyRequest"},
131         {SSL3_MT_NEWSESSION_TICKET,     "NewSessionTicket"},
132         {SSL3_MT_CERTIFICATE,           "Certificate"},
133         {SSL3_MT_SERVER_KEY_EXCHANGE,   "ServerKeyExchange"},
134         {SSL3_MT_CERTIFICATE_REQUEST,   "CertificateRequest"},
135         {SSL3_MT_CLIENT_KEY_EXCHANGE,   "ClientKeyExchange"},
136         {SSL3_MT_CERTIFICATE_STATUS,    "CertificateStatus"},
137         {SSL3_MT_SERVER_DONE,           "ServerHelloDone"},
138         {SSL3_MT_CERTIFICATE_VERIFY,    "CertificateVerify"},
139         {SSL3_MT_CLIENT_KEY_EXCHANGE,   "ClientKeyExchange"},
140         {SSL3_MT_FINISHED,              "Finished"},
141         {SSL3_MT_CERTIFICATE_STATUS,    "CertificateStatus"}
142 };
143 /* Cipher suites */
144 static ssl_trace_tbl ssl_ciphers_tbl[] = {
145         {0x0000, "SSL_NULL_WITH_NULL_NULL"},
146         {0x0001, "SSL_RSA_WITH_NULL_MD5"},
147         {0x0002, "SSL_RSA_WITH_NULL_SHA"},
148         {0x0003, "SSL_RSA_EXPORT_WITH_RC4_40_MD5"},
149         {0x0004, "SSL_RSA_WITH_RC4_128_MD5"},
150         {0x0005, "SSL_RSA_WITH_RC4_128_SHA"},
151         {0x0006, "SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5"},
152         {0x0007, "SSL_RSA_WITH_IDEA_CBC_SHA"},
153         {0x0008, "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA"},
154         {0x0009, "SSL_RSA_WITH_DES_CBC_SHA"},
155         {0x000A, "SSL_RSA_WITH_3DES_EDE_CBC_SHA"},
156         {0x000B, "SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA"},
157         {0x000C, "SSL_DH_DSS_WITH_DES_CBC_SHA"},
158         {0x000D, "SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA"},
159         {0x000E, "SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA"},
160         {0x000F, "SSL_DH_RSA_WITH_DES_CBC_SHA"},
161         {0x0010, "SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA"},
162         {0x0011, "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA"},
163         {0x0012, "SSL_DHE_DSS_WITH_DES_CBC_SHA"},
164         {0x0013, "SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA"},
165         {0x0014, "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA"},
166         {0x0015, "SSL_DHE_RSA_WITH_DES_CBC_SHA"},
167         {0x0016, "SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA"},
168         {0x0017, "SSL_DH_anon_EXPORT_WITH_RC4_40_MD5"},
169         {0x0018, "SSL_DH_anon_WITH_RC4_128_MD5"},
170         {0x0019, "SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA"},
171         {0x001A, "SSL_DH_anon_WITH_DES_CBC_SHA"},
172         {0x001B, "SSL_DH_anon_WITH_3DES_EDE_CBC_SHA"},
173         {0x001D, "SSL_FORTEZZA_KEA_WITH_FORTEZZA_CBC_SHA"},
174         {0x001E, "SSL_FORTEZZA_KEA_WITH_RC4_128_SHA"},
175         {0x001F, "TLS_KRB5_WITH_3DES_EDE_CBC_SHA"},
176         {0x0020, "TLS_KRB5_WITH_RC4_128_SHA"},
177         {0x0021, "TLS_KRB5_WITH_IDEA_CBC_SHA"},
178         {0x0022, "TLS_KRB5_WITH_DES_CBC_MD5"},
179         {0x0023, "TLS_KRB5_WITH_3DES_EDE_CBC_MD5"},
180         {0x0024, "TLS_KRB5_WITH_RC4_128_MD5"},
181         {0x0025, "TLS_KRB5_WITH_IDEA_CBC_MD5"},
182         {0x0026, "TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA"},
183         {0x0027, "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_SHA"},
184         {0x0028, "TLS_KRB5_EXPORT_WITH_RC4_40_SHA"},
185         {0x0029, "TLS_KRB5_EXPORT_WITH_DES_CBC_40_MD5"},
186         {0x002A, "TLS_KRB5_EXPORT_WITH_RC2_CBC_40_MD5"},
187         {0x002B, "TLS_KRB5_EXPORT_WITH_RC4_40_MD5"},
188         {0x002F, "TLS_RSA_WITH_AES_128_CBC_SHA"},
189         {0x0030, "TLS_DH_DSS_WITH_AES_128_CBC_SHA"},
190         {0x0031, "TLS_DH_RSA_WITH_AES_128_CBC_SHA"},
191         {0x0032, "TLS_DHE_DSS_WITH_AES_128_CBC_SHA"},
192         {0x0033, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA"},
193         {0x0034, "TLS_DH_anon_WITH_AES_128_CBC_SHA"},
194         {0x0035, "TLS_RSA_WITH_AES_256_CBC_SHA"},
195         {0x0036, "TLS_DH_DSS_WITH_AES_256_CBC_SHA"},
196         {0x0037, "TLS_DH_RSA_WITH_AES_256_CBC_SHA"},
197         {0x0038, "TLS_DHE_DSS_WITH_AES_256_CBC_SHA"},
198         {0x0039, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA"},
199         {0x003A, "TLS_DH_anon_WITH_AES_256_CBC_SHA"},
200         {0x003B, "TLS_RSA_WITH_NULL_SHA256"},
201         {0x003C, "TLS_RSA_WITH_AES_128_CBC_SHA256"},
202         {0x003D, "TLS_RSA_WITH_AES_256_CBC_SHA256"},
203         {0x003E, "TLS_DH_DSS_WITH_AES_128_CBC_SHA256"},
204         {0x003F, "TLS_DH_RSA_WITH_AES_128_CBC_SHA256"},
205         {0x0040, "TLS_DHE_DSS_WITH_AES_128_CBC_SHA256"},
206         {0x0041, "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA"},
207         {0x0042, "TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA"},
208         {0x0043, "TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA"},
209         {0x0044, "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA"},
210         {0x0045, "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA"},
211         {0x0046, "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA"},
212         {0x0067, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256"},
213         {0x0068, "TLS_DH_DSS_WITH_AES_256_CBC_SHA256"},
214         {0x0069, "TLS_DH_RSA_WITH_AES_256_CBC_SHA256"},
215         {0x006A, "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256"},
216         {0x006B, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256"},
217         {0x006C, "TLS_DH_anon_WITH_AES_128_CBC_SHA256"},
218         {0x006D, "TLS_DH_anon_WITH_AES_256_CBC_SHA256"},
219         {0x0084, "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA"},
220         {0x0085, "TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA"},
221         {0x0086, "TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA"},
222         {0x0087, "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA"},
223         {0x0088, "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA"},
224         {0x0089, "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA"},
225         {0x008A, "TLS_PSK_WITH_RC4_128_SHA"},
226         {0x008B, "TLS_PSK_WITH_3DES_EDE_CBC_SHA"},
227         {0x008C, "TLS_PSK_WITH_AES_128_CBC_SHA"},
228         {0x008D, "TLS_PSK_WITH_AES_256_CBC_SHA"},
229         {0x008E, "TLS_DHE_PSK_WITH_RC4_128_SHA"},
230         {0x008F, "TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA"},
231         {0x0090, "TLS_DHE_PSK_WITH_AES_128_CBC_SHA"},
232         {0x0091, "TLS_DHE_PSK_WITH_AES_256_CBC_SHA"},
233         {0x0092, "TLS_RSA_PSK_WITH_RC4_128_SHA"},
234         {0x0093, "TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA"},
235         {0x0094, "TLS_RSA_PSK_WITH_AES_128_CBC_SHA"},
236         {0x0095, "TLS_RSA_PSK_WITH_AES_256_CBC_SHA"},
237         {0x0096, "TLS_RSA_WITH_SEED_CBC_SHA"},
238         {0x0097, "TLS_DH_DSS_WITH_SEED_CBC_SHA"},
239         {0x0098, "TLS_DH_RSA_WITH_SEED_CBC_SHA"},
240         {0x0099, "TLS_DHE_DSS_WITH_SEED_CBC_SHA"},
241         {0x009A, "TLS_DHE_RSA_WITH_SEED_CBC_SHA"},
242         {0x009B, "TLS_DH_anon_WITH_SEED_CBC_SHA"},
243         {0x009C, "TLS_RSA_WITH_AES_128_GCM_SHA256"},
244         {0x009D, "TLS_RSA_WITH_AES_256_GCM_SHA384"},
245         {0x009E, "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256"},
246         {0x009F, "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384"},
247         {0x00A0, "TLS_DH_RSA_WITH_AES_128_GCM_SHA256"},
248         {0x00A1, "TLS_DH_RSA_WITH_AES_256_GCM_SHA384"},
249         {0x00A2, "TLS_DHE_DSS_WITH_AES_128_GCM_SHA256"},
250         {0x00A3, "TLS_DHE_DSS_WITH_AES_256_GCM_SHA384"},
251         {0x00A4, "TLS_DH_DSS_WITH_AES_128_GCM_SHA256"},
252         {0x00A5, "TLS_DH_DSS_WITH_AES_256_GCM_SHA384"},
253         {0x00A6, "TLS_DH_anon_WITH_AES_128_GCM_SHA256"},
254         {0x00A7, "TLS_DH_anon_WITH_AES_256_GCM_SHA384"},
255         {0x00A8, "TLS_PSK_WITH_AES_128_GCM_SHA256"},
256         {0x00A9, "TLS_PSK_WITH_AES_256_GCM_SHA384"},
257         {0x00AA, "TLS_DHE_PSK_WITH_AES_128_GCM_SHA256"},
258         {0x00AB, "TLS_DHE_PSK_WITH_AES_256_GCM_SHA384"},
259         {0x00AC, "TLS_RSA_PSK_WITH_AES_128_GCM_SHA256"},
260         {0x00AD, "TLS_RSA_PSK_WITH_AES_256_GCM_SHA384"},
261         {0x00AE, "TLS_PSK_WITH_AES_128_CBC_SHA256"},
262         {0x00AF, "TLS_PSK_WITH_AES_256_CBC_SHA384"},
263         {0x00B0, "TLS_PSK_WITH_NULL_SHA256"},
264         {0x00B1, "TLS_PSK_WITH_NULL_SHA384"},
265         {0x00B2, "TLS_DHE_PSK_WITH_AES_128_CBC_SHA256"},
266         {0x00B3, "TLS_DHE_PSK_WITH_AES_256_CBC_SHA384"},
267         {0x00B4, "TLS_DHE_PSK_WITH_NULL_SHA256"},
268         {0x00B5, "TLS_DHE_PSK_WITH_NULL_SHA384"},
269         {0x00B6, "TLS_RSA_PSK_WITH_AES_128_CBC_SHA256"},
270         {0x00B7, "TLS_RSA_PSK_WITH_AES_256_CBC_SHA384"},
271         {0x00B8, "TLS_RSA_PSK_WITH_NULL_SHA256"},
272         {0x00B9, "TLS_RSA_PSK_WITH_NULL_SHA384"},
273         {0x00BA, "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256"},
274         {0x00BB, "TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256"},
275         {0x00BC, "TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256"},
276         {0x00BD, "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256"},
277         {0x00BE, "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256"},
278         {0x00BF, "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA256"},
279         {0x00C0, "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256"},
280         {0x00C1, "TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256"},
281         {0x00C2, "TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256"},
282         {0x00C3, "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256"},
283         {0x00C4, "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256"},
284         {0x00C5, "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA256"},
285         {0x00FF, "TLS_EMPTY_RENEGOTIATION_INFO_SCSV"},
286         {0xC001, "TLS_ECDH_ECDSA_WITH_NULL_SHA"},
287         {0xC002, "TLS_ECDH_ECDSA_WITH_RC4_128_SHA"},
288         {0xC003, "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA"},
289         {0xC004, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA"},
290         {0xC005, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA"},
291         {0xC006, "TLS_ECDHE_ECDSA_WITH_NULL_SHA"},
292         {0xC007, "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA"},
293         {0xC008, "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA"},
294         {0xC009, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA"},
295         {0xC00A, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA"},
296         {0xC00B, "TLS_ECDH_RSA_WITH_NULL_SHA"},
297         {0xC00C, "TLS_ECDH_RSA_WITH_RC4_128_SHA"},
298         {0xC00D, "TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA"},
299         {0xC00E, "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA"},
300         {0xC00F, "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA"},
301         {0xC010, "TLS_ECDHE_RSA_WITH_NULL_SHA"},
302         {0xC011, "TLS_ECDHE_RSA_WITH_RC4_128_SHA"},
303         {0xC012, "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA"},
304         {0xC013, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA"},
305         {0xC014, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA"},
306         {0xC015, "TLS_ECDH_anon_WITH_NULL_SHA"},
307         {0xC016, "TLS_ECDH_anon_WITH_RC4_128_SHA"},
308         {0xC017, "TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA"},
309         {0xC018, "TLS_ECDH_anon_WITH_AES_128_CBC_SHA"},
310         {0xC019, "TLS_ECDH_anon_WITH_AES_256_CBC_SHA"},
311         {0xC01A, "TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA"},
312         {0xC01B, "TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA"},
313         {0xC01C, "TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA"},
314         {0xC01D, "TLS_SRP_SHA_WITH_AES_128_CBC_SHA"},
315         {0xC01E, "TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA"},
316         {0xC01F, "TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA"},
317         {0xC020, "TLS_SRP_SHA_WITH_AES_256_CBC_SHA"},
318         {0xC021, "TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA"},
319         {0xC022, "TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA"},
320         {0xC023, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256"},
321         {0xC024, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384"},
322         {0xC025, "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256"},
323         {0xC026, "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384"},
324         {0xC027, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256"},
325         {0xC028, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384"},
326         {0xC029, "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256"},
327         {0xC02A, "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384"},
328         {0xC02B, "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256"},
329         {0xC02C, "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384"},
330         {0xC02D, "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256"},
331         {0xC02E, "TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384"},
332         {0xC02F, "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256"},
333         {0xC030, "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"},
334         {0xC031, "TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256"},
335         {0xC032, "TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384"},
336         {0xFEFE, "SSL_RSA_FIPS_WITH_DES_CBC_SHA"},
337         {0xFEFF, "SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA"},
338 };
339 /* Compression methods */
340 static ssl_trace_tbl ssl_comp_tbl[] = {
341         {0x0000, "No Compression"},
342         {0x0001, "Zlib Compression"}
343 };
344 /* Extensions */
345 static ssl_trace_tbl ssl_exts_tbl[] = {
346         {TLSEXT_TYPE_server_name, "server_name"},
347         {TLSEXT_TYPE_max_fragment_length, "max_fragment_length"},
348         {TLSEXT_TYPE_client_certificate_url, "client_certificate_url"},
349         {TLSEXT_TYPE_trusted_ca_keys, "trusted_ca_keys"},
350         {TLSEXT_TYPE_truncated_hmac, "truncated_hmac"},
351         {TLSEXT_TYPE_status_request, "status_request"},
352         {TLSEXT_TYPE_user_mapping, "user_mapping"},
353         {TLSEXT_TYPE_client_authz, "client_authz"},
354         {TLSEXT_TYPE_server_authz, "server_authz"},
355         {TLSEXT_TYPE_cert_type, "cert_type"},
356         {TLSEXT_TYPE_elliptic_curves, "elliptic_curves"},
357         {TLSEXT_TYPE_ec_point_formats, "ec_point_formats"},
358         {TLSEXT_TYPE_srp, "srp"},
359         {TLSEXT_TYPE_signature_algorithms, "signature_algorithms"},
360         {TLSEXT_TYPE_use_srtp, "use_srtp"},
361         {TLSEXT_TYPE_heartbeat, "heartbeat"},
362         {TLSEXT_TYPE_session_ticket, "session_ticket"},
363 #ifdef TLSEXT_TYPE_opaque_prf_input
364         {TLSEXT_TYPE_opaque_prf_input, "opaque_prf_input"},
365 #endif
366         {TLSEXT_TYPE_renegotiate, "renegotiate"},
367         {TLSEXT_TYPE_next_proto_neg, "next_proto_neg"}
368 };
369
370 static ssl_trace_tbl ssl_curve_tbl[] = {
371         {1, "sect163k1 (K-163)"},
372         {2, "sect163r1"},
373         {3, "sect163r2 (B-163)"},
374         {4, "sect193r1"},
375         {5, "sect193r2"},
376         {6, "sect233k1 (K-233)"},
377         {7, "sect233r1 (B-233)"},
378         {8, "sect239k1"},
379         {9, "sect283k1 (K-283)"},
380         {10, "sect283r1 (B-283)"},
381         {11, "sect409k1 (K-409)"},
382         {12, "sect409r1 (B-409)"},
383         {13, "sect571k1 (K-571)"},
384         {14, "sect571r1 (B-571)"},
385         {15, "secp160k1"},
386         {16, "secp160r1"},
387         {17, "secp160r2"},
388         {18, "secp192k1"},
389         {19, "secp192r1 (P-192)"},
390         {20, "secp224k1"},
391         {21, "secp224r1 (P-224)"},
392         {22, "secp256k1"},
393         {23, "secp256r1 (P-256)"},
394         {24, "secp384r1 (P-384)"},
395         {25, "secp521r1 (P-521)"},
396         {0xFF01, "arbitrary_explicit_prime_curves"},
397         {0xFF02, "arbitrary_explicit_char2_curves"}
398 };
399
400 static ssl_trace_tbl ssl_point_tbl[] = {
401         {0, "uncompressed"},
402         {1, "ansiX962_compressed_prime"},
403         {2, "ansiX962_compressed_char2"}
404 };
405
406 static ssl_trace_tbl ssl_md_tbl[] = {
407         {0, "none"},
408         {1, "md5"},
409         {2, "sha1"},
410         {3, "sha224"},
411         {4, "sha256"},
412         {5, "sha384"},
413         {6, "sha512"}
414 };
415
416 static ssl_trace_tbl ssl_sig_tbl[] = {
417         {0, "anonymous"},
418         {1, "rsa"},
419         {2, "dsa"},
420         {3, "ecdsa"}
421 };
422
423 static ssl_trace_tbl ssl_hb_tbl[] = {
424         {1, "peer_allowed_to_send"},
425         {2, "peer_not_allowed_to_send"}
426 };
427
428 static ssl_trace_tbl ssl_hb_type_tbl[] = {
429         {1, "heartbeat_request"},
430         {2, "heartbeat_response"}
431 };
432
433 static ssl_trace_tbl ssl_ctype_tbl[] = {
434         {1, "rsa_sign"},
435         {2, "dss_sign"},
436         {3, "rsa_fixed_dh"},
437         {4, "dss_fixed_dh"},
438         {5, "rsa_ephemeral_dh"},
439         {6, "dss_ephemeral_dh"},
440         {20, "fortezza_dms"},
441         {64, "ecdsa_sign"},
442         {65, "rsa_fixed_ecdh"},
443         {66, "ecdsa_fixed_ecdh"}
444 };
445
446 static ssl_trace_tbl ssl_crypto_tbl[] = {
447         {TLS1_RT_CRYPTO_PREMASTER,      "Premaster Secret"},
448         {TLS1_RT_CRYPTO_CLIENT_RANDOM,  "Client Random"},
449         {TLS1_RT_CRYPTO_SERVER_RANDOM,  "Server Random"},
450         {TLS1_RT_CRYPTO_MASTER,         "Master Secret"},
451         {TLS1_RT_CRYPTO_MAC|TLS1_RT_CRYPTO_WRITE,       "Write Mac Secret"},
452         {TLS1_RT_CRYPTO_MAC|TLS1_RT_CRYPTO_READ,        "Read Mac Secret"},
453         {TLS1_RT_CRYPTO_KEY|TLS1_RT_CRYPTO_WRITE,       "Write Key"},
454         {TLS1_RT_CRYPTO_KEY|TLS1_RT_CRYPTO_READ,        "Read Key"},
455         {TLS1_RT_CRYPTO_IV|TLS1_RT_CRYPTO_WRITE,        "Write IV"},
456         {TLS1_RT_CRYPTO_IV|TLS1_RT_CRYPTO_READ,         "Read IV"},
457         {TLS1_RT_CRYPTO_FIXED_IV|TLS1_RT_CRYPTO_WRITE,  "Write IV (fixed part)"},
458         {TLS1_RT_CRYPTO_FIXED_IV|TLS1_RT_CRYPTO_READ,   "Read IV (fixed part)"}
459 };
460
461 static void ssl_print_hex(BIO *bio, int indent, const char *name,
462                                 const unsigned char *msg, size_t msglen)
463         {
464         size_t i;
465         BIO_indent(bio, indent, 80);
466         BIO_printf(bio, "%s (len=%d): ", name, (int)msglen);
467         for (i = 0; i < msglen; i++)
468                 BIO_printf(bio, "%02X", msg[i]);
469         BIO_puts(bio, "\n");
470         }
471
472 static int ssl_print_hexbuf(BIO *bio, int indent,
473                                 const char *name, size_t nlen,
474                                 const unsigned char **pmsg, size_t *pmsglen)
475         {
476         size_t blen;
477         const unsigned char *p = *pmsg;
478         if (*pmsglen < nlen)
479                 return 0;
480         blen = p[0];
481         if (nlen > 1)
482                 blen = (blen << 8)|p[1];
483         if (*pmsglen < nlen + blen)
484                 return 0;
485         p += nlen;
486         ssl_print_hex(bio, indent, name, p, blen);
487         *pmsg += blen + nlen;
488         *pmsglen -= blen + nlen;
489         return 1;
490         }
491
492         
493
494 static int ssl_print_version(BIO *bio, int indent, const char *name,
495                                 const unsigned char **pmsg, size_t *pmsglen)
496         {
497         int vers;
498         if (*pmsglen < 2)
499                 return 0;
500         vers = ((*pmsg)[0] << 8) | (*pmsg)[1];
501         BIO_indent(bio, indent, 80);
502         BIO_printf(bio, "%s=0x%x (%s)\n",
503                                 name, vers,
504                                 ssl_trace_str(vers, ssl_version_tbl));
505         *pmsg += 2;
506         *pmsglen -= 2;
507         return 1;
508         }
509
510 static int ssl_print_random(BIO *bio, int indent,
511                                 const unsigned char **pmsg, size_t *pmsglen)
512         {
513         unsigned int tm;
514         const unsigned char *p = *pmsg;
515         if (*pmsglen < 32)
516                 return 0;
517         tm = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
518         p += 4;
519         BIO_indent(bio, indent, 80);
520         BIO_puts(bio, "Random:\n");
521         BIO_indent(bio, indent + 2, 80);
522         BIO_printf(bio, "gmt_unix_time=0x%08X\n", tm);
523         ssl_print_hex(bio, indent + 2, "random_bytes", p, 28);
524         *pmsg += 32;
525         *pmsglen -= 32;
526         return 1;
527         }
528
529 static int ssl_print_signature(BIO *bio, int indent, SSL *s,
530                                 const unsigned char **pmsg, size_t *pmsglen)
531         {
532         if (*pmsglen < 2)
533                 return 0;
534         if (SSL_USE_SIGALGS(s))
535                 {
536                 const unsigned char *p = *pmsg;
537                 BIO_indent(bio, indent, 80);
538                 BIO_printf(bio, "Signature Algorithm %s+%s (%d+%d)\n",
539                                 ssl_trace_str(p[0], ssl_md_tbl),
540                                 ssl_trace_str(p[1], ssl_sig_tbl),
541                                 p[0], p[1]);
542                 *pmsg += 2;
543                 *pmsglen -= 2;
544                 }
545         return ssl_print_hexbuf(bio, indent, "Signature", 2, pmsg, pmsglen);
546         }
547
548 static int ssl_print_extension(BIO *bio, int indent, int server, int extype,
549                                 const unsigned char *ext, size_t extlen)
550         {
551         size_t xlen;
552         BIO_indent(bio, indent, 80);
553         BIO_printf(bio, "extension_type=%s(%d), length=%d\n",
554                                 ssl_trace_str(extype, ssl_exts_tbl),
555                                 extype, (int)extlen);
556         switch(extype)
557                 {
558         case TLSEXT_TYPE_ec_point_formats:
559                 if (extlen < 1)
560                         return 0;
561                 xlen = ext[0];
562                 if (extlen != xlen + 1)
563                         return 0;
564                 return ssl_trace_list(bio, indent + 2,
565                                         ext + 1, xlen, 1, ssl_point_tbl);
566
567         case TLSEXT_TYPE_elliptic_curves:
568                 if (extlen < 2)
569                         return 0;
570                 xlen = (ext[0] << 8) | ext[1];
571                 if (extlen != xlen + 2)
572                         return 0;
573                 return ssl_trace_list(bio, indent + 2,
574                                         ext + 2, xlen, 2, ssl_curve_tbl);
575
576         case TLSEXT_TYPE_signature_algorithms:
577
578                 if (extlen < 2)
579                         return 0;
580                 xlen = (ext[0] << 8) | ext[1];
581                 if (extlen != xlen + 2)
582                         return 0;
583                 if (xlen & 1)
584                         return 0;
585                 ext += 2;
586                 while(xlen > 0)
587                         {
588                         BIO_indent(bio, indent + 2, 80);
589                         BIO_printf(bio, "%s+%s (%d+%d)\n",
590                                 ssl_trace_str(ext[0], ssl_md_tbl),
591                                 ssl_trace_str(ext[1], ssl_sig_tbl),
592                                 ext[0], ext[1]);
593                         xlen-= 2;
594                         ext+= 2;
595                         }
596                 break;
597         
598         case TLSEXT_TYPE_renegotiate:
599                 if (extlen < 1)
600                         return 0;
601                 xlen = ext[0];
602                 if (xlen + 1 != extlen)
603                         return 0;
604                 ext++;
605                 if (xlen)
606                         {
607                         if (server)
608                                 {
609                                 if (xlen & 1)
610                                         return 0;
611                                 xlen >>= 1;
612                                 }
613                         ssl_print_hex(bio, indent + 4, "client_verify_data",
614                                                                 ext, xlen);
615                         if (server)
616                                 {
617                                 ext += xlen;
618                                 ssl_print_hex(bio, indent + 4,
619                                                         "server_verify_data",
620                                                         ext, xlen);
621                                 }
622                         }
623                 else
624                         {
625                         BIO_indent(bio, indent + 4, 80);
626                         BIO_puts(bio, "<EMPTY>\n");
627                         }
628                 break;
629
630         case TLSEXT_TYPE_heartbeat:
631                 if (extlen != 1)
632                         return 0;
633                 BIO_indent(bio, indent + 2, 80);
634                 BIO_printf(bio, "HeartbeatMode: %s\n",
635                                 ssl_trace_str(ext[0], ssl_hb_tbl));
636                 break;
637
638                 case TLSEXT_TYPE_session_ticket:
639                 if (extlen != 0)
640                         ssl_print_hex(bio, indent + 4, "ticket", ext, extlen);
641                 break;
642
643         default:
644                 BIO_dump_indent(bio, (char *)ext, extlen, indent + 2);
645                 }
646         return 1;
647         }
648
649 static int ssl_print_extensions(BIO *bio, int indent, int server,
650                                 const unsigned char *msg, size_t msglen)
651         {
652         size_t extslen;
653         BIO_indent(bio, indent, 80);
654         if (msglen == 0)
655                 {
656                 BIO_puts(bio, "No Extensions\n");
657                 return 1;
658                 }
659         extslen = (msg[0] << 8) | msg[1];
660         if (extslen != msglen - 2)
661                 return 0;
662         msg += 2;
663         msglen = extslen;
664         BIO_printf(bio, "extensions, length = %d\n", (int)msglen);
665         while (msglen > 0)
666                 {
667                 int extype;
668                 size_t extlen;
669                 if (msglen < 4)
670                         return 0;
671                 extype = (msg[0] << 8) | msg[1];
672                 extlen = (msg[2] << 8) | msg[3];
673                 if (msglen < extlen + 4)
674                         return 0;
675                 msg += 4;
676                 if (!ssl_print_extension(bio, indent + 2, server,
677                                                         extype, msg, extlen))
678                         return 0;
679                 msg += extlen;
680                 msglen -= extlen + 4;
681                 }
682         return 1;
683         }
684
685 static int ssl_print_client_hello(BIO *bio, SSL *ssl, int indent,
686                                 const unsigned char *msg, size_t msglen)
687         {
688         size_t len;
689         unsigned int cs;
690         if (!ssl_print_version(bio, indent, "client_version", &msg, &msglen))
691                 return 0;
692         if (!ssl_print_random(bio, indent, &msg, &msglen))
693                 return 0;
694         if (!ssl_print_hexbuf(bio, indent, "session_id", 1, &msg, &msglen))
695                 return 0;
696         if (SSL_IS_DTLS(ssl))
697                 {
698                 if (!ssl_print_hexbuf(bio, indent, "cookie", 1, &msg, &msglen))
699                         return 0;
700                 }
701         if (msglen < 2)
702                 return 0;
703         len = (msg[0] << 8) | msg[1];
704         msg += 2;
705         msglen -= 2;
706         BIO_indent(bio, indent, 80);
707         BIO_printf(bio, "cipher_suites (len=%d)\n", (int)len);
708         if (msglen < len || len & 1)
709                 return 0;
710         while(len > 0)
711                 {
712                 cs = (msg[0] << 8) | msg[1];
713                 BIO_indent(bio, indent + 2, 80);
714                 BIO_printf(bio, "{0x%02X, 0x%02X} %s\n",
715                                 msg[0], msg[1],
716                                 ssl_trace_str(cs, ssl_ciphers_tbl));
717                 msg += 2;
718                 msglen -= 2;
719                 len -= 2;
720                 }
721         if (msglen < 1)
722                 return 0;
723         len = msg[0];
724         msg++;
725         msglen--;
726         if (msglen < len)
727                 return 0;
728         BIO_indent(bio, indent, 80);
729         BIO_printf(bio, "compression_methods (len=%d)\n", (int)len);
730         while(len > 0)
731                 {
732                 BIO_indent(bio, indent + 2, 80);
733                 BIO_printf(bio, "%s (0x%02X)\n",
734                                 ssl_trace_str(msg[0], ssl_comp_tbl),
735                                 msg[0]);
736                 msg++;
737                 msglen--;
738                 len--;
739                 }
740         if (!ssl_print_extensions(bio, indent, 0, msg, msglen))
741                 return 0;
742         return 1;
743         }
744
745 static int dtls_print_hello_vfyrequest(BIO *bio, int indent,
746                                 const unsigned char *msg, size_t msglen)
747         {
748         if (!ssl_print_version(bio, indent, "server_version", &msg, &msglen))
749                 return 0;
750         if (!ssl_print_hexbuf(bio, indent, "cookie", 1, &msg, &msglen))
751                 return 0;
752         return 1;
753         }
754
755 static int ssl_print_server_hello(BIO *bio, int indent,
756                                 const unsigned char *msg, size_t msglen)
757         {
758         unsigned int cs;
759         if (!ssl_print_version(bio, indent, "server_version", &msg, &msglen))
760                 return 0;
761         if (!ssl_print_random(bio, indent, &msg, &msglen))
762                 return 0;
763         if (!ssl_print_hexbuf(bio, indent, "session_id", 1, &msg, &msglen))
764                 return 0;
765         if (msglen < 2)
766                 return 0;
767         cs = (msg[0] << 8) | msg[1];
768         BIO_indent(bio, indent, 80);
769         BIO_printf(bio, "cipher_suite {0x%02X, 0x%02X} %s\n",
770                                 msg[0], msg[1],
771                                 ssl_trace_str(cs, ssl_ciphers_tbl));
772         msg += 2;
773         msglen -= 2;
774         if (msglen < 1)
775                 return 0;
776         BIO_indent(bio, indent, 80);
777         BIO_printf(bio, "compression_method: %s (0x%02X)\n",
778                         ssl_trace_str(msg[0], ssl_comp_tbl), msg[0]);
779         msg++;
780         msglen--;
781         if (!ssl_print_extensions(bio, indent, 1, msg, msglen))
782                 return 0;
783         return 1;
784         }
785
786 static int ssl_get_keyex(const char **pname, SSL *ssl)
787         {
788         unsigned long alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
789         if (alg_k & SSL_kRSA)
790                 {
791                 *pname = "rsa";
792                 return SSL_kRSA;
793                 }
794         if (alg_k & SSL_kDHr)
795                 {
796                 *pname = "dh_rsa";
797                 return SSL_kDHr;
798                 }
799         if (alg_k & SSL_kDHd)
800                 {
801                 *pname = "dh_dss";
802                 return SSL_kDHd;
803                 }
804         if (alg_k & SSL_kKRB5)
805                 {
806                 *pname = "krb5";
807                 return SSL_kKRB5;
808                 }
809         if (alg_k & SSL_kEDH)
810                 {
811                 *pname = "edh";
812                 return SSL_kEDH;
813                 }
814         if (alg_k & SSL_kEECDH)
815                 {
816                 *pname = "EECDH";
817                 return SSL_kEECDH;
818                 }
819         if (alg_k & SSL_kECDHr)
820                 {
821                 *pname = "ECDH RSA";
822                 return SSL_kECDHr;
823                 }
824         if (alg_k & SSL_kECDHe)
825                 {
826                 *pname = "ECDH ECDSA";
827                 return SSL_kECDHe;
828                 }
829         if (alg_k & SSL_kPSK)
830                 {
831                 *pname = "PSK";
832                 return SSL_kPSK;
833                 }
834         if (alg_k & SSL_kSRP)
835                 {
836                 *pname = "SRP";
837                 return SSL_kSRP;
838                 }
839         if (alg_k & SSL_kGOST)
840                 {
841                 *pname = "GOST";
842                 return SSL_kGOST;
843                 }
844         *pname = "UNKNOWN";
845         return 0;
846         }
847
848 static int ssl_print_client_keyex(BIO *bio, int indent, SSL *ssl,
849                                 const unsigned char *msg, size_t msglen)
850         {
851         const char *algname;
852         int id;
853         id = ssl_get_keyex(&algname, ssl);
854         BIO_indent(bio, indent, 80);
855         BIO_printf(bio, "KeyExchangeAlgorithm=%s\n", algname);
856         switch(id)
857                 {
858
859         case SSL_kRSA:
860                 if (TLS1_get_version(ssl) == SSL3_VERSION)
861                         {
862                         ssl_print_hex(bio, indent + 2,
863                                                 "EncyptedPreMasterSecret",
864                                                 msg, msglen);
865                         }
866                 else
867                         {
868                         if (!ssl_print_hexbuf(bio, indent + 2,
869                                                 "EncyptedPreMasterSecret", 2,
870                                                 &msg, &msglen))
871                                 return 0;
872                         }
873                 break;
874
875                 /* Implicit parameters only allowed for static DH */
876         case SSL_kDHd:
877         case SSL_kDHr:
878                 if (msglen == 0)
879                         {
880                         BIO_indent(bio, indent + 2, 80);
881                         BIO_puts(bio, "implicit\n");
882                         break;
883                         }
884         case SSL_kEDH:
885                 if (!ssl_print_hexbuf(bio, indent + 2, "dh_Yc", 2,
886                                                                 &msg, &msglen))
887                         return 0;
888                 break;
889
890         case SSL_kECDHr:
891         case SSL_kECDHe:
892                 if (msglen == 0)
893                         {
894                         BIO_indent(bio, indent + 2, 80);
895                         BIO_puts(bio, "implicit\n");
896                         break;
897                         }
898         case SSL_kEECDH:
899                 if (!ssl_print_hexbuf(bio, indent + 2, "ecdh_Yc", 1,
900                                                         &msg, &msglen))
901                         return 0;
902                 break;
903                 }
904
905         return 1;
906         }
907
908 static int ssl_print_server_keyex(BIO *bio, int indent, SSL *ssl,
909                                 const unsigned char *msg, size_t msglen)
910         {
911         const char *algname;
912         int id;
913         id = ssl_get_keyex(&algname, ssl);
914         BIO_indent(bio, indent, 80);
915         BIO_printf(bio, "KeyExchangeAlgorithm=%s\n", algname);
916         switch(id)
917                 {
918                 /* Should never happen */
919         case SSL_kDHd:
920         case SSL_kDHr:
921         case SSL_kECDHr:
922         case SSL_kECDHe:
923                 BIO_indent(bio, indent + 2, 80);
924                 BIO_printf(bio, "Unexpected Message\n");
925                 break;
926
927         case SSL_kRSA:
928
929                 if (!ssl_print_hexbuf(bio, indent + 2, "rsa_modulus", 2,
930                                                 &msg, &msglen))
931                         return 0;
932                 if (!ssl_print_hexbuf(bio, indent + 2, "rsa_exponent", 2,
933                                                 &msg, &msglen))
934                         return 0;
935                 break;
936
937         case SSL_kEDH:
938                 if (!ssl_print_hexbuf(bio, indent + 2, "dh_p", 2,
939                                                 &msg, &msglen))
940                         return 0;
941                 if (!ssl_print_hexbuf(bio, indent + 2, "dh_g", 2,
942                                                 &msg, &msglen))
943                         return 0;
944                 if (!ssl_print_hexbuf(bio, indent + 2, "dh_Ys", 2,
945                                                 &msg, &msglen))
946                         return 0;
947                 break;
948
949         case SSL_kEECDH:
950                 if (msglen < 1)
951                         return 0;
952                 BIO_indent(bio, indent + 2, 80);
953                 if (msg[0] == EXPLICIT_PRIME_CURVE_TYPE)
954                         BIO_puts(bio, "explicit_prime\n");
955                 else if (msg[0] == EXPLICIT_CHAR2_CURVE_TYPE)
956                         BIO_puts(bio, "explicit_char2\n");
957                 else if (msg[0] == NAMED_CURVE_TYPE)
958                         {
959                         int curve;
960                         if (msglen < 3)
961                                 return 0;
962                         curve = (msg[1] << 8) | msg[2];
963                         BIO_printf(bio, "named_curve: %s (%d)\n",
964                                         ssl_trace_str(curve, ssl_curve_tbl),
965                                         curve);
966                         msg += 3;
967                         msglen -= 3;
968                         if (!ssl_print_hexbuf(bio, indent + 2, "point", 1,
969                                                         &msg, &msglen))
970                                 return 0;
971                         }
972                 break;
973                 }
974         return ssl_print_signature(bio, indent, ssl, &msg, &msglen);
975         }
976
977 static int ssl_print_certificate(BIO *bio, int indent, 
978                                 const unsigned char **pmsg, size_t *pmsglen)
979         {
980         size_t msglen = *pmsglen;
981         size_t clen;
982         X509 *x;
983         const unsigned char *p = *pmsg, *q;
984         if (msglen < 3)
985                 return 0;
986         clen = (p[0] << 16) | (p[1] << 8) | p[2];
987         if (msglen < clen + 3)
988                 return 0;
989         q = p + 3;
990         BIO_indent(bio, indent, 80);
991         BIO_printf(bio, "ASN.1Cert, length=%d", (int)clen);
992         x = d2i_X509(NULL, &q, clen);
993         if (!x)
994                 BIO_puts(bio, "<UNPARSEABLE CERTIFICATE>\n");
995         else
996                 {
997                 BIO_puts(bio, "\n------details-----\n");
998                 X509_print_ex(bio, x, XN_FLAG_ONELINE, 0);
999                 PEM_write_bio_X509(bio, x);
1000                 /* Print certificate stuff */
1001                 BIO_puts(bio, "------------------\n");
1002                 X509_free(x);
1003                 }
1004         if (q != p + 3 + clen)
1005                 {
1006                 BIO_puts(bio, "<TRAILING GARBAGE AFTER CERTIFICATE>\n");
1007                 }
1008         *pmsg += clen + 3;
1009         *pmsglen -= clen + 3;
1010         return 1;
1011         }
1012
1013 static int ssl_print_certificates(BIO *bio, int indent, 
1014                                 const unsigned char *msg, size_t msglen)
1015         {
1016         size_t clen;
1017         if (msglen < 3)
1018                 return 0;
1019         clen = (msg[0] << 16) | (msg[1] << 8) | msg[2];
1020         if (msglen != clen + 3)
1021                 return 0;
1022         msg += 3;
1023         BIO_indent(bio, indent, 80);
1024         BIO_printf(bio, "certificate_list, length=%d\n", (int)clen);
1025         while (clen > 0)
1026                 {
1027                 if (!ssl_print_certificate(bio, indent + 2, &msg, &clen))
1028                         return 0;
1029                 }
1030         return 1;
1031         }
1032
1033 static int ssl_print_cert_request(BIO *bio, int indent, SSL *s,
1034                                 const unsigned char *msg, size_t msglen)
1035         {
1036         size_t xlen;
1037         if (msglen < 1)
1038                 return 0;
1039         xlen = msg[0];
1040         if (msglen < xlen + 1)
1041                 return 0;
1042         msg++;
1043         BIO_indent(bio, indent, 80);
1044         BIO_printf(bio, "certificate_types (len=%d)\n", (int)xlen);
1045         if (!ssl_trace_list(bio, indent + 2, msg, xlen, 1, ssl_ctype_tbl))
1046                 return 0;
1047         msg += xlen;
1048         msglen -= xlen + 1;
1049         if (!SSL_USE_SIGALGS(s))
1050                 goto skip_sig;
1051         if (msglen < 2)
1052                 return 0;
1053         xlen = (msg[0] << 8) | msg[1];
1054         if (msglen < xlen + 2 || (xlen & 1))
1055                 return 0;
1056         msg += 2;
1057         BIO_indent(bio, indent, 80);
1058         BIO_printf(bio, "signature_algorithms (len=%d)\n", (int)xlen);
1059         while(xlen > 0)
1060                 {
1061                 BIO_indent(bio, indent + 2, 80);
1062                 BIO_printf(bio, "%s+%s (%d+%d)\n",
1063                                 ssl_trace_str(msg[0], ssl_md_tbl),
1064                                 ssl_trace_str(msg[1], ssl_sig_tbl),
1065                                 msg[0], msg[1]);
1066                 xlen -= 2;
1067                 msg += 2;
1068                 }
1069         msg += xlen;
1070         msglen -= xlen + 2;
1071
1072         skip_sig:
1073         xlen = (msg[0] << 8) | msg[1];
1074         BIO_indent(bio, indent, 80);
1075         if (msglen < xlen + 2)
1076                 return 0;
1077         msg += 2;
1078         msglen -= 2;
1079         BIO_printf(bio, "certificate_authorities (len=%d)\n", (int)xlen);
1080         while (xlen > 0)
1081                 {
1082                 size_t dlen;
1083                 X509_NAME *nm;
1084                 const unsigned char *p;
1085                 if (xlen < 2)
1086                         return 0;
1087                 dlen = (msg[0] << 8) | msg[1];
1088                 if (xlen < dlen + 2)
1089                         return 0;
1090                 msg += 2;
1091                 BIO_indent(bio, indent + 2, 80);
1092                 BIO_printf(bio, "DistinguishedName (len=%d): ", (int)dlen);
1093                 p = msg;
1094                 nm = d2i_X509_NAME(NULL, &p, dlen);
1095                 if (!nm)
1096                         {
1097                         BIO_puts(bio, "<UNPARSEABLE DN>\n");
1098                         }
1099                 else
1100                         {
1101                         X509_NAME_print_ex(bio, nm, 0, XN_FLAG_ONELINE);
1102                         BIO_puts(bio, "\n");
1103                         X509_NAME_free(nm);
1104                         }
1105                 xlen -= dlen + 2;
1106                 msg += dlen;
1107                 }
1108         return 1;
1109         }
1110
1111 static int ssl_print_ticket(BIO *bio, int indent,
1112                                 const unsigned char *msg, size_t msglen)
1113         {
1114         unsigned int tick_life;
1115         if (msglen == 0)
1116                 {
1117                 BIO_indent(bio, indent + 2, 80);
1118                 BIO_puts(bio, "No Ticket\n");
1119                 return 1;
1120                 }
1121         if (msglen < 4)
1122                 return 0;
1123         tick_life = (msg[0] << 24) | (msg[1] << 16) | (msg[2] << 8) | msg[3];
1124         msglen -= 4;
1125         msg += 4;       
1126         BIO_indent(bio, indent + 2, 80);
1127         BIO_printf(bio, "ticket_lifetime_hint=%u\n", tick_life);
1128         if (!ssl_print_hexbuf(bio, indent + 2, "ticket", 2, &msg, &msglen))
1129                 return 0;
1130         if (msglen)
1131                 return 0;
1132         return 1;
1133         }
1134
1135
1136 static int ssl_print_handshake(BIO *bio, SSL *ssl,
1137                                 const unsigned char *msg, size_t msglen,
1138                                 int indent)
1139         {
1140         size_t hlen;
1141         unsigned char htype;
1142         if (msglen < 4)
1143                 return 0;
1144         htype = msg[0];
1145         hlen = (msg[1] << 16) | (msg[2] << 8) | msg[3];
1146         BIO_indent(bio, indent, 80);
1147         BIO_printf(bio, "%s, Length=%d\n",
1148                                 ssl_trace_str(htype, ssl_handshake_tbl),
1149                                 (int)hlen);
1150         msg += 4;
1151         msglen -= 4;
1152         if (SSL_IS_DTLS(ssl))
1153                 {
1154                 if (msglen < 8)
1155                         return 0;
1156                 BIO_indent(bio, indent, 80);
1157                 BIO_printf(bio, "message_seq=%d, fragment_offset=%d, "
1158                                 "fragment_length=%d\n",
1159                                 (msg[0] << 8) | msg[1],
1160                                 (msg[2] << 16) | (msg[3] << 8) | msg[4],
1161                                 (msg[5] << 16) | (msg[6] << 8) | msg[7]);
1162                 msg += 8;
1163                 msglen -= 8;
1164                 }
1165         if (msglen < hlen)
1166                 return 0;
1167         switch(htype)
1168                 {
1169         case SSL3_MT_CLIENT_HELLO:
1170                 if (!ssl_print_client_hello(bio, ssl, indent + 2, msg, msglen))
1171                         return 0;
1172                 break;
1173
1174         case DTLS1_MT_HELLO_VERIFY_REQUEST:
1175                 if (!dtls_print_hello_vfyrequest(bio, indent + 2, msg, msglen))
1176                         return 0;
1177                 break;
1178
1179         case SSL3_MT_SERVER_HELLO:
1180                 if (!ssl_print_server_hello(bio, indent + 2, msg, msglen))
1181                         return 0;
1182                 break;
1183
1184         case SSL3_MT_SERVER_KEY_EXCHANGE:
1185                 if (!ssl_print_server_keyex(bio, indent + 2, ssl, msg, msglen))
1186                         return 0;
1187                 break;
1188
1189         case SSL3_MT_CLIENT_KEY_EXCHANGE:
1190                 if (!ssl_print_client_keyex(bio, indent + 2, ssl, msg, msglen))
1191                         return 0;
1192                 break;
1193
1194         case SSL3_MT_CERTIFICATE:
1195                 if (!ssl_print_certificates(bio, indent + 2,  msg, msglen))
1196                         return 0;
1197                 break;
1198
1199         case SSL3_MT_CERTIFICATE_VERIFY:
1200                 if (!ssl_print_signature(bio, indent + 2, ssl, &msg, &msglen))
1201                         return 0;
1202                 break;
1203
1204         case SSL3_MT_CERTIFICATE_REQUEST:
1205                 if (!ssl_print_cert_request(bio, indent + 2,  ssl, msg, msglen))
1206                         return 0;
1207                 break;
1208
1209         case SSL3_MT_FINISHED:
1210                 ssl_print_hex(bio, indent + 2, "verify_data", msg, msglen);
1211                 break;
1212
1213         case SSL3_MT_SERVER_DONE:
1214                 if (msglen != 0)
1215                         ssl_print_hex(bio, indent + 2, "unexpected value",
1216                                                                 msg, msglen);
1217                 break;
1218
1219         case SSL3_MT_NEWSESSION_TICKET:
1220                 if (!ssl_print_ticket(bio, indent + 2,  msg, msglen))
1221                         return 0;
1222                 break;
1223                 
1224         default:
1225                 BIO_indent(bio, indent + 2, 80);
1226                 BIO_puts(bio, "Unsupported, hex dump follows:\n");
1227                 BIO_dump_indent(bio, (char *)msg, msglen, indent + 4);
1228                 }
1229         return 1;
1230         }
1231
1232 static int ssl_print_heartbeat(BIO *bio, int indent,
1233                                 const unsigned char *msg, size_t msglen)
1234         {
1235         if (msglen < 3)
1236                 return 0;
1237         BIO_indent(bio, indent, 80);
1238         BIO_printf(bio, "HeartBeatMessageType: %s\n",
1239                                 ssl_trace_str(msg[0], ssl_hb_type_tbl));
1240         msg++;
1241         msglen--;
1242         if (!ssl_print_hexbuf(bio, indent, "payload", 2, &msg, &msglen))
1243                 return 0;
1244         ssl_print_hex(bio, indent, "padding", msg, msglen);
1245         return 1;
1246         }
1247
1248 const char *SSL_CIPHER_standard_name(const SSL_CIPHER *c)
1249         {
1250         if (c->algorithm_ssl & SSL_SSLV2)
1251                 return NULL;
1252         return ssl_trace_str(c->id & 0xFFFF, ssl_ciphers_tbl);
1253         }
1254
1255 void SSL_trace(int write_p, int version, int content_type,
1256                 const void *buf, size_t msglen, SSL *ssl, void *arg)
1257         {
1258         const unsigned char *msg = buf;
1259         BIO *bio = arg;
1260
1261         if (write_p == 2)
1262                 {
1263                 BIO_puts(bio, "Session ");
1264                 ssl_print_hex(bio, 0,
1265                                 ssl_trace_str(content_type, ssl_crypto_tbl),
1266                                 msg, msglen);
1267                 return;
1268                 }
1269         switch (content_type)
1270                 {
1271         case SSL3_RT_HEADER:
1272                 {
1273                 int hvers = msg[1] << 8 | msg[2];
1274                 BIO_puts(bio, write_p ? "Sent" : "Received");
1275                 BIO_printf(bio, " Record\nHeader:\n  Version = %s (0x%x)\n",
1276                                 ssl_trace_str(hvers, ssl_version_tbl), hvers);
1277                 if (SSL_IS_DTLS(ssl))
1278                         {
1279                         BIO_printf(bio,
1280                                 "  epoch=%d, sequence_number=%04x%04x%04x\n",
1281                                         (msg[3] << 8 | msg[4]),
1282                                         (msg[5] << 8 | msg[6]),
1283                                         (msg[7] << 8 | msg[8]),
1284                                         (msg[9] << 8 | msg[10]));
1285 #if 0
1286                         /* Just print handshake type so we can see what is
1287                          * going on during fragmentation.
1288                          */
1289                         BIO_printf(bio, "(%s)\n",
1290                                 ssl_trace_str(msg[msglen], ssl_handshake_tbl));
1291 #endif
1292                         }
1293
1294                 BIO_printf(bio, "  Content Type = %s (%d)\n  Length = %d",
1295                                 ssl_trace_str(msg[0], ssl_content_tbl), msg[0],
1296                                 msg[msglen - 2] << 8 | msg[msglen - 1]);
1297                 }
1298                 break;
1299         case SSL3_RT_HANDSHAKE:
1300                 if (!ssl_print_handshake(bio, ssl, msg, msglen, 4))
1301                         BIO_printf(bio, "Message length parse error!\n");
1302                 break;
1303
1304         case SSL3_RT_CHANGE_CIPHER_SPEC:
1305                 if (msglen == 1 && msg[0] == 1)
1306                         BIO_puts(bio, "    change_cipher_spec (1)\n");
1307                 else
1308                         ssl_print_hex(bio, 4, "unknown value", msg, msglen);
1309                 break;
1310
1311         case SSL3_RT_ALERT:
1312                 if (msglen != 2)
1313                         BIO_puts(bio, "    Illegal Alert Length\n");
1314                 else
1315                         {
1316                         BIO_printf(bio,"    Level=%s(%d), description=%s(%d)\n",
1317                                         SSL_alert_type_string_long(msg[0] << 8),
1318                                         msg[0],
1319                                         SSL_alert_desc_string_long(msg[1]),
1320                                         msg[1]);
1321                         }
1322         case TLS1_RT_HEARTBEAT:
1323                 ssl_print_heartbeat(bio, 4, msg, msglen);
1324                 break;
1325
1326                 }
1327
1328         BIO_puts(bio, "\n");
1329         }
1330
1331 #endif