]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - openssl.c
openssl: Assume that openssl allocation functions functions succeed.
[paraslash.git] / openssl.c
index f696cd9e83606bc4e6bdd89d666f0885575f1d9f..495d83c2bae0671a7f7d9f0e75a89900bfb4cecb 100644 (file)
--- a/openssl.c
+++ b/openssl.c
@@ -24,15 +24,21 @@ struct asymmetric_key {
        RSA *rsa;
 };
 
+static int openssl_perror(const char *pfx)
+{
+       unsigned long err = ERR_get_error();
+       PARA_ERROR_LOG("%s: \"%s\"\n", pfx, ERR_reason_error_string(err));
+       return -E_OPENSSL;
+}
+
 void get_random_bytes_or_die(unsigned char *buf, int num)
 {
-       unsigned long err;
+       int ret;
 
-       /* RAND_bytes() returns 1 on success, 0 otherwise. */
-       if (RAND_bytes(buf, num) == 1)
+       if (RAND_bytes(buf, num) == 1) /* success */
                return;
-       err = ERR_get_error();
-       PARA_EMERG_LOG("%s\n", ERR_reason_error_string(err));
+       ret = openssl_perror("RAND_bytes");
+       PARA_EMERG_LOG("%s\n", strerror(-ret));
        exit(EXIT_FAILURE);
 }
 
@@ -97,16 +103,15 @@ static int read_bignum(const unsigned char *buf, size_t len, BIGNUM **result)
        return bnsize + 4;
 }
 
-static int read_public_key(const unsigned char *blob, int blen, RSA **result)
+static int read_public_key(const unsigned char *blob, int blen,
+               struct asymmetric_key *result)
 {
        int ret;
        RSA *rsa;
        BIGNUM *n, *e;
        const unsigned char *p = blob, *end = blob + blen;
 
-       rsa = RSA_new();
-       if (!rsa)
-               return -E_BIGNUM;
+       assert((rsa = RSA_new()));
        ret = read_bignum(p, end - p, &e);
        if (ret < 0)
                goto free_rsa;
@@ -120,7 +125,7 @@ static int read_public_key(const unsigned char *blob, int blen, RSA **result)
        rsa->n = n;
        rsa->e = e;
 #endif
-       *result = rsa;
+       result->rsa = rsa;
        return 1;
 free_e:
        BN_free(e);
@@ -129,56 +134,37 @@ free_rsa:
        return ret;
 }
 
