blob: 7c66f3c894d60eca06bb43f5c1c0bbd64c1bb542 [file] [log] [blame]
Wentao Shangbd63e462012-12-03 16:19:33 -08001/**
Jeff Thompson146d7de2012-11-17 16:15:28 -08002 * @author: Meki Cheraoui
Jeff Thompson745026e2012-10-13 12:49:20 -07003 * See COPYING for copyright and distribution information.
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07004 * This class represents ContentObject Objects
5 */
Jeff Thompson86aea882012-09-29 17:32:48 -07006var ContentObject = function ContentObject(_name,_signedInfo,_content,_signature){
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07007
8
Wentao Shangab9018d2012-12-18 11:35:45 -08009 if (typeof _name == 'string') {
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070010 this.name = new Name(_name);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070011 }
12 else{
Jeff Thompson86aea882012-09-29 17:32:48 -070013 //TODO Check the class of _name
14 this.name = _name;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070015 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070016 this.signedInfo = _signedInfo;
Wentao Shangab9018d2012-12-18 11:35:45 -080017
18 if (typeof _content == 'string') {
19 this.content = DataUtils.toNumbersFromString(_content);
20 } else {
21 this.content = _content;
22 }
23
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070024 this.signature = _signature;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070025
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070026
Jeff Thompson86aea882012-09-29 17:32:48 -070027 this.startSIG = null;
28 this.endSIG = null;
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070029
Wentao Shangfddf90d2013-01-05 17:18:49 -080030 //this.startSignedInfo = null;
Jeff Thompson86aea882012-09-29 17:32:48 -070031 this.endContent = null;
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070032
33 this.rawSignatureData = null;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070034};
35
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070036ContentObject.prototype.sign = function(){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070037
Jeff Thompson86aea882012-09-29 17:32:48 -070038 var n1 = this.encodeObject(this.name);
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070039 var n2 = this.encodeObject(this.signedInfo);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070040 var n3 = this.encodeContent();
Jeff Thompson3d2393f2012-11-11 19:11:51 -080041 /*console.log('sign: ');
42 console.log(n1);
43 console.log(n2);
44 console.log(n3);*/
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070045
Jeff Thompson3d2393f2012-11-11 19:11:51 -080046 //var n = n1.concat(n2,n3);
47 var tempBuf = new ArrayBuffer(n1.length + n2.length + n3.length);
48 var n = new Uint8Array(tempBuf);
49 //console.log(n);
50 n.set(n1, 0);
51 //console.log(n);
52 n.set(n2, n1.length);
53 //console.log(n);
54 n.set(n3, n1.length + n2.length);
55 //console.log(n);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070056
Jeff Thompson3d2393f2012-11-11 19:11:51 -080057 if(LOG>4)console.log('Signature Data is (binary) '+n);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070058
Jeff Thompson3d2393f2012-11-11 19:11:51 -080059 if(LOG>4)console.log('Signature Data is (RawString)');
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070060
Jeff Thompson3d2393f2012-11-11 19:11:51 -080061 if(LOG>4)console.log( DataUtils.toString(n) );
Meki Cherkaoui8f173612012-06-06 01:05:40 -070062
Jeff Thompson3d2393f2012-11-11 19:11:51 -080063 //var sig = DataUtils.toString(n);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070064
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070065
66 var rsa = new RSAKey();
67
68 rsa.readPrivateKeyFromPEMString(globalKeyManager.privateKey);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070069
70 //var hSig = rsa.signString(sig, "sha256");
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070071
Meki Cherkaoui8f173612012-06-06 01:05:40 -070072 var hSig = rsa.signByteArrayWithSHA256(n);
73
74
Jeff Thompson3d2393f2012-11-11 19:11:51 -080075 if(LOG>4)console.log('SIGNATURE SAVED IS');
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070076
Jeff Thompson3d2393f2012-11-11 19:11:51 -080077 if(LOG>4)console.log(hSig);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070078
Jeff Thompson3d2393f2012-11-11 19:11:51 -080079 if(LOG>4)console.log( DataUtils.toNumbers(hSig.trim()));
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070080
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070081 this.signature.signature = DataUtils.toNumbers(hSig.trim());
Meki Cherkaoui8f173612012-06-06 01:05:40 -070082
83
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070084};
85
86ContentObject.prototype.encodeObject = function encodeObject(obj){
87 var enc = new BinaryXMLEncoder();
88
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070089 obj.to_ccnb(enc);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070090
91 var num = enc.getReducedOstream();
92
93 return num;
94
95
96};
97
98ContentObject.prototype.encodeContent = function encodeContent(obj){
99 var enc = new BinaryXMLEncoder();
100
Jeff Thompson86aea882012-09-29 17:32:48 -0700101 enc.writeElement(CCNProtocolDTags.Content, this.content);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700102
103 var num = enc.getReducedOstream();
104
105 return num;
106
107
108};
109
110ContentObject.prototype.saveRawData = function(bytes){
111
Jeff Thompson3d2393f2012-11-11 19:11:51 -0800112 var sigBits = bytes.subarray(this.startSIG, this.endSIG);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700113
114 this.rawSignatureData = sigBits;
115};
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700116
Jeff Thompson86bcd022013-07-26 17:55:03 -0700117// Deprecated. Use BinaryXMLWireFormat.decodeContentObject.
Jeff Thompson86aea882012-09-29 17:32:48 -0700118ContentObject.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) {
Jeff Thompson86bcd022013-07-26 17:55:03 -0700119 BinaryXMLWireFormat.decodeContentObject(this, decoder);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700120};
121
Jeff Thompson86bcd022013-07-26 17:55:03 -0700122// Deprecated. Use BinaryXMLWireFormat.encodeContentObject.
Jeff Thompson86aea882012-09-29 17:32:48 -0700123ContentObject.prototype.to_ccnb = function(/*XMLEncoder*/ encoder) {
Jeff Thompson86bcd022013-07-26 17:55:03 -0700124 BinaryXMLWireFormat.encodeContentObject(this, encoder);
125};
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700126
Jeff Thompson86bcd022013-07-26 17:55:03 -0700127/**
128 * Encode this ContentObject for a particular wire format.
129 * @param {WireFormat} wireFormat if null, use BinaryXMLWireFormat.
130 * @returns {Uint8Array}
131 */
132ContentObject.prototype.encode = function(wireFormat) {
133 wireFormat = (wireFormat || BinaryXMLWireFormat.instance);
134 return wireFormat.encodeContentObject(this);
135};
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700136
Jeff Thompson86bcd022013-07-26 17:55:03 -0700137/**
138 * Decode the input using a particular wire format and update this ContentObject.
139 * @param {Uint8Array} input
140 * @param {WireFormat} wireFormat if null, use BinaryXMLWireFormat.
141 */
142ContentObject.prototype.decode = function(input, wireFormat) {
143 wireFormat = (wireFormat || BinaryXMLWireFormat.instance);
144 wireFormat.decodeContentObject(this, input);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700145};
146
147ContentObject.prototype.getElementLabel= function(){return CCNProtocolDTags.ContentObject;};
Jeff Thompsonb9ce4582012-09-30 17:52:51 -0700148
149/**
150 * Signature
151 */
152var Signature = function Signature(_witness,_signature,_digestAlgorithm) {
153
154 this.Witness = _witness;//byte [] _witness;
155 this.signature = _signature;//byte [] _signature;
156 this.digestAlgorithm = _digestAlgorithm//String _digestAlgorithm;
157};
158
Jeff Thompsonb9ce4582012-09-30 17:52:51 -0700159Signature.prototype.from_ccnb =function( decoder) {
160 decoder.readStartElement(this.getElementLabel());
161
Wentao Shang882e34e2013-01-05 02:49:51 -0800162 if(LOG>4)console.log('STARTED DECODING SIGNATURE');
Jeff Thompsonb9ce4582012-09-30 17:52:51 -0700163
164 if (decoder.peekStartElement(CCNProtocolDTags.DigestAlgorithm)) {
Jeff Thompsonb9ce4582012-09-30 17:52:51 -0700165 if(LOG>4)console.log('DIGIEST ALGORITHM FOUND');
166 this.digestAlgorithm = decoder.readUTF8Element(CCNProtocolDTags.DigestAlgorithm);
167 }
168 if (decoder.peekStartElement(CCNProtocolDTags.Witness)) {
Wentao Shang882e34e2013-01-05 02:49:51 -0800169 if(LOG>4)console.log('WITNESS FOUND');
Jeff Thompsonb9ce4582012-09-30 17:52:51 -0700170 this.Witness = decoder.readBinaryElement(CCNProtocolDTags.Witness);
171 }
172
173 //FORCE TO READ A SIGNATURE
174
Wentao Shang882e34e2013-01-05 02:49:51 -0800175 if(LOG>4)console.log('SIGNATURE FOUND');
176 this.signature = decoder.readBinaryElement(CCNProtocolDTags.SignatureBits);
Jeff Thompsonb9ce4582012-09-30 17:52:51 -0700177
178 decoder.readEndElement();
179
180};
181
182
183Signature.prototype.to_ccnb= function( encoder){
184
185 if (!this.validate()) {
186 throw new Error("Cannot encode: field values missing.");
187 }
188
189 encoder.writeStartElement(this.getElementLabel());
190
191 if ((null != this.digestAlgorithm) && (!this.digestAlgorithm.equals(CCNDigestHelper.DEFAULT_DIGEST_ALGORITHM))) {
192 encoder.writeElement(CCNProtocolDTags.DigestAlgorithm, OIDLookup.getDigestOID(this.DigestAlgorithm));
193 }
194
195 if (null != this.Witness) {
196 // needs to handle null witness
197 encoder.writeElement(CCNProtocolDTags.Witness, this.Witness);
198 }
199
200 encoder.writeElement(CCNProtocolDTags.SignatureBits, this.signature);
201
202 encoder.writeEndElement();
203};
204
205Signature.prototype.getElementLabel = function() { return CCNProtocolDTags.Signature; };
206
207
208Signature.prototype.validate = function() {
209 return null != this.signature;
210};
211
212
213/**
214 * SignedInfo
215 */
216var ContentType = {DATA:0, ENCR:1, GONE:2, KEY:3, LINK:4, NACK:5};
217var ContentTypeValue = {0:0x0C04C0, 1:0x10D091,2:0x18E344,3:0x28463F,4:0x2C834A,5:0x34008A};
218var ContentTypeValueReverse = {0x0C04C0:0, 0x10D091:1,0x18E344:2,0x28463F:3,0x2C834A:4,0x34008A:5};
219
220var SignedInfo = function SignedInfo(_publisher,_timestamp,_type,_locator,_freshnessSeconds,_finalBlockID){
221
222 //TODO, Check types
223
224 this.publisher = _publisher; //publisherPublicKeyDigest
225 this.timestamp=_timestamp; // CCN Time
226 this.type=_type; // ContentType
227 this.locator =_locator;//KeyLocator
228 this.freshnessSeconds =_freshnessSeconds; // Integer
229 this.finalBlockID=_finalBlockID; //byte array
Wentao Shangab9018d2012-12-18 11:35:45 -0800230
231 // SWT: merge setFields() method into constructor
232 this.setFields();
Jeff Thompsonb9ce4582012-09-30 17:52:51 -0700233
234};
235
236SignedInfo.prototype.setFields = function(){
237 //BASE64 -> RAW STRING
238
239 //this.locator = new KeyLocator( DataUtils.toNumbersFromString(stringCertificate) ,KeyLocatorType.CERTIFICATE );
240
241 var publicKeyHex = globalKeyManager.publicKey;
242
Jeff Thompson3d2393f2012-11-11 19:11:51 -0800243 if(LOG>4)console.log('PUBLIC KEY TO WRITE TO CONTENT OBJECT IS ');
244 if(LOG>4)console.log(publicKeyHex);
Jeff Thompsonb9ce4582012-09-30 17:52:51 -0700245
246 var publicKeyBytes = DataUtils.toNumbers(globalKeyManager.publicKey) ;
247
248
249
250 //var stringCertificate = DataUtils.base64toString(globalKeyManager.certificate);
251
252 //if(LOG>3)console.log('string Certificate is '+stringCertificate);
253
254 //HEX -> BYTE ARRAY
255 //var publisherkey = DataUtils.toNumbers(hex_sha256(stringCertificate));
256
257 //if(LOG>3)console.log('publisher key is ');
258 //if(LOG>3)console.log(publisherkey);
259
260 var publisherKeyDigest = hex_sha256_from_bytes(publicKeyBytes);
261
262 this.publisher = new PublisherPublicKeyDigest( DataUtils.toNumbers( publisherKeyDigest ) );
263
264 //this.publisher = new PublisherPublicKeyDigest(publisherkey);
265
266 var d = new Date();
267
268 var time = d.getTime();
269
270
271 this.timestamp = new CCNTime( time );
272
273 if(LOG>4)console.log('TIME msec is');
274
275 if(LOG>4)console.log(this.timestamp.msec);
276
277 //DATA
278 this.type = 0;//0x0C04C0;//ContentTypeValue[ContentType.DATA];
279
280 //if(LOG>4)console.log('toNumbersFromString(stringCertificate) '+DataUtils.toNumbersFromString(stringCertificate));
281
Jeff Thompson3d2393f2012-11-11 19:11:51 -0800282 if(LOG>4)console.log('PUBLIC KEY TO WRITE TO CONTENT OBJECT IS ');
283 if(LOG>4)console.log(publicKeyBytes);
Jeff Thompsonb9ce4582012-09-30 17:52:51 -0700284
285 this.locator = new KeyLocator( publicKeyBytes ,KeyLocatorType.KEY );
286
287 //this.locator = new KeyLocator( DataUtils.toNumbersFromString(stringCertificate) ,KeyLocatorType.CERTIFICATE );
288
289};
290
291SignedInfo.prototype.from_ccnb = function( decoder){
292
293 decoder.readStartElement( this.getElementLabel() );
294
295 if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) {
Wentao Shang882e34e2013-01-05 02:49:51 -0800296 if(LOG>4)console.log('DECODING PUBLISHER KEY');
Jeff Thompsonb9ce4582012-09-30 17:52:51 -0700297 this.publisher = new PublisherPublicKeyDigest();
298 this.publisher.from_ccnb(decoder);
299 }
300
301 if (decoder.peekStartElement(CCNProtocolDTags.Timestamp)) {
Wentao Shang882e34e2013-01-05 02:49:51 -0800302 if(LOG>4)console.log('DECODING TIMESTAMP');
Jeff Thompsonb9ce4582012-09-30 17:52:51 -0700303 this.timestamp = decoder.readDateTime(CCNProtocolDTags.Timestamp);
Jeff Thompsonb9ce4582012-09-30 17:52:51 -0700304 }
305
306 if (decoder.peekStartElement(CCNProtocolDTags.Type)) {
Jeff Thompson48ff28a2013-02-18 22:53:29 -0800307 var binType = decoder.readBinaryElement(CCNProtocolDTags.Type);//byte []
Jeff Thompsonb9ce4582012-09-30 17:52:51 -0700308
309
310 //TODO Implement type of Key Reading
311
312 if(LOG>4)console.log('Binary Type of of Signed Info is '+binType);
313
314 this.type = binType;
315
316
317 //TODO Implement type of Key Reading
318
319
320 if (null == this.type) {
321 throw new Error("Cannot parse signedInfo type: bytes.");
322 }
323
324 } else {
325 this.type = ContentType.DATA; // default
326 }
327
328 if (decoder.peekStartElement(CCNProtocolDTags.FreshnessSeconds)) {
329 this.freshnessSeconds = decoder.readIntegerElement(CCNProtocolDTags.FreshnessSeconds);
Wentao Shang882e34e2013-01-05 02:49:51 -0800330 if(LOG>4)console.log('FRESHNESS IN SECONDS IS '+ this.freshnessSeconds);
Jeff Thompsonb9ce4582012-09-30 17:52:51 -0700331 }
332
333 if (decoder.peekStartElement(CCNProtocolDTags.FinalBlockID)) {
Wentao Shang882e34e2013-01-05 02:49:51 -0800334 if(LOG>4)console.log('DECODING FINAL BLOCKID');
Jeff Thompsonb9ce4582012-09-30 17:52:51 -0700335 this.finalBlockID = decoder.readBinaryElement(CCNProtocolDTags.FinalBlockID);
336 }
337
338 if (decoder.peekStartElement(CCNProtocolDTags.KeyLocator)) {
Wentao Shang882e34e2013-01-05 02:49:51 -0800339 if(LOG>4)console.log('DECODING KEY LOCATOR');
Jeff Thompsonb9ce4582012-09-30 17:52:51 -0700340 this.locator = new KeyLocator();
341 this.locator.from_ccnb(decoder);
342 }
343
344 decoder.readEndElement();
345};
346
347SignedInfo.prototype.to_ccnb = function( encoder) {
348 if (!this.validate()) {
349 throw new Error("Cannot encode : field values missing.");
350 }
351 encoder.writeStartElement(this.getElementLabel());
352
353 if (null!=this.publisher) {
354 if(LOG>3) console.log('ENCODING PUBLISHER KEY' + this.publisher.publisherPublicKeyDigest);
355
356 this.publisher.to_ccnb(encoder);
357 }
358
359 if (null!=this.timestamp) {
360 encoder.writeDateTime(CCNProtocolDTags.Timestamp, this.timestamp );
361 }
362
363 if (null!=this.type && this.type !=0) {
364
365 encoder.writeElement(CCNProtocolDTags.type, this.type);
366 }
367
368 if (null!=this.freshnessSeconds) {
369 encoder.writeElement(CCNProtocolDTags.FreshnessSeconds, this.freshnessSeconds);
370 }
371
372 if (null!=this.finalBlockID) {
373 encoder.writeElement(CCNProtocolDTags.FinalBlockID, this.finalBlockID);
374 }
375
376 if (null!=this.locator) {
377 this.locator.to_ccnb(encoder);
378 }
379
380 encoder.writeEndElement();
381};
382
383SignedInfo.prototype.valueToType = function(){
384 //for (Entry<byte [], ContentType> entry : ContentValueTypes.entrySet()) {
385 //if (Arrays.equals(value, entry.getKey()))
386 //return entry.getValue();
387 //}
388 return null;
389
390};
391
392SignedInfo.prototype.getElementLabel = function() {
393 return CCNProtocolDTags.SignedInfo;
394};
395
396SignedInfo.prototype.validate = function() {
397 // We don't do partial matches any more, even though encoder/decoder
398 // is still pretty generous.
399 if (null ==this.publisher || null==this.timestamp ||null== this.locator)
400 return false;
401 return true;
402};