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'){ |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame^] | 9 | this.name = new Name(_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 | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 15 | this.signedInfo = _signedInfo; |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 16 | this.content=_content; |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 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); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 32 | var n2 = this.encodeObject(this.signedInfo); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 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 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 61 | this.signature.signature = DataUtils.toNumbers(hSig.trim()); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 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 | |||||
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 69 | obj.to_ccnb(enc); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 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) ){ |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 105 | this.signature = new Signature(); |
106 | this.signature.from_ccnb(decoder); | ||||
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 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 | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame^] | 113 | this.name = new Name(); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 114 | this.name.from_ccnb(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) ){ | ||||
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 120 | this.signedInfo = new SignedInfo(); |
121 | this.signedInfo.from_ccnb(decoder); | ||||
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 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 | |||||
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 146 | if(null!=this.signature) this.signature.to_ccnb(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 | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 152 | if(null!=this.name) this.name.to_ccnb(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 | |||||
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 158 | if(null!=this.signedInfo) this.signedInfo.to_ccnb(encoder); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 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;}; |