Repository navigation
Conversation
|
There was a problem hiding this comment.
🟡 Changes recommended
Portability failures, unsafe failure paths, RNG lifetime defects, and mutex initialization races remain unresolved.
10 open findings
Protect rngInited reads with the mutex · New Assert holder size without rejecting valid tail padding · New Synchronize lazy mutex initialization · New Clear copied PKEY context before deep-copy failure · New Prevent RSA from retaining a dangling EVP_PKEY RNG · New Propagate deferred-encoding failure · New Free decoded key object in test · New Correct GCM IV comment mislabeled as CCM · New Correct ARIA-GCM IV comment mislabeled as CCM · New Correct SM4-GCM IV comment · New
What changed in this PR
Optimizes OpenSSL compatibility APIs while correcting EVP, PEM, CCM, Base64, key-lifecycle, and X509 caching behavior.
Changes:
- Defers costly RNG and DER initialization and caches decoded X509 public keys.
- Optimizes cipher/digest lookup, Base64 processing, and PEM BIO parsing.
- Expands compatibility and regression coverage.
| File | Description |
|---|---|
wolfssl/wolfcrypt/types.h |
Adds XMEMCHR portability support. |
wolfssl/wolfcrypt/coding.h |
Declares streaming Base64 decoding. |
wolfssl/ssl.h |
Tracks deferred PKEY state. |
wolfssl/openssl/sha3.h |
Corrects SHA3 holder sizing. |
wolfssl/openssl/rsa.h |
Tracks deferred RSA RNG state. |
wolfssl/openssl/evp.h |
Adds CCM message state. |
wolfssl/internal.h |
Adds cached X509 key support. |
wolfcrypt/src/wc_port.c |
Implements wc_memchr. |
wolfcrypt/src/evp_pk.c |
Integrates deferred DER generation. |
wolfcrypt/src/coding.c |
Optimizes Base64 encoding and decoding. |
wolfcrypt/src/evp.c |
Implements EVP optimizations and compatibility fixes. |
wolfcrypt/src/aes.c |
Removes invalid GMULT macros. |
tests/api/test_port.c |
Tests wc_memchr. |
tests/api/test_ossl_x509_pk.h |
Registers X509 cache tests. |
tests/api/test_ossl_x509_pk.c |
Tests cached-key reference handling. |
tests/api/test_ossl_x509_io.h |
Registers PEM BIO tests. |
tests/api/test_ossl_x509_io.c |
Tests multi-certificate BIO parsing. |
tests/api/test_ossl_rsa.h |
Registers lazy-RNG tests. |
tests/api/test_ossl_rsa.c |
Tests deferred RSA blinding RNGs. |
tests/api/test_evp.h |
Registers chunked Base64 tests. |
tests/api/test_evp.c |
Tests chunk-independent encoding. |
tests/api/test_evp_pkey.h |
Registers deferred-DER tests. |
tests/api/test_evp_pkey.c |
Tests lazy PKEY encoding and ownership. |
tests/api/test_evp_digest.h |
Registers digest regression tests. |
tests/api/test_evp_digest.c |
Tests digest context lookup and copying. |
tests/api/test_evp_cipher.h |
Registers cipher and CCM tests. |
tests/api/test_evp_cipher.c |
Tests CCM semantics and cipher lookup. |
tests/api/test_coding.h |
Registers Base64 vectors. |
tests/api/test_coding.c |
Adds Base64 known-answer tests. |
src/x509.c |
Caches keys and optimizes PEM parsing. |
src/ssl.c |
Enables shared RNG use where safe. |
src/ssl_p7p12.c |
Ensures DER before PKCS operations. |
src/ssl_load.c |
Ensures DER before loading keys. |
src/ssl_crypto.c |
Adds SHA3 size validation. |
src/pk.c |
Ensures DER before key serialization. |
src/pk_rsa.c |
Defers RSA blinding RNG creation. |
src/internal.c |
Releases cached X509 keys. |
🧠 Review effort: Balanced
Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.
35ad6ac to
2295f64
Compare
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #11689
Scan targets checked: wolfcrypt-src, wolfcrypt-bugs
Failed targets: wolfssl-src, wolfssl-bugs
Coverage: 12 of 24 in-scope changed file(s) opened by the reviewer; not opened: src/ssl_crypto.c, src/ssl_load.c, src/ssl_p7p12.c, tests/api.c, tests/api/test_coding.c, tests/api/test_evp.c, tests/api/test_evp_cipher.c, wolfssl/internal.h, wolfssl/openssl/evp.h, wolfssl/openssl/rsa.h and 2 more
Findings: 2
2 finding(s) posted as inline comments (see file-level comments below)
This review was generated automatically by Fenrir. Reported findings require changes before merge.
Review tier: Lite
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
7 open findings
TheinConsumedcalculation relies oninLenreflecting all consumed bytes, but there are code… · New MappingEvpPkeyEnsureDer()failures toBAD_FUNC_ARGconflates “internal failure to produce DER”… · New MappingEvpPkeyEnsureDer()failures toBAD_FUNC_ARGconflates “internal failure to produce DER”… · NewEvpPkeyEnsureDer(pkey)is invoked twice on the same path (once during validation and again after… · NewEvpPkeyEnsureDer(pkey)is invoked twice on the same path (once during validation and again after… · Newwolfssl_i_evp_pkey_get_der()takes aconst WOLFSSL_EVP_PKEY*but now callsEvpPkeyEnsureDer()… · New The newly added comment and assignment are visually mis-indented relative to the surrounding block,… · New
10 resolved since last review
Prevent RSA from retaining a dangling EVP_PKEY RNG Clear copied PKEY context before deep-copy failure Synchronize lazy mutex initialization Assert holder size without rejecting valid tail padding Protect rngInited reads with the mutex Free decoded key object in test Propagate deferred-encoding failure Correct SM4-GCM IV comment Correct ARIA-GCM IV comment mislabeled as CCM Correct GCM IV comment mislabeled as CCM
🧠 Review effort: Lite
2295f64 to
3da039a
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Lazy DER synchronization, PKCS7 handling, and CCM state transitions contain unresolved correctness issues.
4 open findings
7 resolved since last review
TheinConsumedcalculation relies oninLenreflecting all consumed bytes, but there are code…wolfssl_i_evp_pkey_get_der()takes aconst WOLFSSL_EVP_PKEY*but now callsEvpPkeyEnsureDer()…EvpPkeyEnsureDer(pkey)is invoked twice on the same path (once during validation and again after…EvpPkeyEnsureDer(pkey)is invoked twice on the same path (once during validation and again after… MappingEvpPkeyEnsureDer()failures toBAD_FUNC_ARGconflates “internal failure to produce DER”… MappingEvpPkeyEnsureDer()failures toBAD_FUNC_ARGconflates “internal failure to produce DER”… The newly added comment and assignment are visually mis-indented relative to the surrounding block,…
🧠 Review effort: Balanced
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #11689
Scan targets checked: wolfcrypt-src, wolfcrypt-bugs
Failed targets: wolfssl-src, wolfssl-bugs
Coverage: 13 of 24 in-scope changed file(s) opened by the reviewer; not opened: src/ssl_crypto.c, tests/api.c, tests/api/test_coding.c, tests/api/test_evp.c, tests/api/test_evp_cipher.c, wolfcrypt/src/aes.c, wolfssl/internal.h, wolfssl/openssl/rsa.h, wolfssl/openssl/sha3.h, wolfssl/ssl.h and 1 more
Findings: 4
4 finding(s) posted as inline comments (see file-level comments below)
This review was generated automatically by Fenrir. Reported findings require changes before merge.
Review tier: Lite
3da039a to
cb36a34
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Lazy DER failure paths can leak resources, crash on empty EC keys, or corrupt retry metadata, and one supported test configuration does not compile portably.
4 open findings
4 resolved since last review
🧠 Review effort: Balanced
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #11689
Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs
Coverage: 13 of 32 in-scope changed file(s) opened by the reviewer; not opened: src/internal.c, src/ssl_crypto.c, tests/api.c, tests/api/test_coding.c, tests/api/test_evp.c, tests/api/test_evp_digest.c, tests/api/test_evp_pkey.c, tests/api/test_ossl_pem.c, tests/api/test_ossl_rsa.c, tests/api/test_ossl_x509_io.c and 9 more
Findings: 3
3 finding(s) posted as inline comments (see file-level comments below)
This review was generated automatically by Fenrir. Reported findings require changes before merge.
Review tier: Lite
cb36a34 to
dcf8a6c
Compare
There was a problem hiding this comment.
🟡 Changes recommended
The new streaming rollback paths lack chunked whitespace tests for incomplete base64 quartets, leaving input-consumption regressions that could lose or duplicate decoded bytes undetected.
1 open issue
2 resolved since last review
Review effort: Balanced
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #11689
Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): .wolfssl_known_macro_extras, tests/api/test_coding.h, tests/api/test_evp.h, tests/api/test_evp_cipher.h, tests/api/test_evp_digest.h, tests/api/test_evp_pkey.h, tests/api/test_ossl_rsa.h, tests/api/test_ossl_x509_io.h
Coverage: 16 of 30 in-scope changed file(s) opened by the reviewer; not opened: tests/api.c, tests/api/test_coding.c, tests/api/test_evp.c, tests/api/test_evp_cipher.c, tests/api/test_evp_digest.c, tests/api/test_evp_pkey.c, tests/api/test_ossl_rsa.c, tests/api/test_ossl_x509_io.c, tests/api/test_port.c, wolfssl/internal.h and 4 more
Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)
This review was generated automatically by Fenrir. Reported findings require changes before merge.
Review tier: Lite
9e19be4 to
0fc4008
Compare
|
@claude review |
There was a problem hiding this comment.
Findings marked 🟡 are optional suggestions and need no follow-up push.
Additional findings (outside the current diff — GitHub can't attach inline comments there):
-
🔴
wolfcrypt/src/evp.c:10177— FIPS builds: RSA keys attached with EVP_PKEY_set1_RSA() can no longer be used for private decryption. EVP_PKEY_decrypt and RSA_private_decrypt now fail because the key has no blinding RNG. set1_RSA now lends the pkey's RNG only whenkey->rngInitedis set (evp.c:10169). Under HAVE_FIPS, wolfssl_rsa_ensure_rng() never sets that flag, so the key keeps a NULL rng. Fix: under HAVE_FIPS, set1_RSA should still give the RSA key an RNG (EvpPkeyRng(pkey)) when it has none, or ensure_rng should provide one in FIPS builds as well.Why this was flagged
In a HAVE_FIPS build with OPENSSL_EXTRA and WC_RSA_BLINDING (the harden default), wolfSSL_RSA_new_ex() has never set an RNG, because that block is guarded by !HAVE_FIPS. On the base branch, wolfSSL_EVP_PKEY_set1_RSA() ran
if (key->ownRng == 0) wc_RsaSetRNG(internal, &pkey->rng). ownRng is always 0 in FIPS, so every RSA key attached that way got the pkey's seeded RNG. The new code at evp.c:10169 lends it only whenkey->rngInited && key->ownRng == 0. wolfssl_rsa_ensure_rng() (pk_rsa.c:288) does nothing under HAVE_FIPS and never sets rngInited, so the lend never happens. wolfSSL_EVP_PKEY_decrypt (evp.c:3295) then calls wolfSSL_RSA_private_decrypt(). That function skips its new ensure_rng check under HAVE_FIPS (pk_rsa.c:3745) and calls wc_RsaPrivateDecrypt(), which takes key->rng (rsa.c:4438). With a NULL rng, blinding fails with MISSING_RNG_E (mp_rand or the SP path), and decryption that worked on the base now returns -1. Signing is unaffected because it passes an RNG explicitly through WOLFSSL_RSA_GetRNG().Verification: On the base, set1_RSA ran
if (key->ownRng == 0) wc_RsaSetRNG(internal, &pkey->rng)unconditionally (base evp.c:9646-9653). The head lends the RNG onlyif (key->rngInited && (key->ownRng == 0))(evp.c:10169). wolfssl_rsa_ensure_rng() does nothing under HAVE_FIPS, so rngInited is never set in FIPS builds. So RSA_private_decrypt returns -1.
f55123d to
840012d
Compare
|
@claude review |
There was a problem hiding this comment.
🟡 Changes recommended
The test requires successful RSA serialization in FIPS/self-test builds without WOLFSSL_KEY_TO_DER, causing failures when the encoder is intentionally unavailable.
1 open issue
1 resolved since last review
Review effort: Balanced
…ound Performance: - Size WOLFSSL_SHA3_CTX's holder in pointers, not bytes. It reserved sizeof(wc_Sha3) pointers for a 440 byte object, so WOLFSSL_Hasher goes 3520 -> 448 bytes and WOLFSSL_EVP_MD_CTX 3552 -> 944. A static assert now catches the same mistake. - Resolve digests and ciphers by table pointer before comparing names, and return the table's own pointer from the accessors. EVP_sha256() 47.4 -> 1.2ns, EVP_MD_type 64.7 -> 4.3ns, EVP_CIPHER_nid 101.2 -> 8.1ns. - Copy only the fields past the hash union in EVP_MD_CTX_copy_ex, and leave the cipher union alone in EVP_CIPHER_CTX_init. - Set up an EVP_PKEY's RNG, an RSA key's blinding RNG and an EVP_PKEY's DER encoding on first use rather than at creation. Seeding a DRBG cost more than everything else about making a key put together. EVP_PKEY_new + free 13366 -> 34ns, RSA_new + free 13748 -> 230ns, EVP_PKEY_set1_RSA 13994 -> 45ns. Certificate parsing roughly halves as a result: d2i_X509 45978 -> 17198ns, X509_dup 45945 -> 17116ns, both now faster than OpenSSL. set1_DSA still encodes immediately, as WOLFSSL_DSA is not reference counted and the pkey only borrows the caller's key. - Keep the decoded public key on the X509 and hand out a reference, as OpenSSL does. X509_get_pubkey + free 27692 -> 9.8ns, against 9.6ns. - Give public key operations the shared global RNG instead of seeding one per call, where the instance locks itself or the build is single threaded. WOLFSSL_PK_LOCAL_RNG forces an RNG per call. EVP_DigestSign with ECDSA P-256 32291 -> 17008ns, against 18852ns. - Encode base64 without a call per output character, and hand whole blocks to Base64_Encode() rather than one 48 byte line at a time. EVP_EncodeUpdate 6446 -> 1855ns per 4KB. Skip the call into Base64_SkipNewline() for characters needing no skipping: EVP_DecodeUpdate 37059 -> 16194ns. What remains there is the constant time character decode, which is deliberate; wc_PemToDer() already picks the table decoder by PEM type. - Search cipher_tbl before the alias table in EVP_get_cipherbyname(), since over half the aliases only spell a name already there in another case, and compare without the platform's strcasecmp: 330 -> 67ns. The comparison is ASCII only, so it no longer follows the locale and works on platforms whose XSTRCASECMP is strcmp. - Find the PEM footer in a memory BIO's buffer and read up to it, rather than reading a byte at a time through the whole input. Loading a certificate from a file that carries a text dump alongside it 100311 -> 23301ns, against 33006ns. Fixes: - AES-CCM through EVP now behaves as OpenSSL's does: the payload length comes from the first Update() carrying payload, AAD before the length is refused, and Final() has nothing left to do. Of 376 observables compared against OpenSSL - the tag and IV length matrices, the defaults, length inference over 48 sizes, and an encrypt/decrypt matrix with the tag then corrupted - 375 agree. The one that differs, EVP_CIPHER_iv_length() reporting CCM_NONCE_MIN_SZ where OpenSSL reports 12, is unchanged by this work. - EVP_get_cipherbyname(NULL) and EVP_get_digestbyname(NULL) dereferenced the name; OpenSSL returns NULL. - EVP_EncodeUpdate() wrote the block it was holding and then wrote over it, so a caller feeding data in pieces got wrong bytes, including bytes never written. - EVP_PKEY_keygen() on DH read the parameters after replacing the key object they came from, which is the same object when a caller passes its own key back in. - EVP_PKEY_set1_EC_KEY() with an empty EC_KEY now succeeds. It reported failure only as a side effect of encoding eagerly; OpenSSL returns 1 and the empty key is caught when the encoding is asked for. - Remove three GMULT macros naming functions that do not exist. Portability: - Add XMEMCHR, with wc_memchr() for builds naming their own string functions through STRING_USER that have no memchr among them. Tests: - Known answer vectors for the base64 encoders, generated independently of wolfSSL. testwolfcrypt passed with the encoder deliberately corrupted, as it only checked return codes. - EVP_EncodeUpdate() over every chunking of ten sizes straddling the 48 byte block, and PEM_read_bio_X509() reading several certificates from one BIO while leaving nothing behind after one. - EVP_get_cipherbyname() over canonical names, alternative spellings, every casing of each, and NULL. - One RSA private key operation per freshly decoded key: sharing a key lets the first operation provide the blinding RNG and hides a missing one in the rest. Plus EVP_PKEY_set1_DSA() followed by the caller releasing its key. - The reference counting behind the cached X509 public key: outliving the certificate and the caller, replacement, and duplication. - wc_memchr(), guarded by USE_WOLF_MEMCHR so it runs where that function is built.
840012d to
88a01d3
Compare
julek-wolfssl
left a comment
There was a problem hiding this comment.
Please address the review in new commits.
| /* The blinding RNG seeds a DRBG, which costs far more than everything else | ||
| * about creating an RSA object put together, and only private key | ||
| * operations ever use it. It is set up on first use, and this records | ||
| * whether that has happened. */ | ||
| WC_BITFIELD rngInited:1; |
There was a problem hiding this comment.
This belongs in the RsaKey object. Additionally I think it would be better to just not allocate the rng object. That way a NULL check is sufficient to know if it was set up.
| /* The DER encoding of the key is cached in pkey.ptr. A set1_* records the | ||
| * key and sets this instead of encoding there and then, because most keys | ||
| * are never asked for their encoding - EvpPkeyEnsureDer() produces it when | ||
| * something does ask. */ | ||
| WC_BITFIELD derStale:1; |
There was a problem hiding this comment.
This stale DER handling is adding a lot of code overhead itself. Please share what is motivating this. Generating the DER encoding of a key is not a crypto operation so it should not be adding huge overhead.
|
|
||
| /* Compare two cipher names without regard to case. Returns 1 when they match. | ||
| * | ||
| * Used instead of XSTRCASECMP because a lookup walks the whole name table and | ||
| * this runs once per entry: it is a few instructions inline rather than a call, | ||
| * and it stops at the first character that differs. It also behaves the same | ||
| * everywhere, where XSTRCASECMP is the platform's strcasecmp, which follows the | ||
| * locale, or on platforms without one is plain strcmp, which is not case | ||
| * insensitive at all. Cipher names are ASCII, so only A-Z is folded. */ | ||
| static int EvpCipherNameEq(const char* a, const char* b) | ||
| { | ||
| for (;;) { | ||
| char ca = *a++; | ||
| char cb = *b++; | ||
|
|
||
| if (ca != cb) { | ||
| /* Differ as written; compare them with case folded away. */ | ||
| if ((ca >= 'A') && (ca <= 'Z')) { | ||
| ca = (char)(ca - 'A' + 'a'); | ||
| } | ||
| if ((cb >= 'A') && (cb <= 'Z')) { | ||
| cb = (char)(cb - 'A' + 'a'); | ||
| } | ||
| if (ca != cb) { | ||
| return 0; | ||
| } | ||
| } | ||
| else if (ca == '\0') { | ||
| /* Ran out together, so every character matched. */ | ||
| return 1; | ||
| } | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
The reason to not use XSTRCASECMP directly doesn't convince me. I prefer we own less code.
| #ifdef WOLFSSL_EVP_PKEY_LENDS_RSA_RNG | ||
| /* Withdraw the blinding RNG that set1_RSA() lent the RSA key. | ||
| * | ||
| * The key is reference counted in its own right and the caller keeps a | ||
| * reference, so it can outlive this pkey. The borrowed pointer has to go | ||
| * before the pkey's RNG does, or a later private key operation reads freed | ||
| * memory. wolfssl_rsa_ensure_rng() gives the key an RNG of its own when it is | ||
| * next used. */ | ||
| static void EvpPkeyUnlendRsaRng(WOLFSSL_EVP_PKEY *pkey) | ||
| { | ||
| RsaKey* internal; | ||
|
|
||
| if ((pkey == NULL) || (!pkey->rngInited) || (pkey->rsa == NULL) || | ||
| (pkey->rsa->internal == NULL)) { | ||
| return; | ||
| } | ||
|
|
||
| internal = (RsaKey*)pkey->rsa->internal; | ||
| if (internal->rng == &pkey->rng) { | ||
| internal->rng = NULL; | ||
| /* Back to having none, which is what ensure_rng() looks for. */ | ||
| pkey->rsa->rngInited = 0; | ||
| pkey->rsa->ownRng = 0; | ||
| } | ||
| } | ||
| #endif /* WOLFSSL_EVP_PKEY_LENDS_RSA_RNG */ |
There was a problem hiding this comment.
This kind of ownership juggling is going to cause edge cases. Please have one owner and that's it. Preferably the top level object.
|
|
||
| /* TODO: Inefficient | ||
| * reading in one byte at a time until see the footer | ||
| */ | ||
| /* A memory source can be looked at where it already sits, so the footer is | ||
| * found without reading anything and the bytes up to and including it are | ||
| * then taken in one read. The loop below pays a BIO read for every | ||
| * character of the input, which for a file carrying a text dump alongside | ||
| * the PEM block is most of the work. The reason it goes a byte at a time is | ||
| * that it must not consume past the footer - a caller reads a chain of | ||
| * certificates by calling again - and that is what examining the buffer | ||
| * first avoids. | ||
| * | ||
| * Only a memory BIO with nothing chained after it qualifies. | ||
| * wolfSSL_BIO_get_mem_data() reports the last memory BIO in a chain while | ||
| * wolfSSL_BIO_read() reads from the tail, so on a chain the two can be | ||
| * different BIOs - and matching pending counts would not prove otherwise. | ||
| * The footer check below would then fail only after end bytes had already | ||
| * been taken from the real source, and the byte-at-a-time loop does not | ||
| * look back inside them, so a footer ending there would be missed. With | ||
| * no next BIO the buffer scanned is the one the read consumes. The footer | ||
| * is still checked for again in what was read, before it is used. */ | ||
| if ((wolfSSL_BIO_method_type(bp) == WOLFSSL_BIO_MEMORY) && | ||
| (wolfSSL_BIO_next(bp) == NULL)) { | ||
| const byte* mem = NULL; | ||
| int avail = wolfSSL_BIO_get_mem_data(bp, (void*)&mem); | ||
|
|
||
| if ((mem != NULL) && (avail > 0) && | ||
| (avail == wolfSSL_BIO_pending(bp)) && (avail <= pemSz)) { | ||
| long footerEnd = 0; /* bytes up to and including the footer */ | ||
| long end; | ||
| long k; | ||
|
|
||
| /* The same test the loop below makes, at the same offsets. */ | ||
| /* A footer can only end where its last character is, so look for | ||
| * that character and test only those places, rather than testing | ||
| * every byte. The C library searches several bytes at a time where | ||
| * this would do one. | ||
| * | ||
| * Both footers in use end with the same character, so the one | ||
| * search offers up the candidates for either. Were that not so | ||
| * there would be no single character to search for, and the walk | ||
| * below serves instead. */ | ||
| if ((altFooter == NULL) || | ||
| (footer[footerSz-1] == altFooter[altFooterSz-1])) { | ||
| byte lastCh = (byte)footer[footerSz-1]; | ||
| long pos = 0; | ||
|
|
||
| while (pos < (long)avail) { | ||
| const byte* at = (const byte*)XMEMCHR(mem + pos, lastCh, | ||
| (size_t)(avail - pos)); | ||
|
|
||
| if (at == NULL) { | ||
| break; | ||
| } | ||
| /* Past pos, so the search always moves on. */ | ||
| k = (long)(at - mem) + 1; | ||
|
|
||
| if (k > footerSz && | ||
| XMEMCMP(&mem[k-footerSz], footer, | ||
| (size_t)footerSz) == 0) { | ||
| footerEnd = k; | ||
| break; | ||
| } | ||
| if (k > altFooterSz && altFooter != NULL && | ||
| XMEMCMP(&mem[k-altFooterSz], altFooter, | ||
| (size_t)altFooterSz) == 0) { | ||
| footerEnd = k; | ||
| break; | ||
| } | ||
| pos = k; | ||
| } | ||
| } | ||
| else | ||
| /* The two footers end differently, so there is no one character to | ||
| * search for and every byte is tested. */ | ||
| for (k = 1; k <= (long)avail; k++) { | ||
| if (k > footerSz && | ||
| mem[k-1] == (byte)footer[footerSz-1] && | ||
| XMEMCMP(&mem[k-footerSz], footer, | ||
| (size_t)footerSz) == 0) { | ||
| footerEnd = k; | ||
| break; | ||
| } | ||
| if (k > altFooterSz && altFooter != NULL && | ||
| mem[k-1] == (byte)altFooter[altFooterSz-1] && | ||
| XMEMCMP(&mem[k-altFooterSz], altFooter, | ||
| (size_t)altFooterSz) == 0) { | ||
| footerEnd = k; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (footerEnd > 0) { | ||
| /* Take the line ending after the footer too, which is what the | ||
| * loop below does once it has found it. */ | ||
| end = footerEnd; | ||
| if (end < (long)avail) { | ||
| end++; | ||
| if ((mem[end-1] == '\r') && (end < (long)avail)) { | ||
| end++; | ||
| } | ||
| } | ||
| } | ||
| else { | ||
| /* No footer anywhere, which is what the loop below would be | ||
| * left with after consuming the lot. */ | ||
| end = (long)avail; | ||
| } | ||
|
|
||
| if (wolfSSL_BIO_read(bp, (char *)pem, (int)end) == (int)end) { | ||
| int footerOk = 1; | ||
|
|
||
| i = end; | ||
| if (footerEnd > 0) { | ||
| /* Where the scan said the footer ended, not where the read | ||
| * ended: the line ending comes after it. */ | ||
| footerOk = (footerEnd >= footerSz) && | ||
| (XMEMCMP((char *)&pem[footerEnd-footerSz], footer, | ||
| (size_t)footerSz) == 0); | ||
| if ((!footerOk) && (altFooter != NULL) && | ||
| (footerEnd >= altFooterSz)) { | ||
| footerOk = XMEMCMP((char *)&pem[footerEnd-altFooterSz], | ||
| altFooter, (size_t)altFooterSz) == 0; | ||
| } | ||
| } | ||
| if (footerOk) { | ||
| l = 0; | ||
| goto scanned; | ||
| } | ||
| /* Not the footer after all, so keep reading as before. A footer | ||
| * can still turn up later in the input. */ | ||
| } | ||
| else { | ||
| i = 0; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /* Sources that cannot be examined in place read a byte at a time until the | ||
| * footer appears, since reading more than that would take data belonging to | ||
| * whatever follows. */ | ||
| while ((l = wolfSSL_BIO_read(bp, (char *)&pem[i], 1)) == 1) { |
There was a problem hiding this comment.
A custom BIO_METHOD registered as BIO_TYPE_MEM passes the look-ahead's checks, and if its read callback returns a short read, the bytes already consumed are dropped before the byte loop takes over, so the certificate is lost. The old one-byte-at-a-time loop never reads past what it has checked, so it is correct for every BIO type, and a performance gain only on PEM with a lot of surrounding text doesn't justify about 120 lines with this kind of edge case.



Description
Performance:
Fixes:
Portability:
Tests:
Testing
Tested on ARM64 with SM algorithms.
Note: wolfsm fixes required:
wolfSSL/wolfsm#37