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.
4
5#ifndef CONTENT_CHILD_WEBCRYPTO_TEST_TEST_HELPERS_H_
6#define CONTENT_CHILD_WEBCRYPTO_TEST_TEST_HELPERS_H_
7
8#include <ostream>
9#include <string>
10#include <vector>
11
12#include "base/memory/scoped_ptr.h"
13#include "testing/gtest/include/gtest/gtest.h"
14#include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
15#include "third_party/WebKit/public/platform/WebCryptoKey.h"
16
17#define EXPECT_BYTES_EQ(expected, actual) \
18  EXPECT_EQ(CryptoData(expected), CryptoData(actual))
19
20#define EXPECT_BYTES_EQ_HEX(expected_hex, actual_bytes) \
21  EXPECT_BYTES_EQ(HexStringToBytes(expected_hex), actual_bytes)
22
23namespace base {
24class DictionaryValue;
25class ListValue;
26class Value;
27}
28
29namespace blink {
30class WebCryptoAlgorithm;
31}
32
33namespace content {
34
35namespace webcrypto {
36
37class Status;
38class CryptoData;
39
40// These functions are used by GTEST to support EXPECT_EQ() for
41// webcrypto::Status and webcrypto::CryptoData
42
43void PrintTo(const Status& status, ::std::ostream* os);
44bool operator==(const Status& a, const Status& b);
45bool operator!=(const Status& a, const Status& b);
46
47void PrintTo(const CryptoData& data, ::std::ostream* os);
48bool operator==(const CryptoData& a, const CryptoData& b);
49bool operator!=(const CryptoData& a, const CryptoData& b);
50
51// TODO(eroman): For Linux builds using system NSS, AES-GCM and RSA-OAEP, and
52// RSA key import are a runtime dependency.
53bool SupportsAesGcm();
54bool SupportsRsaOaep();
55bool SupportsRsaPrivateKeyImport();
56
57blink::WebCryptoAlgorithm CreateRsaHashedKeyGenAlgorithm(
58    blink::WebCryptoAlgorithmId algorithm_id,
59    const blink::WebCryptoAlgorithmId hash_id,
60    unsigned int modulus_length,
61    const std::vector<uint8_t>& public_exponent);
62
63// Returns a slightly modified version of the input vector.
64//
65//  - For non-empty inputs a single bit is inverted.
66//  - For empty inputs, a byte is added.
67std::vector<uint8_t> Corrupted(const std::vector<uint8_t>& input);
68
69std::vector<uint8_t> HexStringToBytes(const std::string& hex);
70
71std::vector<uint8_t> MakeJsonVector(const std::string& json_string);
72std::vector<uint8_t> MakeJsonVector(const base::DictionaryValue& dict);
73
74// ----------------------------------------------------------------
75// Helpers for working with JSON data files for test expectations.
76// ----------------------------------------------------------------
77
78// Reads a file in "src/content/test/data/webcrypto" to a base::Value.
79// The file must be JSON, however it can also include C++ style comments.
80::testing::AssertionResult ReadJsonTestFile(const char* test_file_name,
81                                            scoped_ptr<base::Value>* value);
82// Same as ReadJsonTestFile(), but returns the value as a List.
83::testing::AssertionResult ReadJsonTestFileToList(
84    const char* test_file_name,
85    scoped_ptr<base::ListValue>* list);
86
87// Reads a string property from the dictionary with path |property_name|
88// (which can include periods for nested dictionaries). Interprets the
89// string as a hex encoded string and converts it to a bytes list.
90//
91// Returns empty vector on failure.
92std::vector<uint8_t> GetBytesFromHexString(base::DictionaryValue* dict,
93                                           const char* property_name);
94
95// Reads a string property with path "property_name" and converts it to a
96// WebCryptoAlgorith. Returns null algorithm on failure.
97blink::WebCryptoAlgorithm GetDigestAlgorithm(base::DictionaryValue* dict,
98                                             const char* property_name);
99
100// Returns true if any of the vectors in the input list have identical content.
101bool CopiesExist(const std::vector<std::vector<uint8_t> >& bufs);
102
103blink::WebCryptoAlgorithm CreateAesKeyGenAlgorithm(
104    blink::WebCryptoAlgorithmId aes_alg_id,
105    unsigned short length);
106
107// The following key pair is comprised of the SPKI (public key) and PKCS#8
108// (private key) representations of the key pair provided in Example 1 of the
109// NIST test vectors at
110// ftp://ftp.rsa.com/pub/rsalabs/tmp/pkcs1v15sign-vectors.txt
111extern const unsigned int kModulusLengthBits;
112extern const char* const kPublicKeySpkiDerHex;
113extern const char* const kPrivateKeyPkcs8DerHex;
114
115// The modulus and exponent (in hex) of kPublicKeySpkiDerHex
116extern const char* const kPublicKeyModulusHex;
117extern const char* const kPublicKeyExponentHex;
118
119blink::WebCryptoKey ImportSecretKeyFromRaw(
120    const std::vector<uint8_t>& key_raw,
121    const blink::WebCryptoAlgorithm& algorithm,
122    blink::WebCryptoKeyUsageMask usage);
123
124void ImportRsaKeyPair(const std::vector<uint8_t>& spki_der,
125                      const std::vector<uint8_t>& pkcs8_der,
126                      const blink::WebCryptoAlgorithm& algorithm,
127                      bool extractable,
128                      blink::WebCryptoKeyUsageMask public_key_usage_mask,
129                      blink::WebCryptoKeyUsageMask private_key_usage_mask,
130                      blink::WebCryptoKey* public_key,
131                      blink::WebCryptoKey* private_key);
132
133Status ImportKeyJwkFromDict(const base::DictionaryValue& dict,
134                            const blink::WebCryptoAlgorithm& algorithm,
135                            bool extractable,
136                            blink::WebCryptoKeyUsageMask usage_mask,
137                            blink::WebCryptoKey* key);
138
139// Parses a vector of JSON into a dictionary.
140scoped_ptr<base::DictionaryValue> GetJwkDictionary(
141    const std::vector<uint8_t>& json);
142
143// Verifies the input dictionary contains the expected values. Exact matches are
144// required on the fields examined.
145::testing::AssertionResult VerifyJwk(
146    const scoped_ptr<base::DictionaryValue>& dict,
147    const std::string& kty_expected,
148    const std::string& alg_expected,
149    blink::WebCryptoKeyUsageMask use_mask_expected);
150
151::testing::AssertionResult VerifySecretJwk(
152    const std::vector<uint8_t>& json,
153    const std::string& alg_expected,
154    const std::string& k_expected_hex,
155    blink::WebCryptoKeyUsageMask use_mask_expected);
156
157// Verifies that the JSON in the input vector contains the provided
158// expected values. Exact matches are required on the fields examined.
159::testing::AssertionResult VerifyPublicJwk(
160    const std::vector<uint8_t>& json,
161    const std::string& alg_expected,
162    const std::string& n_expected_hex,
163    const std::string& e_expected_hex,
164    blink::WebCryptoKeyUsageMask use_mask_expected);
165
166// Helper that tests importing ane exporting of symmetric keys as JWK.
167void ImportExportJwkSymmetricKey(
168    int key_len_bits,
169    const blink::WebCryptoAlgorithm& import_algorithm,
170    blink::WebCryptoKeyUsageMask usages,
171    const std::string& jwk_alg);
172
173}  // namespace webcrypto
174
175}  // namesapce content
176
177#endif  // CONTENT_CHILD_WEBCRYPTO_TEST_TEST_HELPERS_H_
178