1

I'm trying to go through the basic exercise of signing and verifying a signature using OpenSSL. Signing works fine

$ openssl pkeyutl -hexdump -sign -inkey id_rsa -in test.txt > test.sig
Enter pass phrase for id_rsa:
$ cat test.sig
0000 - 20 ab 34 00 ff 87 50 1e-de fb c9 3d 10 2f 7b fd    .4...P....=./{.
0010 - 99 a1 61 e0 3d 5f 93 82-63 e9 0a 6f 1a 22 4f 04   ..a.=_..c..o."O.
etc...

However, when I try to verify that signature, I get an error.

$ openssl pkeyutl -verify -pubin -inkey id_rsa.pub.pem -signature test.sig test.txt
unable to load Public Key

The public key that I'm trying to use is a converted SSH public key in PEM format:

-----BEGIN RSA PUBLIC KEY-----
MIICCAKCAf8wxsGr3b/rR/BsRqRMBaKPEGATYhSNaqnThcx9YdA7jHcj1dK7Utmo
nXZz7WwB0W1vzADZpzX96UUX5OIn1iByZ3ZQZpkO6YApMYxo/oHyiBYiCyEhbqKR
TydzmXFGYJ6UtLBFLD1eKfJokhiqdRa1kVMdAd6nBT5JiNb0ADO3VisyPwQZpqyB
Izbq1643JT2PC1BYk1cqqxyTnY/kAEEQvcQwrd0y7p29slRHC6g77HToWJLzNdAj
b2rjkRIfMzDx0LwmCuEqh+9XfVYQFJyrqXTrcgQlKBQhTSneSnn/9SpqYs4ziOIR
4djO9BbhGb/09LSb31NFy6S99rXsElI2aSaaGz5y6/6wyYq+zMxmiS//iCV/heNv
i3PH+D2hxOdz+VEvAJ77x4cMOn3ovVW73aWoBsNP8c9ISKt6MSXClljskA1WFcYJ
9UuBicQ/FGL2j96lzQ2NZZrYru0gAOkj3WUF/z5ORGC7iRvgOc89oqUxKNG+N/RN
jrMbCBAN8hOlJCW0e7/MoangYtBtZ52ZSIxYACRRfKTUlKdjsE9UVYrqBtTeVDGp
Igb+0kuBD61XF6IpnHEX0OZeArL7FY1trDJ85lUU09qNbnlnxNAY2Qo3cZiXSRBW
ROBUxz0OzF2Gvj30BEiY1rshH+XtM5ViQo1nK+YkBQcrCbwfnCFBAgMBAAE=
-----END RSA PUBLIC KEY-----

generated with the command

ssh-keygen -f key.pub -e -m pem

Based on discussions and documentation that I've read about this problem, that should work. But it doesn't.

Any hints?


EDIT: I've just tried the same process with a pair of keys generated with OpenSSL rather than converted SSH keys. That merely fails silently rather than producing an error (openssl gives me pkeyutl usage information rather than verifying the signature).

EDIT 2: Using a binary signature (omitting -hexdump) has the same effect on keys generated with OpenSSL. On converted RSA keys, I get

unable to load Public Key
Error initializing context
[snip usage output]
4
  • What implementation/version of OpenSSL are you using? Jun 14, 2012 at 16:11
  • @notfed - Sorry, should have mentioned. openssl 1.0.1c-3, straight out of the Debian Wheezy repos.
    – Inaimathi
    Jun 14, 2012 at 16:16
  • 1
    It seems that you are outputting hexdump of the signature to a file and use that for verification. I doubt if openssl expects it read hexdump rather then the binary signature. Could you try removing the "-hexdump" option when generating the signature. Jun 14, 2012 at 17:42
  • @RaymondTau - Tried and added notes. Didn't solve the problem, I'm afraid.
    – Inaimathi
    Jun 14, 2012 at 17:56

2 Answers 2

3

You gave non-existing option -signature. Correct is -sigfile. The following works:

$ openssl pkeyutl -sign -inkey key.pem -in data.txt > test.sig
$ openssl pkeyutl -verify -pubin -inkey pubkey.pem -sigfile test.sig -in data.txt
Signature Verified Successfully
0

The -hexdump option is I guess being used because you dont want to get a binary file out? You can use base64 to encode the binary sig as ascii:

openssl pkeyutl -sign -inkey id_rsa -in test.txt | base64 > test.sig

However, when you verify this, you're going to have to convert it back into binary, in some temporary file:

cat test.sig | base64 -d > ~test.sig.bin

Your public keyfile is in 'rsa public key format', you can see in the header line 'BEGIN RSA PUBLIC KEY'. You can convert this into non-rsa public key format, which will have header 'BEGIN PUBLIC KEY':

openssl rsa -in id_rsa.pub.pem -RSAPublicKey > id.pub.pem

Finally, using the 'PUBLIC KEY' pem, and the binary sigfile, you can verify:

openssl pkeyutl -verify -pubin -inkey id.pub.pem -sigfile ~test.sig.bin -in test.txt

Output:

Signature Verified Successfully

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.