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

Side by Side Diff: crypto/signature_creator_openssl.cc

Issue 382633009: Use EVP_DigestSign* rather than EVP_Sign* in SignatureCreator. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: return values are saner Created 5 years, 11 months 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 | Annotate | Revision Log
« no previous file with comments | « crypto/signature_creator_nss.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "crypto/signature_creator.h" 5 #include "crypto/signature_creator.h"
6 6
7 #include <openssl/evp.h> 7 #include <openssl/evp.h>
8 #include <openssl/rsa.h> 8 #include <openssl/rsa.h>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 27 matching lines...) Expand all
38 return NID_undef; 38 return NID_undef;
39 } 39 }
40 40
41 } // namespace 41 } // namespace
42 42
43 // static 43 // static
44 SignatureCreator* SignatureCreator::Create(RSAPrivateKey* key, 44 SignatureCreator* SignatureCreator::Create(RSAPrivateKey* key,
45 HashAlgorithm hash_alg) { 45 HashAlgorithm hash_alg) {
46 OpenSSLErrStackTracer err_tracer(FROM_HERE); 46 OpenSSLErrStackTracer err_tracer(FROM_HERE);
47 scoped_ptr<SignatureCreator> result(new SignatureCreator); 47 scoped_ptr<SignatureCreator> result(new SignatureCreator);
48 result->key_ = key;
49 const EVP_MD* const digest = ToOpenSSLDigest(hash_alg); 48 const EVP_MD* const digest = ToOpenSSLDigest(hash_alg);
50 DCHECK(digest); 49 DCHECK(digest);
51 if (!digest) { 50 if (!digest) {
52 return NULL; 51 return NULL;
53 } 52 }
54 if (!EVP_SignInit_ex(result->sign_context_, digest, NULL)) 53 if (!EVP_DigestSignInit(result->sign_context_, NULL, digest, NULL,
54 key->key())) {
55 return NULL; 55 return NULL;
56 }
56 return result.release(); 57 return result.release();
57 } 58 }
58 59
59 // static 60 // static
60 bool SignatureCreator::Sign(RSAPrivateKey* key, 61 bool SignatureCreator::Sign(RSAPrivateKey* key,
61 HashAlgorithm hash_alg, 62 HashAlgorithm hash_alg,
62 const uint8* data, 63 const uint8* data,
63 int data_len, 64 int data_len,
64 std::vector<uint8>* signature) { 65 std::vector<uint8>* signature) {
65 ScopedRSA rsa_key(EVP_PKEY_get1_RSA(key->key())); 66 ScopedRSA rsa_key(EVP_PKEY_get1_RSA(key->key()));
(...skipping 14 matching lines...) Expand all
80 SignatureCreator::SignatureCreator() 81 SignatureCreator::SignatureCreator()
81 : sign_context_(EVP_MD_CTX_create()) { 82 : sign_context_(EVP_MD_CTX_create()) {
82 } 83 }
83 84
84 SignatureCreator::~SignatureCreator() { 85 SignatureCreator::~SignatureCreator() {
85 EVP_MD_CTX_destroy(sign_context_); 86 EVP_MD_CTX_destroy(sign_context_);
86 } 87 }
87 88
88 bool SignatureCreator::Update(const uint8* data_part, int data_part_len) { 89 bool SignatureCreator::Update(const uint8* data_part, int data_part_len) {
89 OpenSSLErrStackTracer err_tracer(FROM_HERE); 90 OpenSSLErrStackTracer err_tracer(FROM_HERE);
90 return EVP_SignUpdate(sign_context_, data_part, data_part_len) == 1; 91 return !!EVP_DigestSignUpdate(sign_context_, data_part, data_part_len);
91 } 92 }
92 93
93 bool SignatureCreator::Final(std::vector<uint8>* signature) { 94 bool SignatureCreator::Final(std::vector<uint8>* signature) {
94 OpenSSLErrStackTracer err_tracer(FROM_HERE); 95 OpenSSLErrStackTracer err_tracer(FROM_HERE);
95 EVP_PKEY* key = key_->key();
96 signature->resize(EVP_PKEY_size(key));
97 96
98 unsigned int len = 0; 97 // Determine the maximum length of the signature.
99 int rv = EVP_SignFinal(sign_context_, vector_as_array(signature), &len, key); 98 size_t len = 0;
100 if (!rv) { 99 if (!EVP_DigestSignFinal(sign_context_, NULL, &len)) {
101 signature->clear(); 100 signature->clear();
102 return false; 101 return false;
103 } 102 }
103 signature->resize(len);
104
105 // Sign it.
106 if (!EVP_DigestSignFinal(sign_context_, vector_as_array(signature), &len)) {
107 signature->clear();
108 return false;
109 }
104 signature->resize(len); 110 signature->resize(len);
105 return true; 111 return true;
106 } 112 }
107 113
108 } // namespace crypto 114 } // namespace crypto
OLDNEW
« no previous file with comments | « crypto/signature_creator_nss.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698