blob: 3f462043dd74541413707a1c830d2c3d13a99543 [file] [log] [blame]
Jeff Thompson745026e2012-10-13 12:49:20 -07001/*
2 * This file contains utilities to help encode and decode NDN objects.
Jeff Thompson146d7de2012-11-17 16:15:28 -08003 * author: Meki Cheraoui
Jeff Thompson745026e2012-10-13 12:49:20 -07004 * 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){
Jeff Thompsona5a2a2b2012-11-15 00:09:17 -08008 return DataUtils.toHex(encodeToBinaryInterest(interest));
Meki Cherkaoui8f173612012-06-06 01:05:40 -07009}
10
11
Jeff Thompsona5a2a2b2012-11-15 00:09:17 -080012function encodeToBinaryInterest(interest) {
Jeff Thompson344891b2012-11-11 18:41:09 -080013 var enc = new BinaryXMLEncoder();
Jeff Thompson344891b2012-11-11 18:41:09 -080014 interest.to_ccnb(enc);
15
Jeff Thompsona5a2a2b2012-11-15 00:09:17 -080016 return enc.getReducedOstream();
Jeff Thompson344891b2012-11-11 18:41:09 -080017}
18
19
Meki Cherkaoui8f173612012-06-06 01:05:40 -070020function encodeToHexContentObject(co){
Jeff Thompsona5a2a2b2012-11-15 00:09:17 -080021 return DataUtils.toHex(encodeToBinaryContentObject(co));
Meki Cherkaoui8f173612012-06-06 01:05:40 -070022}
23
24function encodeToBinaryContentObject(co){
25 var enc = new BinaryXMLEncoder();
Jeff Thompson86aea882012-09-29 17:32:48 -070026 co.to_ccnb(enc);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070027
Jeff Thompsona5a2a2b2012-11-15 00:09:17 -080028 return enc.getReducedOstream();
Meki Cherkaoui8f173612012-06-06 01:05:40 -070029}
30
31function encodeForwardingEntry(co){
32 var enc = new BinaryXMLEncoder();
33
Jeff Thompson86aea882012-09-29 17:32:48 -070034 co.to_ccnb(enc);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070035
36 var bytes = enc.getReducedOstream();
37
38 return bytes;
39
40
41}
42
43
44
45function decodeHexFaceInstance(result){
46
47 var numbers = DataUtils.toNumbers(result);
48
49
50 decoder = new BinaryXMLDecoder(numbers);
51
52 if(LOG>3)console.log('DECODING HEX FACE INSTANCE \n'+numbers);
53
Jeff Thompson86aea882012-09-29 17:32:48 -070054 var faceInstance = new FaceInstance();
Meki Cherkaoui8f173612012-06-06 01:05:40 -070055
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070056 faceInstance.from_ccnb(decoder);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070057
Jeff Thompson86aea882012-09-29 17:32:48 -070058 return faceInstance;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070059
60}
61
Jeff Thompson344891b2012-11-11 18:41:09 -080062
63
Meki Cherkaoui8f173612012-06-06 01:05:40 -070064function decodeHexInterest(result){
Jeff Thompson344891b2012-11-11 18:41:09 -080065 var numbers = DataUtils.toNumbers(result);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070066
67 decoder = new BinaryXMLDecoder(numbers);
Jeff Thompson344891b2012-11-11 18:41:09 -080068
Meki Cherkaoui8f173612012-06-06 01:05:40 -070069 if(LOG>3)console.log('DECODING HEX INTERST \n'+numbers);
70
Jeff Thompson86aea882012-09-29 17:32:48 -070071 var interest = new Interest();
Meki Cherkaoui8f173612012-06-06 01:05:40 -070072
Jeff Thompson86aea882012-09-29 17:32:48 -070073 interest.from_ccnb(decoder);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070074
Jeff Thompson86aea882012-09-29 17:32:48 -070075 return interest;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070076
77}
78
79
80
81function decodeHexContentObject(result){
82 var numbers = DataUtils.toNumbers(result);
Jeff Thompson344891b2012-11-11 18:41:09 -080083
Meki Cherkaoui8f173612012-06-06 01:05:40 -070084 decoder = new BinaryXMLDecoder(numbers);
Jeff Thompson344891b2012-11-11 18:41:09 -080085
Meki Cherkaoui8f173612012-06-06 01:05:40 -070086 if(LOG>3)console.log('DECODED HEX CONTENT OBJECT \n'+numbers);
87
88 co = new ContentObject();
89
Jeff Thompson86aea882012-09-29 17:32:48 -070090 co.from_ccnb(decoder);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070091
92 return co;
93
94}
95
96
97
98function decodeHexForwardingEntry(result){
99 var numbers = DataUtils.toNumbers(result);
100
101 decoder = new BinaryXMLDecoder(numbers);
102
103 if(LOG>3)console.log('DECODED HEX FORWARDING ENTRY \n'+numbers);
104
Jeff Thompson86aea882012-09-29 17:32:48 -0700105 forwardingEntry = new ForwardingEntry();
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700106
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700107 forwardingEntry.from_ccnb(decoder);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700108
Jeff Thompson86aea882012-09-29 17:32:48 -0700109 return forwardingEntry;
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700110
jeffcc8b3a92012-09-03 15:13:27 -0700111}
112
113/* Return a user friendly HTML string with the contents of co.
114 This also outputs to console.log.
115 */
116function contentObjectToHtml(/* ContentObject */ co) {
117 var output ="";
118
119 if(co==-1)
120 output+= "NO CONTENT FOUND"
121 else if (co==-2)
122 output+= "CONTENT NAME IS EMPTY"
123 else{
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700124 if(co.name!=null && co.name.components!=null){
Jeff Thompsona5a2a2b2012-11-15 00:09:17 -0800125 output+= "NAME: " + co.name.to_uri();
126
jeffcc8b3a92012-09-03 15:13:27 -0700127 output+= "<br />";
128 output+= "<br />";
129 }
130
Jeff Thompson86aea882012-09-29 17:32:48 -0700131 if(co.content !=null){
132 output += "CONTENT(ASCII): "+ DataUtils.toString(co.content);
jeffcc8b3a92012-09-03 15:13:27 -0700133
134 output+= "<br />";
135 output+= "<br />";
136 }
Jeff Thompson86aea882012-09-29 17:32:48 -0700137 if(co.content !=null){
138 output += "CONTENT(hex): "+ DataUtils.toHex(co.content);
jeffcc8b3a92012-09-03 15:13:27 -0700139
140 output+= "<br />";
141 output+= "<br />";
142 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700143 if(co.signature !=null && co.signature.signature!=null){
144 output += "SIGNATURE(hex): "+ DataUtils.toHex(co.signature.signature);
jeffcc8b3a92012-09-03 15:13:27 -0700145
146 output+= "<br />";
147 output+= "<br />";
148 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700149 if(co.signedInfo !=null && co.signedInfo.publisher!=null && co.signedInfo.publisher.publisherPublicKeyDigest!=null){
150 output += "Publisher Public Key Digest(hex): "+ DataUtils.toHex(co.signedInfo.publisher.publisherPublicKeyDigest);
jeffcc8b3a92012-09-03 15:13:27 -0700151
152 output+= "<br />";
153 output+= "<br />";
154 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700155 if(co.signedInfo !=null && co.signedInfo.timestamp!=null){
jeffcc8b3a92012-09-03 15:13:27 -0700156 var d = new Date();
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700157 d.setTime( co.signedInfo.timestamp.msec );
jeffcc8b3a92012-09-03 15:13:27 -0700158
159 var bytes = [217, 185, 12, 225, 217, 185, 12, 225];
160
161 output += "TimeStamp: "+d;
162 output+= "<br />";
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700163 output += "TimeStamp(number): "+ co.signedInfo.timestamp.msec;
jeffcc8b3a92012-09-03 15:13:27 -0700164
165 output+= "<br />";
166 }
Jeff Thompson64447882012-10-14 18:11:33 -0700167 if(co.signedInfo !=null && co.signedInfo.finalBlockID!=null){
168 output += "FinalBlockID: "+ DataUtils.toHex(co.signedInfo.finalBlockID);
169 output+= "<br />";
170 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700171 if(co.signedInfo!=null && co.signedInfo.locator!=null && co.signedInfo.locator.certificate!=null){
172 var tmp = DataUtils.toString(co.signedInfo.locator.certificate);
jeffcc8b3a92012-09-03 15:13:27 -0700173 var publickey = rstr2b64(tmp);
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700174 var publickeyHex = DataUtils.toHex(co.signedInfo.locator.certificate).toLowerCase();
175 var publickeyString = DataUtils.toString(co.signedInfo.locator.certificate);
176 var signature = DataUtils.toHex(co.signature.signature).toLowerCase();
jeffcc8b3a92012-09-03 15:13:27 -0700177 var input = DataUtils.toString(co.rawSignatureData);
178
179 output += "DER Certificate: "+publickey ;
180
181 output+= "<br />";
182 output+= "<br />";
183
184 if(LOG>2) console.log(" ContentName + SignedInfo + Content = "+input);
185
186 if(LOG>2) console.log("HEX OF ContentName + SignedInfo + Content = ");
187 if(LOG>2) console.log(DataUtils.stringtoBase64(input));
188
189 if(LOG>2) console.log(" PublicKey = "+publickey );
190 if(LOG>2) console.log(" PublicKeyHex = "+publickeyHex );
191 if(LOG>2) console.log(" PublicKeyString = "+publickeyString );
192
193 if(LOG>2) console.log(" Signature is");
194 if(LOG>2) console.log( signature );
195 //if(LOG>2) console.log(" Signature NOW IS" );
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700196 //if(LOG>2) console.log(co.signature.signature);
jeffcc8b3a92012-09-03 15:13:27 -0700197
198 var x509 = new X509();
199 x509.readCertPEM(publickey);
200
201 //x509.readCertPEMWithoutRSAInit(publickey);
202
203 var result = x509.subjectPublicKeyRSA.verifyByteArray(co.rawSignatureData, signature);
204 if(LOG>2) console.log('result is '+result);
205
206 var n = x509.subjectPublicKeyRSA.n;
207 var e = x509.subjectPublicKeyRSA.e;
208
209 if(LOG>2) console.log('PUBLIC KEY n after is ');
210 if(LOG>2) console.log(n);
211
212 if(LOG>2) console.log('EXPONENT e after is ');
213 if(LOG>2) console.log(e);
214
215 /*var rsakey = new RSAKey();
216
217 var kp = publickeyHex.slice(56,314);
218
219 output += "PUBLISHER KEY(hex): "+kp ;
220
221 output+= "<br />";
222 output+= "<br />";
223
224 console.log('kp is '+kp);
225
226 var exp = publickeyHex.slice(318,324);
227
228 console.log('kp size is '+kp.length );
229 output += "exponent: "+exp ;
230
231 output+= "<br />";
232 output+= "<br />";
233
234 console.log('exp is '+exp);
235
236 rsakey.setPublic(kp,exp);
237
238 var result = rsakey.verifyString(input, signature);*/
239
240 if(result)
241 output += 'SIGNATURE VALID';
242 else
243 output += 'SIGNATURE INVALID';
244
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700245 //output += "VALID: "+ toHex(co.signedInfo.locator.publicKey);
jeffcc8b3a92012-09-03 15:13:27 -0700246
247 output+= "<br />";
248 output+= "<br />";
249
250 //if(LOG>4) console.log('str'[1]);
251 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700252 if(co.signedInfo!=null && co.signedInfo.locator!=null && co.signedInfo.locator.publicKey!=null){
253 var publickey = rstr2b64(DataUtils.toString(co.signedInfo.locator.publicKey));
254 var publickeyHex = DataUtils.toHex(co.signedInfo.locator.publicKey).toLowerCase();
255 var publickeyString = DataUtils.toString(co.signedInfo.locator.publicKey);
256 var signature = DataUtils.toHex(co.signature.signature).toLowerCase();
jeffcc8b3a92012-09-03 15:13:27 -0700257 var input = DataUtils.toString(co.rawSignatureData);
258
259 output += "DER Certificate: "+publickey ;
260
261 output+= "<br />";
262 output+= "<br />";
263
264 if(LOG>2) console.log(" ContentName + SignedInfo + Content = "+input);
265 if(LOG>2) console.log(" PublicKey = "+publickey );
266 if(LOG>2) console.log(" PublicKeyHex = "+publickeyHex );
267 if(LOG>2) console.log(" PublicKeyString = "+publickeyString );
268
269 if(LOG>2) console.log(" Signature "+signature );
270
271 if(LOG>2) console.log(" Signature NOW IS" );
272
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700273 if(LOG>2) console.log(co.signature.signature);
jeffcc8b3a92012-09-03 15:13:27 -0700274
275 /*var x509 = new X509();
276
277 x509.readCertPEM(publickey);
278
279
280 //x509.readCertPEMWithoutRSAInit(publickey);
281
282 var result = x509.subjectPublicKeyRSA.verifyString(input, signature);*/
283 //console.log('result is '+result);
284
285 var kp = publickeyHex.slice(56,314);
286
287 output += "PUBLISHER KEY(hex): "+kp ;
288
289 output+= "<br />";
290 output+= "<br />";
291
292 console.log('PUBLIC KEY IN HEX is ');
293 console.log(kp);
294
295 var exp = publickeyHex.slice(318,324);
296
297 console.log('kp size is '+kp.length );
298 output += "exponent: "+exp ;
299
300 output+= "<br />";
301 output+= "<br />";
302
303 console.log('EXPONENT is ');
304 console.log(exp);
305
306 /*var c1 = hex_sha256(input);
307 var c2 = signature;
308
309 if(LOG>4)console.log('input is ');
310 if(LOG>4)console.log(input);
311 if(LOG>4)console.log('C1 is ');
312 if(LOG>4)console.log(c1);
313 if(LOG>4)console.log('C2 is ');
314 if(LOG>4)console.log(c2);
315 var result = c1 == c2;*/
316
317 var rsakey = new RSAKey();
318
319 rsakey.setPublic(kp,exp);
320
321 var result = rsakey.verifyByteArray(co.rawSignatureData,signature);
322 // var result = rsakey.verifyString(input, signature);
323
324 console.log('PUBLIC KEY n after is ');
325 console.log(rsakey.n);
326
327 console.log('EXPONENT e after is ');
328 console.log(rsakey.e);
329
330 if(result)
331 output += 'SIGNATURE VALID';
332 else
333 output += 'SIGNATURE INVALID';
334
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700335 //output += "VALID: "+ toHex(co.signedInfo.locator.publicKey);
jeffcc8b3a92012-09-03 15:13:27 -0700336
337 output+= "<br />";
338 output+= "<br />";
339
340 //if(LOG>4) console.log('str'[1]);
341 }
342 }
343
344 return output;
345}