-
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.
Initial extraction of certificate information
Closes #9 Currently only the summary and expiration date are extracted, though the door is open for futher improvements!
- Loading branch information
James Sherlock
committed
May 13, 2018
1 parent
b294709
commit 3cda1bd
Showing
3 changed files
with
72 additions
and
1 deletion.
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
69 changes: 69 additions & 0 deletions
69
Sources/SwiftyProvisioningProfile/Model/SecureCertificate.swift
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,69 @@ | ||
// | ||
// SecureCertificate.swift | ||
// SwiftyProvisioningProfile | ||
// | ||
// Created by Sherlock, James on 13/05/2018. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct SecureCertificate: CustomStringConvertible, Equatable { | ||
|
||
public enum CertificateError: Error { | ||
case failedToCreate | ||
case failedToObtainSummary | ||
case failedToObtainValues | ||
} | ||
|
||
public let summary: String | ||
public let expiryDate: Date? | ||
|
||
public init(base64EncodedData: Data) throws { | ||
|
||
// Create Certificate | ||
|
||
guard let certificate = SecCertificateCreateWithData(nil, base64EncodedData as CFData) else { | ||
throw CertificateError.failedToCreate | ||
} | ||
|
||
// Error | ||
|
||
var error: Unmanaged<CFError>? | ||
|
||
func checkError() throws { | ||
if let error = error { | ||
throw error.takeUnretainedValue() | ||
} | ||
} | ||
|
||
// Summary | ||
|
||
guard let summary = SecCertificateCopySubjectSummary(certificate) else { | ||
throw CertificateError.failedToObtainSummary | ||
} | ||
|
||
self.summary = summary as String | ||
|
||
// Values (Expiry) | ||
|
||
let valuesKeys = [ | ||
kSecOIDInvalidityDate | ||
] as CFArray | ||
|
||
let values = SecCertificateCopyValues(certificate, valuesKeys, &error) | ||
try checkError() | ||
|
||
guard let dictionary = values as? Dictionary<CFString, Any> else { | ||
throw CertificateError.failedToObtainValues | ||
} | ||
|
||
let expiryDateDictionary = dictionary[kSecOIDInvalidityDate] as? [String: Any] | ||
expiryDate = expiryDateDictionary?["value"] as? Date | ||
|
||
} | ||
|
||
public var description: String { | ||
return "\(summary), Expires: \(expiryDate?.description ?? "No Expiry Date")" | ||
} | ||
|
||
} |
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