2012-01-01 11 views
6

¿Cómo se puede anclar un certificado cuando se usa s SqlConnection? Desde SqlConnection Connection String Parameter Keywords & Values, sé que puedo configurar Encrypted en true para forzar (¿alentar?) El uso de SSL/TLS..Net SqlConnection, Server Authentication y Certificate Pinning

Sin embargo, para anclar un certificado, creo que necesitamos usar ServerCertificateValidationCallback de ServicePointManager (el código de ejemplo siguiente fue ofrecido por Arne Vajhøj para HTTP/HTTPS). No tengo claro cómo cablear en PinCertificate (de ServicePointManager) a SqlConnection.

ACTUALIZACIÓN: Hablando con Arne Vajhøj en microsoft.public.dotnet.languages.csharp, parece que no es posible tener el control deseado sobre la conexión. Vajhøj ofreció un enlace al Encrypting Connections to SQL Server.

public static void Main(string[] args) 
{ 
    ServicePointManager.ServerCertificateValidationCallback = PinCertificate; 
    WebRequest wr = WebRequest.Create("https://www.google.com/"); 

    wr.GetResponse(); 
} 

public static bool PinCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
{ 
    byte[] chash = certificate.GetCertHash(); 

    StringBuilder sb = new StringBuilder(chash.Length * 2); 
    foreach (byte b in chash) 
    sb.AppendFormat("{0:X2}", b); 

    // Verify against known SHA1 thumb print of the certificate 
    String hash = sb.ToString(); 
    if (hash != "C1956DC8A7DFB2A5A56934DA09778E3A11023358") 
    return false; 

    return true; 
} 
+0

Un ejemplo de validación de certificados en VB.NET: http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/ 5f5af31c-74f2-4147-8b70-e9e8ec03c76a. Debería ser lo suficientemente fácil de convertir. –

+0

El ejemplo de MSDN utiliza 'ServicePointManager' y' ServerCertificateValidationCallback' (llamando a 'MyCertValidationCb'). No es diferente de la muestra que publiqué. Todavía no tengo claro cómo se conecta 'ServerCertificateValidationCallback' en' SqlConnection'. – jww

+0

http://support.microsoft.com/default.aspx?scid=276553 ¿Esto ayuda? – King

Respuesta

0

¿qué tal algo como:

System.Net.ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateCertificate) 

Private Function ValidateCertificate(ByVal sender As Object, ByVal certificate As X509Certificate, ByVal chain As X509Chain, ByVal sslPolicyErrors As SslPolicyErrors) As Boolean 
    'Return True to force the certificate to be accepted. 
    Return True 
End Function 
Cuestiones relacionadas