JWKPrivateKeyConverter.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.PrivateKey;
  22. import java.security.PublicKey;
  23. import java.util.UUID;

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

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

  35.     public static void main(String [] args) throws UtilsException {
  36.        
  37.         if(args==null || args.length<3) {
  38.             throw new UtilsException("ERROR: argomenti non forniti (USAGE: JWKPrivateKeyConverter pathPublicKey pathPrivateKey[:::password] pathJWK [kid] [jwkset(true/false)] [pretty(true/false)])");
  39.         }
  40.        
  41.         try {
  42.                    
  43.             java.security.Security.addProvider(
  44.                      new org.bouncycastle.jce.provider.BouncyCastleProvider()
  45.             );
  46.            
  47.             String pathPublicKey = args[0];
  48.             byte[] publicKey = FileSystemUtilities.readBytesFromFile(pathPublicKey);
  49.            
  50.             String pathPrivateKey = args[1];
  51.             String passwordPrivateKey = null;
  52.             if(pathPrivateKey.contains(":::")) {
  53.                 String [] tmp = pathPrivateKey.split(":::");
  54.                 if(tmp.length!=2) {
  55.                     throw new UtilsException("Path private key with wrong format");
  56.                 }
  57.                 pathPrivateKey = tmp[0];
  58.                 passwordPrivateKey = tmp[1];
  59.             }
  60.             byte[] privateKey = FileSystemUtilities.readBytesFromFile(pathPrivateKey);
  61.            
  62.             KeyUtils keyUtils = new KeyUtils(KeyUtils.ALGO_RSA);
  63.            
  64.             PublicKey pKey = keyUtils.getPublicKey(publicKey);
  65.            
  66.             PrivateKey privKey = null;
  67.             if(passwordPrivateKey!=null) {
  68.                 privKey = keyUtils.getPrivateKey(privateKey, passwordPrivateKey);
  69.             }
  70.             else {
  71.                 privKey = keyUtils.getPrivateKey(privateKey);
  72.             }
  73.            
  74.             String pathJWK = args[2];
  75.                        
  76.             String kid = null;
  77.             if(args.length>3) {
  78.                 kid = args[3];
  79.             }
  80.             if(kid==null || StringUtils.isEmpty(kid)) {
  81.                 kid = UUID.randomUUID().toString();
  82.             }
  83.             if(JWKPublicKeyConverter.KID_NULL.equals(kid)) {
  84.                 kid = null;
  85.             }
  86.            
  87.             boolean jwks = true;
  88.             if(args.length>4) {
  89.                 String tmp = args[4];
  90.                 jwks = "true".equals(tmp);
  91.             }
  92.            
  93.             boolean pretty = false;
  94.             if(args.length>5) {
  95.                 String tmp = args[5];
  96.                 pretty = "true".equals(tmp);
  97.             }
  98.            
  99.             String json = convert(pKey, privKey, kid, jwks, pretty);
  100.            
  101.             FileSystemUtilities.writeFile(pathJWK, json.getBytes());
  102.            
  103.         }catch(Exception t) {
  104.             throw new UtilsException(t.getMessage(),t);
  105.         }
  106.        
  107.     }
  108.    
  109.     public static String convert(PublicKey pKey, PrivateKey privKey, String kid, boolean jwks, boolean pretty) throws UtilsException {
  110.         JWK jwk = new JWK(pKey,privKey,kid);
  111.         String json = null;
  112.        
  113.         if(jwks) {
  114.             JWKSet jwkSet = new JWKSet();
  115.             jwkSet.addJwk(jwk);
  116.             json = pretty? jwk.getJsonPretty() : jwk.getJson();
  117.             json = "{\"keys\":[" + json + "]}";
  118.         }
  119.         else {
  120.             json = pretty? jwk.getJsonPretty() : jwk.getJson();
  121.         }
  122.        
  123.         return json;
  124.     }
  125. }