t1_trce.c 42 KB

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