Skip to content
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

pkcs12: initial types with decoders/encoders #1165

Merged
merged 31 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ce1e1fc
initial pkcs12 encoder/decoder
carl-wallace Jul 18, 2023
028019f
add a few more OIDs from ASN.1 module in RFC7292
carl-wallace Jul 18, 2023
3c89436
added tests
carl-wallace Jul 18, 2023
2beff94
add higher level function to get key and cert from a PKCS12 object
carl-wallace Jul 19, 2023
842c725
add missing dev-dependencies
carl-wallace Jul 19, 2023
cd586b1
rename utils.rs to decrypt.rs
carl-wallace Jul 19, 2023
fc5ca02
add a few more test cases
carl-wallace Jul 19, 2023
2db1587
remove file added in error
carl-wallace Jul 19, 2023
2dbc115
temporarily copied over kdf.rs from #1154 and refactored decrypt supp…
carl-wallace Jul 19, 2023
45fa045
fix bug with default number of iterations. add test that uses default…
carl-wallace Jul 20, 2023
2d37940
support multiple cert bags
carl-wallace Jul 20, 2023
ee73142
add test with p12 exported from macos (fails due to rc2 usage)
carl-wallace Jul 20, 2023
3e08a57
add windows test cases, fix bug where iterations from params was not …
carl-wallace Jul 20, 2023
17ce9d4
add mac verification
carl-wallace Jul 20, 2023
63dead4
split decrypt tests to separate file
carl-wallace Jul 20, 2023
e4d980b
added additional mac verification algorithms. added more test cases g…
carl-wallace Jul 20, 2023
ceba8f9
add a couple of todos. update Cargo.lock (missed last commit)
carl-wallace Jul 20, 2023
6920df8
minor edits to Error type
carl-wallace Jul 21, 2023
67a0198
remove file
carl-wallace Jul 21, 2023
fe3a799
basic builder structure
carl-wallace Jul 21, 2023
6920508
incremental progress on builder. can emit a pfx containing plaintext …
carl-wallace Jul 21, 2023
49e4224
remove spurious map_err
carl-wallace Jul 21, 2023
0da354a
incremental progress on builder. can now emit shrouded key bag that o…
carl-wallace Jul 21, 2023
f155700
added support for encrypted cert bags and inclusion of a mac
carl-wallace Jul 21, 2023
73528fc
minor cleanup, some feature hygiene, added mac_data presence check to…
carl-wallace Jul 21, 2023
cc3fcbd
Merge branch 'master' into pkcs12
carl-wallace Jul 24, 2023
51eb92e
pare down to encoder/decoder only
carl-wallace Jul 24, 2023
5c2dc55
remove some bits from cargo.toml that are related to decrypt and buil…
carl-wallace Jul 24, 2023
9fd56c0
more cleanup
carl-wallace Jul 24, 2023
c945555
Merge branch 'master' into pkcs12_encoder_decoder
carl-wallace Sep 6, 2023
a148821
address a few review comments (several lifetime names, a stale TODO)
carl-wallace Sep 7, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 38 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions pkcs12/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@ edition = "2021"
rust-version = "1.65"

[dependencies]
der = { version = "0.7.8", features = ["alloc"] }
zeroize = "1.6"

# optional dependencies
digest = { version = "0.10.7", features = ["alloc"], optional = true }
der = { version = "0.7.8", features = ["alloc", "derive", "oid", "pem"] }
spki = { version = "0.7" }
x509-cert = { version = "0.2.3", default-features = false, features = ["pem"] }
const-oid = { version = "0.9", features = ["db"] } # TODO: path = "../const-oid"
cms = "0.2.1"
digest = { version = "0.10.7", features=["alloc"], optional = true }
zeroize = "1.6.0"

[dev-dependencies]
hex-literal = "0.4"
hex-literal = "0.3.3"
pkcs8 = { version = "0.10.2", features = ["pkcs5", "getrandom"] }
pkcs5 = {version = "0.7.1", features = ["pbes2", "3des"]}
subtle-encoding = "0.5.1"
sha2 = "0.10.7"
whirlpool = "0.10.4"

Expand All @@ -32,3 +37,4 @@ kdf = ["dep:digest"]
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

16 changes: 16 additions & 0 deletions pkcs12/src/authenticated_safe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//! AuthenticatedSafe-related types

use alloc::vec::Vec;
use cms::content_info::ContentInfo;

/// The `AuthenticatedSafe` type is defined in [RFC 7292 Section 4.1].
///
/// ```text
/// AuthenticatedSafe ::= SEQUENCE OF ContentInfo
/// -- Data if unencrypted
/// -- EncryptedData if password-encrypted
/// -- EnvelopedData if public key-encrypted
/// ```
///
/// [RFC 7292 Section 4.1]: https://www.rfc-editor.org/rfc/rfc7292#section-4.1
pub type AuthenticatedSafe<'a> = Vec<ContentInfo>;
59 changes: 59 additions & 0 deletions pkcs12/src/bag_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//! BagType-related types

use der::asn1::ObjectIdentifier;
use der::{ErrorKind, FixedTag, Tag};

/// Indicates the type of content.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub enum BagType {
/// Plain data content type
Key,

/// Signed-data content type
Pkcs8,

/// Enveloped-data content type
Cert,

/// Signed-and-enveloped-data content type
Crl,

/// Digested-data content type
Secret,

/// Encrypted-data content type
SafeContents,
}

impl FixedTag for BagType {
const TAG: Tag = Tag::ObjectIdentifier;
}

