VerifyXmlSignature.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.security;

  21. import java.security.Security;
  22. import java.security.cert.Certificate;
  23. import java.security.cert.X509Certificate;

  24. import org.apache.xml.security.keys.KeyInfo;
  25. import org.bouncycastle.jce.provider.BouncyCastleProvider;
  26. import org.openspcoop2.utils.UtilsException;
  27. import org.openspcoop2.utils.certificate.KeyStore;
  28. import org.openspcoop2.utils.xml.DynamicNamespaceContext;
  29. import org.openspcoop2.utils.xml.XPathExpressionEngine;
  30. import org.openspcoop2.utils.xml.XPathReturnType;
  31. import org.w3c.dom.Document;
  32. import org.w3c.dom.Element;
  33. import org.w3c.dom.Node;

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

  42.     private KeyStore keystore;
  43.     private Certificate certificate;
  44.    
  45.     private KeyInfo keyInfo;
  46.        
  47.     public VerifyXmlSignature(java.security.KeyStore keystore) throws UtilsException{
  48.         this(new KeyStore(keystore), null, false);
  49.     }
  50.     public VerifyXmlSignature(java.security.KeyStore keystore, String alias) throws UtilsException{
  51.         this(new KeyStore(keystore), alias, false);
  52.     }
  53.     public VerifyXmlSignature(java.security.KeyStore keystore, boolean addBouncyCastleProvider) throws UtilsException{
  54.         this(new KeyStore(keystore), null, addBouncyCastleProvider);
  55.     }
  56.     public VerifyXmlSignature(java.security.KeyStore keystore, String alias, boolean addBouncyCastleProvider) throws UtilsException{
  57.         this(new KeyStore(keystore), alias, addBouncyCastleProvider);
  58.     }
  59.    
  60.     public VerifyXmlSignature(KeyStore keystore) throws UtilsException{
  61.         this(keystore, null, false);
  62.     }
  63.     public VerifyXmlSignature(KeyStore keystore, String alias) throws UtilsException{
  64.         this(keystore, alias, false);
  65.     }
  66.     public VerifyXmlSignature(KeyStore keystore, boolean addBouncyCastleProvider) throws UtilsException{
  67.         this(keystore, null, addBouncyCastleProvider);
  68.     }
  69.     public VerifyXmlSignature(KeyStore keystore, String alias, boolean addBouncyCastleProvider) throws UtilsException{
  70.         this.keystore = keystore;
  71.         if(alias==null){
  72.             this.certificate = this.keystore.getCertificate();
  73.         }
  74.         else{
  75.             this.certificate = this.keystore.getCertificate(alias);
  76.         }
  77.         try{
  78.        
  79.             // Providers
  80.             if(addBouncyCastleProvider){
  81.                 BouncyCastleProvider bouncyCastleProvider = new BouncyCastleProvider();
  82.                 Security.addProvider(bouncyCastleProvider);
  83.             }
  84.            
  85.             org.apache.xml.security.Init.init();
  86.            
  87.         }catch(Exception e){
  88.             throw new UtilsException(e.getMessage(),e);
  89.         }
  90.     }

  91.     private Node signatureElement;

  92.     public boolean verify(Document element, boolean clean) throws UtilsException{
  93.         return this._verify(element.getDocumentElement(), clean);
  94.     }
  95.     public boolean verify(Element element, boolean clean) throws UtilsException{
  96.         return this._verify(element, clean);
  97.     }
  98.     private boolean _verify(Element element, boolean clean) throws UtilsException{
  99.         try{
  100.            
  101.             XPathExpressionEngine xpathEngine = new XPathExpressionEngine();
  102.             DynamicNamespaceContext dnc = new DynamicNamespaceContext();
  103.             dnc.findPrefixNamespace(element);
  104.             Object o = xpathEngine.getMatchPattern(element, dnc, "//{http://www.w3.org/2000/09/xmldsig#}Signature", XPathReturnType.NODE);
  105.             if (o == null) {
  106.                 throw new Exception("Signature element not found");
  107.             }
  108.             this.signatureElement = (Node) o;
  109.            
  110.             org.apache.xml.security.signature.XMLSignature sigXMLSec = new org.apache.xml.security.signature.XMLSignature((Element)this.signatureElement, null);
  111.            
  112.             boolean valida = sigXMLSec.checkSignatureValue((X509Certificate)this.certificate);
  113.            
  114.             this.keyInfo = sigXMLSec.getKeyInfo();
  115.            
  116.             // elimino elemento signature dal document
  117.             if(clean){
  118.                 this.detach(element);
  119.             }
  120.            
  121.             return valida;
  122.            
  123.         }catch(Exception e){
  124.             throw new UtilsException(e.getMessage(),e);
  125.         }
  126.     }

  127.     public KeyInfo getKeyInfo() {
  128.         return this.keyInfo;
  129.     }
  130.    
  131.     public void detach(Element element) throws UtilsException {
  132.         try{
  133.             if(this.signatureElement==null) {
  134.                 throw new Exception("Signature element not found; invoke 'verify' method first");
  135.             }
  136.             element.removeChild(this.signatureElement);
  137.         }catch(Exception e){
  138.             throw new UtilsException(e.getMessage(),e);
  139.         }
  140.     }

  141. }