blob: 9d86f1e43788063431c9cfe23a87acad01019a90 [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 Thompsonc3164722012-09-30 18:35:02 -07007var Key = function Key(){
8 /* TODO: Port from PyCCN:
9 generateRSA()
10 privateToDER()
11 publicToDER()
12 privateToPEM()
13 publicToPEM()
14 fromDER()
15 fromPEM()
16 */
17}
18
19/**
20 * KeyLocator
21 */
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070022var KeyLocatorType = {
Wentao Shangf8b4a7d2012-12-25 12:52:07 -080023 KEY:1,
24 CERTIFICATE:2,
25 KEYNAME:3
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070026};
27
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070028var KeyLocator = function KeyLocator(_input,_type){
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070029
Wentao Shangf8b4a7d2012-12-25 12:52:07 -080030 this.type = _type;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070031
Wentao Shangf8b4a7d2012-12-25 12:52:07 -080032 if (_type == KeyLocatorType.KEYNAME){
33 if (LOG>3) console.log('KeyLocator: SET KEYNAME');
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070034 this.keyName = _input;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070035 }
Wentao Shangf8b4a7d2012-12-25 12:52:07 -080036 else if (_type == KeyLocatorType.KEY){
37 if (LOG>3) console.log('KeyLocator: SET KEY');
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070038 this.publicKey = _input;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070039 }
Wentao Shangf8b4a7d2012-12-25 12:52:07 -080040 else if (_type == KeyLocatorType.CERTIFICATE){
41 if (LOG>3) console.log('KeyLocator: SET CERTIFICATE');
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070042 this.certificate = _input;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070043 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070044
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070045};
46
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070047KeyLocator.prototype.from_ccnb = function(decoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070048
Wentao Shang82854bd2012-12-27 14:14:41 -080049 decoder.readStartElement(this.getElementLabel());
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070050
Wentao Shang82854bd2012-12-27 14:14:41 -080051 if (decoder.peekStartElement(CCNProtocolDTags.Key)) {
52 try {
53 encodedKey = decoder.readBinaryElement(CCNProtocolDTags.Key);
54 // This is a DER-encoded SubjectPublicKeyInfo.
Wentao Shangf8b4a7d2012-12-25 12:52:07 -080055
Wentao Shang82854bd2012-12-27 14:14:41 -080056 //TODO FIX THIS, This should create a Key Object instead of keeping bytes
57
58 this.publicKey = encodedKey;//CryptoUtil.getPublicKey(encodedKey);
59 this.type = KeyLocatorType.KEY;
60
61
62 if(LOG>4) console.log('PUBLIC KEY FOUND: '+ this.publicKey);
63 //this.publicKey = encodedKey;
64
65
66 } catch (e) {
67 throw new Error("Cannot parse key: ", e);
68 }
69
70 if (null == this.publicKey) {
71 throw new Error("Cannot parse key: ");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070072 }
Wentao Shang82854bd2012-12-27 14:14:41 -080073
74 } else if ( decoder.peekStartElement(CCNProtocolDTags.Certificate)) {
75 try {
76 encodedCert = decoder.readBinaryElement(CCNProtocolDTags.Certificate);
77
78 /*
79 * Certificates not yet working
80 */
81
82 //CertificateFactory factory = CertificateFactory.getInstance("X.509");
83 //this.certificate = (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(encodedCert));
84
85
86 this.certificate = encodedCert;
87 this.type = KeyLocatorType.CERTIFICATE;
88
89 if(LOG>4) console.log('CERTIFICATE FOUND: '+ this.certificate);
90
91 } catch ( e) {
92 throw new Error("Cannot decode certificate: " + e);
93 }
94 if (null == this.certificate) {
95 throw new Error("Cannot parse certificate! ");
96 }
97 } else {
98 this.type = KeyLocatorType.KEYNAME;
99
100 this.keyName = new KeyName();
101 this.keyName.from_ccnb(decoder);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700102 }
Wentao Shang82854bd2012-12-27 14:14:41 -0800103 decoder.readEndElement();
104};
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700105
106
Wentao Shang82854bd2012-12-27 14:14:41 -0800107KeyLocator.prototype.to_ccnb = function( encoder) {
108
109 if(LOG>4) console.log('type is is ' + this.type);
110 //TODO Check if Name is missing
111 if (!this.validate()) {
112 throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing.");
113 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700114
Wentao Shang82854bd2012-12-27 14:14:41 -0800115
116 //TODO FIX THIS TOO
117 encoder.writeStartElement(this.getElementLabel());
118
119 if (this.type == KeyLocatorType.KEY) {
120 if(LOG>5)console.log('About to encode a public key' +this.publicKey);
121 encoder.writeElement(CCNProtocolDTags.Key, this.publicKey);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700122
Wentao Shang82854bd2012-12-27 14:14:41 -0800123 } else if (this.type == KeyLocatorType.CERTIFICATE) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700124
Wentao Shang82854bd2012-12-27 14:14:41 -0800125 try {
126 encoder.writeElement(CCNProtocolDTags.Certificate, this.certificate);
127 } catch ( e) {
128 throw new Error("CertificateEncodingException attempting to write key locator: " + e);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700129 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700130
Wentao Shang82854bd2012-12-27 14:14:41 -0800131 } else if (this.type == KeyLocatorType.KEYNAME) {
132
133 this.keyName.to_ccnb(encoder);
134 }
135 encoder.writeEndElement();
136
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700137};
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700138
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700139KeyLocator.prototype.getElementLabel = function() {
140 return CCNProtocolDTags.KeyLocator;
141};
142
143KeyLocator.prototype.validate = function() {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700144 return ( (null != this.keyName) || (null != this.publicKey) || (null != this.certificate) );
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700145};
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700146
147/**
148 * KeyName is only used by KeyLocator.
149 */
150var KeyName = function KeyName() {
Wentao Shang98b595c2012-12-30 10:14:26 -0800151 this.contentName = this.contentName; //contentName
152 this.publisherID = this.publisherID; //publisherID
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700153
154};
155
156KeyName.prototype.from_ccnb=function( decoder){
157
158
159 decoder.readStartElement(this.getElementLabel());
160
161 this.contentName = new Name();
162 this.contentName.from_ccnb(decoder);
163
164 if(LOG>4) console.log('KEY NAME FOUND: ');
165
166 if ( PublisherID.peek(decoder) ) {
167 this.publisherID = new PublisherID();
168 this.publisherID.from_ccnb(decoder);
169 }
170
171 decoder.readEndElement();
172};
173
174KeyName.prototype.to_ccnb = function( encoder) {
175 if (!this.validate()) {
176 throw new Error("Cannot encode : field values missing.");
177 }
178
179 encoder.writeStartElement(this.getElementLabel());
180
181 this.contentName.to_ccnb(encoder);
182 if (null != this.publisherID)
183 this.publisherID.to_ccnb(encoder);
184
185 encoder.writeEndElement();
186};
187
188KeyName.prototype.getElementLabel = function() { return CCNProtocolDTags.KeyName; };
189
190KeyName.prototype.validate = function() {
191 // DKS -- do we do recursive validation?
192 // null signedInfo ok
193 return (null != this.contentName);
194};
Wentao Shang82854bd2012-12-27 14:14:41 -0800195