Home | History | Annotate | Download | only in Internal
      1 #region Copyright notice and license
      2 
      3 // Copyright 2015 gRPC authors.
      4 //
      5 // Licensed under the Apache License, Version 2.0 (the "License");
      6 // you may not use this file except in compliance with the License.
      7 // You may obtain a copy of the License at
      8 //
      9 //     http://www.apache.org/licenses/LICENSE-2.0
     10 //
     11 // Unless required by applicable law or agreed to in writing, software
     12 // distributed under the License is distributed on an "AS IS" BASIS,
     13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 // See the License for the specific language governing permissions and
     15 // limitations under the License.
     16 
     17 #endregion
     18 
     19 using System;
     20 using System.Collections.Concurrent;
     21 using System.Diagnostics;
     22 using System.IO;
     23 using System.Reflection;
     24 using System.Runtime.InteropServices;
     25 using System.Threading;
     26 
     27 namespace Grpc.Core.Internal
     28 {
     29     /// <summary>
     30     /// Overrides the content of default SSL roots.
     31     /// </summary>
     32     internal static class DefaultSslRootsOverride
     33     {
     34         const string RootsPemResourceName = "Grpc.Core.roots.pem";
     35         static object staticLock = new object();
     36 
     37         /// <summary>
     38         /// Overrides C core's default roots with roots.pem loaded as embedded resource.
     39         /// </summary>
     40         public static void Override(NativeMethods native)
     41         {
     42             lock (staticLock)
     43             {
     44                 var stream = typeof(DefaultSslRootsOverride).GetTypeInfo().Assembly.GetManifestResourceStream(RootsPemResourceName);
     45                 if (stream == null)
     46                 {
     47                     throw new IOException(string.Format("Error loading the embedded resource \"{0}\"", RootsPemResourceName));   
     48                 }
     49                 using (var streamReader = new StreamReader(stream))
     50                 {
     51                     var pemRootCerts = streamReader.ReadToEnd();
     52                     native.grpcsharp_override_default_ssl_roots(pemRootCerts);
     53                 }
     54             }
     55         }
     56     }
     57 }
     58