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