Closed bshaffer closed 3 years ago
The behavior you're seeing is within OpenSSL itself, not pyOpenSSL. To debug this effectively you'll need to rewrite this in C (which it mostly is already!), and if you're still seeing the problem seek help from the OpenSSL user community. If, for whatever reason, you don't see this when translating into pure C then please reopen this issue so we can dig deeper!
Hello! Thank you for taking the time to read this!
Overview
We are trying to use
OpenSSL._util.lib.SSL_CTX_use_PrivateKey
with an EC key, and are getting the following exception:The strange thing is:
SSL_CTX_use_PrivateKey_file
instead ofSSL_CTX_use_PrivateKey
.These two things have lead us to believe this is a bug and not user error.
We are the a
pkcs11
moduleSoftHSM
, but this should be repeatable using anypkcs11
module. We have tested on both Linux and Mac, but not with any other pkcs11 module.Steps to reproduce
Generate an EC key and certificate:
Store the key and cert as
eckey
into your pkcs11 module:Execute the following code. You will get a "key values mismatch" exception:
cert_path="./ec-cert.pem" key_id = b"pkcs11:token=token1;object=eckey;pin-value=realgoodpin" print("============ testing using key from hsm ================")
_lib.ENGINE_load_builtin_engines() e = _lib.ENGINE_by_id(b"dynamic") _lib.ENGINE_ctrl_cmd_string(e, b"ID", b"pkcs11", 0) _lib.ENGINE_ctrl_cmd_string(e, b"SO_PATH", b"/usr/lib/x86_64-linux-gnu/engines-1.1/libpkcs11.so", 0) _lib.ENGINE_ctrl_cmd_string(e, b"LOAD", _ffi.NULL, 0) _lib.ENGINE_ctrl_cmd_string(e, b"MODULE_PATH", b"/usr/local/lib/softhsm/libsofthsm2.so", 0) _lib.ENGINE_init(e)
context = OpenSSL.SSL.Context(6)
_lib.SSL_CTX_use_certificate_file(context._context, cert_path, 1) key = _lib.ENGINE_load_private_key(e, key_id, _ffi.NULL, _ffi.NULL) if not _lib.SSL_CTX_use_PrivateKey(context._context, key): _exception_from_error_queue(Exception) print("test_use_key_from_hsm succeeded")
generate RSA cert/key
openssl req -nodes -x509 -newkey rsa:4096 -keyout rsa-key.pem -out rsa-cert.pem -days 3650
pkcs11-tool --module /usr/local/lib/softhsm/libsofthsm2.so --pin realgoodpin \ --write-object rsa-key.pem --type privkey --id 2222 --label rsakey --slot-index 0 pkcs11-tool --module /usr/local/lib/softhsm/libsofthsm2.so --pin realgoodpin \ --write-object rsa-cert.pem --type cert --id 2222 --label rsakey --slot-index 0
If there's anything obvious we are doing wrong please let us know! Any help is appreciated! At this point it does seem like a bug. Thank you!