blob: 3c8f532cbebc2a31aaf26989fa9b4c622ae337fa [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>
[email protected]ed31834b2013-07-09 08:32:408#include <openssl/rsa.h>
[email protected]be796bb2010-11-18 15:43:439
[email protected]70372d42010-10-22 13:12:3410#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:1511#include "base/memory/scoped_ptr.h"
[email protected]7286e3fc2011-07-19 22:13:2412#include "base/stl_util.h"
[email protected]4b559b4d2011-04-14 17:37:1413#include "crypto/openssl_util.h"
[email protected]5ee44d42012-02-08 00:14:5414#include "crypto/rsa_private_key.h"
[email protected]27410402014-07-14 21:01:5215#include "crypto/scoped_openssl_types.h"
[email protected]70372d42010-10-22 13:12:3416
[email protected]4b559b4d2011-04-14 17:37:1417namespace crypto {
[email protected]70372d42010-10-22 13:12:3418
19// static
20SignatureCreator* SignatureCreator::Create(RSAPrivateKey* key) {
[email protected]be796bb2010-11-18 15:43:4321 OpenSSLErrStackTracer err_tracer(FROM_HERE);
22 scoped_ptr<SignatureCreator> result(new SignatureCreator);
23 result->key_ = key;
24 if (!EVP_SignInit_ex(result->sign_context_, EVP_sha1(), NULL))
25 return NULL;
26 return result.release();
[email protected]70372d42010-10-22 13:12:3427}
28
[email protected]ed31834b2013-07-09 08:32:4029// static
30bool SignatureCreator::Sign(RSAPrivateKey* key,
31 const uint8* data,
32 int data_len,
33 std::vector<uint8>* signature) {
[email protected]27410402014-07-14 21:01:5234 ScopedRSA rsa_key(EVP_PKEY_get1_RSA(key->key()));
[email protected]ed31834b2013-07-09 08:32:4035 if (!rsa_key)
36 return false;
[email protected]27410402014-07-14 21:01:5237 signature->resize(RSA_size(rsa_key.get()));
[email protected]ed31834b2013-07-09 08:32:4038
39 unsigned int len = 0;
40 bool success = RSA_sign(NID_sha1, data, data_len, vector_as_array(signature),
[email protected]27410402014-07-14 21:01:5241 &len, rsa_key.get());
[email protected]ed31834b2013-07-09 08:32:4042 if (!success) {
43 signature->clear();
44 return false;
45 }
46 signature->resize(len);
47 return true;
48}
49
[email protected]be796bb2010-11-18 15:43:4350SignatureCreator::SignatureCreator()
51 : sign_context_(EVP_MD_CTX_create()) {
[email protected]70372d42010-10-22 13:12:3452}
53
54SignatureCreator::~SignatureCreator() {
[email protected]be796bb2010-11-18 15:43:4355 EVP_MD_CTX_destroy(sign_context_);
[email protected]70372d42010-10-22 13:12:3456}
57
58bool SignatureCreator::Update(const uint8* data_part, int data_part_len) {
[email protected]be796bb2010-11-18 15:43:4359 OpenSSLErrStackTracer err_tracer(FROM_HERE);
60 return EVP_SignUpdate(sign_context_, data_part, data_part_len) == 1;
[email protected]70372d42010-10-22 13:12:3461}
62
63bool SignatureCreator::Final(std::vector<uint8>* signature) {
[email protected]be796bb2010-11-18 15:43:4364 OpenSSLErrStackTracer err_tracer(FROM_HERE);
65 EVP_PKEY* key = key_->key();
66 signature->resize(EVP_PKEY_size(key));
67
68 unsigned int len = 0;
[email protected]70be9242010-12-14 13:55:4469 int rv = EVP_SignFinal(sign_context_, vector_as_array(signature), &len, key);
[email protected]be796bb2010-11-18 15:43:4370 if (!rv) {
71 signature->clear();
72 return false;
73 }
74 signature->resize(len);
75 return true;
[email protected]70372d42010-10-22 13:12:3476}
77
[email protected]4b559b4d2011-04-14 17:37:1478} // namespace crypto