Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 1 | /* |
2 | * @author: ucla-cs | ||||
3 | * This class represents ContentObject Objects | ||||
4 | */ | ||||
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 5 | var ContentObject = function ContentObject(_name,_signedInfo,_content,_signature){ |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 6 | |
7 | |||||
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 8 | if (typeof _name === 'string'){ |
9 | this.name = new ContentName(_name); | ||||
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 10 | } |
11 | else{ | ||||
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 12 | //TODO Check the class of _name |
13 | this.name = _name; | ||||
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 14 | } |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 15 | this.SignedInfo = _signedInfo; |
16 | this.content=_content; | ||||
17 | this.Signature = _signature; | ||||
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 18 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 19 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 20 | this.startSIG = null; |
21 | this.endSIG = null; | ||||
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 22 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 23 | this.startSignedInfo = null; |
24 | this.endContent = null; | ||||
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 25 | |
26 | this.rawSignatureData = null; | ||||
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 27 | }; |
28 | |||||
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 29 | ContentObject.prototype.sign = function(){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 30 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 31 | var n1 = this.encodeObject(this.name); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 32 | var n2 = this.encodeObject(this.SignedInfo); |
33 | var n3 = this.encodeContent(); | ||||
34 | |||||
35 | var n = n1.concat(n2,n3); | ||||
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 36 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 37 | if(LOG>2)console.log('Signature Data is (binary) '+n); |
38 | |||||
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 39 | if(LOG>2)console.log('Signature Data is (RawString)'); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 40 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 41 | if(LOG>2)console.log( DataUtils.toString(n) ); |
42 | |||||
43 | var sig = DataUtils.toString(n); | ||||
44 | |||||
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 45 | |
46 | var rsa = new RSAKey(); | ||||
47 | |||||
48 | rsa.readPrivateKeyFromPEMString(globalKeyManager.privateKey); | ||||
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 49 | |
50 | //var hSig = rsa.signString(sig, "sha256"); | ||||
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 51 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 52 | var hSig = rsa.signByteArrayWithSHA256(n); |
53 | |||||
54 | |||||
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 55 | if(LOG>2)console.log('SIGNATURE SAVED IS'); |
56 | |||||
57 | if(LOG>2)console.log(hSig); | ||||
58 | |||||
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 59 | if(LOG>2)console.log( DataUtils.toNumbers(hSig.trim())); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 60 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 61 | this.Signature.Signature = DataUtils.toNumbers(hSig.trim()); |
62 | |||||
63 | |||||
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 64 | }; |
65 | |||||
66 | ContentObject.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 | |||||
78 | ContentObject.prototype.encodeContent = function encodeContent(obj){ | ||||
79 | var enc = new BinaryXMLEncoder(); | ||||
80 | |||||
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 81 | enc.writeElement(CCNProtocolDTags.Content, this.content); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 82 | |
83 | var num = enc.getReducedOstream(); | ||||
84 | |||||
85 | return num; | ||||
86 | |||||
87 | |||||
88 | }; | ||||
89 | |||||
90 | ContentObject.prototype.saveRawData = function(bytes){ | ||||
91 | |||||
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 92 | var sigBits = bytes.slice(this.startSIG, this.endSIG ); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 93 | |
94 | this.rawSignatureData = sigBits; | ||||
95 | }; | ||||
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 96 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 97 | ContentObject.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 98 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 99 | // TODO VALIDATE THAT ALL FIELDS EXCEPT SIGNATURE ARE PRESENT |
100 | |||||
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 101 | decoder.readStartElement(this.getElementLabel()); |
102 | |||||
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 103 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 104 | if( decoder.peekStartElement(CCNProtocolDTags.Signature) ){ |
105 | this.Signature = new Signature(); | ||||
106 | this.Signature.decode(decoder); | ||||
107 | } | ||||
108 | |||||
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 109 | //this.endSIG = decoder.offset; |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 110 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 111 | this.startSIG = decoder.offset; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 112 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 113 | this.name = new ContentName(); |
114 | this.name.decode(decoder); | ||||
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 115 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 116 | //this.startSignedInfo = decoder.offset; |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 117 | |
118 | |||||
119 | if( decoder.peekStartElement(CCNProtocolDTags.SignedInfo) ){ | ||||
120 | this.SignedInfo = new SignedInfo(); | ||||
121 | this.SignedInfo.decode(decoder); | ||||
122 | } | ||||
123 | |||||
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 124 | this.content = decoder.readBinaryElement(CCNProtocolDTags.Content); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 125 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 126 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 127 | //this.endContent = decoder.offset; |
128 | this.endSIG = decoder.offset; | ||||
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 129 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 130 | |
131 | decoder.readEndElement(); | ||||
132 | |||||
133 | this.saveRawData(decoder.istream); | ||||
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 134 | }; |
135 | |||||
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 136 | ContentObject.prototype.to_ccnb = function(/*XMLEncoder*/ encoder) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 137 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 138 | //TODO verify name, SignedInfo and Signature is present |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 139 | |
140 | |||||
141 | encoder.writeStartElement(this.getElementLabel()); | ||||
142 | |||||
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 143 | |
144 | |||||
145 | |||||
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 146 | if(null!=this.Signature) this.Signature.encode(encoder); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 147 | |
148 | |||||
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 149 | this.startSIG = encoder.offset; |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 150 | |
151 | |||||
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 152 | if(null!=this.name) this.name.encode(encoder); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 153 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 154 | //this.endSIG = encoder.offset; |
155 | //this.startSignedInfo = encoder.offset; | ||||
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 156 | |
157 | |||||
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 158 | if(null!=this.SignedInfo) this.SignedInfo.encode(encoder); |
159 | |||||
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 160 | encoder.writeElement(CCNProtocolDTags.Content, this.content); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 161 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 162 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 163 | this.endSIG = encoder.offset; |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 164 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame^] | 165 | //this.endContent = encoder.offset; |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 166 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 167 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 168 | encoder.writeEndElement(); |
169 | |||||
170 | this.saveRawData(encoder.ostream); | ||||
171 | |||||
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 172 | }; |
173 | |||||
174 | ContentObject.prototype.getElementLabel= function(){return CCNProtocolDTags.ContentObject;}; |