[email protected] | 5ee44d4 | 2012-02-08 00:14:54 | [diff] [blame] | 1 | // Copyright (c) 2012 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> |
[email protected] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame^] | 8 | #include <openssl/rsa.h> |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 9 | |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 10 | #include "base/logging.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 11 | #include "base/memory/scoped_ptr.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 12 | #include "base/stl_util.h" |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 13 | #include "crypto/openssl_util.h" |
[email protected] | 5ee44d4 | 2012-02-08 00:14:54 | [diff] [blame] | 14 | #include "crypto/rsa_private_key.h" |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 15 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 16 | namespace crypto { |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 17 | |
| 18 | // static |
| 19 | SignatureCreator* SignatureCreator::Create(RSAPrivateKey* key) { |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 20 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 21 | scoped_ptr<SignatureCreator> result(new SignatureCreator); |
| 22 | result->key_ = key; |
| 23 | if (!EVP_SignInit_ex(result->sign_context_, EVP_sha1(), NULL)) |
| 24 | return NULL; |
| 25 | return result.release(); |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 26 | } |
| 27 | |
[email protected] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame^] | 28 | // static |
| 29 | bool SignatureCreator::Sign(RSAPrivateKey* key, |
| 30 | const uint8* data, |
| 31 | int data_len, |
| 32 | std::vector<uint8>* signature) { |
| 33 | RSA* rsa_key = EVP_PKEY_get1_RSA(key->key()); |
| 34 | if (!rsa_key) |
| 35 | return false; |
| 36 | signature->resize(RSA_size(rsa_key)); |
| 37 | |
| 38 | unsigned int len = 0; |
| 39 | bool success = RSA_sign(NID_sha1, data, data_len, vector_as_array(signature), |
| 40 | &len, rsa_key); |
| 41 | if (!success) { |
| 42 | signature->clear(); |
| 43 | return false; |
| 44 | } |
| 45 | signature->resize(len); |
| 46 | return true; |
| 47 | } |
| 48 | |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 49 | SignatureCreator::SignatureCreator() |
| 50 | : sign_context_(EVP_MD_CTX_create()) { |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | SignatureCreator::~SignatureCreator() { |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 54 | EVP_MD_CTX_destroy(sign_context_); |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | bool SignatureCreator::Update(const uint8* data_part, int data_part_len) { |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 58 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 59 | return EVP_SignUpdate(sign_context_, data_part, data_part_len) == 1; |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | bool SignatureCreator::Final(std::vector<uint8>* signature) { |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 63 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 64 | EVP_PKEY* key = key_->key(); |
| 65 | signature->resize(EVP_PKEY_size(key)); |
| 66 | |
| 67 | unsigned int len = 0; |
[email protected] | 70be924 | 2010-12-14 13:55:44 | [diff] [blame] | 68 | int rv = EVP_SignFinal(sign_context_, vector_as_array(signature), &len, key); |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 69 | if (!rv) { |
| 70 | signature->clear(); |
| 71 | return false; |
| 72 | } |
| 73 | signature->resize(len); |
| 74 | return true; |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 75 | } |
| 76 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 77 | } // namespace crypto |