blob: f398c4c76616dc091a73ecd3ef8ea7cb35da288c [file] [log] [blame]
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07001/*
2 * @author: ucla-cs
3 * This class represents SignedInfo Object
4 * This keeps information about the ContentObject Signature
5 */
6
7var ContentType = {DATA:0, ENCR:1, GONE:2, KEY:3, LINK:4, NACK:5};
8
9var ContentTypeValue = {0:0x0C04C0, 1:0x10D091,2:0x18E344,3:0x28463F,4:0x2C834A,5:0x34008A};
10var ContentTypeValueReverse = {0x0C04C0:0, 0x10D091:1,0x18E344:2,0x28463F:3,0x2C834A:4,0x34008A:5};
11
12
13var SignedInfo = function SignedInfo(_publisher,_timestamp,_type,_locator,_freshnessSeconds,_finalBlockID){
14
15 //TODO, Check types
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070016
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070017 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
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070023
24};
25
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070026SignedInfo.prototype.setFields = function(){
27 //BASE64 -> RAW STRING
28
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070029 //this.locator = new KeyLocator( DataUtils.toNumbersFromString(stringCertificate) ,KeyLocatorType.CERTIFICATE );
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070030
Meki Cherkaoui8f173612012-06-06 01:05:40 -070031 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 Cherkaouif3d8f692012-05-18 15:44:28 -070043
44 //HEX -> BYTE ARRAY
Meki Cherkaoui8f173612012-06-06 01:05:40 -070045 //var publisherkey = DataUtils.toNumbers(hex_sha256(stringCertificate));
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070046
Meki Cherkaoui8f173612012-06-06 01:05:40 -070047 //if(LOG>3)console.log('publisher key is ');
48 //if(LOG>3)console.log(publisherkey);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070049
Meki Cherkaoui8f173612012-06-06 01:05:40 -070050 var publisherKeyDigest = hex_sha256_from_bytes(publicKeyBytes);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070051
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070052 this.publisher = new PublisherPublicKeyDigest( DataUtils.toNumbers( publisherKeyDigest ) );
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070053
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070054 //this.publisher = new PublisherPublicKeyDigest(publisherkey);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070055
56 var d = new Date();
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070057
Meki Cherkaoui8f173612012-06-06 01:05:40 -070058 var time = d.getTime();
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070059
Meki Cherkaoui8f173612012-06-06 01:05:40 -070060
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070061 this.timestamp = new CCNTime( time );
Meki Cherkaoui8f173612012-06-06 01:05:40 -070062
63 if(LOG>4)console.log('TIME msec is');
64
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070065 if(LOG>4)console.log(this.timestamp.msec);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070066
67 //DATA
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070068 this.type = 0;//0x0C04C0;//ContentTypeValue[ContentType.DATA];
Meki Cherkaoui8f173612012-06-06 01:05:40 -070069
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
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070075 this.locator = new KeyLocator( publicKeyBytes ,KeyLocatorType.KEY );
Meki Cherkaoui8f173612012-06-06 01:05:40 -070076
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070077 //this.locator = new KeyLocator( DataUtils.toNumbersFromString(stringCertificate) ,KeyLocatorType.CERTIFICATE );
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070078
79};
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070080
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070081SignedInfo.prototype.from_ccnb = function( decoder){
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070082
83 decoder.readStartElement( this.getElementLabel() );
84
85 if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) {
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070086 if(LOG>3) console.log('DECODING PUBLISHER KEY');
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070087 this.publisher = new PublisherPublicKeyDigest();
88 this.publisher.from_ccnb(decoder);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070089 }
90
91 if (decoder.peekStartElement(CCNProtocolDTags.Timestamp)) {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070092 this.timestamp = decoder.readDateTime(CCNProtocolDTags.Timestamp);
93 if(LOG>4)console.log('TIMESTAMP FOUND IS '+this.timestamp);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070094
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070095 }
96
97 if (decoder.peekStartElement(CCNProtocolDTags.Type)) {
98 binType = decoder.readBinaryElement(CCNProtocolDTags.Type);//byte []
99
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700100
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700101 //TODO Implement type of Key Reading
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700102
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700103 if(LOG>4)console.log('Binary Type of of Signed Info is '+binType);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700104
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700105 this.type = binType;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700106
107
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700108 //TODO Implement type of Key Reading
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700109
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700110
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700111 if (null == this.type) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -0700112 throw new Error("Cannot parse signedInfo type: bytes.");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700113 }
114
115 } else {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700116 this.type = ContentType.DATA; // default
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700117 }
118
119 if (decoder.peekStartElement(CCNProtocolDTags.FreshnessSeconds)) {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700120 this.freshnessSeconds = decoder.readIntegerElement(CCNProtocolDTags.FreshnessSeconds);
121 if(LOG>4) console.log('FRESHNESS IN SECONDS IS '+ this.freshnessSeconds);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700122 }
123
124 if (decoder.peekStartElement(CCNProtocolDTags.FinalBlockID)) {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700125 this.finalBlockID = decoder.readBinaryElement(CCNProtocolDTags.FinalBlockID);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700126 }
127
128 if (decoder.peekStartElement(CCNProtocolDTags.KeyLocator)) {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700129 this.locator = new KeyLocator();
130 this.locator.from_ccnb(decoder);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700131 }
132
133 decoder.readEndElement();
134};
135
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700136SignedInfo.prototype.to_ccnb = function( encoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700137 if (!this.validate()) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -0700138 throw new Error("Cannot encode : field values missing.");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700139 }
140 encoder.writeStartElement(this.getElementLabel());
141
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700142 if (null!=this.publisher) {
143 if(LOG>3) console.log('ENCODING PUBLISHER KEY' + this.publisher.publisherPublicKeyDigest);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700144
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700145 this.publisher.to_ccnb(encoder);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700146 }
147
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700148 if (null!=this.timestamp) {
149 encoder.writeDateTime(CCNProtocolDTags.Timestamp, this.timestamp );
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700150 }
151
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700152 if (null!=this.type && this.type !=0) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700153
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700154 encoder.writeElement(CCNProtocolDTags.type, this.type);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700155 }
156
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700157 if (null!=this.freshnessSeconds) {
158 encoder.writeElement(CCNProtocolDTags.FreshnessSeconds, this.freshnessSeconds);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700159 }
160
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700161 if (null!=this.finalBlockID) {
162 encoder.writeElement(CCNProtocolDTags.FinalBlockID, this.finalBlockID);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700163 }
164
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700165 if (null!=this.locator) {
166 this.locator.to_ccnb(encoder);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700167 }
168
169 encoder.writeEndElement();
170};
171
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700172SignedInfo.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 Cherkaouif441d3a2012-04-22 15:17:52 -0700181SignedInfo.prototype.getElementLabel = function() {
182 return CCNProtocolDTags.SignedInfo;
183};
184
185SignedInfo.prototype.validate = function() {
186 // We don't do partial matches any more, even though encoder/decoder
187 // is still pretty generous.
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700188 if (null ==this.publisher || null==this.timestamp ||null== this.locator)
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700189 return false;
190 return true;
191};
192