SecretKeyStoreCache.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.security.keystore.cache;

  21. import org.openspcoop2.security.SecurityException;
  22. import org.openspcoop2.security.keystore.SecretKeyStore;
  23. import org.openspcoop2.security.keystore.SecretPasswordKeyDerivationConfig;
  24. import org.openspcoop2.utils.certificate.byok.BYOKRequestParams;

  25. /**
  26.  * SecretKeyStoreCache
  27.  *
  28.  * @author Andrea Poli (apoli@link.it)
  29.  * @author $Author$
  30.  * @version $Rev$, $Date$
  31.  */
  32. public class SecretKeyStoreCache extends AbstractKeystoreCache<SecretKeyStore> {

  33.     @Override
  34.     public SecretKeyStore createKeystore(String key, Object... params) throws SecurityException{
  35.         if(params==null){
  36.             throw new SecurityException("Params is null");
  37.         }
  38.        
  39.         if(params.length==1){
  40.             if(params[0] instanceof String) {
  41.                 return createFromPath(key, params);
  42.             }
  43.             else {
  44.                 return createFromPasswordKeyDerivation(params);
  45.             }
  46.         }
  47.         else if(params.length==2){
  48.             if(params[0] instanceof String) {
  49.                 return createFromPath(key, params);
  50.             }
  51.             else if(params[0] instanceof SecretPasswordKeyDerivationConfig) {
  52.                 return createFromPasswordKeyDerivation(params);
  53.             }
  54.             else {
  55.                 return createFromByteArray(params);
  56.             }
  57.         }
  58.         else if(params.length==3){
  59.             return createFromByteArray(params);
  60.         }
  61.         else{
  62.             throw new SecurityException("Params [lenght:"+params.length+"] not supported");
  63.         }
  64.     }
  65.     private SecretKeyStore createFromPath(String key, Object... params) throws SecurityException {
  66.         if(params[0] instanceof String) {
  67.             String pathSecretKey = key;
  68.             String algorithm = (String) params[0];
  69.             if(params.length==2){
  70.                 if( ! (params[1] instanceof BYOKRequestParams) ){
  71.                     throw new SecurityException("Param[1] must be BYOKRequestParams");
  72.                 }
  73.                 BYOKRequestParams requestParams = (BYOKRequestParams) params[1];
  74.                 return new SecretKeyStore(pathSecretKey, algorithm, requestParams);
  75.             }
  76.             else {
  77.                 return new SecretKeyStore(pathSecretKey, algorithm);
  78.             }
  79.         }
  80.         else {
  81.             throw new SecurityException("Param[0] must be String (algorithm)");
  82.         }
  83.     }
  84.     private SecretKeyStore createFromByteArray(Object... params) throws SecurityException {
  85.         if(params[0] instanceof byte[]) {
  86.             if( ! (params[1] instanceof String) ){
  87.                 throw new SecurityException("Param[1] must be String (algorithm)");
  88.             }
  89.             byte [] secretKey = (byte[]) params[0];
  90.             String algorithm = (String) params[1];
  91.             if(params.length==3){
  92.                 if( ! (params[2] instanceof BYOKRequestParams) ){
  93.                     throw new SecurityException("Param[2] must be BYOKRequestParams");
  94.                 }
  95.                 BYOKRequestParams requestParams = (BYOKRequestParams) params[2];
  96.                 return new SecretKeyStore(secretKey, algorithm, requestParams);
  97.             }
  98.             else {
  99.                 return new SecretKeyStore(secretKey, algorithm);
  100.             }
  101.         }
  102.         else {
  103.             throw new SecurityException("Param[0] must be byte[] (secretKey)");
  104.         }
  105.     }
  106.     private SecretKeyStore createFromPasswordKeyDerivation(Object... params) throws SecurityException {
  107.         if(params[0] instanceof SecretPasswordKeyDerivationConfig) {
  108.             SecretPasswordKeyDerivationConfig secretPasswordKeyDerivationConfig = (SecretPasswordKeyDerivationConfig) params[0];
  109.             if(params.length==2){
  110.                 if( ! (params[1] instanceof BYOKRequestParams) ){
  111.                     throw new SecurityException("Param[1] must be BYOKRequestParams");
  112.                 }
  113.                 BYOKRequestParams requestParams = (BYOKRequestParams) params[1];
  114.                 return new SecretKeyStore(secretPasswordKeyDerivationConfig, requestParams);
  115.             }
  116.             else {
  117.                 return new SecretKeyStore(secretPasswordKeyDerivationConfig);
  118.             }
  119.         }
  120.         else {
  121.             throw new SecurityException("Param[0] must be SecretPasswordKeyDerivationConfig");
  122.         }
  123.     }

  124.     @Override
  125.     public String getPrefixKey() {
  126.         return "SecretKey ";
  127.     }
  128. }