|
| 1 | +package com.acuity.iot.dsa.dslink.sys.cert; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.File; |
| 5 | +import java.io.FileReader; |
| 6 | +import java.io.IOException; |
| 7 | +import java.nio.file.Files; |
| 8 | +import java.nio.file.Paths; |
| 9 | + |
| 10 | +import org.iot.dsa.logging.DSLogger; |
| 11 | +import org.iot.dsa.time.DSTime; |
| 12 | + |
| 13 | +public class KeyToolUtil extends DSLogger { |
| 14 | + |
| 15 | + private static KeyToolUtil inst = new KeyToolUtil(); |
| 16 | + private KeyToolUtil() { |
| 17 | + |
| 18 | + } |
| 19 | + |
| 20 | + private void executeCommand(String[] cmd) { |
| 21 | + try { |
| 22 | + ProcessBuilder builder = new ProcessBuilder(); |
| 23 | + Process process = builder.command(cmd).start(); |
| 24 | + process.waitFor(); |
| 25 | + } catch (Exception e) { |
| 26 | + error("", e); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + public static void generateSelfSigned(String keystore, String password) { |
| 31 | + String[] cmd = new String[]{ |
| 32 | + "keytool", |
| 33 | + "-genkey", |
| 34 | + "-keystore", keystore, |
| 35 | + "-storepass", password, |
| 36 | + "-keypass", password, |
| 37 | + "-alias", "dsa", |
| 38 | + "-keyalg", "RSA", |
| 39 | + "-validity", "18000", |
| 40 | + "-dname", "\"CN=dslink-java-v2, O=DSA, C=US\"" |
| 41 | + }; |
| 42 | + inst.executeCommand(cmd); |
| 43 | + } |
| 44 | + |
| 45 | + public static String generateCSR(String keystore) throws IOException { |
| 46 | + String filename = "dsa.csr"; |
| 47 | + String[] cmd = new String[]{ |
| 48 | + "keytool", |
| 49 | + "-certreq", |
| 50 | + "-keystore", keystore, |
| 51 | + "-alias", "dsa", |
| 52 | + "-keyalg", "RSA", |
| 53 | + "-validity", "18000", |
| 54 | + "-dname", "\"CN=dslink-java-v2, O=DSA, C=US\"", |
| 55 | + "-file", filename |
| 56 | + }; |
| 57 | + inst.executeCommand(cmd); |
| 58 | + return new String(Files.readAllBytes(Paths.get(filename))); |
| 59 | + } |
| 60 | + |
| 61 | + public static void importCACert(String keystore, String certStr, String alias) throws IOException { |
| 62 | + String filename = DSTime.encodeForFiles(DSTime.getCalendar(System.currentTimeMillis()), new StringBuilder("tempCACert")).toString(); |
| 63 | + Files.write(Paths.get(filename), certStr.getBytes()); |
| 64 | + String[] cmd = new String[]{ |
| 65 | + "keytool", |
| 66 | + "-import", |
| 67 | + "-trustcacerts", |
| 68 | + "-keystore", keystore, |
| 69 | + "-alias", alias, |
| 70 | + "-file", filename |
| 71 | + }; |
| 72 | + inst.executeCommand(cmd); |
| 73 | + |
| 74 | + new File(filename).delete(); |
| 75 | + } |
| 76 | + |
| 77 | + public static void importPrimaryCert(String keystore, String certStr) throws IOException { |
| 78 | + String filename = DSTime.encodeForFiles(DSTime.getCalendar(System.currentTimeMillis()), new StringBuilder("tempCert")).toString(); |
| 79 | + Files.write(Paths.get(filename), certStr.getBytes()); |
| 80 | + String[] cmd = new String[]{ |
| 81 | + "keytool", |
| 82 | + "-import", |
| 83 | + "-trustcacerts", |
| 84 | + "-keystore", keystore, |
| 85 | + "-alias", "dsa", |
| 86 | + "-file", filename |
| 87 | + }; |
| 88 | + inst.executeCommand(cmd); |
| 89 | + |
| 90 | + new File(filename).delete(); |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments