|
9 | 9 | using System.Security.Cryptography; |
10 | 10 | using SendSafely.Exceptions; |
11 | 11 | using System.Threading; |
| 12 | +using System.Reflection; |
| 13 | +using System.Runtime.Versioning; |
12 | 14 |
|
13 | 15 | namespace SendSafely.Objects |
14 | 16 | { |
@@ -203,6 +205,7 @@ public Stream CallServer(Endpoint p, Object request) |
203 | 205 |
|
204 | 206 | private void Initialize(String host, String privateKey, String apiKey) |
205 | 207 | { |
| 208 | + this.SetTlsProtocol(); |
206 | 209 | this.apiKey = apiKey; |
207 | 210 | this.privateKey = privateKey; |
208 | 211 |
|
@@ -271,6 +274,32 @@ private void WriteOutput(WebRequest req, String requestString) |
271 | 274 | dataStream.Write(reqData, 0, reqData.Length); |
272 | 275 | } |
273 | 276 |
|
| 277 | + private void SetTlsProtocol() |
| 278 | + { |
| 279 | + var assembly = Assembly.GetExecutingAssembly(); |
| 280 | + var attributes = assembly.GetCustomAttributes(typeof(TargetFrameworkAttribute), false); |
| 281 | + var version = (TargetFrameworkAttribute)attributes[0]; |
| 282 | + |
| 283 | + SecurityProtocolType flag; |
| 284 | + |
| 285 | + try |
| 286 | + { |
| 287 | + if (Enum.TryParse("Tls11", out flag)) |
| 288 | + ServicePointManager.SecurityProtocol |= flag; |
| 289 | + if (Enum.TryParse("Tls12", out flag)) |
| 290 | + ServicePointManager.SecurityProtocol |= flag; |
| 291 | + } |
| 292 | + catch (Exception e) |
| 293 | + { |
| 294 | + throw new Exception("Unable to set TLS protocol for " + version.FrameworkDisplayName + ", enabled protocols " + GetEnabledSecurityProtocols(), e); |
| 295 | + } |
| 296 | + } |
| 297 | + |
| 298 | + private string GetEnabledSecurityProtocols() |
| 299 | + { |
| 300 | + return ServicePointManager.SecurityProtocol.ToString(); |
| 301 | + } |
| 302 | + |
274 | 303 | #endregion |
275 | 304 | } |
276 | 305 | } |
0 commit comments