blob: d61ed12bf4212e1d66aac849f61a08a3239c43c3 [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
14
15 this.Type=_Type;
16
17 if (_Type==KeyLocatorType.NAME){
18 this.KeyName = _Input;
19 }
20 else if(_Type==KeyLocatorType.KEY){
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070021 console.log('SET KEY');
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070022 this.PublicKey = _Input;
23 }
24 else if(_Type==KeyLocatorType.CERTIFICATE){
25 this.Certificate = _Input;
26 }
Meki Cherkaouiabb973b2012-05-09 14:25:57 -070027
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070028};
29
30KeyLocator.prototype.decode = function(decoder) {
31
32 decoder.readStartElement(this.getElementLabel());
33
34 if (decoder.peekStartElement(CCNProtocolDTags.Key)) {
35 try {
36 encodedKey = decoder.readBinaryElement(CCNProtocolDTags.Key);
37 // This is a DER-encoded SubjectPublicKeyInfo.
38
39 //TODO FIX THIS, This should create a Key Object instead of keeping bytes
40
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070041 this.PublicKey = encodedKey;//CryptoUtil.getPublicKey(encodedKey);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070042
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070043
44
45 if(LOG>4) console.log('PUBLIC KEY FOUND: '+ this.PublicKey);
46 //this.PublicKey = encodedKey;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070047
48
49 } catch (e) {
50 throw new Exception("Cannot parse key: ", e);
51 }
52
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070053 if (null == this.PublicKey) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070054 throw new Exception("Cannot parse key: ");
55 }
56
57 } else if ( decoder.peekStartElement(CCNProtocolDTags.Certificate)) {
58 try {
59 encodedCert = decoder.readBinaryElement(CCNProtocolDTags.Certificate);
60
61 /*
62 * Certificates not yet working
63 */
64
65 //CertificateFactory factory = CertificateFactory.getInstance("X.509");
66 //this.Certificate = (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(encodedCert));
67
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070068
69 this.Certificate = encodedCert;
70
71 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 {
80
81
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070082 this.KeyName = new KeyName();
83 this.KeyName.decode(decoder);
84 }
85 decoder.readEndElement();
86 }
87
88
89 KeyLocator.prototype.encode = function( encoder) {
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070090
91 if(LOG>2) console.log('type is is ' + this.Type);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070092 //TODO Check if Name is missing
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070093 if (!this.validate()) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070094 throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing.");
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070095 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070096
97
98 //TODO FIX THIS TOO
99 encoder.writeStartElement(this.getElementLabel());
100
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700101 if (this.Type == KeyLocatorType.KEY) {
102 if(LOG>5)console.log('About to encode a public key' +this.PublicKey);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700103 encoder.writeElement(CCNProtocolDTags.Key, this.PublicKey);
104
105 } else if (this.Type == KeyLocatorType.CERTIFICATE) {
106
107 try {
108 encoder.writeElement(CCNProtocolDTags.Certificate, this.Certificate.getEncoded());
109 } catch ( e) {
110 throw new Exception("CertificateEncodingException attempting to write key locator: " + e);
111 }
112
113 } else if (this.Type == KeyLocatorType.NAME) {
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700114
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700115 this.KeyName.encode(encoder);
116 }
117 encoder.writeEndElement();
118
119};
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700120
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700121KeyLocator.prototype.getElementLabel = function() {
122 return CCNProtocolDTags.KeyLocator;
123};
124
125KeyLocator.prototype.validate = function() {
Meki Cherkaouif3d8f692012-05-18 15:44:28 -0700126 return ( (null != this.KeyName) || (null != this.PublicKey) || (null != this.Certificate) );
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700127};
128