blob: 3a784cde3903a44780dfb74d544c238e0b828472 [file] [log] [blame]
Jeff Thompson745026e2012-10-13 12:49:20 -07001/*
2 * This file contains utilities to help encode and decode NDN objects.
3 * author: ucla-cs
4 * See COPYING for copyright and distribution information.
5 */
Meki Cherkaoui8f173612012-06-06 01:05:40 -07006
Jeff Thompson86aea882012-09-29 17:32:48 -07007function encodeToHexInterest(interest){
Meki Cherkaoui8f173612012-06-06 01:05:40 -07008 var enc = new BinaryXMLEncoder();
9
Jeff Thompson86aea882012-09-29 17:32:48 -070010 interest.to_ccnb(enc);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070011
12 var hex = DataUtils.toHex(enc.getReducedOstream());
13
14 return hex;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070015
16}
17
18
Jeff Thompson344891b2012-11-11 18:41:09 -080019function encodeToBinaryInterest(interest){
20 var enc = new BinaryXMLEncoder();
21
22 interest.to_ccnb(enc);
23
24 var hex = enc.getReducedOstream();
25
26 return hex;
27}
28
29
Meki Cherkaoui8f173612012-06-06 01:05:40 -070030function encodeToHexContentObject(co){
31 var enc = new BinaryXMLEncoder();
32
Jeff Thompson86aea882012-09-29 17:32:48 -070033 co.to_ccnb(enc);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070034
35 var hex = DataUtils.toHex(enc.getReducedOstream());
Jeff Thompson344891b2012-11-11 18:41:09 -080036
Meki Cherkaoui8f173612012-06-06 01:05:40 -070037 return hex;
38
39
40}
41
42function encodeToBinaryContentObject(co){
43 var enc = new BinaryXMLEncoder();
44
Jeff Thompson86aea882012-09-29 17:32:48 -070045 co.to_ccnb(enc);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070046
47 var hex = enc.getReducedOstream();
48
49 return hex;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070050}
51
52function encodeForwardingEntry(co){
53 var enc = new BinaryXMLEncoder();
54
Jeff Thompson86aea882012-09-29 17:32:48 -070055 co.to_ccnb(enc);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070056
57 var bytes = enc.getReducedOstream();
58
59 return bytes;
60
61
62}
63
64
65
66function decodeHexFaceInstance(result){
67
68 var numbers = DataUtils.toNumbers(result);
69
70
71 decoder = new BinaryXMLDecoder(numbers);
72
73 if(LOG>3)console.log('DECODING HEX FACE INSTANCE \n'+numbers);
74
Jeff Thompson86aea882012-09-29 17:32:48 -070075 var faceInstance = new FaceInstance();
Meki Cherkaoui8f173612012-06-06 01:05:40 -070076
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070077 faceInstance.from_ccnb(decoder);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070078
Jeff Thompson86aea882012-09-29 17:32:48 -070079 return faceInstance;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070080
81}
82
Jeff Thompson344891b2012-11-11 18:41:09 -080083
84
Meki Cherkaoui8f173612012-06-06 01:05:40 -070085function decodeHexInterest(result){
Jeff Thompson344891b2012-11-11 18:41:09 -080086 var numbers = DataUtils.toNumbers(result);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070087
88 decoder = new BinaryXMLDecoder(numbers);
Jeff Thompson344891b2012-11-11 18:41:09 -080089
Meki Cherkaoui8f173612012-06-06 01:05:40 -070090 if(LOG>3)console.log('DECODING HEX INTERST \n'+numbers);
91
Jeff Thompson86aea882012-09-29 17:32:48 -070092 var interest = new Interest();
Meki Cherkaoui8f173612012-06-06 01:05:40 -070093
Jeff Thompson86aea882012-09-29 17:32:48 -070094 interest.from_ccnb(decoder);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070095
Jeff Thompson86aea882012-09-29 17:32:48 -070096 return interest;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070097
98}
99
100
101
102function decodeHexContentObject(result){
103 var numbers = DataUtils.toNumbers(result);
Jeff Thompson344891b2012-11-11 18:41:09 -0800104
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700105 decoder = new BinaryXMLDecoder(numbers);
Jeff Thompson344891b2012-11-11 18:41:09 -0800106
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700107 if(LOG>3)console.log('DECODED HEX CONTENT OBJECT \n'+numbers);
108
109 co = new ContentObject();
110
Jeff Thompson86aea882012-09-29 17:32:48 -0700111 co.from_ccnb(decoder);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700112
113 return co;
114
115}
116
117
118
119function decodeHexForwardingEntry(result){
120 var numbers = DataUtils.toNumbers(result);
121
122 decoder = new BinaryXMLDecoder(numbers);
123
124 if(LOG>3)console.log('DECODED HEX FORWARDING ENTRY \n'+numbers);
125
Jeff Thompson86aea882012-09-29 17:32:48 -0700126 forwardingEntry = new ForwardingEntry();
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700127
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700128 forwardingEntry.from_ccnb(decoder);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700129
Jeff Thompson86aea882012-09-29 17:32:48 -0700130 return forwardingEntry;
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700131
jeffcc8b3a92012-09-03 15:13:27 -0700132}
133
134/* Return a user friendly HTML string with the contents of co.
135 This also outputs to console.log.
136 */
137function contentObjectToHtml(/* ContentObject */ co) {
138 var output ="";
139
140 if(co==-1)
141 output+= "NO CONTENT FOUND"
142 else if (co==-2)
143 output+= "CONTENT NAME IS EMPTY"
144 else{
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700145 if(co.name!=null && co.name.components!=null){
jeffcc8b3a92012-09-03 15:13:27 -0700146 output+= "NAME: ";
147
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700148 for(var i=0;i<co.name.components.length;i++){
Jeff Thompson344891b2012-11-11 18:41:09 -0800149 output+= "/"+ escape(DataUtils.toString(co.name.components[i]));
jeffcc8b3a92012-09-03 15:13:27 -0700150 }
151 output+= "<br />";
152 output+= "<br />";
153 }
154
Jeff Thompson86aea882012-09-29 17:32:48 -0700155 if(co.content !=null){
156 output += "CONTENT(ASCII): "+ DataUtils.toString(co.content);
jeffcc8b3a92012-09-03 15:13:27 -0700157
158 output+= "<br />";
159 output+= "<br />";
160 }
Jeff Thompson86aea882012-09-29 17:32:48 -0700161 if(co.content !=null){
162 output += "CONTENT(hex): "+ DataUtils.toHex(co.content);
jeffcc8b3a92012-09-03 15:13:27 -0700163
164 output+= "<br />";
165 output+= "<br />";
166 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700167 if(co.signature !=null && co.signature.signature!=null){
168 output += "SIGNATURE(hex): "+ DataUtils.toHex(co.signature.signature);
jeffcc8b3a92012-09-03 15:13:27 -0700169
170 output+= "<br />";
171 output+= "<br />";
172 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700173 if(co.signedInfo !=null && co.signedInfo.publisher!=null && co.signedInfo.publisher.publisherPublicKeyDigest!=null){
174 output += "Publisher Public Key Digest(hex): "+ DataUtils.toHex(co.signedInfo.publisher.publisherPublicKeyDigest);
jeffcc8b3a92012-09-03 15:13:27 -0700175
176 output+= "<br />";
177 output+= "<br />";
178 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700179 if(co.signedInfo !=null && co.signedInfo.timestamp!=null){
jeffcc8b3a92012-09-03 15:13:27 -0700180 var d = new Date();
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700181 d.setTime( co.signedInfo.timestamp.msec );
jeffcc8b3a92012-09-03 15:13:27 -0700182
183 var bytes = [217, 185, 12, 225, 217, 185, 12, 225];
184
185 output += "TimeStamp: "+d;
186 output+= "<br />";
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700187 output += "TimeStamp(number): "+ co.signedInfo.timestamp.msec;
jeffcc8b3a92012-09-03 15:13:27 -0700188
189 output+= "<br />";
190 }
Jeff Thompson64447882012-10-14 18:11:33 -0700191 if(co.signedInfo !=null && co.signedInfo.finalBlockID!=null){
192 output += "FinalBlockID: "+ DataUtils.toHex(co.signedInfo.finalBlockID);
193 output+= "<br />";
194 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700195 if(co.signedInfo!=null && co.signedInfo.locator!=null && co.signedInfo.locator.certificate!=null){
196 var tmp = DataUtils.toString(co.signedInfo.locator.certificate);
jeffcc8b3a92012-09-03 15:13:27 -0700197 var publickey = rstr2b64(tmp);
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700198 var publickeyHex = DataUtils.toHex(co.signedInfo.locator.certificate).toLowerCase();
199 var publickeyString = DataUtils.toString(co.signedInfo.locator.certificate);
200 var signature = DataUtils.toHex(co.signature.signature).toLowerCase();
jeffcc8b3a92012-09-03 15:13:27 -0700201 var input = DataUtils.toString(co.rawSignatureData);
202
203 output += "DER Certificate: "+publickey ;
204
205 output+= "<br />";
206 output+= "<br />";
207
208 if(LOG>2) console.log(" ContentName + SignedInfo + Content = "+input);
209
210 if(LOG>2) console.log("HEX OF ContentName + SignedInfo + Content = ");
211 if(LOG>2) console.log(DataUtils.stringtoBase64(input));
212
213 if(LOG>2) console.log(" PublicKey = "+publickey );
214 if(LOG>2) console.log(" PublicKeyHex = "+publickeyHex );
215 if(LOG>2) console.log(" PublicKeyString = "+publickeyString );
216
217 if(LOG>2) console.log(" Signature is");
218 if(LOG>2) console.log( signature );
219 //if(LOG>2) console.log(" Signature NOW IS" );
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700220 //if(LOG>2) console.log(co.signature.signature);
jeffcc8b3a92012-09-03 15:13:27 -0700221
222 var x509 = new X509();
223 x509.readCertPEM(publickey);
224
225 //x509.readCertPEMWithoutRSAInit(publickey);
226
227 var result = x509.subjectPublicKeyRSA.verifyByteArray(co.rawSignatureData, signature);
228 if(LOG>2) console.log('result is '+result);
229
230 var n = x509.subjectPublicKeyRSA.n;
231 var e = x509.subjectPublicKeyRSA.e;
232
233 if(LOG>2) console.log('PUBLIC KEY n after is ');
234 if(LOG>2) console.log(n);
235
236 if(LOG>2) console.log('EXPONENT e after is ');
237 if(LOG>2) console.log(e);
238
239 /*var rsakey = new RSAKey();
240
241 var kp = publickeyHex.slice(56,314);
242
243 output += "PUBLISHER KEY(hex): "+kp ;
244
245 output+= "<br />";
246 output+= "<br />";
247
248 console.log('kp is '+kp);
249
250 var exp = publickeyHex.slice(318,324);
251
252 console.log('kp size is '+kp.length );
253 output += "exponent: "+exp ;
254
255 output+= "<br />";
256 output+= "<br />";
257
258 console.log('exp is '+exp);
259
260 rsakey.setPublic(kp,exp);
261
262 var result = rsakey.verifyString(input, signature);*/
263
264 if(result)
265 output += 'SIGNATURE VALID';
266 else
267 output += 'SIGNATURE INVALID';
268
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700269 //output += "VALID: "+ toHex(co.signedInfo.locator.publicKey);
jeffcc8b3a92012-09-03 15:13:27 -0700270
271 output+= "<br />";
272 output+= "<br />";
273
274 //if(LOG>4) console.log('str'[1]);
275 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700276 if(co.signedInfo!=null && co.signedInfo.locator!=null && co.signedInfo.locator.publicKey!=null){
277 var publickey = rstr2b64(DataUtils.toString(co.signedInfo.locator.publicKey));
278 var publickeyHex = DataUtils.toHex(co.signedInfo.locator.publicKey).toLowerCase();
279 var publickeyString = DataUtils.toString(co.signedInfo.locator.publicKey);
280 var signature = DataUtils.toHex(co.signature.signature).toLowerCase();
jeffcc8b3a92012-09-03 15:13:27 -0700281 var input = DataUtils.toString(co.rawSignatureData);
282
283 output += "DER Certificate: "+publickey ;
284
285 output+= "<br />";
286 output+= "<br />";
287
288 if(LOG>2) console.log(" ContentName + SignedInfo + Content = "+input);
289 if(LOG>2) console.log(" PublicKey = "+publickey );
290 if(LOG>2) console.log(" PublicKeyHex = "+publickeyHex );
291 if(LOG>2) console.log(" PublicKeyString = "+publickeyString );
292
293 if(LOG>2) console.log(" Signature "+signature );
294
295 if(LOG>2) console.log(" Signature NOW IS" );
296
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700297 if(LOG>2) console.log(co.signature.signature);
jeffcc8b3a92012-09-03 15:13:27 -0700298
299 /*var x509 = new X509();
300
301 x509.readCertPEM(publickey);
302
303
304 //x509.readCertPEMWithoutRSAInit(publickey);
305
306 var result = x509.subjectPublicKeyRSA.verifyString(input, signature);*/
307 //console.log('result is '+result);
308
309 var kp = publickeyHex.slice(56,314);
310
311 output += "PUBLISHER KEY(hex): "+kp ;
312
313 output+= "<br />";
314 output+= "<br />";
315
316 console.log('PUBLIC KEY IN HEX is ');
317 console.log(kp);
318
319 var exp = publickeyHex.slice(318,324);
320
321 console.log('kp size is '+kp.length );
322 output += "exponent: "+exp ;
323
324 output+= "<br />";
325 output+= "<br />";
326
327 console.log('EXPONENT is ');
328 console.log(exp);
329
330 /*var c1 = hex_sha256(input);
331 var c2 = signature;
332
333 if(LOG>4)console.log('input is ');
334 if(LOG>4)console.log(input);
335 if(LOG>4)console.log('C1 is ');
336 if(LOG>4)console.log(c1);
337 if(LOG>4)console.log('C2 is ');
338 if(LOG>4)console.log(c2);
339 var result = c1 == c2;*/
340
341 var rsakey = new RSAKey();
342
343 rsakey.setPublic(kp,exp);
344
345 var result = rsakey.verifyByteArray(co.rawSignatureData,signature);
346 // var result = rsakey.verifyString(input, signature);
347
348 console.log('PUBLIC KEY n after is ');
349 console.log(rsakey.n);
350
351 console.log('EXPONENT e after is ');
352 console.log(rsakey.e);
353
354 if(result)
355 output += 'SIGNATURE VALID';
356 else
357 output += 'SIGNATURE INVALID';
358
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700359 //output += "VALID: "+ toHex(co.signedInfo.locator.publicKey);
jeffcc8b3a92012-09-03 15:13:27 -0700360
361 output+= "<br />";
362 output+= "<br />";
363
364 //if(LOG>4) console.log('str'[1]);
365 }
366 }
367
368 return output;
369}