blob: 32ed4b78a1ff17d4b7ce73b8897b246ebad62516 [file] [log] [blame]
Jan Tattermuschb0829eb2015-03-03 09:30:55 -08001#region Copyright notice and license
2
3// Copyright 2015, 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
34using System;
35using System.Collections.Generic;
Jan Tattermusch286975f2015-03-12 14:04:36 -070036using System.Collections.Immutable;
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080037using Grpc.Core.Internal;
Jan Tattermusch88a9b322015-07-23 21:43:44 -070038using Grpc.Core.Utils;
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080039
40namespace Grpc.Core
41{
Jan Tattermusch286975f2015-03-12 14:04:36 -070042 /// <summary>
43 /// Server side credentials.
44 /// </summary>
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080045 public abstract class ServerCredentials
46 {
Jan Tattermuscha96ac052015-07-24 14:49:30 -070047 static readonly ServerCredentials InsecureInstance = new InsecureServerCredentialsImpl();
48
49 /// <summary>
50 /// Returns instance of credential that provides no security and
51 /// will result in creating an unsecure server port with no encryption whatsoever.
52 /// </summary>
53 public static ServerCredentials Insecure
54 {
55 get
56 {
57 return InsecureInstance;
58 }
59 }
60
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080061 /// <summary>
62 /// Creates native object for the credentials.
63 /// </summary>
64 /// <returns>The native credentials.</returns>
65 internal abstract ServerCredentialsSafeHandle ToNativeCredentials();
Jan Tattermuscha96ac052015-07-24 14:49:30 -070066
67 private sealed class InsecureServerCredentialsImpl : ServerCredentials
68 {
69 internal override ServerCredentialsSafeHandle ToNativeCredentials()
70 {
71 return null;
72 }
73 }
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080074 }
75
76 /// <summary>
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080077 /// Server-side SSL credentials.
78 /// </summary>
79 public class SslServerCredentials : ServerCredentials
80 {
Jan Tattermusch88a9b322015-07-23 21:43:44 -070081 readonly IList<KeyCertificatePair> keyCertificatePairs;
82 readonly string rootCertificates;
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080083
Jan Tattermusch88a9b322015-07-23 21:43:44 -070084 /// <summary>
85 /// Creates server-side SSL credentials.
86 /// </summary>
87 /// <param name="rootCertificates">PEM encoded client root certificates used to authenticate client.</param>
88 /// <param name="keyCertificatePairs">Key-certificates to use.</param>
89 public SslServerCredentials(IEnumerable<KeyCertificatePair> keyCertificatePairs, string rootCertificates)
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080090 {
Jan Tattermusch88a9b322015-07-23 21:43:44 -070091 this.keyCertificatePairs = new List<KeyCertificatePair>(keyCertificatePairs).AsReadOnly();
Jan Tattermuscheea59552015-07-23 22:05:32 -070092 Preconditions.CheckArgument(this.keyCertificatePairs.Count > 0,
Jan Tattermusch88a9b322015-07-23 21:43:44 -070093 "At least one KeyCertificatePair needs to be provided");
Jan Tattermuscheea59552015-07-23 22:05:32 -070094 this.rootCertificates = rootCertificates;
Jan Tattermusch88a9b322015-07-23 21:43:44 -070095 }
96
97 /// <summary>
98 /// Creates server-side SSL credentials.
99 /// This constructor should be use if you do not wish to autheticate client
100 /// using client root certificates.
101 /// </summary>
102 /// <param name="keyCertificatePairs">Key-certificates to use.</param>
103 public SslServerCredentials(IEnumerable<KeyCertificatePair> keyCertificatePairs) : this(keyCertificatePairs, null)
104 {
105 }
106
107 /// <summary>
108 /// Key-certificate pairs.
109 /// </summary>
110 public IList<KeyCertificatePair> KeyCertificatePairs
111 {
112 get
113 {
114 return this.keyCertificatePairs;
115 }
116 }
117
118 /// <summary>
119 /// PEM encoded client root certificates.
120 /// </summary>
121 public string RootCertificates
122 {
123 get
124 {
125 return this.rootCertificates;
126 }
Jan Tattermuschb0829eb2015-03-03 09:30:55 -0800127 }
128
129 internal override ServerCredentialsSafeHandle ToNativeCredentials()
130 {
Jan Tattermusch88a9b322015-07-23 21:43:44 -0700131 int count = keyCertificatePairs.Count;
Jan Tattermuschb0829eb2015-03-03 09:30:55 -0800132 string[] certChains = new string[count];
133 string[] keys = new string[count];
134 for (int i = 0; i < count; i++)
135 {
Jan Tattermusch88a9b322015-07-23 21:43:44 -0700136 certChains[i] = keyCertificatePairs[i].CertificateChain;
137 keys[i] = keyCertificatePairs[i].PrivateKey;
Jan Tattermuschb0829eb2015-03-03 09:30:55 -0800138 }
Jan Tattermusch88a9b322015-07-23 21:43:44 -0700139 return ServerCredentialsSafeHandle.CreateSslCredentials(rootCertificates, certChains, keys);
Jan Tattermuschb0829eb2015-03-03 09:30:55 -0800140 }
141 }
142}