Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / crypto / CryptoKey.cpp
1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #include "config.h"
32 #include "modules/crypto/CryptoKey.h"
33
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"
40
41 namespace blink {
42
43 namespace {
44
45 const char* keyTypeToString(WebCryptoKeyType type)
46 {
47     switch (type) {
48     case WebCryptoKeyTypeSecret:
49         return "secret";
50     case WebCryptoKeyTypePublic:
51         return "public";
52     case WebCryptoKeyTypePrivate:
53         return "private";
54     }
55     ASSERT_NOT_REACHED();
56     return 0;
57 }
58
59 struct KeyUsageMapping {
60     WebCryptoKeyUsage value;
61     const char* const name;
62 };
63
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
66 // spec.
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" },
76 };
77
78 COMPILE_ASSERT(EndOfWebCryptoKeyUsage == (1 << 7) + 1, update_keyUsageMappings);
79
80 const char* keyUsageToString(WebCryptoKeyUsage usage)
81 {
82     for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyUsageMappings); ++i) {
83         if (keyUsageMappings[i].value == usage)
84             return keyUsageMappings[i].name;
85     }
86     ASSERT_NOT_REACHED();
87     return 0;
88 }
89
90 WebCryptoKeyUsageMask keyUsageStringToMask(const String& usageString)
91 {
92     for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyUsageMappings); ++i) {
93         if (keyUsageMappings[i].name == usageString)
94             return keyUsageMappings[i].value;
95     }
96     return 0;
97 }
98
99 WebCryptoKeyUsageMask toKeyUsage(WebCryptoOperation operation)
100 {
101     switch (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:
121         break;
122     }
123
124     ASSERT_NOT_REACHED();
125     return 0;
126 }
127
128 } // namespace
129
130 CryptoKey::~CryptoKey()
131 {
132 }
133
134 CryptoKey::CryptoKey(const WebCryptoKey& key)
135     : m_key(key)
136 {
137     ScriptWrappable::init(this);
138 }
139
140 String CryptoKey::type() const
141 {
142     return keyTypeToString(m_key.type());
143 }
144
145 bool CryptoKey::extractable() const
146 {
147     return m_key.extractable();
148 }
149
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
153 //        different).
154 Vector<String> CryptoKey::usages() const
155 {
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));
161     }
162     return result;
163 }
164
165 bool CryptoKey::canBeUsedForAlgorithm(const WebCryptoAlgorithm& algorithm, WebCryptoOperation op, CryptoResult* result) const
166 {
167     if (!(m_key.usages() & toKeyUsage(op))) {
168         result->completeWithError(WebCryptoErrorTypeInvalidAccess, "key.usages does not permit this operation");
169         return false;
170     }
171
172     if (m_key.algorithm().id() != algorithm.id()) {
173         result->completeWithError(WebCryptoErrorTypeInvalidAccess, "key.algorithm does not match that of operation");
174         return false;
175     }
176
177     return true;
178 }
179
180 bool CryptoKey::parseFormat(const String& formatString, WebCryptoKeyFormat& format, CryptoResult* result)
181 {
182     // There are few enough values that testing serially is fast enough.
183     if (formatString == "raw") {
184         format = WebCryptoKeyFormatRaw;
185         return true;
186     }
187     if (formatString == "pkcs8") {
188         format = WebCryptoKeyFormatPkcs8;
189         return true;
190     }
191     if (formatString == "spki") {
192         format = WebCryptoKeyFormatSpki;
193         return true;
194     }
195     if (formatString == "jwk") {
196         format = WebCryptoKeyFormatJwk;
197         return true;
198     }
199
200     result->completeWithError(WebCryptoErrorTypeSyntax, "Invalid keyFormat argument");
201     return false;
202 }
203
204 bool CryptoKey::parseUsageMask(const Vector<String>& usages, WebCryptoKeyUsageMask& mask, CryptoResult* result)
205 {
206     mask = 0;
207     for (size_t i = 0; i < usages.size(); ++i) {
208         WebCryptoKeyUsageMask usage = keyUsageStringToMask(usages[i]);
209         if (!usage) {
210             result->completeWithError(WebCryptoErrorTypeSyntax, "Invalid keyUsages argument");
211             return false;
212         }
213         mask |= usage;
214     }
215     return true;
216 }
217
218 } // namespace blink