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 | |
| 29 | var stringCertificate = DataUtils.base64toString(globalKeyManager.certificate); |
| 30 | |
| 31 | if(LOG>3)console.log('string Certificate is '+stringCertificate); |
| 32 | |
| 33 | //HEX -> BYTE ARRAY |
| 34 | var publisherkey = toNumbers(hex_sha256(stringCertificate)); |
| 35 | |
| 36 | if(LOG>3)console.log('publisher key is '); |
| 37 | if(LOG>3)console.log(publisherkey); |
| 38 | |
| 39 | this.Publisher = new PublisherPublicKeyDigest(publisherkey); |
| 40 | |
| 41 | |
| 42 | this.Timestamp = new CCNTime('FD0499602d2000'); |
| 43 | |
| 44 | this.Type = ContentType.DATA; |
| 45 | |
| 46 | console.log('toNumbersFromString(stringCertificate) '+toNumbersFromString(stringCertificate)); |
| 47 | this.Locator = new KeyLocator( toNumbersFromString(stringCertificate) ,KeyLocatorType.KEY ); |
| 48 | |
| 49 | }; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 50 | |
| 51 | SignedInfo.prototype.decode = function( decoder){ |
| 52 | |
| 53 | decoder.readStartElement( this.getElementLabel() ); |
| 54 | |
| 55 | if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) { |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 56 | if(LOG>3) console.log('DECODING PUBLISHER KEY'); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 57 | this.Publisher = new PublisherPublicKeyDigest(); |
| 58 | this.Publisher.decode(decoder); |
| 59 | } |
| 60 | |
| 61 | if (decoder.peekStartElement(CCNProtocolDTags.Timestamp)) { |
| 62 | this.Timestamp = decoder.readDateTime(CCNProtocolDTags.Timestamp); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 63 | if(LOG>4)console.log('TIMESTAMP FOUND IS '+this.Timestamp); |
| 64 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | if (decoder.peekStartElement(CCNProtocolDTags.Type)) { |
| 68 | binType = decoder.readBinaryElement(CCNProtocolDTags.Type);//byte [] |
| 69 | |
| 70 | |
| 71 | //TODO Implement Type of Key Reading |
| 72 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 73 | if(LOG>4)console.log('TYPE IS'+bintype); |
| 74 | |
| 75 | this.Type = this.valueToType(binType); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 76 | |
| 77 | |
| 78 | //TODO Implement Type of Key Reading |
| 79 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 80 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 81 | if (null == this.Type) { |
| 82 | throw new Exception("Cannot parse signedInfo type: bytes."); |
| 83 | } |
| 84 | |
| 85 | } else { |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 86 | this.Type = ContentType.DATA; // default |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | if (decoder.peekStartElement(CCNProtocolDTags.FreshnessSeconds)) { |
| 90 | this.FreshnessSeconds = decoder.readIntegerElement(CCNProtocolDTags.FreshnessSeconds); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 91 | if(LOG>4) console.log('FRESHNESS IN SECONDS IS '+ this.FreshnessSeconds); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | if (decoder.peekStartElement(CCNProtocolDTags.FinalBlockID)) { |
| 95 | this.FinalBlockID = decoder.readBinaryElement(CCNProtocolDTags.FinalBlockID); |
| 96 | } |
| 97 | |
| 98 | if (decoder.peekStartElement(CCNProtocolDTags.KeyLocator)) { |
| 99 | this.Locator = new KeyLocator(); |
| 100 | this.Locator.decode(decoder); |
| 101 | } |
| 102 | |
| 103 | decoder.readEndElement(); |
| 104 | }; |
| 105 | |
| 106 | SignedInfo.prototype.encode = function( encoder) { |
| 107 | if (!this.validate()) { |
| 108 | throw new Exception("Cannot encode : field values missing."); |
| 109 | } |
| 110 | encoder.writeStartElement(this.getElementLabel()); |
| 111 | |
| 112 | if (null!=this.Publisher) { |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 113 | if(LOG>3) console.log('ENCODING PUBLISHER KEY' + this.Publisher.PublisherPublicKeyDigest); |
| 114 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 115 | this.Publisher.encode(encoder); |
| 116 | } |
| 117 | |
| 118 | if (null!=this.Timestamp) { |
| 119 | encoder.writeDateTime(CCNProtocolDTags.Timestamp, this.Timestamp); |
| 120 | } |
| 121 | |
| 122 | if (null!=this.Type && this.Type !=0) { |
| 123 | |
| 124 | encoder.writeElement(CCNProtocolDTags.Type, this.Type); |
| 125 | } |
| 126 | |
| 127 | if (null!=this.FreshnessSeconds) { |
| 128 | encoder.writeElement(CCNProtocolDTags.FreshnessSeconds, this.FreshnessSeconds); |
| 129 | } |
| 130 | |
| 131 | if (null!=this.FinalBlockID) { |
| 132 | encoder.writeElement(CCNProtocolDTags.FinalBlockID, this.FinalBlockID); |
| 133 | } |
| 134 | |
| 135 | if (null!=this.Locator) { |
| 136 | this.Locator.encode(encoder); |
| 137 | } |
| 138 | |
| 139 | encoder.writeEndElement(); |
| 140 | }; |
| 141 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 142 | SignedInfo.prototype.valueToType = function(){ |
| 143 | //for (Entry<byte [], ContentType> entry : ContentValueTypes.entrySet()) { |
| 144 | //if (Arrays.equals(value, entry.getKey())) |
| 145 | //return entry.getValue(); |
| 146 | //} |
| 147 | return null; |
| 148 | |
| 149 | }; |
| 150 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 151 | SignedInfo.prototype.getElementLabel = function() { |
| 152 | return CCNProtocolDTags.SignedInfo; |
| 153 | }; |
| 154 | |
| 155 | SignedInfo.prototype.validate = function() { |
| 156 | // We don't do partial matches any more, even though encoder/decoder |
| 157 | // is still pretty generous. |
| 158 | if (null ==this.Publisher || null==this.Timestamp ||null== this.Locator) |
| 159 | return false; |
| 160 | return true; |
| 161 | }; |
| 162 | |