1717import java .util .Map ;
1818import java .util .Set ;
1919import java .util .StringTokenizer ;
20+ import java .util .logging .Level ;
2021import java .util .logging .Logger ;
2122
2223import javax .crypto .SecretKey ;
@@ -53,8 +54,8 @@ static DisabledAlgorithmConstraints create(AlgorithmDecomposer decomposer, Strin
5354 }
5455 }
5556
56- return new DisabledAlgorithmConstraints (decomposer , Collections . unmodifiableSet ( disabledAlgorithms ) ,
57- Collections .unmodifiableMap (constraintsMap ));
57+ return new DisabledAlgorithmConstraints (decomposer , propertyName ,
58+ Collections .unmodifiableSet ( disabledAlgorithms ), Collections . unmodifiableMap (constraintsMap ));
5859 }
5960
6061 private static boolean addConstraint (Set <String > disabledAlgorithms , Map <String , List <Constraint >> constraintsMap ,
@@ -151,27 +152,16 @@ private static String getConstraintsAlgorithm(String algorithm, AlgorithmParamet
151152 return null ;
152153 }
153154
154- private static String getConstraintsAlgorithm (Key key )
155- {
156- if (null != key )
157- {
158- String keyAlgorithm = JsseUtils .getKeyAlgorithm (key );
159- if (null != keyAlgorithm )
160- {
161- return getCanonicalAlgorithm (keyAlgorithm );
162- }
163- }
164- return null ;
165- }
166-
155+ private final String logHeader ;
167156 private final Set <String > disabledAlgorithms ;
168157 private final Map <String , List <Constraint >> constraintsMap ;
169158
170- private DisabledAlgorithmConstraints (AlgorithmDecomposer decomposer , Set < String > disabledAlgorithms ,
171- Map <String , List <Constraint >> constraintsMap )
159+ private DisabledAlgorithmConstraints (AlgorithmDecomposer decomposer , String propertyName ,
160+ Set < String > disabledAlgorithms , Map <String , List <Constraint >> constraintsMap )
172161 {
173162 super (decomposer );
174163
164+ this .logHeader = "[" + propertyName + "]" ;
175165 this .disabledAlgorithms = disabledAlgorithms ;
176166 this .constraintsMap = constraintsMap ;
177167 }
@@ -181,20 +171,7 @@ public final boolean permits(Set<BCCryptoPrimitive> primitives, String algorithm
181171 checkPrimitives (primitives );
182172 checkAlgorithmName (algorithm );
183173
184- if (containsAnyPartIgnoreCase (disabledAlgorithms , algorithm ))
185- {
186- return false ;
187- }
188-
189- for (Constraint constraint : getConstraints (getConstraintsAlgorithm (algorithm , parameters )))
190- {
191- if (!constraint .permits (parameters ))
192- {
193- return false ;
194- }
195- }
196-
197- return true ;
174+ return implPermitsAlgorithm (primitives , algorithm , parameters );
198175 }
199176
200177 public final boolean permits (Set <BCCryptoPrimitive > primitives , Key key )
@@ -216,23 +193,32 @@ private boolean checkConstraints(Set<BCCryptoPrimitive> primitives, String algor
216193 checkPrimitives (primitives );
217194 checkKey (key );
218195
219- if (JsseUtils .isNameSpecified (algorithm )
220- && !permits (primitives , algorithm , parameters ))
196+ String keyAlgorithm = JsseUtils .getKeyAlgorithm (key );
197+ checkAlgorithmName (keyAlgorithm );
198+
199+ if (JsseUtils .isNameSpecified (algorithm ) &&
200+ !implPermitsAlgorithm (primitives , algorithm , parameters ))
221201 {
222202 return false ;
223203 }
224204
225- if (!permits (primitives , JsseUtils . getKeyAlgorithm ( key ), null ))
205+ if (!implPermitsKeyAlgorithm (primitives , keyAlgorithm ))
226206 {
227207 return false ;
228208 }
229209
230210 // TODO[jsse] SunJSSE also checks the named curve for EC keys
231211
232- for (Constraint constraint : getConstraints (getConstraintsAlgorithm (key )))
212+ String constraintsAlgorithm = getCanonicalAlgorithm (keyAlgorithm );
213+ for (Constraint constraint : getConstraints (constraintsAlgorithm ))
233214 {
234215 if (!constraint .permits (key ))
235216 {
217+ if (LOG .isLoggable (Level .FINEST ))
218+ {
219+ LOG .finest (logHeader + " constraints for '" + constraintsAlgorithm + "' do not permit given '"
220+ + keyAlgorithm + "' key" );
221+ }
236222 return false ;
237223 }
238224 }
@@ -253,6 +239,49 @@ private List<Constraint> getConstraints(String algorithm)
253239 return Collections .<Constraint > emptyList ();
254240 }
255241
242+ private boolean implPermitsAlgorithm (Set <BCCryptoPrimitive > primitives , String algorithm ,
243+ AlgorithmParameters parameters )
244+ {
245+ if (containsAnyPartIgnoreCase (disabledAlgorithms , algorithm ))
246+ {
247+ if (LOG .isLoggable (Level .FINEST ))
248+ {
249+ LOG .finest (logHeader + " disabled algorithm '" + algorithm + "'" );
250+ }
251+ return false ;
252+ }
253+
254+ String constraintsAlgorithm = getConstraintsAlgorithm (algorithm , parameters );
255+ for (Constraint constraint : getConstraints (constraintsAlgorithm ))
256+ {
257+ if (!constraint .permits (parameters ))
258+ {
259+ if (LOG .isLoggable (Level .FINEST ))
260+ {
261+ LOG .finest (logHeader + " constraints for '" + constraintsAlgorithm +
262+ "' do not permit algorithm '" + algorithm + "' for given parameters" );
263+ }
264+ return false ;
265+ }
266+ }
267+
268+ return true ;
269+ }
270+
271+ private boolean implPermitsKeyAlgorithm (Set <BCCryptoPrimitive > primitives , String keyAlgorithm )
272+ {
273+ if (containsAnyPartIgnoreCase (disabledAlgorithms , keyAlgorithm ))
274+ {
275+ if (LOG .isLoggable (Level .FINEST ))
276+ {
277+ LOG .finest (logHeader + " disabled key algorithm '" + keyAlgorithm + "'" );
278+ }
279+ return false ;
280+ }
281+
282+ return true ;
283+ }
284+
256285 private static enum BinOp
257286 {
258287 EQ ("==" ), GE (">=" ), GT (">" ), LE ("<=" ), LT ("<" ), NE ("!=" );
0 commit comments