]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - openssl.c
apc_pub_encrypt: Let the callee allocate the buffer.
[paraslash.git] / openssl.c
index 495d83c2bae0671a7f7d9f0e75a89900bfb4cecb..acf1120dd139aeb121a1533108f0f07b24dc6e61 100644 (file)
--- a/openssl.c
+++ b/openssl.c
@@ -310,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 {