Search code examples
wcfsslhttpwebrequestx509certificate2

Can't get two-way SSL to work with HttpWebRequest


I have a WCF service application that is supposed to connect to another server X to retrieve data. When server X is configured with one-way SSL, the WCF service application is able to connect to it without problem. But when server X is configured with two-way SSL, the WCF service application will have problem connecting to it.

The WCF service application uses a self-signed certificate. This certificate was exported without private key into a .cer file, and imported into server X's trust store. The same exported certificate was used in the WCF service application's codes:

string URL = "https://11.205.112.49:5311/Ping";
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(URL);
X509Certificate2 Certificate = new X509Certificate2("F:\\cert.cer");
Request.ClientCertificates.Add(Certificate);
Request.Method = "GET";
HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();

The last line of the codes above will throw WebException with the following message:

The request was aborted: Could not create SSL/TLS secure channel.

The above error does not provide much details.

Help is appreciate.

Thanks in advance.

EDIT:

This is my entire program:

using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.IO;

namespace TestTwoWaySSL
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpWebResponse Response = null;
            try
            {
                string URL = "https://11.205.112.49:5311/Ping";
                Console.WriteLine("URL: [{0}].", URL);
                HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(URL);
                X509Certificate2 Certificate = new X509Certificate2("F:\\cert.pfx", "password");
                Request.ClientCertificates.Add(Certificate);
                Request.Method = "GET";
                Console.WriteLine("Sending request.");
                Response = (HttpWebResponse)Request.GetResponse();
                Console.WriteLine("Request sent.");
            }
            catch (WebException webException)
            {
                Console.WriteLine("Web exception occurred: [{0}].", webException.Message);
                Console.WriteLine(webException.StackTrace);
                Response = webException.Response as HttpWebResponse;
            }
            catch (Exception exception)
            {
                Console.WriteLine("Exception occurred: [{0}].", exception.Message);
                Console.WriteLine(exception.StackTrace);
            }
            Console.WriteLine("Response status code: [{0}].", Response.StatusCode);
            Console.WriteLine("Response \"Content-Type\" header value: [{0}].", Response.ContentType);
            Console.WriteLine("Response \"Content-Length\" header value: [{0}].", Response.ContentLength);
            String ResponseMessageBody = new StreamReader(Response.GetResponseStream()).ReadToEnd();
            Console.WriteLine("Response message body: [{0}].", ResponseMessageBody);
        }
    }
}

When it reaches the line Request.GetResponse(), an AccessVioationException is thrown:

Unhandled Exception: System.AccessViolationException: Attempted to read or write
 protected memory. This is often an indication that other memory is corrupt.
   at System.Net.UnsafeNclNativeMethods.SafeNetHandles_SECURITY.AcquireCredentialsHandleW(String principal, String moduleName, Int32 usage, Void* logonID, SecureCredential& authData, Void* keyCallback, Void* keyArgument, SSPIHandle& handlePtr, Int64& timeStamp)
   at System.Net.SafeFreeCredentials.AcquireCredentialsHandle(SecurDll dll, String package, CredentialUse intent, SecureCredential& authdata, SafeFreeCredentials& outCredential)
   at System.Net.SSPISecureChannelType.AcquireCredentialsHandle(String moduleName, CredentialUse usage, SecureCredential& authdata, SafeFreeCredentials& outCredential)
   at System.Net.SSPIWrapper.AcquireCredentialsHandle(SSPIInterface SecModule, String package, CredentialUse intent, SecureCredential scc)
   at System.Net.Security.SecureChannel.AcquireCredentialsHandle(CredentialUse credUsage, SecureCredential& secureCredential)
   at System.Net.Security.SecureChannel.AcquireClientCredentials(Byte[]& thumbPrint)
   at System.Net.Security.SecureChannel.GenerateToken(Byte[] input, Int32 offset, Int32 count, Byte[]& output)
   at System.Net.Security.SecureChannel.NextMessage(Byte[] incoming, Int32 offset, Int32 count)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
   at System.Net.TlsStream.CallProcessAuthentication(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
   at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.ConnectStream.WriteHeaders(Boolean async)
   at System.Net.HttpWebRequest.EndSubmitRequest()
   at System.Net.HttpWebRequest.SetRequestSubmitDone(ConnectStream submitStream)
   at System.Net.Connection.CompleteConnection(Boolean async, HttpWebRequest request)
   at System.Net.Connection.CompleteStartConnection(Boolean async, HttpWebRequest httpWebRequest)
   at System.Net.Connection.CompleteStartRequest(Boolean onSubmitThread, HttpWebRequest request, TriState needReConnect)
   at System.Net.Connection.SubmitRequest(HttpWebRequest request, Boolean forcedsubmit)
   at System.Net.ServicePoint.SubmitRequest(HttpWebRequest request, String connName)
   at System.Net.HttpWebRequest.SubmitRequest(ServicePoint servicePoint)
   at System.Net.HttpWebRequest.GetResponse()
   at TestTwoWaySSL.Program.Main(String[] args)

Solution

  • The problem is that you are missing private key. You are using only the certificate.

    Either use pkcs#12 when loading certificate into X509Certificate2 class

    X509Certificate2 Certificate = new X509Certificate2("F:\\cert.p12", "p12 password");
    

    or load certificate from store

    X509Store store = new X509Store("MY",StoreLocation.CurrentUser);
    store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
    
    X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates;
    X509Certificate2 certificate = collection.Find(...)[0];
    store.Close();