HttpsClientStream.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #if !NET_1_0
  53. // also saved from reflection
  54. base.CheckCertRevocationStatus = ServicePointManager.CheckCertificateRevocationList;
  55. #endif
  56. ClientCertSelection += delegate (X509CertificateCollection clientCerts, X509Certificate serverCertificate,
  57. string targetHost, X509CertificateCollection serverRequestedCertificates) {
  58. return ((clientCerts == null) || (clientCerts.Count == 0)) ? null : clientCerts [0];
  59. };
  60. PrivateKeySelection += delegate (X509Certificate certificate, string targetHost) {
  61. X509Certificate2 cert = (certificate as X509Certificate2);
  62. return (cert == null) ? null : cert.PrivateKey;
  63. };
  64. }
  65. public bool TrustFailure {
  66. get {
  67. switch (_status) {
  68. case -2146762486: // CERT_E_CHAINING 0x800B010A
  69. case -2146762487: // CERT_E_UNTRUSTEDROOT 0x800B0109
  70. return true;
  71. default:
  72. return false;
  73. }
  74. }
  75. }
  76. internal override bool RaiseServerCertificateValidation (X509Certificate certificate, int[] certificateErrors)
  77. {
  78. bool failed = (certificateErrors.Length > 0);
  79. // only one problem can be reported by this interface
  80. _status = ((failed) ? certificateErrors [0] : 0);
  81. #pragma warning disable 618
  82. if (ServicePointManager.CertificatePolicy != null) {
  83. ServicePoint sp = _request.ServicePoint;
  84. bool res = ServicePointManager.CertificatePolicy.CheckValidationResult (sp, certificate, _request, _status);
  85. if (!res)
  86. return false;
  87. failed = true;
  88. }
  89. #pragma warning restore 618
  90. if (HaveRemoteValidation2Callback)
  91. return failed; // The validation already tried the 2.0 callback
  92. SNS.RemoteCertificateValidationCallback cb = ServicePointManager.ServerCertificateValidationCallback;
  93. if (cb != null) {
  94. SNS.SslPolicyErrors ssl_errors = 0;
  95. foreach (int i in certificateErrors) {
  96. if (i == (int)-2146762490) // TODO: is this what happens when the purpose is wrong?
  97. ssl_errors |= SNS.SslPolicyErrors.RemoteCertificateNotAvailable;
  98. else if (i == (int) -2146762481)
  99. ssl_errors |= SNS.SslPolicyErrors.RemoteCertificateNameMismatch;
  100. else
  101. ssl_errors |= SNS.SslPolicyErrors.RemoteCertificateChainErrors;
  102. }
  103. SNCX.X509Certificate2 cert2 = new SNCX.X509Certificate2 (certificate.GetRawCertData ());
  104. SNCX.X509Chain chain = new SNCX.X509Chain ();
  105. if (!chain.Build (cert2))
  106. ssl_errors |= SNS.SslPolicyErrors.RemoteCertificateChainErrors;
  107. return cb (_request, cert2, chain, ssl_errors);
  108. }
  109. return failed;
  110. }
  111. }
  112. }