From ef35326c8a5852356307728cd07365e502f295ff Mon Sep 17 00:00:00 2001 From: David Nadoba Date: Tue, 31 Jan 2023 17:54:06 +0100 Subject: [PATCH] `CryptographicMessageSyntax` types --- .../CMSContentInfo.swift | 26 ++++++++++ .../CMSEncapsulatedContentInfo.swift | 27 +++++++++++ .../CMSIssuerAndSerialNumber.swift | 28 +++++++++++ .../CMSSignedData.swift | 48 +++++++++++++++++++ .../CMSSignerIdentifier.swift | 26 ++++++++++ .../CMSSignerInfo.swift | 42 ++++++++++++++++ .../CMSVersion.swift | 37 ++++++++++++++ 7 files changed, 234 insertions(+) create mode 100644 Sources/X509/CryptographicMessageSyntax/CMSContentInfo.swift create mode 100644 Sources/X509/CryptographicMessageSyntax/CMSEncapsulatedContentInfo.swift create mode 100644 Sources/X509/CryptographicMessageSyntax/CMSIssuerAndSerialNumber.swift create mode 100644 Sources/X509/CryptographicMessageSyntax/CMSSignedData.swift create mode 100644 Sources/X509/CryptographicMessageSyntax/CMSSignerIdentifier.swift create mode 100644 Sources/X509/CryptographicMessageSyntax/CMSSignerInfo.swift create mode 100644 Sources/X509/CryptographicMessageSyntax/CMSVersion.swift diff --git a/Sources/X509/CryptographicMessageSyntax/CMSContentInfo.swift b/Sources/X509/CryptographicMessageSyntax/CMSContentInfo.swift new file mode 100644 index 0000000..035594a --- /dev/null +++ b/Sources/X509/CryptographicMessageSyntax/CMSContentInfo.swift @@ -0,0 +1,26 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftCertificates open source project +// +// Copyright (c) 2023 Apple Inc. and the SwiftCertificates project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftCertificates project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import SwiftASN1 + +/// ``ContentInfo`` is defined in ASN.1 as: +/// ``` +/// ContentInfo ::= SEQUENCE { +/// contentType ContentType, +/// content [0] EXPLICIT ANY DEFINED BY contentType } +/// ContentType ::= OBJECT IDENTIFIER +/// ``` +enum CMSContentInfo { + case signedData(CMSSignedData) +} diff --git a/Sources/X509/CryptographicMessageSyntax/CMSEncapsulatedContentInfo.swift b/Sources/X509/CryptographicMessageSyntax/CMSEncapsulatedContentInfo.swift new file mode 100644 index 0000000..4a90eaf --- /dev/null +++ b/Sources/X509/CryptographicMessageSyntax/CMSEncapsulatedContentInfo.swift @@ -0,0 +1,27 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftCertificates open source project +// +// Copyright (c) 2023 Apple Inc. and the SwiftCertificates project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftCertificates project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import SwiftASN1 + +/// ``CMSEncapsulatedContentInfo`` is defined in ASN.1 as: +/// ``` +/// EncapsulatedContentInfo ::= SEQUENCE { +/// eContentType ContentType, +/// eContent [0] EXPLICIT OCTET STRING OPTIONAL } +/// ContentType ::= OBJECT IDENTIFIER +/// ``` +struct CMSEncapsulatedContentInfo { + var eContentType: ASN1ObjectIdentifier + var eContent: ASN1OctetString? +} diff --git a/Sources/X509/CryptographicMessageSyntax/CMSIssuerAndSerialNumber.swift b/Sources/X509/CryptographicMessageSyntax/CMSIssuerAndSerialNumber.swift new file mode 100644 index 0000000..a15e79d --- /dev/null +++ b/Sources/X509/CryptographicMessageSyntax/CMSIssuerAndSerialNumber.swift @@ -0,0 +1,28 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftCertificates open source project +// +// Copyright (c) 2023 Apple Inc. and the SwiftCertificates project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftCertificates project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import SwiftASN1 + +/// ``CMSIssuerAndSerialNumber`` is defined in ASN.1 as: +/// ``` +/// IssuerAndSerialNumber ::= SEQUENCE { +/// issuer Name, +/// serialNumber CertificateSerialNumber } +/// ``` +/// The definition of `Name` is taken from X.501 [X.501-88], and the +/// definition of `CertificateSerialNumber` is taken from X.509 [X.509-97]. +struct CMSIssuerAndSerialNumber { + var issuer: DistinguishedName + var serialNumber: Certificate.SerialNumber +} diff --git a/Sources/X509/CryptographicMessageSyntax/CMSSignedData.swift b/Sources/X509/CryptographicMessageSyntax/CMSSignedData.swift new file mode 100644 index 0000000..8e5e2a5 --- /dev/null +++ b/Sources/X509/CryptographicMessageSyntax/CMSSignedData.swift @@ -0,0 +1,48 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftCertificates open source project +// +// Copyright (c) 2023 Apple Inc. and the SwiftCertificates project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftCertificates project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import SwiftASN1 + +/// ``SignedData`` is defined in ASN.1 as: +/// ``` +/// SignedData ::= SEQUENCE { +/// version CMSVersion, +/// digestAlgorithms DigestAlgorithmIdentifiers, +/// encapContentInfo EncapsulatedContentInfo, +/// certificates [0] IMPLICIT CertificateSet OPTIONAL, +/// crls [1] IMPLICIT RevocationInfoChoices OPTIONAL, +/// signerInfos SignerInfos } +/// +/// DigestAlgorithmIdentifiers ::= SET OF DigestAlgorithmIdentifier +/// DigestAlgorithmIdentifier ::= AlgorithmIdentifier +/// SignerInfos ::= SET OF SignerInfo +/// +/// CertificateChoices ::= CHOICE { +/// certificate Certificate, +/// extendedCertificate [0] IMPLICIT ExtendedCertificate, -- Obsolete +/// v1AttrCert [1] IMPLICIT AttributeCertificateV1, -- Obsolete +/// v2AttrCert [2] IMPLICIT AttributeCertificateV2, +/// other [3] IMPLICIT OtherCertificateFormat } +/// +/// OtherCertificateFormat ::= SEQUENCE { +/// otherCertFormat OBJECT IDENTIFIER, +/// otherCert ANY DEFINED BY otherCertFormat } +/// ``` +struct CMSSignedData { + var version: CMSVersion + var digestAlgorithms: [AlgorithmIdentifier] + var encapContentInfo: CMSEncapsulatedContentInfo + var certificates: [Certificate]? + var signerInfos: [CMSSignerInfo] +} diff --git a/Sources/X509/CryptographicMessageSyntax/CMSSignerIdentifier.swift b/Sources/X509/CryptographicMessageSyntax/CMSSignerIdentifier.swift new file mode 100644 index 0000000..eedf85c --- /dev/null +++ b/Sources/X509/CryptographicMessageSyntax/CMSSignerIdentifier.swift @@ -0,0 +1,26 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftCertificates open source project +// +// Copyright (c) 2023 Apple Inc. and the SwiftCertificates project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftCertificates project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import SwiftASN1 + +/// ``CMSSignerIdentifier`` is defined in ASN.1 as: +/// ``` +/// SignerIdentifier ::= CHOICE { +/// issuerAndSerialNumber IssuerAndSerialNumber, +/// subjectKeyIdentifier [0] SubjectKeyIdentifier } +/// ``` +enum CMSSignerIdentifier { + case issuerAndSerialNumber(CMSIssuerAndSerialNumber) + case subjectKeyIdentifier(Certificate.Extensions.SubjectKeyIdentifier) +} diff --git a/Sources/X509/CryptographicMessageSyntax/CMSSignerInfo.swift b/Sources/X509/CryptographicMessageSyntax/CMSSignerInfo.swift new file mode 100644 index 0000000..c3aad9b --- /dev/null +++ b/Sources/X509/CryptographicMessageSyntax/CMSSignerInfo.swift @@ -0,0 +1,42 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftCertificates open source project +// +// Copyright (c) 2023 Apple Inc. and the SwiftCertificates project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftCertificates project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import SwiftASN1 + +/// ``CMSSignerInfo`` is defined in ASN.1 as: +/// ``` +/// SignerInfo ::= SEQUENCE { +/// version CMSVersion, +/// sid SignerIdentifier, +/// digestAlgorithm DigestAlgorithmIdentifier, +/// signedAttrs [0] IMPLICIT SignedAttributes OPTIONAL, +/// signatureAlgorithm SignatureAlgorithmIdentifier, +/// signature SignatureValue, +/// unsignedAttrs [1] IMPLICIT UnsignedAttributes OPTIONAL } +/// +/// SignatureValue ::= OCTET STRING +/// DigestAlgorithmIdentifier ::= AlgorithmIdentifier +/// SignatureAlgorithmIdentifier ::= AlgorithmIdentifier +/// ``` +/// - Note: If the `SignerIdentifier` is the CHOICE `issuerAndSerialNumber`, +/// then the `version` MUST be 1. If the `SignerIdentifier` is `subjectKeyIdentifier`, +/// then the `version` MUST be 3. +struct CMSSignerInfo { + var version: CMSVersion + var signerIdentifier: CMSSignerIdentifier + var digestAlgorithm: AlgorithmIdentifier + var signatureAlgorithm: AlgorithmIdentifier + var signature: ASN1OctetString +} + diff --git a/Sources/X509/CryptographicMessageSyntax/CMSVersion.swift b/Sources/X509/CryptographicMessageSyntax/CMSVersion.swift new file mode 100644 index 0000000..3c9c004 --- /dev/null +++ b/Sources/X509/CryptographicMessageSyntax/CMSVersion.swift @@ -0,0 +1,37 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftCertificates open source project +// +// Copyright (c) 2023 Apple Inc. and the SwiftCertificates project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftCertificates project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import SwiftASN1 + +/// ``CMPVersion`` is defined in ASN1. as: +/// ``` +/// CMSVersion ::= INTEGER +/// { v0(0), v1(1), v2(2), v3(3), v4(4), v5(5) } +/// ``` +struct CMSVersion: RawRepresentable, Hashable { + var rawValue: Int + + static let v0 = Self(rawValue: 0) + static let v1 = Self(rawValue: 1) + static let v2 = Self(rawValue: 2) + static let v3 = Self(rawValue: 3) + static let v4 = Self(rawValue: 4) + static let v5 = Self(rawValue: 5) +} + +extension CMSVersion: CustomStringConvertible { + var description: String { + "CMSv\(rawValue)" + } +}