How to download password protected private key from .pem file using M2Crypto?

I have a private key with a password in a .pem file; I want to use it to sign requests to a remote server. I can download the key and enter the passphrase after asking for it:

python
>>> import M2Crypto
>>> pk = M2Crypto.RSA.load_key('private.pem')
Enter passphrase:
>>>

However, I need this for a server process that restarts every morning, and thus the passphrase should be passed automatically. The load_key method supports a callback argument for this purpose, so I tried several options:

>>> def gimmepw():
...     return 'mysecret'
...
>>> pk = M2Crypto.RSA.load_key('private.pem', gimmepw)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/local/Plone/Python-2.4/.../M2Crypto/RSA.py", line 351, in load_key
    return load_key_bio(bio, callback)
  File "/usr/local/Plone/Python-2.4/.../M2Crypto/RSA.py", line 372, in load_key_bio
    rsa_error()
  File "/usr/local/Plone/Python-2.4/.../M2Crypto/RSA.py", line 302, in rsa_error
    raise RSAError, m2.err_reason_error_string(m2.err_get_error())
M2Crypto.RSA.RSAError: bad password read
>>>

(replace "..." with "lib / python2.4 / site-packages")

What am I doing wrong?

+3
source share
2 answers

. , TypeError ( M2Crypto).

>>> def gimmepw(*args):
...     print 'args:', repr(args)
...     return 'test'
... 
>>> M2Crypto.RSA.load_key('key.pem', gimmepw)
args: (0,)
<M2Crypto.RSA.RSA instance at 0xb6e8050c>

:

def gimmepw(*args):
    return 'mysecret'
+6

: Python 2.7 callback str.

, unicode .

>>> def gimmepw(*args):
...     return u'test'
... 
>>> M2Crypto.RSA.load_key('key.pem', gimmepw)
Traceback (most recent call last):
  File "test_intuit_data.py", line 76, in <module>
    intuit_rsa_key = RSA.load_key(file='key.pem', callback=gimmepw)
  File "lib/python2.7/site-packages/M2Crypto/RSA.py", line 351, in load_key
    return load_key_bio(bio, callback)
  File "lib/python2.7/site-packages/M2Crypto/RSA.py", line 372, in load_key_bio
    rsa_error()
  File "lib/python2.7/site-packages/M2Crypto/RSA.py", line 302, in rsa_error
    raise RSAError, m2.err_reason_error_string(m2.err_get_error())
M2Crypto.RSA.RSAError: bad password read

- , str, , str:

>>> def gimmepw(*args):
...     return str(u'test')
... 
>>> M2Crypto.RSA.load_key('key.pem', gimmepw)
<M2Crypto.RSA.RSA instance at 0xb6e8050c>
0

All Articles