Mono https webrequest falla con " La autenticación o descifrado ha fallado"


Estoy haciendo un cliente REST simple para usar en mis aplicaciones de C#. En. net en Windows Funciona muy bien con conexiones http: / / y https://. En mono 2.6.7 (También probado con 2.8 con los mismos resultados) en Ubuntu 10.10 solo funciona http://. las conexiones https:// generan esta excepción en la solicitud.Método GetResponse ():

Unhandled Exception: System.Net.WebException: Error getting response stream (Write: The authentication or decryption has failed.): SendFailure ---> System.IO.IOException: The authentication or decryption has failed. ---> Mono.Security.Protocol.Tls.TlsException: Invalid certificate received from server. Error code: 0xffffffff800b010a
  at Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.validateCertificates (Mono.Security.X509.X509CertificateCollection certificates) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.ProcessAsTls1 () [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.Handshake.HandshakeMessage.Process () [0x00000] in <filename unknown>:0 
  at (wrapper remoting-invoke-with-check) Mono.Security.Protocol.Tls.Handshake.HandshakeMessage:Process ()
  at Mono.Security.Protocol.Tls.ClientRecordProtocol.ProcessHandshakeMessage (Mono.Security.Protocol.Tls.TlsStream handMsg) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.InternalReceiveRecordCallback (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0 
  at System.Net.HttpWebRequest.GetResponse () [0x00000] in <filename unknown>:0 

No he podido encontrar ninguna manera de arreglar esto. ¿Alguien tiene alguna idea de por qué está pasando esto y cómo arreglarlo?

Nuevamente, esto solo falla en Mono,. Net no parece tener ningún problema para establecer una conexión.

Aquí está el código de llamada:

public JToken DoRequest(string path, params string[] parameters) {
    if(!path.StartsWith("/")) {
        path = "/" + path;
    }
    string fullUrl = url + path + ToQueryString(parameters);

    if(DebugUrls) Console.WriteLine("Requesting: {0}", fullUrl);

    WebRequest request = HttpWebRequest.CreateDefault(new Uri(fullUrl));
    using(WebResponse response = request.GetResponse())
    using(Stream responseStream = response.GetResponseStream()) {
        return ReadResponse(responseStream);
    }
}
Author: jww, 2011-02-08

10 answers

Mono no confía en ningún certificado de forma predeterminada, para importar las autoridades raíz de confianza de Mozilla puede ejecutar mozroots --import --quiet en la carpeta de instalación mono donde mozroots.exe se encuentra

 4
Author: LiriB,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-12 14:42:51

Tuve el mismo problema con Unity (que también usa mono) y este post me ayudó a resolverlo.

Simplemente agregue la siguiente línea antes de hacer su solicitud:

ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;

Y este método:

public bool MyRemoteCertificateValidationCallback(System.Object sender,
    X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    bool isOk = true;
    // If there are errors in the certificate chain,
    // look at each error to determine the cause.
    if (sslPolicyErrors != SslPolicyErrors.None) {
        for (int i=0; i<chain.ChainStatus.Length; i++) {
            if (chain.ChainStatus[i].Status == X509ChainStatusFlags.RevocationStatusUnknown) {
                continue;
            }
            chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EntireChain;
            chain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
            chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan (0, 1, 0);
            chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
            bool chainIsValid = chain.Build ((X509Certificate2)certificate);
            if (!chainIsValid) {
                isOk = false;
                break;
            }
        }
    }
    return isOk;
}
 44
Author: Ludovic Feltz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-06-15 10:03:58

. NET Framework en Windows usa el almacén de certificados de Windows (mmc, Agregar/Quitar complementos, Certificados) para determinar si se acepta un certificado SSL desde un sitio remoto. Windows se envía con un montón de Autoridades de Certificación (CA) Raíz e Intermedia y se actualizan periódicamente por Windows Update. Como resultado, su código. NET generalmente confiará en un certificado siempre que haya sido emitido por una CA o un descendiente de una CA en el almacén de certificados (las CA comerciales de mayor reputación son incluir).

En Mono, no hay un almacén de certificados de Windows. Mono tiene su propia tienda. De forma predeterminada, está vacía (no hay CA predeterminadas de confianza). Es necesario administrar las entradas usted mismo.

Echa un vistazo aquí:

Las mozroots.el punto exe causará su instalación mono para confiar en todo lo que Firefox confía después de una instalación predeterminada.

 27
Author: scottt732,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-22 19:08:58

Escriba esta línea antes de hacer solicitud http solicitud. esto debería ser trabajo.

ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) => { return true; });


