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 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 12 | var KeyLocator = function KeyLocator(_input,_type){ |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 13 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 14 | this.type=_type; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 15 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 16 | if (_type==KeyLocatorType.NAME){ |
| 17 | this.keyName = _input; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 18 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 19 | else if(_type==KeyLocatorType.KEY){ |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 20 | console.log('SET KEY'); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 21 | this.publicKey = _input; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 22 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 23 | else if(_type==KeyLocatorType.CERTIFICATE){ |
| 24 | this.certificate = _input; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 25 | } |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 26 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 27 | }; |
| 28 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 29 | KeyLocator.prototype.from_ccnb = function(decoder) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 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 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 40 | this.publicKey = encodedKey;//CryptoUtil.getPublicKey(encodedKey); |
| 41 | this.type = 2; |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 42 | |
| 43 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 44 | if(LOG>4) console.log('PUBLIC KEY FOUND: '+ this.publicKey); |
| 45 | //this.publicKey = encodedKey; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 46 | |
| 47 | |
| 48 | } catch (e) { |
| 49 | throw new Exception("Cannot parse key: ", e); |
| 50 | } |
| 51 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 52 | if (null == this.publicKey) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 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"); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 65 | //this.certificate = (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(encodedCert)); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 66 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 67 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 68 | this.certificate = encodedCert; |
| 69 | this.type = 3; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 70 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 71 | if(LOG>4) console.log('CERTIFICATE FOUND: '+ this.certificate); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 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 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 76 | if (null == this.certificate) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 77 | throw new Exception("Cannot parse certificate! "); |
| 78 | } |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 79 | } else { |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 80 | this.type = 1; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 81 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 82 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 83 | this.keyName = new KeyName(); |
| 84 | this.keyName.from_ccnb(decoder); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 85 | } |
| 86 | decoder.readEndElement(); |
| 87 | } |
| 88 | |
| 89 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 90 | KeyLocator.prototype.to_ccnb = function( encoder) { |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 91 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 92 | if(LOG>2) console.log('type is is ' + this.type); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 93 | //TODO Check if Name is missing |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 94 | if (!this.validate()) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 95 | throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing."); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 96 | } |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 97 | |
| 98 | |
| 99 | //TODO FIX THIS TOO |
| 100 | encoder.writeStartElement(this.getElementLabel()); |
| 101 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 102 | if (this.type == KeyLocatorType.KEY) { |
| 103 | if(LOG>5)console.log('About to encode a public key' +this.publicKey); |
| 104 | encoder.writeElement(CCNProtocolDTags.Key, this.publicKey); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 105 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 106 | } else if (this.type == KeyLocatorType.CERTIFICATE) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 107 | |
| 108 | try { |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 109 | encoder.writeElement(CCNProtocolDTags.Certificate, this.certificate); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 110 | } catch ( e) { |
| 111 | throw new Exception("CertificateEncodingException attempting to write key locator: " + e); |
| 112 | } |
| 113 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 114 | } else if (this.type == KeyLocatorType.NAME) { |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 115 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 116 | this.keyName.to_ccnb(encoder); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 117 | } |
| 118 | encoder.writeEndElement(); |
| 119 | |
| 120 | }; |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 121 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 122 | KeyLocator.prototype.getElementLabel = function() { |
| 123 | return CCNProtocolDTags.KeyLocator; |
| 124 | }; |
| 125 | |
| 126 | KeyLocator.prototype.validate = function() { |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame^] | 127 | return ( (null != this.keyName) || (null != this.publicKey) || (null != this.certificate) ); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 128 | }; |
| 129 | |