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