HmacJwsSignatureProviderExtended.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.InvalidAlgorithmParameterException;
  22. import java.security.InvalidKeyException;
  23. import java.security.spec.AlgorithmParameterSpec;

  24. import javax.crypto.Mac;

  25. import org.apache.cxf.rs.security.jose.jwa.AlgorithmUtils;
  26. import org.apache.cxf.rs.security.jose.jwa.SignatureAlgorithm;
  27. import org.apache.cxf.rs.security.jose.jws.JwsHeaders;
  28. import org.apache.cxf.rs.security.jose.jws.JwsSignature;
  29. import org.apache.cxf.rt.security.crypto.HmacUtils;

  30. /**
  31.  * HmacJwsSignatureProvider
  32.  *
  33.  * @author Poli Andrea (apoli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  */
  37. public class HmacJwsSignatureProviderExtended extends org.apache.cxf.rs.security.jose.jws.HmacJwsSignatureProvider {

  38.     public HmacJwsSignatureProviderExtended(byte[] key, AlgorithmParameterSpec spec, SignatureAlgorithm algo) {
  39.         super(key, spec, algo);
  40.     }

  41.     public HmacJwsSignatureProviderExtended(byte[] key, SignatureAlgorithm algo) {
  42.         super(key, algo);
  43.     }

  44.     public HmacJwsSignatureProviderExtended(String arg0, SignatureAlgorithm arg1) {
  45.         super(arg0, arg1);
  46.     }
  47.    
  48.     // METODI PER PKCS11
  49.     private SecretKeyPkcs11 secretKey;
  50.     private AlgorithmParameterSpec hmacSpec;
  51.     public HmacJwsSignatureProviderExtended(SecretKeyPkcs11 secretKey, AlgorithmParameterSpec spec, SignatureAlgorithm algo) {
  52.         super(secretKey.getSecretKey().getEncoded(), spec, algo);
  53.         this.secretKey = secretKey;
  54.         this.hmacSpec = spec;
  55.     }
  56.     public HmacJwsSignatureProviderExtended(SecretKeyPkcs11 secretKey, SignatureAlgorithm algo) {
  57.         super(secretKey.getSecretKey().getEncoded(), algo);
  58.         this.secretKey = secretKey;
  59.     }
  60.     // METODI PER PKCS11

  61.     @Override
  62.     protected JwsSignature doCreateJwsSignature(JwsHeaders headers) {
  63.        
  64.         if(this.secretKey==null) {
  65.             return super.doCreateJwsSignature(headers);
  66.         }
  67.        
  68.         final String sigAlgo = headers.getSignatureAlgorithm().getJwaName();
  69.         final Mac mac = HmacUtils.getMac(AlgorithmUtils.toJavaName(sigAlgo), this.secretKey.getProvider());
  70.         try {
  71.             if (this.hmacSpec == null) {
  72.                 mac.init(this.secretKey.getSecretKey());
  73.             }
  74.             else {
  75.                 mac.init(this.secretKey.getSecretKey(), this.hmacSpec);
  76.             }
  77.         } catch (InvalidKeyException e) {
  78.             throw new SecurityException(e);
  79.         } catch (InvalidAlgorithmParameterException e) {
  80.             throw new SecurityException(e);
  81.         }
  82.        
  83.         return new JwsSignature() {

  84.             @Override
  85.             public void update(byte[] src, int off, int len) {
  86.                 mac.update(src, off, len);
  87.             }

  88.             @Override
  89.             public byte[] sign() {
  90.                 return mac.doFinal();
  91.             }

  92.         };
  93.     }

  94. }