-static int read_pem_private_key(const char *path, RSA **rsa)
+static int read_pem_private_key(const char *path, struct asymmetric_key *priv)
 {
        EVP_PKEY *pkey;
-       BIO *bio = BIO_new(BIO_s_file());
+       BIO *bio;
 
-       *rsa = NULL;
-       if (!bio)
-               return -E_PRIVATE_KEY;
+       assert((bio = BIO_new(BIO_s_file())));
+       priv->rsa = NULL;
        if (BIO_read_filename(bio, path) <= 0)
                goto bio_free;
        pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
        if (!pkey)
                goto bio_free;
-       *rsa = EVP_PKEY_get1_RSA(pkey);
+       priv->rsa = EVP_PKEY_get1_RSA(pkey);
        EVP_PKEY_free(pkey);
 bio_free:
        BIO_free(bio);
-       return *rsa? RSA_size(*rsa) : -E_PRIVATE_KEY;
+       return priv->rsa? RSA_size(priv->rsa) : -E_PRIVATE_KEY;
 }
 
 static int read_openssh_private_key(const unsigned char *blob,
-               const unsigned char *end, RSA **result)
+               const unsigned char *end, struct asymmetric_key *priv)
 {
        int ret;
        RSA *rsa;
-       BN_CTX *ctx;
        BIGNUM *n, *e, *d, *iqmp, *p, *q; /* stored in the key file */
-       BIGNUM *dmp1, *dmq1; /* these will be computed */
-       BIGNUM *tmp;
        const unsigned char *cp = blob;
 
-       rsa = RSA_new();
-       if (!rsa)
-               return -E_BIGNUM;
-       ret = -E_BIGNUM;
-       tmp = BN_new();
-       if (!tmp)
-               goto free_rsa;
-       ctx = BN_CTX_new();
-       if (!ctx)
-               goto free_tmp;
-       dmp1 = BN_new();
-       if (!dmp1)
-               goto free_ctx;
-       dmq1 = BN_new();
-       if (!dmq1)
-               goto free_dmp1;
+       assert((rsa = RSA_new()));
        ret = read_bignum(cp, end - cp, &n);
        if (ret < 0)
-               goto free_dmq1;
+               goto free_rsa;
        cp += ret;
        ret = read_bignum(cp, end - cp, &e);
        if (ret < 0)
@@ -199,19 +185,11 @@ static int read_openssh_private_key(const unsigned char *blob,
        ret = read_bignum(cp, end - cp, &q);
        if (ret < 0)
                goto free_p;
-       ret = -E_BIGNUM;
-       if (!BN_sub(tmp, q, BN_value_one()))
-               goto free_q;
-       if (!BN_mod(dmp1, d, tmp, ctx))
-               goto free_q;
-       if (!BN_sub(tmp, q, BN_value_one()))
-               goto free_q;
-       if (!BN_mod(dmq1, d, tmp, ctx))
-               goto free_q;
 #ifdef HAVE_RSA_SET0_KEY
        RSA_set0_key(rsa, n, e, d);
        RSA_set0_factors(rsa, p, q);
-       RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp);
+       RSA_set0_crt_params(rsa, NULL, NULL, iqmp);
+
 #else
        rsa->n = n;
        rsa->e = e;
@@ -219,14 +197,9 @@ static int read_openssh_private_key(const unsigned char *blob,
        rsa->iqmp = iqmp;
        rsa->p = p;
        rsa->q = q;
-       rsa->dmp1 = dmp1;
-       rsa->dmq1 = dmq1;
 #endif
-       *result = rsa;
-       ret = 1;
-       goto free_ctx;
-free_q:
-       BN_clear_free(q);
+       priv->rsa = rsa;
+       return 1;
 free_p:
        BN_clear_free(p);
 free_iqmp:
@@ -237,27 +210,18 @@ free_e:
        BN_free(e);
 free_n:
        BN_free(n);
-free_dmq1:
-       BN_clear_free(dmq1);
-free_dmp1:
-       BN_clear_free(dmp1);
-free_ctx:
-       BN_CTX_free(ctx);
-free_tmp:
-       BN_clear_free(tmp);
 free_rsa:
-       if (ret < 0)
-               RSA_free(rsa);
+       RSA_free(rsa);
        return ret;
 }
 
-static int get_private_key(const char *path, RSA **rsa)
+static int get_private_key(const char *path, struct asymmetric_key *priv)
 {
        int ret;
        unsigned char *blob, *end;
        size_t blob_size;
 
-       *rsa = NULL;
+       priv->rsa = NULL;
        ret = decode_private_key(path, &blob, &blob_size);
        if (ret < 0)
                return ret;
@@ -267,9 +231,9 @@ static int get_private_key(const char *path, RSA **rsa)
                if (ret < 0)
                        goto free_blob;
                PARA_INFO_LOG("reading RSA params at offset %d\n", ret);
-               ret = read_openssh_private_key(blob + ret, end, rsa);
+               ret = read_openssh_private_key(blob + ret, end, priv);
        } else
-               ret = read_pem_private_key(path, rsa);
+               ret = read_pem_private_key(path, priv);
 free_blob:
        free(blob);
        return ret;
@@ -285,7 +249,7 @@ int apc_get_pubkey(const char *key_file, struct asymmetric_key **result)
        ret = decode_public_key(key_file, &blob, &decoded_size);
        if (ret < 0)
                goto out;
-       ret = read_public_key(blob + ret, decoded_size - ret, &pub->rsa);
+       ret = read_public_key(blob + ret, decoded_size - ret, pub);
        if (ret < 0)
                goto free_blob;
        ret = RSA_size(pub->rsa);
@@ -322,7 +286,7 @@ int apc_priv_decrypt(const char *key_file, unsigned char *outbuf,
        if (inlen < 0)
                return -E_RSA;
        priv = alloc(sizeof(*priv));
-       ret = get_private_key(key_file, &priv->rsa);
+       ret = get_private_key(key_file, priv);
        if (ret < 0) {
                free(priv);
                return ret;
@@ -366,7 +330,7 @@ struct stream_cipher *sc_new(const unsigned char *data, int len)
        struct stream_cipher *sc = alloc(sizeof(*sc));
 
        assert(len >= 2 * AES_CRT128_BLOCK_SIZE);
-       sc->aes = EVP_CIPHER_CTX_new();
+       assert((sc->aes = EVP_CIPHER_CTX_new()));
        EVP_EncryptInit_ex(sc->aes, EVP_aes_128_ctr(), NULL, data,
                data + AES_CRT128_BLOCK_SIZE);
        return sc;
@@ -406,8 +370,11 @@ void sc_crypt(struct stream_cipher *sc, struct iovec *src, struct iovec *dst)
 
 void hash_function(const char *data, unsigned long len, unsigned char *hash)
 {
-       EVP_MD_CTX *c = EVP_MD_CTX_new();
-       int ret = EVP_DigestInit_ex(c, EVP_sha1(), NULL);
+       int ret;
+       EVP_MD_CTX *c;
+
+       assert((c = EVP_MD_CTX_new()));
+       ret = EVP_DigestInit_ex(c, EVP_sha1(), NULL);
        assert(ret != 0);
        ret = EVP_DigestUpdate(c, data, len);
        assert(ret != 0);
@@ -418,8 +385,11 @@ void hash_function(const char *data, unsigned long len, unsigned char *hash)
 
 void hash2_function(const char *data, unsigned long len, unsigned char *hash)
 {
-       EVP_MD_CTX *c = EVP_MD_CTX_new();
-       int ret = EVP_DigestInit_ex(c, EVP_sha256(), NULL);
+       int ret;
+       EVP_MD_CTX *c;
+
+       assert((c = EVP_MD_CTX_new()));
+       ret = EVP_DigestInit_ex(c, EVP_sha256(), NULL);
        assert(ret != 0);
        ret = EVP_DigestUpdate(c, data, len);
        assert(ret != 0);