]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - openssl.c
apc_pub_encrypt: Let the callee allocate the buffer.
[paraslash.git] / openssl.c
index e34169499dcc9bfa14560b41d08cf5516465eec1..acf1120dd139aeb121a1533108f0f07b24dc6e61 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);
 }
 
@@ -105,9 +111,7 @@ static int read_public_key(const unsigned char *blob, int blen,
        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;
@@ -133,11 +137,10 @@ free_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;
 
+       assert((bio = BIO_new(BIO_s_file())));
        priv->rsa = NULL;
-       if (!bio)
-               return -E_PRIVATE_KEY;
        if (BIO_read_filename(bio, path) <= 0)
                goto bio_free;
        pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
@@ -158,9 +161,7 @@ static int read_openssh_private_key(const unsigned char *blob,
        BIGNUM *n, *e, *d, *iqmp, *p, *q; /* stored in the key file */
        const unsigned char *cp = blob;
 
-       rsa = RSA_new();
-       if (!rsa)
-               return -E_BIGNUM;
+       assert((rsa = RSA_new()));
        ret = read_bignum(cp, end - cp, &n);
        if (ret < 0)
                goto free_rsa;
@@ -309,15 +310,22 @@ out:
 }
 
 int apc_pub_encrypt(struct asymmetric_key *pub, unsigned char *inbuf,
-               unsigned len, unsigned char *outbuf)
+               unsigned len, unsigned char **outbuf)
 {
        int ret, flen = len; /* RSA_public_encrypt expects a signed int */
 
+       *outbuf = NULL;
        if (flen < 0)
                return -E_ENCRYPT;
-       ret = RSA_public_encrypt(flen, inbuf, outbuf, pub->rsa,
+       *outbuf = alloc(RSA_size(pub->rsa));
+       ret = RSA_public_encrypt(flen, inbuf, *outbuf, pub->rsa,
                RSA_PKCS1_OAEP_PADDING);
-       return ret < 0? -E_ENCRYPT : ret;
+       if (ret < 0) {
+               free(*outbuf);
+               *outbuf = NULL;
+               return -E_ENCRYPT;
+       }
+       return ret;
 }
 
 struct stream_cipher {
@@ -329,7 +337,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;
@@ -369,8 +377,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);
@@ -381,8 +392,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);