Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(507)

Side by Side Diff: content/renderer/webcrypto/webcrypto_impl_nss.cc

Issue 75653002: [webcrypto] Add RSAES-PKCS1-v1_5 encrypt and decrypt for NSS. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 #include "content/renderer/webcrypto/webcrypto_impl.h" 5 #include "content/renderer/webcrypto/webcrypto_impl.h"
6 6
7 #include <cryptohi.h> 7 #include <cryptohi.h>
8 #include <pk11pub.h> 8 #include <pk11pub.h>
9 #include <sechash.h> 9 #include <sechash.h>
10 10
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 void WebCryptoImpl::Init() { 243 void WebCryptoImpl::Init() {
244 crypto::EnsureNSSInit(); 244 crypto::EnsureNSSInit();
245 } 245 }
246 246
247 bool WebCryptoImpl::EncryptInternal( 247 bool WebCryptoImpl::EncryptInternal(
248 const blink::WebCryptoAlgorithm& algorithm, 248 const blink::WebCryptoAlgorithm& algorithm,
249 const blink::WebCryptoKey& key, 249 const blink::WebCryptoKey& key,
250 const unsigned char* data, 250 const unsigned char* data,
251 unsigned data_size, 251 unsigned data_size,
252 blink::WebArrayBuffer* buffer) { 252 blink::WebArrayBuffer* buffer) {
253
254 DCHECK_EQ(algorithm.id(), key.algorithm().id());
255 DCHECK(key.handle());
256 DCHECK(buffer);
257
253 if (algorithm.id() == blink::WebCryptoAlgorithmIdAesCbc) { 258 if (algorithm.id() == blink::WebCryptoAlgorithmIdAesCbc) {
254 return AesCbcEncryptDecrypt( 259 return AesCbcEncryptDecrypt(
255 CKA_ENCRYPT, algorithm, key, data, data_size, buffer); 260 CKA_ENCRYPT, algorithm, key, data, data_size, buffer);
261 } else if (algorithm.id() == blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5) {
262
263 // RSAES encryption does not support empty input
264 if (!data_size)
265 return false;
266 DCHECK(data);
267
268 if (!key.type() == blink::WebCryptoKeyTypePublic)
eroman 2013/11/21 00:18:26 I imagine you meant to use != (since as written th
eroman 2013/11/21 01:02:15 FYI: Nico showed me that clang already has a warni
padolph 2013/11/21 01:56:11 Doh! Not sure how that happened.
padolph 2013/11/21 01:56:11 Thanks. I only use clang sometimes. I find it _muc
269 return false; // TODO(padolph): should be DCHECK?
eroman 2013/11/21 00:18:26 Can remove the TODO: should not be a DCHECK, since
270
271 PublicKeyHandle* const public_key =
272 reinterpret_cast<PublicKeyHandle*>(key.handle());
273
274 const unsigned encrypted_length_bytes =
275 SECKEY_PublicKeyStrength(public_key->key());
276
277 // RSAES can operate on messages up to a length of k - 11, where k is the
278 // octet length of the RSA modulus.
279 if (encrypted_length_bytes < data_size + 11)
eroman 2013/11/21 00:18:26 paranoia: what if data_size + 11 overflows? Could
padolph 2013/11/21 01:56:11 Done.
padolph 2013/11/21 01:56:11 Done.
280 return false;
281
282 *buffer = blink::WebArrayBuffer::create(encrypted_length_bytes, 1);
283 unsigned char* const buffer_data =
284 reinterpret_cast<unsigned char*>(buffer->data());
285
286 if (PK11_PubEncryptPKCS1(public_key->key(),
287 buffer_data,
288 const_cast<unsigned char*>(data),
289 data_size,
290 NULL) != SECSuccess) {
291 return false;
292 }
293 return true;
256 } 294 }
257 295
258 return false; 296 return false;
259 } 297 }
260 298
261 bool WebCryptoImpl::DecryptInternal( 299 bool WebCryptoImpl::DecryptInternal(
262 const blink::WebCryptoAlgorithm& algorithm, 300 const blink::WebCryptoAlgorithm& algorithm,
263 const blink::WebCryptoKey& key, 301 const blink::WebCryptoKey& key,
264 const unsigned char* data, 302 const unsigned char* data,
265 unsigned data_size, 303 unsigned data_size,
266 blink::WebArrayBuffer* buffer) { 304 blink::WebArrayBuffer* buffer) {
305
306 DCHECK_EQ(algorithm.id(), key.algorithm().id());
307 DCHECK(key.handle());
308 DCHECK(buffer);
309
267 if (algorithm.id() == blink::WebCryptoAlgorithmIdAesCbc) { 310 if (algorithm.id() == blink::WebCryptoAlgorithmIdAesCbc) {
268 return AesCbcEncryptDecrypt( 311 return AesCbcEncryptDecrypt(
269 CKA_DECRYPT, algorithm, key, data, data_size, buffer); 312 CKA_DECRYPT, algorithm, key, data, data_size, buffer);
313 } else if (algorithm.id() == blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5) {
314
315 // RSAES decryption does not support empty input
316 if (!data_size)
317 return false;
318 DCHECK(data);
319
320 if (!key.type() == blink::WebCryptoKeyTypePrivate)
eroman 2013/11/21 00:18:26 Same comment as above. You surely meant either X !
padolph 2013/11/21 01:56:11 Done.
padolph 2013/11/21 01:56:11 Done.
321 return false; // TODO(padolph): should be DCHECK instead?
322
323 PrivateKeyHandle* const private_key =
324 reinterpret_cast<PrivateKeyHandle*>(key.handle());
325
326 const int modulus_length_bytes =
327 PK11_GetPrivateModulusLen(private_key->key());
328 if (modulus_length_bytes < 0) // TODO(padolph): should be a DCHECK only?
eroman 2013/11/21 00:18:26 Should this check <= 0?
padolph 2013/11/21 01:56:11 NSS returns -1 in case of error. Returning 0 is a
329 return false;
330 DCHECK(modulus_length_bytes);
331 const unsigned& max_output_length_bytes = modulus_length_bytes;
eroman 2013/11/21 00:18:26 Rather than a reference, either make a copy or rem
padolph 2013/11/21 01:56:11 Done.
332
333 *buffer = blink::WebArrayBuffer::create(max_output_length_bytes, 1);
334 unsigned char* const buffer_data =
335 reinterpret_cast<unsigned char*>(buffer->data());
336
337 unsigned output_length_bytes = 0;
338 if (PK11_PrivDecryptPKCS1(private_key->key(),
339 buffer_data,
340 &output_length_bytes,
341 max_output_length_bytes,
342 const_cast<unsigned char*>(data),
343 data_size) != SECSuccess) {
344 return false;
345 }
346 DCHECK_LE(output_length_bytes, max_output_length_bytes);
347 WebCryptoImpl::ShrinkBuffer(buffer, output_length_bytes);
348 return true;
270 } 349 }
271 350
272 return false; 351 return false;
273 } 352 }
274 353
275 bool WebCryptoImpl::DigestInternal( 354 bool WebCryptoImpl::DigestInternal(
276 const blink::WebCryptoAlgorithm& algorithm, 355 const blink::WebCryptoAlgorithm& algorithm,
277 const unsigned char* data, 356 const unsigned char* data,
278 unsigned data_size, 357 unsigned data_size,
279 blink::WebArrayBuffer* buffer) { 358 blink::WebArrayBuffer* buffer) {
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after
642 break; 721 break;
643 } 722 }
644 default: 723 default:
645 return false; 724 return false;
646 } 725 }
647 726
648 return true; 727 return true;
649 } 728 }
650 729
651 } // namespace content 730 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698