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 // 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 |
| 33 // must have kty of xpected_kty, have an ext compatible with |
| 34 // |expected_extractable| and have usages compatible with expected_usages. |
| 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; |
| 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|. |
| 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 |
| 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 // Checks if the |alg| property matches |expected_algorithm|. |
| 84 Status VerifyAlg(const std::string& expected_algorithm) const; |
| 85 |
| 86 private: |
| 87 scoped_ptr<base::DictionaryValue> dict_; |
| 88 }; |
| 89 |
| 90 // Helper class for building the JSON for a JWK. |
| 91 class JwkWriter { |
| 92 public: |
| 93 // Initializes a writer, and sets the standard JWK properties as indicated. |
| 94 JwkWriter(const std::string& algorithm, |
| 95 bool extractable, |
| 96 blink::WebCryptoKeyUsageMask usages, |
| 97 const std::string& kty); |
| 98 |
| 99 // Sets a string parameter |value|. |
| 100 void SetString(const std::string& key, const std::string& value); |
| 101 |
| 102 // Sets a bytes parameter |value|, by base64 url-safe encoding it. |
| 103 void SetBytes(const std::string& key, const CryptoData& value); |
| 104 |
| 105 // Flattens the JWK to JSON (utf-8 encoded if necessary, however in practice |
| 106 // it will be ASCII). |
| 107 void ToJson(std::vector<uint8_t>* utf8_bytes) const; |
| 108 |
| 109 private: |
| 110 base::DictionaryValue dict_; |
| 111 }; |
| 112 |
25 // Writes a JWK-formatted symmetric key to |jwk_key_data|. | 113 // Writes a JWK-formatted symmetric key to |jwk_key_data|. |
26 // * raw_key_data: The actual key data | 114 // * raw_key_data: The actual key data |
27 // * algorithm: The JWK algorithm name (i.e. "alg") | 115 // * algorithm: The JWK algorithm name (i.e. "alg") |
28 // * extractable: The JWK extractability (i.e. "ext") | 116 // * extractable: The JWK extractability (i.e. "ext") |
29 // * usages: The JWK usages (i.e. "key_ops") | 117 // * usages: The JWK usages (i.e. "key_ops") |
30 void WriteSecretKeyJwk(const CryptoData& raw_key_data, | 118 void WriteSecretKeyJwk(const CryptoData& raw_key_data, |
31 const std::string& algorithm, | 119 const std::string& algorithm, |
32 bool extractable, | 120 bool extractable, |
33 blink::WebCryptoKeyUsageMask usages, | 121 blink::WebCryptoKeyUsageMask usages, |
34 std::vector<uint8_t>* jwk_key_data); | 122 std::vector<uint8_t>* jwk_key_data); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 // Base64DecodeUrlSafe() above. | 219 // Base64DecodeUrlSafe() above. |
132 CONTENT_EXPORT std::string Base64EncodeUrlSafe(const base::StringPiece& input); | 220 CONTENT_EXPORT std::string Base64EncodeUrlSafe(const base::StringPiece& input); |
133 CONTENT_EXPORT std::string Base64EncodeUrlSafe( | 221 CONTENT_EXPORT std::string Base64EncodeUrlSafe( |
134 const std::vector<uint8_t>& input); | 222 const std::vector<uint8_t>& input); |
135 | 223 |
136 } // namespace webcrypto | 224 } // namespace webcrypto |
137 | 225 |
138 } // namespace content | 226 } // namespace content |
139 | 227 |
140 #endif // CONTENT_CHILD_WEBCRYPTO_JWK_H_ | 228 #endif // CONTENT_CHILD_WEBCRYPTO_JWK_H_ |
OLD | NEW |