JWKPublicKeyConverter.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.certificate;

  21. import java.security.PublicKey;
  22. import java.util.UUID;

  23. import org.apache.commons.lang.StringUtils;
  24. import org.openspcoop2.utils.UtilsException;
  25. import org.openspcoop2.utils.resources.FileSystemUtilities;

  26. /**
  27.  * JWKPublicKeyConverter
  28.  *
  29.  * @author Poli Andrea (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class JWKPublicKeyConverter {

  34.     public static final String KID_NULL = "#none#";
  35.    
  36.     public static void main(String [] args) throws UtilsException {
  37.        
  38.         if(args==null || args.length<2) {
  39.             throw new UtilsException("ERROR: argomenti non forniti (USAGE: JWKPublicKeyConverter pathPublicKey pathJWK [kid] [jwkset(true/false)] [pretty(true/false)])");
  40.         }
  41.        
  42.         try {
  43.        
  44.             java.security.Security.addProvider(
  45.                      new org.bouncycastle.jce.provider.BouncyCastleProvider()
  46.             );
  47.            
  48.             String pathPublicKey = args[0];
  49.             byte[] publicKey = FileSystemUtilities.readBytesFromFile(pathPublicKey);
  50.            
  51.             KeyUtils keyUtils = new KeyUtils(KeyUtils.ALGO_RSA);
  52.            
  53.             PublicKey pKey = keyUtils.getPublicKey(publicKey);
  54.            
  55.             String pathJWK = args[1];
  56.                        
  57.             String kid = null;
  58.             if(args.length>2) {
  59.                 kid = args[2];
  60.             }
  61.             if(kid==null || StringUtils.isEmpty(kid)) {
  62.                 kid = UUID.randomUUID().toString();
  63.             }
  64.             if(KID_NULL.equals(kid)) {
  65.                 kid = null;
  66.             }
  67.            
  68.             boolean jwks = true;
  69.             if(args.length>3) {
  70.                 String tmp = args[3];
  71.                 jwks = "true".equals(tmp);
  72.             }
  73.            
  74.             boolean pretty = false;
  75.             if(args.length>4) {
  76.                 String tmp = args[4];
  77.                 pretty = "true".equals(tmp);
  78.             }
  79.            
  80.             String json = convert(pKey, kid, jwks, pretty);
  81.            
  82.             FileSystemUtilities.writeFile(pathJWK, json.getBytes());
  83.            
  84.         }catch(Exception t) {
  85.             throw new UtilsException(t.getMessage(),t);
  86.         }
  87.        
  88.     }

  89.     public static String convert(PublicKey pKey, String kid, boolean jwks, boolean pretty) throws UtilsException {
  90.        
  91.         JWK jwk = new JWK(pKey, kid);
  92.         String json = null;
  93.        
  94.         if(jwks) {
  95.             JWKSet jwkSet = new JWKSet();
  96.             jwkSet.addJwk(jwk);
  97.             json = pretty? jwkSet.getJsonPretty() : jwkSet.getJson();
  98.         }
  99.         else {
  100.             json = pretty? jwk.getJsonPretty() : jwk.getJson();
  101.         }
  102.        
  103.         return json;
  104.     }
  105. }