impl From<BagType> for ObjectIdentifier {
fn from(content_type: BagType) -> ObjectIdentifier {
match content_type {
BagType::Key => crate::PKCS_12_KEY_BAG_OID,
BagType::Pkcs8 => crate::PKCS_12_PKCS8_KEY_BAG_OID,
BagType::Cert => crate::PKCS_12_CERT_BAG_OID,
BagType::Crl => crate::PKCS_12_CRL_BAG_OID,
BagType::Secret => crate::PKCS_12_SECRET_BAG_OID,
BagType::SafeContents => crate::PKCS_12_SAFE_CONTENTS_BAG_OID,
}
}
}

impl TryFrom<ObjectIdentifier> for BagType {
type Error = der::Error;

fn try_from(oid: ObjectIdentifier) -> der::Result<Self> {
match oid {
crate::PKCS_12_KEY_BAG_OID => Ok(Self::Key),
crate::PKCS_12_PKCS8_KEY_BAG_OID => Ok(Self::Pkcs8),
crate::PKCS_12_CERT_BAG_OID => Ok(Self::Cert),
crate::PKCS_12_CRL_BAG_OID => Ok(Self::Crl),
crate::PKCS_12_SECRET_BAG_OID => Ok(Self::Secret),
crate::PKCS_12_SAFE_CONTENTS_BAG_OID => Ok(Self::SafeContents),
_ => Err(ErrorKind::OidUnknown { oid }.into()),
}
}
}
43 changes: 43 additions & 0 deletions pkcs12/src/cert_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//! CertBag-related types

use der::asn1::{ObjectIdentifier, OctetString};
use der::Sequence;

/// The `CertBag` type is defined in [RFC 7292 Section 4.2.3].
///
///```text
/// CertBag ::= SEQUENCE {
/// certId BAG-TYPE.&id ({CertTypes}),
/// certValue [0] EXPLICIT BAG-TYPE.&Type ({CertTypes}{@certId})
/// }
///```
///
/// [RFC 7292 Section 4.2.3]: https://www.rfc-editor.org/rfc/rfc7292#section-4.2.3
#[derive(Clone, Debug, Eq, PartialEq, Sequence)]
#[allow(missing_docs)]
pub struct CertBag {
pub cert_id: ObjectIdentifier,
#[asn1(context_specific = "0", tag_mode = "EXPLICIT")]
pub cert_value: CertTypes,
}

// todo defer: add sdsiCertificate support
/// The `CertTypes` type is defined in [RFC 7292 Section 4.2.3].
///
///```text
/// x509Certificate BAG-TYPE ::=
/// {OCTET STRING IDENTIFIED BY {certTypes 1}}
/// -- DER-encoded X.509 certificate stored in OCTET STRING
/// sdsiCertificate BAG-TYPE ::=
/// {IA5String IDENTIFIED BY {certTypes 2}}
/// -- Base64-encoded SDSI certificate stored in IA5String
///
/// CertTypes BAG-TYPE ::= {
/// x509Certificate |
/// sdsiCertificate,
/// ... -- For future extensions
/// }
///```
///
/// [RFC 7292 Section 4.2.3]: https://www.rfc-editor.org/rfc/rfc7292#section-4.2.3
pub type CertTypes = OctetString;
39 changes: 39 additions & 0 deletions pkcs12/src/crl_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//! CertBag-related types

use der::asn1::{ObjectIdentifier, OctetString};
use der::Sequence;

/// The `CertBag` type is defined in [RFC 7292 Section 4.2.4].
///
///```text
/// CRLBag ::= SEQUENCE {
/// crlId BAG-TYPE.&id ({CRLTypes}),
/// crltValue [0] EXPLICIT BAG-TYPE.&Type ({CRLTypes}{@crlId})
/// }
///```
///
/// [RFC 7292 Section 4.2.4]: https://www.rfc-editor.org/rfc/rfc7292#section-4.2.4
#[derive(Clone, Debug, Eq, PartialEq, Sequence)]
#[allow(missing_docs)]
pub struct CrlBag {
pub crl_id: ObjectIdentifier,
#[asn1(context_specific = "0", tag_mode = "EXPLICIT")]
pub crl_value: CrlTypes,
}

// todo defer: add support for other CRL types
/// The `CRLTypes` type is defined in [RFC 7292 Section 4.2.4].
///
///```text
/// x509CRL BAG-TYPE ::=
/// {OCTET STRING IDENTIFIED BY {crlTypes 1}}
/// -- DER-encoded X.509 CRL stored in OCTET STRING
///
/// CRLTypes BAG-TYPE ::= {
/// x509CRL,
/// ... -- For future extensions
/// }
///```
///
/// [RFC 7292 Section 4.2.4]: https://www.rfc-editor.org/rfc/rfc7292#section-4.2.4
pub type CrlTypes = OctetString;
18 changes: 18 additions & 0 deletions pkcs12/src/digest_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! DigestInfo-related types

use der::{asn1::OctetString, Sequence, ValueOrd};
use spki::AlgorithmIdentifierOwned;

/// ```text
/// DigestInfo ::= SEQUENCE {
/// digestAlgorithm DigestAlgorithmIdentifier,
/// digest Digest }
/// ```
#[derive(Clone, Debug, Eq, PartialEq, Sequence, ValueOrd)]
pub struct DigestInfo {
/// the algorithm.
pub algorithm: AlgorithmIdentifierOwned,

/// the digest
pub digest: OctetString,
}
Loading