XmlEncrypt.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.apache.xml.security.keys.KeyInfo;
  29. import org.openspcoop2.utils.UtilsException;
  30. import org.openspcoop2.utils.certificate.KeyStore;
  31. import org.w3c.dom.Document;
  32. import org.w3c.dom.Element;

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

  41.     // BOTH
  42.    
  43.     public XmlEncrypt(java.security.KeyStore keystore, boolean symmetricKey, SymmetricKeyWrappedMode wrappedSymmetricKeyMode, String alias, String passwordPrivateKey) throws UtilsException{
  44.         super(XMLCipher.ENCRYPT_MODE, keystore, symmetricKey, wrappedSymmetricKeyMode, alias, passwordPrivateKey, false);
  45.     }
  46.     public XmlEncrypt(java.security.KeyStore keystore, boolean symmetricKey, SymmetricKeyWrappedMode wrappedSymmetricKeyMode, String alias, String passwordPrivateKey,boolean addBouncyCastleProvider) throws UtilsException{
  47.         super(XMLCipher.ENCRYPT_MODE, keystore, symmetricKey, wrappedSymmetricKeyMode, alias, passwordPrivateKey, addBouncyCastleProvider);
  48.     }
  49.     public XmlEncrypt(KeyStore keystore, boolean symmetricKey, SymmetricKeyWrappedMode wrappedSymmetricKeyMode, String alias, String passwordPrivateKey) throws UtilsException{
  50.         super(XMLCipher.ENCRYPT_MODE, keystore, symmetricKey, wrappedSymmetricKeyMode, alias, passwordPrivateKey, false);
  51.     }
  52.     public XmlEncrypt(KeyStore keystore, boolean symmetricKey, SymmetricKeyWrappedMode wrappedSymmetricKeyMode, String alias, String passwordPrivateKey,boolean addBouncyCastleProvider) throws UtilsException{
  53.         super(XMLCipher.ENCRYPT_MODE, keystore, symmetricKey, wrappedSymmetricKeyMode, alias, passwordPrivateKey, addBouncyCastleProvider);
  54.     }
  55.    
  56.     public XmlEncrypt(SymmetricKeyWrappedMode wrappedSymmetricKeyMode, java.security.KeyStore keystore, String alias, String passwordPrivateKey,
  57.             boolean addBouncyCastleProvider) throws UtilsException {
  58.         super(XMLCipher.ENCRYPT_MODE, wrappedSymmetricKeyMode, keystore, alias, passwordPrivateKey, addBouncyCastleProvider);
  59.     }
  60.     public XmlEncrypt(SymmetricKeyWrappedMode wrappedSymmetricKeyMode, java.security.KeyStore keystore, String alias, String passwordPrivateKey) throws UtilsException {
  61.         super(XMLCipher.ENCRYPT_MODE, wrappedSymmetricKeyMode, keystore, alias, passwordPrivateKey);
  62.     }
  63.     public XmlEncrypt(SymmetricKeyWrappedMode wrappedSymmetricKeyMode, KeyStore keystore, String alias, String passwordPrivateKey,
  64.             boolean addBouncyCastleProvider) throws UtilsException {
  65.         super(XMLCipher.ENCRYPT_MODE, wrappedSymmetricKeyMode, keystore, alias, passwordPrivateKey, addBouncyCastleProvider);
  66.     }
  67.     public XmlEncrypt(SymmetricKeyWrappedMode wrappedSymmetricKeyMode, KeyStore keystore, String alias, String passwordPrivateKey) throws UtilsException {
  68.         super(XMLCipher.ENCRYPT_MODE, wrappedSymmetricKeyMode, keystore, alias, passwordPrivateKey);
  69.     }
  70.    
  71.    
  72.     // SYMMETRIC
  73.    
  74.     public XmlEncrypt(SymmetricKeyWrappedMode wrappedSymmetricKeyMode, SecretKey secretKey) throws UtilsException{
  75.         super(XMLCipher.ENCRYPT_MODE, wrappedSymmetricKeyMode, secretKey);
  76.     }
  77.     public XmlEncrypt(SymmetricKeyWrappedMode wrappedSymmetricKeyMode, SecretKey secretKey,boolean addBouncyCastleProvider) throws UtilsException{
  78.         super(XMLCipher.ENCRYPT_MODE, wrappedSymmetricKeyMode, secretKey, addBouncyCastleProvider);
  79.     }
  80.    
  81.     public XmlEncrypt(SymmetricKeyWrappedMode wrappedSymmetricKeyMode, String keyAlgorithm, boolean addBouncyCastleProvider)
  82.             throws UtilsException {
  83.         super(XMLCipher.ENCRYPT_MODE, wrappedSymmetricKeyMode, keyAlgorithm, addBouncyCastleProvider);
  84.     }
  85.     public XmlEncrypt(SymmetricKeyWrappedMode wrappedSymmetricKeyMode, String keyAlgorithm) throws UtilsException {
  86.         super(XMLCipher.ENCRYPT_MODE, wrappedSymmetricKeyMode, keyAlgorithm);
  87.     }

  88.    
  89.     // ASYMMETRIC
  90.    
  91.     public XmlEncrypt(Certificate certificate, boolean addBouncyCastleProvider) throws UtilsException {
  92.         super(XMLCipher.ENCRYPT_MODE, certificate, addBouncyCastleProvider);
  93.     }
  94.     public XmlEncrypt(Certificate certificate) throws UtilsException {
  95.         super(XMLCipher.ENCRYPT_MODE, certificate);
  96.     }

  97.     public XmlEncrypt(PrivateKey key, boolean addBouncyCastleProvider) throws UtilsException {
  98.         super(XMLCipher.ENCRYPT_MODE, SymmetricKeyWrappedMode.SYM_ENC_KEY_WRAPPED_ASYMMETRIC_KEY, key, addBouncyCastleProvider);
  99.     }
  100.     public XmlEncrypt(PrivateKey key) throws UtilsException {
  101.         super(XMLCipher.ENCRYPT_MODE, SymmetricKeyWrappedMode.SYM_ENC_KEY_WRAPPED_ASYMMETRIC_KEY, key);
  102.     }
  103.    
  104.     public XmlEncrypt(PublicKey key, boolean addBouncyCastleProvider) throws UtilsException {
  105.         super(XMLCipher.ENCRYPT_MODE, SymmetricKeyWrappedMode.SYM_ENC_KEY_WRAPPED_ASYMMETRIC_KEY, key, addBouncyCastleProvider);
  106.     }
  107.     public XmlEncrypt(PublicKey key) throws UtilsException {
  108.         super(XMLCipher.ENCRYPT_MODE, SymmetricKeyWrappedMode.SYM_ENC_KEY_WRAPPED_ASYMMETRIC_KEY, key);
  109.     }

  110.     public XmlEncrypt(java.security.KeyStore keystore, boolean addBouncyCastleProvider) throws UtilsException {
  111.         super(XMLCipher.ENCRYPT_MODE, keystore, addBouncyCastleProvider);
  112.     }
  113.     public XmlEncrypt(java.security.KeyStore keystore) throws UtilsException {
  114.         super(XMLCipher.ENCRYPT_MODE, keystore);
  115.     }
  116.     public XmlEncrypt(KeyStore keystore, boolean addBouncyCastleProvider) throws UtilsException {
  117.         super(XMLCipher.ENCRYPT_MODE, keystore, addBouncyCastleProvider);
  118.     }
  119.     public XmlEncrypt(KeyStore keystore) throws UtilsException {
  120.         super(XMLCipher.ENCRYPT_MODE, keystore);
  121.     }

  122.     public XmlEncrypt(java.security.KeyStore keystore, String alias, boolean addBouncyCastleProvider)
  123.             throws UtilsException {
  124.         super(XMLCipher.ENCRYPT_MODE, keystore, alias, addBouncyCastleProvider);
  125.     }
  126.     public XmlEncrypt(java.security.KeyStore keystore, String alias) throws UtilsException {
  127.         super(XMLCipher.ENCRYPT_MODE, keystore, alias);
  128.     }
  129.     public XmlEncrypt(KeyStore keystore, String alias, boolean addBouncyCastleProvider)
  130.             throws UtilsException {
  131.         super(XMLCipher.ENCRYPT_MODE, keystore, alias, addBouncyCastleProvider);
  132.     }
  133.     public XmlEncrypt(KeyStore keystore, String alias) throws UtilsException {
  134.         super(XMLCipher.ENCRYPT_MODE, keystore, alias);
  135.     }



  136.     public Document encryptSymmetric(Document document, String encryptAlgorithm) throws UtilsException{
  137.         return this.encrypt(document, document.getDocumentElement(), encryptAlgorithm, null, null, null, null);
  138.     }
  139.     public Document encryptSymmetric(Document document, String encryptAlgorithm, String canonicalizationMethod, String digestMethod) throws UtilsException{
  140.         return this.encrypt(document, document.getDocumentElement(), encryptAlgorithm, canonicalizationMethod, digestMethod, null, null);
  141.     }
  142.     public Document encrypt(Document document, String encryptAlgorithm, String keyAlgorithm, String wrappedKeyAlgorithm) throws UtilsException{
  143.         return this.encrypt(document, document.getDocumentElement(), encryptAlgorithm, null, null, keyAlgorithm, wrappedKeyAlgorithm);
  144.     }
  145.     public Document encrypt(Document document, String encryptAlgorithm, String keyAlgorithm, String canonicalizationMethod, String digestMethod,
  146.             String wrappedKeyAlgorithm) throws UtilsException{
  147.         return this.encrypt(document, document.getDocumentElement(), encryptAlgorithm, canonicalizationMethod, digestMethod,keyAlgorithm, wrappedKeyAlgorithm);
  148.     }
  149.    
  150.     public Document encryptSymmetric(Element element, String encryptAlgorithm) throws UtilsException{
  151.         return this.encrypt(element.getOwnerDocument(), element, encryptAlgorithm, null, null, null, null);
  152.     }
  153.     public Document encryptSymmetric(Element element, String encryptAlgorithm, String canonicalizationMethod, String digestMethod) throws UtilsException{
  154.         return this.encrypt(element.getOwnerDocument(), element, encryptAlgorithm, canonicalizationMethod, digestMethod, null, null);
  155.     }
  156.     public Document encrypt(Element element, String encryptAlgorithm, String keyAlgorithm, String wrappedKeyAlgorithm) throws UtilsException{
  157.         return this.encrypt(element.getOwnerDocument(), element, encryptAlgorithm, null,null, keyAlgorithm, wrappedKeyAlgorithm);
  158.     }
  159.     public Document encrypt(Element element, String encryptAlgorithm, String canonicalizationMethod, String digestMethod,
  160.             String keyAlgorithm, String wrappedKeyAlgorithm) throws UtilsException{
  161.         return this.encrypt(element.getOwnerDocument(), element, encryptAlgorithm, canonicalizationMethod, digestMethod, keyAlgorithm, wrappedKeyAlgorithm);
  162.     }
  163.    
  164.     // String canonicalizationMethod, String digestMethod
  165.    
  166.     public Document encryptSymmetric(Document document, Element element, String encryptAlgorithm) throws UtilsException{
  167.         return this.encrypt(document, element, encryptAlgorithm, null, null, null, null);
  168.     }
  169.     public Document encryptSymmetric(Document document, Element element, String encryptAlgorithm, String canonicalizationMethod, String digestMethod) throws UtilsException{
  170.         return this.encrypt(document, element, encryptAlgorithm, canonicalizationMethod, digestMethod, null, null);
  171.     }
  172.     public Document encrypt(Document document, Element element, String encryptAlgorithm, String keyAlgorithm, String wrappedKeyAlgorithm) throws UtilsException{
  173.         return this.encrypt(document, element, encryptAlgorithm, null, null, keyAlgorithm, wrappedKeyAlgorithm);
  174.     }
  175.     public Document encrypt(Document document, Element element, String encryptAlgorithm, String canonicalizationMethod, String digestMethod,
  176.             String keyAlgorithm, String wrappedKeyAlgorithm) throws UtilsException{
  177.         try{
  178.             if(encryptAlgorithm==null){
  179.                 throw new UtilsException("Encrypt Algorithm undefined");
  180.             }
  181.                        
  182.             EncryptedKey encryptedKey = null;
  183.             if(super.isEncryptedKey()){
  184.                 if(wrappedKeyAlgorithm==null){
  185.                     throw new UtilsException("WrappedKeyAlgorithm undefined");
  186.                 }
  187.                 //System.out.println("PROVIDER ["+this.provider+"]");
  188.                 org.apache.xml.security.encryption.XMLCipher xmlCipherWrappedAlgorithm = super.getXMLCipherWrappedKey(wrappedKeyAlgorithm);
  189.                 if(keyAlgorithm==null){
  190.                     throw new UtilsException("KeyAlgorithm undefined");
  191.                 }
  192.                 super.secretKeyEncrypt = generateSecretKey(keyAlgorithm, this.provider);
  193.                 encryptedKey = xmlCipherWrappedAlgorithm.encryptKey(document, super.secretKeyEncrypt);
  194.             }
  195.            
  196.             org.apache.xml.security.encryption.XMLCipher xmlCipher = super.getXMLCipher(encryptAlgorithm, canonicalizationMethod, digestMethod);
  197.            
  198.             if(encryptedKey!=null){
  199.                 EncryptedData encryptedData = xmlCipher.getEncryptedData();
  200.                 KeyInfo info = new KeyInfo(document);
  201.                 info.add(encryptedKey);
  202.                 encryptedData.setKeyInfo(info);
  203.             }
  204.            
  205.             return xmlCipher.doFinal(document, element, true);          
  206.         }catch(Exception e){
  207.             throw new UtilsException(e.getMessage(),e);
  208.         }
  209.     }

  210. }