2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
32 #include "modules/crypto/Key.h"
34 #include "bindings/v8/ExceptionState.h"
35 #include "core/dom/ExceptionCode.h"
36 #include "modules/crypto/Algorithm.h"
37 #include "public/platform/WebCryptoAlgorithmParams.h"
45 const char* keyTypeToString(blink::WebCryptoKeyType type)
48 case blink::WebCryptoKeyTypeSecret:
50 case blink::WebCryptoKeyTypePublic:
52 case blink::WebCryptoKeyTypePrivate:
59 struct KeyUsageMapping {
60 blink::WebCryptoKeyUsage value;
61 const char* const name;
64 // Keep this array sorted.
65 const KeyUsageMapping keyUsageMappings[] = {
66 { blink::WebCryptoKeyUsageDecrypt, "decrypt" },
67 { blink::WebCryptoKeyUsageDeriveKey, "deriveKey" },
68 { blink::WebCryptoKeyUsageEncrypt, "encrypt" },
69 { blink::WebCryptoKeyUsageSign, "sign" },
70 { blink::WebCryptoKeyUsageUnwrapKey, "unwrapKey" },
71 { blink::WebCryptoKeyUsageVerify, "verify" },
72 { blink::WebCryptoKeyUsageWrapKey, "wrapKey" },
75 COMPILE_ASSERT(blink::EndOfWebCryptoKeyUsage == (1 << 6) + 1, update_keyUsageMappings);
77 const char* keyUsageToString(blink::WebCryptoKeyUsage usage)
79 for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyUsageMappings); ++i) {
80 if (keyUsageMappings[i].value == usage)
81 return keyUsageMappings[i].name;
87 blink::WebCryptoKeyUsageMask keyUsageStringToMask(const String& usageString)
89 for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyUsageMappings); ++i) {
90 if (keyUsageMappings[i].name == usageString)
91 return keyUsageMappings[i].value;
96 blink::WebCryptoKeyUsageMask toKeyUsage(AlgorithmOperation operation)
100 return blink::WebCryptoKeyUsageEncrypt;
102 return blink::WebCryptoKeyUsageDecrypt;
104 return blink::WebCryptoKeyUsageSign;
106 return blink::WebCryptoKeyUsageVerify;
108 return blink::WebCryptoKeyUsageDeriveKey;
110 return blink::WebCryptoKeyUsageWrapKey;
112 return blink::WebCryptoKeyUsageUnwrapKey;
119 ASSERT_NOT_REACHED();
123 bool getHmacHashId(const blink::WebCryptoAlgorithm& algorithm, blink::WebCryptoAlgorithmId& hashId)
125 if (algorithm.hmacParams()) {
126 hashId = algorithm.hmacParams()->hash().id();
129 if (algorithm.hmacKeyParams()) {
130 hashId = algorithm.hmacKeyParams()->hash().id();
142 Key::Key(const blink::WebCryptoKey& key)
145 ScriptWrappable::init(this);
148 String Key::type() const
150 return keyTypeToString(m_key.type());
153 bool Key::extractable() const
155 return m_key.extractable();
158 Algorithm* Key::algorithm()
161 m_algorithm = Algorithm::create(m_key.algorithm());
162 return m_algorithm.get();
165 // FIXME: This creates a new javascript array each time. What should happen
166 // instead is return the same (immutable) array. (Javascript callers can
167 // distinguish this by doing an == test on the arrays and seeing they are
169 Vector<String> Key::usages() const
171 Vector<String> result;
172 for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyUsageMappings); ++i) {
173 blink::WebCryptoKeyUsage usage = keyUsageMappings[i].value;
174 if (m_key.usages() & usage)
175 result.append(keyUsageToString(usage));
180 bool Key::canBeUsedForAlgorithm(const blink::WebCryptoAlgorithm& algorithm, AlgorithmOperation op, String& errorDetails) const
182 if (!(m_key.usages() & toKeyUsage(op))) {
183 errorDetails = "key.usages does not permit this operation";
187 if (m_key.algorithm().id() != algorithm.id()) {
188 errorDetails = "key.algorithm does not match that of operation";
192 // Verify that the algorithm-specific parameters for the key conform to the
194 // FIXME: This is incomplete and not future proof. Operational parameters
195 // should be enumerated when defining new parameters.
197 if (m_key.algorithm().id() == blink::WebCryptoAlgorithmIdHmac) {
198 blink::WebCryptoAlgorithmId keyHash;
199 blink::WebCryptoAlgorithmId algorithmHash;
200 if (!getHmacHashId(m_key.algorithm(), keyHash) || !getHmacHashId(algorithm, algorithmHash) || keyHash != algorithmHash) {
201 errorDetails = "key.algorithm does not match that of operation (HMAC's hash differs)";
209 bool Key::parseFormat(const String& formatString, blink::WebCryptoKeyFormat& format, ExceptionState& exceptionState)
211 // There are few enough values that testing serially is fast enough.
212 if (formatString == "raw") {
213 format = blink::WebCryptoKeyFormatRaw;
216 if (formatString == "pkcs8") {
217 format = blink::WebCryptoKeyFormatPkcs8;
220 if (formatString == "spki") {
221 format = blink::WebCryptoKeyFormatSpki;
224 if (formatString == "jwk") {
225 format = blink::WebCryptoKeyFormatJwk;
229 exceptionState.throwTypeError("Invalid keyFormat argument");
233 bool Key::parseUsageMask(const Vector<String>& usages, blink::WebCryptoKeyUsageMask& mask, ExceptionState& exceptionState)
236 for (size_t i = 0; i < usages.size(); ++i) {
237 blink::WebCryptoKeyUsageMask usage = keyUsageStringToMask(usages[i]);
239 exceptionState.throwTypeError("Invalid keyUsages argument");
247 void Key::trace(Visitor* visitor)
249 visitor->trace(m_algorithm);
252 } // namespace WebCore