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
3 // found in the LICENSE file.
5 #ifndef CONTENT_CHILD_WEBCRYPTO_JWK_H_
6 #define CONTENT_CHILD_WEBCRYPTO_JWK_H_
11 #include "base/strings/string_piece.h"
12 #include "base/values.h"
13 #include "content/common/content_export.h"
14 #include "third_party/WebKit/public/platform/WebCrypto.h"
23 // Helper class for parsing a JWK from JSON.
25 // This primarily exists to ensure strict enforcement of the JWK schema, as the
26 // type and presence of particular members is security relevant. For example,
27 // GetString() will ensure a given JSON member is present and is a string type,
28 // and will fail if these conditions aren't met.
30 // Users of JwkReader must call Init() successfully before any other method can
37 // Initializes a JWK reader by parsing the JSON |bytes|. To succeed, the JWK
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|
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);
51 // Returns true if the member |member_name| is present.
52 bool HasMember(const std::string& member_name) const;
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
57 Status GetString(const std::string& member_name, std::string* result) const;
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
63 Status GetOptionalString(const std::string& member_name,
65 bool* member_exists) const;
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
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;
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;
82 // Extracts the required base64url member, which is interpreted as being a
83 // big-endian unsigned integer.
85 // Sequences that contain leading zeros will be rejected.
86 Status GetBigInteger(const std::string& member_name,
87 std::string* result) const;
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
93 Status GetOptionalBool(const std::string& member_name,
95 bool* member_exists) const;
97 // Gets the optional algorithm ("alg") string.
98 Status GetAlg(std::string* alg, bool* has_alg) const;
100 // Checks if the "alg" member matches |expected_alg|.
101 Status VerifyAlg(const std::string& expected_alg) const;
104 scoped_ptr<base::DictionaryValue> dict_;
107 // Helper class for building the JSON for a JWK.
110 // Initializes a writer, and sets the standard JWK members as indicated.
111 JwkWriter(const std::string& algorithm,
113 blink::WebCryptoKeyUsageMask usages,
114 const std::string& kty);
116 // Sets a string member |member_name| to |value|.
117 void SetString(const std::string& member_name, const std::string& value);
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);
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;
127 base::DictionaryValue dict_;
130 // Writes a JWK-formatted symmetric key to |jwk_key_data|.
131 // * raw_key_data: The actual key data
132 // * algorithm: The JWK algorithm name (i.e. "alg")
133 // * extractable: The JWK extractability (i.e. "ext")
134 // * usages: The JWK usages (i.e. "key_ops")
135 void WriteSecretKeyJwk(const CryptoData& raw_key_data,
136 const std::string& algorithm,
138 blink::WebCryptoKeyUsageMask usages,
139 std::vector<uint8_t>* jwk_key_data);
141 // Parses a UTF-8 encoded JWK (key_data), and extracts the key material to
142 // |*raw_key_data|. Returns Status::Success() on success, otherwise an error.
143 // In order for this to succeed:
144 // * expected_alg must match the JWK's "alg", if present.
145 // * expected_extractable must be consistent with the JWK's "ext", if
147 // * expected_usages must be a subset of the JWK's "key_ops" if present.
148 Status ReadSecretKeyJwk(const CryptoData& key_data,
149 const std::string& expected_alg,
150 bool expected_extractable,
151 blink::WebCryptoKeyUsageMask expected_usages,
152 std::vector<uint8_t>* raw_key_data);
154 // Creates an AES algorithm name for the given key size (in bytes). For
155 // instance "A128CBC" is the result of suffix="CBC", keylen_bytes=16.
156 std::string MakeJwkAesAlgorithmName(const std::string& suffix,
157 unsigned int keylen_bytes);
159 // This is very similar to ReadSecretKeyJwk(), except instead of specifying an
160 // absolute "expected_alg", the suffix for an AES algorithm name is given
161 // (See MakeJwkAesAlgorithmName() for an explanation of what the suffix is).
163 // This is because the algorithm name for AES keys is dependent on the length
164 // of the key. This function expects key lengths to be either 128, 192, or 256
166 Status ReadAesSecretKeyJwk(const CryptoData& key_data,
167 const std::string& algorithm_name_suffix,
168 bool expected_extractable,
169 blink::WebCryptoKeyUsageMask expected_usages,
170 std::vector<uint8_t>* raw_key_data);
172 // Writes a JWK-formated RSA public key and saves the result to
174 void WriteRsaPublicKeyJwk(const CryptoData& n,
176 const std::string& algorithm,
178 blink::WebCryptoKeyUsageMask usages,
179 std::vector<uint8_t>* jwk_key_data);
181 // Writes a JWK-formated RSA private key and saves the result to
183 void WriteRsaPrivateKeyJwk(const CryptoData& n,
188 const CryptoData& dp,
189 const CryptoData& dq,
190 const CryptoData& qi,
191 const std::string& algorithm,
193 blink::WebCryptoKeyUsageMask usages,
194 std::vector<uint8_t>* jwk_key_data);
196 // Describes the RSA components for a parsed key. The names of the properties
197 // correspond with those from the JWK spec. Note that Chromium's WebCrypto
198 // implementation does not support multi-primes, so there is no parsed field
215 // Parses a UTF-8 encoded JWK (key_data), and extracts the RSA components to
216 // |*result|. Returns Status::Success() on success, otherwise an error.
217 // In order for this to succeed:
218 // * expected_alg must match the JWK's "alg", if present.
219 // * expected_extractable must be consistent with the JWK's "ext", if
221 // * expected_usages must be a subset of the JWK's "key_ops" if present.
222 Status ReadRsaKeyJwk(const CryptoData& key_data,
223 const std::string& expected_alg,
224 bool expected_extractable,
225 blink::WebCryptoKeyUsageMask expected_usages,
228 const char* GetJwkHmacAlgorithmName(blink::WebCryptoAlgorithmId hash);
230 // This function decodes unpadded 'base64url' encoded data, as described in
231 // RFC4648 (http://www.ietf.org/rfc/rfc4648.txt) Section 5.
232 CONTENT_EXPORT bool Base64DecodeUrlSafe(const std::string& input,
233 std::string* output);
235 // Returns an unpadded 'base64url' encoding of the input data, the opposite of
236 // Base64DecodeUrlSafe() above.
237 CONTENT_EXPORT std::string Base64EncodeUrlSafe(const base::StringPiece& input);
238 CONTENT_EXPORT std::string Base64EncodeUrlSafe(
239 const std::vector<uint8_t>& input);
241 } // namespace webcrypto
243 } // namespace content
245 #endif // CONTENT_CHILD_WEBCRYPTO_JWK_H_