Ksecattrkeytypeec Causing Encryptmessagewithpublickey() to Fail

SwiftJWT crashes iOS App on JWT.init() call

CryptorEEC is open source. The last call to the stack that isn't a system framework is ECPublicKey.swift:144.

As you can see from that link, it's creating a dictionary with this value kSecAttrKeyTypeECSECPrimeRandom. According to the next call in the stack, this is causing the crash.

I searched for kSecAttrKeyTypeECSECPrimeRandom on Google and I found this question on SO.

This answer suggests that a key larger than 256 is too big for ECSEC.

This is an old question with old answers, which leads me to believe it's crashing on older devices with older iOS versions because support for larger keys was introduced in iOS 13 or later, or in devices with Secure Enclave (I'm not sure).

Since I don't know how you're using this encrypted token, I can't make a suggestion on how to fix this, but I hope it's at least enough information for you to make a decision.

CFDictionaryCreate crashes in Xcode 8 swift 3

to generate a key pair

func generateKeyPair(_ publicTag: String, privateTag: String, keySize: Int)->Bool {
let privateAttributes = [String(kSecAttrIsPermanent): true,
String(kSecAttrApplicationTag): privateTag] as [String : Any]
let publicAttributes = [String(kSecAttrIsPermanent): true,
String(kSecAttrApplicationTag): publicTag] as [String : Any]

let pairAttributes = [String(kSecAttrKeyType): kSecAttrKeyTypeRSA,
String(kSecAttrKeySizeInBits): keySize,
String(kSecPublicKeyAttrs): publicAttributes,
String(kSecPrivateKeyAttrs): privateAttributes] as [String : Any]

var publicRef: SecKey?
var privateRef: SecKey?
switch SecKeyGeneratePair(pairAttributes as CFDictionary, &publicRef, &privateRef) {
case noErr: return true
default: return false
}
}

and few helper functions

func obtainKey(_ tag: String) -> SecKey? {
var keyRef: AnyObject?
let query: Dictionary<String, AnyObject> = [
String(kSecAttrKeyType): kSecAttrKeyTypeRSA,
String(kSecReturnRef): kCFBooleanTrue as CFBoolean,
String(kSecClass): kSecClassKey as CFString,
String(kSecAttrApplicationTag): tag as CFString,
]

let status = SecItemCopyMatching(query as CFDictionary, &keyRef)

switch status {
case noErr:
if let ref = keyRef {
return (ref as! SecKey)
}
default:
break
}

return nil
}

func obtainKeyData(_ tag: String) -> Data? {
var keyRef: AnyObject?
let query: Dictionary<String, AnyObject> = [
String(kSecAttrKeyType): kSecAttrKeyTypeRSA,
String(kSecReturnData): kCFBooleanTrue as CFBoolean,
String(kSecClass): kSecClassKey as CFString,
String(kSecAttrApplicationTag): tag as CFString,
]

let result: Data?

switch SecItemCopyMatching(query as CFDictionary, &keyRef) {
case noErr:
result = keyRef as? Data
default:
result = nil
}

return result
}

func insertPublicKey(_ publicTag: String, data: Data) -> SecKey? {
let query: Dictionary<String, AnyObject> = [
String(kSecAttrKeyType): kSecAttrKeyTypeRSA,
String(kSecClass): kSecClassKey as CFString,
String(kSecAttrApplicationTag): publicTag as CFString,
String(kSecValueData): data as CFData,
String(kSecReturnPersistentRef): true as CFBoolean]

var persistentRef: AnyObject?
let status = SecItemAdd(query as CFDictionary, &persistentRef)

if status != noErr && status != errSecDuplicateItem {
return nil
}

return obtainKey(publicTag)
}

func deleteKey(_ tag: String) -> Bool {
let query: Dictionary<String, AnyObject> = [
String(kSecAttrKeyType): kSecAttrKeyTypeRSA,
String(kSecClass): kSecClassKey as CFString,
String(kSecAttrApplicationTag): tag as CFString]

return SecItemDelete(query as CFDictionary) == noErr
}

func updateKey(_ tag: String, data: Data) -> Bool {
let query: Dictionary<String, AnyObject> = [
String(kSecAttrKeyType): kSecAttrKeyTypeRSA,
String(kSecClass): kSecClassKey as CFString,
String(kSecAttrApplicationTag): tag as CFString]

return SecItemUpdate(query as CFDictionary, [String(kSecValueData): data] as CFDictionary) == noErr
}

What is the difference between encrypting and signing in asymmetric encryption?

When encrypting, you use their public key to write a message and they use their private key to read it.

When signing, you use your private key to write message's signature, and they use your public key to check if it's really yours.

I want to use my private key to generate messages so only I can possibly be the sender.

I want my public key to be used to read the messages and I do not care who reads them

This is signing, it is done with your private key.

I want to be able to encrypt certain information and use it as a product key for my software.

I only care that I am the only one who can generate these.

If you only need to know it to yourself, you don't need to mess with keys to do this. You may just generate random data and keep it in a database.

But if you want people to know that the keys are really yours, you need to generate random data, keep in it a database AND sign it with your key.

I would like to include my public key in my software to decrypt/read the signature of the key.

You'll probably need to purchase a certificate for your public key from a commercial provider like Verisign or Thawte, so that people may check that no one had forged your software and replaced your public key with theirs.



Related Topics



Leave a reply



Submit