1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/webcrypto/webcrypto_util.h"
#include "base/logging.h"
#include "base/numerics/safe_math.h"
#include "components/webcrypto/status.h"
#include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
#include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
#include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
namespace webcrypto {
namespace {
// Converts a (big-endian) WebCrypto BigInteger, with or without leading zeros,
// to unsigned int.
bool BigIntegerToUint(const uint8_t* data,
size_t data_size,
unsigned int* result) {
if (data_size == 0)
return false;
*result = 0;
for (size_t i = 0; i < data_size; ++i) {
size_t reverse_i = data_size - i - 1;
if (reverse_i >= sizeof(*result) && data[i])
return false; // Too large for a uint.
*result |= data[i] << 8 * reverse_i;
}
return true;
}
Status GetShaBlockSizeBits(const blink::WebCryptoAlgorithm& algorithm,
unsigned int* block_size_bits) {
switch (algorithm.id()) {
case blink::WebCryptoAlgorithmIdSha1:
case blink::WebCryptoAlgorithmIdSha256:
*block_size_bits = 512;
return Status::Success();
case blink::WebCryptoAlgorithmIdSha384:
case blink::WebCryptoAlgorithmIdSha512:
*block_size_bits = 1024;
return Status::Success();
default:
return Status::ErrorUnsupported();
}
}
} // namespace
blink::WebCryptoAlgorithm CreateAlgorithm(blink::WebCryptoAlgorithmId id) {
return blink::WebCryptoAlgorithm::adoptParamsAndCreate(id, NULL);
}
blink::WebCryptoAlgorithm CreateHmacImportAlgorithm(
blink::WebCryptoAlgorithmId hash_id,
unsigned int length_bits) {
DCHECK(blink::WebCryptoAlgorithm::isHash(hash_id));
return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
blink::WebCryptoAlgorithmIdHmac,
new blink::WebCryptoHmacImportParams(CreateAlgorithm(hash_id), true,
length_bits));
}
blink::WebCryptoAlgorithm CreateHmacImportAlgorithmNoLength(
blink::WebCryptoAlgorithmId hash_id) {
DCHECK(blink::WebCryptoAlgorithm::isHash(hash_id));
return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
blink::WebCryptoAlgorithmIdHmac,
new blink::WebCryptoHmacImportParams(CreateAlgorithm(hash_id), false, 0));
}
blink::WebCryptoAlgorithm CreateRsaHashedImportAlgorithm(
blink::WebCryptoAlgorithmId id,
blink::WebCryptoAlgorithmId hash_id) {
DCHECK(blink::WebCryptoAlgorithm::isHash(hash_id));
return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
id, new blink::WebCryptoRsaHashedImportParams(CreateAlgorithm(hash_id)));
}
blink::WebCryptoAlgorithm CreateEcImportAlgorithm(
blink::WebCryptoAlgorithmId id,
blink::WebCryptoNamedCurve named_curve) {
return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
id, new blink::WebCryptoEcKeyImportParams(named_curve));
}
bool ContainsKeyUsages(blink::WebCryptoKeyUsageMask a,
blink::WebCryptoKeyUsageMask b) {
return (a & b) == b;
}
// TODO(eroman): Move this helper to WebCryptoKey.
bool KeyUsageAllows(const blink::WebCryptoKey& key,
const blink::WebCryptoKeyUsage usage) {
return ((key.usages() & usage) != 0);
}
// The WebCrypto spec defines the default value for the tag length, as well as
// the allowed values for tag length.
Status GetAesGcmTagLengthInBits(const blink::WebCryptoAesGcmParams* params,
unsigned int* tag_length_bits) {
*tag_length_bits = 128;
if (params->hasTagLengthBits())
*tag_length_bits = params->optionalTagLengthBits();
if (*tag_length_bits != 32 && *tag_length_bits != 64 &&
*tag_length_bits != 96 && *tag_length_bits != 104 &&
*tag_length_bits != 112 && *tag_length_bits != 120 &&
*tag_length_bits != 128)
return Status::ErrorInvalidAesGcmTagLength();
return Status::Success();
}
Status GetAesKeyGenLengthInBits(const blink::WebCryptoAesKeyGenParams* params,
unsigned int* keylen_bits) {
*keylen_bits = params->lengthBits();
if (*keylen_bits == 128 || *keylen_bits == 256)
return Status::Success();
// BoringSSL does not support 192-bit AES.
if (*keylen_bits == 192)
return Status::ErrorAes192BitUnsupported();
return Status::ErrorGenerateAesKeyLength();
}
Status GetHmacKeyGenLengthInBits(const blink::WebCryptoHmacKeyGenParams* params,
unsigned int* keylen_bits) {
if (!params->hasLengthBits())
return GetShaBlockSizeBits(params->hash(), keylen_bits);
*keylen_bits = params->optionalLengthBits();
// Zero-length HMAC keys are disallowed by the spec.
if (*keylen_bits == 0)
return Status::ErrorGenerateHmacKeyLengthZero();
return Status::Success();
}
Status GetHmacImportKeyLengthBits(
const blink::WebCryptoHmacImportParams* params,
unsigned int key_data_byte_length,
unsigned int* keylen_bits) {
if (key_data_byte_length == 0)
return Status::ErrorHmacImportEmptyKey();
// Make sure that the key data's length can be represented in bits without
// overflow.
base::CheckedNumeric<unsigned int> checked_keylen_bits(key_data_byte_length);
checked_keylen_bits *= 8;
if (!checked_keylen_bits.IsValid())
return Status::ErrorDataTooLarge();
unsigned int data_keylen_bits = checked_keylen_bits.ValueOrDie();
// Determine how many bits of the input to use.
*keylen_bits = data_keylen_bits;
if (params->hasLengthBits()) {
// The requested bit length must be:
// * No longer than the input data length
// * At most 7 bits shorter.
if (NumBitsToBytes(params->optionalLengthBits()) != key_data_byte_length)
return Status::ErrorHmacImportBadLength();
*keylen_bits = params->optionalLengthBits();
}
return Status::Success();
}
Status VerifyAesKeyLengthForImport(unsigned int keylen_bytes) {
if (keylen_bytes == 16 || keylen_bytes == 32)
return Status::Success();
// BoringSSL does not support 192-bit AES.
if (keylen_bytes == 24)
return Status::ErrorAes192BitUnsupported();
return Status::ErrorImportAesKeyLength();
}
Status CheckKeyCreationUsages(blink::WebCryptoKeyUsageMask all_possible_usages,
blink::WebCryptoKeyUsageMask actual_usages,
bool allow_empty_usages) {
if (!allow_empty_usages && actual_usages == 0)
return Status::ErrorCreateKeyEmptyUsages();
if (!ContainsKeyUsages(all_possible_usages, actual_usages))
return Status::ErrorCreateKeyBadUsages();
return Status::Success();
}
Status GetRsaKeyGenParameters(
const blink::WebCryptoRsaHashedKeyGenParams* params,
unsigned int* public_exponent,
unsigned int* modulus_length_bits) {
*modulus_length_bits = params->modulusLengthBits();
// Limit key sizes to those supported by NSS:
// * Multiple of 8 bits
// * 256 bits to 16K bits
if (*modulus_length_bits < 256 || *modulus_length_bits > 16384 ||
(*modulus_length_bits % 8) != 0) {
return Status::ErrorGenerateRsaUnsupportedModulus();
}
if (!BigIntegerToUint(params->publicExponent().data(),
params->publicExponent().size(), public_exponent)) {
return Status::ErrorGenerateKeyPublicExponent();
}
// OpenSSL hangs when given bad public exponents, whereas NSS simply fails. To
// avoid feeding OpenSSL data that will hang use a whitelist.
if (*public_exponent != 3 && *public_exponent != 65537)
return Status::ErrorGenerateKeyPublicExponent();
return Status::Success();
}
Status VerifyUsagesBeforeImportAsymmetricKey(
blink::WebCryptoKeyFormat format,
blink::WebCryptoKeyUsageMask all_public_key_usages,
blink::WebCryptoKeyUsageMask all_private_key_usages,
blink::WebCryptoKeyUsageMask usages) {
switch (format) {
case blink::WebCryptoKeyFormatSpki:
return CheckKeyCreationUsages(all_public_key_usages, usages, true);
case blink::WebCryptoKeyFormatPkcs8:
return CheckKeyCreationUsages(all_private_key_usages, usages, false);
case blink::WebCryptoKeyFormatJwk: {
// The JWK could represent either a public key or private key. The usages
// must make sense for one of the two. The usages will be checked again by
// ImportKeyJwk() once the key type has been determined.
if (CheckKeyCreationUsages(all_public_key_usages, usages, true)
.IsError() &&
CheckKeyCreationUsages(all_private_key_usages, usages, false)
.IsError()) {
return Status::ErrorCreateKeyBadUsages();
}
return Status::Success();
}
default:
return Status::ErrorUnsupportedImportKeyFormat();
}
}
void TruncateToBitLength(size_t length_bits, std::vector<uint8_t>* bytes) {
size_t length_bytes = NumBitsToBytes(length_bits);
if (bytes->size() != length_bytes) {
CHECK_LT(length_bytes, bytes->size());
bytes->resize(length_bytes);
}
size_t remainder_bits = length_bits % 8;
// Zero any "unused bits" in the final byte
if (remainder_bits)
(*bytes)[bytes->size() - 1] &= ~((0xFF) >> remainder_bits);
}
Status GetAesKeyLength(const blink::WebCryptoAlgorithm& key_length_algorithm,
bool* has_length_bits,
unsigned int* length_bits) {
const blink::WebCryptoAesDerivedKeyParams* params =
key_length_algorithm.aesDerivedKeyParams();
*has_length_bits = true;
*length_bits = params->lengthBits();
if (*length_bits == 128 || *length_bits == 256)
return Status::Success();
// BoringSSL does not support 192-bit AES.
if (*length_bits == 192)
return Status::ErrorAes192BitUnsupported();
return Status::ErrorGetAesKeyLength();
}
Status GetHmacKeyLength(const blink::WebCryptoAlgorithm& key_length_algorithm,
bool* has_length_bits,
unsigned int* length_bits) {
const blink::WebCryptoHmacImportParams* params =
key_length_algorithm.hmacImportParams();
if (params->hasLengthBits()) {
*has_length_bits = true;
*length_bits = params->optionalLengthBits();
if (*length_bits == 0)
return Status::ErrorGetHmacKeyLengthZero();
return Status::Success();
}
*has_length_bits = true;
return GetShaBlockSizeBits(params->hash(), length_bits);
}
Status GetUsagesForGenerateAsymmetricKey(
blink::WebCryptoKeyUsageMask combined_usages,
blink::WebCryptoKeyUsageMask all_public_usages,
blink::WebCryptoKeyUsageMask all_private_usages,
blink::WebCryptoKeyUsageMask* public_usages,
blink::WebCryptoKeyUsageMask* private_usages) {
Status status = CheckKeyCreationUsages(all_public_usages | all_private_usages,
combined_usages, true);
if (status.IsError())
return status;
*public_usages = combined_usages & all_public_usages;
*private_usages = combined_usages & all_private_usages;
if (*private_usages == 0)
return Status::ErrorCreateKeyEmptyUsages();
return Status::Success();
}
} // namespace webcrypto
|