| | | 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; |
| | | 5 | | using System.ComponentModel; |
| | | 6 | | using System.Net.Security; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.Security |
| | | 9 | | { |
| | | 10 | | internal static class ProtectionLevelHelper |
| | | 11 | | { |
| | | 12 | | internal static bool IsDefined(ProtectionLevel value) |
| | | 13 | | { |
| | 0 | 14 | | return (value == ProtectionLevel.None |
| | 0 | 15 | | || value == ProtectionLevel.Sign |
| | 0 | 16 | | || value == ProtectionLevel.EncryptAndSign); |
| | | 17 | | } |
| | | 18 | | |
| | | 19 | | internal static void Validate(ProtectionLevel value) |
| | | 20 | | { |
| | 0 | 21 | | if (!IsDefined(value)) |
| | | 22 | | { |
| | 0 | 23 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidEnumArgumentException(nameof(value) |
| | 0 | 24 | | typeof(ProtectionLevel))); |
| | | 25 | | } |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | internal static bool IsStronger(ProtectionLevel v1, ProtectionLevel v2) |
| | | 29 | | { |
| | 0 | 30 | | return ((v1 == ProtectionLevel.EncryptAndSign && v2 != ProtectionLevel.EncryptAndSign) |
| | 0 | 31 | | || (v1 == ProtectionLevel.Sign && v2 == ProtectionLevel.None)); |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | internal static bool IsStrongerOrEqual(ProtectionLevel v1, ProtectionLevel v2) |
| | | 35 | | { |
| | 0 | 36 | | return (v1 == ProtectionLevel.EncryptAndSign |
| | 0 | 37 | | || (v1 == ProtectionLevel.Sign && v2 != ProtectionLevel.EncryptAndSign)); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | internal static ProtectionLevel Max(ProtectionLevel v1, ProtectionLevel v2) |
| | | 41 | | { |
| | 0 | 42 | | return IsStronger(v1, v2) ? v1 : v2; |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | internal static int GetOrdinal(Nullable<ProtectionLevel> p) |
| | | 46 | | { |
| | 0 | 47 | | if (p.HasValue) |
| | | 48 | | { |
| | 0 | 49 | | switch ((ProtectionLevel)p) |
| | | 50 | | { |
| | | 51 | | default: |
| | 0 | 52 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidEnumArgumentException(nameo |
| | 0 | 53 | | typeof(ProtectionLevel))); |
| | | 54 | | case ProtectionLevel.None: |
| | 0 | 55 | | return 2; |
| | | 56 | | case ProtectionLevel.Sign: |
| | 0 | 57 | | return 3; |
| | | 58 | | case ProtectionLevel.EncryptAndSign: |
| | 0 | 59 | | return 4; |
| | | 60 | | } |
| | | 61 | | } |
| | | 62 | | else |
| | | 63 | | { |
| | 0 | 64 | | return 1; |
| | | 65 | | } |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | } |