Fix to handle carriage return case in PEM end of line character handling (for Windows). Cleanup to consolidate duplicate end of line character handling code.

This commit is contained in:
David Garske
2018-08-09 08:42:15 -07:00
parent ff7d2fefdc
commit 6ca56ee98c

View File

@@ -7556,6 +7556,15 @@ const char* const END_PUB_KEY = "-----END PUBLIC KEY-----";
#endif #endif
static WC_INLINE char* SkipEndOfLineChars(char* line, const char* endOfLine)
{
/* eat end of line characters */
while (line < endOfLine &&
(line[0] == '\r' || line[0] == '\n')) {
line++;
}
return line;
}
int wc_PemGetHeaderFooter(int type, const char** header, const char** footer) int wc_PemGetHeaderFooter(int type, const char** header, const char** footer)
{ {
@@ -7794,11 +7803,8 @@ static int wc_EncryptedInfoParse(EncryptedInfo* info,
else else
return BUFFER_E; return BUFFER_E;
/* eat blank line */ /* eat end of line characters */
while (newline < bufferEnd && newline = SkipEndOfLineChars(newline, bufferEnd);
(*newline == '\r' || *newline == '\n')) {
newline++;
}
/* return new headerEnd */ /* return new headerEnd */
if (pBuffer) if (pBuffer)
@@ -8051,19 +8057,8 @@ int PemToDer(const unsigned char* buff, long longSz, int type,
headerEnd += XSTRLEN(header); headerEnd += XSTRLEN(header);
if ((headerEnd + 1) >= bufferEnd) /* eat end of line characters */
return BUFFER_E; headerEnd = SkipEndOfLineChars(headerEnd, bufferEnd);
/* eat end of line */
if (headerEnd[0] == '\n')
headerEnd++;
else if (headerEnd[1] == '\n')
headerEnd += 2;
else {
if (info)
info->consumed = (long)(headerEnd+2 - (char*)buff);
return BUFFER_E;
}
if (type == PRIVATEKEY_TYPE) { if (type == PRIVATEKEY_TYPE) {
if (eccKey) { if (eccKey) {
@@ -8096,10 +8091,8 @@ int PemToDer(const unsigned char* buff, long longSz, int type,
consumedEnd = footerEnd + XSTRLEN(footer); consumedEnd = footerEnd + XSTRLEN(footer);
if (consumedEnd < bufferEnd) { /* handle no end of line on last line */ if (consumedEnd < bufferEnd) { /* handle no end of line on last line */
/* eat new line characters */ /* eat end of line characters */
while (consumedEnd < bufferEnd && consumedEnd[0] == '\n') { consumedEnd = SkipEndOfLineChars(consumedEnd, bufferEnd);
consumedEnd++;
}
} }
if (info) if (info)