RemoteStoreUtils.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.utils.certificate.remote;

  21. import java.io.ByteArrayOutputStream;
  22. import java.security.PublicKey;
  23. import java.util.HashMap;

  24. import org.apache.commons.lang.StringUtils;
  25. import org.openspcoop2.utils.UtilsException;
  26. import org.openspcoop2.utils.certificate.ArchiveLoader;
  27. import org.openspcoop2.utils.certificate.Certificate;
  28. import org.openspcoop2.utils.certificate.JWK;
  29. import org.openspcoop2.utils.certificate.KeyUtils;
  30. import org.openspcoop2.utils.resources.Charset;
  31. import org.openspcoop2.utils.transport.TransportUtils;
  32. import org.openspcoop2.utils.transport.http.ExternalResourceUtils;

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

  41.     private RemoteStoreUtils() {}

  42.     public static JWK readJWK(String keyId, RemoteStoreConfig remoteConfig) throws UtilsException {
  43.         return (JWK) readResource(keyId, remoteConfig, RemoteKeyType.JWK, null);
  44.     }
  45.     public static JWK readJWK(String keyId, RemoteStoreConfig remoteConfig, ByteArrayOutputStream bout) throws UtilsException {
  46.         return (JWK) readResource(keyId, remoteConfig, RemoteKeyType.JWK, bout);
  47.     }
  48.     public static Certificate readX509(String keyId, RemoteStoreConfig remoteConfig) throws UtilsException {
  49.         return (Certificate) readResource(keyId, remoteConfig, RemoteKeyType.X509, null);
  50.     }
  51.     public static Certificate readX509(String keyId, RemoteStoreConfig remoteConfig, ByteArrayOutputStream bout) throws UtilsException {
  52.         return (Certificate) readResource(keyId, remoteConfig, RemoteKeyType.X509, bout);
  53.     }
  54.     public static PublicKey readPublicKey(String keyId, RemoteStoreConfig remoteConfig) throws UtilsException {
  55.         return (PublicKey) readResource(keyId, remoteConfig, RemoteKeyType.PUBLIC_KEY, null);
  56.     }
  57.     public static PublicKey readPublicKey(String keyId, RemoteStoreConfig remoteConfig, ByteArrayOutputStream bout) throws UtilsException {
  58.         return (PublicKey) readResource(keyId, remoteConfig, RemoteKeyType.PUBLIC_KEY, bout);
  59.     }
  60.     private static Object readResource(String keyId, RemoteStoreConfig remoteConfig, RemoteKeyType keyType, ByteArrayOutputStream bout) throws UtilsException {
  61.         try {

  62.             checkParams(keyId, remoteConfig, keyType);
  63.            
  64.             String url = remoteConfig.getBaseUrl();
  65.            
  66.             switch (remoteConfig.getIdMode()) {
  67.             case URL_SUFFIX:
  68.                 if(!remoteConfig.getBaseUrl().endsWith("/")) {
  69.                     url = url + "/";
  70.                 }
  71.                 url = url + TransportUtils.urlEncodePath(keyId, Charset.UTF_8.getValue());
  72.                 break;
  73.             case URL_PARAMETER:
  74.                 remoteConfig.setQueryParameters(new HashMap<>());
  75.                 remoteConfig.getQueryParameters().put(remoteConfig.getParameterName(),
  76.                         TransportUtils.urlEncodeParam(keyId, Charset.UTF_8.getValue()));
  77.                 break;
  78.             case HEADER:
  79.                 remoteConfig.setHeaders(new HashMap<>());
  80.                 remoteConfig.getHeaders().put(remoteConfig.getParameterName(), keyId);
  81.                 break;
  82.             default:
  83.                 break;
  84.             }
  85.            
  86.             byte [] resource = ExternalResourceUtils.readResource(url, remoteConfig);
  87.            
  88.             if(resource==null || resource.length<=0) {
  89.                 throw new UtilsException("Retrieved empty key?");
  90.             }
  91.            
  92.             if(bout!=null) {
  93.                 bout.write(resource);
  94.             }
  95.            
  96.             switch (keyType) {
  97.             case JWK:
  98.                 return new JWK(new String(resource));
  99.             case X509:
  100.                 return ArchiveLoader.load(resource);
  101.             case PUBLIC_KEY:
  102.                 if(remoteConfig.getKeyAlgorithm()==null) {
  103.                     throw new UtilsException("Key algorithm undefined");
  104.                 }
  105.                 return KeyUtils.getInstance(remoteConfig.getKeyAlgorithm()).getPublicKey(resource);
  106.             }
  107.            
  108.             throw new UtilsException("Key unknown");
  109.            
  110.         }catch(Exception t) {
  111.             throw new UtilsException("Retrieve remote key '"+keyId+"' failed: "+t.getMessage(),t);
  112.         }
  113.     }
  114.    
  115.     private static void checkParams(String keyId, RemoteStoreConfig remoteConfig, RemoteKeyType keyType) throws UtilsException {
  116.         if(keyId==null) {
  117.             throw new UtilsException("KeyId undefined");
  118.         }
  119.         if(remoteConfig==null) {
  120.             throw new UtilsException("Config undefined");
  121.         }
  122.         if(keyType==null) {
  123.             throw new UtilsException("KeyType undefined");
  124.         }
  125.        
  126.         if(remoteConfig.getBaseUrl()==null || StringUtils.isEmpty(remoteConfig.getBaseUrl())) {
  127.             throw new UtilsException("BaseUrl undefined");
  128.         }

  129.         if(remoteConfig.getIdMode()==null) {
  130.             throw new UtilsException("KeyId mode undefined");
  131.         }
  132.     }
  133.    
  134. }