xbps_pubkey2fp: simplify and eliminate the extra copy of returned data type.

This commit is contained in:
Juan RP 2013-11-08 09:10:40 +01:00
parent 89ab9a4b0c
commit 0909c81cdb

View File

@ -52,7 +52,7 @@ xbps_pubkey2fp(struct xbps_handle *xhp, xbps_data_t pubkey)
RSA *pRsa = NULL; RSA *pRsa = NULL;
BIO *bio = NULL; BIO *bio = NULL;
const void *pubkeydata; const void *pubkeydata;
unsigned char *fpstr = NULL, md_value[EVP_MAX_MD_SIZE]; unsigned char *md_value = NULL;
unsigned char *nBytes = NULL, *eBytes = NULL, *pEncoding = NULL; unsigned char *nBytes = NULL, *eBytes = NULL, *pEncoding = NULL;
unsigned int md_len = 0; unsigned int md_len = 0;
int index = 0, nLen = 0, eLen = 0, encodingLength = 0; int index = 0, nLen = 0, eLen = 0, encodingLength = 0;
@ -112,16 +112,16 @@ xbps_pubkey2fp(struct xbps_handle *xhp, xbps_data_t pubkey)
*/ */
EVP_MD_CTX_init(&mdctx); EVP_MD_CTX_init(&mdctx);
EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL); EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL);
assert(EVP_DigestUpdate(&mdctx, pEncoding, encodingLength) != -1); EVP_DigestUpdate(&mdctx, pEncoding, encodingLength);
assert(EVP_DigestFinal_ex(&mdctx, md_value, &md_len) != -1); md_value = malloc(EVP_MAX_MD_SIZE);
if (EVP_DigestFinal_ex(&mdctx, md_value, &md_len) == 0) {
free(md_value);
md_value = NULL;
} else {
md_value[md_len] = '\0';
}
EVP_MD_CTX_cleanup(&mdctx); EVP_MD_CTX_cleanup(&mdctx);
fpstr = malloc(md_len+1);
for (unsigned int i = 0; i < md_len; i++)
fpstr[i] = md_value[i];
fpstr[md_len] = '\0';
error: error:
if (bio) if (bio)
BIO_free_all(bio); BIO_free_all(bio);
@ -139,5 +139,5 @@ error:
EVP_cleanup(); EVP_cleanup();
ERR_free_strings(); ERR_free_strings();
return fpstr; return md_value;
} }