pkcs12.rs

Overview

The pkcs12.rs file provides a comprehensive implementation of the PKCS#12 (Public-Key Cryptography Standards #12) data structures and cryptographic operations for securely packaging and encrypting private keys, certificates, and related attributes. It supports creating PKCS#12 PFX (Personal Information Exchange) files that include certificates, private keys (optionally encrypted), and certificate authority (CA) certificates, along with associated metadata such as friendly names and local key IDs.

This file primarily handles ASN.1 encoding/decoding via the yasna crate, password-based encryption (PBE) using SHA-1 and symmetric ciphers (RC2 and Triple-DES), message authentication codes (MACs) via HMAC-SHA1, and management of the PKCS#12 safe bags and content info structures.

Key cryptographic algorithms and data types are represented as Rust structs and enums, with encoding methods that produce DER-encoded bytes conforming to the PKCS#12 specification.


Constants and Static OIDs


Utility Functions

sha1(bytes: &[u8]) -> Vec<u8>

Computes the SHA-1 digest of the input byte slice.


rand() -> Option<[u8; 8]>

Generates 8 random bytes using the system's random number generator.


bmp_string(s: &str) -> Vec<u8>

Encodes a Rust UTF-8 string into a BMPString (UTF-16BE with trailing null terminator) format, as required by PKCS#12 attributes.


Main Data Structures and Their Methods

EncryptedContentInfo

Represents the encrypted content info structure containing the encryption algorithm identifier and the encrypted content bytes.


EncryptedData

Wraps an EncryptedContentInfo instance.


ContentInfo

Enum representing two content types in PKCS#12:


Pkcs12PbeParams

Parameters for PKCS#12 password-based encryption (PBE) algorithms.


AlgorithmIdentifier

Enum representing supported cryptographic algorithms:


DigestInfo

Holds a digest algorithm and the computed digest bytes.


MacData

Holds MAC-related data for PKCS#12 integrity checking.


Pfx

Represents the top-level PKCS#12 PFX structure.

Usage Example:

let pfx = Pfx::new(cert_der_bytes, key_der_bytes, Some(ca_der_bytes), "password", "friendly_name");
if let Some(pfx) = pfx {
    let der_data = pfx.to_der();
    // der_data contains the PKCS#12 file bytes
}

CertBag

Represents a certificate bag in PKCS#12.


EncryptedPrivateKeyInfo

Represents an encrypted private key structure.


SafeBagKind

Enum representing the types of safe bags that can be stored inside PKCS#12:


PKCS12Attribute

Attributes associated with safe bags.


SafeBag

Encapsulates a safe bag and its associated attributes.


Cryptographic Functions

Password-Based Key Derivation and Encryption


Interaction with Other System Components


Implementation Details and Algorithms


Visual Diagram: Structure of Main Types and Their Relationships

classDiagram
class Pfx {
+version: u8
+auth_safe: ContentInfo
+mac_data: Option<MacData>
+new()
+write()
+to_der()
}
class ContentInfo {
<<enum>>
+Data(Vec<u8>)
+EncryptedData(EncryptedData)
+write()
}
class EncryptedData {
+encrypted_content_info: EncryptedContentInfo
+write()
+from_safe_bags()
}
class EncryptedContentInfo {
+content_encryption_algorithm: AlgorithmIdentifier
+encrypted_content: Vec<u8>
+write()
+from_safe_bags()
}
class SafeBag {
+bag: SafeBagKind
+attributes: Vec<PKCS12Attribute>
+write()
}
class SafeBagKind {
<<enum>>
+Key(Vec<u8>)
+Pkcs8ShroudedKey(EncryptedPrivateKeyInfo)
+Cert(CertBag)
+write()
+oid()
}
class PKCS12Attribute {
<<enum>>
+FriendlyName(String)
+LocalKeyId(Vec<u8>)
+write()
}
class AlgorithmIdentifier {
<<enum>>
+Sha1
+PbeWithSHAAnd40BitRC2CBC(Pkcs12PbeParams)
+PbeWithSHAAnd3KeyTripleDESCBC(Pkcs12PbeParams)
+write()
}
class Pkcs12PbeParams {
+salt: Vec<u8>
+iterations: u64
+write()
}
class MacData {
+mac: DigestInfo
+salt: Vec<u8>
+iterations: u32
+write()
+new()
}
class DigestInfo {
+digest_algorithm: AlgorithmIdentifier
+digest: Vec<u8>
+write()
}
class CertBag {
<<enum>>
+X509(Vec<u8>)
+write()
}
class EncryptedPrivateKeyInfo {
+encryption_algorithm: AlgorithmIdentifier
+encrypted_data: Vec<u8>
+write()
}
Pfx --> ContentInfo
ContentInfo --> EncryptedData
EncryptedData --> EncryptedContentInfo
EncryptedContentInfo --> AlgorithmIdentifier
SafeBag --> SafeBagKind
SafeBag --> PKCS12Attribute
SafeBagKind --> EncryptedPrivateKeyInfo
SafeBagKind --> CertBag
PKCS12Attribute --> AlgorithmIdentifier
MacData --> DigestInfo
DigestInfo --> AlgorithmIdentifier
EncryptedPrivateKeyInfo --> AlgorithmIdentifier
Pkcs12PbeParams ..> AlgorithmIdentifier : used in

Important Notes


This file is central to the creation and encoding of PKCS#12 bundles, managing the secure packaging of private key material with certificates and associated metadata. For related cryptographic topics, see Password-Based Encryption, ASN.1 Encoding, and PKCS#12 Specification.