Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * @author: ucla-cs |
| 3 | * This class represents SignedInfo Object |
| 4 | * This keeps information about the ContentObject Signature |
| 5 | */ |
| 6 | |
| 7 | var ContentType = {DATA:0, ENCR:1, GONE:2, KEY:3, LINK:4, NACK:5}; |
| 8 | |
| 9 | var ContentTypeValue = {0:0x0C04C0, 1:0x10D091,2:0x18E344,3:0x28463F,4:0x2C834A,5:0x34008A}; |
| 10 | var ContentTypeValueReverse = {0x0C04C0:0, 0x10D091:1,0x18E344:2,0x28463F:3,0x2C834A:4,0x34008A:5}; |
| 11 | |
| 12 | |
| 13 | var SignedInfo = function SignedInfo(_publisher,_timestamp,_type,_locator,_freshnessSeconds,_finalBlockID){ |
| 14 | |
| 15 | //TODO, Check types |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 16 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 17 | this.Publisher = _publisher; //PublisherPublicKeyDigest |
| 18 | this.Timestamp=_timestamp; // CCN Time |
| 19 | this.Type=_type; // ContentType |
| 20 | this.Locator =_locator;//KeyLocator |
| 21 | this.FreshnessSeconds =_freshnessSeconds; // Integer |
| 22 | this.FinalBlockID=_finalBlockID; //byte array |
| 23 | |
| 24 | }; |
| 25 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 26 | SignedInfo.prototype.setFields = function(){ |
| 27 | //BASE64 -> RAW STRING |
| 28 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 29 | //this.Locator = new KeyLocator( DataUtils.toNumbersFromString(stringCertificate) ,KeyLocatorType.CERTIFICATE ); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 30 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 31 | var publicKeyHex = globalKeyManager.publicKey; |
| 32 | |
| 33 | console.log('PUBLIC KEY TO WRITE TO CONTENT OBJECT IS '); |
| 34 | console.log(publicKeyHex); |
| 35 | |
| 36 | var publicKeyBytes = DataUtils.toNumbers(globalKeyManager.publicKey) ; |
| 37 | |
| 38 | |
| 39 | |
| 40 | //var stringCertificate = DataUtils.base64toString(globalKeyManager.certificate); |
| 41 | |
| 42 | //if(LOG>3)console.log('string Certificate is '+stringCertificate); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 43 | |
| 44 | //HEX -> BYTE ARRAY |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 45 | //var publisherkey = DataUtils.toNumbers(hex_sha256(stringCertificate)); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 46 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 47 | //if(LOG>3)console.log('publisher key is '); |
| 48 | //if(LOG>3)console.log(publisherkey); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 49 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 50 | var publisherKeyDigest = hex_sha256_from_bytes(publicKeyBytes); |
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 | this.Publisher = new PublisherPublicKeyDigest( DataUtils.toNumbers( publisherKeyDigest ) ); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 53 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 54 | //this.Publisher = new PublisherPublicKeyDigest(publisherkey); |
| 55 | |
| 56 | var d = new Date(); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 57 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 58 | var time = d.getTime(); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 59 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 60 | |
| 61 | this.Timestamp = new CCNTime( time ); |
| 62 | |
| 63 | if(LOG>4)console.log('TIME msec is'); |
| 64 | |
| 65 | if(LOG>4)console.log(this.Timestamp.msec); |
| 66 | |
| 67 | //DATA |
| 68 | this.Type = 0;//0x0C04C0;//ContentTypeValue[ContentType.DATA]; |
| 69 | |
| 70 | //if(LOG>4)console.log('toNumbersFromString(stringCertificate) '+DataUtils.toNumbersFromString(stringCertificate)); |
| 71 | |
| 72 | console.log('PUBLIC KEY TO WRITE TO CONTENT OBJECT IS '); |
| 73 | console.log(publicKeyBytes); |
| 74 | |
| 75 | this.Locator = new KeyLocator( publicKeyBytes ,KeyLocatorType.KEY ); |
| 76 | |
| 77 | //this.Locator = new KeyLocator( DataUtils.toNumbersFromString(stringCertificate) ,KeyLocatorType.CERTIFICATE ); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 78 | |
| 79 | }; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 80 | |
| 81 | SignedInfo.prototype.decode = function( decoder){ |
| 82 | |
| 83 | decoder.readStartElement( this.getElementLabel() ); |
| 84 | |
| 85 | if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) { |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 86 | if(LOG>3) console.log('DECODING PUBLISHER KEY'); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 87 | this.Publisher = new PublisherPublicKeyDigest(); |
| 88 | this.Publisher.decode(decoder); |
| 89 | } |
| 90 | |
| 91 | if (decoder.peekStartElement(CCNProtocolDTags.Timestamp)) { |
| 92 | this.Timestamp = decoder.readDateTime(CCNProtocolDTags.Timestamp); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 93 | if(LOG>4)console.log('TIMESTAMP FOUND IS '+this.Timestamp); |
| 94 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | if (decoder.peekStartElement(CCNProtocolDTags.Type)) { |
| 98 | binType = decoder.readBinaryElement(CCNProtocolDTags.Type);//byte [] |
| 99 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 100 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 101 | //TODO Implement Type of Key Reading |
| 102 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 103 | if(LOG>4)console.log('Binary Type of of Signed Info is '+binType); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 104 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 105 | this.Type = binType; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 106 | |
| 107 | |
| 108 | //TODO Implement Type of Key Reading |
| 109 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 110 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 111 | if (null == this.Type) { |
| 112 | throw new Exception("Cannot parse signedInfo type: bytes."); |
| 113 | } |
| 114 | |
| 115 | } else { |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 116 | this.Type = ContentType.DATA; // default |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | if (decoder.peekStartElement(CCNProtocolDTags.FreshnessSeconds)) { |
| 120 | this.FreshnessSeconds = decoder.readIntegerElement(CCNProtocolDTags.FreshnessSeconds); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 121 | if(LOG>4) console.log('FRESHNESS IN SECONDS IS '+ this.FreshnessSeconds); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | if (decoder.peekStartElement(CCNProtocolDTags.FinalBlockID)) { |
| 125 | this.FinalBlockID = decoder.readBinaryElement(CCNProtocolDTags.FinalBlockID); |
| 126 | } |
| 127 | |
| 128 | if (decoder.peekStartElement(CCNProtocolDTags.KeyLocator)) { |
| 129 | this.Locator = new KeyLocator(); |
| 130 | this.Locator.decode(decoder); |
| 131 | } |
| 132 | |
| 133 | decoder.readEndElement(); |
| 134 | }; |
| 135 | |
| 136 | SignedInfo.prototype.encode = function( encoder) { |
| 137 | if (!this.validate()) { |
| 138 | throw new Exception("Cannot encode : field values missing."); |
| 139 | } |
| 140 | encoder.writeStartElement(this.getElementLabel()); |
| 141 | |
| 142 | if (null!=this.Publisher) { |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 143 | if(LOG>3) console.log('ENCODING PUBLISHER KEY' + this.Publisher.PublisherPublicKeyDigest); |
| 144 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 145 | this.Publisher.encode(encoder); |
| 146 | } |
| 147 | |
| 148 | if (null!=this.Timestamp) { |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 149 | encoder.writeDateTime(CCNProtocolDTags.Timestamp, this.Timestamp ); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | if (null!=this.Type && this.Type !=0) { |
| 153 | |
| 154 | encoder.writeElement(CCNProtocolDTags.Type, this.Type); |
| 155 | } |
| 156 | |
| 157 | if (null!=this.FreshnessSeconds) { |
| 158 | encoder.writeElement(CCNProtocolDTags.FreshnessSeconds, this.FreshnessSeconds); |
| 159 | } |
| 160 | |
| 161 | if (null!=this.FinalBlockID) { |
| 162 | encoder.writeElement(CCNProtocolDTags.FinalBlockID, this.FinalBlockID); |
| 163 | } |
| 164 | |
| 165 | if (null!=this.Locator) { |
| 166 | this.Locator.encode(encoder); |
| 167 | } |
| 168 | |
| 169 | encoder.writeEndElement(); |
| 170 | }; |
| 171 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 172 | SignedInfo.prototype.valueToType = function(){ |
| 173 | //for (Entry<byte [], ContentType> entry : ContentValueTypes.entrySet()) { |
| 174 | //if (Arrays.equals(value, entry.getKey())) |
| 175 | //return entry.getValue(); |
| 176 | //} |
| 177 | return null; |
| 178 | |
| 179 | }; |
| 180 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 181 | SignedInfo.prototype.getElementLabel = function() { |
| 182 | return CCNProtocolDTags.SignedInfo; |
| 183 | }; |
| 184 | |
| 185 | SignedInfo.prototype.validate = function() { |
| 186 | // We don't do partial matches any more, even though encoder/decoder |
| 187 | // is still pretty generous. |
| 188 | if (null ==this.Publisher || null==this.Timestamp ||null== this.Locator) |
| 189 | return false; |
| 190 | return true; |
| 191 | }; |
| 192 | |