HttpsClientStream.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // HttpsClientStream.cs: Glue between HttpWebRequest and SslClientStream to
  3. // reduce reflection usage.
  4. //
  5. // Author:
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) 2004-2007 Novell, Inc. (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.IO;
  31. using System.Net;
  32. using System.Security.Cryptography;
  33. using System.Security.Cryptography.X509Certificates;
  34. using SNS = System.Net.Security;
  35. using SNCX = System.Security.Cryptography.X509Certificates;
  36. namespace Mono.Security.Protocol.Tls {
  37. // Note: DO NOT REUSE this class - instead use SslClientStream
  38. internal class HttpsClientStream : SslClientStream {
  39. private HttpWebRequest _request;
  40. private int _status;
  41. public HttpsClientStream (Stream stream, X509CertificateCollection clientCertificates,
  42. HttpWebRequest request, byte [] buffer)
  43. : base (stream, request.Address.Host, false, (Mono.Security.Protocol.Tls.SecurityProtocolType)
  44. ServicePointManager.SecurityProtocol, clientCertificates)
  45. {
  46. // this constructor permit access to the WebRequest to call
  47. // ICertificatePolicy.CheckValidationResult
  48. _request = request;
  49. _status = 0;
  50. if (buffer != null)
  51. InputBuffer.Write (buffer, 0, buffer.Length);
  52. // also saved from reflection
  53. base.CheckCertRevocationStatus = ServicePointManager.CheckCertificateRevocationList;
  54. ClientCertSelection += delegate (X509CertificateCollection clientCerts, X509Certificate serverCertificate,
  55. string targetHost, X509CertificateCollection serverRequestedCertificates) {
  56. return ((clientCerts == null) || (clientCerts.Count == 0)) ? null : clientCerts [0];
  57. };
  58. PrivateKeySelection += delegate (X509Certificate certificate, string targetHost) {
  59. X509Certificate2 cert = (certificate as X509Certificate2);
  60. return (cert == null) ? null : cert.PrivateKey;
  61. };
  62. }
  63. public bool TrustFailure {
  64. get {
  65. switch (_status) {
  66. case -2146762486: // CERT_E_CHAINING 0x800B010A
  67. case -2146762487: // CERT_E_UNTRUSTEDROOT 0x800B0109
  68. return true;
  69. default:
  70. return false;
  71. }
  72. }
  73. }
  74. internal override bool RaiseServerCertificateValidation (X509Certificate certificate, int[] certificateErrors)
  75. {
  76. bool failed = (certificateErrors.Length > 0);
  77. // only one problem can be reported by this interface
  78. _status = ((failed) ? certificateErrors [0] : 0);
  79. #pragma warning disable 618
  80. if (ServicePointManager.CertificatePolicy != null) {
  81. ServicePoint sp = _request.ServicePoint;
  82. bool res = ServicePointManager.CertificatePolicy.CheckValidationResult (sp, certificate, _request, _status);
  83. if (!res)
  84. return false;
  85. failed = true;
  86. }
  87. #pragma warning restore 618
  88. if (HaveRemoteValidation2Callback)
  89. return failed; // The validation already tried the 2.0 callback
  90. SNS.RemoteCertificateValidationCallback cb = ServicePointManager.ServerCertificateValidationCallback;
  91. if (cb != null) {
  92. SNS.SslPolicyErrors ssl_errors = 0;
  93. foreach (int i in certificateErrors) {
  94. if (i == (int)-2146762490) // TODO: is this what happens when the purpose is wrong?
  95. ssl_errors |= SNS.SslPolicyErrors.RemoteCertificateNotAvailable;
  96. else if (i == (int) -2146762481)
  97. ssl_errors |= SNS.SslPolicyErrors.RemoteCertificateNameMismatch;
  98. else
  99. ssl_errors |= SNS.SslPolicyErrors.RemoteCertificateChainErrors;
  100. }
  101. SNCX.X509Certificate2 cert2 = new SNCX.X509Certificate2 (certificate.GetRawCertData ());
  102. SNCX.X509Chain chain = new SNCX.X509Chain ();
  103. if (!chain.Build (cert2))
  104. ssl_errors |= SNS.SslPolicyErrors.RemoteCertificateChainErrors;
  105. return cb (_request, cert2, chain, ssl_errors);
  106. }
  107. return failed;
  108. }
  109. }
  110. }