JsonUtils.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.io.File;
  22. import java.io.InputStream;
  23. import java.net.URI;
  24. import java.nio.file.Files;
  25. import java.security.Key;
  26. import java.security.PrivateKey;
  27. import java.security.cert.CertStore;
  28. import java.security.cert.Certificate;
  29. import java.security.cert.CertificateExpiredException;
  30. import java.security.cert.CertificateNotYetValidException;
  31. import java.util.Properties;

  32. import javax.crypto.SecretKey;

  33. import org.apache.commons.lang.StringUtils;
  34. import org.apache.cxf.message.Exchange;
  35. import org.apache.cxf.message.ExchangeImpl;
  36. import org.apache.cxf.message.Message;
  37. import org.apache.cxf.message.MessageImpl;
  38. import org.apache.cxf.phase.PhaseInterceptorChain;
  39. import org.apache.cxf.rs.security.jose.common.JoseConstants;
  40. import org.apache.cxf.rs.security.jose.common.KeyManagementUtils;
  41. import org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm;
  42. import org.apache.cxf.rs.security.jose.jwe.ContentEncryptionProvider;
  43. import org.apache.cxf.rs.security.jose.jwe.JweDecryptionProvider;
  44. import org.apache.cxf.rs.security.jose.jwe.JweEncryption;
  45. import org.apache.cxf.rs.security.jose.jwe.JweEncryptionProvider;
  46. import org.apache.cxf.rs.security.jose.jwe.JweException;
  47. import org.apache.cxf.rs.security.jose.jwe.JweHeaders;
  48. import org.apache.cxf.rs.security.jose.jwe.JweUtils;
  49. import org.apache.cxf.rs.security.jose.jwe.KeyEncryptionProvider;
  50. import org.apache.cxf.rs.security.jose.jwe.WrappedKeyDecryptionAlgorithm;
  51. import org.apache.cxf.rs.security.jose.jwk.JsonWebKey;
  52. import org.apache.cxf.rs.security.jose.jwk.JsonWebKeys;
  53. import org.apache.cxf.rs.security.jose.jwk.JwkUtils;
  54. import org.apache.cxf.rs.security.jose.jwk.KeyOperation;
  55. import org.apache.cxf.rs.security.jose.jws.JwsException;
  56. import org.apache.cxf.rs.security.jose.jws.JwsSignatureProvider;
  57. import org.apache.cxf.rs.security.jose.jws.JwsSignatureVerifier;
  58. import org.apache.cxf.rs.security.jose.jws.JwsUtils;
  59. import org.apache.cxf.rs.security.jose.jws.PrivateKeyJwsSignatureProvider;
  60. import org.apache.cxf.rt.security.rs.RSSecurityConstants;
  61. import org.openspcoop2.utils.Utilities;
  62. import org.openspcoop2.utils.UtilsException;
  63. import org.openspcoop2.utils.certificate.CertificateInfo;
  64. import org.openspcoop2.utils.certificate.KeyStore;
  65. import org.openspcoop2.utils.certificate.KeystoreType;
  66. import org.openspcoop2.utils.io.Base64Utilities;
  67. import org.openspcoop2.utils.json.JSONUtils;
  68. import org.openspcoop2.utils.resources.FileSystemUtilities;
  69. import org.openspcoop2.utils.transport.http.HttpResponse;
  70. import org.openspcoop2.utils.transport.http.HttpUtilities;
  71. import org.openspcoop2.utils.transport.http.IOCSPValidator;

  72. import com.fasterxml.jackson.databind.JsonNode;

  73. /**
  74.  * JsonUtils
  75.  *
  76.  * @author Bussu Giovanni (bussu@link.it)
  77.  * @author  $Author$
  78.  * @version $Rev$, $Date$
  79.  *
  80.  */
  81. public class JsonUtils {
  82.    
  83.     private JsonUtils(){}

  84.     public static Message newMessage() {
  85.         Message m = new MessageImpl();
  86.         Exchange ex = new ExchangeImpl();
  87.         m.setExchange(ex);
  88.         return m;
  89.     }
  90.    
  91.     private static final String KEYSTORE_PREFIX_FILE = "keystore";
  92.    
  93.     private static final String JCEKS_SIGNATURE_ALGO_UNDEFINED = "(JCEKS) Signature Algorithm undefined";
  94.     private static final String PKCS11_SIGNATURE_ALGO_UNDEFINED = "(SecretKey PKCS11) Signature Algorithm undefined";
  95.     private static final String SECRET_SIGNATURE_ALGO_UNDEFINED = "(Secret) Signature Algorithm undefined";
  96.    
  97.     private static final String JCEKS_CONTENT_ALGO_UNDEFINED = "(JCEKS) Content Algorithm undefined";
  98.    
  99.     public static final boolean SIGNATURE = true;
  100.     public static final boolean ENCRYPT = false;
  101.     public static final boolean DECRYPT = false;
  102.     public static final boolean SENDER = true;
  103.     public static final boolean RECEIVER = false;
  104.     public static UtilsException convert(JOSESerialization serialization, boolean signature, boolean roleSender, Throwable t) {
  105.        
  106.         StringBuilder bf = new StringBuilder();
  107.         if(serialization!=null) {
  108.             bf.append("[").append(serialization.name()).append("] ");
  109.         }
  110.        
  111.         if(t instanceof JwsException) {
  112.             JwsException exc = (JwsException) t;
  113.             if(exc.getError()==null) {
  114.                 if(roleSender) {
  115.                     bf.append("Signature failure");
  116.                 }
  117.                 else {
  118.                     bf.append("Signature verification failure");
  119.                 }
  120.             }          
  121.             else {
  122.                 bf.append(exc.getError().name());
  123.             }
  124.             if(exc.getMessage()==null && exc.getCause()==null && exc.getLocalizedMessage()==null) {
  125.                 return new UtilsException(bf.toString(),t);
  126.             }
  127.         }
  128.         else if(t instanceof JweException) {
  129.             JweException exc = (JweException) t;
  130.             if(exc.getError()==null) {
  131.                 if(roleSender) {
  132.                     bf.append("Encrypt failure");
  133.                 }
  134.                 else {
  135.                     bf.append("Decrypt failure");
  136.                 }
  137.             }          
  138.             else {
  139.                 bf.append(exc.getError().name());
  140.             }
  141.             if(exc.getMessage()==null && exc.getCause()==null && exc.getLocalizedMessage()==null) {
  142.                 return new UtilsException(bf.toString(),t);
  143.             }
  144.         }
  145.         else if(signature) {
  146.             if(roleSender) {
  147.                 bf.append("Signature failure");
  148.             }
  149.             else {
  150.                 bf.append("Signature verification failure");
  151.             }
  152.         }
  153.         else {
  154.             if(roleSender) {
  155.                 bf.append("Encrypt failure");
  156.             }
  157.             else {
  158.                 bf.append("Decrypt failure");
  159.             }
  160.         }
  161.                
  162.         String msg = Utilities.getInnerNotEmptyMessageException(t).getMessage();
  163.        
  164.         Throwable innerExc = Utilities.getLastInnerException(t);
  165.         String innerMsg = null;
  166.         if(innerExc!=null){
  167.             innerMsg = innerExc.getMessage();
  168.         }
  169.        
  170.         String messaggio = null;
  171.         if(msg!=null && !"".equals(msg) && !"null".equals(msg)) {
  172.             messaggio = msg+"";
  173.             if(innerMsg!=null && !"".equals(innerMsg) && !"null".equals(innerMsg) && !innerMsg.equals(msg) &&
  174.                 !messaggio.contains(innerMsg)) {
  175.                 messaggio = messaggio + " ; " + innerMsg;
  176.             }
  177.         }
  178.         else{
  179.             if(innerMsg!=null && !"".equals(innerMsg) && !"null".equals(innerMsg)) {
  180.                 messaggio = innerMsg;
  181.             }
  182.         }
  183.        
  184.         if(messaggio!=null) {
  185.             bf.append(": ");
  186.             bf.append(messaggio);
  187.         }
  188.         return new UtilsException(bf.toString(),t);
  189.     }
  190.    
  191.     public static byte[] readKeystoreFromURI(Properties props) throws UtilsException {
  192.        
  193.         String propertyKeystoreName = RSSecurityConstants.RSSEC_KEY_STORE_FILE;
  194.         String path = props.getProperty(propertyKeystoreName);
  195.         byte[]content = null;
  196.         if(path!=null && (path.startsWith("http") || path.startsWith("https"))) {
  197.             HttpResponse httpResponse = null;
  198.             String trustStoreProperty =  RSSecurityConstants.RSSEC_KEY_STORE_FILE+".ssl";
  199.             String trustStorePasswordProperty =  RSSecurityConstants.RSSEC_KEY_STORE_PSWD+".ssl";
  200.             String trustStoreTypeProperty =  RSSecurityConstants.RSSEC_KEY_STORE_TYPE+".ssl";
  201.             String trustStore = props.getProperty(trustStoreProperty);
  202.             String trustStorePassword = props.getProperty(trustStorePasswordProperty);
  203.             String trustStoreType = props.getProperty(trustStoreTypeProperty);
  204.             if(trustStore!=null) {
  205.                 if(trustStorePassword==null) {
  206.                     throw new UtilsException("TrustStore ssl password undefined");
  207.                 }
  208.                 if(trustStoreType==null) {
  209.                     throw new UtilsException("TrustStore ssl type undefined");
  210.                 }
  211.             }
  212.             if( (path.startsWith("https:") && trustStore==null) || path.startsWith("http:") ) {
  213.                 /**System.out.println("http");*/
  214.                 httpResponse = HttpUtilities.getHTTPResponse(path, 60000, 10000);
  215.             }
  216.             else {
  217.                 /**System.out.println("https");*/
  218.                 httpResponse = HttpUtilities.getHTTPSResponse(path, 60000, 10000, trustStore, trustStorePassword, trustStoreType);
  219.             }
  220.             if(httpResponse==null || httpResponse.getContent()==null) {
  221.                 throw new UtilsException("Keystore '"+path+"' unavailable");
  222.             }
  223.             if(httpResponse.getResultHTTPOperation()!=200) {
  224.                 throw new UtilsException("Retrieve keystore '"+path+"' failed (returnCode:"+httpResponse.getResultHTTPOperation()+")");
  225.             }
  226.             content = httpResponse.getContent();
  227.         }
  228.         else if(path!=null && (path.startsWith("file"))){
  229.             try {
  230.                 File f = new File(new URI(path));
  231.                 content = FileSystemUtilities.readBytesFromFile(f);
  232.             }catch(Exception e) {
  233.                 throw new UtilsException(e.getMessage(),e);
  234.             }
  235.         }
  236.        
  237.         return content;
  238.     }
  239.    
  240.     public static File normalizeProperties(Properties props) throws UtilsException {
  241.        
  242.         String propertyKeystoreName = RSSecurityConstants.RSSEC_KEY_STORE_FILE;
  243.         byte[] content = readKeystoreFromURI(props);
  244.        
  245.         File fTmp = null;
  246.         if(content!=null) {
  247.        
  248.             String tipo = props.getProperty(RSSecurityConstants.RSSEC_KEY_STORE_TYPE);
  249.             if(tipo==null) {
  250.                 tipo = KeystoreType.JKS.getNome();
  251.             }
  252.            
  253.             try {
  254.                 fTmp = FileSystemUtilities.createTempFile(KEYSTORE_PREFIX_FILE, "."+tipo);
  255.             }catch(Exception e) {
  256.                 throw new UtilsException(e.getMessage(),e);
  257.             }
  258.             FileSystemUtilities.writeFile(fTmp, content);
  259.             props.remove(propertyKeystoreName);
  260.             props.put(propertyKeystoreName, fTmp.getAbsolutePath());
  261.            
  262.         }
  263.        
  264.         return fTmp;
  265.     }
  266.    
  267.     public static boolean isDynamicProvider(Properties props) {
  268.         String alias = props.getProperty(RSSecurityConstants.RSSEC_KEY_STORE_ALIAS);
  269.         if("*".equalsIgnoreCase(alias)) {
  270.             props.remove(RSSecurityConstants.RSSEC_KEY_STORE_ALIAS);
  271.             return true;
  272.         }
  273.         return false;
  274.     }
  275.    
  276.     public static String readAlias(String jsonString) throws UtilsException {
  277.         if(!jsonString.contains(".")) {
  278.             throw new UtilsException("Invalid format (expected COMPACT)");
  279.         }
  280.         String [] tmp = jsonString.split("\\.");
  281.         byte[] header = Base64Utilities.decode(tmp[0].trim());
  282.         JsonNode node = JSONUtils.getInstance().getAsNode(header).get("kid");
  283.         if(node==null) {
  284.             throw new UtilsException("Claim 'kid' not found");
  285.         }
  286.         String kid = node.asText();
  287.         if(kid==null) {
  288.             throw new UtilsException("Claim 'kid' without value");
  289.         }
  290.         return kid;
  291.     }

  292.     public static JsonWebKey readKey(JsonWebKeys jsonWebKeys, String alias) throws UtilsException {
  293.         if(alias==null) {
  294.             throw new UtilsException("Alias unknonw");
  295.         }
  296.         JsonWebKey jsonWebKey = jsonWebKeys.getKey(alias);
  297.         if(jsonWebKey==null) {
  298.             throw new UtilsException("Key with alias '"+alias+"' unknonw");
  299.         }
  300.         return jsonWebKey;
  301.     }
  302.    
  303.     public static KeyStore getKeyStore(Properties props) throws UtilsException {
  304.         return getKeyStore(true, true, props);
  305.     }
  306.     public static KeyStore getKeyStore(boolean jksPasswordRequired, boolean pkcs12PasswordRequired, Properties props) throws UtilsException {
  307.         Object oKeystore = props.get(RSSecurityConstants.RSSEC_KEY_STORE);
  308.         if(oKeystore instanceof KeyStore) {
  309.             return (KeyStore) oKeystore;
  310.         }
  311.         else if(oKeystore instanceof java.security.KeyStore) {
  312.             return new KeyStore((java.security.KeyStore) oKeystore);
  313.         }
  314.         else {
  315.             String fileK = props.getProperty(RSSecurityConstants.RSSEC_KEY_STORE_FILE);
  316.             if(fileK!=null) {
  317.                 String password = props.getProperty(RSSecurityConstants.RSSEC_KEY_STORE_PSWD);
  318.                 String type = props.getProperty(RSSecurityConstants.RSSEC_KEY_STORE_TYPE);
  319.                
  320.                 if(password==null || "".equals(password)){
  321.                     boolean required = true;
  322.                     if(KeystoreType.JKS.isType(type)) {
  323.                         required = jksPasswordRequired;
  324.                     }
  325.                     else if(KeystoreType.PKCS12.isType(type)) {
  326.                         required = pkcs12PasswordRequired;
  327.                     }
  328.                     if(required) {
  329.                         throw new UtilsException("Keystore password undefined");
  330.                     }
  331.                 }
  332.                 if(type==null || "".equals(type)){
  333.                     type = KeystoreType.JKS.getNome();
  334.                 }
  335.                 if(KeystoreType.JWK_SET.getNome().equalsIgnoreCase(type)
  336.                     ||
  337.                     KeystoreType.PUBLIC_KEY.getNome().equalsIgnoreCase(type)
  338.                     ||
  339.                     KeystoreType.KEY_PAIR.getNome().equalsIgnoreCase(type)) {
  340.                     return null;
  341.                 }
  342.                
  343.                 File file = new File(fileK);
  344.                 if(file.exists()) {
  345.                     return new KeyStore(file.getAbsolutePath(), type, password);
  346.                 }
  347.                 else {
  348.                     InputStream is = JsonUtils.class.getResourceAsStream(fileK);
  349.                     File fTmp = null;
  350.                     try {
  351.                         if(is!=null) {
  352.                             byte[] f = Utilities.getAsByteArray(is);
  353.                             try {
  354.                                 fTmp = FileSystemUtilities.createTempFile(KEYSTORE_PREFIX_FILE, ".tmp");
  355.                                 FileSystemUtilities.writeFile(fTmp, f);
  356.                                 return new KeyStore(fTmp.getAbsolutePath(), type, password);
  357.                             }catch(Exception e) {
  358.                                 throw new UtilsException(e.getMessage(),e);
  359.                             }
  360.                         }
  361.                     }finally {
  362.                         try {
  363.                             if(is!=null) {
  364.                                 is.close();
  365.                             }
  366.                         }catch(Exception e) {
  367.                             // ignore
  368.                         }
  369.                         try {
  370.                             if(fTmp!=null) {
  371.                                 Files.delete(fTmp.toPath());
  372.                             }
  373.                         }catch(Exception e) {
  374.                             // delete
  375.                         }
  376.                     }
  377.                 }  
  378.             }
  379.         }
  380.         return null;
  381.     }
  382.     public static Certificate getCertificateKey(Properties props) throws UtilsException {
  383.         return getCertificateKey(true, true, props);
  384.     }
  385.     public static Certificate getCertificateKey(boolean jksPasswordRequired, boolean pkcs12PasswordRequired, Properties props) throws UtilsException {
  386.         KeyStore keystore = getKeyStore(jksPasswordRequired, pkcs12PasswordRequired, props);
  387.         if(keystore!=null) {
  388.             String alias = props.getProperty(RSSecurityConstants.RSSEC_KEY_STORE_ALIAS);
  389.             if(alias!=null && !"".equals(alias)){
  390.                 return keystore.getCertificate(alias);
  391.             }
  392.         }
  393.         return null;
  394.     }
  395.    
  396.     public static SecretKeyPkcs11 getSecretKeyPKCS11(Properties props) throws UtilsException {
  397.         if(props.containsKey(RSSecurityConstants.RSSEC_KEY_STORE_TYPE)) {
  398.             String type = props.getProperty(RSSecurityConstants.RSSEC_KEY_STORE_TYPE);
  399.            
  400.             if(KeystoreType.PKCS11.getNome().equalsIgnoreCase(type)) {
  401.                 Object oKeystore = props.get(RSSecurityConstants.RSSEC_KEY_STORE);
  402.                 if(oKeystore instanceof java.security.KeyStore) {
  403.                     java.security.KeyStore keystorePKCS11 = (java.security.KeyStore) oKeystore;
  404.                     String alias = props.getProperty(RSSecurityConstants.RSSEC_KEY_STORE_ALIAS);
  405.                     if(alias==null || "".equals(alias)){
  406.                         throw new UtilsException("(Secret-PKCS11) Alias key undefined");
  407.                     }              
  408.                     Key key = null;
  409.                     try {
  410.                         key = keystorePKCS11.getKey(alias, null);
  411.                     }catch(Exception e) {
  412.                         throw new UtilsException(e.getMessage(),e);
  413.                     }
  414.                     if(key instanceof SecretKey) {
  415.                         return new SecretKeyPkcs11(keystorePKCS11.getProvider(), (SecretKey) key);
  416.                     }
  417.                 }
  418.             }
  419.         }
  420.         return null;
  421.     }
  422.     public static SecretKey getSecretKey(Properties props) throws UtilsException {
  423.         if(props.containsKey(RSSecurityConstants.RSSEC_KEY_STORE_TYPE)) {
  424.             String type = props.getProperty(RSSecurityConstants.RSSEC_KEY_STORE_TYPE);
  425.            
  426.             if(KeystoreType.JCEKS.getNome().equalsIgnoreCase(type)) {
  427.                 Object oKeystore = props.get(RSSecurityConstants.RSSEC_KEY_STORE);
  428.                 String fileK = props.getProperty(RSSecurityConstants.RSSEC_KEY_STORE_FILE);
  429.                 String password = props.getProperty(RSSecurityConstants.RSSEC_KEY_STORE_PSWD);
  430.                 String alias = props.getProperty(RSSecurityConstants.RSSEC_KEY_STORE_ALIAS);
  431.                 String passwordKey = props.getProperty(RSSecurityConstants.RSSEC_KEY_PSWD);
  432.                
  433.                 if(oKeystore instanceof java.security.KeyStore) {
  434.                     java.security.KeyStore keystoreJCEKS = (java.security.KeyStore) oKeystore;
  435.                    
  436.                     if(alias==null || "".equals(alias)){
  437.                         throw new UtilsException("(JCEKS) Alias key undefined");
  438.                     }
  439.                     if(passwordKey==null || "".equals(passwordKey)){
  440.                         throw new UtilsException("(JCEKS) Password key undefined");
  441.                     }
  442.                    
  443.                     try {
  444.                         return (SecretKey) keystoreJCEKS.getKey(alias, passwordKey.toCharArray());
  445.                     }catch(Exception e) {
  446.                         throw new UtilsException(e.getMessage(),e);
  447.                     }
  448.                 }
  449.                 else if(fileK!=null) {
  450.                    
  451.                     if(password==null || "".equals(password)){
  452.                         throw new UtilsException("(JCEKS) Keystore password undefined");
  453.                     }
  454.                    
  455.                     File file = new File(fileK);
  456.                     KeyStore keystoreJCEKS = null;
  457.                     if(file.exists()) {
  458.                         keystoreJCEKS = new KeyStore(file.getAbsolutePath(), KeystoreType.JCEKS.getNome(), password);
  459.                     }
  460.                     else {
  461.                         InputStream is = JsonUtils.class.getResourceAsStream(fileK);
  462.                         File fTmp = null;
  463.                         try {
  464.                             if(is!=null) {
  465.                                 byte[] f = Utilities.getAsByteArray(is);
  466.                                 try {
  467.                                     fTmp = FileSystemUtilities.createTempFile(KEYSTORE_PREFIX_FILE, ".jceks");
  468.                                     FileSystemUtilities.writeFile(fTmp, f);
  469.                                 }catch(Exception e) {
  470.                                     throw new UtilsException(e.getMessage(),e);
  471.                                 }
  472.                                 keystoreJCEKS = new KeyStore(fTmp.getAbsolutePath(), KeystoreType.JCEKS.getNome(), password);
  473.                             }
  474.                         }finally {
  475.                             try {
  476.                                 if(is!=null) {
  477.                                     is.close();
  478.                                 }
  479.                             }catch(Exception e) {
  480.                                 // ignore
  481.                             }
  482.                             try {
  483.                                 if(fTmp!=null) {
  484.                                     java.nio.file.Files.delete(fTmp.toPath());
  485.                                 }
  486.                             }catch(Exception e) {
  487.                                 // delete
  488.                             }
  489.                         }
  490.                     }  
  491.                     if(keystoreJCEKS!=null) {
  492.                        
  493.                         if(alias==null || "".equals(alias)){
  494.                             throw new UtilsException("(JCEKS) Alias key undefined");
  495.                         }
  496.                         if(passwordKey==null || "".equals(passwordKey)){
  497.                             throw new UtilsException("(JCEKS) Password key undefined");
  498.                         }
  499.                        
  500.                         return keystoreJCEKS.getSecretKey(alias, passwordKey);
  501.                     }

  502.                 }
  503.             }
  504.         }
  505.         return null;
  506.     }
  507.     public static final String RSSEC_KEY_STORE_TYPE_SECRET = "secret";
  508.     public static String getSecret(Properties props, org.apache.cxf.rs.security.jose.jwa.SignatureAlgorithm algorithm) {
  509.         if(props.containsKey(RSSecurityConstants.RSSEC_KEY_STORE_TYPE)) {
  510.             String type = props.getProperty(RSSecurityConstants.RSSEC_KEY_STORE_TYPE);
  511.             if(RSSEC_KEY_STORE_TYPE_SECRET.equalsIgnoreCase(type)) { // customized
  512.                 if(algorithm!=null) {
  513.                     if(algorithm.name().toLowerCase().startsWith("hs")) {
  514.                         String passwordKey = props.getProperty(RSSecurityConstants.RSSEC_KEY_PSWD);
  515.                         if(passwordKey!=null && StringUtils.isNotEmpty(passwordKey)) {
  516.                             return passwordKey;
  517.                         }
  518.                     }
  519.                 }
  520.                 else {
  521.                     if(props.containsKey(RSSecurityConstants.RSSEC_SIGNATURE_ALGORITHM)) {
  522.                        
  523.                         String algo = props.getProperty(RSSecurityConstants.RSSEC_SIGNATURE_ALGORITHM);
  524.                         if(algo!=null && algo.toLowerCase().startsWith("hs")) {
  525.                             String passwordKey = props.getProperty(RSSecurityConstants.RSSEC_KEY_PSWD);
  526.                             if(passwordKey!=null && StringUtils.isNotEmpty(passwordKey)) {
  527.                                 return passwordKey;
  528.                             }
  529.                         }
  530.                        
  531.                     }          
  532.                 }
  533.             }
  534.         }
  535.         return null;
  536.     }
  537.        
  538.     public static JwsSignatureProvider getJwsSymmetricProvider(Properties props) throws UtilsException {
  539.         String algorithm = props.getProperty(RSSecurityConstants.RSSEC_SIGNATURE_ALGORITHM);
  540.         return getJwsSymmetricProvider(props, algorithm);
  541.     }
  542.     public static JwsSignatureProvider getJwsSymmetricProvider(Properties props, String algorithm) throws UtilsException {
  543.         org.apache.cxf.rs.security.jose.jwa.SignatureAlgorithm algo = null;
  544.         if(algorithm==null || "".equals(algorithm)) {
  545.             String type = props.getProperty(RSSecurityConstants.RSSEC_KEY_STORE_TYPE);
  546.             if(KeystoreType.JCEKS.getNome().equalsIgnoreCase(type)) {
  547.                 throw new UtilsException(JCEKS_SIGNATURE_ALGO_UNDEFINED);  
  548.             }
  549.         }
  550.         else{
  551.             algo = org.apache.cxf.rs.security.jose.jwa.SignatureAlgorithm.getAlgorithm(algorithm);
  552.         }
  553.         return getJwsSymmetricProvider(props, algo);
  554.     }
  555.     public static JwsSignatureProvider getJwsSymmetricProvider(Properties props, org.apache.cxf.rs.security.jose.jwa.SignatureAlgorithm algorithm) throws UtilsException {
  556.        
  557.         SecretKeyPkcs11 secretKeyPkcs11 = getSecretKeyPKCS11(props);
  558.         if(secretKeyPkcs11!=null) {
  559.             if(algorithm==null) {
  560.                 throw new UtilsException(PKCS11_SIGNATURE_ALGO_UNDEFINED);
  561.             }
  562.             return new HmacJwsSignatureProviderExtended(secretKeyPkcs11, algorithm);
  563.         }
  564.        
  565.         SecretKey secretKey = getSecretKey(props);
  566.         if(secretKey!=null) {
  567.             if(algorithm==null) {
  568.                 throw new UtilsException(JCEKS_SIGNATURE_ALGO_UNDEFINED);
  569.             }
  570.             byte[] encoded = secretKey.getEncoded();
  571.             JwsSignatureProvider provider = JwsUtils.getHmacSignatureProvider(encoded, algorithm);
  572.             if(provider==null) {
  573.                 throw new UtilsException("(JCEKS) JwsSignatureProvider init failed; check signature algorithm ("+algorithm+")");
  574.             }
  575.             return provider;
  576.         }
  577.         else {
  578.             String secret = getSecret(props, algorithm);
  579.             if(secret!=null) {
  580.                 if(algorithm==null) {
  581.                     throw new UtilsException(SECRET_SIGNATURE_ALGO_UNDEFINED);
  582.                 }
  583.                 byte[] encoded = secret.getBytes();
  584.                 JwsSignatureProvider provider = JwsUtils.getHmacSignatureProvider(encoded, algorithm);
  585.                 if(provider==null) {
  586.                     throw new UtilsException("(Secret) JwsSignatureProvider init failed; check signature algorithm ("+algorithm+")");
  587.                 }
  588.                 return provider;
  589.             }
  590.         }
  591.         return null;
  592.     }
  593.    
  594.     // ** INIZIO METODI PER OTTENERE UN JwsSignatureProvider CON PKCS11
  595.     public static JwsSignatureProvider getJwsAsymmetricProvider(Properties props) throws UtilsException {
  596.         String algorithm = props.getProperty(RSSecurityConstants.RSSEC_SIGNATURE_ALGORITHM);
  597.         return getJwsAsymmetricProvider(props, algorithm);
  598.     }
  599.     public static JwsSignatureProvider getJwsAsymmetricProvider(Properties props, String algorithm) throws UtilsException {
  600.         org.apache.cxf.rs.security.jose.jwa.SignatureAlgorithm algo = null;
  601.         if(algorithm!=null && !"".equals(algorithm)) {
  602.             algo = org.apache.cxf.rs.security.jose.jwa.SignatureAlgorithm.getAlgorithm(algorithm);
  603.         }
  604.         return getJwsAsymmetricProvider(props, algo);
  605.     }
  606.     public static JwsSignatureProvider getJwsAsymmetricProvider(Properties props, org.apache.cxf.rs.security.jose.jwa.SignatureAlgorithm algorithm) throws UtilsException {
  607.         String type = props.getProperty(RSSecurityConstants.RSSEC_KEY_STORE_TYPE);
  608.         if(type==null || "".equals(type)) {
  609.             type="undefined";
  610.         }
  611.         if(algorithm==null) {
  612.             throw new UtilsException("("+type+") Signature Algorithm undefined");
  613.         }
  614.         PrivateKey pKey = KeyManagementUtils.loadPrivateKey(null, props, KeyOperation.SIGN);
  615.         return getJwsAsymmetricProvider(pKey, algorithm);
  616.     }
  617.     public static JwsSignatureProvider getJwsAsymmetricProvider(PrivateKey pKey, org.apache.cxf.rs.security.jose.jwa.SignatureAlgorithm algorithm) {
  618.         return new PrivateKeyJwsSignatureProvider(pKey, algorithm);
  619.     }
  620.     // ** FINE METODI PER OTTENERE UN JwsSignatureProvider CON PKCS11
  621.    
  622.     public static JwsSignatureVerifier getJwsSignatureVerifier(Properties props) throws UtilsException {
  623.         String algorithm = props.getProperty(RSSecurityConstants.RSSEC_SIGNATURE_ALGORITHM);
  624.         return getJwsSignatureVerifier(props, algorithm);
  625.     }
  626.     public static JwsSignatureVerifier getJwsSignatureVerifier(Properties props, String algorithm) throws UtilsException {
  627.         org.apache.cxf.rs.security.jose.jwa.SignatureAlgorithm algo = null;
  628.         if(algorithm==null || "".equals(algorithm)) {
  629.             String type = props.getProperty(RSSecurityConstants.RSSEC_KEY_STORE_TYPE);
  630.             if(KeystoreType.JCEKS.getNome().equalsIgnoreCase(type)) {
  631.                 throw new UtilsException(JCEKS_SIGNATURE_ALGO_UNDEFINED);
  632.             }
  633.         }
  634.         else {
  635.             algo = org.apache.cxf.rs.security.jose.jwa.SignatureAlgorithm.getAlgorithm(algorithm);
  636.         }
  637.         return getJwsSignatureVerifier(props, algo);
  638.     }
  639.     public static JwsSignatureVerifier getJwsSignatureVerifier(Properties props, org.apache.cxf.rs.security.jose.jwa.SignatureAlgorithm algorithm) throws UtilsException {
  640.        
  641.         SecretKey secretKey = getSecretKey(props);
  642.         if(secretKey!=null) {
  643.             if(algorithm==null) {
  644.                 throw new UtilsException(JCEKS_SIGNATURE_ALGO_UNDEFINED);
  645.             }
  646.             JwsSignatureVerifier verifier = JwsUtils.getHmacSignatureVerifier(secretKey.getEncoded(), algorithm);
  647.             if(verifier==null) {
  648.                 throw new UtilsException("(JCEKS) JwsSignatureVerifier init failed; check signature algorithm ("+algorithm+")");
  649.             }
  650.             return verifier;
  651.         }
  652.         else {
  653.             String secret = getSecret(props, algorithm);
  654.             if(secret!=null) {
  655.                 if(algorithm==null) {
  656.                     throw new UtilsException(SECRET_SIGNATURE_ALGO_UNDEFINED);
  657.                 }
  658.                 byte[] encoded = secret.getBytes();
  659.                 JwsSignatureVerifier verifier = JwsUtils.getHmacSignatureVerifier(encoded, algorithm);
  660.                 if(verifier==null) {
  661.                     throw new UtilsException("(Secret) JwsSignatureVerifier init failed; check signature algorithm ("+algorithm+")");
  662.                 }
  663.                 return verifier;
  664.             }
  665.         }
  666.         return null;
  667.     }
  668.    
  669.    
  670.    
  671.     public static JweEncryptionProvider getJweEncryptionProvider(Properties props) throws UtilsException {
  672.         String algorithm = props.getProperty(JoseConstants.RSSEC_ENCRYPTION_CONTENT_ALGORITHM);
  673.         return getJweEncryptionProvider(props, algorithm);
  674.     }
  675.     public static JweEncryptionProvider getJweEncryptionProvider(Properties props, String algorithm) throws UtilsException {
  676.         org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm algo = null;
  677.         if(algorithm==null || "".equals(algorithm)) {
  678.             String type = props.getProperty(RSSecurityConstants.RSSEC_KEY_STORE_TYPE);
  679.             if(KeystoreType.JCEKS.getNome().equalsIgnoreCase(type)) {
  680.                 throw new UtilsException(JCEKS_CONTENT_ALGO_UNDEFINED);
  681.             }
  682.         }
  683.         else {
  684.             algo = org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm.getAlgorithm(algorithm);
  685.         }
  686.         return getJweEncryptionProvider(props, algo);
  687.     }
  688.     public static JweEncryptionProvider getJweEncryptionProvider(Properties props, org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm algorithm) throws UtilsException {
  689.        
  690.         SecretKey secretKey = getSecretKey(props);
  691.         if(secretKey!=null) {
  692.             if(algorithm==null) {
  693.                 throw new UtilsException(JCEKS_CONTENT_ALGO_UNDEFINED);
  694.             }
  695.             JweEncryptionProvider provider = JweUtils.getDirectKeyJweEncryption(secretKey, algorithm);
  696.             if(provider==null) {
  697.                 throw new UtilsException("(JCEKS) JweEncryptionProvider init failed; check content algorithm ("+algorithm+")");
  698.             }
  699.             return provider;
  700.         }
  701.         return null;
  702.     }
  703.    
  704.     public static JweDecryptionProvider getJweDecryptionProvider(Properties props) throws UtilsException {
  705.         String algorithm = props.getProperty(JoseConstants.RSSEC_ENCRYPTION_CONTENT_ALGORITHM);
  706.         return getJweDecryptionProvider(props, algorithm);
  707.     }
  708.     public static JweDecryptionProvider getJweDecryptionProvider(Properties props, String algorithm) throws UtilsException {
  709.         org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm algo = null;
  710.         if(algorithm==null || "".equals(algorithm)) {
  711.             String type = props.getProperty(RSSecurityConstants.RSSEC_KEY_STORE_TYPE);
  712.             if(KeystoreType.JCEKS.getNome().equalsIgnoreCase(type)) {
  713.                 throw new UtilsException(JCEKS_CONTENT_ALGO_UNDEFINED);
  714.             }
  715.         }
  716.         else {
  717.             algo = org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm.getAlgorithm(algorithm);
  718.         }
  719.         return getJweDecryptionProvider(props, algo);
  720.     }
  721.     public static JweDecryptionProvider getJweDecryptionProvider(Properties props, org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm algorithm) throws UtilsException {
  722.        
  723.         SecretKey secretKey = getSecretKey(props);
  724.         if(secretKey!=null) {
  725.             if(algorithm==null) {
  726.                 throw new UtilsException(JCEKS_CONTENT_ALGO_UNDEFINED);
  727.             }
  728.             JweDecryptionProvider verifier = JweUtils.getDirectKeyJweDecryption(secretKey, algorithm);
  729.             if(verifier==null) {
  730.                 throw new UtilsException("(JCEKS) JweDecryptionProvider init failed; check content algorithm ("+algorithm+")");
  731.             }
  732.             return verifier;
  733.         }
  734.         return null;
  735.     }
  736.    
  737.     // ** INIZIO METODI PER OTTENERE UN JweDecryptionProvider CON PKCS11
  738.     public static JweDecryptionProvider getJweAsymmetricDecryptionProvider(Properties props) throws UtilsException {
  739.         String  contentAlgorithm = props.getProperty(JoseConstants.RSSEC_ENCRYPTION_CONTENT_ALGORITHM);
  740.         String  keyAlgorithm = props.getProperty(JoseConstants.RSSEC_ENCRYPTION_KEY_ALGORITHM);
  741.         return getJweAsymmetricDecryptionProvider(props, keyAlgorithm, contentAlgorithm);
  742.     }
  743.     public static JweDecryptionProvider getJweAsymmetricDecryptionProvider(Properties props, String keyAlgorithm, String contentAlgorithm) throws UtilsException {
  744.         org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm contentAlgo = null;
  745.         if(contentAlgorithm!=null && !"".equals(contentAlgorithm)) {
  746.             contentAlgo = org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm.getAlgorithm(contentAlgorithm);
  747.         }
  748.         org.apache.cxf.rs.security.jose.jwa.KeyAlgorithm keyAlgo = null;
  749.         if(keyAlgorithm!=null && !"".equals(keyAlgorithm)) {
  750.             keyAlgo = org.apache.cxf.rs.security.jose.jwa.KeyAlgorithm.getAlgorithm(keyAlgorithm);
  751.         }
  752.         return getJweAsymmetricDecryptionProvider(props, keyAlgo, contentAlgo);
  753.     }
  754.     public static JweDecryptionProvider getJweAsymmetricDecryptionProvider(Properties props,
  755.             org.apache.cxf.rs.security.jose.jwa.KeyAlgorithm keyAlgorithm,
  756.             org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm contentAlgorithm) throws UtilsException {
  757.         String type = props.getProperty(RSSecurityConstants.RSSEC_KEY_STORE_TYPE);
  758.         if(type==null || "".equals(type)) {
  759.             type="undefined";
  760.         }
  761.         if(contentAlgorithm==null) {
  762.             throw new UtilsException("("+type+") Content Algorithm undefined");
  763.         }
  764.         if(keyAlgorithm==null) {
  765.             throw new UtilsException("("+type+") Key Algorithm undefined");
  766.         }
  767.         PrivateKey privateKey = KeyManagementUtils.loadPrivateKey(null, props, KeyOperation.DECRYPT);
  768.         return getJweAsymmetricDecryptionProvider(privateKey, keyAlgorithm, contentAlgorithm);
  769.     }
  770.     public static JweDecryptionProvider getJweAsymmetricDecryptionProvider(PrivateKey privateKey,
  771.             org.apache.cxf.rs.security.jose.jwa.KeyAlgorithm keyAlgorithm,
  772.             org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm contentAlgorithm) {
  773.         WrappedKeyDecryptionAlgorithm privateKeyDecryptionProvider = new WrappedKeyDecryptionAlgorithm(privateKey, keyAlgorithm, false);
  774.         return JweUtils.createJweDecryptionProvider(privateKeyDecryptionProvider, contentAlgorithm);
  775.     }
  776.     // ** FINE METODI PER OTTENERE UN JweDecryptionProvider CON PKCS11
  777.    
  778.     public static JweEncryptionProvider getJweEncryptionProviderFromJWKSymmetric(Properties props, JweHeaders headers) {

  779.         // Metodo copiato da JweUtils per risolvere il problema indicato sotto.
  780.        
  781.         Message m = PhaseInterceptorChain.getCurrentMessage();
  782.        
  783.         KeyEncryptionProvider keyEncryptionProvider = JweUtils.loadKeyEncryptionProvider(props, m, headers);
  784.         ContentAlgorithm contentAlgo = JweUtils.getContentEncryptionAlgorithm(m, props, null, ContentAlgorithm.A128GCM);
  785.         if (m != null) {
  786.             m.put(JoseConstants.RSSEC_ENCRYPTION_CONTENT_ALGORITHM, contentAlgo.getJwaName());
  787.         }
  788.         ContentEncryptionProvider ctEncryptionProvider = null;
  789.         JsonWebKey jwk = JwkUtils.loadJsonWebKey(m, props, KeyOperation.ENCRYPT);
  790.         // FIX
  791.         if(jwk.getAlgorithm()==null) {
  792.             jwk.setAlgorithm(contentAlgo.getJwaName());
  793.         }
  794.         // FIX
  795.         contentAlgo = JweUtils.getContentEncryptionAlgorithm(m, props,
  796.                 jwk.getAlgorithm() != null ? ContentAlgorithm.getAlgorithm(jwk.getAlgorithm()) : null, contentAlgo);
  797.         ctEncryptionProvider = JweUtils.getContentEncryptionProvider(jwk, contentAlgo);
  798.         String compression = props.getProperty(JoseConstants.RSSEC_ENCRYPTION_ZIP_ALGORITHM);
  799.        
  800.         headers = headers != null ? headers : new JweHeaders();
  801.         headers.setKeyEncryptionAlgorithm(keyEncryptionProvider.getAlgorithm());
  802.         headers.setContentEncryptionAlgorithm(contentAlgo);
  803.         if (compression != null) {
  804.             headers.setZipAlgorithm(compression);
  805.         }
  806.        
  807.         return new JweEncryption(keyEncryptionProvider, ctEncryptionProvider);
  808.        
  809.     }
  810.    
  811.     public static JweDecryptionProvider getJweDecryptionProviderFromJWKSymmetric(Properties props, JweHeaders headers) {

  812.         // Metodo copiato da JweUtils per risolvere il problema indicato sotto.
  813.        
  814.         if(headers!=null) {
  815.             // nop
  816.         }
  817.        
  818.         Message m = PhaseInterceptorChain.getCurrentMessage();
  819.        
  820.         JsonWebKey jwk = JwkUtils.loadJsonWebKey(m, props, KeyOperation.DECRYPT);

  821.         ContentAlgorithm contentAlgo = JweUtils.getContentEncryptionAlgorithm(m, props,
  822.                                          ContentAlgorithm.getAlgorithm(jwk.getAlgorithm()),
  823.                                          ContentAlgorithm.A128GCM);
  824.         // FIX
  825.         if(jwk.getAlgorithm()==null) {
  826.             jwk.setAlgorithm(contentAlgo.getJwaName());
  827.         }
  828.         // FIX
  829.        
  830.         SecretKey ctDecryptionKey = JweUtils.getContentDecryptionSecretKey(jwk, contentAlgo.getJwaName());

  831.         return JweUtils.getDirectKeyJweDecryption(ctDecryptionKey, contentAlgo);
  832.     }
  833.    
  834.     private static String getErrorNonValido(Exception e) {
  835.         return "non valido: "+e.getMessage();
  836.     }
  837.    
  838.     public static void validate(CertificateInfo certificatoInfo,
  839.             KeyStore trustStoreCertificatiX509, CertStore crlX509, IOCSPValidator ocspValidatorX509, String headerName,
  840.             boolean verifaCA,
  841.             CertificateValidityCheck validityCheck) throws UtilsException {
  842.         if(trustStoreCertificatiX509!=null) {
  843.             String prefisso = headerName!=null ? ("Certificato presente nell'header '"+headerName+"' ") : "Certificato di firma ";
  844.             if(verifaCA && !certificatoInfo.isVerified(trustStoreCertificatiX509, true)) {
  845.                 throw new UtilsException(prefisso+"non รจ verificabile rispetto alle CA conosciute");
  846.             }
  847.             if(validityCheck==null // default
  848.                     ||
  849.                     CertificateValidityCheck.ENABLED.equals(validityCheck)
  850.                     ||
  851.                     (CertificateValidityCheck.IF_NOT_IN_TRUSTSTORE.equals(validityCheck) && !trustStoreCertificatiX509.existsCertificateBySubject(certificatoInfo.getCertificate().getSubjectX500Principal()))
  852.                 ) {
  853.                 try {
  854.                     certificatoInfo.checkValid();
  855.                 }catch(CertificateExpiredException e) {
  856.                     throw new UtilsException(prefisso+"scaduto: "+e.getMessage(),e);
  857.                 }catch(CertificateNotYetValidException e) {
  858.                     throw new UtilsException(prefisso+"non ancora valido: "+e.getMessage(),e);
  859.                 }catch(Exception e) {
  860.                     throw new UtilsException(prefisso+getErrorNonValido(e),e);
  861.                 }
  862.             }
  863.             if(ocspValidatorX509!=null) {
  864.                 try {
  865.                     ocspValidatorX509.valid(certificatoInfo.getCertificate());
  866.                 }catch(Exception e) {
  867.                     throw new UtilsException(prefisso+getErrorNonValido(e),e);
  868.                 }
  869.             }
  870.             if(crlX509!=null) {
  871.                 try {
  872.                     certificatoInfo.checkValid(crlX509, trustStoreCertificatiX509);
  873.                 }catch(Exception e) {
  874.                     throw new UtilsException(prefisso+getErrorNonValido(e),e);
  875.                 }
  876.             }
  877.         }
  878.     }
  879.    
  880.  }