Bug #5170 - simplify i2d_X509

This commit is contained in:
Marek Majkowski 2013-06-06 12:45:25 +01:00
parent 6f1c67195c
commit a022930fda

View File

@ -806,24 +806,24 @@ tor_cert_new(X509 *x509_cert)
tor_cert_t *cert; tor_cert_t *cert;
EVP_PKEY *pkey; EVP_PKEY *pkey;
RSA *rsa; RSA *rsa;
int length, length2; int length;
unsigned char *cp; unsigned char *buf = NULL;
if (!x509_cert) if (!x509_cert)
return NULL; return NULL;
length = i2d_X509(x509_cert, NULL); length = i2d_X509(x509_cert, &buf);
cert = tor_malloc_zero(sizeof(tor_cert_t)); cert = tor_malloc_zero(sizeof(tor_cert_t));
if (length <= 0) { if (length <= 0 || buf == NULL) {
tor_free(cert); tor_free(cert);
log_err(LD_CRYPTO, "Couldn't get length of encoded x509 certificate"); log_err(LD_CRYPTO, "Couldn't get length of encoded x509 certificate");
X509_free(x509_cert); X509_free(x509_cert);
return NULL; return NULL;
} }
cert->encoded_len = (size_t) length; cert->encoded_len = (size_t) length;
cp = cert->encoded = tor_malloc(length); cert->encoded = tor_malloc(length);
length2 = i2d_X509(x509_cert, &cp); memcpy(cert->encoded, buf, length);
tor_assert(length2 == length); OPENSSL_free(buf);
cert->cert = x509_cert; cert->cert = x509_cert;