Daniel Erat | 59c5f4b | 2015-08-24 12:50:25 -0600 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "crypto/signature_creator.h" |
| 6 | |
| 7 | #include <openssl/evp.h> |
| 8 | #include <openssl/rsa.h> |
| 9 | |
| 10 | #include "base/logging.h" |
| 11 | #include "base/memory/scoped_ptr.h" |
| 12 | #include "base/stl_util.h" |
| 13 | #include "crypto/openssl_util.h" |
| 14 | #include "crypto/rsa_private_key.h" |
| 15 | #include "crypto/scoped_openssl_types.h" |
| 16 | |
| 17 | namespace crypto { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | const EVP_MD* ToOpenSSLDigest(SignatureCreator::HashAlgorithm hash_alg) { |
| 22 | switch (hash_alg) { |
| 23 | case SignatureCreator::SHA1: |
| 24 | return EVP_sha1(); |
| 25 | case SignatureCreator::SHA256: |
| 26 | return EVP_sha256(); |
| 27 | } |
| 28 | return NULL; |
| 29 | } |
| 30 | |
| 31 | int ToOpenSSLDigestType(SignatureCreator::HashAlgorithm hash_alg) { |
| 32 | switch (hash_alg) { |
| 33 | case SignatureCreator::SHA1: |
| 34 | return NID_sha1; |
| 35 | case SignatureCreator::SHA256: |
| 36 | return NID_sha256; |
| 37 | } |
| 38 | return NID_undef; |
| 39 | } |
| 40 | |
| 41 | } // namespace |
| 42 | |
| 43 | // static |
| 44 | SignatureCreator* SignatureCreator::Create(RSAPrivateKey* key, |
| 45 | HashAlgorithm hash_alg) { |
| 46 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 47 | scoped_ptr<SignatureCreator> result(new SignatureCreator); |
| 48 | const EVP_MD* const digest = ToOpenSSLDigest(hash_alg); |
| 49 | DCHECK(digest); |
| 50 | if (!digest) { |
| 51 | return NULL; |
| 52 | } |
| 53 | if (!EVP_DigestSignInit(result->sign_context_, NULL, digest, NULL, |
| 54 | key->key())) { |
| 55 | return NULL; |
| 56 | } |
| 57 | return result.release(); |
| 58 | } |
| 59 | |
| 60 | // static |
| 61 | bool SignatureCreator::Sign(RSAPrivateKey* key, |
| 62 | HashAlgorithm hash_alg, |
| 63 | const uint8* data, |
| 64 | int data_len, |
| 65 | std::vector<uint8>* signature) { |
| 66 | ScopedRSA rsa_key(EVP_PKEY_get1_RSA(key->key())); |
| 67 | if (!rsa_key) |
| 68 | return false; |
| 69 | signature->resize(RSA_size(rsa_key.get())); |
| 70 | |
| 71 | unsigned int len = 0; |
| 72 | if (!RSA_sign(ToOpenSSLDigestType(hash_alg), data, data_len, |
| 73 | vector_as_array(signature), &len, rsa_key.get())) { |
| 74 | signature->clear(); |
| 75 | return false; |
| 76 | } |
| 77 | signature->resize(len); |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | SignatureCreator::SignatureCreator() |
| 82 | : sign_context_(EVP_MD_CTX_create()) { |
| 83 | } |
| 84 | |
| 85 | SignatureCreator::~SignatureCreator() { |
| 86 | EVP_MD_CTX_destroy(sign_context_); |
| 87 | } |
| 88 | |
| 89 | bool SignatureCreator::Update(const uint8* data_part, int data_part_len) { |
| 90 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 91 | return !!EVP_DigestSignUpdate(sign_context_, data_part, data_part_len); |
| 92 | } |
| 93 | |
| 94 | bool SignatureCreator::Final(std::vector<uint8>* signature) { |
| 95 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 96 | |
| 97 | // Determine the maximum length of the signature. |
| 98 | size_t len = 0; |
| 99 | if (!EVP_DigestSignFinal(sign_context_, NULL, &len)) { |
| 100 | signature->clear(); |
| 101 | return false; |
| 102 | } |
| 103 | signature->resize(len); |
| 104 | |
| 105 | // Sign it. |
| 106 | if (!EVP_DigestSignFinal(sign_context_, vector_as_array(signature), &len)) { |
| 107 | signature->clear(); |
| 108 | return false; |
| 109 | } |
| 110 | signature->resize(len); |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | } // namespace crypto |