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