-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelper.java
More file actions
311 lines (283 loc) · 11.1 KB
/
Helper.java
File metadata and controls
311 lines (283 loc) · 11.1 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
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
/*
***************************************************************************
* File: Helper.java
***************************************************************************
***************************************************************************
* This file demonstrates the following common functions shared by other samples
* 1. Initialization library
* 2. Startup session
* 3. Close down session
* 4. Create key
* 5. Create Key Object
* 6. Find Key by Name
* 7. Find Vormetric PKCS11 library path
*/
import java.io.*;
import java.security.*;
import sun.security.pkcs11.wrapper.*;
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
import sun.security.pkcs11.Secmod.*;
import java.util.Arrays;
class Vpkcs11Session {
public PKCS11 p11;
long sessionHandle;
}
public class Helper {
public static final String UnixInstallPath = "/opt/vormetric/DataSecurityExpert/agent/pkcs11/lib/libvorpkcs11.so";
public static final String WinX86InstallPath = "C:\\Program Files (x86)\\Vormetric\\DataSecurityExpert\\Agent\\pkcs11\\bin\\vorpkcs11.dll";
public static final String WinX64InstallPath = "C:\\Program Files\\Vormetric\\DataSecurityExpert\\Agent\\pkcs11\\bin\\vorpkcs11.dll";
public static void saveKey(byte[] keyValue, String keyFileName)
{
FileOutputStream out = null;
try {
out = new FileOutputStream(keyFileName);
out.write(keyValue);
out.close();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
}
}
public static boolean isFileExist(String filePath)
{
File f = new File(filePath);
return f.exists();
}
public static long findKey(Vpkcs11Session session, String keyName)
{
/* Find the key on the DSM */
CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[]
{
new CK_ATTRIBUTE (CKA_LABEL, keyName),
};
try
{
/* Call C_FindObjectsFinal in case there is another find objects going on */
session.p11.C_FindObjectsFinal (session.sessionHandle);
session.p11.C_FindObjectsInit (session.sessionHandle, attrs);
long[] keyID = session.p11.C_FindObjects (session.sessionHandle, 1);
session.p11.C_FindObjectsFinal (session.sessionHandle);
if (keyID.length > 0)
{
return keyID[0];
}
else
{
/* System.out.println ("The key: " + keyName + " is not found."); */
return 0;
}
}
catch (PKCS11Exception e)
{
System.out.println (e.getMessage());
e.printStackTrace();
}
catch (Exception e)
{
System.out.println ("Exception thrown.");
System.out.println (e.getMessage());
e.printStackTrace();
}
return 0;
}
public static String getPKCS11LibPath(String libPath)
{
String path = null;
if (( libPath != null ) && (!libPath.equals("")))
{
path = libPath;
}
else
{
path = System.getenv("VPKCS11LIBPATH");
if ((path == null) || (path.equals("")))
{
String osName = System.getProperty("os.name");
if (osName.contains("Windows"))
{
if (isFileExist(WinX64InstallPath))
{
path = WinX64InstallPath;
}else if ( isFileExist(WinX86InstallPath))
{
path = WinX86InstallPath;
}
}
else
{
if (isFileExist(UnixInstallPath))
{
path = UnixInstallPath;
}
}
if ((path == null )|| (path.equals("")))
{
System.out.println ("Cannot find Vormetric PKCS11 library, please install Vormetric key agent. " );
System.exit(4);
}
}
else
{
if ( isFileExist(path))
{
// this is okay
}
else
{
System.out.println ("VPKCS11LIBPATH point to a file that does not exist: " + path);
System.exit(4);
}
}
}
System.out.println ("Loading the Vormetric PKCS11 library from : " + path);
return path;
}
// compare two binary files return true if the content is equal false otherwise
public static boolean CompareTwoFiles(String pathA, String pathB)
{
boolean match = false;
try
{
int BLOCK_SIZE = 4096;
int bytesReadA, bytesReadB;
// assume inputStreamA and inputStreamB are streams from your two files.
byte[] streamABlock = new byte[BLOCK_SIZE];
byte[] streamBBlock = new byte[BLOCK_SIZE];
FileInputStream inputStreamA = new FileInputStream(pathA);
FileInputStream inputStreamB = new FileInputStream(pathB);
do
{
bytesReadA = inputStreamA.read(streamABlock);
bytesReadB = inputStreamB.read(streamBBlock);
match = ((bytesReadA == bytesReadB) && Arrays.equals( streamABlock, streamBBlock));
} while (match && (bytesReadA > -1));
}
catch (Exception e)
{
System.out.println ("Exception thrown.");
System.out.println (e.getMessage());
}
return match;
}
public static long createKey(Vpkcs11Session session, String keyName)
{
long keyID = 0;
/* Create an AES 256 key on the DSM without pass in key value */
try {
CK_MECHANISM mechanism = new CK_MECHANISM (CKM_AES_KEY_GEN);
CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[]
{
new CK_ATTRIBUTE (CKA_LABEL, keyName),
new CK_ATTRIBUTE (CKA_CLASS, CKO_SECRET_KEY),
new CK_ATTRIBUTE (CKA_KEY_TYPE, CKK_AES),
new CK_ATTRIBUTE (CKA_VALUE_LEN, 32),
new CK_ATTRIBUTE (CKA_TOKEN, true),
new CK_ATTRIBUTE (CKA_ENCRYPT, true),
new CK_ATTRIBUTE (CKA_DECRYPT, true),
new CK_ATTRIBUTE (CKA_SIGN, false),
new CK_ATTRIBUTE (CKA_VERIFY, false),
new CK_ATTRIBUTE (CKA_WRAP, true),
new CK_ATTRIBUTE (CKA_UNWRAP, true),
new CK_ATTRIBUTE (CKA_EXTRACTABLE, false),
new CK_ATTRIBUTE (CKA_ALWAYS_SENSITIVE, false),
new CK_ATTRIBUTE (CKA_NEVER_EXTRACTABLE, true),
new CK_ATTRIBUTE (CKA_SENSITIVE, true),
new CK_ATTRIBUTE (0x80000061, true),
new CK_ATTRIBUTE (0x80000063, 44640)
};
System.out.println ("Before generating Key. Key Handle: " + keyID);
keyID = session.p11.C_GenerateKey (session.sessionHandle, mechanism, attrs);
System.out.println ("Key successfully Generated. Key Handle: " + keyID);
}
catch (PKCS11Exception e)
{
e.printStackTrace();
}
return keyID;
}
public static long createKeyObject(Vpkcs11Session session, String keyName, String appName, String keyValue)
{
long keyID = 0;
try {
/* Create an AES 256 key on the DSM */
/* AES key template.
* CKA_LABEL is the name of the key and will be displayed on the DSM
* CKA_VALUE is the bytes that make up the AES key.
*/
CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[]
{
new CK_ATTRIBUTE (CKA_LABEL, keyName),
new CK_ATTRIBUTE( CKA_APPLICATION, appName),
new CK_ATTRIBUTE (CKA_CLASS, CKO_SECRET_KEY),
new CK_ATTRIBUTE (CKA_KEY_TYPE, CKK_AES),
new CK_ATTRIBUTE (CKA_VALUE, keyValue) ,
new CK_ATTRIBUTE (CKA_VALUE_LEN, 32),
new CK_ATTRIBUTE (CKA_TOKEN, true),
new CK_ATTRIBUTE (CKA_ENCRYPT, true),
new CK_ATTRIBUTE (CKA_DECRYPT, true),
new CK_ATTRIBUTE (CKA_SIGN, false),
new CK_ATTRIBUTE (CKA_VERIFY, false),
new CK_ATTRIBUTE (CKA_WRAP, true),
new CK_ATTRIBUTE (CKA_UNWRAP, true),
new CK_ATTRIBUTE (CKA_EXTRACTABLE, false),
new CK_ATTRIBUTE (CKA_ALWAYS_SENSITIVE, false),
new CK_ATTRIBUTE (CKA_NEVER_EXTRACTABLE, true),
new CK_ATTRIBUTE (CKA_SENSITIVE, true)
};
keyID = session.p11.C_CreateObject(session.sessionHandle, attrs);
System.out.println ("Object successfully created. Object Handle: " + keyID);
} catch (PKCS11Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return keyID;
}
public static Vpkcs11Session startUp(String libPath, String pin)
{
try
{
Vpkcs11Session session = new Vpkcs11Session();
/* Initialization of the PKCS11 instance, open session and login */
session.p11 = PKCS11.getInstance(libPath, "C_GetFunctionList", null, false);
long[] slots = session.p11.C_GetSlotList (false);
session.sessionHandle = session.p11.C_OpenSession (slots[0], 0, null, null);
System.out.println ("Session successfully opened. Handle: " + session.sessionHandle);
session.p11.C_Login (session.sessionHandle, CKU_USER, pin.toCharArray());
System.out.println ("Successfully Logged in");
return session;
}
catch (PKCS11Exception e)
{
System.out.println (e.getMessage());
e.printStackTrace();
}
catch (Exception e)
{
System.out.println ("Exception thrown.");
System.out.println (e.getMessage());
}
return null;
}
public static void closeDown(Vpkcs11Session session)
{
/* Logout and close session */
try
{
session.p11.C_Logout(session.sessionHandle);
System.out.println ("Successfully logged out.");
session.p11.C_CloseSession (session.sessionHandle);
System.out.println ("Successfully closed session.");
}
catch (PKCS11Exception e)
{
System.out.println (e.getMessage());
e.printStackTrace();
}
catch (Exception e)
{
System.out.println ("Exception thrown.");
System.out.println (e.getMessage());
}
}
}