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/include/internal/pem_int.h | 23 ------------------ crypto/pem/pvkfmt.c | 51 ++++++++++++++++++++++----------------- crypto/store/loader_file.c | 2 +- include/internal/pem.h | 31 ++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 46 deletions(-) delete mode 100644 crypto/include/internal/pem_int.h create mode 100644 include/internal/pem.h diff --git a/crypto/include/internal/pem_int.h b/crypto/include/internal/pem_int.h deleted file mode 100644 index c8f90528c3..0000000000 --- a/crypto/include/internal/pem_int.h +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved. - * - * Licensed under the OpenSSL license (the "License"). You may not use - * this file except in compliance with the License. You can obtain a copy - * in the file LICENSE in the source distribution or at - * https://www.openssl.org/source/license.html - */ - -#ifndef HEADER_PEM_INT_H -# define HEADER_PEM_INT_H - -# include - -/* Found in crypto/pem/pvkfmt.c */ -int ossl_do_blob_header(const unsigned char **in, unsigned int length, - unsigned int *pmagic, unsigned int *pbitlen, - int *pisdss, int *pispub); -int ossl_do_PVK_header(const unsigned char **in, unsigned int length, - int skip_magic, - unsigned int *psaltlen, unsigned int *pkeylen); - -#endif 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) diff --git a/crypto/store/loader_file.c b/crypto/store/loader_file.c index 5ff93e33ab..da4e96b989 100644 --- a/crypto/store/loader_file.c +++ b/crypto/store/loader_file.c @@ -21,7 +21,7 @@ #include #include #include -#include "internal/pem_int.h" +#include "internal/pem.h" #include /* For the PKCS8 stuff o.O */ #include /* For d2i_RSAPrivateKey */ #include diff --git a/include/internal/pem.h b/include/internal/pem.h new file mode 100644 index 0000000000..b6a10241f3 --- /dev/null +++ b/include/internal/pem.h @@ -0,0 +1,31 @@ +/* + * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef OSSL_INTERNAL_PEM_H +# define OSSL_INTERNAL_PEM_H + +# include + +# ifndef OPENSSL_NO_DSA +/* Found in crypto/pem/pvkfmt.c */ +int ossl_do_blob_header(const unsigned char **in, unsigned int length, + unsigned int *pmagic, unsigned int *pbitlen, + int *pisdss, int *pispub); +# ifndef OPENSSL_NO_RC4 +int ossl_do_PVK_header(const unsigned char **in, unsigned int length, + int skip_magic, + unsigned int *psaltlen, unsigned int *pkeylen); +# endif + +EVP_PKEY *ossl_b2i(const unsigned char **in, unsigned int length, int *ispub); +EVP_PKEY *ossl_b2i_bio(BIO *in, int *ispub); + +# endif + +#endif -- cgit v1.2.3