blob: 2baa022e6fd4505d8c5c55428dd5911be0f7fd25 [file] [log] [blame]
[email protected]c263fbe2014-03-26 09:50:571// 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.
4
5#ifndef CONTENT_CHILD_WEBCRYPTO_JWK_H_
6#define CONTENT_CHILD_WEBCRYPTO_JWK_H_
7
[email protected]53b6c9d22014-07-19 05:08:388#include <stdint.h>
[email protected]88be98562014-04-30 11:18:599#include <vector>
10
[email protected]38409aec2014-07-19 00:54:5111#include "base/values.h"
[email protected]c263fbe2014-03-26 09:50:5712#include "third_party/WebKit/public/platform/WebArrayBuffer.h"
13#include "third_party/WebKit/public/platform/WebCrypto.h"
14#include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
15
16namespace content {
17
18namespace webcrypto {
19
20class CryptoData;
21class Status;
22
[email protected]38409aec2014-07-19 00:54:5123// Writes a JWK-formatted symmetric key to |jwk_key_data|.
24// * raw_key_data: The actual key data
25// * algorithm: The JWK algorithm name (i.e. "alg")
26// * extractable: The JWK extractability (i.e. "ext")
27// * usage_mask: The JWK usages (i.e. "key_ops")
28void WriteSecretKeyJwk(const CryptoData& raw_key_data,
29 const std::string& algorithm,
30 bool extractable,
31 blink::WebCryptoKeyUsageMask usage_mask,
[email protected]53b6c9d22014-07-19 05:08:3832 std::vector<uint8_t>* jwk_key_data);
[email protected]c263fbe2014-03-26 09:50:5733
[email protected]38409aec2014-07-19 00:54:5134// Parses a UTF-8 encoded JWK (key_data), and extracts the key material to
35// |*raw_key_data|. Returns Status::Success() on success, otherwise an error.
36// In order for this to succeed:
37// * expected_algorithm must match the JWK's "alg", if present.
38// * expected_extractable must be consistent with the JWK's "ext", if
39// present.
40// * expected_usage_mask must be a subset of the JWK's "key_ops" if present.
41Status ReadSecretKeyJwk(const CryptoData& key_data,
42 const std::string& expected_algorithm,
43 bool expected_extractable,
44 blink::WebCryptoKeyUsageMask expected_usage_mask,
[email protected]53b6c9d22014-07-19 05:08:3845 std::vector<uint8_t>* raw_key_data);
[email protected]38409aec2014-07-19 00:54:5146
47// Creates an AES algorithm name for the given key size (in bytes). For
48// instance "A128CBC" is the result of suffix="CBC", keylen_bytes=16.
49std::string MakeJwkAesAlgorithmName(const std::string& suffix,
50 unsigned int keylen_bytes);
51
52// This is very similar to ReadSecretKeyJwk(), except instead of specifying an
53// absolut "expected_algorithm", the suffix for an AES algorithm name is given
54// (See MakeJwkAesAlgorithmName() for an explanation of what the suffix is).
55//
56// This is because the algorithm name for AES keys is dependent on the length
57// of the key. This function expects key lengths to be either 128, 192, or 256
58// bits.
59Status ReadAesSecretKeyJwk(const CryptoData& key_data,
60 const std::string& algorithm_name_suffix,
61 bool expected_extractable,
62 blink::WebCryptoKeyUsageMask expected_usage_mask,
[email protected]53b6c9d22014-07-19 05:08:3863 std::vector<uint8_t>* raw_key_data);
[email protected]38409aec2014-07-19 00:54:5164
65// Writes a JWK-formated RSA public key and saves the result to
66// |*jwk_key_data|.
67void WriteRsaPublicKeyJwk(const CryptoData& n,
68 const CryptoData& e,
69 const std::string& algorithm,
70 bool extractable,
71 blink::WebCryptoKeyUsageMask usage_mask,
[email protected]53b6c9d22014-07-19 05:08:3872 std::vector<uint8_t>* jwk_key_data);
[email protected]38409aec2014-07-19 00:54:5173
74// Writes a JWK-formated RSA private key and saves the result to
75// |*jwk_key_data|.
76void WriteRsaPrivateKeyJwk(const CryptoData& n,
77 const CryptoData& e,
78 const CryptoData& d,
79 const CryptoData& p,
80 const CryptoData& q,
81 const CryptoData& dp,
82 const CryptoData& dq,
83 const CryptoData& qi,
84 const std::string& algorithm,
85 bool extractable,
86 blink::WebCryptoKeyUsageMask usage_mask,
[email protected]53b6c9d22014-07-19 05:08:3887 std::vector<uint8_t>* jwk_key_data);
[email protected]38409aec2014-07-19 00:54:5188
89// Describes the RSA components for a parsed key. The names of the properties
90// correspond with those from the JWK spec. Note that Chromium's WebCrypto
91// implementation does not support multi-primes, so there is no parsed field
92// for othinfo.
93struct JwkRsaInfo {
94 JwkRsaInfo();
95 ~JwkRsaInfo();
96
97 bool is_private_key;
98 std::string n;
99 std::string e;
100 std::string d;
101 std::string p;
102 std::string q;
103 std::string dp;
104 std::string dq;
105 std::string qi;
106};
107
108// Parses a UTF-8 encoded JWK (key_data), and extracts the RSA components to
109// |*result|. Returns Status::Success() on success, otherwise an error.
110// In order for this to succeed:
111// * expected_algorithm must match the JWK's "alg", if present.
112// * expected_extractable must be consistent with the JWK's "ext", if
113// present.
114// * expected_usage_mask must be a subset of the JWK's "key_ops" if present.
115Status ReadRsaKeyJwk(const CryptoData& key_data,
116 const std::string& expected_algorithm,
117 bool expected_extractable,
118 blink::WebCryptoKeyUsageMask expected_usage_mask,
119 JwkRsaInfo* result);
120
121const char* GetJwkHmacAlgorithmName(blink::WebCryptoAlgorithmId hash);
[email protected]c263fbe2014-03-26 09:50:57122
123} // namespace webcrypto
124
125} // namespace content
126
127#endif // CONTENT_CHILD_WEBCRYPTO_JWK_H_