[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 5 | #include "crypto/signature_creator.h" |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 6 | |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 7 | #include <openssl/evp.h> |
8 | |||||
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 9 | #include "base/logging.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 10 | #include "base/memory/scoped_ptr.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame^] | 11 | #include "base/stl_util.h" |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 12 | #include "crypto/openssl_util.h" |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 13 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 14 | namespace crypto { |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 15 | |
16 | // static | ||||
17 | SignatureCreator* SignatureCreator::Create(RSAPrivateKey* key) { | ||||
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 18 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
19 | scoped_ptr<SignatureCreator> result(new SignatureCreator); | ||||
20 | result->key_ = key; | ||||
21 | if (!EVP_SignInit_ex(result->sign_context_, EVP_sha1(), NULL)) | ||||
22 | return NULL; | ||||
23 | return result.release(); | ||||
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 24 | } |
25 | |||||
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 26 | SignatureCreator::SignatureCreator() |
27 | : sign_context_(EVP_MD_CTX_create()) { | ||||
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 28 | } |
29 | |||||
30 | SignatureCreator::~SignatureCreator() { | ||||
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 31 | EVP_MD_CTX_destroy(sign_context_); |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 32 | } |
33 | |||||
34 | bool SignatureCreator::Update(const uint8* data_part, int data_part_len) { | ||||
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 35 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
36 | return EVP_SignUpdate(sign_context_, data_part, data_part_len) == 1; | ||||
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 37 | } |
38 | |||||
39 | bool SignatureCreator::Final(std::vector<uint8>* signature) { | ||||
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 40 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
41 | EVP_PKEY* key = key_->key(); | ||||
42 | signature->resize(EVP_PKEY_size(key)); | ||||
43 | |||||
44 | unsigned int len = 0; | ||||
[email protected] | 70be924 | 2010-12-14 13:55:44 | [diff] [blame] | 45 | int rv = EVP_SignFinal(sign_context_, vector_as_array(signature), &len, key); |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 46 | if (!rv) { |
47 | signature->clear(); | ||||
48 | return false; | ||||
49 | } | ||||
50 | signature->resize(len); | ||||
51 | return true; | ||||
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 52 | } |
53 | |||||
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 54 | } // namespace crypto |