SslCredentials.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #region Copyright notice and license
  2. // Copyright 2019 The gRPC Authors
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. namespace Grpc.Core
  17. {
  18. /// <summary>
  19. /// Callback invoked with the expected targetHost and the peer's certificate.
  20. /// If false is returned by this callback then it is treated as a
  21. /// verification failure and the attempted connection will fail.
  22. /// Invocation of the callback is blocking, so any
  23. /// implementation should be light-weight.
  24. /// Note that the callback can potentially be invoked multiple times,
  25. /// concurrently from different threads (e.g. when multiple connections
  26. /// are being created for the same credentials).
  27. /// </summary>
  28. /// <param name="context">The <see cref="T:Grpc.Core.VerifyPeerContext"/> associated with the callback</param>
  29. /// <returns>true if verification succeeded, false otherwise.</returns>
  30. /// Note: experimental API that can change or be removed without any prior notice.
  31. public delegate bool VerifyPeerCallback(VerifyPeerContext context);
  32. /// <summary>
  33. /// Client-side SSL credentials.
  34. /// </summary>
  35. public sealed class SslCredentials : ChannelCredentials
  36. {
  37. readonly string rootCertificates;
  38. readonly KeyCertificatePair keyCertificatePair;
  39. readonly VerifyPeerCallback verifyPeerCallback;
  40. /// <summary>
  41. /// Creates client-side SSL credentials loaded from
  42. /// disk file pointed to by the GRPC_DEFAULT_SSL_ROOTS_FILE_PATH environment variable.
  43. /// If that fails, gets the roots certificates from a well known place on disk.
  44. /// </summary>
  45. public SslCredentials() : this(null, null, null)
  46. {
  47. }
  48. /// <summary>
  49. /// Creates client-side SSL credentials from
  50. /// a string containing PEM encoded root certificates.
  51. /// </summary>
  52. public SslCredentials(string rootCertificates) : this(rootCertificates, null, null)
  53. {
  54. }
  55. /// <summary>
  56. /// Creates client-side SSL credentials.
  57. /// </summary>
  58. /// <param name="rootCertificates">string containing PEM encoded server root certificates.</param>
  59. /// <param name="keyCertificatePair">a key certificate pair.</param>
  60. public SslCredentials(string rootCertificates, KeyCertificatePair keyCertificatePair) :
  61. this(rootCertificates, keyCertificatePair, null)
  62. {
  63. }
  64. /// <summary>
  65. /// Creates client-side SSL credentials.
  66. /// </summary>
  67. /// <param name="rootCertificates">string containing PEM encoded server root certificates.</param>
  68. /// <param name="keyCertificatePair">a key certificate pair.</param>
  69. /// <param name="verifyPeerCallback">a callback to verify peer's target name and certificate.</param>
  70. /// Note: experimental API that can change or be removed without any prior notice.
  71. public SslCredentials(string rootCertificates, KeyCertificatePair keyCertificatePair, VerifyPeerCallback verifyPeerCallback)
  72. {
  73. this.rootCertificates = rootCertificates;
  74. this.keyCertificatePair = keyCertificatePair;
  75. this.verifyPeerCallback = verifyPeerCallback;
  76. }
  77. /// <summary>
  78. /// PEM encoding of the server root certificates.
  79. /// </summary>
  80. public string RootCertificates
  81. {
  82. get
  83. {
  84. return this.rootCertificates;
  85. }
  86. }
  87. /// <summary>
  88. /// Client side key and certificate pair.
  89. /// If null, client will not use key and certificate pair.
  90. /// </summary>
  91. public KeyCertificatePair KeyCertificatePair
  92. {
  93. get
  94. {
  95. return this.keyCertificatePair;
  96. }
  97. }
  98. /// <summary>
  99. /// Populates channel credentials configurator with this instance's configuration.
  100. /// End users never need to invoke this method as it is part of internal implementation.
  101. /// </summary>
  102. public override void InternalPopulateConfiguration(ChannelCredentialsConfiguratorBase configurator, object state)
  103. {
  104. configurator.SetSslCredentials(state, rootCertificates, keyCertificatePair, verifyPeerCallback);
  105. }
  106. internal override bool IsComposable => true;
  107. }
  108. }