Skip to content

Commit

Permalink
Use SecCertificateCopyKey API when available, falling back in edge ca…
Browse files Browse the repository at this point in the history
…ses (Kitura#60)
  • Loading branch information
levi authored and djones6 committed Jan 22, 2020
1 parent 95e3fcd commit 8ea901f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 32 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "https://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
86 changes: 54 additions & 32 deletions Sources/CryptorRSA/CryptorRSAKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down

0 comments on commit 8ea901f

Please sign in to comment.