P7MInfo.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.protocol.sdi.utils;

  21. import java.io.ByteArrayInputStream;
  22. import java.io.StringReader;

  23. import org.bouncycastle.util.io.pem.PemReader;
  24. import org.openspcoop2.utils.io.Base64Utilities;
  25. import org.slf4j.Logger;

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

  34.     /*
  35.      * Infatti il formato del file rispecchia quanto indicato nella delibera CNIPA numero 45 del 9 Novembre 2009 all’articolo 21, comma 3.
  36.      *
  37.      * La delibera cita quanto segue:
  38.      * “Per la codifica della busta crittografica deve essere utilizzato il formato
  39.      * ASN.1 (ISO/IEC8824) in rappresentazione binaria (ISO/IEC 8825, BER - DER) o
  40.      * alfanumerica ottenuta applicando la trasformazione BASE 64 (RFC 1421, RFC
  41.      * 2045). La testata e la coda previsti nelle specifiche RFC 1421 e RFC 2045
  42.      * possono essere assenti.”
  43.      *
  44.      * In pratica la fattura P7M puo' essere ricevuta direttamente come
  45.      * rappresentazione binaria o codificata in base64.
  46.      *
  47.      **/
  48.    
  49.     private byte[] xmlDecoded;
  50.     private boolean base64Encoded = false;
  51.    
  52.     public P7MInfo(byte [] fattura, Logger log) throws Throwable{
  53.        
  54.         try{
  55.             org.bouncycastle.cms.CMSSignedData cmsSignedData = new org.bouncycastle.cms.CMSSignedData(new ByteArrayInputStream(fattura));
  56.             this.xmlDecoded = (byte[]) cmsSignedData.getSignedContent().getContent();
  57.            
  58.         }catch(Throwable e){
  59.             //System.out.println("ERRORE");
  60.            
  61.             byte[] decoded = null;
  62.             try{
  63.                 String fatturaS = new String(fattura);
  64.                 if(fatturaS.trim().startsWith("-----BEGIN")) {
  65.                     PemReader pemReader = null;
  66.                     StringReader stringReader = null;
  67.                     try {
  68.                         stringReader = new StringReader(fatturaS);
  69.                         pemReader = new PemReader(stringReader);
  70.                         decoded = pemReader.readPemObject().getContent();
  71.                     }catch(Throwable eDecodePEM) {
  72.                         //System.out.println("ERRORE PEM");
  73.                         log.error("DecodificaBase64 via PEMReader non riuscita: "+eDecodePEM.getMessage(), eDecodePEM);
  74.                     }finally {
  75.                         try {
  76.                             if(pemReader!=null) {
  77.                                 pemReader.close();
  78.                             }
  79.                         }catch(Throwable eClose) {
  80.                             // ignore
  81.                         }
  82.                         try {
  83.                             if(stringReader!=null) {
  84.                                 stringReader.close();
  85.                             }
  86.                         }catch(Throwable eClose) {
  87.                             // ignore
  88.                         }
  89.                     }
  90.                 }
  91. //              else {
  92. //                  System.out.println("NO PEM");
  93. //              }
  94.                 if(decoded==null) {
  95.                     decoded = Base64Utilities.decode(fatturaS);
  96.                 }
  97.             }catch(Throwable eDecode){
  98.                 //System.out.println("ERRORE BASE64");
  99.                 log.error("DecodificaBase64 non riuscita: "+eDecode.getMessage(), eDecode);
  100.                 throw e; // lancio l'eccezione originale, poiche' piu' interessante. La seconda mi informa solo che non e' una rappresentazione Base64
  101.             }
  102.             this.base64Encoded = true;
  103.            
  104.             try{
  105.                 org.bouncycastle.cms.CMSSignedData cmsSignedData = new org.bouncycastle.cms.CMSSignedData(new ByteArrayInputStream(decoded));
  106.                 this.xmlDecoded = (byte[]) cmsSignedData.getSignedContent().getContent();              
  107.             }catch(Throwable eSecond){
  108.                 throw eSecond;
  109.             }
  110.         }
  111.        
  112.     }
  113.    
  114.     public byte[] getXmlDecoded() {
  115.         return this.xmlDecoded;
  116.     }
  117.     public boolean isBase64Encoded() {
  118.         return this.base64Encoded;
  119.     }
  120.    
  121. }