OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 | 60 |
61 struct OperationParamsMapping { | 61 struct OperationParamsMapping { |
62 WebKit::WebCryptoAlgorithmId algorithmId; | 62 WebKit::WebCryptoAlgorithmId algorithmId; |
63 AlgorithmOperation operation; | 63 AlgorithmOperation operation; |
64 AlgorithmParamsForOperation params; | 64 AlgorithmParamsForOperation params; |
65 }; | 65 }; |
66 | 66 |
67 const AlgorithmNameMapping algorithmNameMappings[] = { | 67 const AlgorithmNameMapping algorithmNameMappings[] = { |
68 {"AES-CBC", WebKit::WebCryptoAlgorithmIdAesCbc}, | 68 {"AES-CBC", WebKit::WebCryptoAlgorithmIdAesCbc}, |
69 {"HMAC", WebKit::WebCryptoAlgorithmIdHmac}, | 69 {"HMAC", WebKit::WebCryptoAlgorithmIdHmac}, |
| 70 {"RSASSA-PKCS1-v1_5", WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5}, |
70 {"SHA-1", WebKit::WebCryptoAlgorithmIdSha1}, | 71 {"SHA-1", WebKit::WebCryptoAlgorithmIdSha1}, |
71 {"SHA-224", WebKit::WebCryptoAlgorithmIdSha224}, | 72 {"SHA-224", WebKit::WebCryptoAlgorithmIdSha224}, |
72 {"SHA-256", WebKit::WebCryptoAlgorithmIdSha256}, | 73 {"SHA-256", WebKit::WebCryptoAlgorithmIdSha256}, |
73 {"SHA-384", WebKit::WebCryptoAlgorithmIdSha384}, | 74 {"SHA-384", WebKit::WebCryptoAlgorithmIdSha384}, |
74 {"SHA-512", WebKit::WebCryptoAlgorithmIdSha512}, | 75 {"SHA-512", WebKit::WebCryptoAlgorithmIdSha512}, |
75 }; | 76 }; |
76 | 77 |
77 // What operations each algorithm supports, and what parameters it expects. | 78 // What operations each algorithm supports, and what parameters it expects. |
78 const OperationParamsMapping operationParamsMappings[] = { | 79 const OperationParamsMapping operationParamsMappings[] = { |
79 // AES-CBC (section 18.10.) | 80 // AES-CBC (section 18.10.) |
80 {WebKit::WebCryptoAlgorithmIdAesCbc, Decrypt, WebKit::WebCryptoAlgorithmPara
msTypeAesCbcParams}, | 81 {WebKit::WebCryptoAlgorithmIdAesCbc, Decrypt, WebKit::WebCryptoAlgorithmPara
msTypeAesCbcParams}, |
81 {WebKit::WebCryptoAlgorithmIdAesCbc, Encrypt, WebKit::WebCryptoAlgorithmPara
msTypeAesCbcParams}, | 82 {WebKit::WebCryptoAlgorithmIdAesCbc, Encrypt, WebKit::WebCryptoAlgorithmPara
msTypeAesCbcParams}, |
82 {WebKit::WebCryptoAlgorithmIdAesCbc, GenerateKey, WebKit::WebCryptoAlgorithm
ParamsTypeAesKeyGenParams}, | 83 {WebKit::WebCryptoAlgorithmIdAesCbc, GenerateKey, WebKit::WebCryptoAlgorithm
ParamsTypeAesKeyGenParams}, |
83 {WebKit::WebCryptoAlgorithmIdAesCbc, ImportKey, WebKit::WebCryptoAlgorithmPa
ramsTypeNone}, | 84 {WebKit::WebCryptoAlgorithmIdAesCbc, ImportKey, WebKit::WebCryptoAlgorithmPa
ramsTypeNone}, |
84 | 85 |
85 // HMAC (section 18.14.) | 86 // HMAC (section 18.14.) |
86 {WebKit::WebCryptoAlgorithmIdHmac, Sign, WebKit::WebCryptoAlgorithmParamsTyp
eHmacParams}, | 87 {WebKit::WebCryptoAlgorithmIdHmac, Sign, WebKit::WebCryptoAlgorithmParamsTyp
eHmacParams}, |
87 {WebKit::WebCryptoAlgorithmIdHmac, Verify, WebKit::WebCryptoAlgorithmParamsT
ypeHmacParams}, | 88 {WebKit::WebCryptoAlgorithmIdHmac, Verify, WebKit::WebCryptoAlgorithmParamsT
ypeHmacParams}, |
88 {WebKit::WebCryptoAlgorithmIdHmac, GenerateKey, WebKit::WebCryptoAlgorithmPa
ramsTypeHmacParams}, | 89 {WebKit::WebCryptoAlgorithmIdHmac, GenerateKey, WebKit::WebCryptoAlgorithmPa
ramsTypeHmacParams}, |
89 {WebKit::WebCryptoAlgorithmIdHmac, ImportKey, WebKit::WebCryptoAlgorithmPara
msTypeHmacParams}, | 90 {WebKit::WebCryptoAlgorithmIdHmac, ImportKey, WebKit::WebCryptoAlgorithmPara
msTypeHmacParams}, |
90 | 91 |
| 92 // RSASSA-PKCS1-v1_5 (section 18.4.) |
| 93 {WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, Sign, WebKit::WebCryptoAlgorit
hmParamsTypeRsaSsaParams}, |
| 94 {WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, Verify, WebKit::WebCryptoAlgor
ithmParamsTypeRsaSsaParams}, |
| 95 {WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, GenerateKey, WebKit::WebCrypto
AlgorithmParamsTypeRsaKeyGenParams}, |
| 96 {WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, ImportKey, WebKit::WebCryptoAl
gorithmParamsTypeNone}, |
| 97 |
91 // SHA-1 (section 18.16.) | 98 // SHA-1 (section 18.16.) |
92 {WebKit::WebCryptoAlgorithmIdSha1, Digest, WebKit::WebCryptoAlgorithmParamsT
ypeNone}, | 99 {WebKit::WebCryptoAlgorithmIdSha1, Digest, WebKit::WebCryptoAlgorithmParamsT
ypeNone}, |
93 | 100 |
94 // SHA-224 (section 18.16.) | 101 // SHA-224 (section 18.16.) |
95 {WebKit::WebCryptoAlgorithmIdSha224, Digest, WebKit::WebCryptoAlgorithmParam
sTypeNone}, | 102 {WebKit::WebCryptoAlgorithmIdSha224, Digest, WebKit::WebCryptoAlgorithmParam
sTypeNone}, |
96 | 103 |
97 // SHA-256 (section 18.16.) | 104 // SHA-256 (section 18.16.) |
98 {WebKit::WebCryptoAlgorithmIdSha256, Digest, WebKit::WebCryptoAlgorithmParam
sTypeNone}, | 105 {WebKit::WebCryptoAlgorithmIdSha256, Digest, WebKit::WebCryptoAlgorithmParam
sTypeNone}, |
99 | 106 |
100 // SHA-384 (section 18.16.) | 107 // SHA-384 (section 18.16.) |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 PassOwnPtr<WebKit::WebCryptoAlgorithmParams> parseAesKeyGenParams(const Dictiona
ry& raw) | 183 PassOwnPtr<WebKit::WebCryptoAlgorithmParams> parseAesKeyGenParams(const Dictiona
ry& raw) |
177 { | 184 { |
178 int32_t length; | 185 int32_t length; |
179 if (!raw.get("length", length)) | 186 if (!raw.get("length", length)) |
180 return nullptr; | 187 return nullptr; |
181 if (length < 0 || length > 0xFFFF) | 188 if (length < 0 || length > 0xFFFF) |
182 return nullptr; | 189 return nullptr; |
183 return adoptPtr(new WebKit::WebCryptoAesKeyGenParams(length)); | 190 return adoptPtr(new WebKit::WebCryptoAesKeyGenParams(length)); |
184 } | 191 } |
185 | 192 |
| 193 bool parseHash(const Dictionary& raw, WebKit::WebCryptoAlgorithm& hash) |
| 194 { |
| 195 Dictionary rawHash; |
| 196 if (!raw.get("hash", rawHash)) |
| 197 return false; |
| 198 |
| 199 NonThrowExceptionState es; |
| 200 return normalizeAlgorithm(rawHash, Digest, hash, es); |
| 201 } |
| 202 |
186 PassOwnPtr<WebKit::WebCryptoAlgorithmParams> parseHmacParams(const Dictionary& r
aw) | 203 PassOwnPtr<WebKit::WebCryptoAlgorithmParams> parseHmacParams(const Dictionary& r
aw) |
187 { | 204 { |
188 Dictionary rawHash; | 205 WebKit::WebCryptoAlgorithm hash; |
189 if (!raw.get("hash", rawHash)) | 206 if (!parseHash(raw, hash)) |
190 return nullptr; | 207 return nullptr; |
191 | |
192 // Normalizing the algorithm for a Digest operation means it will only | |
193 // match the SHA-* algorithms. | |
194 WebKit::WebCryptoAlgorithm hash; | |
195 NonThrowExceptionState es; | |
196 if (!normalizeAlgorithm(rawHash, Digest, hash, es)) | |
197 return nullptr; | |
198 | |
199 return adoptPtr(new WebKit::WebCryptoHmacParams(hash)); | 208 return adoptPtr(new WebKit::WebCryptoHmacParams(hash)); |
200 } | 209 } |
201 | 210 |
| 211 PassOwnPtr<WebKit::WebCryptoAlgorithmParams> parseRsaSsaParams(const Dictionary&
raw) |
| 212 { |
| 213 WebKit::WebCryptoAlgorithm hash; |
| 214 if (!parseHash(raw, hash)) |
| 215 return nullptr; |
| 216 return adoptPtr(new WebKit::WebCryptoRsaSsaParams(hash)); |
| 217 } |
| 218 |
| 219 PassOwnPtr<WebKit::WebCryptoAlgorithmParams> parseRsaKeyGenParams(const Dictiona
ry& raw) |
| 220 { |
| 221 // FIXME: This is losing precision; modulusLength is supposed to be a uint32 |
| 222 int32_t modulusLength; |
| 223 if (!raw.get("modulusLength", modulusLength)) |
| 224 return nullptr; |
| 225 if (modulusLength < 0) |
| 226 return nullptr; |
| 227 |
| 228 RefPtr<Uint8Array> publicExponent; |
| 229 if (!raw.get("publicExponent", publicExponent) || !publicExponent) |
| 230 return nullptr; |
| 231 return adoptPtr(new WebKit::WebCryptoRsaKeyGenParams(modulusLength, static_c
ast<const unsigned char*>(publicExponent->baseAddress()), publicExponent->byteLe
ngth())); |
| 232 } |
| 233 |
202 PassOwnPtr<WebKit::WebCryptoAlgorithmParams> parseAlgorithmParams(const Dictiona
ry& raw, WebKit::WebCryptoAlgorithmParamsType type) | 234 PassOwnPtr<WebKit::WebCryptoAlgorithmParams> parseAlgorithmParams(const Dictiona
ry& raw, WebKit::WebCryptoAlgorithmParamsType type) |
203 { | 235 { |
204 switch (type) { | 236 switch (type) { |
205 case WebKit::WebCryptoAlgorithmParamsTypeNone: | 237 case WebKit::WebCryptoAlgorithmParamsTypeNone: |
206 return nullptr; | 238 return nullptr; |
207 case WebKit::WebCryptoAlgorithmParamsTypeAesCbcParams: | 239 case WebKit::WebCryptoAlgorithmParamsTypeAesCbcParams: |
208 return parseAesCbcParams(raw); | 240 return parseAesCbcParams(raw); |
209 case WebKit::WebCryptoAlgorithmParamsTypeAesKeyGenParams: | 241 case WebKit::WebCryptoAlgorithmParamsTypeAesKeyGenParams: |
210 return parseAesKeyGenParams(raw); | 242 return parseAesKeyGenParams(raw); |
211 case WebKit::WebCryptoAlgorithmParamsTypeHmacParams: | 243 case WebKit::WebCryptoAlgorithmParamsTypeHmacParams: |
212 return parseHmacParams(raw); | 244 return parseHmacParams(raw); |
| 245 case WebKit::WebCryptoAlgorithmParamsTypeRsaSsaParams: |
| 246 return parseRsaSsaParams(raw); |
| 247 case WebKit::WebCryptoAlgorithmParamsTypeRsaKeyGenParams: |
| 248 return parseRsaKeyGenParams(raw); |
213 } | 249 } |
214 ASSERT_NOT_REACHED(); | 250 ASSERT_NOT_REACHED(); |
215 return nullptr; | 251 return nullptr; |
216 } | 252 } |
217 | 253 |
218 const AlgorithmInfo* algorithmInfo(const Dictionary& raw, ExceptionState& es) | 254 const AlgorithmInfo* algorithmInfo(const Dictionary& raw, ExceptionState& es) |
219 { | 255 { |
220 String algorithmName; | 256 String algorithmName; |
221 if (!raw.get("name", algorithmName)) { | 257 if (!raw.get("name", algorithmName)) { |
222 es.throwDOMException(NotSupportedError); | 258 es.throwDOMException(NotSupportedError); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 if (!params && paramsType != WebKit::WebCryptoAlgorithmParamsTypeNone) { | 295 if (!params && paramsType != WebKit::WebCryptoAlgorithmParamsTypeNone) { |
260 es.throwDOMException(NotSupportedError); | 296 es.throwDOMException(NotSupportedError); |
261 return false; | 297 return false; |
262 } | 298 } |
263 | 299 |
264 algorithm = WebKit::WebCryptoAlgorithm(info->algorithmId, info->algorithmNam
e, params.release()); | 300 algorithm = WebKit::WebCryptoAlgorithm(info->algorithmId, info->algorithmNam
e, params.release()); |
265 return true; | 301 return true; |
266 } | 302 } |
267 | 303 |
268 } // namespace WebCore | 304 } // namespace WebCore |
OLD | NEW |