diff --git a/Sources/X509/OCSP/OCSPRequest.swift b/Sources/X509/OCSP/OCSPRequest.swift index 6c677f6..47ed13f 100644 --- a/Sources/X509/OCSP/OCSPRequest.swift +++ b/Sources/X509/OCSP/OCSPRequest.swift @@ -6,7 +6,7 @@ // Licensed under Apache License v2.0 // // See LICENSE.txt for license information -// See CONTRIBUTORS.md for the list of SwiftCertificates project authors +// See CONTRIBUTORS.txt for the list of SwiftCertificates project authors // // SPDX-License-Identifier: Apache-2.0 // @@ -29,92 +29,30 @@ struct OCSPRequest: DERImplicitlyTaggable, Hashable { var signature: OCSPSignature? - init(derEncoded: ASN1Node, withIdentifier identifier: ASN1Identifier) throws { - fatalError() + init(tbsRequest: OCSPTBSRequest, signature: OCSPSignature? = nil) { + self.tbsRequest = tbsRequest + self.signature = signature } + + init(derEncoded rootNode: ASN1Node, withIdentifier identifier: ASN1Identifier) throws { + self = try DER.sequence(rootNode, identifier: identifier) { nodes in + let tbsRequest = try OCSPTBSRequest(derEncoded: &nodes) + let signature = try DER.optionalExplicitlyTagged(&nodes, tagNumber: 0, tagClass: .contextSpecific) { + try OCSPSignature(derEncoded: $0) + } + return .init(tbsRequest: tbsRequest, signature: signature) + } + } + func serialize(into coder: inout DER.Serializer, withIdentifier identifier: ASN1Identifier) throws { - fatalError() - } -} - -/// ``OCSPTBSRequest`` is defined in ASN.1 as: -/// ``` -/// TBSRequest ::= SEQUENCE { -/// version [0] EXPLICIT Version DEFAULT v1, -/// requestorName [1] EXPLICIT GeneralName OPTIONAL, -/// requestList SEQUENCE OF Request, -/// requestExtensions [2] EXPLICIT Extensions OPTIONAL } -/// -/// Version ::= INTEGER { v1(0) } -/// ``` -struct OCSPTBSRequest: DERImplicitlyTaggable, Hashable { - static var defaultIdentifier: ASN1Identifier { - .sequence - } - - var version: Int - - var requestorName: GeneralName? - - var requestList: [OCSPSingleRequest] - - var requestExtensions: Certificate.Extensions? - - init(derEncoded: ASN1Node, withIdentifier identifier: ASN1Identifier) throws { - fatalError() - } - func serialize(into coder: inout DER.Serializer, withIdentifier identifier: ASN1Identifier) throws { - fatalError() - } -} - -/// ``OCSPSingleRequest`` is defined in ASN.1 as: -/// ``` -/// SingleRequest ::= SEQUENCE { -/// reqCert CertID, -/// singleRequestExtensions [0] EXPLICIT Extensions OPTIONAL } -/// ``` -/// - note: originally named just `Request` in RFC 6960 but prefix `Single` added to avoid naming conflicts with ``OCSPRequest`` -struct OCSPSingleRequest: DERImplicitlyTaggable, Hashable { - static var defaultIdentifier: ASN1Identifier { - .sequence - } - - var certID: OCSPCertID - - var singleRequestExtensions: Certificate.Extensions? - - init(derEncoded: ASN1Node, withIdentifier identifier: ASN1Identifier) throws { - fatalError() - } - func serialize(into coder: inout DER.Serializer, withIdentifier identifier: ASN1Identifier) throws { - fatalError() + try coder.appendConstructedNode(identifier: identifier) { coder in + try coder.serialize(self.tbsRequest) + if let signature = self.signature { + try coder.serialize(signature, explicitlyTaggedWithTagNumber: 0, tagClass: .contextSpecific) + } + } } } -/// ``OCSPSignature`` is defined in ASN.1 as: -/// ``` -/// Signature ::= SEQUENCE { -/// signatureAlgorithm AlgorithmIdentifier, -/// signature BIT STRING, -/// certs [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL } -/// ``` -struct OCSPSignature: DERImplicitlyTaggable, Hashable { - static var defaultIdentifier: ASN1Identifier { - .sequence - } - - var algortihmIdentifier: AlgorithmIdentifier - - var signature: ASN1BitString - - var certs: [Certificate]? - - init(derEncoded: ASN1Node, withIdentifier identifier: ASN1Identifier) throws { - fatalError() - } - func serialize(into coder: inout DER.Serializer, withIdentifier identifier: ASN1Identifier) throws { - fatalError() - } -} + diff --git a/Sources/X509/OCSP/OCSPSignature.swift b/Sources/X509/OCSP/OCSPSignature.swift new file mode 100644 index 0000000..b96f70f --- /dev/null +++ b/Sources/X509/OCSP/OCSPSignature.swift @@ -0,0 +1,63 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftCertificates open source project +// +// Copyright (c) 2022 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 + +/// ``OCSPSignature`` is defined in ASN.1 as: +/// ``` +/// Signature ::= SEQUENCE { +/// signatureAlgorithm AlgorithmIdentifier, +/// signature BIT STRING, +/// certs [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL } +/// ``` +struct OCSPSignature: DERImplicitlyTaggable, Hashable { + static var defaultIdentifier: ASN1Identifier { + .sequence + } + + var algorithmIIdentifier: AlgorithmIdentifier + + var signature: ASN1BitString + + var certs: [Certificate]? + + init(algorithmIIdentifier: AlgorithmIdentifier, signature: ASN1BitString, certs: [Certificate]? = nil) { + self.algorithmIIdentifier = algorithmIIdentifier + self.signature = signature + self.certs = certs + } + + init(derEncoded: ASN1Node, withIdentifier identifier: ASN1Identifier) throws { + self = try DER.sequence(derEncoded, identifier: identifier) { nodes in + let algorithmIIdentifier = try AlgorithmIdentifier(derEncoded: &nodes) + let signature = try ASN1BitString(derEncoded: &nodes) + let certs = try DER.optionalExplicitlyTagged(&nodes, tagNumber: 0, tagClass: .contextSpecific) { node in + try DER.sequence(of: Certificate.self, identifier: .sequence, rootNode: node) + } + return .init(algorithmIIdentifier: algorithmIIdentifier, signature: signature, certs: certs) + } + } + + func serialize(into coder: inout DER.Serializer, withIdentifier identifier: ASN1Identifier) throws { + try coder.appendConstructedNode(identifier: identifier) { coder in + try self.algorithmIIdentifier.serialize(into: &coder) + try self.signature.serialize(into: &coder) + if let certs = self.certs { + try coder.serialize(explicitlyTaggedWithTagNumber: 0, tagClass: .contextSpecific) { coder in + try coder.serializeSequenceOf(certs) + } + } + } + } +} diff --git a/Sources/X509/OCSP/OCSPSingleRequest.swift b/Sources/X509/OCSP/OCSPSingleRequest.swift new file mode 100644 index 0000000..f14833e --- /dev/null +++ b/Sources/X509/OCSP/OCSPSingleRequest.swift @@ -0,0 +1,60 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftCertificates open source project +// +// Copyright (c) 2022 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 + +/// ``OCSPSingleRequest`` is defined in ASN.1 as: +/// ``` +/// Request ::= SEQUENCE { +/// reqCert CertID, +/// singleRequestExtensions [0] EXPLICIT Extensions OPTIONAL } +/// ``` +/// - note: originally named just `Request` in RFC 6960 but prefix `Single` added to avoid naming conflicts with ``OCSPRequest`` +struct OCSPSingleRequest: DERImplicitlyTaggable, Hashable { + static var defaultIdentifier: ASN1Identifier { + .sequence + } + + var certID: OCSPCertID + + var singleRequestExtensions: Certificate.Extensions? + + init(certID: OCSPCertID, singleRequestExtensions: Certificate.Extensions? = nil) { + self.certID = certID + self.singleRequestExtensions = singleRequestExtensions + } + + init(derEncoded: ASN1Node, withIdentifier identifier: ASN1Identifier) throws { + self = try DER.sequence(derEncoded, identifier: identifier) { nodes in + let certID = try OCSPCertID(derEncoded: &nodes) + let singleRequestExtensions = try DER.optionalExplicitlyTagged(&nodes, tagNumber: 0, tagClass: .contextSpecific) { node in + Certificate.Extensions( + extensions: try DER.sequence(of: Certificate.Extension.self, identifier: .sequence, rootNode: node) + ) + } + return .init(certID: certID, singleRequestExtensions: singleRequestExtensions) + } + } + + func serialize(into coder: inout DER.Serializer, withIdentifier identifier: ASN1Identifier) throws { + try coder.appendConstructedNode(identifier: identifier) { coder in + try self.certID.serialize(into: &coder) + if let singleRequestExtensions = self.singleRequestExtensions { + try coder.serialize(explicitlyTaggedWithTagNumber: 0, tagClass: .contextSpecific) { coder in + try coder.serializeSequenceOf(singleRequestExtensions) + } + } + } + } +} diff --git a/Sources/X509/OCSP/OCSPTBSRequest.swift b/Sources/X509/OCSP/OCSPTBSRequest.swift new file mode 100644 index 0000000..a00758b --- /dev/null +++ b/Sources/X509/OCSP/OCSPTBSRequest.swift @@ -0,0 +1,83 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftCertificates open source project +// +// Copyright (c) 2022 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 + +/// ``OCSPTBSRequest`` is defined in ASN.1 as: +/// ``` +/// TBSRequest ::= SEQUENCE { +/// version [0] EXPLICIT Version DEFAULT v1, +/// requestorName [1] EXPLICIT GeneralName OPTIONAL, +/// requestList SEQUENCE OF Request, +/// requestExtensions [2] EXPLICIT Extensions OPTIONAL } +/// +/// Version ::= INTEGER { v1(0) } +/// ``` +struct OCSPTBSRequest: DERImplicitlyTaggable, Hashable { + static var defaultIdentifier: ASN1Identifier { + .sequence + } + + var version: OCSPVersion + + var requestorName: GeneralName? + + var requestList: [OCSPSingleRequest] + + var requestExtensions: Certificate.Extensions? + + init(version: OCSPVersion, requestorName: GeneralName? = nil, requestList: [OCSPSingleRequest], requestExtensions: Certificate.Extensions? = nil) { + self.version = version + self.requestorName = requestorName + self.requestList = requestList + self.requestExtensions = requestExtensions + } + + init(derEncoded rootNode: ASN1Node, withIdentifier identifier: ASN1Identifier) throws { + self = try DER.sequence(rootNode, identifier: identifier) { nodes in + let version = try DER.decodeDefaultExplicitlyTagged(&nodes, tagNumber: 0, tagClass: .contextSpecific, defaultValue: Int(0)) + let requestorName = try DER.optionalExplicitlyTagged(&nodes, tagNumber: 1, tagClass: .contextSpecific) { + try GeneralName(derEncoded: $0) + } + let requestList = try DER.sequence(of: OCSPSingleRequest.self,identifier: .sequence, nodes: &nodes) + let extensions = try DER.optionalExplicitlyTagged(&nodes, tagNumber: 2, tagClass: .contextSpecific) { + try DER.sequence(of: Certificate.Extension.self, identifier: .sequence, rootNode: $0) + } + + return .init( + version: .init(rawValue: version), + requestorName: requestorName, + requestList: requestList, + requestExtensions: extensions.map { .init(extensions: $0) } + ) + } + } + + func serialize(into coder: inout DER.Serializer, withIdentifier identifier: ASN1Identifier) throws { + try coder.appendConstructedNode(identifier: identifier) { coder in + if self.version != .v1 { + try coder.serialize(self.version.rawValue, explicitlyTaggedWithTagNumber: 0, tagClass: .contextSpecific) + } + if let requestorName = self.requestorName { + try coder.serialize(requestorName, explicitlyTaggedWithTagNumber: 1, tagClass: .contextSpecific) + } + try coder.serializeSequenceOf(requestList) + if let requestExtensions = self.requestExtensions { + try coder.serialize(explicitlyTaggedWithTagNumber: 2, tagClass: .contextSpecific) { coder in + try coder.serializeSequenceOf(requestExtensions._extensions) + } + } + } + } +} diff --git a/Sources/X509/OCSP/OCSPVersion.swift b/Sources/X509/OCSP/OCSPVersion.swift new file mode 100644 index 0000000..0b23b56 --- /dev/null +++ b/Sources/X509/OCSP/OCSPVersion.swift @@ -0,0 +1,44 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftCertificates open source project +// +// Copyright (c) 2022 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 +// +//===----------------------------------------------------------------------===// + +struct OCSPVersion { + var rawValue: Int + + init(rawValue: Int) { + self.rawValue = rawValue + } + + static let v1 = Self(rawValue: 0) +} + +extension OCSPVersion: Hashable { } + +extension OCSPVersion: Sendable { } + +extension OCSPVersion: Comparable { + static func < (lhs: Self, rhs: Self) -> Bool { + lhs.rawValue < rhs.rawValue + } +} + +extension OCSPVersion: CustomStringConvertible { + var description: String { + switch self { + case .v1: + return "OCSPv1" + case let unknown: + return "OCSPv\(unknown.rawValue + 1)" + } + } +}