-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSwiftyCertificate.swift
47 lines (35 loc) · 1.31 KB
/
SwiftyCertificate.swift
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
//
// SwiftyCertificate.swift
// SwiftyProvisioningProfile
//
// Created by Sherlock, James on 20/11/2018.
//
import Foundation
import Security
public extension Certificate {
public enum ParseError: Error {
case failedToCreateCertificate
case failedToCreateTrust
case failedToExtractValues
}
public static func parse(from data: Data) throws -> Certificate {
let certificate = try getSecCertificate(data: data)
var error: Unmanaged<CFError>?
let values = SecCertificateCopyValues(certificate, nil, &error)
if let error = error {
throw error.takeRetainedValue() as Error
}
guard let valuesDict = values as? [CFString: Any] else {
throw ParseError.failedToExtractValues
}
var commonName: CFString?
SecCertificateCopyCommonName(certificate, &commonName)
return try Certificate(results: valuesDict, commonName: commonName as String?)
}
private static func getSecCertificate(data: Data) throws -> SecCertificate {
guard let certificate = SecCertificateCreateWithData(kCFAllocatorDefault, data as CFData) else {
throw ParseError.failedToCreateCertificate
}
return certificate
}
}