| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | #if DEBUG |
| | | 6 | | using System.Collections; |
| | | 7 | | using System.Diagnostics; |
| | | 8 | | using System.Reflection; |
| | | 9 | | #endif |
| | | 10 | | |
| | | 11 | | namespace System.Net.Security |
| | | 12 | | { |
| | | 13 | | internal static class SslClientAuthenticationOptionsExtensions |
| | | 14 | | { |
| | | 15 | | public static SslClientAuthenticationOptions ShallowClone(this SslClientAuthenticationOptions options) |
| | 0 | 16 | | { |
| | | 17 | | // Use non-default values to verify the clone works fine. |
| | 0 | 18 | | var clone = new SslClientAuthenticationOptions() |
| | 0 | 19 | | { |
| | 0 | 20 | | AllowRenegotiation = options.AllowRenegotiation, |
| | 0 | 21 | | AllowTlsResume = options.AllowTlsResume, |
| | 0 | 22 | | ApplicationProtocols = options.ApplicationProtocols != null ? new List<SslApplicationProtocol>(options.A |
| | 0 | 23 | | CertificateRevocationCheckMode = options.CertificateRevocationCheckMode, |
| | 0 | 24 | | CertificateChainPolicy = options.CertificateChainPolicy, |
| | 0 | 25 | | CipherSuitesPolicy = options.CipherSuitesPolicy, |
| | 0 | 26 | | ClientCertificates = options.ClientCertificates, |
| | 0 | 27 | | ClientCertificateContext = options.ClientCertificateContext, |
| | 0 | 28 | | EnabledSslProtocols = options.EnabledSslProtocols, |
| | 0 | 29 | | EncryptionPolicy = options.EncryptionPolicy, |
| | 0 | 30 | | LocalCertificateSelectionCallback = options.LocalCertificateSelectionCallback, |
| | 0 | 31 | | RemoteCertificateValidationCallback = options.RemoteCertificateValidationCallback, |
| | 0 | 32 | | TargetHost = options.TargetHost, |
| | 0 | 33 | | #pragma warning disable CA1416 // Ignore SupportedOSPlatform checks, the value will be validated at runtime inside SslSt |
| | 0 | 34 | | AllowRsaPssPadding = options.AllowRsaPssPadding, |
| | 0 | 35 | | AllowRsaPkcs1Padding = options.AllowRsaPkcs1Padding |
| | 0 | 36 | | #pragma warning restore CA1416 |
| | 0 | 37 | | }; |
| | | 38 | | |
| | | 39 | | #if DEBUG |
| | | 40 | | // Try to detect if a property gets added that we're not copying correctly. |
| | | 41 | | // The property count is guard for new properties that also needs to be added above. |
| | 0 | 42 | | PropertyInfo[] properties = typeof(SslClientAuthenticationOptions).GetProperties(BindingFlags.Public | Bindi |
| | 0 | 43 | | Debug.Assert(properties.Length == 15); |
| | 0 | 44 | | foreach (PropertyInfo pi in properties) |
| | 0 | 45 | | { |
| | 0 | 46 | | object? origValue = pi.GetValue(options); |
| | 0 | 47 | | object? cloneValue = pi.GetValue(clone); |
| | | 48 | | |
| | 0 | 49 | | if (origValue is IEnumerable origEnumerable) |
| | 0 | 50 | | { |
| | 0 | 51 | | IEnumerable? cloneEnumerable = cloneValue as IEnumerable; |
| | 0 | 52 | | Debug.Assert(cloneEnumerable != null, $"{pi.Name}. Expected enumerable cloned value."); |
| | | 53 | | |
| | 0 | 54 | | IEnumerator e1 = origEnumerable.GetEnumerator(); |
| | | 55 | | try |
| | 0 | 56 | | { |
| | 0 | 57 | | IEnumerator e2 = cloneEnumerable.GetEnumerator(); |
| | | 58 | | try |
| | 0 | 59 | | { |
| | 0 | 60 | | while (e1.MoveNext()) |
| | 0 | 61 | | { |
| | 0 | 62 | | Debug.Assert(e2.MoveNext(), $"{pi.Name}. Cloned enumerator too short."); |
| | 0 | 63 | | Debug.Assert(Equals(e1.Current, e2.Current), $"{pi.Name}. Cloned enumerator's values don |
| | 0 | 64 | | } |
| | 0 | 65 | | Debug.Assert(!e2.MoveNext(), $"{pi.Name}. Cloned enumerator too long."); |
| | 0 | 66 | | } |
| | | 67 | | finally |
| | 0 | 68 | | { |
| | 0 | 69 | | (e2 as IDisposable)?.Dispose(); |
| | 0 | 70 | | } |
| | 0 | 71 | | } |
| | | 72 | | finally |
| | 0 | 73 | | { |
| | 0 | 74 | | (e1 as IDisposable)?.Dispose(); |
| | 0 | 75 | | } |
| | 0 | 76 | | } |
| | | 77 | | else |
| | 0 | 78 | | { |
| | 0 | 79 | | Debug.Assert(Equals(origValue, cloneValue), $"{pi.Name}. Expected: {origValue}, Actual: {cloneValue} |
| | 0 | 80 | | } |
| | 0 | 81 | | } |
| | | 82 | | #endif |
| | | 83 | | |
| | 0 | 84 | | return clone; |
| | 0 | 85 | | } |
| | | 86 | | } |
| | | 87 | | } |