[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] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 12 | #include "crypto/openssl_util.h" |
[email protected] | 5ee44d4 | 2012-02-08 00:14:54 | [diff] [blame] | 13 | #include "crypto/rsa_private_key.h" |
[email protected] | 2741040 | 2014-07-14 21:01:52 | [diff] [blame] | 14 | #include "crypto/scoped_openssl_types.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 | |
dougsteed | 0cf460ec | 2014-09-19 18:46:09 | [diff] [blame] | 18 | namespace { |
| 19 | |
| 20 | const EVP_MD* ToOpenSSLDigest(SignatureCreator::HashAlgorithm hash_alg) { |
| 21 | switch (hash_alg) { |
| 22 | case SignatureCreator::SHA1: |
| 23 | return EVP_sha1(); |
| 24 | case SignatureCreator::SHA256: |
| 25 | return EVP_sha256(); |
| 26 | } |
| 27 | return NULL; |
| 28 | } |
| 29 | |
| 30 | int ToOpenSSLDigestType(SignatureCreator::HashAlgorithm hash_alg) { |
| 31 | switch (hash_alg) { |
| 32 | case SignatureCreator::SHA1: |
| 33 | return NID_sha1; |
| 34 | case SignatureCreator::SHA256: |
| 35 | return NID_sha256; |
| 36 | } |
| 37 | return NID_undef; |
| 38 | } |
| 39 | |
| 40 | } // namespace |
| 41 | |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 42 | // static |
dougsteed | 0cf460ec | 2014-09-19 18:46:09 | [diff] [blame] | 43 | SignatureCreator* SignatureCreator::Create(RSAPrivateKey* key, |
| 44 | HashAlgorithm hash_alg) { |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 45 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 46 | scoped_ptr<SignatureCreator> result(new SignatureCreator); |
dougsteed | 0cf460ec | 2014-09-19 18:46:09 | [diff] [blame] | 47 | const EVP_MD* const digest = ToOpenSSLDigest(hash_alg); |
| 48 | DCHECK(digest); |
| 49 | if (!digest) { |
| 50 | return NULL; |
| 51 | } |
davidben | 183ce63 | 2015-01-21 14:21:36 | [diff] [blame] | 52 | if (!EVP_DigestSignInit(result->sign_context_, NULL, digest, NULL, |
| 53 | key->key())) { |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 54 | return NULL; |
davidben | 183ce63 | 2015-01-21 14:21:36 | [diff] [blame] | 55 | } |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 56 | return result.release(); |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 57 | } |
| 58 | |
[email protected] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame] | 59 | // static |
| 60 | bool SignatureCreator::Sign(RSAPrivateKey* key, |
dougsteed | 0cf460ec | 2014-09-19 18:46:09 | [diff] [blame] | 61 | HashAlgorithm hash_alg, |
[email protected] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame] | 62 | const uint8* data, |
| 63 | int data_len, |
| 64 | std::vector<uint8>* signature) { |
[email protected] | 2741040 | 2014-07-14 21:01:52 | [diff] [blame] | 65 | ScopedRSA rsa_key(EVP_PKEY_get1_RSA(key->key())); |
[email protected] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame] | 66 | if (!rsa_key) |
| 67 | return false; |
[email protected] | 2741040 | 2014-07-14 21:01:52 | [diff] [blame] | 68 | signature->resize(RSA_size(rsa_key.get())); |
[email protected] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame] | 69 | |
| 70 | unsigned int len = 0; |
davidben | 50a133b5 | 2014-10-02 02:20:43 | [diff] [blame] | 71 | if (!RSA_sign(ToOpenSSLDigestType(hash_alg), data, data_len, |
davidben | 4507eaa | 2015-11-19 19:07:06 | [diff] [blame^] | 72 | signature->data(), &len, rsa_key.get())) { |
[email protected] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame] | 73 | signature->clear(); |
| 74 | return false; |
| 75 | } |
| 76 | signature->resize(len); |
| 77 | return true; |
| 78 | } |
| 79 | |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 80 | SignatureCreator::SignatureCreator() |
| 81 | : sign_context_(EVP_MD_CTX_create()) { |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | SignatureCreator::~SignatureCreator() { |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 85 | EVP_MD_CTX_destroy(sign_context_); |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | bool SignatureCreator::Update(const uint8* data_part, int data_part_len) { |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 89 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
davidben | 183ce63 | 2015-01-21 14:21:36 | [diff] [blame] | 90 | return !!EVP_DigestSignUpdate(sign_context_, data_part, data_part_len); |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | bool SignatureCreator::Final(std::vector<uint8>* signature) { |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 94 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 95 | |
davidben | 183ce63 | 2015-01-21 14:21:36 | [diff] [blame] | 96 | // Determine the maximum length of the signature. |
| 97 | size_t len = 0; |
| 98 | if (!EVP_DigestSignFinal(sign_context_, NULL, &len)) { |
| 99 | signature->clear(); |
| 100 | return false; |
| 101 | } |
| 102 | signature->resize(len); |
| 103 | |
| 104 | // Sign it. |
davidben | 4507eaa | 2015-11-19 19:07:06 | [diff] [blame^] | 105 | if (!EVP_DigestSignFinal(sign_context_, signature->data(), &len)) { |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 106 | signature->clear(); |
| 107 | return false; |
| 108 | } |
| 109 | signature->resize(len); |
| 110 | return true; |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 111 | } |
| 112 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 113 | } // namespace crypto |