blob: d90a24076e4108ec9711e376b122f903f97edf36 [file] [log] [blame]
Meki Cherkaoui8f173612012-06-06 01:05:40 -07001
2
3
4
Jeff Thompson86aea882012-09-29 17:32:48 -07005function encodeToHexInterest(interest){
Meki Cherkaoui8f173612012-06-06 01:05:40 -07006
7 var enc = new BinaryXMLEncoder();
8
Jeff Thompson86aea882012-09-29 17:32:48 -07009 interest.to_ccnb(enc);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070010
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
Jeff Thompson86aea882012-09-29 17:32:48 -070022 co.to_ccnb(enc);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070023
24 var hex = DataUtils.toHex(enc.getReducedOstream());
25
26 return hex;
27
28
29}
30
31function encodeToBinaryContentObject(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 hex = enc.getReducedOstream();
37
38 return hex;
39
40
41}
42
43function encodeForwardingEntry(co){
44 var enc = new BinaryXMLEncoder();
45
Jeff Thompson86aea882012-09-29 17:32:48 -070046 co.to_ccnb(enc);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070047
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
Jeff Thompson86aea882012-09-29 17:32:48 -070066 var faceInstance = new FaceInstance();
Meki Cherkaoui8f173612012-06-06 01:05:40 -070067
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070068 faceInstance.from_ccnb(decoder);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070069
Jeff Thompson86aea882012-09-29 17:32:48 -070070 return faceInstance;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070071
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
Jeff Thompson86aea882012-09-29 17:32:48 -070081 var interest = new Interest();
Meki Cherkaoui8f173612012-06-06 01:05:40 -070082
Jeff Thompson86aea882012-09-29 17:32:48 -070083 interest.from_ccnb(decoder);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070084
Jeff Thompson86aea882012-09-29 17:32:48 -070085 return interest;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070086
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
Jeff Thompson86aea882012-09-29 17:32:48 -070099 co.from_ccnb(decoder);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700100
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
Jeff Thompson86aea882012-09-29 17:32:48 -0700114 forwardingEntry = new ForwardingEntry();
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700115
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700116 forwardingEntry.from_ccnb(decoder);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700117
Jeff Thompson86aea882012-09-29 17:32:48 -0700118 return forwardingEntry;
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700119
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{
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700133 if(co.name!=null && co.name.components!=null){
jeffcc8b3a92012-09-03 15:13:27 -0700134 output+= "NAME: ";
135
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700136 for(var i=0;i<co.name.components.length;i++){
137 output+= "/"+ DataUtils.toString(co.name.components[i]);
jeffcc8b3a92012-09-03 15:13:27 -0700138 }
139 output+= "<br />";
140 output+= "<br />";
141 }
142
Jeff Thompson86aea882012-09-29 17:32:48 -0700143 if(co.content !=null){
144 output += "CONTENT(ASCII): "+ DataUtils.toString(co.content);
jeffcc8b3a92012-09-03 15:13:27 -0700145
146 output+= "<br />";
147 output+= "<br />";
148 }
Jeff Thompson86aea882012-09-29 17:32:48 -0700149 if(co.content !=null){
150 output += "CONTENT(hex): "+ DataUtils.toHex(co.content);
jeffcc8b3a92012-09-03 15:13:27 -0700151
152 output+= "<br />";
153 output+= "<br />";
154 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700155 if(co.signature !=null && co.signature.signature!=null){
156 output += "SIGNATURE(hex): "+ DataUtils.toHex(co.signature.signature);
jeffcc8b3a92012-09-03 15:13:27 -0700157
158 output+= "<br />";
159 output+= "<br />";
160 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700161 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);
jeffcc8b3a92012-09-03 15:13:27 -0700163
164 output+= "<br />";
165 output+= "<br />";
166 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700167 if(co.signedInfo !=null && co.signedInfo.timestamp!=null){
jeffcc8b3a92012-09-03 15:13:27 -0700168 var d = new Date();
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700169 d.setTime( co.signedInfo.timestamp.msec );
jeffcc8b3a92012-09-03 15:13:27 -0700170
171 var bytes = [217, 185, 12, 225, 217, 185, 12, 225];
172
173 output += "TimeStamp: "+d;
174 output+= "<br />";
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700175 output += "TimeStamp(number): "+ co.signedInfo.timestamp.msec;
jeffcc8b3a92012-09-03 15:13:27 -0700176
177 output+= "<br />";
178 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700179 if(co.signedInfo!=null && co.signedInfo.locator!=null && co.signedInfo.locator.certificate!=null){
180 var tmp = DataUtils.toString(co.signedInfo.locator.certificate);
jeffcc8b3a92012-09-03 15:13:27 -0700181 var publickey = rstr2b64(tmp);
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700182 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();
jeffcc8b3a92012-09-03 15:13:27 -0700185 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" );
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700204 //if(LOG>2) console.log(co.signature.signature);
jeffcc8b3a92012-09-03 15:13:27 -0700205
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
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700253 //output += "VALID: "+ toHex(co.signedInfo.locator.publicKey);
jeffcc8b3a92012-09-03 15:13:27 -0700254
255 output+= "<br />";
256 output+= "<br />";
257
258 //if(LOG>4) console.log('str'[1]);
259 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700260 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();
jeffcc8b3a92012-09-03 15:13:27 -0700265 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
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700281 if(LOG>2) console.log(co.signature.signature);
jeffcc8b3a92012-09-03 15:13:27 -0700282
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
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700343 //output += "VALID: "+ toHex(co.signedInfo.locator.publicKey);
jeffcc8b3a92012-09-03 15:13:27 -0700344
345 output+= "<br />";
346 output+= "<br />";
347
348 //if(LOG>4) console.log('str'[1]);
349 }
350 }
351
352 return output;
353}