AbstractXmlCipher.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.Provider;
  22. import java.security.Security;

  23. import javax.crypto.KeyGenerator;
  24. import javax.crypto.SecretKey;

  25. import org.apache.xml.security.encryption.XMLCipher;
  26. import org.bouncycastle.jce.provider.BouncyCastleProvider;
  27. import org.openspcoop2.utils.UtilsException;
  28. import org.openspcoop2.utils.certificate.KeyStore;

  29. /**
  30.  * Encrypt
  31.  *
  32.  * @author Poli Andrea (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public abstract class AbstractXmlCipher {

  37.     protected java.security.Provider provider;
  38.     private java.security.Key keyWrapped;
  39.     private java.security.cert.Certificate certificateWrapped;
  40.     protected SecretKey secretKeyWrapped;
  41.     private int mode;
  42.     private SymmetricKeyWrappedMode encryptedKeyMode;
  43.     protected SecretKey secretKeyEncrypt;

  44.     public boolean isEncryptedKey() throws UtilsException {
  45.         if(this.encryptedKeyMode!=null) {
  46.             switch (this.encryptedKeyMode) {
  47.                 case SYM_ENC_KEY_NO_WRAPPED:
  48.                     return false;
  49.                 case SYM_ENC_KEY_WRAPPED_SYMMETRIC_KEY:
  50.                 case SYM_ENC_KEY_WRAPPED_ASYMMETRIC_KEY:
  51.                     return true;
  52.             }
  53.         }
  54.         throw new UtilsException("Wrapped Key Mode undefined");
  55.     }
  56.    
  57.     // BOTH
  58.    
  59.     protected AbstractXmlCipher(int mode, java.security.KeyStore keystore, boolean symmetricKey, SymmetricKeyWrappedMode wrappedSymmetricKeyMode, String alias, String passwordPrivateKey) throws UtilsException{
  60.         this(mode, new KeyStore(keystore), symmetricKey, wrappedSymmetricKeyMode, alias, passwordPrivateKey, false);
  61.     }
  62.     protected AbstractXmlCipher(int mode, java.security.KeyStore keystore, boolean symmetricKey, SymmetricKeyWrappedMode wrappedSymmetricKeyMode, String alias, String passwordPrivateKey,boolean addBouncyCastleProvider) throws UtilsException{
  63.         this(mode, new KeyStore(keystore), symmetricKey, wrappedSymmetricKeyMode, alias, passwordPrivateKey, addBouncyCastleProvider);
  64.     }
  65.     protected AbstractXmlCipher(int mode, KeyStore keystore, boolean symmetricKey, SymmetricKeyWrappedMode wrappedSymmetricKeyMode, String alias, String passwordPrivateKey) throws UtilsException{
  66.         this(mode, keystore, symmetricKey, wrappedSymmetricKeyMode, alias, passwordPrivateKey, false);
  67.     }
  68.     protected AbstractXmlCipher(int mode, KeyStore keystore, boolean symmetricKey, SymmetricKeyWrappedMode wrappedSymmetricKeyMode, String alias, String passwordPrivateKey,boolean addBouncyCastleProvider) throws UtilsException{
  69.         if(symmetricKey) {
  70.             SecretKey secretKey = keystore.getSecretKey(alias, passwordPrivateKey);
  71.             this.initSymmetric(mode, wrappedSymmetricKeyMode, secretKey, addBouncyCastleProvider);
  72.         }
  73.         else {
  74.             this.initPrivate(mode, keystore, alias, passwordPrivateKey, addBouncyCastleProvider);
  75.         }
  76.     }
  77.    
  78.     protected AbstractXmlCipher(int mode, SymmetricKeyWrappedMode wrappedSymmetricKeyMode, java.security.Key key) throws UtilsException{
  79.         this(mode, wrappedSymmetricKeyMode, key, false);
  80.     }
  81.     protected AbstractXmlCipher(int mode, SymmetricKeyWrappedMode wrappedSymmetricKeyMode, java.security.Key key,boolean addBouncyCastleProvider) throws UtilsException{
  82.         if(key instanceof SecretKey){
  83.             this.initSymmetric(mode, wrappedSymmetricKeyMode, (SecretKey) key, addBouncyCastleProvider);
  84.         }
  85.         else{
  86.             this.mode = mode;
  87.             this.keyWrapped = key;
  88.             this.encryptedKeyMode = SymmetricKeyWrappedMode.SYM_ENC_KEY_WRAPPED_ASYMMETRIC_KEY;
  89.             this.init(addBouncyCastleProvider);
  90.         }
  91.     }

  92.     protected AbstractXmlCipher(int mode, SymmetricKeyWrappedMode wrappedSymmetricKeyMode, java.security.KeyStore keystore, String alias, String passwordPrivateKey) throws UtilsException{
  93.         this(mode, wrappedSymmetricKeyMode, new KeyStore(keystore), alias, passwordPrivateKey, false);
  94.     }
  95.     protected AbstractXmlCipher(int mode, SymmetricKeyWrappedMode wrappedSymmetricKeyMode, java.security.KeyStore keystore, String alias, String passwordPrivateKey,boolean addBouncyCastleProvider) throws UtilsException{
  96.         this(mode, wrappedSymmetricKeyMode, new KeyStore(keystore), alias, passwordPrivateKey, addBouncyCastleProvider);
  97.     }
  98.     protected AbstractXmlCipher(int mode, SymmetricKeyWrappedMode wrappedSymmetricKeyMode, KeyStore keystore, String alias, String passwordPrivateKey) throws UtilsException{
  99.         this(mode, wrappedSymmetricKeyMode, keystore, alias, passwordPrivateKey, false);
  100.     }
  101.     protected AbstractXmlCipher(int mode, SymmetricKeyWrappedMode wrappedSymmetricKeyMode, KeyStore keystore, String alias, String passwordPrivateKey,boolean addBouncyCastleProvider) throws UtilsException{
  102.         if("jceks".equalsIgnoreCase(keystore.getKeystore().getType())) {
  103.             SecretKey secretKey = keystore.getSecretKey(alias, passwordPrivateKey);
  104.             this.initSymmetric(mode, wrappedSymmetricKeyMode, secretKey, addBouncyCastleProvider);
  105.         }
  106.         else {
  107.             this.initPrivate(mode, keystore, alias, passwordPrivateKey, addBouncyCastleProvider);
  108.         }
  109.     }
  110.    
  111.    
  112.     // SYMMETRIC
  113.    
  114.     protected AbstractXmlCipher(int mode, SymmetricKeyWrappedMode wrappedSymmetricKeyMode, SecretKey secretKey) throws UtilsException{
  115.         this(mode, wrappedSymmetricKeyMode, secretKey, false);
  116.     }
  117.     protected AbstractXmlCipher(int mode, SymmetricKeyWrappedMode wrappedSymmetricKeyMode, SecretKey secretKey,boolean addBouncyCastleProvider) throws UtilsException{
  118.         this.initSymmetric(mode, wrappedSymmetricKeyMode, secretKey, addBouncyCastleProvider);
  119.     }
  120.    
  121.     protected AbstractXmlCipher(int mode, SymmetricKeyWrappedMode wrappedSymmetricKeyMode, String keyAlgorithm) throws UtilsException{
  122.         this(mode, wrappedSymmetricKeyMode, keyAlgorithm, false);
  123.     }
  124.     protected AbstractXmlCipher(int mode, SymmetricKeyWrappedMode wrappedSymmetricKeyMode, String keyAlgorithm,boolean addBouncyCastleProvider) throws UtilsException{
  125.         this(mode, wrappedSymmetricKeyMode, generateSecretKey(keyAlgorithm),addBouncyCastleProvider);
  126.     }
  127.    
  128.     private void initSymmetric(int mode, SymmetricKeyWrappedMode wrappedSymmetricKeyMode, SecretKey secretKey, boolean addBouncyCastleProvider) throws UtilsException{
  129.         this.mode = mode;
  130.         switch (wrappedSymmetricKeyMode) {
  131.         case SYM_ENC_KEY_NO_WRAPPED:
  132.             this.secretKeyEncrypt  = secretKey;
  133.             break;
  134.         default:
  135.             this.secretKeyWrapped = secretKey;
  136.             break;
  137.         }
  138.         this.encryptedKeyMode = wrappedSymmetricKeyMode;
  139.         this.init(addBouncyCastleProvider);
  140.     }
  141.    
  142.     public SecretKey getSecretKeyEncryption() {
  143.         return this.secretKeyEncrypt;
  144.     }
  145.     public static SecretKey generateSecretKey(String algorithm) throws UtilsException {
  146.         return generateSecretKey(algorithm, null);
  147.     }
  148.     public static SecretKey generateSecretKey(String algorithm, Provider provider) throws UtilsException {
  149.         KeyGenerator keyGenerator = null;
  150.         try {
  151.             if(provider!=null) {
  152.                 keyGenerator = KeyGenerator.getInstance(algorithm, provider);
  153.             }
  154.             else {
  155.                 keyGenerator = KeyGenerator.getInstance(algorithm);
  156.             }
  157.         }catch(Exception e){
  158.             throw new UtilsException(e.getMessage(),e);
  159.         }
  160.         return keyGenerator.generateKey();
  161.     }
  162.    
  163.    
  164.     // ASYMMETRIC
  165.    
  166.     private void initPrivate(int mode, KeyStore keystore, String alias, String passwordPrivateKey,boolean addBouncyCastleProvider) throws UtilsException{
  167.         this.mode = mode;
  168.         this.keyWrapped = keystore.getPrivateKey(alias, passwordPrivateKey);
  169.         this.encryptedKeyMode = SymmetricKeyWrappedMode.SYM_ENC_KEY_WRAPPED_ASYMMETRIC_KEY;
  170.         // Non sembra necessario
  171. /**     if(keystore.getKeystoreType().equalsIgnoreCase(KeystoreType.PKCS11.getNome())) {
  172. //          this.provider = keystore.getKeystoreProvider();
  173. //      }*/
  174.         this.init(addBouncyCastleProvider);
  175.     }

  176.     protected AbstractXmlCipher(int mode, java.security.cert.Certificate certificate) throws UtilsException{
  177.         this(mode, certificate, false);
  178.     }
  179.     protected AbstractXmlCipher(int mode, java.security.cert.Certificate certificate,boolean addBouncyCastleProvider) throws UtilsException{
  180.         this.mode = mode;
  181.         this.certificateWrapped = certificate;
  182.         this.encryptedKeyMode = SymmetricKeyWrappedMode.SYM_ENC_KEY_WRAPPED_ASYMMETRIC_KEY;
  183.         this.init(addBouncyCastleProvider);
  184.     }

  185.     protected AbstractXmlCipher(int mode, java.security.KeyStore keystore, String alias) throws UtilsException{
  186.         this(mode, new KeyStore(keystore), alias, false);
  187.     }
  188.     protected AbstractXmlCipher(int mode, java.security.KeyStore keystore, String alias,boolean addBouncyCastleProvider) throws UtilsException{
  189.         this(mode, new KeyStore(keystore), alias, addBouncyCastleProvider);
  190.     }
  191.     protected AbstractXmlCipher(int mode, KeyStore keystore, String alias) throws UtilsException{
  192.         this(mode, keystore, alias, false);
  193.     }
  194.     protected AbstractXmlCipher(int mode, KeyStore keystore, String alias,boolean addBouncyCastleProvider) throws UtilsException{
  195.         this.mode = mode;
  196.         this.certificateWrapped = keystore.getCertificate(alias);
  197.         this.encryptedKeyMode = SymmetricKeyWrappedMode.SYM_ENC_KEY_WRAPPED_ASYMMETRIC_KEY;
  198.         this.init(addBouncyCastleProvider);
  199.     }

  200.     protected AbstractXmlCipher(int mode, java.security.KeyStore keystore) throws UtilsException{
  201.         this(mode, new KeyStore(keystore), false);
  202.     }
  203.     protected AbstractXmlCipher(int mode, java.security.KeyStore keystore,boolean addBouncyCastleProvider) throws UtilsException{
  204.         this(mode, new KeyStore(keystore), addBouncyCastleProvider);
  205.     }
  206.     protected AbstractXmlCipher(int mode, KeyStore keystore) throws UtilsException{
  207.         this(mode, keystore, false);
  208.     }
  209.     protected AbstractXmlCipher(int mode, KeyStore keystore,boolean addBouncyCastleProvider) throws UtilsException{
  210.         this.mode = mode;
  211.         this.certificateWrapped = keystore.getCertificate();
  212.         this.encryptedKeyMode = SymmetricKeyWrappedMode.SYM_ENC_KEY_WRAPPED_ASYMMETRIC_KEY;
  213.         this.init(addBouncyCastleProvider);
  214.     }

  215.     private void init(boolean addBouncyCastleProvider) throws UtilsException{
  216.         try{

  217.             // Providers
  218.             if(addBouncyCastleProvider){
  219.                 BouncyCastleProvider bouncyCastleProvider = new BouncyCastleProvider();
  220.                 Security.addProvider(bouncyCastleProvider);
  221.             }

  222.             org.apache.xml.security.Init.init();

  223.         }catch(Exception e){
  224.             throw new UtilsException(e.getMessage(),e);
  225.         }
  226.     }

  227.     protected org.apache.xml.security.encryption.XMLCipher getXMLCipher() throws UtilsException{
  228.         try{
  229.             org.apache.xml.security.encryption.XMLCipher xmlCipher = null;
  230.             if(this.provider!=null) {
  231.                 xmlCipher = org.apache.xml.security.encryption.XMLCipher.getInstance(this.provider.getName());
  232.             }
  233.             else {
  234.                 xmlCipher = org.apache.xml.security.encryption.XMLCipher.getInstance();
  235.             }
  236.             xmlCipher.init(this.mode, null);
  237.             return xmlCipher;          
  238.         }catch(Exception e){
  239.             throw new UtilsException(e.getMessage(),e);
  240.         }
  241.     }

  242.     protected org.apache.xml.security.encryption.XMLCipher getXMLCipher(String algorithm, String canon, String digest) throws UtilsException{
  243.         try{
  244.             org.apache.xml.security.encryption.XMLCipher xmlCipher = null;
  245.             if(canon!=null || digest!=null) {
  246.                 if(this.provider!=null) {
  247.                     xmlCipher = org.apache.xml.security.encryption.XMLCipher.getProviderInstance(algorithm, this.provider.getName(), canon, digest);
  248.                 }
  249.                 else {
  250.                     xmlCipher = org.apache.xml.security.encryption.XMLCipher.getInstance(algorithm,canon,digest);
  251.                 }
  252.             }
  253.             else {
  254.                 if(this.provider!=null) {
  255.                     xmlCipher = org.apache.xml.security.encryption.XMLCipher.getProviderInstance(algorithm, this.provider.getName());
  256.                 }
  257.                 else {
  258.                     xmlCipher = org.apache.xml.security.encryption.XMLCipher.getInstance(algorithm);
  259.                 }
  260.             }
  261.             if(this.secretKeyEncrypt==null) {
  262.                 throw new UtilsException("Key for initialize cipher engine not found");
  263.             }
  264.             xmlCipher.init(this.mode, this.secretKeyEncrypt);
  265.             return xmlCipher;          
  266.         }catch(Exception e){
  267.             throw new UtilsException(e.getMessage(),e);
  268.         }
  269.     }
  270.    
  271.     protected org.apache.xml.security.encryption.XMLCipher getXMLCipherUnwrappedKey() throws UtilsException{
  272.         return this.getXMLCipherWrappedKey(null);
  273.     }
  274.     protected org.apache.xml.security.encryption.XMLCipher getXMLCipherWrappedKey(String algorithm) throws UtilsException{
  275.         try{
  276.             org.apache.xml.security.encryption.XMLCipher xmlCipher = null;
  277.             if(algorithm!=null){
  278.                 if(this.provider!=null) {
  279.                     xmlCipher = org.apache.xml.security.encryption.XMLCipher.getProviderInstance(algorithm, this.provider.getName());
  280.                 }
  281.                 else {
  282.                     xmlCipher = org.apache.xml.security.encryption.XMLCipher.getInstance(algorithm);
  283.                 }
  284.             }
  285.             else{
  286.                 if(this.provider!=null) {
  287.                     xmlCipher = org.apache.xml.security.encryption.XMLCipher.getProviderInstance(this.provider.getName());
  288.                 }
  289.                 else {
  290.                     xmlCipher = org.apache.xml.security.encryption.XMLCipher.getInstance();
  291.                 }
  292.             }
  293.             int wrapMode = -1;
  294.             if(XMLCipher.ENCRYPT_MODE == this.mode){
  295.                 wrapMode = XMLCipher.WRAP_MODE;
  296.             }
  297.             else{
  298.                 wrapMode = XMLCipher.UNWRAP_MODE;
  299.             }
  300.             if(this.secretKeyWrapped!=null){
  301.                 xmlCipher.init(wrapMode, this.secretKeyWrapped);
  302.             }
  303.             else if(this.keyWrapped!=null){
  304.                 xmlCipher.init(wrapMode, this.keyWrapped);
  305.             }
  306.             else if(this.certificateWrapped!=null){
  307.                 xmlCipher.init(wrapMode, this.certificateWrapped.getPublicKey());
  308.             }
  309.             else {
  310.                 throw new UtilsException("Key for initialize cipher engine 'wrapped key' not found");
  311.             }
  312.             return xmlCipher;          
  313.         }catch(Exception e){
  314.             throw new UtilsException(e.getMessage(),e);
  315.         }
  316.     }

  317. }