12

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?

cnst
  • 1,884
  • 2
  • 19
  • 30
  • 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? http://stackoverflow.com/questions/25044418/can-dtrace-find-non-extractable-private-key-from-keychain-on-os-x – cnst Jul 30 '14 at 18:44

3 Answers3

6

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
fel1x
  • 389
  • 1
  • 5
  • 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 https://github.com/torsten/keychain_access as per http://apple.stackexchange.com/questions/140890/non-extractable-private-key-in-keychain-access-app, but it's not exporting the key, either. – cnst Aug 04 '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 04 '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: http://stackoverflow.com/questions/25152504/include-files-not-found-in-apple-open-source-software, including, apart from the fixable include errors, `ld: library not found for -lASN1`. – cnst Aug 06 '14 at 06:27
  • Have you tried https://github.com/juuso/keychaindump? – fel1x Aug 17 '14 at 18:26
  • https://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
5

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 pyOpenSSL

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

cd chainbreaker

python2 chainbreaker.py -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
MikeSchem
  • 2,266
  • 1
  • 13
  • 33
Nikita
  • 151
  • 1
  • 3
1

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.

cnst
  • 1,884
  • 2
  • 19
  • 30