diff options
author | 2015-03-02 15:32:47 -0800 | |
---|---|---|
committer | 2015-03-03 09:56:47 -0800 | |
commit | 1532923d65684ddd006914f82af18d26a6a91659 (patch) | |
tree | 866ca1c1584cc46a5ce6ba4d5d28d194d62b4420 /src/csharp/Grpc.Core | |
parent | d318bba527710711d480114e8713a41ef07322a0 (diff) |
client side support for TLS
Diffstat (limited to 'src/csharp/Grpc.Core')
-rw-r--r-- | src/csharp/Grpc.Core/Channel.cs | 55 | ||||
-rw-r--r-- | src/csharp/Grpc.Core/ChannelArgs.cs | 112 | ||||
-rw-r--r-- | src/csharp/Grpc.Core/Credentials.cs | 77 | ||||
-rw-r--r-- | src/csharp/Grpc.Core/Grpc.Core.csproj | 4 | ||||
-rw-r--r-- | src/csharp/Grpc.Core/Internal/ChannelArgsSafeHandle.cs | 77 | ||||
-rw-r--r-- | src/csharp/Grpc.Core/Internal/ChannelSafeHandle.cs | 35 | ||||
-rw-r--r-- | src/csharp/Grpc.Core/Internal/CredentialsSafeHandle.cs | 64 |
7 files changed, 396 insertions, 28 deletions
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; - } + /// <summary> + /// Creates a channel. + /// </summary> + 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<string,string> stringArgs = new Dictionary<string,string>(); + // 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<string,string> stringArgs; + + private ChannelArgs(Dictionary<string, string> stringArgs) + { + // TODO: use immutable dict? + this.stringArgs = new Dictionary<string, string>(stringArgs); + } + + public string GetSslTargetNameOverride() + { + string result; + if (stringArgs.TryGetValue(SslTargetNameOverrideKey, out result)) + { + return result; + } + return null; + } + + public static Builder NewBuilder() + { + return new Builder(); + } + + /// <summary> + /// Creates native object for the channel arguments. + /// </summary> + /// <returns>The native channel arguments.</returns> + 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 + { + /// <summary> + /// Creates native object for the credentials. + /// </summary> + /// <returns>The native credentials.</returns> + internal abstract CredentialsSafeHandle ToNativeCredentials(); + } + + /// <summary> + /// Client-side SSL credentials. + /// </summary> + public class SslCredentials : Credentials + { + string pemRootCerts; + + public SslCredentials(string pemRootCerts) + { + this.pemRootCerts = pemRootCerts; + } + + /// <summary> + /// PEM encoding of the server root certificates. + /// </summary> + 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 @@ <Compile Include="Internal\BatchContextSafeHandleNotOwned.cs" /> <Compile Include="Utils\BenchmarkUtil.cs" /> <Compile Include="Utils\ExceptionHelper.cs" /> + <Compile Include="Internal\CredentialsSafeHandle.cs" /> + <Compile Include="Credentials.cs" /> + <Compile Include="Internal\ChannelArgsSafeHandle.cs" /> + <Compile Include="ChannelArgs.cs" /> </ItemGroup> <Choose> <!-- Under older versions of Monodevelop, Choose is not supported and is just diff --git a/src/csharp/Grpc.Core/Internal/ChannelArgsSafeHandle.cs b/src/csharp/Grpc.Core/Internal/ChannelArgsSafeHandle.cs new file mode 100644 index 0000000000..ca3c21d84c --- /dev/null +++ b/src/csharp/Grpc.Core/Internal/ChannelArgsSafeHandle.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 System.Runtime.InteropServices; +using System.Threading.Tasks; + +namespace Grpc.Core.Internal +{ + /// <summary> + /// grpc_channel_args from <grpc/grpc.h> + /// </summary> + internal class ChannelArgsSafeHandle : SafeHandleZeroIsInvalid + { + [DllImport("grpc_csharp_ext.dll")] + static extern ChannelArgsSafeHandle grpcsharp_channel_args_create(UIntPtr numArgs); + + [DllImport("grpc_csharp_ext.dll", CharSet = CharSet.Ansi)] + static extern void grpcsharp_channel_args_set_string(ChannelArgsSafeHandle args, UIntPtr index, string key, string value); + + [DllImport("grpc_csharp_ext.dll")] + static extern void grpcsharp_channel_args_destroy(IntPtr args); + + private ChannelArgsSafeHandle() + { + } + + public static ChannelArgsSafeHandle CreateNull() + { + return new ChannelArgsSafeHandle(); + } + + public static ChannelArgsSafeHandle Create(int size) + { + return grpcsharp_channel_args_create(new UIntPtr((uint)size)); + } + + public void SetString(int index, string key, string value) + { + grpcsharp_channel_args_set_string(this, new UIntPtr((uint)index), key, value); + } + + protected override bool ReleaseHandle() + { + grpcsharp_channel_args_destroy(handle); + return true; + } + } +} + diff --git a/src/csharp/Grpc.Core/Internal/ChannelSafeHandle.cs b/src/csharp/Grpc.Core/Internal/ChannelSafeHandle.cs index f15ead3572..f046f4c6d0 100644 --- a/src/csharp/Grpc.Core/Internal/ChannelSafeHandle.cs +++ b/src/csharp/Grpc.Core/Internal/ChannelSafeHandle.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; @@ -41,27 +38,35 @@ namespace Grpc.Core.Internal /// <summary> /// grpc_channel from <grpc/grpc.h> /// </summary> - internal class ChannelSafeHandle : SafeHandleZeroIsInvalid - { + internal class ChannelSafeHandle : SafeHandleZeroIsInvalid + { [DllImport("grpc_csharp_ext.dll")] - static extern ChannelSafeHandle grpcsharp_channel_create(string target, IntPtr channelArgs); + static extern ChannelSafeHandle grpcsharp_channel_create(string target, ChannelArgsSafeHandle channelArgs); - [DllImport("grpc_csharp_ext.dll")] - static extern void grpcsharp_channel_destroy(IntPtr channel); + [DllImport("grpc_csharp_ext.dll")] + static extern ChannelSafeHandle grpcsharp_secure_channel_create(CredentialsSafeHandle credentials, string target, ChannelArgsSafeHandle channelArgs); + + [DllImport("grpc_csharp_ext.dll")] + static extern void grpcsharp_channel_destroy(IntPtr channel); private ChannelSafeHandle() { } - public static ChannelSafeHandle Create(string target, IntPtr channelArgs) + public static ChannelSafeHandle Create(string target, ChannelArgsSafeHandle channelArgs) { return grpcsharp_channel_create(target, channelArgs); } - protected override bool ReleaseHandle() - { - grpcsharp_channel_destroy(handle); - return true; - } - } + public static ChannelSafeHandle CreateSecure(CredentialsSafeHandle credentials, string target, ChannelArgsSafeHandle channelArgs) + { + return grpcsharp_secure_channel_create(credentials, target, channelArgs); + } + + protected override bool ReleaseHandle() + { + grpcsharp_channel_destroy(handle); + return true; + } + } } diff --git a/src/csharp/Grpc.Core/Internal/CredentialsSafeHandle.cs b/src/csharp/Grpc.Core/Internal/CredentialsSafeHandle.cs new file mode 100644 index 0000000000..f361199068 --- /dev/null +++ b/src/csharp/Grpc.Core/Internal/CredentialsSafeHandle.cs @@ -0,0 +1,64 @@ +#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.Runtime.InteropServices; +using System.Threading; +using System.Threading.Tasks; + +namespace Grpc.Core.Internal +{ + /// <summary> + /// grpc_credentials from <grpc/grpc_security.h> + /// </summary> + internal class CredentialsSafeHandle : SafeHandleZeroIsInvalid + { + [DllImport("grpc_csharp_ext.dll", CharSet = CharSet.Ansi)] + static extern CredentialsSafeHandle grpcsharp_ssl_credentials_create(string pemRootCerts, string keyCertPairCertChain, string keyCertPairPrivateKey); + + [DllImport("grpc_csharp_ext.dll")] + static extern void grpcsharp_credentials_release(IntPtr credentials); + + private CredentialsSafeHandle() + { + } + + public static CredentialsSafeHandle CreateSslCredentials(string pemRootCerts) + { + return grpcsharp_ssl_credentials_create(pemRootCerts, null, null); + } + + protected override bool ReleaseHandle() + { + grpcsharp_credentials_release(handle); + return true; + } + } +} |