Merge branch 'refs/heads/t/openssl-1.1'
authorAndre Noll <maan@tuebingen.mpg.de>
Tue, 27 Dec 2016 15:29:29 +0000 (16:29 +0100)
committerAndre Noll <maan@tuebingen.mpg.de>
Tue, 27 Dec 2016 15:31:07 +0000 (16:31 +0100)
In openssl-1.1 several structures have been made opaque, breaking both
the stream cipher and the public key functions in crypt.c. This series
deals with these issues, trying to minimize the ifdeffery.

* refs/heads/t/openssl-1.1 (cooking for three months):
  openssl: RSA fixes for openssl-1.1.
  openssl: Use EVP API for AES.

NEWS.md
configure.ac
crypt.c

diff --git a/NEWS.md b/NEWS.md
index 37d855b9cb22edeff12b0a9bc09f23dda39ef608..cad0169bfd4a5f5d2ff38cdd145e30414719e42c 100644 (file)
--- a/NEWS.md
+++ b/NEWS.md
@@ -11,6 +11,7 @@ NEWS
 - Two race conditions in para_server have been fixed.
 - ls -p is now deprecated in favor of -F or -b. See the help text of
   the ls command for details.
+- The openssl code has been adjusted to work also with openssl-1.1.
 
 Download: [tarball](./releases/paraslash-git.tar.bz2)
 
index 8d4ce59a0eec619086b0e8234903efaed945ef37..74f3fd97f97cd1228ca76fadbda92bd77b5896bd 100644 (file)
@@ -116,6 +116,10 @@ LIB_ARG_WITH([openssl], [-lssl -lcrypto])
 AC_CHECK_HEADER(openssl/ssl.h, [], [HAVE_OPENSSL=no])
 AC_CHECK_LIB([crypto], [RAND_bytes], [], [HAVE_OPENSSL=no])
 LIB_SUBST_FLAGS(openssl)
+if test $HAVE_OPENSSL = yes; then
+       AC_CHECK_LIB([crypto], [RSA_set0_key],
+               AC_DEFINE([HAVE_RSA_SET0_KEY], [1], [openssl-1.1]))
+fi
 UNSTASH_FLAGS
 ######################################################################### gcrypt
 STASH_FLAGS
diff --git a/crypt.c b/crypt.c
index f227eb39907691d24f0eae360c04027c1653be75..8116fb6eeacdbeaa0f1f22d14cfcc3726ee3bbbf 100644 (file)
--- a/crypt.c
+++ b/crypt.c
@@ -134,18 +134,25 @@ static int read_rsa_bignums(const unsigned char *blob, int blen, RSA **result)
 {
        int ret;
        RSA *rsa;
+       BIGNUM *n, *e;
        const unsigned char *p = blob, *end = blob + blen;
 
        rsa = RSA_new();
        if (!rsa)
                return -E_BIGNUM;
-       ret = read_bignum(p, end - p, &rsa->e);
+       ret = read_bignum(p, end - p, &e);
        if (ret < 0)
                goto fail;
        p += ret;
-       ret = read_bignum(p, end - p, &rsa->n);
+       ret = read_bignum(p, end - p, &n);
        if (ret < 0)
                goto fail;
+#ifdef HAVE_RSA_SET0_KEY
+       RSA_set0_key(rsa, n, e, NULL);
+#else
+       rsa->n = n;
+       rsa->e = e;
+#endif
        *result = rsa;
        return 1;
 fail:
@@ -256,27 +263,18 @@ int pub_encrypt(struct asymmetric_key *pub, unsigned char *inbuf,
        return ret < 0? -E_ENCRYPT : ret;
 }
 
-struct aes_ctr_128_context {
-       AES_KEY key;
-       unsigned char ivec[AES_CRT128_BLOCK_SIZE];
-       unsigned char ecount[AES_CRT128_BLOCK_SIZE];
-       unsigned int num;
-};
-
 struct stream_cipher {
        bool use_aes;
        union {
                RC4_KEY rc4_key;
-               struct aes_ctr_128_context aes;
+               EVP_CIPHER_CTX *aes;
        } context;
 };
 
 struct stream_cipher *sc_new(const unsigned char *data, int len,
                bool use_aes)
 {
-       int ret;
        struct stream_cipher *sc = para_malloc(sizeof(*sc));
-       struct aes_ctr_128_context *aes;
 
        sc->use_aes = use_aes;
        if (!use_aes) {
@@ -284,17 +282,17 @@ struct stream_cipher *sc_new(const unsigned char *data, int len,
                return sc;
        }
        assert(len >= 2 * AES_CRT128_BLOCK_SIZE);
-       aes = &sc->context.aes;
-       ret = AES_set_encrypt_key(data, AES_CRT128_BLOCK_SIZE * 8 /* bits */,
-               &aes->key);
-       assert(ret == 0);
-       memcpy(aes->ivec, data + AES_CRT128_BLOCK_SIZE, AES_CRT128_BLOCK_SIZE);
-       aes->num = 0;
+       sc->context.aes = EVP_CIPHER_CTX_new();
+       EVP_EncryptInit_ex(sc->context.aes, EVP_aes_128_ctr(), NULL, data,
+               data + AES_CRT128_BLOCK_SIZE);
        return sc;
 }
 
 void sc_free(struct stream_cipher *sc)
 {
+       if (!sc)
+               return;
+       EVP_CIPHER_CTX_free(sc->context.aes);
        free(sc);
 }
 
@@ -328,25 +326,29 @@ static void rc4_crypt(RC4_KEY *key, struct iovec *src, struct iovec *dst)
        ((char *)dst->iov_base)[len] = '\0';
 }
 
-static void aes_ctr128_crypt(struct aes_ctr_128_context *aes, struct iovec *src,
+static void aes_ctr128_crypt(EVP_CIPHER_CTX *ctx, struct iovec *src,
                struct iovec *dst)
 {
-       size_t len = src->iov_len;
+       int ret, inlen = src->iov_len, outlen, tmplen;
 
        *dst = (typeof(*dst)) {
                /* Add one for the terminating zero byte. */
-               .iov_base = para_malloc(len + 1),
-               .iov_len = len
+               .iov_base = para_malloc(inlen + 1),
+               .iov_len = inlen
        };
-       AES_ctr128_encrypt(src->iov_base, dst->iov_base, len,
-               &aes->key, aes->ivec, aes->ecount, &aes->num);
-       ((char *)dst->iov_base)[len] = '\0';
+       ret = EVP_EncryptUpdate(ctx, dst->iov_base, &outlen, src->iov_base, inlen);
+       assert(ret != 0);
+       ret = EVP_EncryptFinal_ex(ctx, dst->iov_base + outlen, &tmplen);
+       assert(ret != 0);
+       outlen += tmplen;
+       ((char *)dst->iov_base)[outlen] = '\0';
+       dst->iov_len = outlen;
 }
 
 void sc_crypt(struct stream_cipher *sc, struct iovec *src, struct iovec *dst)
 {
        if (sc->use_aes)
-               return aes_ctr128_crypt(&sc->context.aes, src, dst);
+               return aes_ctr128_crypt(sc->context.aes, src, dst);
        return rc4_crypt(&sc->context.rc4_key, src, dst);
 }