7

I have a PKI certificate in Keychain Access.app on OS X 10.9, together with the private key.

The manual page for /usr/bin/security indicates that there is a -x option for security import to specify that private keys are non-extractable after being imported, which I believe must have been used on my key.

However, there's no magic storage with copy protection, and if the certificate is still usable in the system somehow, then surely it is still in there somewhere, stored in its full glory with all the relevant bits, and surely there must be a way to still export it. How?

  • Perhaps it's possible to do something like this with DTrace? E.g. when the private key must be used by an application (be that VPN or web-browser) in order to perform user authentication with the remote service? stackoverflow.com/questions/25044418/… – cnst Jul 30 '14 at 18:44
4
+25

Apparently, the open source Security-framework has a check whether the key attributes are set to non-exportable. Overwriting these attributes would work. You can also try running your VPN/browser with a debugger and break on a function which uses the key. From there you can get a pointer to memory and extract the private key.

lldb -- security export -k test.keychain -o asdf -w
break impExpWrappedKeyOpenSslExport
  • 1
    Thanks for the pointers! Do you mind going into a bit more detail of how to actually end up with a private key, preferably in p12 format? Can I just use some kind of API directly to get the key, ignoring the attributes? I've tried using github.com/torsten/keychain_access as per apple.stackexchange.com/questions/140890/…, but it's not exporting the key, either. – cnst Aug 4 '14 at 19:04
  • 1
    Do I have to recompile the security library in order to get a hold of this? I'm being told that the breakpoint is invalid (perhaps the symbols are missing?). – cnst Aug 4 '14 at 19:13
  • 1
    Also, how did you actually manage to compile it? It doesn't seem like break is working with the stock version, and I'm getting various errors when trying to compile SecurityTool from source: stackoverflow.com/questions/25152504/…, including, apart from the fixable include errors, ld: library not found for -lASN1. – cnst Aug 6 '14 at 6:27
  • Have you tried github.com/juuso/keychaindump? – fel1x Aug 17 '14 at 18:26
  • github.com/juuso/keychaindump does look very interesting; however, from my reading of it, it looks like it only supports regular passwords, without any code for certificates or private keys. – cnst Aug 21 '14 at 16:39
2

I was able to extract non-extractable private key from macOS High Sierra 10.13.6 using this tool chainbreaker. It requires Python2 with hexdump and pycrypto packages. This README file in the tool's repo explains how to do that README-keydump.txt.

In general:

sudo pip2 install hexdump pycrypto

git clone https://github.com/n0fate/chainbreaker

cd chainbreaker

python2 chainbreaker -f ~/Library/Keychains/login.keychain-db -p 'YOUR_KEYCHAIN_PASS'

# After the command has finished running, there will be a new folder
# exported with 3 more folders for certs, keys and key-cert pairs
ls exported/associated

# This will show you folders with found key-certificate pairs as numbered
# subfolders and separate files there. Those files are in DER format, so,
# for example, to convert those to PEM:
openssl x509 -inform DER -in exported/associated/1/3.crt -out exported/associated/1/cert.pem
openssl rsa -inform DER -in exported/associated/1/5.key -out exported/associated/1/key.pem
0

As per https://reverseengineering.stackexchange.com/questions/6043/extract-non-extractable-private-key-from-os-x-keychain, it appears that OS X 10.5 does not support kSecAttrIsExtractable dictionary key and the CSSM_KEYATTR_EXTRACTABLE bit.

As such, it appears possible to copy login.keychain from OS X 10.9 to 10.5, and perform the p12 export.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.