Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VerifyingKey from PublicKey PEM fails #240

Closed
justindthomas opened this issue Dec 26, 2022 · 2 comments
Closed

VerifyingKey from PublicKey PEM fails #240

justindthomas opened this issue Dec 26, 2022 · 2 comments

Comments

@justindthomas
Copy link

justindthomas commented Dec 26, 2022

This may totally be ignorance on my part, but I cannot figure out how to verify a pkcs1v15 signature using a PublicKey that has been instantiated from a PEM string.

Verification starting with this sequence works fine.

let private_key = RsaPrivateKey::from_pkcs8_pem(&private_key_pem).unwrap();
let signing_key = SigningKey::<Sha256>::new_with_prefix(private_key.clone());

let verifying_key: VerifyingKey<_> = (&signing_key).into();

Starting with this sequence fails.

let private_key = RsaPrivateKey::from_pkcs8_pem(&private_key_pem).unwrap();
let public_key = RsaPublicKey::from(&private_key);

let verifying_key: VerifyingKey<Sha256> = public_key.into();

Note that I've simplified the above to remove the PEM conversion because I found that even just going straight to the RsaPublicKey from the RsaPrivateKey directly also fails and I figured I'd just eliminate the possibility that my PEM file was corrupt in some way. I also tried using VerifyingKey::from(public_key). That also fails.

What am I doing wrong?

@tarcieri
Copy link
Member

Offhand your problem seems unrelated to PEM.

I think the issue is you're doing SigningKey::new_with_prefix but not also doing VerifyingKey::new_with_prefix.

This will cause a mismatch between the digest used for signing and the one used for verification.

FWIW we understand these APIs are somewhat confusing and have #238 open as a tracking issue for improvements, ideally getting rid of the new vs new_with_prefix distinction and making the selection automatic.

@justindthomas
Copy link
Author

Oh wow, that solved all my problems. Thanks! I saw those threads, but I think I didn't look at anything that started with new because I was stuck on figuring out how to convert from either the SigningKey or the PublicKey (per the example documentation). So into and from seemed natural, but new not so much.

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants