blob: 4d55cd7269a6fcb789441a06f7c8ba25f39ba033 [file] [log] [blame]
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -07001<?xml version = "1.0" encoding="utf-8" ?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3"DTD/xhtml1-strict.dtd">
4<html xmlns = "http://www.w3.org/1999/xhtml">
5
6<head>
Meki Cherkaoui8f173612012-06-06 01:05:40 -07007 <title>NDN Encode/Decode Content Object</title>
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -07008
9 <script type="text/javascript" src="java_socket_bridge.js"></script>
10
11 <script type="text/javascript" src="CCNProtocolDTags.js"></script>
12
13 <script type="text/javascript" src="CCNTime.js"></script>
14
15 <script type="text/javascript" src="ContentName.js"></script>
16
17 <script type="text/javascript" src="ContentObject.js"></script>
Meki Cherkaoui8f173612012-06-06 01:05:40 -070018
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070019 <script type="text/javascript" src="Exclude.js"></script>
20
21 <script type="text/javascript" src="ExcludeAny.js"></script>
22
23 <script type="text/javascript" src="ExcludeComponent.js"></script>
24
25 <script type="text/javascript" src="Interest.js"></script>
26
27 <script type="text/javascript" src="KeyLocator.js"></script>
28
29 <script type="text/javascript" src="KeyName.js"></script>
30
31 <script type="text/javascript" src="PublisherID.js"></script>
32
33 <script type="text/javascript" src="Signature.js"></script>
34
35 <script type="text/javascript" src="SignedInfo.js"></script>
36
37 <script type="text/javascript" src="PublisherPublicKeyDigest.js"></script>
38
39 <script type="text/javascript" src="FaceInstance.js"></script>
40
41 <script type="text/javascript" src="ForwardingEntry.js"></script>
42
43 <script type="text/javascript" src="encoding/BinaryXMLEncoder.js"></script>
44
45 <script type="text/javascript" src="encoding/BinaryXMLCodec.js"></script>
46
47 <script type="text/javascript" src="encoding/BinaryXMLDecoder.js"></script>
48
49 <script type="text/javascript" src="encoding/DataUtils.js"></script>
50
Meki Cherkaoui8f173612012-06-06 01:05:40 -070051 <script type="text/javascript" src="encoding/EncodingUtils.js"></script>
52
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070053 <script language="JavaScript" type="text/javascript" src="security/KeyManager.js"></script>
54
55 <script language="JavaScript" type="text/javascript" src="securityLib/jsbn.js"></script>
56 <script language="JavaScript" type="text/javascript" src="securityLib/jsbn2.js"></script>
57 <script language="JavaScript" type="text/javascript" src="securityLib/rsa.js"></script>
58 <script language="JavaScript" type="text/javascript" src="securityLib/rsa2.js"></script>
59
60 <script language="JavaScript" type="text/javascript" src="securityLib/sha1.js"></script>
61 <script language="JavaScript" type="text/javascript" src="securityLib/sha256.js"></script>
62 <script language="JavaScript" type="text/javascript" src="securityLib/sha512.js"></script>
63 <script language="JavaScript" type="text/javascript" src="securityLib/md5.js"></script>
64 <script language="JavaScript" type="text/javascript" src="securityLib/ripemd160.js"></script>
65 <script language="JavaScript" type="text/javascript" src="securityLib/base64.js"></script>
66
67 <script language="JavaScript" type="text/javascript" src="securityLib/rsapem-1.1.js"></script>
68 <script language="JavaScript" type="text/javascript" src="securityLib/rsasign-1.2.js"></script>
69 <script language="JavaScript" type="text/javascript" src="securityLib/asn1hex-1.1.js"></script>
70 <script language="JavaScript" type="text/javascript" src="securityLib/x509-1.1.js"></script>
71
72 <script type="text/javascript">
73
74 function encode(){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070075 var contentname = new ContentName( document.getElementById('contentname').value );
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070076
77 var content = document.getElementById('content').value;
78
79 var signedInfo = new SignedInfo();
Meki Cherkaoui8f173612012-06-06 01:05:40 -070080
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -070081 signedInfo.setFields();
82 //var signatureBits = generateSignatureBits(contentname,content,signedInfo);
83
84 //witness is null
85 var signature = new Signature();
86
87
88 var co = new ContentObject(contentname,signedInfo,content,signature);
89
90 co.sign();
91
92
93 var output = encodeToHexContentObject(co);
94
95 document.getElementById('result').innerHTML = output;
96
97 }
98
99 function decode(){
100
101
102
103 var input = document.getElementById('result').innerHTML;
104
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700105 input = input.toUpperCase();
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700106
107 var co = decodeHexContentObject(input);
108
109 if(LOG>3)console.log('CONTENT OBJECT DECODED');
110 if(LOG>3)console.log(co);
111
112 ///////////////////////////////////////
113
114 var output ="";
115
116 if(co==-1)
117 output+= "NO CONTENT FOUND"
118 else if (co==-2)
119 output+= "CONTENT NAME IS EMPTY"
120 else{
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700121
122
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700123 if(co.Name!=null && co.Name.Components!=null){
124 output+= "NAME: ";
125
126 for(var i=0;i<co.Name.Components.length;i++){
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700127 output+= "/"+ DataUtils.toString(co.Name.Components[i]);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700128 }
129 output+= "<br />";
130 output+= "<br />";
131 }
132
133 if(co.Content !=null){
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700134 output += "CONTENT(ASCII): "+ DataUtils.toString(co.Content);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700135
136 output+= "<br />";
137 output+= "<br />";
138 }
139 if(co.Content !=null){
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700140 output += "CONTENT(hex): "+ DataUtils.toHex(co.Content);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700141
142 output+= "<br />";
143 output+= "<br />";
144 }
145 if(co.Signature !=null && co.Signature.Signature!=null){
146
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700147 output += "SIGNATURE(hex): "+ DataUtils.toHex(co.Signature.Signature);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700148
149 output+= "<br />";
150 output+= "<br />";
151 }
152 if(co.SignedInfo !=null && co.SignedInfo.Publisher!=null && co.SignedInfo.Publisher.PublisherPublicKeyDigest!=null){
153
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700154 output += "Publisher Public Key Digest(hex): "+ DataUtils.toHex(co.SignedInfo.Publisher.PublisherPublicKeyDigest);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700155
156 output+= "<br />";
157 output+= "<br />";
158 }
159 if(co.SignedInfo !=null && co.SignedInfo.Timestamp!=null){
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700160 var d = new Date();
161 d.setTime( co.SignedInfo.Timestamp.msec );
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700162
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700163 var bytes = [217, 185, 12, 225, 217, 185, 12, 225];
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700164
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700165 output += "TimeStamp: "+d;
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700166 output+= "<br />";
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700167 output += "TimeStamp(number): "+ co.SignedInfo.Timestamp.msec;
168
169
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700170 output+= "<br />";
171 }
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700172 if(co.SignedInfo!=null && co.SignedInfo.Locator!=null && co.SignedInfo.Locator.Certificate!=null){
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700173
174
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700175 var tmp = DataUtils.toString(co.SignedInfo.Locator.Certificate);
176
177
178 var publickey = rstr2b64(tmp);
179
180
181
182 var publickeyHex = DataUtils.toHex(co.SignedInfo.Locator.Certificate).toLowerCase();
183
184 var publickeyString = DataUtils.toString(co.SignedInfo.Locator.Certificate);
185
186 var signature = DataUtils.toHex(co.Signature.Signature).toLowerCase();
187
188
189 var input = DataUtils.toString(co.rawSignatureData);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700190
191
192 output += "DER Certificate: "+publickey ;
193
194 output+= "<br />";
195 output+= "<br />";
196
197
198
199 if(LOG>2) console.log(" ContentName + SignedInfo + Content = "+input);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700200
201 if(LOG>2) console.log("HEX OF ContentName + SignedInfo + Content = ");
202 if(LOG>2) console.log(DataUtils.stringtoBase64(input));
203
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700204 if(LOG>2) console.log(" PublicKey = "+publickey );
205 if(LOG>2) console.log(" PublicKeyHex = "+publickeyHex );
206 if(LOG>2) console.log(" PublicKeyString = "+publickeyString );
207
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700208 if(LOG>2) console.log(" Signature is");
209 if(LOG>2) console.log( signature );
210 //if(LOG>2) console.log(" Signature NOW IS" );
211 //if(LOG>2) console.log(co.Signature.Signature);
212
213
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700214 var x509 = new X509();
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700215
216
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700217 x509.readCertPEM(publickey);
218
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700219 //x509.readCertPEMWithoutRSAInit(publickey);
220
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700221 var result = x509.subjectPublicKeyRSA.verifyByteArray(co.rawSignatureData, signature);
222 if(LOG>2) console.log('result is '+result);
223
224
225 var n = x509.subjectPublicKeyRSA.n;
226
227 var e = x509.subjectPublicKeyRSA.e;
228
229 if(LOG>2) console.log('PUBLIC KEY n after is ');
230 if(LOG>2) console.log(n);
231
232 if(LOG>2) console.log('EXPONENT e after is ');
233 if(LOG>2) console.log(e);
234
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700235 /*var rsakey = new RSAKey();
236
237 var kp = publickeyHex.slice(56,314);
238
239 output += "PUBLISHER KEY(hex): "+kp ;
240
241 output+= "<br />";
242 output+= "<br />";
243
244 console.log('kp is '+kp);
245
246 var exp = publickeyHex.slice(318,324);
247
248 console.log('kp size is '+kp.length );
249 output += "exponent: "+exp ;
250
251 output+= "<br />";
252 output+= "<br />";
253
254 console.log('exp is '+exp);
255
256
257 rsakey.setPublic(kp,exp);
258
259 var result = rsakey.verifyString(input, signature);*/
260
261 if(result)
262 output += 'SIGNATURE VALID';
263 else
264 output += 'SIGNATURE INVALID';
265
266
267
268
269 //output += "VALID: "+ toHex(co.SignedInfo.Locator.PublicKey);
270
271 output+= "<br />";
272 output+= "<br />";
273
274
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700275 //if(LOG>4) console.log('str'[1]);
276 }
277 if(co.SignedInfo!=null && co.SignedInfo.Locator!=null && co.SignedInfo.Locator.PublicKey!=null){
278
279
280 var publickey = rstr2b64(DataUtils.toString(co.SignedInfo.Locator.PublicKey));
281 var publickeyHex = DataUtils.toHex(co.SignedInfo.Locator.PublicKey).toLowerCase();
282 var publickeyString = DataUtils.toString(co.SignedInfo.Locator.PublicKey);
283
284 var signature = DataUtils.toHex(co.Signature.Signature).toLowerCase();
285
286
287 var input = DataUtils.toString(co.rawSignatureData);
288
289
290 output += "DER Certificate: "+publickey ;
291
292 output+= "<br />";
293 output+= "<br />";
294
295 if(LOG>2) console.log(" ContentName + SignedInfo + Content = "+input);
296 if(LOG>2) console.log(" PublicKey = "+publickey );
297 if(LOG>2) console.log(" PublicKeyHex = "+publickeyHex );
298 if(LOG>2) console.log(" PublicKeyString = "+publickeyString );
299
300 if(LOG>2) console.log(" Signature "+signature );
301
302 if(LOG>2) console.log(" Signature NOW IS" );
303
304 if(LOG>2) console.log(co.Signature.Signature);
305
306
307 /*var x509 = new X509();
308
309 x509.readCertPEM(publickey);
310
311
312 //x509.readCertPEMWithoutRSAInit(publickey);
313
314 var result = x509.subjectPublicKeyRSA.verifyString(input, signature);*/
315 //console.log('result is '+result);
316
317
318 var kp = publickeyHex.slice(56,314);
319
320 output += "PUBLISHER KEY(hex): "+kp ;
321
322 output+= "<br />";
323 output+= "<br />";
324
325 console.log('PUBLIC KEY IN HEX is ');
326 console.log(kp);
327
328 var exp = publickeyHex.slice(318,324);
329
330 console.log('kp size is '+kp.length );
331 output += "exponent: "+exp ;
332
333 output+= "<br />";
334 output+= "<br />";
335
336 console.log('EXPONENT is ');
337 console.log(exp);
338
339
340
341 /*var c1 = hex_sha256(input);
342 var c2 = signature;
343
344 if(LOG>4)console.log('input is ');
345 if(LOG>4)console.log(input);
346 if(LOG>4)console.log('C1 is ');
347 if(LOG>4)console.log(c1);
348 if(LOG>4)console.log('C2 is ');
349 if(LOG>4)console.log(c2);
350 var result = c1 == c2;*/
351
352 var rsakey = new RSAKey();
353
354 rsakey.setPublic(kp,exp);
355
356 var result = rsakey.verifyByteArray(co.rawSignatureData,signature);
357 // var result = rsakey.verifyString(input, signature);
358
359
360
361 console.log('PUBLIC KEY n after is ');
362 console.log(rsakey.n);
363
364 console.log('EXPONENT e after is ');
365 console.log(rsakey.e);
366
367
368 if(result)
369 output += 'SIGNATURE VALID';
370 else
371 output += 'SIGNATURE INVALID';
372
373
374
375
376 //output += "VALID: "+ toHex(co.SignedInfo.Locator.PublicKey);
377
378 output+= "<br />";
379 output+= "<br />";
380
381
382 //if(LOG>4) console.log('str'[1]);
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700383 }
384 }
385
386 document.getElementById('result').innerHTML = output;
387
388 }
389
390 </script>
391
392</head>
393<body >
394 <form>
395
396 Please Enter a Content Name:<br />
397
398 <input id="contentname" type="text" name="CONTENTNAME" value="/PARC/abc" />
399
400 Please Enter the Content:<br />
401
402 <textarea id="content" cols="40" rows="5" name="CONTENT" value="SUCCESS" >SUCCESS!</textarea>
403
404 </form>
405 <button onclick="encode()">Encode</button>
406 <button onclick="decode()">Decode</button>
407
408
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700409 <p id="result">048202aa03b20885a8592ebf94205e85de1b143fb423680729910012bce6e2c9b2045e2988b2d7a358bf659d8bd714fbf1859560df6dacef07d07760e29d37a9a71d61ed51d04aa1ba1f6470fb985e3a328e5dcc215bc4c3aa61e3fe8f609709b34912dda892463e611fd86af2e69ff7688008f0dd1ee5680a037be308f4fbb1aef333ad33bf31170000f2fafdc12e4d2e532e6c6f63616c686f737400fabdc12e4d2e53525600faa563636e6400fa9d4b455900fa02b5c12e4d2e4b00e0a01e093968f9740ce7f4361babf5bb05a4e55aaca5e58f73eddeb8e013aa8f00fabdfd04fcc87f5ade00fa8d00000001a203e20285e0a01e093968f9740ce7f4361babf5bb05a4e55aaca5e58f73eddeb8e013aa8f0002bab504fcc88dd36f0002c29d28463f0003d29e3630300003da8d000001e201eaf2fafdc12e4d2e532e6c6f63616c686f737400fabdc12e4d2e53525600faa563636e6400fa9d4b455900fa02b5c12e4d2e4b00e0a01e093968f9740ce7f4361babf5bb05a4e55aaca5e58f73eddeb8e013aa8f00fabdfd04fcc87f5ade0000000000019a0a9530819f300d06092a864886f70d010101050003818d0030818902818100b945ae17ef820242f542eb1af9eb92ad00a475741db5f8b4cb9d3a1131ad7f7d977e7b7809258f50cee91b56d6f16adb9c7a9492fe004f789bc5d4919b309740f910c26b8603d3f165bb62fef303663df67f244370492c6f1c5e5e9917fe7d4a3f6b0756760a805c467bcb0f143a159e267dc10eb1f4320f1ff2f247c3cbfd25020301000100000000000</p>
410 <!-- p id="result">058203e20285e0a01e093968f9740ce7f4361babf5bb05a4e55aaca5e58f73eddeb8e013aa8f0004d29632310004da9631370004e2c631302e312e312e310004eaa6393639350003d2d6323134373438333634370000</p-->
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700411
Meki Cherkaoui88d59cd2012-05-14 07:34:58 -0700412
413</body>
414</html>