blob: 4fcac0c4c00e97d6c5c25390ee1027c8912e8c0a [file] [log] [blame]
Jan Tattermusch15329232015-03-02 15:32:47 -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 Grpc.Core.Internal;
36
37namespace Grpc.Core
38{
Jan Tattermusch286975f2015-03-12 14:04:36 -070039 /// <summary>
Jan Tattermuscha5272b62015-04-30 11:56:46 -070040 /// Client-side credentials. Used for creation of a secure channel.
Jan Tattermusch286975f2015-03-12 14:04:36 -070041 /// </summary>
Jan Tattermusch15329232015-03-02 15:32:47 -080042 public abstract class Credentials
43 {
Jan Tattermuscha96ac052015-07-24 14:49:30 -070044 static readonly Credentials InsecureInstance = new InsecureCredentialsImpl();
45
Jan Tattermusch15329232015-03-02 15:32:47 -080046 /// <summary>
Jan Tattermuscha96ac052015-07-24 14:49:30 -070047 /// Returns instance of credential that provides no security and
48 /// will result in creating an unsecure channel with no encryption whatsoever.
49 /// </summary>
50 public static Credentials Insecure
51 {
52 get
53 {
54 return InsecureInstance;
55 }
56 }
57
58 /// <summary>
59 /// Creates native object for the credentials. May return null if insecure channel
60 /// should be created.
Jan Tattermusch15329232015-03-02 15:32:47 -080061 /// </summary>
62 /// <returns>The native credentials.</returns>
63 internal abstract CredentialsSafeHandle ToNativeCredentials();
Jan Tattermuscha96ac052015-07-24 14:49:30 -070064
65 private sealed class InsecureCredentialsImpl : Credentials
66 {
67 internal override CredentialsSafeHandle ToNativeCredentials()
68 {
69 return null;
70 }
71 }
Jan Tattermusch15329232015-03-02 15:32:47 -080072 }
73
74 /// <summary>
75 /// Client-side SSL credentials.
76 /// </summary>
Jan Tattermuscha96ac052015-07-24 14:49:30 -070077 public sealed class SslCredentials : Credentials
Jan Tattermusch15329232015-03-02 15:32:47 -080078 {
Jan Tattermusch88a9b322015-07-23 21:43:44 -070079 readonly string rootCertificates;
80 readonly KeyCertificatePair keyCertificatePair;
Jan Tattermusch15329232015-03-02 15:32:47 -080081
Jan Tattermusch88a9b322015-07-23 21:43:44 -070082 /// <summary>
83 /// Creates client-side SSL credentials loaded from
84 /// disk file pointed to by the GRPC_DEFAULT_SSL_ROOTS_FILE_PATH environment variable.
85 /// If that fails, gets the roots certificates from a well known place on disk.
86 /// </summary>
87 public SslCredentials() : this(null, null)
Jan Tattermusch15329232015-03-02 15:32:47 -080088 {
Jan Tattermusch88a9b322015-07-23 21:43:44 -070089 }
90
91 /// <summary>
92 /// Creates client-side SSL credentials from
93 /// a string containing PEM encoded root certificates.
94 /// </summary>
95 public SslCredentials(string rootCertificates) : this(rootCertificates, null)
96 {
97 }
98
99 /// <summary>
100 /// Creates client-side SSL credentials.
101 /// </summary>
102 /// <param name="rootCertificates">string containing PEM encoded server root certificates.</param>
103 /// <param name="keyCertificatePair">a key certificate pair.</param>
104 public SslCredentials(string rootCertificates, KeyCertificatePair keyCertificatePair)
105 {
106 this.rootCertificates = rootCertificates;
107 this.keyCertificatePair = keyCertificatePair;
Jan Tattermusch15329232015-03-02 15:32:47 -0800108 }
109
110 /// <summary>
111 /// PEM encoding of the server root certificates.
112 /// </summary>
Jan Tattermusch88a9b322015-07-23 21:43:44 -0700113 public string RootCertificates
Jan Tattermusch15329232015-03-02 15:32:47 -0800114 {
115 get
116 {
Jan Tattermusch88a9b322015-07-23 21:43:44 -0700117 return this.rootCertificates;
118 }
119 }
120
121 /// <summary>
122 /// Client side key and certificate pair.
123 /// If null, client will not use key and certificate pair.
124 /// </summary>
125 public KeyCertificatePair KeyCertificatePair
126 {
127 get
128 {
129 return this.keyCertificatePair;
Jan Tattermusch15329232015-03-02 15:32:47 -0800130 }
131 }
132
133 internal override CredentialsSafeHandle ToNativeCredentials()
134 {
Jan Tattermusch88a9b322015-07-23 21:43:44 -0700135 return CredentialsSafeHandle.CreateSslCredentials(rootCertificates, keyCertificatePair);
Jan Tattermusch15329232015-03-02 15:32:47 -0800136 }
137 }
138}