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 |
|
package issrg.SAWS; |
47 |
|
|
48 |
|
import issrg.SAWS.util.SAWSLogWriter; |
49 |
|
|
50 |
|
import java.util.*; |
51 |
|
import java.io.*; |
52 |
|
import java.io.File; |
53 |
|
|
54 |
|
import java.io.BufferedReader; |
55 |
|
import java.io.FileInputStream; |
56 |
|
import java.io.FileNotFoundException; |
57 |
|
import java.io.InputStreamReader; |
58 |
|
|
59 |
|
import java.security.*; |
60 |
|
import javax.crypto.*; |
61 |
|
import java.security.AlgorithmParameters; |
62 |
|
import javax.crypto.spec.* ; |
63 |
|
|
64 |
|
import javax.security.auth.callback.*; |
65 |
|
|
66 |
|
import javax.swing.*; |
67 |
|
|
68 |
|
import issrg.SAWS.callback.SAWSGUICallbackHandler; |
69 |
|
import issrg.SAWS.callback.SAWSTextOutputCallback; |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
@author |
74 |
|
|
|
|
| 59.1% |
Uncovered Elements: 142 (347) |
Complexity: 51 |
Complexity Density: 0.28 |
|
75 |
|
public class LogFileWriter { |
76 |
|
|
77 |
|
|
78 |
|
public int currentRecordWriteCount= -1 ; |
79 |
|
public byte[] accumulatedHash = null; |
80 |
|
public byte [] finalSignature = null; |
81 |
|
|
82 |
|
private String currentLogFilename = null; |
83 |
|
private int currentLogSequence = -1; |
84 |
|
|
85 |
|
private File CurrentLogFile = null; |
86 |
|
private java.security.MessageDigest accMD = null; |
87 |
|
private RandomAccessFile rafCurrentLogFile = null; |
88 |
|
private String logFileRoot; |
89 |
|
|
90 |
|
private byte[] secureRandomBytes = new byte[20]; |
91 |
|
private SecretKey symmetricKeyInLog = null; |
92 |
|
private byte[] baSigningPublicKeyCert = null; |
93 |
|
|
94 |
|
private int thisRecordLength = 0; |
95 |
|
private int lastRecordLength = 0; |
96 |
|
|
97 |
|
|
98 |
|
private PublicKey sawsEncryptionPublicKey = null; |
99 |
|
private PrivateKey sawsEncryptionPrivateKey = null; |
100 |
|
private PublicKey sawsSigningPublicKey = null; |
101 |
|
private PrivateKey sawsSigningPrivateKey = null; |
102 |
|
private PublicKey vtEncryptionPublicKey = null; |
103 |
|
private PublicKey rootCAPublicKey = null; |
104 |
|
private Map UserIDPKMap = null; |
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
private byte hashAlgorithm = SAWSConstant.SHA1; |
110 |
|
private String hashAlgorithmName = "SHA1"; |
111 |
|
|
112 |
|
private byte [] lastAccumulatedHash = null; |
113 |
|
|
114 |
|
private int debugLevel = 0; |
115 |
|
|
116 |
|
private CallbackHandler callbackHandler = new SAWSGUICallbackHandler(); |
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
private static SAWSLogWriter sawsDebugLog = new SAWSLogWriter(LogFileWriter.class.getName()); |
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
128 |
0
|
public LogFileWriter() {... |
129 |
|
} |
130 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
131 |
11
|
public void setDebugLevel(int debugLevel) {... |
132 |
11
|
this.debugLevel = debugLevel; |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
@param |
139 |
|
@param |
140 |
|
@param |
141 |
|
@param |
142 |
|
@param |
143 |
|
@param |
144 |
|
|
145 |
|
@param |
146 |
|
@param |
147 |
|
|
148 |
|
@return |
149 |
|
|
|
|
| 81.2% |
Uncovered Elements: 3 (16) |
Complexity: 3 |
Complexity Density: 0.21 |
|
150 |
11
|
public LogFileWriter(String root, String cLogFilename, PublicKey vt, PublicKey sawsEncPK, PrivateKey sawsSignPIK,... |
151 |
|
byte[] ba, byte hashAlgorithm, byte[] secureBytes, Map UserIDPKMap) { |
152 |
11
|
logFileRoot = root; |
153 |
11
|
currentLogFilename = cLogFilename; |
154 |
11
|
vtEncryptionPublicKey = vt; |
155 |
11
|
sawsEncryptionPublicKey = sawsEncPK; |
156 |
11
|
sawsSigningPrivateKey = sawsSignPIK; |
157 |
11
|
baSigningPublicKeyCert = ba; |
158 |
|
|
159 |
11
|
this.hashAlgorithm = hashAlgorithm; |
160 |
11
|
this.setHashAlgorithmName(hashAlgorithm); |
161 |
|
|
162 |
11
|
if (secureBytes != null) |
163 |
0
|
secureRandomBytes = secureBytes; |
164 |
|
|
165 |
11
|
try{ |
166 |
11
|
accMD = java.security.MessageDigest.getInstance(this.hashAlgorithmName); |
167 |
|
} catch (Exception e) { |
168 |
0
|
e.printStackTrace(System.err); |
169 |
|
} |
170 |
11
|
this.UserIDPKMap = UserIDPKMap; |
171 |
|
|
172 |
|
|
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
@param |
179 |
|
@param |
180 |
|
@param |
181 |
|
@param |
182 |
|
@param |
183 |
|
@param |
184 |
|
|
185 |
|
@param |
186 |
|
@param |
187 |
|
|
188 |
|
@return |
189 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
190 |
11
|
public LogFileWriter(String root, String cLogFilename, PublicKey vt, PublicKey sawsEncPK, PrivateKey sawsSignPIK,... |
191 |
|
byte[] ba, byte hashAlgorithm, byte[] secureBytes, Map UserIDPKMap, CallbackHandler ch) { |
192 |
11
|
this(root, cLogFilename, vt, sawsEncPK, sawsSignPIK, ba, hashAlgorithm, |
193 |
|
secureBytes, UserIDPKMap); |
194 |
11
|
this.callbackHandler = ch; |
195 |
|
} |
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
@param |
202 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
203 |
0
|
public void setCallbackHandler(CallbackHandler ch) {... |
204 |
0
|
if (ch != null) { |
205 |
0
|
this.callbackHandler = ch; |
206 |
|
} |
207 |
|
} |
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
@see |
213 |
|
|
214 |
|
@param |
215 |
|
|
|
|
| 30% |
Uncovered Elements: 7 (10) |
Complexity: 4 |
Complexity Density: 0.67 |
|
216 |
11
|
public void setHashAlgorithmName(byte hashAlgorithm) {... |
217 |
11
|
if ((hashAlgorithm >= 0) && (hashAlgorithm < SAWSConstant.HASH_ALG_NAMES.length)) { |
218 |
11
|
this.hashAlgorithmName = SAWSConstant.HASH_ALG_NAMES[hashAlgorithm]; |
219 |
|
} else { |
220 |
0
|
this.showMessage("Hash algorithm not supported. SAWS will stop", |
221 |
|
SAWSTextOutputCallback.WARNING); |
222 |
|
|
223 |
0
|
if (debugLevel > SAWSConstant.NoInfo) { |
224 |
0
|
sawsDebugLog.write("Hash algorithm not supported."); |
225 |
|
} |
226 |
0
|
System.exit(-1); |
227 |
|
} |
228 |
|
} |
229 |
|
|
230 |
|
|
231 |
|
|
232 |
|
@param |
233 |
|
|
234 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
235 |
0
|
public void setHashAlgorithm(byte hashAlgorithm) {... |
236 |
0
|
this.hashAlgorithm = hashAlgorithm; |
237 |
|
} |
238 |
|
|
239 |
|
|
240 |
|
|
241 |
|
@return |
242 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
243 |
0
|
public byte getHashAlgorithm() {... |
244 |
0
|
return this.hashAlgorithm; |
245 |
|
} |
246 |
|
|
247 |
|
|
248 |
|
|
249 |
|
|
250 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
251 |
7
|
public byte[] getSignature(){... |
252 |
7
|
return finalSignature; |
253 |
|
} |
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
|
258 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
259 |
115
|
public byte[] getAccHash(){... |
260 |
115
|
return accumulatedHash; |
261 |
|
} |
262 |
|
|
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
267 |
173
|
public int getCurrentRecordWriteCount(){... |
268 |
173
|
return currentRecordWriteCount; |
269 |
|
} |
270 |
|
|
271 |
|
|
272 |
|
|
273 |
|
|
274 |
|
@return |
275 |
|
|
|
|
| 55.9% |
Uncovered Elements: 26 (59) |
Complexity: 11 |
Complexity Density: 0.23 |
|
276 |
11
|
public int prepareNewLog(){... |
277 |
11
|
try{ |
278 |
11
|
accMD.reset(); |
279 |
11
|
accMD.update(currentLogFilename.getBytes()); |
280 |
|
} catch (Exception e){ |
281 |
0
|
this.showMessage("Updating accumulated hash error. SAWS will stop.", |
282 |
|
SAWSTextOutputCallback.ERROR); |
283 |
|
|
284 |
0
|
if (debugLevel > SAWSConstant.NoInfo) { |
285 |
|
|
286 |
0
|
sawsDebugLog.write(e.toString()); |
287 |
|
} |
288 |
0
|
return -1; |
289 |
|
} |
290 |
|
|
291 |
11
|
if (debugLevel > SAWSConstant.NoInfo) { |
292 |
|
|
293 |
0
|
sawsDebugLog.write("Now creating a new log."); |
294 |
|
} |
295 |
11
|
currentRecordWriteCount = -1; |
296 |
|
|
297 |
11
|
CurrentLogFile = new File(logFileRoot, currentLogFilename ); |
298 |
|
|
299 |
11
|
try{ |
300 |
11
|
rafCurrentLogFile = new RandomAccessFile(CurrentLogFile, "rw"); |
301 |
|
} catch (Exception e) { |
302 |
0
|
this.showMessage("Open file " + currentLogFilename + " error. SAWS will stop.", |
303 |
|
SAWSTextOutputCallback.ERROR); |
304 |
|
|
305 |
0
|
if (debugLevel > SAWSConstant.NoInfo) { |
306 |
|
|
307 |
0
|
sawsDebugLog.write(e.toString()); |
308 |
|
} |
309 |
0
|
return -1; |
310 |
|
} |
311 |
|
|
312 |
|
|
313 |
11
|
SecureRandom random = null; |
314 |
11
|
try { |
315 |
11
|
random = SecureRandom.getInstance("SHA1PRNG", "SUN"); |
316 |
11
|
random.nextBytes(secureRandomBytes); |
317 |
|
} catch (Exception e1) { |
318 |
0
|
this.showMessage("Generating random number error. SAWS will stop.", |
319 |
|
SAWSTextOutputCallback.ERROR); |
320 |
|
|
321 |
|
|
322 |
0
|
if (debugLevel > SAWSConstant.NoInfo) { |
323 |
0
|
sawsDebugLog.write(e1.toString()); |
324 |
|
} |
325 |
0
|
return -1; |
326 |
|
} |
327 |
|
|
328 |
11
|
try{ |
329 |
11
|
this.createSAWSRecord(new byte[]{this.hashAlgorithm}, SAWSConstant.SAWSHashAlgorithmType, |
330 |
|
SAWSConstant.NoEncryptionFlag); |
331 |
|
|
332 |
11
|
KeyGenerator kgen = KeyGenerator.getInstance("AES"); |
333 |
11
|
kgen.init(128); |
334 |
11
|
symmetricKeyInLog = kgen.generateKey(); |
335 |
11
|
byte[] rawKey = symmetricKeyInLog.getEncoded(); |
336 |
|
|
337 |
11
|
createSAWSRecord(secureRandomBytes, SAWSConstant.SAWSSecretRandomNumberType, |
338 |
|
SAWSConstant.AsymmetricEncryptionFlag) ; |
339 |
|
|
340 |
11
|
createSAWSRecord(rawKey, SAWSConstant.SymmetricEncryptionKeyType, |
341 |
|
SAWSConstant.AsymmetricEncryptionFlag); |
342 |
|
|
343 |
11
|
createSAWSRecord( rawKey, SAWSConstant.SymmetricEncryptionKeyType, (byte) SAWSConstant.USERVT, |
344 |
|
SAWSConstant.AsymmetricEncryptionFlag, |
345 |
|
vtEncryptionPublicKey) ; |
346 |
|
|
347 |
11
|
Iterator idpkIt = UserIDPKMap.entrySet().iterator(); |
348 |
33
|
while ( idpkIt.hasNext() ) { |
349 |
22
|
Map.Entry userIDPK = (Map.Entry) idpkIt.next(); |
350 |
22
|
byte uID = ((Byte) userIDPK.getKey() ).byteValue(); |
351 |
22
|
PublicKey pk = (PublicKey) userIDPK.getValue(); |
352 |
|
|
353 |
22
|
createSAWSRecord( rawKey, SAWSConstant.SymmetricEncryptionKeyType, |
354 |
|
(byte) uID, |
355 |
|
SAWSConstant.AsymmetricEncryptionFlag, |
356 |
|
pk) ; |
357 |
|
|
358 |
|
} |
359 |
|
|
360 |
11
|
createSAWSRecord(baSigningPublicKeyCert, SAWSConstant.SAWSCertificateType, |
361 |
|
SAWSConstant.NoEncryptionFlag); |
362 |
|
|
363 |
11
|
writeSignatureRecord(SAWSConstant.SAWSHeaderSignatureType); |
364 |
|
|
365 |
|
} catch (Exception e3) { |
366 |
0
|
this.showMessage("Prepare new log error. SAWS will stop", |
367 |
|
SAWSTextOutputCallback.ERROR); |
368 |
|
|
369 |
0
|
if (debugLevel > SAWSConstant.NoInfo) { |
370 |
|
|
371 |
0
|
sawsDebugLog.write(e3.toString()); |
372 |
|
} |
373 |
0
|
return -1; |
374 |
|
} |
375 |
11
|
return 0; |
376 |
|
|
377 |
|
} |
378 |
|
|
379 |
|
|
380 |
|
|
381 |
|
|
382 |
|
|
383 |
|
@param |
384 |
|
|
385 |
|
|
|
|
| 42.9% |
Uncovered Elements: 4 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
386 |
214
|
private void writeLogRecord (byte[] logRecord){ ... |
387 |
|
|
388 |
214
|
try{ |
389 |
214
|
rafCurrentLogFile.write(logRecord); |
390 |
|
} catch (Exception e) { |
391 |
0
|
if (debugLevel > SAWSConstant.NoInfo) { |
392 |
|
|
393 |
0
|
sawsDebugLog.write(e.toString()); |
394 |
|
} |
395 |
|
} |
396 |
|
|
397 |
214
|
long currentTime = System.currentTimeMillis(); |
398 |
|
} |
399 |
|
|
400 |
|
|
401 |
|
|
402 |
|
|
403 |
|
|
404 |
|
@param |
405 |
|
@param |
406 |
|
@param |
407 |
|
|
408 |
|
@return |
409 |
|
|
|
|
| 84.6% |
Uncovered Elements: 2 (13) |
Complexity: 2 |
Complexity Density: 0.18 |
|
410 |
11
|
public void createLastFileRecord(String lastFilename, byte[] lastAccHash, byte[] lastSignature){... |
411 |
11
|
byte[] b1 = lastFilename.getBytes(); |
412 |
11
|
byte[] all = new byte[b1.length + lastAccHash.length + lastSignature.length + 4*3 ]; |
413 |
11
|
System.arraycopy(utility.intToByteArray(b1.length), 0, all, 0, 4); |
414 |
11
|
System.arraycopy(b1, 0, all, 4, b1.length); |
415 |
|
|
416 |
11
|
System.arraycopy(utility.intToByteArray(lastAccHash.length), 0, all, 4 + b1.length , 4); |
417 |
11
|
System.arraycopy(lastAccHash, 0, all, 4 + b1.length + 4, lastAccHash.length); |
418 |
|
|
419 |
11
|
System.arraycopy(utility.intToByteArray(lastSignature.length), 0, all, 4 + b1.length + 4 |
420 |
|
+ lastAccHash.length , 4); |
421 |
11
|
System.arraycopy(lastSignature, 0, all, |
422 |
|
4 + b1.length + 4 + lastAccHash.length + 4, lastSignature.length); |
423 |
11
|
if (debugLevel > SAWSConstant.NoInfo) { |
424 |
|
|
425 |
0
|
sawsDebugLog.write("\n last log file record created, length = " + all.length); |
426 |
|
} |
427 |
|
|
428 |
11
|
createSAWSRecord(all, SAWSConstant.SAWSLastFileType, SAWSConstant.NoEncryptionFlag); |
429 |
|
} |
430 |
|
|
431 |
|
|
432 |
|
|
433 |
|
|
434 |
|
|
435 |
|
|
436 |
|
|
437 |
|
|
438 |
|
@param |
439 |
|
@param |
440 |
|
@param |
441 |
|
@param |
442 |
|
@param |
443 |
|
@param |
444 |
|
|
445 |
|
@return |
446 |
|
|
|
|
| 68.6% |
Uncovered Elements: 11 (35) |
Complexity: 4 |
Complexity Density: 0.14 |
|
447 |
214
|
private byte[] createRecordHeader(int countN, byte recordType, byte UserID, byte encryptionFlag, ... |
448 |
|
int lastLength, int thisLength) |
449 |
|
{ |
450 |
|
|
451 |
214
|
byte recordHeader[] = new byte[SAWSConstant.HeaderLength]; |
452 |
214
|
recordHeader[0] = (byte)0xf0; |
453 |
214
|
recordHeader[1] = (byte)0xf0; |
454 |
214
|
recordHeader[2] = (byte)0xf0; |
455 |
214
|
recordHeader[3] = (byte)0xf0; |
456 |
|
|
457 |
|
|
458 |
214
|
if (debugLevel > SAWSConstant.NoInfo) { |
459 |
|
|
460 |
|
|
461 |
0
|
sawsDebugLog.write("\nSAWSWriter:createRecordHeader: count = " + countN); |
462 |
|
|
463 |
|
|
464 |
0
|
sawsDebugLog.write("SAWSWriter:createRecordHeader: recordType = " + SAWSConstant.getRecordTypeString( recordType )); |
465 |
0
|
sawsDebugLog.write("SAWSWriter:createRecordHeader: LastLength = " + lastLength); |
466 |
0
|
sawsDebugLog.write("SAWSWriter:createRecordHeader: thisLength = " + thisLength); |
467 |
|
} |
468 |
|
|
469 |
214
|
byte[] ba1 = utility.intToByteArray(countN); |
470 |
214
|
System.arraycopy(ba1, 0, recordHeader,4, 4); |
471 |
|
|
472 |
|
|
473 |
214
|
recordHeader[8] = recordType; |
474 |
214
|
recordHeader[9] = UserID; |
475 |
214
|
recordHeader[10] = encryptionFlag; |
476 |
|
|
477 |
214
|
long ltemp = System.currentTimeMillis(); |
478 |
|
|
479 |
214
|
byte[] ba2 = utility.longToByteArray(ltemp ); |
480 |
214
|
if (debugLevel > SAWSConstant.NoInfo) { |
481 |
|
|
482 |
|
|
483 |
0
|
sawsDebugLog.write("SAWSWriter:createRecordHeader: current time is: " + ltemp ); |
484 |
|
|
485 |
|
|
486 |
0
|
sawsDebugLog.write("SAWSWriter:createRecordHeader: length of ba2 is: " + ba2.length ); |
487 |
|
|
488 |
|
|
489 |
0
|
sawsDebugLog.write("SAWSWriter:createRecordHeader: ba2 is: " + utility.toHexString(ba2) ); |
490 |
|
} |
491 |
214
|
System.arraycopy(ba2, 0, recordHeader,11, 8); |
492 |
|
|
493 |
214
|
if (debugLevel > SAWSConstant.NoInfo) |
494 |
|
|
495 |
|
|
496 |
0
|
sawsDebugLog.write("SAWSWriter:createRecordHeader: after transmision: " + utility.byteArrayToLong(ba2) ); |
497 |
|
|
498 |
|
|
499 |
214
|
ba1 = utility.intToByteArray(lastLength); |
500 |
214
|
System.arraycopy(ba1, 0, recordHeader,19, 4); |
501 |
|
|
502 |
214
|
ba1 = utility.intToByteArray(thisLength ); |
503 |
214
|
System.arraycopy(ba1, 0, recordHeader,23, 4); |
504 |
|
|
505 |
214
|
return(recordHeader); |
506 |
|
|
507 |
|
} |
508 |
|
|
509 |
|
|
510 |
|
|
511 |
|
|
512 |
|
@return |
513 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
514 |
88
|
public void createSAWSRecord(byte[] messageBlock, byte recordType, byte encryptionFlag)... |
515 |
|
{ |
516 |
88
|
createSAWSRecord(messageBlock, recordType, (byte) 0x00, encryptionFlag); |
517 |
|
} |
518 |
|
|
519 |
|
|
520 |
|
|
521 |
|
|
522 |
|
|
523 |
|
@return |
524 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
525 |
88
|
public void createSAWSRecord(byte[] messageBlock, byte recordType, byte userID, byte encryptionFlag) {... |
526 |
88
|
createSAWSRecord( messageBlock, recordType, userID, encryptionFlag, sawsEncryptionPublicKey); |
527 |
|
} |
528 |
|
|
529 |
|
|
530 |
|
|
531 |
|
|
532 |
|
|
533 |
|
@param |
534 |
|
@param |
535 |
|
@param |
536 |
|
@param |
537 |
|
@param |
538 |
|
|
539 |
|
|
540 |
|
@return |
541 |
|
|
|
|
| 72.6% |
Uncovered Elements: 31 (113) |
Complexity: 18 |
Complexity Density: 0.2 |
|
542 |
214
|
public void createSAWSRecord(byte[] messageBlock, byte recordType, byte userID, ... |
543 |
|
byte encryptionFlag, PublicKey encryptionPublicKey) { |
544 |
|
|
545 |
214
|
byte[] bodyTemp = null; |
546 |
214
|
int thisRecordLength; |
547 |
214
|
if (debugLevel > SAWSConstant.NoInfo) |
548 |
|
|
549 |
|
|
550 |
0
|
sawsDebugLog.write("\nSAWSWriter:createSAWSRecord: currentCount = " + (currentRecordWriteCount+1) ); |
551 |
|
|
552 |
214
|
java.security.MessageDigest md = null; |
553 |
214
|
try { |
554 |
214
|
md = java.security.MessageDigest.getInstance(this.hashAlgorithmName); |
555 |
|
|
556 |
|
} catch (Exception e) { |
557 |
0
|
this.showMessage("Message digest error. SAWS will stop.", |
558 |
|
SAWSTextOutputCallback.ERROR); |
559 |
|
|
560 |
0
|
if (debugLevel > SAWSConstant.NoInfo) { |
561 |
|
|
562 |
0
|
sawsDebugLog.write(e.toString()); |
563 |
|
} |
564 |
0
|
System.exit(-1); |
565 |
|
} |
566 |
|
|
567 |
214
|
byte record[] = null; |
568 |
|
|
569 |
214
|
if (encryptionFlag == SAWSConstant.AsymmetricEncryptionFlag) { |
570 |
55
|
int messageLength = messageBlock.length; |
571 |
55
|
thisRecordLength = SAWSConstant.OUTPUTSIZE + SAWSConstant.HeaderLength + SAWSConstant.HashLength[this.hashAlgorithm] ; |
572 |
55
|
record = new byte[thisRecordLength]; |
573 |
|
|
574 |
55
|
++ currentRecordWriteCount; |
575 |
55
|
System.arraycopy( |
576 |
|
createRecordHeader(currentRecordWriteCount, recordType, userID, encryptionFlag, |
577 |
|
lastRecordLength, thisRecordLength), |
578 |
|
0, record, 0, SAWSConstant.HeaderLength); |
579 |
55
|
lastRecordLength = thisRecordLength; |
580 |
|
|
581 |
55
|
byte[] cipherText = null, c2=null; |
582 |
|
|
583 |
55
|
try{ |
584 |
|
|
585 |
55
|
javax.crypto.Cipher c1 = javax.crypto.Cipher.getInstance(encryptionPublicKey.getAlgorithm()); |
586 |
|
|
587 |
55
|
c1.init(Cipher.ENCRYPT_MODE, encryptionPublicKey); |
588 |
55
|
cipherText = c1.doFinal( messageBlock ); |
589 |
55
|
System.arraycopy(cipherText, 0, record,SAWSConstant.HeaderLength, SAWSConstant.OUTPUTSIZE); |
590 |
|
|
591 |
55
|
bodyTemp = new byte[SAWSConstant.HeaderLength+SAWSConstant.OUTPUTSIZE]; |
592 |
55
|
System.arraycopy(record, 0, bodyTemp,0, SAWSConstant.HeaderLength+SAWSConstant.OUTPUTSIZE); |
593 |
|
|
594 |
55
|
md.reset(); |
595 |
55
|
md.update(bodyTemp); |
596 |
55
|
md.update(secureRandomBytes); |
597 |
55
|
byte[] digest = md.digest(); |
598 |
|
|
599 |
55
|
System.arraycopy(digest, 0, record,SAWSConstant.HeaderLength+SAWSConstant.OUTPUTSIZE, |
600 |
|
SAWSConstant.HashLength[this.hashAlgorithm]); |
601 |
|
|
602 |
|
} catch(Exception e) { |
603 |
0
|
this.showMessage("Asymmetric encryption error. SAWS will stop.", |
604 |
|
SAWSTextOutputCallback.ERROR); |
605 |
|
|
606 |
0
|
if (debugLevel > SAWSConstant.NoInfo) { |
607 |
|
|
608 |
0
|
sawsDebugLog.write(e.toString()); |
609 |
|
} |
610 |
0
|
System.exit(-1); |
611 |
0
|
e.printStackTrace(); |
612 |
|
} |
613 |
|
} |
614 |
|
|
615 |
214
|
if (encryptionFlag == SAWSConstant.SymmetricEncryptionFlag) { |
616 |
65
|
int messageLength = messageBlock.length; |
617 |
65
|
byte[] encrypted = null; |
618 |
|
|
619 |
65
|
try{ |
620 |
65
|
Cipher cipher = Cipher.getInstance("AES"); |
621 |
65
|
cipher.init(Cipher.ENCRYPT_MODE, symmetricKeyInLog); |
622 |
65
|
encrypted = cipher.doFinal(messageBlock); |
623 |
|
|
624 |
65
|
thisRecordLength = encrypted.length + SAWSConstant.HeaderLength + SAWSConstant.HashLength[this.hashAlgorithm] ; |
625 |
65
|
record = new byte[thisRecordLength]; |
626 |
|
|
627 |
65
|
++ currentRecordWriteCount; |
628 |
65
|
System.arraycopy( |
629 |
|
createRecordHeader(currentRecordWriteCount, recordType, userID, |
630 |
|
encryptionFlag, lastRecordLength, thisRecordLength), |
631 |
|
0, record, 0, SAWSConstant.HeaderLength); |
632 |
65
|
lastRecordLength = thisRecordLength; |
633 |
|
|
634 |
65
|
System.arraycopy(encrypted, 0, record,SAWSConstant.HeaderLength, encrypted.length); |
635 |
|
|
636 |
65
|
bodyTemp = new byte[SAWSConstant.HeaderLength+encrypted.length]; |
637 |
65
|
System.arraycopy(record, 0, bodyTemp,0, SAWSConstant.HeaderLength+encrypted.length); |
638 |
|
|
639 |
65
|
md.reset(); |
640 |
65
|
md.update(bodyTemp); |
641 |
65
|
md.update(secureRandomBytes); |
642 |
65
|
byte[] digest = md.digest(); |
643 |
|
|
644 |
65
|
System.arraycopy(digest, 0, record,SAWSConstant.HeaderLength+encrypted.length, SAWSConstant.HashLength[this.hashAlgorithm]); |
645 |
|
|
646 |
|
|
647 |
|
|
648 |
|
} catch (Exception e) { |
649 |
0
|
this.showMessage("Symmetric encryption error. SAWS will stop.", |
650 |
|
SAWSTextOutputCallback.ERROR); |
651 |
|
|
652 |
0
|
if (debugLevel > SAWSConstant.NoInfo) { |
653 |
|
|
654 |
0
|
sawsDebugLog.write(e.toString()); |
655 |
|
} |
656 |
0
|
System.exit(-1); |
657 |
|
} |
658 |
|
} |
659 |
|
|
660 |
214
|
if (encryptionFlag == SAWSConstant.NoEncryptionFlag) { |
661 |
94
|
int messageLength = messageBlock.length; |
662 |
|
|
663 |
|
|
664 |
94
|
thisRecordLength = messageLength + SAWSConstant.HeaderLength + SAWSConstant.HashLength[this.hashAlgorithm] ; |
665 |
94
|
record = new byte[thisRecordLength]; |
666 |
|
|
667 |
94
|
++ currentRecordWriteCount; |
668 |
94
|
System.arraycopy( |
669 |
|
createRecordHeader(currentRecordWriteCount, recordType, userID, |
670 |
|
encryptionFlag, lastRecordLength, thisRecordLength), |
671 |
|
0, record, 0, SAWSConstant.HeaderLength ); |
672 |
94
|
lastRecordLength = thisRecordLength; |
673 |
|
|
674 |
94
|
System.arraycopy(messageBlock, 0, record,SAWSConstant.HeaderLength , messageBlock.length); |
675 |
|
|
676 |
94
|
bodyTemp = new byte[SAWSConstant.HeaderLength +messageBlock.length]; |
677 |
94
|
System.arraycopy(record, 0, bodyTemp,0, SAWSConstant.HeaderLength +messageBlock.length); |
678 |
|
|
679 |
94
|
try{ |
680 |
94
|
md.reset(); |
681 |
94
|
md.update(bodyTemp); |
682 |
94
|
md.update(secureRandomBytes); |
683 |
94
|
byte[] digest = md.digest(); |
684 |
|
|
685 |
94
|
System.arraycopy(digest, 0, record,SAWSConstant.HeaderLength +messageBlock.length, SAWSConstant.HashLength[this.hashAlgorithm]); |
686 |
|
|
687 |
|
} catch (Exception e) { |
688 |
0
|
this.showMessage("No-encryption record creating error. SAWS will stop.", |
689 |
|
SAWSTextOutputCallback.ERROR); |
690 |
|
|
691 |
0
|
if (debugLevel > SAWSConstant.NoInfo) |
692 |
|
|
693 |
|
|
694 |
0
|
sawsDebugLog.write(e.toString()); |
695 |
0
|
System.exit(-1); |
696 |
|
} |
697 |
|
|
698 |
|
|
699 |
|
|
700 |
|
|
701 |
|
|
702 |
|
|
703 |
|
|
704 |
|
|
705 |
|
|
706 |
|
|
707 |
|
|
708 |
|
|
709 |
|
|
710 |
|
} |
711 |
|
|
712 |
214
|
if ( ( recordType != SAWSConstant.SAWSAccumulatedHashType) && (recordType != SAWSConstant.SAWSLogFileSignatureType)) { |
713 |
192
|
try{ |
714 |
192
|
accMD.update(record); |
715 |
192
|
java.security.MessageDigest tc1 =(java.security.MessageDigest) accMD.clone(); |
716 |
192
|
if (this.accumulatedHash == null) { |
717 |
11
|
this.accumulatedHash = new byte[SAWSConstant.HashLength[this.hashAlgorithm]]; |
718 |
|
} |
719 |
192
|
accumulatedHash = tc1.digest(); |
720 |
|
|
721 |
|
} catch (Exception e){ |
722 |
0
|
if (debugLevel > SAWSConstant.NoInfo) |
723 |
|
|
724 |
|
|
725 |
0
|
sawsDebugLog.write(e.toString()); |
726 |
|
} |
727 |
|
} |
728 |
|
|
729 |
214
|
writeLogRecord (record); |
730 |
|
} |
731 |
|
|
732 |
|
|
733 |
|
|
734 |
|
|
735 |
|
@param |
736 |
|
@param |
737 |
|
@param |
738 |
|
@param |
739 |
|
|
740 |
|
@return |
741 |
|
|
|
|
| 0% |
Uncovered Elements: 32 (32) |
Complexity: 7 |
Complexity Density: 0.29 |
|
742 |
0
|
public int repairLog(String logRoot, String logFilename, byte[] secureRandomB, logReadingException e) {... |
743 |
0
|
File logFile = new File(logRoot, logFilename); |
744 |
|
|
745 |
0
|
if ( e.getErrorCode() != SAWSConstant.LogFileIncompleteErrCode ) { |
746 |
|
|
747 |
0
|
this.showMessage("This broken log file " + logFilename + "cannot be recovered. ", |
748 |
|
SAWSTextOutputCallback.WARNING); |
749 |
|
|
750 |
|
|
751 |
|
|
752 |
|
|
753 |
|
|
754 |
|
|
755 |
|
|
756 |
0
|
return -1; |
757 |
|
} |
758 |
|
|
759 |
0
|
try{ |
760 |
0
|
rafCurrentLogFile = new RandomAccessFile(logFile, "rw"); |
761 |
0
|
rafCurrentLogFile.seek(rafCurrentLogFile.length()); |
762 |
|
} catch (Exception e2) { |
763 |
0
|
if (debugLevel > SAWSConstant.NoInfo) |
764 |
|
|
765 |
|
|
766 |
0
|
sawsDebugLog.write(e2.toString()); |
767 |
0
|
return -1; |
768 |
|
} |
769 |
|
|
770 |
0
|
accMD = e.getAccMD(); |
771 |
|
|
772 |
0
|
if (this.accumulatedHash == null) { |
773 |
0
|
accumulatedHash = new byte[SAWSConstant.HashLength[this.hashAlgorithm]]; |
774 |
|
} |
775 |
|
|
776 |
0
|
accumulatedHash = e.getAccumulatedHash(); |
777 |
0
|
currentRecordWriteCount = e.getSequence(); |
778 |
0
|
secureRandomBytes = secureRandomB; |
779 |
|
|
780 |
0
|
String s1 = new String("" + SAWSConstant.LogFileIncompleteErrCode + ";log file is recovered here."); |
781 |
0
|
try{ |
782 |
0
|
createSAWSRecord( s1.getBytes(), SAWSConstant.SysAuditorNotificationType, |
783 |
|
(byte) SAWSConstant.USERSAWS, SAWSConstant.NoEncryptionFlag, |
784 |
|
null) ; |
785 |
0
|
finalizeLogFile(); |
786 |
|
} catch (Exception e3) { |
787 |
0
|
if (debugLevel > SAWSConstant.NoInfo) |
788 |
|
|
789 |
|
|
790 |
0
|
sawsDebugLog.write(e3.toString()); |
791 |
0
|
return -1; |
792 |
|
} |
793 |
0
|
return 0; |
794 |
|
} |
795 |
|
|
796 |
|
|
797 |
|
|
798 |
|
|
799 |
|
|
800 |
|
@param |
801 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
802 |
11
|
public void finalizeLogFile(){... |
803 |
11
|
if (debugLevel > 0) |
804 |
|
|
805 |
|
|
806 |
0
|
sawsDebugLog.write("\n\t ****************SAWSWriter:finalizeLogfile()******************* \n"); |
807 |
11
|
createSAWSRecord(accumulatedHash, SAWSConstant.SAWSAccumulatedHashType, SAWSConstant.NoEncryptionFlag) ; |
808 |
11
|
writeSignatureRecord(SAWSConstant.SAWSLogFileSignatureType); |
809 |
|
|
810 |
|
|
811 |
|
} |
812 |
|
|
813 |
|
|
814 |
|
|
815 |
|
|
816 |
|
|
817 |
|
@param |
818 |
|
|
|
|
| 57.1% |
Uncovered Elements: 6 (14) |
Complexity: 4 |
Complexity Density: 0.4 |
|
819 |
22
|
public void writeSignatureRecord(byte signatureType){... |
820 |
22
|
if (debugLevel >= SAWSConstant.VerboseInfo) |
821 |
|
|
822 |
|
|
823 |
0
|
sawsDebugLog.write("\n*********** SAWSWriter:writeSignatureRecord() ******************* \n"); |
824 |
|
|
825 |
|
|
826 |
22
|
try{ |
827 |
22
|
Signature sig = Signature.getInstance(sawsSigningPrivateKey.getAlgorithm()); |
828 |
|
|
829 |
22
|
sig.initSign(sawsSigningPrivateKey); |
830 |
22
|
sig.update(accumulatedHash); |
831 |
22
|
finalSignature = sig.sign(); |
832 |
22
|
createSAWSRecord(finalSignature, signatureType , SAWSConstant.NoEncryptionFlag); |
833 |
|
} catch (Exception e){ |
834 |
0
|
if (debugLevel >= SAWSConstant.ErrorInfo) |
835 |
|
|
836 |
|
|
837 |
0
|
sawsDebugLog.write(e.toString()); |
838 |
|
} |
839 |
|
} |
840 |
|
|
841 |
|
|
842 |
|
|
843 |
|
|
844 |
|
|
845 |
|
@param |
846 |
|
@param |
847 |
|
|
848 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
849 |
0
|
private void showMessage(String message, int type) {... |
850 |
0
|
Callback[] cbs = new Callback[1]; |
851 |
0
|
cbs[0] = new SAWSTextOutputCallback(type, message); |
852 |
0
|
try { |
853 |
0
|
this.callbackHandler.handle(cbs); |
854 |
|
} |
855 |
|
catch (Exception e) { |
856 |
0
|
System.err.println(e.getMessage()); |
857 |
0
|
sawsDebugLog.write(e); |
858 |
|
} |
859 |
|
} |
860 |
|
} |