blob: ba482897d753e7f80d953acf79b1a9e1a1d5bbed [file] [log] [blame]
Jan Tattermusch15329232015-03-02 15:32:47 -08001#region Copyright notice and license
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003// Copyright 2015 gRPC authors.
Jan Tattermusch15329232015-03-02 15:32:47 -08004//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
Jan Tattermusch15329232015-03-02 15:32:47 -08008//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009// http://www.apache.org/licenses/LICENSE-2.0
Jan Tattermusch15329232015-03-02 15:32:47 -080010//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
Jan Tattermusch15329232015-03-02 15:32:47 -080016
17#endregion
18
19using System;
Jan Tattermusch5bd75d72015-09-08 10:55:20 -070020using System.Collections.Generic;
21using System.Threading.Tasks;
22
Jan Tattermusch15329232015-03-02 15:32:47 -080023using Grpc.Core.Internal;
Jan Tattermusch5bd75d72015-09-08 10:55:20 -070024using Grpc.Core.Utils;
Jan Tattermusch15329232015-03-02 15:32:47 -080025
26namespace Grpc.Core
27{
Jan Tattermusch286975f2015-03-12 14:04:36 -070028 /// <summary>
Jan Tattermusch5bd70052015-10-06 16:47:49 -070029 /// Client-side channel credentials. Used for creation of a secure channel.
Jan Tattermusch286975f2015-03-12 14:04:36 -070030 /// </summary>
Jan Tattermusch5bd70052015-10-06 16:47:49 -070031 public abstract class ChannelCredentials
Jan Tattermusch15329232015-03-02 15:32:47 -080032 {
Jan Tattermusch5bd70052015-10-06 16:47:49 -070033 static readonly ChannelCredentials InsecureInstance = new InsecureCredentialsImpl();
Jan Tattermuscha96ac052015-07-24 14:49:30 -070034
Jan Tattermusch15329232015-03-02 15:32:47 -080035 /// <summary>
Jan Tattermusch5bd70052015-10-06 16:47:49 -070036 /// Returns instance of credentials that provides no security and
Jan Tattermuscha96ac052015-07-24 14:49:30 -070037 /// will result in creating an unsecure channel with no encryption whatsoever.
38 /// </summary>
Jan Tattermusch5bd70052015-10-06 16:47:49 -070039 public static ChannelCredentials Insecure
Jan Tattermuscha96ac052015-07-24 14:49:30 -070040 {
41 get
42 {
43 return InsecureInstance;
44 }
45 }
46
47 /// <summary>
Jan Tattermusch5bd70052015-10-06 16:47:49 -070048 /// Creates a new instance of <c>ChannelCredentials</c> class by composing
49 /// given channel credentials with call credentials.
50 /// </summary>
51 /// <param name="channelCredentials">Channel credentials.</param>
52 /// <param name="callCredentials">Call credentials.</param>
53 /// <returns>The new composite <c>ChannelCredentials</c></returns>
54 public static ChannelCredentials Create(ChannelCredentials channelCredentials, CallCredentials callCredentials)
55 {
56 return new CompositeChannelCredentials(channelCredentials, callCredentials);
57 }
58
59 /// <summary>
Jan Tattermuscha96ac052015-07-24 14:49:30 -070060 /// Creates native object for the credentials. May return null if insecure channel
61 /// should be created.
Jan Tattermusch15329232015-03-02 15:32:47 -080062 /// </summary>
63 /// <returns>The native credentials.</returns>
Jan Tattermusch08dea322015-10-26 17:34:10 -070064 internal abstract ChannelCredentialsSafeHandle ToNativeCredentials();
Jan Tattermuscha96ac052015-07-24 14:49:30 -070065
Jan Tattermusch74f39e12015-09-23 20:14:56 -070066 /// <summary>
67 /// Returns <c>true</c> if this credential type allows being composed by <c>CompositeCredentials</c>.
68 /// </summary>
69 internal virtual bool IsComposable
70 {
Jan Tattermusch5bd70052015-10-06 16:47:49 -070071 get { return false; }
Jan Tattermusch74f39e12015-09-23 20:14:56 -070072 }
73
Jan Tattermusch5bd70052015-10-06 16:47:49 -070074 private sealed class InsecureCredentialsImpl : ChannelCredentials
Jan Tattermuscha96ac052015-07-24 14:49:30 -070075 {
Jan Tattermusch08dea322015-10-26 17:34:10 -070076 internal override ChannelCredentialsSafeHandle ToNativeCredentials()
Jan Tattermuscha96ac052015-07-24 14:49:30 -070077 {
78 return null;
79 }
80 }
Jan Tattermusch15329232015-03-02 15:32:47 -080081 }
82
83 /// <summary>
84 /// Client-side SSL credentials.
85 /// </summary>
Jan Tattermusch5bd70052015-10-06 16:47:49 -070086 public sealed class SslCredentials : ChannelCredentials
Jan Tattermusch15329232015-03-02 15:32:47 -080087 {
Jan Tattermusch88a9b322015-07-23 21:43:44 -070088 readonly string rootCertificates;
89 readonly KeyCertificatePair keyCertificatePair;
Jan Tattermusch15329232015-03-02 15:32:47 -080090
Jan Tattermusch88a9b322015-07-23 21:43:44 -070091 /// <summary>
92 /// Creates client-side SSL credentials loaded from
93 /// disk file pointed to by the GRPC_DEFAULT_SSL_ROOTS_FILE_PATH environment variable.
94 /// If that fails, gets the roots certificates from a well known place on disk.
95 /// </summary>
96 public SslCredentials() : this(null, null)
Jan Tattermusch15329232015-03-02 15:32:47 -080097 {
Jan Tattermusch88a9b322015-07-23 21:43:44 -070098 }
99
100 /// <summary>
101 /// Creates client-side SSL credentials from
102 /// a string containing PEM encoded root certificates.
103 /// </summary>
104 public SslCredentials(string rootCertificates) : this(rootCertificates, null)
105 {
106 }
107
108 /// <summary>
109 /// Creates client-side SSL credentials.
110 /// </summary>
111 /// <param name="rootCertificates">string containing PEM encoded server root certificates.</param>
112 /// <param name="keyCertificatePair">a key certificate pair.</param>
113 public SslCredentials(string rootCertificates, KeyCertificatePair keyCertificatePair)
114 {
115 this.rootCertificates = rootCertificates;
116 this.keyCertificatePair = keyCertificatePair;
Jan Tattermusch15329232015-03-02 15:32:47 -0800117 }
118
119 /// <summary>
120 /// PEM encoding of the server root certificates.
121 /// </summary>
Jan Tattermusch88a9b322015-07-23 21:43:44 -0700122 public string RootCertificates
Jan Tattermusch15329232015-03-02 15:32:47 -0800123 {
124 get
125 {
Jan Tattermusch88a9b322015-07-23 21:43:44 -0700126 return this.rootCertificates;
127 }
128 }
129
130 /// <summary>
131 /// Client side key and certificate pair.
132 /// If null, client will not use key and certificate pair.
133 /// </summary>
134 public KeyCertificatePair KeyCertificatePair
135 {
136 get
137 {
138 return this.keyCertificatePair;
Jan Tattermusch15329232015-03-02 15:32:47 -0800139 }
140 }
141
Jan Tattermusch5bd70052015-10-06 16:47:49 -0700142 // Composing composite makes no sense.
143 internal override bool IsComposable
144 {
145 get { return true; }
146 }
147
Jan Tattermusch08dea322015-10-26 17:34:10 -0700148 internal override ChannelCredentialsSafeHandle ToNativeCredentials()
Jan Tattermusch15329232015-03-02 15:32:47 -0800149 {
Jan Tattermusch08dea322015-10-26 17:34:10 -0700150 return ChannelCredentialsSafeHandle.CreateSslCredentials(rootCertificates, keyCertificatePair);
Jan Tattermusch15329232015-03-02 15:32:47 -0800151 }
152 }
Jan Tattermusch5bd75d72015-09-08 10:55:20 -0700153
154 /// <summary>
Jan Tattermusch5bd70052015-10-06 16:47:49 -0700155 /// Credentials that allow composing one <see cref="ChannelCredentials"/> object and
156 /// one or more <see cref="CallCredentials"/> objects into a single <see cref="ChannelCredentials"/>.
Jan Tattermusch74f39e12015-09-23 20:14:56 -0700157 /// </summary>
Jan Tattermusch5bd70052015-10-06 16:47:49 -0700158 internal sealed class CompositeChannelCredentials : ChannelCredentials
Jan Tattermusch5bd75d72015-09-08 10:55:20 -0700159 {
Jan Tattermusch5bd70052015-10-06 16:47:49 -0700160 readonly ChannelCredentials channelCredentials;
161 readonly CallCredentials callCredentials;
Jan Tattermusch5bd75d72015-09-08 10:55:20 -0700162
Jan Tattermusch74f39e12015-09-23 20:14:56 -0700163 /// <summary>
Jan Tattermusch5bd70052015-10-06 16:47:49 -0700164 /// Initializes a new instance of <c>CompositeChannelCredentials</c> class.
Jan Tattermusch74f39e12015-09-23 20:14:56 -0700165 /// The resulting credentials object will be composite of all the credentials specified as parameters.
166 /// </summary>
Jan Tattermusch5bd70052015-10-06 16:47:49 -0700167 /// <param name="channelCredentials">channelCredentials to compose</param>
168 /// <param name="callCredentials">channelCredentials to compose</param>
169 public CompositeChannelCredentials(ChannelCredentials channelCredentials, CallCredentials callCredentials)
Jan Tattermusch5bd75d72015-09-08 10:55:20 -0700170 {
Jan Tattermusch7a3ee6a2016-02-18 10:36:02 -0800171 this.channelCredentials = GrpcPreconditions.CheckNotNull(channelCredentials);
172 this.callCredentials = GrpcPreconditions.CheckNotNull(callCredentials);
173 GrpcPreconditions.CheckArgument(channelCredentials.IsComposable, "Supplied channel credentials do not allow composition.");
Jan Tattermusch5bd75d72015-09-08 10:55:20 -0700174 }
175
Jan Tattermusch08dea322015-10-26 17:34:10 -0700176 internal override ChannelCredentialsSafeHandle ToNativeCredentials()
Jan Tattermusch5bd75d72015-09-08 10:55:20 -0700177 {
Jan Tattermusch08dea322015-10-26 17:34:10 -0700178 using (var channelCreds = channelCredentials.ToNativeCredentials())
179 using (var callCreds = callCredentials.ToNativeCredentials())
Jan Tattermusch74f39e12015-09-23 20:14:56 -0700180 {
Jan Tattermusch08dea322015-10-26 17:34:10 -0700181 var nativeComposite = ChannelCredentialsSafeHandle.CreateComposite(channelCreds, callCreds);
Jan Tattermusch74f39e12015-09-23 20:14:56 -0700182 if (nativeComposite.IsInvalid)
183 {
184 throw new ArgumentException("Error creating native composite credentials. Likely, this is because you are trying to compose incompatible credentials.");
185 }
186 return nativeComposite;
187 }
Jan Tattermusch5bd75d72015-09-08 10:55:20 -0700188 }
189 }
Jan Tattermusch15329232015-03-02 15:32:47 -0800190}