From Peter Wu via https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=6596 :
[metze/wireshark/wip.git] / epan / dissectors / packet-ssl-utils.c
index 156036add9a9d1b00301b98cca5a01230cce90da..11a0fc80b6b960c8918a047c1c7ce1ef26b0877b 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
+#include "config.h"
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
 #ifdef HAVE_LIBZ
 #include <zlib.h>
 #endif
+
+#include <ctype.h>
 #include "packet-ssl-utils.h"
+#include "packet-ssl.h"
 
 #include <epan/emem.h>
 #include <epan/strutil.h>
+#include <epan/addr_resolv.h>
+#include <epan/ipv6-utils.h>
 #include <wsutil/file_util.h>
 
 /*
@@ -49,6 +49,7 @@ const value_string ssl_version_short_names[] = {
     { SSL_VER_TLS,        "TLSv1" },
     { SSL_VER_TLSv1DOT1,  "TLSv1.1" },
     { SSL_VER_DTLS,       "DTLSv1.0" },
+    { SSL_VER_DTLS1DOT2,  "DTLSv1.2" },
     { SSL_VER_PCT,        "PCT" },
     { SSL_VER_TLSv1DOT2,  "TLSv1.2" },
     { 0x00, NULL }
@@ -166,7 +167,7 @@ static const value_string ssl_20_cipher_suites[] = {
     { 0x00006B, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256" },
     { 0x00006C, "TLS_DH_anon_WITH_AES_128_CBC_SHA256" },
     { 0x00006D, "TLS_DH_anon_WITH_AES_256_CBC_SHA256" },
-       /* 0x00,0x6E-83 Unassigned  */
+    /* 0x00,0x6E-83 Unassigned  */
     { 0x000084, "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA" },
     { 0x000085, "TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA" },
     { 0x000086, "TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA" },
