views:

301

answers:

2

I have pub key in xml format:

<RSAKeyValue><Modulus>xF9y25EXh8n99sXtU/JAsYTwML6PB7gSCE8tWw8Www2KBfDqohQBL8FMs8jzsDQa7WwoEmiVJ1resEC9YXJGbwQyWgb9qgooC9oSnCB/TkRdBybwby0DKuZOzq+609OBGkwWpgnS4QVCBc6eW+10l3qE3/2hKdcSV+08iRYp7zs=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>

So i try thms like this:

from M2Crypto import RSA
from xml.dom.minidom import parseString
import base64

dom = parseString(pubKey)
e = base64.b64decode(dom.getElementsByTagName('Exponent')[0].childNodes[0].data)
n = base64.b64decode(dom.getElementsByTagName('Modulus')[0].childNodes[0].data)
rsa = RSA.new_pub_key((e, n))

Got error:

    ...
    rsa = RSA.new_pub_key((e, n))
  File "/usr/lib/pymodules/python2.6/M2Crypto/RSA.py", line 390, in new_pub_key
    m2.rsa_set_e(rsa, e)
M2Crypto.RSA.RSAError: invalid length

Any ideas?

A: 

The RSA.new_pub_key documentation states that e and n need to be in OpenSSL MPINT format (4-byte big-endian bit-count followed by the appropriate number of bits). It seems like at least your e is not in that format. If you take a look at test_rsa.py, you can see comments that say:

'\000\000\000\003\001\000\001' # aka 65537 aka 0xf4

It seems your e is just '\001\000\001'. If we prepend the '\000\000\000\003' to it, your sample app gets a bit further along, but then fails trying to set n. I haven't looked into how to create valid OpenSSL MPINT values, so this isn't a complete answer to your question.

Heikki Toivonen
A: 

This binary format is unknown for me too. But manufactures gave me converted key in p12 format. After i convert in into pem using

openssl pkcs12 -in ./public_key.p12

it looks like

Bag Attributes: <Empty Attributes>
subject=/CN=db725fc41791c987fdf6c5ed53f240b1
issuer=/CN=db725fc41791c987fdf6c5ed53f240b1
-----BEGIN CERTIFICATE-----
MIIBUTCCAT6gAwIBAgIQ0jw+Mno+07BCwv6ZfVjAVDAJBgUrDgMCHQUAMCsxKTAn
BgNVBAMTIGRiNzI1ZmM0MTc5MWM5ODdmZGY2YzVlZDUzZjI0MGIxMB4XDTA5MTIx
NTA2NDcyNloXDTEwMTIxNTEyNDcyNlowKzEpMCcGA1UEAxMgZGI3MjVmYzQxNzkx
Yzk4N2ZkZjZjNWVkNTNmMjQwYjEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGB
AJoeWWjFudEXDaHsjGFJ8TgcqL8zbariBfLVuAwn/jkXnQKXVl+miu10aWeYJrJq
3VIibefB8n3NQpTJQaPRTYEE1z8J9qi3xIu8vSgLvac6klz7RO9aUl5tR57JSPao
xQE+aey9hpjEZQvw78dAj3xAoKS07DNFKdXxs89WkXafAgMBAAEwCQYFKw4DAh0F
AAMCAEI=
-----END CERTIFICATE-----

Where is a PUBLIC KEY block?

RSA.load_pub_key('public_key.pem')

produce error

M2Crypto.RSA.RSAError: no start line
baz