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

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

Issue 25906002: [webcrypto] Add JWK import for HMAC and AES-CBC key. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixes for eroman, plus additional validation of AES key size 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 <vector> 7 #include <vector>
8 #include <openssl/aes.h> 8 #include <openssl/aes.h>
9 #include <openssl/evp.h> 9 #include <openssl/evp.h>
10 #include <openssl/hmac.h> 10 #include <openssl/hmac.h>
11 #include <openssl/sha.h> 11 #include <openssl/sha.h>
12 #include <openssl/evp.h> 12 #include <openssl/evp.h>
13 #include <openssl/rand.h> 13 #include <openssl/rand.h>
14 14
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "content/renderer/webcrypto/webcrypto_util.h"
16 #include "crypto/openssl_util.h" 17 #include "crypto/openssl_util.h"
17 #include "crypto/secure_util.h" 18 #include "crypto/secure_util.h"
18 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" 19 #include "third_party/WebKit/public/platform/WebArrayBuffer.h"
19 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" 20 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
20 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" 21 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
21 22
22 namespace content { 23 namespace content {
23 24
24 namespace { 25 namespace {
25 26
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 int final_output_chunk_len = 0; 145 int final_output_chunk_len = 0;
145 if (!EVP_CipherFinal_ex( 146 if (!EVP_CipherFinal_ex(
146 context.get(), buffer_data + output_len, &final_output_chunk_len)) 147 context.get(), buffer_data + output_len, &final_output_chunk_len))
147 return false; 148 return false;
148 149
149 const unsigned final_output_len = 150 const unsigned final_output_len =
150 static_cast<unsigned>(output_len) + 151 static_cast<unsigned>(output_len) +
151 static_cast<unsigned>(final_output_chunk_len); 152 static_cast<unsigned>(final_output_chunk_len);
152 DCHECK_LE(final_output_len, output_max_len); 153 DCHECK_LE(final_output_len, output_max_len);
153 154
154 WebCryptoImpl::ShrinkBuffer(buffer, final_output_len); 155 ShrinkBuffer(buffer, final_output_len);
155 156
156 return true; 157 return true;
157 } 158 }
158 159
159 } // namespace 160 } // namespace
160 161
161 void WebCryptoImpl::Init() { crypto::EnsureOpenSSLInit(); } 162 void WebCryptoImpl::Init() { crypto::EnsureOpenSSLInit(); }
162 163
163 bool WebCryptoImpl::EncryptInternal(const WebKit::WebCryptoAlgorithm& algorithm, 164 bool WebCryptoImpl::EncryptInternal(const WebKit::WebCryptoAlgorithm& algorithm,
164 const WebKit::WebCryptoKey& key, 165 const WebKit::WebCryptoKey& key,
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 if (algorithm_or_null.isNull()) 324 if (algorithm_or_null.isNull())
324 return false; 325 return false;
325 const WebKit::WebCryptoAlgorithm& algorithm = algorithm_or_null; 326 const WebKit::WebCryptoAlgorithm& algorithm = algorithm_or_null;
326 327
327 // TODO(padolph): Support all relevant alg types and then remove this gate. 328 // TODO(padolph): Support all relevant alg types and then remove this gate.
328 if (algorithm.id() != WebKit::WebCryptoAlgorithmIdHmac && 329 if (algorithm.id() != WebKit::WebCryptoAlgorithmIdHmac &&
329 algorithm.id() != WebKit::WebCryptoAlgorithmIdAesCbc) { 330 algorithm.id() != WebKit::WebCryptoAlgorithmIdAesCbc) {
330 return false; 331 return false;
331 } 332 }
332 333
333 // TODO(padolph): Need to split handling for symmetric (raw or jwk format) and 334 // TODO(padolph): Need to split handling for symmetric (raw format) and
334 // asymmetric (jwk, spki, or pkcs8 format) keys. 335 // asymmetric (spki or pkcs8 format) keys.
335 // Currently only supporting symmetric. 336 // Currently only supporting symmetric.
336 337
337 // TODO(padolph): jwk handling. Define precedence between jwk contents and
338 // this method's parameters, e.g. 'alg' in jwk vs algorithm.id(). Who wins if
339 // they differ? (jwk, probably)
340
341 // Symmetric keys are always type secret 338 // Symmetric keys are always type secret
342 WebKit::WebCryptoKeyType type = WebKit::WebCryptoKeyTypeSecret; 339 WebKit::WebCryptoKeyType type = WebKit::WebCryptoKeyTypeSecret;
343 340
344 const unsigned char* raw_key_data; 341 const unsigned char* raw_key_data;
345 unsigned raw_key_data_size; 342 unsigned raw_key_data_size;
346 switch (format) { 343 switch (format) {
347 case WebKit::WebCryptoKeyFormatRaw: 344 case WebKit::WebCryptoKeyFormatRaw:
348 raw_key_data = key_data; 345 raw_key_data = key_data;
349 raw_key_data_size = key_data_size; 346 raw_key_data_size = key_data_size;
350 // The NSS implementation fails when importing a raw AES key with a length 347 // The NSS implementation fails when importing a raw AES key with a length
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
482 479
483 break; 480 break;
484 } 481 }
485 default: 482 default:
486 return false; 483 return false;
487 } 484 }
488 return true; 485 return true;
489 } 486 }
490 487
491 } // namespace content 488 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698