blob: b2ff0b2a56bb6e09d93477d66cd2ee34363d21fb [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
Meki Cherkaouif441d3a2012-04-22 15:17:52 -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
23
24};
25
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070026SignedInfo.prototype.setFields = function(){
27 //BASE64 -> RAW STRING
28
Meki Cherkaoui8f173612012-06-06 01:05:40 -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
Meki Cherkaoui8f173612012-06-06 01:05:40 -070052 this.Publisher = new PublisherPublicKeyDigest( DataUtils.toNumbers( publisherKeyDigest ) );
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070053
Meki Cherkaoui8f173612012-06-06 01:05:40 -070054 //this.Publisher = new PublisherPublicKeyDigest(publisherkey);
55
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
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 Cherkaouif3d8f692012-05-18 15:44:28 -070078
79};
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070080
81SignedInfo.prototype.decode = function( decoder){
82
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');
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070087 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 Cherkaouif3d8f692012-05-18 15:44:28 -070093 if(LOG>4)console.log('TIMESTAMP FOUND IS '+this.Timestamp);
94
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
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700101 //TODO Implement Type of Key Reading
102
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
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700105 this.Type = binType;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700106
107
108 //TODO Implement Type of Key Reading
109
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700110
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700111 if (null == this.Type) {
112 throw new Exception("Cannot parse signedInfo type: bytes.");
113 }
114
115 } else {
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700116 this.Type = ContentType.DATA; // default
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700117 }
118
119 if (decoder.peekStartElement(CCNProtocolDTags.FreshnessSeconds)) {
120 this.FreshnessSeconds = decoder.readIntegerElement(CCNProtocolDTags.FreshnessSeconds);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700121 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)) {
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
136SignedInfo.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 Cherkaouif3d8f692012-05-18 15:44:28 -0700143 if(LOG>3) console.log('ENCODING PUBLISHER KEY' + this.Publisher.PublisherPublicKeyDigest);
144
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700145 this.Publisher.encode(encoder);
146 }
147
148 if (null!=this.Timestamp) {
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700149 encoder.writeDateTime(CCNProtocolDTags.Timestamp, this.Timestamp );
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700150 }
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 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.
188 if (null ==this.Publisher || null==this.Timestamp ||null== this.Locator)
189 return false;
190 return true;
191};
192