]> git.ipfire.org Git - thirdparty/postfix.git/commitdiff
Bitrot: OpenSSL 1.1.0 DH structure is opaque
authorViktor Dukhovni <postfix-users@dukhovni.org>
Sat, 20 Aug 2016 05:54:36 +0000 (01:54 -0400)
committerViktor Dukhovni <postfix-users@dukhovni.org>
Sun, 21 Aug 2016 19:14:36 +0000 (15:14 -0400)
Backport DHE code refactoring from 3.0, but keep the default DH
size at 1024 for backwards-compatible behaviour with earlier stable
2.11 releases.

postfix/src/tls/tls_dh.c

index be126c9a1ecd22249939915fdc2f5493ab436f2c..bb1e29b1902237d3fb807b3744eaea0c7b04884f 100644 (file)
 /* Application-specific. */
 
  /*
-  * Compiled-in EDH primes (the compiled-in generator is always 2). These are
-  * used when no parameters are explicitly loaded from a site-specific file.
+  * Compiled-in DH parameters.  Used when no parameters are explicitly loaded
+  * from a site-specific file.  Using an ASN.1 DER encoding avoids the need
+  * to explicitly manipulate the internal representation of DH parameter
+  * objects.
   * 
   * 512-bit parameters are used for export ciphers, and 1024-bit parameters are
-  * used for non-export ciphers. An ~80-bit strong EDH key exchange is really
-  * too weak to protect 128+ bit keys, but larger DH primes are
-  * computationally expensive. When greater security is required, use EECDH.
+  * used for non-export ciphers.  It is recommended that users override the
+  * built-in 1024-bit default with a 2048-bit group and/or use EECDH.
   */
 
- /*
-  * Generated via "openssl dhparam -2 -noout -C 512 2>/dev/null" TODO:
-  * generate at compile-time.
+ /*-
+  * Generated via:
+  *   $ openssl dhparam -2 -outform DER 512 2>/dev/null |
+  *     hexdump -ve '/1 "0x%02x, "' | fmt
+  * TODO: generate at compile-time. But that is no good for the majority of
+  * sites that install pre-compiled binaries, and breaks reproducible builds.
+  * Instead, generate at installation time and use main.cf configuration.
   */
