| | | 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 | | |
| | | 6 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// This class defines the encrypting credentials which can be used to |
| | | 10 | | /// encrypt the proof key. It is very similar to SigningCredentials class defined |
| | | 11 | | /// in System.IdentityModel.dll |
| | | 12 | | /// </summary> |
| | | 13 | | public class EncryptingCredentials |
| | | 14 | | { |
| | | 15 | | private string _algorithm; |
| | | 16 | | private SecurityKey _key; |
| | | 17 | | private SecurityKeyIdentifier _keyIdentifier; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Constructor for easy subclassing. |
| | | 21 | | /// </summary> |
| | 0 | 22 | | public EncryptingCredentials() |
| | | 23 | | { |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Constructs an EncryptingCredentials with a security key, a security key identifier and |
| | | 28 | | /// the encryption algorithm. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="key">A security key for encryption.</param> |
| | | 31 | | /// <param name="keyIdentifier">A security key identifier for the encryption key.</param> |
| | | 32 | | /// <param name="algorithm">The encryption algorithm.</param> |
| | | 33 | | /// <exception cref="ArgumentNullException">When key is null.</exception> |
| | | 34 | | /// <exception cref="ArgumentNullException">When key identifier is null.</exception> |
| | | 35 | | /// <exception cref="ArgumentNullException">When algorithm is null.</exception> |
| | 0 | 36 | | public EncryptingCredentials(SecurityKey key, SecurityKeyIdentifier keyIdentifier, string algorithm) |
| | | 37 | | { |
| | 0 | 38 | | if (key == null) |
| | | 39 | | { |
| | 0 | 40 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(key)); |
| | | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | if (keyIdentifier == null) |
| | | 44 | | { |
| | 0 | 45 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifier)); |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | if (string.IsNullOrEmpty(algorithm)) |
| | | 49 | | { |
| | 0 | 50 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(algorithm)); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | // |
| | | 54 | | // It is possible that keyIdentifier is pointing to a token which |
| | | 55 | | // is not capable of doing the given algorithm, we have no way verify |
| | | 56 | | // that at this level. |
| | | 57 | | // |
| | 0 | 58 | | _algorithm = algorithm; |
| | 0 | 59 | | _key = key; |
| | 0 | 60 | | _keyIdentifier = keyIdentifier; |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Gets or sets the encryption algorithm. |
| | | 65 | | /// </summary> |
| | | 66 | | public string Algorithm |
| | | 67 | | { |
| | | 68 | | get |
| | | 69 | | { |
| | 0 | 70 | | return _algorithm; |
| | | 71 | | } |
| | | 72 | | set |
| | | 73 | | { |
| | 0 | 74 | | if (string.IsNullOrEmpty(value)) |
| | | 75 | | { |
| | 0 | 76 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value)); |
| | | 77 | | } |
| | | 78 | | |
| | 0 | 79 | | _algorithm = value; |
| | 0 | 80 | | } |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Gets or sets the encryption key material. |
| | | 85 | | /// </summary> |
| | | 86 | | public SecurityKey SecurityKey |
| | | 87 | | { |
| | | 88 | | get |
| | | 89 | | { |
| | 0 | 90 | | return _key; |
| | | 91 | | } |
| | | 92 | | set |
| | | 93 | | { |
| | 0 | 94 | | if (value == null) |
| | | 95 | | { |
| | 0 | 96 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value)); |
| | | 97 | | } |
| | | 98 | | |
| | 0 | 99 | | _key = value; |
| | 0 | 100 | | } |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Gets or sets the SecurityKeyIdentifier that identifies the encrypting credential. |
| | | 105 | | /// </summary> |
| | | 106 | | public SecurityKeyIdentifier SecurityKeyIdentifier |
| | | 107 | | { |
| | | 108 | | get |
| | | 109 | | { |
| | 0 | 110 | | return _keyIdentifier; |
| | | 111 | | } |
| | | 112 | | set |
| | | 113 | | { |
| | 0 | 114 | | if (value == null) |
| | | 115 | | { |
| | 0 | 116 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value)); |
| | | 117 | | } |
| | | 118 | | |
| | 0 | 119 | | _keyIdentifier = value; |
| | 0 | 120 | | } |
| | | 121 | | } |
| | | 122 | | } |
| | | 123 | | } |