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 #ifndef CONTENT_CHILD_WEBCRYPTO_JWK_H_ | 5 #ifndef CONTENT_CHILD_WEBCRYPTO_JWK_H_ |
6 #define CONTENT_CHILD_WEBCRYPTO_JWK_H_ | 6 #define CONTENT_CHILD_WEBCRYPTO_JWK_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/strings/string_piece.h" | 11 #include "base/strings/string_piece.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "content/common/content_export.h" | 13 #include "content/common/content_export.h" |
14 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | |
15 #include "third_party/WebKit/public/platform/WebCrypto.h" | 14 #include "third_party/WebKit/public/platform/WebCrypto.h" |
16 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | |
17 | 15 |
18 namespace content { | 16 namespace content { |
19 | 17 |
20 namespace webcrypto { | 18 namespace webcrypto { |
21 | 19 |
22 class CryptoData; | 20 class CryptoData; |
23 class Status; | 21 class Status; |
24 | 22 |
23 // Helper class for parsing a JWK from JSON. | |
Ryan Sleevi
2014/10/30 02:30:40
Explain why this exists.
// Helper class for pars
eroman
2014/10/30 16:04:35
Thanks for all the comment reviews BTW!
eroman
2014/10/30 17:00:29
Done.
| |
24 // | |
25 // Init() must be called (and succeed) before it is valid to call any other | |
26 // method. | |
27 class JwkReader { | |
28 public: | |
29 JwkReader(); | |
30 ~JwkReader(); | |
31 | |
32 // Initializes a JWK reader by parsing the JSON |bytes|. To succeed the JWK | |
Ryan Sleevi
2014/10/30 02:30:40
"To succeed,"
| |
33 // must have kty of xpected_kty, have an ext compatible with | |
Ryan Sleevi
2014/10/30 02:30:39
typo: expected_kty
eroman
2014/10/30 17:00:29
Done.
| |
34 // |expected_extractable| and have usages compatible with expected_usages. | |
Ryan Sleevi
2014/10/30 02:30:40
"|expected_extractable|, and"
Oxford commas rule!
eroman
2014/10/30 17:00:29
Done (changed to bullets)
| |
35 Status Init(const CryptoData& bytes, | |
36 bool expected_extractable, | |
37 blink::WebCryptoKeyUsageMask expected_usages, | |
38 const std::string& expected_kty); | |
39 | |
40 // Returns true if the key |key| is present. | |
41 bool HasKey(const std::string& key) const; | |
Ryan Sleevi
2014/10/30 02:30:39
s/Key/Member/ throughout
That is, the name of pro
eroman
2014/10/30 17:00:29
Done.
| |
42 | |
43 // Extracts the required string property with key |key| and saves | |
44 // the result to |*result|. If the property does not exist or is not a string, | |
45 // returns an error. | |
46 Status GetString(const std::string& key, std::string* result) const; | |
47 | |
48 // Extracts the optional string property with key |key| and saves | |
49 // the result to |*result| if it was found. If the property exists and is not | |
50 // a string, returns an error. Otherwise returns success, and sets | |
51 // |*property_exists| if it was found. | |
52 Status GetOptionalString(const std::string& key, | |
53 std::string* result, | |
54 bool* property_exists) const; | |
55 | |
56 // Extracts the optional array property with key |key| and saves | |
57 // the result to |*result| if it was found. If the property exists and is not | |
58 // an array, returns an error. Otherwise returns success, and sets | |
59 // |*property_exists| if it was found. Note that |*result| is owned by |dict|. | |
Ryan Sleevi
2014/10/30 02:30:39
|dict_|
eroman
2014/10/30 17:00:29
Done.
| |
60 Status GetOptionalList(const std::string& key, | |
61 base::ListValue** result, | |
62 bool* property_exists) const; | |
63 | |
64 // Extracts the required string property with key |key| and saves | |
Ryan Sleevi
2014/10/30 02:30:40
also s/property/member/ throughout.
| |
65 // the base64url-decoded bytes to |*result|. If the property does not exist or | |
66 // is not a string, or could not be base64url-decoded, returns an error. | |
67 Status GetBytes(const std::string& key, std::string* result) const; | |
68 | |
69 // Extracts the required base64url property, which is interpreted as being a | |
70 // big-endian unsigned integer. | |
71 // | |
72 // Sequences that contain leading zeros will be rejected. | |
73 Status GetBigInteger(const std::string& key, std::string* result) const; | |
74 | |
75 // Extracts the optional boolean property with key |key| and | |
76 // saves the result to |*result| if it was found. If the property exists and | |
77 // is not a boolean, returns an error. Otherwise returns success, and sets | |
78 // |*property_exists| if it was found. | |
79 Status GetOptionalBool(const std::string& key, | |
80 bool* result, | |
81 bool* property_exists) const; | |
82 | |
83 // Gets the optional algorithm ("alg") string. | |
84 Status GetAlg(std::string* alg, bool* has_alg) const; | |
Ryan Sleevi
2014/10/30 02:30:40
Why is this a custom method instead of GetOptional
eroman
2014/10/30 16:04:35
I thought it was advantageous to internalize the "
| |
85 | |
86 // Checks if the "alg" property matches |expected_algorithm|. | |
87 Status VerifyAlg(const std::string& expected_algorithm) const; | |
Ryan Sleevi
2014/10/30 02:30:40
Why isn't this part of Init?
eroman
2014/10/30 16:04:35
The reason for this aberration is AES keys.
In th
eroman
2014/10/30 17:00:29
Done, made an optional part of Init()
| |
88 | |
89 private: | |
90 scoped_ptr<base::DictionaryValue> dict_; | |
91 }; | |
92 | |
93 // Helper class for building the JSON for a JWK. | |
94 class JwkWriter { | |
95 public: | |
96 // Initializes a writer, and sets the standard JWK properties as indicated. | |
97 JwkWriter(const std::string& algorithm, | |
98 bool extractable, | |
99 blink::WebCryptoKeyUsageMask usages, | |
100 const std::string& kty); | |
101 | |
102 // Sets a string parameter |value|. | |
103 void SetString(const std::string& key, const std::string& value); | |
104 | |
105 // Sets a bytes parameter |value|, by base64 url-safe encoding it. | |
106 void SetBytes(const std::string& key, const CryptoData& value); | |
107 | |
108 // Flattens the JWK to JSON (utf-8 encoded if necessary, however in practice | |
Ryan Sleevi
2014/10/30 02:30:40
s/utf-8/UTF-8/
| |
109 // it will be ASCII). | |
110 void ToJson(std::vector<uint8_t>* utf8_bytes) const; | |
111 | |
112 private: | |
113 base::DictionaryValue dict_; | |
114 }; | |
115 | |
25 // Writes a JWK-formatted symmetric key to |jwk_key_data|. | 116 // Writes a JWK-formatted symmetric key to |jwk_key_data|. |
26 // * raw_key_data: The actual key data | 117 // * raw_key_data: The actual key data |
27 // * algorithm: The JWK algorithm name (i.e. "alg") | 118 // * algorithm: The JWK algorithm name (i.e. "alg") |
28 // * extractable: The JWK extractability (i.e. "ext") | 119 // * extractable: The JWK extractability (i.e. "ext") |
29 // * usages: The JWK usages (i.e. "key_ops") | 120 // * usages: The JWK usages (i.e. "key_ops") |
30 void WriteSecretKeyJwk(const CryptoData& raw_key_data, | 121 void WriteSecretKeyJwk(const CryptoData& raw_key_data, |
31 const std::string& algorithm, | 122 const std::string& algorithm, |
32 bool extractable, | 123 bool extractable, |
33 blink::WebCryptoKeyUsageMask usages, | 124 blink::WebCryptoKeyUsageMask usages, |
34 std::vector<uint8_t>* jwk_key_data); | 125 std::vector<uint8_t>* jwk_key_data); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
131 // Base64DecodeUrlSafe() above. | 222 // Base64DecodeUrlSafe() above. |
132 CONTENT_EXPORT std::string Base64EncodeUrlSafe(const base::StringPiece& input); | 223 CONTENT_EXPORT std::string Base64EncodeUrlSafe(const base::StringPiece& input); |
133 CONTENT_EXPORT std::string Base64EncodeUrlSafe( | 224 CONTENT_EXPORT std::string Base64EncodeUrlSafe( |
134 const std::vector<uint8_t>& input); | 225 const std::vector<uint8_t>& input); |
135 | 226 |
136 } // namespace webcrypto | 227 } // namespace webcrypto |
137 | 228 |
138 } // namespace content | 229 } // namespace content |
139 | 230 |
140 #endif // CONTENT_CHILD_WEBCRYPTO_JWK_H_ | 231 #endif // CONTENT_CHILD_WEBCRYPTO_JWK_H_ |
OLD | NEW |