MerlinKeystoreCache.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.MerlinKeystore;
  23. import org.openspcoop2.utils.certificate.byok.BYOKRequestParams;

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

  32.     @Override
  33.     public MerlinKeystore createKeystore(String key, Object... params) throws SecurityException{
  34.         if(params==null){
  35.             throw new SecurityException("Params is null");
  36.         }
  37.         if(params.length==0){
  38.             String propertyFilePath = key;
  39.             return new MerlinKeystore(propertyFilePath);
  40.         }
  41.         else if(params.length==1){
  42.             if( params[0]!=null && ! (params[0] instanceof String) && ! (params[0] instanceof BYOKRequestParams) ){
  43.                 throw new SecurityException("Param[0] must be String (passwordPrivateKey) or BYOKRequestParams");
  44.             }
  45.             String propertyFilePath = key;
  46.             if(params[0]==null || params[0] instanceof String) {
  47.                 String passwordPrivateKey = (String) params[0];
  48.                 return new MerlinKeystore(propertyFilePath, passwordPrivateKey);
  49.             }
  50.             else {
  51.                 BYOKRequestParams requestParams = (BYOKRequestParams) params[0];
  52.                 return new MerlinKeystore(propertyFilePath, requestParams);
  53.             }
  54.         }
  55.         else if(params.length==2){
  56.             if( ! (params[0] instanceof String) ){
  57.                 throw new SecurityException("Param[0] must be String (tipoStore or passwordPrivateKey)");
  58.             }
  59.             if( params[1]!=null && ! (params[1] instanceof String) && ! (params[1] instanceof BYOKRequestParams) ){
  60.                 throw new SecurityException("Param[1] must be String (passwordStore) or BYOKRequestParams");
  61.             }
  62.             String pathStore = key;
  63.             if( params[1]==null || (params[1] instanceof String) ){
  64.                 String tipoStore = (String) params[0];
  65.                 String passwordStore = (String) params[1];
  66.                 return new MerlinKeystore(pathStore, tipoStore, passwordStore);
  67.             }
  68.             else {
  69.                 String passwordPrivateKey = (String) params[0];
  70.                 BYOKRequestParams requestParams = (BYOKRequestParams) params[1];
  71.                 return new MerlinKeystore(pathStore, passwordPrivateKey, requestParams);
  72.             }
  73.         }
  74.         else if(params.length==3){
  75.             if(params[0] instanceof String) {
  76.                 if(params[1]!=null && ! (params[1] instanceof String) ){
  77.                     throw new SecurityException("Param[1] must be String (passwordStore)");
  78.                 }
  79.                 if(params[2] !=null && ! (params[2] instanceof String) && ! (params[2] instanceof BYOKRequestParams) ){
  80.                     throw new SecurityException("Param[2] must be String (passwordPrivateKey) or BYOKRequestParams");
  81.                 }
  82.                 String pathStore = key;
  83.                 String tipoStore = (String) params[0];
  84.                 String passwordStore = (String) params[1];
  85.                 if(params[2]==null || params[2] instanceof String) {
  86.                     String passwordPrivateKey = (String) params[2];
  87.                     return new MerlinKeystore(pathStore, tipoStore, passwordStore, passwordPrivateKey);
  88.                 }
  89.                 else {
  90.                     BYOKRequestParams requestParams = (BYOKRequestParams) params[2];
  91.                     return new MerlinKeystore(pathStore, tipoStore, passwordStore, requestParams);
  92.                 }
  93.             }
  94.             else if(params[0] instanceof byte[]) {
  95.                 if( ! (params[1] instanceof String) ){
  96.                     throw new SecurityException("Param[1] must be String (tipoStore)");
  97.                 }
  98.                 if( params[2]!=null && ! (params[2] instanceof String) ){
  99.                     throw new SecurityException("Param[2] must be String (passwordStore)");
  100.                 }
  101.                 byte [] store = (byte[]) params[0];
  102.                 String tipoStore = (String) params[1];
  103.                 String passwordStore = (String) params[2];
  104.                 return new MerlinKeystore(store, tipoStore, passwordStore);
  105.             }
  106.             else {
  107.                 throw new SecurityException("Param[0] must be String (tipoStore) or byte[] (store)");
  108.             }
  109.         }
  110.         else if(params.length==4){
  111.             if( ! (params[0] instanceof byte[]) && ! (params[0] instanceof String) ){
  112.                 throw new SecurityException("Param[0] must be byte[] (store) or String (tipoStore)");
  113.             }
  114.             if( params[0] instanceof byte[] ){
  115.                 if( ! (params[1] instanceof String) ){
  116.                     throw new SecurityException("Param[1] must be String (tipoStore)");
  117.                 }
  118.                 if( params[2]!=null && ! (params[2] instanceof String) ){
  119.                     throw new SecurityException("Param[2] must be String (passwordStore)");
  120.                 }
  121.                 if( params[3]!=null && ! (params[3] instanceof String) && ! (params[3] instanceof BYOKRequestParams) ){
  122.                     throw new SecurityException("Param[3] must be String (passwordPrivateKey) or BYOKRequestParams");
  123.                 }
  124.                 byte [] store = (byte[]) params[0];
  125.                 String tipoStore = (String) params[1];
  126.                 String passwordStore = (String) params[2];
  127.                 if(params[3]==null || params[3] instanceof String) {
  128.                     String passwordPrivateKey = (String) params[3];
  129.                     return new MerlinKeystore(store, tipoStore, passwordStore, passwordPrivateKey);
  130.                 }
  131.                 else {
  132.                     BYOKRequestParams requestParams = (BYOKRequestParams) params[3];
  133.                     return new MerlinKeystore(store, tipoStore, passwordStore, requestParams);
  134.                 }
  135.             }
  136.             else {
  137.                 if( params[1]!=null && ! (params[1] instanceof String) ){
  138.                     throw new SecurityException("Param[1] must be String (passwordStore)");
  139.                 }
  140.                 if( params[2]!=null && ! (params[2] instanceof String) ){
  141.                     throw new SecurityException("Param[2] must be String (passwordPrivateKey)");
  142.                 }
  143.                 if( ! (params[3] instanceof BYOKRequestParams) ){
  144.                     throw new SecurityException("Param[3] must be BYOKRequestParams");
  145.                 }
  146.                 String pathStore = key;
  147.                 String tipoStore = (String) params[0];
  148.                 String passwordStore = (String) params[1];
  149.                 String passwordPrivateKey = (String) params[2];
  150.                 BYOKRequestParams requestParams = (BYOKRequestParams) params[3];
  151.                 return new MerlinKeystore(pathStore, tipoStore, passwordStore, passwordPrivateKey, requestParams);
  152.             }
  153.         }
  154.         else if(params.length==5){
  155.             if( ! (params[0] instanceof byte[])){
  156.                 throw new SecurityException("Param[0] must be byte[] (store)");
  157.             }
  158.             if( ! (params[1] instanceof String) ){
  159.                 throw new SecurityException("Param[1] must be String (tipoStore)");
  160.             }
  161.             if( params[2]!=null && ! (params[2] instanceof String) ){
  162.                 throw new SecurityException("Param[2] must be String (passwordStore)");
  163.             }
  164.             if( params[3]!=null && ! (params[3] instanceof String) ){
  165.                 throw new SecurityException("Param[3] must be String (passwordPrivateKey)");
  166.             }
  167.             if( ! (params[4] instanceof BYOKRequestParams) ){
  168.                 throw new SecurityException("Param[4] must be BYOKRequestParams");
  169.             }
  170.             byte [] store = (byte[]) params[0];
  171.             String tipoStore = (String) params[1];
  172.             String passwordStore = (String) params[2];
  173.             String passwordPrivateKey = (String) params[3];
  174.             BYOKRequestParams requestParams = (BYOKRequestParams) params[4];
  175.             return new MerlinKeystore(store, tipoStore, passwordStore, passwordPrivateKey, requestParams);
  176.         }
  177.         else{
  178.             throw new SecurityException("Params [lenght:"+params.length+"] not supported");
  179.         }
  180.     }

  181.     @Override
  182.     public String getPrefixKey() {
  183.         return "Keystore ";
  184.     }

  185. }