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 { |
Jeff Thompson | 48ff28a | 2013-02-18 22:53:29 -0800 | [diff] [blame] | 53 | var encodedKey = decoder.readBinaryElement(CCNProtocolDTags.Key); |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 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 { |
Jeff Thompson | 48ff28a | 2013-02-18 22:53:29 -0800 | [diff] [blame] | 76 | var encodedCert = decoder.readBinaryElement(CCNProtocolDTags.Certificate); |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 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() { |
Wentao Shang | 98b595c | 2012-12-30 10:14:26 -0800 | [diff] [blame] | 151 | this.contentName = this.contentName; //contentName |
| 152 | this.publisherID = this.publisherID; //publisherID |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 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 | }; |
Wentao Shang | 82854bd | 2012-12-27 14:14:41 -0800 | [diff] [blame] | 195 | |