Adam Johns
Adam Johns

Reputation: 36353

Is there any way to get the key type of a SecKey?

Given a SecKey, is there any way to infer its type (e.g. whether it is kSecAttrKeyTypeRSA or kSecAttrKeyTypeEC)?

I see SecKeyGetTypeID(), but it is unclear to me what key object this function operates on as it accepts no parameters.

Upvotes: 2

Views: 792

Answers (1)

Martin R
Martin R

Reputation: 539745

You can retrieve the kSecAttrKeyType from the key and check if it is kSecAttrKeyTypeRSA (or kSecAttrKeyTypeEC). Example (taken from SwiftyRSA):

func isRSAKey(seckey: SecKey) -> Bool {
    guard let attributes = SecKeyCopyAttributes(seckey) as? [CFString: Any],
        let keyType = attributes[kSecAttrKeyType] as? String else {
            return false
    }

    let isRSA = keyType == (kSecAttrKeyTypeRSA as String)
    return isRSA
}

Upvotes: 5

Related Questions