Merge pull request #15 from apple/cb-new-asn1-error

Update to swift-asn1 new error type
This commit is contained in:
Cory Benfield authored and GitHub committed 2023-02-06 12:10:27 +00:00
commit aaafa59d48
16 files changed
+26 -24

No files matched your search

+2 -2
View File
@@ -31,8 +31,8 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/apple/swift-crypto.git", from: "2.2.1"),
// swift-asn1 repo is private, so we can't access it anonymously yet
// .package(url: "https://github.com/apple/swift-asn1.git", .upToNextMinor(from: "0.4.0")),
.package(url: "git@github.com:apple/swift-asn1.git", .upToNextMinor(from: "0.4.0")),
// .package(url: "https://github.com/apple/swift-asn1.git", .upToNextMinor(from: "0.5.0")),
.package(url: "git@github.com:apple/swift-asn1.git", .upToNextMinor(from: "0.5.0")),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
],
targets: [
+1 -1
View File
@@ -264,7 +264,7 @@ extension Certificate: DERImplicitlyTaggable {
public init(derEncoded rootNode: ASN1Node, withIdentifier identifier: ASN1Identifier) throws {
self = try DER.sequence(rootNode, identifier: identifier) { nodes in
guard let tbsCertificateNode = nodes.next() else {
throw ASN1Error.invalidASN1Object
throw ASN1Error.invalidASN1Object(reason: "TBSCertificate missing")
}
let tbsCertificate = try TBSCertificate(derEncoded: tbsCertificateNode)
let signatureAlgorithm = try AlgorithmIdentifier(derEncoded: &nodes)
@@ -36,7 +36,7 @@ enum CMSSignerIdentifier: DERParseable, DERSerializable, Hashable {
self = try .subjectKeyIdentifier(.init(keyIdentifier: .init(derEncoded: node, withIdentifier: Self.skiIdentifier)))
default:
throw ASN1Error.invalidASN1Object
throw ASN1Error.unexpectedFieldType(node.identifier)
}
}
@@ -111,7 +111,7 @@ struct BasicConstraintsValue: DERImplicitlyTaggable {
// CA's must not assert the path len constraint field unless isCA is true.
guard pathLenConstraint == nil || isCA else {
throw ASN1Error.invalidASN1Object
throw ASN1Error.invalidASN1Object(reason: "Invalid combination of isCA (\(isCA)) and path length constraint (\(pathLenConstraint)")
}
}
+3 -3
View File
@@ -259,16 +259,16 @@ extension Certificate.Extensions {
precondition(bitstring.paddingBits < 8)
let bitMask = UInt8(0x01) << bitstring.paddingBits
if (bitstring.bytes[bitstring.bytes.startIndex] & bitMask) == 0 {
throw ASN1Error.invalidASN1Object
throw ASN1Error.invalidASN1Object(reason: "Invalid leading padding bit")
}
case 2 where bitstring.paddingBits == 7:
// This is fine, there are 9 valid bits: 8 from the prior byte and 1 here.
if (bitstring.bytes[bitstring.bytes.startIndex &+ 1] & 0x80) == 0 {
throw ASN1Error.invalidASN1Object
throw ASN1Error.invalidASN1Object(reason: "Invalid padding bit")
}
default:
// Too many bits!
throw ASN1Error.invalidASN1Object
throw ASN1Error.invalidASN1Object(reason: "Too many bits for Key Usage")
}
}
}
@@ -278,7 +278,7 @@ extension Certificate.Extensions {
let nameConstraintsValue = try NameConstraintsValue(derEncoded: ext.value)
guard nameConstraintsValue.permittedSubtrees != nil || nameConstraintsValue.excludedSubtrees != nil else {
throw ASN1Error.invalidASN1Object
throw ASN1Error.invalidASN1Object(reason: "Name Constraints has no permitted or excluded subtrees")
}
self.permittedSubtrees = nameConstraintsValue.permittedSubtrees ?? []
+1 -1
View File
@@ -69,7 +69,7 @@ public enum GeneralName: Hashable, Sendable, DERParseable, DERSerializable {
case Self.registeredIDTag:
self = try .registeredID(ASN1ObjectIdentifier(derEncoded: rootNode, withIdentifier: Self.registeredIDTag))
default:
throw ASN1Error.invalidFieldIdentifier
throw ASN1Error.unexpectedFieldType(rootNode.identifier)
}
}
+5 -5
View File
@@ -129,26 +129,26 @@ enum ResponderID: DERParseable, DERSerializable, Hashable {
switch derEncoded.identifier {
case ResponderID.nameIdentifier:
guard case .constructed(let nodes) = derEncoded.content else {
throw ASN1Error.invalidASN1Object
throw ASN1Error.invalidASN1Object(reason: "ResponderID content must be constructed.")
}
var iterator = nodes.makeIterator()
guard let rootNode = iterator.next(), iterator.next() == nil else {
throw ASN1Error.invalidASN1Object
throw ASN1Error.invalidASN1Object(reason: "Invalid number of responder nodes.")
}
self = try .byName(.init(derEncoded: rootNode))
case ResponderID.keyIdentifier:
guard case .constructed(let nodes) = derEncoded.content else {
throw ASN1Error.invalidASN1Object
throw ASN1Error.invalidASN1Object(reason: "ResponderID content must be constructed")
}
var iterator = nodes.makeIterator()
guard let rootNode = iterator.next(), iterator.next() == nil else {
throw ASN1Error.invalidASN1Object
throw ASN1Error.invalidASN1Object(reason: "Invalid number of responder nodes")
}
self = try .byKey(.init(derEncoded: rootNode))
default:
throw ASN1Error.unexpectedFieldType
throw ASN1Error.unexpectedFieldType(derEncoded.identifier)
}
}
+1 -1
View File
@@ -48,7 +48,7 @@ enum DirectoryString: DERParseable, DERSerializable, Hashable {
case .bmpString:
self = .bmpString(try ASN1BMPString(derEncoded: rootNode))
default:
throw ASN1Error.unexpectedFieldType
throw ASN1Error.unexpectedFieldType(rootNode.identifier)
}
}
+1 -1
View File
@@ -65,7 +65,7 @@ enum OCSPCertStatus: DERParseable, DERSerializable, Hashable {
self = .unknown
default:
throw ASN1Error.invalidASN1Object
throw ASN1Error.unexpectedFieldType(node.identifier)
}
}
+1 -1
View File
@@ -46,7 +46,7 @@ struct OCSPNonce: DERImplicitlyTaggable, Hashable, Sendable {
init(derEncoded rootNode: ASN1Node, withIdentifier identifier: ASN1Identifier) throws {
self.rawValue = try ASN1OctetString(derEncoded: rootNode, withIdentifier: identifier)
guard (1...32).contains(self.rawValue.bytes.count) else {
throw ASN1Error.unsupportedFieldLength
throw ASN1Error.unsupportedFieldLength(reason: "OCSP Nonce has invalid number of bytes: \(self.rawValue.bytes.count)")
}
}
+4 -2
View File
@@ -46,7 +46,7 @@ enum OCSPResponse: DERImplicitlyTaggable, Hashable {
guard let responseBytes,
responseBytes.responseType == .OCSP.basicResponse
else {
throw ASN1Error.invalidASN1Object
throw ASN1Error.invalidASN1Object(reason: "Successful response does not have appropriate response bytes: \(responseBytes)")
}
return .successful(try BasicOCSPResponse(derEncoded: responseBytes.response.bytes))
case .malformedRequest:
@@ -67,7 +67,9 @@ enum OCSPResponse: DERImplicitlyTaggable, Hashable {
if case .successful = unsuccessfulStatus {
preconditionFailure("this init is not allowed to be called with a successful response status")
}
guard responseBytes == nil else { throw ASN1Error.invalidASN1Object }
guard responseBytes == nil else {
throw ASN1Error.invalidASN1Object(reason: "Must not have response bytes for unsuccessful OCSP response")
}
self = unsuccessfulStatus
}
+1 -1
View File
@@ -60,7 +60,7 @@ struct OCSPResponseBytes: DERImplicitlyTaggable, Hashable {
extension BasicOCSPResponse {
init(decoding original: OCSPResponseBytes) throws {
guard original.responseType == .OCSP.basicResponse else {
throw ASN1Error.invalidASN1Object
throw ASN1Error.invalidASN1Object(reason: "Cannot decode BasicOCSPResponse from \(original.responseType)")
}
self = try .init(derEncoded: original.response.bytes)
+1 -1
View File
@@ -58,7 +58,7 @@ enum OCSPResponseStatus: DERImplicitlyTaggable, Hashable {
case 6:
self = .unauthorized
default:
throw ASN1Error.invalidASN1Object
throw ASN1Error.invalidASN1Object(reason: "Unexpected OCSP response status: \(rawValue)")
}
}
@@ -106,7 +106,7 @@ struct TBSCertificate: DERImplicitlyTaggable, Hashable, Sendable {
self = try DER.sequence(rootNode, identifier: identifier) { nodes in
let version = try DER.decodeDefaultExplicitlyTagged(&nodes, tagNumber: 0, tagClass: .contextSpecific, defaultValue: Int(0))
guard (0...2).contains(version) else {
throw ASN1Error.invalidASN1Object
throw ASN1Error.invalidASN1Object(reason: "Invalid X.509 version \(version)")
}
let serialNumber = try ArraySlice<UInt8>(derEncoded: &nodes)
+1 -1
View File
@@ -31,7 +31,7 @@ enum Time: DERParseable, DERSerializable, Hashable, Sendable {
case UTCTime.defaultIdentifier:
self = .utcTime(try UTCTime(derEncoded: rootNode))
default:
throw ASN1Error.invalidASN1Object
throw ASN1Error.unexpectedFieldType(rootNode.identifier)
}
}