Repository navigation
Add X.509 certificate revocation list support - #586
Conversation
There was a problem hiding this comment.
Superseded by the review of
e9ad065e.
This review, of 71fa34c6
💬 Approved with comments — One WARNING: verify_cert_with_crls accepts a revoked certificate unless the caller has already set the CRL-check verify flags, and its documentation does not say so. No blocking issues.
Findings: 1 warning (1 is an inline comment on the diff)
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
🔧 Fix with your agent
Paste the following into your coding agent to investigate and fix these issues.
Fix the following code review issues found in pull request #586.
Before making changes, review each issue and present a brief summary table:
- For each finding, state whether you agree, disagree, or need clarification
- If you disagree (e.g., the fix would require disproportionate changes
for minimal ROI, or the finding is factually incorrect), explain why
- If you need to clarify something before deciding, ask those questions
- Then share your plan for which issues to tackle and in what order
## WARNING (1)
### [Security] `boring/src/x509/mod.rs:270`: `verify_cert_with_crls` does not enable CRL checking, so a revoked certificate passes unless the caller has separately set `X509_V_FLAG_CRL_CHECK`. The doc comment does not mention this.
- **File:** boring/src/x509/mod.rs:270
🔒 Security: ⚠️ 1 warning
verify_cert_with_crls does not enable CRL checking itself
Recommendation: 📝 Minor Comments
- [WARNING]
boring/src/x509/mod.rs:270:verify_cert_with_crlsdoes not enable CRL checking, so a revoked certificate passes unless the caller has separately setX509_V_FLAG_CRL_CHECK. The doc comment does not mention this. (boring/src/x509/mod.rs:270)
View full review
boring/src/x509/mod.rs:270-288: verify_cert_with_crls passes the supplied CRLs to X509_verify_cert but does not set X509_V_FLAG_CRL_CHECK. The tests have to set the flags separately (boring/src/x509/tests/crl.rs:50-51). A caller who relies on the method name alone gets a successful result for a revoked certificate. The impact depends on how the caller configures verification, so this is not blocking. Document the required flags, or enable CRL checking inside the method.
✅ All other checks passed
📖 AGENTS.md: ✅ LGTM
This change does not call for an AGENTS.md update.
📋 Code Quality: ✅ LGTM
The ownership and lifetime handling across the new CRL, revoked-entry, store and stack code is sound. add_revoked calls mem::forget only after a successful X509_CRL_add0_revoked. The ResetCRLs guard clears the context's CRL pointer before the Stack drops. Fields that can be null are returned as Option. The tests cover store CRLs, untrusted CRLs, a wrong-key signature and DER/PEM round trips.
📝 Documentation: ✅ LGTM
No documentation issues.
Reviewed commit: 71fa34c6
There was a problem hiding this comment.
Superseded by the review of
8b929f57.
This review, of e9ad065e
✅ Approved — The only earlier finding (CRL-check flags not documented) is fixed at this commit, and no new issues were found.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
The doc comment on verify_cert_with_crls (boring/src/x509/mod.rs:268-270) now says that CRL_CHECK is required and that CRL_CHECK_ALL covers the entire chain. No ownership or lifetime problems were found in the CRL handling.
📖 AGENTS.md: ✅ LGTM
This change does not call for an AGENTS.md update.
📋 Code Quality: ✅ LGTM
Ownership and lifetimes in the new CRL, revoked-entry, store and stack code are correct, and the tests can fail on a real regression.
📝 Documentation: ✅ LGTM
No documentation issues.
Reviewed commit: e9ad065e
dotjs
left a comment
There was a problem hiding this comment.
Reviewed at e9ad065. CRL/refcount ownership, add0 transfer, temporary verification-stack lifetime, parsing/serialization, and signature verification look correct. The required CRL_CHECK flags are now explicit in the API documentation. CI is green.
dotjs
left a comment
There was a problem hiding this comment.
One follow-up note on e9ad065e. It does not change my approval, and I read it in the source without reproducing it.
X509Crl is Send + Sync, and clone() shares the same X509_CRL. At the pinned BoringSSL revision, crypto/x509/x_crl.cc sorts a CRL's revoked list lazily, under g_crl_sort_lock, the first time a verification looks up a serial. X509CrlRef::revoked() hands out a &StackRef<X509Revoked> over that same list, and reading it does not take the lock.
So one thread iterating crl.revoked() while another thread verifies a certificate against the same CRL for the first time is a data race reachable from safe code. It is narrow: it needs a shared CRL, a not-yet-sorted list, and the first lookup to overlap an iteration.
Possible fixes, in rough order of cost: sort the list once when the X509Crl is created (after from_der, from_pem and build()), so the lazy sort never runs later; or have revoked() return owned entries instead of a borrowed stack.
Withdrawing my approval: I reproduced a data race between X509CrlRef::revoked() and CRL verification using only the safe API. Details in my comment below.
dotjs
left a comment
There was a problem hiding this comment.
I have withdrawn my approval of e9ad065e. The data race I described in my earlier comment reproduces with the safe public API alone, with no unsafe and no sanitizer. The rest of the PR still looks good to me, so this is the only thing I am asking to be resolved.
What I ran (tests added to a scratch checkout of this head; code below):
| Test | Result |
|---|---|
Single thread: hold crl.revoked(), verify a certificate against a store holding the same CRL, read the list again through the same reference |
The list changed from [8, 7, 6, 5, 4, 3, 2, 1] to [1, 2, 3, 4, 5, 6, 7, 8] |
Two threads: one iterates crl.revoked() summing serials while the other verifies against the same CRL (200,000 entries, fresh CRL each round) |
Wrong sum in 40 of 40 rounds, each time on the first pass |
The first test shows that the data behind a live &StackRef<X509Revoked> is modified by an unrelated safe call. The second shows the consequence across threads: the sums were about 50% too high, so the reader saw duplicated entries while BoringSSL sorted the list in place. I chose 200,000 entries to make the sort slow enough to hit every time; a small CRL would hit far less often, but the bug is the same.
Cause. X509Crl is Send + Sync and clone() shares the X509_CRL. crl_lookup in crypto/x509/x_crl.cc sorts crl->crl->revoked lazily under g_crl_sort_lock on the first serial lookup, and revoked() reads the same stack without that lock.
A possible non-breaking fix. X509_CRL_get0_by_serial is in the generated bindings and performs that lazy sort. Calling it once when an X509Crl is created (from_der, from_pem, build()) would sort the list before it can be shared, so later verifications never write to it. I have not tested this. It would make revoked() report serial order instead of encoded order, but verification already reorders the list as a side effect today. The alternatives, dropping Sync or returning owned entries from revoked(), would be breaking changes once this API has shipped, which is why I would prefer this settled before merge.
Reproduction: boring/src/x509/tests/crl_race.rs (add mod crl_race; to tests/mod.rs, run with cargo test -p boring --lib crl_race -- --nocapture --test-threads=1)
// Scratch reproduction only; uses the safe public API exclusively.
use crate::asn1::{Asn1Integer, Asn1Time};
use crate::bn::BigNum;
use crate::hash::MessageDigest;
use crate::pkey::{PKey, Private};
use crate::stack::Stack;
use crate::x509::crl::{X509Crl, X509CrlBuilder, X509Revoked};
use crate::x509::store::{X509Store, X509StoreBuilder};
use crate::x509::verify::X509VerifyFlags;
use crate::x509::{X509StoreContext, X509};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Barrier};
const ROOT_CA: &[u8] = include_bytes!("../../../test/root-ca.pem");
const ROOT_KEY: &[u8] = include_bytes!("../../../test/root-ca.key");
fn crl_time(offset: libc::time_t) -> Asn1Time {
Asn1Time::from_unix(super::fixture_verify_time() + offset).unwrap()
}
// A CRL whose entries are in descending serial order, so the first lookup has to sort it.
fn big_crl_der(n: u32) -> Vec<u8> {
let root = X509::from_pem(ROOT_CA).unwrap();
let key: PKey<Private> = PKey::private_key_from_pem(ROOT_KEY).unwrap();
let date = crl_time(-43_200);
let mut builder = X509CrlBuilder::new().unwrap();
builder.set_version(1).unwrap();
builder.set_issuer_name(root.subject_name()).unwrap();
builder.set_last_update(&crl_time(-86_400)).unwrap();
builder.set_next_update(&crl_time(86_400)).unwrap();
for serial in (1..=n).rev() {
let serial = Asn1Integer::from_bn(&BigNum::from_u32(serial).unwrap()).unwrap();
builder
.add_revoked(X509Revoked::new(&serial, &date).unwrap())
.unwrap();
}
builder.sign(&key, MessageDigest::sha256()).unwrap();
builder.build().to_der().unwrap()
}
fn store_with(crl: &X509Crl) -> X509Store {
let mut builder = X509StoreBuilder::new().unwrap();
builder.add_cert(X509::from_pem(ROOT_CA).unwrap()).unwrap();
builder.add_crl(crl).unwrap();
builder
.verify_param_mut()
.set_flags(X509VerifyFlags::CRL_CHECK | X509VerifyFlags::CRL_CHECK_ALL);
builder
.verify_param_mut()
.set_time(super::fixture_verify_time());
builder.build()
}
fn verify(store: &X509Store) {
let cert = X509::from_pem(super::TEST_CERT).unwrap();
let chain = Stack::new().unwrap();
let mut context = X509StoreContext::new().unwrap();
let _ = context
.init(store, &cert, &chain, |context| context.verify_cert())
.unwrap();
}
#[test]
fn shared_borrow_is_mutated_by_verification() {
let crl = X509Crl::from_der(&big_crl_der(8)).unwrap();
let revoked = crl.revoked().unwrap(); // shared borrow, held across the verification
let before: Vec<i64> = revoked.iter().map(|r| r.serial_number().get()).collect();
let store = store_with(&crl);
verify(&store);
let after: Vec<i64> = revoked.iter().map(|r| r.serial_number().get()).collect();
eprintln!("before verification: {before:?}");
eprintln!("after verification: {after:?}");
assert_ne!(before, after, "the list behind the live `&StackRef` did not change");
}
#[test]
fn concurrent_iteration_and_verification() {
const N: u32 = 200_000;
const ROUNDS: usize = 40;
let der = big_crl_der(N);
let expected: i64 = (1..=i64::from(N)).sum();
let mut anomalies = 0;
for round in 0..ROUNDS {
let crl = X509Crl::from_der(&der).unwrap(); // fresh, so not yet sorted
let store = store_with(&crl);
let barrier = Arc::new(Barrier::new(2));
let done = Arc::new(AtomicBool::new(false));
let verifier = std::thread::spawn({
let (barrier, done) = (barrier.clone(), done.clone());
move || {
barrier.wait();
verify(&store);
done.store(true, Ordering::SeqCst);
}
});
barrier.wait();
let mut passes = 0;
loop {
let finished = done.load(Ordering::SeqCst);
let revoked = crl.revoked().unwrap();
let sum: i64 = revoked.iter().map(|r| r.serial_number().get()).sum();
passes += 1;
if sum != expected {
anomalies += 1;
eprintln!(
"round {round}, pass {passes}: saw serial sum {sum}, expected {expected} (diff {})",
sum - expected
);
}
if finished {
break;
}
}
verifier.join().unwrap();
}
eprintln!("anomalous passes: {anomalies}");
assert_eq!(anomalies, 0, "a reader saw an inconsistent revoked list");
}
dotjs
left a comment
There was a problem hiding this comment.
Requesting changes for the data race between X509CrlRef::revoked() and CRL verification, which reproduces with the safe API alone. The results, cause, a possible non-breaking fix and the test code are in my previous comment. Everything else in the PR looks good to me, and I will re-approve once this is resolved.
|
@dotjs The safe-code race is resolved in |
There was a problem hiding this comment.
Superseded by the review of
ab0ce3e2.
This review, of 8b929f57
💬 Approved with comments — The revoked-list data race is fixed and no defects were found. The only remaining item is an INFO about undocumented serial ordering of revoked entries.
Findings: 1 info (1 is an inline comment on the diff)
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
🔧 Fix with your agent
Paste the following into your coding agent to investigate and fix these issues.
Fix the following code review issues found in pull request #586.
Before making changes, review each issue and present a brief summary table:
- For each finding, state whether you agree, disagree, or need clarification
- If you disagree (e.g., the fix would require disproportionate changes
for minimal ROI, or the finding is factually incorrect), explain why
- If you need to clarify something before deciding, ask those questions
- Then share your plan for which issues to tackle and in what order
## INFO (1)
### [Code Quality] `boring/src/x509/crl.rs:247`: `revoked()` (and the CRL that `sign()` builds) lists entries sorted by serial, not in parse or insertion order, and the docs don't say so.
- **File:** boring/src/x509/crl.rs:247
📋 Code Quality: 💬 1 info
The lazy-sort fix for the revoked() data race is sound and covers every path that reads the revoked list. One INFO: the serial ordering isn't documented.
Recommendation: 💬 Optional Nits
- [INFO]
boring/src/x509/crl.rs:247:revoked()(and the CRL thatsign()builds) lists entries sorted by serial, not in parse or insertion order, and the docs don't say so. (boring/src/x509/crl.rs:247)
View full review
- The race fix covers
revoked(),digest(),verify(),to_pem()andto_der(). The other getters read fields that the lazy sort never touches. sign()consuming the builder and callingX509_CRL_sortbefore signing means the signed bytes match what is encoded later.add_revokedcallsmem::forgetonly afteradd0_revokedsucceeds, so it neither leaks nor double-frees.- INFO:
revoked()andsign()return or encode entries sorted by serial, not in parse or insertion order. Neither doc comment says so.
✅ All other checks passed
🔒 Security: ✅ LGTM
No issues found. verify_cert_with_crls documents that CRL_CHECK is required. The revoked-list data race is fixed because the list is sorted under BoringSSL's lock before it is borrowed.
📖 AGENTS.md: ✅ LGTM
This change does not call for an AGENTS.md update.
📝 Documentation: ✅ LGTM
No documentation issues.
Reviewed commit: 8b929f57
|
|
||
| /// Returns the revoked certificate entries. | ||
| #[corresponds(X509_CRL_get_REVOKED)] | ||
| pub fn revoked(&self) -> Result<Option<&StackRef<X509Revoked>>, ErrorStack> { |
There was a problem hiding this comment.
ℹ️ INFO · Code Quality
boring/src/x509/crl.rs:247: revoked() (and the CRL that sign() builds) lists entries sorted by serial, not in parse or insertion order, and the docs don't say so.
ensure_revoked_sorted() sorts the parsed list in place, and sign() calls X509_CRL_sort. A caller reading revoked() therefore gets entries ordered by serial, not in the order they were parsed or added. One sentence in the doc comments of revoked() and sign() would explain this.
There was a problem hiding this comment.
Documented in 1ab96133 that revoked() returns entries sorted by serial number.
There was a problem hiding this comment.
Superseded by the review of
1ab96133.
This review, of ab0ce3e2
💬 Approved with comments — One WARNING: a CRL built through X509CrlBuilder with entries out of serial order is signed before the list is sorted, so it can fail to verify after to_der(). The fix is a one-line sort before signing. Apart from that, the race fix and the FFI ownership look correct.
Findings: 1 warning (1 is an inline comment on the diff)
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
🔧 Fix with your agent
Paste the following into your coding agent to investigate and fix these issues.
Fix the following code review issues found in pull request #586.
Before making changes, review each issue and present a brief summary table:
- For each finding, state whether you agree, disagree, or need clarification
- If you disagree (e.g., the fix would require disproportionate changes
for minimal ROI, or the finding is factually incorrect), explain why
- If you need to clarify something before deciding, ask those questions
- Then share your plan for which issues to tackle and in what order
## WARNING (1)
### [Code Quality] `boring/src/x509/crl.rs:388`: `sign()` signs the revoked list before it is sorted. `to_der()`/`verify()` later sort it in place through `ensure_revoked_sorted`, so a builder-made CRL with entries out of serial order can be encoded differently from the bytes that were signed.
- **File:** boring/src/x509/crl.rs:388
📋 Code Quality: ⚠️ 1 warning
sign() signs the revoked list before it is sorted, so a builder-made CRL with entries out of serial order can be re-encoded in a different order than the one signed.
Recommendation: 📝 Minor Comments
- [WARNING]
boring/src/x509/crl.rs:388:sign()signs the revoked list before it is sorted.to_der()/verify()later sort it in place throughensure_revoked_sorted, so a builder-made CRL with entries out of serial order can be encoded differently from the bytes that were signed. (boring/src/x509/crl.rs:388)
View full review
- WARNING,
crl.rs:384-396.sign()callsX509_CRL_signon the revoked list in insertion order.- Every later
to_der(),to_pem(),digest()orverify()first runsensure_revoked_sorted(crl.rs:185-199), which sorts that list in place. - A CRL built here has no encoding cached from parsing. If the entries were added out of serial order, a re-encode after the sort can differ from the signed bytes, and the signature then fails.
- Sorting before signing fixes this at no cost.
- No test builds a CRL through the builder with several entries out of order and then re-checks
verify()afterto_der()/from_der(). Such a test would cover this.
- Every later
- Checked and correct:
verify_cert_with_crlslifetime handling (ResetCRLsguard).add_crlreference counting.add_revokedownership transfer.FallibleOnceLockretries after a failed init.- The
StackRef/X509RevokedRefDebugimpls.
✅ All other checks passed
🔒 Security: ✅ LGTM
No security defects found.
verify_cert_with_crlsdocuments thatCRL_CHECKis required.- The
revoked()data race is fixed: the list is sorted under BoringSSL's lock before it is borrowed. - Ownership transfers in the builder and the store are sound.
📖 AGENTS.md: ✅ LGTM
This change does not call for an AGENTS.md update.
📝 Documentation: ✅ LGTM
No documentation issues.
Reviewed commit: ab0ce3e2
| where | ||
| T: HasPrivate, | ||
| { | ||
| unsafe { |
There was a problem hiding this comment.
boring/src/x509/crl.rs:388: sign() signs the revoked list before it is sorted. to_der()/verify() later sort it in place through ensure_revoked_sorted, so a builder-made CRL with entries out of serial order can be encoded differently from the bytes that were signed.
sign() signs the revoked entries in insertion order. Later, to_der(), to_pem(), digest() and verify() all call ensure_revoked_sorted first, which sorts that same list in place. A CRL built through this builder has no encoding cached from parsing, so if add_revoked was called out of serial order, the re-encoded TBS can differ from the bytes that were signed, and the CRL then fails signature verification. Sorting before signing makes the signed order match the order that is encoded later. A builder test that adds entries in descending serial order and checks verify() after a to_der()/from_der() round trip would cover this.
| unsafe { | |
| self.0.ensure_revoked_sorted()?; | |
| unsafe { |
There was a problem hiding this comment.
Fixed in 1ab96133: X509_CRL_sort now runs immediately before X509_CRL_sign, and a descending-entry builder regression verifies the signature both before and after a DER round trip.
CRL parsing, construction, store integration, and validation with additional untrusted CRLs are supported. This work builds on the original CRL implementation by Ravi Khadiwala.
There was a problem hiding this comment.
✅ Approved — The earlier sign-before-sort WARNING is fixed, and the revoked-list race fix covers every borrowing path in the safe API. No open findings remain.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
No security defects found. verify_cert_with_crls documents that CRL_CHECK is required, and the borrowed revoked list is sorted under BoringSSL's lock before it is handed out.
📖 AGENTS.md: ✅ LGTM
This change does not call for an AGENTS.md update.
📋 Code Quality: ✅ LGTM
sign()now sorts the revoked list before signing and consumes the builder. A regression test covers entries added out of order, checkingverify()before and after a DER round trip.revoked(),digest(),verify(),to_pem()andto_der()all sort the list first under BoringSSL's lock.- These are sound:
- the
FallibleOnceLockretry after a failed init - the
ResetCRLsguard inverify_cert_with_crls - reference counting in
add_crl - the ownership transfer in
add_revoked
- the
📝 Documentation: ✅ LGTM
No documentation issues.
Reviewed commit: 1ab96133
dotjs
left a comment
There was a problem hiding this comment.
Re-reviewed at 1ab96133. The data race is fixed, thank you for the quick turnaround.
I re-ran my reproduction against this head, with a fixture CRL whose entries are encoded in descending serial order (checked with openssl crl):
| Test | e9ad065e |
1ab96133 |
|---|---|---|
Hold revoked(), verify a certificate, read the list again |
List changed under the reference | Unchanged |
| Two threads, 200,000 entries, 40 rounds | 40 of 40 rounds saw a corrupted list | 0 anomalies |
The boring library tests also pass on this head (389).
The fix is sound as I read it: revoked() triggers the locked sort in BoringSSL before handing out the stack, so no reference to an unsorted list can exist; digest(), verify(), to_der() and to_pem() take the same guard; and sign() sorts before signing and consumes the builder, so a CRL cannot change after it is signed. Documenting that revoked() returns entries in serial order covers the visible behaviour change.
This adds CRL parsing, serialization, construction, signature verification, store integration, and certificate validation with additional untrusted CRLs.
The implementation builds on the original CRL work by Ravi Khadiwala in #81 and updates it for the current API and BoringSSL version.