OLD | NEW |
---|---|
(Empty) | |
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/NormalizeAlgorithm.h" | |
33 | |
34 #include "bindings/v8/Dictionary.h" | |
35 #include "core/dom/ExceptionCode.h" | |
36 #include "public/platform/WebCryptoAlgorithm.h" | |
37 #include "public/platform/WebCryptoAlgorithmParams.h" | |
38 #include "wtf/ArrayBuffer.h" | |
39 #include "wtf/ArrayBufferView.h" | |
40 #include "wtf/HashMap.h" | |
41 #include "wtf/Uint8Array.h" | |
42 #include "wtf/Vector.h" | |
43 #include "wtf/text/StringHash.h" | |
44 | |
45 namespace WebCore { | |
46 | |
47 namespace { | |
48 | |
49 struct AlgorithmNameMapping { | |
50 const char* const algorithmName; | |
51 WebKit::WebCryptoAlgorithmId algorithmId; | |
52 }; | |
53 | |
54 // Indicates that the algorithm doesn't support the specified operation. | |
55 enum { UnsupportedOp = -1 }; | |
abarth-chromium
2013/07/02 06:46:36
Rather than use an enum, you can just use a global
eroman
2013/07/02 08:12:27
Done.
| |
56 | |
57 // Either UnsupportedOp, or a value from WebKit::WebCryptoAlgorithmParamsType | |
58 typedef int AlgorithmParamsForOperation; | |
59 | |
60 struct OperationParamsMapping { | |
61 WebKit::WebCryptoAlgorithmId algorithmId; | |
62 AlgorithmOperation operation; | |
63 AlgorithmParamsForOperation params; | |
64 }; | |
65 | |
66 const AlgorithmNameMapping algorithmNameMappings[] = { | |
67 {"AES-CBC", WebKit::AesCbc}, | |
68 {"SHA-1", WebKit::Sha1}, | |
69 {"SHA-224", WebKit::Sha224}, | |
70 {"SHA-256", WebKit::Sha256}, | |
71 {"SHA-384", WebKit::Sha384}, | |
72 {"SHA-512", WebKit::Sha512}, | |
73 }; | |
74 | |
75 // What operations each algorithm supports, and what parameters it expects. | |
76 const OperationParamsMapping operationParamsMappings[] = { | |
77 // AES-CBC (section 18.10.) | |
78 {WebKit::AesCbc, Decrypt, WebKit::AesCbcParams}, | |
79 {WebKit::AesCbc, Encrypt, WebKit::AesCbcParams}, | |
80 {WebKit::AesCbc, GenerateKey, WebKit::AesKeyGenParams}, | |
81 | |
82 // SHA-1 (section 18.16.) | |
83 {WebKit::Sha1, Digest, WebKit::NoParams}, | |
84 | |
85 // SHA-224 (section 18.16.) | |
86 {WebKit::Sha224, Digest, WebKit::NoParams}, | |
87 | |
88 // SHA-256 (section 18.16.) | |
89 {WebKit::Sha256, Digest, WebKit::NoParams}, | |
90 | |
91 // SHA-384 (section 18.16.) | |
92 {WebKit::Sha384, Digest, WebKit::NoParams}, | |
93 | |
94 // SHA-512 (section 18.16.) | |
95 {WebKit::Sha512, Digest, WebKit::NoParams}, | |
96 }; | |
97 | |
98 // This structure describes an algorithm and its supported operations. | |
99 struct AlgorithmInfo { | |
100 AlgorithmInfo() | |
101 : algorithmName(0) | |
102 { | |
103 for (size_t i = 0; i < WTF_ARRAY_LENGTH(paramsForOperation); ++i) | |
104 paramsForOperation[i] = UnsupportedOp; | |
105 } | |
106 | |
107 WebKit::WebCryptoAlgorithmId algorithmId; | |
108 const char* algorithmName; | |
109 AlgorithmParamsForOperation paramsForOperation[NumAlgorithmOperations]; | |
110 }; | |
111 | |
112 // AlgorithmRegistry enumerates each of the different algorithms and its | |
113 // parameters. This describes the same information as the static tables above, | |
114 // but in a more convenient runtime form. | |
115 class AlgorithmRegistry { | |
116 public: | |
117 static const AlgorithmInfo* lookupAlgorithmByName(const String& algorithmNam e); | |
118 | |
119 private: | |
120 AlgorithmRegistry(); | |
121 | |
122 // Algorithm name to ID. | |
123 typedef HashMap<String, WebKit::WebCryptoAlgorithmId, CaseFoldingHash> Algor ithmNameToIdMap; | |
124 AlgorithmNameToIdMap m_algorithmNameToId; | |
125 | |
126 // Algorithm ID to information. | |
127 AlgorithmInfo m_algorithms[WebKit::NumAlgorithmId]; | |
128 }; | |
129 | |
130 // static | |
abarth-chromium
2013/07/02 06:46:36
Please skip these sorts of comments. They're igno
eroman
2013/07/02 08:12:27
Done.
| |
131 const AlgorithmInfo* AlgorithmRegistry::lookupAlgorithmByName(const String& algo rithmName) | |
132 { | |
133 // Singleton | |
abarth-chromium
2013/07/02 06:46:36
Please omit comments that redundantly state what t
eroman
2013/07/02 08:12:27
Done.
| |
134 static AlgorithmRegistry registry; | |
abarth-chromium
2013/07/02 06:46:36
Please use DEFINE_STATIC_LOCAL. That lets us avoi
eroman
2013/07/02 08:12:27
Done.
| |
135 | |
136 // Do a case-insensitive lookup for algorithmName. | |
abarth-chromium
2013/07/02 06:46:36
Please omit comments that redundantly state what t
eroman
2013/07/02 08:12:27
Done.
| |
137 AlgorithmNameToIdMap::const_iterator it = registry.m_algorithmNameToId.find( algorithmName); | |
138 if (it == registry.m_algorithmNameToId.end()) | |
139 return 0; | |
140 return ®istry.m_algorithms[it->value]; | |
141 } | |
142 | |
143 AlgorithmRegistry::AlgorithmRegistry() | |
144 { | |
145 // Initialize AlgorithmRegistry by populating it with the static mappings. | |
abarth-chromium
2013/07/02 06:46:36
Please skip comments that say what the code does.
eroman
2013/07/02 08:12:27
Done.
| |
146 for (size_t i = 0; i < WTF_ARRAY_LENGTH(algorithmNameMappings); ++i) { | |
147 const AlgorithmNameMapping& mapping = algorithmNameMappings[i]; | |
148 m_algorithmNameToId.add(mapping.algorithmName, mapping.algorithmId); | |
149 m_algorithms[mapping.algorithmId].algorithmName = mapping.algorithmName; | |
150 } | |
151 | |
152 for (size_t i = 0; i < WTF_ARRAY_LENGTH(operationParamsMappings); ++i) { | |
153 const OperationParamsMapping& mapping = operationParamsMappings[i]; | |
154 m_algorithms[mapping.algorithmId].paramsForOperation[mapping.operation] = mapping.params; | |
155 } | |
156 } | |
157 | |
158 WebKit::WebCryptoAlgorithmParams* parseAesCbcParams(const Dictionary& raw) | |
abarth-chromium
2013/07/02 06:46:36
WebKit::WebCryptoAlgorithmParams* --> PassOwnPt
eroman
2013/07/02 08:12:27
Done.
| |
159 { | |
160 RefPtr<ArrayBufferView> iv; | |
161 if (!raw.get("iv", iv) || !iv) | |
162 return 0; | |
163 | |
164 if (iv->byteLength() != 16) | |
165 return 0; | |
166 | |
167 return new WebKit::WebCryptoAesCbcParams(static_cast<unsigned char*>(iv->bas eAddress()), iv->byteLength()); | |
abarth-chromium
2013/07/02 06:46:36
We try to avoid "naked new" calls. Please wrap al
eroman
2013/07/02 08:12:27
Done.
| |
168 } | |
169 | |
170 WebKit::WebCryptoAlgorithmParams* parseAesKeyGenParams(const Dictionary& raw) | |
171 { | |
172 int32_t length; | |
173 if (!raw.get("length", length)) | |
174 return 0; | |
175 if (length < 0 || length > 0xFFFF) | |
176 return 0; | |
177 return new WebKit::WebCryptoAesKeyGenParams(length); | |
abarth-chromium
2013/07/02 06:46:36
Naked new
eroman
2013/07/02 08:12:27
Done.
| |
178 } | |
179 | |
180 WebKit::WebCryptoAlgorithmParams* parseAlgorithmParams(const Dictionary& raw, We bKit::WebCryptoAlgorithmParamsType type) | |
181 { | |
182 switch (type) { | |
183 case WebKit::NoParams: | |
184 return 0; | |
185 case WebKit::AesCbcParams: | |
186 return parseAesCbcParams(raw); | |
187 case WebKit::AesKeyGenParams: | |
188 return parseAesKeyGenParams(raw); | |
189 } | |
190 | |
abarth-chromium
2013/07/02 06:46:36
ASSERT_NOT_REACHED() ?
eroman
2013/07/02 08:12:27
Done.
| |
191 return 0; | |
192 } | |
193 | |
194 } // namespace | |
195 | |
196 // TODO(eroman): Throw the correct exception types! | |
abarth-chromium
2013/07/02 06:46:36
"TODO(eroman)" --> "FIXME"
We use FIXME rather
eroman
2013/07/02 08:12:27
Done.
| |
197 bool normalizeAlgorithm(const Dictionary& raw, AlgorithmOperation op, WebKit::We bCryptoAlgorithm& algorithm, ExceptionCode& ec) | |
198 { | |
199 String algorithmName; | |
200 if (!raw.get("name", algorithmName)) { | |
201 // No name was specified by the AlgorithmIdentifier. | |
abarth-chromium
2013/07/02 06:46:36
Please remove these sorts of redundant comments.
eroman
2013/07/02 08:12:27
Done.
| |
202 ec = NOT_SUPPORTED_ERR; | |
203 return false; | |
204 } | |
205 | |
206 if (!algorithmName.containsOnlyASCII()) { | |
207 // The spec defines this case separately. | |
abarth-chromium
2013/07/02 06:46:36
Please add a link to the part of the spec you're r
eroman
2013/07/02 08:12:27
Done.
| |
208 ec = SYNTAX_ERR; | |
209 return false; | |
210 } | |
211 | |
212 const AlgorithmInfo* info = AlgorithmRegistry::lookupAlgorithmByName(algorit hmName); | |
213 if (!info) { | |
214 // No algorithm by that name exists. | |
abarth-chromium
2013/07/02 06:46:36
Please omit this comment as well.
eroman
2013/07/02 08:12:27
Done.
| |
215 ec = NOT_SUPPORTED_ERR; | |
216 return false; | |
217 } | |
218 | |
219 if (info->paramsForOperation[op] == UnsupportedOp) { | |
220 // The algorithm does not support the requested operation. | |
abarth-chromium
2013/07/02 06:46:36
Please omit this redundant comment.
eroman
2013/07/02 08:12:27
Done.
| |
221 ec = NOT_SUPPORTED_ERR; | |
222 return false; | |
223 } | |
224 | |
225 // Parse the algorithm-specific parameters. | |
abarth-chromium
2013/07/02 06:46:36
Please omit this redundant comment.
eroman
2013/07/02 08:12:27
Done.
| |
226 WebKit::WebCryptoAlgorithmParamsType paramsType = static_cast<WebKit::WebCry ptoAlgorithmParamsType>(info->paramsForOperation[op]); | |
227 OwnPtr<WebKit::WebCryptoAlgorithmParams> params = adoptPtr(parseAlgorithmPar ams(raw, paramsType)); | |
abarth-chromium
2013/07/02 06:46:36
I see, we're not actually leaking this pointer. T
eroman
2013/07/02 08:12:27
Done.
| |
228 | |
229 if (!params && paramsType != WebKit::NoParams) { | |
230 // Failed parsing the parameters. | |
abarth-chromium
2013/07/02 06:46:36
Please omit this redundant comment.
eroman
2013/07/02 08:12:27
Done.
| |
231 ec = NOT_SUPPORTED_ERR; | |
232 return false; | |
233 } | |
234 | |
235 algorithm = WebKit::WebCryptoAlgorithm(info->algorithmId, info->algorithmNam e, params.release()); | |
236 return true; | |
237 } | |
238 | |
239 } // namespace WebCore | |
OLD | NEW |