PHP's openssl_sign() using M2Crypto?

K

KW

I'm trying to convert some PHP code using OpenSSL to Python and I'm stuck
on openssl_sign() which uses an RSA private key to compute a signature.

Example PHP code:
$privkeyid = openssl_get_privatekey($priv_key, $key_pass);
openssl_sign($data, $signature, $privkeyid);
openssl_free_key($privkeyid);

I've tried several permutations of the stuff in M2Crypto.EVP but I can't get
it to work...

The openssl module in PHP basicly does this (C code):
EVP_SignInit(&md_ctx, EVP_sha1());
EVP_SignUpdate(&md_ctx, data, data_len);
EVP_SignFinal(&md_ctx, sigbuf, &siglen, pkey);

Looks like some magic is used to get pkey, I think that's what I'm missing.
See php_openssl_evp_from_zval() in PHP's ext/openssl/openssl.c.

I've tried the following:
key = M2Crypto.EVP.load_key(keyfile, lambda x: passphr)
hmac = M2Crypto.EVP.HMAC(key, 'sha1')
hmac.update(message)
hmac.final()

But this results in:
File "/usr/lib/python2.4/site-packages/M2Crypto/EVP.py", line 39, in __init__
m2.hmac_init(self.ctx, key, self.md)
TypeError: expected a readable buffer object
Segmentation fault

Unfortunately M2Crypto documentation is practically nonexistent..

Best regards,
 
K

KW

I'm trying to convert some PHP code using OpenSSL to Python and I'm stuck
on openssl_sign() which uses an RSA private key to compute a signature.

I think basicly my question is: how do I extract the key from a private
key in M2Crypto?

Best regards,
 
H

heikki

KW said:
The openssl module in PHP basicly does this (C code):
EVP_SignInit(&md_ctx, EVP_sha1());
EVP_SignUpdate(&md_ctx, data, data_len);
EVP_SignFinal(&md_ctx, sigbuf, &siglen, pkey);

Looks like some magic is used to get pkey, I think that's what I'm missing.
See php_openssl_evp_from_zval() in PHP's ext/openssl/openssl.c.

I've tried the following:
key = M2Crypto.EVP.load_key(keyfile, lambda x: passphr)
hmac = M2Crypto.EVP.HMAC(key, 'sha1')
hmac.update(message)
hmac.final()

Does this work?:

key = M2Crypto.EVP.load_key(keyfile, lambda x: passphr)
key.sign_init()
key.sign_update(message)
signature = key.final()
Unfortunately M2Crypto documentation is practically nonexistent..

A lot of the OpenSSL documentation works fine, the names are usually
straight mapping.
 
K

KW

Does this work?:

key = M2Crypto.EVP.load_key(keyfile, lambda x: passphr)
key.sign_init()
key.sign_update(message)
signature = key.final()

No, I get this:
AttributeError: PKey instance has no attribute 'sign_init'

Best regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top