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/CryptoKey.h"
34 #include "bindings/core/v8/ExceptionState.h"
35 #include "core/dom/ExceptionCode.h"
36 #include "platform/CryptoResult.h"
37 #include "public/platform/WebCryptoAlgorithmParams.h"
38 #include "public/platform/WebCryptoKeyAlgorithm.h"
39 #include "public/platform/WebString.h"
45 const char* keyTypeToString(WebCryptoKeyType type)
48 case WebCryptoKeyTypeSecret:
50 case WebCryptoKeyTypePublic:
52 case WebCryptoKeyTypePrivate:
59 struct KeyUsageMapping {
60 WebCryptoKeyUsage value;
61 const char* const name;
64 // The order of this array is the same order that will appear in
65 // CryptoKey.usages. It must be kept ordered as described by the Web Crypto
67 const KeyUsageMapping keyUsageMappings[] = {
68 { WebCryptoKeyUsageEncrypt, "encrypt" },
69 { WebCryptoKeyUsageDecrypt, "decrypt" },
70 { WebCryptoKeyUsageSign, "sign" },
71 { WebCryptoKeyUsageVerify, "verify" },
72 { WebCryptoKeyUsageDeriveKey, "deriveKey" },
73 { WebCryptoKeyUsageDeriveBits, "deriveBits" },
74 { WebCryptoKeyUsageWrapKey, "wrapKey" },
75 { WebCryptoKeyUsageUnwrapKey, "unwrapKey" },
78 COMPILE_ASSERT(EndOfWebCryptoKeyUsage == (1 << 7) + 1, update_keyUsageMappings);
80 const char* keyUsageToString(WebCryptoKeyUsage usage)
82 for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyUsageMappings); ++i) {
83 if (keyUsageMappings[i].value == usage)
84 return keyUsageMappings[i].name;
90 WebCryptoKeyUsageMask keyUsageStringToMask(const String& usageString)
92 for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyUsageMappings); ++i) {
93 if (keyUsageMappings[i].name == usageString)
94 return keyUsageMappings[i].value;
99 WebCryptoKeyUsageMask toKeyUsage(WebCryptoOperation operation)
102 case WebCryptoOperationEncrypt:
103 return WebCryptoKeyUsageEncrypt;
104 case WebCryptoOperationDecrypt:
105 return WebCryptoKeyUsageDecrypt;
106 case WebCryptoOperationSign:
107 return WebCryptoKeyUsageSign;
108 case WebCryptoOperationVerify:
109 return WebCryptoKeyUsageVerify;
110 case WebCryptoOperationDeriveKey:
111 return WebCryptoKeyUsageDeriveKey;
112 case WebCryptoOperationDeriveBits:
113 return WebCryptoKeyUsageDeriveBits;
114 case WebCryptoOperationWrapKey:
115 return WebCryptoKeyUsageWrapKey;
116 case WebCryptoOperationUnwrapKey:
117 return WebCryptoKeyUsageUnwrapKey;
118 case WebCryptoOperationDigest:
119 case WebCryptoOperationGenerateKey:
120 case WebCryptoOperationImportKey:
124 ASSERT_NOT_REACHED();
130 CryptoKey::~CryptoKey()
134 CryptoKey::CryptoKey(const WebCryptoKey& key)
137 ScriptWrappable::init(this);
140 String CryptoKey::type() const
142 return keyTypeToString(m_key.type());
145 bool CryptoKey::extractable() const
147 return m_key.extractable();
150 // FIXME: This creates a new javascript array each time. What should happen
151 // instead is return the same (immutable) array. (Javascript callers can
152 // distinguish this by doing an == test on the arrays and seeing they are
154 Vector<String> CryptoKey::usages() const
156 Vector<String> result;
157 for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyUsageMappings); ++i) {
158 WebCryptoKeyUsage usage = keyUsageMappings[i].value;
159 if (m_key.usages() & usage)
160 result.append(keyUsageToString(usage));
165 bool CryptoKey::canBeUsedForAlgorithm(const WebCryptoAlgorithm& algorithm, WebCryptoOperation op, CryptoResult* result) const
167 if (!(m_key.usages() & toKeyUsage(op))) {
168 result->completeWithError(WebCryptoErrorTypeInvalidAccess, "key.usages does not permit this operation");
172 if (m_key.algorithm().id() != algorithm.id()) {
173 result->completeWithError(WebCryptoErrorTypeInvalidAccess, "key.algorithm does not match that of operation");
180 bool CryptoKey::parseFormat(const String& formatString, WebCryptoKeyFormat& format, CryptoResult* result)
182 // There are few enough values that testing serially is fast enough.
183 if (formatString == "raw") {
184 format = WebCryptoKeyFormatRaw;
187 if (formatString == "pkcs8") {
188 format = WebCryptoKeyFormatPkcs8;
191 if (formatString == "spki") {
192 format = WebCryptoKeyFormatSpki;
195 if (formatString == "jwk") {
196 format = WebCryptoKeyFormatJwk;
200 result->completeWithError(WebCryptoErrorTypeSyntax, "Invalid keyFormat argument");
204 bool CryptoKey::parseUsageMask(const Vector<String>& usages, WebCryptoKeyUsageMask& mask, CryptoResult* result)
207 for (size_t i = 0; i < usages.size(); ++i) {
208 WebCryptoKeyUsageMask usage = keyUsageStringToMask(usages[i]);
210 result->completeWithError(WebCryptoErrorTypeSyntax, "Invalid keyUsages argument");