PEMReader.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.util.ArrayList;
  23. import java.util.List;

  24. import org.apache.commons.lang.StringUtils;

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

  33.     private static final long serialVersionUID = 1L;
  34.    
  35.     public static final String PKCS1_BEGIN = "-----BEGIN RSA PRIVATE KEY-----";
  36.     public static final String PKCS1_END = "-----END RSA PRIVATE KEY-----";

  37.     public static final String PKCS8_BEGIN = "-----BEGIN PRIVATE KEY-----";
  38.     public static final String PKCS8_END = "-----END PRIVATE KEY-----";
  39.    
  40.     public static final String PKCS8_ENCRYPTED_BEGIN = "-----BEGIN ENCRYPTED PRIVATE KEY-----";
  41.     public static final String PKCS8_ENCRYPTED_END = "-----END ENCRYPTED PRIVATE KEY-----";
  42.    
  43.     public static final String PUBLIC_KEY_BEGIN = "-----BEGIN PUBLIC KEY-----";
  44.     public static final String PUBLIC_KEY_END = "-----END PUBLIC KEY-----";
  45.    
  46.     public static final String X509_BEGIN = "-----BEGIN CERTIFICATE-----";
  47.     public static final String X509_END = "-----END CERTIFICATE-----";
  48.    
  49.    
  50.     private String privateKey;
  51.     private String publicKey;
  52.     private List<String> certificates = new ArrayList<>();
  53.     private boolean pkcs1; // l'encrypted dell'1 รจ una informazione interna
  54.     private boolean pkcs8;
  55.     private boolean pkcs8encrypted;
  56.    
  57.     public PEMReader(byte[] pem) {
  58.         this(pem, true, true, true);
  59.     }
  60.     public PEMReader(byte[] pem, boolean pkcs1, boolean pkcs8, boolean pkcs8encrypted) {
  61.         this(getAsString(pem), pkcs1, pkcs8, pkcs8encrypted);
  62.     }
  63.     static String getAsString(byte[] pem) {
  64.         String publicKeyString = null;
  65.         try {
  66.             publicKeyString = new String(pem);
  67.         }catch(Exception e) {
  68.             // ignore
  69.         }
  70.         return publicKeyString;
  71.     }
  72.     public PEMReader(String pem) {
  73.         this(pem, true, true, true);
  74.     }
  75.     public PEMReader(String pem, boolean pkcs1, boolean pkcs8, boolean pkcs8encrypted) {
  76.        
  77.         if(pem==null) {
  78.             return;
  79.         }
  80.        
  81.         pem = pem.trim();
  82.        
  83.         if(pkcs1 && pem.contains(PKCS1_BEGIN)) {
  84.             this.privateKey = read(pem, PKCS1_BEGIN, PKCS1_END);
  85.             this.pkcs1 = this.privateKey!=null && StringUtils.isNotEmpty(this.privateKey);
  86.         }
  87.         else if(pkcs8 && pem.contains(PKCS8_BEGIN)) {
  88.             this.privateKey = read(pem, PKCS8_BEGIN, PKCS8_END);
  89.             this.pkcs8 = this.privateKey!=null && StringUtils.isNotEmpty(this.privateKey);
  90.         }
  91.         else if(pkcs8encrypted && pem.contains(PKCS8_ENCRYPTED_BEGIN)) {
  92.             this.privateKey = read(pem, PKCS8_ENCRYPTED_BEGIN, PKCS8_ENCRYPTED_END);
  93.             this.pkcs8encrypted = this.privateKey!=null && StringUtils.isNotEmpty(this.privateKey);
  94.         }
  95.        
  96.         if(pem.contains(PUBLIC_KEY_BEGIN)) {
  97.             this.publicKey = read(pem, PUBLIC_KEY_BEGIN, PUBLIC_KEY_END);
  98.         }
  99.        
  100.         if(pem.contains(X509_BEGIN)) {
  101.             initCertificate(pem);
  102.         }
  103.     }
  104.     private void initCertificate(String pem) {
  105.         String cert = read(pem, X509_BEGIN, X509_END);
  106.         String check = pem;
  107.         int index = 0;
  108.         while(cert!=null && StringUtils.isNotEmpty(cert) && index<=1000) {
  109.             this.certificates.add(cert);
  110.             check = check.replace(cert, "");
  111.             if(check.contains(X509_BEGIN)) {
  112.                 cert = read(check, X509_BEGIN, X509_END);
  113.             }
  114.             else {
  115.                 cert=null;
  116.             }
  117.             index++;
  118.         }
  119.     }
  120.    
  121.     private String read(String pem, String begin, String end) {
  122.         if(pem.contains(begin) && pem.contains(end)) {
  123.             int indexOf = pem.indexOf(begin);
  124.             if(indexOf>=0) {
  125.                 String tmp = indexOf>0 ? pem.substring(indexOf) : pem;
  126.                 int indexOfEnd = tmp.indexOf(end);
  127.                 if(indexOfEnd>=0) {
  128.                     return readEngine(tmp, indexOfEnd);
  129.                 }
  130.             }
  131.         }
  132.         return null;
  133.     }
  134.     private String readEngine(String tmp, int indexOfEnd) {
  135.         /**indexOfEnd = indexOfEnd + end.length();
  136.         String s =  (indexOfEnd==(tmp.length()-1)) ? tmp : tmp.substring(0,indexOfEnd);
  137.         System.out.println("LETTO ["+begin+"]: '"+s+"'");
  138.         return s;*/
  139.        
  140.         int indexOfEndLine = tmp.indexOf("\n", indexOfEnd+1);
  141.         /**System.out.println("END LINE '"+indexOfEndLine+"'");*/
  142.         if(indexOfEndLine<=0) {
  143.             indexOfEndLine = tmp.indexOf("\r", indexOfEnd+1);
  144.             /**System.out.println("END LINE 2 '"+indexOfEndLine+"'");*/
  145.         }
  146.         if(indexOfEndLine<=0) {
  147.             indexOfEndLine = tmp.length();
  148.             /**System.out.println("END LINE 3 '"+indexOfEndLine+"'");*/
  149.         }
  150.        
  151.         String s = tmp.substring(0,indexOfEndLine);
  152.         if(s!=null) {
  153.             s = s.trim();
  154.         }
  155.         /**System.out.println("LETTO ["+begin+"]: '"+s+"'");*/
  156.         return s;
  157.     }
  158.    
  159.     public String getPrivateKey() {
  160.         return this.privateKey;
  161.     }
  162.     public String getPublicKey() {
  163.         return this.publicKey;
  164.     }
  165.     public List<String> getCertificates() {
  166.         return this.certificates;
  167.     }
  168.    
  169.     public boolean isPkcs1() {
  170.         return this.pkcs1;
  171.     }
  172.     public boolean isPkcs8() {
  173.         return this.pkcs8;
  174.     }
  175.     public boolean isPkcs8encrypted() {
  176.         return this.pkcs8encrypted;
  177.     }
  178.    
  179. }