blob: e120a984ed0e64049bac9dabfe68d13246cac391 [file] [log] [blame]
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07001/*
2 * @author: ucla-cs
3 * This class represents ContentObject Objects
4 */
5var ContentObject = function ContentObject(_Name,_SignedInfo,_Content,_Signature){
6
7
8 if (typeof _Name === 'string'){
Meki Cherkaouif3d8f692012-05-18 15:44:28 -07009 this.Name = new ContentName(_Name);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070010 }
11 else{
12 //TODO Check the class of _Name
13 this.Name = _Name;
14 }
15 this.SignedInfo = _SignedInfo;
16 this.Content=_Content;
17 this.Signature = _Signature;
18
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070019
20 this.StartSIG = null;
21 this.EndSIG = null;
22
23 this.StartSignedInfo = null;
24 this.EndContent = null;
25
26 this.rawSignatureData = null;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070027};
28
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070029ContentObject.prototype.sign = function(){
30 var n1 = this.encodeObject(this.Name);
31 var n2 = this.encodeObject(this.SignedInfo);
32 var n3 = this.encodeContent();
33
34 var n = n1.concat(n2,n3);
35 if(LOG>2)console.log('Signature Data is (binary) '+n);
36
37 if(LOG>2)console.log('Signature Data is (RawString) '+ toString(n) );
38 if(LOG>2)console.log(toString(n) );
39
40
41 var rsa = new RSAKey();
42
43 rsa.readPrivateKeyFromPEMString(globalKeyManager.privateKey);
44
45 var hSig = rsa.signString(toString(n), "sha256");
46
47 if(LOG>2)console.log('SIGNATURE SAVED IS');
48
49 if(LOG>2)console.log(hSig);
50
51 if(LOG>2)console.log(toNumbers(hSig.trim()));
52
53 this.Signature.Signature = toNumbers(hSig.trim());
54};
55
56ContentObject.prototype.encodeObject = function encodeObject(obj){
57 var enc = new BinaryXMLEncoder();
58
59 obj.encode(enc);
60
61 var num = enc.getReducedOstream();
62
63 return num;
64
65
66};
67
68ContentObject.prototype.encodeContent = function encodeContent(obj){
69 var enc = new BinaryXMLEncoder();
70
71 enc.writeElement(CCNProtocolDTags.Content, this.Content);
72
73 var num = enc.getReducedOstream();
74
75 return num;
76
77
78};
79
80ContentObject.prototype.saveRawData = function(bytes){
81
82 var sigBits = bytes.slice(this.StartSIG, this.EndSIG );
83
84 //var sigIngoPlusContentBits = bytes.slice(this.StartSignedInfo, this.EndContent );
85
86 //var concat = sigBits.concat(sigIngoPlusContentBits);
87
88 //this.rawSignatureData = concat;
89
90 this.rawSignatureData = sigBits;
91};
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070092
93ContentObject.prototype.decode = function(/*XMLDecoder*/ decoder) {
94
95 decoder.readStartElement(this.getElementLabel());
96
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070097
98 if( decoder.peekStartElement(CCNProtocolDTags.Signature) ){
99 this.Signature = new Signature();
100 this.Signature.decode(decoder);
101 }
102
103 //this.EndSIG = decoder.offset;
104
105 this.StartSIG = decoder.offset;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700106
107 this.Name = new ContentName();
108 this.Name.decode(decoder);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700109
110 //this.StartSignedInfo = decoder.offset;
111
112
113 if( decoder.peekStartElement(CCNProtocolDTags.SignedInfo) ){
114 this.SignedInfo = new SignedInfo();
115 this.SignedInfo.decode(decoder);
116 }
117
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700118 this.Content = decoder.readBinaryElement(CCNProtocolDTags.Content);
119
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700120
121 //this.EndContent = decoder.offset;
122 this.EndSIG = decoder.offset;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700123
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700124
125 decoder.readEndElement();
126
127 this.saveRawData(decoder.istream);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700128};
129
130ContentObject.prototype.encode = function(/*XMLEncoder*/ encoder) {
131
132 //TODO verify Name, SignedInfo and Signature is present
133
134
135 encoder.writeStartElement(this.getElementLabel());
136
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700137
138
139
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700140 if(null!=this.Signature) this.Signature.encode(encoder);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700141
142
143 this.StartSIG = encoder.offset;
144
145
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700146 if(null!=this.Name) this.Name.encode(encoder);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700147
148 //this.EndSIG = encoder.offset;
149 //this.StartSignedInfo = encoder.offset;
150
151
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700152 if(null!=this.SignedInfo) this.SignedInfo.encode(encoder);
153
154 encoder.writeElement(CCNProtocolDTags.Content, this.Content);
155
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700156
157 this.EndSIG = encoder.offset;
158
159 //this.EndContent = encoder.offset;
160
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700161
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700162 encoder.writeEndElement();
163
164 this.saveRawData(encoder.ostream);
165
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700166};
167
168ContentObject.prototype.getElementLabel= function(){return CCNProtocolDTags.ContentObject;};