Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * @author: ucla-cs |
| 3 | * This class represents KeyLocator Objects |
| 4 | */ |
| 5 | |
| 6 | var KeyLocatorType = { |
| 7 | NAME:1, |
| 8 | KEY:2, |
| 9 | CERTIFICATE:3 |
| 10 | }; |
| 11 | |
| 12 | var 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){ |
| 21 | this.PublicKey = _Input; |
| 22 | } |
| 23 | else if(_Type==KeyLocatorType.CERTIFICATE){ |
| 24 | this.Certificate = _Input; |
| 25 | } |
| 26 | |
| 27 | }; |
| 28 | |
| 29 | KeyLocator.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 | |
| 40 | this.Key = encodedKey;//CryptoUtil.getPublicKey(encodedKey); |
| 41 | |
| 42 | this.PublicKey = encodedKey; |
| 43 | |
| 44 | |
| 45 | } catch (e) { |
| 46 | throw new Exception("Cannot parse key: ", e); |
| 47 | } |
| 48 | |
| 49 | if (null == this.Key) { |
| 50 | throw new Exception("Cannot parse key: "); |
| 51 | } |
| 52 | |
| 53 | } else if ( decoder.peekStartElement(CCNProtocolDTags.Certificate)) { |
| 54 | try { |
| 55 | encodedCert = decoder.readBinaryElement(CCNProtocolDTags.Certificate); |
| 56 | |
| 57 | /* |
| 58 | * Certificates not yet working |
| 59 | */ |
| 60 | |
| 61 | //CertificateFactory factory = CertificateFactory.getInstance("X.509"); |
| 62 | //this.Certificate = (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(encodedCert)); |
| 63 | |
| 64 | } catch ( e) { |
| 65 | throw new Exception("Cannot decode certificate: " + e); |
| 66 | } |
| 67 | if (null == this.Certificate) { |
| 68 | throw new Exception("Cannot parse certificate! "); |
| 69 | } |
| 70 | } else { |
| 71 | this.KeyName = new KeyName(); |
| 72 | this.KeyName.decode(decoder); |
| 73 | } |
| 74 | decoder.readEndElement(); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | KeyLocator.prototype.encode = function( encoder) { |
| 79 | |
| 80 | //TODO Check if Name is missing |
| 81 | /*if (!validate()) { |
| 82 | throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing."); |
| 83 | }*/ |
| 84 | |
| 85 | |
| 86 | //TODO FIX THIS TOO |
| 87 | encoder.writeStartElement(this.getElementLabel()); |
| 88 | |
| 89 | if (this._Type == KeyLocatorType.KEY) { |
| 90 | |
| 91 | encoder.writeElement(CCNProtocolDTags.Key, this.PublicKey); |
| 92 | |
| 93 | } else if (this.Type == KeyLocatorType.CERTIFICATE) { |
| 94 | |
| 95 | try { |
| 96 | encoder.writeElement(CCNProtocolDTags.Certificate, this.Certificate.getEncoded()); |
| 97 | } catch ( e) { |
| 98 | throw new Exception("CertificateEncodingException attempting to write key locator: " + e); |
| 99 | } |
| 100 | |
| 101 | } else if (this.Type == KeyLocatorType.NAME) { |
| 102 | console.log('ACTUALLY HERE'); |
| 103 | this.KeyName.encode(encoder); |
| 104 | } |
| 105 | encoder.writeEndElement(); |
| 106 | |
| 107 | }; |
| 108 | |
| 109 | KeyLocator.prototype.getElementLabel = function() { |
| 110 | return CCNProtocolDTags.KeyLocator; |
| 111 | }; |
| 112 | |
| 113 | KeyLocator.prototype.validate = function() { |
| 114 | return ( (null != this.keyName) || (null != this.PublicKey) || (null != this.Certificate) ); |
| 115 | }; |
| 116 | |