I'm trying to implement authentication for MasterCard Match, as part of their following documentation, they have a sample private key:
https://developer.mastercard.com/portal/display/api/OAuth+Validation
On that page, they have two versions of the key, one in base64 encoded text, visible on the page, and a .p12 file downloadable.
How do I import this key to use as an x509certificate2?
Whatever I try I get the message "Cannot find the requested object.".
I tried digging into it with the .net source, but I get a dead end at an imported object
[SecurityCritical]
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern uint _QueryCertFileType(string fileName);
I've tried the following, and all of them fail with the same aforementioned message
new X509Certificate2(@"c:estmc-openapi-csr.pem")
new X509Certificate2(@"c:estmc-openapi-csr.pem", "mcapi")
new X509Certificate2(@"c:estmc-openapi-csr.pem", "mckp")
so I copied the text block into "copied.txt", and tried using that file, I've also tried reading the bytes in, and passing them in manually, I've also tried using
X509Certificate.CreateFromCertFile(fileName)
with both files.
Any ideas? Is the certificate bad? Am I using the wrong class? What does that error message mean?
--Update--
At Bad Zombie's suggestion, I tried BouncyCastle:
var pem = new Org.BouncyCastle.OpenSsl.PemReader(File.OpenText(fileName));
RsaPrivateCrtKeyParameters rsaParameters = (RsaPrivateCrtKeyParameters)pem.ReadObject();
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(new RSAParameters
{
DP = rsaParameters.DP.ToByteArray(),
DQ = rsaParameters.DQ.ToByteArray(),
Exponent = rsaParameters.Exponent.ToByteArray(),
InverseQ = rsaParameters.QInv.ToByteArray(),
Modulus = rsaParameters.Modulus.ToByteArray(),
P = rsaParameters.P.ToByteArray(),
Q = rsaParameters.Q.ToByteArray(),
});
}
on the "ImportParameters" call, I get "Bad Data". Am I doing something wrong?
See Question&Answers more detail:
os