-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenTestScript.java
More file actions
135 lines (105 loc) · 3.59 KB
/
GenTestScript.java
File metadata and controls
135 lines (105 loc) · 3.59 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
package test_data;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SignatureException;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import signature.CertImport;
import stored_keys.GetSigExistingKeys;
import stored_keys.LoadKeystore;
import testing.CertToFromByte;
public class GenTestScript {
public static void main(String args[]){
String f_name = "hello.js",
language = "javascript",
type = "Valid",
dataFile = "/home/varun/Documents/projects/GSoC/T1_testData/files/"+f_name,
signFile = "TestScripts/"+type+"/"+language+"/"+f_name,
pathToKeyStore = "/home/varun/keystore",
keyStorePass = "",
alias = "Varun Raval",
privateKeyPass = "",
begin = "/********BEGIN SIGNATURE********",
end = "\n********END SIGNATURE********/\n";
Certificate certificate = null;
GetSigExistingKeys gsek = new GetSigExistingKeys();
KeyStore keyStore = LoadKeystore.load(pathToKeyStore, keyStorePass);
//perform signature and get Certificate in Certificate object from keystore
byte[] realsig = null;
try {
realsig = gsek.sigUsingExistKeys(keyStore, alias, privateKeyPass, dataFile, signFile);
CertImport certImport = new CertImport(keyStore);
certificate = certImport.importCert(alias);
} catch (UnrecoverableKeyException | InvalidKeyException | KeyStoreException | NoSuchAlgorithmException
| NoSuchProviderException | SignatureException e) {
e.printStackTrace();
return;
}
//convert signature to base64
String strSig = CertToFromByte.convertBytesToBase64(realsig);
//get certificate in bytes format
byte[] certByte;
try {
certByte = certificate.getEncoded();
} catch (CertificateEncodingException e1) {
e1.printStackTrace();
return;
}
//convert cert bytes to base64
String strCert = CertToFromByte.convertBytesToBase64(certByte);
//Appending Signature and Certificate
try(BufferedWriter brW = new BufferedWriter(new FileWriter(signFile))){
BufferedReader br = new BufferedReader(new FileReader(dataFile));
int c;
while((c=br.read())!=-1){
brW.append((char)c);
System.out.print(c+" ");
}
br.close();
/* By default, last line in every file is ended by \n which is not visible directly.
* But if generated programmatically, it may not.
* To be sure, two \n characters are added.
* Fist to add \n if not already there and second to bring pointer to next line.
* This gives at most two empty lines and at least one empty line.
*/
brW.append("\n\n");
String ext = (String)signFile.subSequence(signFile.lastIndexOf(".")+1, signFile.length());
System.out.println(ext);
//Each line is terminated by '\n'
switch(ext){
case "js":
break;
case "py":
begin = "\"\"\"*****BEGIN SIGNSTURE********\n";
end = "\n********END SIGNSTURE*****\"\"\"\n";
break;
}
brW.append(begin);
brW.append("\n");
brW.append(strSig);
brW.append("\n\n");
int i = 0;
for(String s: strCert.split("")){
brW.append(s);
i++;
if(i%48 == 0)
brW.append('\n');
}
brW.append(end);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}