|
25 | 25 | //
|
26 | 26 | //------------------------------------------------------------------------------
|
27 | 27 |
|
| 28 | +using System; |
| 29 | +using System.Security.Cryptography.X509Certificates; |
28 | 30 | using Microsoft.IdentityModel.Logging;
|
29 | 31 |
|
30 | 32 | namespace Microsoft.IdentityModel.Tokens
|
31 | 33 | {
|
32 | 34 | /// <summary>
|
33 |
| - /// A wrapper class for properties that are used for token encryption. |
| 35 | + /// A class for properties that are used for token encryption. |
34 | 36 | /// </summary>
|
35 | 37 | public class EncryptingCredentials
|
36 | 38 | {
|
| 39 | + private string _alg; |
| 40 | + private string _enc; |
| 41 | + private SecurityKey _key; |
| 42 | + |
37 | 43 | /// <summary>
|
38 | 44 | /// Initializes a new instance of the <see cref="EncryptingCredentials"/> class.
|
39 | 45 | /// </summary>
|
40 |
| - /// <param name="key"><see cref="SecurityKey"/></param> |
41 |
| - /// <param name="alg">The key encryption algorithm to apply.</param> |
42 |
| - /// <param name="enc">The encryption algorithm to apply.</param> |
43 |
| - public EncryptingCredentials(SecurityKey key, string alg, string enc) |
| 46 | + /// <param name="certificate"><see cref="X509Certificate2"/>.</param> |
| 47 | + /// <param name="alg">A key wrap algorithm to use when encrypting a session key.</param> |
| 48 | + /// <param name="enc">Data encryption algorithm to apply.</param> |
| 49 | + /// <exception cref="ArgumentNullException">if 'certificate' is null.</exception> |
| 50 | + /// <exception cref="ArgumentNullException">if 'alg' is null or empty.</exception> |
| 51 | + /// <exception cref="ArgumentNullException">if 'enc' is null or empty.</exception> |
| 52 | + protected EncryptingCredentials(X509Certificate2 certificate, string alg, string enc) |
44 | 53 | {
|
45 |
| - if (key == null) |
46 |
| - throw LogHelper.LogArgumentNullException(nameof(key)); |
47 |
| - |
48 |
| - if (string.IsNullOrWhiteSpace(alg)) |
49 |
| - throw LogHelper.LogArgumentNullException(nameof(alg)); |
50 |
| - |
51 |
| - if (string.IsNullOrWhiteSpace(enc)) |
52 |
| - throw LogHelper.LogArgumentNullException(nameof(enc)); |
| 54 | + if (certificate == null) |
| 55 | + throw LogHelper.LogArgumentNullException(nameof(certificate)); |
53 | 56 |
|
| 57 | + Key = new X509SecurityKey(certificate); |
54 | 58 | Alg = alg;
|
55 | 59 | Enc = enc;
|
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Initializes a new instance of the <see cref="EncryptingCredentials"/> class. |
| 64 | + /// </summary> |
| 65 | + /// <param name="key"><see cref="SecurityKey"/> to use when encrypting a session key.</param> |
| 66 | + /// <param name="alg">A key wrap algorithm to use when encrypting a session key.</param> |
| 67 | + /// <param name="enc">Data encryption algorithm to apply.</param> |
| 68 | + /// <exception cref="ArgumentNullException">if 'key' is null.</exception> |
| 69 | + /// <exception cref="ArgumentNullException">if 'alg' is null or empty.</exception> |
| 70 | + /// <exception cref="ArgumentNullException">if 'enc' is null or empty.</exception> |
| 71 | + public EncryptingCredentials(SecurityKey key, string alg, string enc) |
| 72 | + { |
56 | 73 | Key = key;
|
| 74 | + Alg = alg; |
| 75 | + Enc = enc; |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Initializes a new instance of the <see cref="EncryptingCredentials"/> class. |
| 80 | + /// </summary> |
| 81 | + /// <remarks> Used in scenarios when a key represents a 'shared' symmetric key. |
| 82 | + /// For example, SAML 2.0 Assertion will be encrypted using a provided symmetric key |
| 83 | + /// which won't be serialized to a SAML token. |
| 84 | + /// </remarks> |
| 85 | + /// <param name="key"><see cref="SymmetricSecurityKey"/> to apply.</param> |
| 86 | + /// <param name="enc">Data encryption algorithm to apply.</param> |
| 87 | + /// <exception cref="ArgumentException">If the <see cref="SecurityKey"/> is not a <see cref="SymmetricSecurityKey"/>.</exception> |
| 88 | + /// <exception cref="ArgumentNullException">if 'enc' is null or empty.</exception> |
| 89 | + public EncryptingCredentials(SymmetricSecurityKey key, string enc) |
| 90 | + : this(key, SecurityAlgorithms.None, enc) |
| 91 | + { |
57 | 92 | }
|
58 | 93 |
|
59 | 94 | /// <summary>
|
60 |
| - /// Gets the algorithm which used for token encryption. |
| 95 | + /// Gets the key wrap algorithm used for session key encryption. |
61 | 96 | /// </summary>
|
62 | 97 | public string Alg
|
63 | 98 | {
|
64 |
| - get; |
65 |
| - private set; |
| 99 | + get => _alg; |
| 100 | + private set => _alg = string.IsNullOrEmpty(value) ? throw LogHelper.LogArgumentNullException("alg") : value; |
66 | 101 | }
|
67 | 102 |
|
68 | 103 | /// <summary>
|
69 |
| - /// Gets the algorithm which used for token encryption. |
| 104 | + /// Gets the data encryption algorithm. |
70 | 105 | /// </summary>
|
71 | 106 | public string Enc
|
72 | 107 | {
|
73 |
| - get; |
74 |
| - private set; |
| 108 | + get => _enc; |
| 109 | + private set => _enc = string.IsNullOrEmpty(value) ? throw LogHelper.LogArgumentNullException("enc") : value; |
75 | 110 | }
|
76 | 111 |
|
77 | 112 | /// <summary>
|
78 |
| - /// Users can override the default <see cref="CryptoProviderFactory"/> with this property. This factory will be used for creating encryition providers. |
| 113 | + /// Users can override the default <see cref="CryptoProviderFactory"/> with this property. This factory will be used for creating encryption providers. |
79 | 114 | /// </summary>
|
80 | 115 | public CryptoProviderFactory CryptoProviderFactory { get; set; }
|
81 | 116 |
|
82 | 117 | /// <summary>
|
83 |
| - /// Gets the <see cref="SecurityKey"/> which used for signature valdiation. |
| 118 | + /// Gets the <see cref="SecurityKey"/> used for encryption. |
84 | 119 | /// </summary>
|
85 | 120 | public SecurityKey Key
|
86 | 121 | {
|
87 |
| - get; |
88 |
| - private set; |
| 122 | + get => _key; |
| 123 | + private set => _key = value ?? throw LogHelper.LogArgumentNullException("key"); |
89 | 124 | }
|
90 | 125 | }
|
91 | 126 | }
|
0 commit comments