12

I'm trying to extract a non-extractable private key as a .p12 file from Keychain Access.app or /usr/bin/security on Mac OS X 10.9.4.


Basically, it appears that security import has a -x option to specify that private keys are non-extractable after being imported.

As per above, this is implemented by explicitly omitting CSSM_KEYATTR_EXTRACTABLE during the import. Subsequently, when trying to export, the key without such an attribute cannot be exported.

I could find at least two relevant mentions which appear to prevent such keys from being exported (hint: use fgrep -OR to avoid following the symbolic links when searching through Apple source code):


I've tried keychain_access from github.com/torsten, but it didn't work.

I've tried using lldb, lldb -- security export -t identities -f pkcs12 -o /tmp/mycerts.p12, to break on SecWrappedKeys.cpp::impExpWrappedKeyOpenSslExport(), but that doesn't seem to work, either, with break impExpWrappedKeyOpenSslExport giving out an error invalid command 'breakpoint impExpWrappedKeyOpenSslExport'.

I've tried DTrace to trace Cisco AnyConnect, but I'm not familiar enough with DTrace for it to reveal the datastructures.

I've tried re-compiling the Security and SecurityTool packages, but they all appear to require bootstrapping that does not appear to be in place.

How do I get the private key?

2 Answers 2

6

I've arrived at a solution by researching an answer for one of my Keychain subquestions.

According to SecItem.h, this kSecAttrIsExtractable has been introduced with OS X 10.6.

As such, I've tried and succeeded in the following:

  • scp ~/Library/Keychains/login.keychain from OS X 10.9 to OS X 10.5

  • On OS X 10.5, as per the example from the manual page:

    security export -k /tmp/login.keychain -t identities -f pkcs12 -o /tmp/mycerts.p12

    The security app will ask you for several passwords:

    1. the new password from the first dialogue is to protect the p12 that's being exported;
    2. the second one is the login.keychain password, which normally would be the same as the login password of the user on the original OS X 10.9 from where the keychain file came from.
  • scp /tmp/mycerts.p12 back to OS X 10.9 from 10.5

4

I found an easier way with chainbreaker fork. It works with MacOS 10.14 Mojave. See full instructions here.

In my case, I did the following:

$ pip2 install hexdump pycrypto pyopenssl
$ git clone https://github.com/gremwell/chainbreaker.git && cd chainbreaker
$ python2 chainbreaker.py -f ~/Library/Keychains/login.keychain-db -p <PASS>
$ openssl x509 -inform DER -in exported/associated/1/*.crt -out secret.crt
$ openssl rsa -inform DER -in exported/associated/1/*.key -out secret.key
$ openssl pkcs12 -export -out secret.p12 -inkey secret.key -in secret.crt

Then secret.p12 is ready to be imported on another machine.

1
  • You saved my life, respect Commented Sep 1, 2021 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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