blob: 0d58ba6cc6ed93722dd79d5ff683eaf55473856f [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(){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070030
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070031 var n1 = this.encodeObject(this.Name);
32 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
81 enc.writeElement(CCNProtocolDTags.Content, this.Content);
82
83 var num = enc.getReducedOstream();
84
85 return num;
86
87
88};
89
90ContentObject.prototype.saveRawData = function(bytes){
91
92 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
97ContentObject.prototype.decode = function(/*XMLDecoder*/ decoder) {
98
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
109 //this.EndSIG = decoder.offset;
110
111 this.StartSIG = decoder.offset;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700112
113 this.Name = new ContentName();
114 this.Name.decode(decoder);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700115
116 //this.StartSignedInfo = decoder.offset;
117
118
119 if( decoder.peekStartElement(CCNProtocolDTags.SignedInfo) ){
120 this.SignedInfo = new SignedInfo();
121 this.SignedInfo.decode(decoder);
122 }
123
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700124 this.Content = decoder.readBinaryElement(CCNProtocolDTags.Content);
125
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700126
127 //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
136ContentObject.prototype.encode = function(/*XMLEncoder*/ encoder) {
137
138 //TODO verify Name, SignedInfo and Signature is present
139
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
149 this.StartSIG = encoder.offset;
150
151
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700152 if(null!=this.Name) this.Name.encode(encoder);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700153
154 //this.EndSIG = encoder.offset;
155 //this.StartSignedInfo = encoder.offset;
156
157
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700158 if(null!=this.SignedInfo) this.SignedInfo.encode(encoder);
159
160 encoder.writeElement(CCNProtocolDTags.Content, this.Content);
161
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700162
163 this.EndSIG = encoder.offset;
164
165 //this.EndContent = encoder.offset;
166
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;};