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 System.Threading.Tasks; |
| 37 |
| 38 using Grpc.Core.Internal; |
| 39 using Grpc.Core.Utils; |
| 40 |
| 41 namespace Grpc.Core |
| 42 { |
| 43 /// <summary> |
| 44 /// Client-side channel credentials. Used for creation of a secure channel. |
| 45 /// </summary> |
| 46 public abstract class ChannelCredentials |
| 47 { |
| 48 static readonly ChannelCredentials InsecureInstance = new InsecureCreden
tialsImpl(); |
| 49 |
| 50 /// <summary> |
| 51 /// Returns instance of credentials that provides no security and |
| 52 /// will result in creating an unsecure channel with no encryption whats
oever. |
| 53 /// </summary> |
| 54 public static ChannelCredentials Insecure |
| 55 { |
| 56 get |
| 57 { |
| 58 return InsecureInstance; |
| 59 } |
| 60 } |
| 61 |
| 62 /// <summary> |
| 63 /// Creates a new instance of <c>ChannelCredentials</c> class by composi
ng |
| 64 /// given channel credentials with call credentials. |
| 65 /// </summary> |
| 66 /// <param name="channelCredentials">Channel credentials.</param> |
| 67 /// <param name="callCredentials">Call credentials.</param> |
| 68 /// <returns>The new composite <c>ChannelCredentials</c></returns> |
| 69 public static ChannelCredentials Create(ChannelCredentials channelCreden
tials, CallCredentials callCredentials) |
| 70 { |
| 71 return new CompositeChannelCredentials(channelCredentials, callCrede
ntials); |
| 72 } |
| 73 |
| 74 /// <summary> |
| 75 /// Creates native object for the credentials. May return null if insecu
re channel |
| 76 /// should be created. |
| 77 /// </summary> |
| 78 /// <returns>The native credentials.</returns> |
| 79 internal abstract ChannelCredentialsSafeHandle ToNativeCredentials(); |
| 80 |
| 81 /// <summary> |
| 82 /// Returns <c>true</c> if this credential type allows being composed by
<c>CompositeCredentials</c>. |
| 83 /// </summary> |
| 84 internal virtual bool IsComposable |
| 85 { |
| 86 get { return false; } |
| 87 } |
| 88 |
| 89 private sealed class InsecureCredentialsImpl : ChannelCredentials |
| 90 { |
| 91 internal override ChannelCredentialsSafeHandle ToNativeCredentials() |
| 92 { |
| 93 return null; |
| 94 } |
| 95 } |
| 96 } |
| 97 |
| 98 /// <summary> |
| 99 /// Client-side SSL credentials. |
| 100 /// </summary> |
| 101 public sealed class SslCredentials : ChannelCredentials |
| 102 { |
| 103 readonly string rootCertificates; |
| 104 readonly KeyCertificatePair keyCertificatePair; |
| 105 |
| 106 /// <summary> |
| 107 /// Creates client-side SSL credentials loaded from |
| 108 /// disk file pointed to by the GRPC_DEFAULT_SSL_ROOTS_FILE_PATH environ
ment variable. |
| 109 /// If that fails, gets the roots certificates from a well known place o
n disk. |
| 110 /// </summary> |
| 111 public SslCredentials() : this(null, null) |
| 112 { |
| 113 } |
| 114 |
| 115 /// <summary> |
| 116 /// Creates client-side SSL credentials from |
| 117 /// a string containing PEM encoded root certificates. |
| 118 /// </summary> |
| 119 public SslCredentials(string rootCertificates) : this(rootCertificates,
null) |
| 120 { |
| 121 } |
| 122 |
| 123 /// <summary> |
| 124 /// Creates client-side SSL credentials. |
| 125 /// </summary> |
| 126 /// <param name="rootCertificates">string containing PEM encoded server
root certificates.</param> |
| 127 /// <param name="keyCertificatePair">a key certificate pair.</param> |
| 128 public SslCredentials(string rootCertificates, KeyCertificatePair keyCer
tificatePair) |
| 129 { |
| 130 this.rootCertificates = rootCertificates; |
| 131 this.keyCertificatePair = keyCertificatePair; |
| 132 } |
| 133 |
| 134 /// <summary> |
| 135 /// PEM encoding of the server root certificates. |
| 136 /// </summary> |
| 137 public string RootCertificates |
| 138 { |
| 139 get |
| 140 { |
| 141 return this.rootCertificates; |
| 142 } |
| 143 } |
| 144 |
| 145 /// <summary> |
| 146 /// Client side key and certificate pair. |
| 147 /// If null, client will not use key and certificate pair. |
| 148 /// </summary> |
| 149 public KeyCertificatePair KeyCertificatePair |
| 150 { |
| 151 get |
| 152 { |
| 153 return this.keyCertificatePair; |
| 154 } |
| 155 } |
| 156 |
| 157 // Composing composite makes no sense. |
| 158 internal override bool IsComposable |
| 159 { |
| 160 get { return true; } |
| 161 } |
| 162 |
| 163 internal override ChannelCredentialsSafeHandle ToNativeCredentials() |
| 164 { |
| 165 return ChannelCredentialsSafeHandle.CreateSslCredentials(rootCertifi
cates, keyCertificatePair); |
| 166 } |
| 167 } |
| 168 |
| 169 /// <summary> |
| 170 /// Credentials that allow composing one <see cref="ChannelCredentials"/> ob
ject and |
| 171 /// one or more <see cref="CallCredentials"/> objects into a single <see cre
f="ChannelCredentials"/>. |
| 172 /// </summary> |
| 173 internal sealed class CompositeChannelCredentials : ChannelCredentials |
| 174 { |
| 175 readonly ChannelCredentials channelCredentials; |
| 176 readonly CallCredentials callCredentials; |
| 177 |
| 178 /// <summary> |
| 179 /// Initializes a new instance of <c>CompositeChannelCredentials</c> cla
ss. |
| 180 /// The resulting credentials object will be composite of all the creden
tials specified as parameters. |
| 181 /// </summary> |
| 182 /// <param name="channelCredentials">channelCredentials to compose</para
m> |
| 183 /// <param name="callCredentials">channelCredentials to compose</param> |
| 184 public CompositeChannelCredentials(ChannelCredentials channelCredentials
, CallCredentials callCredentials) |
| 185 { |
| 186 this.channelCredentials = GrpcPreconditions.CheckNotNull(channelCred
entials); |
| 187 this.callCredentials = GrpcPreconditions.CheckNotNull(callCredential
s); |
| 188 GrpcPreconditions.CheckArgument(channelCredentials.IsComposable, "Su
pplied channel credentials do not allow composition."); |
| 189 } |
| 190 |
| 191 internal override ChannelCredentialsSafeHandle ToNativeCredentials() |
| 192 { |
| 193 using (var channelCreds = channelCredentials.ToNativeCredentials()) |
| 194 using (var callCreds = callCredentials.ToNativeCredentials()) |
| 195 { |
| 196 var nativeComposite = ChannelCredentialsSafeHandle.CreateComposi
te(channelCreds, callCreds); |
| 197 if (nativeComposite.IsInvalid) |
| 198 { |
| 199 throw new ArgumentException("Error creating native composite
credentials. Likely, this is because you are trying to compose incompatible cre
dentials."); |
| 200 } |
| 201 return nativeComposite; |
| 202 } |
| 203 } |
| 204 } |
| 205 } |
OLD | NEW |