blob: fd20c0ec711208c1c177dd8fd0e2f5d2750589fd [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 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
Jeff Thompson48ff28a2013-02-18 22:53:29 -080050 var decoder = new BinaryXMLDecoder(numbers);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070051
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
Jeff Thompson48ff28a2013-02-18 22:53:29 -080067 var 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
Jeff Thompson48ff28a2013-02-18 22:53:29 -080084 var 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
Jeff Thompson48ff28a2013-02-18 22:53:29 -080088 var co = new ContentObject();
Meki Cherkaoui8f173612012-06-06 01:05:40 -070089
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
Jeff Thompson48ff28a2013-02-18 22:53:29 -0800101 var decoder = new BinaryXMLDecoder(numbers);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700102
103 if(LOG>3)console.log('DECODED HEX FORWARDING ENTRY \n'+numbers);
104
Jeff Thompson48ff28a2013-02-18 22:53:29 -0800105 var 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
Jeff Thompson68fccd62012-12-29 17:38:23 -0800113/*
114 * Decode the Uint8Array which holds SubjectPublicKeyInfo and return an RSAKey.
115 */
116function decodeSubjectPublicKeyInfo(array) {
117 var hex = DataUtils.toHex(array).toLowerCase();
118 var a = _x509_getPublicKeyHexArrayFromCertHex(hex, _x509_getSubjectPublicKeyPosFromCertHex(hex, 0));
119 var rsaKey = new RSAKey();
120 rsaKey.setPublic(a[0], a[1]);
121 return rsaKey;
122}
123
jeffcc8b3a92012-09-03 15:13:27 -0700124/* 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){
Jeff Thompsona5a2a2b2012-11-15 00:09:17 -0800136 output+= "NAME: " + co.name.to_uri();
137
jeffcc8b3a92012-09-03 15:13:27 -0700138 output+= "<br />";
139 output+= "<br />";
140 }
141
Jeff Thompson86aea882012-09-29 17:32:48 -0700142 if(co.content !=null){
143 output += "CONTENT(ASCII): "+ DataUtils.toString(co.content);
jeffcc8b3a92012-09-03 15:13:27 -0700144
145 output+= "<br />";
146 output+= "<br />";
147 }
Jeff Thompson86aea882012-09-29 17:32:48 -0700148 if(co.content !=null){
149 output += "CONTENT(hex): "+ DataUtils.toHex(co.content);
jeffcc8b3a92012-09-03 15:13:27 -0700150
151 output+= "<br />";
152 output+= "<br />";
153 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700154 if(co.signature !=null && co.signature.signature!=null){
155 output += "SIGNATURE(hex): "+ DataUtils.toHex(co.signature.signature);
jeffcc8b3a92012-09-03 15:13:27 -0700156
157 output+= "<br />";
158 output+= "<br />";
159 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700160 if(co.signedInfo !=null && co.signedInfo.publisher!=null && co.signedInfo.publisher.publisherPublicKeyDigest!=null){
161 output += "Publisher Public Key Digest(hex): "+ DataUtils.toHex(co.signedInfo.publisher.publisherPublicKeyDigest);
jeffcc8b3a92012-09-03 15:13:27 -0700162
163 output+= "<br />";
164 output+= "<br />";
165 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700166 if(co.signedInfo !=null && co.signedInfo.timestamp!=null){
jeffcc8b3a92012-09-03 15:13:27 -0700167 var d = new Date();
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700168 d.setTime( co.signedInfo.timestamp.msec );
jeffcc8b3a92012-09-03 15:13:27 -0700169
170 var bytes = [217, 185, 12, 225, 217, 185, 12, 225];
171
172 output += "TimeStamp: "+d;
173 output+= "<br />";
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700174 output += "TimeStamp(number): "+ co.signedInfo.timestamp.msec;
jeffcc8b3a92012-09-03 15:13:27 -0700175
176 output+= "<br />";
177 }
Jeff Thompson64447882012-10-14 18:11:33 -0700178 if(co.signedInfo !=null && co.signedInfo.finalBlockID!=null){
179 output += "FinalBlockID: "+ DataUtils.toHex(co.signedInfo.finalBlockID);
180 output+= "<br />";
181 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700182 if(co.signedInfo!=null && co.signedInfo.locator!=null && co.signedInfo.locator.certificate!=null){
Jeff Thompson68fccd62012-12-29 17:38:23 -0800183 var certificateHex = DataUtils.toHex(co.signedInfo.locator.certificate).toLowerCase();
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700184 var signature = DataUtils.toHex(co.signature.signature).toLowerCase();
jeffcc8b3a92012-09-03 15:13:27 -0700185 var input = DataUtils.toString(co.rawSignatureData);
186
Jeff Thompson68fccd62012-12-29 17:38:23 -0800187 output += "Hex Certificate: "+ certificateHex ;
jeffcc8b3a92012-09-03 15:13:27 -0700188
189 output+= "<br />";
190 output+= "<br />";
191
jeffcc8b3a92012-09-03 15:13:27 -0700192 var x509 = new X509();
Jeff Thompson68fccd62012-12-29 17:38:23 -0800193 x509.readCertHex(certificateHex);
Jeff Thompson253cab42012-12-29 17:48:40 -0800194 output += "Public key (hex) modulus: " + x509.subjectPublicKeyRSA.n.toString(16) + "<br/>";
195 output += "exponent: " + x509.subjectPublicKeyRSA.e.toString(16) + "<br/>";
196 output += "<br/>";
jeffcc8b3a92012-09-03 15:13:27 -0700197
Wentao Shangfddf90d2013-01-05 17:18:49 -0800198 var result = x509.subjectPublicKeyRSA.verifyByteArray(co.rawSignatureData, null, signature);
jeffcc8b3a92012-09-03 15:13:27 -0700199 if(LOG>2) console.log('result is '+result);
200
201 var n = x509.subjectPublicKeyRSA.n;
202 var e = x509.subjectPublicKeyRSA.e;
203
204 if(LOG>2) console.log('PUBLIC KEY n after is ');
205 if(LOG>2) console.log(n);
206
207 if(LOG>2) console.log('EXPONENT e after is ');
208 if(LOG>2) console.log(e);
209
jeffcc8b3a92012-09-03 15:13:27 -0700210 if(result)
Jeff Thompson68fccd62012-12-29 17:38:23 -0800211 output += 'SIGNATURE VALID';
jeffcc8b3a92012-09-03 15:13:27 -0700212 else
Jeff Thompson68fccd62012-12-29 17:38:23 -0800213 output += 'SIGNATURE INVALID';
jeffcc8b3a92012-09-03 15:13:27 -0700214
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700215 //output += "VALID: "+ toHex(co.signedInfo.locator.publicKey);
jeffcc8b3a92012-09-03 15:13:27 -0700216
217 output+= "<br />";
218 output+= "<br />";
219
220 //if(LOG>4) console.log('str'[1]);
221 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700222 if(co.signedInfo!=null && co.signedInfo.locator!=null && co.signedInfo.locator.publicKey!=null){
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700223 var publickeyHex = DataUtils.toHex(co.signedInfo.locator.publicKey).toLowerCase();
224 var publickeyString = DataUtils.toString(co.signedInfo.locator.publicKey);
225 var signature = DataUtils.toHex(co.signature.signature).toLowerCase();
jeffcc8b3a92012-09-03 15:13:27 -0700226 var input = DataUtils.toString(co.rawSignatureData);
227
Wentao Shangfddf90d2013-01-05 17:18:49 -0800228 var wit = null;
229 var witHex = "";
230 if (co.signature.Witness != null) {
231 wit = new Witness();
232 wit.decode(co.signature.Witness);
233 witHex = DataUtils.toHex(co.signature.Witness);
234 }
235
Jeff Thompson68fccd62012-12-29 17:38:23 -0800236 output += "Public key: " + publickeyHex;
jeffcc8b3a92012-09-03 15:13:27 -0700237
238 output+= "<br />";
239 output+= "<br />";
240
241 if(LOG>2) console.log(" ContentName + SignedInfo + Content = "+input);
jeffcc8b3a92012-09-03 15:13:27 -0700242 if(LOG>2) console.log(" PublicKeyHex = "+publickeyHex );
243 if(LOG>2) console.log(" PublicKeyString = "+publickeyString );
244
245 if(LOG>2) console.log(" Signature "+signature );
Wentao Shangfddf90d2013-01-05 17:18:49 -0800246 if(LOG>2) console.log(" Witness "+witHex );
jeffcc8b3a92012-09-03 15:13:27 -0700247
248 if(LOG>2) console.log(" Signature NOW IS" );
249
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700250 if(LOG>2) console.log(co.signature.signature);
Jeff Thompson68fccd62012-12-29 17:38:23 -0800251
252 var rsakey = decodeSubjectPublicKeyInfo(co.signedInfo.locator.publicKey);
jeffcc8b3a92012-09-03 15:13:27 -0700253
Jeff Thompson68fccd62012-12-29 17:38:23 -0800254 output += "Public key (hex) modulus: " + rsakey.n.toString(16) + "<br/>";
255 output += "exponent: " + rsakey.e.toString(16) + "<br/>";
256 output += "<br/>";
257
Wentao Shangfddf90d2013-01-05 17:18:49 -0800258 var result = rsakey.verifyByteArray(co.rawSignatureData, wit, signature);
jeffcc8b3a92012-09-03 15:13:27 -0700259 // var result = rsakey.verifyString(input, signature);
260
Wentao Shang2b740e62012-12-07 00:02:53 -0800261 if(LOG>2) console.log('PUBLIC KEY n after is ');
262 if(LOG>2) console.log(rsakey.n);
jeffcc8b3a92012-09-03 15:13:27 -0700263
Wentao Shang2b740e62012-12-07 00:02:53 -0800264 if(LOG>2) console.log('EXPONENT e after is ');
265 if(LOG>2) console.log(rsakey.e);
jeffcc8b3a92012-09-03 15:13:27 -0700266
267 if(result)
Wentao Shangfddf90d2013-01-05 17:18:49 -0800268 output += 'SIGNATURE VALID';
jeffcc8b3a92012-09-03 15:13:27 -0700269 else
Wentao Shangfddf90d2013-01-05 17:18:49 -0800270 output += 'SIGNATURE INVALID';
jeffcc8b3a92012-09-03 15:13:27 -0700271
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700272 //output += "VALID: "+ toHex(co.signedInfo.locator.publicKey);
jeffcc8b3a92012-09-03 15:13:27 -0700273
274 output+= "<br />";
275 output+= "<br />";
276
277 //if(LOG>4) console.log('str'[1]);
278 }
279 }
280
281 return output;
282}
Jeff Thompson68fccd62012-12-29 17:38:23 -0800283
284