pythonrsatorstempycryptodome

Tor Stem: public key don't match one in hidden servce descriptor


I am trying to get the public key of a hidden service I created. There are 2 ways, a one from the private key I have, the other is accessing the hidden service descriptor and taking the public key field.

I wrote a python script to test both. But, they were different !

Here is the script (pkey is a test private key generated before from Tor controller)

import stem
from stem.control import Controller

from Cryptodome.PublicKey import RSA

from base64 import b64encode

controller = Controller.from_port(port=9051)

controller.authenticate()

pkey="MIICXQIBAAKBgQCsqrc5jvobvNiZ4g0IdoxkQmYtFtRqGMeGCfh8IZ1s7IJ2owzrZ+8GE+HJhBr1YYFJ/9UegD9APfnybIfgmVY+PCxNlE16uMreigogAqpN4Vp8aic0s/6sXbf9EyHkafcHdMX3N3LUYhOQZfI02zNjUTRSkHp9jS+MFcWwOeYMzwIDAQABAoGANX5uZZa2RgGr2YD/KLARrr4jibSY+oXSyuQr58lCTIy5nqfFSOlaVDe0rAoRkBQ1SgfIrzpsXirVVRXqwtq+rOcRjVT4cRZnb3MNRqSFiqSocL2/TkJg97pXKfGgLkoopsGfvrgXmlpI03w8TMwbcSHvObUkC9xKqcKABtdYfQECQQDSQgyvh7YnQ2qdYeFYc9f9kT3XaUjYMgel7Zq8GAQEmsZ3YqW/vDmNVYsnQj/ZHyS/VwKFyE66jw/po+p8bBKZAkEA0jsX3AZyrd24lVkK/5gcMQLfRuOyJDtEjNXUNWxS2axUoVDBIRcArsa5IKG4yRYYRgIVSEDLxGxciTWgU5MjpwJAShTRiWskp/Ld9rPNBTBT0tjA1EwZSWtfPHxaP5mqmMC72sacJdE46e5RCDUUb/jEJtKOZfqsmpjbf3Oso8gkSQJBAJkBrUHT548nOfpojhqsSIhbQMnFWpgIEsI1tncTWdFXHVky/vzoUvThaUeYrHaeABij1314u1VAULEEOKXbBy0CQQDJJeDolidHG8UkWISq2jP6QIkpqOx3E6zxMNXAKwfz/O+yyanNtL1+MpFbk0H5n1fUhSDjR5daziUcmU93NOW9"


response = controller.create_ephemeral_hidden_service([9000, 9001],key_type ='RSA1024',key_content =pkey,await_publication=True)
onionAddr = response.service_id
#privKey  = pkey#response.private_key

print('onion: '+onionAddr)
print("\nPrivate Key: ")
print(pkey)
print("================")

pkey ="-----BEGIN RSA PRIVATE KEY-----\n"+pkey+"\n-----END RSA PRIVATE KEY-----"
privKeyObj = RSA.importKey(pkey)

#pubKey = b64encode(privKeyObj.publickey().exportKey('DER',pkcs=8))
pubKey = privKeyObj.publickey().exportKey('PEM',pkcs=1).decode()
print(pubKey)

print("\n\n\n HSD response:")
print(controller.get_hidden_service_descriptor(onionAddr+".onion"))

The public key I get from the private one is :

-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCsqrc5jvobvNiZ4g0IdoxkQmYt
FtRqGMeGCfh8IZ1s7IJ2owzrZ+8GE+HJhBr1YYFJ/9UegD9APfnybIfgmVY+PCxN
lE16uMreigogAqpN4Vp8aic0s/6sXbf9EyHkafcHdMX3N3LUYhOQZfI02zNjUTRS
kHp9jS+MFcWwOeYMzwIDAQAB
-----END PUBLIC KEY-----

and the one from hidden service descriptor is:

-----BEGIN RSA PUBLIC KEY-----
MIGJAoGBAKyqtzmO+hu82JniDQh2jGRCZi0W1GoYx4YJ+HwhnWzsgnajDOtn7wYT
4cmEGvVhgUn/1R6AP0A9+fJsh+CZVj48LE2UTXq4yt6KCiACqk3hWnxqJzSz/qxd
t/0TIeRp9wd0xfc3ctRiE5Bl8jTbM2NRNFKQen2NL4wVxbA55gzPAgMBAAE=
-----END RSA PUBLIC KEY-----

Doing some search lead my to the different key formats for RSA. I tried to make the formats match but I had no luck !

How to solve this ?


Solution

  • I was wrong, they was the same from the beginning but just different versions of encoding. PyCryptoDome can handle this difference in encoding. if both keys imported into key objects and compared, they are equal