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 <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/12574)
This commit is contained in:
Richard Levitte 2020-08-03 21:01:35 +02:00
parent 6ce6ad39fe
commit 413835f5d1
3 changed files with 40 additions and 25 deletions

View File

@ -20,7 +20,7 @@
#include "internal/cryptlib.h"
#include <openssl/pem.h>
#include "internal/pem_int.h"
#include "internal/pem.h"
#include <openssl/rand.h>
#include <openssl/bn.h>
#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)

View File

@ -21,7 +21,7 @@
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include "internal/pem_int.h"
#include "internal/pem.h"
#include <openssl/pkcs12.h> /* For the PKCS8 stuff o.O */
#include <openssl/rsa.h> /* For d2i_RSAPrivateKey */
#include <openssl/safestack.h>

View File

@ -7,17 +7,25 @@
* https://www.openssl.org/source/license.html
*/
#ifndef HEADER_PEM_INT_H
# define HEADER_PEM_INT_H
#ifndef OSSL_INTERNAL_PEM_H
# define OSSL_INTERNAL_PEM_H
# include <openssl/pem.h>
# 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