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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)