blob: 4a7a03c2ddd84e4ed0312e27eecebb49121d83f4 [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
9 var enc = new BinaryXMLEncoder();
10
Jeff Thompson86aea882012-09-29 17:32:48 -070011 interest.to_ccnb(enc);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070012
13 var hex = DataUtils.toHex(enc.getReducedOstream());
14
15 return hex;
16
17
18}
19
20
21function encodeToHexContentObject(co){
22 var enc = new BinaryXMLEncoder();
23
Jeff Thompson86aea882012-09-29 17:32:48 -070024 co.to_ccnb(enc);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070025
26 var hex = DataUtils.toHex(enc.getReducedOstream());
27
28 return hex;
29
30
31}
32
33function encodeToBinaryContentObject(co){
34 var enc = new BinaryXMLEncoder();
35
Jeff Thompson86aea882012-09-29 17:32:48 -070036 co.to_ccnb(enc);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070037
38 var hex = enc.getReducedOstream();
39
40 return hex;
41
42
43}
44
45function encodeForwardingEntry(co){
46 var enc = new BinaryXMLEncoder();
47
Jeff Thompson86aea882012-09-29 17:32:48 -070048 co.to_ccnb(enc);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070049
50 var bytes = enc.getReducedOstream();
51
52 return bytes;
53
54
55}
56
57
58
59function decodeHexFaceInstance(result){
60
61 var numbers = DataUtils.toNumbers(result);
62
63
64 decoder = new BinaryXMLDecoder(numbers);
65
66 if(LOG>3)console.log('DECODING HEX FACE INSTANCE \n'+numbers);
67
Jeff Thompson86aea882012-09-29 17:32:48 -070068 var faceInstance = new FaceInstance();
Meki Cherkaoui8f173612012-06-06 01:05:40 -070069
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070070 faceInstance.from_ccnb(decoder);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070071
Jeff Thompson86aea882012-09-29 17:32:48 -070072 return faceInstance;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070073
74}
75
76function decodeHexInterest(result){
77 var numbers = DataUtils.toNumbers(result);
78
79
80 decoder = new BinaryXMLDecoder(numbers);
81 if(LOG>3)console.log('DECODING HEX INTERST \n'+numbers);
82
Jeff Thompson86aea882012-09-29 17:32:48 -070083 var interest = new Interest();
Meki Cherkaoui8f173612012-06-06 01:05:40 -070084
Jeff Thompson86aea882012-09-29 17:32:48 -070085 interest.from_ccnb(decoder);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070086
Jeff Thompson86aea882012-09-29 17:32:48 -070087 return interest;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070088
89}
90
91
92
93function decodeHexContentObject(result){
94 var numbers = DataUtils.toNumbers(result);
95
96 decoder = new BinaryXMLDecoder(numbers);
97 if(LOG>3)console.log('DECODED HEX CONTENT OBJECT \n'+numbers);
98
99 co = new ContentObject();
100
Jeff Thompson86aea882012-09-29 17:32:48 -0700101 co.from_ccnb(decoder);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700102
103 return co;
104
105}
106
107
108
109function decodeHexForwardingEntry(result){
110 var numbers = DataUtils.toNumbers(result);
111
112 decoder = new BinaryXMLDecoder(numbers);
113
114 if(LOG>3)console.log('DECODED HEX FORWARDING ENTRY \n'+numbers);
115
Jeff Thompson86aea882012-09-29 17:32:48 -0700116 forwardingEntry = new ForwardingEntry();
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700117
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700118 forwardingEntry.from_ccnb(decoder);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700119
Jeff Thompson86aea882012-09-29 17:32:48 -0700120 return forwardingEntry;
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700121
jeffcc8b3a92012-09-03 15:13:27 -0700122}
123
124/* Return a user friendly HTML string with the contents of co.
125 This also outputs to console.log.
126 */
127function contentObjectToHtml(/* ContentObject */ co) {
128 var output ="";
129
130 if(co==-1)
131 output+= "NO CONTENT FOUND"
132 else if (co==-2)
133 output+= "CONTENT NAME IS EMPTY"
134 else{
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700135 if(co.name!=null && co.name.components!=null){
jeffcc8b3a92012-09-03 15:13:27 -0700136 output+= "NAME: ";
137
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700138 for(var i=0;i<co.name.components.length;i++){
139 output+= "/"+ DataUtils.toString(co.name.components[i]);
jeffcc8b3a92012-09-03 15:13:27 -0700140 }
141 output+= "<br />";
142 output+= "<br />";
143 }
144
Jeff Thompson86aea882012-09-29 17:32:48 -0700145 if(co.content !=null){
146 output += "CONTENT(ASCII): "+ DataUtils.toString(co.content);
jeffcc8b3a92012-09-03 15:13:27 -0700147
148 output+= "<br />";
149 output+= "<br />";
150 }
Jeff Thompson86aea882012-09-29 17:32:48 -0700151 if(co.content !=null){
152 output += "CONTENT(hex): "+ DataUtils.toHex(co.content);
jeffcc8b3a92012-09-03 15:13:27 -0700153
154 output+= "<br />";
155 output+= "<br />";
156 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700157 if(co.signature !=null && co.signature.signature!=null){
158 output += "SIGNATURE(hex): "+ DataUtils.toHex(co.signature.signature);
jeffcc8b3a92012-09-03 15:13:27 -0700159
160 output+= "<br />";
161 output+= "<br />";
162 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700163 if(co.signedInfo !=null && co.signedInfo.publisher!=null && co.signedInfo.publisher.publisherPublicKeyDigest!=null){
164 output += "Publisher Public Key Digest(hex): "+ DataUtils.toHex(co.signedInfo.publisher.publisherPublicKeyDigest);
jeffcc8b3a92012-09-03 15:13:27 -0700165
166 output+= "<br />";
167 output+= "<br />";
168 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700169 if(co.signedInfo !=null && co.signedInfo.timestamp!=null){
jeffcc8b3a92012-09-03 15:13:27 -0700170 var d = new Date();
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700171 d.setTime( co.signedInfo.timestamp.msec );
jeffcc8b3a92012-09-03 15:13:27 -0700172
173 var bytes = [217, 185, 12, 225, 217, 185, 12, 225];
174
175 output += "TimeStamp: "+d;
176 output+= "<br />";
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700177 output += "TimeStamp(number): "+ co.signedInfo.timestamp.msec;
jeffcc8b3a92012-09-03 15:13:27 -0700178
179 output+= "<br />";
180 }
Jeff Thompson64447882012-10-14 18:11:33 -0700181 if(co.signedInfo !=null && co.signedInfo.finalBlockID!=null){
182 output += "FinalBlockID: "+ DataUtils.toHex(co.signedInfo.finalBlockID);
183 output+= "<br />";
184 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700185 if(co.signedInfo!=null && co.signedInfo.locator!=null && co.signedInfo.locator.certificate!=null){
186 var tmp = DataUtils.toString(co.signedInfo.locator.certificate);
jeffcc8b3a92012-09-03 15:13:27 -0700187 var publickey = rstr2b64(tmp);
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700188 var publickeyHex = DataUtils.toHex(co.signedInfo.locator.certificate).toLowerCase();
189 var publickeyString = DataUtils.toString(co.signedInfo.locator.certificate);
190 var signature = DataUtils.toHex(co.signature.signature).toLowerCase();
jeffcc8b3a92012-09-03 15:13:27 -0700191 var input = DataUtils.toString(co.rawSignatureData);
192
193 output += "DER Certificate: "+publickey ;
194
195 output+= "<br />";
196 output+= "<br />";
197
198 if(LOG>2) console.log(" ContentName + SignedInfo + Content = "+input);
199
200 if(LOG>2) console.log("HEX OF ContentName + SignedInfo + Content = ");
201 if(LOG>2) console.log(DataUtils.stringtoBase64(input));
202
203 if(LOG>2) console.log(" PublicKey = "+publickey );
204 if(LOG>2) console.log(" PublicKeyHex = "+publickeyHex );
205 if(LOG>2) console.log(" PublicKeyString = "+publickeyString );
206
207 if(LOG>2) console.log(" Signature is");
208 if(LOG>2) console.log( signature );
209 //if(LOG>2) console.log(" Signature NOW IS" );
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700210 //if(LOG>2) console.log(co.signature.signature);
jeffcc8b3a92012-09-03 15:13:27 -0700211
212 var x509 = new X509();
213 x509.readCertPEM(publickey);
214
215 //x509.readCertPEMWithoutRSAInit(publickey);
216
217 var result = x509.subjectPublicKeyRSA.verifyByteArray(co.rawSignatureData, signature);
218 if(LOG>2) console.log('result is '+result);
219
220 var n = x509.subjectPublicKeyRSA.n;
221 var e = x509.subjectPublicKeyRSA.e;
222
223 if(LOG>2) console.log('PUBLIC KEY n after is ');
224 if(LOG>2) console.log(n);
225
226 if(LOG>2) console.log('EXPONENT e after is ');
227 if(LOG>2) console.log(e);
228
229 /*var rsakey = new RSAKey();
230
231 var kp = publickeyHex.slice(56,314);
232
233 output += "PUBLISHER KEY(hex): "+kp ;
234
235 output+= "<br />";
236 output+= "<br />";
237
238 console.log('kp is '+kp);
239
240 var exp = publickeyHex.slice(318,324);
241
242 console.log('kp size is '+kp.length );
243 output += "exponent: "+exp ;
244
245 output+= "<br />";
246 output+= "<br />";
247
248 console.log('exp is '+exp);
249
250 rsakey.setPublic(kp,exp);
251
252 var result = rsakey.verifyString(input, signature);*/
253
254 if(result)
255 output += 'SIGNATURE VALID';
256 else
257 output += 'SIGNATURE INVALID';
258
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700259 //output += "VALID: "+ toHex(co.signedInfo.locator.publicKey);
jeffcc8b3a92012-09-03 15:13:27 -0700260
261 output+= "<br />";
262 output+= "<br />";
263
264 //if(LOG>4) console.log('str'[1]);
265 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700266 if(co.signedInfo!=null && co.signedInfo.locator!=null && co.signedInfo.locator.publicKey!=null){
267 var publickey = rstr2b64(DataUtils.toString(co.signedInfo.locator.publicKey));
268 var publickeyHex = DataUtils.toHex(co.signedInfo.locator.publicKey).toLowerCase();
269 var publickeyString = DataUtils.toString(co.signedInfo.locator.publicKey);
270 var signature = DataUtils.toHex(co.signature.signature).toLowerCase();
jeffcc8b3a92012-09-03 15:13:27 -0700271 var input = DataUtils.toString(co.rawSignatureData);
272
273 output += "DER Certificate: "+publickey ;
274
275 output+= "<br />";
276 output+= "<br />";
277
278 if(LOG>2) console.log(" ContentName + SignedInfo + Content = "+input);
279 if(LOG>2) console.log(" PublicKey = "+publickey );
280 if(LOG>2) console.log(" PublicKeyHex = "+publickeyHex );
281 if(LOG>2) console.log(" PublicKeyString = "+publickeyString );
282
283 if(LOG>2) console.log(" Signature "+signature );
284
285 if(LOG>2) console.log(" Signature NOW IS" );
286
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700287 if(LOG>2) console.log(co.signature.signature);
jeffcc8b3a92012-09-03 15:13:27 -0700288
289 /*var x509 = new X509();
290
291 x509.readCertPEM(publickey);
292
293
294 //x509.readCertPEMWithoutRSAInit(publickey);
295
296 var result = x509.subjectPublicKeyRSA.verifyString(input, signature);*/
297 //console.log('result is '+result);
298
299 var kp = publickeyHex.slice(56,314);
300
301 output += "PUBLISHER KEY(hex): "+kp ;
302
303 output+= "<br />";
304 output+= "<br />";
305
306 console.log('PUBLIC KEY IN HEX is ');
307 console.log(kp);
308
309 var exp = publickeyHex.slice(318,324);
310
311 console.log('kp size is '+kp.length );
312 output += "exponent: "+exp ;
313
314 output+= "<br />";
315 output+= "<br />";
316
317 console.log('EXPONENT is ');
318 console.log(exp);
319
320 /*var c1 = hex_sha256(input);
321 var c2 = signature;
322
323 if(LOG>4)console.log('input is ');
324 if(LOG>4)console.log(input);
325 if(LOG>4)console.log('C1 is ');
326 if(LOG>4)console.log(c1);
327 if(LOG>4)console.log('C2 is ');
328 if(LOG>4)console.log(c2);
329 var result = c1 == c2;*/
330
331 var rsakey = new RSAKey();
332
333 rsakey.setPublic(kp,exp);
334
335 var result = rsakey.verifyByteArray(co.rawSignatureData,signature);
336 // var result = rsakey.verifyString(input, signature);
337
338 console.log('PUBLIC KEY n after is ');
339 console.log(rsakey.n);
340
341 console.log('EXPONENT e after is ');
342 console.log(rsakey.e);
343
344 if(result)
345 output += 'SIGNATURE VALID';
346 else
347 output += 'SIGNATURE INVALID';
348
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700349 //output += "VALID: "+ toHex(co.signedInfo.locator.publicKey);
jeffcc8b3a92012-09-03 15:13:27 -0700350
351 output+= "<br />";
352 output+= "<br />";
353
354 //if(LOG>4) console.log('str'[1]);
355 }
356 }
357
358 return output;
359}