OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/webcrypto/webcrypto_util.h" | 5 #include "content/renderer/webcrypto/webcrypto_util.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 9 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
10 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 10 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
11 | 11 |
12 namespace content { | 12 namespace content { |
13 | 13 |
14 namespace webcrypto { | 14 namespace webcrypto { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 blink::WebCryptoAlgorithm CreateAesKeyGenAlgorithm( | 18 blink::WebCryptoAlgorithm CreateAesKeyGenAlgorithm( |
19 blink::WebCryptoAlgorithmId aes_alg_id, | 19 blink::WebCryptoAlgorithmId aes_alg_id, |
20 unsigned short length) { | 20 unsigned short length) { |
21 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | 21 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
22 aes_alg_id, new blink::WebCryptoAesKeyGenParams(length)); | 22 aes_alg_id, new blink::WebCryptoAesKeyGenParams(length)); |
23 } | 23 } |
24 | 24 |
25 bool IsHashAlgorithm(blink::WebCryptoAlgorithmId alg_id) { | |
26 return alg_id == blink::WebCryptoAlgorithmIdSha1 || | |
27 alg_id == blink::WebCryptoAlgorithmIdSha224 || | |
28 alg_id == blink::WebCryptoAlgorithmIdSha256 || | |
29 alg_id == blink::WebCryptoAlgorithmIdSha384 || | |
30 alg_id == blink::WebCryptoAlgorithmIdSha512; | |
31 } | |
32 | |
33 } // namespace | 25 } // namespace |
34 | 26 |
35 const uint8* Uint8VectorStart(const std::vector<uint8>& data) { | 27 const uint8* Uint8VectorStart(const std::vector<uint8>& data) { |
36 if (data.empty()) | 28 if (data.empty()) |
37 return NULL; | 29 return NULL; |
38 return &data[0]; | 30 return &data[0]; |
39 } | 31 } |
40 | 32 |
41 void ShrinkBuffer(blink::WebArrayBuffer* buffer, unsigned new_size) { | 33 void ShrinkBuffer(blink::WebArrayBuffer* buffer, unsigned new_size) { |
42 DCHECK_LE(new_size, buffer->byteLength()); | 34 DCHECK_LE(new_size, buffer->byteLength()); |
(...skipping 21 matching lines...) Expand all Loading... |
64 // transformation including adding padding if required, and then call a base64 | 56 // transformation including adding padding if required, and then call a base64 |
65 // decoder. | 57 // decoder. |
66 bool Base64DecodeUrlSafe(const std::string& input, std::string* output) { | 58 bool Base64DecodeUrlSafe(const std::string& input, std::string* output) { |
67 std::string base64EncodedText(input); | 59 std::string base64EncodedText(input); |
68 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '-', '+'); | 60 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '-', '+'); |
69 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '_', '/'); | 61 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '_', '/'); |
70 base64EncodedText.append((4 - base64EncodedText.size() % 4) % 4, '='); | 62 base64EncodedText.append((4 - base64EncodedText.size() % 4) % 4, '='); |
71 return base::Base64Decode(base64EncodedText, output); | 63 return base::Base64Decode(base64EncodedText, output); |
72 } | 64 } |
73 | 65 |
| 66 bool IsHashAlgorithm(blink::WebCryptoAlgorithmId alg_id) { |
| 67 return alg_id == blink::WebCryptoAlgorithmIdSha1 || |
| 68 alg_id == blink::WebCryptoAlgorithmIdSha224 || |
| 69 alg_id == blink::WebCryptoAlgorithmIdSha256 || |
| 70 alg_id == blink::WebCryptoAlgorithmIdSha384 || |
| 71 alg_id == blink::WebCryptoAlgorithmIdSha512; |
| 72 } |
| 73 |
74 blink::WebCryptoAlgorithm GetInnerHashAlgorithm( | 74 blink::WebCryptoAlgorithm GetInnerHashAlgorithm( |
75 const blink::WebCryptoAlgorithm& algorithm) { | 75 const blink::WebCryptoAlgorithm& algorithm) { |
76 if (algorithm.hmacParams()) | 76 DCHECK(!algorithm.isNull()); |
77 return algorithm.hmacParams()->hash(); | 77 switch (algorithm.id()) { |
78 if (algorithm.hmacKeyParams()) | 78 case blink::WebCryptoAlgorithmIdHmac: |
79 return algorithm.hmacKeyParams()->hash(); | 79 if (algorithm.hmacParams()) |
80 if (algorithm.rsaSsaParams()) | 80 return algorithm.hmacParams()->hash(); |
81 return algorithm.rsaSsaParams()->hash(); | 81 else if (algorithm.hmacKeyParams()) |
82 if (algorithm.rsaOaepParams()) | 82 return algorithm.hmacKeyParams()->hash(); |
83 return algorithm.rsaOaepParams()->hash(); | 83 break; |
| 84 case blink::WebCryptoAlgorithmIdRsaOaep: |
| 85 if (algorithm.rsaOaepParams()) |
| 86 return algorithm.rsaOaepParams()->hash(); |
| 87 break; |
| 88 case blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5: |
| 89 if (algorithm.rsaSsaParams()) |
| 90 return algorithm.rsaSsaParams()->hash(); |
| 91 break; |
| 92 default: |
| 93 break; |
| 94 } |
84 return blink::WebCryptoAlgorithm::createNull(); | 95 return blink::WebCryptoAlgorithm::createNull(); |
85 } | 96 } |
86 | 97 |
87 blink::WebCryptoAlgorithm CreateAlgorithm(blink::WebCryptoAlgorithmId id) { | 98 blink::WebCryptoAlgorithm CreateAlgorithm(blink::WebCryptoAlgorithmId id) { |
88 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(id, NULL); | 99 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(id, NULL); |
89 } | 100 } |
90 | 101 |
91 blink::WebCryptoAlgorithm CreateHmacAlgorithmByHashId( | 102 blink::WebCryptoAlgorithm CreateHmacAlgorithmByHashId( |
92 blink::WebCryptoAlgorithmId hash_id) { | 103 blink::WebCryptoAlgorithmId hash_id) { |
93 DCHECK(IsHashAlgorithm(hash_id)); | 104 DCHECK(IsHashAlgorithm(hash_id)); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 | 179 |
169 blink::WebCryptoAlgorithm CreateAesGcmKeyGenAlgorithm( | 180 blink::WebCryptoAlgorithm CreateAesGcmKeyGenAlgorithm( |
170 unsigned short key_length_bits) { | 181 unsigned short key_length_bits) { |
171 return CreateAesKeyGenAlgorithm(blink::WebCryptoAlgorithmIdAesGcm, | 182 return CreateAesKeyGenAlgorithm(blink::WebCryptoAlgorithmIdAesGcm, |
172 key_length_bits); | 183 key_length_bits); |
173 } | 184 } |
174 | 185 |
175 } // namespace webcrypto | 186 } // namespace webcrypto |
176 | 187 |
177 } // namespace content | 188 } // namespace content |
OLD | NEW |