OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 |
| 3 // Copyright 2015-2016, Google Inc. |
| 4 // All rights reserved. |
| 5 // |
| 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are |
| 8 // met: |
| 9 // |
| 10 // * Redistributions of source code must retain the above copyright |
| 11 // notice, this list of conditions and the following disclaimer. |
| 12 // * Redistributions in binary form must reproduce the above |
| 13 // copyright notice, this list of conditions and the following disclaimer |
| 14 // in the documentation and/or other materials provided with the |
| 15 // distribution. |
| 16 // * Neither the name of Google Inc. nor the names of its |
| 17 // contributors may be used to endorse or promote products derived from |
| 18 // this software without specific prior written permission. |
| 19 // |
| 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
| 32 #endregion |
| 33 |
| 34 using System; |
| 35 using System.Collections.Generic; |
| 36 using Grpc.Core.Internal; |
| 37 using Grpc.Core.Utils; |
| 38 |
| 39 namespace Grpc.Core |
| 40 { |
| 41 /// <summary> |
| 42 /// Server side credentials. |
| 43 /// </summary> |
| 44 public abstract class ServerCredentials |
| 45 { |
| 46 static readonly ServerCredentials InsecureInstance = new InsecureServerC
redentialsImpl(); |
| 47 |
| 48 /// <summary> |
| 49 /// Returns instance of credential that provides no security and |
| 50 /// will result in creating an unsecure server port with no encryption w
hatsoever. |
| 51 /// </summary> |
| 52 public static ServerCredentials Insecure |
| 53 { |
| 54 get |
| 55 { |
| 56 return InsecureInstance; |
| 57 } |
| 58 } |
| 59 |
| 60 /// <summary> |
| 61 /// Creates native object for the credentials. |
| 62 /// </summary> |
| 63 /// <returns>The native credentials.</returns> |
| 64 internal abstract ServerCredentialsSafeHandle ToNativeCredentials(); |
| 65 |
| 66 private sealed class InsecureServerCredentialsImpl : ServerCredentials |
| 67 { |
| 68 internal override ServerCredentialsSafeHandle ToNativeCredentials() |
| 69 { |
| 70 return null; |
| 71 } |
| 72 } |
| 73 } |
| 74 |
| 75 /// <summary> |
| 76 /// Server-side SSL credentials. |
| 77 /// </summary> |
| 78 public class SslServerCredentials : ServerCredentials |
| 79 { |
| 80 readonly IList<KeyCertificatePair> keyCertificatePairs; |
| 81 readonly string rootCertificates; |
| 82 readonly bool forceClientAuth; |
| 83 |
| 84 /// <summary> |
| 85 /// Creates server-side SSL credentials. |
| 86 /// </summary> |
| 87 /// <param name="keyCertificatePairs">Key-certificates to use.</param> |
| 88 /// <param name="rootCertificates">PEM encoded client root certificates
used to authenticate client.</param> |
| 89 /// <param name="forceClientAuth">If true, client will be rejected unles
s it proves its unthenticity using against rootCertificates.</param> |
| 90 public SslServerCredentials(IEnumerable<KeyCertificatePair> keyCertifica
tePairs, string rootCertificates, bool forceClientAuth) |
| 91 { |
| 92 this.keyCertificatePairs = new List<KeyCertificatePair>(keyCertifica
tePairs).AsReadOnly(); |
| 93 GrpcPreconditions.CheckArgument(this.keyCertificatePairs.Count > 0, |
| 94 "At least one KeyCertificatePair needs to be provided."); |
| 95 if (forceClientAuth) |
| 96 { |
| 97 GrpcPreconditions.CheckNotNull(rootCertificates, |
| 98 "Cannot force client authentication unless you provide rootC
ertificates."); |
| 99 } |
| 100 this.rootCertificates = rootCertificates; |
| 101 this.forceClientAuth = forceClientAuth; |
| 102 } |
| 103 |
| 104 /// <summary> |
| 105 /// Creates server-side SSL credentials. |
| 106 /// This constructor should be use if you do not wish to autheticate cli
ent |
| 107 /// using client root certificates. |
| 108 /// </summary> |
| 109 /// <param name="keyCertificatePairs">Key-certificates to use.</param> |
| 110 public SslServerCredentials(IEnumerable<KeyCertificatePair> keyCertifica
tePairs) : this(keyCertificatePairs, null, false) |
| 111 { |
| 112 } |
| 113 |
| 114 /// <summary> |
| 115 /// Key-certificate pairs. |
| 116 /// </summary> |
| 117 public IList<KeyCertificatePair> KeyCertificatePairs |
| 118 { |
| 119 get |
| 120 { |
| 121 return this.keyCertificatePairs; |
| 122 } |
| 123 } |
| 124 |
| 125 /// <summary> |
| 126 /// PEM encoded client root certificates. |
| 127 /// </summary> |
| 128 public string RootCertificates |
| 129 { |
| 130 get |
| 131 { |
| 132 return this.rootCertificates; |
| 133 } |
| 134 } |
| 135 |
| 136 /// <summary> |
| 137 /// If true, the authenticity of client check will be enforced. |
| 138 /// </summary> |
| 139 public bool ForceClientAuthentication |
| 140 { |
| 141 get |
| 142 { |
| 143 return this.forceClientAuth; |
| 144 } |
| 145 } |
| 146 |
| 147 internal override ServerCredentialsSafeHandle ToNativeCredentials() |
| 148 { |
| 149 int count = keyCertificatePairs.Count; |
| 150 string[] certChains = new string[count]; |
| 151 string[] keys = new string[count]; |
| 152 for (int i = 0; i < count; i++) |
| 153 { |
| 154 certChains[i] = keyCertificatePairs[i].CertificateChain; |
| 155 keys[i] = keyCertificatePairs[i].PrivateKey; |
| 156 } |
| 157 return ServerCredentialsSafeHandle.CreateSslCredentials(rootCertific
ates, certChains, keys, forceClientAuth); |
| 158 } |
| 159 } |
| 160 } |
OLD | NEW |