blob: 7d230dac1d7b397854c6c82e10a0afe9309453d4 [file] [log] [blame]
Meki Cherkaouib0365a72012-02-18 00:59:57 -08001var CCNProtocolDTags = require('./CCNProtocolDTags').CCNProtocolDTags;
2
3var KeyLocatorType = function(){
4
5};
6
7
8exports.KeyLocatorType = {
9 NAME:1,
10 KEY:2,
11 CERTIFICATE:3
12};
13
14exports.KeyLocatorType = KeyLocatorType;
15
16var KeyLocator = function KeyLocator(_Input,_Type){
17
18 //this.KeyName = _KeyName;
19 //this.PublicKey = _PublicKey;
20 //this.Certificate = _Certificate;
21
22 this.Type=_Type;
23
24 if (_Type==KeyLocatorType.NAME){
25 this.KeyName = _Input;
26 }
27 else if(_Type==KeyLocatorType.KEY){
28 this.PublicKey = _Input;
29 }
30 else if(_Type==KeyLocatorType.CERTIFICATE){
31 this.Certificate = _Input;
32 }
33
34};
35
36exports.KeyLocator = KeyLocator;
37
38KeyLocator.prototype.decode = function(decoder) {
39
40 decoder.readStartElement(this.getElementLabel());
41
42 if (decoder.peekStartElement(CCNProtocolDTags.Key)) {
43 try {
44 encodedKey = decoder.readBinaryElement(CCNProtocolDTags.Key);
45 // This is a DER-encoded SubjectPublicKeyInfo.
46
47 //TODO FIX THIS, SHOULDN'T be like that
48 //this.Key = CryptoUtil.getPublicKey(encodedKey);
49
50 this.PublicKey = encodedKey;
51
52
53 } catch (e) {
54 console.log("Cannot parse stored key: error: " + e);
55 throw new ContentDecodingException("Cannot parse key: ", e);
56 }
57
58 if (null == Key) {
59 throw new Exception("Cannot parse key: ");
60 }
61
62 } else if ( decoder.peekStartElement(CCNProtocolDTags.Certificate)) {
63 try {
64 encodedCert = decoder.readBinaryElement(CCNProtocolDTags.Certificate);
65 /*
66 * Certificates not yet working
67 */
68
69 //CertificateFactory factory = CertificateFactory.getInstance("X.509");
70 //this.Certificate = (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(encodedCert));
71
72 } catch ( e) {
73 throw new Exception("Cannot decode certificate: " + e);
74 }
75 if (null == this.Certificate) {
76 throw new Exception("Cannot parse certificate! ");
77 }
78 } else {
79 this.KeyName = new KeyName();
80 this.KeyName.decode(decoder);
81 }
82 decoder.readEndElement();
83 }
84
85
86 KeyLocator.prototype.encode = function( encoder) {
87 /*if (!validate()) {
88 throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing.");
89 }*/
90
91
92 //TODO FIX THIS TOO
93 encoder.writeStartElement(this.getElementLabel());
94 if (this._Type == KeyLocatorType.KEY) {
95 encoder.writeElement(CCNProtocolDTags.Key, this.PublicKey.getEncoded() );
96
97 } else if (this.Type == KeyLocatorType.CERTIFICATE) {
98 try {
99 encoder.writeElement(CCNProtocolDTags.Certificate, this.Certificate.getEncoded());
100 } catch ( e) {
101
102 throw new Exception("CertificateEncodingException attempting to write key locator: " + e);
103 }
104
105 } else if (this.Type == KeyLocatorType.NAME) {
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