-
Notifications
You must be signed in to change notification settings - Fork 12
uno: RsaUnwrap bring-up — OAEP endianness fix + non-CRT RSA private-key import #602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Rajib Dutta (radutta99)
wants to merge
16
commits into
main
Choose a base branch
from
user/radutta/rsa-unwrap-bringup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c83f728
fix(uno/rsa): flip mod_exp_priv result LE->BE before OAEP decode
radutta99 e59d80c
uno: import RSA private keys for RsaUnwrap
radutta99 61f7b2b
uno/rsa: return RsaInvalidKeyLength for oversized OAEP output
radutta99 92d1f8b
uno/rsa: harden DER INTEGER parsing and scrub OAEP scratch
radutta99 c300383
uno/rsa: scrub leftover DER and reject trailing garbage on import
radutta99 c395e1e
uno/rsa: reject non-minimal DER length encodings
radutta99 ce1523e
uno/rsa: scrub recovered DER on all import error paths
radutta99 4c437bd
uno/rsa: guard vault_len against buf and check AlgorithmIdentifier ex…
radutta99 b302200
uno/rsa: avoid redundant DER parse and double zero-fill on key import
radutta99 76737f4
uno/rsa: parse imported RSA private keys with the der crate
radutta99 6fa6f59
uno/rsa: extract RSA key DER parsing into pal::asn1
radutta99 2976a64
uno/pal: taplo-format the der dependency line
radutta99 c37cb2e
uno/rsa: require PKCS#8 for imported RSA private keys
radutta99 9bbdb03
uno/rsa: assemble the non-CRT RSA vault operand in place
radutta99 5b5d61b
uno/rsa: address review feedback on the RSA import path
radutta99 5f9a843
uno/crypto: share one reverse_copy helper in the PAL
radutta99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| //! ASN.1 / DER decoders for imported private keys, built on the no-alloc | ||
| //! [`der`] crate (the same dependency `fw/core/crypto/x509-chain` already uses). | ||
| //! | ||
| //! The wrapped-key import paths (`RsaUnwrap`) recover a plaintext DER private | ||
| //! key and must decode it into the raw components the PKA vault operand is | ||
| //! assembled from. This module owns only the **pure ASN.1 decode** (PKCS#8 / PKCS#1, and — as they land — SEC1 EC); the | ||
| //! platform-specific little-endian PKA operand layout lives in the | ||
| //! per-algorithm modules ([`rsa`](crate::crypto::rsa), [`ecc`](crate::crypto::ecc)) so this | ||
| //! stays a small, hardware-agnostic parsing layer. | ||
| //! | ||
| //! ## Scope and forward-looking extension points | ||
| //! | ||
| //! - **RSA non-CRT (today):** [`parse_rsa_private_key`] → | ||
| //! [`RsaPrivateKeyAsn1`]; `rsa` assembles `[d ‖ n ‖ e]`. | ||
| //! - **RSA CRT (#608):** reuses [`RsaPrivateKeyAsn1`] as-is — it already decodes | ||
| //! the CRT fields (`prime1`, `prime2`, `exponent1`, `exponent2`, | ||
| //! `coefficient`). Only the CRT operand assembly (and the derived `n1q`/`n2p` | ||
| //! PKA math) is added in `rsa`; **no change is needed here**. | ||
| //! - **ECC (#604):** add the SEC1 `ECPrivateKey` / PKCS#8 EC `PrivateKeyInfo` | ||
| //! decoders alongside the RSA ones below, plus a `parse_ec_private_key` | ||
| //! returning the scalar + curve OID; consumed by `ecc`. | ||
|
|
||
| use der::Decode; | ||
| use der::Sequence; | ||
| use der::asn1::Null; | ||
| use der::asn1::ObjectIdentifier; | ||
| use der::asn1::OctetStringRef; | ||
| use der::asn1::UintRef; | ||
|
|
||
| // ── RSA (PKCS#8 PrivateKeyInfo / PKCS#1 RSAPrivateKey) ───────────────────── | ||
|
|
||
| /// rsaEncryption OID (1.2.840.113549.1.1.1) — the algorithm identifier of an | ||
| /// RSA key inside a PKCS#8 `PrivateKeyInfo`. | ||
| const RSA_ENCRYPTION: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.1"); | ||
|
|
||
| /// PKCS#8 `AlgorithmIdentifier` for RSA: the `rsaEncryption` OID with the | ||
| /// explicit `NULL` parameters required by RFC 3279 §2.3.1. | ||
| #[derive(Sequence)] | ||
| struct RsaAlgorithmIdentifier { | ||
| oid: ObjectIdentifier, | ||
| parameters: Null, | ||
| } | ||
|
|
||
| /// PKCS#8 `PrivateKeyInfo` (RFC 5208) whose `privateKey` OCTET STRING wraps a | ||
| /// PKCS#1 `RSAPrivateKey`. | ||
| #[derive(Sequence)] | ||
| struct RsaPrivateKeyInfo<'a> { | ||
| version: u8, | ||
| algorithm: RsaAlgorithmIdentifier, | ||
| private_key: &'a OctetStringRef, | ||
| } | ||
|
|
||
| /// PKCS#1 `RSAPrivateKey` (RFC 8017 A.1.2), two-prime form (version 0). | ||
| /// | ||
| /// The full SEQUENCE is decoded so the `der` reader validates every field. The | ||
| /// non-CRT import path reads only `modulus` / `public_exponent` / | ||
| /// `private_exponent`; the CRT import path (#608) additionally reads the CRT | ||
| /// quintuple (`prime1`, `prime2`, `exponent1`, `exponent2`, `coefficient`), | ||
| /// which is why they are decoded and exposed here rather than skipped. | ||
| #[derive(Sequence)] | ||
| pub(crate) struct RsaPrivateKeyAsn1<'a> { | ||
| pub(crate) version: u8, | ||
| pub(crate) modulus: UintRef<'a>, | ||
| pub(crate) public_exponent: UintRef<'a>, | ||
| pub(crate) private_exponent: UintRef<'a>, | ||
| pub(crate) prime1: UintRef<'a>, | ||
| pub(crate) prime2: UintRef<'a>, | ||
| pub(crate) exponent1: UintRef<'a>, | ||
| pub(crate) exponent2: UintRef<'a>, | ||
| pub(crate) coefficient: UintRef<'a>, | ||
| } | ||
|
|
||
| /// Decodes a recovered RSA private key — a PKCS#8 `PrivateKeyInfo` wrapping a | ||
| /// PKCS#1 `RSAPrivateKey` — into the validated [`RsaPrivateKeyAsn1`]. | ||
| /// | ||
| /// The `der` crate enforces canonical DER (definite minimal-length encodings, | ||
| /// no trailing bytes via `from_der`) and rejects negative / non-minimal | ||
| /// INTEGERs. On top of that this rejects key versions the two-prime SEQUENCE | ||
| /// does not model, so callers get a fully-structurally-validated key. Field | ||
| /// sizes (modulus width, exponent width) are a PKA concern and are checked by | ||
| /// the caller during operand assembly. | ||
| pub(crate) fn parse_rsa_private_key(der_bytes: &[u8]) -> Option<RsaPrivateKeyAsn1<'_>> { | ||
| // The recovered wire key is always a PKCS#8 PrivateKeyInfo (the format the | ||
| // RsaUnwrap collateral is generated in); a bare PKCS#1 RSAPrivateKey is not | ||
| // accepted. | ||
| let pki = RsaPrivateKeyInfo::from_der(der_bytes).ok()?; | ||
| // RFC 5208 §5: PrivateKeyInfo `version` is v1 (= 0). RFC 5958 adds v2 | ||
| // (= 1) with public-key / attributes, which this parser does not model | ||
| // — reject rather than silently drop the extra fields. | ||
| if pki.version != 0 || pki.algorithm.oid != RSA_ENCRYPTION { | ||
| return None; | ||
| } | ||
| let key = RsaPrivateKeyAsn1::from_der(pki.private_key.as_bytes()).ok()?; | ||
| // RFC 8017 §A.1.2: `version` is 0 for two-prime keys. Version 1 (multi-prime) | ||
| // would require `otherPrimeInfos`, which our SEQUENCE does not declare. | ||
| if key.version != 0 { | ||
| return None; | ||
| } | ||
| Some(key) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.