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

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

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

  113.     @Override
  114.     public String getPrefixKey() {
  115.         return "KeyPair ";
  116.     }
  117. }