blob: 02f0b3aff7cbff42c219d13c8c5b9c86be9fcefe [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 Thompsonbc0d3262013-07-26 18:15:36 -070011// Deprecated: Use interest.encode().
Jeff Thompsona5a2a2b2012-11-15 00:09:17 -080012function encodeToBinaryInterest(interest) {
Jeff Thompsonbc0d3262013-07-26 18:15:36 -070013 return interest.encode();
Jeff Thompson344891b2012-11-11 18:41:09 -080014}
15
Jeff Thompsonbc0d3262013-07-26 18:15:36 -070016function encodeToHexContentObject(contentObject) {
17 return DataUtils.toHex(contentObject.encode());
Meki Cherkaoui8f173612012-06-06 01:05:40 -070018}
19
Jeff Thompsonbc0d3262013-07-26 18:15:36 -070020// Deprecated: Use contentObject.encode().
21function encodeToBinaryContentObject(contentObject) {
22 contentObject.encode();
Meki Cherkaoui8f173612012-06-06 01:05:40 -070023}
24
Jeff Thompsonbc0d3262013-07-26 18:15:36 -070025function encodeForwardingEntry(co) {
Meki Cherkaoui8f173612012-06-06 01:05:40 -070026 var enc = new BinaryXMLEncoder();
27
Jeff Thompson86aea882012-09-29 17:32:48 -070028 co.to_ccnb(enc);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070029
30 var bytes = enc.getReducedOstream();
31
32 return bytes;
33
34
35}
36
37
38
39function decodeHexFaceInstance(result){
40
41 var numbers = DataUtils.toNumbers(result);
42
43
Jeff Thompson48ff28a2013-02-18 22:53:29 -080044 var decoder = new BinaryXMLDecoder(numbers);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070045
46 if(LOG>3)console.log('DECODING HEX FACE INSTANCE \n'+numbers);
47
Jeff Thompson86aea882012-09-29 17:32:48 -070048 var faceInstance = new FaceInstance();
Meki Cherkaoui8f173612012-06-06 01:05:40 -070049
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070050 faceInstance.from_ccnb(decoder);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070051
Jeff Thompson86aea882012-09-29 17:32:48 -070052 return faceInstance;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070053
54}
55
Jeff Thompsonbc0d3262013-07-26 18:15:36 -070056function decodeHexInterest(input){
Jeff Thompson86aea882012-09-29 17:32:48 -070057 var interest = new Interest();
Jeff Thompsonbc0d3262013-07-26 18:15:36 -070058 interest.decode(DataUtils.toNumbers(input));
Jeff Thompson86aea882012-09-29 17:32:48 -070059 return interest;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070060}
61
Jeff Thompsonbc0d3262013-07-26 18:15:36 -070062function decodeHexContentObject(input){
63 var contentObject = new ContentObject();
64 contentObject.decode(DataUtils.toNumbers(input));
65 return contentObject;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070066}
67
Meki Cherkaoui8f173612012-06-06 01:05:40 -070068function decodeHexForwardingEntry(result){
69 var numbers = DataUtils.toNumbers(result);
70
Jeff Thompson48ff28a2013-02-18 22:53:29 -080071 var decoder = new BinaryXMLDecoder(numbers);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070072
73 if(LOG>3)console.log('DECODED HEX FORWARDING ENTRY \n'+numbers);
74
Jeff Thompson48ff28a2013-02-18 22:53:29 -080075 var forwardingEntry = new ForwardingEntry();
Meki Cherkaoui8f173612012-06-06 01:05:40 -070076
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070077 forwardingEntry.from_ccnb(decoder);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070078
Jeff Thompson86aea882012-09-29 17:32:48 -070079 return forwardingEntry;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070080
jeffcc8b3a92012-09-03 15:13:27 -070081}
82
Jeff Thompson68fccd62012-12-29 17:38:23 -080083/*
84 * Decode the Uint8Array which holds SubjectPublicKeyInfo and return an RSAKey.
85 */
86function decodeSubjectPublicKeyInfo(array) {
87 var hex = DataUtils.toHex(array).toLowerCase();
88 var a = _x509_getPublicKeyHexArrayFromCertHex(hex, _x509_getSubjectPublicKeyPosFromCertHex(hex, 0));
89 var rsaKey = new RSAKey();
90 rsaKey.setPublic(a[0], a[1]);
91 return rsaKey;
92}
93
jeffcc8b3a92012-09-03 15:13:27 -070094/* Return a user friendly HTML string with the contents of co.
95 This also outputs to console.log.
96 */
97function contentObjectToHtml(/* ContentObject */ co) {
98 var output ="";
99
100 if(co==-1)
101 output+= "NO CONTENT FOUND"
102 else if (co==-2)
103 output+= "CONTENT NAME IS EMPTY"
104 else{
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700105 if(co.name!=null && co.name.components!=null){
Jeff Thompsona5a2a2b2012-11-15 00:09:17 -0800106 output+= "NAME: " + co.name.to_uri();
107
jeffcc8b3a92012-09-03 15:13:27 -0700108 output+= "<br />";
109 output+= "<br />";
110 }
111
Jeff Thompson86aea882012-09-29 17:32:48 -0700112 if(co.content !=null){
113 output += "CONTENT(ASCII): "+ DataUtils.toString(co.content);
jeffcc8b3a92012-09-03 15:13:27 -0700114
115 output+= "<br />";
116 output+= "<br />";
117 }
Jeff Thompson86aea882012-09-29 17:32:48 -0700118 if(co.content !=null){
119 output += "CONTENT(hex): "+ DataUtils.toHex(co.content);
jeffcc8b3a92012-09-03 15:13:27 -0700120
121 output+= "<br />";
122 output+= "<br />";
123 }
Jeff Thompson612f2552013-07-09 19:31:44 -0700124 if(co.signature !=null && co.signature.digestAlgorithm!=null){
125 output += "DigestAlgorithm (hex): "+ DataUtils.toHex(co.signature.digestAlgorithm);
126
127 output+= "<br />";
128 output+= "<br />";
129 }
130 if(co.signature !=null && co.signature.witness!=null){
131 output += "Witness (hex): "+ DataUtils.toHex(co.signature.witness);
132
133 output+= "<br />";
134 output+= "<br />";
135 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700136 if(co.signature !=null && co.signature.signature!=null){
Jeff Thompson612f2552013-07-09 19:31:44 -0700137 output += "Signature(hex): "+ DataUtils.toHex(co.signature.signature);
jeffcc8b3a92012-09-03 15:13:27 -0700138
139 output+= "<br />";
140 output+= "<br />";
141 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700142 if(co.signedInfo !=null && co.signedInfo.publisher!=null && co.signedInfo.publisher.publisherPublicKeyDigest!=null){
143 output += "Publisher Public Key Digest(hex): "+ DataUtils.toHex(co.signedInfo.publisher.publisherPublicKeyDigest);
jeffcc8b3a92012-09-03 15:13:27 -0700144
145 output+= "<br />";
146 output+= "<br />";
147 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700148 if(co.signedInfo !=null && co.signedInfo.timestamp!=null){
jeffcc8b3a92012-09-03 15:13:27 -0700149 var d = new Date();
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700150 d.setTime( co.signedInfo.timestamp.msec );
jeffcc8b3a92012-09-03 15:13:27 -0700151
152 var bytes = [217, 185, 12, 225, 217, 185, 12, 225];
153
154 output += "TimeStamp: "+d;
155 output+= "<br />";
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700156 output += "TimeStamp(number): "+ co.signedInfo.timestamp.msec;
jeffcc8b3a92012-09-03 15:13:27 -0700157
158 output+= "<br />";
159 }
Jeff Thompson64447882012-10-14 18:11:33 -0700160 if(co.signedInfo !=null && co.signedInfo.finalBlockID!=null){
161 output += "FinalBlockID: "+ DataUtils.toHex(co.signedInfo.finalBlockID);
162 output+= "<br />";
163 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700164 if(co.signedInfo!=null && co.signedInfo.locator!=null && co.signedInfo.locator.certificate!=null){
Jeff Thompson68fccd62012-12-29 17:38:23 -0800165 var certificateHex = DataUtils.toHex(co.signedInfo.locator.certificate).toLowerCase();
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700166 var signature = DataUtils.toHex(co.signature.signature).toLowerCase();
jeffcc8b3a92012-09-03 15:13:27 -0700167 var input = DataUtils.toString(co.rawSignatureData);
168
Jeff Thompson68fccd62012-12-29 17:38:23 -0800169 output += "Hex Certificate: "+ certificateHex ;
jeffcc8b3a92012-09-03 15:13:27 -0700170
171 output+= "<br />";
172 output+= "<br />";
173
jeffcc8b3a92012-09-03 15:13:27 -0700174 var x509 = new X509();
Jeff Thompson68fccd62012-12-29 17:38:23 -0800175 x509.readCertHex(certificateHex);
Jeff Thompson253cab42012-12-29 17:48:40 -0800176 output += "Public key (hex) modulus: " + x509.subjectPublicKeyRSA.n.toString(16) + "<br/>";
177 output += "exponent: " + x509.subjectPublicKeyRSA.e.toString(16) + "<br/>";
178 output += "<br/>";
jeffcc8b3a92012-09-03 15:13:27 -0700179
Wentao Shangfddf90d2013-01-05 17:18:49 -0800180 var result = x509.subjectPublicKeyRSA.verifyByteArray(co.rawSignatureData, null, signature);
jeffcc8b3a92012-09-03 15:13:27 -0700181 if(LOG>2) console.log('result is '+result);
182
183 var n = x509.subjectPublicKeyRSA.n;
184 var e = x509.subjectPublicKeyRSA.e;
185
186 if(LOG>2) console.log('PUBLIC KEY n after is ');
187 if(LOG>2) console.log(n);
188
189 if(LOG>2) console.log('EXPONENT e after is ');
190 if(LOG>2) console.log(e);
191
jeffcc8b3a92012-09-03 15:13:27 -0700192 if(result)
Jeff Thompson68fccd62012-12-29 17:38:23 -0800193 output += 'SIGNATURE VALID';
jeffcc8b3a92012-09-03 15:13:27 -0700194 else
Jeff Thompson68fccd62012-12-29 17:38:23 -0800195 output += 'SIGNATURE INVALID';
jeffcc8b3a92012-09-03 15:13:27 -0700196
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700197 //output += "VALID: "+ toHex(co.signedInfo.locator.publicKey);
jeffcc8b3a92012-09-03 15:13:27 -0700198
199 output+= "<br />";
200 output+= "<br />";
201
202 //if(LOG>4) console.log('str'[1]);
203 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700204 if(co.signedInfo!=null && co.signedInfo.locator!=null && co.signedInfo.locator.publicKey!=null){
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700205 var publickeyHex = DataUtils.toHex(co.signedInfo.locator.publicKey).toLowerCase();
206 var publickeyString = DataUtils.toString(co.signedInfo.locator.publicKey);
207 var signature = DataUtils.toHex(co.signature.signature).toLowerCase();
jeffcc8b3a92012-09-03 15:13:27 -0700208 var input = DataUtils.toString(co.rawSignatureData);
209
Wentao Shangfddf90d2013-01-05 17:18:49 -0800210 var wit = null;
211 var witHex = "";
212 if (co.signature.Witness != null) {
213 wit = new Witness();
214 wit.decode(co.signature.Witness);
215 witHex = DataUtils.toHex(co.signature.Witness);
216 }
217
Jeff Thompson68fccd62012-12-29 17:38:23 -0800218 output += "Public key: " + publickeyHex;
jeffcc8b3a92012-09-03 15:13:27 -0700219
220 output+= "<br />";
221 output+= "<br />";
222
223 if(LOG>2) console.log(" ContentName + SignedInfo + Content = "+input);
jeffcc8b3a92012-09-03 15:13:27 -0700224 if(LOG>2) console.log(" PublicKeyHex = "+publickeyHex );
225 if(LOG>2) console.log(" PublicKeyString = "+publickeyString );
226
227 if(LOG>2) console.log(" Signature "+signature );
Wentao Shangfddf90d2013-01-05 17:18:49 -0800228 if(LOG>2) console.log(" Witness "+witHex );
jeffcc8b3a92012-09-03 15:13:27 -0700229
230 if(LOG>2) console.log(" Signature NOW IS" );
231
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700232 if(LOG>2) console.log(co.signature.signature);
Jeff Thompson68fccd62012-12-29 17:38:23 -0800233
234 var rsakey = decodeSubjectPublicKeyInfo(co.signedInfo.locator.publicKey);
jeffcc8b3a92012-09-03 15:13:27 -0700235
Jeff Thompson68fccd62012-12-29 17:38:23 -0800236 output += "Public key (hex) modulus: " + rsakey.n.toString(16) + "<br/>";
237 output += "exponent: " + rsakey.e.toString(16) + "<br/>";
238 output += "<br/>";
239
Wentao Shangfddf90d2013-01-05 17:18:49 -0800240 var result = rsakey.verifyByteArray(co.rawSignatureData, wit, signature);
jeffcc8b3a92012-09-03 15:13:27 -0700241 // var result = rsakey.verifyString(input, signature);
242
Wentao Shang2b740e62012-12-07 00:02:53 -0800243 if(LOG>2) console.log('PUBLIC KEY n after is ');
244 if(LOG>2) console.log(rsakey.n);
jeffcc8b3a92012-09-03 15:13:27 -0700245
Wentao Shang2b740e62012-12-07 00:02:53 -0800246 if(LOG>2) console.log('EXPONENT e after is ');
247 if(LOG>2) console.log(rsakey.e);
jeffcc8b3a92012-09-03 15:13:27 -0700248
249 if(result)
Wentao Shangfddf90d2013-01-05 17:18:49 -0800250 output += 'SIGNATURE VALID';
jeffcc8b3a92012-09-03 15:13:27 -0700251 else
Wentao Shangfddf90d2013-01-05 17:18:49 -0800252 output += 'SIGNATURE INVALID';
jeffcc8b3a92012-09-03 15:13:27 -0700253
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700254 //output += "VALID: "+ toHex(co.signedInfo.locator.publicKey);
jeffcc8b3a92012-09-03 15:13:27 -0700255
256 output+= "<br />";
257 output+= "<br />";
258
259 //if(LOG>4) console.log('str'[1]);
260 }
261 }
262
263 return output;
264}
Jeff Thompson68fccd62012-12-29 17:38:23 -0800265
266