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/KeyAlgorithm.h"
37 #include "platform/CryptoResult.h"
38 #include "public/platform/WebCryptoAlgorithmParams.h"
39 #include "public/platform/WebString.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 // The order of this array is the same order that will appear in Key.usages. It
65 // must be kept ordered as described by the Web Crypto spec.
66 const KeyUsageMapping keyUsageMappings[] = {
67 { blink::WebCryptoKeyUsageEncrypt, "encrypt" },
68 { blink::WebCryptoKeyUsageDecrypt, "decrypt" },
69 { blink::WebCryptoKeyUsageSign, "sign" },
70 { blink::WebCryptoKeyUsageVerify, "verify" },
71 { blink::WebCryptoKeyUsageDeriveKey, "deriveKey" },
72 { blink::WebCryptoKeyUsageDeriveBits, "deriveBits" },
73 { blink::WebCryptoKeyUsageWrapKey, "wrapKey" },
74 { blink::WebCryptoKeyUsageUnwrapKey, "unwrapKey" },
77 COMPILE_ASSERT(blink::EndOfWebCryptoKeyUsage == (1 << 7) + 1, update_keyUsageMappings);
79 const char* keyUsageToString(blink::WebCryptoKeyUsage usage)
81 for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyUsageMappings); ++i) {
82 if (keyUsageMappings[i].value == usage)
83 return keyUsageMappings[i].name;
89 blink::WebCryptoKeyUsageMask keyUsageStringToMask(const String& usageString)
91 for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyUsageMappings); ++i) {
92 if (keyUsageMappings[i].name == usageString)
93 return keyUsageMappings[i].value;
98 blink::WebCryptoKeyUsageMask toKeyUsage(AlgorithmOperation operation)
102 return blink::WebCryptoKeyUsageEncrypt;
104 return blink::WebCryptoKeyUsageDecrypt;
106 return blink::WebCryptoKeyUsageSign;
108 return blink::WebCryptoKeyUsageVerify;
110 return blink::WebCryptoKeyUsageDeriveKey;
112 return blink::WebCryptoKeyUsageDeriveBits;
114 return blink::WebCryptoKeyUsageWrapKey;
116 return blink::WebCryptoKeyUsageUnwrapKey;
123 ASSERT_NOT_REACHED();
133 Key::Key(const blink::WebCryptoKey& key)
136 ScriptWrappable::init(this);
139 String Key::type() const
141 return keyTypeToString(m_key.type());
144 bool Key::extractable() const
146 return m_key.extractable();
149 KeyAlgorithm* Key::algorithm()
152 m_algorithm = KeyAlgorithm::create(m_key.algorithm());
153 return m_algorithm.get();
156 // FIXME: This creates a new javascript array each time. What should happen
157 // instead is return the same (immutable) array. (Javascript callers can
158 // distinguish this by doing an == test on the arrays and seeing they are
160 Vector<String> Key::usages() const
162 Vector<String> result;
163 for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyUsageMappings); ++i) {
164 blink::WebCryptoKeyUsage usage = keyUsageMappings[i].value;
165 if (m_key.usages() & usage)
166 result.append(keyUsageToString(usage));
171 bool Key::canBeUsedForAlgorithm(const blink::WebCryptoAlgorithm& algorithm, AlgorithmOperation op, CryptoResult* result) const
173 if (!(m_key.usages() & toKeyUsage(op))) {
174 result->completeWithError(blink::WebCryptoErrorTypeInvalidAccess, "key.usages does not permit this operation");
178 if (m_key.algorithm().id() != algorithm.id()) {
179 result->completeWithError(blink::WebCryptoErrorTypeInvalidAccess, "key.algorithm does not match that of operation");
186 bool Key::parseFormat(const String& formatString, blink::WebCryptoKeyFormat& format, CryptoResult* result)
188 // There are few enough values that testing serially is fast enough.
189 if (formatString == "raw") {
190 format = blink::WebCryptoKeyFormatRaw;
193 if (formatString == "pkcs8") {
194 format = blink::WebCryptoKeyFormatPkcs8;
197 if (formatString == "spki") {
198 format = blink::WebCryptoKeyFormatSpki;
201 if (formatString == "jwk") {
202 format = blink::WebCryptoKeyFormatJwk;
206 result->completeWithError(blink::WebCryptoErrorTypeSyntax, "Invalid keyFormat argument");
210 bool Key::parseUsageMask(const Vector<String>& usages, blink::WebCryptoKeyUsageMask& mask, CryptoResult* result)
213 for (size_t i = 0; i < usages.size(); ++i) {
214 blink::WebCryptoKeyUsageMask usage = keyUsageStringToMask(usages[i]);
216 result->completeWithError(blink::WebCryptoErrorTypeSyntax, "Invalid keyUsages argument");
224 void Key::trace(Visitor* visitor)
226 visitor->trace(m_algorithm);
229 } // namespace WebCore