ChannelCredentials.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #region Copyright notice and license
  2. // Copyright 2015 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. using System;
  17. using System.Collections.Generic;
  18. using System.Threading.Tasks;
  19. using Grpc.Core.Internal;
  20. using Grpc.Core.Utils;
  21. namespace Grpc.Core
  22. {
  23. /// <summary>
  24. /// Client-side channel credentials. Used for creation of a secure channel.
  25. /// </summary>
  26. public abstract class ChannelCredentials
  27. {
  28. static readonly ChannelCredentials InsecureInstance = new InsecureCredentialsImpl();
  29. /// <summary>
  30. /// Returns instance of credentials that provides no security and
  31. /// will result in creating an unsecure channel with no encryption whatsoever.
  32. /// </summary>
  33. public static ChannelCredentials Insecure
  34. {
  35. get
  36. {
  37. return InsecureInstance;
  38. }
  39. }
  40. /// <summary>
  41. /// Creates a new instance of <c>ChannelCredentials</c> class by composing
  42. /// given channel credentials with call credentials.
  43. /// </summary>
  44. /// <param name="channelCredentials">Channel credentials.</param>
  45. /// <param name="callCredentials">Call credentials.</param>
  46. /// <returns>The new composite <c>ChannelCredentials</c></returns>
  47. public static ChannelCredentials Create(ChannelCredentials channelCredentials, CallCredentials callCredentials)
  48. {
  49. return new CompositeChannelCredentials(channelCredentials, callCredentials);
  50. }
  51. /// <summary>
  52. /// Creates native object for the credentials. May return null if insecure channel
  53. /// should be created.
  54. /// </summary>
  55. /// <returns>The native credentials.</returns>
  56. internal abstract ChannelCredentialsSafeHandle ToNativeCredentials();
  57. /// <summary>
  58. /// Returns <c>true</c> if this credential type allows being composed by <c>CompositeCredentials</c>.
  59. /// </summary>
  60. internal virtual bool IsComposable
  61. {
  62. get { return false; }
  63. }
  64. private sealed class InsecureCredentialsImpl : ChannelCredentials
  65. {
  66. internal override ChannelCredentialsSafeHandle ToNativeCredentials()
  67. {
  68. return null;
  69. }
  70. }
  71. }
  72. /// <summary>
  73. /// Client-side SSL credentials.
  74. /// </summary>
  75. public sealed class SslCredentials : ChannelCredentials
  76. {
  77. readonly string rootCertificates;
  78. readonly KeyCertificatePair keyCertificatePair;
  79. /// <summary>
  80. /// Creates client-side SSL credentials loaded from
  81. /// disk file pointed to by the GRPC_DEFAULT_SSL_ROOTS_FILE_PATH environment variable.
  82. /// If that fails, gets the roots certificates from a well known place on disk.
  83. /// </summary>
  84. public SslCredentials() : this(null, null)
  85. {
  86. }
  87. /// <summary>
  88. /// Creates client-side SSL credentials from
  89. /// a string containing PEM encoded root certificates.
  90. /// </summary>
  91. public SslCredentials(string rootCertificates) : this(rootCertificates, null)
  92. {
  93. }
  94. /// <summary>
  95. /// Creates client-side SSL credentials.
  96. /// </summary>
  97. /// <param name="rootCertificates">string containing PEM encoded server root certificates.</param>
  98. /// <param name="keyCertificatePair">a key certificate pair.</param>
  99. public SslCredentials(string rootCertificates, KeyCertificatePair keyCertificatePair)
  100. {
  101. this.rootCertificates = rootCertificates;
  102. this.keyCertificatePair = keyCertificatePair;
  103. }
  104. /// <summary>
  105. /// PEM encoding of the server root certificates.
  106. /// </summary>
  107. public string RootCertificates
  108. {
  109. get
  110. {
  111. return this.rootCertificates;
  112. }
  113. }
  114. /// <summary>
  115. /// Client side key and certificate pair.
  116. /// If null, client will not use key and certificate pair.
  117. /// </summary>
  118. public KeyCertificatePair KeyCertificatePair
  119. {
  120. get
  121. {
  122. return this.keyCertificatePair;
  123. }
  124. }
  125. // Composing composite makes no sense.
  126. internal override bool IsComposable
  127. {
  128. get { return true; }
  129. }
  130. internal override ChannelCredentialsSafeHandle ToNativeCredentials()
  131. {
  132. return ChannelCredentialsSafeHandle.CreateSslCredentials(rootCertificates, keyCertificatePair);
  133. }
  134. }
  135. /// <summary>
  136. /// Credentials that allow composing one <see cref="ChannelCredentials"/> object and
  137. /// one or more <see cref="CallCredentials"/> objects into a single <see cref="ChannelCredentials"/>.
  138. /// </summary>
  139. internal sealed class CompositeChannelCredentials : ChannelCredentials
  140. {
  141. readonly ChannelCredentials channelCredentials;
  142. readonly CallCredentials callCredentials;
  143. /// <summary>
  144. /// Initializes a new instance of <c>CompositeChannelCredentials</c> class.
  145. /// The resulting credentials object will be composite of all the credentials specified as parameters.
  146. /// </summary>
  147. /// <param name="channelCredentials">channelCredentials to compose</param>
  148. /// <param name="callCredentials">channelCredentials to compose</param>
  149. public CompositeChannelCredentials(ChannelCredentials channelCredentials, CallCredentials callCredentials)
  150. {
  151. this.channelCredentials = GrpcPreconditions.CheckNotNull(channelCredentials);
  152. this.callCredentials = GrpcPreconditions.CheckNotNull(callCredentials);
  153. GrpcPreconditions.CheckArgument(channelCredentials.IsComposable, "Supplied channel credentials do not allow composition.");
  154. }
  155. internal override ChannelCredentialsSafeHandle ToNativeCredentials()
  156. {
  157. using (var channelCreds = channelCredentials.ToNativeCredentials())
  158. using (var callCreds = callCredentials.ToNativeCredentials())
  159. {
  160. var nativeComposite = ChannelCredentialsSafeHandle.CreateComposite(channelCreds, callCreds);
  161. if (nativeComposite.IsInvalid)
  162. {
  163. throw new ArgumentException("Error creating native composite credentials. Likely, this is because you are trying to compose incompatible credentials.");
  164. }
  165. return nativeComposite;
  166. }
  167. }
  168. }
  169. }