From 413835f5d158acb14147e9f1c4f85b9c518b1fa6 Mon Sep 17 00:00:00 2001 From: Richard Levitte Date: Mon, 3 Aug 2020 21:01:35 +0200 Subject: PEM: Make general MSBLOB reader functions exposed internally Fly-by fix is to move crypto/include/internal/pem_int.h to include/internal/pem.h. Reviewed-by: Shane Lontis (Merged from https://github.com/openssl/openssl/pull/12574) --- crypto/pem/pvkfmt.c | 51 +++++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 22 deletions(-) (limited to 'crypto/pem') diff --git a/crypto/pem/pvkfmt.c b/crypto/pem/pvkfmt.c index 6d85a8a4e1..3745a1c1e3 100644 --- a/crypto/pem/pvkfmt.c +++ b/crypto/pem/pvkfmt.c @@ -20,7 +20,7 @@ #include "internal/cryptlib.h" #include -#include "internal/pem_int.h" +#include "internal/pem.h" #include #include #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA) @@ -186,28 +186,27 @@ static unsigned int blob_length(unsigned bitlen, int isdss, int ispub) } -static EVP_PKEY *do_b2i(const unsigned char **in, unsigned int length, - int ispub) +EVP_PKEY *ossl_b2i(const unsigned char **in, unsigned int length, int *ispub) { const unsigned char *p = *in; unsigned int bitlen, magic; int isdss; - if (ossl_do_blob_header(&p, length, &magic, &bitlen, &isdss, &ispub) <= 0) { - PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_HEADER_PARSE_ERROR); + if (ossl_do_blob_header(&p, length, &magic, &bitlen, &isdss, ispub) <= 0) { + PEMerr(0, PEM_R_KEYBLOB_HEADER_PARSE_ERROR); return NULL; } length -= 16; - if (length < blob_length(bitlen, isdss, ispub)) { - PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_TOO_SHORT); + if (length < blob_length(bitlen, isdss, *ispub)) { + PEMerr(0, PEM_R_KEYBLOB_TOO_SHORT); return NULL; } if (isdss) - return b2i_dss(&p, bitlen, ispub); + return b2i_dss(&p, bitlen, *ispub); else - return b2i_rsa(&p, bitlen, ispub); + return b2i_rsa(&p, bitlen, *ispub); } -static EVP_PKEY *do_b2i_bio(BIO *in, int ispub) +EVP_PKEY *ossl_b2i_bio(BIO *in, int *ispub) { const unsigned char *p; unsigned char hdr_buf[16], *buf = NULL; @@ -215,33 +214,33 @@ static EVP_PKEY *do_b2i_bio(BIO *in, int ispub) int isdss; EVP_PKEY *ret = NULL; if (BIO_read(in, hdr_buf, 16) != 16) { - PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT); + PEMerr(0, PEM_R_KEYBLOB_TOO_SHORT); return NULL; } p = hdr_buf; - if (ossl_do_blob_header(&p, 16, &magic, &bitlen, &isdss, &ispub) <= 0) + if (ossl_do_blob_header(&p, 16, &magic, &bitlen, &isdss, ispub) <= 0) return NULL; - length = blob_length(bitlen, isdss, ispub); + length = blob_length(bitlen, isdss, *ispub); if (length > BLOB_MAX_LENGTH) { - PEMerr(PEM_F_DO_B2I_BIO, PEM_R_HEADER_TOO_LONG); + PEMerr(0, PEM_R_HEADER_TOO_LONG); return NULL; } buf = OPENSSL_malloc(length); if (buf == NULL) { - PEMerr(PEM_F_DO_B2I_BIO, ERR_R_MALLOC_FAILURE); + PEMerr(0, ERR_R_MALLOC_FAILURE); goto err; } p = buf; if (BIO_read(in, buf, length) != (int)length) { - PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT); + PEMerr(0, PEM_R_KEYBLOB_TOO_SHORT); goto err; } if (isdss) - ret = b2i_dss(&p, bitlen, ispub); + ret = b2i_dss(&p, bitlen, *ispub); else - ret = b2i_rsa(&p, bitlen, ispub); + ret = b2i_rsa(&p, bitlen, *ispub); err: OPENSSL_free(buf); @@ -391,22 +390,30 @@ static EVP_PKEY *b2i_rsa(const unsigned char **in, EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length) { - return do_b2i(in, length, 0); + int ispub = 0; + + return ossl_b2i(in, length, &ispub); } EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length) { - return do_b2i(in, length, 1); + int ispub = 1; + + return ossl_b2i(in, length, &ispub); } EVP_PKEY *b2i_PrivateKey_bio(BIO *in) { - return do_b2i_bio(in, 0); + int ispub = 0; + + return ossl_b2i_bio(in, &ispub); } EVP_PKEY *b2i_PublicKey_bio(BIO *in) { - return do_b2i_bio(in, 1); + int ispub = 1; + + return ossl_b2i_bio(in, &ispub); } static void write_ledword(unsigned char **out, unsigned int dw) -- cgit v1.2.3