1#![doc = include_str!("../README.md")]
2#![no_std]
3
4extern crate alloc;
5
6pub mod algorithms;
7pub mod authenticode;
8pub mod firmware_wrapper;
9pub mod pkcs7_compat;
10
11use alloc::boxed::Box;
12
13use rasn::prelude::*;
14pub use rasn_pkix::{
15 AlgorithmIdentifier, Attribute, Certificate, CertificateList, CertificateSerialNumber, Name,
16 SubjectKeyIdentifier,
17};
18
19pub const CONTENT_INFO: &Oid = Oid::ISO_MEMBER_BODY_US_RSADSI_PKCS9_SMIME_CT_CONTENTINFO;
21
22pub const CONTENT_TYPE: &Oid = Oid::ISO_MEMBER_BODY_US_RSADSI_PKCS9_CONTENT_TYPE;
24
25pub const MESSAGE_DIGEST: &Oid = Oid::ISO_MEMBER_BODY_US_RSADSI_PKCS9_MESSAGE_DIGEST;
27
28pub const SIGNING_TIME: &Oid = Oid::ISO_MEMBER_BODY_US_RSADSI_PKCS9_SIGNING_TIME;
30
31pub const COUNTER_SIGNATURE: &Oid = Oid::ISO_MEMBER_BODY_US_RSADSI_PKCS9_COUNTER_SIGNATURE;
33
34pub const CONTENT_DATA: &Oid = Oid::ISO_MEMBER_BODY_US_RSADSI_PKCS7_DATA;
37
38pub const CONTENT_SIGNED_DATA: &Oid = Oid::ISO_MEMBER_BODY_US_RSADSI_PKCS7_SIGNED_DATA;
40
41pub const CONTENT_ENVELOPED_DATA: &Oid = Oid::ISO_MEMBER_BODY_US_RSADSI_PKCS7_ENVELOPED_DATA;
43
44pub const CONTENT_DIGESTED_DATA: &Oid = Oid::ISO_MEMBER_BODY_US_RSADSI_PKCS7_DIGESTED_DATA;
46
47pub const CONTENT_ENCRYPTED_DATA: &Oid = Oid::ISO_MEMBER_BODY_US_RSADSI_PKCS7_ENCRYPTED_DATA;
49
50pub const CONTENT_AUTHENTICATED_DATA: &Oid =
52 Oid::ISO_MEMBER_BODY_US_RSADSI_PKCS9_SMIME_CT_AUTHENTICATED_DATA;
53
54pub type CmsVersion = Integer;
55pub type ContentType = ObjectIdentifier;
56pub type DigestAlgorithmIdentifier = AlgorithmIdentifier;
57pub type DigestAlgorithmIdentifiers = SetOf<DigestAlgorithmIdentifier>;
58pub type SignatureAlgorithmIdentifier = AlgorithmIdentifier;
59pub type ContentEncryptionAlgorithmIdentifier = AlgorithmIdentifier;
60pub type KeyEncryptionAlgorithmIdentifier = AlgorithmIdentifier;
61pub type KeyDerivationAlgorithmIdentifier = AlgorithmIdentifier;
62pub type MessageAuthenticationCodeAlgorithm = AlgorithmIdentifier;
63pub type CertificateSet = SetOf<CertificateChoices>;
64pub type RevocationInfoChoices = SetOf<RevocationInfoChoice>;
65pub type SignerInfos = SetOf<SignerInfo>;
66pub type SignedAttributes = SetOf<Attribute>;
67pub type UnsignedAttributes = SetOf<Attribute>;
68pub type SignatureValue = OctetString;
69pub type RecipientInfos = SetOf<RecipientInfo>;
70pub type UnprotectedAttributes = SetOf<Attribute>;
71pub type EncryptedContent = OctetString;
72pub type EncryptedKey = OctetString;
73pub type RecipientEncryptedKeys = SequenceOf<RecipientEncryptedKey>;
74pub type UserKeyingMaterial = OctetString;
75pub type Digest = OctetString;
76pub type AuthAttributes = SetOf<Attribute>;
77pub type UnauthAttributes = SetOf<Attribute>;
78pub type MessageAuthenticationCode = OctetString;
79pub type Signature = BitString;
80
81#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, Hash)]
82pub struct AuthEnvelopedData {
83 pub version: CmsVersion,
84 #[rasn(tag(0))]
85 pub originator_info: Option<OriginatorInfo>,
86 pub recipient_infos: RecipientInfos,
87 pub auth_encrypted_content_info: EncryptedContentInfo,
88 #[rasn(tag(1))]
89 pub auth_attrs: Option<AuthAttributes>,
90 pub mac: MessageAuthenticationCode,
91 #[rasn(tag(2))]
92 pub unauth_attrs: Option<UnauthAttributes>,
93}
94
95#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, PartialOrd, Ord, Hash)]
98pub struct ContentInfo {
99 pub content_type: ContentType,
100 #[rasn(tag(explicit(0)))]
101 pub content: Any,
102}
103
104#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, Hash)]
106pub struct SignedData {
107 pub version: CmsVersion,
108 pub digest_algorithms: DigestAlgorithmIdentifiers,
109 pub encap_content_info: EncapsulatedContentInfo,
110 #[rasn(tag(0))]
111 pub certificates: Option<CertificateSet>,
112 #[rasn(tag(1))]
113 pub crls: Option<RevocationInfoChoices>,
114 pub signer_infos: SignerInfos,
115}
116
117#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, Hash)]
119pub struct EnvelopedData {
120 pub version: CmsVersion,
121 #[rasn(tag(0))]
122 pub originator_info: Option<OriginatorInfo>,
123 pub recipient_infos: RecipientInfos,
124 pub encrypted_content_info: EncryptedContentInfo,
125 #[rasn(tag(1))]
126 pub unprotected_attrs: Option<UnprotectedAttributes>,
127}
128
129#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, PartialOrd, Ord, Hash)]
131pub struct DigestedData {
132 pub version: CmsVersion,
133 pub digest_algorithm: DigestAlgorithmIdentifier,
134 pub encap_content_info: EncapsulatedContentInfo,
135 pub digest: Digest,
136}
137
138#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, Hash)]
140pub struct EncryptedData {
141 pub version: CmsVersion,
142 pub encrypted_content_info: EncryptedContentInfo,
143 #[rasn(tag(1))]
144 pub unprotected_attrs: Option<UnprotectedAttributes>,
145}
146
147#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, Hash)]
149pub struct AuthenticatedData {
150 pub version: CmsVersion,
151 #[rasn(tag(0))]
152 pub originator_info: Option<OriginatorInfo>,
153 pub recipient_infos: RecipientInfos,
154 pub mac_algorithm: MessageAuthenticationCodeAlgorithm,
155 #[rasn(tag(1))]
156 pub digest_algorithm: Option<DigestAlgorithmIdentifier>,
157 pub encap_content_info: EncapsulatedContentInfo,
158 #[rasn(tag(2))]
159 pub auth_attrs: Option<AuthAttributes>,
160 pub mac: MessageAuthenticationCode,
161 #[rasn(tag(3))]
162 pub unauth_attrs: Option<UnauthAttributes>,
163}
164
165#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, Hash)]
172#[rasn(choice)]
173pub enum CertificateChoices {
174 Certificate(Box<Certificate>),
175 #[rasn(tag(0))]
176 ExtendedCertificate(Box<ExtendedCertificate>),
177 #[rasn(tag(2))]
178 V2AttributeCertificate(Box<rasn_pkix::attribute_certificate::AttributeCertificate>),
179 #[rasn(tag(3))]
180 Other(OtherCertificateFormat),
181}
182
183#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, PartialOrd, Ord, Hash)]
185pub struct OtherCertificateFormat {
186 pub other_cert_format: ObjectIdentifier,
187 pub other_cert: Any,
188}
189
190#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, Hash)]
196#[rasn(choice)]
197pub enum RevocationInfoChoice {
198 Crl(CertificateList),
199 #[rasn(tag(1))]
200 Other(OtherRevocationInfoFormat),
201}
202
203#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, PartialOrd, Ord, Hash)]
207pub struct OtherRevocationInfoFormat {
208 pub other_rev_info_format: ObjectIdentifier,
209 pub other_rev_info: Any,
210}
211
212#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, PartialOrd, Ord, Hash)]
214pub struct EncapsulatedContentInfo {
215 pub content_type: ContentType,
216 #[rasn(tag(explicit(0)))]
217 pub content: Option<OctetString>,
218}
219
220#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, Hash)]
222pub struct SignerInfo {
223 pub version: CmsVersion,
224 pub sid: SignerIdentifier,
225 pub digest_algorithm: DigestAlgorithmIdentifier,
226 #[rasn(tag(0))]
227 pub signed_attrs: Option<SignedAttributes>,
228 pub signature_algorithm: SignatureAlgorithmIdentifier,
229 pub signature: SignatureValue,
230 #[rasn(tag(1))]
231 pub unsigned_attrs: Option<UnsignedAttributes>,
232}
233
234#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, Hash)]
236#[rasn(choice)]
237pub enum SignerIdentifier {
238 IssuerAndSerialNumber(IssuerAndSerialNumber),
239 #[rasn(tag(0))]
240 SubjectKeyIdentifier(SubjectKeyIdentifier),
241}
242
243#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, Hash)]
247pub struct IssuerAndSerialNumber {
248 pub issuer: Name,
249 pub serial_number: CertificateSerialNumber,
250}
251
252#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, Hash)]
255pub struct OriginatorInfo {
256 #[rasn(tag(0))]
257 pub certs: Option<CertificateSet>,
258 #[rasn(tag(1))]
259 pub crls: Option<RevocationInfoChoices>,
260}
261
262#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, PartialOrd, Ord, Hash)]
264pub struct EncryptedContentInfo {
265 pub content_type: ContentType,
266 pub content_encryption_algorithm: ContentEncryptionAlgorithmIdentifier,
267 #[rasn(tag(0))]
268 pub encrypted_content: Option<EncryptedContent>,
269}
270
271#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, Hash)]
273#[rasn(choice)]
274pub enum RecipientInfo {
275 KeyTransRecipientInfo(KeyTransRecipientInfo),
276 #[rasn(tag(1))]
277 KeyAgreeRecipientInfo(KeyAgreeRecipientInfo),
278 #[rasn(tag(2))]
279 KekRecipientInfo(KekRecipientInfo),
280 #[rasn(tag(3))]
281 PasswordRecipientInfo(PasswordRecipientInfo),
282 #[rasn(tag(4))]
283 OtherRecipientInfo(OtherRecipientInfo),
284}
285
286#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, Hash)]
290pub struct KeyTransRecipientInfo {
291 pub version: CmsVersion,
292 pub rid: RecipientIdentifier,
293 pub key_encryption_algorithm: KeyEncryptionAlgorithmIdentifier,
294 pub encrypted_key: EncryptedKey,
295}
296
297#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, Hash)]
300#[rasn(choice)]
301pub enum RecipientIdentifier {
302 IssuerAndSerialNumber(IssuerAndSerialNumber),
303 #[rasn(tag(0))]
304 SubjectKeyIdentifier(SubjectKeyIdentifier),
305}
306
307#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, Hash)]
310pub struct KeyAgreeRecipientInfo {
311 pub version: CmsVersion,
312 #[rasn(tag(explicit(0)))]
313 pub originator: OriginatorIdentifierOrKey,
314 #[rasn(tag(explicit(1)))]
315 pub user_keying_material: Option<UserKeyingMaterial>,
316 pub key_encryption_algorithm: KeyEncryptionAlgorithmIdentifier,
317 pub recipient_encrypted_keys: RecipientEncryptedKeys,
318}
319
320#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, Hash)]
323pub struct RecipientEncryptedKey {
324 pub key_agree_recipient_identifier: KeyAgreeRecipientIdentifier,
325 pub encrypted_key: EncryptedKey,
326}
327
328#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, Hash)]
334#[rasn(choice)]
335pub enum KeyAgreeRecipientIdentifier {
336 IssuerAndSerialNumber(IssuerAndSerialNumber),
337 #[rasn(tag(0))]
338 RecipientKeyIdentifier(RecipientKeyIdentifier),
339}
340
341#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, PartialOrd, Ord, Hash)]
343pub struct RecipientKeyIdentifier {
344 pub subject_key_identifier: SubjectKeyIdentifier,
345 pub date: Option<GeneralizedTime>,
346 pub other: Option<OtherKeyAttribute>,
347}
348
349#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, PartialOrd, Ord, Hash)]
352pub struct OtherKeyAttribute {
353 pub key_attr_id: ObjectIdentifier,
354 pub key_attr: Option<Any>,
355}
356
357#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, Hash)]
360#[rasn(choice)]
361pub enum OriginatorIdentifierOrKey {
362 IssuerAndSerialNumber(IssuerAndSerialNumber),
363 #[rasn(tag(0))]
364 SubjectKeyIdentifier(SubjectKeyIdentifier),
365 #[rasn(tag(1))]
366 OriginatorPublicKey(OriginatorPublicKey),
367}
368
369#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, PartialOrd, Ord, Hash)]
373pub struct OriginatorPublicKey {
374 pub algorithm: AlgorithmIdentifier,
375 pub public_key: BitString,
376}
377
378#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, PartialOrd, Ord, Hash)]
381pub struct KekRecipientInfo {
382 pub version: CmsVersion,
383 pub kek_id: KekIdentifier,
384 pub key_encryption_algorithm: KeyEncryptionAlgorithmIdentifier,
385 pub encrypted_key: EncryptedKey,
386}
387
388#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, PartialOrd, Ord, Hash)]
391pub struct KekIdentifier {
392 pub key_identifier: OctetString,
393 pub date: Option<GeneralizedTime>,
394 pub other: Option<OtherKeyAttribute>,
395}
396
397#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, PartialOrd, Ord, Hash)]
400pub struct PasswordRecipientInfo {
401 pub version: CmsVersion,
402 #[rasn(tag(0))]
403 pub key_derivation_algorithm: Option<KeyDerivationAlgorithmIdentifier>,
404 pub key_encryption_algorithm: KeyEncryptionAlgorithmIdentifier,
405 pub encrypted_eey: EncryptedKey,
406}
407
408#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, PartialOrd, Ord, Hash)]
411pub struct OtherRecipientInfo {
412 pub ori_type: ObjectIdentifier,
413 pub ori_value: Any,
414}
415
416#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, Hash)]
417#[rasn(choice)]
418pub enum ExtendedCertificateOrCertificate {
419 Certificate(Certificate),
420 #[rasn(tag(0))]
421 ExtendedCertificate(ExtendedCertificate),
422}
423
424#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, Hash)]
425pub struct ExtendedCertificate {
426 pub extended_certificate_info: ExtendedCertificateInfo,
427 pub signature_algorithm: SignatureAlgorithmIdentifier,
428 pub signature: Signature,
429}
430
431#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, Hash)]
432pub struct ExtendedCertificateInfo {
433 pub version: CmsVersion,
434 pub certificate: Certificate,
435 pub attributes: UnauthAttributes,
436}