blob: 69d1aec88d73cbb5901cd3dd202987646d445aeb [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 Thompsone85ff1d2012-09-29 21:21:57 -0700181 if(co.signedInfo!=null && co.signedInfo.locator!=null && co.signedInfo.locator.certificate!=null){
182 var tmp = DataUtils.toString(co.signedInfo.locator.certificate);
jeffcc8b3a92012-09-03 15:13:27 -0700183 var publickey = rstr2b64(tmp);
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700184 var publickeyHex = DataUtils.toHex(co.signedInfo.locator.certificate).toLowerCase();
185 var publickeyString = DataUtils.toString(co.signedInfo.locator.certificate);
186 var signature = DataUtils.toHex(co.signature.signature).toLowerCase();
jeffcc8b3a92012-09-03 15:13:27 -0700187 var input = DataUtils.toString(co.rawSignatureData);
188
189 output += "DER Certificate: "+publickey ;
190
191 output+= "<br />";
192 output+= "<br />";
193
194 if(LOG>2) console.log(" ContentName + SignedInfo + Content = "+input);
195
196 if(LOG>2) console.log("HEX OF ContentName + SignedInfo + Content = ");
197 if(LOG>2) console.log(DataUtils.stringtoBase64(input));
198
199 if(LOG>2) console.log(" PublicKey = "+publickey );
200 if(LOG>2) console.log(" PublicKeyHex = "+publickeyHex );
201 if(LOG>2) console.log(" PublicKeyString = "+publickeyString );
202
203 if(LOG>2) console.log(" Signature is");
204 if(LOG>2) console.log( signature );
205 //if(LOG>2) console.log(" Signature NOW IS" );
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700206 //if(LOG>2) console.log(co.signature.signature);
jeffcc8b3a92012-09-03 15:13:27 -0700207
208 var x509 = new X509();
209 x509.readCertPEM(publickey);
210
211 //x509.readCertPEMWithoutRSAInit(publickey);
212
213 var result = x509.subjectPublicKeyRSA.verifyByteArray(co.rawSignatureData, signature);
214 if(LOG>2) console.log('result is '+result);
215
216 var n = x509.subjectPublicKeyRSA.n;
217 var e = x509.subjectPublicKeyRSA.e;
218
219 if(LOG>2) console.log('PUBLIC KEY n after is ');
220 if(LOG>2) console.log(n);
221
222 if(LOG>2) console.log('EXPONENT e after is ');
223 if(LOG>2) console.log(e);
224
225 /*var rsakey = new RSAKey();
226
227 var kp = publickeyHex.slice(56,314);
228
229 output += "PUBLISHER KEY(hex): "+kp ;
230
231 output+= "<br />";
232 output+= "<br />";
233
234 console.log('kp is '+kp);
235
236 var exp = publickeyHex.slice(318,324);
237
238 console.log('kp size is '+kp.length );
239 output += "exponent: "+exp ;
240
241 output+= "<br />";
242 output+= "<br />";
243
244 console.log('exp is '+exp);
245
246 rsakey.setPublic(kp,exp);
247
248 var result = rsakey.verifyString(input, signature);*/
249
250 if(result)
251 output += 'SIGNATURE VALID';
252 else
253 output += 'SIGNATURE INVALID';
254
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700255 //output += "VALID: "+ toHex(co.signedInfo.locator.publicKey);
jeffcc8b3a92012-09-03 15:13:27 -0700256
257 output+= "<br />";
258 output+= "<br />";
259
260 //if(LOG>4) console.log('str'[1]);
261 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700262 if(co.signedInfo!=null && co.signedInfo.locator!=null && co.signedInfo.locator.publicKey!=null){
263 var publickey = rstr2b64(DataUtils.toString(co.signedInfo.locator.publicKey));
264 var publickeyHex = DataUtils.toHex(co.signedInfo.locator.publicKey).toLowerCase();
265 var publickeyString = DataUtils.toString(co.signedInfo.locator.publicKey);
266 var signature = DataUtils.toHex(co.signature.signature).toLowerCase();
jeffcc8b3a92012-09-03 15:13:27 -0700267 var input = DataUtils.toString(co.rawSignatureData);
268
269 output += "DER Certificate: "+publickey ;
270
271 output+= "<br />";
272 output+= "<br />";
273
274 if(LOG>2) console.log(" ContentName + SignedInfo + Content = "+input);
275 if(LOG>2) console.log(" PublicKey = "+publickey );
276 if(LOG>2) console.log(" PublicKeyHex = "+publickeyHex );
277 if(LOG>2) console.log(" PublicKeyString = "+publickeyString );
278
279 if(LOG>2) console.log(" Signature "+signature );
280
281 if(LOG>2) console.log(" Signature NOW IS" );
282
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700283 if(LOG>2) console.log(co.signature.signature);
jeffcc8b3a92012-09-03 15:13:27 -0700284
285 /*var x509 = new X509();
286
287 x509.readCertPEM(publickey);
288
289
290 //x509.readCertPEMWithoutRSAInit(publickey);
291
292 var result = x509.subjectPublicKeyRSA.verifyString(input, signature);*/
293 //console.log('result is '+result);
294
295 var kp = publickeyHex.slice(56,314);
296
297 output += "PUBLISHER KEY(hex): "+kp ;
298
299 output+= "<br />";
300 output+= "<br />";
301
302 console.log('PUBLIC KEY IN HEX is ');
303 console.log(kp);
304
305 var exp = publickeyHex.slice(318,324);
306
307 console.log('kp size is '+kp.length );
308 output += "exponent: "+exp ;
309
310 output+= "<br />";
311 output+= "<br />";
312
313 console.log('EXPONENT is ');
314 console.log(exp);
315
316 /*var c1 = hex_sha256(input);
317 var c2 = signature;
318
319 if(LOG>4)console.log('input is ');
320 if(LOG>4)console.log(input);
321 if(LOG>4)console.log('C1 is ');
322 if(LOG>4)console.log(c1);
323 if(LOG>4)console.log('C2 is ');
324 if(LOG>4)console.log(c2);
325 var result = c1 == c2;*/
326
327 var rsakey = new RSAKey();
328
329 rsakey.setPublic(kp,exp);
330
331 var result = rsakey.verifyByteArray(co.rawSignatureData,signature);
332 // var result = rsakey.verifyString(input, signature);
333
334 console.log('PUBLIC KEY n after is ');
335 console.log(rsakey.n);
336
337 console.log('EXPONENT e after is ');
338 console.log(rsakey.e);
339
340 if(result)
341 output += 'SIGNATURE VALID';
342 else
343 output += 'SIGNATURE INVALID';
344
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700345 //output += "VALID: "+ toHex(co.signedInfo.locator.publicKey);
jeffcc8b3a92012-09-03 15:13:27 -0700346
347 output+= "<br />";
348 output+= "<br />";
349
350 //if(LOG>4) console.log('str'[1]);
351 }
352 }
353
354 return output;
355}