@@ -296,69 +296,70 @@ Map<String, String> loadProfileConfig(Long profileId) {
296296 /**
297297 * Validates HSM profile configuration for PKCS#11 provider.
298298 *
299- * <p>Validates:
299+ * <p>
300+ * Validates:
300301 * <ul>
301- * <li>{@code library}: Required, should point to PKCS#11 library</li>
302- * <li>{@code slot} or {@code token_label}: At least one required</li>
303- * <li>{@code pin}: Required for HSM authentication</li>
304- * <li>{@code max_sessions}: Optional, must be positive integer if provided</li>
302+ * <li>{@code library}: Required, should point to PKCS#11 library</li>
303+ * <li>{@code slot}, {@code slot_list_index}, or {@code token_label}: At least
304+ * one required</li>
305+ * <li>{@code pin}: Required for HSM authentication</li>
306+ * <li>{@code max_sessions}: Optional, must be positive integer if provided</li>
305307 * </ul>
306308 *
307309 * @param config Configuration map from HSM profile details
308310 * @throws KMSException with {@code INVALID_PARAMETER} if validation fails
309311 */
310- void validateProfileConfig (Map <String , String > config ) throws KMSException {
312+ @ Override
313+ public void validateProfileConfig (Map <String , String > config ) throws KMSException {
311314 String libraryPath = config .get ("library" );
312- if (StringUtils .isEmpty (libraryPath )) {
315+ if (StringUtils .isBlank (libraryPath )) {
313316 throw KMSException .invalidParameter ("library is required for PKCS#11 HSM profile" );
314317 }
315318
316319 String slot = config .get ("slot" );
320+ String slotListIndex = config .get ("slot_list_index" );
317321 String tokenLabel = config .get ("token_label" );
318- if (StringUtils .isEmpty (slot ) && StringUtils .isEmpty (tokenLabel )) {
319- throw KMSException .invalidParameter ("Either 'slot' or 'token_label' is required for PKCS#11 HSM profile" );
322+ if (StringUtils .isAllBlank (slot , slotListIndex , tokenLabel )) {
323+ throw KMSException .invalidParameter (
324+ "One of 'slot', 'slot_list_index', or 'token_label' is required for PKCS#11 HSM profile" );
320325 }
321326
322- if (! StringUtils .isEmpty (slot )) {
327+ if (StringUtils .isNotBlank (slot )) {
323328 try {
324329 Integer .parseInt (slot );
325330 } catch (NumberFormatException e ) {
326331 throw KMSException .invalidParameter ("slot must be a valid integer: " + slot );
327332 }
328333 }
329334
335+ if (StringUtils .isNotBlank (slotListIndex )) {
336+ try {
337+ int idx = Integer .parseInt (slotListIndex );
338+ if (idx < 0 ) {
339+ throw KMSException .invalidParameter ("slot_list_index must be a non-negative integer" );
340+ }
341+ } catch (NumberFormatException e ) {
342+ throw KMSException .invalidParameter ("slot_list_index must be a valid integer: " + slotListIndex );
343+ }
344+ }
345+
330346 File libraryFile = new File (libraryPath );
331347 if (!libraryFile .exists () && !libraryFile .isAbsolute ()) {
332348 // The HSM library might be in the system library path
333349 logger .debug ("Library path {} does not exist as absolute path, will rely on system library path" ,
334350 libraryPath );
335351 }
336352
337- parsePositiveInteger (config , "max_sessions" , "max_sessions" );
338- }
339-
340- /**
341- * Parses a positive integer from configuration.
342- *
343- * @param config Configuration map
344- * @param key Configuration key
345- * @param errorPrefix Prefix for error messages
346- * @return Parsed integer value, or -1 if not provided
347- * @throws KMSException if value is invalid or not positive
348- */
349- private int parsePositiveInteger (Map <String , String > config , String key , String errorPrefix ) throws KMSException {
350- String value = config .get (key );
351- if (StringUtils .isEmpty (value )) {
352- return -1 ; // Not provided
353- }
354- try {
355- int parsed = Integer .parseInt (value );
356- if (parsed <= 0 ) {
357- throw KMSException .invalidParameter (errorPrefix + " must be greater than 0" );
353+ String max_sessions = config .get ("max_sessions" );
354+ if (StringUtils .isNotBlank (max_sessions )) {
355+ try {
356+ int idx = Integer .parseInt (max_sessions );
357+ if (idx <= 0 ) {
358+ throw KMSException .invalidParameter ("max_sessions must be greater than 0" );
359+ }
360+ } catch (NumberFormatException e ) {
361+ throw KMSException .invalidParameter ("max_sessions must be a valid integer: " + max_sessions );
358362 }
359- return parsed ;
360- } catch (NumberFormatException e ) {
361- throw KMSException .invalidParameter (errorPrefix + " must be a valid integer: " + value );
362363 }
363364 }
364365
@@ -615,7 +616,7 @@ private void connect(Map<String, String> config) throws KMSException {
615616 */
616617 private String buildSunPKCS11Config (Map <String , String > config , String nameSuffix ) throws KMSException {
617618 String libraryPath = config .get ("library" );
618- if (StringUtils .isEmpty (libraryPath )) {
619+ if (StringUtils .isBlank (libraryPath )) {
619620 throw KMSException .invalidParameter ("library is required" );
620621 }
621622
@@ -627,14 +628,17 @@ private String buildSunPKCS11Config(Map<String, String> config, String nameSuffi
627628 configBuilder .append ("library=" ).append (libraryPath ).append ("\n " );
628629
629630 String tokenLabel = config .get ("token_label" );
631+ String slotListIndex = config .get ("slot_list_index" );
630632 String slot = config .get ("slot" );
631633
632- if (! StringUtils .isEmpty (tokenLabel )) {
634+ if (StringUtils .isNotBlank (tokenLabel )) {
633635 configBuilder .append ("tokenLabel=" ).append (tokenLabel ).append ("\n " );
634- } else if (!StringUtils .isEmpty (slot )) {
636+ } else if (StringUtils .isNotBlank (slotListIndex )) {
637+ configBuilder .append ("slotListIndex=" ).append (slotListIndex ).append ("\n " );
638+ } else if (StringUtils .isNotBlank (slot )) {
635639 configBuilder .append ("slot=" ).append (slot ).append ("\n " );
636640 } else {
637- throw KMSException .invalidParameter ("Either 'slot' or 'token_label' is required" );
641+ throw KMSException .invalidParameter ("One of 'slot', 'slot_list_index', or 'token_label' is required" );
638642 }
639643
640644 return configBuilder .toString ();
0 commit comments