-static unsigned char dh512_p[] = {
-    0x88, 0x3F, 0x00, 0xAF, 0xFC, 0x0C, 0x8A, 0xB8, 0x35, 0xCD, 0xE5, 0xC2,
-    0x0F, 0x55, 0xDF, 0x06, 0x3F, 0x16, 0x07, 0xBF, 0xCE, 0x13, 0x35, 0xE4,
-    0x1C, 0x1E, 0x03, 0xF3, 0xAB, 0x17, 0xF6, 0x63, 0x50, 0x63, 0x67, 0x3E,
-    0x10, 0xD7, 0x3E, 0xB4, 0xEB, 0x46, 0x8C, 0x40, 0x50, 0xE6, 0x91, 0xA5,
-    0x6E, 0x01, 0x45, 0xDE, 0xC9, 0xB1, 0x1F, 0x64, 0x54, 0xFA, 0xD9, 0xAB,
-    0x4F, 0x70, 0xBA, 0x5B,
+static unsigned char dh512_der[] = {
+    0x30, 0x46, 0x02, 0x41, 0x00, 0xd8, 0xbf, 0x11, 0xd6, 0x41, 0x2a, 0x7a,
+    0x9c, 0x78, 0xb2, 0xaa, 0x41, 0x23, 0x0a, 0xdc, 0xcf, 0xb7, 0x19, 0xc5,
+    0x16, 0x4c, 0xcb, 0x4a, 0xd0, 0xd2, 0x1f, 0x1f, 0x70, 0x24, 0x86, 0x6f,
+    0x51, 0x52, 0xc6, 0x5b, 0x28, 0xbb, 0x82, 0xe1, 0x24, 0x91, 0x3d, 0x4d,
+    0x95, 0x56, 0xf8, 0x0b, 0x2c, 0xe0, 0x36, 0x67, 0x88, 0x64, 0x15, 0x1f,
+    0x45, 0xd5, 0xb8, 0x0a, 0x00, 0x03, 0x76, 0x32, 0x0b, 0x02, 0x01, 0x02,
 };
 
- /*
-  * Generated via "openssl dhparam -2 -noout -C 1024 2>/dev/null" TODO:
-  * generate at compile-time.
+ /*-
+  * Generated via:
+  *   $ openssl dhparam -2 -outform DER 1024 2>/dev/null |
+  *     hexdump -ve '/1 "0x%02x, "' | fmt
+  * TODO: generate at compile-time. But that is no good for the majority of
+  * sites that install pre-compiled binaries, and breaks reproducible builds.
+  * Instead, generate at installation time and use main.cf configuration.
   */
-static unsigned char dh1024_p[] = {
-    0xB0, 0xFE, 0xB4, 0xCF, 0xD4, 0x55, 0x07, 0xE7, 0xCC, 0x88, 0x59, 0x0D,
-    0x17, 0x26, 0xC5, 0x0C, 0xA5, 0x4A, 0x92, 0x23, 0x81, 0x78, 0xDA, 0x88,
-    0xAA, 0x4C, 0x13, 0x06, 0xBF, 0x5D, 0x2F, 0x9E, 0xBC, 0x96, 0xB8, 0x51,
-    0x00, 0x9D, 0x0C, 0x0D, 0x75, 0xAD, 0xFD, 0x3B, 0xB1, 0x7E, 0x71, 0x4F,
-    0x3F, 0x91, 0x54, 0x14, 0x44, 0xB8, 0x30, 0x25, 0x1C, 0xEB, 0xDF, 0x72,
-    0x9C, 0x4C, 0xF1, 0x89, 0x0D, 0x68, 0x3F, 0x94, 0x8E, 0xA4, 0xFB, 0x76,
-    0x89, 0x18, 0xB2, 0x91, 0x16, 0x90, 0x01, 0x99, 0x66, 0x8C, 0x53, 0x81,
-    0x4E, 0x27, 0x3D, 0x99, 0xE7, 0x5A, 0x7A, 0xAF, 0xD5, 0xEC, 0xE2, 0x7E,
-    0xFA, 0xED, 0x01, 0x18, 0xC2, 0x78, 0x25, 0x59, 0x06, 0x5C, 0x39, 0xF6,
-    0xCD, 0x49, 0x54, 0xAF, 0xC1, 0xB1, 0xEA, 0x4A, 0xF9, 0x53, 0xD0, 0xDF,
-    0x6D, 0xAF, 0xD4, 0x93, 0xE7, 0xBA, 0xAE, 0x9B,
+static unsigned char dh1024_der[] = {
+    0x30, 0x81, 0x87, 0x02, 0x81, 0x81, 0x00, 0xe2, 0x5a, 0x94, 0xe3, 0xf7,
+    0xa6, 0x73, 0x2a, 0x4f, 0x31, 0x2e, 0xf9, 0x91, 0x39, 0x6d, 0x84, 0xee,
+    0x44, 0x95, 0x68, 0xc8, 0x50, 0x39, 0x82, 0x9b, 0x65, 0x3b, 0xe7, 0x37,
+    0xbf, 0xc7, 0x91, 0xf0, 0x66, 0x15, 0x5b, 0x27, 0xab, 0x7e, 0x25, 0xf6,
+    0x94, 0x1d, 0xfd, 0x3b, 0xd3, 0x78, 0x29, 0xfb, 0x63, 0x2a, 0xd0, 0x87,
+    0x01, 0x0c, 0x3a, 0x29, 0xac, 0x20, 0x59, 0xc5, 0x01, 0x88, 0xd6, 0x09,
+    0xf1, 0x64, 0x37, 0x63, 0x1c, 0x36, 0xbe, 0x99, 0xc1, 0x43, 0x90, 0x4a,
+    0x78, 0x1b, 0x06, 0xd9, 0x75, 0x82, 0x6e, 0xc4, 0x9a, 0x01, 0x03, 0x3d,
+    0x43, 0x7f, 0xc8, 0xca, 0x14, 0x77, 0x4d, 0x27, 0xc1, 0xef, 0x5b, 0xe5,
+    0x62, 0xfb, 0xc5, 0x41, 0x1d, 0x30, 0x01, 0xa2, 0xaf, 0x9a, 0xe7, 0x16,
+    0xd6, 0xa2, 0xaa, 0x93, 0xce, 0x95, 0xf8, 0x94, 0x08, 0x53, 0xcd, 0x7d,
+    0x45, 0x31, 0x4b, 0x02, 0x01, 0x02,
 };
 
  /*
@@ -151,6 +161,14 @@ void    tls_set_dh_from_file(const char *path, int bits)
        msg_panic("Invalid DH parameters size %d, file %s", bits, path);
     }
 
+    /*
+     * This function is the first to set the DH parameters, but free any
+     * prior value just in case the call sequence changes some day.
+     */
+    if (*dhPtr) {
+       DH_free(*dhPtr);
+       *dhPtr = 0;
+    }
     if ((paramfile = fopen(path, "r")) != 0) {
        if ((*dhPtr = PEM_read_DHparams(paramfile, 0, 0, 0)) == 0) {
            msg_warn("cannot load %d-bit DH parameters from file %s"
@@ -166,24 +184,18 @@ void    tls_set_dh_from_file(const char *path, int bits)
 
 /* tls_get_dh - get compiled-in DH parameters */
 
-static DH *tls_get_dh(const unsigned char *p, int plen)
+static DH *tls_get_dh(const unsigned char *p, size_t plen)
 {
-    DH     *dh;
-    static unsigned char g[] = {0x02,};
+    const unsigned char *endp = p;
+    DH     *dh = 0;
 
-    /* Use the compiled-in parameters. */
-    if ((dh = DH_new()) == 0) {
-       msg_warn("cannot create DH parameter set: %m"); /* 200411 */
-       return (0);
-    }
-    dh->p = BN_bin2bn(p, plen, (BIGNUM *) 0);
-    dh->g = BN_bin2bn(g, 1, (BIGNUM *) 0);
-    if ((dh->p == 0) || (dh->g == 0)) {
-       msg_warn("cannot load compiled-in DH parameters");      /* 200411 */
-       DH_free(dh);                            /* 200411 */
-       return (0);
-    }
-    return (dh);
+    if (d2i_DHparams(&dh, &endp, plen) && plen == endp - p)
+       return (dh);
+
+    msg_warn("cannot load compiled-in DH parameters");
+    if (dh)
+       DH_free(dh);
+    return (0);
 }
 
 /* tls_tmp_dh_cb - call-back for Diffie-Hellman parameters */
@@ -194,11 +206,11 @@ DH     *tls_tmp_dh_cb(SSL *unused_ssl, int export, int keylength)
 
     if (export && keylength == 512) {          /* 40-bit export cipher */
        if (dh_512 == 0)
-           dh_512 = tls_get_dh(dh512_p, (int) sizeof(dh512_p));
+           dh_512 = tls_get_dh(dh512_der, sizeof(dh512_der));
        dh_tmp = dh_512;
     } else {                                   /* ADH, DHE-RSA or DSA */
        if (dh_1024 == 0)
-           dh_1024 = tls_get_dh(dh1024_p, (int) sizeof(dh1024_p));
+           dh_1024 = tls_get_dh(dh1024_der, sizeof(dh1024_der));
        dh_tmp = dh_1024;
     }
     return (dh_tmp);
@@ -255,10 +267,12 @@ int     tls_set_eecdh_curve(SSL_CTX *server_ctx, const char *grade)
     ERR_clear_error();
     if ((ecdh = EC_KEY_new_by_curve_name(nid)) == 0
        || SSL_CTX_set_tmp_ecdh(server_ctx, ecdh) == 0) {
+       EC_KEY_free(ecdh);                      /* OK if NULL */
        msg_warn("unable to use curve \"%s\": disabling EECDH support", curve);
        tls_print_errors();
        return (0);
     }
+    EC_KEY_free(ecdh);
 #endif
     return (1);
 }