Commit 7ddf94b2 authored by eroman's avatar eroman Committed by Commit bot

Move base64-urlsafe helpers to jwk.{cc,h}

BUG=407858

Review URL: https://codereview.chromium.org/504413004

Cr-Commit-Position: refs/heads/master@{#292456}
parent aaacc303
...@@ -8,8 +8,10 @@ ...@@ -8,8 +8,10 @@
#include <functional> #include <functional>
#include <map> #include <map>
#include "base/base64.h"
#include "base/json/json_reader.h" #include "base/json/json_reader.h"
#include "base/json/json_writer.h" #include "base/json/json_writer.h"
#include "base/stl_util.h"
#include "base/strings/string_piece.h" #include "base/strings/string_piece.h"
#include "content/child/webcrypto/crypto_data.h" #include "content/child/webcrypto/crypto_data.h"
#include "content/child/webcrypto/status.h" #include "content/child/webcrypto/status.h"
...@@ -702,6 +704,32 @@ const char* GetJwkHmacAlgorithmName(blink::WebCryptoAlgorithmId hash) { ...@@ -702,6 +704,32 @@ const char* GetJwkHmacAlgorithmName(blink::WebCryptoAlgorithmId hash) {
} }
} }
// TODO(eroman): This accepts invalid inputs. http://crbug.com/378034
bool Base64DecodeUrlSafe(const std::string& input, std::string* output) {
std::string base64_encoded_text(input);
std::replace(
base64_encoded_text.begin(), base64_encoded_text.end(), '-', '+');
std::replace(
base64_encoded_text.begin(), base64_encoded_text.end(), '_', '/');
base64_encoded_text.append((4 - base64_encoded_text.size() % 4) % 4, '=');
return base::Base64Decode(base64_encoded_text, output);
}
std::string Base64EncodeUrlSafe(const base::StringPiece& input) {
std::string output;
base::Base64Encode(input, &output);
std::replace(output.begin(), output.end(), '+', '-');
std::replace(output.begin(), output.end(), '/', '_');
output.erase(std::remove(output.begin(), output.end(), '='), output.end());
return output;
}
std::string Base64EncodeUrlSafe(const std::vector<uint8_t>& input) {
const base::StringPiece string_piece(
reinterpret_cast<const char*>(vector_as_array(&input)), input.size());
return Base64EncodeUrlSafe(string_piece);
}
} // namespace webcrypto } // namespace webcrypto
} // namespace content } // namespace content
...@@ -8,7 +8,9 @@ ...@@ -8,7 +8,9 @@
#include <stdint.h> #include <stdint.h>
#include <vector> #include <vector>
#include "base/strings/string_piece.h"
#include "base/values.h" #include "base/values.h"
#include "content/common/content_export.h"
#include "third_party/WebKit/public/platform/WebArrayBuffer.h" #include "third_party/WebKit/public/platform/WebArrayBuffer.h"
#include "third_party/WebKit/public/platform/WebCrypto.h" #include "third_party/WebKit/public/platform/WebCrypto.h"
#include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
...@@ -120,6 +122,17 @@ Status ReadRsaKeyJwk(const CryptoData& key_data, ...@@ -120,6 +122,17 @@ Status ReadRsaKeyJwk(const CryptoData& key_data,
const char* GetJwkHmacAlgorithmName(blink::WebCryptoAlgorithmId hash); const char* GetJwkHmacAlgorithmName(blink::WebCryptoAlgorithmId hash);
// This function decodes unpadded 'base64url' encoded data, as described in
// RFC4648 (http://www.ietf.org/rfc/rfc4648.txt) Section 5.
CONTENT_EXPORT bool Base64DecodeUrlSafe(const std::string& input,
std::string* output);
// Returns an unpadded 'base64url' encoding of the input data, the opposite of
// Base64DecodeUrlSafe() above.
CONTENT_EXPORT std::string Base64EncodeUrlSafe(const base::StringPiece& input);
CONTENT_EXPORT std::string Base64EncodeUrlSafe(
const std::vector<uint8_t>& input);
} // namespace webcrypto } // namespace webcrypto
} // namespace content } // namespace content
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "base/stl_util.h" #include "base/stl_util.h"
#include "content/child/webcrypto/algorithm_dispatch.h" #include "content/child/webcrypto/algorithm_dispatch.h"
#include "content/child/webcrypto/crypto_data.h" #include "content/child/webcrypto/crypto_data.h"
#include "content/child/webcrypto/jwk.h"
#include "content/child/webcrypto/status.h" #include "content/child/webcrypto/status.h"
#include "content/child/webcrypto/test/test_helpers.h" #include "content/child/webcrypto/test/test_helpers.h"
#include "content/child/webcrypto/webcrypto_util.h" #include "content/child/webcrypto/webcrypto_util.h"
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "base/values.h" #include "base/values.h"
#include "content/child/webcrypto/algorithm_dispatch.h" #include "content/child/webcrypto/algorithm_dispatch.h"
#include "content/child/webcrypto/crypto_data.h" #include "content/child/webcrypto/crypto_data.h"
#include "content/child/webcrypto/jwk.h"
#include "content/child/webcrypto/status.h" #include "content/child/webcrypto/status.h"
#include "content/child/webcrypto/webcrypto_util.h" #include "content/child/webcrypto/webcrypto_util.h"
#include "content/public/common/content_paths.h" #include "content/public/common/content_paths.h"
......
...@@ -4,9 +4,7 @@ ...@@ -4,9 +4,7 @@
#include "content/child/webcrypto/webcrypto_util.h" #include "content/child/webcrypto/webcrypto_util.h"
#include "base/base64.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/stl_util.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "content/child/webcrypto/status.h" #include "content/child/webcrypto/status.h"
#include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
...@@ -42,36 +40,6 @@ bool BigIntegerToUint(const uint8_t* data, ...@@ -42,36 +40,6 @@ bool BigIntegerToUint(const uint8_t* data,
} // namespace } // namespace
// This function decodes unpadded 'base64url' encoded data, as described in
// RFC4648 (http://www.ietf.org/rfc/rfc4648.txt) Section 5. To do this, first
// change the incoming data to 'base64' encoding by applying the appropriate
// transformation including adding padding if required, and then call a base64
// decoder.
bool Base64DecodeUrlSafe(const std::string& input, std::string* output) {
std::string base64EncodedText(input);
std::replace(base64EncodedText.begin(), base64EncodedText.end(), '-', '+');
std::replace(base64EncodedText.begin(), base64EncodedText.end(), '_', '/');
base64EncodedText.append((4 - base64EncodedText.size() % 4) % 4, '=');
return base::Base64Decode(base64EncodedText, output);
}
// Returns an unpadded 'base64url' encoding of the input data, using the
// inverse of the process above.
std::string Base64EncodeUrlSafe(const base::StringPiece& input) {
std::string output;
base::Base64Encode(input, &output);
std::replace(output.begin(), output.end(), '+', '-');
std::replace(output.begin(), output.end(), '/', '_');
output.erase(std::remove(output.begin(), output.end(), '='), output.end());
return output;
}
std::string Base64EncodeUrlSafe(const std::vector<uint8_t>& input) {
const base::StringPiece string_piece(
reinterpret_cast<const char*>(vector_as_array(&input)), input.size());
return Base64EncodeUrlSafe(string_piece);
}
struct JwkToWebCryptoUsage { struct JwkToWebCryptoUsage {
const char* const jwk_key_op; const char* const jwk_key_op;
const blink::WebCryptoKeyUsage webcrypto_usage; const blink::WebCryptoKeyUsage webcrypto_usage;
......
...@@ -7,9 +7,7 @@ ...@@ -7,9 +7,7 @@
#include <stdint.h> #include <stdint.h>
#include <string> #include <string>
#include <vector>
#include "base/strings/string_piece.h"
#include "base/values.h" #include "base/values.h"
#include "content/common/content_export.h" #include "content/common/content_export.h"
#include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
...@@ -21,18 +19,6 @@ namespace webcrypto { ...@@ -21,18 +19,6 @@ namespace webcrypto {
class Status; class Status;
// This function decodes unpadded 'base64url' encoded data, as described in
// RFC4648 (http://www.ietf.org/rfc/rfc4648.txt) Section 5.
// In Web Crypto, this type of encoding is only used inside JWK.
CONTENT_EXPORT bool Base64DecodeUrlSafe(const std::string& input,
std::string* output);
// Returns an unpadded 'base64url' encoding of the input data, the opposite of
// Base64DecodeUrlSafe() above.
CONTENT_EXPORT std::string Base64EncodeUrlSafe(const base::StringPiece& input);
CONTENT_EXPORT std::string Base64EncodeUrlSafe(
const std::vector<uint8_t>& input);
// Composes a Web Crypto usage mask from an array of JWK key_ops values. // Composes a Web Crypto usage mask from an array of JWK key_ops values.
CONTENT_EXPORT Status GetWebCryptoUsagesFromJwkKeyOps( CONTENT_EXPORT Status GetWebCryptoUsagesFromJwkKeyOps(
const base::ListValue* jwk_key_ops_value, const base::ListValue* jwk_key_ops_value,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment