blob: 20b3e414f3edc78d05e3dc9bc70e73ed5ba5d6fe [file] [log] [blame]
Wentao Shangbd63e462012-12-03 16:19:33 -08001/**
Jeff Thompson146d7de2012-11-17 16:15:28 -08002 * @author: Meki Cheraoui
Jeff Thompson745026e2012-10-13 12:49:20 -07003 * See COPYING for copyright and distribution information.
Jeff Thompsonc3164722012-09-30 18:35:02 -07004 * This class represents Key Objects
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07005 */
6
Jeff Thompson2b14c7e2013-07-29 15:09:56 -07007/**
8 * @constructor
9 */
Jeff Thompsonc3164722012-09-30 18:35:02 -070010var Key = function Key(){
11 /* TODO: Port from PyCCN:
12 generateRSA()
13 privateToDER()
14 publicToDER()
15 privateToPEM()
16 publicToPEM()
17 fromDER()
18 fromPEM()
19 */
20}
21
22/**
23 * KeyLocator
24 */
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070025var KeyLocatorType = {
Wentao Shangf8b4a7d2012-12-25 12:52:07 -080026 KEY:1,
27 CERTIFICATE:2,
28 KEYNAME:3
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070029};
30
Jeff Thompson2b14c7e2013-07-29 15:09:56 -070031/**
32 * @constructor
33 */
34var KeyLocator = function KeyLocator(input,type) {
35 this.type = type;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070036
Jeff Thompson2b14c7e2013-07-29 15:09:56 -070037 if (type == KeyLocatorType.KEYNAME){
38 if (LOG>3) console.log('KeyLocator: SET KEYNAME');
39 this.keyName = input;
40 }
41 else if (type == KeyLocatorType.KEY){
42 if (LOG>3) console.log('KeyLocator: SET KEY');
43 this.publicKey = input;
44 }
45 else if (type == KeyLocatorType.CERTIFICATE){
46 if (LOG>3) console.log('KeyLocator: SET CERTIFICATE');
47 this.certificate = input;
48 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070049};
50
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070051KeyLocator.prototype.from_ccnb = function(decoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070052
Wentao Shang82854bd2012-12-27 14:14:41 -080053 decoder.readStartElement(this.getElementLabel());
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070054
Wentao Shang82854bd2012-12-27 14:14:41 -080055 if (decoder.peekStartElement(CCNProtocolDTags.Key)) {
56 try {
Jeff Thompson48ff28a2013-02-18 22:53:29 -080057 var encodedKey = decoder.readBinaryElement(CCNProtocolDTags.Key);
Wentao Shang82854bd2012-12-27 14:14:41 -080058 // This is a DER-encoded SubjectPublicKeyInfo.
Wentao Shangf8b4a7d2012-12-25 12:52:07 -080059
Wentao Shang82854bd2012-12-27 14:14:41 -080060 //TODO FIX THIS, This should create a Key Object instead of keeping bytes
61
62 this.publicKey = encodedKey;//CryptoUtil.getPublicKey(encodedKey);
63 this.type = KeyLocatorType.KEY;
64
65
66 if(LOG>4) console.log('PUBLIC KEY FOUND: '+ this.publicKey);
67 //this.publicKey = encodedKey;
68
69
70 } catch (e) {
71 throw new Error("Cannot parse key: ", e);
72 }
73
74 if (null == this.publicKey) {
75 throw new Error("Cannot parse key: ");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070076 }
Wentao Shang82854bd2012-12-27 14:14:41 -080077
78 } else if ( decoder.peekStartElement(CCNProtocolDTags.Certificate)) {
79 try {
Jeff Thompson48ff28a2013-02-18 22:53:29 -080080 var encodedCert = decoder.readBinaryElement(CCNProtocolDTags.Certificate);
Wentao Shang82854bd2012-12-27 14:14:41 -080081
82 /*
83 * Certificates not yet working
84 */
85
86 //CertificateFactory factory = CertificateFactory.getInstance("X.509");
87 //this.certificate = (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(encodedCert));
88
89
90 this.certificate = encodedCert;
91 this.type = KeyLocatorType.CERTIFICATE;
92
93 if(LOG>4) console.log('CERTIFICATE FOUND: '+ this.certificate);
94
95 } catch ( e) {
96 throw new Error("Cannot decode certificate: " + e);
97 }
98 if (null == this.certificate) {
99 throw new Error("Cannot parse certificate! ");
100 }
101 } else {
102 this.type = KeyLocatorType.KEYNAME;
103
104 this.keyName = new KeyName();
105 this.keyName.from_ccnb(decoder);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700106 }
Wentao Shang82854bd2012-12-27 14:14:41 -0800107 decoder.readEndElement();
108};
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700109
110
Wentao Shang82854bd2012-12-27 14:14:41 -0800111KeyLocator.prototype.to_ccnb = function( encoder) {
112
113 if(LOG>4) console.log('type is is ' + this.type);
114 //TODO Check if Name is missing
115 if (!this.validate()) {
116 throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing.");
117 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700118
Wentao Shang82854bd2012-12-27 14:14:41 -0800119
120 //TODO FIX THIS TOO
121 encoder.writeStartElement(this.getElementLabel());
122
123 if (this.type == KeyLocatorType.KEY) {
124 if(LOG>5)console.log('About to encode a public key' +this.publicKey);
125 encoder.writeElement(CCNProtocolDTags.Key, this.publicKey);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700126
Wentao Shang82854bd2012-12-27 14:14:41 -0800127 } else if (this.type == KeyLocatorType.CERTIFICATE) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700128
Wentao Shang82854bd2012-12-27 14:14:41 -0800129 try {
130 encoder.writeElement(CCNProtocolDTags.Certificate, this.certificate);
131 } catch ( e) {
132 throw new Error("CertificateEncodingException attempting to write key locator: " + e);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700133 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700134
Wentao Shang82854bd2012-12-27 14:14:41 -0800135 } else if (this.type == KeyLocatorType.KEYNAME) {
136
137 this.keyName.to_ccnb(encoder);
138 }
139 encoder.writeEndElement();
140
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700141};
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700142
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700143KeyLocator.prototype.getElementLabel = function() {
144 return CCNProtocolDTags.KeyLocator;
145};
146
147KeyLocator.prototype.validate = function() {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700148 return ( (null != this.keyName) || (null != this.publicKey) || (null != this.certificate) );
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700149};
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700150
151/**
152 * KeyName is only used by KeyLocator.
Jeff Thompson2b14c7e2013-07-29 15:09:56 -0700153 * @constructor
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700154 */
155var KeyName = function KeyName() {
Wentao Shang98b595c2012-12-30 10:14:26 -0800156 this.contentName = this.contentName; //contentName
157 this.publisherID = this.publisherID; //publisherID
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700158
159};
160
161KeyName.prototype.from_ccnb=function( decoder){
162
163
164 decoder.readStartElement(this.getElementLabel());
165
166 this.contentName = new Name();
167 this.contentName.from_ccnb(decoder);
168
169 if(LOG>4) console.log('KEY NAME FOUND: ');
170
171 if ( PublisherID.peek(decoder) ) {
172 this.publisherID = new PublisherID();
173 this.publisherID.from_ccnb(decoder);
174 }
175
176 decoder.readEndElement();
177};
178
179KeyName.prototype.to_ccnb = function( encoder) {
180 if (!this.validate()) {
181 throw new Error("Cannot encode : field values missing.");
182 }
183
184 encoder.writeStartElement(this.getElementLabel());
185
186 this.contentName.to_ccnb(encoder);
187 if (null != this.publisherID)
188 this.publisherID.to_ccnb(encoder);
189
190 encoder.writeEndElement();
191};
192
193KeyName.prototype.getElementLabel = function() { return CCNProtocolDTags.KeyName; };
194
195KeyName.prototype.validate = function() {
196 // DKS -- do we do recursive validation?
197 // null signedInfo ok
198 return (null != this.contentName);
199};
Wentao Shang82854bd2012-12-27 14:14:41 -0800200