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