2014-06-05 4 views
1

간단한 C# SSL 서버 코드가 실패하는 이유를 이해하는 데 어려움을 겪고 있습니다. 파일 시스템에서로드하는 .p12 파일을 사용하여 '서버로 인증'을 시도하면 오류가 발생합니다.간단한 C# SSL 서버 : 패키지에 제공된 자격 증명을 인식하지 못했습니다.

System.ComponentModel.Win32Exception was unhandled 
    HResult=-2147467259 
    Message=The credentials supplied to the package were not recognized 
    Source=System 
    ErrorCode=-2147467259 
    NativeErrorCode=-2146893043 

X509Certificate2 개체 '= 진정한 개인 키가'내가 시도한 것을 나타냅니다 : 여기

내 코드입니다 :

IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 2045); 

Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
server.Bind(localEndPoint); 

while (true) 
{ 
server.Listen(10); 
Socket client = server.Accept(); 

Stream clientStream = new NetworkStream(client, true); 
var clientTlsStream = new SslStream(clientStream, false); 

var p12Cert = 
    new X509Certificate2(
     @"C:\Repos\ITEL-Trunk\Code\Applications\Server\bin\Debug\Configuration\Certificate_tool_signed_with_ca.p12", 
     "Passw0rd"); 

clientTlsStream.AuthenticateAsServer(p12Cert); 

var cr = clientTlsStream.CanRead; 

}

그리고 여기에 예외 세부입니다 행운없이 Admininstrator로 실행됩니다. 이것에 대해 본 다른 질문은 CAPI 사용 권한을 중심으로하지만, 파일 시스템에서 곧바로로드되므로 이러한 응답을 적용 할 수 없었습니다.

답변

1

이 문제를 p12 파일이 생성되는 방식으로 다시 추적 할 수있었습니다. .p12는 인증서 및 개인 키를 올바르게 패키징하지 않는 Bouncy-Castle 기반의 C# 유틸리티로 만들어졌습니다. 인증서 생성기를 변경하여 인증서 및 서명에 사용 된 CA 인증서를 올바르게 패키지화해야했습니다.

인증서 생성기를 변경 한 후 "패키지에 제공된 자격 증명을 인식하지 못했습니다"예외가 사라졌습니다. 여기

작동 보인다 P12 패키저 코드 :

// Create the PKCS12 store 
Pkcs12Store store = new Pkcs12Store(); 

// Add a Certificate entry 
string certCn = cert.SubjectDN.GetValues(X509Name.CN).OfType<string>().Single(); 
X509CertificateEntry certEntry = new X509CertificateEntry(cert); 
store.SetCertificateEntry(certCn, certEntry); // use DN as the Alias. 

// Add a key entry & cert chain (if applicable) 
AsymmetricKeyEntry keyEntry = new AsymmetricKeyEntry(kp.Private); 

X509CertificateEntry[] certChain; 
if (_issuerCert != null) 
{ 
    X509CertificateEntry issuerCertEntry = new X509CertificateEntry(_issuerCert); 
    certChain = new X509CertificateEntry[] { certEntry, issuerCertEntry}; 
} 
else 
{ 
    certChain = new X509CertificateEntry[] { certEntry }; 
} 

store.SetKeyEntry(certCn, keyEntry, certChain); // Set the friendly name along with the generated certs key and its chain 

// Write the p12 file to disk 
FileInfo p12File = new FileInfo(pathToP12File); 
Directory.CreateDirectory(p12File.DirectoryName); 

using (FileStream filestream = new FileStream(pathToP12File, FileMode.Create, FileAccess.ReadWrite)) 
{ 
    store.Save(filestream, password.ToCharArray(), new SecureRandom()); 
} 
관련 문제