blob: 14f283cd7b7037d2ffbdccbcfea9fccdaea855f1 [file] [log] [blame]
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07001/*
2 * @author: ucla-cs
3 * This class represents KeyLocator Objects
4 */
5
6var KeyLocatorType = {
7 NAME:1,
8 KEY:2,
9 CERTIFICATE:3
10};
11
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070012var KeyLocator = function KeyLocator(_input,_type){
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070013
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070014 this.type=_type;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070015
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070016 if (_type==KeyLocatorType.NAME){
17 this.keyName = _input;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070018 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070019 else if(_type==KeyLocatorType.KEY){
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070020 console.log('SET KEY');
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070021 this.publicKey = _input;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070022 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070023 else if(_type==KeyLocatorType.CERTIFICATE){
24 this.certificate = _input;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070025 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070026
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070027};
28
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070029KeyLocator.prototype.from_ccnb = function(decoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070030
31 decoder.readStartElement(this.getElementLabel());
32
33 if (decoder.peekStartElement(CCNProtocolDTags.Key)) {
34 try {
35 encodedKey = decoder.readBinaryElement(CCNProtocolDTags.Key);
36 // This is a DER-encoded SubjectPublicKeyInfo.
37
38 //TODO FIX THIS, This should create a Key Object instead of keeping bytes
39
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070040 this.publicKey = encodedKey;//CryptoUtil.getPublicKey(encodedKey);
41 this.type = 2;
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070042
43
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070044 if(LOG>4) console.log('PUBLIC KEY FOUND: '+ this.publicKey);
45 //this.publicKey = encodedKey;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070046
47
48 } catch (e) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -070049 throw new Error("Cannot parse key: ", e);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070050 }
51
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070052 if (null == this.publicKey) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -070053 throw new Error("Cannot parse key: ");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070054 }
55
56 } else if ( decoder.peekStartElement(CCNProtocolDTags.Certificate)) {
57 try {
58 encodedCert = decoder.readBinaryElement(CCNProtocolDTags.Certificate);
59
60 /*
61 * Certificates not yet working
62 */
63
64 //CertificateFactory factory = CertificateFactory.getInstance("X.509");
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070065 //this.certificate = (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(encodedCert));
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070066
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070067
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070068 this.certificate = encodedCert;
69 this.type = 3;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070070
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070071 if(LOG>4) console.log('CERTIFICATE FOUND: '+ this.certificate);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070072
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070073 } catch ( e) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -070074 throw new Error("Cannot decode certificate: " + e);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070075 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070076 if (null == this.certificate) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -070077 throw new Error("Cannot parse certificate! ");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070078 }
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070079 } else {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070080 this.type = 1;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070081
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070082
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070083 this.keyName = new KeyName();
84 this.keyName.from_ccnb(decoder);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070085 }
86 decoder.readEndElement();
87 }
88
89
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070090 KeyLocator.prototype.to_ccnb = function( encoder) {
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070091
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070092 if(LOG>2) console.log('type is is ' + this.type);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070093 //TODO Check if Name is missing
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070094 if (!this.validate()) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070095 throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing.");
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070096 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070097
98
99 //TODO FIX THIS TOO
100 encoder.writeStartElement(this.getElementLabel());
101
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700102 if (this.type == KeyLocatorType.KEY) {
103 if(LOG>5)console.log('About to encode a public key' +this.publicKey);
104 encoder.writeElement(CCNProtocolDTags.Key, this.publicKey);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700105
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700106 } else if (this.type == KeyLocatorType.CERTIFICATE) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700107
108 try {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700109 encoder.writeElement(CCNProtocolDTags.Certificate, this.certificate);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700110 } catch ( e) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -0700111 throw new Error("CertificateEncodingException attempting to write key locator: " + e);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700112 }
113
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700114 } else if (this.type == KeyLocatorType.NAME) {
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700115
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700116 this.keyName.to_ccnb(encoder);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700117 }
118 encoder.writeEndElement();
119
120};
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700121
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700122KeyLocator.prototype.getElementLabel = function() {
123 return CCNProtocolDTags.KeyLocator;
124};
125
126KeyLocator.prototype.validate = function() {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700127 return ( (null != this.keyName) || (null != this.publicKey) || (null != this.certificate) );
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700128};
129