Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -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 System.Collections.Generic; |
Jan Tattermusch | 286975f | 2015-03-12 14:04:36 -0700 | [diff] [blame] | 36 | using System.Collections.Immutable; |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 37 | using Grpc.Core.Internal; |
Jan Tattermusch | 88a9b32 | 2015-07-23 21:43:44 -0700 | [diff] [blame] | 38 | using Grpc.Core.Utils; |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 39 | |
| 40 | namespace Grpc.Core |
| 41 | { |
Jan Tattermusch | 286975f | 2015-03-12 14:04:36 -0700 | [diff] [blame] | 42 | /// <summary> |
| 43 | /// Server side credentials. |
| 44 | /// </summary> |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 45 | public abstract class ServerCredentials |
| 46 | { |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 47 | 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 Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 61 | /// <summary> |
| 62 | /// Creates native object for the credentials. |
| 63 | /// </summary> |
| 64 | /// <returns>The native credentials.</returns> |
| 65 | internal abstract ServerCredentialsSafeHandle ToNativeCredentials(); |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 66 | |
| 67 | private sealed class InsecureServerCredentialsImpl : ServerCredentials |
| 68 | { |
| 69 | internal override ServerCredentialsSafeHandle ToNativeCredentials() |
| 70 | { |
| 71 | return null; |
| 72 | } |
| 73 | } |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /// <summary> |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 77 | /// Server-side SSL credentials. |
| 78 | /// </summary> |
| 79 | public class SslServerCredentials : ServerCredentials |
| 80 | { |
Jan Tattermusch | 88a9b32 | 2015-07-23 21:43:44 -0700 | [diff] [blame] | 81 | readonly IList<KeyCertificatePair> keyCertificatePairs; |
| 82 | readonly string rootCertificates; |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 83 | |
Jan Tattermusch | 88a9b32 | 2015-07-23 21:43:44 -0700 | [diff] [blame] | 84 | /// <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 Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 90 | { |
Jan Tattermusch | 88a9b32 | 2015-07-23 21:43:44 -0700 | [diff] [blame] | 91 | this.keyCertificatePairs = new List<KeyCertificatePair>(keyCertificatePairs).AsReadOnly(); |
Jan Tattermusch | eea5955 | 2015-07-23 22:05:32 -0700 | [diff] [blame] | 92 | Preconditions.CheckArgument(this.keyCertificatePairs.Count > 0, |
Jan Tattermusch | 88a9b32 | 2015-07-23 21:43:44 -0700 | [diff] [blame] | 93 | "At least one KeyCertificatePair needs to be provided"); |
Jan Tattermusch | eea5955 | 2015-07-23 22:05:32 -0700 | [diff] [blame] | 94 | this.rootCertificates = rootCertificates; |
Jan Tattermusch | 88a9b32 | 2015-07-23 21:43:44 -0700 | [diff] [blame] | 95 | } |
| 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 Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | internal override ServerCredentialsSafeHandle ToNativeCredentials() |
| 130 | { |
Jan Tattermusch | 88a9b32 | 2015-07-23 21:43:44 -0700 | [diff] [blame] | 131 | int count = keyCertificatePairs.Count; |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 132 | string[] certChains = new string[count]; |
| 133 | string[] keys = new string[count]; |
| 134 | for (int i = 0; i < count; i++) |
| 135 | { |
Jan Tattermusch | 88a9b32 | 2015-07-23 21:43:44 -0700 | [diff] [blame] | 136 | certChains[i] = keyCertificatePairs[i].CertificateChain; |
| 137 | keys[i] = keyCertificatePairs[i].PrivateKey; |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 138 | } |
Jan Tattermusch | 88a9b32 | 2015-07-23 21:43:44 -0700 | [diff] [blame] | 139 | return ServerCredentialsSafeHandle.CreateSslCredentials(rootCertificates, certChains, keys); |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | } |