ServerCredentials.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 Grpc.Core.Internal;
  19. using Grpc.Core.Utils;
  20. namespace Grpc.Core
  21. {
  22. /// <summary>
  23. /// Server side credentials.
  24. /// </summary>
  25. public abstract class ServerCredentials
  26. {
  27. static readonly ServerCredentials InsecureInstance = new InsecureServerCredentialsImpl();
  28. /// <summary>
  29. /// Returns instance of credential that provides no security and
  30. /// will result in creating an unsecure server port with no encryption whatsoever.
  31. /// </summary>
  32. public static ServerCredentials Insecure
  33. {
  34. get
  35. {
  36. return InsecureInstance;
  37. }
  38. }
  39. /// <summary>
  40. /// Creates native object for the credentials.
  41. /// </summary>
  42. /// <returns>The native credentials.</returns>
  43. internal abstract ServerCredentialsSafeHandle ToNativeCredentials();
  44. private sealed class InsecureServerCredentialsImpl : ServerCredentials
  45. {
  46. internal override ServerCredentialsSafeHandle ToNativeCredentials()
  47. {
  48. return null;
  49. }
  50. }
  51. }
  52. /// <summary>
  53. /// Server-side SSL credentials.
  54. /// </summary>
  55. public class SslServerCredentials : ServerCredentials
  56. {
  57. readonly IList<KeyCertificatePair> keyCertificatePairs;
  58. readonly string rootCertificates;
  59. readonly bool forceClientAuth;
  60. /// <summary>
  61. /// Creates server-side SSL credentials.
  62. /// </summary>
  63. /// <param name="keyCertificatePairs">Key-certificates to use.</param>
  64. /// <param name="rootCertificates">PEM encoded client root certificates used to authenticate client.</param>
  65. /// <param name="forceClientAuth">If true, client will be rejected unless it proves its unthenticity using against rootCertificates.</param>
  66. public SslServerCredentials(IEnumerable<KeyCertificatePair> keyCertificatePairs, string rootCertificates, bool forceClientAuth)
  67. {
  68. this.keyCertificatePairs = new List<KeyCertificatePair>(keyCertificatePairs).AsReadOnly();
  69. GrpcPreconditions.CheckArgument(this.keyCertificatePairs.Count > 0,
  70. "At least one KeyCertificatePair needs to be provided.");
  71. if (forceClientAuth)
  72. {
  73. GrpcPreconditions.CheckNotNull(rootCertificates,
  74. "Cannot force client authentication unless you provide rootCertificates.");
  75. }
  76. this.rootCertificates = rootCertificates;
  77. this.forceClientAuth = forceClientAuth;
  78. }
  79. /// <summary>
  80. /// Creates server-side SSL credentials.
  81. /// This constructor should be use if you do not wish to autheticate client
  82. /// using client root certificates.
  83. /// </summary>
  84. /// <param name="keyCertificatePairs">Key-certificates to use.</param>
  85. public SslServerCredentials(IEnumerable<KeyCertificatePair> keyCertificatePairs) : this(keyCertificatePairs, null, false)
  86. {
  87. }
  88. /// <summary>
  89. /// Key-certificate pairs.
  90. /// </summary>
  91. public IList<KeyCertificatePair> KeyCertificatePairs
  92. {
  93. get
  94. {
  95. return this.keyCertificatePairs;
  96. }
  97. }
  98. /// <summary>
  99. /// PEM encoded client root certificates.
  100. /// </summary>
  101. public string RootCertificates
  102. {
  103. get
  104. {
  105. return this.rootCertificates;
  106. }
  107. }
  108. /// <summary>
  109. /// If true, the authenticity of client check will be enforced.
  110. /// </summary>
  111. public bool ForceClientAuthentication
  112. {
  113. get
  114. {
  115. return this.forceClientAuth;
  116. }
  117. }
  118. internal override ServerCredentialsSafeHandle ToNativeCredentials()
  119. {
  120. int count = keyCertificatePairs.Count;
  121. string[] certChains = new string[count];
  122. string[] keys = new string[count];
  123. for (int i = 0; i < count; i++)
  124. {
  125. certChains[i] = keyCertificatePairs[i].CertificateChain;
  126. keys[i] = keyCertificatePairs[i].PrivateKey;
  127. }
  128. return ServerCredentialsSafeHandle.CreateSslCredentials(rootCertificates, certChains, keys, forceClientAuth);
  129. }
  130. }
  131. }