blob: ee6af87627e433dd7103f4bad4b8035777d576ec [file] [log] [blame]
Meki Cherkaoui8f173612012-06-06 01:05:40 -07001
2
3
4
5function encodeToHexInterest(int){
6
7 var enc = new BinaryXMLEncoder();
8
9 int.encode(enc);
10
11 var hex = DataUtils.toHex(enc.getReducedOstream());
12
13 return hex;
14
15
16}
17
18
19function encodeToHexContentObject(co){
20 var enc = new BinaryXMLEncoder();
21
22 co.encode(enc);
23
24 var hex = DataUtils.toHex(enc.getReducedOstream());
25
26 return hex;
27
28
29}
30
31function encodeToBinaryContentObject(co){
32 var enc = new BinaryXMLEncoder();
33
34 co.encode(enc);
35
36 var hex = enc.getReducedOstream();
37
38 return hex;
39
40
41}
42
43function encodeForwardingEntry(co){
44 var enc = new BinaryXMLEncoder();
45
46 co.encode(enc);
47
48 var bytes = enc.getReducedOstream();
49
50 return bytes;
51
52
53}
54
55
56
57function decodeHexFaceInstance(result){
58
59 var numbers = DataUtils.toNumbers(result);
60
61
62 decoder = new BinaryXMLDecoder(numbers);
63
64 if(LOG>3)console.log('DECODING HEX FACE INSTANCE \n'+numbers);
65
66 i = new FaceInstance();
67
68 i.decode(decoder);
69
70 return i;
71
72}
73
74function decodeHexInterest(result){
75 var numbers = DataUtils.toNumbers(result);
76
77
78 decoder = new BinaryXMLDecoder(numbers);
79 if(LOG>3)console.log('DECODING HEX INTERST \n'+numbers);
80
81 i = new Interest();
82
83 i.decode(decoder);
84
85 return i;
86
87}
88
89
90
91function decodeHexContentObject(result){
92 var numbers = DataUtils.toNumbers(result);
93
94 decoder = new BinaryXMLDecoder(numbers);
95 if(LOG>3)console.log('DECODED HEX CONTENT OBJECT \n'+numbers);
96
97 co = new ContentObject();
98
99 co.decode(decoder);
100
101 return co;
102
103}
104
105
106
107function decodeHexForwardingEntry(result){
108 var numbers = DataUtils.toNumbers(result);
109
110 decoder = new BinaryXMLDecoder(numbers);
111
112 if(LOG>3)console.log('DECODED HEX FORWARDING ENTRY \n'+numbers);
113
114 co = new ForwardingEntry();
115
116 co.decode(decoder);
117
118 return co;
119
jeffcc8b3a92012-09-03 15:13:27 -0700120}
121
122/* Return a user friendly HTML string with the contents of co.
123 This also outputs to console.log.
124 */
125function contentObjectToHtml(/* ContentObject */ co) {
126 var output ="";
127
128 if(co==-1)
129 output+= "NO CONTENT FOUND"
130 else if (co==-2)
131 output+= "CONTENT NAME IS EMPTY"
132 else{
133 if(co.Name!=null && co.Name.Components!=null){
134 output+= "NAME: ";
135
136 for(var i=0;i<co.Name.Components.length;i++){
137 output+= "/"+ DataUtils.toString(co.Name.Components[i]);
138 }
139 output+= "<br />";
140 output+= "<br />";
141 }
142
143 if(co.Content !=null){
144 output += "CONTENT(ASCII): "+ DataUtils.toString(co.Content);
145
146 output+= "<br />";
147 output+= "<br />";
148 }
149 if(co.Content !=null){
150 output += "CONTENT(hex): "+ DataUtils.toHex(co.Content);
151
152 output+= "<br />";
153 output+= "<br />";
154 }
155 if(co.Signature !=null && co.Signature.Signature!=null){
156 output += "SIGNATURE(hex): "+ DataUtils.toHex(co.Signature.Signature);
157
158 output+= "<br />";
159 output+= "<br />";
160 }
161 if(co.SignedInfo !=null && co.SignedInfo.Publisher!=null && co.SignedInfo.Publisher.PublisherPublicKeyDigest!=null){
162 output += "Publisher Public Key Digest(hex): "+ DataUtils.toHex(co.SignedInfo.Publisher.PublisherPublicKeyDigest);
163
164 output+= "<br />";
165 output+= "<br />";
166 }
167 if(co.SignedInfo !=null && co.SignedInfo.Timestamp!=null){
168 var d = new Date();
169 d.setTime( co.SignedInfo.Timestamp.msec );
170
171 var bytes = [217, 185, 12, 225, 217, 185, 12, 225];
172
173 output += "TimeStamp: "+d;
174 output+= "<br />";
175 output += "TimeStamp(number): "+ co.SignedInfo.Timestamp.msec;
176
177 output+= "<br />";
178 }
179 if(co.SignedInfo!=null && co.SignedInfo.Locator!=null && co.SignedInfo.Locator.Certificate!=null){
180 var tmp = DataUtils.toString(co.SignedInfo.Locator.Certificate);
181 var publickey = rstr2b64(tmp);
182 var publickeyHex = DataUtils.toHex(co.SignedInfo.Locator.Certificate).toLowerCase();
183 var publickeyString = DataUtils.toString(co.SignedInfo.Locator.Certificate);
184 var signature = DataUtils.toHex(co.Signature.Signature).toLowerCase();
185 var input = DataUtils.toString(co.rawSignatureData);
186
187 output += "DER Certificate: "+publickey ;
188
189 output+= "<br />";
190 output+= "<br />";
191
192 if(LOG>2) console.log(" ContentName + SignedInfo + Content = "+input);
193
194 if(LOG>2) console.log("HEX OF ContentName + SignedInfo + Content = ");
195 if(LOG>2) console.log(DataUtils.stringtoBase64(input));
196
197 if(LOG>2) console.log(" PublicKey = "+publickey );
198 if(LOG>2) console.log(" PublicKeyHex = "+publickeyHex );
199 if(LOG>2) console.log(" PublicKeyString = "+publickeyString );
200
201 if(LOG>2) console.log(" Signature is");
202 if(LOG>2) console.log( signature );
203 //if(LOG>2) console.log(" Signature NOW IS" );
204 //if(LOG>2) console.log(co.Signature.Signature);
205
206 var x509 = new X509();
207 x509.readCertPEM(publickey);
208
209 //x509.readCertPEMWithoutRSAInit(publickey);
210
211 var result = x509.subjectPublicKeyRSA.verifyByteArray(co.rawSignatureData, signature);
212 if(LOG>2) console.log('result is '+result);
213
214 var n = x509.subjectPublicKeyRSA.n;
215 var e = x509.subjectPublicKeyRSA.e;
216
217 if(LOG>2) console.log('PUBLIC KEY n after is ');
218 if(LOG>2) console.log(n);
219
220 if(LOG>2) console.log('EXPONENT e after is ');
221 if(LOG>2) console.log(e);
222
223 /*var rsakey = new RSAKey();
224
225 var kp = publickeyHex.slice(56,314);
226
227 output += "PUBLISHER KEY(hex): "+kp ;
228
229 output+= "<br />";
230 output+= "<br />";
231
232 console.log('kp is '+kp);
233
234 var exp = publickeyHex.slice(318,324);
235
236 console.log('kp size is '+kp.length );
237 output += "exponent: "+exp ;
238
239 output+= "<br />";
240 output+= "<br />";
241
242 console.log('exp is '+exp);
243
244 rsakey.setPublic(kp,exp);
245
246 var result = rsakey.verifyString(input, signature);*/
247
248 if(result)
249 output += 'SIGNATURE VALID';
250 else
251 output += 'SIGNATURE INVALID';
252
253 //output += "VALID: "+ toHex(co.SignedInfo.Locator.PublicKey);
254
255 output+= "<br />";
256 output+= "<br />";
257
258 //if(LOG>4) console.log('str'[1]);
259 }
260 if(co.SignedInfo!=null && co.SignedInfo.Locator!=null && co.SignedInfo.Locator.PublicKey!=null){
261 var publickey = rstr2b64(DataUtils.toString(co.SignedInfo.Locator.PublicKey));
262 var publickeyHex = DataUtils.toHex(co.SignedInfo.Locator.PublicKey).toLowerCase();
263 var publickeyString = DataUtils.toString(co.SignedInfo.Locator.PublicKey);
264 var signature = DataUtils.toHex(co.Signature.Signature).toLowerCase();
265 var input = DataUtils.toString(co.rawSignatureData);
266
267 output += "DER Certificate: "+publickey ;
268
269 output+= "<br />";
270 output+= "<br />";
271
272 if(LOG>2) console.log(" ContentName + SignedInfo + Content = "+input);
273 if(LOG>2) console.log(" PublicKey = "+publickey );
274 if(LOG>2) console.log(" PublicKeyHex = "+publickeyHex );
275 if(LOG>2) console.log(" PublicKeyString = "+publickeyString );
276
277 if(LOG>2) console.log(" Signature "+signature );
278
279 if(LOG>2) console.log(" Signature NOW IS" );
280
281 if(LOG>2) console.log(co.Signature.Signature);
282
283 /*var x509 = new X509();
284
285 x509.readCertPEM(publickey);
286
287
288 //x509.readCertPEMWithoutRSAInit(publickey);
289
290 var result = x509.subjectPublicKeyRSA.verifyString(input, signature);*/
291 //console.log('result is '+result);
292
293 var kp = publickeyHex.slice(56,314);
294
295 output += "PUBLISHER KEY(hex): "+kp ;
296
297 output+= "<br />";
298 output+= "<br />";
299
300 console.log('PUBLIC KEY IN HEX is ');
301 console.log(kp);
302
303 var exp = publickeyHex.slice(318,324);
304
305 console.log('kp size is '+kp.length );
306 output += "exponent: "+exp ;
307
308 output+= "<br />";
309 output+= "<br />";
310
311 console.log('EXPONENT is ');
312 console.log(exp);
313
314 /*var c1 = hex_sha256(input);
315 var c2 = signature;
316
317 if(LOG>4)console.log('input is ');
318 if(LOG>4)console.log(input);
319 if(LOG>4)console.log('C1 is ');
320 if(LOG>4)console.log(c1);
321 if(LOG>4)console.log('C2 is ');
322 if(LOG>4)console.log(c2);
323 var result = c1 == c2;*/
324
325 var rsakey = new RSAKey();
326
327 rsakey.setPublic(kp,exp);
328
329 var result = rsakey.verifyByteArray(co.rawSignatureData,signature);
330 // var result = rsakey.verifyString(input, signature);
331
332 console.log('PUBLIC KEY n after is ');
333 console.log(rsakey.n);
334
335 console.log('EXPONENT e after is ');
336 console.log(rsakey.e);
337
338 if(result)
339 output += 'SIGNATURE VALID';
340 else
341 output += 'SIGNATURE INVALID';
342
343 //output += "VALID: "+ toHex(co.SignedInfo.Locator.PublicKey);
344
345 output+= "<br />";
346 output+= "<br />";
347
348 //if(LOG>4) console.log('str'[1]);
349 }
350 }
351
352 return output;
353}