blob: 9ada28b9dd516e3749ad9eb94387604e3ba40466 [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
12var KeyLocator = function KeyLocator(_Input,_Type){
13
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070014 this.Type=_Type;
15
16 if (_Type==KeyLocatorType.NAME){
17 this.KeyName = _Input;
18 }
19 else if(_Type==KeyLocatorType.KEY){
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070020 console.log('SET KEY');
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070021 this.PublicKey = _Input;
22 }
23 else if(_Type==KeyLocatorType.CERTIFICATE){
24 this.Certificate = _Input;
25 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070026
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070027};
28
29KeyLocator.prototype.decode = function(decoder) {
30
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
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070040 this.PublicKey = encodedKey;//CryptoUtil.getPublicKey(encodedKey);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070041 this.Type = 2;
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070042
43
44 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) {
49 throw new Exception("Cannot parse key: ", e);
50 }
51
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070052 if (null == this.PublicKey) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070053 throw new Exception("Cannot parse key: ");
54 }
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");
65 //this.Certificate = (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(encodedCert));
66
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070067
68 this.Certificate = encodedCert;
Meki Cherkaoui8f173612012-06-06 01:05:40 -070069 this.Type = 3;
70
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070071 if(LOG>4) console.log('CERTIFICATE FOUND: '+ this.Certificate);
72
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070073 } catch ( e) {
74 throw new Exception("Cannot decode certificate: " + e);
75 }
76 if (null == this.Certificate) {
77 throw new Exception("Cannot parse certificate! ");
78 }
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070079 } else {
Meki Cherkaoui8f173612012-06-06 01:05:40 -070080 this.Type = 1;
81
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070082
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070083 this.KeyName = new KeyName();
84 this.KeyName.decode(decoder);
85 }
86 decoder.readEndElement();
87 }
88
89
90 KeyLocator.prototype.encode = function( encoder) {
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070091
92 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
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700102 if (this.Type == KeyLocatorType.KEY) {
103 if(LOG>5)console.log('About to encode a public key' +this.PublicKey);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700104 encoder.writeElement(CCNProtocolDTags.Key, this.PublicKey);
105
106 } else if (this.Type == KeyLocatorType.CERTIFICATE) {
107
108 try {
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700109 encoder.writeElement(CCNProtocolDTags.Certificate, this.Certificate);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700110 } catch ( e) {
111 throw new Exception("CertificateEncodingException attempting to write key locator: " + e);
112 }
113
114 } else if (this.Type == KeyLocatorType.NAME) {
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700115
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700116 this.KeyName.encode(encoder);
117 }
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() {
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700127 return ( (null != this.KeyName) || (null != this.PublicKey) || (null != this.Certificate) );
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700128};
129