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 |
| 16 | |
| 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 | |
| 26 | |
| 27 | SignedInfo.prototype.decode = function( decoder){ |
| 28 | |
| 29 | decoder.readStartElement( this.getElementLabel() ); |
| 30 | |
| 31 | if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) { |
| 32 | this.Publisher = new PublisherPublicKeyDigest(); |
| 33 | this.Publisher.decode(decoder); |
| 34 | } |
| 35 | |
| 36 | if (decoder.peekStartElement(CCNProtocolDTags.Timestamp)) { |
| 37 | this.Timestamp = decoder.readDateTime(CCNProtocolDTags.Timestamp); |
| 38 | } |
| 39 | |
| 40 | if (decoder.peekStartElement(CCNProtocolDTags.Type)) { |
| 41 | binType = decoder.readBinaryElement(CCNProtocolDTags.Type);//byte [] |
| 42 | |
| 43 | |
| 44 | //TODO Implement Type of Key Reading |
| 45 | |
| 46 | //this.Type = valueToType(binType); |
| 47 | |
| 48 | |
| 49 | //TODO Implement Type of Key Reading |
| 50 | |
| 51 | /* |
| 52 | if (null == this.Type) { |
| 53 | throw new Exception("Cannot parse signedInfo type: bytes."); |
| 54 | } |
| 55 | |
| 56 | } else { |
| 57 | this.Type = ContentType.DATA; // default*/ |
| 58 | } |
| 59 | |
| 60 | if (decoder.peekStartElement(CCNProtocolDTags.FreshnessSeconds)) { |
| 61 | this.FreshnessSeconds = decoder.readIntegerElement(CCNProtocolDTags.FreshnessSeconds); |
| 62 | } |
| 63 | |
| 64 | if (decoder.peekStartElement(CCNProtocolDTags.FinalBlockID)) { |
| 65 | this.FinalBlockID = decoder.readBinaryElement(CCNProtocolDTags.FinalBlockID); |
| 66 | } |
| 67 | |
| 68 | if (decoder.peekStartElement(CCNProtocolDTags.KeyLocator)) { |
| 69 | this.Locator = new KeyLocator(); |
| 70 | this.Locator.decode(decoder); |
| 71 | } |
| 72 | |
| 73 | decoder.readEndElement(); |
| 74 | }; |
| 75 | |
| 76 | SignedInfo.prototype.encode = function( encoder) { |
| 77 | if (!this.validate()) { |
| 78 | throw new Exception("Cannot encode : field values missing."); |
| 79 | } |
| 80 | encoder.writeStartElement(this.getElementLabel()); |
| 81 | |
| 82 | if (null!=this.Publisher) { |
| 83 | this.Publisher.encode(encoder); |
| 84 | } |
| 85 | |
| 86 | if (null!=this.Timestamp) { |
| 87 | encoder.writeDateTime(CCNProtocolDTags.Timestamp, this.Timestamp); |
| 88 | } |
| 89 | |
| 90 | if (null!=this.Type && this.Type !=0) { |
| 91 | |
| 92 | encoder.writeElement(CCNProtocolDTags.Type, this.Type); |
| 93 | } |
| 94 | |
| 95 | if (null!=this.FreshnessSeconds) { |
| 96 | encoder.writeElement(CCNProtocolDTags.FreshnessSeconds, this.FreshnessSeconds); |
| 97 | } |
| 98 | |
| 99 | if (null!=this.FinalBlockID) { |
| 100 | encoder.writeElement(CCNProtocolDTags.FinalBlockID, this.FinalBlockID); |
| 101 | } |
| 102 | |
| 103 | if (null!=this.Locator) { |
| 104 | this.Locator.encode(encoder); |
| 105 | } |
| 106 | |
| 107 | encoder.writeEndElement(); |
| 108 | }; |
| 109 | |
| 110 | SignedInfo.prototype.getElementLabel = function() { |
| 111 | return CCNProtocolDTags.SignedInfo; |
| 112 | }; |
| 113 | |
| 114 | SignedInfo.prototype.validate = function() { |
| 115 | // We don't do partial matches any more, even though encoder/decoder |
| 116 | // is still pretty generous. |
| 117 | if (null ==this.Publisher || null==this.Timestamp ||null== this.Locator) |
| 118 | return false; |
| 119 | return true; |
| 120 | }; |
| 121 | |