forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyMgmt.java
More file actions
executable file
·159 lines (140 loc) · 4.17 KB
/
Copy pathKeyMgmt.java
File metadata and controls
executable file
·159 lines (140 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package javaforce;
/**
* Key Management API - just some members to load OpenSSL cert.
*
* @author pquiring
*
* Created : Oct 8, 2013
*/
import java.security.*;
import java.security.spec.*;
import java.security.cert.*;
import java.io.*;
import java.util.*;
public class KeyMgmt {
private KeyStore keyStore = null;
/** Executes keytool directly */
public static boolean keytool(String args[]) {
ArrayList<String> cmd = new ArrayList<String>();
try {
if (JF.isWindows()) {
cmd.add(System.getProperty("java.home") + "\\bin\\keytool.exe");
} else {
cmd.add(System.getProperty("java.home") + "/bin/keytool");
}
for(int a=0;a<args.length;a++) {
cmd.add(args[a]);
}
String sa[] = cmd.toArray(new String[cmd.size()]);
/*
System.out.print("cmd=");
for(int a=0;a<sa.length;a++) {
System.out.print(sa[a] + " ");
}
*/
Process p = Runtime.getRuntime().exec(sa);
p.waitFor();
return true;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
/**
* Open an existing keystore (Note: use null for InputStream to create a blank keystore)
*/
public boolean open(InputStream is, char[] pwd) {
try {
keyStore = KeyStore.getInstance("JKS", "SUN");
keyStore.load(is, pwd);
return true;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
public boolean save(OutputStream os, char[] pwd) {
try {
keyStore.store(os, pwd);
return true;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
public boolean loadKEYandCRT(String alias, InputStream keyStream, InputStream certStream, char[] pwd) {
try {
// loading Key
KeyFactory kf = KeyFactory.getInstance("RSA");
byte key[] = JF.readAll(keyStream);
PKCS8EncodedKeySpec keysp = new PKCS8EncodedKeySpec(key);
PrivateKey ff = kf.generatePrivate(keysp);
// loading CertificateChain
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Collection c = cf.generateCertificates(certStream);
java.security.cert.Certificate[] certs;
certs = (java.security.cert.Certificate[]) c.toArray();
// set key / cert pair
keyStore.setKeyEntry(alias, ff, pwd, certs);
return true;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
public boolean loadCRT(String alias, InputStream certStream) {
try {
// loading CertificateChain
CertificateFactory cf = CertificateFactory.getInstance("X.509");
java.security.cert.Certificate crt = cf.generateCertificate(certStream);
// set one cert
keyStore.setCertificateEntry(alias, crt);
return true;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
public boolean hasCRT(InputStream certStream) {
try {
// loading CertificateChain
CertificateFactory cf = CertificateFactory.getInstance("X.509");
java.security.cert.Certificate crt = cf.generateCertificate(certStream);
return keyStore.getCertificateAlias(crt) != null;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
public java.security.cert.Certificate getCRT(String alias) {
try {
return keyStore.getCertificate(alias);
} catch (Exception e) {
return null;
}
}
public java.security.Key getKEY(String alias, char[] password) {
try {
return keyStore.getKey(alias, password);
} catch (Exception e) {
return null;
}
}
public static String fingerprintSHA256(byte key[]) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte res[] = md.digest(key);
StringBuilder sb = new StringBuilder();
for(int a=0;a<res.length;a++) {
int b = ((int)res[a]) & 0xff;
if (a > 0) sb.append(":");
if (b < 16) sb.append("0");
sb.append(Integer.toString(b, 16).toUpperCase());
}
return sb.toString();
} catch (Exception e) {
JFLog.log(e);
return null;
}
}
}