Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Could not generate keypair." when running on simulator #19

Open
ma-pe opened this issue Feb 19, 2018 · 17 comments
Open

"Could not generate keypair." when running on simulator #19

ma-pe opened this issue Feb 19, 2018 · 17 comments

Comments

@ma-pe
Copy link

ma-pe commented Feb 19, 2018

Hi! :)

thanks for developing and maintaining this library - this is great!

I just added some code to generate a keypair and sign a digest with it. While this works perfectly on my device (using secure enclave), I have some trouble getting it to run on simulator (using the keychain).

I get this error:

underlying(message: "Could not generate keypair.", error: Error Domain=NSOSStatusErrorDomain Code=-25293 "Could not generate keypair." UserInfo={NSLocalizedRecoverySuggestion=See https://www.osstatus.com/search/results?platform=all&framework=all&search=-25293, NSLocalizedDescription=Could not generate keypair.})

Here is my code:

struct KeyPair {
  static let manager: EllipticCurveKeyPair.Manager = {
    let publicAccessControl = EllipticCurveKeyPair.AccessControl(protection: kSecAttrAccessibleAlwaysThisDeviceOnly, flags: [])
    let privateAccessControl = EllipticCurveKeyPair.AccessControl(protection: kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly, flags: [.userPresence, .privateKeyUsage])
    let config = EllipticCurveKeyPair.Config(
      publicLabel: "n.sign.public",
      privateLabel: "n.sign.private",
      operationPrompt: "N SecureElement",
      publicKeyAccessControl: publicAccessControl,
      privateKeyAccessControl: privateAccessControl,
      publicKeyAccessGroup: nil,
      privateKeyAccessGroup: nil,
      fallbackToKeychainIfSecureEnclaveIsNotAvailable: true
    )
    return EllipticCurveKeyPair.Manager(config: config)
  }()
}

and

do {
      let digest = "some_digest".data(using: .utf8)!
      signature = try KeyPair.manager.sign(digest)
} catch {
...
}

Any ideas what I am doing wrong?
Thank you.

@hfossli
Copy link
Contributor

hfossli commented Feb 19, 2018

Hey. Thanks for the kind words. ❤️

Can you try latest code from master? Seems like you are using release. Which simulator are you using?

@hfossli
Copy link
Contributor

hfossli commented Feb 19, 2018

I’m guessing you are using iOS 9 or something? I think your issue has been corrected in master. Sorry about the breaking api changes.

@ma-pe
Copy link
Author

ma-pe commented Feb 19, 2018

@hfossli
thank you very much for the fast response.

You were right, I was using release. I just updated to master and did the corresponding api-changes.

Testing on device (secure enclave) is still successful. Testing on simulator (keychain) still responds with an error, but now it is another one:

underlying(message: "Could not generate keypair.", error: Error Domain=NSOSStatusErrorDomain Code=-26276 "Could not generate keypair." UserInfo={NSLocalizedRecoverySuggestion=See https://www.osstatus.com/search/results?platform=all&framework=all&search=-26276, NSLocalizedDescription=Could not generate keypair.})

OSStatus.com sais: An internal error occured in the Security framework.

I tried different iPhone simulators featuring iOS 11.2.

@hfossli
Copy link
Contributor

hfossli commented Feb 19, 2018

Are you on main thread/queue? Try to be on another queue. Can you paste the code?

@hfossli
Copy link
Contributor

hfossli commented Feb 19, 2018

Also, make sure you delete and recreate the keys.

@hfossli
Copy link
Contributor

hfossli commented Feb 19, 2018

And try with these access flags

let publicAccessControl = EllipticCurveKeyPair.AccessControl(protection: kSecAttrAccessibleAlwaysThisDeviceOnly, flags: [])
            let privateAccessControl = EllipticCurveKeyPair.AccessControl(protection: kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly, flags: {
                return EllipticCurveKeyPair.Device.hasSecureEnclave ? [.userPresence, .privateKeyUsage] : [.userPresence]
            }())

@ma-pe
Copy link
Author

ma-pe commented Feb 20, 2018

The code I am using is:

struct KeyPair {
  static let manager: EllipticCurveKeyPair.Manager = {
    let publicAccessControl = EllipticCurveKeyPair.AccessControl(protection: kSecAttrAccessibleAlwaysThisDeviceOnly, flags: [])
    let privateAccessControl = EllipticCurveKeyPair.AccessControl(protection: kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly, flags: {
      return EllipticCurveKeyPair.Device.hasSecureEnclave ? [.userPresence, .privateKeyUsage] : [.userPresence]
    }())
    let config = EllipticCurveKeyPair.Config(
      publicLabel: "n.sign.public",
      privateLabel: "n.sign.private",
      operationPrompt: "n Ident",
      publicKeyAccessControl: publicAccessControl,
      privateKeyAccessControl: privateAccessControl,
      token: .secureEnclave)
    return EllipticCurveKeyPair.Manager(config: config)
  }()
}

No difference for me on different queues :(

How do I remove keys?

@hfossli
Copy link
Contributor

hfossli commented Feb 20, 2018

do {
      try KeyPair.manager.deleteKeypair()
} catch {
...
}

@hfossli
Copy link
Contributor

hfossli commented Feb 20, 2018

Or just delete the app before you install

@ma-pe
Copy link
Author

ma-pe commented Feb 20, 2018

Thank you.

I am now deleting the keypair before creating a new one. On device everything works fine, on simulator still the -26276-error.

@hfossli
Copy link
Contributor

hfossli commented Feb 20, 2018

I will try to recreate the bug on my end. I need xcode version, mac version, simulator version etc.

@ma-pe
Copy link
Author

ma-pe commented Feb 20, 2018

That would be great!

macOS High Sierra 10.13.3
XCode Version 9.2
iPhone simulators iOS 11.2
Deployment Target 10.0

do you need anything else?

@hfossli
Copy link
Contributor

hfossli commented Feb 20, 2018

Xcode build version and swift version would also be useful.

@ma-pe
Copy link
Author

ma-pe commented Feb 20, 2018

XCode Version 9.2 (9C40b)
Swift 3.2

@ma-pe
Copy link
Author

ma-pe commented Feb 25, 2018

After studying your README again, I could resolve it:
I forgot to replace the token to token: .secureEnclaveIfAvailable.

Thanks for your support!

@ma-pe ma-pe closed this as completed Feb 25, 2018
@hfossli
Copy link
Contributor

hfossli commented Feb 25, 2018

Ah! That makes sense! Maybe a better error message could be nice in that case. Keeping it open until I fix that. Thanks!

@hfossli hfossli reopened this Feb 25, 2018
@Venkatasatishb
Copy link

why can I generate key pair from login screen?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants