PublicKeyStore.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.security.keystore;

  21. import java.io.Serializable;
  22. import java.security.PublicKey;
  23. import java.util.UUID;

  24. import org.openspcoop2.security.SecurityException;
  25. import org.openspcoop2.utils.certificate.JWKPublicKeyConverter;
  26. import org.openspcoop2.utils.certificate.JWKSet;
  27. import org.openspcoop2.utils.certificate.KeyUtils;

  28. /**
  29.  * PublicKeyStore
  30.  *
  31.  * @author Andrea Poli (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class PublicKeyStore implements Serializable {

  36.     /**
  37.      *
  38.      */
  39.     private static final long serialVersionUID = 1L;
  40.    
  41.     private String publicKeyPath;
  42.    
  43.     private byte[] publicKeyContent;
  44.     private transient PublicKey publicKey;
  45.    
  46.     private String algorithm;
  47.    
  48.     private String jwkSetContent;
  49.     private String jwkSetKid;
  50.     private transient JWKSet jwkSet;

  51.     @Override
  52.     public String toString() {
  53.         StringBuilder bf = new StringBuilder();
  54.         bf.append("PublicKeyStore (public:").append(this.publicKeyPath).append(")");
  55.         return bf.toString();
  56.     }
  57.    
  58.     public PublicKeyStore(String publicKeyPath, String algorithm) throws SecurityException{

  59.         this.publicKeyPath = publicKeyPath;
  60.                
  61.         this.algorithm = algorithm==null ? KeyUtils.ALGO_RSA : algorithm;
  62.        
  63.         this.publicKeyContent = StoreUtils.readContent("PublicKey", this.publicKeyPath);
  64.        
  65.     }
  66.    
  67.     public PublicKeyStore(byte[] publicKey, String algorithm) throws SecurityException{

  68.         try{            
  69.             if(publicKey==null){
  70.                 throw new SecurityException("Store publicKey non indicato");
  71.             }
  72.             this.publicKeyContent = publicKey;

  73.             this.algorithm = algorithm==null ? KeyUtils.ALGO_RSA : algorithm;
  74.            
  75.         }catch(Exception e){
  76.             throw new SecurityException(e.getMessage(),e);
  77.         }
  78.        
  79.     }

  80.     public PublicKey getPublicKey() throws SecurityException {
  81.         if(this.publicKey==null) {
  82.             initializePublicKey();
  83.         }
  84.         return this.publicKey;
  85.     }
  86.     private synchronized void initializePublicKey() throws SecurityException {
  87.         if(this.publicKey==null) {
  88.             try {
  89.                 this.publicKey = KeyUtils.getInstance(this.algorithm).getPublicKey(this.publicKeyContent);
  90.             }catch(Exception e){
  91.                 throw new SecurityException("Load public key failed: "+e.getMessage(),e);
  92.             }
  93.         }
  94.     }
  95.    
  96.    
  97.     public JWKSet getJwkSet() throws SecurityException {
  98.         if(this.jwkSet==null) {
  99.             initializeJwkSet();
  100.         }
  101.         return this.jwkSet;
  102.     }
  103.     private synchronized void initializeJwkSet() throws SecurityException {
  104.         if(this.jwkSet==null) {
  105.             if(this.jwkSetContent==null) {
  106.                 this.jwkSetKid = UUID.randomUUID().toString();
  107.                 try {
  108.                     this.jwkSetContent = JWKPublicKeyConverter.convert(this.getPublicKey(), this.jwkSetKid, true, false);
  109.                 }catch(Exception e){
  110.                     throw new SecurityException(e.getMessage(),e);
  111.                 }
  112.             }
  113.                        
  114.             this.jwkSet = new JWKSet(this.jwkSetContent);
  115.         }
  116.     }
  117.    
  118.     public String getJwkSetKid() {
  119.         return this.jwkSetKid;
  120.     }
  121. }