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" | |
8 #include "base/logging.h" | 7 #include "base/logging.h" |
9 #include "base/stl_util.h" | |
10 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
11 #include "content/child/webcrypto/status.h" | 9 #include "content/child/webcrypto/status.h" |
12 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 10 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
13 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 11 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
14 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | 12 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
15 | 13 |
16 namespace content { | 14 namespace content { |
17 | 15 |
18 namespace webcrypto { | 16 namespace webcrypto { |
19 | 17 |
(...skipping 15 matching lines...) Expand all Loading... |
35 if (reverse_i >= sizeof(*result) && data[i]) | 33 if (reverse_i >= sizeof(*result) && data[i]) |
36 return false; // Too large for a uint. | 34 return false; // Too large for a uint. |
37 | 35 |
38 *result |= data[i] << 8 * reverse_i; | 36 *result |= data[i] << 8 * reverse_i; |
39 } | 37 } |
40 return true; | 38 return true; |
41 } | 39 } |
42 | 40 |
43 } // namespace | 41 } // namespace |
44 | 42 |
45 // This function decodes unpadded 'base64url' encoded data, as described in | |
46 // RFC4648 (http://www.ietf.org/rfc/rfc4648.txt) Section 5. To do this, first | |
47 // change the incoming data to 'base64' encoding by applying the appropriate | |
48 // transformation including adding padding if required, and then call a base64 | |
49 // decoder. | |
50 bool Base64DecodeUrlSafe(const std::string& input, std::string* output) { | |
51 std::string base64EncodedText(input); | |
52 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '-', '+'); | |
53 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '_', '/'); | |
54 base64EncodedText.append((4 - base64EncodedText.size() % 4) % 4, '='); | |
55 return base::Base64Decode(base64EncodedText, output); | |
56 } | |
57 | |
58 // Returns an unpadded 'base64url' encoding of the input data, using the | |
59 // inverse of the process above. | |
60 std::string Base64EncodeUrlSafe(const base::StringPiece& input) { | |
61 std::string output; | |
62 base::Base64Encode(input, &output); | |
63 std::replace(output.begin(), output.end(), '+', '-'); | |
64 std::replace(output.begin(), output.end(), '/', '_'); | |
65 output.erase(std::remove(output.begin(), output.end(), '='), output.end()); | |
66 return output; | |
67 } | |
68 | |
69 std::string Base64EncodeUrlSafe(const std::vector<uint8_t>& input) { | |
70 const base::StringPiece string_piece( | |
71 reinterpret_cast<const char*>(vector_as_array(&input)), input.size()); | |
72 return Base64EncodeUrlSafe(string_piece); | |
73 } | |
74 | |
75 struct JwkToWebCryptoUsage { | 43 struct JwkToWebCryptoUsage { |
76 const char* const jwk_key_op; | 44 const char* const jwk_key_op; |
77 const blink::WebCryptoKeyUsage webcrypto_usage; | 45 const blink::WebCryptoKeyUsage webcrypto_usage; |
78 }; | 46 }; |
79 | 47 |
80 // Keep this ordered according to the definition | 48 // Keep this ordered according to the definition |
81 // order of WebCrypto's "recognized key usage | 49 // order of WebCrypto's "recognized key usage |
82 // values". | 50 // values". |
83 // | 51 // |
84 // This is not required for spec compliance, | 52 // This is not required for spec compliance, |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 // avoid feeding OpenSSL data that will hang use a whitelist. | 266 // avoid feeding OpenSSL data that will hang use a whitelist. |
299 if (*public_exponent != 3 && *public_exponent != 65537) | 267 if (*public_exponent != 3 && *public_exponent != 65537) |
300 return Status::ErrorGenerateKeyPublicExponent(); | 268 return Status::ErrorGenerateKeyPublicExponent(); |
301 | 269 |
302 return Status::Success(); | 270 return Status::Success(); |
303 } | 271 } |
304 | 272 |
305 } // namespace webcrypto | 273 } // namespace webcrypto |
306 | 274 |
307 } // namespace content | 275 } // namespace content |
OLD | NEW |