RemoteStoreProvider.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.pdd.core.keystore;

  21. import java.io.ByteArrayOutputStream;
  22. import java.security.PublicKey;

  23. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  24. import org.openspcoop2.security.keystore.RemoteStore;
  25. import org.openspcoop2.utils.UtilsException;
  26. import org.openspcoop2.utils.certificate.Certificate;
  27. import org.openspcoop2.utils.certificate.JWK;
  28. import org.openspcoop2.utils.certificate.remote.IRemoteStoreProvider;
  29. import org.openspcoop2.utils.certificate.remote.RemoteKeyType;
  30. import org.openspcoop2.utils.certificate.remote.RemoteStoreClientInfo;
  31. import org.openspcoop2.utils.certificate.remote.RemoteStoreConfig;

  32. /**
  33.  * RemoteStoreProvider
  34.  *
  35.  * @author Poli Andrea (apoli@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  */
  39. public class RemoteStoreProvider implements IRemoteStoreProvider {

  40.     private RemoteKeyType keyType;
  41.    
  42.     private RequestInfo requestInfo;
  43.    
  44.     public RemoteStoreProvider(RequestInfo requestInfo, RemoteKeyType keyType) throws KeystoreException {
  45.        
  46.         this.requestInfo = requestInfo;
  47.        
  48.         if(keyType==null) {
  49.             throw new KeystoreException("RemoteKeyType undefined");
  50.         }
  51.         this.keyType = keyType;
  52.     }
  53.    
  54.        
  55.     @Override
  56.     public JWK readJWK(String keyId, RemoteStoreConfig remoteConfig) throws UtilsException {
  57.         try {
  58.             return GestoreKeystoreCaching.getRemoteStore(this.requestInfo, keyId, this.keyType, remoteConfig, RemoteStoreProviderDriver.getProviderStore(remoteConfig.getStoreName())).getJWK();
  59.         }catch(Exception e) {
  60.             throw new UtilsException(e.getMessage(),e);
  61.         }
  62.     }
  63.     @Override
  64.     public JWK readJWK(String keyId, RemoteStoreConfig remoteConfig, ByteArrayOutputStream bout) throws UtilsException {
  65.         try {
  66.             RemoteStore remoteStore = GestoreKeystoreCaching.getRemoteStore(this.requestInfo, keyId, this.keyType, remoteConfig, RemoteStoreProviderDriver.getProviderStore(remoteConfig.getStoreName()));
  67.             if(bout!=null) {
  68.                 bout.write(remoteStore.getResource());
  69.             }
  70.             return remoteStore.getJWK();
  71.         }catch(Exception e) {
  72.             throw new UtilsException(e.getMessage(),e);
  73.         }
  74.     }
  75.     @Override
  76.     public Certificate readX509(String keyId, RemoteStoreConfig remoteConfig) throws UtilsException {
  77.         try {
  78.             return GestoreKeystoreCaching.getRemoteStore(this.requestInfo, keyId, this.keyType, remoteConfig, RemoteStoreProviderDriver.getProviderStore(remoteConfig.getStoreName())).getCertificate();
  79.         }catch(Exception e) {
  80.             throw new UtilsException(e.getMessage(),e);
  81.         }
  82.     }
  83.     @Override
  84.     public Certificate readX509(String keyId, RemoteStoreConfig remoteConfig, ByteArrayOutputStream bout)
  85.             throws UtilsException {
  86.         try {
  87.             RemoteStore remoteStore = GestoreKeystoreCaching.getRemoteStore(this.requestInfo, keyId, this.keyType, remoteConfig, RemoteStoreProviderDriver.getProviderStore(remoteConfig.getStoreName()));
  88.             if(bout!=null) {
  89.                 bout.write(remoteStore.getResource());
  90.             }
  91.             return remoteStore.getCertificate();
  92.         }catch(Exception e) {
  93.             throw new UtilsException(e.getMessage(),e);
  94.         }
  95.     }
  96.     @Override
  97.     public PublicKey readPublicKey(String keyId, RemoteStoreConfig remoteConfig) throws UtilsException {
  98.         try {
  99.             return GestoreKeystoreCaching.getRemoteStore(this.requestInfo, keyId, this.keyType, remoteConfig, RemoteStoreProviderDriver.getProviderStore(remoteConfig.getStoreName())).getPublicKey();
  100.         }catch(Exception e) {
  101.             throw new UtilsException(e.getMessage(),e);
  102.         }
  103.     }
  104.     @Override
  105.     public PublicKey readPublicKey(String keyId, RemoteStoreConfig remoteConfig, ByteArrayOutputStream bout)
  106.             throws UtilsException {
  107.         try {
  108.             RemoteStore remoteStore = GestoreKeystoreCaching.getRemoteStore(this.requestInfo, keyId, this.keyType, remoteConfig, RemoteStoreProviderDriver.getProviderStore(remoteConfig.getStoreName()));
  109.             if(bout!=null) {
  110.                 bout.write(remoteStore.getResource());
  111.             }
  112.             return remoteStore.getPublicKey();
  113.         }catch(Exception e) {
  114.             throw new UtilsException(e.getMessage(),e);
  115.         }
  116.     }

  117.     @Override
  118.     public RemoteStoreClientInfo readClientInfo(String keyId, String clientId, RemoteStoreConfig remoteConfig, org.openspcoop2.utils.Map<Object> context)
  119.             throws UtilsException {
  120.         try {
  121.             return RemoteStoreProviderDriver.getProviderStore(remoteConfig.getStoreName()).readClientInfo(keyId, clientId, remoteConfig, context);
  122.         }catch(Exception e) {
  123.             throw new UtilsException(e.getMessage(),e);
  124.         }
  125.     }
  126.    
  127. }