Wentao Shang | bd63e46 | 2012-12-03 16:19:33 -0800 | [diff] [blame] | 1 | /** |
Jeff Thompson | 146d7de | 2012-11-17 16:15:28 -0800 | [diff] [blame] | 2 | * @author: Meki Cheraoui |
Jeff Thompson | 745026e | 2012-10-13 12:49:20 -0700 | [diff] [blame] | 3 | * See COPYING for copyright and distribution information. |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 4 | * This class represents ContentObject Objects |
| 5 | */ |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 6 | var ContentObject = function ContentObject(_name,_signedInfo,_content,_signature){ |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 7 | |
| 8 | |
Wentao Shang | ab9018d | 2012-12-18 11:35:45 -0800 | [diff] [blame] | 9 | if (typeof _name == 'string') { |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 10 | this.name = new Name(_name); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 11 | } |
| 12 | else{ |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 13 | //TODO Check the class of _name |
| 14 | this.name = _name; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 15 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 16 | this.signedInfo = _signedInfo; |
Wentao Shang | ab9018d | 2012-12-18 11:35:45 -0800 | [diff] [blame] | 17 | |
| 18 | if (typeof _content == 'string') { |
| 19 | this.content = DataUtils.toNumbersFromString(_content); |
| 20 | } else { |
| 21 | this.content = _content; |
| 22 | } |
| 23 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 24 | this.signature = _signature; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 25 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 26 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 27 | this.startSIG = null; |
| 28 | this.endSIG = null; |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 29 | |
Wentao Shang | fddf90d | 2013-01-05 17:18:49 -0800 | [diff] [blame] | 30 | //this.startSignedInfo = null; |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 31 | this.endContent = null; |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 32 | |
| 33 | this.rawSignatureData = null; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 36 | ContentObject.prototype.sign = function(){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 37 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 38 | var n1 = this.encodeObject(this.name); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 39 | var n2 = this.encodeObject(this.signedInfo); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 40 | var n3 = this.encodeContent(); |
Jeff Thompson | 3d2393f | 2012-11-11 19:11:51 -0800 | [diff] [blame] | 41 | /*console.log('sign: '); |
| 42 | console.log(n1); |
| 43 | console.log(n2); |
| 44 | console.log(n3);*/ |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 45 | |
Jeff Thompson | 3d2393f | 2012-11-11 19:11:51 -0800 | [diff] [blame] | 46 | //var n = n1.concat(n2,n3); |
| 47 | var tempBuf = new ArrayBuffer(n1.length + n2.length + n3.length); |
| 48 | var n = new Uint8Array(tempBuf); |
| 49 | //console.log(n); |
| 50 | n.set(n1, 0); |
| 51 | //console.log(n); |
| 52 | n.set(n2, n1.length); |
| 53 | //console.log(n); |
| 54 | n.set(n3, n1.length + n2.length); |
| 55 | //console.log(n); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 56 | |
Jeff Thompson | 3d2393f | 2012-11-11 19:11:51 -0800 | [diff] [blame] | 57 | if(LOG>4)console.log('Signature Data is (binary) '+n); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 58 | |
Jeff Thompson | 3d2393f | 2012-11-11 19:11:51 -0800 | [diff] [blame] | 59 | if(LOG>4)console.log('Signature Data is (RawString)'); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 60 | |
Jeff Thompson | 3d2393f | 2012-11-11 19:11:51 -0800 | [diff] [blame] | 61 | if(LOG>4)console.log( DataUtils.toString(n) ); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 62 | |
Jeff Thompson | 3d2393f | 2012-11-11 19:11:51 -0800 | [diff] [blame] | 63 | //var sig = DataUtils.toString(n); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 64 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 65 | |
| 66 | var rsa = new RSAKey(); |
| 67 | |
| 68 | rsa.readPrivateKeyFromPEMString(globalKeyManager.privateKey); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 69 | |
| 70 | //var hSig = rsa.signString(sig, "sha256"); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 71 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 72 | var hSig = rsa.signByteArrayWithSHA256(n); |
| 73 | |
| 74 | |
Jeff Thompson | 3d2393f | 2012-11-11 19:11:51 -0800 | [diff] [blame] | 75 | if(LOG>4)console.log('SIGNATURE SAVED IS'); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 76 | |
Jeff Thompson | 3d2393f | 2012-11-11 19:11:51 -0800 | [diff] [blame] | 77 | if(LOG>4)console.log(hSig); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 78 | |
Jeff Thompson | 3d2393f | 2012-11-11 19:11:51 -0800 | [diff] [blame] | 79 | if(LOG>4)console.log( DataUtils.toNumbers(hSig.trim())); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 80 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 81 | this.signature.signature = DataUtils.toNumbers(hSig.trim()); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 82 | |
| 83 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | ContentObject.prototype.encodeObject = function encodeObject(obj){ |
| 87 | var enc = new BinaryXMLEncoder(); |
| 88 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 89 | obj.to_ccnb(enc); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 90 | |
| 91 | var num = enc.getReducedOstream(); |
| 92 | |
| 93 | return num; |
| 94 | |
| 95 | |
| 96 | }; |
| 97 | |
| 98 | ContentObject.prototype.encodeContent = function encodeContent(obj){ |
| 99 | var enc = new BinaryXMLEncoder(); |
| 100 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 101 | enc.writeElement(CCNProtocolDTags.Content, this.content); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 102 | |
| 103 | var num = enc.getReducedOstream(); |
| 104 | |
| 105 | return num; |
| 106 | |
| 107 | |
| 108 | }; |
| 109 | |
| 110 | ContentObject.prototype.saveRawData = function(bytes){ |
| 111 | |
Jeff Thompson | 3d2393f | 2012-11-11 19:11:51 -0800 | [diff] [blame] | 112 | var sigBits = bytes.subarray(this.startSIG, this.endSIG); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 113 | |
| 114 | this.rawSignatureData = sigBits; |
| 115 | }; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 116 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 117 | ContentObject.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 118 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 119 | // TODO VALIDATE THAT ALL FIELDS EXCEPT SIGNATURE ARE PRESENT |
| 120 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 121 | decoder.readStartElement(this.getElementLabel()); |
| 122 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 123 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 124 | if( decoder.peekStartElement(CCNProtocolDTags.Signature) ){ |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 125 | this.signature = new Signature(); |
| 126 | this.signature.from_ccnb(decoder); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 129 | //this.endSIG = decoder.offset; |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 130 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 131 | this.startSIG = decoder.offset; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 132 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 133 | this.name = new Name(); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 134 | this.name.from_ccnb(decoder); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 135 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 136 | //this.startSignedInfo = decoder.offset; |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 137 | |
| 138 | |
| 139 | if( decoder.peekStartElement(CCNProtocolDTags.SignedInfo) ){ |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 140 | this.signedInfo = new SignedInfo(); |
| 141 | this.signedInfo.from_ccnb(decoder); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 144 | this.content = decoder.readBinaryElement(CCNProtocolDTags.Content); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 145 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 146 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 147 | //this.endContent = decoder.offset; |
| 148 | this.endSIG = decoder.offset; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 149 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 150 | |
| 151 | decoder.readEndElement(); |
| 152 | |
| 153 | this.saveRawData(decoder.istream); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 156 | ContentObject.prototype.to_ccnb = function(/*XMLEncoder*/ encoder) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 157 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 158 | //TODO verify name, SignedInfo and Signature is present |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 159 | |
| 160 | |
| 161 | encoder.writeStartElement(this.getElementLabel()); |
| 162 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 163 | |
| 164 | |
| 165 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 166 | if(null!=this.signature) this.signature.to_ccnb(encoder); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 167 | |
| 168 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 169 | this.startSIG = encoder.offset; |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 170 | |
| 171 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 172 | if(null!=this.name) this.name.to_ccnb(encoder); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 173 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 174 | //this.endSIG = encoder.offset; |
| 175 | //this.startSignedInfo = encoder.offset; |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 176 | |
| 177 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 178 | if(null!=this.signedInfo) this.signedInfo.to_ccnb(encoder); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 179 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 180 | encoder.writeElement(CCNProtocolDTags.Content, this.content); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 181 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 182 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 183 | this.endSIG = encoder.offset; |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 184 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 185 | //this.endContent = encoder.offset; |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 186 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 187 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 188 | encoder.writeEndElement(); |
| 189 | |
| 190 | this.saveRawData(encoder.ostream); |
| 191 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | ContentObject.prototype.getElementLabel= function(){return CCNProtocolDTags.ContentObject;}; |
Jeff Thompson | b9ce458 | 2012-09-30 17:52:51 -0700 | [diff] [blame] | 195 | |
| 196 | /** |
| 197 | * Signature |
| 198 | */ |
| 199 | var Signature = function Signature(_witness,_signature,_digestAlgorithm) { |
| 200 | |
| 201 | this.Witness = _witness;//byte [] _witness; |
| 202 | this.signature = _signature;//byte [] _signature; |
| 203 | this.digestAlgorithm = _digestAlgorithm//String _digestAlgorithm; |
| 204 | }; |
| 205 | |
Jeff Thompson | b9ce458 | 2012-09-30 17:52:51 -0700 | [diff] [blame] | 206 | Signature.prototype.from_ccnb =function( decoder) { |
| 207 | decoder.readStartElement(this.getElementLabel()); |
| 208 | |
Wentao Shang | 882e34e | 2013-01-05 02:49:51 -0800 | [diff] [blame] | 209 | if(LOG>4)console.log('STARTED DECODING SIGNATURE'); |
Jeff Thompson | b9ce458 | 2012-09-30 17:52:51 -0700 | [diff] [blame] | 210 | |
| 211 | if (decoder.peekStartElement(CCNProtocolDTags.DigestAlgorithm)) { |
Jeff Thompson | b9ce458 | 2012-09-30 17:52:51 -0700 | [diff] [blame] | 212 | if(LOG>4)console.log('DIGIEST ALGORITHM FOUND'); |
| 213 | this.digestAlgorithm = decoder.readUTF8Element(CCNProtocolDTags.DigestAlgorithm); |
| 214 | } |
| 215 | if (decoder.peekStartElement(CCNProtocolDTags.Witness)) { |
Wentao Shang | 882e34e | 2013-01-05 02:49:51 -0800 | [diff] [blame] | 216 | if(LOG>4)console.log('WITNESS FOUND'); |
Jeff Thompson | b9ce458 | 2012-09-30 17:52:51 -0700 | [diff] [blame] | 217 | this.Witness = decoder.readBinaryElement(CCNProtocolDTags.Witness); |
| 218 | } |
| 219 | |
| 220 | //FORCE TO READ A SIGNATURE |
| 221 | |
Wentao Shang | 882e34e | 2013-01-05 02:49:51 -0800 | [diff] [blame] | 222 | if(LOG>4)console.log('SIGNATURE FOUND'); |
| 223 | this.signature = decoder.readBinaryElement(CCNProtocolDTags.SignatureBits); |
Jeff Thompson | b9ce458 | 2012-09-30 17:52:51 -0700 | [diff] [blame] | 224 | |
| 225 | decoder.readEndElement(); |
| 226 | |
| 227 | }; |
| 228 | |
| 229 | |
| 230 | Signature.prototype.to_ccnb= function( encoder){ |
| 231 | |
| 232 | if (!this.validate()) { |
| 233 | throw new Error("Cannot encode: field values missing."); |
| 234 | } |
| 235 | |
| 236 | encoder.writeStartElement(this.getElementLabel()); |
| 237 | |
| 238 | if ((null != this.digestAlgorithm) && (!this.digestAlgorithm.equals(CCNDigestHelper.DEFAULT_DIGEST_ALGORITHM))) { |
| 239 | encoder.writeElement(CCNProtocolDTags.DigestAlgorithm, OIDLookup.getDigestOID(this.DigestAlgorithm)); |
| 240 | } |
| 241 | |
| 242 | if (null != this.Witness) { |
| 243 | // needs to handle null witness |
| 244 | encoder.writeElement(CCNProtocolDTags.Witness, this.Witness); |
| 245 | } |
| 246 | |
| 247 | encoder.writeElement(CCNProtocolDTags.SignatureBits, this.signature); |
| 248 | |
| 249 | encoder.writeEndElement(); |
| 250 | }; |
| 251 | |
| 252 | Signature.prototype.getElementLabel = function() { return CCNProtocolDTags.Signature; }; |
| 253 | |
| 254 | |
| 255 | Signature.prototype.validate = function() { |
| 256 | return null != this.signature; |
| 257 | }; |
| 258 | |
| 259 | |
| 260 | /** |
| 261 | * SignedInfo |
| 262 | */ |
| 263 | var ContentType = {DATA:0, ENCR:1, GONE:2, KEY:3, LINK:4, NACK:5}; |
| 264 | var ContentTypeValue = {0:0x0C04C0, 1:0x10D091,2:0x18E344,3:0x28463F,4:0x2C834A,5:0x34008A}; |
| 265 | var ContentTypeValueReverse = {0x0C04C0:0, 0x10D091:1,0x18E344:2,0x28463F:3,0x2C834A:4,0x34008A:5}; |
| 266 | |
| 267 | var SignedInfo = function SignedInfo(_publisher,_timestamp,_type,_locator,_freshnessSeconds,_finalBlockID){ |
| 268 | |
| 269 | //TODO, Check types |
| 270 | |
| 271 | this.publisher = _publisher; //publisherPublicKeyDigest |
| 272 | this.timestamp=_timestamp; // CCN Time |
| 273 | this.type=_type; // ContentType |
| 274 | this.locator =_locator;//KeyLocator |
| 275 | this.freshnessSeconds =_freshnessSeconds; // Integer |
| 276 | this.finalBlockID=_finalBlockID; //byte array |
Wentao Shang | ab9018d | 2012-12-18 11:35:45 -0800 | [diff] [blame] | 277 | |
| 278 | // SWT: merge setFields() method into constructor |
| 279 | this.setFields(); |
Jeff Thompson | b9ce458 | 2012-09-30 17:52:51 -0700 | [diff] [blame] | 280 | |
| 281 | }; |
| 282 | |
| 283 | SignedInfo.prototype.setFields = function(){ |
| 284 | //BASE64 -> RAW STRING |
| 285 | |
| 286 | //this.locator = new KeyLocator( DataUtils.toNumbersFromString(stringCertificate) ,KeyLocatorType.CERTIFICATE ); |
| 287 | |
| 288 | var publicKeyHex = globalKeyManager.publicKey; |
| 289 | |
Jeff Thompson | 3d2393f | 2012-11-11 19:11:51 -0800 | [diff] [blame] | 290 | if(LOG>4)console.log('PUBLIC KEY TO WRITE TO CONTENT OBJECT IS '); |
| 291 | if(LOG>4)console.log(publicKeyHex); |
Jeff Thompson | b9ce458 | 2012-09-30 17:52:51 -0700 | [diff] [blame] | 292 | |
| 293 | var publicKeyBytes = DataUtils.toNumbers(globalKeyManager.publicKey) ; |
| 294 | |
| 295 | |
| 296 | |
| 297 | //var stringCertificate = DataUtils.base64toString(globalKeyManager.certificate); |
| 298 | |
| 299 | //if(LOG>3)console.log('string Certificate is '+stringCertificate); |
| 300 | |
| 301 | //HEX -> BYTE ARRAY |
| 302 | //var publisherkey = DataUtils.toNumbers(hex_sha256(stringCertificate)); |
| 303 | |
| 304 | //if(LOG>3)console.log('publisher key is '); |
| 305 | //if(LOG>3)console.log(publisherkey); |
| 306 | |
| 307 | var publisherKeyDigest = hex_sha256_from_bytes(publicKeyBytes); |
| 308 | |
| 309 | this.publisher = new PublisherPublicKeyDigest( DataUtils.toNumbers( publisherKeyDigest ) ); |
| 310 | |
| 311 | //this.publisher = new PublisherPublicKeyDigest(publisherkey); |
| 312 | |
| 313 | var d = new Date(); |
| 314 | |
| 315 | var time = d.getTime(); |
| 316 | |
| 317 | |
| 318 | this.timestamp = new CCNTime( time ); |
| 319 | |
| 320 | if(LOG>4)console.log('TIME msec is'); |
| 321 | |
| 322 | if(LOG>4)console.log(this.timestamp.msec); |
| 323 | |
| 324 | //DATA |
| 325 | this.type = 0;//0x0C04C0;//ContentTypeValue[ContentType.DATA]; |
| 326 | |
| 327 | //if(LOG>4)console.log('toNumbersFromString(stringCertificate) '+DataUtils.toNumbersFromString(stringCertificate)); |
| 328 | |
Jeff Thompson | 3d2393f | 2012-11-11 19:11:51 -0800 | [diff] [blame] | 329 | if(LOG>4)console.log('PUBLIC KEY TO WRITE TO CONTENT OBJECT IS '); |
| 330 | if(LOG>4)console.log(publicKeyBytes); |
Jeff Thompson | b9ce458 | 2012-09-30 17:52:51 -0700 | [diff] [blame] | 331 | |
| 332 | this.locator = new KeyLocator( publicKeyBytes ,KeyLocatorType.KEY ); |
| 333 | |
| 334 | //this.locator = new KeyLocator( DataUtils.toNumbersFromString(stringCertificate) ,KeyLocatorType.CERTIFICATE ); |
| 335 | |
| 336 | }; |
| 337 | |
| 338 | SignedInfo.prototype.from_ccnb = function( decoder){ |
| 339 | |
| 340 | decoder.readStartElement( this.getElementLabel() ); |
| 341 | |
| 342 | if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) { |
Wentao Shang | 882e34e | 2013-01-05 02:49:51 -0800 | [diff] [blame] | 343 | if(LOG>4)console.log('DECODING PUBLISHER KEY'); |
Jeff Thompson | b9ce458 | 2012-09-30 17:52:51 -0700 | [diff] [blame] | 344 | this.publisher = new PublisherPublicKeyDigest(); |
| 345 | this.publisher.from_ccnb(decoder); |
| 346 | } |
| 347 | |
| 348 | if (decoder.peekStartElement(CCNProtocolDTags.Timestamp)) { |
Wentao Shang | 882e34e | 2013-01-05 02:49:51 -0800 | [diff] [blame] | 349 | if(LOG>4)console.log('DECODING TIMESTAMP'); |
Jeff Thompson | b9ce458 | 2012-09-30 17:52:51 -0700 | [diff] [blame] | 350 | this.timestamp = decoder.readDateTime(CCNProtocolDTags.Timestamp); |
Jeff Thompson | b9ce458 | 2012-09-30 17:52:51 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | if (decoder.peekStartElement(CCNProtocolDTags.Type)) { |
| 354 | binType = decoder.readBinaryElement(CCNProtocolDTags.Type);//byte [] |
| 355 | |
| 356 | |
| 357 | //TODO Implement type of Key Reading |
| 358 | |
| 359 | if(LOG>4)console.log('Binary Type of of Signed Info is '+binType); |
| 360 | |
| 361 | this.type = binType; |
| 362 | |
| 363 | |
| 364 | //TODO Implement type of Key Reading |
| 365 | |
| 366 | |
| 367 | if (null == this.type) { |
| 368 | throw new Error("Cannot parse signedInfo type: bytes."); |
| 369 | } |
| 370 | |
| 371 | } else { |
| 372 | this.type = ContentType.DATA; // default |
| 373 | } |
| 374 | |
| 375 | if (decoder.peekStartElement(CCNProtocolDTags.FreshnessSeconds)) { |
| 376 | this.freshnessSeconds = decoder.readIntegerElement(CCNProtocolDTags.FreshnessSeconds); |
Wentao Shang | 882e34e | 2013-01-05 02:49:51 -0800 | [diff] [blame] | 377 | if(LOG>4)console.log('FRESHNESS IN SECONDS IS '+ this.freshnessSeconds); |
Jeff Thompson | b9ce458 | 2012-09-30 17:52:51 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | if (decoder.peekStartElement(CCNProtocolDTags.FinalBlockID)) { |
Wentao Shang | 882e34e | 2013-01-05 02:49:51 -0800 | [diff] [blame] | 381 | if(LOG>4)console.log('DECODING FINAL BLOCKID'); |
Jeff Thompson | b9ce458 | 2012-09-30 17:52:51 -0700 | [diff] [blame] | 382 | this.finalBlockID = decoder.readBinaryElement(CCNProtocolDTags.FinalBlockID); |
| 383 | } |
| 384 | |
| 385 | if (decoder.peekStartElement(CCNProtocolDTags.KeyLocator)) { |
Wentao Shang | 882e34e | 2013-01-05 02:49:51 -0800 | [diff] [blame] | 386 | if(LOG>4)console.log('DECODING KEY LOCATOR'); |
Jeff Thompson | b9ce458 | 2012-09-30 17:52:51 -0700 | [diff] [blame] | 387 | this.locator = new KeyLocator(); |
| 388 | this.locator.from_ccnb(decoder); |
| 389 | } |
| 390 | |
| 391 | decoder.readEndElement(); |
| 392 | }; |
| 393 | |
| 394 | SignedInfo.prototype.to_ccnb = function( encoder) { |
| 395 | if (!this.validate()) { |
| 396 | throw new Error("Cannot encode : field values missing."); |
| 397 | } |
| 398 | encoder.writeStartElement(this.getElementLabel()); |
| 399 | |
| 400 | if (null!=this.publisher) { |
| 401 | if(LOG>3) console.log('ENCODING PUBLISHER KEY' + this.publisher.publisherPublicKeyDigest); |
| 402 | |
| 403 | this.publisher.to_ccnb(encoder); |
| 404 | } |
| 405 | |
| 406 | if (null!=this.timestamp) { |
| 407 | encoder.writeDateTime(CCNProtocolDTags.Timestamp, this.timestamp ); |
| 408 | } |
| 409 | |
| 410 | if (null!=this.type && this.type !=0) { |
| 411 | |
| 412 | encoder.writeElement(CCNProtocolDTags.type, this.type); |
| 413 | } |
| 414 | |
| 415 | if (null!=this.freshnessSeconds) { |
| 416 | encoder.writeElement(CCNProtocolDTags.FreshnessSeconds, this.freshnessSeconds); |
| 417 | } |
| 418 | |
| 419 | if (null!=this.finalBlockID) { |
| 420 | encoder.writeElement(CCNProtocolDTags.FinalBlockID, this.finalBlockID); |
| 421 | } |
| 422 | |
| 423 | if (null!=this.locator) { |
| 424 | this.locator.to_ccnb(encoder); |
| 425 | } |
| 426 | |
| 427 | encoder.writeEndElement(); |
| 428 | }; |
| 429 | |
| 430 | SignedInfo.prototype.valueToType = function(){ |
| 431 | //for (Entry<byte [], ContentType> entry : ContentValueTypes.entrySet()) { |
| 432 | //if (Arrays.equals(value, entry.getKey())) |
| 433 | //return entry.getValue(); |
| 434 | //} |
| 435 | return null; |
| 436 | |
| 437 | }; |
| 438 | |
| 439 | SignedInfo.prototype.getElementLabel = function() { |
| 440 | return CCNProtocolDTags.SignedInfo; |
| 441 | }; |
| 442 | |
| 443 | SignedInfo.prototype.validate = function() { |
| 444 | // We don't do partial matches any more, even though encoder/decoder |
| 445 | // is still pretty generous. |
| 446 | if (null ==this.publisher || null==this.timestamp ||null== this.locator) |
| 447 | return false; |
| 448 | return true; |
| 449 | }; |