Skip to content

Issue certificate serials as positive minimal integers (R74) - #138

Open
LucaCappelletti94 wants to merge 1 commit into
mainfrom
fix/device-serial-leading-zero
Open

LucaCappelletti94 wants to merge 1 commit into
mainfrom
fix/device-serial-leading-zero

Conversation

@LucaCappelletti94

@LucaCappelletti94 LucaCappelletti94 commented Oct 8, 2026 •

Copy link
Copy Markdown
Owner

Device and issuer certificate serials were sixteen raw random bytes. About one draw in 256 started with a zero byte. The certificate's serial then reads without that byte, while the enrolment record keeps it, so the record and the certificate disagree. This is what made enrolment::a_signed_in_device_enrols_and_is_recorded fail once on CI. About half of all draws started with a byte of 128 or more, which DER can only encode with an extra leading byte.

Serials now come from a small validated type, CertificateSerial, whose first byte is always between 1 and 127, so every serial is the positive, minimal integer RFC 5280 asks for. It draws again until the first byte fits, which keeps the draw unbiased. Both the server's enrolment and connetto-ca issue through it, and the stored record is unchanged. A new test feeds the server a draw starting with a zero byte. It failed on main exactly as CI did and passes now. The plan's R74 decision 17 states the rule.

A random 16-byte serial could begin with zero or have its high bit set. Those values do not meet the required positive, minimal integer encoding and could cause the certificate serial to differ from the serial recorded during enrolment.

The fix validates serials before use and draws again until the first byte is from 0x01 through 0x7F. Both issuer and device certificate issuance use this validated type, which keeps the certificate and enrolment record serials consistent.

@coderabbitai

coderabbitai Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: LucaCappelletti94/coderabbit/.coderabbit.yaml
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 1e3e67a6-fe95-4620-94ab-fa682c60befe
📥 Commits

Reviewing files that changed from the base of the PR and between f84efe8 and 83edfc3.

📒 Files selected for processing (15)
  • crates/connetto-ca/src/lib.rs
  • crates/connetto-client/src/enrolment/tests.rs
  • crates/connetto-client/tests/it/enrolment.rs
  • crates/connetto-core/src/device_cert/authority.rs
  • crates/connetto-core/src/device_cert/mod.rs
  • crates/connetto-core/src/device_cert/serial.rs
  • crates/connetto-core/src/device_cert/tests.rs
  • crates/connetto-server/src/device_cert/attestation.rs
  • crates/connetto-server/src/device_cert/enrolment.rs
  • crates/connetto-server/src/device_cert/mod.rs
  • crates/connetto-server/src/device_cert/tests.rs
  • crates/connetto-server/tests/it/device_identity.rs
  • crates/connetto-server/tests/it/enrolment.rs
  • crates/connetto-server/tests/it/revocation.rs
  • plans/master-implementation-plan.md

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

Certificate serials now use a validated 16-byte type across certificate issuance. CA signing and server enrolment generate serials through random sources, and enrolment retries draws that do not satisfy the serial validation rule.

Changes

Certificate serial handling

