blob: 0b81201fa86b9a1d644a3a094873caa67205cb5d [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
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 Cherkaouif441d3a2012-04-22 15:17:52 -070050
51SignedInfo.prototype.decode = function( decoder){
52
53 decoder.readStartElement( this.getElementLabel() );
54
55 if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) {
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070056 if(LOG>3) console.log('DECODING PUBLISHER KEY');
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070057 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 Cherkaouif3d8f692012-05-18 15:44:28 -070063 if(LOG>4)console.log('TIMESTAMP FOUND IS '+this.Timestamp);
64
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070065 }
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 Cherkaouif3d8f692012-05-18 15:44:28 -070073 if(LOG>4)console.log('TYPE IS'+bintype);
74
75 this.Type = this.valueToType(binType);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070076
77
78 //TODO Implement Type of Key Reading
79
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070080
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070081 if (null == this.Type) {
82 throw new Exception("Cannot parse signedInfo type: bytes.");
83 }
84
85 } else {
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070086 this.Type = ContentType.DATA; // default
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070087 }
88
89 if (decoder.peekStartElement(CCNProtocolDTags.FreshnessSeconds)) {
90 this.FreshnessSeconds = decoder.readIntegerElement(CCNProtocolDTags.FreshnessSeconds);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070091 if(LOG>4) console.log('FRESHNESS IN SECONDS IS '+ this.FreshnessSeconds);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070092 }
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
106SignedInfo.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 Cherkaouif3d8f692012-05-18 15:44:28 -0700113 if(LOG>3) console.log('ENCODING PUBLISHER KEY' + this.Publisher.PublisherPublicKeyDigest);
114
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700115 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 Cherkaouif3d8f692012-05-18 15:44:28 -0700142SignedInfo.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 Cherkaouif441d3a2012-04-22 15:17:52 -0700151SignedInfo.prototype.getElementLabel = function() {
152 return CCNProtocolDTags.SignedInfo;
153};
154
155SignedInfo.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