Loading passphrase protected key fails
Trying to load a passphrase protected key fails in Python3 (Ubuntu 18.04, Python 3.6.8, M2Crypto 0.35.2). Probably a confusion of bytes and str.
from M2Crypto import SMIME
smime = SMIME()
smime.load_key('key.pem', 'cert.pem')
Enter passphrase:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.6/dist-packages/M2Crypto/SMIME.py", line 163, in load_key
self.pkey = EVP.load_key(keyfile, callback)
File "/usr/local/lib/python3.6/dist-packages/M2Crypto/EVP.py", line 395, in load_key
cptr = m2.pkey_read_pem(bio.bio, callback)
M2Crypto.EVP.EVPError: Unable to read private key in function pkey_read_pem.
But the following works:
from M2Crypto import SMIME
from getpass import getpass
smime = SMIME()
smime.load_key('key.pem', 'cert.pem', callback=lambda x: bytes(getpass(), 'ascii'))