Layer / File(s) Summary
Validated serial type and certificate issuance
crates/connetto-core/src/device_cert/serial.rs, crates/connetto-core/src/device_cert/mod.rs, crates/connetto-core/src/device_cert/authority.rs, crates/connetto-core/src/device_cert/tests.rs
Adds CertificateSerial, which accepts 16-byte values with a first octet from 0x01 to 0x7F. Issuer and device certificate issuance use this type, with tests for validation and certificate serial round-tripping.
Serial creation and certificate call sites
crates/connetto-ca/src/lib.rs, crates/connetto-client/src/enrolment/tests.rs, crates/connetto-client/tests/it/enrolment.rs, crates/connetto-server/src/device_cert/attestation.rs, crates/connetto-server/src/device_cert/tests.rs, crates/connetto-server/tests/it/device_identity.rs, crates/connetto-server/tests/it/enrolment.rs, crates/connetto-server/tests/it/revocation.rs
The CA creates issuer serials using CertificateSerial::random and OsRng. Certificate-signing call sites and test fixtures pass validated serials.
Enrolment serial generation
crates/connetto-server/src/device_cert/enrolment.rs, crates/connetto-server/src/device_cert/mod.rs, crates/connetto-server/src/device_cert/tests.rs, crates/connetto-server/tests/it/enrolment.rs
DeviceEnrolment uses a configurable RandomSource to draw serials. It maps source errors to EnrolRefusal::IssuerUnavailable and records the generated serial bytes. Tests cover invalid-draw retries and certificate serial round-tripping.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant DeviceEnrolment
  participant CertificateSerial
  participant RandomSource
  DeviceEnrolment->>CertificateSerial: Request a random serial using RandomSource
  loop Until a valid serial is drawn
    CertificateSerial->>RandomSource: Fill a 16-byte candidate
    RandomSource-->>CertificateSerial: Candidate bytes or fill error
  end
  CertificateSerial-->>DeviceEnrolment: Valid serial or fill error
  DeviceEnrolment->>DeviceEnrolment: Map errors or store serial bytes
Loading

Merge Risk: ⚪ Minimal · up to 83edf

No concrete issue remains that should block merging after normal checks.

🚥 Pre-merge checks | ✅ 12
✅ Passed checks (12 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is imperative, describes the serial-generation change, is 60 characters, has no conventional-commit prefix, contains no file path, and has no trailing period.
Docstring Coverage ✅ Passed Docstring coverage is 88.64% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 44 functions across 14 files. (1 skipped: 1…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
No Placeholder Implementations ✅ Passed No added lines contain the specified placeholder implementations or deferral markers. The reviewed diff adds 303 lines, and targeted checks found no todo!, unimplemented!, prohibited panic! mess…
No Blanket Diagnostic Suppression ✅ Passed No added line introduces a blanket diagnostic suppression. The diff contains no Rust allow/deny attributes, Python type: ignore or unqualified noqa, TypeScript suppression, or any escape.
Behavior Change Carries A Test ✅ Passed The pull request changes runtime behavior in source files, including serial validation and redraw logic in CertificateSerial::random, server enrolment, and CA issuance. It also adds and updates test…
Git Dependency Pin Stays Out Of Commits ✅ Passed Cargo.lock is unchanged in the reviewed range. The check therefore passes, regardless of existing unpinned git dependencies in Cargo.toml.
Crate Readme Is The Crate Documentation ✅ Passed The pull request changes crates/connetto-ca/src/lib.rs, and that file contains #![doc = include_str!("../README.md")]. Its README has a plain rust fence and no ignore or no_run annotation.
Pre-Alpha Has No Deployments ✅ Passed The workspace version is 0.0.0, so the pre-alpha rule applies. The added lines contain no migration guide or SQL, upgrade path, existing-deployment statement, rollout sequence, deprecation window, com…
Prose Punctuation ✅ Passed No added prose violates the punctuation rule. The added doc comments and Markdown use no semicolons, em dashes, en dashes, curly quotes, or ellipsis glyphs. The ASCII hyphens in 16-octet and `lost-d…
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sonarqubecloud

sonarqubecloud Bot commented Oct 8, 2026

Copy link
Copy Markdown

@codecov

codecov Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.50000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 86.40%. Comparing base (f84efe8) to head (83edfc3).

Files with missing lines Patch % Lines
crates/connetto-core/src/device_cert/serial.rs 94.11% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #138      +/-   ##
==========================================
- Coverage   86.40%   86.40%   -0.01%     
==========================================
  Files         163      164       +1     
  Lines       39726    39754      +28     
  Branches    39726    39754      +28     
==========================================
+ Hits        34325    34348      +23     
- Misses       3525     3529       +4     
- Partials     1876     1877       +1     
Flag Coverage Δ
client 55.91% <72.22%> (+0.34%) ⬆️
rest 51.66% <69.44%> (-0.35%) ⬇️
server 55.24% <85.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant