2525//
2626//------------------------------------------------------------------------------
2727
28- using Microsoft . IdentityModel . Logging ;
2928using System ;
29+ using System . Collections . Generic ;
30+ using System . Collections . ObjectModel ;
3031using System . Security . Cryptography ;
32+ using Microsoft . IdentityModel . Logging ;
3133
3234namespace Microsoft . IdentityModel . Tokens
3335{
@@ -36,6 +38,76 @@ namespace Microsoft.IdentityModel.Tokens
3638 /// </summary>
3739 internal static class SupportedAlgorithms
3840 {
41+ internal static readonly ICollection < string > EcdsaSigningAlgorithms = new Collection < string >
42+ {
43+ SecurityAlgorithms . EcdsaSha256 ,
44+ SecurityAlgorithms . EcdsaSha256Signature ,
45+ SecurityAlgorithms . EcdsaSha384 ,
46+ SecurityAlgorithms . EcdsaSha384Signature ,
47+ SecurityAlgorithms . EcdsaSha512 ,
48+ SecurityAlgorithms . EcdsaSha512Signature
49+ } ;
50+
51+ internal static readonly ICollection < string > HashAlgorithms = new Collection < string >
52+ {
53+ SecurityAlgorithms . Sha256 ,
54+ SecurityAlgorithms . Sha256Digest ,
55+ SecurityAlgorithms . Sha384 ,
56+ SecurityAlgorithms . Sha384Digest ,
57+ SecurityAlgorithms . Sha512 ,
58+ SecurityAlgorithms . Sha512Digest
59+ } ;
60+
61+ internal static readonly ICollection < string > RsaEncryptionAlgorithms = new Collection < string >
62+ {
63+ SecurityAlgorithms . RsaOAEP ,
64+ SecurityAlgorithms . RsaPKCS1 ,
65+ SecurityAlgorithms . RsaOaepKeyWrap
66+ } ;
67+
68+ internal static readonly ICollection < string > RsaSigningAlgorithms = new Collection < string >
69+ {
70+ SecurityAlgorithms . RsaSha256 ,
71+ SecurityAlgorithms . RsaSha384 ,
72+ SecurityAlgorithms . RsaSha512 ,
73+ SecurityAlgorithms . RsaSha256Signature ,
74+ SecurityAlgorithms . RsaSha384Signature ,
75+ SecurityAlgorithms . RsaSha512Signature
76+ } ;
77+
78+ internal static readonly ICollection < string > RsaPssSigningAlgorithms = new Collection < string >
79+ {
80+ SecurityAlgorithms . RsaSsaPssSha256 ,
81+ SecurityAlgorithms . RsaSsaPssSha384 ,
82+ SecurityAlgorithms . RsaSsaPssSha512 ,
83+ SecurityAlgorithms . RsaSsaPssSha256Signature ,
84+ SecurityAlgorithms . RsaSsaPssSha384Signature ,
85+ SecurityAlgorithms . RsaSsaPssSha512Signature
86+ } ;
87+
88+ internal static readonly ICollection < string > SymmetricEncryptionAlgorithms = new Collection < string >
89+ {
90+ SecurityAlgorithms . Aes128CbcHmacSha256 ,
91+ SecurityAlgorithms . Aes192CbcHmacSha384 ,
92+ SecurityAlgorithms . Aes256CbcHmacSha512
93+ } ;
94+
95+ internal static readonly ICollection < string > SymmetricKeyWrapAlgorithms = new Collection < string >
96+ {
97+ SecurityAlgorithms . Aes128KW ,
98+ SecurityAlgorithms . Aes256KW
99+ } ;
100+
101+ internal static readonly ICollection < string > SymmetricSigningAlgorithms = new Collection < string >
102+ {
103+ SecurityAlgorithms . HmacSha256Signature ,
104+ SecurityAlgorithms . HmacSha384Signature ,
105+ SecurityAlgorithms . HmacSha512Signature ,
106+ SecurityAlgorithms . HmacSha256 ,
107+ SecurityAlgorithms . HmacSha384 ,
108+ SecurityAlgorithms . HmacSha512
109+ } ;
110+
39111 /// <summary>
40112 /// Checks if an 'algorithm, key' pair is supported.
41113 /// </summary>
@@ -101,35 +173,12 @@ internal static bool IsSupportedAuthenticatedEncryptionAlgorithm(string algorith
101173
102174 private static bool IsSupportedEcdsaAlgorithm ( string algorithm )
103175 {
104- switch ( algorithm )
105- {
106- case SecurityAlgorithms . EcdsaSha256 :
107- case SecurityAlgorithms . EcdsaSha256Signature :
108- case SecurityAlgorithms . EcdsaSha384 :
109- case SecurityAlgorithms . EcdsaSha384Signature :
110- case SecurityAlgorithms . EcdsaSha512 :
111- case SecurityAlgorithms . EcdsaSha512Signature :
112- return true ;
113- }
114-
115- return false ;
176+ return EcdsaSigningAlgorithms . Contains ( algorithm ) ;
116177 }
117178
118179 internal static bool IsSupportedHashAlgorithm ( string algorithm )
119180 {
120- switch ( algorithm )
121- {
122- case SecurityAlgorithms . Sha256 :
123- case SecurityAlgorithms . Sha256Digest :
124- case SecurityAlgorithms . Sha384 :
125- case SecurityAlgorithms . Sha384Digest :
126- case SecurityAlgorithms . Sha512 :
127- case SecurityAlgorithms . Sha512Digest :
128- return true ;
129-
130- default :
131- return false ;
132- }
181+ return HashAlgorithms . Contains ( algorithm ) ;
133182 }
134183
135184 internal static bool IsSupportedKeyWrapAlgorithm ( string algorithm , SecurityKey key )
@@ -140,7 +189,10 @@ internal static bool IsSupportedKeyWrapAlgorithm(string algorithm, SecurityKey k
140189 if ( string . IsNullOrEmpty ( algorithm ) )
141190 return false ;
142191
143- if ( algorithm . Equals ( SecurityAlgorithms . RsaPKCS1 , StringComparison . Ordinal )
192+ if ( key . KeySize < 2048 )
193+ return false ;
194+
195+ if ( algorithm . Equals ( SecurityAlgorithms . RsaPKCS1 , StringComparison . Ordinal )
144196 || algorithm . Equals ( SecurityAlgorithms . RsaOAEP , StringComparison . Ordinal )
145197 || algorithm . Equals ( SecurityAlgorithms . RsaOaepKeyWrap , StringComparison . Ordinal ) )
146198 {
@@ -159,28 +211,9 @@ internal static bool IsSupportedKeyWrapAlgorithm(string algorithm, SecurityKey k
159211
160212 internal static bool IsSupportedRsaAlgorithm ( string algorithm , SecurityKey key )
161213 {
162- switch ( algorithm )
163- {
164- case SecurityAlgorithms . RsaSha256 :
165- case SecurityAlgorithms . RsaSha384 :
166- case SecurityAlgorithms . RsaSha512 :
167- case SecurityAlgorithms . RsaSha256Signature :
168- case SecurityAlgorithms . RsaSha384Signature :
169- case SecurityAlgorithms . RsaSha512Signature :
170- case SecurityAlgorithms . RsaOAEP :
171- case SecurityAlgorithms . RsaPKCS1 :
172- case SecurityAlgorithms . RsaOaepKeyWrap :
173- return true ;
174- case SecurityAlgorithms . RsaSsaPssSha256 :
175- case SecurityAlgorithms . RsaSsaPssSha384 :
176- case SecurityAlgorithms . RsaSsaPssSha512 :
177- case SecurityAlgorithms . RsaSsaPssSha256Signature :
178- case SecurityAlgorithms . RsaSsaPssSha384Signature :
179- case SecurityAlgorithms . RsaSsaPssSha512Signature :
180- return IsSupportedRsaPss ( key ) ;
181- }
182-
183- return false ;
214+ return RsaSigningAlgorithms . Contains ( algorithm )
215+ || RsaEncryptionAlgorithms . Contains ( algorithm )
216+ || ( RsaPssSigningAlgorithms . Contains ( algorithm ) && IsSupportedRsaPss ( key ) ) ;
184217 }
185218
186219 private static bool IsSupportedRsaPss ( SecurityKey key )
@@ -212,23 +245,9 @@ private static bool IsSupportedRsaPss(SecurityKey key)
212245
213246 internal static bool IsSupportedSymmetricAlgorithm ( string algorithm )
214247 {
215- switch ( algorithm )
216- {
217- case SecurityAlgorithms . Aes128CbcHmacSha256 :
218- case SecurityAlgorithms . Aes192CbcHmacSha384 :
219- case SecurityAlgorithms . Aes256CbcHmacSha512 :
220- case SecurityAlgorithms . Aes128KW :
221- case SecurityAlgorithms . Aes256KW :
222- case SecurityAlgorithms . HmacSha256Signature :
223- case SecurityAlgorithms . HmacSha384Signature :
224- case SecurityAlgorithms . HmacSha512Signature :
225- case SecurityAlgorithms . HmacSha256 :
226- case SecurityAlgorithms . HmacSha384 :
227- case SecurityAlgorithms . HmacSha512 :
228- return true ;
229- }
230-
231- return false ;
248+ return SymmetricEncryptionAlgorithms . Contains ( algorithm )
249+ || SymmetricKeyWrapAlgorithms . Contains ( algorithm )
250+ || SymmetricSigningAlgorithms . Contains ( algorithm ) ;
232251 }
233252 }
234253}
0 commit comments