Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * @author: ucla-cs |
Jeff Thompson | c316472 | 2012-09-30 18:35:02 -0700 | [diff] [blame^] | 3 | * This class represents Key Objects |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Jeff Thompson | c316472 | 2012-09-30 18:35:02 -0700 | [diff] [blame^] | 6 | var Key = function Key(){ |
| 7 | /* TODO: Port from PyCCN: |
| 8 | generateRSA() |
| 9 | privateToDER() |
| 10 | publicToDER() |
| 11 | privateToPEM() |
| 12 | publicToPEM() |
| 13 | fromDER() |
| 14 | fromPEM() |
| 15 | */ |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * KeyLocator |
| 20 | */ |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 21 | var KeyLocatorType = { |
| 22 | NAME:1, |
| 23 | KEY:2, |
| 24 | CERTIFICATE:3 |
| 25 | }; |
| 26 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 27 | var KeyLocator = function KeyLocator(_input,_type){ |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 28 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 29 | this.type=_type; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 30 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 31 | if (_type==KeyLocatorType.NAME){ |
| 32 | this.keyName = _input; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 33 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 34 | else if(_type==KeyLocatorType.KEY){ |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 35 | console.log('SET KEY'); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 36 | this.publicKey = _input; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 37 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 38 | else if(_type==KeyLocatorType.CERTIFICATE){ |
| 39 | this.certificate = _input; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 40 | } |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 41 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 44 | KeyLocator.prototype.from_ccnb = function(decoder) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 45 | |
| 46 | decoder.readStartElement(this.getElementLabel()); |
| 47 | |
| 48 | if (decoder.peekStartElement(CCNProtocolDTags.Key)) { |
| 49 | try { |
| 50 | encodedKey = decoder.readBinaryElement(CCNProtocolDTags.Key); |
| 51 | // This is a DER-encoded SubjectPublicKeyInfo. |
| 52 | |
| 53 | //TODO FIX THIS, This should create a Key Object instead of keeping bytes |
| 54 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 55 | this.publicKey = encodedKey;//CryptoUtil.getPublicKey(encodedKey); |
| 56 | this.type = 2; |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 57 | |
| 58 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 59 | if(LOG>4) console.log('PUBLIC KEY FOUND: '+ this.publicKey); |
| 60 | //this.publicKey = encodedKey; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 61 | |
| 62 | |
| 63 | } catch (e) { |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 64 | throw new Error("Cannot parse key: ", e); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 67 | if (null == this.publicKey) { |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 68 | throw new Error("Cannot parse key: "); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | } else if ( decoder.peekStartElement(CCNProtocolDTags.Certificate)) { |
| 72 | try { |
| 73 | encodedCert = decoder.readBinaryElement(CCNProtocolDTags.Certificate); |
| 74 | |
| 75 | /* |
| 76 | * Certificates not yet working |
| 77 | */ |
| 78 | |
| 79 | //CertificateFactory factory = CertificateFactory.getInstance("X.509"); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 80 | //this.certificate = (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(encodedCert)); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -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.certificate = encodedCert; |
| 84 | this.type = 3; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 85 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 86 | if(LOG>4) console.log('CERTIFICATE FOUND: '+ this.certificate); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 87 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 88 | } catch ( e) { |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 89 | throw new Error("Cannot decode certificate: " + e); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 90 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 91 | if (null == this.certificate) { |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 92 | throw new Error("Cannot parse certificate! "); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 93 | } |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 94 | } else { |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 95 | this.type = 1; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 96 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 97 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 98 | this.keyName = new KeyName(); |
| 99 | this.keyName.from_ccnb(decoder); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 100 | } |
| 101 | decoder.readEndElement(); |
| 102 | } |
| 103 | |
| 104 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 105 | KeyLocator.prototype.to_ccnb = function( encoder) { |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 106 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 107 | if(LOG>2) console.log('type is is ' + this.type); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 108 | //TODO Check if Name is missing |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 109 | if (!this.validate()) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 110 | throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing."); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 111 | } |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 112 | |
| 113 | |
| 114 | //TODO FIX THIS TOO |
| 115 | encoder.writeStartElement(this.getElementLabel()); |
| 116 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 117 | if (this.type == KeyLocatorType.KEY) { |
| 118 | if(LOG>5)console.log('About to encode a public key' +this.publicKey); |
| 119 | encoder.writeElement(CCNProtocolDTags.Key, this.publicKey); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 120 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 121 | } else if (this.type == KeyLocatorType.CERTIFICATE) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 122 | |
| 123 | try { |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 124 | encoder.writeElement(CCNProtocolDTags.Certificate, this.certificate); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 125 | } catch ( e) { |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 126 | throw new Error("CertificateEncodingException attempting to write key locator: " + e); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 129 | } else if (this.type == KeyLocatorType.NAME) { |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 130 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 131 | this.keyName.to_ccnb(encoder); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 132 | } |
| 133 | encoder.writeEndElement(); |
| 134 | |
| 135 | }; |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 136 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 137 | KeyLocator.prototype.getElementLabel = function() { |
| 138 | return CCNProtocolDTags.KeyLocator; |
| 139 | }; |
| 140 | |
| 141 | KeyLocator.prototype.validate = function() { |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 142 | return ( (null != this.keyName) || (null != this.publicKey) || (null != this.certificate) ); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 143 | }; |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 144 | |
| 145 | /** |
| 146 | * KeyName is only used by KeyLocator. |
| 147 | */ |
| 148 | var KeyName = function KeyName() { |
| 149 | |
| 150 | |
| 151 | this.contentName = this.contentName;//contentName |
| 152 | this.publisherID =this.publisherID;//publisherID |
| 153 | |
| 154 | }; |
| 155 | |
| 156 | KeyName.prototype.from_ccnb=function( decoder){ |
| 157 | |
| 158 | |
| 159 | decoder.readStartElement(this.getElementLabel()); |
| 160 | |
| 161 | this.contentName = new Name(); |
| 162 | this.contentName.from_ccnb(decoder); |
| 163 | |
| 164 | if(LOG>4) console.log('KEY NAME FOUND: '); |
| 165 | |
| 166 | if ( PublisherID.peek(decoder) ) { |
| 167 | this.publisherID = new PublisherID(); |
| 168 | this.publisherID.from_ccnb(decoder); |
| 169 | } |
| 170 | |
| 171 | decoder.readEndElement(); |
| 172 | }; |
| 173 | |
| 174 | KeyName.prototype.to_ccnb = function( encoder) { |
| 175 | if (!this.validate()) { |
| 176 | throw new Error("Cannot encode : field values missing."); |
| 177 | } |
| 178 | |
| 179 | encoder.writeStartElement(this.getElementLabel()); |
| 180 | |
| 181 | this.contentName.to_ccnb(encoder); |
| 182 | if (null != this.publisherID) |
| 183 | this.publisherID.to_ccnb(encoder); |
| 184 | |
| 185 | encoder.writeEndElement(); |
| 186 | }; |
| 187 | |
| 188 | KeyName.prototype.getElementLabel = function() { return CCNProtocolDTags.KeyName; }; |
| 189 | |
| 190 | KeyName.prototype.validate = function() { |
| 191 | // DKS -- do we do recursive validation? |
| 192 | // null signedInfo ok |
| 193 | return (null != this.contentName); |
| 194 | }; |