blob: 73c95fb2478fce12ba4dab3af7b5aa136e4e97f5 [file] [log] [blame]
Meki Cherkaoui97e7a592012-04-14 02:50:06 -07001
2
3
4
5
6var KeyLocatorType = {
7 NAME:1,
8 KEY:2,
9 CERTIFICATE:3
10};
11
12var KeyLocator = function KeyLocator(_Input,_Type){
13
14 //this.KeyName = _KeyName;
15 //this.PublicKey = _PublicKey;
16 //this.Certificate = _Certificate;
17
18 this.Type=_Type;
19
20 if (_Type==KeyLocatorType.NAME){
21 this.KeyName = _Input;
22 }
23 else if(_Type==KeyLocatorType.KEY){
24 this.PublicKey = _Input;
25 }
26 else if(_Type==KeyLocatorType.CERTIFICATE){
27 this.Certificate = _Input;
28 }
29
30};
31
32KeyLocator.prototype.decode = function(decoder) {
33
34 decoder.readStartElement(this.getElementLabel());
35
36 if (decoder.peekStartElement(CCNProtocolDTags.Key)) {
37 try {
38 encodedKey = decoder.readBinaryElement(CCNProtocolDTags.Key);
39 // This is a DER-encoded SubjectPublicKeyInfo.
40
41 //TODO FIX THIS, SHOULDN'T be like that
42 this.Key = encodedKey;//CryptoUtil.getPublicKey(encodedKey);
43
44 this.PublicKey = encodedKey;
45
46
47 } catch (e) {
48 console.log("Cannot parse stored key: error: " + e);
49 throw new ContentDecodingException("Cannot parse key: ", e);
50 }
51
52 if (null == this.Key) {
53 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
67 } catch ( e) {
68 throw new Exception("Cannot decode certificate: " + e);
69 }
70 if (null == this.Certificate) {
71 throw new Exception("Cannot parse certificate! ");
72 }
73 } else {
74 this.KeyName = new KeyName();
75 this.KeyName.decode(decoder);
76 }
77 decoder.readEndElement();
78 }
79
80
81 KeyLocator.prototype.encode = function( encoder) {
82 /*if (!validate()) {
83 throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing.");
84 }*/
85
86
87 //TODO FIX THIS TOO
88 encoder.writeStartElement(this.getElementLabel());
89
90 if (this._Type == KeyLocatorType.KEY) {
91 console.log('HERE');
92
93 encoder.writeElement(CCNProtocolDTags.Key, this.PublicKey);
94
95 } else if (this.Type == KeyLocatorType.CERTIFICATE) {
96 console.log('NO HERE');
97
98 try {
99 encoder.writeElement(CCNProtocolDTags.Certificate, this.Certificate.getEncoded());
100 } catch ( e) {
101 throw new Exception("CertificateEncodingException attempting to write key locator: " + e);
102 }
103
104 } else if (this.Type == KeyLocatorType.NAME) {
105 console.log('ACTUALLY HERE');
106 this.KeyName.encode(encoder);
107 }
108 encoder.writeEndElement();
109
110};
111
112KeyLocator.prototype.getElementLabel = function() {
113 return CCNProtocolDTags.KeyLocator;
114};
115
116KeyLocator.prototype.validate = function() {
117 return ( (null != this.keyName) || (null != this.PublicKey) || (null != this.Certificate) );
118};
119