Add official extension value.
[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_2_VERSION,       "DTLS 1.2"},
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         {TLSEXT_TYPE_padding, "padding"}
370 };
371
372 static ssl_trace_tbl ssl_curve_tbl[] = {
373         {1, "sect163k1 (K-163)"},
374         {2, "sect163r1"},
375         {3, "sect163r2 (B-163)"},
376         {4, "sect193r1"},
377         {5, "sect193r2"},
378         {6, "sect233k1 (K-233)"},
379         {7, "sect233r1 (B-233)"},
380         {8, "sect239k1"},
381         {9, "sect283k1 (K-283)"},
382         {10, "sect283r1 (B-283)"},
383         {11, "sect409k1 (K-409)"},
384         {12, "sect409r1 (B-409)"},
385         {13, "sect571k1 (K-571)"},
386         {14, "sect571r1 (B-571)"},
387         {15, "secp160k1"},
388         {16, "secp160r1"},
389         {17, "secp160r2"},
390         {18, "secp192k1"},
391         {19, "secp192r1 (P-192)"},
392         {20, "secp224k1"},
393         {21, "secp224r1 (P-224)"},
394         {22, "secp256k1"},
395         {23, "secp256r1 (P-256)"},
396         {24, "secp384r1 (P-384)"},
397         {25, "secp521r1 (P-521)"},
398         {26, "brainpoolP256r1"},
399         {27, "brainpoolP384r1"},
400         {28, "brainpoolP512r1"},
401         {0xFF01, "arbitrary_explicit_prime_curves"},
402         {0xFF02, "arbitrary_explicit_char2_curves"}
403 };
404
405 static ssl_trace_tbl ssl_point_tbl[] = {
406         {0, "uncompressed"},
407         {1, "ansiX962_compressed_prime"},
408         {2, "ansiX962_compressed_char2"}
409 };
410
411 static ssl_trace_tbl ssl_md_tbl[] = {
412         {0, "none"},
413         {1, "md5"},
414         {2, "sha1"},
415         {3, "sha224"},
416         {4, "sha256"},
417         {5, "sha384"},
418         {6, "sha512"}
419 };
420
421 static ssl_trace_tbl ssl_sig_tbl[] = {
422         {0, "anonymous"},
423         {1, "rsa"},
424         {2, "dsa"},
425         {3, "ecdsa"}
426 };
427
428 static ssl_trace_tbl ssl_hb_tbl[] = {
429         {1, "peer_allowed_to_send"},
430         {2, "peer_not_allowed_to_send"}
431 };
432
433 static ssl_trace_tbl ssl_hb_type_tbl[] = {
434         {1, "heartbeat_request"},
435         {2, "heartbeat_response"}
436 };
437
438 static ssl_trace_tbl ssl_ctype_tbl[] = {
439         {1, "rsa_sign"},
440         {2, "dss_sign"},
441         {3, "rsa_fixed_dh"},
442         {4, "dss_fixed_dh"},
443         {5, "rsa_ephemeral_dh"},
444         {6, "dss_ephemeral_dh"},
445         {20, "fortezza_dms"},
446         {64, "ecdsa_sign"},
447         {65, "rsa_fixed_ecdh"},
448         {66, "ecdsa_fixed_ecdh"}
449 };
450
451 static ssl_trace_tbl ssl_crypto_tbl[] = {
452         {TLS1_RT_CRYPTO_PREMASTER,      "Premaster Secret"},
453         {TLS1_RT_CRYPTO_CLIENT_RANDOM,  "Client Random"},
454         {TLS1_RT_CRYPTO_SERVER_RANDOM,  "Server Random"},
455         {TLS1_RT_CRYPTO_MASTER,         "Master Secret"},
456         {TLS1_RT_CRYPTO_MAC|TLS1_RT_CRYPTO_WRITE,       "Write Mac Secret"},
457         {TLS1_RT_CRYPTO_MAC|TLS1_RT_CRYPTO_READ,        "Read Mac Secret"},
458         {TLS1_RT_CRYPTO_KEY|TLS1_RT_CRYPTO_WRITE,       "Write Key"},
459         {TLS1_RT_CRYPTO_KEY|TLS1_RT_CRYPTO_READ,        "Read Key"},
460         {TLS1_RT_CRYPTO_IV|TLS1_RT_CRYPTO_WRITE,        "Write IV"},
461         {TLS1_RT_CRYPTO_IV|TLS1_RT_CRYPTO_READ,         "Read IV"},
462         {TLS1_RT_CRYPTO_FIXED_IV|TLS1_RT_CRYPTO_WRITE,  "Write IV (fixed part)"},
463         {TLS1_RT_CRYPTO_FIXED_IV|TLS1_RT_CRYPTO_READ,   "Read IV (fixed part)"}
464 };
465
466 static void ssl_print_hex(BIO *bio, int indent, const char *name,
467                                 const unsigned char *msg, size_t msglen)
468         {
469         size_t i;
470         BIO_indent(bio, indent, 80);
471         BIO_printf(bio, "%s (len=%d): ", name, (int)msglen);
472         for (i = 0; i < msglen; i++)
473                 BIO_printf(bio, "%02X", msg[i]);
474         BIO_puts(bio, "\n");
475         }
476
477 static int ssl_print_hexbuf(BIO *bio, int indent,
478                                 const char *name, size_t nlen,
479                                 const unsigned char **pmsg, size_t *pmsglen)
480         {
481         size_t blen;
482         const unsigned char *p = *pmsg;
483         if (*pmsglen < nlen)
484                 return 0;
485         blen = p[0];
486         if (nlen > 1)
487                 blen = (blen << 8)|p[1];
488         if (*pmsglen < nlen + blen)
489                 return 0;
490         p += nlen;
491         ssl_print_hex(bio, indent, name, p, blen);
492         *pmsg += blen + nlen;
493         *pmsglen -= blen + nlen;
494         return 1;
495         }
496
497         
498
499 static int ssl_print_version(BIO *bio, int indent, const char *name,
500                                 const unsigned char **pmsg, size_t *pmsglen)
501         {
502         int vers;
503         if (*pmsglen < 2)
504                 return 0;
505         vers = ((*pmsg)[0] << 8) | (*pmsg)[1];
506         BIO_indent(bio, indent, 80);
507         BIO_printf(bio, "%s=0x%x (%s)\n",
508                                 name, vers,
509                                 ssl_trace_str(vers, ssl_version_tbl));
510         *pmsg += 2;
511         *pmsglen -= 2;
512         return 1;
513         }
514
515 static int ssl_print_random(BIO *bio, int indent,
516                                 const unsigned char **pmsg, size_t *pmsglen)
517         {
518         unsigned int tm;
519         const unsigned char *p = *pmsg;
520         if (*pmsglen < 32)
521                 return 0;
522         tm = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
523         p += 4;
524         BIO_indent(bio, indent, 80);
525         BIO_puts(bio, "Random:\n");
526         BIO_indent(bio, indent + 2, 80);
527         BIO_printf(bio, "gmt_unix_time=0x%08X\n", tm);
528         ssl_print_hex(bio, indent + 2, "random_bytes", p, 28);
529         *pmsg += 32;
530         *pmsglen -= 32;
531         return 1;
532         }
533
534 static int ssl_print_signature(BIO *bio, int indent, SSL *s,
535                                 const unsigned char **pmsg, size_t *pmsglen)
536         {
537         if (*pmsglen < 2)
538                 return 0;
539         if (SSL_USE_SIGALGS(s))
540                 {
541                 const unsigned char *p = *pmsg;
542                 BIO_indent(bio, indent, 80);
543                 BIO_printf(bio, "Signature Algorithm %s+%s (%d+%d)\n",
544                                 ssl_trace_str(p[0], ssl_md_tbl),
545                                 ssl_trace_str(p[1], ssl_sig_tbl),
546                                 p[0], p[1]);
547                 *pmsg += 2;
548                 *pmsglen -= 2;
549                 }
550         return ssl_print_hexbuf(bio, indent, "Signature", 2, pmsg, pmsglen);
551         }
552
553 static int ssl_print_extension(BIO *bio, int indent, int server, int extype,
554                                 const unsigned char *ext, size_t extlen)
555         {
556         size_t xlen;
557         BIO_indent(bio, indent, 80);
558         BIO_printf(bio, "extension_type=%s(%d), length=%d\n",
559                                 ssl_trace_str(extype, ssl_exts_tbl),
560                                 extype, (int)extlen);
561         switch(extype)
562                 {
563         case TLSEXT_TYPE_ec_point_formats:
564                 if (extlen < 1)
565                         return 0;
566                 xlen = ext[0];
567                 if (extlen != xlen + 1)
568                         return 0;
569                 return ssl_trace_list(bio, indent + 2,
570                                         ext + 1, xlen, 1, ssl_point_tbl);
571
572         case TLSEXT_TYPE_elliptic_curves:
573                 if (extlen < 2)
574                         return 0;
575                 xlen = (ext[0] << 8) | ext[1];
576                 if (extlen != xlen + 2)
577                         return 0;
578                 return ssl_trace_list(bio, indent + 2,
579                                         ext + 2, xlen, 2, ssl_curve_tbl);
580
581         case TLSEXT_TYPE_signature_algorithms:
582
583                 if (extlen < 2)
584                         return 0;
585                 xlen = (ext[0] << 8) | ext[1];
586                 if (extlen != xlen + 2)
587                         return 0;
588                 if (xlen & 1)
589                         return 0;
590                 ext += 2;
591                 while(xlen > 0)
592                         {
593                         BIO_indent(bio, indent + 2, 80);
594                         BIO_printf(bio, "%s+%s (%d+%d)\n",
595                                 ssl_trace_str(ext[0], ssl_md_tbl),
596                                 ssl_trace_str(ext[1], ssl_sig_tbl),
597                                 ext[0], ext[1]);
598                         xlen-= 2;
599                         ext+= 2;
600                         }
601                 break;
602         
603         case TLSEXT_TYPE_renegotiate:
604                 if (extlen < 1)
605                         return 0;
606                 xlen = ext[0];
607                 if (xlen + 1 != extlen)
608                         return 0;
609                 ext++;
610                 if (xlen)
611                         {
612                         if (server)
613                                 {
614                                 if (xlen & 1)
615                                         return 0;
616                                 xlen >>= 1;
617                                 }
618                         ssl_print_hex(bio, indent + 4, "client_verify_data",
619                                                                 ext, xlen);
620                         if (server)
621                                 {
622                                 ext += xlen;
623                                 ssl_print_hex(bio, indent + 4,
624                                                         "server_verify_data",
625                                                         ext, xlen);
626                                 }
627                         }
628                 else
629                         {
630                         BIO_indent(bio, indent + 4, 80);
631                         BIO_puts(bio, "<EMPTY>\n");
632                         }
633                 break;
634
635         case TLSEXT_TYPE_heartbeat:
636                 if (extlen != 1)
637                         return 0;
638                 BIO_indent(bio, indent + 2, 80);
639                 BIO_printf(bio, "HeartbeatMode: %s\n",
640                                 ssl_trace_str(ext[0], ssl_hb_tbl));
641                 break;
642
643                 case TLSEXT_TYPE_session_ticket:
644                 if (extlen != 0)
645                         ssl_print_hex(bio, indent + 4, "ticket", ext, extlen);
646                 break;
647
648         default:
649                 BIO_dump_indent(bio, (char *)ext, extlen, indent + 2);
650                 }
651         return 1;
652         }
653
654 static int ssl_print_extensions(BIO *bio, int indent, int server,
655                                 const unsigned char *msg, size_t msglen)
656         {
657         size_t extslen;
658         BIO_indent(bio, indent, 80);
659         if (msglen == 0)
660                 {
661                 BIO_puts(bio, "No Extensions\n");
662                 return 1;
663                 }
664         extslen = (msg[0] << 8) | msg[1];
665         if (extslen != msglen - 2)
666                 return 0;
667         msg += 2;
668         msglen = extslen;
669         BIO_printf(bio, "extensions, length = %d\n", (int)msglen);
670         while (msglen > 0)
671                 {
672                 int extype;
673                 size_t extlen;
674                 if (msglen < 4)
675                         return 0;
676                 extype = (msg[0] << 8) | msg[1];
677                 extlen = (msg[2] << 8) | msg[3];
678                 if (msglen < extlen + 4)
679                         return 0;
680                 msg += 4;
681                 if (!ssl_print_extension(bio, indent + 2, server,
682                                                         extype, msg, extlen))
683                         return 0;
684                 msg += extlen;
685                 msglen -= extlen + 4;
686                 }
687         return 1;
688         }
689
690 static int ssl_print_client_hello(BIO *bio, SSL *ssl, int indent,
691                                 const unsigned char *msg, size_t msglen)
692         {
693         size_t len;
694         unsigned int cs;
695         if (!ssl_print_version(bio, indent, "client_version", &msg, &msglen))
696                 return 0;
697         if (!ssl_print_random(bio, indent, &msg, &msglen))
698                 return 0;
699         if (!ssl_print_hexbuf(bio, indent, "session_id", 1, &msg, &msglen))
700                 return 0;
701         if (SSL_IS_DTLS(ssl))
702                 {
703                 if (!ssl_print_hexbuf(bio, indent, "cookie", 1, &msg, &msglen))
704                         return 0;
705                 }
706         if (msglen < 2)
707                 return 0;
708         len = (msg[0] << 8) | msg[1];
709         msg += 2;
710         msglen -= 2;
711         BIO_indent(bio, indent, 80);
712         BIO_printf(bio, "cipher_suites (len=%d)\n", (int)len);
713         if (msglen < len || len & 1)
714                 return 0;
715         while(len > 0)
716                 {
717                 cs = (msg[0] << 8) | msg[1];
718                 BIO_indent(bio, indent + 2, 80);
719                 BIO_printf(bio, "{0x%02X, 0x%02X} %s\n",
720                                 msg[0], msg[1],
721                                 ssl_trace_str(cs, ssl_ciphers_tbl));
722                 msg += 2;
723                 msglen -= 2;
724                 len -= 2;
725                 }
726         if (msglen < 1)
727                 return 0;
728         len = msg[0];
729         msg++;
730         msglen--;
731         if (msglen < len)
732                 return 0;
733         BIO_indent(bio, indent, 80);
734         BIO_printf(bio, "compression_methods (len=%d)\n", (int)len);
735         while(len > 0)
736                 {
737                 BIO_indent(bio, indent + 2, 80);
738                 BIO_printf(bio, "%s (0x%02X)\n",
739                                 ssl_trace_str(msg[0], ssl_comp_tbl),
740                                 msg[0]);
741                 msg++;
742                 msglen--;
743                 len--;
744                 }
745         if (!ssl_print_extensions(bio, indent, 0, msg, msglen))
746                 return 0;
747         return 1;
748         }
749
750 static int dtls_print_hello_vfyrequest(BIO *bio, int indent,
751                                 const unsigned char *msg, size_t msglen)
752         {
753         if (!ssl_print_version(bio, indent, "server_version", &msg, &msglen))
754                 return 0;
755         if (!ssl_print_hexbuf(bio, indent, "cookie", 1, &msg, &msglen))
756                 return 0;
757         return 1;
758         }
759
760 static int ssl_print_server_hello(BIO *bio, int indent,
761                                 const unsigned char *msg, size_t msglen)
762         {
763         unsigned int cs;
764         if (!ssl_print_version(bio, indent, "server_version", &msg, &msglen))
765                 return 0;
766         if (!ssl_print_random(bio, indent, &msg, &msglen))
767                 return 0;
768         if (!ssl_print_hexbuf(bio, indent, "session_id", 1, &msg, &msglen))
769                 return 0;
770         if (msglen < 2)
771                 return 0;
772         cs = (msg[0] << 8) | msg[1];
773         BIO_indent(bio, indent, 80);
774         BIO_printf(bio, "cipher_suite {0x%02X, 0x%02X} %s\n",
775                                 msg[0], msg[1],
776                                 ssl_trace_str(cs, ssl_ciphers_tbl));
777         msg += 2;
778         msglen -= 2;
779         if (msglen < 1)
780                 return 0;
781         BIO_indent(bio, indent, 80);
782         BIO_printf(bio, "compression_method: %s (0x%02X)\n",
783                         ssl_trace_str(msg[0], ssl_comp_tbl), msg[0]);
784         msg++;
785         msglen--;
786         if (!ssl_print_extensions(bio, indent, 1, msg, msglen))
787                 return 0;
788         return 1;
789         }
790
791 static int ssl_get_keyex(const char **pname, SSL *ssl)
792         {
793         unsigned long alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
794         if (alg_k & SSL_kRSA)
795                 {
796                 *pname = "rsa";
797                 return SSL_kRSA;
798                 }
799         if (alg_k & SSL_kDHr)
800                 {
801                 *pname = "dh_rsa";
802                 return SSL_kDHr;
803                 }
804         if (alg_k & SSL_kDHd)
805                 {
806                 *pname = "dh_dss";
807                 return SSL_kDHd;
808                 }
809         if (alg_k & SSL_kKRB5)
810                 {
811                 *pname = "krb5";
812                 return SSL_kKRB5;
813                 }
814         if (alg_k & SSL_kDHE)
815                 {
816                 *pname = "DHE";
817                 return SSL_kDHE;
818                 }
819         if (alg_k & SSL_kECDHE)
820                 {
821                 *pname = "ECDHE";
822                 return SSL_kECDHE;
823                 }
824         if (alg_k & SSL_kECDHr)
825                 {
826                 *pname = "ECDH RSA";
827                 return SSL_kECDHr;
828                 }
829         if (alg_k & SSL_kECDHe)
830                 {
831                 *pname = "ECDH ECDSA";
832                 return SSL_kECDHe;
833                 }
834         if (alg_k & SSL_kPSK)
835                 {
836                 *pname = "PSK";
837                 return SSL_kPSK;
838                 }
839         if (alg_k & SSL_kSRP)
840                 {
841                 *pname = "SRP";
842                 return SSL_kSRP;
843                 }
844         if (alg_k & SSL_kGOST)
845                 {
846                 *pname = "GOST";
847                 return SSL_kGOST;
848                 }
849         *pname = "UNKNOWN";
850         return 0;
851         }
852
853 static int ssl_print_client_keyex(BIO *bio, int indent, SSL *ssl,
854                                 const unsigned char *msg, size_t msglen)
855         {
856         const char *algname;
857         int id;
858         id = ssl_get_keyex(&algname, ssl);
859         BIO_indent(bio, indent, 80);
860         BIO_printf(bio, "KeyExchangeAlgorithm=%s\n", algname);
861         switch(id)
862                 {
863
864         case SSL_kRSA:
865                 if (TLS1_get_version(ssl) == SSL3_VERSION)
866                         {
867                         ssl_print_hex(bio, indent + 2,
868                                                 "EncyptedPreMasterSecret",
869                                                 msg, msglen);
870                         }
871                 else
872                         {
873                         if (!ssl_print_hexbuf(bio, indent + 2,
874                                                 "EncyptedPreMasterSecret", 2,
875                                                 &msg, &msglen))
876                                 return 0;
877                         }
878                 break;
879
880                 /* Implicit parameters only allowed for static DH */
881         case SSL_kDHd:
882         case SSL_kDHr:
883                 if (msglen == 0)
884                         {
885                         BIO_indent(bio, indent + 2, 80);
886                         BIO_puts(bio, "implicit\n");
887                         break;
888                         }
889         case SSL_kDHE:
890                 if (!ssl_print_hexbuf(bio, indent + 2, "dh_Yc", 2,
891                                                                 &msg, &msglen))
892                         return 0;
893                 break;
894
895         case SSL_kECDHr:
896         case SSL_kECDHe:
897                 if (msglen == 0)
898                         {
899                         BIO_indent(bio, indent + 2, 80);
900                         BIO_puts(bio, "implicit\n");
901                         break;
902                         }
903         case SSL_kECDHE:
904                 if (!ssl_print_hexbuf(bio, indent + 2, "ecdh_Yc", 1,
905                                                         &msg, &msglen))
906                         return 0;
907                 break;
908                 }
909
910         return 1;
911         }
912
913 static int ssl_print_server_keyex(BIO *bio, int indent, SSL *ssl,
914                                 const unsigned char *msg, size_t msglen)
915         {
916         const char *algname;
917         int id;
918         id = ssl_get_keyex(&algname, ssl);
919         BIO_indent(bio, indent, 80);
920         BIO_printf(bio, "KeyExchangeAlgorithm=%s\n", algname);
921         switch(id)
922                 {
923                 /* Should never happen */
924         case SSL_kDHd:
925         case SSL_kDHr:
926         case SSL_kECDHr:
927         case SSL_kECDHe:
928                 BIO_indent(bio, indent + 2, 80);
929                 BIO_printf(bio, "Unexpected Message\n");
930                 break;
931
932         case SSL_kRSA:
933
934                 if (!ssl_print_hexbuf(bio, indent + 2, "rsa_modulus", 2,
935                                                 &msg, &msglen))
936                         return 0;
937                 if (!ssl_print_hexbuf(bio, indent + 2, "rsa_exponent", 2,
938                                                 &msg, &msglen))
939                         return 0;
940                 break;
941
942         case SSL_kDHE:
943                 if (!ssl_print_hexbuf(bio, indent + 2, "dh_p", 2,
944                                                 &msg, &msglen))
945                         return 0;
946                 if (!ssl_print_hexbuf(bio, indent + 2, "dh_g", 2,
947                                                 &msg, &msglen))
948                         return 0;
949                 if (!ssl_print_hexbuf(bio, indent + 2, "dh_Ys", 2,
950                                                 &msg, &msglen))
951                         return 0;
952                 break;
953
954         case SSL_kECDHE:
955                 if (msglen < 1)
956                         return 0;
957                 BIO_indent(bio, indent + 2, 80);
958                 if (msg[0] == EXPLICIT_PRIME_CURVE_TYPE)
959                         BIO_puts(bio, "explicit_prime\n");
960                 else if (msg[0] == EXPLICIT_CHAR2_CURVE_TYPE)
961                         BIO_puts(bio, "explicit_char2\n");
962                 else if (msg[0] == NAMED_CURVE_TYPE)
963                         {
964                         int curve;
965                         if (msglen < 3)
966                                 return 0;
967                         curve = (msg[1] << 8) | msg[2];
968                         BIO_printf(bio, "named_curve: %s (%d)\n",
969                                         ssl_trace_str(curve, ssl_curve_tbl),
970                                         curve);
971                         msg += 3;
972                         msglen -= 3;
973                         if (!ssl_print_hexbuf(bio, indent + 2, "point", 1,
974                                                         &msg, &msglen))
975                                 return 0;
976                         }
977                 break;
978                 }
979         return ssl_print_signature(bio, indent, ssl, &msg, &msglen);
980         }
981
982 static int ssl_print_certificate(BIO *bio, int indent, 
983                                 const unsigned char **pmsg, size_t *pmsglen)
984         {
985         size_t msglen = *pmsglen;
986         size_t clen;
987         X509 *x;
988         const unsigned char *p = *pmsg, *q;
989         if (msglen < 3)
990                 return 0;
991         clen = (p[0] << 16) | (p[1] << 8) | p[2];
992         if (msglen < clen + 3)
993                 return 0;
994         q = p + 3;
995         BIO_indent(bio, indent, 80);
996         BIO_printf(bio, "ASN.1Cert, length=%d", (int)clen);
997         x = d2i_X509(NULL, &q, clen);
998         if (!x)
999                 BIO_puts(bio, "<UNPARSEABLE CERTIFICATE>\n");
1000         else
1001                 {
1002                 BIO_puts(bio, "\n------details-----\n");
1003                 X509_print_ex(bio, x, XN_FLAG_ONELINE, 0);
1004                 PEM_write_bio_X509(bio, x);
1005                 /* Print certificate stuff */
1006                 BIO_puts(bio, "------------------\n");
1007                 X509_free(x);
1008                 }
1009         if (q != p + 3 + clen)
1010                 {
1011                 BIO_puts(bio, "<TRAILING GARBAGE AFTER CERTIFICATE>\n");
1012                 }
1013         *pmsg += clen + 3;
1014         *pmsglen -= clen + 3;
1015         return 1;
1016         }
1017
1018 static int ssl_print_certificates(BIO *bio, int indent, 
1019                                 const unsigned char *msg, size_t msglen)
1020         {
1021         size_t clen;
1022         if (msglen < 3)
1023                 return 0;
1024         clen = (msg[0] << 16) | (msg[1] << 8) | msg[2];
1025         if (msglen != clen + 3)
1026                 return 0;
1027         msg += 3;
1028         BIO_indent(bio, indent, 80);
1029         BIO_printf(bio, "certificate_list, length=%d\n", (int)clen);
1030         while (clen > 0)
1031                 {
1032                 if (!ssl_print_certificate(bio, indent + 2, &msg, &clen))
1033                         return 0;
1034                 }
1035         return 1;
1036         }
1037
1038 static int ssl_print_cert_request(BIO *bio, int indent, SSL *s,
1039                                 const unsigned char *msg, size_t msglen)
1040         {
1041         size_t xlen;
1042         if (msglen < 1)
1043                 return 0;
1044         xlen = msg[0];
1045         if (msglen < xlen + 1)
1046                 return 0;
1047         msg++;
1048         BIO_indent(bio, indent, 80);
1049         BIO_printf(bio, "certificate_types (len=%d)\n", (int)xlen);
1050         if (!ssl_trace_list(bio, indent + 2, msg, xlen, 1, ssl_ctype_tbl))
1051                 return 0;
1052         msg += xlen;
1053         msglen -= xlen + 1;
1054         if (!SSL_USE_SIGALGS(s))
1055                 goto skip_sig;
1056         if (msglen < 2)
1057                 return 0;
1058         xlen = (msg[0] << 8) | msg[1];
1059         if (msglen < xlen + 2 || (xlen & 1))
1060                 return 0;
1061         msg += 2;
1062         BIO_indent(bio, indent, 80);
1063         BIO_printf(bio, "signature_algorithms (len=%d)\n", (int)xlen);
1064         while(xlen > 0)
1065                 {
1066                 BIO_indent(bio, indent + 2, 80);
1067                 BIO_printf(bio, "%s+%s (%d+%d)\n",
1068                                 ssl_trace_str(msg[0], ssl_md_tbl),
1069                                 ssl_trace_str(msg[1], ssl_sig_tbl),
1070                                 msg[0], msg[1]);
1071                 xlen -= 2;
1072                 msg += 2;
1073                 }
1074         msg += xlen;
1075         msglen -= xlen + 2;
1076
1077         skip_sig:
1078         xlen = (msg[0] << 8) | msg[1];
1079         BIO_indent(bio, indent, 80);
1080         if (msglen < xlen + 2)
1081                 return 0;
1082         msg += 2;
1083         msglen -= 2;
1084         BIO_printf(bio, "certificate_authorities (len=%d)\n", (int)xlen);
1085         while (xlen > 0)
1086                 {
1087                 size_t dlen;
1088                 X509_NAME *nm;
1089                 const unsigned char *p;
1090                 if (xlen < 2)
1091                         return 0;
1092                 dlen = (msg[0] << 8) | msg[1];
1093                 if (xlen < dlen + 2)
1094                         return 0;
1095                 msg += 2;
1096                 BIO_indent(bio, indent + 2, 80);
1097                 BIO_printf(bio, "DistinguishedName (len=%d): ", (int)dlen);
1098                 p = msg;
1099                 nm = d2i_X509_NAME(NULL, &p, dlen);
1100                 if (!nm)
1101                         {
1102                         BIO_puts(bio, "<UNPARSEABLE DN>\n");
1103                         }
1104                 else
1105                         {
1106                         X509_NAME_print_ex(bio, nm, 0, XN_FLAG_ONELINE);
1107                         BIO_puts(bio, "\n");
1108                         X509_NAME_free(nm);
1109                         }
1110                 xlen -= dlen + 2;
1111                 msg += dlen;
1112                 }
1113         return 1;
1114         }
1115
1116 static int ssl_print_ticket(BIO *bio, int indent,
1117                                 const unsigned char *msg, size_t msglen)
1118         {
1119         unsigned int tick_life;
1120         if (msglen == 0)
1121                 {
1122                 BIO_indent(bio, indent + 2, 80);
1123                 BIO_puts(bio, "No Ticket\n");
1124                 return 1;
1125                 }
1126         if (msglen < 4)
1127                 return 0;
1128         tick_life = (msg[0] << 24) | (msg[1] << 16) | (msg[2] << 8) | msg[3];
1129         msglen -= 4;
1130         msg += 4;       
1131         BIO_indent(bio, indent + 2, 80);
1132         BIO_printf(bio, "ticket_lifetime_hint=%u\n", tick_life);
1133         if (!ssl_print_hexbuf(bio, indent + 2, "ticket", 2, &msg, &msglen))
1134                 return 0;
1135         if (msglen)
1136                 return 0;
1137         return 1;
1138         }
1139
1140
1141 static int ssl_print_handshake(BIO *bio, SSL *ssl,
1142                                 const unsigned char *msg, size_t msglen,
1143                                 int indent)
1144         {
1145         size_t hlen;
1146         unsigned char htype;
1147         if (msglen < 4)
1148                 return 0;
1149         htype = msg[0];
1150         hlen = (msg[1] << 16) | (msg[2] << 8) | msg[3];
1151         BIO_indent(bio, indent, 80);
1152         BIO_printf(bio, "%s, Length=%d\n",
1153                                 ssl_trace_str(htype, ssl_handshake_tbl),
1154                                 (int)hlen);
1155         msg += 4;
1156         msglen -= 4;
1157         if (SSL_IS_DTLS(ssl))
1158                 {
1159                 if (msglen < 8)
1160                         return 0;
1161                 BIO_indent(bio, indent, 80);
1162                 BIO_printf(bio, "message_seq=%d, fragment_offset=%d, "
1163                                 "fragment_length=%d\n",
1164                                 (msg[0] << 8) | msg[1],
1165                                 (msg[2] << 16) | (msg[3] << 8) | msg[4],
1166                                 (msg[5] << 16) | (msg[6] << 8) | msg[7]);
1167                 msg += 8;
1168                 msglen -= 8;
1169                 }
1170         if (msglen < hlen)
1171                 return 0;
1172         switch(htype)
1173                 {
1174         case SSL3_MT_CLIENT_HELLO:
1175                 if (!ssl_print_client_hello(bio, ssl, indent + 2, msg, msglen))
1176                         return 0;
1177                 break;
1178
1179         case DTLS1_MT_HELLO_VERIFY_REQUEST:
1180                 if (!dtls_print_hello_vfyrequest(bio, indent + 2, msg, msglen))
1181                         return 0;
1182                 break;
1183
1184         case SSL3_MT_SERVER_HELLO:
1185                 if (!ssl_print_server_hello(bio, indent + 2, msg, msglen))
1186                         return 0;
1187                 break;
1188
1189         case SSL3_MT_SERVER_KEY_EXCHANGE:
1190                 if (!ssl_print_server_keyex(bio, indent + 2, ssl, msg, msglen))
1191                         return 0;
1192                 break;
1193
1194         case SSL3_MT_CLIENT_KEY_EXCHANGE:
1195                 if (!ssl_print_client_keyex(bio, indent + 2, ssl, msg, msglen))
1196                         return 0;
1197                 break;
1198
1199         case SSL3_MT_CERTIFICATE:
1200                 if (!ssl_print_certificates(bio, indent + 2,  msg, msglen))
1201                         return 0;
1202                 break;
1203
1204         case SSL3_MT_CERTIFICATE_VERIFY:
1205                 if (!ssl_print_signature(bio, indent + 2, ssl, &msg, &msglen))
1206                         return 0;
1207                 break;
1208
1209         case SSL3_MT_CERTIFICATE_REQUEST:
1210                 if (!ssl_print_cert_request(bio, indent + 2,  ssl, msg, msglen))
1211                         return 0;
1212                 break;
1213
1214         case SSL3_MT_FINISHED:
1215                 ssl_print_hex(bio, indent + 2, "verify_data", msg, msglen);
1216                 break;
1217
1218         case SSL3_MT_SERVER_DONE:
1219                 if (msglen != 0)
1220                         ssl_print_hex(bio, indent + 2, "unexpected value",
1221                                                                 msg, msglen);
1222                 break;
1223
1224         case SSL3_MT_NEWSESSION_TICKET:
1225                 if (!ssl_print_ticket(bio, indent + 2,  msg, msglen))
1226                         return 0;
1227                 break;
1228                 
1229         default:
1230                 BIO_indent(bio, indent + 2, 80);
1231                 BIO_puts(bio, "Unsupported, hex dump follows:\n");
1232                 BIO_dump_indent(bio, (char *)msg, msglen, indent + 4);
1233                 }
1234         return 1;
1235         }
1236
1237 static int ssl_print_heartbeat(BIO *bio, int indent,
1238                                 const unsigned char *msg, size_t msglen)
1239         {
1240         if (msglen < 3)
1241                 return 0;
1242         BIO_indent(bio, indent, 80);
1243         BIO_printf(bio, "HeartBeatMessageType: %s\n",
1244                                 ssl_trace_str(msg[0], ssl_hb_type_tbl));
1245         msg++;
1246         msglen--;
1247         if (!ssl_print_hexbuf(bio, indent, "payload", 2, &msg, &msglen))
1248                 return 0;
1249         ssl_print_hex(bio, indent, "padding", msg, msglen);
1250         return 1;
1251         }
1252
1253 const char *SSL_CIPHER_standard_name(const SSL_CIPHER *c)
1254         {
1255         if (c->algorithm_ssl & SSL_SSLV2)
1256                 return NULL;
1257         return ssl_trace_str(c->id & 0xFFFF, ssl_ciphers_tbl);
1258         }
1259
1260 void SSL_trace(int write_p, int version, int content_type,
1261                 const void *buf, size_t msglen, SSL *ssl, void *arg)
1262         {
1263         const unsigned char *msg = buf;
1264         BIO *bio = arg;
1265
1266         if (write_p == 2)
1267                 {
1268                 BIO_puts(bio, "Session ");
1269                 ssl_print_hex(bio, 0,
1270                                 ssl_trace_str(content_type, ssl_crypto_tbl),
1271                                 msg, msglen);
1272                 return;
1273                 }
1274         switch (content_type)
1275                 {
1276         case SSL3_RT_HEADER:
1277                 {
1278                 int hvers = msg[1] << 8 | msg[2];
1279                 BIO_puts(bio, write_p ? "Sent" : "Received");
1280                 BIO_printf(bio, " Record\nHeader:\n  Version = %s (0x%x)\n",
1281                                 ssl_trace_str(hvers, ssl_version_tbl), hvers);
1282                 if (SSL_IS_DTLS(ssl))
1283                         {
1284                         BIO_printf(bio,
1285                                 "  epoch=%d, sequence_number=%04x%04x%04x\n",
1286                                         (msg[3] << 8 | msg[4]),
1287                                         (msg[5] << 8 | msg[6]),
1288                                         (msg[7] << 8 | msg[8]),
1289                                         (msg[9] << 8 | msg[10]));
1290 #if 0
1291                         /* Just print handshake type so we can see what is
1292                          * going on during fragmentation.
1293                          */
1294                         BIO_printf(bio, "(%s)\n",
1295                                 ssl_trace_str(msg[msglen], ssl_handshake_tbl));
1296 #endif
1297                         }
1298
1299                 BIO_printf(bio, "  Content Type = %s (%d)\n  Length = %d",
1300                                 ssl_trace_str(msg[0], ssl_content_tbl), msg[0],
1301                                 msg[msglen - 2] << 8 | msg[msglen - 1]);
1302                 }
1303                 break;
1304         case SSL3_RT_HANDSHAKE:
1305                 if (!ssl_print_handshake(bio, ssl, msg, msglen, 4))
1306                         BIO_printf(bio, "Message length parse error!\n");
1307                 break;
1308
1309         case SSL3_RT_CHANGE_CIPHER_SPEC:
1310                 if (msglen == 1 && msg[0] == 1)
1311                         BIO_puts(bio, "    change_cipher_spec (1)\n");
1312                 else
1313                         ssl_print_hex(bio, 4, "unknown value", msg, msglen);
1314                 break;
1315
1316         case SSL3_RT_ALERT:
1317                 if (msglen != 2)
1318                         BIO_puts(bio, "    Illegal Alert Length\n");
1319                 else
1320                         {
1321                         BIO_printf(bio,"    Level=%s(%d), description=%s(%d)\n",
1322                                         SSL_alert_type_string_long(msg[0] << 8),
1323                                         msg[0],
1324                                         SSL_alert_desc_string_long(msg[1]),
1325                                         msg[1]);
1326                         }
1327         case TLS1_RT_HEARTBEAT:
1328                 ssl_print_heartbeat(bio, 4, msg, msglen);
1329                 break;
1330
1331                 }
1332
1333         BIO_puts(bio, "\n");
1334         }
1335
1336 #endif