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 iaik.asn1.*; |
49 |
|
import iaik.asn1.structures.AlgorithmID; |
50 |
|
|
51 |
|
import issrg.SAWS.callback.SAWSGUICallbackHandler; |
52 |
|
import issrg.SAWS.callback.SAWSTextOutputCallback; |
53 |
|
import issrg.SAWS.util.SAWSLogWriter; |
54 |
|
|
55 |
|
import java.security.*; |
56 |
|
import javax.crypto.*; |
57 |
|
import java.security.AlgorithmParameters; |
58 |
|
import javax.crypto.spec.* ; |
59 |
|
import java.io.*; |
60 |
|
import java.io.File; |
61 |
|
|
62 |
|
import javax.security.auth.callback.Callback; |
63 |
|
import javax.security.auth.callback.CallbackHandler; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
@author |
68 |
|
|
|
|
| 58.2% |
Uncovered Elements: 59 (141) |
Complexity: 19 |
Complexity Density: 0.32 |
|
69 |
|
public class TCBContentRW { |
70 |
|
|
71 |
|
private String trustedLocationLocal; |
72 |
|
private SecretKey secretKey; |
73 |
|
private PBEParameterSpec paramSpec = null; |
74 |
|
private String lastFilename; |
75 |
|
private int lastSN; |
76 |
|
private byte[] lastAccHash; |
77 |
|
private int debugLevel = 0; |
78 |
|
|
79 |
|
private CallbackHandler callbackHandler = new SAWSGUICallbackHandler(); |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
private static SAWSLogWriter sawsDebugLog = new SAWSLogWriter(TCBContentRW.class.getName()); |
86 |
|
|
87 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
88 |
7
|
public TCBContentRW( String trustedLocationL, SecretKey secretKeyL, PBEParameterSpec param,... |
89 |
|
int debugLevel, CallbackHandler ch){ |
90 |
7
|
trustedLocationLocal = trustedLocationL; |
91 |
7
|
secretKey = secretKeyL; |
92 |
7
|
paramSpec = param; |
93 |
7
|
this.debugLevel = debugLevel; |
94 |
7
|
this.setCallbackHandler(ch); |
95 |
|
} |
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
@param |
102 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
103 |
7
|
public void setCallbackHandler(CallbackHandler ch) {... |
104 |
7
|
if (ch != null) { |
105 |
7
|
this.callbackHandler = ch; |
106 |
|
} |
107 |
|
} |
108 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
109 |
0
|
public TCBContentRW(String lastFilename, int SN, byte[] hash, int debugLevel, CallbackHandler ch) {... |
110 |
0
|
lastFilename = lastFilename; |
111 |
0
|
lastSN = SN; |
112 |
0
|
lastAccHash = hash; |
113 |
0
|
this.debugLevel = debugLevel; |
114 |
0
|
this.setCallbackHandler(ch); |
115 |
|
} |
116 |
|
|
|
|
| 60.6% |
Uncovered Elements: 13 (33) |
Complexity: 7 |
Complexity Density: 0.28 |
|
117 |
108
|
public int write(){... |
118 |
108
|
File fileTemp = new File(trustedLocationLocal); |
119 |
108
|
byte [] fileBytes = null; |
120 |
108
|
int result = 0; |
121 |
108
|
byte [] asn1Block = generateASN1Block(); |
122 |
108
|
if (asn1Block == null) { |
123 |
0
|
return -1; |
124 |
|
} |
125 |
|
|
126 |
108
|
try{ |
127 |
108
|
if ( !fileTemp.exists() ) { |
128 |
1
|
fileTemp.createNewFile(); |
129 |
|
} |
130 |
108
|
RandomAccessFile raf = new RandomAccessFile(fileTemp, "rw"); |
131 |
108
|
raf.setLength(0); |
132 |
108
|
try{ |
133 |
108
|
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES"); |
134 |
108
|
cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec); |
135 |
108
|
fileBytes = cipher.doFinal(asn1Block); |
136 |
108
|
raf.write(fileBytes); |
137 |
|
} catch (Exception e) { |
138 |
0
|
this.showMessage("Using secretKey to write TCB failed", |
139 |
|
SAWSTextOutputCallback.ERROR); |
140 |
|
|
141 |
0
|
if (debugLevel >= SAWSConstant.ErrorInfo) |
142 |
|
|
143 |
0
|
sawsDebugLog.write(e + "\nUsing secretKey to write TCB failed"); |
144 |
0
|
result = -1; |
145 |
|
} |
146 |
108
|
raf.close(); |
147 |
|
} catch (Exception e2) { |
148 |
0
|
if (debugLevel >= SAWSConstant.ErrorInfo) |
149 |
|
|
150 |
|
|
151 |
0
|
sawsDebugLog.write(e2); |
152 |
0
|
result = -1; |
153 |
|
} |
154 |
108
|
return result; |
155 |
|
} |
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
|
|
|
| 60.6% |
Uncovered Elements: 13 (33) |
Complexity: 7 |
Complexity Density: 0.28 |
|
160 |
3
|
public int read() {... |
161 |
3
|
File fileTemp = new File(trustedLocationLocal); |
162 |
3
|
int fileLength = (int) fileTemp.length(); |
163 |
3
|
byte [] fileBytes = new byte [fileLength]; |
164 |
3
|
byte [] decryptedBytes = null; |
165 |
3
|
byte[] block = null; |
166 |
3
|
int result = 0; |
167 |
|
|
168 |
3
|
if ( fileTemp.exists() ) { |
169 |
3
|
try{ |
170 |
3
|
RandomAccessFile raf = new RandomAccessFile(fileTemp, "rw"); |
171 |
3
|
int length = raf.read(fileBytes); |
172 |
3
|
try{ |
173 |
3
|
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES"); |
174 |
3
|
cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec); |
175 |
3
|
block = cipher.doFinal(fileBytes); |
176 |
|
} catch (Exception e) { |
177 |
0
|
this.showMessage("Using secretKey to read TCB failed", |
178 |
|
SAWSTextOutputCallback.ERROR); |
179 |
|
|
180 |
0
|
if (debugLevel >= SAWSConstant.ErrorInfo) |
181 |
|
|
182 |
|
|
183 |
0
|
sawsDebugLog.write(e + "\nUsing secretKey to read TCB failed"); |
184 |
0
|
result = -1; |
185 |
|
} |
186 |
3
|
raf.close(); |
187 |
|
} catch (Exception e) { |
188 |
0
|
if (debugLevel >= SAWSConstant.ErrorInfo) |
189 |
|
|
190 |
|
|
191 |
0
|
sawsDebugLog.write(e); |
192 |
0
|
result = -1; |
193 |
|
} |
194 |
3
|
if (result != -1) |
195 |
|
{ |
196 |
3
|
result = extractASN1Block(block); |
197 |
|
} |
198 |
|
} |
199 |
3
|
return result; |
200 |
|
} |
201 |
|
|
202 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
203 |
0
|
public void setLastFilename(String Filename){ ... |
204 |
0
|
lastFilename = Filename; |
205 |
|
} |
206 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
207 |
0
|
public void setLastSN ( int SN){... |
208 |
0
|
lastSN = SN; |
209 |
|
} |
210 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
211 |
0
|
public void setLastAccHash(byte [] hash){... |
212 |
0
|
lastAccHash = hash; |
213 |
|
} |
214 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
215 |
108
|
public void setTCBContent(String Filename, int SN, byte[] hash ) {... |
216 |
108
|
lastFilename = Filename; |
217 |
108
|
lastSN = SN; |
218 |
108
|
lastAccHash = hash; |
219 |
|
} |
220 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
221 |
3
|
public String getLastFilename(){... |
222 |
3
|
return lastFilename; |
223 |
|
} |
224 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
225 |
3
|
public int getLastSN(){... |
226 |
3
|
return lastSN; |
227 |
|
} |
228 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
229 |
3
|
public byte[] getLastAccHash(){... |
230 |
3
|
return lastAccHash; |
231 |
|
} |
232 |
|
|
|
|
| 57.1% |
Uncovered Elements: 6 (14) |
Complexity: 3 |
Complexity Density: 0.25 |
|
233 |
108
|
public byte[] generateASN1Block(){... |
234 |
108
|
byte[] arrayASN = null; |
235 |
108
|
SEQUENCE ASN1Seq = new SEQUENCE(); |
236 |
108
|
try { |
237 |
108
|
ASN1Seq.addComponent(new IA5String(lastFilename)); |
238 |
108
|
ASN1Seq.addComponent(new INTEGER(lastSN)); |
239 |
108
|
ASN1Seq.addComponent(new OCTET_STRING(lastAccHash)); |
240 |
|
} catch (Exception e) { |
241 |
0
|
this.showMessage("ASN1 doesn't work! Generating ASN1 block failed!", |
242 |
|
SAWSTextOutputCallback.ERROR); |
243 |
|
|
244 |
0
|
if (debugLevel >= SAWSConstant.ErrorInfo) |
245 |
|
|
246 |
|
|
247 |
0
|
sawsDebugLog.write(e + "\nASN1 doesn't work! Generating ASN1 block failed!"); |
248 |
0
|
return null; |
249 |
|
} |
250 |
108
|
arrayASN = DerCoder.encode(ASN1Seq); |
251 |
108
|
return arrayASN; |
252 |
|
} |
253 |
|
|
|
|
| 62.5% |
Uncovered Elements: 6 (16) |
Complexity: 3 |
Complexity Density: 0.21 |
|
254 |
3
|
public int extractASN1Block (byte[] asn1Block){... |
255 |
3
|
try{ |
256 |
3
|
ASN1 asn1 = new ASN1(asn1Block); |
257 |
3
|
IA5String s0 = (IA5String)asn1.getComponentAt(0); |
258 |
3
|
lastFilename = (String)s0.getValue(); |
259 |
|
|
260 |
3
|
INTEGER i1 = (INTEGER)asn1.getComponentAt(1); |
261 |
3
|
java.math.BigInteger b1 = (java.math.BigInteger)i1.getValue(); |
262 |
3
|
lastSN = b1.intValue(); |
263 |
|
|
264 |
3
|
OCTET_STRING b = (OCTET_STRING)asn1.getComponentAt(2); |
265 |
3
|
lastAccHash = (byte[]) b.getValue(); |
266 |
|
} catch (Exception e) { |
267 |
0
|
this.showMessage("ASN1 doesn't work! Reading ASN1 block failed!", |
268 |
|
SAWSTextOutputCallback.ERROR); |
269 |
|
|
270 |
0
|
if (debugLevel >= SAWSConstant.ErrorInfo) |
271 |
|
|
272 |
|
|
273 |
0
|
sawsDebugLog.write(e + "\nASN1 doesn't work! Reading ASN1 block failed!"); |
274 |
0
|
return -1; |
275 |
|
} |
276 |
3
|
return 0; |
277 |
|
} |
278 |
|
|
279 |
|
|
280 |
|
|
281 |
|
|
282 |
|
|
283 |
|
@param |
284 |
|
@param |
285 |
|
|
286 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
|
287 |
0
|
private void showMessage(String message, int type) {... |
288 |
0
|
Callback[] cbs; |
289 |
0
|
cbs = new Callback[1]; |
290 |
0
|
cbs[0] = new SAWSTextOutputCallback(type, message); |
291 |
0
|
try { |
292 |
0
|
this.callbackHandler.handle(cbs); |
293 |
|
} |
294 |
|
catch (Exception e) { |
295 |
0
|
System.err.println(e.getMessage()); |
296 |
0
|
sawsDebugLog.write(e); |
297 |
|
} |
298 |
|
} |
299 |
|
} |