From 8ea901f2582296837d88f882b0fa5a0601759598 Mon Sep 17 00:00:00 2001 From: Levi McCallum Date: Wed, 22 Jan 2020 03:14:30 -0800 Subject: [PATCH] Use SecCertificateCopyKey API when available, falling back in edge cases (#60) --- .../contents.xcworkspacedata | 7 ++ .../xcshareddata/IDEWorkspaceChecks.plist | 8 ++ Sources/CryptorRSA/CryptorRSAKey.swift | 86 ++++++++++++------- 3 files changed, 69 insertions(+), 32 deletions(-) create mode 100644 CryptorRSA.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 CryptorRSA.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist diff --git a/CryptorRSA.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/CryptorRSA.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/CryptorRSA.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/CryptorRSA.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/CryptorRSA.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/CryptorRSA.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/Sources/CryptorRSA/CryptorRSAKey.swift b/Sources/CryptorRSA/CryptorRSAKey.swift index e50ad83..5f60317 100644 --- a/Sources/CryptorRSA/CryptorRSAKey.swift +++ b/Sources/CryptorRSA/CryptorRSAKey.swift @@ -321,38 +321,60 @@ extension CryptorRSA { } var key: SecKey? = nil - - #if swift(>=4.2) - - if #available(macOS 10.14, iOS 12.0, watchOS 5.0, *) { - - key = SecCertificateCopyKey(certData) - - } - - #endif - - if key == nil { - - #if os(macOS) - - // Now extract the public key from it... - let status: OSStatus = withUnsafeMutablePointer(to: &key) { ptr in - - // Retrieves the public key from a certificate... - SecCertificateCopyPublicKey(certData, UnsafeMutablePointer(ptr)) - } - if status != errSecSuccess { - - throw Error(code: ERR_EXTRACT_PUBLIC_KEY_FAILED, reason: "Unable to extract public key from data.") - } - - #else - - key = SecCertificateCopyPublicKey(certData) - - #endif - } + + #if swift(>=4.2) + #if os(macOS) + if #available(macOS 10.14, *) { + key = SecCertificateCopyKey(certData) + } else { + // Now extract the public key from it... + let status: OSStatus = withUnsafeMutablePointer(to: &key) { ptr in + + // Retrieves the public key from a certificate... + SecCertificateCopyPublicKey(certData, UnsafeMutablePointer(ptr)) + } + + if status != errSecSuccess { + + throw Error(code: ERR_EXTRACT_PUBLIC_KEY_FAILED, reason: "Unable to extract public key from data.") + } + } + #else + let copyKey: (SecCertificate) -> SecKey? + + #if targetEnvironment(macCatalyst) + copyKey = SecCertificateCopyKey + #else + if #available(iOS 12.0, watchOS 5.0, *) { + copyKey = SecCertificateCopyKey + } else { + copyKey = SecCertificateCopyPublicKey + } + #endif + + key = copyKey(certData) + #endif + #else + #if os(macOS) + + // Now extract the public key from it... + let status: OSStatus = withUnsafeMutablePointer(to: &key) { ptr in + + // Retrieves the public key from a certificate... + SecCertificateCopyPublicKey(certData, UnsafeMutablePointer(ptr)) + } + + if status != errSecSuccess { + + throw Error(code: ERR_EXTRACT_PUBLIC_KEY_FAILED, reason: "Unable to extract public key from data.") + } + + #else + + key = SecCertificateCopyPublicKey(certData) + + #endif + #endif guard let createdKey = key else {