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