OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/child/webcrypto/platform_crypto.h" | 5 #include "content/child/webcrypto/platform_crypto.h" |
6 | 6 |
7 #include <cryptohi.h> | 7 #include <cryptohi.h> |
8 #include <pk11pub.h> | 8 #include <pk11pub.h> |
9 #include <secerr.h> | 9 #include <secerr.h> |
10 #include <sechash.h> | 10 #include <sechash.h> |
11 #include <secoid.h> | 11 #include <secoid.h> |
12 | 12 |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/memory/scoped_ptr.h" | |
17 #include "content/child/webcrypto/crypto_data.h" | 18 #include "content/child/webcrypto/crypto_data.h" |
18 #include "content/child/webcrypto/status.h" | 19 #include "content/child/webcrypto/status.h" |
19 #include "content/child/webcrypto/webcrypto_util.h" | 20 #include "content/child/webcrypto/webcrypto_util.h" |
20 #include "crypto/nss_util.h" | 21 #include "crypto/nss_util.h" |
21 #include "crypto/scoped_nss_types.h" | 22 #include "crypto/scoped_nss_types.h" |
22 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | 23 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" |
23 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 24 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
24 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 25 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
25 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | 26 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
26 | 27 |
(...skipping 620 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
647 SECITEM_FreeItem(&out->prime1, PR_FALSE); | 648 SECITEM_FreeItem(&out->prime1, PR_FALSE); |
648 SECITEM_FreeItem(&out->prime2, PR_FALSE); | 649 SECITEM_FreeItem(&out->prime2, PR_FALSE); |
649 SECITEM_FreeItem(&out->exponent1, PR_FALSE); | 650 SECITEM_FreeItem(&out->exponent1, PR_FALSE); |
650 SECITEM_FreeItem(&out->exponent2, PR_FALSE); | 651 SECITEM_FreeItem(&out->exponent2, PR_FALSE); |
651 SECITEM_FreeItem(&out->coefficient, PR_FALSE); | 652 SECITEM_FreeItem(&out->coefficient, PR_FALSE); |
652 } | 653 } |
653 }; | 654 }; |
654 | 655 |
655 } // namespace | 656 } // namespace |
656 | 657 |
658 class DigestorNSS : public blink::WebCryptoDigestor { | |
659 public: | |
660 DigestorNSS(blink::WebCryptoAlgorithmId algorithm_id) | |
eroman
2014/03/25 23:26:23
Mark as explicit
| |
661 : hash_context_(0), algorithm_id_(algorithm_id) {} | |
eroman
2014/03/25 23:26:23
use NULL rather than 0.
jww
2014/03/26 00:42:31
Done.
| |
662 | |
663 virtual ~DigestorNSS() OVERRIDE { | |
664 if (!hash_context_) | |
665 return; | |
666 | |
667 HASH_Destroy(hash_context_); | |
668 hash_context_ = 0; | |
669 } | |
670 | |
671 virtual bool consume(const unsigned char* data, unsigned int size) OVERRIDE { | |
eroman
2014/03/25 23:26:23
In general we omit OVERRIDE for objects coming fr
jww
2014/03/26 00:42:31
Done.
| |
672 return consumeWithStatus(data, size).IsSuccess(); | |
673 } | |
674 | |
675 Status consumeWithStatus(const unsigned char* data, unsigned int size) { | |
676 // Initialize everything if the object hasn't been initialized yet. | |
677 if (!hash_context_) { | |
678 Status error = init(); | |
679 if (!error.IsSuccess()) | |
680 return error; | |
681 } | |
682 | |
683 HASH_Update(hash_context_, data, size); | |
684 | |
685 return Status::Success(); | |
686 } | |
687 | |
688 virtual bool finish(unsigned char*& result_data, | |
689 unsigned int& result_data_size) OVERRIDE { | |
eroman
2014/03/25 23:26:23
ditto here
jww
2014/03/26 00:42:31
Done.
| |
690 Status error = finishInternal(result_, &result_data_size); | |
691 if (!error.IsSuccess()) | |
692 return false; | |
693 result_data = result_; | |
694 return true; | |
695 } | |
696 | |
697 Status finishWithWebArrayAndStatus(blink::WebArrayBuffer& result) { | |
eroman
2014/03/25 23:26:23
FinishWithWebArrayAndStatus. Also make result be a
jww
2014/03/26 00:42:31
This is meant to be an explicit method to get a We
eroman
2014/03/26 00:49:08
What i meant, is in chromium-style an out paramete
eroman
2014/03/26 00:50:13
And then instead of
result = ...
you do
*result =
| |
698 if (!hash_context_) | |
699 return Status::ErrorUnexpected(); | |
700 | |
701 unsigned int result_length = HASH_ResultLenContext(hash_context_); | |
702 result = blink::WebArrayBuffer::create(result_length, 1); | |
703 unsigned char* digest = reinterpret_cast<unsigned char*>(result.data()); | |
704 unsigned int digest_size; // ignored | |
705 return finishInternal(digest, &digest_size); | |
706 } | |
707 | |
708 private: | |
709 HASHContext* hash_context_; | |
710 blink::WebCryptoAlgorithmId algorithm_id_; | |
711 unsigned char result_[HASH_LENGTH_MAX]; | |
712 | |
713 Status init() { | |
eroman
2014/03/25 23:26:23
Init
jww
2014/03/26 00:42:31
Done.
| |
714 HASH_HashType hash_type = WebCryptoAlgorithmToNSSHashType(algorithm_id_); | |
715 | |
716 if (hash_type == HASH_AlgNULL) | |
717 return Status::ErrorUnsupported(); | |
718 | |
719 hash_context_ = HASH_Create(hash_type); | |
720 if (!hash_context_) | |
721 return Status::Error(); | |
722 | |
723 HASH_Begin(hash_context_); | |
724 | |
725 return Status::Success(); | |
726 } | |
727 | |
728 Status finishInternal(unsigned char* result, unsigned int* result_size) { | |
eroman
2014/03/25 23:26:23
FinishInternal
jww
2014/03/26 00:42:31
Done.
| |
729 if (!hash_context_) { | |
730 Status error = init(); | |
731 if (!error.IsSuccess()) | |
732 return error; | |
733 } | |
734 | |
735 unsigned int hash_result_length = HASH_ResultLenContext(hash_context_); | |
736 DCHECK_LE(hash_result_length, static_cast<size_t>(HASH_LENGTH_MAX)); | |
737 | |
738 HASH_End(hash_context_, result, result_size, hash_result_length); | |
739 | |
740 if (*result_size != hash_result_length) | |
741 return Status::ErrorUnexpected(); | |
742 return Status::Success(); | |
743 } | |
744 }; | |
745 | |
657 Status ImportKeyRaw(const blink::WebCryptoAlgorithm& algorithm, | 746 Status ImportKeyRaw(const blink::WebCryptoAlgorithm& algorithm, |
658 const CryptoData& key_data, | 747 const CryptoData& key_data, |
659 bool extractable, | 748 bool extractable, |
660 blink::WebCryptoKeyUsageMask usage_mask, | 749 blink::WebCryptoKeyUsageMask usage_mask, |
661 blink::WebCryptoKey* key) { | 750 blink::WebCryptoKey* key) { |
662 | 751 |
663 DCHECK(!algorithm.isNull()); | 752 DCHECK(!algorithm.isNull()); |
664 | 753 |
665 CK_MECHANISM_TYPE mechanism; | 754 CK_MECHANISM_TYPE mechanism; |
666 CK_FLAGS flags; | 755 CK_FLAGS flags; |
(...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1170 usage_mask); | 1259 usage_mask); |
1171 | 1260 |
1172 return Status::Success(); | 1261 return Status::Success(); |
1173 } | 1262 } |
1174 | 1263 |
1175 void Init() { crypto::EnsureNSSInit(); } | 1264 void Init() { crypto::EnsureNSSInit(); } |
1176 | 1265 |
1177 Status DigestSha(blink::WebCryptoAlgorithmId algorithm, | 1266 Status DigestSha(blink::WebCryptoAlgorithmId algorithm, |
1178 const CryptoData& data, | 1267 const CryptoData& data, |
1179 blink::WebArrayBuffer* buffer) { | 1268 blink::WebArrayBuffer* buffer) { |
1180 HASH_HashType hash_type = WebCryptoAlgorithmToNSSHashType(algorithm); | 1269 DigestorNSS digestor(algorithm); |
1181 if (hash_type == HASH_AlgNULL) | 1270 Status error = digestor.consumeWithStatus(data.bytes(), data.byte_length()); |
1182 return Status::ErrorUnsupported(); | 1271 if (!error.IsSuccess()) |
1272 return error; | |
1273 return digestor.finishWithWebArrayAndStatus(*buffer); | |
1274 } | |
1183 | 1275 |
1184 HASHContext* context = HASH_Create(hash_type); | 1276 blink::WebCryptoDigestor* CreateDigestor( |
1185 if (!context) | 1277 blink::WebCryptoAlgorithmId algorithm_id) { |
1186 return Status::Error(); | 1278 return new DigestorNSS(algorithm_id); |
1187 | |
1188 HASH_Begin(context); | |
1189 | |
1190 HASH_Update(context, data.bytes(), data.byte_length()); | |
1191 | |
1192 unsigned int hash_result_length = HASH_ResultLenContext(context); | |
1193 DCHECK_LE(hash_result_length, static_cast<size_t>(HASH_LENGTH_MAX)); | |
1194 | |
1195 *buffer = blink::WebArrayBuffer::create(hash_result_length, 1); | |
1196 | |
1197 unsigned char* digest = reinterpret_cast<unsigned char*>(buffer->data()); | |
1198 | |
1199 unsigned int result_length = 0; | |
1200 HASH_End(context, digest, &result_length, hash_result_length); | |
1201 | |
1202 HASH_Destroy(context); | |
1203 | |
1204 if (result_length != hash_result_length) | |
1205 return Status::ErrorUnexpected(); | |
1206 return Status::Success(); | |
1207 } | 1279 } |
1208 | 1280 |
1209 Status GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm, | 1281 Status GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm, |
1210 bool extractable, | 1282 bool extractable, |
1211 blink::WebCryptoKeyUsageMask usage_mask, | 1283 blink::WebCryptoKeyUsageMask usage_mask, |
1212 unsigned keylen_bytes, | 1284 unsigned keylen_bytes, |
1213 blink::WebCryptoKey* key) { | 1285 blink::WebCryptoKey* key) { |
1214 CK_MECHANISM_TYPE mech = WebCryptoAlgorithmToGenMechanism(algorithm); | 1286 CK_MECHANISM_TYPE mech = WebCryptoAlgorithmToGenMechanism(algorithm); |
1215 blink::WebCryptoKeyType key_type = blink::WebCryptoKeyTypeSecret; | 1287 blink::WebCryptoKeyType key_type = blink::WebCryptoKeyTypeSecret; |
1216 | 1288 |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1474 key_algorithm, | 1546 key_algorithm, |
1475 usage_mask); | 1547 usage_mask); |
1476 return Status::Success(); | 1548 return Status::Success(); |
1477 } | 1549 } |
1478 | 1550 |
1479 } // namespace platform | 1551 } // namespace platform |
1480 | 1552 |
1481 } // namespace webcrypto | 1553 } // namespace webcrypto |
1482 | 1554 |
1483 } // namespace content | 1555 } // namespace content |
OLD | NEW |