Implement de/encoding

This commit is contained in:
David Nadoba committed 2022-12-14 12:56:57 +01:00
1 parent 54aa3a0d96
commit eef0736e81
5 files changed
+272 -84

No files matched your search

+22 -84
View File
@@ -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()
}
}
+63
View File
@@ -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)
}
}
}
}
}
+60
View File
@@ -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)
}
}
}
}
}
+83
View File
@@ -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)
}
}
}
}
}
+44
View File
@@ -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)"
}
}
}