OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/child/webcrypto/webcrypto_util.h" | 5 #include "content/child/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 "base/stl_util.h" |
9 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
10 #include "content/child/webcrypto/status.h" | 11 #include "content/child/webcrypto/status.h" |
11 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 12 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
12 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 13 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
13 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | 14 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
14 | 15 |
15 namespace content { | 16 namespace content { |
16 | 17 |
17 namespace webcrypto { | 18 namespace webcrypto { |
18 | 19 |
19 const uint8_t* Uint8VectorStart(const std::vector<uint8_t>& data) { | |
20 if (data.empty()) | |
21 return NULL; | |
22 return &data[0]; | |
23 } | |
24 | |
25 uint8_t* Uint8VectorStart(std::vector<uint8_t>* data) { | |
26 if (data->empty()) | |
27 return NULL; | |
28 return &(*data)[0]; | |
29 } | |
30 | |
31 // This function decodes unpadded 'base64url' encoded data, as described in | 20 // This function decodes unpadded 'base64url' encoded data, as described in |
32 // RFC4648 (http://www.ietf.org/rfc/rfc4648.txt) Section 5. To do this, first | 21 // RFC4648 (http://www.ietf.org/rfc/rfc4648.txt) Section 5. To do this, first |
33 // change the incoming data to 'base64' encoding by applying the appropriate | 22 // change the incoming data to 'base64' encoding by applying the appropriate |
34 // transformation including adding padding if required, and then call a base64 | 23 // transformation including adding padding if required, and then call a base64 |
35 // decoder. | 24 // decoder. |
36 bool Base64DecodeUrlSafe(const std::string& input, std::string* output) { | 25 bool Base64DecodeUrlSafe(const std::string& input, std::string* output) { |
37 std::string base64EncodedText(input); | 26 std::string base64EncodedText(input); |
38 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '-', '+'); | 27 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '-', '+'); |
39 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '_', '/'); | 28 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '_', '/'); |
40 base64EncodedText.append((4 - base64EncodedText.size() % 4) % 4, '='); | 29 base64EncodedText.append((4 - base64EncodedText.size() % 4) % 4, '='); |
41 return base::Base64Decode(base64EncodedText, output); | 30 return base::Base64Decode(base64EncodedText, output); |
42 } | 31 } |
43 | 32 |
44 // Returns an unpadded 'base64url' encoding of the input data, using the | 33 // Returns an unpadded 'base64url' encoding of the input data, using the |
45 // inverse of the process above. | 34 // inverse of the process above. |
46 std::string Base64EncodeUrlSafe(const base::StringPiece& input) { | 35 std::string Base64EncodeUrlSafe(const base::StringPiece& input) { |
47 std::string output; | 36 std::string output; |
48 base::Base64Encode(input, &output); | 37 base::Base64Encode(input, &output); |
49 std::replace(output.begin(), output.end(), '+', '-'); | 38 std::replace(output.begin(), output.end(), '+', '-'); |
50 std::replace(output.begin(), output.end(), '/', '_'); | 39 std::replace(output.begin(), output.end(), '/', '_'); |
51 output.erase(std::remove(output.begin(), output.end(), '='), output.end()); | 40 output.erase(std::remove(output.begin(), output.end(), '='), output.end()); |
52 return output; | 41 return output; |
53 } | 42 } |
54 | 43 |
55 std::string Base64EncodeUrlSafe(const std::vector<uint8_t>& input) { | 44 std::string Base64EncodeUrlSafe(const std::vector<uint8_t>& input) { |
56 const base::StringPiece string_piece( | 45 const base::StringPiece string_piece( |
57 reinterpret_cast<const char*>(Uint8VectorStart(input)), input.size()); | 46 reinterpret_cast<const char*>(vector_as_array(&input)), input.size()); |
58 return Base64EncodeUrlSafe(string_piece); | 47 return Base64EncodeUrlSafe(string_piece); |
59 } | 48 } |
60 | 49 |
61 struct JwkToWebCryptoUsage { | 50 struct JwkToWebCryptoUsage { |
62 const char* const jwk_key_op; | 51 const char* const jwk_key_op; |
63 const blink::WebCryptoKeyUsage webcrypto_usage; | 52 const blink::WebCryptoKeyUsage webcrypto_usage; |
64 }; | 53 }; |
65 | 54 |
66 // Keep this ordered according to the definition | 55 // Keep this ordered according to the definition |
67 // order of WebCrypto's "recognized key usage | 56 // order of WebCrypto's "recognized key usage |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 Status CheckKeyCreationUsages(blink::WebCryptoKeyUsageMask all_possible_usages, | 245 Status CheckKeyCreationUsages(blink::WebCryptoKeyUsageMask all_possible_usages, |
257 blink::WebCryptoKeyUsageMask actual_usages) { | 246 blink::WebCryptoKeyUsageMask actual_usages) { |
258 if (!ContainsKeyUsages(all_possible_usages, actual_usages)) | 247 if (!ContainsKeyUsages(all_possible_usages, actual_usages)) |
259 return Status::ErrorCreateKeyBadUsages(); | 248 return Status::ErrorCreateKeyBadUsages(); |
260 return Status::Success(); | 249 return Status::Success(); |
261 } | 250 } |
262 | 251 |
263 } // namespace webcrypto | 252 } // namespace webcrypto |
264 | 253 |
265 } // namespace content | 254 } // namespace content |
OLD | NEW |