title: Generating key pair in iOS using SecKeyGeneratePair failed with errSecInteractionNotAllowed
body: <p>In my app I am using <code>SecKeyGeneratePair</code> to generate RSA key pair. After releasing the app, I started to notice occasional <code>errSecInteractionNotAllowed</code> errors (currently very rare) when using this function, so far only on iOS 10 devices. It is unclear to me why the key pair generation failed, or what I should do to fix that. Also, I could not find any documentation as to why key pair generation should fail with this error.</p>
  
  <p>This is the code I used to generate the key pair:</p>
  
  <pre><code>guard let access = SecAccessControlCreateWithFlags(nil,
                                                         kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly,
                                                         [],
                                                         nil) else {throw Error(description: "Failed to create access control")}
  
      let privateAttributes = [String(kSecAttrIsPermanent): true,
                               String(kSecAttrApplicationTag): keyTag + self.privateKeyExtension,
                               String(kSecAttrAccessControl): access] as [String : Any]
      let publicAttributes = [String(kSecAttrIsPermanent): true,
                              String(kSecAttrApplicationTag): keyTag + self.publicKeyExtension] as [String : Any]
  
      let pairAttributes = [String(kSecAttrKeyType): kSecAttrKeyTypeRSA,
                            String(kSecAttrKeySizeInBits): self.rsaKeySize,
                            String(kSecPublicKeyAttrs): publicAttributes,
                            String(kSecPrivateKeyAttrs): privateAttributes] as [String : Any]
      var pubKey, privKey: SecKey?
      let status = SecKeyGeneratePair(pairAttributes as CFDictionary, &amp;pubKey, &amp;privKey)
  </code></pre>
  
  <p>After this code, I am checking the status, and if it is not <code>errSecSuccess</code>, I am logging an error with the status returned from the function. This is where I noticed the <code>errSecInteractionNotAllowed</code> error.
  So, why does key pair generation or what I could do in order to fix it?</p>
  
  <p>Thanks,</p>
  
  <p>Omer</p>