XmlDecrypt.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.PrivateKey;
  22. import java.security.PublicKey;
  23. import java.security.cert.Certificate;

  24. import javax.crypto.SecretKey;

  25. import org.apache.xml.security.encryption.EncryptedData;
  26. import org.apache.xml.security.encryption.EncryptedKey;
  27. import org.apache.xml.security.encryption.XMLCipher;
  28. import org.openspcoop2.utils.UtilsException;
  29. import org.openspcoop2.utils.certificate.KeyStore;
  30. import org.openspcoop2.utils.xml.DynamicNamespaceContext;
  31. import org.openspcoop2.utils.xml.XPathExpressionEngine;
  32. import org.openspcoop2.utils.xml.XPathReturnType;
  33. import org.w3c.dom.Document;
  34. import org.w3c.dom.Element;

  35. /**
  36.  * Encrypt
  37.  *
  38.  * @author Poli Andrea (apoli@link.it)
  39.  * @author $Author$
  40.  * @version $Rev$, $Date$
  41.  */
  42. public class XmlDecrypt extends AbstractXmlCipher {
  43.    
  44.     // BOTH
  45.    
  46.     public XmlDecrypt(java.security.KeyStore keystore, boolean symmetricKey, SymmetricKeyWrappedMode wrappedSymmetricKeyMode, String alias, String passwordPrivateKey) throws UtilsException{
  47.         super(XMLCipher.DECRYPT_MODE, keystore, symmetricKey, wrappedSymmetricKeyMode, alias, passwordPrivateKey, false);
  48.     }
  49.     public XmlDecrypt(java.security.KeyStore keystore, boolean symmetricKey, SymmetricKeyWrappedMode wrappedSymmetricKeyMode, String alias, String passwordPrivateKey,boolean addBouncyCastleProvider) throws UtilsException{
  50.         super(XMLCipher.DECRYPT_MODE, keystore, symmetricKey, wrappedSymmetricKeyMode, alias, passwordPrivateKey, addBouncyCastleProvider);
  51.     }
  52.     public XmlDecrypt(KeyStore keystore, boolean symmetricKey, SymmetricKeyWrappedMode wrappedSymmetricKeyMode, String alias, String passwordPrivateKey) throws UtilsException{
  53.         super(XMLCipher.DECRYPT_MODE, keystore, symmetricKey, wrappedSymmetricKeyMode, alias, passwordPrivateKey, false);
  54.     }
  55.     public XmlDecrypt(KeyStore keystore, boolean symmetricKey, SymmetricKeyWrappedMode wrappedSymmetricKeyMode, String alias, String passwordPrivateKey,boolean addBouncyCastleProvider) throws UtilsException{
  56.         super(XMLCipher.DECRYPT_MODE, keystore, symmetricKey, wrappedSymmetricKeyMode, alias, passwordPrivateKey, addBouncyCastleProvider);
  57.     }
  58.    
  59.     public XmlDecrypt(SymmetricKeyWrappedMode wrappedSymmetricKeyMode, java.security.KeyStore keystore, String alias, String passwordPrivateKey,
  60.             boolean addBouncyCastleProvider) throws UtilsException {
  61.         super(XMLCipher.DECRYPT_MODE, wrappedSymmetricKeyMode, keystore, alias, passwordPrivateKey, addBouncyCastleProvider);
  62.     }
  63.     public XmlDecrypt(SymmetricKeyWrappedMode wrappedSymmetricKeyMode, java.security.KeyStore keystore, String alias, String passwordPrivateKey) throws UtilsException {
  64.         super(XMLCipher.DECRYPT_MODE, wrappedSymmetricKeyMode, keystore, alias, passwordPrivateKey);
  65.     }
  66.     public XmlDecrypt(SymmetricKeyWrappedMode wrappedSymmetricKeyMode, KeyStore keystore, String alias, String passwordPrivateKey,
  67.             boolean addBouncyCastleProvider) throws UtilsException {
  68.         super(XMLCipher.DECRYPT_MODE, wrappedSymmetricKeyMode, keystore, alias, passwordPrivateKey, addBouncyCastleProvider);
  69.     }
  70.     public XmlDecrypt(SymmetricKeyWrappedMode wrappedSymmetricKeyMode, KeyStore keystore, String alias, String passwordPrivateKey) throws UtilsException {
  71.         super(XMLCipher.DECRYPT_MODE, wrappedSymmetricKeyMode, keystore, alias, passwordPrivateKey);
  72.     }

  73.    
  74.    
  75.     // SYMMETRIC
  76.    
  77.     public XmlDecrypt(SymmetricKeyWrappedMode wrappedSymmetricKeyMode, SecretKey secretKey) throws UtilsException{
  78.         super(XMLCipher.DECRYPT_MODE, wrappedSymmetricKeyMode, secretKey);
  79.     }
  80.     public XmlDecrypt(SymmetricKeyWrappedMode wrappedSymmetricKeyMode, SecretKey secretKey,boolean addBouncyCastleProvider) throws UtilsException{
  81.         super(XMLCipher.DECRYPT_MODE, wrappedSymmetricKeyMode, secretKey, addBouncyCastleProvider);
  82.     }
  83.    
  84.     // Questo costruttore non ha senso, una chiave segreta ci vuole!
  85. //  public XmlDecrypt(String keyAlgorithm, boolean addBouncyCastleProvider)
  86. //          throws UtilsException {
  87. //      super(XMLCipher.DECRYPT_MODE, keyAlgorithm, addBouncyCastleProvider);
  88. //  }
  89. //  public XmlDecrypt(String keyAlgorithm) throws UtilsException {
  90. //      super(XMLCipher.DECRYPT_MODE, keyAlgorithm);
  91. //  }

  92.    
  93.     // ASYMMETRIC
  94.    
  95.     public XmlDecrypt(Certificate certificate, boolean addBouncyCastleProvider) throws UtilsException {
  96.         super(XMLCipher.DECRYPT_MODE, certificate, addBouncyCastleProvider);
  97.     }
  98.     public XmlDecrypt(Certificate certificate) throws UtilsException {
  99.         super(XMLCipher.DECRYPT_MODE, certificate);
  100.     }

  101.     public XmlDecrypt(PrivateKey key, boolean addBouncyCastleProvider) throws UtilsException {
  102.         super(XMLCipher.DECRYPT_MODE, SymmetricKeyWrappedMode.SYM_ENC_KEY_WRAPPED_ASYMMETRIC_KEY, key, addBouncyCastleProvider);
  103.     }
  104.     public XmlDecrypt(PrivateKey key) throws UtilsException {
  105.         super(XMLCipher.DECRYPT_MODE, SymmetricKeyWrappedMode.SYM_ENC_KEY_WRAPPED_ASYMMETRIC_KEY, key);
  106.     }

  107.     public XmlDecrypt(PublicKey key, boolean addBouncyCastleProvider) throws UtilsException {
  108.         super(XMLCipher.DECRYPT_MODE, SymmetricKeyWrappedMode.SYM_ENC_KEY_WRAPPED_ASYMMETRIC_KEY, key, addBouncyCastleProvider);
  109.     }
  110.     public XmlDecrypt(PublicKey key) throws UtilsException {
  111.         super(XMLCipher.DECRYPT_MODE, SymmetricKeyWrappedMode.SYM_ENC_KEY_WRAPPED_ASYMMETRIC_KEY, key);
  112.     }

  113.     public XmlDecrypt(java.security.KeyStore keystore, boolean addBouncyCastleProvider) throws UtilsException {
  114.         super(XMLCipher.DECRYPT_MODE, keystore, addBouncyCastleProvider);
  115.     }
  116.     public XmlDecrypt(java.security.KeyStore keystore) throws UtilsException {
  117.         super(XMLCipher.DECRYPT_MODE, keystore);
  118.     }
  119.     public XmlDecrypt(KeyStore keystore, boolean addBouncyCastleProvider) throws UtilsException {
  120.         super(XMLCipher.DECRYPT_MODE, keystore, addBouncyCastleProvider);
  121.     }
  122.     public XmlDecrypt(KeyStore keystore) throws UtilsException {
  123.         super(XMLCipher.DECRYPT_MODE, keystore);
  124.     }

  125.     public XmlDecrypt(java.security.KeyStore keystore, String alias, boolean addBouncyCastleProvider)
  126.             throws UtilsException {
  127.         super(XMLCipher.DECRYPT_MODE, keystore, alias, addBouncyCastleProvider);
  128.     }
  129.     public XmlDecrypt(java.security.KeyStore keystore, String alias) throws UtilsException {
  130.         super(XMLCipher.DECRYPT_MODE, keystore, alias);
  131.     }
  132.     public XmlDecrypt(KeyStore keystore, String alias, boolean addBouncyCastleProvider)
  133.             throws UtilsException {
  134.         super(XMLCipher.DECRYPT_MODE, keystore, alias, addBouncyCastleProvider);
  135.     }
  136.     public XmlDecrypt(KeyStore keystore, String alias) throws UtilsException {
  137.         super(XMLCipher.DECRYPT_MODE, keystore, alias);
  138.     }


  139.     public Document decrypt(Document document) throws UtilsException{
  140.         return this.decrypt(document, document.getDocumentElement());
  141.     }
  142.     public Document decrypt(Element element) throws UtilsException{
  143.         return this.decrypt(element.getOwnerDocument(), element);
  144.     }
  145.     public Document decrypt(Document document, Element element) throws UtilsException{
  146.         try{
  147.             XPathExpressionEngine xpathEngine = new XPathExpressionEngine();
  148.             DynamicNamespaceContext dnc = new DynamicNamespaceContext();
  149.             dnc.findPrefixNamespace(element);
  150.            
  151.             // Estrazione element xenc
  152.            
  153.             Element encryptedDataElement = null;
  154.             Object o = xpathEngine.getMatchPattern(element, dnc, "//{http://www.w3.org/2001/04/xmlenc#}EncryptedData", XPathReturnType.NODE);
  155.             if (o == null) {
  156.                 throw new Exception("EncryptData not found");
  157.             }
  158.             encryptedDataElement = (Element) o;
  159.            
  160.             Element encryptedKeyElement = null;
  161.             if(super.isEncryptedKey()){
  162.                 o = xpathEngine.getMatchPattern(encryptedDataElement, dnc, "//{http://www.w3.org/2001/04/xmlenc#}EncryptedKey", XPathReturnType.NODE);
  163.                 if (o == null) {
  164.                     throw new Exception("EncryptedKey not found");
  165.                 }
  166.                 encryptedKeyElement = (Element) o;
  167.             }
  168.            
  169.             // Identificazione algoritmi
  170.            
  171.             org.apache.xml.security.encryption.XMLCipher xmlCipherReaderAlgo = super.getXMLCipher();
  172.             EncryptedData encryptedData = xmlCipherReaderAlgo.loadEncryptedData(document, encryptedDataElement);
  173.             EncryptedKey encryptedKey = null;
  174.             if(super.isEncryptedKey()){
  175.                 encryptedKey = xmlCipherReaderAlgo.loadEncryptedKey(document, encryptedKeyElement);
  176.             }
  177.             String encryptAlgorithm = encryptedData.getEncryptionMethod().getAlgorithm();
  178.             String digestAlgorithm = encryptedData.getEncryptionMethod().getDigestAlgorithm();
  179.             String canonicalizationAlgorithm = null; // TODO
  180.             if(super.isEncryptedKey()){
  181.                 org.apache.xml.security.encryption.XMLCipher xmlCipherUnwrap = super.getXMLCipherUnwrappedKey();
  182.                 super.secretKeyEncrypt = (SecretKey) xmlCipherUnwrap.decryptKey(encryptedKey, encryptAlgorithm);
  183.             }
  184.                    
  185.             org.apache.xml.security.encryption.XMLCipher xmlCipher = super.getXMLCipher(encryptAlgorithm, canonicalizationAlgorithm, digestAlgorithm);
  186.             return xmlCipher.doFinal(document, encryptedDataElement);          
  187.         }catch(Exception e){
  188.             throw new UtilsException(e.getMessage(),e);
  189.         }
  190.     }

  191. }