Strip BOM on loading PEM files

Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Viktor Dukhovni <openssl-users@dukhovni.org>
This commit is contained in:
Dmitry Belyavskiy 2019-10-06 22:25:10 +03:00 committed by Viktor Dukhovni
parent 1b0d1bf7f1
commit 7c43eb5dcf
3 changed files with 45 additions and 3 deletions

View File

@ -680,9 +680,20 @@ int PEM_read(FILE *fp, char **name, char **header, unsigned char **data,
#endif
/* Some helpers for PEM_read_bio_ex(). */
static int sanitize_line(char *linebuf, int len, unsigned int flags)
static int sanitize_line(char *linebuf, int len, unsigned int flags, int first_call)
{
int i;
if (first_call) {
/* Other BOMs imply unsupported multibyte encoding,
* so don't strip them and let the error raise */
const unsigned char utf8_bom[3] = {0xEF, 0xBB, 0xBF};
if (len > 3 && memcmp(linebuf, utf8_bom, 3) == 0) {
memmove(linebuf, linebuf + 3, len - 3);
linebuf[len - 3] = 0;
len -= 3;
}
}
if (flags & PEM_FLAG_EAY_COMPATIBLE) {
/* Strip trailing whitespace */
@ -727,6 +738,7 @@ static int get_name(BIO *bp, char **name, unsigned int flags)
char *linebuf;
int ret = 0;
int len;
int first_call = 1;
/*
* Need to hold trailing NUL (accounted for by BIO_gets() and the newline
@ -747,7 +759,8 @@ static int get_name(BIO *bp, char **name, unsigned int flags)
}
/* Strip trailing garbage and standardize ending. */
len = sanitize_line(linebuf, len, flags & ~PEM_FLAG_ONLY_B64);
len = sanitize_line(linebuf, len, flags & ~PEM_FLAG_ONLY_B64, first_call);
first_call = 0;
/* Allow leading empty or non-matching lines. */
} while (strncmp(linebuf, beginstr, BEGINLEN) != 0
@ -819,7 +832,7 @@ static int get_header_and_data(BIO *bp, BIO **header, BIO **data, char *name,
}
if (!strncmp(linebuf, endstr, ENDLEN) || got_header == IN_HEADER)
flags_mask &= ~PEM_FLAG_ONLY_B64;
len = sanitize_line(linebuf, len, flags & flags_mask);
len = sanitize_line(linebuf, len, flags & flags_mask, 0);
/* Check for end of header. */
if (linebuf[0] == '\n') {

View File

@ -32,6 +32,7 @@ my %cert_expected = (
"cert-256line.pem" => 1,
"cert-257line.pem" => 1,
"cert-blankline.pem" => 0,
"cert-bom.pem" => 1,
"cert-comment.pem" => 0,
"cert-earlypad.pem" => 0,
"cert-extrapad.pem" => 0,

View File

@ -0,0 +1,28 @@
-----BEGIN CERTIFICATE-----
MIIEzDCCA7QCCQCgxkRox+YljjANBgkqhkiG9w0BAQsFADCCASYxYzBhBgNVBAgM
WlRoZSBHcmVhdCBTdGF0ZSBvZiBMb25nLVdpbmRlZCBDZXJ0aWZpY2F0ZSBGaWVs
ZCBOYW1lcyBXaGVyZWJ5IHRvIEluY3JlYXNlIHRoZSBPdXRwdXQgU2l6ZTEfMB0G
A1UEBwwWVG9vbWFueWNoYXJhY3RlcnN2aWxsZTFIMEYGA1UECgw/VGhlIEJlbmV2
b2xlbnQgU29jaWV0eSBvZiBMb3F1YWNpb3VzIGFuZCBQbGVvbmFzdGljIFBlcmlw
aHJhc2lzMT0wOwYDVQQLDDRFbmRvcnNlbWVudCBvZiBWb3VjaHNhZmUnZCBFdmlk
ZW50aWFyeSBDZXJ0aWZpY2F0aW9uMRUwEwYDVQQDDAxjZXJ0LmV4YW1wbGUwHhcN
MTcwMjIzMjAyNTM2WhcNMTcwMzI1MjAyNTM2WjCCASYxYzBhBgNVBAgMWlRoZSBH
cmVhdCBTdGF0ZSBvZiBMb25nLVdpbmRlZCBDZXJ0aWZpY2F0ZSBGaWVsZCBOYW1l
cyBXaGVyZWJ5IHRvIEluY3JlYXNlIHRoZSBPdXRwdXQgU2l6ZTEfMB0GA1UEBwwW
VG9vbWFueWNoYXJhY3RlcnN2aWxsZTFIMEYGA1UECgw/VGhlIEJlbmV2b2xlbnQg
U29jaWV0eSBvZiBMb3F1YWNpb3VzIGFuZCBQbGVvbmFzdGljIFBlcmlwaHJhc2lz
MT0wOwYDVQQLDDRFbmRvcnNlbWVudCBvZiBWb3VjaHNhZmUnZCBFdmlkZW50aWFy
eSBDZXJ0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