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