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. | |
24 // | |
25 // This primarily exists to ensure strict enforcement of the JWK schema, as the | |
26 // type and presence of particular fields is security relevant. For example, | |
Ryan Sleevi
2014/11/04 20:59:50
s/fields/members/
eroman
2014/11/04 21:50:45
Done.
| |
27 // GetString() will ensure a given JSON field is present and is a string type, | |
Ryan Sleevi
2014/11/04 20:59:50
s/field/member/
eroman
2014/11/04 21:50:45
Done.
| |
28 // and will fail if these conditions aren't met. | |
29 // | |
30 // Users of JwkReader must call Init() successfully before any other method can | |
31 // be called. | |
32 class JwkReader { | |
33 public: | |
34 JwkReader(); | |
35 ~JwkReader(); | |
36 | |
37 // Initializes a JWK reader by parsing the JSON |bytes|. To succeed, the JWK | |
38 // must: | |
39 // * Have "kty" matching |expected_kty| | |
40 // * Have "ext" compatible with |expected_extractable| | |
41 // * Have usages ("use", "key_ops") compatible with |expected_usages| | |
42 // * Have an "alg" matching |expected_alg| | |
43 // | |
44 // NOTE: If |expected_alg| is empty, then the test on "alg" is skipped. | |
45 Status Init(const CryptoData& bytes, | |
46 bool expected_extractable, | |
47 blink::WebCryptoKeyUsageMask expected_usages, | |
48 const std::string& expected_kty, | |
49 const std::string& expected_alg); | |
50 | |
51 // Returns true if the member |member_name| is present. | |
52 bool HasMember(const std::string& member_name) const; | |
53 | |
54 // Extracts the required string member |member_name| and saves the result to | |
55 // |*result|. If the member does not exist or is not a string, returns an | |
56 // error. | |
57 Status GetString(const std::string& member_name, std::string* result) const; | |
58 | |
59 // Extracts the optional string member |member_name| and saves the result to | |
60 // |*result| if it was found. If the member exists and is not a string, | |
61 // returns an error. Otherwise returns success, and sets |*member_exists| if | |
62 // it was found. | |
63 Status GetOptionalString(const std::string& member_name, | |
64 std::string* result, | |
65 bool* member_exists) const; | |
66 | |
67 // Extracts the optional array member |member_name| and saves the result to | |
68 // |*result| if it was found. If the member exists and is not an array, | |
69 // returns an error. Otherwise returns success, and sets |*member_exists| if | |
70 // it was found. | |
71 // | |
72 // NOTE: |*result| is owned by the JwkReader. | |
73 Status GetOptionalList(const std::string& member_name, | |
74 base::ListValue** result, | |
75 bool* member_exists) const; | |
76 | |
77 // Extracts the required string member |member_name| and saves the | |
78 // base64url-decoded bytes to |*result|. If the member does not exist or is | |
79 // not a string, or could not be base64url-decoded, returns an error. | |
80 Status GetBytes(const std::string& member_name, std::string* result) const; | |
81 | |
82 // Extracts the required base64url member, which is interpreted as being a | |
83 // big-endian unsigned integer. | |
84 // | |
85 // Sequences that contain leading zeros will be rejected. | |
86 Status GetBigInteger(const std::string& member_name, | |
87 std::string* result) const; | |
88 | |
89 // Extracts the optional boolean member |member_name| and saves the result to | |
90 // |*result| if it was found. If the member exists and is not a boolean, | |
91 // returns an error. Otherwise returns success, and sets |*member_exists| if | |
92 // it was found. | |
93 Status GetOptionalBool(const std::string& member_name, | |
94 bool* result, | |
95 bool* member_exists) const; | |
96 | |
97 // Gets the optional algorithm ("alg") string. | |
98 Status GetAlg(std::string* alg, bool* has_alg) const; | |
99 | |
100 // Checks if the "alg" member matches |expected_alg|. | |
101 Status VerifyAlg(const std::string& expected_alg) const; | |
102 | |
103 private: | |
104 scoped_ptr<base::DictionaryValue> dict_; | |
105 }; | |
106 | |
107 // Helper class for building the JSON for a JWK. | |
108 class JwkWriter { | |
109 public: | |
110 // Initializes a writer, and sets the standard JWK members as indicated. | |
111 JwkWriter(const std::string& algorithm, | |
112 bool extractable, | |
113 blink::WebCryptoKeyUsageMask usages, | |
114 const std::string& kty); | |
115 | |
116 // Sets a string member |member_name| to |value|. | |
117 void SetString(const std::string& member_name, const std::string& value); | |
118 | |
119 // Sets a bytes member |value| to |value| by base64 url-safe encoding it. | |
120 void SetBytes(const std::string& member_name, const CryptoData& value); | |
121 | |
122 // Flattens the JWK to JSON (UTF-8 encoded if necessary, however in practice | |
123 // it will be ASCII). | |
124 void ToJson(std::vector<uint8_t>* utf8_bytes) const; | |
125 | |
126 private: | |
127 base::DictionaryValue dict_; | |
128 }; | |
129 | |
25 // Writes a JWK-formatted symmetric key to |jwk_key_data|. | 130 // Writes a JWK-formatted symmetric key to |jwk_key_data|. |
26 // * raw_key_data: The actual key data | 131 // * raw_key_data: The actual key data |
27 // * algorithm: The JWK algorithm name (i.e. "alg") | 132 // * algorithm: The JWK algorithm name (i.e. "alg") |
28 // * extractable: The JWK extractability (i.e. "ext") | 133 // * extractable: The JWK extractability (i.e. "ext") |
29 // * usages: The JWK usages (i.e. "key_ops") | 134 // * usages: The JWK usages (i.e. "key_ops") |
30 void WriteSecretKeyJwk(const CryptoData& raw_key_data, | 135 void WriteSecretKeyJwk(const CryptoData& raw_key_data, |
31 const std::string& algorithm, | 136 const std::string& algorithm, |
32 bool extractable, | 137 bool extractable, |
33 blink::WebCryptoKeyUsageMask usages, | 138 blink::WebCryptoKeyUsageMask usages, |
34 std::vector<uint8_t>* jwk_key_data); | 139 std::vector<uint8_t>* jwk_key_data); |
35 | 140 |
36 // Parses a UTF-8 encoded JWK (key_data), and extracts the key material to | 141 // Parses a UTF-8 encoded JWK (key_data), and extracts the key material to |
37 // |*raw_key_data|. Returns Status::Success() on success, otherwise an error. | 142 // |*raw_key_data|. Returns Status::Success() on success, otherwise an error. |
38 // In order for this to succeed: | 143 // In order for this to succeed: |
39 // * expected_algorithm must match the JWK's "alg", if present. | 144 // * expected_alg must match the JWK's "alg", if present. |
40 // * expected_extractable must be consistent with the JWK's "ext", if | 145 // * expected_extractable must be consistent with the JWK's "ext", if |
41 // present. | 146 // present. |
42 // * expected_usages must be a subset of the JWK's "key_ops" if present. | 147 // * expected_usages must be a subset of the JWK's "key_ops" if present. |
43 Status ReadSecretKeyJwk(const CryptoData& key_data, | 148 Status ReadSecretKeyJwk(const CryptoData& key_data, |
44 const std::string& expected_algorithm, | 149 const std::string& expected_alg, |
45 bool expected_extractable, | 150 bool expected_extractable, |
46 blink::WebCryptoKeyUsageMask expected_usages, | 151 blink::WebCryptoKeyUsageMask expected_usages, |
47 std::vector<uint8_t>* raw_key_data); | 152 std::vector<uint8_t>* raw_key_data); |
48 | 153 |
49 // Creates an AES algorithm name for the given key size (in bytes). For | 154 // Creates an AES algorithm name for the given key size (in bytes). For |
50 // instance "A128CBC" is the result of suffix="CBC", keylen_bytes=16. | 155 // instance "A128CBC" is the result of suffix="CBC", keylen_bytes=16. |
51 std::string MakeJwkAesAlgorithmName(const std::string& suffix, | 156 std::string MakeJwkAesAlgorithmName(const std::string& suffix, |
52 unsigned int keylen_bytes); | 157 unsigned int keylen_bytes); |
53 | 158 |
54 // This is very similar to ReadSecretKeyJwk(), except instead of specifying an | 159 // This is very similar to ReadSecretKeyJwk(), except instead of specifying an |
55 // absolut "expected_algorithm", the suffix for an AES algorithm name is given | 160 // absolute "expected_alg", the suffix for an AES algorithm name is given |
56 // (See MakeJwkAesAlgorithmName() for an explanation of what the suffix is). | 161 // (See MakeJwkAesAlgorithmName() for an explanation of what the suffix is). |
57 // | 162 // |
58 // This is because the algorithm name for AES keys is dependent on the length | 163 // This is because the algorithm name for AES keys is dependent on the length |
59 // of the key. This function expects key lengths to be either 128, 192, or 256 | 164 // of the key. This function expects key lengths to be either 128, 192, or 256 |
60 // bits. | 165 // bits. |
61 Status ReadAesSecretKeyJwk(const CryptoData& key_data, | 166 Status ReadAesSecretKeyJwk(const CryptoData& key_data, |
62 const std::string& algorithm_name_suffix, | 167 const std::string& algorithm_name_suffix, |
63 bool expected_extractable, | 168 bool expected_extractable, |
64 blink::WebCryptoKeyUsageMask expected_usages, | 169 blink::WebCryptoKeyUsageMask expected_usages, |
65 std::vector<uint8_t>* raw_key_data); | 170 std::vector<uint8_t>* raw_key_data); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
103 std::string p; | 208 std::string p; |
104 std::string q; | 209 std::string q; |
105 std::string dp; | 210 std::string dp; |
106 std::string dq; | 211 std::string dq; |
107 std::string qi; | 212 std::string qi; |
108 }; | 213 }; |
109 | 214 |
110 // Parses a UTF-8 encoded JWK (key_data), and extracts the RSA components to | 215 // Parses a UTF-8 encoded JWK (key_data), and extracts the RSA components to |
111 // |*result|. Returns Status::Success() on success, otherwise an error. | 216 // |*result|. Returns Status::Success() on success, otherwise an error. |
112 // In order for this to succeed: | 217 // In order for this to succeed: |
113 // * expected_algorithm must match the JWK's "alg", if present. | 218 // * expected_alg must match the JWK's "alg", if present. |
114 // * expected_extractable must be consistent with the JWK's "ext", if | 219 // * expected_extractable must be consistent with the JWK's "ext", if |
115 // present. | 220 // present. |
116 // * expected_usages must be a subset of the JWK's "key_ops" if present. | 221 // * expected_usages must be a subset of the JWK's "key_ops" if present. |
117 Status ReadRsaKeyJwk(const CryptoData& key_data, | 222 Status ReadRsaKeyJwk(const CryptoData& key_data, |
118 const std::string& expected_algorithm, | 223 const std::string& expected_alg, |
119 bool expected_extractable, | 224 bool expected_extractable, |
120 blink::WebCryptoKeyUsageMask expected_usages, | 225 blink::WebCryptoKeyUsageMask expected_usages, |
121 JwkRsaInfo* result); | 226 JwkRsaInfo* result); |
122 | 227 |
123 const char* GetJwkHmacAlgorithmName(blink::WebCryptoAlgorithmId hash); | 228 const char* GetJwkHmacAlgorithmName(blink::WebCryptoAlgorithmId hash); |
124 | 229 |
125 // This function decodes unpadded 'base64url' encoded data, as described in | 230 // This function decodes unpadded 'base64url' encoded data, as described in |
126 // RFC4648 (http://www.ietf.org/rfc/rfc4648.txt) Section 5. | 231 // RFC4648 (http://www.ietf.org/rfc/rfc4648.txt) Section 5. |
127 CONTENT_EXPORT bool Base64DecodeUrlSafe(const std::string& input, | 232 CONTENT_EXPORT bool Base64DecodeUrlSafe(const std::string& input, |
128 std::string* output); | 233 std::string* output); |
129 | 234 |
130 // Returns an unpadded 'base64url' encoding of the input data, the opposite of | 235 // Returns an unpadded 'base64url' encoding of the input data, the opposite of |
131 // Base64DecodeUrlSafe() above. | 236 // Base64DecodeUrlSafe() above. |
132 CONTENT_EXPORT std::string Base64EncodeUrlSafe(const base::StringPiece& input); | 237 CONTENT_EXPORT std::string Base64EncodeUrlSafe(const base::StringPiece& input); |
133 CONTENT_EXPORT std::string Base64EncodeUrlSafe( | 238 CONTENT_EXPORT std::string Base64EncodeUrlSafe( |
134 const std::vector<uint8_t>& input); | 239 const std::vector<uint8_t>& input); |
135 | 240 |
136 } // namespace webcrypto | 241 } // namespace webcrypto |
137 | 242 |
138 } // namespace content | 243 } // namespace content |
139 | 244 |
140 #endif // CONTENT_CHILD_WEBCRYPTO_JWK_H_ | 245 #endif // CONTENT_CHILD_WEBCRYPTO_JWK_H_ |
OLD | NEW |