blob: f5b56e0e62c0c2b078c80c803d82ba30dbdf6886 [file] [log] [blame]
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07001/*
2 * @author: ucla-cs
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 = {
23 NAME:1,
24 KEY:2,
25 CERTIFICATE:3
26};
27
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070028var KeyLocator = function KeyLocator(_input,_type){
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070029
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070030 this.type=_type;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070031
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070032 if (_type==KeyLocatorType.NAME){
33 this.keyName = _input;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070034 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070035 else if(_type==KeyLocatorType.KEY){
Jeff Thompson8b44aa12012-11-11 18:39:47 -080036 if(LOG>4)console.log('SET KEY');
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070037 this.publicKey = _input;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070038 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070039 else if(_type==KeyLocatorType.CERTIFICATE){
40 this.certificate = _input;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070041 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070042
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070043};
44
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070045KeyLocator.prototype.from_ccnb = function(decoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070046
47 decoder.readStartElement(this.getElementLabel());
48
49 if (decoder.peekStartElement(CCNProtocolDTags.Key)) {
50 try {
51 encodedKey = decoder.readBinaryElement(CCNProtocolDTags.Key);
52 // This is a DER-encoded SubjectPublicKeyInfo.
53
54 //TODO FIX THIS, This should create a Key Object instead of keeping bytes
55
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070056 this.publicKey = encodedKey;//CryptoUtil.getPublicKey(encodedKey);
57 this.type = 2;
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070058
59
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070060 if(LOG>4) console.log('PUBLIC KEY FOUND: '+ this.publicKey);
61 //this.publicKey = encodedKey;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070062
63
64 } catch (e) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -070065 throw new Error("Cannot parse key: ", e);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070066 }
67
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070068 if (null == this.publicKey) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -070069 throw new Error("Cannot parse key: ");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070070 }
71
72 } else if ( decoder.peekStartElement(CCNProtocolDTags.Certificate)) {
73 try {
74 encodedCert = decoder.readBinaryElement(CCNProtocolDTags.Certificate);
75
76 /*
77 * Certificates not yet working
78 */
79
80 //CertificateFactory factory = CertificateFactory.getInstance("X.509");
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070081 //this.certificate = (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(encodedCert));
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070082
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070083
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070084 this.certificate = encodedCert;
85 this.type = 3;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070086
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070087 if(LOG>4) console.log('CERTIFICATE FOUND: '+ this.certificate);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070088
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070089 } catch ( e) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -070090 throw new Error("Cannot decode certificate: " + e);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070091 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070092 if (null == this.certificate) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -070093 throw new Error("Cannot parse certificate! ");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070094 }
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070095 } else {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070096 this.type = 1;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070097
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070098
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070099 this.keyName = new KeyName();
100 this.keyName.from_ccnb(decoder);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700101 }
102 decoder.readEndElement();
103 }
104
105
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700106 KeyLocator.prototype.to_ccnb = function( encoder) {
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700107
Jeff Thompson8b44aa12012-11-11 18:39:47 -0800108 if(LOG>4) console.log('type is is ' + this.type);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700109 //TODO Check if Name is missing
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700110 if (!this.validate()) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700111 throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing.");
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700112 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700113
114
115 //TODO FIX THIS TOO
116 encoder.writeStartElement(this.getElementLabel());
117
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700118 if (this.type == KeyLocatorType.KEY) {
119 if(LOG>5)console.log('About to encode a public key' +this.publicKey);
120 encoder.writeElement(CCNProtocolDTags.Key, this.publicKey);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700121
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700122 } else if (this.type == KeyLocatorType.CERTIFICATE) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700123
124 try {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700125 encoder.writeElement(CCNProtocolDTags.Certificate, this.certificate);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700126 } catch ( e) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -0700127 throw new Error("CertificateEncodingException attempting to write key locator: " + e);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700128 }
129
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700130 } else if (this.type == KeyLocatorType.NAME) {
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700131
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700132 this.keyName.to_ccnb(encoder);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700133 }
134 encoder.writeEndElement();
135
136};
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700137
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700138KeyLocator.prototype.getElementLabel = function() {
139 return CCNProtocolDTags.KeyLocator;
140};
141
142KeyLocator.prototype.validate = function() {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700143 return ( (null != this.keyName) || (null != this.publicKey) || (null != this.certificate) );
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700144};
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700145
146/**
147 * KeyName is only used by KeyLocator.
148 */
149var KeyName = function KeyName() {
150
151
152 this.contentName = this.contentName;//contentName
153 this.publisherID =this.publisherID;//publisherID
154
155};
156
157KeyName.prototype.from_ccnb=function( decoder){
158
159
160 decoder.readStartElement(this.getElementLabel());
161
162 this.contentName = new Name();
163 this.contentName.from_ccnb(decoder);
164
165 if(LOG>4) console.log('KEY NAME FOUND: ');
166
167 if ( PublisherID.peek(decoder) ) {
168 this.publisherID = new PublisherID();
169 this.publisherID.from_ccnb(decoder);
170 }
171
172 decoder.readEndElement();
173};
174
175KeyName.prototype.to_ccnb = function( encoder) {
176 if (!this.validate()) {
177 throw new Error("Cannot encode : field values missing.");
178 }
179
180 encoder.writeStartElement(this.getElementLabel());
181
182 this.contentName.to_ccnb(encoder);
183 if (null != this.publisherID)
184 this.publisherID.to_ccnb(encoder);
185
186 encoder.writeEndElement();
187};
188
189KeyName.prototype.getElementLabel = function() { return CCNProtocolDTags.KeyName; };
190
191KeyName.prototype.validate = function() {
192 // DKS -- do we do recursive validation?
193 // null signedInfo ok
194 return (null != this.contentName);
195};