Debian Codegraph @

src/tools/org/h2/dev/security/SecureKeyStoreBuilder.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.dev.security;
7
8 import java.security.Key;
9 import java.security.KeyStore;
10 import java.security.KeyStoreException;
11 import java.security.NoSuchAlgorithmException;
12 import java.security.UnrecoverableKeyException;
13 import java.security.cert.Certificate;
14 import java.security.cert.CertificateEncodingException;
15 import java.util.Enumeration;
16
17 import org.h2.security.CipherFactory;
18 import org.h2.util.StringUtils;
19
20 /**
21 * Tool to generate source code for the SecureSocketFactory. First, create a
22 * keystore using:
23 * <pre>
24 * keytool -genkey -alias h2 -keyalg RSA -dname &quot;cn=H2&quot;
25 * -validity 25000 -keypass h2pass -keystore h2.keystore
26 * -storepass h2pass
27 * </pre>
28 * Then run this application to generate the source code. Then replace the code
29 * in the function SecureSocketFactory.getKeyStore as specified
30 */
31 public class SecureKeyStoreBuilder {
32
33 private SecureKeyStoreBuilder() {
34 // utility class
35 }
36
37 /**
38 * This method is called when executing this application from the command
39 * line.
40 *
41 * @param args the command line parameters
42 */
43 public static void main(String... args) throws Exception {
44 String password = CipherFactory.KEYSTORE_PASSWORD;
45 KeyStore store = CipherFactory.getKeyStore(password);
46 printKeystore(store, password);
47 }
48
49 private static void printKeystore(KeyStore store, String password)
50 throws KeyStoreException, NoSuchAlgorithmException,
51 UnrecoverableKeyException, CertificateEncodingException {
52 System.out.println("KeyStore store = KeyStore.getInstance(\""
53 + store.getType() + "\");");
54 System.out.println("store.load(null, password.toCharArray());");
55 // System.out.println("keystore provider=" +
56 // store.getProvider().getName());
57 Enumeration<String> en = store.aliases();
58 while (en.hasMoreElements()) {
59 String alias = en.nextElement();
60 Key key = store.getKey(alias, password.toCharArray());
61 System.out.println(
62 "KeyFactory keyFactory = KeyFactory.getInstance(\""
63 + key.getAlgorithm() + "\");");
64 System.out.println("store.load(null, password.toCharArray());");
65 String pkFormat = key.getFormat();
66 String encoded = StringUtils.convertBytesToHex(key.getEncoded());
67 System.out.println(
68 pkFormat + "EncodedKeySpec keySpec = new "
69 + pkFormat + "EncodedKeySpec(getBytes(\""
70 + encoded + "\"));");
71 System.out.println(
72 "PrivateKey privateKey = keyFactory.generatePrivate(keySpec);");
73 System.out.println("Certificate[] certs = {");
74 for (Certificate cert : store.getCertificateChain(alias)) {
75 System.out.println(
76 " CertificateFactory.getInstance(\""+cert.getType()+"\").");
77 String enc = StringUtils.convertBytesToHex(cert.getEncoded());
78 System.out.println(
79 " generateCertificate(new ByteArrayInputStream(getBytes(\""
80 + enc + "\"))),");
81 // PublicKey pubKey = cert.getPublicKey();
82 // System.out.println(" pubKey algorithm=" +
83 // pubKey.getAlgorithm());
84 // System.out.println(" pubKey format=" +
85 // pubKey.getFormat());
86 // System.out.println(" pubKey format="+
87 // Utils.convertBytesToString(pubKey.getEncoded()));
88 }
89 System.out.println("};");
90 System.out.println("store.setKeyEntry(\"" + alias
91 + "\", privateKey, password.toCharArray(), certs);");
92 }
93 }
94
95 // private void listCipherSuites(SSLServerSocketFactory f) {
96 // String[] def = f.getDefaultCipherSuites();
97 // for (int i = 0; i < def.length; i++) {
98 // System.out.println("default = " + def[i]);
99 // }
100 // String[] sup = f.getSupportedCipherSuites();
101 // for (int i = 0; i < sup.length; i++) {
102 // System.out.println("supported = " + sup[i]);
103 // }
104 // }
105
106 }