Clover Coverage Report
Coverage timestamp: Sun Mar 23 2008 08:24:39 GMT
104   299   19   6.93
22   185   0.32   15
15     2.2  
1    
 
 
  TCBContentRW       Line # 69 104 19 58.2% 0.58156025
 
No Tests
 
1    /*
2    * Copyright (c) 2006, University of Kent
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * Redistributions of source code must retain the above copyright notice, this
9    * list of conditions and the following disclaimer.
10    *
11    * Redistributions in binary form must reproduce the above copyright notice,
12    * this list of conditions and the following disclaimer in the documentation
13    * and/or other materials provided with the distribution.
14    *
15    * 1. Neither the name of the University of Kent nor the names of its
16    * contributors may be used to endorse or promote products derived from this
17    * software without specific prior written permission.
18    *
19    * 2. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20    * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21    * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22    * PURPOSE ARE DISCLAIMED.
23    *
24    * 3. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31    * POSSIBILITY OF SUCH DAMAGE.
32    *
33    * 4. YOU AGREE THAT THE EXCLUSIONS IN PARAGRAPHS 2 AND 3 ABOVE ARE REASONABLE
34    * IN THE CIRCUMSTANCES. IN PARTICULAR, YOU ACKNOWLEDGE (1) THAT THIS
35    * SOFTWARE HAS BEEN MADE AVAILABLE TO YOU FREE OF CHARGE, (2) THAT THIS
36    * SOFTWARE IS NOT "PRODUCT" QUALITY, BUT HAS BEEN PRODUCED BY A RESEARCH
37    * GROUP WHO DESIRE TO MAKE THIS SOFTWARE FREELY AVAILABLE TO PEOPLE WHO WISH
38    * TO USE IT, AND (3) THAT BECAUSE THIS SOFTWARE IS NOT OF "PRODUCT" QUALITY
39    * IT IS INEVITABLE THAT THERE WILL BE BUGS AND ERRORS, AND POSSIBLY MORE
40    * SERIOUS FAULTS, IN THIS SOFTWARE.
41    *
42    * 5. This license is governed, except to the extent that local laws
43    * necessarily apply, by the laws of England and Wales.
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 W.Xu
68    */
 
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    //adding log4j logging
81   
82    /**
83    * @aggregation composite
84    */
85    private static SAWSLogWriter sawsDebugLog = new SAWSLogWriter(TCBContentRW.class.getName());
86   
87   
 
88  7 toggle 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    * Method that sets the callback handler for the class. If the handler
99    * is null, the class will keep using the default callback handler.
100    *
101    * @param ch The callback handler.
102    */
 
103  7 toggle public void setCallbackHandler(CallbackHandler ch) {
104  7 if (ch != null) {
105  7 this.callbackHandler = ch;
106    }
107    }
108   
 
109  0 toggle 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   
 
117  108 toggle 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    //System.err.println("Using secretKey to write TCB failed");
141  0 if (debugLevel >= SAWSConstant.ErrorInfo)
142    //adding log4j logging
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    //e2.printStackTrace(System.err);
150    //adding log4j logging
151  0 sawsDebugLog.write(e2);
152  0 result = -1;
153    }
154  108 return result;
155    }
156   
157    /**
158    * This method is for reading the TCBLocation
159    */
 
160  3 toggle 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    //System.err.println("Using secretKey to read TCB failed");
180  0 if (debugLevel >= SAWSConstant.ErrorInfo)
181    //e.printStackTrace(System.err);
182    //adding log4j logging
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    //e.printStackTrace(System.err);
190    //adding log4j logging
191  0 sawsDebugLog.write(e);
192  0 result = -1;
193    }
194  3 if (result != -1)
195    {
196  3 result = extractASN1Block(block);
197    }
198    } // fileTemp exists.
199  3 return result;
200    }
201   
202   
 
203  0 toggle public void setLastFilename(String Filename){
204  0 lastFilename = Filename;
205    }
206   
 
207  0 toggle public void setLastSN ( int SN){
208  0 lastSN = SN;
209    }
210   
 
211  0 toggle public void setLastAccHash(byte [] hash){
212  0 lastAccHash = hash;
213    }
214   
 
215  108 toggle public void setTCBContent(String Filename, int SN, byte[] hash ) {
216  108 lastFilename = Filename;
217  108 lastSN = SN;
218  108 lastAccHash = hash;
219    }
220   
 
221  3 toggle public String getLastFilename(){
222  3 return lastFilename;
223    }
224   
 
225  3 toggle public int getLastSN(){
226  3 return lastSN;
227    }
228   
 
229  3 toggle public byte[] getLastAccHash(){
230  3 return lastAccHash;
231    }
232   
 
233  108 toggle 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    //System.err.println("ASN1 doesn't work! Generating ASN1 block failed!");
244  0 if (debugLevel >= SAWSConstant.ErrorInfo)
245    //e.printStackTrace(System.err);
246    //adding log4j logging
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   
 
254  3 toggle 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    //System.err.println("ASN1 doesn't work! Reading ASN1 block failed!");
270  0 if (debugLevel >= SAWSConstant.ErrorInfo)
271    //e.printStackTrace(System.err);
272    //adding log4j logging
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    * Method to create the callback (SAWSTextOutputCallback) with the message to be
281    * presented to the user and send it to the callback handler.
282    *
283    * @param message The message to be presented.
284    * @param type The type of the message (SAWSTextOutputCallback.WARNING,
285    * SAWSTextOutputCallback.ERROR, SAWSTextOutputCallback.INFORMATION)
286    */
 
287  0 toggle 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    }