Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 1 | #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 | |
| 34 | using System; |
| 35 | using Grpc.Core.Internal; |
| 36 | |
| 37 | namespace Grpc.Core |
| 38 | { |
Jan Tattermusch | 286975f | 2015-03-12 14:04:36 -0700 | [diff] [blame] | 39 | /// <summary> |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 40 | /// Client-side credentials. Used for creation of a secure channel. |
Jan Tattermusch | 286975f | 2015-03-12 14:04:36 -0700 | [diff] [blame] | 41 | /// </summary> |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 42 | public abstract class Credentials |
| 43 | { |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 44 | static readonly Credentials InsecureInstance = new InsecureCredentialsImpl(); |
| 45 | |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 46 | /// <summary> |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 47 | /// 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 Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 61 | /// </summary> |
| 62 | /// <returns>The native credentials.</returns> |
| 63 | internal abstract CredentialsSafeHandle ToNativeCredentials(); |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 64 | |
| 65 | private sealed class InsecureCredentialsImpl : Credentials |
| 66 | { |
| 67 | internal override CredentialsSafeHandle ToNativeCredentials() |
| 68 | { |
| 69 | return null; |
| 70 | } |
| 71 | } |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /// <summary> |
| 75 | /// Client-side SSL credentials. |
| 76 | /// </summary> |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 77 | public sealed class SslCredentials : Credentials |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 78 | { |
Jan Tattermusch | 88a9b32 | 2015-07-23 21:43:44 -0700 | [diff] [blame] | 79 | readonly string rootCertificates; |
| 80 | readonly KeyCertificatePair keyCertificatePair; |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 81 | |
Jan Tattermusch | 88a9b32 | 2015-07-23 21:43:44 -0700 | [diff] [blame] | 82 | /// <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 Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 88 | { |
Jan Tattermusch | 88a9b32 | 2015-07-23 21:43:44 -0700 | [diff] [blame] | 89 | } |
| 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 Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /// <summary> |
| 111 | /// PEM encoding of the server root certificates. |
| 112 | /// </summary> |
Jan Tattermusch | 88a9b32 | 2015-07-23 21:43:44 -0700 | [diff] [blame] | 113 | public string RootCertificates |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 114 | { |
| 115 | get |
| 116 | { |
Jan Tattermusch | 88a9b32 | 2015-07-23 21:43:44 -0700 | [diff] [blame] | 117 | 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 Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | internal override CredentialsSafeHandle ToNativeCredentials() |
| 134 | { |
Jan Tattermusch | 88a9b32 | 2015-07-23 21:43:44 -0700 | [diff] [blame] | 135 | return CredentialsSafeHandle.CreateSslCredentials(rootCertificates, keyCertificatePair); |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | } |