KeystoreUtils.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;

  21. import java.io.ByteArrayInputStream;
  22. import java.io.InputStream;
  23. import java.security.KeyStore;
  24. import java.security.Provider;
  25. import java.security.Security;

  26. import org.openspcoop2.utils.Utilities;
  27. import org.openspcoop2.utils.UtilsException;


  28. /**
  29.  * KeystoreUtils
  30.  *
  31.  * @author Poli Andrea (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class KeystoreUtils {

  36.     private KeystoreUtils() {}
  37.    
  38.     public static KeyStore readKeystore(InputStream is, String tipoKeystore, String passwordKeystore) throws UtilsException{
  39.         byte [] keystoreBytes = Utilities.getAsByteArray(is);
  40.         return readKeystore(keystoreBytes, tipoKeystore, passwordKeystore);
  41.     }
  42.     public static KeyStore readKeystore(byte [] keystoreBytes, String tipoKeystore, String passwordKeystore) throws UtilsException{
  43.        
  44.         try (ByteArrayInputStream bin = new ByteArrayInputStream(keystoreBytes)){
  45.             java.security.KeyStore keystore = java.security.KeyStore.getInstance(tipoKeystore);
  46.             keystore.load(bin, passwordKeystore!=null ? passwordKeystore.toCharArray() : "".toCharArray());
  47.             return keystore;
  48.         }catch(Exception e){
  49.            
  50.             // Fix #128
  51.             try (ByteArrayInputStream bin = new ByteArrayInputStream(keystoreBytes)){
  52.                 Provider p = Security.getProvider(org.bouncycastle.jce.provider.BouncyCastleProvider.PROVIDER_NAME);
  53.                 if(p!=null) {
  54.                     java.security.KeyStore keystore = java.security.KeyStore.getInstance(tipoKeystore, p);
  55.                     keystore.load(bin, passwordKeystore!=null ? passwordKeystore.toCharArray() : "".toCharArray());
  56.                     return keystore;
  57.                 }
  58.             }catch(Exception eFix){
  59.                 // ignore
  60.             }
  61.            
  62.             // rilancio eccezione originale
  63.             throw new UtilsException(e.getMessage(),e);
  64.         }
  65.     }
  66.    
  67. }