| 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 16 matching lines...) Expand all  Loading... | 
| 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/Algorithm.h" | 
|  | 37 #include "platform/CryptoResult.h" | 
| 37 #include "public/platform/WebCryptoAlgorithmParams.h" | 38 #include "public/platform/WebCryptoAlgorithmParams.h" | 
|  | 39 #include "public/platform/WebString.h" | 
| 38 | 40 | 
| 39 namespace WebCore { | 41 namespace WebCore { | 
| 40 | 42 | 
| 41 DEFINE_GC_INFO(Key); | 43 DEFINE_GC_INFO(Key); | 
| 42 | 44 | 
| 43 namespace { | 45 namespace { | 
| 44 | 46 | 
| 45 const char* keyTypeToString(blink::WebCryptoKeyType type) | 47 const char* keyTypeToString(blink::WebCryptoKeyType type) | 
| 46 { | 48 { | 
| 47     switch (type) { | 49     switch (type) { | 
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 170 { | 172 { | 
| 171     Vector<String> result; | 173     Vector<String> result; | 
| 172     for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyUsageMappings); ++i) { | 174     for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyUsageMappings); ++i) { | 
| 173         blink::WebCryptoKeyUsage usage = keyUsageMappings[i].value; | 175         blink::WebCryptoKeyUsage usage = keyUsageMappings[i].value; | 
| 174         if (m_key.usages() & usage) | 176         if (m_key.usages() & usage) | 
| 175             result.append(keyUsageToString(usage)); | 177             result.append(keyUsageToString(usage)); | 
| 176     } | 178     } | 
| 177     return result; | 179     return result; | 
| 178 } | 180 } | 
| 179 | 181 | 
| 180 bool Key::canBeUsedForAlgorithm(const blink::WebCryptoAlgorithm& algorithm, Algo
     rithmOperation op, String& errorDetails) const | 182 bool Key::canBeUsedForAlgorithm(const blink::WebCryptoAlgorithm& algorithm, Algo
     rithmOperation op, CryptoResult* result) const | 
| 181 { | 183 { | 
| 182     if (!(m_key.usages() & toKeyUsage(op))) { | 184     if (!(m_key.usages() & toKeyUsage(op))) { | 
| 183         errorDetails = "key.usages does not permit this operation"; | 185         result->completeWithError("key.usages does not permit this operation"); | 
| 184         return false; | 186         return false; | 
| 185     } | 187     } | 
| 186 | 188 | 
| 187     if (m_key.algorithm().id() != algorithm.id()) { | 189     if (m_key.algorithm().id() != algorithm.id()) { | 
| 188         errorDetails = "key.algorithm does not match that of operation"; | 190         result->completeWithError("key.algorithm does not match that of operatio
     n"); | 
| 189         return false; | 191         return false; | 
| 190     } | 192     } | 
| 191 | 193 | 
| 192     // Verify that the algorithm-specific parameters for the key conform to the | 194     // Verify that the algorithm-specific parameters for the key conform to the | 
| 193     // algorithm. | 195     // algorithm. | 
| 194     // FIXME: This is incomplete and not future proof. Operational parameters | 196     // FIXME: This is incomplete and not future proof. Operational parameters | 
| 195     //        should be enumerated when defining new parameters. | 197     //        should be enumerated when defining new parameters. | 
| 196 | 198 | 
| 197     if (m_key.algorithm().id() == blink::WebCryptoAlgorithmIdHmac) { | 199     if (m_key.algorithm().id() == blink::WebCryptoAlgorithmIdHmac) { | 
| 198         blink::WebCryptoAlgorithmId keyHash; | 200         blink::WebCryptoAlgorithmId keyHash; | 
| 199         blink::WebCryptoAlgorithmId algorithmHash; | 201         blink::WebCryptoAlgorithmId algorithmHash; | 
| 200         if (!getHmacHashId(m_key.algorithm(), keyHash) || !getHmacHashId(algorit
     hm, algorithmHash) || keyHash != algorithmHash) { | 202         if (!getHmacHashId(m_key.algorithm(), keyHash) || !getHmacHashId(algorit
     hm, algorithmHash) || keyHash != algorithmHash) { | 
| 201             errorDetails = "key.algorithm does not match that of operation (HMAC
     's hash differs)"; | 203             result->completeWithError("key.algorithm does not match that of oper
     ation (HMAC's hash differs)"); | 
| 202             return false; | 204             return false; | 
| 203         } | 205         } | 
| 204     } | 206     } | 
| 205 | 207 | 
| 206     return true; | 208     return true; | 
| 207 } | 209 } | 
| 208 | 210 | 
| 209 bool Key::parseFormat(const String& formatString, blink::WebCryptoKeyFormat& for
     mat, ExceptionState& exceptionState) | 211 bool Key::parseFormat(const String& formatString, blink::WebCryptoKeyFormat& for
     mat, CryptoResult* result) | 
| 210 { | 212 { | 
| 211     // There are few enough values that testing serially is fast enough. | 213     // There are few enough values that testing serially is fast enough. | 
| 212     if (formatString == "raw") { | 214     if (formatString == "raw") { | 
| 213         format = blink::WebCryptoKeyFormatRaw; | 215         format = blink::WebCryptoKeyFormatRaw; | 
| 214         return true; | 216         return true; | 
| 215     } | 217     } | 
| 216     if (formatString == "pkcs8") { | 218     if (formatString == "pkcs8") { | 
| 217         format = blink::WebCryptoKeyFormatPkcs8; | 219         format = blink::WebCryptoKeyFormatPkcs8; | 
| 218         return true; | 220         return true; | 
| 219     } | 221     } | 
| 220     if (formatString == "spki") { | 222     if (formatString == "spki") { | 
| 221         format = blink::WebCryptoKeyFormatSpki; | 223         format = blink::WebCryptoKeyFormatSpki; | 
| 222         return true; | 224         return true; | 
| 223     } | 225     } | 
| 224     if (formatString == "jwk") { | 226     if (formatString == "jwk") { | 
| 225         format = blink::WebCryptoKeyFormatJwk; | 227         format = blink::WebCryptoKeyFormatJwk; | 
| 226         return true; | 228         return true; | 
| 227     } | 229     } | 
| 228 | 230 | 
| 229     exceptionState.throwTypeError("Invalid keyFormat argument"); | 231     result->completeWithError("Invalid keyFormat argument"); | 
| 230     return false; | 232     return false; | 
| 231 } | 233 } | 
| 232 | 234 | 
| 233 bool Key::parseUsageMask(const Vector<String>& usages, blink::WebCryptoKeyUsageM
     ask& mask, ExceptionState& exceptionState) | 235 bool Key::parseUsageMask(const Vector<String>& usages, blink::WebCryptoKeyUsageM
     ask& mask, CryptoResult* result) | 
| 234 { | 236 { | 
| 235     mask = 0; | 237     mask = 0; | 
| 236     for (size_t i = 0; i < usages.size(); ++i) { | 238     for (size_t i = 0; i < usages.size(); ++i) { | 
| 237         blink::WebCryptoKeyUsageMask usage = keyUsageStringToMask(usages[i]); | 239         blink::WebCryptoKeyUsageMask usage = keyUsageStringToMask(usages[i]); | 
| 238         if (!usage) { | 240         if (!usage) { | 
| 239             exceptionState.throwTypeError("Invalid keyUsages argument"); | 241             result->completeWithError("Invalid keyUsages argument"); | 
| 240             return false; | 242             return false; | 
| 241         } | 243         } | 
| 242         mask |= usage; | 244         mask |= usage; | 
| 243     } | 245     } | 
| 244     return true; | 246     return true; | 
| 245 } | 247 } | 
| 246 | 248 | 
| 247 void Key::trace(Visitor* visitor) | 249 void Key::trace(Visitor* visitor) | 
| 248 { | 250 { | 
| 249     visitor->trace(m_algorithm); | 251     visitor->trace(m_algorithm); | 
| 250 } | 252 } | 
| 251 | 253 | 
| 252 } // namespace WebCore | 254 } // namespace WebCore | 
| OLD | NEW | 
|---|