HttpsClientStream.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. namespace Mono.Security.Protocol.Tls {
  35. // Note: DO NOT REUSE this class - instead use SslClientStream
  36. internal class HttpsClientStream : SslClientStream {
  37. private HttpWebRequest _request;
  38. private int _status;
  39. public HttpsClientStream (Stream stream, X509CertificateCollection clientCertificates,
  40. HttpWebRequest request, byte [] buffer)
  41. : base (stream, request.RequestUri.Host, false, (Mono.Security.Protocol.Tls.SecurityProtocolType)
  42. ServicePointManager.SecurityProtocol, clientCertificates)
  43. {
  44. // this constructor permit access to the WebRequest to call
  45. // ICertificatePolicy.CheckValidationResult
  46. _request = request;
  47. _status = 0;
  48. if (buffer != null)
  49. InputBuffer.Write (buffer, 0, buffer.Length);
  50. #if !NET_1_0
  51. // also saved from reflection
  52. base.CheckCertRevocationStatus = ServicePointManager.CheckCertificateRevocationList;
  53. #endif
  54. #if NET_2_0
  55. ClientCertSelection += delegate (X509CertificateCollection clientCerts, X509Certificate serverCertificate,
  56. string targetHost, X509CertificateCollection serverRequestedCertificates) {
  57. return ((clientCerts == null) || (clientCerts.Count == 0)) ? null : clientCerts [0];
  58. };
  59. PrivateKeySelection += delegate (X509Certificate certificate, string targetHost) {
  60. X509Certificate2 cert = (certificate as X509Certificate2);
  61. return (cert == null) ? null : cert.PrivateKey;
  62. };
  63. #endif
  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. if (ServicePointManager.CertificatePolicy != null) {
  82. ServicePoint sp = _request.ServicePoint;
  83. return ServicePointManager.CertificatePolicy.CheckValidationResult (sp, certificate, _request, _status);
  84. }
  85. return failed;
  86. }
  87. }
  88. }