@@ -238,9 +239,9 @@ static const value_string ssl_20_cipher_suites[] = {
     { 0x0000C3, "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256" },
     { 0x0000C4, "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256" },
     { 0x0000C5, "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA256" },
-       /* 0x00,0xC6-FE Unassigned  */
-       { 0x0000FF, "TLS_EMPTY_RENEGOTIATION_INFO_SCSV" },
-       /* 0x01-BF,* Unassigned  */
+    /* 0x00,0xC6-FE Unassigned  */
+    { 0x0000FF, "TLS_EMPTY_RENEGOTIATION_INFO_SCSV" },
+    /* 0x01-BF,* Unassigned  */
     /* From RFC 4492 */
     { 0x00c001, "TLS_ECDH_ECDSA_WITH_NULL_SHA" },
     { 0x00c002, "TLS_ECDH_ECDSA_WITH_RC4_128_SHA" },
@@ -304,12 +305,12 @@ static const value_string ssl_20_cipher_suites[] = {
     { 0x00C039, "TLS_ECDHE_PSK_WITH_NULL_SHA" },
     { 0x00C03A, "TLS_ECDHE_PSK_WITH_NULL_SHA256" },
     { 0x00C03B, "TLS_ECDHE_PSK_WITH_NULL_SHA384" },
-       /*      0xC0,0x3C-FF Unassigned
-               0xC1-FD,* Unassigned
-               0xFE,0x00-FD Unassigned
-               0xFE,0xFE-FF Reserved to avoid conflicts with widely deployed implementations [Pasi_Eronen]
-               0xFF,0x00-FF Reserved for Private Use [RFC5246]
-               */
+    /* 0xC0,0x3C-FF Unassigned
+            0xC1-FD,* Unassigned
+            0xFE,0x00-FD Unassigned
+            0xFE,0xFE-FF Reserved to avoid conflicts with widely deployed implementations [Pasi_Eronen]
+            0xFF,0x00-FF Reserved for Private Use [RFC5246]
+            */
 
     /* these from http://www.mozilla.org/projects/
          security/pki/nss/ssl/fips-ssl-ciphersuites.html */
@@ -349,15 +350,15 @@ value_string_ext ssl_20_cipher_suites_ext = VALUE_STRING_EXT_INIT(ssl_20_cipher_
 
 
 const value_string ssl_extension_curves[] = {
-    { 1, "sect163k1" },
-    { 2, "sect163r1" },
-    { 3, "sect163r2" },
-    { 4, "sect193r1" },
-    { 5, "sect193r2" },
-    { 6, "sect233k1" },
-    { 7, "sect233r1" },
-    { 8, "sect239k1" },
-    { 9, "sect283k1" },
+    {  1, "sect163k1" },
+    {  2, "sect163r1" },
+    {  3, "sect163r2" },
+    {  4, "sect193r1" },
+    {  5, "sect193r2" },
+    {  6, "sect233k1" },
+    {  7, "sect233r1" },
+    {  8, "sect239k1" },
+    {  9, "sect283k1" },
     { 10, "sect283r1" },
     { 11, "sect409k1" },
     { 12, "sect409r1" },
@@ -379,6 +380,13 @@ const value_string ssl_extension_curves[] = {
     { 0x00, NULL }
 };
 
+const value_string ssl_curve_types[] = {
+    { 1, "explicit_prime" },
+    { 2, "explicit_char2" },
+    { 3, "named_curve" },
+    { 0x00, NULL }
+};
+
 const value_string ssl_extension_ec_point_formats[] = {
     { 0, "uncompressed" },
     { 1, "ansiX962_compressed_prime" },
@@ -397,10 +405,12 @@ const value_string ssl_31_content_type[] = {
     { 21, "Alert" },
     { 22, "Handshake" },
     { 23, "Application Data" },
+    { 24, "Heartbeat" },
     { 0x00, NULL }
 };
 
 const value_string ssl_versions[] = {
+    { 0xfefd, "DTLS 1.2" },
     { 0xfeff, "DTLS 1.0" },
     { 0x0100, "DTLS 1.0 (OpenSSL pre 0.9.8f)" },
     { 0x0303, "TLS 1.2" },
@@ -427,29 +437,29 @@ const value_string ssl_31_alert_level[] = {
 };
 
 const value_string ssl_31_alert_description[] = {
-    {  0,  "Close Notify" },
-    { 10,  "Unexpected Message" },
-    { 20,  "Bad Record MAC" },
-    { 21,  "Decryption Failed" },
-    { 22,  "Record Overflow" },
-    { 30,  "Decompression Failure" },
-    { 40,  "Handshake Failure" },
-    { 41,  "No Certificate" },
-    { 42,  "Bad Certificate" },
-    { 43,  "Unsupported Certificate" },
-    { 44,  "Certificate Revoked" },
-    { 45,  "Certificate Expired" },
-    { 46,  "Certificate Unknown" },
-    { 47,  "Illegal Parameter" },
-    { 48,  "Unknown CA" },
-    { 49,  "Access Denied" },
-    { 50,  "Decode Error" },
-    { 51,  "Decrypt Error" },
-    { 60,  "Export Restriction" },
-    { 70,  "Protocol Version" },
-    { 71,  "Insufficient Security" },
-    { 80,  "Internal Error" },
-    { 90,  "User Canceled" },
+    {   0,  "Close Notify" },
+    {  10,  "Unexpected Message" },
+    {  20,  "Bad Record MAC" },
+    {  21,  "Decryption Failed" },
+    {  22,  "Record Overflow" },
+    {  30,  "Decompression Failure" },
+    {  40,  "Handshake Failure" },
+    {  41,  "No Certificate" },
+    {  42,  "Bad Certificate" },
+    {  43,  "Unsupported Certificate" },
+    {  44,  "Certificate Revoked" },
+    {  45,  "Certificate Expired" },
+    {  46,  "Certificate Unknown" },
+    {  47,  "Illegal Parameter" },
+    {  48,  "Unknown CA" },
+    {  49,  "Access Denied" },
+    {  50,  "Decode Error" },
+    {  51,  "Decrypt Error" },
+    {  60,  "Export Restriction" },
+    {  70,  "Protocol Version" },
+    {  71,  "Insufficient Security" },
+    {  80,  "Internal Error" },
+    {  90,  "User Canceled" },
     { 100, "No Renegotiation" },
     { 110, "Unsupported Extension" },
     { 111, "Certificate Unobtainable" },
@@ -465,6 +475,7 @@ const value_string ssl_31_handshake_type[] = {
     { SSL_HND_CLIENT_HELLO,      "Client Hello" },
     { SSL_HND_SERVER_HELLO,      "Server Hello" },
     { SSL_HND_HELLO_VERIFY_REQUEST, "Hello Verify Request"},
+    { SSL_HND_NEWSESSION_TICKET, "New Session Ticket" },
     { SSL_HND_CERTIFICATE,       "Certificate" },
     { SSL_HND_SERVER_KEY_EXCHG,  "Server Key Exchange" },
     { SSL_HND_CERT_REQUEST,      "Certificate Request" },
@@ -476,9 +487,21 @@ const value_string ssl_31_handshake_type[] = {
     { 0x00, NULL }
 };
 
+const value_string tls_heartbeat_type[] = {
+    { 1, "Request" },
+    { 2, "Response" },
+    { 0x00, NULL }
+};
+
+const value_string tls_heartbeat_mode[] = {
+    { 1, "Peer allowed to send requests" },
+    { 2, "Peer not allowed to send requests" },
+    { 0x00, NULL }
+};
+
 const value_string ssl_31_compression_method[] = {
-    { 0, "null" },
-    { 1, "DEFLATE" },
+    {  0, "null" },
+    {  1, "DEFLATE" },
     { 64, "LZS" },
     { 0x00, NULL }
 };
@@ -505,6 +528,11 @@ const value_string ssl_31_client_certificate_type[] = {
     { 2, "DSS Sign" },
     { 3, "RSA Fixed DH" },
     { 4, "DSS Fixed DH" },
+    /* GOST certificate types */
+    /* Section 3.5 of draft-chudov-cryptopro-cptls-04 */
+    { 21, "GOST R 34.10-94" },
+    { 22, "GOST R 34.10-2001" },
+    /* END GOST certificate types */
     { 64, "ECDSA Sign" },
     { 65, "RSA Fixed ECDH" },
     { 66, "ECDSA Fixed ECDH" },
@@ -631,6 +659,11 @@ static const value_string ssl_31_ciphersuite[] = {
     { 0x006C, "TLS_DH_anon_WITH_AES_128_CBC_SHA256" },
     { 0x006D, "TLS_DH_anon_WITH_AES_256_CBC_SHA256" },
 
+    /* draft-chudov-cryptopro-cptls-04.txt */
+    { 0x0080,  "TLS_GOSTR341094_WITH_28147_CNT_IMIT" },
+    { 0x0081,  "TLS_GOSTR341001_WITH_28147_CNT_IMIT" },
+    { 0x0082,  "TLS_GOSTR341094_WITH_NULL_GOSTR3411" },
+    { 0x0083,  "TLS_GOSTR341001_WITH_NULL_GOSTR3411" },
 
     /* RFC 4132 */
     { 0x0084, "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA" },
@@ -709,10 +742,10 @@ static const value_string ssl_31_ciphersuite[] = {
     { 0x00C3, "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256" },
     { 0x00C4, "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256" },
     { 0x00C5, "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA256" },
-       /* 0x00,0xC6-FE Unassigned  */
+    /* 0x00,0xC6-FE Unassigned  */
     /* From RFC 5746 */
     { 0x0000FF, "TLS_EMPTY_RENEGOTIATION_INFO_SCSV" },
-       /* 0x01-BF,* Unassigned */
+    /* 0x01-BF,* Unassigned */
     /* From RFC 4492 */
     { 0xc001, "TLS_ECDH_ECDSA_WITH_NULL_SHA" },
     { 0xc002, "TLS_ECDH_ECDSA_WITH_RC4_128_SHA" },
@@ -779,8 +812,126 @@ static const value_string ssl_31_ciphersuite[] = {
     { 0xC039, "TLS_ECDHE_PSK_WITH_NULL_SHA" },
     { 0xC03A, "TLS_ECDHE_PSK_WITH_NULL_SHA256" },
     { 0xC03B, "TLS_ECDHE_PSK_WITH_NULL_SHA384" },
+
+    /* RFC 6209 */
+    { 0xC03C, "TLS_RSA_WITH_ARIA_128_CBC_SHA256" },
+    { 0xC03D, "TLS_RSA_WITH_ARIA_256_CBC_SHA384" },
+    { 0xC03E, "TLS_DH_DSS_WITH_ARIA_128_CBC_SHA256" },
+    { 0xC03F, "TLS_DH_DSS_WITH_ARIA_256_CBC_SHA384" },
+    { 0xC040, "TLS_DH_RSA_WITH_ARIA_128_CBC_SHA256" },
+    { 0xC041, "TLS_DH_RSA_WITH_ARIA_256_CBC_SHA384" },
+    { 0xC042, "TLS_DHE_DSS_WITH_ARIA_128_CBC_SHA256" },
+    { 0xC043, "TLS_DHE_DSS_WITH_ARIA_256_CBC_SHA384" },
+    { 0xC044, "TLS_DHE_RSA_WITH_ARIA_128_CBC_SHA256" },
+    { 0xC045, "TLS_DHE_RSA_WITH_ARIA_256_CBC_SHA384" },
+    { 0xC046, "TLS_DH_anon_WITH_ARIA_128_CBC_SHA256" },
+    { 0xC047, "TLS_DH_anon_WITH_ARIA_256_CBC_SHA384" },
+    { 0xC048, "TLS_ECDHE_ECDSA_WITH_ARIA_128_CBC_SHA256" },
+    { 0xC049, "TLS_ECDHE_ECDSA_WITH_ARIA_256_CBC_SHA384" },
+    { 0xC04A, "TLS_ECDH_ECDSA_WITH_ARIA_128_CBC_SHA256" },
+    { 0xC04B, "TLS_ECDH_ECDSA_WITH_ARIA_256_CBC_SHA384" },
+    { 0xC04C, "TLS_ECDHE_RSA_WITH_ARIA_128_CBC_SHA256" },
+    { 0xC04D, "TLS_ECDHE_RSA_WITH_ARIA_256_CBC_SHA384" },
+    { 0xC04E, "TLS_ECDH_RSA_WITH_ARIA_128_CBC_SHA256" },
+    { 0xC04F, "TLS_ECDH_RSA_WITH_ARIA_256_CBC_SHA384" },
+    { 0xC050, "TLS_RSA_WITH_ARIA_128_GCM_SHA256" },
+    { 0xC051, "TLS_RSA_WITH_ARIA_256_GCM_SHA384" },
+    { 0xC052, "TLS_DHE_RSA_WITH_ARIA_128_GCM_SHA256" },
+    { 0xC053, "TLS_DHE_RSA_WITH_ARIA_256_GCM_SHA384" },
+    { 0xC054, "TLS_DH_RSA_WITH_ARIA_128_GCM_SHA256" },
+    { 0xC055, "TLS_DH_RSA_WITH_ARIA_256_GCM_SHA384" },
+    { 0xC056, "TLS_DHE_DSS_WITH_ARIA_128_GCM_SHA256" },
+    { 0xC057, "TLS_DHE_DSS_WITH_ARIA_256_GCM_SHA384" },
+    { 0xC058, "TLS_DH_DSS_WITH_ARIA_128_GCM_SHA256" },
+    { 0xC059, "TLS_DH_DSS_WITH_ARIA_256_GCM_SHA384" },
+    { 0xC05A, "TLS_DH_anon_WITH_ARIA_128_GCM_SHA256" },
+    { 0xC05B, "TLS_DH_anon_WITH_ARIA_256_GCM_SHA384" },
+    { 0xC05C, "TLS_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256" },
+    { 0xC05D, "TLS_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384" },
+    { 0xC05E, "TLS_ECDH_ECDSA_WITH_ARIA_128_GCM_SHA256" },
+    { 0xC05F, "TLS_ECDH_ECDSA_WITH_ARIA_256_GCM_SHA384" },
+    { 0xC060, "TLS_ECDHE_RSA_WITH_ARIA_128_GCM_SHA256" },
+    { 0xC061, "TLS_ECDHE_RSA_WITH_ARIA_256_GCM_SHA384" },
+    { 0xC062, "TLS_ECDH_RSA_WITH_ARIA_128_GCM_SHA256" },
+    { 0xC063, "TLS_ECDH_RSA_WITH_ARIA_256_GCM_SHA384" },
+    { 0xC064, "TLS_PSK_WITH_ARIA_128_CBC_SHA256" },
+    { 0xC065, "TLS_PSK_WITH_ARIA_256_CBC_SHA384" },
+    { 0xC066, "TLS_DHE_PSK_WITH_ARIA_128_CBC_SHA256" },
+    { 0xC067, "TLS_DHE_PSK_WITH_ARIA_256_CBC_SHA384" },
+    { 0xC068, "TLS_RSA_PSK_WITH_ARIA_128_CBC_SHA256" },
+    { 0xC069, "TLS_RSA_PSK_WITH_ARIA_256_CBC_SHA384" },
+    { 0xC06A, "TLS_PSK_WITH_ARIA_128_GCM_SHA256" },
+    { 0xC06B, "TLS_PSK_WITH_ARIA_256_GCM_SHA384" },
+    { 0xC06C, "TLS_DHE_PSK_WITH_ARIA_128_GCM_SHA256" },
+    { 0xC06D, "TLS_DHE_PSK_WITH_ARIA_256_GCM_SHA384" },
+    { 0xC06E, "TLS_RSA_PSK_WITH_ARIA_128_GCM_SHA256" },
+    { 0xC06F, "TLS_RSA_PSK_WITH_ARIA_256_GCM_SHA384" },
+    { 0xC070, "TLS_ECDHE_PSK_WITH_ARIA_128_CBC_SHA256" },
+    { 0xC071, "TLS_ECDHE_PSK_WITH_ARIA_256_CBC_SHA384" },
+
+    /* RFC 6367 */
+    { 0xC072, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256" },
+    { 0xC073, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384" },
+    { 0xC074, "TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256" },
+    { 0xC075, "TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384" },
+    { 0xC076, "TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
+    { 0xC077, "TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384" },
+    { 0xC078, "TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256" },
+    { 0xC079, "TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384" },
+    { 0xC07A, "TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256" },
+    { 0xC07B, "TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384" },
+    { 0xC07C, "TLS_DHE_RSA_WITH_CAMELLIA_128_GCM_SHA256" },
+    { 0xC07D, "TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384" },
+    { 0xC07E, "TLS_DH_RSA_WITH_CAMELLIA_128_GCM_SHA256" },
+    { 0xC07F, "TLS_DH_RSA_WITH_CAMELLIA_256_GCM_SHA384" },
+    { 0xC080, "TLS_DHE_DSS_WITH_CAMELLIA_128_GCM_SHA256" },
+    { 0xC081, "TLS_DHE_DSS_WITH_CAMELLIA_256_GCM_SHA384" },
+    { 0xC082, "TLS_DH_DSS_WITH_CAMELLIA_128_GCM_SHA256" },
+    { 0xC083, "TLS_DH_DSS_WITH_CAMELLIA_256_GCM_SHA384" },
+    { 0xC084, "TLS_DH_anon_WITH_CAMELLIA_128_GCM_SHA256" },
+    { 0xC085, "TLS_DH_anon_WITH_CAMELLIA_256_GCM_SHA384" },
+    { 0xC086, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_GCM_SHA256" },
+    { 0xC087, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_GCM_SHA384" },
+    { 0xC088, "TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256" },
+    { 0xC089, "TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384" },
+    { 0xC08A, "TLS_ECDHE_RSA_WITH_CAMELLIA_128_GCM_SHA256" },
+    { 0xC08B, "TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384" },
+    { 0xC08C, "TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256" },
+    { 0xC08D, "TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384" },
+    { 0xC08E, "TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256" },
+    { 0xC08F, "TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384" },
+    { 0xC090, "TLS_DHE_PSK_WITH_CAMELLIA_128_GCM_SHA256" },
+    { 0xC091, "TLS_DHE_PSK_WITH_CAMELLIA_256_GCM_SHA384" },
+    { 0xC092, "TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256" },
+    { 0xC093, "TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384" },
+    { 0xC094, "TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256" },
+    { 0xC095, "TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384" },
+    { 0xC096, "TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256" },
+    { 0xC097, "TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384" },
+    { 0xC098, "TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256" },
+    { 0xC099, "TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384" },
+    { 0xC09A, "TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256" },
+    { 0xC09B, "TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384" },
+
+    /* RFC 6655 */
+    { 0xC09C, "TLS_RSA_WITH_AES_128_CCM" },
+    { 0xC09D, "TLS_RSA_WITH_AES_256_CCM" },
+    { 0xC09E, "TLS_DHE_RSA_WITH_AES_128_CCM" },
+    { 0xC09F, "TLS_DHE_RSA_WITH_AES_256_CCM" },
+    { 0xC0A0, "TLS_RSA_WITH_AES_128_CCM_8" },
+    { 0xC0A1, "TLS_RSA_WITH_AES_256_CCM_8" },
+    { 0xC0A2, "TLS_DHE_RSA_WITH_AES_128_CCM_8" },
+    { 0xC0A3, "TLS_DHE_RSA_WITH_AES_256_CCM_8" },
+    { 0xC0A4, "TLS_PSK_WITH_AES_128_CCM" },
+    { 0xC0A5, "TLS_PSK_WITH_AES_256_CCM" },
+    { 0xC0A6, "TLS_DHE_PSK_WITH_AES_128_CCM" },
+    { 0xC0A7, "TLS_DHE_PSK_WITH_AES_256_CCM" },
+    { 0xC0A8, "TLS_PSK_WITH_AES_128_CCM_8" },
+    { 0xC0A9, "TLS_PSK_WITH_AES_256_CCM_8" },
+    { 0xC0AA, "TLS_PSK_DHE_WITH_AES_128_CCM_8" },
+    { 0xC0AB, "TLS_PSK_DHE_WITH_AES_256_CCM_8" },
 /*
-0xC0,0x3C-FF Unassigned
+0xC0,0xAB-FF Unassigned
 0xC1-FD,* Unassigned
 0xFE,0x00-FD Unassigned
 0xFE,0xFE-FF Reserved to avoid conflicts with widely deployed implementations [Pasi_Eronen]
@@ -866,7 +1017,7 @@ const value_string pct_error_code[] = {
 
 /* RFC 4366 */
 const value_string tls_hello_extension_types[] = {
-    { 0, "server_name" },
+    { SSL_HND_HELLO_EXT_SERVER_NAME, "server_name" }, /* RFC 3546 */
     { 1, "max_fragment_length" },
     { 2, "client_certificate_url" },
     { 3, "trusted_ca_keys" },
@@ -881,8 +1032,16 @@ const value_string tls_hello_extension_types[] = {
     { 12, "srp" },  /* RFC 5054 */
     { 13, "signature_algorithms" },  /* RFC 5246 */
     { 14, "use_srtp" },
+    { SSL_HND_HELLO_EXT_HEARTBEAT, "Heartbeat" },  /* RFC 6520 */
+    { SSL_HND_HELLO_EXT_ALPN, "Application Layer Protocol Negotiation" }, /* draft-ietf-tls-applayerprotoneg-01 */
     { 35, "SessionTicket TLS" },  /* RFC 4507 */
-    { 65281, "renegotiation_info" },
+    { SSL_HND_HELLO_EXT_NPN, "next_protocol_negotiation"}, /* http://technotes.googlecode.com/git/nextprotoneg.html */
+    { SSL_HND_HELLO_EXT_RENEG_INFO, "renegotiation_info" }, /* RFC 5746 */
+    { 0, NULL }
+};
+
+const value_string tls_hello_ext_server_name_type_vs[] = {
+    { 0, "host_name" },
     { 0, NULL }
 };
 
@@ -906,6 +1065,13 @@ const value_string tls_signature_algorithm[] = {
     { 0, NULL }
 };
 
+/* RFC 6091 3.1 */
+const value_string tls_certificate_type[] = {
+    { 0, "X.509" },
+    { 1, "OpenPGP" },
+    { 0, NULL }
+};
+
 const value_string tls_cert_status_type[] = {
     { SSL_HND_CERT_STATUS_TYPE_OCSP, "OCSP" },
     { 0, NULL }
@@ -923,10 +1089,240 @@ struct _SslDecompress {
 #endif
 };
 
+/* To assist in parsing client/server key exchange messages
+   0 indicates unknown */
+gint ssl_get_keyex_alg(gint cipher)
+{
+    switch(cipher) {
+    case 0x0001:
+    case 0x0002:
+    case 0x0003:
+    case 0x0004:
+    case 0x0005:
+    case 0x0006:
+    case 0x0007:
+    case 0x0008:
+    case 0x0009:
+    case 0x000a:
+    case 0x002f:
+    case 0x0035:
+    case 0x003b:
+    case 0x003c:
+    case 0x003d:
+    case 0x0041:
+    case 0x0060:
+    case 0x0061:
+    case 0x0062:
+    case 0x0064:
+    case 0x0084:
+    case 0x0096:
+    case 0x009c:
+    case 0x009d:
+    case 0x00ba:
+    case 0x00c0:
+    case 0xfefe:
+    case 0xfeff:
+    case 0xffe0:
+    case 0xffe1:
+        return KEX_RSA;
+    case 0x000b:
+    case 0x000c:
+    case 0x000d:
+    case 0x000e:
+    case 0x000f:
+    case 0x0010:
+    case 0x0011:
+    case 0x0012:
+    case 0x0013:
+    case 0x0014:
+    case 0x0015:
+    case 0x0016:
+    case 0x0017:
+    case 0x0018:
+    case 0x0019:
+    case 0x001a:
+    case 0x001b:
+    case 0x002d:
+    case 0x0030:
+    case 0x0031:
+    case 0x0032:
+    case 0x0033:
+    case 0x0034:
+    case 0x0036:
+    case 0x0037:
+    case 0x0038:
+    case 0x0039:
+    case 0x003a:
+    case 0x003e:
+    case 0x003f:
+    case 0x0040:
+    case 0x0042:
+    case 0x0043:
+    case 0x0044:
+    case 0x0045:
+    case 0x0046:
+    case 0x0063:
+    case 0x0065:
+    case 0x0066:
+    case 0x0067:
+    case 0x0068:
+    case 0x0069:
+    case 0x006a:
+    case 0x006b:
+    case 0x006c:
+    case 0x006d:
+    case 0x0085:
+    case 0x0086:
+    case 0x0087:
+    case 0x0088:
+    case 0x0089:
+    case 0x008e:
+    case 0x008f:
+    case 0x0090:
+    case 0x0091:
+    case 0x0097:
+    case 0x0098:
+    case 0x0099:
+    case 0x009a:
+    case 0x009b:
+    case 0x009e:
+    case 0x009f:
+    case 0x00a0:
+    case 0x00a1:
+    case 0x00a2:
+    case 0x00a3:
+    case 0x00a4:
+    case 0x00a5:
+    case 0x00a6:
+    case 0x00a7:
+    case 0x00aa:
+    case 0x00ab:
+    case 0x00b2:
+    case 0x00b3:
+    case 0x00b4:
+    case 0x00b5:
+    case 0x00bb:
+    case 0x00bc:
+    case 0x00bd:
+    case 0x00be:
+    case 0x00bf:
+    case 0x00c1:
+    case 0x00c2:
+    case 0x00c3:
+    case 0x00c4:
+    case 0x00c5:
+        return KEX_DH;
+    case 0xc001:
+    case 0xc002:
+    case 0xc003:
+    case 0xc004:
+    case 0xc005:
+    case 0xc006:
+    case 0xc007:
+    case 0xc008:
+    case 0xc009:
+    case 0xc00a:
+    case 0xc00b:
+    case 0xc00c:
+    case 0xc00d:
+    case 0xc00e:
+    case 0xc00f:
+    case 0xc010:
+    case 0xc011:
+    case 0xc012:
+    case 0xc013:
+    case 0xc014:
+    case 0xc015:
+    case 0xc016:
+    case 0xc017:
+    case 0xc018:
+    case 0xc019:
+    case 0xc023:
+    case 0xc024:
+    case 0xc025:
+    case 0xc026:
+    case 0xc027:
+    case 0xc028:
+    case 0xc029:
+    case 0xc02a:
+    case 0xc02b:
+    case 0xc02c:
+    case 0xc02d:
+    case 0xc02e:
+    case 0xc02f:
+    case 0xc030:
+    case 0xc031:
+    case 0xc032:
+    case 0xc033:
+    case 0xc034:
+    case 0xc035:
+    case 0xc036:
+    case 0xc037:
+    case 0xc038:
+    case 0xc039:
+    case 0xc03a:
+    case 0xc03b:
+        return KEX_ECDH;
+    case 0x002C:
+    case 0x008A:
+    case 0x008B:
+    case 0x008C:
+    case 0x008D:
+    case 0x00A8:
+    case 0x00A9:
+    case 0x00AE:
+    case 0x00AF:
+    case 0x00B0:
+    case 0x00B1:
+    case 0xC064:
+    case 0xC065:
+    case 0xC06A:
+    case 0xC06B:
+    case 0xC08E:
+    case 0xC08F:
+    case 0xC094:
+    case 0xC095:
+    case 0xC0A4:
+    case 0xC0A5:
+    case 0xC0A8:
+    case 0xC0A9:
+    case 0xC0AA:
+    case 0xC0AB:
+        return KEX_PSK;
+    case 0x002E:
+    case 0x0092:
+    case 0x0093:
+    case 0x0094:
+    case 0x0095:
+    case 0x00AC:
+    case 0x00AD:
+    case 0x00B6:
+    case 0x00B7:
+    case 0x00B8:
+    case 0x00B9:
+    case 0xC068:
+    case 0xC069:
+    case 0xC06E:
+    case 0xC06F:
+    case 0xC092:
+    case 0xC093:
+    case 0xC098:
+    case 0xC099:
+        return KEX_RSA_PSK;
+    default:
+        break;
+    }
+
+    return 0;
+}
+
+
+
+
 static gint
 ssl_data_alloc(StringInfo* str, size_t len)
 {
-    str->data = g_malloc(len);
+    str->data = (guchar *)g_malloc(len);
     /* the allocator can return a null pointer for a size equal to 0,
      * and that must be allowed */
     if (len > 0 && !str->data)
@@ -942,9 +1338,7 @@ ssl_data_set(StringInfo* str, const guchar* data, guint len)
     str->data_len = len;
 }
 
-#ifdef HAVE_LIBGNUTLS
-
-static gint ver_major, ver_minor, ver_patch;
+#if defined(HAVE_LIBGNUTLS) && defined(HAVE_LIBGCRYPT)
 
 /* hmac abstraction layer */
 #define SSL_HMAC gcry_md_hd_t
@@ -952,8 +1346,9 @@ static gint ver_major, ver_minor, ver_patch;
 static inline gint
 ssl_hmac_init(SSL_HMAC* md, const void * key, gint len, gint algo)
 {
-    gcry_error_t err;
-    const char *err_str, *err_src;
+    gcry_error_t  err;
+    const char   *err_str, *err_src;
+
     err = gcry_md_open(md,algo, GCRY_MD_FLAG_HMAC);
     if (err != 0) {
         err_str = gcry_strerror(err);
@@ -972,12 +1367,13 @@ ssl_hmac_update(SSL_HMAC* md, const void* data, gint len)
 static inline void
 ssl_hmac_final(SSL_HMAC* md, guchar* data, guint* datalen)
 {
-    gint algo;
+    gint  algo;
     guint len;
+
     algo = gcry_md_get_algo (*(md));
-    len = gcry_md_get_algo_dlen(algo);
+    len  = gcry_md_get_algo_dlen(algo);
     memcpy(data, gcry_md_read(*(md), algo), len);
-    *datalen =len;
+    *datalen = len;
 }
 static inline void
 ssl_hmac_cleanup(SSL_HMAC* md)
@@ -991,8 +1387,8 @@ ssl_hmac_cleanup(SSL_HMAC* md)
 static inline gint
 ssl_md_init(SSL_MD* md, gint algo)
 {
-    gcry_error_t err;
-    const char *err_str, *err_src;
+    gcry_error_t  err;
+    const char   *err_str, *err_src;
     err = gcry_md_open(md,algo, 0);
     if (err != 0) {
         err_str = gcry_strerror(err);
@@ -1041,7 +1437,7 @@ static inline void
 ssl_sha_final(guchar* buf, SSL_SHA_CTX* md)
 {
     memcpy(buf, gcry_md_read(*(md),  GCRY_MD_SHA1),
-        gcry_md_get_algo_dlen(GCRY_MD_SHA1));
+           gcry_md_get_algo_dlen(GCRY_MD_SHA1));
 }
 static inline void
 ssl_sha_cleanup(SSL_SHA_CTX* md)
@@ -1063,7 +1459,7 @@ static inline void
 ssl_md5_final(guchar* buf, SSL_MD5_CTX* md)
 {
     memcpy(buf, gcry_md_read(*(md),  GCRY_MD_MD5),
-        gcry_md_get_algo_dlen(GCRY_MD_MD5));
+           gcry_md_get_algo_dlen(GCRY_MD_MD5));
 }
 static inline void
 ssl_md5_cleanup(SSL_MD5_CTX* md)
@@ -1074,28 +1470,30 @@ ssl_md5_cleanup(SSL_MD5_CTX* md)
 gint
 ssl_cipher_setiv(SSL_CIPHER_CTX *cipher, guchar* iv, gint iv_len)
 {
-    /* guchar * ivp; */
     gint ret;
-    /* gint i; */
+#if 0
+    guchar *ivp;
+    gint i;
     gcry_cipher_hd_t c;
-    ret=0;
     c=(gcry_cipher_hd_t)*cipher;
-
+#endif
     ssl_debug_printf("--------------------------------------------------------------------");
-    /*for(ivp=c->iv,i=0; i < iv_len; i++ )
+#if 0
+    for(ivp=c->iv,i=0; i < iv_len; i++ )
         {
         ssl_debug_printf("%d ",ivp[i]);
         i++;
         }
-    */
+#endif
     ssl_debug_printf("--------------------------------------------------------------------");
     ret = gcry_cipher_setiv(*(cipher), iv, iv_len);
-    /*for(ivp=c->iv,i=0; i < iv_len; i++ )
+#if 0
+    for(ivp=c->iv,i=0; i < iv_len; i++ )
         {
         ssl_debug_printf("%d ",ivp[i]);
         i++;
         }
-    */
+#endif
     ssl_debug_printf("--------------------------------------------------------------------");
     return ret;
 }
@@ -1104,7 +1502,7 @@ static gint
 ssl_cipher_init(gcry_cipher_hd_t *cipher, gint algo, guchar* sk,
         guchar* iv, gint mode)
 {
-    gint gcry_modes[]={GCRY_CIPHER_MODE_STREAM,GCRY_CIPHER_MODE_CBC};
+    gint gcry_modes[]={GCRY_CIPHER_MODE_STREAM,GCRY_CIPHER_MODE_CBC,GCRY_CIPHER_MODE_CTR};
     gint err;
     if (algo == -1) {
         /* NULL mode */
@@ -1166,15 +1564,15 @@ _gcry_rsa_decrypt (int algo, gcry_mpi_t *result, gcry_mpi_t *data,
 const gchar*
 ssl_private_key_to_str(SSL_PRIVATE_KEY* pk)
 {
-    const gchar *str="NULL";
-    size_t n;
-    gchar *buf;
+    const gchar *str = "NULL";
+    size_t       n;
+    gchar       *buf;
 
     if (!pk) return str;
 #ifndef SSL_FAST
     n = gcry_sexp_sprint(pk, GCRYSEXP_FMT_ADVANCED, NULL, 0);
-    buf = ep_alloc(n);
-    n = gcry_sexp_sprint(pk, GCRYSEXP_FMT_ADVANCED, buf, n);
+    buf = (gchar *)ep_alloc(n);
+    /*n = gcry_sexp_sprint(pk, GCRYSEXP_FMT_ADVANCED, buf, n);*/
     str = buf;
 #else /* SSL_FAST */
     str = "TO DO: dump mpi gcry_mpi_print()";
@@ -1188,16 +1586,16 @@ ssl_private_key_to_str(SSL_PRIVATE_KEY* pk)
 int
 ssl_private_decrypt(guint len, guchar* encr_data, SSL_PRIVATE_KEY* pk)
 {
-    gint rc;
-    size_t decr_len;
-    gcry_sexp_t  s_data, s_plain;
-    gcry_mpi_t encr_mpi;
-    size_t i, encr_len;
-    guchar* decr_data_ptr;
-    gcry_mpi_t text;
+    gint        rc;
+    size_t      decr_len;
+    gcry_sexp_t s_data, s_plain;
+    gcry_mpi_t  encr_mpi;
+    size_t      i, encr_len;
+    guchar*     decr_data_ptr;
+    gcry_mpi_t  text;
     decr_len = 0;
     encr_len = len;
-    text=NULL;
+    text     = NULL;
 
     /* build up a mpi rappresentation for encrypted data */
     rc = gcry_mpi_scan(&encr_mpi, GCRYMPI_FMT_USG,encr_data, encr_len, &encr_len);
@@ -1318,7 +1716,7 @@ out:
 static gint
 ssl_data_realloc(StringInfo* str, guint len)
 {
-    str->data = g_realloc(str->data, len);
+    str->data = (guchar *)g_realloc(str->data, len);
     if (!str->data)
         return -1;
     str->data_len = len;
@@ -1337,15 +1735,19 @@ ssl_data_copy(StringInfo* dst, StringInfo* src)
     return 0;
 }
 
-#define PRF(ssl,secret,usage,rnd1,rnd2,out) ((ssl->version_netorder==SSLV3_VERSION)? \
-        ssl3_prf(secret,usage,rnd1,rnd2,out): \
-        tls_prf(secret,usage,rnd1,rnd2,out))
-
-static const gchar *digests[]={
-    "MD5",
-    "SHA1"
+static const SslDigestAlgo digests[]={
+    {"MD5",     16},
+    {"SHA1",    20},
+    {"SHA256",  32},
+    {"SHA384",  48},
 };
 
+/* get index digest index */
+static const SslDigestAlgo *
+ssl_cipher_suite_dig(SslCipherSuite *cs) {
+    return &digests[cs->dig - DIG_MD5];
+}
+
 static const gchar *ciphers[]={
     "DES",
     "3DES",
@@ -1354,52 +1756,159 @@ static const gchar *ciphers[]={
     "IDEA",
     "AES",
     "AES256",
+    "CAMELLIA128",
+    "CAMELLIA256",
+    "SEED",
     "*UNKNOWN*"
 };
 
 static SslCipherSuite cipher_suites[]={
-    {1,KEX_RSA,SIG_RSA,ENC_NULL,1,0,0,DIG_MD5,16,0, SSL_CIPHER_MODE_STREAM},
-    {2,KEX_RSA,SIG_RSA,ENC_NULL,1,0,0,DIG_SHA,20,0, SSL_CIPHER_MODE_STREAM},
-    {3,KEX_RSA,SIG_RSA,ENC_RC4,1,128,40,DIG_MD5,16,1, SSL_CIPHER_MODE_STREAM},
-    {4,KEX_RSA,SIG_RSA,ENC_RC4,1,128,128,DIG_MD5,16,0, SSL_CIPHER_MODE_STREAM},
-    {5,KEX_RSA,SIG_RSA,ENC_RC4,1,128,128,DIG_SHA,20,0, SSL_CIPHER_MODE_STREAM},
-    {6,KEX_RSA,SIG_RSA,ENC_RC2,8,128,40,DIG_SHA,20,1, SSL_CIPHER_MODE_STREAM},
-    {7,KEX_RSA,SIG_RSA,ENC_IDEA,8,128,128,DIG_SHA,20,0, SSL_CIPHER_MODE_STREAM},
-    {8,KEX_RSA,SIG_RSA,ENC_DES,8,64,40,DIG_SHA,20,1, SSL_CIPHER_MODE_CBC},
-    {9,KEX_RSA,SIG_RSA,ENC_DES,8,64,64,DIG_SHA,20,0, SSL_CIPHER_MODE_CBC},
-    {10,KEX_RSA,SIG_RSA,ENC_3DES,8,192,192,DIG_SHA,20,0, SSL_CIPHER_MODE_CBC},
-    {11,KEX_DH,SIG_DSS,ENC_DES,8,64,40,DIG_SHA,20,1, SSL_CIPHER_MODE_CBC},
-    {12,KEX_DH,SIG_DSS,ENC_DES,8,64,64,DIG_SHA,20,0, SSL_CIPHER_MODE_CBC},
-    {13,KEX_DH,SIG_DSS,ENC_3DES,8,192,192,DIG_SHA,20,0, SSL_CIPHER_MODE_CBC},
-    {14,KEX_DH,SIG_RSA,ENC_DES,8,64,40,DIG_SHA,20,1, SSL_CIPHER_MODE_CBC},
-    {15,KEX_DH,SIG_RSA,ENC_DES,8,64,64,DIG_SHA,20,0, SSL_CIPHER_MODE_CBC},
-    {16,KEX_DH,SIG_RSA,ENC_3DES,8,192,192,DIG_SHA,20,0, SSL_CIPHER_MODE_CBC},
-    {17,KEX_DH,SIG_DSS,ENC_DES,8,64,40,DIG_SHA,20,1, SSL_CIPHER_MODE_CBC},
-    {18,KEX_DH,SIG_DSS,ENC_DES,8,64,64,DIG_SHA,20,0, SSL_CIPHER_MODE_CBC},
-    {19,KEX_DH,SIG_DSS,ENC_3DES,8,192,192,DIG_SHA,20,0, SSL_CIPHER_MODE_CBC},
-    {20,KEX_DH,SIG_RSA,ENC_DES,8,64,40,DIG_SHA,20,1, SSL_CIPHER_MODE_CBC},
-    {21,KEX_DH,SIG_RSA,ENC_DES,8,64,64,DIG_SHA,20,0, SSL_CIPHER_MODE_CBC},
-    {22,KEX_DH,SIG_RSA,ENC_3DES,8,192,192,DIG_SHA,20,0, SSL_CIPHER_MODE_CBC},
-    {23,KEX_DH,SIG_NONE,ENC_RC4,1,128,40,DIG_MD5,16,1, SSL_CIPHER_MODE_STREAM},
-    {24,KEX_DH,SIG_NONE,ENC_RC4,1,128,128,DIG_MD5,16,0, SSL_CIPHER_MODE_STREAM},
-    {25,KEX_DH,SIG_NONE,ENC_DES,8,64,40,DIG_MD5,16,1, SSL_CIPHER_MODE_CBC},
-    {26,KEX_DH,SIG_NONE,ENC_DES,8,64,64,DIG_MD5,16,0, SSL_CIPHER_MODE_CBC},
-    {27,KEX_DH,SIG_NONE,ENC_3DES,8,192,192,DIG_MD5,16,0, SSL_CIPHER_MODE_CBC},
-    {47,KEX_RSA,SIG_RSA,ENC_AES,16,128,128,DIG_SHA,20,0, SSL_CIPHER_MODE_CBC},
-    {51,KEX_DH, SIG_RSA,ENC_AES,16,128,128,DIG_SHA,20,0, SSL_CIPHER_MODE_CBC},
-    {53,KEX_RSA,SIG_RSA,ENC_AES256,16,256,256,DIG_SHA,20,0, SSL_CIPHER_MODE_CBC},
-    {96,KEX_RSA,SIG_RSA,ENC_RC4,1,128,56,DIG_MD5,16,1, SSL_CIPHER_MODE_STREAM},
-    {97,KEX_RSA,SIG_RSA,ENC_RC2,1,128,56,DIG_MD5,16,1, SSL_CIPHER_MODE_STREAM},
-    {98,KEX_RSA,SIG_RSA,ENC_DES,8,64,64,DIG_SHA,20,1, SSL_CIPHER_MODE_STREAM},
-    {99,KEX_DH,SIG_DSS,ENC_DES,8,64,64,DIG_SHA,16,1, SSL_CIPHER_MODE_CBC},
-    {100,KEX_RSA,SIG_RSA,ENC_RC4,1,128,56,DIG_SHA,20,1, SSL_CIPHER_MODE_STREAM},
-    {101,KEX_DH,SIG_DSS,ENC_RC4,1,128,56,DIG_SHA,20,1, SSL_CIPHER_MODE_STREAM},
-    {102,KEX_DH,SIG_DSS,ENC_RC4,1,128,128,DIG_SHA,20,0, SSL_CIPHER_MODE_STREAM},
-    /*{138,KEX_PSK,SIG_RSA,ENC_RC4,16,128,128,DIG_SHA,20,0, SSL_CIPHER_MODE_CBC},*/
-    {139,KEX_PSK,SIG_RSA,ENC_3DES,8,192,192,DIG_SHA,20,0, SSL_CIPHER_MODE_CBC},
-    {140,KEX_PSK,SIG_RSA,ENC_AES,16,128,128,DIG_SHA,20,0, SSL_CIPHER_MODE_CBC},
-    {141,KEX_PSK,SIG_RSA,ENC_AES256,16,256,256,DIG_SHA,20,0, SSL_CIPHER_MODE_CBC},
-    {-1, 0,0,0,0,0,0,0,0,0, 0}
+    {1,KEX_RSA,SIG_RSA,ENC_NULL,1,0,0,DIG_MD5, SSL_CIPHER_MODE_STREAM},   /* TLS_RSA_WITH_NULL_MD5 */
+    {2,KEX_RSA,SIG_RSA,ENC_NULL,1,0,0,DIG_SHA, SSL_CIPHER_MODE_STREAM},   /* TLS_RSA_WITH_NULL_SHA */
+    {3,KEX_RSA,SIG_RSA,ENC_RC4,1,128,40,DIG_MD5, SSL_CIPHER_MODE_STREAM},   /* TLS_RSA_EXPORT_WITH_RC4_40_MD5 */
+    {4,KEX_RSA,SIG_RSA,ENC_RC4,1,128,128,DIG_MD5, SSL_CIPHER_MODE_STREAM},   /* TLS_RSA_WITH_RC4_128_MD5 */
+    {5,KEX_RSA,SIG_RSA,ENC_RC4,1,128,128,DIG_SHA, SSL_CIPHER_MODE_STREAM},   /* TLS_RSA_WITH_RC4_128_SHA */
+    {6,KEX_RSA,SIG_RSA,ENC_RC2,8,128,40,DIG_MD5, SSL_CIPHER_MODE_CBC},   /* TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5 */
+    {7,KEX_RSA,SIG_RSA,ENC_IDEA,8,128,128,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_RSA_WITH_IDEA_CBC_SHA */
+    {8,KEX_RSA,SIG_RSA,ENC_DES,8,64,40,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_RSA_EXPORT_WITH_DES40_CBC_SHA */
+    {9,KEX_RSA,SIG_RSA,ENC_DES,8,64,64,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_RSA_WITH_DES_CBC_SHA */
+    {10,KEX_RSA,SIG_RSA,ENC_3DES,8,192,192,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_RSA_WITH_3DES_EDE_CBC_SHA */
+    {11,KEX_DH,SIG_DSS,ENC_DES,8,64,40,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA */
+    {12,KEX_DH,SIG_DSS,ENC_DES,8,64,64,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DH_DSS_WITH_DES_CBC_SHA */
+    {13,KEX_DH,SIG_DSS,ENC_3DES,8,192,192,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA */
+    {14,KEX_DH,SIG_RSA,ENC_DES,8,64,40,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA */
+    {15,KEX_DH,SIG_RSA,ENC_DES,8,64,64,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DH_RSA_WITH_DES_CBC_SHA */
+    {16,KEX_DH,SIG_RSA,ENC_3DES,8,192,192,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA */
+    {17,KEX_DH,SIG_DSS,ENC_DES,8,64,40,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA */
+    {18,KEX_DH,SIG_DSS,ENC_DES,8,64,64,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DHE_DSS_WITH_DES_CBC_SHA */
+    {19,KEX_DH,SIG_DSS,ENC_3DES,8,192,192,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA */
+    {20,KEX_DH,SIG_RSA,ENC_DES,8,64,40,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA */
+    {21,KEX_DH,SIG_RSA,ENC_DES,8,64,64,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DHE_RSA_WITH_DES_CBC_SHA */
+    {22,KEX_DH,SIG_RSA,ENC_3DES,8,192,192,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */
+    {23,KEX_DH,SIG_NONE,ENC_RC4,1,128,40,DIG_MD5, SSL_CIPHER_MODE_STREAM},   /* TLS_DH_anon_EXPORT_WITH_RC4_40_MD5 */
+    {24,KEX_DH,SIG_NONE,ENC_RC4,1,128,128,DIG_MD5, SSL_CIPHER_MODE_STREAM},   /* TLS_DH_anon_WITH_RC4_128_MD5 */
+    {25,KEX_DH,SIG_NONE,ENC_DES,8,64,40,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA */
+    {26,KEX_DH,SIG_NONE,ENC_DES,8,64,64,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DH_anon_WITH_DES_CBC_SHA */
+    {27,KEX_DH,SIG_NONE,ENC_3DES,8,192,192,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DH_anon_WITH_3DES_EDE_CBC_SHA */
+    {47,KEX_RSA,SIG_RSA,ENC_AES,16,128,128,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_RSA_WITH_AES_128_CBC_SHA */
+    {48,KEX_DH,SIG_DSS,ENC_AES,16,128,128,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DH_DSS_WITH_AES_128_CBC_SHA */
+    {49,KEX_DH,SIG_RSA,ENC_AES,16,128,128,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DH_RSA_WITH_AES_128_CBC_SHA */
+    {50,KEX_DH,SIG_DSS,ENC_AES,16,128,128,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DHE_DSS_WITH_AES_128_CBC_SHA */
+    {51,KEX_DH,SIG_RSA,ENC_AES,16,128,128,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */
+    {52,KEX_DH,SIG_NONE,ENC_AES,16,128,128,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DH_anon_WITH_AES_128_CBC_SHA */
+    {53,KEX_RSA,SIG_RSA,ENC_AES256,16,256,256,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_RSA_WITH_AES_256_CBC_SHA */
+    {54,KEX_DH,SIG_DSS,ENC_AES256,16,256,256,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DH_DSS_WITH_AES_256_CBC_SHA */
+    {55,KEX_DH,SIG_RSA,ENC_AES256,16,256,256,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DH_RSA_WITH_AES_256_CBC_SHA */
+    {56,KEX_DH,SIG_DSS,ENC_AES256,16,256,256,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DHE_DSS_WITH_AES_256_CBC_SHA */
+    {57,KEX_DH,SIG_RSA,ENC_AES256,16,256,256,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */
+    {58,KEX_DH,SIG_NONE,ENC_AES256,16,256,256,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DH_anon_WITH_AES_256_CBC_SHA */
+    {59,KEX_RSA,SIG_RSA,ENC_NULL,1,0,0,DIG_SHA256, SSL_CIPHER_MODE_STREAM},   /* TLS_RSA_WITH_NULL_SHA256 */
+    {60,KEX_RSA,SIG_RSA,ENC_AES,16,128,128,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_RSA_WITH_AES_128_CBC_SHA256 */
+    {61,KEX_RSA,SIG_RSA,ENC_AES256,16,256,256,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_RSA_WITH_AES_256_CBC_SHA256 */
+    {62,KEX_DH,SIG_DSS,ENC_AES,16,128,128,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_DH_DSS_WITH_AES_128_CBC_SHA256 */
+    {63,KEX_DH,SIG_RSA,ENC_AES,16,128,128,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_DH_RSA_WITH_AES_128_CBC_SHA256 */
+    {64,KEX_DH,SIG_DSS,ENC_AES,16,128,128,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_DHE_DSS_WITH_AES_128_CBC_SHA256 */
+    {65,KEX_RSA,SIG_RSA,ENC_CAMELLIA128,16,128,128,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */
+    {66,KEX_DH,SIG_DSS,ENC_CAMELLIA128,16,128,128,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA */
+    {67,KEX_DH,SIG_RSA,ENC_CAMELLIA128,16,128,128,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA */
+    {68,KEX_DH,SIG_DSS,ENC_CAMELLIA128,16,128,128,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA */
+    {69,KEX_DH,SIG_RSA,ENC_CAMELLIA128,16,128,128,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */
+    {70,KEX_DH,SIG_NONE,ENC_CAMELLIA128,16,128,128,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA */
+    {96,KEX_RSA,SIG_RSA,ENC_RC4,1,128,56,DIG_MD5, SSL_CIPHER_MODE_STREAM},
+    {97,KEX_RSA,SIG_RSA,ENC_RC2,1,128,56,DIG_MD5, SSL_CIPHER_MODE_STREAM},
+    {98,KEX_RSA,SIG_RSA,ENC_DES,8,64,56,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA */
+    {99,KEX_DH,SIG_DSS,ENC_DES,8,64,56,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA */
+    {100,KEX_RSA,SIG_RSA,ENC_RC4,1,128,56,DIG_SHA, SSL_CIPHER_MODE_STREAM},
+    {101,KEX_DH,SIG_DSS,ENC_RC4,1,128,56,DIG_SHA, SSL_CIPHER_MODE_STREAM},
+    {102,KEX_DH,SIG_DSS,ENC_RC4,1,128,128,DIG_SHA, SSL_CIPHER_MODE_STREAM},
+    {103,KEX_DH,SIG_RSA,ENC_AES,16,128,128,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */
+    {104,KEX_DH,SIG_DSS,ENC_AES256,16,256,256,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_DH_DSS_WITH_AES_256_CBC_SHA256 */
+    {105,KEX_DH,SIG_RSA,ENC_AES256,16,256,256,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_DH_RSA_WITH_AES_256_CBC_SHA256 */
+    {106,KEX_DH,SIG_DSS,ENC_AES256,16,256,256,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_DHE_DSS_WITH_AES_256_CBC_SHA256 */
+    {107,KEX_DH,SIG_RSA,ENC_AES256,16,256,256,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */
+    {108,KEX_DH,SIG_NONE,ENC_AES,16,128,128,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_DH_anon_WITH_AES_128_CBC_SHA256 */
+    {109,KEX_DH,SIG_NONE,ENC_AES256,16,256,256,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_DH_anon_WITH_AES_256_CBC_SHA256 */
+    {132,KEX_RSA,SIG_RSA,ENC_CAMELLIA256,16,256,256,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */
+    {133,KEX_DH,SIG_DSS,ENC_CAMELLIA256,16,256,256,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA */
+    {134,KEX_DH,SIG_RSA,ENC_CAMELLIA256,16,256,256,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA */
+    {135,KEX_DH,SIG_DSS,ENC_CAMELLIA256,16,256,256,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA */
+    {136,KEX_DH,SIG_RSA,ENC_CAMELLIA256,16,256,256,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */
+    {137,KEX_DH,SIG_NONE,ENC_CAMELLIA256,16,256,256,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA */
+    {139,KEX_PSK,SIG_RSA,ENC_3DES,8,192,192,DIG_SHA, SSL_CIPHER_MODE_CBC},
+    {140,KEX_PSK,SIG_RSA,ENC_AES,16,128,128,DIG_SHA, SSL_CIPHER_MODE_CBC},
+    {141,KEX_PSK,SIG_RSA,ENC_AES256,16,256,256,DIG_SHA, SSL_CIPHER_MODE_CBC},
+    {150,KEX_RSA,SIG_RSA,ENC_SEED,16,128,128,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_RSA_WITH_SEED_CBC_SHA */
+    {151,KEX_DH,SIG_DSS,ENC_SEED,16,128,128,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DH_DSS_WITH_SEED_CBC_SHA */
+    {152,KEX_DH,SIG_RSA,ENC_SEED,16,128,128,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DH_RSA_WITH_SEED_CBC_SHA */
+    {153,KEX_DH,SIG_DSS,ENC_SEED,16,128,128,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DHE_DSS_WITH_SEED_CBC_SHA */
+    {154,KEX_DH,SIG_RSA,ENC_SEED,16,128,128,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DHE_RSA_WITH_SEED_CBC_SHA */
+    {155,KEX_DH,SIG_NONE,ENC_SEED,16,128,128,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_DH_anon_WITH_SEED_CBC_SHA */
+    {156,KEX_RSA,SIG_RSA,ENC_AES,4,128,128,DIG_SHA256, SSL_CIPHER_MODE_GCM},   /* TLS_RSA_WITH_AES_128_GCM_SHA256 */
+    {157,KEX_RSA,SIG_RSA,ENC_AES256,4,256,256,DIG_SHA384, SSL_CIPHER_MODE_GCM},   /* TLS_RSA_WITH_AES_256_GCM_SHA384 */
+    {158,KEX_DH,SIG_RSA,ENC_AES,4,128,128,DIG_SHA256, SSL_CIPHER_MODE_GCM},   /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */
+    {159,KEX_DH,SIG_RSA,ENC_AES256,4,256,256,DIG_SHA384, SSL_CIPHER_MODE_GCM},   /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */
+    {160,KEX_DH,SIG_RSA,ENC_AES,4,128,128,DIG_SHA256, SSL_CIPHER_MODE_GCM},   /* TLS_DH_RSA_WITH_AES_128_GCM_SHA256 */
+    {161,KEX_DH,SIG_RSA,ENC_AES256,4,256,256,DIG_SHA384, SSL_CIPHER_MODE_GCM},   /* TLS_DH_RSA_WITH_AES_256_GCM_SHA384 */
+    {162,KEX_DH,SIG_DSS,ENC_AES,4,128,128,DIG_SHA256, SSL_CIPHER_MODE_GCM},   /* TLS_DHE_DSS_WITH_AES_128_GCM_SHA256 */
+    {163,KEX_DH,SIG_DSS,ENC_AES256,4,256,256,DIG_SHA384, SSL_CIPHER_MODE_GCM},   /* TLS_DHE_DSS_WITH_AES_256_GCM_SHA384 */
+    {164,KEX_DH,SIG_DSS,ENC_AES,4,128,128,DIG_SHA256, SSL_CIPHER_MODE_GCM},   /* TLS_DH_DSS_WITH_AES_128_GCM_SHA256 */
+    {165,KEX_DH,SIG_DSS,ENC_AES256,4,256,256,DIG_SHA384, SSL_CIPHER_MODE_GCM},   /* TLS_DH_DSS_WITH_AES_256_GCM_SHA384 */
+    {166,KEX_DH,SIG_NONE,ENC_AES,4,128,128,DIG_SHA256, SSL_CIPHER_MODE_GCM},   /* TLS_DH_anon_WITH_AES_128_GCM_SHA256 */
+    {167,KEX_DH,SIG_NONE,ENC_AES256,4,256,256,DIG_SHA384, SSL_CIPHER_MODE_GCM},   /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */
+    {186,KEX_RSA,SIG_RSA,ENC_CAMELLIA128,16,128,128,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */
+    {187,KEX_DH,SIG_DSS,ENC_CAMELLIA128,16,128,128,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256 */
+    {188,KEX_DH,SIG_RSA,ENC_CAMELLIA128,16,128,128,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256 */
+    {189,KEX_DH,SIG_DSS,ENC_CAMELLIA128,16,128,128,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256 */
+    {190,KEX_DH,SIG_RSA,ENC_CAMELLIA128,16,128,128,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */
+    {191,KEX_DH,SIG_NONE,ENC_CAMELLIA128,16,128,128,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA256 */
+    {192,KEX_RSA,SIG_RSA,ENC_CAMELLIA256,16,256,256,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */
+    {193,KEX_DH,SIG_DSS,ENC_CAMELLIA256,16,256,256,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256 */
+    {194,KEX_DH,SIG_RSA,ENC_CAMELLIA256,16,256,256,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256 */
+    {195,KEX_DH,SIG_DSS,ENC_CAMELLIA256,16,256,256,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256 */
+    {196,KEX_DH,SIG_RSA,ENC_CAMELLIA256,16,256,256,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */
+    {197,KEX_DH,SIG_NONE,ENC_CAMELLIA256,16,256,256,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA256 */
+    {49153,KEX_DH,SIG_DSS,ENC_NULL,1,0,0,DIG_SHA, SSL_CIPHER_MODE_STREAM},   /* TLS_ECDH_ECDSA_WITH_NULL_SHA */
+    {49154,KEX_DH,SIG_DSS,ENC_RC4,1,128,128,DIG_SHA, SSL_CIPHER_MODE_STREAM},   /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */
+    {49155,KEX_DH,SIG_DSS,ENC_3DES,8,192,192,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */
+    {49156,KEX_DH,SIG_DSS,ENC_AES,16,128,128,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */
+    {49157,KEX_DH,SIG_DSS,ENC_AES256,16,256,256,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */
+    {49158,KEX_DH,SIG_DSS,ENC_NULL,1,0,0,DIG_SHA, SSL_CIPHER_MODE_STREAM},   /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */
+    {49159,KEX_DH,SIG_DSS,ENC_RC4,1,128,128,DIG_SHA, SSL_CIPHER_MODE_STREAM},   /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */
+    {49160,KEX_DH,SIG_DSS,ENC_3DES,8,192,192,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */
+    {49161,KEX_DH,SIG_DSS,ENC_AES,16,128,128,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */
+    {49162,KEX_DH,SIG_DSS,ENC_AES256,16,256,256,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */
+    {49163,KEX_DH,SIG_RSA,ENC_NULL,1,0,0,DIG_SHA, SSL_CIPHER_MODE_STREAM},   /* TLS_ECDH_RSA_WITH_NULL_SHA */
+    {49164,KEX_DH,SIG_RSA,ENC_RC4,1,128,128,DIG_SHA, SSL_CIPHER_MODE_STREAM},   /* TLS_ECDH_RSA_WITH_RC4_128_SHA */
+    {49165,KEX_DH,SIG_RSA,ENC_3DES,8,192,192,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */
+    {49166,KEX_DH,SIG_RSA,ENC_AES,16,128,128,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */
+    {49167,KEX_DH,SIG_RSA,ENC_AES256,16,256,256,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */
+    {49168,KEX_DH,SIG_RSA,ENC_NULL,1,0,0,DIG_SHA, SSL_CIPHER_MODE_STREAM},   /* TLS_ECDHE_RSA_WITH_NULL_SHA */
+    {49169,KEX_DH,SIG_RSA,ENC_RC4,1,128,128,DIG_SHA, SSL_CIPHER_MODE_STREAM},   /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */
+    {49170,KEX_DH,SIG_RSA,ENC_3DES,8,192,192,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */
+    {49171,KEX_DH,SIG_RSA,ENC_AES,16,128,128,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */
+    {49172,KEX_DH,SIG_RSA,ENC_AES256,16,256,256,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */
+    {49173,KEX_DH,SIG_NONE,ENC_NULL,1,0,0,DIG_SHA, SSL_CIPHER_MODE_STREAM},   /* TLS_ECDH_anon_WITH_NULL_SHA */
+    {49174,KEX_DH,SIG_NONE,ENC_RC4,1,128,128,DIG_SHA, SSL_CIPHER_MODE_STREAM},   /* TLS_ECDH_anon_WITH_RC4_128_SHA */
+    {49175,KEX_DH,SIG_NONE,ENC_3DES,8,192,192,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA */
+    {49176,KEX_DH,SIG_NONE,ENC_AES,16,128,128,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_ECDH_anon_WITH_AES_128_CBC_SHA */
+    {49177,KEX_DH,SIG_NONE,ENC_AES256,16,256,256,DIG_SHA, SSL_CIPHER_MODE_CBC},   /* TLS_ECDH_anon_WITH_AES_256_CBC_SHA */
+    {49187,KEX_DH,SIG_DSS,ENC_AES,16,128,128,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 */
+    {49188,KEX_DH,SIG_DSS,ENC_AES256,16,256,256,DIG_SHA384, SSL_CIPHER_MODE_CBC},   /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 */
+    {49189,KEX_DH,SIG_DSS,ENC_AES,16,128,128,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */
+    {49190,KEX_DH,SIG_DSS,ENC_AES256,16,256,256,DIG_SHA384, SSL_CIPHER_MODE_CBC},   /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */
+    {49191,KEX_DH,SIG_RSA,ENC_AES,16,128,128,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */
+    {49192,KEX_DH,SIG_RSA,ENC_AES256,16,256,256,DIG_SHA384, SSL_CIPHER_MODE_CBC},   /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */
+    {49193,KEX_DH,SIG_RSA,ENC_AES,16,128,128,DIG_SHA256, SSL_CIPHER_MODE_CBC},   /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */
+    {49194,KEX_DH,SIG_RSA,ENC_AES256,16,256,256,DIG_SHA384, SSL_CIPHER_MODE_CBC},   /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */
+    {49195,KEX_DH,SIG_DSS,ENC_AES,4,128,128,DIG_SHA256, SSL_CIPHER_MODE_GCM},   /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */
+    {49196,KEX_DH,SIG_DSS,ENC_AES256,4,256,256,DIG_SHA384, SSL_CIPHER_MODE_GCM},   /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */
+    {49197,KEX_DH,SIG_DSS,ENC_AES,4,128,128,DIG_SHA256, SSL_CIPHER_MODE_GCM},   /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */
+    {49198,KEX_DH,SIG_DSS,ENC_AES256,4,256,256,DIG_SHA384, SSL_CIPHER_MODE_GCM},   /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */
+    {49199,KEX_DH,SIG_RSA,ENC_AES,4,128,128,DIG_SHA256, SSL_CIPHER_MODE_GCM},   /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */
+    {49200,KEX_DH,SIG_RSA,ENC_AES256,4,256,256,DIG_SHA384, SSL_CIPHER_MODE_GCM},   /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */
+    {49201,KEX_DH,SIG_RSA,ENC_AES,4,128,128,DIG_SHA256, SSL_CIPHER_MODE_GCM},   /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */
+    {49202,KEX_DH,SIG_RSA,ENC_AES256,4,256,256,DIG_SHA384, SSL_CIPHER_MODE_GCM},   /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */
+    {-1, 0,0,0,0,0,0,0, 0}
 };
 
 #define MAX_BLOCK_SIZE 16
@@ -1423,15 +1932,15 @@ ssl_find_cipher(int num,SslCipherSuite* cs)
 static gint
 tls_hash(StringInfo* secret, StringInfo* seed, gint md, StringInfo* out)
 {
-    guint8 *ptr;
-    guint left;
-    gint tocpy;
-    guint8 *A;
-    guint8 _A[20],tmp[20];
-    guint A_l,tmp_l;
-    SSL_HMAC hm;
-    ptr=out->data;
-    left=out->data_len;
+    guint8   *ptr;
+    guint     left;
+    gint      tocpy;
+    guint8   *A;
+    guint8    _A[48],tmp[48];
+    guint     A_l,tmp_l;
+    SSL_HMAC  hm;
+    ptr  = out->data;
+    left = out->data_len;
 
 
     ssl_print_string("tls_hash: hash secret", secret);
@@ -1466,12 +1975,12 @@ static gint
 tls_prf(StringInfo* secret, const gchar *usage,
         StringInfo* rnd1, StringInfo* rnd2, StringInfo* out)
 {
-    StringInfo seed, sha_out, md5_out;
-    guint8 *ptr;
-    StringInfo s1, s2;
-    guint i,s_l, r;
-    size_t usage_len;
-    r=-1;
+    StringInfo  seed, sha_out, md5_out;
+    guint8     *ptr;
+    StringInfo  s1, s2;
+    guint       i,s_l, r;
+    size_t      usage_len;
+    r         = -1;
     usage_len = strlen(usage);
 
     /* initalize buffer for sha, md5 random seed*/
@@ -1490,9 +1999,12 @@ tls_prf(StringInfo* secret, const gchar *usage,
     }
 
     ptr=seed.data;
-    memcpy(ptr,usage,usage_len); ptr+=usage_len;
-    memcpy(ptr,rnd1->data,rnd1->data_len); ptr+=rnd1->data_len;
-    memcpy(ptr,rnd2->data,rnd2->data_len); ptr+=rnd2->data_len;
+    memcpy(ptr,usage,usage_len);
+    ptr+=usage_len;
+    memcpy(ptr,rnd1->data,rnd1->data_len);
+    ptr+=rnd1->data_len;
+    memcpy(ptr,rnd2->data,rnd2->data_len);
+    /*ptr+=rnd2->data_len;*/
 
     /* initalize buffer for client/server seeds*/
     s_l=secret->data_len/2 + secret->data_len%2;
@@ -1533,12 +2045,36 @@ free_sha:
     return r;
 }
 
+static gint
+tls12_prf(gint md, StringInfo* secret, const gchar* usage, StringInfo* rnd1, StringInfo* rnd2, StringInfo* out)
+{
+    StringInfo label_seed;
+    size_t     usage_len;
+
+    usage_len = strlen(usage);
+    if (ssl_data_alloc(&label_seed, usage_len+rnd1->data_len+rnd2->data_len) < 0) {
+        ssl_debug_printf("tls12_prf: can't allocate label_seed\n");
+        return -1;
+    }
+    memcpy(label_seed.data, usage, usage_len);
+    memcpy(label_seed.data+usage_len, rnd1->data, rnd1->data_len);
+    memcpy(label_seed.data+usage_len+rnd1->data_len, rnd2->data, rnd2->data_len);
+
+    ssl_debug_printf("tls12_prf: tls_hash(hash_alg %s secret_len %d seed_len %d )\n", gcry_md_algo_name(md), secret->data_len, label_seed.data_len);
+    if (tls_hash(secret, &label_seed, md, out) != 0){
+        g_free(label_seed.data);
+        return -1;
+    }
+    ssl_print_string("PRF out", out);
+    return 0;
+}
+
 static gint
 ssl3_generate_export_iv(StringInfo* r1,
         StringInfo* r2, StringInfo* out)
 {
     SSL_MD5_CTX md5;
-    guint8 tmp[16];
+    guint8      tmp[16];
 
     ssl_md5_init(&md5);
     ssl_md5_update(&md5,r1->data,r1->data_len);
@@ -1557,12 +2093,12 @@ ssl3_prf(StringInfo* secret, const gchar* usage,
         StringInfo* r1,
         StringInfo* r2,StringInfo* out)
 {
-    SSL_MD5_CTX md5;
-    SSL_SHA_CTX sha;
-    StringInfo *rnd1,*rnd2;
-    guint off;
-    gint i=0,j;
-    guint8 buf[20];
+    SSL_MD5_CTX  md5;
+    SSL_SHA_CTX  sha;
+    StringInfo  *rnd1,*rnd2;
+    guint        off;
+    gint         i = 0,j;
+    guint8       buf[20];
 
     rnd1=r1; rnd2=r2;
 
@@ -1608,15 +2144,33 @@ ssl3_prf(StringInfo* secret, const gchar* usage,
     return(0);
 }
 
+static gint prf(SslDecryptSession* ssl,StringInfo* secret,const gchar* usage,StringInfo* rnd1,StringInfo* rnd2,StringInfo* out)
+{
+    gint ret;
+    if (ssl->version_netorder==SSLV3_VERSION){
+        ret = ssl3_prf(secret,usage,rnd1,rnd2,out);
+    }else if (ssl->version_netorder==TLSV1_VERSION || ssl->version_netorder==TLSV1DOT1_VERSION ||
+            ssl->version_netorder==DTLSV1DOT0_VERSION || ssl->version_netorder==DTLSV1DOT0_VERSION_NOT){
+        ret = tls_prf(secret,usage,rnd1,rnd2,out);
+    }else{
+        if (ssl->cipher_suite.dig == DIG_SHA384){
+            ret = tls12_prf(GCRY_MD_SHA384, secret, usage, rnd1, rnd2, out);
+        }else{
+            ret = tls12_prf(GCRY_MD_SHA256, secret, usage, rnd1, rnd2, out);
+        }
+    }
+    return ret;
+}
+
 static SslFlow*
 ssl_create_flow(void)
 {
   SslFlow *flow;
 
-  flow = se_alloc(sizeof(SslFlow));
+  flow = (SslFlow *)wmem_alloc(wmem_file_scope(), sizeof(SslFlow));
   flow->byte_seq = 0;
   flow->flags = 0;
-  flow->multisegment_pdus = se_tree_create_non_persistent(EMEM_TREE_TYPE_RED_BLACK, "ssl_multisegment_pdus");
+  flow->multisegment_pdus = wmem_tree_new(wmem_file_scope());
   return flow;
 }
 
@@ -1642,7 +2196,7 @@ ssl_create_decompressor(gint compression)
 
     if (compression == 0) return NULL;
     ssl_debug_printf("ssl_create_decompressor: compression method %d\n", compression);
-    decomp = se_alloc(sizeof(SslDecompress));
+    decomp = (SslDecompress *)wmem_alloc(wmem_file_scope(), sizeof(SslDecompress));
     decomp->compression = compression;
     switch (decomp->compression) {
 #ifdef HAVE_LIBZ
@@ -1673,10 +2227,9 @@ ssl_create_decoder(SslCipherSuite *cipher_suite, gint compression,
         guint8 *mk, guint8 *sk, guint8 *iv)
 {
     SslDecoder *dec;
-    gint ciph;
-    ciph=0;
+    gint        ciph;
 
-    dec = se_alloc0(sizeof(SslDecoder));
+    dec = (SslDecoder *)wmem_alloc0(wmem_file_scope(), sizeof(SslDecoder));
     /* Find the SSLeay cipher */
     if(cipher_suite->enc!=ENC_NULL) {
         ssl_debug_printf("ssl_create_decoder CIPHER: %s\n", ciphers[cipher_suite->enc-0x30]);
@@ -1687,7 +2240,7 @@ ssl_create_decoder(SslCipherSuite *cipher_suite, gint compression,
     }
     if (ciph == 0) {
         ssl_debug_printf("ssl_create_decoder can't find cipher %s\n",
-            ciphers[(cipher_suite->enc-0x30) > 7 ? 7 : (cipher_suite->enc-0x30)]);
+            ciphers[cipher_suite->enc > ENC_NULL ? ENC_NULL : (cipher_suite->enc-0x30)]);
         return NULL;
     }
 
@@ -1695,8 +2248,14 @@ ssl_create_decoder(SslCipherSuite *cipher_suite, gint compression,
      memory allocation and waste samo more memory*/
     dec->cipher_suite=cipher_suite;
     dec->compression = compression;
-    dec->mac_key.data = dec->_mac_key;
-    ssl_data_set(&dec->mac_key, mk, cipher_suite->dig_len);
+    /* AEED ciphers don't have a MAC but need to keep the write IV instead */
+    if (mk == NULL) {
+        dec->write_iv.data = dec->_mac_key_or_write_iv;
+        ssl_data_set(&dec->write_iv, iv, cipher_suite->block);
+    } else {
+        dec->mac_key.data = dec->_mac_key_or_write_iv;
+        ssl_data_set(&dec->mac_key, mk, ssl_cipher_suite_dig(cipher_suite)->len);
+    }
     dec->seq = 0;
     dec->decomp = ssl_create_decompressor(compression);
     dec->flow = ssl_create_flow();
@@ -1710,18 +2269,18 @@ ssl_create_decoder(SslCipherSuite *cipher_suite, gint compression,
         return NULL;
     }
 
-    ssl_debug_printf("decoder initialized (digest len %d)\n", cipher_suite->dig_len);
+    ssl_debug_printf("decoder initialized (digest len %d)\n", ssl_cipher_suite_dig(cipher_suite)->len);
     return dec;
 }
 
 int
 ssl_generate_keyring_material(SslDecryptSession*ssl_session)
 {
-    StringInfo key_block;
-    guint8 _iv_c[MAX_BLOCK_SIZE],_iv_s[MAX_BLOCK_SIZE];
-    guint8 _key_c[MAX_KEY_SIZE],_key_s[MAX_KEY_SIZE];
-    gint needed;
-    guint8 *ptr,*c_wk,*s_wk,*c_mk,*s_mk,*c_iv = _iv_c,*s_iv = _iv_s;
+    StringInfo  key_block;
+    guint8      _iv_c[MAX_BLOCK_SIZE],_iv_s[MAX_BLOCK_SIZE];
+    guint8      _key_c[MAX_KEY_SIZE],_key_s[MAX_KEY_SIZE];
+    gint        needed;
+    guint8     *ptr,*c_wk,*s_wk,*c_mk,*s_mk,*c_iv = _iv_c,*s_iv = _iv_s;
 
     /* check for enough info to proced */
     guint need_all = SSL_CIPHER|SSL_CLIENT_RANDOM|SSL_SERVER_RANDOM|SSL_VERSION;
@@ -1739,7 +2298,7 @@ ssl_generate_keyring_material(SslDecryptSession*ssl_session)
         ssl_print_string("pre master secret",&ssl_session->pre_master_secret);
         ssl_print_string("client random",&ssl_session->client_random);
         ssl_print_string("server random",&ssl_session->server_random);
-        if (PRF(ssl_session,&ssl_session->pre_master_secret,"master secret",
+        if (prf(ssl_session,&ssl_session->pre_master_secret,"master secret",
                 &ssl_session->client_random,
                 &ssl_session->server_random, &ssl_session->master_secret)) {
             ssl_debug_printf("ssl_generate_keyring_material can't generate master_secret\n");
@@ -1753,19 +2312,15 @@ ssl_generate_keyring_material(SslDecryptSession*ssl_session)
     }
 
     /* Compute the key block. First figure out how much data we need*/
-    needed=ssl_session->cipher_suite.dig_len*2;
+    needed=ssl_cipher_suite_dig(&ssl_session->cipher_suite)->len*2;
     needed+=ssl_session->cipher_suite.bits / 4;
     if(ssl_session->cipher_suite.block>1)
         needed+=ssl_session->cipher_suite.block*2;
 
     key_block.data_len = needed;
-    key_block.data = g_malloc(needed);
-    if (!key_block.data) {
-        ssl_debug_printf("ssl_generate_keyring_material can't allocate key_block (len %d)\n", needed);
-        return -1;
-    }
+    key_block.data = (guchar *)g_malloc(needed);
     ssl_debug_printf("ssl_generate_keyring_material sess key generation\n");
-    if (PRF(ssl_session,&ssl_session->master_secret,"key expansion",
+    if (prf(ssl_session,&ssl_session->master_secret,"key expansion",
             &ssl_session->server_random,&ssl_session->client_random,
             &key_block)) {
         ssl_debug_printf("ssl_generate_keyring_material can't generate key_block\n");
@@ -1774,18 +2329,24 @@ ssl_generate_keyring_material(SslDecryptSession*ssl_session)
     ssl_print_string("key expansion", &key_block);
 
     ptr=key_block.data;
-    c_mk=ptr; ptr+=ssl_session->cipher_suite.dig_len;
-    s_mk=ptr; ptr+=ssl_session->cipher_suite.dig_len;
+    /* AEAD ciphers do not have a separate MAC */
+    if (ssl_session->cipher_suite.mode == SSL_CIPHER_MODE_GCM) {
+        c_mk = s_mk = NULL;
+    } else {
+        c_mk=ptr; ptr+=ssl_cipher_suite_dig(&ssl_session->cipher_suite)->len;
+        s_mk=ptr; ptr+=ssl_cipher_suite_dig(&ssl_session->cipher_suite)->len;
+    }
 
     c_wk=ptr; ptr+=ssl_session->cipher_suite.eff_bits/8;
     s_wk=ptr; ptr+=ssl_session->cipher_suite.eff_bits/8;
 
     if(ssl_session->cipher_suite.block>1){
         c_iv=ptr; ptr+=ssl_session->cipher_suite.block;
-        s_iv=ptr; ptr+=ssl_session->cipher_suite.block;
+        s_iv=ptr; /*ptr+=ssl_session->cipher_suite.block;*/
     }
 
-    if(ssl_session->cipher_suite.export){
+    /* export ciphers work with a smaller key length */
+    if (ssl_session->cipher_suite.eff_bits < ssl_session->cipher_suite.bits) {
         StringInfo iv_c,iv_s;
         StringInfo key_c,key_s;
         StringInfo k;
@@ -1832,7 +2393,7 @@ ssl_generate_keyring_material(SslDecryptSession*ssl_session)
                 iv_block.data_len = ssl_session->cipher_suite.block*2;
 
                 ssl_debug_printf("ssl_generate_keyring_material prf(iv_block)\n");
-                if(PRF(ssl_session,&key_null, "IV block",
+                if(prf(ssl_session,&key_null, "IV block",
                         &ssl_session->client_random,
                         &ssl_session->server_random,&iv_block)) {
                     ssl_debug_printf("ssl_generate_keyring_material can't generate tls31 iv block\n");
@@ -1883,7 +2444,7 @@ ssl_generate_keyring_material(SslDecryptSession*ssl_session)
             k.data = c_wk;
             k.data_len = ssl_session->cipher_suite.eff_bits/8;
             ssl_debug_printf("ssl_generate_keyring_material PRF(key_c)\n");
-            if (PRF(ssl_session,&k,"client write key",
+            if (prf(ssl_session,&k,"client write key",
                     &ssl_session->client_random,
                     &ssl_session->server_random, &key_c)) {
                 ssl_debug_printf("ssl_generate_keyring_material can't generate tll31 server key \n");
@@ -1894,7 +2455,7 @@ ssl_generate_keyring_material(SslDecryptSession*ssl_session)
             k.data = s_wk;
             k.data_len = ssl_session->cipher_suite.eff_bits/8;
             ssl_debug_printf("ssl_generate_keyring_material PRF(key_s)\n");
-            if(PRF(ssl_session,&k,"server write key",
+            if(prf(ssl_session,&k,"server write key",
                     &ssl_session->client_random,
                     &ssl_session->server_random, &key_s)) {
                 ssl_debug_printf("ssl_generate_keyring_material can't generate tll31 client key \n");
@@ -1905,8 +2466,10 @@ ssl_generate_keyring_material(SslDecryptSession*ssl_session)
     }
 
     /* show key material info */
-    ssl_print_data("Client MAC key",c_mk,ssl_session->cipher_suite.dig_len);
-    ssl_print_data("Server MAC key",s_mk,ssl_session->cipher_suite.dig_len);
+    if (c_mk != NULL) {
+        ssl_print_data("Client MAC key",c_mk,ssl_cipher_suite_dig(&ssl_session->cipher_suite)->len);
+        ssl_print_data("Server MAC key",s_mk,ssl_cipher_suite_dig(&ssl_session->cipher_suite)->len);
+    }
     ssl_print_data("Client Write key",c_wk,ssl_session->cipher_suite.bits/8);
     ssl_print_data("Server Write key",s_wk,ssl_session->cipher_suite.bits/8);
 
@@ -1963,6 +2526,9 @@ ssl_decrypt_pre_master_secret(SslDecryptSession*ssl_session,
 {
     gint i;
 
+    if (!encrypted_pre_master)
+        return -1;
+
     if(ssl_session->cipher_suite.kex == KEX_DH) {
         ssl_debug_printf("ssl_decrypt_pre_master_secret session uses DH (%d) key exchange, which is impossible to decrypt\n",
             KEX_DH);
@@ -2016,17 +2582,17 @@ tls_check_mac(SslDecoder*decoder, gint ct, gint ver, guint8* data,
         guint32 datalen, guint8* mac)
 {
     SSL_HMAC hm;
-    gint md;
-    guint32 len;
-    guint8 buf[20];
-    gint16 temp;
+    gint     md;
+    guint32  len;
+    guint8   buf[48];
+    gint16   temp;
 
-    md=ssl_get_digest_by_name(digests[decoder->cipher_suite->dig-0x40]);
+    md=ssl_get_digest_by_name(ssl_cipher_suite_dig(decoder->cipher_suite)->name);
     ssl_debug_printf("tls_check_mac mac type:%s md %d\n",
-        digests[decoder->cipher_suite->dig-0x40], md);
+        ssl_cipher_suite_dig(decoder->cipher_suite)->name, md);
 
     if (ssl_hmac_init(&hm,decoder->mac_key.data,decoder->mac_key.data_len,md) != 0)
-        return -1;;
+        return -1;
 
     /* hash sequence number */
     fmt_seq(decoder->seq,buf);
@@ -2065,17 +2631,17 @@ int
 ssl3_check_mac(SslDecoder*decoder,int ct,guint8* data,
         guint32 datalen, guint8* mac)
 {
-    SSL_MD mc;
-    gint md;
+    SSL_MD  mc;
+    gint    md;
     guint32 len;
-    guint8 buf[64],dgst[20];
-    gint pad_ct;
-    gint16 temp;
+    guint8  buf[64],dgst[20];
+    gint    pad_ct;
+    gint16  temp;
 
     pad_ct=(decoder->cipher_suite->dig==DIG_SHA)?40:48;
 
     /* get cipher used for digest comptuation */
-    md=ssl_get_digest_by_name(digests[decoder->cipher_suite->dig-0x40]);
+    md=ssl_get_digest_by_name(ssl_cipher_suite_dig(decoder->cipher_suite)->name);
     if (ssl_md_init(&mc,md) !=0)
         return -1;
 
@@ -2125,19 +2691,19 @@ ssl3_check_mac(SslDecoder*decoder,int ct,guint8* data,
     return(0);
 }
 
-#if 0
 static gint
 dtls_check_mac(SslDecoder*decoder, gint ct,int ver, guint8* data,
         guint32 datalen, guint8* mac)
 {
     SSL_HMAC hm;
-    gint md;
-    guint32 len;
-    guint8 buf[20];
-    guint32 netnum;
-    md=ssl_get_digest_by_name(digests[decoder->cipher_suite->dig-0x40]);
+    gint     md;
+    guint32  len;
+    guint8   buf[20];
+    gint16   temp;
+
+    md=ssl_get_digest_by_name(ssl_cipher_suite_dig(decoder->cipher_suite)->name);
     ssl_debug_printf("dtls_check_mac mac type:%s md %d\n",
-        digests[decoder->cipher_suite->dig-0x40], md);
+        ssl_cipher_suite_dig(decoder->cipher_suite)->name, md);
 
     if (ssl_hmac_init(&hm,decoder->mac_key.data,decoder->mac_key.data_len,md) != 0)
         return -1;
@@ -2145,7 +2711,7 @@ dtls_check_mac(SslDecoder*decoder, gint ct,int ver, guint8* data,
     /* hash sequence number */
     fmt_seq(decoder->seq,buf);
     buf[0]=decoder->epoch>>8;
-    buf[1]=decoder->epoch;
+    buf[1]=(guint8)decoder->epoch;
 
     ssl_hmac_update(&hm,buf,8);
 
@@ -2154,10 +2720,12 @@ dtls_check_mac(SslDecoder*decoder, gint ct,int ver, guint8* data,
     ssl_hmac_update(&hm,buf,1);
 
     /* hash version,data length and data */
-    *((gint16*)buf) = g_htons(ver);
+    temp = g_htons(ver);
+    memcpy(buf, &temp, 2);
     ssl_hmac_update(&hm,buf,2);
 
-    *((gint16*)buf) = g_htons(datalen);
+    temp = g_htons(datalen);
+    memcpy(buf, &temp, 2);
     ssl_hmac_update(&hm,buf,2);
     ssl_hmac_update(&hm,data,datalen);
     /* get digest and digest len */
@@ -2169,7 +2737,6 @@ dtls_check_mac(SslDecoder*decoder, gint ct,int ver, guint8* data,
 
     return(0);
 }
-#endif
 
 #ifdef HAVE_LIBZ
 int
@@ -2214,7 +2781,7 @@ int
 ssl_decrypt_record(SslDecryptSession*ssl,SslDecoder* decoder, gint ct,
         const guchar* in, guint inl, StringInfo* comp_str, StringInfo* out_str, guint* outl)
 {
-    guint pad, worklen, uncomplen;
+    guint   pad, worklen, uncomplen;
     guint8 *mac;
 
     ssl_debug_printf("ssl_decrypt_record ciphertext len %d\n", inl);
@@ -2228,6 +2795,66 @@ ssl_decrypt_record(SslDecryptSession*ssl,SslDecoder* decoder, gint ct,
         ssl_data_realloc(out_str, inl + 32);
     }
 
+    /* RFC 6101/2246: SSLCipherText/TLSCipherText has two structures for types:
+     * (notation: { unencrypted, [ encrypted ] })
+     * GenericStreamCipher: { [content, mac] }
+     * GenericBlockCipher: { IV (TLS 1.1+), [content, mac, padding, padding_len] }
+     * RFC 5426 (TLS 1.2): TLSCipherText has additionally:
+     * GenericAEADCipher: { nonce_explicit, [content] }
+     * RFC 4347 (DTLS): based on TLS 1.1, only GenericBlockCipher is supported.
+     * RFC 6347 (DTLS 1.2): based on TLS 1.2, includes GenericAEADCipher too.
+     */
+
+    /* (TLS 1.1 and later, DTLS) Extract explicit IV for GenericBlockCipher */
+    if (decoder->cipher_suite->mode == SSL_CIPHER_MODE_CBC) {
+        switch (ssl->version_netorder) {
+        case TLSV1DOT1_VERSION:
+        case TLSV1DOT2_VERSION:
+        case DTLSV1DOT0_VERSION:
+        case DTLSV1DOT2_VERSION:
+        case DTLSV1DOT0_VERSION_NOT:
+            if ((gint)inl < decoder->cipher_suite->block) {
+                ssl_debug_printf("ssl_decrypt_record failed: input %d has no space for IV %d\n",
+                        inl, decoder->cipher_suite->block);
+                return -1;
+            }
+            pad = gcry_cipher_setiv(decoder->evp, in, decoder->cipher_suite->block);
+            if (pad != 0) {
+                ssl_debug_printf("ssl_decrypt_record failed: failed to set IV: %s %s\n",
+                        gcry_strsource (pad), gcry_strerror (pad));
+            }
+
+            inl -= decoder->cipher_suite->block;
+            in += decoder->cipher_suite->block;
+            break;
+        }
+    }
+
+    /* Nonce for GenericAEADCipher */
+    if (decoder->cipher_suite->mode == SSL_CIPHER_MODE_GCM) {
+        /* 4 bytes write_iv, 8 bytes explicit_nonce, 4 bytes counter */
+        guchar gcm_nonce[16] = { 0 };
+
+        if ((gint)inl < SSL_EX_NONCE_LEN_GCM) {
+            ssl_debug_printf("ssl_decrypt_record failed: input %d has no space for nonce %d\n",
+                inl, SSL_EX_NONCE_LEN_GCM);
+            return -1;
+        }
+        memcpy(gcm_nonce, decoder->write_iv.data, decoder->write_iv.data_len); /* salt */
+        memcpy(gcm_nonce + decoder->write_iv.data_len, in, SSL_EX_NONCE_LEN_GCM);
+        /* NIST SP 800-38D, sect. 7.2 says that the 32-bit counter part starts
+         * at 1, and gets incremented before passing to the block cipher. */
+        gcm_nonce[4 + SSL_EX_NONCE_LEN_GCM + 3] = 2;
+        pad = gcry_cipher_setctr (decoder->evp, gcm_nonce, sizeof (gcm_nonce));
+        if (pad != 0) {
+            ssl_debug_printf("ssl_decrypt_record failed: failed to set CTR: %s %s\n",
+                    gcry_strsource (pad), gcry_strerror (pad));
+            return -1;
+        }
+        inl -= SSL_EX_NONCE_LEN_GCM;
+        in += SSL_EX_NONCE_LEN_GCM;
+    }
+
     /* First decrypt*/
     if ((pad = ssl_cipher_decrypt(&decoder->evp, out_str->data, out_str->data_len, in, inl))!= 0) {
         ssl_debug_printf("ssl_decrypt_record failed: ssl_cipher_decrypt: %s %s\n", gcry_strsource (pad),
@@ -2238,96 +2865,123 @@ ssl_decrypt_record(SslDecryptSession*ssl,SslDecoder* decoder, gint ct,
     ssl_print_data("Plaintext", out_str->data, inl);
     worklen=inl;
 
-    /* Now strip off the padding*/
-    if(decoder->cipher_suite->block!=1) {
+    /* RFC 5116 sect 5.1/5.3: AES128/256 GCM/CCM uses 16 bytes for auth tag */
+    if (decoder->cipher_suite->mode == SSL_CIPHER_MODE_GCM) {
+        if (worklen < 16) {
+            ssl_debug_printf("ssl_decrypt_record failed: missing tag, work %d\n", worklen);
+            return -1;
+        }
+        /* XXX - validate auth tag */
+        worklen -= 16;
+    }
+
+    /* strip padding for GenericBlockCipher */
+    if (decoder->cipher_suite->mode == SSL_CIPHER_MODE_CBC) {
         pad=out_str->data[inl-1];
+        if (worklen <= pad) {
+            ssl_debug_printf("ssl_decrypt_record failed: padding %d too large for work %d\n",
+                pad, worklen);
+            return -1;
+        }
         worklen-=(pad+1);
         ssl_debug_printf("ssl_decrypt_record found padding %d final len %d\n",
             pad, worklen);
     }
 
-    /* And the MAC */
-    if (decoder->cipher_suite->dig_len > (gint)worklen)
-    {
-        ssl_debug_printf("ssl_decrypt_record wrong record len/padding outlen %d\n work %d\n",*outl, worklen);
-        return -1;
+    /* MAC for GenericStreamCipher and GenericBlockCipher */
+    if (decoder->cipher_suite->mode == SSL_CIPHER_MODE_STREAM ||
+        decoder->cipher_suite->mode == SSL_CIPHER_MODE_CBC) {
+        if (ssl_cipher_suite_dig(decoder->cipher_suite)->len > (gint)worklen) {
+            ssl_debug_printf("ssl_decrypt_record wrong record len/padding outlen %d\n work %d\n",*outl, worklen);
+            return -1;
+        }
+        worklen-=ssl_cipher_suite_dig(decoder->cipher_suite)->len;
+        mac = out_str->data + worklen;
+    } else /* if (decoder->cipher_suite->mode == SSL_CIPHER_MODE_GCM) */ {
+        /* GenericAEADCipher has no MAC */
+        goto skip_mac;
     }
-    worklen-=decoder->cipher_suite->dig_len;
-    mac = out_str->data + worklen;
 
-    /* if TLS 1.1 we use the transmitted IV and remove it after (to not modify dissector in others parts)*/
-    if(ssl->version_netorder==TLSV1DOT1_VERSION){
-        worklen=worklen-decoder->cipher_suite->block;
-        memcpy(out_str->data,out_str->data+decoder->cipher_suite->block,worklen);
-    }
-    if(ssl->version_netorder==DTLSV1DOT0_VERSION ||
-      ssl->version_netorder==DTLSV1DOT0_VERSION_NOT){
-        worklen=worklen-decoder->cipher_suite->block;
-        memcpy(out_str->data,out_str->data+decoder->cipher_suite->block,worklen);
-    }
     /* Now check the MAC */
     ssl_debug_printf("checking mac (len %d, version %X, ct %d seq %d)\n",
         worklen, ssl->version_netorder, ct, decoder->seq);
     if(ssl->version_netorder==SSLV3_VERSION){
         if(ssl3_check_mac(decoder,ct,out_str->data,worklen,mac) < 0) {
-            ssl_debug_printf("ssl_decrypt_record: mac failed\n");
-            return -1;
+            if(ssl_ignore_mac_failed) {
+                ssl_debug_printf("ssl_decrypt_record: mac failed, but ignored for troubleshooting ;-)\n");
+            }
+            else{
+                ssl_debug_printf("ssl_decrypt_record: mac failed\n");
+                return -1;
+            }
+        }
+        else{
+            ssl_debug_printf("ssl_decrypt_record: mac ok\n");
         }
     }
-    else if(ssl->version_netorder==TLSV1_VERSION || ssl->version_netorder==TLSV1DOT1_VERSION){
+    else if(ssl->version_netorder==TLSV1_VERSION || ssl->version_netorder==TLSV1DOT1_VERSION || ssl->version_netorder==TLSV1DOT2_VERSION){
         if(tls_check_mac(decoder,ct,ssl->version_netorder,out_str->data,worklen,mac)< 0) {
-            ssl_debug_printf("ssl_decrypt_record: mac failed\n");
-            return -1;
+            if(ssl_ignore_mac_failed) {
+                ssl_debug_printf("ssl_decrypt_record: mac failed, but ignored for troubleshooting ;-)\n");
+            }
+            else{
+                ssl_debug_printf("ssl_decrypt_record: mac failed\n");
+                return -1;
+            }
+        }
+        else{
+            ssl_debug_printf("ssl_decrypt_record: mac ok\n");
         }
     }
     else if(ssl->version_netorder==DTLSV1DOT0_VERSION ||
+        ssl->version_netorder==DTLSV1DOT2_VERSION ||
         ssl->version_netorder==DTLSV1DOT0_VERSION_NOT){
-        /* following the openssl dtls errors the right test is:
-        if(dtls_check_mac(decoder,ct,ssl->version_netorder,out_str->data,worklen,mac)< 0) { */
-        if(tls_check_mac(decoder,ct,TLSV1_VERSION,out_str->data,worklen,mac)< 0) {
+        /* Try rfc-compliant mac first, and if failed, try old openssl's non-rfc-compliant mac */
+        if(dtls_check_mac(decoder,ct,ssl->version_netorder,out_str->data,worklen,mac)>= 0) {
+            ssl_debug_printf("ssl_decrypt_record: mac ok\n");
+        }
+        else if(tls_check_mac(decoder,ct,TLSV1_VERSION,out_str->data,worklen,mac)>= 0) {
+            ssl_debug_printf("ssl_decrypt_record: dtls rfc-compliant mac failed, but old openssl's non-rfc-compliant mac ok\n");
+        }
+        else if(ssl_ignore_mac_failed) {
+            ssl_debug_printf("ssl_decrypt_record: mac failed, but ignored for troubleshooting ;-)\n");
+        }
+        else{
             ssl_debug_printf("ssl_decrypt_record: mac failed\n");
             return -1;
         }
     }
-    ssl_debug_printf("ssl_decrypt_record: mac ok\n");
+skip_mac:
+
     *outl = worklen;
 
     if (decoder->compression > 0) {
-      ssl_debug_printf("ssl_decrypt_record: compression method %d\n", decoder->compression);
-      ssl_data_copy(comp_str, out_str);
-      ssl_print_data("Plaintext compressed", comp_str->data, worklen);
-      if (!decoder->decomp) {
-        ssl_debug_printf("decrypt_ssl3_record: no decoder available\n");
-        return -1;
-      }
-      if (ssl_decompress_record(decoder->decomp, comp_str->data, worklen, out_str, &uncomplen) < 0) return -1;
-      ssl_print_data("Plaintext uncompressed", out_str->data, uncomplen);
-      *outl = uncomplen;
+        ssl_debug_printf("ssl_decrypt_record: compression method %d\n", decoder->compression);
+        ssl_data_copy(comp_str, out_str);
+        ssl_print_data("Plaintext compressed", comp_str->data, worklen);
+        if (!decoder->decomp) {
+            ssl_debug_printf("decrypt_ssl3_record: no decoder available\n");
+            return -1;
+        }
+        if (ssl_decompress_record(decoder->decomp, comp_str->data, worklen, out_str, &uncomplen) < 0) return -1;
+        ssl_print_data("Plaintext uncompressed", out_str->data, uncomplen);
+        *outl = uncomplen;
     }
 
     return 0;
 }
 
-static void
-ssl_get_version(gint* major, gint* minor, gint* patch)
-{
-  *major = ver_major;
-  *minor = ver_minor;
-  *patch = ver_patch;
-}
-
 #define RSA_PARS 6
 SSL_PRIVATE_KEY*
 ssl_privkey_to_sexp(struct gnutls_x509_privkey_int* priv_key)
 {
     gnutls_datum_t rsa_datum[RSA_PARS]; /* m, e, d, p, q, u */
-    size_t tmp_size;
-    gcry_sexp_t rsa_priv_key;
-    gint major, minor, patch;
-    gint i, p_idx, q_idx;
-    int ret;
-    size_t buf_len;
-    unsigned char buf_keyid[32];
+    size_t         tmp_size;
+    gcry_sexp_t    rsa_priv_key = NULL;
+    gint           i;
+    int            ret;
+    size_t         buf_len;
+    unsigned char  buf_keyid[32];
 
 #ifdef SSL_FAST
     gcry_mpi_t* rsa_params = g_malloc(sizeof(gcry_mpi_t)*RSA_PARS);
@@ -2343,24 +2997,13 @@ ssl_privkey_to_sexp(struct gnutls_x509_privkey_int* priv_key)
         ssl_debug_printf( "Private key imported: KeyID %s\n", bytes_to_str_punct(buf_keyid, (int) buf_len, ':'));
     }
 
-    /*
-     * note: openssl and gnutls use 'p' and 'q' with opposite meaning:
-     * our 'p' must be equal to 'q' as provided from openssl and viceversa
-     */
-
-#if (LIBGNUTLS_VERSION_MAJOR>2)||((LIBGNUTLS_VERSION_MAJOR==2)&&(LIBGNUTLS_VERSION_MINOR>=5))
-    p_idx = 3; q_idx = 4;
-#else /* versions 2.4.x and older need 'p' and 'q' swapped */
-    p_idx = 4; q_idx = 3;
-#endif
-
     /* RSA get parameter */
     if (gnutls_x509_privkey_export_rsa_raw(priv_key,
                                            &rsa_datum[0],
                                            &rsa_datum[1],
                                            &rsa_datum[2],
-                                           &rsa_datum[p_idx],
-                                           &rsa_datum[q_idx],
+                                           &rsa_datum[3],
+                                           &rsa_datum[4],
                                            &rsa_datum[5])  != 0) {
         ssl_debug_printf("ssl_load_key: can't export rsa param (is a rsa private key file ?!?)\n");
 #ifdef SSL_FAST
@@ -2380,16 +3023,12 @@ ssl_privkey_to_sexp(struct gnutls_x509_privkey_int* priv_key)
       }
     }
 
-    ssl_get_version(&major, &minor, &patch);
-
-    /* certain versions of gnutls require swap of rsa params 'p' and 'q' */
-    if ((major <= 1) && (minor <= 0) && (patch <=13))
+    /* libgcrypt expects p < q, and gnutls might not return it as such, depending on gnutls version and its crypto backend */
+    if (gcry_mpi_cmp(rsa_params[3], rsa_params[4]) > 0)
     {
-        gcry_mpi_t tmp;
-        ssl_debug_printf("ssl_load_key: swapping p and q parameters\n");
-        tmp = rsa_params[4];
-        rsa_params[4] = rsa_params[3];
-        rsa_params[3] = tmp;
+        ssl_debug_printf("ssl_load_key: swapping p and q parameters and recomputing u\n");
+        gcry_mpi_swap(rsa_params[3], rsa_params[4]);
+        gcry_mpi_invm(rsa_params[5], rsa_params[3], rsa_params[4]);
     }
 
     if  (gcry_sexp_build( &rsa_priv_key, NULL,
@@ -2403,7 +3042,7 @@ ssl_privkey_to_sexp(struct gnutls_x509_privkey_int* priv_key)
         return NULL;
     }
 
-#if SSL_FAST
+#ifdef SSL_FAST
     return rsa_params;
 #else
     for (i=0; i< 6; i++)
@@ -2416,19 +3055,17 @@ ssl_privkey_to_sexp(struct gnutls_x509_privkey_int* priv_key)
 Ssl_private_key_t *
 ssl_load_key(FILE* fp)
 {
-    /* gnutls make our work much harded, since we have to work internally with
-     * s-exp formatted data, but PEM loader export only in "gnutls_datum"
+    /* gnutls makes our work much harder, since we have to work internally with
+     * s-exp formatted data, but PEM loader exports only in "gnutls_datum_t"
      * format, and a datum -> s-exp convertion function does not exist.
      */
     gnutls_x509_privkey_t priv_key;
-    gnutls_datum key;
-    gint size;
-    guint bytes;
+    gnutls_datum_t        key;
+    long                  size;
+    gint                  ret;
+    guint                 bytes;
 
-    Ssl_private_key_t *private_key = g_malloc(sizeof(Ssl_private_key_t));
-    private_key->x509_cert = 0;
-    private_key->x509_pkey = 0;
-    private_key->sexp_pkey = 0;
+    Ssl_private_key_t *private_key = (Ssl_private_key_t *)g_malloc0(sizeof(Ssl_private_key_t));
 
     /* init private key data*/
     gnutls_x509_privkey_init(&priv_key);
@@ -2449,8 +3086,8 @@ ssl_load_key(FILE* fp)
         g_free(private_key);
         return NULL;
     }
-    key.data = g_malloc(size);
-    key.size = size;
+    key.data = (unsigned char *)g_malloc(size);
+    key.size = (int)size;
     bytes = (guint) fread(key.data, 1, key.size, fp);
     if (bytes < key.size) {
         ssl_debug_printf("ssl_load_key: can't read from file %d bytes, got %d\n",
@@ -2461,8 +3098,8 @@ ssl_load_key(FILE* fp)
     }
 
     /* import PEM data*/
-    if (gnutls_x509_privkey_import(priv_key, &key, GNUTLS_X509_FMT_PEM)!=0) {
-        ssl_debug_printf("ssl_load_key: can't import pem data\n");
+    if ((ret = gnutls_x509_privkey_import(priv_key, &key, GNUTLS_X509_FMT_PEM)) != GNUTLS_E_SUCCESS) {
+        ssl_debug_printf("ssl_load_key: can't import pem data: %s\n", gnutls_strerror(ret));
         g_free(private_key);
         g_free(key.data);
         return NULL;
@@ -2500,31 +3137,36 @@ BAGTYPE(gnutls_pkcs12_bag_type_t x) {
     }
 }
 
-Ssl_private_key_t *
-ssl_load_pkcs12(FILE* fp, const gchar *cert_passwd) {
-
-    int i, j, ret;
-    int rest;
-    unsigned char *p;
-    gnutls_datum_t data;
-    gnutls_pkcs12_bag_t bag = NULL;
-    gnutls_pkcs12_bag_type_t bag_type;
-    size_t len, buf_len;
-    static char buf_name[256];
-    static char buf_email[128];
-    unsigned char buf_keyid[32];
+/**
+ * Load a RSA private key from a PKCS#12 file.
+ * @param fp the file that contains the key data.
+ * @param cert_passwd password to decrypt the PKCS#12 file.
+ * @param[out] err error message upon failure; NULL upon success.
+ * @return a pointer to the loaded key on success; NULL upon failure.
+ */
+static Ssl_private_key_t *
+ssl_load_pkcs12(FILE* fp, const gchar *cert_passwd, const char** err) {
+
+    int                       i, j, ret;
+    int                       rest;
+    unsigned char            *p;
+    gnutls_datum_t            data;
+    gnutls_pkcs12_bag_t       bag = NULL;
+    gnutls_pkcs12_bag_type_t  bag_type;
+    size_t                    len, buf_len;
+    static char               buf_name[256];
+    static char               buf_email[128];
+    unsigned char             buf_keyid[32];
 
     gnutls_pkcs12_t       ssl_p12  = NULL;
     gnutls_x509_crt_t     ssl_cert = NULL;
     gnutls_x509_privkey_t ssl_pkey = NULL;
 
-    Ssl_private_key_t *private_key = g_malloc(sizeof(Ssl_private_key_t));
-    private_key->x509_cert = 0;
-    private_key->x509_pkey = 0;
-    private_key->sexp_pkey = 0;
+    Ssl_private_key_t *private_key = (Ssl_private_key_t *)g_malloc0(sizeof(Ssl_private_key_t));
+    *err = NULL;
 
     rest = 4096;
-    data.data = g_malloc(rest);
+    data.data = (unsigned char *)g_malloc(rest);
     data.size = rest;
     p = data.data;
     while ((len = fread(p, 1, rest, fp)) > 0) {
@@ -2532,7 +3174,7 @@ ssl_load_pkcs12(FILE* fp, const gchar *cert_passwd) {
         rest -= (int) len;
         if (!rest) {
             rest = 1024;
-            data.data = g_realloc(data.data, data.size + rest);
+            data.data = (unsigned char *)g_realloc(data.data, data.size + rest);
             p = data.data + data.size;
             data.size += rest;
         }
@@ -2540,21 +3182,38 @@ ssl_load_pkcs12(FILE* fp, const gchar *cert_passwd) {
     data.size -= rest;
     ssl_debug_printf("%d bytes read\n", data.size);
     if (!feof(fp)) {
-        ssl_debug_printf( "Error during certificate reading.\n");
+        *err = "Error during certificate reading.";
+        ssl_debug_printf("%s\n", *err);
         g_free(private_key);
+        g_free(data.data);
         return 0;
     }
 
     ret = gnutls_pkcs12_init(&ssl_p12);
     if (ret < 0) {
-        ssl_debug_printf("gnutls_pkcs12_init(&st_p12) - %s", gnutls_strerror(ret));
+        *err = wmem_strdup_printf(wmem_file_scope(), "gnutls_pkcs12_init(&st_p12) - %s", gnutls_strerror(ret));
+        ssl_debug_printf("%s\n", *err);
         g_free(private_key);
+        g_free(data.data);
         return 0;
     }
+
+    /* load PKCS#12 in DER or PEM format */
     ret = gnutls_pkcs12_import(ssl_p12, &data, GNUTLS_X509_FMT_DER, 0);
+    if (ret < 0) {
+        *err = wmem_strdup_printf(wmem_file_scope(), "could not load PKCS#12 in DER format: %s", gnutls_strerror(ret));
+        ssl_debug_printf("%s\n", *err);
+
+        ret = gnutls_pkcs12_import(ssl_p12, &data, GNUTLS_X509_FMT_PEM, 0);
+        if (ret < 0) {
+            *err = wmem_strdup_printf(wmem_file_scope(), "could not load PKCS#12 in PEM format: %s", gnutls_strerror(ret));
+            ssl_debug_printf("%s\n", *err);
+        } else {
+            *err = NULL;
+        }
+    }
     g_free(data.data);
     if (ret < 0) {
-        ssl_debug_printf("gnutls_pkcs12_import(ssl_p12, &data, GNUTLS_X509_FMT_DER, 0) - %s\n", gnutls_strerror(ret));
         g_free(private_key);
         return 0;
     }
@@ -2583,7 +3242,6 @@ ssl_load_pkcs12(FILE* fp, const gchar *cert_passwd) {
                     if (bag_type >= GNUTLS_BAG_UNKNOWN) continue;
                     ssl_debug_printf( "Bag %d/%d decrypted: %s\n", i, j, BAGTYPE(bag_type));
                 }
-                ret = 0;
             }
 
             ret = gnutls_pkcs12_bag_get_data(bag, j, &data);
@@ -2595,14 +3253,16 @@ ssl_load_pkcs12(FILE* fp, const gchar *cert_passwd) {
 
                     ret = gnutls_x509_crt_init(&ssl_cert);
                     if (ret < 0) {
-                        ssl_debug_printf( "gnutls_x509_crt_init(&ssl_cert) - %s\n", gnutls_strerror(ret));
+                        *err = wmem_strdup_printf(wmem_file_scope(), "gnutls_x509_crt_init(&ssl_cert) - %s", gnutls_strerror(ret));
+                        ssl_debug_printf("%s\n", *err);
                         g_free(private_key);
                         return 0;
                     }
 
                     ret = gnutls_x509_crt_import(ssl_cert, &data, GNUTLS_X509_FMT_DER);
                     if (ret < 0) {
-                        ssl_debug_printf( "gnutls_x509_crt_import(ssl_cert, &data, GNUTLS_X509_FMT_DER) - %s\n", gnutls_strerror(ret));
+                        *err = wmem_strdup_printf(wmem_file_scope(), "gnutls_x509_crt_import(ssl_cert, &data, GNUTLS_X509_FMT_DER) - %s", gnutls_strerror(ret));
+                        ssl_debug_printf("%s\n", *err);
                         g_free(private_key);
                         return 0;
                     }
@@ -2627,20 +3287,23 @@ ssl_load_pkcs12(FILE* fp, const gchar *cert_passwd) {
 
                     ret = gnutls_x509_privkey_init(&ssl_pkey);
                     if (ret < 0) {
-                        ssl_debug_printf( "gnutls_x509_privkey_init(&ssl_pkey) - %s\n", gnutls_strerror(ret));
+                        *err = wmem_strdup_printf(wmem_file_scope(), "gnutls_x509_privkey_init(&ssl_pkey) - %s", gnutls_strerror(ret));
+                        ssl_debug_printf("%s\n", *err);
                         g_free(private_key);
                         return 0;
                     }
                     ret = gnutls_x509_privkey_import_pkcs8(ssl_pkey, &data, GNUTLS_X509_FMT_DER, cert_passwd,
                                                            (bag_type==GNUTLS_BAG_PKCS8_KEY) ? GNUTLS_PKCS_PLAIN : 0);
                     if (ret < 0) {
-                        ssl_debug_printf( "Can not decrypt private key - %s\n", gnutls_strerror(ret));
+                        *err = wmem_strdup_printf(wmem_file_scope(), "Can not decrypt private key - %s", gnutls_strerror(ret));
+                        ssl_debug_printf("%s\n", *err);
                         g_free(private_key);
                         return 0;
                     }
 
                     if (gnutls_x509_privkey_get_pk_algorithm(ssl_pkey) != GNUTLS_PK_RSA) {
-                        ssl_debug_printf("ssl_load_pkcs12: private key public key algorithm isn't RSA\n");
+                        *err = "ssl_load_pkcs12: private key public key algorithm isn't RSA";
+                        ssl_debug_printf("%s\n", *err);
                         g_free(private_key);
                         return 0;
                     }
@@ -2648,6 +3311,8 @@ ssl_load_pkcs12(FILE* fp, const gchar *cert_passwd) {
                     private_key->x509_pkey = ssl_pkey;
                     private_key->sexp_pkey = ssl_privkey_to_sexp(ssl_pkey);
                     if ( !private_key->sexp_pkey ) {
+                        *err = "ssl_load_pkcs12: could not create sexp_pkey";
+                        ssl_debug_printf("%s\n", *err);
                         g_free(private_key);
                         return NULL;
                     }
@@ -2664,7 +3329,7 @@ ssl_load_pkcs12(FILE* fp, const gchar *cert_passwd) {
 
 void ssl_free_key(Ssl_private_key_t* key)
 {
-#if SSL_FAST
+#ifdef SSL_FAST
     gint i;
     for (i=0; i< 6; i++)
         gcry_mpi_release(key->sexp_pkey[i]);
@@ -2684,10 +3349,14 @@ void ssl_free_key(Ssl_private_key_t* key)
 gint
 ssl_find_private_key(SslDecryptSession *ssl_session, GHashTable *key_hash, GTree* associations, packet_info *pinfo) {
     SslService dummy;
-    char ip_addr_any[] = {0,0,0,0};
-    guint32 port = 0;
+    char       ip_addr_any[] = {0,0,0,0};
+    guint32    port    = 0;
     Ssl_private_key_t * private_key;
 
+    if (!ssl_session) {
+        return 0;
+    }
+
     /* we need to know which side of the conversation is speaking */
     if (ssl_packet_from_server(ssl_session, associations, pinfo)) {
         dummy.addr = pinfo->src;
@@ -2706,13 +3375,13 @@ ssl_find_private_key(SslDecryptSession *ssl_session, GHashTable *key_hash, GTree
 
 
     ssl_session->private_key = 0;
-    private_key = g_hash_table_lookup(key_hash, &dummy);
+    private_key = (Ssl_private_key_t *)g_hash_table_lookup(key_hash, &dummy);
 
     if (!private_key) {
         ssl_debug_printf("ssl_find_private_key can't find private key for this server! Try it again with universal port 0\n");
 
         dummy.port = 0;
-        private_key = g_hash_table_lookup(key_hash, &dummy);
+        private_key = (Ssl_private_key_t *)g_hash_table_lookup(key_hash, &dummy);
     }
 
     if (!private_key) {
@@ -2723,7 +3392,7 @@ ssl_find_private_key(SslDecryptSession *ssl_session, GHashTable *key_hash, GTree
         dummy.addr.data = ip_addr_any;
 
         dummy.port = port;
-        private_key = g_hash_table_lookup(key_hash, &dummy);
+        private_key = (Ssl_private_key_t *)g_hash_table_lookup(key_hash, &dummy);
     }
 
     if (!private_key) {
@@ -2738,16 +3407,10 @@ ssl_find_private_key(SslDecryptSession *ssl_session, GHashTable *key_hash, GTree
 void
 ssl_lib_init(void)
 {
-    const gchar* str = gnutls_check_version(NULL);
-
-    /* get library version */
-    /* old relase of gnutls does not define the appropriate macros, so get
-     * them from the string*/
-    ssl_debug_printf("gnutls version: %s\n", str);
-    sscanf(str, "%d.%d.%d", &ver_major, &ver_minor, &ver_patch);
+    ssl_debug_printf("gnutls version: %s\n", gnutls_check_version(NULL));
 }
 
-#else /* HAVE_LIBGNUTLS */
+#else /* defined(HAVE_LIBGNUTLS) && defined(HAVE_LIBGCRYPT) */
 /* no libgnutl: dummy operation to keep interface consistent*/
 void
 ssl_lib_init(void)
@@ -2762,7 +3425,8 @@ ssl_load_key(FILE* fp)
 }
 
 Ssl_private_key_t *
-ssl_load_pkcs12(FILE* fp, const gchar *cert_passwd _U_) {
+ssl_load_pkcs12(FILE* fp, const gchar *cert_passwd _U_, const char** err) {
+    *err = NULL;
     ssl_debug_printf("ssl_load_pkcs12: impossible without gnutls. fp %p\n",fp);
     return NULL;
 }
@@ -2917,12 +3581,13 @@ guint
 ssl_private_key_hash  (gconstpointer v)
 {
     const SslService *key;
-    guint l, hash, len ;
+    guint        l, hash, len ;
     const guint* cur;
-    key = (const SslService *)v;
+
+    key  = (const SslService *)v;
     hash = key->port;
-    len = key->addr.len;
-    cur = (const guint*) key->addr.data;
+    len  = key->addr.len;
+    cur  = (const guint*) key->addr.data;
 
     for (l=4; (l<len); l+=4, cur++)
         hash = hash ^ (*cur);
@@ -2935,8 +3600,10 @@ ssl_private_key_hash  (gconstpointer v)
 void
 ssl_private_key_free(gpointer id, gpointer key, gpointer dummy _U_)
 {
-    g_free(id);
-    ssl_free_key((Ssl_private_key_t*) key);
+    if (id != NULL) {
+        g_free(id);
+        ssl_free_key((Ssl_private_key_t*) key);
+    }
 }
 
 /* handling of association between tls/dtls ports and clear text protocol */
@@ -2945,7 +3612,7 @@ ssl_association_add(GTree* associations, dissector_handle_t handle, guint port,
 {
 
     SslAssociation* assoc;
-    assoc = g_malloc(sizeof(SslAssociation));
+    assoc = (SslAssociation *)g_malloc(sizeof(SslAssociation));
 
     assoc->tcp = tcp;
     assoc->ssl_port = port;
@@ -2962,16 +3629,18 @@ ssl_association_add(GTree* associations, dissector_handle_t handle, guint port,
         assoc->handle = find_dissector("data");
     }
 
-    if(!assoc->handle) {
+    if (!assoc->handle) {
         fprintf(stderr, "association_add() could not find handle for protocol:%s\n",protocol);
     } else {
-        if(port) {
-            if(tcp)
+        if (port) {
+            if (tcp)
                 dissector_add_uint("tcp.port", port, handle);
             else
                 dissector_add_uint("udp.port", port, handle);
         }
         g_tree_insert(associations, assoc, assoc);
+
+        dissector_add_uint("sctp.port", port, handle);
     }
 }
 
@@ -2992,7 +3661,7 @@ ssl_association_remove(GTree* associations, SslAssociation *assoc)
 gint
 ssl_association_cmp(gconstpointer a, gconstpointer b)
 {
-    const SslAssociation *assoc_a=a, *assoc_b=b;
+    const SslAssociation *assoc_a=(SslAssociation *)a, *assoc_b=(SslAssociation *)b;
     if (assoc_a->tcp != assoc_b->tcp) return (assoc_a->tcp)?1:-1;
     return assoc_a->ssl_port - assoc_b->ssl_port;
 }
@@ -3001,11 +3670,11 @@ SslAssociation*
 ssl_association_find(GTree * associations, guint port, gboolean tcp)
 {
     register SslAssociation* ret;
-    SslAssociation assoc_tmp;
+    SslAssociation           assoc_tmp;
 
     assoc_tmp.tcp = tcp;
     assoc_tmp.ssl_port = port;
-    ret = g_tree_lookup(associations, &assoc_tmp);
+    ret = (SslAssociation *)g_tree_lookup(associations, &assoc_tmp);
 
     ssl_debug_printf("association_find: %s port %d found %p\n", (tcp)?"TCP":"UDP", port, (void *)ret);
     return ret;
@@ -3033,47 +3702,49 @@ ssl_packet_from_server(SslDecryptSession* ssl, GTree* associations, packet_info
     return ret;
 }
 
-/* add to packet data a newly allocated tvb with the specified real data*/
+/* add to packet data a copy of the specified real data */
 void
 ssl_add_record_info(gint proto, packet_info *pinfo, guchar* data, gint data_len, gint record_id)
 {
-    guchar* real_data;
+    guchar*        real_data;
     SslRecordInfo* rec;
     SslPacketInfo* pi;
-    real_data = se_alloc(data_len);
-    rec = se_alloc(sizeof(SslRecordInfo));
-    pi = p_get_proto_data(pinfo->fd, proto);
 
+    pi = (SslPacketInfo *)p_get_proto_data(pinfo->fd, proto, 0);
     if (!pi)
     {
-        pi = se_alloc0(sizeof(SslPacketInfo));
-        p_add_proto_data(pinfo->fd, proto,pi);
+        pi = (SslPacketInfo *)wmem_alloc0(wmem_file_scope(), sizeof(SslPacketInfo));
+        p_add_proto_data(pinfo->fd, proto, 0, pi);
     }
 
-    rec->id = record_id;
-    rec->tvb = tvb_new_real_data(real_data, data_len, data_len);
+    real_data = (guchar *)wmem_alloc(wmem_file_scope(), data_len);
     memcpy(real_data, data, data_len);
 
+    rec = (SslRecordInfo *)wmem_alloc(wmem_file_scope(), sizeof(SslRecordInfo));
+    rec->id = record_id;
+    rec->real_data = real_data;
+    rec->data_len = data_len;
+
     /* head insertion */
     rec->next= pi->handshake_data;
     pi->handshake_data = rec;
 }
 
-
-/* search in packet data the tvbuff associated to the specified id */
+/* search in packet data for the specified id; return a newly created tvb for the associated data */
 tvbuff_t*
-ssl_get_record_info(int proto, packet_info *pinfo, gint record_id)
+ssl_get_record_info(tvbuff_t *parent_tvb, int proto, packet_info *pinfo, gint record_id)
 {
     SslRecordInfo* rec;
     SslPacketInfo* pi;
-    pi = p_get_proto_data(pinfo->fd, proto);
+    pi = (SslPacketInfo *)p_get_proto_data(pinfo->fd, proto, 0);
 
     if (!pi)
         return NULL;
 
     for (rec = pi->handshake_data; rec; rec = rec->next)
         if (rec->id == record_id)
-            return rec->tvb;
+            /* link new real_data_tvb with a parent tvb so it is freed when frame dissection is complete */
+            return tvb_new_child_real_data(parent_tvb, rec->real_data, rec->data_len, rec->data_len);
 
     return NULL;
 }
@@ -3081,28 +3752,30 @@ ssl_get_record_info(int proto, packet_info *pinfo, gint record_id)
 void
 ssl_add_data_info(gint proto, packet_info *pinfo, guchar* data, gint data_len, gint key, SslFlow *flow)
 {
-    SslDataInfo *rec, **prec;
+    SslDataInfo   *rec, **prec;
     SslPacketInfo *pi;
 
-    pi = p_get_proto_data(pinfo->fd, proto);
+    pi = (SslPacketInfo *)p_get_proto_data(pinfo->fd, proto, 0);
     if (!pi)
     {
-        pi = se_alloc0(sizeof(SslPacketInfo));
-        p_add_proto_data(pinfo->fd, proto,pi);
+        pi = (SslPacketInfo *)wmem_alloc0(wmem_file_scope(), sizeof(SslPacketInfo));
+        p_add_proto_data(pinfo->fd, proto, 0, pi);
     }
 
-    rec = se_alloc(sizeof(SslDataInfo)+data_len);
+    rec = (SslDataInfo *)wmem_alloc(wmem_file_scope(), sizeof(SslDataInfo)+data_len);
     rec->key = key;
     rec->plain_data.data = (guchar*)(rec + 1);
     memcpy(rec->plain_data.data, data, data_len);
     rec->plain_data.data_len = data_len;
-    rec->seq = flow->byte_seq;
-    rec->nxtseq = flow->byte_seq + data_len;
-    rec->flow = flow;
+    if (flow)
+    {
+        rec->seq = flow->byte_seq;
+        rec->nxtseq = flow->byte_seq + data_len;
+        rec->flow = flow;
+        flow->byte_seq += data_len;
+    }
     rec->next = NULL;
 
-    flow->byte_seq += data_len;
-
     /* insertion */
     prec = &pi->appl_data;
     while (*prec) prec = &(*prec)->next;
@@ -3115,9 +3788,9 @@ ssl_add_data_info(gint proto, packet_info *pinfo, guchar* data, gint data_len, g
 SslDataInfo*
 ssl_get_data_info(int proto, packet_info *pinfo, gint key)
 {
-    SslDataInfo* rec;
+    SslDataInfo*   rec;
     SslPacketInfo* pi;
-    pi = p_get_proto_data(pinfo->fd, proto);
+    pi = (SslPacketInfo *)p_get_proto_data(pinfo->fd, proto, 0);
 
     if (!pi) return NULL;
 
@@ -3147,150 +3820,93 @@ ssl_common_init(GHashTable **session_hash, StringInfo *decrypted_data, StringInf
 
 /* parse ssl related preferences (private keys and ports association strings) */
 void
-ssl_parse_key_list(const gchar * keys_list, GHashTable *key_hash, GTree* associations, dissector_handle_t handle, gboolean tcp)
-{
-    gchar* end;
-    gchar* start;
-    gchar* tmp;
-    guchar* ip;
-    SslService* service;
-    Ssl_private_key_t * private_key, *tmp_private_key;
-    FILE* fp;
-
-    start = g_strdup(keys_list);
-    tmp = start;
-    ssl_debug_printf("ssl_init keys string:\n%s\n", start);
-    do {
-        int read_index, write_index;
-        gchar* addr, *port, *protocol, *filename, *cert_passwd;
-
-        addr = start;
-        /* split ip/file couple with ';' separator*/
-        end = strpbrk(start, ";\n\r");
-        if (end) {
-            *end = 0;
-            start = end+1;
-        }
+ssl_parse_key_list(const ssldecrypt_assoc_t * uats, GHashTable *key_hash, GTree* associations, dissector_handle_t handle, gboolean tcp)
+{
+    SslService*        service;
+    Ssl_private_key_t* private_key, *tmp_private_key;
+    FILE*              fp     = NULL;
+    guint32            addr_data[4];
+    int                addr_len, at;
+    address_type addr_type[2] = { AT_IPv4, AT_IPv6 };
+
+    /* try to load keys file first */
+    fp = ws_fopen(uats->keyfile, "rb");
+    if (!fp) {
+        fprintf(stderr, "Can't open file %s\n",uats->keyfile);
+        return;
+    }
 
-        /* skip comments (in file) */
-        if (addr[0] == '#') continue;
+    for (at = 0; at < 2; at++) {
+        memset(addr_data, 0, sizeof(addr_data));
+        addr_len = 0;
 
-        /* for each entry split ip, port, protocol, filename with ',' separator */
-        ssl_debug_printf("ssl_init found host entry %s\n", addr);
-        port = strchr(addr, ',');
-        if (!port)
-        {
-            ssl_debug_printf("ssl_init entry malformed can't find port in '%s'\n", addr);
-            continue;
-        }
-        *port = 0;
-        port++;
+        /* any: IPv4 or IPv6 wildcard */
+        /* anyipv4: IPv4 wildcard */
+        /* anyipv6: IPv6 wildcard */
 
-        protocol = strchr(port,',');
-        if (!protocol)
-        {
-            ssl_debug_printf("ssl_init entry malformed can't find protocol in %s\n", port);
-            continue;
+        if(addr_type[at] == AT_IPv4) {
+            if (strcmp(uats->ipaddr, "any") == 0 || strcmp(uats->ipaddr, "anyipv4") == 0 ||
+                    get_host_ipaddr(uats->ipaddr, &addr_data[0])) {
+                addr_len = 4;
+            }
+        } else { /* AT_IPv6 */
+            if(strcmp(uats->ipaddr, "any") == 0 || strcmp(uats->ipaddr, "anyipv6") == 0 ||
+                    get_host_ipaddr6(uats->ipaddr, (struct e_in6_addr *) addr_data)) {
+                addr_len = 16;
+            }
         }
-        *protocol=0;
-        protocol++;
 
-        filename = strchr(protocol,',');
-        if (!filename)
-        {
-            ssl_debug_printf("ssl_init entry malformed can't find filename in %s\n", protocol);
+        if (! addr_len) {
             continue;
         }
-        *filename=0;
-        filename++;
-
-        cert_passwd = strchr(filename,',');
-        if (cert_passwd)
-        {
-            *cert_passwd=0;
-            cert_passwd++;
-        }
-
-        /* convert ip and port string to network rappresentation*/
-        service = g_malloc(sizeof(SslService) + 4);
-        service->addr.type = AT_IPv4;
-        service->addr.len = 4;
-        service->addr.data = ip = ((guchar*)service) + sizeof(SslService);
 
-        /* remove all spaces in addr */
-        read_index = 0;
-        write_index = 0;
-
-        while(addr[read_index]) {
-            if (addr[read_index] != ' ') {
-                addr[write_index] = addr[read_index];
-                write_index++;
+        if ((gint)strlen(uats->password) == 0) {
+            private_key = ssl_load_key(fp);
+        } else {
+            const char *err = NULL;
+            private_key = ssl_load_pkcs12(fp, uats->password, &err);
+            if (err) {
+                fprintf(stderr, "%s\n", err);
             }
-            read_index++;
         }
-        addr[write_index] = 0;
-
-        if ( !strcmp("any", addr) || !strcmp("ANY", addr) ) {
-            ip[0] = 0;
-            ip[1] = 0;
-            ip[2] = 0;
-            ip[3] = 0;
-        } else {
-           guint tmp0, tmp1, tmp2, tmp3;
 
-            sscanf(addr, "%u.%u.%u.%u", &tmp0, &tmp1, &tmp2, &tmp3);
-           ip[0] = (guchar)tmp0;
-           ip[1] = (guchar)tmp1;
-           ip[2] = (guchar)tmp2;
-           ip[3] = (guchar)tmp3;
+        if (!private_key) {
+            fprintf(stderr,"Can't load private key from %s\n", uats->keyfile);
+            fclose(fp);
+            return;
         }
 
-        if(!strcmp("start_tls", port)) {
+        service = (SslService *)g_malloc(sizeof(SslService) + addr_len);
+        service->addr.type = addr_type[at];
+        service->addr.len = addr_len;
+        service->addr.data = ((guchar*)service) + sizeof(SslService);
+        memcpy((void*)service->addr.data, addr_data, addr_len);
+
+        if(strcmp(uats->port,"start_tls")==0) {
             service->port = 0;
         } else {
-            service->port = atoi(port);
+            service->port = atoi(uats->port);
         }
-        ssl_debug_printf("ssl_init addr '%u.%u.%u.%u' port '%d' filename '%s' password(only for p12 file) '%s'\n",
-                         ip[0], ip[1], ip[2], ip[3], service->port, filename, cert_passwd ? cert_passwd : "(null)");
 
-        /* try to load pen or p12 file*/
-        fp = ws_fopen(filename, "rb");
-        if (!fp) {
-            fprintf(stderr, "can't open file %s \n",filename);
-            continue;
-        }
+        ssl_debug_printf("ssl_init %s addr '%s' (%s) port '%d' filename '%s' password(only for p12 file) '%s'\n",
+            (addr_type[at] == AT_IPv4) ? "IPv4" : "IPv6", uats->ipaddr, ep_address_to_str(&service->addr),
+            service->port, uats->keyfile, uats->password);
 
-        if (!cert_passwd) {
-            private_key = ssl_load_key(fp);
-        }
-        else
-        {
-            private_key = ssl_load_pkcs12(fp,cert_passwd);
-        }
-        /* !!! */
-        if (!private_key) {
-            fprintf(stderr,"can't load private key from %s\n",
-                    filename);
-            fclose(fp);
-            continue;
-        }
-
-        fclose(fp);
-
-        ssl_debug_printf("ssl_init private key file %s successfully loaded\n",filename);
+        ssl_debug_printf("ssl_init private key file %s successfully loaded.\n", uats->keyfile);
 
         /* if item exists, remove first */
-        tmp_private_key = g_hash_table_lookup(key_hash, service);
+        tmp_private_key = (Ssl_private_key_t *)g_hash_table_lookup(key_hash, service);
         if (tmp_private_key) {
             g_hash_table_remove(key_hash, service);
             ssl_free_key(tmp_private_key);
         }
+
         g_hash_table_insert(key_hash, service, private_key);
 
-        ssl_association_add(associations, handle, service->port, protocol, tcp, TRUE);
+        ssl_association_add(associations, handle, service->port, uats->protocol, tcp, TRUE);
+    }
 
-    } while (end != NULL);
-    g_free(tmp);
+    fclose(fp);
 }
 
 /* store master secret into session data cache */
@@ -3300,8 +3916,8 @@ ssl_save_session(SslDecryptSession* ssl, GHashTable *session_hash)
     /* allocate stringinfo chunks for session id and master secret data*/
     StringInfo* session_id;
     StringInfo* master_secret;
-    session_id = se_alloc0(sizeof(StringInfo) + ssl->session_id.data_len);
-    master_secret = se_alloc0(48 + sizeof(StringInfo));
+    session_id = (StringInfo *)wmem_alloc0(wmem_file_scope(), sizeof(StringInfo) + ssl->session_id.data_len);
+    master_secret = (StringInfo *)wmem_alloc0(wmem_file_scope(), 48 + sizeof(StringInfo));
 
     master_secret->data = ((guchar*)master_secret+sizeof(StringInfo));
 
@@ -3317,25 +3933,26 @@ ssl_save_session(SslDecryptSession* ssl, GHashTable *session_hash)
     ssl_print_string("ssl_save_session stored master secret", master_secret);
 }
 
-void
+gboolean
 ssl_restore_session(SslDecryptSession* ssl, GHashTable *session_hash)
 {
     StringInfo* ms;
-    ms = g_hash_table_lookup(session_hash, &ssl->session_id);
+    ms = (StringInfo *)g_hash_table_lookup(session_hash, &ssl->session_id);
 
     if (!ms) {
         ssl_debug_printf("ssl_restore_session can't find stored session\n");
-        return;
+        return FALSE;
     }
     ssl_data_set(&ssl->master_secret, ms->data, ms->data_len);
     ssl->state |= SSL_MASTER_SECRET;
     ssl_debug_printf("ssl_restore_session master key retrieved\n");
+    return TRUE;
 }
 
 int
 ssl_is_valid_content_type(guint8 type)
 {
-    if (type >= 0x14 && type <= 0x17)
+    if ((type >= 0x14) && (type <= 0x18))
     {
         return 1;
     }
@@ -3343,17 +3960,274 @@ ssl_is_valid_content_type(guint8 type)
     return 0;
 }
 
+static guint8
+from_hex_char(gchar c) {
+    if ((c >= '0') && (c <= '9'))
+        return c - '0';
+    if ((c >= 'A') && (c <= 'F'))
+        return c - 'A' + 10;
+    if ((c >= 'a') && (c <= 'f'))
+        return c - 'a' + 10;
+    return 16;
+}
+
+/* from_hex converts |hex_len| bytes of hex data from |in| and sets |*out| to
+ * the result. |out->data| will be allocated using se_alloc. Returns TRUE on
+ * success. */
+static gboolean from_hex(StringInfo* out, const char* in, gsize hex_len) {
+    gsize i;
+
+    if (hex_len & 1)
+        return FALSE;
+
+    out->data_len = (guint)hex_len/2;
+    out->data = (guchar *)wmem_alloc(wmem_file_scope(), out->data_len);
+    for (i = 0; i < out->data_len; i++) {
+        guint8 a = from_hex_char(in[i*2]);
+        guint8 b = from_hex_char(in[i*2 + 1]);
+        if (a == 16 || b == 16)
+            return FALSE;
+        out->data[i] = a << 4 | b;
+    }
+    return TRUE;
+}
+
+static const unsigned int kRSAMasterSecretLength = 48; /* RFC5246 8.1 */
+
+/* ssl_keylog_parse_session_id parses, from |line|, a string that looks like:
+ *   RSA Session-ID:<hex session id> Master-Key:<hex TLS master secret>.
+ *
+ * It returns TRUE iff the session id matches |ssl_session| and the master
+ * secret is correctly extracted. */
+static gboolean
+ssl_keylog_parse_session_id(const char* line,
+                            SslDecryptSession* ssl_session)
+{
+    gsize len = strlen(line);
+    unsigned int i;
+
+    if (ssl_session->session_id.data_len == 0)
+        return FALSE;
+
+    if (len < 15 || memcmp(line, "RSA Session-ID:", 15) != 0)
+        return FALSE;
+    line += 15;
+    len -= 15;
+
+    if (len < ssl_session->session_id.data_len*2)
+        return FALSE;
+
+    for (i = 0; i < ssl_session->session_id.data_len; i++) {
+        if (from_hex_char(line[2*i]) != (ssl_session->session_id.data[i] >> 4) ||
+            from_hex_char(line[2*i+1]) != (ssl_session->session_id.data[i] & 15)) {
+            ssl_debug_printf("    line does not match session id\n");
+            return FALSE;
+        }
+    }
+
+    line += 2*i;
+    len -= 2*i;
+
+    if (len != 12 + kRSAMasterSecretLength*2 ||
+        memcmp(line, " Master-Key:", 12) != 0) {
+        return FALSE;
+    }
+    line += 12;
+    len -= 12;
+
+    if (!from_hex(&ssl_session->master_secret, line, len))
+        return FALSE;
+    ssl_session->state &= ~(SSL_PRE_MASTER_SECRET|SSL_HAVE_SESSION_KEY);
+    ssl_session->state |= SSL_MASTER_SECRET;
+    ssl_debug_printf("found master secret in key log\n");
+    return TRUE;
+}
+
+/* ssl_keylog_parse_client_random parses, from |line|, a string that looks like:
+ *   CLIENT_RANDOM <hex client_random> <hex TLS master secret>.
+ *
+ * It returns TRUE iff the client_random matches |ssl_session| and the master
+ * secret is correctly extracted. */
+static gboolean
+ssl_keylog_parse_client_random(const char* line,
+                               SslDecryptSession* ssl_session)
+{
+    static const unsigned int kTLSRandomSize = 32; /* RFC5246 A.6 */
+    gsize len = strlen(line);
+    unsigned int i;
+
+    if (len < 14 || memcmp(line, "CLIENT_RANDOM ", 14) != 0)
+        return FALSE;
+    line += 14;
+    len -= 14;
+
+    if (len < kTLSRandomSize*2 ||
+        ssl_session->client_random.data_len != kTLSRandomSize) {
+        return FALSE;
+    }
+
+    for (i = 0; i < kTLSRandomSize; i++) {
+        if (from_hex_char(line[2*i]) != (ssl_session->client_random.data[i] >> 4) ||
+            from_hex_char(line[2*i+1]) != (ssl_session->client_random.data[i] & 15)) {
+            ssl_debug_printf("    line does not match client random\n");
+            return FALSE;
+        }
+    }
+
+    line += 2*kTLSRandomSize;
+    len -= 2*kTLSRandomSize;
+
+    if (len != 1 + kRSAMasterSecretLength*2 || line[0] != ' ')
+        return FALSE;
+    line++;
+    len--;
+
+    if (!from_hex(&ssl_session->master_secret, line, len))
+        return FALSE;
+    ssl_session->state &= ~(SSL_PRE_MASTER_SECRET|SSL_HAVE_SESSION_KEY);
+    ssl_session->state |= SSL_MASTER_SECRET;
+    ssl_debug_printf("found master secret in key log\n");
+    return TRUE;
+}
+
+/* ssl_keylog_parse_session_id parses, from |line|, a string that looks like:
+ *   RSA <hex, 8-bytes of encrypted pre-master secret> <hex pre-master secret>.
+ *
+ * It returns TRUE iff the session id matches |ssl_session| and the master
+ * secret is correctly extracted. */
+static gboolean
+ssl_keylog_parse_rsa_premaster(const char* line,
+                               SslDecryptSession* ssl_session,
+                               StringInfo* encrypted_pre_master)
+{
+    static const unsigned int kRSAPremasterLength = 48; /* RFC5246 7.4.7.1 */
+    gsize len = strlen(line);
+    unsigned int i;
+
+    if (encrypted_pre_master == NULL)
+        return FALSE;
+
+    if (encrypted_pre_master->data_len < 8)
+        return FALSE;
+
+    if (len < 4 || memcmp(line, "RSA ", 4) != 0)
+        return FALSE;
+    line += 4;
+    len -= 4;
+
+    if (len < 16)
+        return FALSE;
+
+    for (i = 0; i < 8; i++) {
+        if (from_hex_char(line[2*i]) != (encrypted_pre_master->data[i] >> 4) ||
+            from_hex_char(line[2*i+1]) != (encrypted_pre_master->data[i] & 15)) {
+            ssl_debug_printf("    line does not match encrypted pre-master secret");
+            return FALSE;
+        }
+    }
+
+    line += 16;
+    len -= 16;
+
+    if (len != 1 + kRSAPremasterLength*2 || line[0] != ' ')
+        return FALSE;
+    line++;
+    len--;
+
+    if (!from_hex(&ssl_session->pre_master_secret, line, len))
+        return FALSE;
+    ssl_session->state &= ~(SSL_MASTER_SECRET|SSL_HAVE_SESSION_KEY);
+    ssl_session->state |= SSL_PRE_MASTER_SECRET;
+    ssl_debug_printf("found pre-master secret in key log\n");
+
+    return TRUE;
+}
+
+int
+ssl_keylog_lookup(SslDecryptSession* ssl_session,
+                  const gchar* ssl_keylog_filename,
+                  StringInfo* encrypted_pre_master) {
+    FILE* ssl_keylog;
+    int ret = -1;
+
+    if (!ssl_keylog_filename)
+        return -1;
+
+    ssl_debug_printf("trying to use SSL keylog in %s\n", ssl_keylog_filename);
+
+    ssl_keylog = ws_fopen(ssl_keylog_filename, "r");
+    if (!ssl_keylog) {
+        ssl_debug_printf("failed to open SSL keylog\n");
+        return -1;
+    }
+
+    /* The format of the file is a series of records with one of the following formats:
+     *   - "RSA xxxx yyyy"
+     *     Where xxxx are the first 8 bytes of the encrypted pre-master secret (hex-encoded)
+     *     Where yyyy is the cleartext pre-master secret (hex-encoded)
+     *     (this is the original format introduced with bug 4349)
+     *
+     *   - "RSA Session-ID:xxxx Master-Key:yyyy"
+     *     Where xxxx is the SSL session ID (hex-encoded)
+     *     Where yyyy is the cleartext master secret (hex-encoded)
+     *     (added to support openssl s_client Master-Key output)
+     *     This is somewhat is a misnomer because there's nothing RSA specific
+     *     about this.
+     *
+     *   - "CLIENT_RANDOM xxxx yyyy"
+     *     Where xxxx is the client_random from the ClientHello (hex-encoded)
+     *     Where yyy is the cleartext master secret (hex-encoded)
+     *     (This format allows non-RSA SSL connections to be decrypted, i.e.
+     *     ECDHE-RSA.)
+     */
+    for (;;) {
+        char buf[512], *line;
+        gsize bytes_read;
+
+        line = fgets(buf, sizeof(buf), ssl_keylog);
+        if (!line)
+            break;
+
+        bytes_read = strlen(line);
+        /* fgets includes the \n at the end of the line. */
+        if (bytes_read > 0) {
+            line[bytes_read - 1] = 0;
+            bytes_read--;
+        }
+        if (bytes_read > 0 && line[bytes_read - 1] == '\r') {
+            line[bytes_read - 1] = 0;
+            bytes_read--;
+        }
+
+        ssl_debug_printf("  checking keylog line: %s\n", line);
+
+        if (ssl_keylog_parse_session_id(line, ssl_session) ||
+            ssl_keylog_parse_rsa_premaster(line, ssl_session,
+                                           encrypted_pre_master) ||
+            ssl_keylog_parse_client_random(line, ssl_session)) {
+            ret = 1;
+            break;
+        } else {
+            ssl_debug_printf("    line does not match\n");
+        }
+    }
+
+    fclose(ssl_keylog);
+    return ret;
+}
+
 #ifdef SSL_DECRYPT_DEBUG
 
 static FILE* ssl_debug_file=NULL;
 
 void
-ssl_set_debug(char* name)
+ssl_set_debug(const gchar* name)
 {
     static gint debug_file_must_be_closed;
-    gint use_stderr;
+    gint        use_stderr;
+
     debug_file_must_be_closed = 0;
-    use_stderr = name?(strcmp(name, SSL_DEBUG_USE_STDERR) == 0):0;
+    use_stderr                = name?(strcmp(name, SSL_DEBUG_USE_STDERR) == 0):0;
 
     if (debug_file_must_be_closed)
         fclose(ssl_debug_file);
@@ -3365,6 +4239,8 @@ ssl_set_debug(char* name)
         ssl_debug_file = ws_fopen(name, "w");
     if (!use_stderr && ssl_debug_file)
         debug_file_must_be_closed = 1;
+
+    ssl_debug_printf("Wireshark SSL debug log \n\n");
 }
 
 void
@@ -3378,43 +4254,38 @@ void
 ssl_debug_printf(const gchar* fmt, ...)
 {
     va_list ap;
-    gint ret;
-    ret=0;
 
     if (!ssl_debug_file)
         return;
 
     va_start(ap, fmt);
-    ret += vfprintf(ssl_debug_file, fmt, ap);
+    vfprintf(ssl_debug_file, fmt, ap);
     va_end(ap);
 }
 
-void
-ssl_print_text_data(const gchar* name, const guchar* data, size_t len)
-{
-    size_t i;
-    if (!ssl_debug_file)
-        return;
-    fprintf(ssl_debug_file,"%s: ",name);
-    for (i=0; i< len; i++) {
-      fprintf(ssl_debug_file,"%c",data[i]);
-    }
-    fprintf(ssl_debug_file,"\n");
-}
-
 void
 ssl_print_data(const gchar* name, const guchar* data, size_t len)
 {
-    size_t i;
+    size_t i, j, k;
     if (!ssl_debug_file)
         return;
     fprintf(ssl_debug_file,"%s[%d]:\n",name, (int) len);
-    for (i=0; i< len; i++) {
-        if ((i>0) && (i%16 == 0))
-            fprintf(ssl_debug_file,"\n");
-        fprintf(ssl_debug_file,"%.2x ",data[i]&255);
+    for (i=0; i<len; i+=16) {
+        fprintf(ssl_debug_file,"| ");
+        for (j=i, k=0; k<16 && j<len; ++j, ++k)
+            fprintf(ssl_debug_file,"%.2x ",data[j]);
+        for (; k<16; ++k)
+            fprintf(ssl_debug_file,"   ");
+        fputc('|', ssl_debug_file);
+        for (j=i, k=0; k<16 && j<len; ++j, ++k) {
+            guchar c = data[j];
+            if (!isprint(c) || (c=='\t')) c = '.';
+            fputc(c, ssl_debug_file);
+        }
+        for (; k<16; ++k)
+            fputc(' ', ssl_debug_file);
+        fprintf(ssl_debug_file,"|\n");
     }
-    fprintf(ssl_debug_file,"\n");
 }
 
 void
@@ -3423,3 +4294,112 @@ ssl_print_string(const gchar* name, const StringInfo* data)
     ssl_print_data(name, data->data, data->data_len);
 }
 #endif /* SSL_DECRYPT_DEBUG */
+
+/* checks for SSL and DTLS UAT key list fields */
+
+gboolean
+ssldecrypt_uat_fld_ip_chk_cb(void* r _U_, const char* p, guint len _U_, const void* u1 _U_, const void* u2 _U_, const char** err)
+{
+    if (!p || strlen(p) == 0u) {
+        *err = ep_strdup_printf("No IP address given.");
+        return FALSE;
+    }
+
+    *err = NULL;
+    return TRUE;
+}
+
+gboolean
+ssldecrypt_uat_fld_port_chk_cb(void* r _U_, const char* p, guint len _U_, const void* u1 _U_, const void* u2 _U_, const char** err)
+{
+    if (!p || strlen(p) == 0u) {
+        *err = ep_strdup_printf("No Port given.");
+        return FALSE;
+    }
+
+    if (strcmp(p, "start_tls") != 0){
+        const gint i = atoi(p);
+        if (i < 0 || i > 65535) {
+            *err = ep_strdup_printf("Invalid port given.");
+            return FALSE;
+        }
+    }
+
+    *err = NULL;
+    return TRUE;
+}
+
+gboolean
+ssldecrypt_uat_fld_protocol_chk_cb(void* r _U_, const char* p, guint len _U_, const void* u1 _U_, const void* u2 _U_, const char** err)
+{
+    if (!p || strlen(p) == 0u) {
+        *err = ep_strdup_printf("No protocol given.");
+        return FALSE;
+    }
+
+    if (!find_dissector(p)) {
+        *err = ep_strdup_printf("Could not find dissector for: '%s'\nValid dissectors are:\n%s", p, ssl_association_info());
+        return FALSE;
+    }
+
+    *err = NULL;
+    return TRUE;
+}
+
+gboolean
+ssldecrypt_uat_fld_fileopen_chk_cb(void* r _U_, const char* p, guint len _U_, const void* u1 _U_, const void* u2 _U_, const char** err)
+{
+    ws_statb64 st;
+
+    if (!p || strlen(p) == 0u) {
+        *err = ep_strdup_printf("No filename given.");
+        return FALSE;
+    } else {
+        if (ws_stat64(p, &st) != 0) {
+            *err = ep_strdup_printf("File '%s' does not exist or access is denied.", p);
+            return FALSE;
+        }
+    }
+
+    *err = NULL;
+    return TRUE;
+}
+
+gboolean
+ssldecrypt_uat_fld_password_chk_cb(void* r _U_, const char* p, guint len _U_, const void* u1 _U_, const void* u2 _U_, const char ** err)
+{
+    ssldecrypt_assoc_t*  f  = (ssldecrypt_assoc_t *)r;
+    FILE                *fp = NULL;
+
+    if (p && (strlen(p) > 0u)) {
+        fp = ws_fopen(f->keyfile, "rb");
+        if (fp) {
+            const char *msg = NULL;
+            if (!ssl_load_pkcs12(fp, p, &msg)) {
+                fclose(fp);
+                *err = ep_strdup_printf("Could not load PKCS#12 key file: %s", msg);
+                return FALSE;
+            }
+            fclose(fp);
+        } else {
+            *err = ep_strdup_printf("Leave this field blank if the keyfile is not PKCS#12.");
+            return FALSE;
+        }
+    }
+
+    *err = NULL;
+    return TRUE;
+}
+
+/*
+ * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
+ *
+ * Local variables:
+ * c-basic-offset: 4
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ *
+ * vi: set shiftwidth=4 tabstop=8 expandtab:
+ * :indentSize=4:tabSize=8:noTabs=true:
+ */