| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "config.h" | 31 #include "config.h" |
| 32 #include "modules/crypto/Key.h" | 32 #include "modules/crypto/Key.h" |
| 33 | 33 |
| 34 #include "bindings/v8/ExceptionState.h" | 34 #include "bindings/v8/ExceptionState.h" |
| 35 #include "core/dom/ExceptionCode.h" | 35 #include "core/dom/ExceptionCode.h" |
| 36 #include "modules/crypto/Algorithm.h" | 36 #include "modules/crypto/KeyAlgorithm.h" |
| 37 #include "platform/CryptoResult.h" | 37 #include "platform/CryptoResult.h" |
| 38 #include "public/platform/WebCryptoAlgorithmParams.h" | 38 #include "public/platform/WebCryptoAlgorithmParams.h" |
| 39 #include "public/platform/WebString.h" | 39 #include "public/platform/WebString.h" |
| 40 | 40 |
| 41 namespace WebCore { | 41 namespace WebCore { |
| 42 | 42 |
| 43 namespace { | 43 namespace { |
| 44 | 44 |
| 45 const char* keyTypeToString(blink::WebCryptoKeyType type) | 45 const char* keyTypeToString(blink::WebCryptoKeyType type) |
| 46 { | 46 { |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 case Digest: | 113 case Digest: |
| 114 case GenerateKey: | 114 case GenerateKey: |
| 115 case ImportKey: | 115 case ImportKey: |
| 116 break; | 116 break; |
| 117 } | 117 } |
| 118 | 118 |
| 119 ASSERT_NOT_REACHED(); | 119 ASSERT_NOT_REACHED(); |
| 120 return 0; | 120 return 0; |
| 121 } | 121 } |
| 122 | 122 |
| 123 bool getHmacHashId(const blink::WebCryptoAlgorithm& algorithm, blink::WebCryptoA
lgorithmId& hashId) | |
| 124 { | |
| 125 if (algorithm.hmacParams()) { | |
| 126 hashId = algorithm.hmacParams()->hash().id(); | |
| 127 return true; | |
| 128 } | |
| 129 if (algorithm.hmacKeyParams()) { | |
| 130 hashId = algorithm.hmacKeyParams()->hash().id(); | |
| 131 return true; | |
| 132 } | |
| 133 return false; | |
| 134 } | |
| 135 | |
| 136 } // namespace | 123 } // namespace |
| 137 | 124 |
| 138 Key::~Key() | 125 Key::~Key() |
| 139 { | 126 { |
| 140 } | 127 } |
| 141 | 128 |
| 142 Key::Key(const blink::WebCryptoKey& key) | 129 Key::Key(const blink::WebCryptoKey& key) |
| 143 : m_key(key) | 130 : m_key(key) |
| 144 { | 131 { |
| 145 ScriptWrappable::init(this); | 132 ScriptWrappable::init(this); |
| 146 } | 133 } |
| 147 | 134 |
| 148 String Key::type() const | 135 String Key::type() const |
| 149 { | 136 { |
| 150 return keyTypeToString(m_key.type()); | 137 return keyTypeToString(m_key.type()); |
| 151 } | 138 } |
| 152 | 139 |
| 153 bool Key::extractable() const | 140 bool Key::extractable() const |
| 154 { | 141 { |
| 155 return m_key.extractable(); | 142 return m_key.extractable(); |
| 156 } | 143 } |
| 157 | 144 |
| 158 Algorithm* Key::algorithm() | 145 KeyAlgorithm* Key::algorithm() |
| 159 { | 146 { |
| 160 if (!m_algorithm) | 147 if (!m_algorithm) |
| 161 m_algorithm = Algorithm::create(m_key.algorithm()); | 148 m_algorithm = KeyAlgorithm::create(m_key.algorithm()); |
| 162 return m_algorithm.get(); | 149 return m_algorithm.get(); |
| 163 } | 150 } |
| 164 | 151 |
| 165 // FIXME: This creates a new javascript array each time. What should happen | 152 // FIXME: This creates a new javascript array each time. What should happen |
| 166 // instead is return the same (immutable) array. (Javascript callers can | 153 // instead is return the same (immutable) array. (Javascript callers can |
| 167 // distinguish this by doing an == test on the arrays and seeing they are | 154 // distinguish this by doing an == test on the arrays and seeing they are |
| 168 // different). | 155 // different). |
| 169 Vector<String> Key::usages() const | 156 Vector<String> Key::usages() const |
| 170 { | 157 { |
| 171 Vector<String> result; | 158 Vector<String> result; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 182 if (!(m_key.usages() & toKeyUsage(op))) { | 169 if (!(m_key.usages() & toKeyUsage(op))) { |
| 183 result->completeWithError("key.usages does not permit this operation"); | 170 result->completeWithError("key.usages does not permit this operation"); |
| 184 return false; | 171 return false; |
| 185 } | 172 } |
| 186 | 173 |
| 187 if (m_key.algorithm().id() != algorithm.id()) { | 174 if (m_key.algorithm().id() != algorithm.id()) { |
| 188 result->completeWithError("key.algorithm does not match that of operatio
n"); | 175 result->completeWithError("key.algorithm does not match that of operatio
n"); |
| 189 return false; | 176 return false; |
| 190 } | 177 } |
| 191 | 178 |
| 192 // Verify that the algorithm-specific parameters for the key conform to the | |
| 193 // algorithm. | |
| 194 // FIXME: This is incomplete and not future proof. Operational parameters | |
| 195 // should be enumerated when defining new parameters. | |
| 196 | |
| 197 if (m_key.algorithm().id() == blink::WebCryptoAlgorithmIdHmac) { | |
| 198 blink::WebCryptoAlgorithmId keyHash; | |
| 199 blink::WebCryptoAlgorithmId algorithmHash; | |
| 200 if (!getHmacHashId(m_key.algorithm(), keyHash) || !getHmacHashId(algorit
hm, algorithmHash) || keyHash != algorithmHash) { | |
| 201 result->completeWithError("key.algorithm does not match that of oper
ation (HMAC's hash differs)"); | |
| 202 return false; | |
| 203 } | |
| 204 } | |
| 205 | |
| 206 return true; | 179 return true; |
| 207 } | 180 } |
| 208 | 181 |
| 209 bool Key::parseFormat(const String& formatString, blink::WebCryptoKeyFormat& for
mat, CryptoResult* result) | 182 bool Key::parseFormat(const String& formatString, blink::WebCryptoKeyFormat& for
mat, CryptoResult* result) |
| 210 { | 183 { |
| 211 // There are few enough values that testing serially is fast enough. | 184 // There are few enough values that testing serially is fast enough. |
| 212 if (formatString == "raw") { | 185 if (formatString == "raw") { |
| 213 format = blink::WebCryptoKeyFormatRaw; | 186 format = blink::WebCryptoKeyFormatRaw; |
| 214 return true; | 187 return true; |
| 215 } | 188 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 243 } | 216 } |
| 244 return true; | 217 return true; |
| 245 } | 218 } |
| 246 | 219 |
| 247 void Key::trace(Visitor* visitor) | 220 void Key::trace(Visitor* visitor) |
| 248 { | 221 { |
| 249 visitor->trace(m_algorithm); | 222 visitor->trace(m_algorithm); |
| 250 } | 223 } |
| 251 | 224 |
| 252 } // namespace WebCore | 225 } // namespace WebCore |
| OLD | NEW |