From 1532923d65684ddd006914f82af18d26a6a91659 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Mon, 2 Mar 2015 15:32:47 -0800 Subject: client side support for TLS --- src/csharp/Grpc.Core/Channel.cs | 55 +++++++--- src/csharp/Grpc.Core/ChannelArgs.cs | 112 +++++++++++++++++++++ src/csharp/Grpc.Core/Credentials.cs | 77 ++++++++++++++ src/csharp/Grpc.Core/Grpc.Core.csproj | 4 + .../Grpc.Core/Internal/ChannelArgsSafeHandle.cs | 77 ++++++++++++++ src/csharp/Grpc.Core/Internal/ChannelSafeHandle.cs | 35 ++++--- .../Grpc.Core/Internal/CredentialsSafeHandle.cs | 64 ++++++++++++ .../Grpc.IntegrationTesting.csproj | 13 +++ .../Grpc.IntegrationTesting/InteropClient.cs | 25 ++++- src/csharp/Grpc.IntegrationTesting/data/README | 1 + src/csharp/Grpc.IntegrationTesting/data/ca.pem | 15 +++ .../Grpc.IntegrationTesting/data/server1.key | 16 +++ .../Grpc.IntegrationTesting/data/server1.pem | 16 +++ src/csharp/ext/grpc_csharp_ext.c | 68 +++++++++++++ 14 files changed, 545 insertions(+), 33 deletions(-) create mode 100644 src/csharp/Grpc.Core/ChannelArgs.cs create mode 100644 src/csharp/Grpc.Core/Credentials.cs create mode 100644 src/csharp/Grpc.Core/Internal/ChannelArgsSafeHandle.cs create mode 100644 src/csharp/Grpc.Core/Internal/CredentialsSafeHandle.cs create mode 100644 src/csharp/Grpc.IntegrationTesting/data/README create mode 100644 src/csharp/Grpc.IntegrationTesting/data/ca.pem create mode 100644 src/csharp/Grpc.IntegrationTesting/data/server1.key create mode 100644 src/csharp/Grpc.IntegrationTesting/data/server1.pem (limited to 'src') diff --git a/src/csharp/Grpc.Core/Channel.cs b/src/csharp/Grpc.Core/Channel.cs index 942651cf39..83d965debf 100644 --- a/src/csharp/Grpc.Core/Channel.cs +++ b/src/csharp/Grpc.Core/Channel.cs @@ -1,5 +1,4 @@ #region Copyright notice and license - // Copyright 2015, Google Inc. // All rights reserved. // @@ -28,9 +27,7 @@ // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - #endregion - using System; using System.Runtime.InteropServices; using System.Threading; @@ -39,18 +36,32 @@ using Grpc.Core.Internal; namespace Grpc.Core { - public class Channel : IDisposable - { + public class Channel : IDisposable + { readonly ChannelSafeHandle handle; readonly String target; - // TODO: add way how to create grpc_secure_channel.... - // TODO: add support for channel args... - public Channel(string target) - { - this.handle = ChannelSafeHandle.Create(target, IntPtr.Zero); - this.target = target; - } + /// + /// Creates a channel. + /// + public Channel(string target, Credentials credentials = null, ChannelArgs channelArgs = null) + { + using (ChannelArgsSafeHandle nativeChannelArgs = CreateNativeChannelArgs(channelArgs)) + { + if (credentials != null) + { + using (CredentialsSafeHandle nativeCredentials = credentials.ToNativeCredentials()) + { + this.handle = ChannelSafeHandle.CreateSecure(nativeCredentials, target, nativeChannelArgs); + } + } + else + { + this.handle = ChannelSafeHandle.Create(target, nativeChannelArgs); + } + } + this.target = GetOverridenTarget(target, channelArgs); + } internal ChannelSafeHandle Handle { @@ -81,5 +92,23 @@ namespace Grpc.Core handle.Dispose(); } } - } + + private static string GetOverridenTarget(string target, ChannelArgs args) + { + if (args != null && !string.IsNullOrEmpty(args.GetSslTargetNameOverride())) + { + return args.GetSslTargetNameOverride(); + } + return target; + } + + private static ChannelArgsSafeHandle CreateNativeChannelArgs(ChannelArgs args) + { + if (args == null) + { + return ChannelArgsSafeHandle.CreateNull(); + } + return args.ToNativeChannelArgs(); + } + } } diff --git a/src/csharp/Grpc.Core/ChannelArgs.cs b/src/csharp/Grpc.Core/ChannelArgs.cs new file mode 100644 index 0000000000..653a5780a3 --- /dev/null +++ b/src/csharp/Grpc.Core/ChannelArgs.cs @@ -0,0 +1,112 @@ +#region Copyright notice and license +// Copyright 2015, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#endregion +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Threading; +using System.Threading.Tasks; +using Grpc.Core.Internal; + +namespace Grpc.Core +{ + // TODO: should we be using the builder pattern? + public class ChannelArgs + { + public const string SslTargetNameOverrideKey = "grpc.ssl_target_name_override"; + + public class Builder + { + Dictionary stringArgs = new Dictionary(); + // TODO: AddInteger not supported yet. + public Builder AddString(string key, string value) + { + stringArgs.Add(key, value); + return this; + } + + public ChannelArgs Build() + { + return new ChannelArgs(stringArgs); + } + } + + Dictionary stringArgs; + + private ChannelArgs(Dictionary stringArgs) + { + // TODO: use immutable dict? + this.stringArgs = new Dictionary(stringArgs); + } + + public string GetSslTargetNameOverride() + { + string result; + if (stringArgs.TryGetValue(SslTargetNameOverrideKey, out result)) + { + return result; + } + return null; + } + + public static Builder NewBuilder() + { + return new Builder(); + } + + /// + /// Creates native object for the channel arguments. + /// + /// The native channel arguments. + internal ChannelArgsSafeHandle ToNativeChannelArgs() + { + ChannelArgsSafeHandle nativeArgs = null; + try + { + nativeArgs = ChannelArgsSafeHandle.Create(stringArgs.Count); + int i = 0; + foreach (var entry in stringArgs) + { + nativeArgs.SetString(i, entry.Key, entry.Value); + i++; + } + return nativeArgs; + } + catch (Exception e) + { + if (nativeArgs != null) + { + nativeArgs.Dispose(); + } + throw; + } + } + } +} diff --git a/src/csharp/Grpc.Core/Credentials.cs b/src/csharp/Grpc.Core/Credentials.cs new file mode 100644 index 0000000000..5116c277f7 --- /dev/null +++ b/src/csharp/Grpc.Core/Credentials.cs @@ -0,0 +1,77 @@ +#region Copyright notice and license + +// Copyright 2015, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#endregion + +using System; +using Grpc.Core.Internal; + +namespace Grpc.Core +{ + public abstract class Credentials + { + /// + /// Creates native object for the credentials. + /// + /// The native credentials. + internal abstract CredentialsSafeHandle ToNativeCredentials(); + } + + /// + /// Client-side SSL credentials. + /// + public class SslCredentials : Credentials + { + string pemRootCerts; + + public SslCredentials(string pemRootCerts) + { + this.pemRootCerts = pemRootCerts; + } + + /// + /// PEM encoding of the server root certificates. + /// + public string RootCerts + { + get + { + return this.pemRootCerts; + } + } + + internal override CredentialsSafeHandle ToNativeCredentials() + { + return CredentialsSafeHandle.CreateSslCredentials(pemRootCerts); + } + } +} + diff --git a/src/csharp/Grpc.Core/Grpc.Core.csproj b/src/csharp/Grpc.Core/Grpc.Core.csproj index 05d40d45a6..93d5430591 100644 --- a/src/csharp/Grpc.Core/Grpc.Core.csproj +++ b/src/csharp/Grpc.Core/Grpc.Core.csproj @@ -65,6 +65,10 @@ + + + +