Skip to content

Commit aaafa59

Browse files
authored
Merge pull request #15 from apple/cb-new-asn1-error
Update to swift-asn1 new error type
2 parents 3a6e5c8 + b408daf commit aaafa59

16 files changed

+26
-24
lines changed

Package.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ let package = Package(
3131
dependencies: [
3232
.package(url: "https://github.com/apple/swift-crypto.git", from: "2.2.1"),
3333
// swift-asn1 repo is private, so we can't access it anonymously yet
34-
// .package(url: "https://github.com/apple/swift-asn1.git", .upToNextMinor(from: "0.4.0")),
35-
.package(url: "[email protected]:apple/swift-asn1.git", .upToNextMinor(from: "0.4.0")),
34+
// .package(url: "https://github.com/apple/swift-asn1.git", .upToNextMinor(from: "0.5.0")),
35+
.package(url: "[email protected]:apple/swift-asn1.git", .upToNextMinor(from: "0.5.0")),
3636
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
3737
],
3838
targets: [

Sources/X509/Certificate.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ extension Certificate: DERImplicitlyTaggable {
264264
public init(derEncoded rootNode: ASN1Node, withIdentifier identifier: ASN1Identifier) throws {
265265
self = try DER.sequence(rootNode, identifier: identifier) { nodes in
266266
guard let tbsCertificateNode = nodes.next() else {
267-
throw ASN1Error.invalidASN1Object
267+
throw ASN1Error.invalidASN1Object(reason: "TBSCertificate missing")
268268
}
269269
let tbsCertificate = try TBSCertificate(derEncoded: tbsCertificateNode)
270270
let signatureAlgorithm = try AlgorithmIdentifier(derEncoded: &nodes)

Sources/X509/CryptographicMessageSyntax/CMSSignerIdentifier.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ enum CMSSignerIdentifier: DERParseable, DERSerializable, Hashable {
3636
self = try .subjectKeyIdentifier(.init(keyIdentifier: .init(derEncoded: node, withIdentifier: Self.skiIdentifier)))
3737

3838
default:
39-
throw ASN1Error.invalidASN1Object
39+
throw ASN1Error.unexpectedFieldType(node.identifier)
4040
}
4141
}
4242

Sources/X509/Extension Types/BasicConstraints.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ struct BasicConstraintsValue: DERImplicitlyTaggable {
111111

112112
// CA's must not assert the path len constraint field unless isCA is true.
113113
guard pathLenConstraint == nil || isCA else {
114-
throw ASN1Error.invalidASN1Object
114+
throw ASN1Error.invalidASN1Object(reason: "Invalid combination of isCA (\(isCA)) and path length constraint (\(pathLenConstraint)")
115115
}
116116
}
117117

Sources/X509/Extension Types/KeyUsage.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -259,16 +259,16 @@ extension Certificate.Extensions {
259259
precondition(bitstring.paddingBits < 8)
260260
let bitMask = UInt8(0x01) << bitstring.paddingBits
261261
if (bitstring.bytes[bitstring.bytes.startIndex] & bitMask) == 0 {
262-
throw ASN1Error.invalidASN1Object
262+
throw ASN1Error.invalidASN1Object(reason: "Invalid leading padding bit")
263263
}
264264
case 2 where bitstring.paddingBits == 7:
265265
// This is fine, there are 9 valid bits: 8 from the prior byte and 1 here.
266266
if (bitstring.bytes[bitstring.bytes.startIndex &+ 1] & 0x80) == 0 {
267-
throw ASN1Error.invalidASN1Object
267+
throw ASN1Error.invalidASN1Object(reason: "Invalid padding bit")
268268
}
269269
default:
270270
// Too many bits!
271-
throw ASN1Error.invalidASN1Object
271+
throw ASN1Error.invalidASN1Object(reason: "Too many bits for Key Usage")
272272
}
273273
}
274274
}

Sources/X509/Extension Types/NameConstraints.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ extension Certificate.Extensions {
278278

279279
let nameConstraintsValue = try NameConstraintsValue(derEncoded: ext.value)
280280
guard nameConstraintsValue.permittedSubtrees != nil || nameConstraintsValue.excludedSubtrees != nil else {
281-
throw ASN1Error.invalidASN1Object
281+
throw ASN1Error.invalidASN1Object(reason: "Name Constraints has no permitted or excluded subtrees")
282282
}
283283

284284
self.permittedSubtrees = nameConstraintsValue.permittedSubtrees ?? []

Sources/X509/GeneralName.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public enum GeneralName: Hashable, Sendable, DERParseable, DERSerializable {
6969
case Self.registeredIDTag:
7070
self = try .registeredID(ASN1ObjectIdentifier(derEncoded: rootNode, withIdentifier: Self.registeredIDTag))
7171
default:
72-
throw ASN1Error.invalidFieldIdentifier
72+
throw ASN1Error.unexpectedFieldType(rootNode.identifier)
7373
}
7474
}
7575

Sources/X509/OCSP/BasicOCSPResponse.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -129,26 +129,26 @@ enum ResponderID: DERParseable, DERSerializable, Hashable {
129129
switch derEncoded.identifier {
130130
case ResponderID.nameIdentifier:
131131
guard case .constructed(let nodes) = derEncoded.content else {
132-
throw ASN1Error.invalidASN1Object
132+
throw ASN1Error.invalidASN1Object(reason: "ResponderID content must be constructed.")
133133
}
134134
var iterator = nodes.makeIterator()
135135
guard let rootNode = iterator.next(), iterator.next() == nil else {
136-
throw ASN1Error.invalidASN1Object
136+
throw ASN1Error.invalidASN1Object(reason: "Invalid number of responder nodes.")
137137
}
138138

139139
self = try .byName(.init(derEncoded: rootNode))
140140
case ResponderID.keyIdentifier:
141141
guard case .constructed(let nodes) = derEncoded.content else {
142-
throw ASN1Error.invalidASN1Object
142+
throw ASN1Error.invalidASN1Object(reason: "ResponderID content must be constructed")
143143
}
144144
var iterator = nodes.makeIterator()
145145
guard let rootNode = iterator.next(), iterator.next() == nil else {
146-
throw ASN1Error.invalidASN1Object
146+
throw ASN1Error.invalidASN1Object(reason: "Invalid number of responder nodes")
147147
}
148148

149149
self = try .byKey(.init(derEncoded: rootNode))
150150
default:
151-
throw ASN1Error.unexpectedFieldType
151+
throw ASN1Error.unexpectedFieldType(derEncoded.identifier)
152152
}
153153
}
154154

Sources/X509/OCSP/DirectoryString.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ enum DirectoryString: DERParseable, DERSerializable, Hashable {
4848
case .bmpString:
4949
self = .bmpString(try ASN1BMPString(derEncoded: rootNode))
5050
default:
51-
throw ASN1Error.unexpectedFieldType
51+
throw ASN1Error.unexpectedFieldType(rootNode.identifier)
5252
}
5353
}
5454

Sources/X509/OCSP/OCSPCertStatus.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ enum OCSPCertStatus: DERParseable, DERSerializable, Hashable {
6565
self = .unknown
6666

6767
default:
68-
throw ASN1Error.invalidASN1Object
68+
throw ASN1Error.unexpectedFieldType(node.identifier)
6969
}
7070
}
7171

