blob: d495fe3be51643f2183d6307082faea15f0d7bff [file] [log] [blame]
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07001/*
2 * @author: ucla-cs
Jeff Thompsonc3164722012-09-30 18:35:02 -07003 * This class represents Key Objects
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07004 */
5
Jeff Thompsonc3164722012-09-30 18:35:02 -07006var Key = function Key(){
7 /* TODO: Port from PyCCN:
8 generateRSA()
9 privateToDER()
10 publicToDER()
11 privateToPEM()
12 publicToPEM()
13 fromDER()
14 fromPEM()
15 */
16}
17
18/**
19 * KeyLocator
20 */
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070021var KeyLocatorType = {
22 NAME:1,
23 KEY:2,
24 CERTIFICATE:3
25};
26
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070027var KeyLocator = function KeyLocator(_input,_type){
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070028
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070029 this.type=_type;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070030
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070031 if (_type==KeyLocatorType.NAME){
32 this.keyName = _input;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070033 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070034 else if(_type==KeyLocatorType.KEY){
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070035 console.log('SET KEY');
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070036 this.publicKey = _input;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070037 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070038 else if(_type==KeyLocatorType.CERTIFICATE){
39 this.certificate = _input;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070040 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070041
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070042};
43
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070044KeyLocator.prototype.from_ccnb = function(decoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070045
46 decoder.readStartElement(this.getElementLabel());
47
48 if (decoder.peekStartElement(CCNProtocolDTags.Key)) {
49 try {
50 encodedKey = decoder.readBinaryElement(CCNProtocolDTags.Key);
51 // This is a DER-encoded SubjectPublicKeyInfo.
52
53 //TODO FIX THIS, This should create a Key Object instead of keeping bytes
54
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070055 this.publicKey = encodedKey;//CryptoUtil.getPublicKey(encodedKey);
56 this.type = 2;
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070057
58
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070059 if(LOG>4) console.log('PUBLIC KEY FOUND: '+ this.publicKey);
60 //this.publicKey = encodedKey;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070061
62
63 } catch (e) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -070064 throw new Error("Cannot parse key: ", e);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070065 }
66
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070067 if (null == this.publicKey) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -070068 throw new Error("Cannot parse key: ");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070069 }
70
71 } else if ( decoder.peekStartElement(CCNProtocolDTags.Certificate)) {
72 try {
73 encodedCert = decoder.readBinaryElement(CCNProtocolDTags.Certificate);
74
75 /*
76 * Certificates not yet working
77 */
78
79 //CertificateFactory factory = CertificateFactory.getInstance("X.509");
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070080 //this.certificate = (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(encodedCert));
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070081
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070082
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070083 this.certificate = encodedCert;
84 this.type = 3;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070085
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070086 if(LOG>4) console.log('CERTIFICATE FOUND: '+ this.certificate);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070087
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070088 } catch ( e) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -070089 throw new Error("Cannot decode certificate: " + e);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070090 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070091 if (null == this.certificate) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -070092 throw new Error("Cannot parse certificate! ");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070093 }
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070094 } else {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070095 this.type = 1;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070096
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070097
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070098 this.keyName = new KeyName();
99 this.keyName.from_ccnb(decoder);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700100 }
101 decoder.readEndElement();
102 }
103
104
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700105 KeyLocator.prototype.to_ccnb = function( encoder) {
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700106
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700107 if(LOG>2) console.log('type is is ' + this.type);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700108 //TODO Check if Name is missing
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700109 if (!this.validate()) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700110 throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing.");
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700111 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700112
113
114 //TODO FIX THIS TOO
115 encoder.writeStartElement(this.getElementLabel());
116
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700117 if (this.type == KeyLocatorType.KEY) {
118 if(LOG>5)console.log('About to encode a public key' +this.publicKey);
119 encoder.writeElement(CCNProtocolDTags.Key, this.publicKey);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700120
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700121 } else if (this.type == KeyLocatorType.CERTIFICATE) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700122
123 try {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700124 encoder.writeElement(CCNProtocolDTags.Certificate, this.certificate);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700125 } catch ( e) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -0700126 throw new Error("CertificateEncodingException attempting to write key locator: " + e);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700127 }
128
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700129 } else if (this.type == KeyLocatorType.NAME) {
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700130
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700131 this.keyName.to_ccnb(encoder);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700132 }
133 encoder.writeEndElement();
134
135};
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700136
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700137KeyLocator.prototype.getElementLabel = function() {
138 return CCNProtocolDTags.KeyLocator;
139};
140
141KeyLocator.prototype.validate = function() {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700142 return ( (null != this.keyName) || (null != this.publicKey) || (null != this.certificate) );
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700143};
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700144
145/**
146 * KeyName is only used by KeyLocator.
147 */
148var KeyName = function KeyName() {
149
150
151 this.contentName = this.contentName;//contentName
152 this.publisherID =this.publisherID;//publisherID
153
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};