Creating a rsa public key from its modulus and exponent

opensslrsa

I want to verify a RSA signature. I have data to verify, the signature and a public key in a form of modulus and exponent. I'd like to do the verification using openssl. Is it possible? I know I can use openssl rsautl -verify -in sig -inkey key.pem but I don't know how (using openssl) to create a public key having just it's modulus and exponent.

Maybe other ideas how to check this signature (except writing some programs)?

Best Answer

In order to generate a RSA public key in PEM format to be used with openssl, you can follow these steps.

Create an ASN1 definition file

Modify the following template to include your modulus and exponent

# Start with a SEQUENCE
asn1=SEQUENCE:pubkeyinfo

# pubkeyinfo contains an algorithm identifier and the public key wrapped
# in a BIT STRING
[pubkeyinfo]
algorithm=SEQUENCE:rsa_alg
pubkey=BITWRAP,SEQUENCE:rsapubkey

# algorithm ID for RSA is just an OID and a NULL
[rsa_alg]
algorithm=OID:rsaEncryption
parameter=NULL

# Actual public key: modulus and exponent
[rsapubkey]
n=INTEGER:0x%%MODULUS%%

e=INTEGER:0x%%EXPONENT%%

Instead of editing, you also may want to script this using sed

sed -i "s/%%MODULUS%%/$(xxd -ps -c 256 mymodulus.bin)/" def.asn1

Note the -c 256 should be chosen according to your key length in bytes.

You can use a similar command for the exponent.

Generate your RSA key

Use the following openssl command. This will give you a DER encoded RSA key.

openssl asn1parse -genconf def.asn1 -out pubkey.der -noout

Then convert it into a PEM key

openssl rsa -in pubkey.der -inform der -pubin -out pubkey.pem

Verify using your key

You can use either openssl dgst -verify or openssl rsautl -verify