private static bool RemoteCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
        //Return true if the server certificate is ok
        if (sslPolicyErrors == SslPolicyErrors.None)
            return true;

        bool acceptCertificate = true;
        string msg = "The server could not be validated for the following reason(s):\r\n";

        //The server did not present a certificate
        if ((sslPolicyErrors &
             SslPolicyErrors.RemoteCertificateNotAvailable) == SslPolicyErrors.RemoteCertificateNotAvailable)
        {
            msg = msg + "\r\n    -The server did not present a certificate.\r\n";
            acceptCertificate = false;
        }
        else
        {
            //The certificate does not match the server name
            if ((sslPolicyErrors &
                 SslPolicyErrors.RemoteCertificateNameMismatch) == SslPolicyErrors.RemoteCertificateNameMismatch)
            {
                msg = msg + "\r\n    -The certificate name does not match the authenticated name.\r\n";
                acceptCertificate = false;
            }

            //There is some other problem with the certificate
            if ((sslPolicyErrors &
                 SslPolicyErrors.RemoteCertificateChainErrors) == SslPolicyErrors.RemoteCertificateChainErrors)
            {
                foreach (X509ChainStatus item in chain.ChainStatus)
                {
                    if (item.Status != X509ChainStatusFlags.RevocationStatusUnknown &&
                        item.Status != X509ChainStatusFlags.OfflineRevocation)
                        break;

                    if (item.Status != X509ChainStatusFlags.NoError)
                    {
                        msg = msg + "\r\n    -" + item.StatusInformation;
                        acceptCertificate = false;
                    }
                }
            }
        }

        //If Validation failed, present message box
        if (acceptCertificate == false)
        {
            msg = msg + "\r\nDo you wish to override the security check?";
//          if (MessageBox.Show(msg, "Security Alert: Server could not be validated",
//                       MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                acceptCertificate = true;
        }

        return acceptCertificate;
    }
 7
Author: Pawan Chaurasiya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-12-03 12:07:33

También encuentro el error.

Probé ServicePointManager.ServerCertificateValidationCallback y ServicePointManager.CertificatePolicy pero todavía no funciona.

Me enfado. construye una envoltura de rizo. Funciona bien para mi proyecto de juguetes.

/// <summary>
/// For MONO ssl decryption failed
/// </summary>
public static string PostString(string url, string data)
{
    Process p = null;
    try
    {
        var psi = new ProcessStartInfo
        {
            FileName = "curl",
            Arguments = string.Format("-k {0} --data \"{1}\"", url, data),
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = false,
        };

        p = Process.Start(psi);

        return p.StandardOutput.ReadToEnd();
    }
    finally
    {
        if (p != null && p.HasExited == false)
            p.Kill();
    }
}
 3
Author: IlPADlI,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-30 15:47:57

Yo tenía el mismo problema. Cuando la respuesta http lanza esta excepción entonces lo hago:

System.Diagnostics.Process.Start("mozroots","--import --quiet");

Esto importa los certificados que faltan y la excepción nunca volverá a ocurrir.

 3
Author: harry4516,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-11 22:57:26

La primera respuesta ya lo dice: Mono en cualquier cosa que no sea Windows no viene con nada, por lo que inicialmente no confía en ningún certificado. Entonces, ¿qué hacer?

Aquí hay un buen artículo sobre diferentes maneras de lidiar con el problema desde la perspectiva del desarrollador: http://www.mono-project.com/archived/usingtrustedrootsrespectfully /

Breve resumen: Puedes:

  • ignorar el problema de seguridad
  • ignora el problema
  • deje que el usuario conocer y abortar
  • hágale saber al usuario y déle la opción de continuar bajo su propio riesgo

El enlace anterior viene con ejemplos de código para cada caso.

 2
Author: Gerhard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-16 23:13:58

Otra solución para Unity es inicializar el ServicePointManager una vez para aceptar siempre los certificados. Esto funciona, pero obviamente no es seguro.

System.Net.ServicePointManager.ServerCertificateValidationCallback +=
           delegate (object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                                   System.Security.Cryptography.X509Certificates.X509Chain chain,
                                   System.Net.Security.SslPolicyErrors sslPolicyErrors)
           {
               return true; // **** Always accept
       };
 2
Author: Erik Kalkoken,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-19 21:03:35

Puede establecer la implementación Mono TLS en la compilación de iOS y todo funcionará bien como se describe aquí: http://massivepixel.co/blog/post/xamarin-studio-6-certificate-unknown (aunque Mono TLS no es compatible con las versiones más recientes de TLS, pero todavía no he tropezado con el problema de que es un problema).

 0
Author: Ivan Ičin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-03 11:09:29

Todavía tenía este problema después de importar los certificados según la respuesta aceptada.

Encontré que el soporte para TLS 1.2 se agregó en Mono 4.8.0que usa Google BoringSSL, y que estaba usando una versión de Mono más antigua que esta. Actualizé a Mono 5.10 y ahora puedo conectarme sin recibir esta excepción.

 0
Author: Owen Pauling,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-26 13:17:07