This code relates to DKIM signature verification used in anti-spam efforts.
I have a byte[]
from s1024._domainkey.yahoo.com
that is ASN.1 encoded, but I don't know if that alone contains enough information to materialize a public key.
Based on this class, it appears I can convert an ASN.1 key into a X509Certificate Public key, but I need to supply an OID and some ASN.1-encoded parameters.
In this example I have metadata that the ASN1 key is:
- An RSA encoded key (ASN.1 DER-encoded [ITU-X660-1997] RSAPublicKey per RFC3447)
- Used with either sha1 sha256 hash algorithms
- Uses an OID relating to the following table from section A.2 of RFC3447 (though I don't know how to turn this information into a full OID)
/*
* 1.2.840.113549.1
*
MD2 md2WithRSAEncryption ::= {pkcs-1 2}
MD5 md5WithRSAEncryption ::= {pkcs-1 4}
SHA-1 sha1WithRSAEncryption ::= {pkcs-1 5}
SHA-256 sha256WithRSAEncryption ::= {pkcs-1 11}
SHA-384 sha384WithRSAEncryption ::= {pkcs-1 12}
SHA-512 sha512WithRSAEncryption ::= {pkcs-1 13}
*/
Code sample
string pubkey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDrEee0Ri4Juz+QfiWYui/E9UGSXau/2P8LjnTD8V4Unn+2FAZVGE3kL23bzeoULYv4PeleB3gfmJiDJOKU3Ns5L4KJAUUHjFwDebt0NP+sBK0VKeTATL2Yr/S3bT/xhy+1xtj4RkdV7fVxTn56Lb4udUnwuxK4V5b5PdOKj/+XcwIDAQAB";
byte[] pubkeyByteArray = Convert.FromBase64String(pubkey);
AsnEncodedData aData = new AsnEncodedData(pubkeyByteArray);
// OID must not be null, but it is here. What is it?
System.Security.Cryptography.X509Certificates.PublicKey pubKeyRdr = new System.Security.Cryptography.X509Certificates.PublicKey(aData.Oid, null, aData);
Question
- What OID should I use?
- What are examples of ASN.1 parameters?
AsnEncodedData
actually parses the data. You'll have to set the oid through,aData.Oid = new Oid("1.2.840.113549.1.1")
. After parsing the DER-TLV, you can use oid tool to convert the hex data to string representation, UNIVERSAL OID.1.2.840.113549.1.1.1. – SouseCspParameters
andRSACryptoServiceProvider
: Load ASN.1/DER encoded RSA keypair in C#. I point it out because so many Stack overflow answers tell you do things with certificates or use BouncyCastle when all you are doing is trying to load a key. Also note... the pain point is due to .Net and their use of XML encoding from RFC 3275. .Net does not accept ASN.1/DER or PEM encoded keys. – Spleenful