Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Unified Diff: content/child/webcrypto/jwk.h

Issue 687063002: Refactor: Expose JwkReader/JwkWriter helper classes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | content/child/webcrypto/jwk.cc » ('j') | content/child/webcrypto/jwk.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/child/webcrypto/jwk.h
diff --git a/content/child/webcrypto/jwk.h b/content/child/webcrypto/jwk.h
index a5872090cb7410409038a3b8b1b1fd6d315c5a8d..d8f7ed31d09f3ed43894a30c9cf4a6022d8ed24a 100644
--- a/content/child/webcrypto/jwk.h
+++ b/content/child/webcrypto/jwk.h
@@ -11,9 +11,7 @@
#include "base/strings/string_piece.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/WebCrypto.h"
-#include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
namespace content {
@@ -22,6 +20,96 @@ namespace webcrypto {
class CryptoData;
class Status;
+// Helper class for parsing a JWK from JSON.
+//
+// Init() must be called (and succeed) before it is valid to call any other
+// method.
+class JwkReader {
+ public:
+ JwkReader();
+ ~JwkReader();
+
+ // Initializes a JWK reader by parsing the JSON |bytes|. To succeed the JWK
+ // must have kty of xpected_kty, have an ext compatible with
+ // |expected_extractable| and have usages compatible with expected_usages.
+ Status Init(const CryptoData& bytes,
+ bool expected_extractable,
+ blink::WebCryptoKeyUsageMask expected_usages,
+ const std::string& expected_kty);
+
+ // Returns true if the key |key| is present.
+ bool HasKey(const std::string& key) const;
+
+ // Extracts the required string property with key |key| and saves
+ // the result to |*result|. If the property does not exist or is not a string,
+ // returns an error.
+ Status GetString(const std::string& key, std::string* result) const;
+
+ // Extracts the optional string property with key |key| and saves
+ // the result to |*result| if it was found. If the property exists and is not
+ // a string, returns an error. Otherwise returns success, and sets
+ // |*property_exists| if it was found.
+ Status GetOptionalString(const std::string& key,
+ std::string* result,
+ bool* property_exists) const;
+
+ // Extracts the optional array property with key |key| and saves
+ // the result to |*result| if it was found. If the property exists and is not
+ // an array, returns an error. Otherwise returns success, and sets
+ // |*property_exists| if it was found. Note that |*result| is owned by |dict|.
+ Status GetOptionalList(const std::string& key,
+ base::ListValue** result,
+ bool* property_exists) const;
+
+ // Extracts the required string property with key |key| and saves
+ // the base64url-decoded bytes to |*result|. If the property does not exist or
+ // is not a string, or could not be base64url-decoded, returns an error.
+ Status GetBytes(const std::string& key, std::string* result) const;
+
+ // Extracts the required base64url property, which is interpreted as being a
+ // big-endian unsigned integer.
+ //
+ // Sequences that contain leading zeros will be rejected.
+ Status GetBigInteger(const std::string& key, std::string* result) const;
+
+ // Extracts the optional boolean property with key |key| and
+ // saves the result to |*result| if it was found. If the property exists and
+ // is not a boolean, returns an error. Otherwise returns success, and sets
+ // |*property_exists| if it was found.
+ Status GetOptionalBool(const std::string& key,
+ bool* result,
+ bool* property_exists) const;
+
+ // Checks if the |alg| property matches |expected_algorithm|.
+ Status VerifyAlg(const std::string& expected_algorithm) const;
+
+ private:
+ scoped_ptr<base::DictionaryValue> dict_;
+};
+
+// Helper class for building the JSON for a JWK.
+class JwkWriter {
+ public:
+ // Initializes a writer, and sets the standard JWK properties as indicated.
+ JwkWriter(const std::string& algorithm,
+ bool extractable,
+ blink::WebCryptoKeyUsageMask usages,
+ const std::string& kty);
+
+ // Sets a string parameter |value|.
+ void SetString(const std::string& key, const std::string& value);
+
+ // Sets a bytes parameter |value|, by base64 url-safe encoding it.
+ void SetBytes(const std::string& key, const CryptoData& value);
+
+ // Flattens the JWK to JSON (utf-8 encoded if necessary, however in practice
+ // it will be ASCII).
+ void ToJson(std::vector<uint8_t>* utf8_bytes) const;
+
+ private:
+ base::DictionaryValue dict_;
+};
+
// Writes a JWK-formatted symmetric key to |jwk_key_data|.
// * raw_key_data: The actual key data
// * algorithm: The JWK algorithm name (i.e. "alg")
« no previous file with comments | « no previous file | content/child/webcrypto/jwk.cc » ('j') | content/child/webcrypto/jwk.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698