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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
| package org.ximodante.certificatemanagers;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.Security;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
public class PKCSUtils {
public static String[][] CARD_DRIVERS =
new String[][]
{ {"WIN_G&D","c:\\windows\\system32\\aetpkss1.dll"},
{"WIN_SIEMENS_OLD","c:\\windows\\system32\\siecap11.dll"},
{"UNIX_G&D","/usr/lib/libaetpkss.so.3"}
};
public static OsTypes getOsType() {
String OSName=System.getProperty("os.name").toLowerCase();
if (OSName.indexOf("win") >= 0 ) return OsTypes.WINDOWS;
else if (OSName.indexOf("nix") >= 0 || OSName.indexOf("nux") >= 0 || OSName.indexOf("aix") > 0 ) return OsTypes.UNIX;
else if (OSName.indexOf("mac") >= 0 ) return OsTypes.MAC;
else return OsTypes.ERROR;
}
public static String getJavaVersion() {
return System.getProperty("java.version");
}
/**
* Return location of the Smart Card driver. It is indicated in CARD_DRIVERS
*
* example call: getPKCS11ConfigString("UNIX_G&D")
*
* @param smartCardDriverdriver
* @return
*/
public static String getPKCS11ConfigString(String smartCardDriverdriver) {
// See See https://stackoverflow.com/a/46524444/7704658 to indicate not a file but the content of the configuration
// a prefix= "--" is used to know it is not a file name
String PKCS11CfgStr="";
if (!getJavaVersion().startsWith("1.")) PKCS11CfgStr="-- ";
PKCS11CfgStr=PKCS11CfgStr + "name = SmartCard";
boolean found=false;
for (String[] as: CARD_DRIVERS) {
if (as[0].equalsIgnoreCase(smartCardDriverdriver) && !found) {
PKCS11CfgStr= PKCS11CfgStr + "\n library = " + as[1];
found=true;
}
}
if (!found) throw new java.lang.RuntimeException("Driver name: " + smartCardDriverdriver + " NOT FOUND in PKCS11Utils.CARD_DRIVERS");
return PKCS11CfgStr;
}
/**
* Get a Keystore from a PKCS11 Certificate (Smart Card)
*
* Example call
* getPKCS11KeyStore("UNIX_G&D", "mySmartCardPassword")
*
* @param smartCardDriver : Specifies the OS and the Card and it is defined in CARD_DRIVERS static parameter of the class
* @param CertificatePassword
* @return a Java KeyStore
* @throws KeyStoreException
* @throws NoSuchAlgorithmException
* @throws CertificateException
* @throws IOException
* @throws ClassNotFoundException
* @throws SecurityException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static KeyStore getPKCS11KeyStore(String smartCardDriver, String CertificatePassword) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
String pkcs11config=getPKCS11ConfigString(smartCardDriver);
Provider pkcs11Provider = null;
// As the class does not exists in Java 9 +, we will construct it by reflection or the compiler crashes
if (getJavaVersion().startsWith("1.")) {
byte[] pkcs11configBytes = pkcs11config.getBytes();
ByteArrayInputStream configStream = new ByteArrayInputStream(pkcs11configBytes);
Class<?> plcs11Class = Class.forName("sun.security.pkcs11.SunPKCS11");
Constructor<?> constructor = plcs11Class.getConstructor(new Class[]{ByteArrayInputStream.class});
pkcs11Provider = (Provider) constructor.newInstance(configStream);
// For JaVA 9+
} else {
pkcs11Provider = Security.getProvider("SunPKCS11");
pkcs11Provider = pkcs11Provider.configure(pkcs11config);
}
Security.addProvider(pkcs11Provider);
KeyStore ks = KeyStore.getInstance("PKCS11", pkcs11Provider);
ks.load(null, CertificatePassword.toCharArray());
return ks;
}
/**
*
* @param certURL : The full name of the file for instance: "/home/myuser/certs/mycert.p12"
* @param CertificatePassword: for instance "MyPassword"
* @return a Java Keystore
* @throws KeyStoreException
* @throws NoSuchAlgorithmException
* @throws CertificateException
* @throws FileNotFoundException
* @throws IOException
*/
public static KeyStore getPKCS12KeyStore(String certURL, String CertificatePassword) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException {
KeyStore ks = KeyStore.getInstance("PKCS12");
if (CertificatePassword.length()>0) ks.load(new FileInputStream(certURL), CertificatePassword.toCharArray());
else ks.load(new FileInputStream(certURL), null);
return ks;
}
/**
* Get the certificate chain from a java keystore
* @param ks
* @return
* @throws KeyStoreException
*/
public static Certificate[] getCertificateChain(KeyStore ks) throws KeyStoreException {
String alias = (String) ks.aliases().nextElement();
Certificate[] myCertChain = ks.getCertificateChain(alias);
return myCertChain;
}
/**
* Get the certificate chain from the parameter to access a Java Smart Card certificate
* @param smartCardDriver Specifies the OS and the Card and it is defined in CARD_DRIVERS static parameter of the class
* @param CertificatePassword is the Smart Card Certificate
* @return the certificate chain
* @throws KeyStoreException
* @throws NoSuchAlgorithmException
* @throws CertificateException
* @throws IOException
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InvocationTargetException
*/
public static Certificate[] getPKCS11CertificateChain(String smartCardDriver, String CertificatePassword) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
KeyStore ks = getPKCS11KeyStore(smartCardDriver, CertificatePassword);
return getCertificateChain(ks);
}
/**
* get the alias of keystore
* @param ks
* @return
* @throws KeyStoreException
*/
public static String getCertAlias(KeyStore ks) throws KeyStoreException {
return (String) ks.aliases().nextElement();
}
/**
* Gets the alias defining the parameters of to access Smart card
* @param smartCardDriver Specifies the OS and the Card and it is defined in CARD_DRIVERS static parameter of the class
* @param CertificatePassword the smart card password
* @return
* @throws KeyStoreException
* @throws NoSuchAlgorithmException
* @throws CertificateException
* @throws IOException
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InvocationTargetException
*/
public static String getPKCS11CertAlias(String smartCardDriver, String CertificatePassword) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
KeyStore ks = getPKCS11KeyStore(smartCardDriver, CertificatePassword);
return (String) ks.aliases().nextElement();
}
/**
* Get the private key from a Java keystore knowing its password
* @param ks
* @param CertificatePassword
* @return
* @throws KeyStoreException
* @throws UnrecoverableKeyException
* @throws NoSuchAlgorithmException
*/
public static PrivateKey getPrivateKey(KeyStore ks, String CertificatePassword) throws KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException {
String alias=getCertAlias(ks);
return (PrivateKey) ks.getKey(alias, CertificatePassword.toCharArray());
}
/**
* Get the private key from a smart card accessing by its configuration and password
* @param smartCardDriver Specifies the OS and the Card and it is defined in CARD_DRIVERS static parameter of the class
* @param CertificatePassword the smart card password
* @return
* @throws KeyStoreException
* @throws UnrecoverableKeyException
* @throws NoSuchAlgorithmException
* @throws CertificateException
* @throws IOException
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InvocationTargetException
*/
public static PrivateKey getPKCS11PrivateKey(String smartCardDriver, String CertificatePassword) throws KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, CertificateException, IOException, ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
KeyStore ks = getPKCS11KeyStore(smartCardDriver, CertificatePassword);
String alias=getCertAlias(ks);
return (PrivateKey) ks.getKey(alias, CertificatePassword.toCharArray());
}
/**
* Tests
* @param args
* @throws KeyStoreException
* @throws NoSuchAlgorithmException
* @throws CertificateException
* @throws IOException
* @throws UnrecoverableKeyException
* @throws InvocationTargetException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws SecurityException
* @throws NoSuchMethodException
* @throws ClassNotFoundException
*/
public static void main(String args[]) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException, ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
String myCardPwd="MySmartCardPassword";
String myP12Pwd="MyP12Password";
String myCardDriver="UNIX_G&D";
String myP12File="/home/myuser/mycert.p12";
//0. Show java version and OOS
System.out.println("0.---------------------------------------------------------------------------");
System.out.println(getJavaVersion());
System.out.println(System.getProperty("os.name"));
System.out.println(getOsType());
System.out.println();
//1. Test getPKCS11KeyStore
System.out.println("1.---------------------------------------------------------------------------");
KeyStore ks1=getPKCS11KeyStore(myCardDriver, myCardPwd);
System.out.println(ks1.toString());
System.out.println();
//2. Test getPKCS12KeyStore
System.out.println("2.---------------------------------------------------------------------------");
KeyStore ks2=getPKCS12KeyStore(myP12File, myP12Pwd);
System.out.println(ks2.toString());
System.out.println();
//3. Test getCertificateChain
System.out.println("3.1.---------------------------------------------------------------------------");
Certificate[] cert1 =getCertificateChain(ks1);
System.out.println(cert1[0].toString());
System.out.println();
System.out.println("3.2.---------------------------------------------------------------------------");
Certificate[] cert2 =getCertificateChain(ks2);
System.out.println(cert2[0].toString());
System.out.println();
//4. Test getCertAlias
System.out.println("4.---------------------------------------------------------------------------");
String alias1 =getCertAlias(ks1);
System.out.println(alias1);
System.out.println();
String alias2 =getCertAlias(ks1);
System.out.println(alias2);
System.out.println();
//5. Test Private Key
System.out.println("5.---------------------------------------------------------------------------");
PrivateKey pKey1 =getPrivateKey(ks1, myCardPwd);
System.out.print(pKey1.getFormat()+" ");
System.out.print(pKey1.getAlgorithm()+" ");
System.out.print(pKey1.getClass()+" ");
System.out.println(pKey1.hashCode());
System.out.println();
PrivateKey pKey2 =getPrivateKey(ks2, myP12Pwd);
System.out.print(pKey2.getFormat()+" ");
System.out.print(pKey2.getAlgorithm()+" ");
System.out.print(pKey2.getClass()+" ");
System.out.println(pKey2.hashCode());
}
}
|