blob: 0715f27a56adc3a0c365c60aa348f13df5af22a8 [file] [log] [blame]
Wentao Shangbd63e462012-12-03 16:19:33 -08001/**
Jeff Thompson745026e2012-10-13 12:49:20 -07002 * 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 Thompsonbc0d3262013-07-26 18:15:36 -07008 return DataUtils.toHex(interest.encode());
Meki Cherkaoui8f173612012-06-06 01:05:40 -07009}
10
Jeff Thompson2b14c7e2013-07-29 15:09:56 -070011/**
12 * @deprecated Use interest.encode().
13 */
Jeff Thompsona5a2a2b2012-11-15 00:09:17 -080014function encodeToBinaryInterest(interest) {
Jeff Thompsonbc0d3262013-07-26 18:15:36 -070015 return interest.encode();
Jeff Thompson344891b2012-11-11 18:41:09 -080016}
17
Jeff Thompsonbc0d3262013-07-26 18:15:36 -070018function encodeToHexContentObject(contentObject) {
19 return DataUtils.toHex(contentObject.encode());
Meki Cherkaoui8f173612012-06-06 01:05:40 -070020}
21
Jeff Thompson2b14c7e2013-07-29 15:09:56 -070022/**
23 * @deprecated Use contentObject.encode().
24 */
Jeff Thompsonbc0d3262013-07-26 18:15:36 -070025function encodeToBinaryContentObject(contentObject) {
26 contentObject.encode();
Meki Cherkaoui8f173612012-06-06 01:05:40 -070027}
28
Jeff Thompsonbc0d3262013-07-26 18:15:36 -070029function encodeForwardingEntry(co) {
Meki Cherkaoui8f173612012-06-06 01:05:40 -070030 var enc = new BinaryXMLEncoder();
31
Jeff Thompson86aea882012-09-29 17:32:48 -070032 co.to_ccnb(enc);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070033
34 var bytes = enc.getReducedOstream();
35
36 return bytes;
37
38
39}
40
41
42
43function decodeHexFaceInstance(result){
44
45 var numbers = DataUtils.toNumbers(result);
46
47
Jeff Thompson48ff28a2013-02-18 22:53:29 -080048 var decoder = new BinaryXMLDecoder(numbers);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070049
50 if(LOG>3)console.log('DECODING HEX FACE INSTANCE \n'+numbers);
51
Jeff Thompson86aea882012-09-29 17:32:48 -070052 var faceInstance = new FaceInstance();
Meki Cherkaoui8f173612012-06-06 01:05:40 -070053
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070054 faceInstance.from_ccnb(decoder);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070055
Jeff Thompson86aea882012-09-29 17:32:48 -070056 return faceInstance;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070057
58}
59
Jeff Thompsonbc0d3262013-07-26 18:15:36 -070060function decodeHexInterest(input){
Jeff Thompson86aea882012-09-29 17:32:48 -070061 var interest = new Interest();
Jeff Thompsonbc0d3262013-07-26 18:15:36 -070062 interest.decode(DataUtils.toNumbers(input));
Jeff Thompson86aea882012-09-29 17:32:48 -070063 return interest;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070064}
65
Jeff Thompsonbc0d3262013-07-26 18:15:36 -070066function decodeHexContentObject(input){
67 var contentObject = new ContentObject();
68 contentObject.decode(DataUtils.toNumbers(input));
69 return contentObject;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070070}
71
Meki Cherkaoui8f173612012-06-06 01:05:40 -070072function decodeHexForwardingEntry(result){
73 var numbers = DataUtils.toNumbers(result);
74
Jeff Thompson48ff28a2013-02-18 22:53:29 -080075 var decoder = new BinaryXMLDecoder(numbers);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070076
77 if(LOG>3)console.log('DECODED HEX FORWARDING ENTRY \n'+numbers);
78
Jeff Thompson48ff28a2013-02-18 22:53:29 -080079 var forwardingEntry = new ForwardingEntry();
Meki Cherkaoui8f173612012-06-06 01:05:40 -070080
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070081 forwardingEntry.from_ccnb(decoder);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070082
Jeff Thompson86aea882012-09-29 17:32:48 -070083 return forwardingEntry;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070084
jeffcc8b3a92012-09-03 15:13:27 -070085}
86
Jeff Thompson2b14c7e2013-07-29 15:09:56 -070087/**
Jeff Thompson68fccd62012-12-29 17:38:23 -080088 * Decode the Uint8Array which holds SubjectPublicKeyInfo and return an RSAKey.
89 */
90function decodeSubjectPublicKeyInfo(array) {
91 var hex = DataUtils.toHex(array).toLowerCase();
92 var a = _x509_getPublicKeyHexArrayFromCertHex(hex, _x509_getSubjectPublicKeyPosFromCertHex(hex, 0));
93 var rsaKey = new RSAKey();
94 rsaKey.setPublic(a[0], a[1]);
95 return rsaKey;
96}
97
Jeff Thompson2b14c7e2013-07-29 15:09:56 -070098/**
99 * Return a user friendly HTML string with the contents of co.
100 * This also outputs to console.log.
jeffcc8b3a92012-09-03 15:13:27 -0700101 */
102function contentObjectToHtml(/* ContentObject */ co) {
103 var output ="";
104
105 if(co==-1)
106 output+= "NO CONTENT FOUND"
107 else if (co==-2)
108 output+= "CONTENT NAME IS EMPTY"
109 else{
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700110 if(co.name!=null && co.name.components!=null){
Jeff Thompsona5a2a2b2012-11-15 00:09:17 -0800111 output+= "NAME: " + co.name.to_uri();
112
jeffcc8b3a92012-09-03 15:13:27 -0700113 output+= "<br />";
114 output+= "<br />";
115 }
116
Jeff Thompson86aea882012-09-29 17:32:48 -0700117 if(co.content !=null){
118 output += "CONTENT(ASCII): "+ DataUtils.toString(co.content);
jeffcc8b3a92012-09-03 15:13:27 -0700119
120 output+= "<br />";
121 output+= "<br />";
122 }
Jeff Thompson86aea882012-09-29 17:32:48 -0700123 if(co.content !=null){
124 output += "CONTENT(hex): "+ DataUtils.toHex(co.content);
jeffcc8b3a92012-09-03 15:13:27 -0700125
126 output+= "<br />";
127 output+= "<br />";
128 }
Jeff Thompson612f2552013-07-09 19:31:44 -0700129 if(co.signature !=null && co.signature.digestAlgorithm!=null){
130 output += "DigestAlgorithm (hex): "+ DataUtils.toHex(co.signature.digestAlgorithm);
131
132 output+= "<br />";
133 output+= "<br />";
134 }
135 if(co.signature !=null && co.signature.witness!=null){
136 output += "Witness (hex): "+ DataUtils.toHex(co.signature.witness);
137
138 output+= "<br />";
139 output+= "<br />";
140 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700141 if(co.signature !=null && co.signature.signature!=null){
Jeff Thompson612f2552013-07-09 19:31:44 -0700142 output += "Signature(hex): "+ DataUtils.toHex(co.signature.signature);
jeffcc8b3a92012-09-03 15:13:27 -0700143
144 output+= "<br />";
145 output+= "<br />";
146 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700147 if(co.signedInfo !=null && co.signedInfo.publisher!=null && co.signedInfo.publisher.publisherPublicKeyDigest!=null){
148 output += "Publisher Public Key Digest(hex): "+ DataUtils.toHex(co.signedInfo.publisher.publisherPublicKeyDigest);
jeffcc8b3a92012-09-03 15:13:27 -0700149
150 output+= "<br />";
151 output+= "<br />";
152 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700153 if(co.signedInfo !=null && co.signedInfo.timestamp!=null){
jeffcc8b3a92012-09-03 15:13:27 -0700154 var d = new Date();
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700155 d.setTime( co.signedInfo.timestamp.msec );
jeffcc8b3a92012-09-03 15:13:27 -0700156
157 var bytes = [217, 185, 12, 225, 217, 185, 12, 225];
158
159 output += "TimeStamp: "+d;
160 output+= "<br />";
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700161 output += "TimeStamp(number): "+ co.signedInfo.timestamp.msec;
jeffcc8b3a92012-09-03 15:13:27 -0700162
163 output+= "<br />";
164 }
Jeff Thompson64447882012-10-14 18:11:33 -0700165 if(co.signedInfo !=null && co.signedInfo.finalBlockID!=null){
166 output += "FinalBlockID: "+ DataUtils.toHex(co.signedInfo.finalBlockID);
167 output+= "<br />";
168 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700169 if(co.signedInfo!=null && co.signedInfo.locator!=null && co.signedInfo.locator.certificate!=null){
Jeff Thompson68fccd62012-12-29 17:38:23 -0800170 var certificateHex = DataUtils.toHex(co.signedInfo.locator.certificate).toLowerCase();
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700171 var signature = DataUtils.toHex(co.signature.signature).toLowerCase();
jeffcc8b3a92012-09-03 15:13:27 -0700172 var input = DataUtils.toString(co.rawSignatureData);
173
Jeff Thompson68fccd62012-12-29 17:38:23 -0800174 output += "Hex Certificate: "+ certificateHex ;
jeffcc8b3a92012-09-03 15:13:27 -0700175
176 output+= "<br />";
177 output+= "<br />";
178
jeffcc8b3a92012-09-03 15:13:27 -0700179 var x509 = new X509();
Jeff Thompson68fccd62012-12-29 17:38:23 -0800180 x509.readCertHex(certificateHex);
Jeff Thompson253cab42012-12-29 17:48:40 -0800181 output += "Public key (hex) modulus: " + x509.subjectPublicKeyRSA.n.toString(16) + "<br/>";
182 output += "exponent: " + x509.subjectPublicKeyRSA.e.toString(16) + "<br/>";
183 output += "<br/>";
jeffcc8b3a92012-09-03 15:13:27 -0700184
Wentao Shangfddf90d2013-01-05 17:18:49 -0800185 var result = x509.subjectPublicKeyRSA.verifyByteArray(co.rawSignatureData, null, signature);
jeffcc8b3a92012-09-03 15:13:27 -0700186 if(LOG>2) console.log('result is '+result);
187
188 var n = x509.subjectPublicKeyRSA.n;
189 var e = x509.subjectPublicKeyRSA.e;
190
191 if(LOG>2) console.log('PUBLIC KEY n after is ');
192 if(LOG>2) console.log(n);
193
194 if(LOG>2) console.log('EXPONENT e after is ');
195 if(LOG>2) console.log(e);
196
jeffcc8b3a92012-09-03 15:13:27 -0700197 if(result)
Jeff Thompson68fccd62012-12-29 17:38:23 -0800198 output += 'SIGNATURE VALID';
jeffcc8b3a92012-09-03 15:13:27 -0700199 else
Jeff Thompson68fccd62012-12-29 17:38:23 -0800200 output += 'SIGNATURE INVALID';
jeffcc8b3a92012-09-03 15:13:27 -0700201
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700202 //output += "VALID: "+ toHex(co.signedInfo.locator.publicKey);
jeffcc8b3a92012-09-03 15:13:27 -0700203
204 output+= "<br />";
205 output+= "<br />";
206
207 //if(LOG>4) console.log('str'[1]);
208 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700209 if(co.signedInfo!=null && co.signedInfo.locator!=null && co.signedInfo.locator.publicKey!=null){
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700210 var publickeyHex = DataUtils.toHex(co.signedInfo.locator.publicKey).toLowerCase();
211 var publickeyString = DataUtils.toString(co.signedInfo.locator.publicKey);
212 var signature = DataUtils.toHex(co.signature.signature).toLowerCase();
jeffcc8b3a92012-09-03 15:13:27 -0700213 var input = DataUtils.toString(co.rawSignatureData);
214
Wentao Shangfddf90d2013-01-05 17:18:49 -0800215 var wit = null;
216 var witHex = "";
217 if (co.signature.Witness != null) {
218 wit = new Witness();
219 wit.decode(co.signature.Witness);
220 witHex = DataUtils.toHex(co.signature.Witness);
221 }
222
Jeff Thompson68fccd62012-12-29 17:38:23 -0800223 output += "Public key: " + publickeyHex;
jeffcc8b3a92012-09-03 15:13:27 -0700224
225 output+= "<br />";
226 output+= "<br />";
227
228 if(LOG>2) console.log(" ContentName + SignedInfo + Content = "+input);
jeffcc8b3a92012-09-03 15:13:27 -0700229 if(LOG>2) console.log(" PublicKeyHex = "+publickeyHex );
230 if(LOG>2) console.log(" PublicKeyString = "+publickeyString );
231
232 if(LOG>2) console.log(" Signature "+signature );
Wentao Shangfddf90d2013-01-05 17:18:49 -0800233 if(LOG>2) console.log(" Witness "+witHex );
jeffcc8b3a92012-09-03 15:13:27 -0700234
235 if(LOG>2) console.log(" Signature NOW IS" );
236
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700237 if(LOG>2) console.log(co.signature.signature);
Jeff Thompson68fccd62012-12-29 17:38:23 -0800238
239 var rsakey = decodeSubjectPublicKeyInfo(co.signedInfo.locator.publicKey);
jeffcc8b3a92012-09-03 15:13:27 -0700240
Jeff Thompson68fccd62012-12-29 17:38:23 -0800241 output += "Public key (hex) modulus: " + rsakey.n.toString(16) + "<br/>";
242 output += "exponent: " + rsakey.e.toString(16) + "<br/>";
243 output += "<br/>";
244
Wentao Shangfddf90d2013-01-05 17:18:49 -0800245 var result = rsakey.verifyByteArray(co.rawSignatureData, wit, signature);
jeffcc8b3a92012-09-03 15:13:27 -0700246 // var result = rsakey.verifyString(input, signature);
247
Wentao Shang2b740e62012-12-07 00:02:53 -0800248 if(LOG>2) console.log('PUBLIC KEY n after is ');
249 if(LOG>2) console.log(rsakey.n);
jeffcc8b3a92012-09-03 15:13:27 -0700250
Wentao Shang2b740e62012-12-07 00:02:53 -0800251 if(LOG>2) console.log('EXPONENT e after is ');
252 if(LOG>2) console.log(rsakey.e);
jeffcc8b3a92012-09-03 15:13:27 -0700253
254 if(result)
Wentao Shangfddf90d2013-01-05 17:18:49 -0800255 output += 'SIGNATURE VALID';
jeffcc8b3a92012-09-03 15:13:27 -0700256 else
Wentao Shangfddf90d2013-01-05 17:18:49 -0800257 output += 'SIGNATURE INVALID';
jeffcc8b3a92012-09-03 15:13:27 -0700258
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 }
266 }
267
268 return output;
269}
Jeff Thompson68fccd62012-12-29 17:38:23 -0800270
271