-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaced system for extracting certificates
Allows for independent parsing of certificates for external tooling. Breaking change.
- Loading branch information
James Sherlock
committed
Nov 20, 2018
1 parent
3cda1bd
commit e0bb732
Showing
6 changed files
with
129 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
/.build | ||
/Packages | ||
/*.xcodeproj | ||
*.mobileprovision | ||
Tests/SwiftyProvisioningProfileTests/Resources/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// Certificate.swift | ||
// SwiftyProvisioningProfile | ||
// | ||
// Created by Sherlock, James on 20/11/2018. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct Certificate: Encodable, Equatable { | ||
|
||
public enum InitError: Error { | ||
case failedToFindValue(key: String) | ||
case failedToCastValue(expected: String, actual: String) | ||
} | ||
|
||
public let notValidBefore: Date | ||
public let notValidAfter: Date | ||
|
||
init(results: [CFString: Any]) throws { | ||
notValidBefore = try Certificate.getValue(for: kSecOIDX509V1ValidityNotBefore, from: results) | ||
notValidAfter = try Certificate.getValue(for: kSecOIDX509V1ValidityNotAfter, from: results) | ||
// TODO: Add more values to this | ||
} | ||
|
||
static func getValue<T>(for key: CFString, from values: [CFString: Any]) throws -> T { | ||
let node = values[key] as? [CFString: Any] | ||
|
||
guard let rawValue = node?[kSecPropertyKeyValue] else { | ||
throw InitError.failedToFindValue(key: key as String) | ||
} | ||
|
||
if T.self is Date.Type { | ||
if let value = rawValue as? TimeInterval { | ||
// Force unwrap here is fine as we've validated the type above | ||
return Date(timeIntervalSinceReferenceDate: value) as! T | ||
} | ||
} | ||
|
||
guard let value = rawValue as? T else { | ||
let type = (node?[kSecPropertyKeyType] as? String) ?? String(describing: rawValue) | ||
throw InitError.failedToCastValue(expected: String(describing: T.self), actual: type) | ||
} | ||
|
||
return value | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 0 additions & 69 deletions
69
Sources/SwiftyProvisioningProfile/Model/SecureCertificate.swift
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// 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 | ||
} | ||
|
||
return try Certificate(results: valuesDict) | ||
} | ||
|
||
private static func getSecCertificate(data: Data) throws -> SecCertificate { | ||
guard let certificate = SecCertificateCreateWithData(kCFAllocatorDefault, data as CFData) else { | ||
throw ParseError.failedToCreateCertificate | ||
} | ||
|
||
return certificate | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters