I know this is an older post, but I wanted to answer the direct question of - how to pass an alias to pyopenssl to get a private key. I still think that the other answer is a viable solution, but maybe a bit more context would be helpful for some.
The short answer, unfortunately, is that you can't specify the alias for a specific key in openssl
with python. PyOpenSSL
uses cryptography
underneath to load a pkcs12
file
def load_pkcs12(buffer, passphrase=None):
"""
Load pkcs12 data from the string *buffer*. If the pkcs12 structure is
encrypted, a *passphrase* must be included. The MAC is always
checked and thus required.
See also the man page for the C function :py:func:`PKCS12_parse`.
:param buffer: The buffer the certificate is stored in
:param passphrase: (Optional) The password to decrypt the PKCS12 lump
:returns: The PKCS12 object
"""
... (omitting a lot of code) ...
if cert[0] == _ffi.NULL:
pycert = None
friendlyname = None
else:
pycert = X509._from_raw_x509_ptr(cert[0])
friendlyname_length = _ffi.new("int*")
friendlyname_buffer = _lib.X509_alias_get0(
cert[0], friendlyname_length
)
friendlyname = _ffi.buffer(
frien
dlyname_buffer, friendlyname_length[0]
)[:]
if friendlyname_buffer == _ffi.NULL:
friendlyname = None
... (more code to omit) ...
the lib.X509_alias_get0(X509*, int*)
function comes from the cryptography
library (which actually is a C
function from _openssl.lib
as far as I can tell. Here is its usage in the openssl code base and definition here).
In any case, it doesn't look like the API of pyOpenSSL
would allow to specify an alias. There is nothing in their documentation to suggest that, nor in their issue tracker.
But what about using cryptography
directly. PyOpenSSL does warn us that it's a good idea to use the direct api in any case...
<stdin>:1: DeprecationWarning: PKCS#12 support in pyOpenSSL is deprecated. You should use the APIs in cryptography.
Sure, we can re-do the steps that pyOpenSSL does and load our .p12
file, but oh no...
The cryptography documentations has a warning for us
cryptography only supports a single private key and associated certificates when parsing PKCS12 files at this time.
And in fact, it does seem we can't currently load different keys with different aliases through these libraries.
Why tho?
I can only assume that the reason this is done is because most .p12
users have only a single certificate and a single private key that match against one alias in their file. That is, they don't use the functionality of this specific standard to hold multiple pairs.
I'm judging this based on the wiki
But in practice it is normally used to store just one private key and its associated certificate chain.
PKCS #12 files are usually created using OpenSSL, which only supports a single private key from the command line interface.
However - if you only have one private key and certificates and you still want to make sure that the alias for those are correct, pyOpenSSL
does have a method get_friendlyname()
, which returns the alias of the loaded file. It's not really that helpful in your specific case (OP), but it can be done as an additional check.