Use SwiftCrypto/SwiftCertificates for some stuff
- Commit
2b2ee98fd9401ca9f3182cf3fde1cdd08b0edd43- Author
- Kabir Oberai <[email protected]>
- When
- 2023-09-01 16:40 UTC (3 years ago)
- Parents
668ecdd
Diff
Supersign/Package.swift
diff --git a/Supersign/Package.swift b/Supersign/Package.swiftindex 6995d15..b232446 100644--- a/Supersign/Package.swift+++ b/Supersign/Package.swift@@ -34,6 +34,7 @@ let package = Package( .package(path: "../SwiftyMobileDevice"), .package(path: "../USBMuxSim"), .package(url: "https://github.com/swift-server/async-http-client.git", from: "1.18.0"),+ .package(url: "https://github.com/apple/swift-certificates", from: "1.0.0-beta.1"), ], targets: [ .target(@@ -53,6 +54,7 @@ let package = Package( .product(name: "SignerSupport", package: "SuperchargeCore"), .product(name: "ProtoCodable", package: "SuperchargeCore"), .product(name: "Superutils", package: "SuperchargeCore"),+ .product(name: "X509", package: "swift-certificates"), .product( name: "AsyncHTTPClient", package: "async-http-client",
Supersign/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swift
diff --git a/Supersign/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swift b/Supersign/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swiftindex 74b77ac..3601f89 100644--- a/Supersign/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swift+++ b/Supersign/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swift@@ -117,8 +117,7 @@ public struct DeveloperServicesFetchCertificateOperation: DeveloperServicesOpera } guard let signingInfo = self.context.signingInfoManager[self.context.teamID],- let serialNumber = try? signingInfo.certificate.serialNumber(),- certificate.attributes.serialNumber.rawValue == serialNumber,+ certificate.attributes.serialNumber.rawValue == signingInfo.certificate.serialNumber(), certificate.attributes.expiry > Date() else { return self.revokeCreateSaveCertificate( certificates: [certificate], completion: completion
Supersign/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesSubmitCSRRequest.swift
diff --git a/Supersign/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesSubmitCSRRequest.swift b/Supersign/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesSubmitCSRRequest.swiftindex e95c19f..76cae21 100644--- a/Supersign/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesSubmitCSRRequest.swift+++ b/Supersign/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesSubmitCSRRequest.swift@@ -23,10 +23,9 @@ public class DeveloperServicesSubmitCSRRequest: DeveloperServicesPlatformRequest var subAction: String { return "submitDevelopmentCSR" } var subParameters: [String: Any] {- let csrString = String(data: csr.data, encoding: .utf8) ?? "" return [ "teamId": teamID.rawValue,- "csrContent": csrString,+ "csrContent": csr.pemString, "machineId": machineID, "machineName": machineName ]
Supersign/Sources/Supersign/GrandSlam/Anisette/ADIDataProvider.swift
diff --git a/Supersign/Sources/Supersign/GrandSlam/Anisette/ADIDataProvider.swift b/Supersign/Sources/Supersign/GrandSlam/Anisette/ADIDataProvider.swiftindex 00ef540..4ee2561 100644--- a/Supersign/Sources/Supersign/GrandSlam/Anisette/ADIDataProvider.swift+++ b/Supersign/Sources/Supersign/GrandSlam/Anisette/ADIDataProvider.swift@@ -1,5 +1,5 @@ import Foundation-import CSupersign+import Crypto enum HashingError: Error { case hashingFailed@@ -7,12 +7,7 @@ enum HashingError: Error { private extension String { func sha256() throws -> [UInt8] {- try [UInt8](unsafeUninitializedCapacity: sup_sha256_buf_size()) { buf, count in- guard sup_sha256(self, utf8.count, buf.baseAddress!) == 0 else {- throw HashingError.hashingFailed- }- count = buf.count- }+ Array(SHA256.hash(data: Data(utf8))) } }
Supersign/Sources/Supersign/Model/Certificate/Certificate.swift
diff --git a/Supersign/Sources/Supersign/Model/Certificate/Certificate.swift b/Supersign/Sources/Supersign/Model/Certificate/Certificate.swiftindex 06adebc..c5d9e3e 100644--- a/Supersign/Sources/Supersign/Model/Certificate/Certificate.swift+++ b/Supersign/Sources/Supersign/Model/Certificate/Certificate.swift@@ -7,33 +7,31 @@ // import Foundation-import CSupersign+import X509+import SwiftASN1 /// A certificate in DER format-public final class Certificate: Codable {+public struct Certificate: Codable { public enum Error: Swift.Error { case invalidCertificate } - let raw: certificate_t+ let raw: X509.Certificate++ public init(raw: X509.Certificate) throws {+ self.raw = raw+ } public init(data: Data) throws {- self.raw = try data.withUnsafeBytes { buf -> certificate_t in- guard let base = buf.baseAddress,- let cert = certificate_create_from_data(base, buf.count)- else { throw Error.invalidCertificate }- return cert- }+ try self.init(raw: .init(derEncoded: DER.parse(Array(data)))) } public init(contentsOf url: URL) throws {- guard let certificate = url.withUnsafeFileSystemRepresentation({ $0.flatMap(certificate_create_from_path) })- else { throw Error.invalidCertificate }- self.raw = certificate+ try self.init(data: Data(contentsOf: url)) } - public required convenience init(from decoder: Decoder) throws {+ public init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() let data = try container.decode(Data.self) try self.init(data: data)@@ -44,31 +42,29 @@ public final class Certificate: Codable { try container.encode(data()) } - deinit {- certificate_free(raw)- }- public func developerIdentity() throws -> String {- guard let identity = certificate_get_developer_identity(raw) else {- throw Error.invalidCertificate- }- return String(cString: identity)+ let commonName = raw.subject.lazy.flatMap { $0 }.first { $0.type == .NameAttributes.commonName }+ guard let commonName else { throw Error.invalidCertificate }+ return "\(commonName.value)" } - public func serialNumber() throws -> String {- guard let serial = certificate_copy_serial_number(raw),- let string = String(bytesNoCopy: serial, length: strlen(serial), encoding: .utf8, freeWhenDone: true)- else { throw Error.invalidCertificate }- return string+ public func serialNumber() -> String {+ // big endian, hex-encoded+ let content = raw.serialNumber.bytes.lazy+ .map { String($0, radix: 16, uppercase: true) }+ .joined()+ .drop { $0 == "0" }+ return String(content) } public func wasIssuedBefore(_ other: Certificate) -> Bool {- certificate_was_issued_before(raw, other.raw)+ raw.notValidBefore < other.raw.notValidBefore } public func data() throws -> Data {- try Data { certificate_generate_data(raw, &$0) }- .orThrow(Error.invalidCertificate)+ var serializer = DER.Serializer()+ try serializer.serialize(raw)+ return Data(serializer.serializedBytes) } }
Supersign/Sources/Supersign/Model/Keypair/Keypair.swift
diff --git a/Supersign/Sources/Supersign/Model/Keypair/Keypair.swift b/Supersign/Sources/Supersign/Model/Keypair/Keypair.swiftindex e60921e..6131cc6 100644--- a/Supersign/Sources/Supersign/Model/Keypair/Keypair.swift+++ b/Supersign/Sources/Supersign/Model/Keypair/Keypair.swift@@ -7,58 +7,57 @@ // import Foundation-import CSupersign+import Crypto+import X509+import _CryptoExtras public struct PrivateKey: Codable {+ // pem encoded public let data: Data public init(data: Data) { self.data = data }-- public init(contentsOf url: URL) throws {- self.data = try Data(contentsOf: url)- } } public struct CSR {- public let data: Data+ public let pemString: String - public init(data: Data) {- self.data = data- }-- public init(contentsOf url: URL) throws {- self.data = try Data(contentsOf: url)+ public init(pemString: String) {+ self.pemString = pemString } } -public final class Keypair {+public struct Keypair { public enum Error: Swift.Error { case couldNotCreate case invalidKeypair } - let raw: keypair_t+ // Apple developer certs seem to require RSA2048+ let raw: _RSA.Signing.PrivateKey public init() throws {- guard let keypair = keypair_create() else {- throw Error.couldNotCreate- }- self.raw = keypair- }-- deinit {- keypair_free(raw)+ self.raw = try _RSA.Signing.PrivateKey(keySize: .bits2048) } public func privateKey() throws -> PrivateKey {- try PrivateKey(data: Data { keypair_copy_private_key(raw, &$0) }.orThrow(Error.invalidKeypair))+ PrivateKey(data: Data(raw.pemRepresentation.utf8)) } public func generateCSR() throws -> CSR {- try CSR(data: Data { keypair_generate_csr(raw, &$0) }.orThrow(Error.invalidKeypair))+ let request = try CertificateSigningRequest(+ version: .v1,+ subject: DistinguishedName {+ CountryName("US")+ CommonName("Supercharge")+ },+ privateKey: X509.Certificate.PrivateKey(raw),+ attributes: CertificateSigningRequest.Attributes(),+ signatureAlgorithm: .sha1WithRSAEncryption+ )+ return CSR(pemString: try request.serializeAsPEM().pemString) } }