MacOS command line tool to interact with secure enclave

We are trying to write a command line tool that will setup a VPN tunnel following a customized protocol.

One step is making sure that the client is starting the connection from a company MacBook by responding to a challenge by signing it with a private key stored in the secure enclave. For the communication with the secure enclave we want to use the EllipticCurveKeyPair project.


When we first ran our tool and tried to access the secure enclave, we got an error thrown from the EllipticCurveKeyPair manager:


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

which refers to the error

errSecMissingEntitlement-34018Internal error when a required entitlement isn't present.

So, we figured that entitlements are missing to access the secure enclave.


To resolve this, we tried to apply the settings of the EllipticCurveKeyPair MacOS demo project to our MacOS command line tool project. So, we added the capability App Sandbox and configured the entitlements keychain-access-groups, com.apple.security.get-task-allow, com.apple.security.files.user-selected.read-only, as in

?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.application-identifier</key>
<string>####</string>
<key>com.apple.developer.game-center</key>
<true/>
<key>com.apple.developer.team-identifier</key>
<string>####</string>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
<key>com.apple.security.get-task-allow</key>
<true/>
<key>keychain-access-groups</key>
<array>
<string>####</string>
</array>
</dict>
</plist>

With the sandbox configured we run into the next problems:

When we execute the tool in Xcode, Xcode will halt execution, and display an error message in the debugger, some place in the code where the sandbox appears to be initialized:

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

We made sure that our tool uses a bundleID on its own to avoid sandbox initialization issues arising from multiple apps using the same bundle ID.

When we execute the tool from the command line, we get the error

Illegal instruction: 4


When using codesign -d --entitlements - CmdLineTool we get this list of entitlements


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
<key>com.apple.security.get-task-allow</key>
<true/>
</dict>
</plist>


Also, we read through the articles available here:


Now the question:

  • How do we have to proceed to get access to the secure enclave from a command line tool to
    • create key pairs
    • use private keys to sign / encrypt strings
  • Is it necessary or even possible to configure App Sandbox for the command line tool?

Thanks for any help!

Replies

I'm having the same problem, did you figure this out?

Did you solve this? It is hard to find out if this is possible.

To interact with keys protected by the SE you must use the iOS-style keychain, and that requires an entitlement that’s authorised by a provisioning profile. A tool has nowhere to store a provisioning profile and thus Xcode doesn’t do the right thing out of the box.

The solution is to embed your tool in an app-like structure. See my Packaging a Daemon with a Provisioning Profile post for an example of this.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

  • Thank you! I just tested and it works for me!

  • Yay!

Add a Comment