Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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 #include "content/renderer/webcrypto_sha_digest.h" | |
| 6 | |
| 7 #include <pk11pub.h> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "crypto/nss_util.h" | |
| 11 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | |
| 12 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | |
| 13 | |
| 14 namespace content { | |
| 15 | |
|
Ryan Sleevi
2013/07/31 18:19:19
I'm a bit mixed on using the PK11 functions direct
Bryan Eyler
2013/08/02 00:49:05
I didn't know about this interface. Looks a bit c
| |
| 16 WebCryptoSHADigest::WebCryptoSHADigest( | |
| 17 const WebKit::WebCryptoAlgorithmId algorithm_id, | |
| 18 WebKit::WebCryptoOperationResult& result) | |
| 19 : result_(result), | |
| 20 context_(NULL), | |
| 21 hash_algorithm_(SEC_OID_UNKNOWN), | |
| 22 hash_result_length_(0) { | |
| 23 switch (algorithm_id) { | |
| 24 case WebKit::WebCryptoAlgorithmIdSha1: | |
| 25 hash_algorithm_ = SEC_OID_SHA1; | |
| 26 hash_result_length_ = 20; | |
|
Ryan Sleevi
2013/07/31 18:19:19
If you keep with the OID formation, all of these c
Bryan Eyler
2013/08/02 00:49:05
Done.
| |
| 27 break; | |
| 28 case WebKit::WebCryptoAlgorithmIdSha224: | |
| 29 hash_algorithm_ = SEC_OID_SHA224; | |
| 30 hash_result_length_ = 28; | |
| 31 break; | |
| 32 case WebKit::WebCryptoAlgorithmIdSha256: | |
| 33 hash_algorithm_ = SEC_OID_SHA256; | |
| 34 hash_result_length_ = 32; | |
| 35 break; | |
| 36 case WebKit::WebCryptoAlgorithmIdSha384: | |
| 37 hash_algorithm_ = SEC_OID_SHA384; | |
| 38 hash_result_length_ = 48; | |
| 39 break; | |
| 40 case WebKit::WebCryptoAlgorithmIdSha512: | |
| 41 hash_algorithm_ = SEC_OID_SHA512; | |
| 42 hash_result_length_ = 64; | |
| 43 break; | |
| 44 default: | |
| 45 NOTREACHED(); | |
| 46 hash_algorithm_ = SEC_OID_UNKNOWN; | |
| 47 hash_result_length_ = 0; | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 WebCryptoSHADigest::~WebCryptoSHADigest() { | |
| 52 if (context_) { | |
| 53 PK11_DestroyContext(context_, PR_TRUE); | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 bool WebCryptoSHADigest::Initialize() { | |
| 58 crypto::EnsureNSSInit(); | |
| 59 | |
| 60 context_ = PK11_CreateDigestContext(hash_algorithm_); | |
| 61 if (!context_) { | |
| 62 LOG(ERROR) << "Could not create digest context for hash algorithm: " | |
| 63 << hash_algorithm_; | |
|
Ryan Sleevi
2013/07/31 18:19:19
Seems that this is unused beyond the constructor -
Bryan Eyler
2013/08/02 00:49:05
Make sense. I want to keep the creation of the co
| |
| 64 return false; | |
| 65 } | |
| 66 | |
| 67 if (PK11_DigestBegin(context_) != SECSuccess) { | |
| 68 LOG(ERROR) << "Could not initialize digest context."; | |
| 69 return false; | |
| 70 } | |
| 71 | |
| 72 return true; | |
| 73 } | |
| 74 | |
| 75 void WebCryptoSHADigest::process(const unsigned char* bytes, size_t size) { | |
| 76 DCHECK(context_); | |
| 77 | |
| 78 if (PK11_DigestOp(context_, bytes, size) != SECSuccess) { | |
|
eroman
2013/07/31 02:23:50
Is it safe to call this when |bytes| is garbage, b
Ryan Sleevi
2013/07/31 18:19:19
It should be.
Bryan Eyler
2013/08/02 00:49:05
Test added for this.
| |
| 79 LOG(ERROR) << "Could not process digest contents of size: " << size; | |
| 80 result_.completeWithError(); | |
|
eroman
2013/07/31 02:23:50
delete this;
(Any time completing the operation,
Bryan Eyler
2013/08/02 00:49:05
Obsolete now.
| |
| 81 } | |
| 82 } | |
| 83 | |
| 84 void WebCryptoSHADigest::abort() { | |
| 85 delete this; | |
| 86 } | |
| 87 | |
| 88 void WebCryptoSHADigest::finish() { | |
| 89 DCHECK(context_); | |
| 90 | |
| 91 unsigned char* digest = NULL; | |
|
Ryan Sleevi
2013/07/31 18:19:19
Move this to line 96 (keep declaration and definit
Bryan Eyler
2013/08/02 00:49:05
Done.
| |
| 92 | |
| 93 WebKit::WebArrayBuffer buffer( | |
| 94 WebKit::WebArrayBuffer::create(hash_result_length_, 1)); | |
|
Ryan Sleevi
2013/07/31 18:19:19
Same here - hash_result_length_ is unused outside
eroman
2013/07/31 21:10:35
This was based on my recommendation, so that he ha
| |
| 95 | |
| 96 digest = reinterpret_cast<unsigned char*>(buffer.data()); | |
| 97 DCHECK(digest); | |
| 98 | |
| 99 unsigned int result_length = 0; | |
| 100 if (PK11_DigestFinal(context_, digest, &result_length, hash_result_length_) | |
| 101 != SECSuccess || result_length != hash_result_length_) { | |
|
Ryan Sleevi
2013/07/31 18:19:19
Seems like this should match Chromium style (the !
Bryan Eyler
2013/08/02 00:49:05
Obsolete after moving to new interface. Will look
| |
| 102 LOG(ERROR) << "Could not finalize digest data."; | |
| 103 result_.completeWithError(); | |
|
eroman
2013/07/31 02:23:50
delete this;
Bryan Eyler
2013/08/02 00:49:05
Done.
| |
| 104 return; | |
| 105 } | |
| 106 | |
| 107 result_.completeWithArrayBuffer(buffer); | |
| 108 | |
| 109 delete this; | |
| 110 } | |
| 111 | |
| 112 } // namespace content | |
| OLD | NEW |