blob: 99fc813421e34913c2d4aec1a881b4fc0e4cba26 [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
49 decoder.readStartElement(this.getElementLabel());
50
51 if (decoder.peekStartElement(CCNProtocolDTags.Key)) {
52 try {
53 encodedKey = decoder.readBinaryElement(CCNProtocolDTags.Key);
54 // This is a DER-encoded SubjectPublicKeyInfo.
55
56 //TODO FIX THIS, This should create a Key Object instead of keeping bytes
57
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070058 this.publicKey = encodedKey;//CryptoUtil.getPublicKey(encodedKey);
Wentao Shangf8b4a7d2012-12-25 12:52:07 -080059 this.type = KeyLocatorType.KEY;
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070060
61
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070062 if(LOG>4) console.log('PUBLIC KEY FOUND: '+ this.publicKey);
63 //this.publicKey = encodedKey;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070064
65
66 } catch (e) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -070067 throw new Error("Cannot parse key: ", e);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070068 }
69
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070070 if (null == this.publicKey) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -070071 throw new Error("Cannot parse key: ");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070072 }
73
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");
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070083 //this.certificate = (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(encodedCert));
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070084
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070085
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070086 this.certificate = encodedCert;
Wentao Shangf8b4a7d2012-12-25 12:52:07 -080087 this.type = KeyLocatorType.CERTIFICATE;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070088
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070089 if(LOG>4) console.log('CERTIFICATE FOUND: '+ this.certificate);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070090
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070091 } catch ( e) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -070092 throw new Error("Cannot decode certificate: " + e);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070093 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070094 if (null == this.certificate) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -070095 throw new Error("Cannot parse certificate! ");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070096 }
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070097 } else {
Wentao Shangf8b4a7d2012-12-25 12:52:07 -080098 this.type = KeyLocatorType.KEYNAME;
99
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700100 this.keyName = new KeyName();
101 this.keyName.from_ccnb(decoder);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700102 }
103 decoder.readEndElement();
104 }
105
106
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700107 KeyLocator.prototype.to_ccnb = function( encoder) {
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700108
Jeff Thompson8b44aa12012-11-11 18:39:47 -0800109 if(LOG>4) console.log('type is is ' + this.type);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700110 //TODO Check if Name is missing
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700111 if (!this.validate()) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700112 throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing.");
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700113 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700114
115
116 //TODO FIX THIS TOO
117 encoder.writeStartElement(this.getElementLabel());
118
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700119 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
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700123 } else if (this.type == KeyLocatorType.CERTIFICATE) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700124
125 try {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700126 encoder.writeElement(CCNProtocolDTags.Certificate, this.certificate);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700127 } catch ( e) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -0700128 throw new Error("CertificateEncodingException attempting to write key locator: " + e);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700129 }
130
Wentao Shangf8b4a7d2012-12-25 12:52:07 -0800131 } else if (this.type == KeyLocatorType.KEYNAME) {
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700132
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700133 this.keyName.to_ccnb(encoder);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700134 }
135 encoder.writeEndElement();
136
137};
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() {
151
152
153 this.contentName = this.contentName;//contentName
154 this.publisherID =this.publisherID;//publisherID
155
156};
157
158KeyName.prototype.from_ccnb=function( decoder){
159
160
161 decoder.readStartElement(this.getElementLabel());
162
163 this.contentName = new Name();
164 this.contentName.from_ccnb(decoder);
165
166 if(LOG>4) console.log('KEY NAME FOUND: ');
167
168 if ( PublisherID.peek(decoder) ) {
169 this.publisherID = new PublisherID();
170 this.publisherID.from_ccnb(decoder);
171 }
172
173 decoder.readEndElement();
174};
175
176KeyName.prototype.to_ccnb = function( encoder) {
177 if (!this.validate()) {
178 throw new Error("Cannot encode : field values missing.");
179 }
180
181 encoder.writeStartElement(this.getElementLabel());
182
183 this.contentName.to_ccnb(encoder);
184 if (null != this.publisherID)
185 this.publisherID.to_ccnb(encoder);
186
187 encoder.writeEndElement();
188};
189
190KeyName.prototype.getElementLabel = function() { return CCNProtocolDTags.KeyName; };
191
192KeyName.prototype.validate = function() {
193 // DKS -- do we do recursive validation?
194 // null signedInfo ok
195 return (null != this.contentName);
196};