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){ |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 21 | console.log('SET KEY'); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 22 | this.PublicKey = _Input; |
| 23 | } |
| 24 | else if(_Type==KeyLocatorType.CERTIFICATE){ |
| 25 | this.Certificate = _Input; |
| 26 | } |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 27 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | KeyLocator.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 Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 41 | this.PublicKey = encodedKey;//CryptoUtil.getPublicKey(encodedKey); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 42 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 43 | |
| 44 | |
| 45 | if(LOG>4) console.log('PUBLIC KEY FOUND: '+ this.PublicKey); |
| 46 | //this.PublicKey = encodedKey; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 47 | |
| 48 | |
| 49 | } catch (e) { |
| 50 | throw new Exception("Cannot parse key: ", e); |
| 51 | } |
| 52 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 53 | if (null == this.PublicKey) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 54 | 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 Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 68 | |
| 69 | this.Certificate = encodedCert; |
| 70 | |
| 71 | if(LOG>4) console.log('CERTIFICATE FOUND: '+ this.Certificate); |
| 72 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 73 | } 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 Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 79 | } else { |
| 80 | |
| 81 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 82 | this.KeyName = new KeyName(); |
| 83 | this.KeyName.decode(decoder); |
| 84 | } |
| 85 | decoder.readEndElement(); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | KeyLocator.prototype.encode = function( encoder) { |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 90 | |
| 91 | if(LOG>2) console.log('type is is ' + this.Type); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 92 | //TODO Check if Name is missing |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 93 | if (!this.validate()) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 94 | throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing."); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 95 | } |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 96 | |
| 97 | |
| 98 | //TODO FIX THIS TOO |
| 99 | encoder.writeStartElement(this.getElementLabel()); |
| 100 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 101 | if (this.Type == KeyLocatorType.KEY) { |
| 102 | if(LOG>5)console.log('About to encode a public key' +this.PublicKey); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 103 | 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 Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 114 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 115 | this.KeyName.encode(encoder); |
| 116 | } |
| 117 | encoder.writeEndElement(); |
| 118 | |
| 119 | }; |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 120 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 121 | KeyLocator.prototype.getElementLabel = function() { |
| 122 | return CCNProtocolDTags.KeyLocator; |
| 123 | }; |
| 124 | |
| 125 | KeyLocator.prototype.validate = function() { |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 126 | return ( (null != this.KeyName) || (null != this.PublicKey) || (null != this.Certificate) ); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 127 | }; |
| 128 | |