Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / crypto / Key.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/Key.h"
33
34 #include "bindings/v8/ExceptionState.h"
35 #include "core/dom/ExceptionCode.h"
36 #include "modules/crypto/Algorithm.h"
37 #include "public/platform/WebCryptoAlgorithmParams.h"
38
39 namespace WebCore {
40
41 DEFINE_GC_INFO(Key);
42
43 namespace {
44
45 const char* keyTypeToString(blink::WebCryptoKeyType type)
46 {
47     switch (type) {
48     case blink::WebCryptoKeyTypeSecret:
49         return "secret";
50     case blink::WebCryptoKeyTypePublic:
51         return "public";
52     case blink::WebCryptoKeyTypePrivate:
53         return "private";
54     }
55     ASSERT_NOT_REACHED();
56     return 0;
57 }
58
59 struct KeyUsageMapping {
60     blink::WebCryptoKeyUsage value;
61     const char* const name;
62 };
63
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" },
73 };
74
75 COMPILE_ASSERT(blink::EndOfWebCryptoKeyUsage == (1 << 6) + 1, update_keyUsageMappings);
76
77 const char* keyUsageToString(blink::WebCryptoKeyUsage usage)
78 {
79     for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyUsageMappings); ++i) {
80         if (keyUsageMappings[i].value == usage)
81             return keyUsageMappings[i].name;
82     }
83     ASSERT_NOT_REACHED();
84     return 0;
85 }
86
87 blink::WebCryptoKeyUsageMask keyUsageStringToMask(const String& usageString)
88 {
89     for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyUsageMappings); ++i) {
90         if (keyUsageMappings[i].name == usageString)
91             return keyUsageMappings[i].value;
92     }
93     return 0;
94 }
95
96 blink::WebCryptoKeyUsageMask toKeyUsage(AlgorithmOperation operation)
97 {
98     switch (operation) {
99     case Encrypt:
100         return blink::WebCryptoKeyUsageEncrypt;
101     case Decrypt:
102         return blink::WebCryptoKeyUsageDecrypt;
103     case Sign:
104         return blink::WebCryptoKeyUsageSign;
105     case Verify:
106         return blink::WebCryptoKeyUsageVerify;
107     case DeriveKey:
108         return blink::WebCryptoKeyUsageDeriveKey;
109     case WrapKey:
110         return blink::WebCryptoKeyUsageWrapKey;
111     case UnwrapKey:
112         return blink::WebCryptoKeyUsageUnwrapKey;
113     case Digest:
114     case GenerateKey:
115     case ImportKey:
116         break;
117     }
118
119     ASSERT_NOT_REACHED();
120     return 0;
121 }
122
123 bool getHmacHashId(const blink::WebCryptoAlgorithm& algorithm, blink::WebCryptoAlgorithmId& hashId)
124 {
125     if (algorithm.hmacParams()) {
126         hashId = algorithm.hmacParams()->hash().id();
127         return true;
128     }
129     if (algorithm.hmacKeyParams()) {
130         hashId = algorithm.hmacKeyParams()->hash().id();
131         return true;
132     }
133     return false;
134 }
135
136 } // namespace
137
138 Key::~Key()
139 {
140 }
141
142 Key::Key(const blink::WebCryptoKey& key)
143     : m_key(key)
144 {
145     ScriptWrappable::init(this);
146 }
147
148 String Key::type() const
149 {
150     return keyTypeToString(m_key.type());
151 }
152
153 bool Key::extractable() const
154 {
155     return m_key.extractable();
156 }
157
158 Algorithm* Key::algorithm()
159 {
160     if (!m_algorithm)
161         m_algorithm = Algorithm::create(m_key.algorithm());
162     return m_algorithm.get();
163 }
164
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
168 //        different).
169 Vector<String> Key::usages() const
170 {
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));
176     }
177     return result;
178 }
179
180 bool Key::canBeUsedForAlgorithm(const blink::WebCryptoAlgorithm& algorithm, AlgorithmOperation op, String& errorDetails) const
181 {
182     if (!(m_key.usages() & toKeyUsage(op))) {
183         errorDetails = "key.usages does not permit this operation";
184         return false;
185     }
186
187     if (m_key.algorithm().id() != algorithm.id()) {
188         errorDetails = "key.algorithm does not match that of operation";
189         return false;
190     }
191
192     // Verify that the algorithm-specific parameters for the key conform to the
193     // algorithm.
194     // FIXME: This is incomplete and not future proof. Operational parameters
195     //        should be enumerated when defining new parameters.
196
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)";
202             return false;
203         }
204     }
205
206     return true;
207 }
208
209 bool Key::parseFormat(const String& formatString, blink::WebCryptoKeyFormat& format, ExceptionState& exceptionState)
210 {
211     // There are few enough values that testing serially is fast enough.
212     if (formatString == "raw") {
213         format = blink::WebCryptoKeyFormatRaw;
214         return true;
215     }
216     if (formatString == "pkcs8") {
217         format = blink::WebCryptoKeyFormatPkcs8;
218         return true;
219     }
220     if (formatString == "spki") {
221         format = blink::WebCryptoKeyFormatSpki;
222         return true;
223     }
224     if (formatString == "jwk") {
225         format = blink::WebCryptoKeyFormatJwk;
226         return true;
227     }
228
229     exceptionState.throwTypeError("Invalid keyFormat argument");
230     return false;
231 }
232
233 bool Key::parseUsageMask(const Vector<String>& usages, blink::WebCryptoKeyUsageMask& mask, ExceptionState& exceptionState)
234 {
235     mask = 0;
236     for (size_t i = 0; i < usages.size(); ++i) {
237         blink::WebCryptoKeyUsageMask usage = keyUsageStringToMask(usages[i]);
238         if (!usage) {
239             exceptionState.throwTypeError("Invalid keyUsages argument");
240             return false;
241         }
242         mask |= usage;
243     }
244     return true;
245 }
246
247 void Key::trace(Visitor* visitor)
248 {
249     visitor->trace(m_algorithm);
250 }
251
252 } // namespace WebCore