Based on an older post, I am using Azure Key Vault to sign something with an EC Key created in Azure Key Vault. In the C# snippet everything works well and both signatures (IEEE P1363 and RFC 3279 Der Sequence) can be verified successfully.
If I were to verify the signature with OpenSSL, the verification fails.
I have downloaded the Public Key from Azure using this command, just to make sure I have the proper Public Key in PEM format (I’ve saved it as pubkey.pem). For testing purposes I have saved the signature resulted in my C# script in a file called sig.txt. I made sure the digest is the same in both C# and OpenSSL. I made sure the Public key is correctly formatted.
Here is a small snippet in C# where I hard-codded the signature and the public key.
using System.Security.Cryptography;
string b64 = "SGVsbG9Xb3JsZA==";
byte[] b64_bytes = Convert.FromBase64String(b64);
byte[] digest = SHA256.Create().ComputeHash(b64_bytes);
string b64_hex = BitConverter.ToString(digest).Replace("-", ""); // compare the result
string signature = "base64_string";
string publicKey = "base64_string";
byte[] signatureBytes = Convert.FromBase64String(signature);
byte[] publicKeyBytes = Convert.FromBase64String(publicKey);
using (ECDsa ecdsa = ECDsa.Create())
{
ecdsa.ImportSubjectPublicKeyInfo(publicKeyBytes, out _);
bool isValid = ecdsa.VerifyHash(digest, signatureBytes, DSASignatureFormat.Rfc3279DerSequence);
Console.WriteLine(isValid ? "Signature is valid." : "Signature is invalid.");
}
Now my commands in OpenSSL
openssl base64 -d -in b64.txt -out b64.der
openssl dgst -sha256 b64.der # compare the result
openssl dgst -sha256 -verify pubkey.pem -signature sig.txt b64.der
Error verifying data
Now, I’ve tried to see if the Public Key is correct by running:
openssl ec -pubin -in pubkey.pem -text -noout
And it returns the key details and they seem okay.
Then I’ve tired to see if the signature is valid by running these commands:
openssl base64 -d -in sig.txt -out sig.der
openssl asn1parse -in sig.der -inform DER
openssl dgst -sha256 -verify pubkey.pem -signature sig.der b64.der
Error verifying data
Is there a problem with the signature, or I am not verifying the signature correctly?
7