Fetchez la vache
Fetchez la vache

Reputation: 5230

.pem files and .p12 file and URL's - oh my

I'm having real issues attempting to contact a secure rest URL. Basically I have little knowledge of certificates, and am wondering whether what I am doing with the certificate file(s) is correct.

  1. I have 2 URL's effectively one for read and one for write.
  2. I have 2 .pem files, one for each, that were provided with the URLs. The act of importing these into Certificate manager didn't change the outcome below).
  3. I have also a .p12 file, which if I'm honest don't know where it fits in to all this... (I cannot import this into my Win7 cert magager as I do not have a password)

When running req.GetResponse() an exception occurs:

"The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."
Inner Ex: "The remote certificate is invalid according to the validation procedure."

I'm currently unsure as to whether things need a little tinkering or I'm simply way off. code is basically...

if (!File.Exists(certificateLocation)) 
{
    throw new Exception(string.Format("The specified certificate file does not exist: {0}", certificateLocation));
}

//Cert Challenge URL 
Uri requestURI = new Uri(url);

//Create the Request Object
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(requestURI);

//Set the Request Object parameters
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
req.AllowAutoRedirect = false;

//Create certificate from our file
X509Certificate cert = X509Certificate.CreateFromCertFile(certificateLocation);
req.ClientCertificates.Add(cert);

WebResponse response = req.GetResponse(); // *** Errors here

...

Edit: Currently I'm only attempting the "read" url - seemed logical.

After contacting the issuer for the password accompanying the .p12 certificate, and importing that into the "Trusted Root Certification Authorities section" of certification manager, the error has now changed to the following...

System.Net.WebException
"The underlying connection was closed: An unexpected error occurred on a receive."
Inner Ex:
"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."

Upvotes: 1

Views: 789

Answers (1)

Robert
Robert

Reputation: 5302

-- re-posting comment as answer --

The password is required for this to work.

It is created at the same time as the .p12 file.

Upvotes: 1

Related Questions