11/*
2- * Copyright (c) 2005, 2021 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2005, 2022 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
@@ -63,11 +63,11 @@ abstract class PBKDF2Core extends SecretKeyFactorySpi {
6363 protected SecretKey engineGenerateSecret (KeySpec keySpec )
6464 throws InvalidKeySpecException
6565 {
66- if (!(keySpec instanceof PBEKeySpec )) {
67- throw new InvalidKeySpecException ("Invalid key spec" );
66+ if (keySpec instanceof PBEKeySpec ks ) {
67+ return new PBKDF2KeyImpl (ks , prfAlgo );
68+ } else {
69+ throw new InvalidKeySpecException ("Only PBEKeySpec is accepted" );
6870 }
69- PBEKeySpec ks = (PBEKeySpec ) keySpec ;
70- return new PBKDF2KeyImpl (ks , prfAlgo );
7171 }
7272
7373 /**
@@ -89,12 +89,10 @@ protected SecretKey engineGenerateSecret(KeySpec keySpec)
8989 */
9090 protected KeySpec engineGetKeySpec (SecretKey key , Class <?> keySpecCl )
9191 throws InvalidKeySpecException {
92- if (key instanceof javax .crypto .interfaces .PBEKey ) {
92+ if (key instanceof javax .crypto .interfaces .PBEKey pKey ) {
9393 // Check if requested key spec is amongst the valid ones
9494 if ((keySpecCl != null )
9595 && keySpecCl .isAssignableFrom (PBEKeySpec .class )) {
96- javax .crypto .interfaces .PBEKey pKey =
97- (javax .crypto .interfaces .PBEKey ) key ;
9896 char [] passwd = pKey .getPassword ();
9997 byte [] encoded = pKey .getEncoded ();
10098 try {
@@ -107,11 +105,11 @@ protected KeySpec engineGetKeySpec(SecretKey key, Class<?> keySpecCl)
107105 Arrays .fill (encoded , (byte )0 );
108106 }
109107 } else {
110- throw new InvalidKeySpecException ("Invalid key spec" );
108+ throw new InvalidKeySpecException
109+ ("Only PBEKeySpec is accepted" );
111110 }
112111 } else {
113- throw new InvalidKeySpecException ("Invalid key " +
114- "format/algorithm" );
112+ throw new InvalidKeySpecException ("Only PBEKey is accepted" );
115113 }
116114 }
117115
@@ -138,9 +136,7 @@ protected SecretKey engineTranslateKey(SecretKey key)
138136 return key ;
139137 }
140138 // Check if key implements the PBEKey
141- if (key instanceof javax .crypto .interfaces .PBEKey ) {
142- javax .crypto .interfaces .PBEKey pKey =
143- (javax .crypto .interfaces .PBEKey ) key ;
139+ if (key instanceof javax .crypto .interfaces .PBEKey pKey ) {
144140 char [] password = pKey .getPassword ();
145141 byte [] encoding = pKey .getEncoded ();
146142 PBEKeySpec spec =
@@ -160,9 +156,12 @@ protected SecretKey engineTranslateKey(SecretKey key)
160156 }
161157 Arrays .fill (encoding , (byte )0 );
162158 }
159+ } else {
160+ throw new InvalidKeyException ("Only PBEKey is accepted" );
163161 }
164162 }
165- throw new InvalidKeyException ("Invalid key format/algorithm" );
163+ throw new InvalidKeyException ("Only PBKDF2With" + prfAlgo +
164+ " key with RAW format is accepted" );
166165 }
167166
168167 public static final class HmacSHA1 extends PBKDF2Core {
0 commit comments