*/
{
/* decrypted challenge/session key buffer */
- unsigned char crypt_buf[1024];
+ unsigned char *crypt_buf;
struct sb_buffer sbb;
ret = recv_sb(ct, &sbb);
}
n = sbb.iov.iov_len;
PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n);
- ret = apc_priv_decrypt(ct->key_file, crypt_buf,
+ ret = apc_priv_decrypt(ct->key_file, &crypt_buf,
sbb.iov.iov_base, n);
free(sbb.iov.iov_base);
if (ret < 0)
goto out;
if (ret != APC_CHALLENGE_SIZE + 2 * SESSION_KEY_LEN) {
+ free(crypt_buf);
ret = -E_DECRYPT;
goto out;
}
SESSION_KEY_LEN);
ct->scc.recv = sc_new(crypt_buf + APC_CHALLENGE_SIZE
+ SESSION_KEY_LEN, SESSION_KEY_LEN);
+ free(crypt_buf);
PARA_INFO_LOG("--> %s\n", buf);
ct->status = CL_RECEIVED_CHALLENGE;
return 0;
* Decrypt a buffer using a private key.
*
* \param key_file Full path of the key.
- * \param outbuf The output buffer.
+ * \param outbuf The output buffer is allocated by the callee.
* \param inbuf The encrypted input buffer.
* \param inlen The length of \a inbuf.
*
*
* \return The size of the recovered plaintext on success, negative on errors.
*/
-int apc_priv_decrypt(const char *key_file, unsigned char *outbuf,
+int apc_priv_decrypt(const char *key_file, unsigned char **outbuf,
unsigned char *inbuf, int inlen);
/**
free(key);
}
-static int decode_rsa(gcry_sexp_t sexp, unsigned char *outbuf, size_t *nbytes)
+static int decode_rsa(gcry_sexp_t sexp, unsigned char **outbuf, size_t *nbytes)
{
const char *p = gcry_sexp_nth_data(sexp, 1, nbytes);
- if (!p)
+ if (!p) {
+ *outbuf = NULL;
return -E_RSA_DECODE;
- memcpy(outbuf, p, *nbytes);
+ }
+ *outbuf = alloc(*nbytes);
+ memcpy(*outbuf, p, *nbytes);
return 1;
}
-int apc_priv_decrypt(const char *key_file, unsigned char *outbuf,
+int apc_priv_decrypt(const char *key_file, unsigned char **outbuf,
unsigned char *inbuf, int inlen)
{
gcry_error_t gret;
gcry_sexp_t in, out, priv_key;
size_t nbytes;
+ *outbuf = NULL;
ret = check_private_key_file(key_file);
if (ret < 0)
return ret;
free(pub);
}
-int apc_priv_decrypt(const char *key_file, unsigned char *outbuf,
+int apc_priv_decrypt(const char *key_file, unsigned char **outbuf,
unsigned char *inbuf, int inlen)
{
struct asymmetric_key *priv;
int ret;
+ *outbuf = NULL;
ret = check_private_key_file(key_file);
if (ret < 0)
return ret;
ret = -E_BLINDING;
if (RSA_blinding_on(priv->rsa, NULL) == 0)
goto out;
- ret = RSA_private_decrypt(inlen, inbuf, outbuf, priv->rsa,
+ *outbuf = alloc(RSA_size(priv->rsa));
+ ret = RSA_private_decrypt(inlen, inbuf, *outbuf, priv->rsa,
RSA_PKCS1_OAEP_PADDING);
RSA_blinding_off(priv->rsa);
- if (ret <= 0)
+ if (ret <= 0) {
+ free(*outbuf);
+ *outbuf = NULL;
ret = -E_DECRYPT;
+ }
out:
RSA_free(priv->rsa);
free(priv);