blob: 703f9ff6b32d7bdeb00e0a9f38718ae3d4bc7799 [file] [log] [blame]
Jan Tattermuschb0829eb2015-03-03 09:30:55 -08001#region Copyright notice and license
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003// Copyright 2015 gRPC authors.
Jan Tattermuschb0829eb2015-03-03 09:30:55 -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 Tattermuschb0829eb2015-03-03 09:30:55 -08008//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009// http://www.apache.org/licenses/LICENSE-2.0
Jan Tattermuschb0829eb2015-03-03 09:30:55 -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 Tattermuschb0829eb2015-03-03 09:30:55 -080016
17#endregion
18
19using System;
20using System.Collections.Generic;
21using Grpc.Core.Internal;
Jan Tattermusch88a9b322015-07-23 21:43:44 -070022using Grpc.Core.Utils;
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080023
24namespace Grpc.Core
25{
Jan Tattermusch286975f2015-03-12 14:04:36 -070026 /// <summary>
27 /// Server side credentials.
28 /// </summary>
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080029 public abstract class ServerCredentials
30 {
Jan Tattermuscha96ac052015-07-24 14:49:30 -070031 static readonly ServerCredentials InsecureInstance = new InsecureServerCredentialsImpl();
32
33 /// <summary>
34 /// Returns instance of credential that provides no security and
35 /// will result in creating an unsecure server port with no encryption whatsoever.
36 /// </summary>
37 public static ServerCredentials Insecure
38 {
39 get
40 {
41 return InsecureInstance;
42 }
43 }
44
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080045 /// <summary>
46 /// Creates native object for the credentials.
47 /// </summary>
48 /// <returns>The native credentials.</returns>
49 internal abstract ServerCredentialsSafeHandle ToNativeCredentials();
Jan Tattermuscha96ac052015-07-24 14:49:30 -070050
51 private sealed class InsecureServerCredentialsImpl : ServerCredentials
52 {
53 internal override ServerCredentialsSafeHandle ToNativeCredentials()
54 {
55 return null;
56 }
57 }
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080058 }
59
60 /// <summary>
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080061 /// Server-side SSL credentials.
62 /// </summary>
63 public class SslServerCredentials : ServerCredentials
64 {
Jan Tattermusch88a9b322015-07-23 21:43:44 -070065 readonly IList<KeyCertificatePair> keyCertificatePairs;
66 readonly string rootCertificates;
Jan Tattermuschd27dfa72015-08-04 18:10:54 -070067 readonly bool forceClientAuth;
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080068
Jan Tattermusch88a9b322015-07-23 21:43:44 -070069 /// <summary>
70 /// Creates server-side SSL credentials.
71 /// </summary>
Jan Tattermusch88a9b322015-07-23 21:43:44 -070072 /// <param name="keyCertificatePairs">Key-certificates to use.</param>
Jan Tattermuschd27dfa72015-08-04 18:10:54 -070073 /// <param name="rootCertificates">PEM encoded client root certificates used to authenticate client.</param>
74 /// <param name="forceClientAuth">If true, client will be rejected unless it proves its unthenticity using against rootCertificates.</param>
75 public SslServerCredentials(IEnumerable<KeyCertificatePair> keyCertificatePairs, string rootCertificates, bool forceClientAuth)
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080076 {
Jan Tattermusch88a9b322015-07-23 21:43:44 -070077 this.keyCertificatePairs = new List<KeyCertificatePair>(keyCertificatePairs).AsReadOnly();
Jan Tattermusch7a3ee6a2016-02-18 10:36:02 -080078 GrpcPreconditions.CheckArgument(this.keyCertificatePairs.Count > 0,
Jan Tattermusch39a9ec82015-08-09 15:37:23 -070079 "At least one KeyCertificatePair needs to be provided.");
Jan Tattermuschd27dfa72015-08-04 18:10:54 -070080 if (forceClientAuth)
81 {
Jan Tattermusch7a3ee6a2016-02-18 10:36:02 -080082 GrpcPreconditions.CheckNotNull(rootCertificates,
Jan Tattermuschd27dfa72015-08-04 18:10:54 -070083 "Cannot force client authentication unless you provide rootCertificates.");
84 }
Jan Tattermuscheea59552015-07-23 22:05:32 -070085 this.rootCertificates = rootCertificates;
Jan Tattermuschd27dfa72015-08-04 18:10:54 -070086 this.forceClientAuth = forceClientAuth;
Jan Tattermusch88a9b322015-07-23 21:43:44 -070087 }
88
89 /// <summary>
90 /// Creates server-side SSL credentials.
91 /// This constructor should be use if you do not wish to autheticate client
92 /// using client root certificates.
93 /// </summary>
94 /// <param name="keyCertificatePairs">Key-certificates to use.</param>
Jan Tattermuschd27dfa72015-08-04 18:10:54 -070095 public SslServerCredentials(IEnumerable<KeyCertificatePair> keyCertificatePairs) : this(keyCertificatePairs, null, false)
Jan Tattermusch88a9b322015-07-23 21:43:44 -070096 {
97 }
98
99 /// <summary>
100 /// Key-certificate pairs.
101 /// </summary>
102 public IList<KeyCertificatePair> KeyCertificatePairs
103 {
104 get
105 {
106 return this.keyCertificatePairs;
107 }
108 }
109
110 /// <summary>
111 /// PEM encoded client root certificates.
112 /// </summary>
113 public string RootCertificates
114 {
115 get
116 {
117 return this.rootCertificates;
118 }
Jan Tattermuschb0829eb2015-03-03 09:30:55 -0800119 }
120
Jan Tattermuschd27dfa72015-08-04 18:10:54 -0700121 /// <summary>
122 /// If true, the authenticity of client check will be enforced.
123 /// </summary>
124 public bool ForceClientAuthentication
125 {
126 get
127 {
128 return this.forceClientAuth;
129 }
130 }
131
Jan Tattermuschb0829eb2015-03-03 09:30:55 -0800132 internal override ServerCredentialsSafeHandle ToNativeCredentials()
133 {
Jan Tattermusch88a9b322015-07-23 21:43:44 -0700134 int count = keyCertificatePairs.Count;
Jan Tattermuschb0829eb2015-03-03 09:30:55 -0800135 string[] certChains = new string[count];
136 string[] keys = new string[count];
137 for (int i = 0; i < count; i++)
138 {
Jan Tattermusch88a9b322015-07-23 21:43:44 -0700139 certChains[i] = keyCertificatePairs[i].CertificateChain;
140 keys[i] = keyCertificatePairs[i].PrivateKey;
Jan Tattermuschb0829eb2015-03-03 09:30:55 -0800141 }
Jan Tattermuschd27dfa72015-08-04 18:10:54 -0700142 return ServerCredentialsSafeHandle.CreateSslCredentials(rootCertificates, certChains, keys, forceClientAuth);
Jan Tattermuschb0829eb2015-03-03 09:30:55 -0800143 }
144 }
145}