Sources/X509/OCSP/OCSPNonce.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct OCSPNonce: DERImplicitlyTaggable, Hashable, Sendable {
4646
init(derEncoded rootNode: ASN1Node, withIdentifier identifier: ASN1Identifier) throws {
4747
self.rawValue = try ASN1OctetString(derEncoded: rootNode, withIdentifier: identifier)
4848
guard (1...32).contains(self.rawValue.bytes.count) else {
49-
throw ASN1Error.unsupportedFieldLength
49+
throw ASN1Error.unsupportedFieldLength(reason: "OCSP Nonce has invalid number of bytes: \(self.rawValue.bytes.count)")
5050
}
5151
}
5252

Sources/X509/OCSP/OCSPResponse.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ enum OCSPResponse: DERImplicitlyTaggable, Hashable {
4646
guard let responseBytes,
4747
responseBytes.responseType == .OCSP.basicResponse
4848
else {
49-
throw ASN1Error.invalidASN1Object
49+
throw ASN1Error.invalidASN1Object(reason: "Successful response does not have appropriate response bytes: \(responseBytes)")
5050
}
5151
return .successful(try BasicOCSPResponse(derEncoded: responseBytes.response.bytes))
5252
case .malformedRequest:
@@ -67,7 +67,9 @@ enum OCSPResponse: DERImplicitlyTaggable, Hashable {
6767
if case .successful = unsuccessfulStatus {
6868
preconditionFailure("this init is not allowed to be called with a successful response status")
6969
}
70-
guard responseBytes == nil else { throw ASN1Error.invalidASN1Object }
70+
guard responseBytes == nil else {
71+
throw ASN1Error.invalidASN1Object(reason: "Must not have response bytes for unsuccessful OCSP response")
72+
}
7173
self = unsuccessfulStatus
7274
}
7375

Sources/X509/OCSP/OCSPResponseBytes.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ struct OCSPResponseBytes: DERImplicitlyTaggable, Hashable {
6060
extension BasicOCSPResponse {
6161
init(decoding original: OCSPResponseBytes) throws {
6262
guard original.responseType == .OCSP.basicResponse else {
63-
throw ASN1Error.invalidASN1Object
63+
throw ASN1Error.invalidASN1Object(reason: "Cannot decode BasicOCSPResponse from \(original.responseType)")
6464
}
6565

6666
self = try .init(derEncoded: original.response.bytes)

Sources/X509/OCSP/OCSPResponseStatus.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ enum OCSPResponseStatus: DERImplicitlyTaggable, Hashable {
5858
case 6:
5959
self = .unauthorized
6060
default:
61-
throw ASN1Error.invalidASN1Object
61+
throw ASN1Error.invalidASN1Object(reason: "Unexpected OCSP response status: \(rawValue)")
6262
}
6363
}
6464

Sources/X509/X509BaseTypes/TBSCertificate.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ struct TBSCertificate: DERImplicitlyTaggable, Hashable, Sendable {
106106
self = try DER.sequence(rootNode, identifier: identifier) { nodes in
107107
let version = try DER.decodeDefaultExplicitlyTagged(&nodes, tagNumber: 0, tagClass: .contextSpecific, defaultValue: Int(0))
108108
guard (0...2).contains(version) else {
109-
throw ASN1Error.invalidASN1Object
109+
throw ASN1Error.invalidASN1Object(reason: "Invalid X.509 version \(version)")
110110
}
111111

112112
let serialNumber = try ArraySlice<UInt8>(derEncoded: &nodes)

Sources/X509/X509BaseTypes/Time.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ enum Time: DERParseable, DERSerializable, Hashable, Sendable {
3131
case UTCTime.defaultIdentifier:
3232
self = .utcTime(try UTCTime(derEncoded: rootNode))
3333
default:
34-
throw ASN1Error.invalidASN1Object
34+
throw ASN1Error.unexpectedFieldType(rootNode.identifier)
3535
}
3636
}
3737

0 commit comments

Comments
 (0)