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