Open pem files in binary mode

In order to avoid an MSVCRT bug affecting ftell and text mode[1], open PEM files
in binary mode. The PEM parser already handles CRLF translation[2].

[1] 8300a8742b
[2] https://github.com/openssl/openssl/pull/24249#issuecomment-2192025429

Reviewed-by: Viktor Dukhovni <viktor@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/25716)

(cherry picked from commit 4f20de0c8a)
This commit is contained in:
Josh Cooper 2024-10-16 15:06:16 -07:00 committed by Tomas Mraz
parent 43eea71e56
commit 53db212c4f
3 changed files with 20 additions and 0 deletions

View file

@ -422,7 +422,11 @@ static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id,
EVP_PKEY *key;
fprintf(stderr, "(TEST_ENG_OPENSSL_PKEY)Loading Private key %s\n",
key_id);
# if defined(OPENSSL_SYS_WINDOWS)
in = BIO_new_file(key_id, "rb");
# else
in = BIO_new_file(key_id, "r");
# endif
if (!in)
return NULL;
key = PEM_read_bio_PrivateKey(in, NULL, 0, NULL);

View file

@ -50,7 +50,11 @@ X509 *TS_CONF_load_cert(const char *file)
BIO *cert = NULL;
X509 *x = NULL;
#if defined(OPENSSL_SYS_WINDOWS)
if ((cert = BIO_new_file(file, "rb")) == NULL)
#else
if ((cert = BIO_new_file(file, "r")) == NULL)
#endif
goto end;
x = PEM_read_bio_X509_AUX(cert, NULL, NULL, NULL);
end:
@ -67,7 +71,11 @@ STACK_OF(X509) *TS_CONF_load_certs(const char *file)
STACK_OF(X509_INFO) *allcerts = NULL;
int i;
#if defined(OPENSSL_SYS_WINDOWS)
if ((certs = BIO_new_file(file, "rb")) == NULL)
#else
if ((certs = BIO_new_file(file, "r")) == NULL)
#endif
goto end;
if ((othercerts = sk_X509_new_null()) == NULL)
goto end;
@ -98,7 +106,11 @@ EVP_PKEY *TS_CONF_load_key(const char *file, const char *pass)
BIO *key = NULL;
EVP_PKEY *pkey = NULL;
#if defined(OPENSSL_SYS_WINDOWS)
if ((key = BIO_new_file(file, "rb")) == NULL)
#else
if ((key = BIO_new_file(file, "r")) == NULL)
#endif
goto end;
pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, (char *)pass);
end:

View file

@ -228,7 +228,11 @@ int X509_load_cert_crl_file_ex(X509_LOOKUP *ctx, const char *file, int type,
if (type != X509_FILETYPE_PEM)
return X509_load_cert_file_ex(ctx, file, type, libctx, propq);
#if defined(OPENSSL_SYS_WINDOWS)
in = BIO_new_file(file, "rb");
#else
in = BIO_new_file(file, "r");
#endif
if (in == NULL) {
ERR_raise(ERR_LIB_X509, ERR_R_BIO_LIB);
return 0;