Always set *error when CreateDirectoryAndGetError fails.
[chromium-blink-merge.git] / crypto / signature_creator_openssl.cc
blobfa9ba07c3cf0f9b73fbf6ae6c0d1d9ef7fec0dd0
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
3 // found in the LICENSE file.
5 #include "crypto/signature_creator.h"
7 #include <openssl/evp.h>
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/stl_util.h"
12 #include "crypto/openssl_util.h"
13 #include "crypto/rsa_private_key.h"
15 namespace crypto {
17 // static
18 SignatureCreator* SignatureCreator::Create(RSAPrivateKey* key) {
19 OpenSSLErrStackTracer err_tracer(FROM_HERE);
20 scoped_ptr<SignatureCreator> result(new SignatureCreator);
21 result->key_ = key;
22 if (!EVP_SignInit_ex(result->sign_context_, EVP_sha1(), NULL))
23 return NULL;
24 return result.release();
27 SignatureCreator::SignatureCreator()
28 : sign_context_(EVP_MD_CTX_create()) {
31 SignatureCreator::~SignatureCreator() {
32 EVP_MD_CTX_destroy(sign_context_);
35 bool SignatureCreator::Update(const uint8* data_part, int data_part_len) {
36 OpenSSLErrStackTracer err_tracer(FROM_HERE);
37 return EVP_SignUpdate(sign_context_, data_part, data_part_len) == 1;
40 bool SignatureCreator::Final(std::vector<uint8>* signature) {
41 OpenSSLErrStackTracer err_tracer(FROM_HERE);
42 EVP_PKEY* key = key_->key();
43 signature->resize(EVP_PKEY_size(key));
45 unsigned int len = 0;
46 int rv = EVP_SignFinal(sign_context_, vector_as_array(signature), &len, key);
47 if (!rv) {
48 signature->clear();
49 return false;
51 signature->resize(len);
52 return true;
55 } // namespace crypto