blob: fa9ba07c3cf0f9b73fbf6ae6c0d1d9ef7fec0dd0 [file] [log] [blame]
[email protected]5ee44d42012-02-08 00:14:541// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]70372d42010-10-22 13:12:342// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4b559b4d2011-04-14 17:37:145#include "crypto/signature_creator.h"
[email protected]70372d42010-10-22 13:12:346
[email protected]be796bb2010-11-18 15:43:437#include <openssl/evp.h>
8
[email protected]70372d42010-10-22 13:12:349#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:1510#include "base/memory/scoped_ptr.h"
[email protected]7286e3fc2011-07-19 22:13:2411#include "base/stl_util.h"
[email protected]4b559b4d2011-04-14 17:37:1412#include "crypto/openssl_util.h"
[email protected]5ee44d42012-02-08 00:14:5413#include "crypto/rsa_private_key.h"
[email protected]70372d42010-10-22 13:12:3414
[email protected]4b559b4d2011-04-14 17:37:1415namespace crypto {
[email protected]70372d42010-10-22 13:12:3416
17// static
18SignatureCreator* SignatureCreator::Create(RSAPrivateKey* key) {
[email protected]be796bb2010-11-18 15:43:4319 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();
[email protected]70372d42010-10-22 13:12:3425}
26
[email protected]be796bb2010-11-18 15:43:4327SignatureCreator::SignatureCreator()
28 : sign_context_(EVP_MD_CTX_create()) {
[email protected]70372d42010-10-22 13:12:3429}
30
31SignatureCreator::~SignatureCreator() {
[email protected]be796bb2010-11-18 15:43:4332 EVP_MD_CTX_destroy(sign_context_);
[email protected]70372d42010-10-22 13:12:3433}
34
35bool SignatureCreator::Update(const uint8* data_part, int data_part_len) {
[email protected]be796bb2010-11-18 15:43:4336 OpenSSLErrStackTracer err_tracer(FROM_HERE);
37 return EVP_SignUpdate(sign_context_, data_part, data_part_len) == 1;
[email protected]70372d42010-10-22 13:12:3438}
39
40bool SignatureCreator::Final(std::vector<uint8>* signature) {
[email protected]be796bb2010-11-18 15:43:4341 OpenSSLErrStackTracer err_tracer(FROM_HERE);
42 EVP_PKEY* key = key_->key();
43 signature->resize(EVP_PKEY_size(key));
44
45 unsigned int len = 0;
[email protected]70be9242010-12-14 13:55:4446 int rv = EVP_SignFinal(sign_context_, vector_as_array(signature), &len, key);
[email protected]be796bb2010-11-18 15:43:4347 if (!rv) {
48 signature->clear();
49 return false;
50 }
51 signature->resize(len);
52 return true;
[email protected]70372d42010-10-22 13:12:3453}
54
[email protected]4b559b4d2011-04-14 17:37:1455} // namespace crypto