PEMArchive.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.Serializable;
  22. import java.security.PrivateKey;
  23. import java.security.PublicKey;
  24. import java.util.ArrayList;
  25. import java.util.List;

  26. import org.openspcoop2.utils.UtilsException;

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

  35.     private static final long serialVersionUID = 1L;
  36.    
  37.     private PEMReader reader;
  38.     private String algo;
  39.     private String keyPassword;
  40.    
  41.     private transient Boolean initialized;
  42.     private transient PrivateKey privateKey;
  43.     private transient PublicKey publicKey;
  44.     private transient List<CertificateInfo> certificates = null;
  45.    
  46.     public PEMArchive(byte[] pem) throws UtilsException {
  47.         this(pem, KeyUtils.ALGO_RSA, null);
  48.     }
  49.     public PEMArchive(byte[] pem, String keyPassword) throws UtilsException {
  50.         this(pem, KeyUtils.ALGO_RSA, keyPassword);
  51.     }
  52.     public PEMArchive(byte[] pem, String algo, String keyPassword) throws UtilsException {
  53.         this.reader = new PEMReader(pem);
  54.         this.algo = algo;
  55.         this.keyPassword = keyPassword;
  56.         this.init();
  57.     }
  58.    
  59.     public PEMArchive(String pem) throws UtilsException {
  60.         this(pem, KeyUtils.ALGO_RSA, null);
  61.     }
  62.     public PEMArchive(String pem, String keyPassword) throws UtilsException {
  63.         this(pem, KeyUtils.ALGO_RSA, keyPassword);
  64.     }
  65.     public PEMArchive(String pem, String algo, String keyPassword) throws UtilsException {
  66.         this.reader = new PEMReader(pem);
  67.         this.algo = algo;
  68.         this.keyPassword = keyPassword;
  69.         this.init();
  70.     }
  71.    
  72.     private synchronized void init() throws UtilsException {
  73.        
  74.         if(this.initialized==null || !this.initialized.booleanValue()) {
  75.        
  76.             KeyUtils keyUtils = KeyUtils.getInstance(this.algo);
  77.            
  78.             initPrivateKey(keyUtils);
  79.            
  80.             initPublicKey(keyUtils);
  81.                        
  82.             initCertificates();
  83.            
  84.             this.initialized = true;
  85.            
  86.         }
  87.     }
  88.     private void initPrivateKey(KeyUtils keyUtils) throws UtilsException {
  89.         if(this.reader.getPrivateKey()!=null) {
  90.             if(this.keyPassword!=null) {
  91.                 try {
  92.                     this.privateKey = keyUtils.getPrivateKey(this.reader.getPrivateKey().getBytes(), this.keyPassword);
  93.                 }catch(Exception e) {
  94.                     throw new UtilsException("Load encrypted private key failed: "+e.getMessage(),e);
  95.                 }
  96.             }
  97.             else {
  98.                 try {
  99.                     this.privateKey = keyUtils.getPrivateKey(this.reader.getPrivateKey().getBytes());
  100.                 }catch(Exception e) {
  101.                     throw new UtilsException("Load private key failed: "+e.getMessage(),e);
  102.                 }
  103.             }
  104.         }
  105.     }
  106.     private void initPublicKey(KeyUtils keyUtils) throws UtilsException {
  107.         if(this.reader.getPublicKey()!=null) {
  108.             try {
  109.                 this.publicKey = keyUtils.getPublicKey(this.reader.getPublicKey().getBytes());
  110.             }catch(Exception e) {
  111.                 throw new UtilsException("Load public key failed: "+e.getMessage(),e);
  112.             }
  113.         }
  114.     }
  115.     private void initCertificates() throws UtilsException {
  116.         if(this.reader.getCertificates()!=null && !this.reader.getCertificates().isEmpty()) {
  117.             this.certificates = new ArrayList<>();
  118.             for (String c : this.reader.getCertificates()) {
  119.                 try {
  120.                     CertificateInfo cInfo = ArchiveLoader.load(c.getBytes()).getCertificate();
  121.                     this.certificates.add(cInfo);
  122.                 }catch(Exception e) {
  123.                     throw new UtilsException("Load x509 failed: "+e.getMessage(),e);
  124.                 }
  125.             }
  126.         }
  127.     }
  128.    
  129.     private void checkInit() throws UtilsException {
  130.         if(this.initialized==null || !this.initialized.booleanValue()) {
  131.             this.init();
  132.         }
  133.     }
  134.    
  135.     public PrivateKey getPrivateKey() throws UtilsException {
  136.         checkInit();
  137.         return this.privateKey;
  138.     }
  139.     public PublicKey getPublicKey() throws UtilsException {
  140.         checkInit();
  141.         return this.publicKey;
  142.     }
  143.     public List<CertificateInfo> getCertificates() throws UtilsException {
  144.         checkInit();
  145.         return this.certificates;
  146.     }
  147. }