SoapBoxSecurityConfig.java

  1. /*
  2.  * AdroitLogic UltraESB Enterprise Service Bus
  3.  *
  4.  * Copyright (c) 2010-2012 AdroitLogic Private Ltd. (http://adroitlogic.org). All Rights Reserved.
  5.  *
  6.  * GNU Affero General Public License Usage
  7.  *
  8.  * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General
  9.  * Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option)
  10.  * any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  13.  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for
  14.  * more details.
  15.  *
  16.  * You should have received a copy of the GNU Affero General Public License along with this program (See LICENSE-AGPL.TXT).
  17.  * If not, see http://www.gnu.org/licenses/agpl-3.0.html
  18.  *
  19.  * Commercial Usage
  20.  *
  21.  * Licensees holding valid UltraESB Commercial licenses may use this file in accordance with the UltraESB Commercial
  22.  * License Agreement provided with the Software or, alternatively, in accordance with the terms contained in a written
  23.  * agreement between you and AdroitLogic.
  24.  *
  25.  * If you are unsure which license is appropriate for your use, or have questions regarding the use of this file,
  26.  * please contact AdroitLogic at info@adroitlogic.com
  27.  */
  28. /*
  29.  * Modificato da Link.it (https://link.it) per supportare le seguenti funzionalità:
  30.  * - firma e cifratura degli attachments
  31.  * - cifratura con chiave simmetrica
  32.  * - supporto CRL
  33.  *
  34.  * Copyright (c) 2011-2025 Link.it srl (https://link.it).
  35.  *
  36.  */

  37. package org.openspcoop2.security.message.soapbox;

  38. import java.security.InvalidAlgorithmParameterException;
  39. import java.security.Key;
  40. import java.security.KeyStore;
  41. import java.security.KeyStoreException;
  42. import java.security.NoSuchAlgorithmException;
  43. import java.security.UnrecoverableKeyException;
  44. import java.security.cert.CertPath;
  45. import java.security.cert.CertPathValidator;
  46. import java.security.cert.CertificateException;
  47. import java.security.cert.CertificateExpiredException;
  48. import java.security.cert.CertificateFactory;
  49. import java.security.cert.CertificateNotYetValidException;
  50. import java.security.cert.PKIXParameters;
  51. import java.security.cert.X509Certificate;
  52. import java.util.Arrays;
  53. import java.util.Map;
  54. import java.util.WeakHashMap;

  55. import org.openspcoop2.core.constants.Costanti;
  56. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  57. import org.openspcoop2.security.SecurityException;
  58. import org.openspcoop2.security.keystore.CRLCertstore;
  59. import org.openspcoop2.security.keystore.cache.GestoreKeystoreCache;
  60. import org.openspcoop2.utils.date.DateManager;

  61. /**
  62.  * SecurityConfig
  63.  *
  64.  * Author of the original AdroitLogic code:
  65.  * @author asankha
  66.  *
  67.  * Authors of the Link.it modification to the code:
  68.  * @author Andrea Poli (apoli@link.it)
  69.  * @author Giovanni Bussu (bussu@link.it)
  70.  * @author $Author$
  71.  * @version $Rev$, $Date$
  72.  */
  73. public class SoapBoxSecurityConfig extends org.adroitlogic.soapbox.SecurityConfig {

  74.    
  75.     private boolean symmetricSharedKey = false;
  76.     public boolean isSymmetricSharedKey() {
  77.         return this.symmetricSharedKey;
  78.     }
  79.     public void setSymmetricSharedKey(boolean symmetricSharedKey) {
  80.         this.symmetricSharedKey = symmetricSharedKey;
  81.     }
  82.    
  83.    
  84.     private KeyStore identityStore = null;
  85.    
  86.     public SoapBoxSecurityConfig(org.openspcoop2.utils.certificate.KeyStore identityStore, org.openspcoop2.utils.certificate.KeyStore trustStore, Map<String, String> keyPasswords,
  87.             org.openspcoop2.utils.Map<Object> ctx)
  88.             throws KeyStoreException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, CertificateException {
  89.         this(identityStore, trustStore, keyPasswords, null,
  90.                 ctx);
  91.     }
  92.     public SoapBoxSecurityConfig(org.openspcoop2.utils.certificate.KeyStore identityStore, org.openspcoop2.utils.certificate.KeyStore trustStore, Map<String, String> keyPasswords,String crlPath,
  93.             org.openspcoop2.utils.Map<Object> ctx)
  94.         throws KeyStoreException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, CertificateException {
  95.         this(identityStore!=null? identityStore.getKeystore() : null,
  96.                 trustStore!=null ? trustStore.getKeystore() : null,
  97.                         keyPasswords, crlPath,
  98.                         ctx);
  99.     }
  100.     public SoapBoxSecurityConfig(KeyStore identityStore, KeyStore trustStore, Map<String, String> keyPasswords,
  101.             org.openspcoop2.utils.Map<Object> ctx)
  102.             throws KeyStoreException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, CertificateException {
  103.         this(identityStore, trustStore, keyPasswords, null,
  104.                 ctx);
  105.     }
  106.     public SoapBoxSecurityConfig(KeyStore identityStore, KeyStore trustStore, Map<String, String> keyPasswords,String crlPath,
  107.             org.openspcoop2.utils.Map<Object> ctx)
  108.         throws KeyStoreException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, CertificateException {

  109.         super(identityStore, trustStore, keyPasswords);

  110.         if(trustStore==null) {
  111.             throw new KeyStoreException("TrustStore param is null");
  112.         }
  113.        
  114.         this.identityStore = identityStore;
  115.        
  116.         // create the parameters for the validator
  117.         this.validatorParams = new PKIXParameters(trustStore);
  118.         // disable CRL checking since we are not supplying any CRLs
  119.         this.validatorParams.setRevocationEnabled(false);
  120.         // create the validator and validate the path
  121.         this.certPathValidator = org.openspcoop2.utils.certificate.CertificateFactory.getCertPathValidator();
  122.         // create a X509 certificate factory for later use
  123.         this.certFactory = org.openspcoop2.utils.certificate.CertificateFactory.getCertificateFactory();

  124.         if(crlPath!=null){
  125.             try{
  126.                 RequestInfo requestInfo = null;
  127.                 if(ctx!=null && ctx.containsKey(Costanti.REQUEST_INFO)) {
  128.                     requestInfo = (RequestInfo) ctx.get(Costanti.REQUEST_INFO);
  129.                 }
  130.                
  131.                 CRLCertstore crlCertstore = GestoreKeystoreCache.getCRLCertstore(requestInfo, crlPath);
  132.                 this.validatorParams.addCertStore(crlCertstore.getCertStore());
  133.                 this.validatorParams.setRevocationEnabled(true);
  134.                 this.validatorParams.setDate(DateManager.getDate());
  135.                 //System.out.println("INIT ["+crlPath+"]");
  136.             }catch(Exception e){
  137.                 throw new CertificateException(e.getMessage(),e);
  138.             }
  139.         }
  140.        
  141.         /*try{
  142.             this.validatorParams.setRevocationEnabled(true);
  143.             Security.setProperty("ocsp.enable", "true");
  144.             Security.setProperty("ocsp.responderURL","http://siipki.acquirenteunico.it/ocsp");
  145.             //System.out.println("SETTING [http://siipki.acquirenteunico.it/ocsp]");
  146.          }catch(Exception e){
  147.             System.out.println(e.getMessage());
  148.             e.printStackTrace(System.out);
  149.         }*/
  150.        
  151.     }


  152.     public Key getSymmetricKey(String alias) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException{  
  153.         String password = this.getKeyPassword(alias);
  154.         Key key = this.identityStore.getKey(alias, password.toCharArray());
  155.         return key;
  156.     }


  157.     private final WeakHashMap<X509Certificate[], Boolean> validCerts = new WeakHashMap<X509Certificate[], Boolean>();
  158.     private final CertPathValidator certPathValidator;
  159.     private final CertificateFactory certFactory;
  160.     private final PKIXParameters validatorParams;
  161.    
  162.    public void validateX509Certificate(X509Certificate[] certs) throws SecurityException {

  163.         if(certs==null || certs.length==0) {
  164.             throw new SecurityException("Certificates undefined");
  165.         }
  166.        
  167.         Boolean validity = this.validCerts.get(certs);
  168.         if (validity == null) {
  169.             synchronized (this) {
  170.                
  171.                 StringBuilder bf = new StringBuilder();
  172.                 if(certs!=null){
  173.                     for (int i = 0; i < certs.length; i++) {
  174.                         if(i>0){
  175.                             bf.append(" ");
  176.                         }
  177.                         bf.append("(Certificate["+i+"]: DN="+certs[i].getSubjectDN().toString()+")");
  178.                     }
  179.                 }
  180.                 bf.append(" Does not validate against the trusted CA certificates ");
  181.                
  182.                 try {
  183.                     //System.out.println("Validate...");
  184.                     CertPath certPath = this.certFactory.generateCertPath(Arrays.asList(certs));
  185.                     if(certPath==null) {
  186.                         throw new Exception("CertPath unavailable");
  187.                     }
  188.                     this.certPathValidator.validate(certPath, this.validatorParams);
  189.                     this.validCerts.put(certs, Boolean.TRUE);
  190.                     //System.out.println("Validate OK");
  191.                 } catch (CertificateNotYetValidException e) {
  192.                     this.validCerts.put(certs, Boolean.FALSE);
  193.                     throw new SecurityException(bf.toString()+"[Certificate is not yet valid] - " + e.getMessage());
  194.                 } catch (CertificateExpiredException e) {
  195.                     this.validCerts.put(certs, Boolean.FALSE);
  196.                     throw new SecurityException(bf.toString()+"[Certificate has expired] - " + e.getMessage());
  197.                 } catch (Exception e) {
  198.                       this.validCerts.put(certs, Boolean.FALSE);
  199.                       throw new SecurityException(bf.toString()+"[Errore generico] - " + e.getMessage());
  200.                     //return false;
  201.                 }
  202.             }
  203.         } else {
  204.              throw new SecurityException("Certificate not found in validCerts");
  205.         }
  206.     }

  207. }