1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
import Foundation
import Security
struct SSLCheckService {
static func check(domain: String) async throws -> SSLCertificateInfo {
let delegate = SSLSessionDelegate()
let session = URLSession(
configuration: .ephemeral,
delegate: delegate,
delegateQueue: nil
)
defer { session.invalidateAndCancel() }
let url = URL(string: "https://\(domain)")!
let request = URLRequest(url: url, timeoutInterval: 10)
// We only need to establish the connection to grab the cert
_ = try await session.data(for: request)
guard let trust = delegate.serverTrust else {
throw SSLError.noCertificate
}
return try extractCertificateInfo(from: trust)
}
private static func extractCertificateInfo(from trust: SecTrust) throws -> SSLCertificateInfo {
let chainCount = SecTrustGetCertificateCount(trust)
guard chainCount > 0,
let certChain = SecTrustCopyCertificateChain(trust) as? [SecCertificate],
let leaf = certChain.first else {
throw SSLError.noCertificate
}
// Common Name — use subject summary (available on iOS)
let commonName = SecCertificateCopySubjectSummary(leaf) as String? ?? "Unknown"
// Validity dates
let validFrom: Date
let validUntil: Date
if let notBefore = SecCertificateCopyNotValidBeforeDate(leaf) as Date? {
validFrom = notBefore
} else {
validFrom = Date.distantPast
}
if let notAfter = SecCertificateCopyNotValidAfterDate(leaf) as Date? {
validUntil = notAfter
} else {
validUntil = Date.distantFuture
}
let daysUntilExpiry = Calendar.current.dateComponents([.day], from: Date(), to: validUntil).day ?? 0
// Parse the DER-encoded certificate to extract SANs and Issuer
let derData = SecCertificateCopyData(leaf) as Data
let parsed = DERCertificateParser.parse(derData)
let sans = parsed.subjectAltNames.isEmpty ? [commonName] : parsed.subjectAltNames
// Issuer: prefer parsed issuer, fall back to chain's next cert summary
var issuer = parsed.issuerCommonName ?? "Unknown"
if issuer == "Unknown" && certChain.count > 1 {
let issuerCert = certChain[1]
if let issuerSummary = SecCertificateCopySubjectSummary(issuerCert) as String? {
issuer = issuerSummary
}
}
return SSLCertificateInfo(
commonName: commonName,
subjectAltNames: sans,
issuer: issuer,
validFrom: validFrom,
validUntil: validUntil,
daysUntilExpiry: daysUntilExpiry,
chainDepth: Int(chainCount)
)
}
}
// MARK: - Minimal DER/ASN.1 parser for X.509 certificate fields
private enum DERCertificateParser {
struct Result {
var issuerCommonName: String?
var subjectAltNames: [String] = []
}
static func parse(_ data: Data) -> Result {
var result = Result()
let bytes = [UInt8](data)
// X.509 structure: SEQUENCE { tbsCertificate, signatureAlgorithm, signatureValue }
// tbsCertificate: SEQUENCE { version, serialNumber, signature, issuer, validity, subject, ... extensions }
guard let tbsRange = readSequence(bytes, offset: 0),
let tbsContent = readSequence(bytes, offset: tbsRange.contentStart) else {
return result
}
var offset = tbsContent.contentStart
// Skip version (explicit tag [0]) if present
if offset < bytes.count && (bytes[offset] & 0xE0) == 0xA0 {
if let tagLen = readTagAndLength(bytes, offset: offset) {
offset = tagLen.contentStart + tagLen.length
}
}
// Skip serialNumber
if let serial = readTagAndLength(bytes, offset: offset) {
offset = serial.contentStart + serial.length
}
// Skip signature algorithm
if let sigAlg = readTagAndLength(bytes, offset: offset) {
offset = sigAlg.contentStart + sigAlg.length
}
// Issuer — a SEQUENCE of SETs of attribute type-value pairs
if let issuerSeq = readTagAndLength(bytes, offset: offset) {
result.issuerCommonName = extractCommonName(bytes, sequenceStart: issuerSeq.contentStart, length: issuerSeq.length)
offset = issuerSeq.contentStart + issuerSeq.length
}
// Skip validity
if let validity = readTagAndLength(bytes, offset: offset) {
offset = validity.contentStart + validity.length
}
// Skip subject
if let subject = readTagAndLength(bytes, offset: offset) {
offset = subject.contentStart + subject.length
}
// Skip subjectPublicKeyInfo
if let spki = readTagAndLength(bytes, offset: offset) {
offset = spki.contentStart + spki.length
}
// Extensions are in an explicit tag [3]
while offset < tbsContent.contentStart + tbsContent.length {
if bytes[offset] == 0xA3 {
if let extWrapper = readTagAndLength(bytes, offset: offset) {
// Inside is a SEQUENCE of SEQUENCE extensions
if let extsSeq = readTagAndLength(bytes, offset: extWrapper.contentStart) {
result.subjectAltNames = extractSANs(bytes, sequenceStart: extsSeq.contentStart, length: extsSeq.length)
}
}
break
}
// Skip optional implicit tags (issuerUniqueID [1], subjectUniqueID [2])
if let tl = readTagAndLength(bytes, offset: offset) {
offset = tl.contentStart + tl.length
} else {
break
}
}
return result
}
// OID for commonName: 2.5.4.3 = 55 04 03
private static let cnOID: [UInt8] = [0x55, 0x04, 0x03]
// OID for subjectAltName: 2.5.29.17 = 55 1D 11
private static let sanOID: [UInt8] = [0x55, 0x1D, 0x11]
private static func extractCommonName(_ bytes: [UInt8], sequenceStart: Int, length: Int) -> String? {
let end = sequenceStart + length
var pos = sequenceStart
while pos < end {
// Each SET in the issuer
guard let setTL = readTagAndLength(bytes, offset: pos) else { break }
let setEnd = setTL.contentStart + setTL.length
// Inside the SET is a SEQUENCE with OID + value
if let seqTL = readTagAndLength(bytes, offset: setTL.contentStart) {
let seqEnd = seqTL.contentStart + seqTL.length
if let oidTL = readTagAndLength(bytes, offset: seqTL.contentStart) {
let oidBytes = Array(bytes[oidTL.contentStart..<oidTL.contentStart + oidTL.length])
if oidBytes == cnOID {
let valueStart = oidTL.contentStart + oidTL.length
if let valueTL = readTagAndLength(bytes, offset: valueStart) {
let strBytes = bytes[valueTL.contentStart..<valueTL.contentStart + valueTL.length]
return String(bytes: strBytes, encoding: .utf8)
}
}
_ = seqEnd // suppress unused warning
}
}
pos = setEnd
}
return nil
}
private static func extractSANs(_ bytes: [UInt8], sequenceStart: Int, length: Int) -> [String] {
let end = sequenceStart + length
var pos = sequenceStart
var sans: [String] = []
while pos < end {
guard let extSeq = readTagAndLength(bytes, offset: pos) else { break }
let extEnd = extSeq.contentStart + extSeq.length
// Each extension is SEQUENCE { OID, [critical], value }
if let oidTL = readTagAndLength(bytes, offset: extSeq.contentStart) {
let oidBytes = Array(bytes[oidTL.contentStart..<oidTL.contentStart + oidTL.length])
if oidBytes == sanOID {
var valuePos = oidTL.contentStart + oidTL.length
// Skip optional critical BOOLEAN
if valuePos < extEnd && bytes[valuePos] == 0x01 {
if let boolTL = readTagAndLength(bytes, offset: valuePos) {
valuePos = boolTL.contentStart + boolTL.length
}
}
// The value is an OCTET STRING wrapping a SEQUENCE of GeneralNames
if let octetTL = readTagAndLength(bytes, offset: valuePos) {
if let sanSeq = readTagAndLength(bytes, offset: octetTL.contentStart) {
let sanEnd = sanSeq.contentStart + sanSeq.length
var sanPos = sanSeq.contentStart
while sanPos < sanEnd {
guard let nameTL = readTagAndLength(bytes, offset: sanPos) else { break }
// Context tag [2] = dNSName (IA5String)
if (bytes[sanPos] & 0x1F) == 2 {
let nameBytes = bytes[nameTL.contentStart..<nameTL.contentStart + nameTL.length]
if let name = String(bytes: nameBytes, encoding: .ascii) {
sans.append(name)
}
}
sanPos = nameTL.contentStart + nameTL.length
}
}
}
}
}
pos = extEnd
}
return sans
}
private struct TLV {
let contentStart: Int
let length: Int
}
private static func readSequence(_ bytes: [UInt8], offset: Int) -> TLV? {
guard offset < bytes.count, bytes[offset] == 0x30 else { return nil }
return readTagAndLength(bytes, offset: offset)
}
private static func readTagAndLength(_ bytes: [UInt8], offset: Int) -> TLV? {
guard offset < bytes.count else { return nil }
var pos = offset + 1 // skip tag byte
guard pos < bytes.count else { return nil }
let firstLen = bytes[pos]
pos += 1
let length: Int
if firstLen < 0x80 {
length = Int(firstLen)
} else {
let numBytes = Int(firstLen & 0x7F)
guard numBytes > 0, numBytes <= 4, pos + numBytes <= bytes.count else { return nil }
var len = 0
for i in 0..<numBytes {
len = (len << 8) | Int(bytes[pos + i])
}
pos += numBytes
length = len
}
return TLV(contentStart: pos, length: length)
}
}
enum SSLError: LocalizedError {
case noCertificate
case connectionFailed
var errorDescription: String? {
switch self {
case .noCertificate:
return "No certificate found"
case .connectionFailed:
return "Failed to connect to server"
}
}
}
final class SSLSessionDelegate: NSObject, URLSessionDelegate, @unchecked Sendable {
private let lock = NSLock()
private var _serverTrust: SecTrust?
var serverTrust: SecTrust? {
lock.lock()
defer { lock.unlock() }
return _serverTrust
}
func urlSession(
_ session: URLSession,
didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void
) {
guard challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust,
let trust = challenge.protectionSpace.serverTrust else {
completionHandler(.performDefaultHandling, nil)
return
}
lock.lock()
_serverTrust = trust
lock.unlock()
let credential = URLCredential(trust: trust)
completionHandler(.useCredential, credential)
}
}
|