diff options
author | Alexander Færøy <ahf@torproject.org> | 2020-06-30 13:47:55 +0000 |
---|---|---|
committer | Alexander Færøy <ahf@torproject.org> | 2020-06-30 13:47:55 +0000 |
commit | c3ad2a1d2367d6c0ba5c1c81491770056e881a77 (patch) | |
tree | 317e52ac2f707bdb91332cda285ec6538c32673a /src | |
parent | bebdd2888fd43510ff7786c6c8f52cd8a5565742 (diff) | |
parent | 5721ec22d8be99a6f24fa69d51d0cbdc4ff21739 (diff) | |
download | tor-c3ad2a1d2367d6c0ba5c1c81491770056e881a77.tar.gz tor-c3ad2a1d2367d6c0ba5c1c81491770056e881a77.zip |
Merge branch 'tor-github/pr/1785' into maint-0.3.5
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/encoding/pem.c | 8 | ||||
-rw-r--r-- | src/test/test_pem.c | 30 |
2 files changed, 37 insertions, 1 deletions
diff --git a/src/lib/encoding/pem.c b/src/lib/encoding/pem.c index 24b238b130..51f37d0840 100644 --- a/src/lib/encoding/pem.c +++ b/src/lib/encoding/pem.c @@ -85,13 +85,19 @@ pem_decode(uint8_t *dest, size_t destlen, const char *src, size_t srclen, src = eat_whitespace_eos(src, eos); char *tag = NULL; - tor_asprintf(&tag, "-----BEGIN %s-----\n", objtype); + tor_asprintf(&tag, "-----BEGIN %s-----", objtype); if ((size_t)(eos-src) < strlen(tag) || fast_memneq(src, tag, strlen(tag))) { tor_free(tag); return -1; } src += strlen(tag); tor_free(tag); + /* At this point we insist on spaces (including CR), then an LF. */ + src = eat_whitespace_eos_no_nl(src, eos); + if (src == eos || *src != '\n') { + /* Extra junk at end of line: this isn't valid. */ + return -1; + } // NOTE lack of trailing \n. We do not enforce its presence. tor_asprintf(&tag, "\n-----END %s-----", objtype); diff --git a/src/test/test_pem.c b/src/test/test_pem.c index 865688b1a7..9eb99181e2 100644 --- a/src/test/test_pem.c +++ b/src/test/test_pem.c @@ -115,8 +115,38 @@ test_crypto_pem_decode(void *arg) ; } +static void +test_crypto_pem_decode_crlf(void *arg) +{ + (void)arg; + char crlf_version[4096]; + uint8_t buf[4096]; + + /* Convert 'expected' to a version with CRLF instead of LF. */ + const char *inp = expected; + char *outp = crlf_version; + while (*inp) { + if (*inp == '\n') { + *outp++ = '\r'; + } + *outp++ = *inp++; + } + *outp = 0; + + /* Decoding should succeed (or else we have bug 33032 again) */ + int n = pem_decode(buf, sizeof(buf), + crlf_version, strlen(crlf_version), + "WOMBAT QUOTE"); + tt_int_op(n, OP_EQ, strlen(example_pre)); + tt_mem_op(buf, OP_EQ, example_pre, n); + + done: + ; +} + struct testcase_t pem_tests[] = { { "encode", test_crypto_pem_encode, 0, NULL, NULL }, { "decode", test_crypto_pem_decode, 0, NULL, NULL }, + { "decode_crlf", test_crypto_pem_decode_crlf, 0, NULL, NULL }, END_OF_TESTCASES }; |