blob: 5ac4d2841274c95b6cfb4486ebb3f5416c9164fe [file] [log] [blame]
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07001/*
2 * @author: ucla-cs
3 * This class represents ContentObject Objects
4 */
Jeff Thompson86aea882012-09-29 17:32:48 -07005var ContentObject = function ContentObject(_name,_signedInfo,_content,_signature){
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07006
7
Jeff Thompson86aea882012-09-29 17:32:48 -07008 if (typeof _name === 'string'){
9 this.name = new ContentName(_name);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070010 }
11 else{
Jeff Thompson86aea882012-09-29 17:32:48 -070012 //TODO Check the class of _name
13 this.name = _name;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070014 }
Jeff Thompson86aea882012-09-29 17:32:48 -070015 this.SignedInfo = _signedInfo;
16 this.content=_content;
17 this.Signature = _signature;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070018
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070019
Jeff Thompson86aea882012-09-29 17:32:48 -070020 this.startSIG = null;
21 this.endSIG = null;
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070022
Jeff Thompson86aea882012-09-29 17:32:48 -070023 this.startSignedInfo = null;
24 this.endContent = null;
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070025
26 this.rawSignatureData = null;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070027};
28
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070029ContentObject.prototype.sign = function(){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070030
Jeff Thompson86aea882012-09-29 17:32:48 -070031 var n1 = this.encodeObject(this.name);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070032 var n2 = this.encodeObject(this.SignedInfo);
33 var n3 = this.encodeContent();
34
35 var n = n1.concat(n2,n3);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070036
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070037 if(LOG>2)console.log('Signature Data is (binary) '+n);
38
Meki Cherkaoui8f173612012-06-06 01:05:40 -070039 if(LOG>2)console.log('Signature Data is (RawString)');
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070040
Meki Cherkaoui8f173612012-06-06 01:05:40 -070041 if(LOG>2)console.log( DataUtils.toString(n) );
42
43 var sig = DataUtils.toString(n);
44
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070045
46 var rsa = new RSAKey();
47
48 rsa.readPrivateKeyFromPEMString(globalKeyManager.privateKey);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070049
50 //var hSig = rsa.signString(sig, "sha256");
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070051
Meki Cherkaoui8f173612012-06-06 01:05:40 -070052 var hSig = rsa.signByteArrayWithSHA256(n);
53
54
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070055 if(LOG>2)console.log('SIGNATURE SAVED IS');
56
57 if(LOG>2)console.log(hSig);
58
Meki Cherkaoui8f173612012-06-06 01:05:40 -070059 if(LOG>2)console.log( DataUtils.toNumbers(hSig.trim()));
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070060
Meki Cherkaoui8f173612012-06-06 01:05:40 -070061 this.Signature.Signature = DataUtils.toNumbers(hSig.trim());
62
63
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070064};
65
66ContentObject.prototype.encodeObject = function encodeObject(obj){
67 var enc = new BinaryXMLEncoder();
68
69 obj.encode(enc);
70
71 var num = enc.getReducedOstream();
72
73 return num;
74
75
76};
77
78ContentObject.prototype.encodeContent = function encodeContent(obj){
79 var enc = new BinaryXMLEncoder();
80
Jeff Thompson86aea882012-09-29 17:32:48 -070081 enc.writeElement(CCNProtocolDTags.Content, this.content);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070082
83 var num = enc.getReducedOstream();
84
85 return num;
86
87
88};
89
90ContentObject.prototype.saveRawData = function(bytes){
91
Jeff Thompson86aea882012-09-29 17:32:48 -070092 var sigBits = bytes.slice(this.startSIG, this.endSIG );
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070093
94 this.rawSignatureData = sigBits;
95};
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070096
Jeff Thompson86aea882012-09-29 17:32:48 -070097ContentObject.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070098
Meki Cherkaoui8f173612012-06-06 01:05:40 -070099 // TODO VALIDATE THAT ALL FIELDS EXCEPT SIGNATURE ARE PRESENT
100
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700101 decoder.readStartElement(this.getElementLabel());
102
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700103
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700104 if( decoder.peekStartElement(CCNProtocolDTags.Signature) ){
105 this.Signature = new Signature();
106 this.Signature.decode(decoder);
107 }
108
Jeff Thompson86aea882012-09-29 17:32:48 -0700109 //this.endSIG = decoder.offset;
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700110
Jeff Thompson86aea882012-09-29 17:32:48 -0700111 this.startSIG = decoder.offset;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700112
Jeff Thompson86aea882012-09-29 17:32:48 -0700113 this.name = new ContentName();
114 this.name.decode(decoder);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700115
Jeff Thompson86aea882012-09-29 17:32:48 -0700116 //this.startSignedInfo = decoder.offset;
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700117
118
119 if( decoder.peekStartElement(CCNProtocolDTags.SignedInfo) ){
120 this.SignedInfo = new SignedInfo();
121 this.SignedInfo.decode(decoder);
122 }
123
Jeff Thompson86aea882012-09-29 17:32:48 -0700124 this.content = decoder.readBinaryElement(CCNProtocolDTags.Content);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700125
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700126
Jeff Thompson86aea882012-09-29 17:32:48 -0700127 //this.endContent = decoder.offset;
128 this.endSIG = decoder.offset;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700129
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700130
131 decoder.readEndElement();
132
133 this.saveRawData(decoder.istream);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700134};
135
Jeff Thompson86aea882012-09-29 17:32:48 -0700136ContentObject.prototype.to_ccnb = function(/*XMLEncoder*/ encoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700137
Jeff Thompson86aea882012-09-29 17:32:48 -0700138 //TODO verify name, SignedInfo and Signature is present
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700139
140
141 encoder.writeStartElement(this.getElementLabel());
142
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700143
144
145
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700146 if(null!=this.Signature) this.Signature.encode(encoder);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700147
148
Jeff Thompson86aea882012-09-29 17:32:48 -0700149 this.startSIG = encoder.offset;
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700150
151
Jeff Thompson86aea882012-09-29 17:32:48 -0700152 if(null!=this.name) this.name.encode(encoder);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700153
Jeff Thompson86aea882012-09-29 17:32:48 -0700154 //this.endSIG = encoder.offset;
155 //this.startSignedInfo = encoder.offset;
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700156
157
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700158 if(null!=this.SignedInfo) this.SignedInfo.encode(encoder);
159
Jeff Thompson86aea882012-09-29 17:32:48 -0700160 encoder.writeElement(CCNProtocolDTags.Content, this.content);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700161
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700162
Jeff Thompson86aea882012-09-29 17:32:48 -0700163 this.endSIG = encoder.offset;
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700164
Jeff Thompson86aea882012-09-29 17:32:48 -0700165 //this.endContent = encoder.offset;
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700166
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700167
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700168 encoder.writeEndElement();
169
170 this.saveRawData(encoder.ostream);
171
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700172};
173
174ContentObject.prototype.getElementLabel= function(){return CCNProtocolDTags.ContentObject;};