OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "crypto/signature_creator.h" | 5 #include "crypto/signature_creator.h" |
6 | 6 |
7 #include <openssl/evp.h> | 7 #include <openssl/evp.h> |
8 #include <openssl/rsa.h> | 8 #include <openssl/rsa.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
13 #include "crypto/openssl_util.h" | 13 #include "crypto/openssl_util.h" |
14 #include "crypto/rsa_private_key.h" | 14 #include "crypto/rsa_private_key.h" |
15 #include "crypto/scoped_openssl_types.h" | 15 #include "crypto/scoped_openssl_types.h" |
16 | 16 |
17 namespace crypto { | 17 namespace crypto { |
18 | 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 |
19 // static | 43 // static |
20 SignatureCreator* SignatureCreator::Create(RSAPrivateKey* key) { | 44 SignatureCreator* SignatureCreator::Create(RSAPrivateKey* key) { |
| 45 return CreateUsingSpecifiedHash(key, SignatureCreator::SHA1); |
| 46 } |
| 47 |
| 48 // static |
| 49 SignatureCreator* SignatureCreator::CreateUsingSpecifiedHash( |
| 50 RSAPrivateKey* key, HashAlgorithm hash_alg) { |
21 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 51 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
22 scoped_ptr<SignatureCreator> result(new SignatureCreator); | 52 scoped_ptr<SignatureCreator> result(new SignatureCreator); |
23 result->key_ = key; | 53 result->key_ = key; |
24 if (!EVP_SignInit_ex(result->sign_context_, EVP_sha1(), NULL)) | 54 const EVP_MD* const digest = ToOpenSSLDigest(hash_alg); |
| 55 DCHECK(digest); |
| 56 if (!digest) { |
| 57 return NULL; |
| 58 } |
| 59 if (!EVP_SignInit_ex(result->sign_context_, digest, NULL)) |
25 return NULL; | 60 return NULL; |
26 return result.release(); | 61 return result.release(); |
27 } | 62 } |
28 | 63 |
29 // static | 64 // static |
30 bool SignatureCreator::Sign(RSAPrivateKey* key, | 65 bool SignatureCreator::Sign(RSAPrivateKey* key, |
31 const uint8* data, | 66 const uint8* data, |
32 int data_len, | 67 int data_len, |
33 std::vector<uint8>* signature) { | 68 std::vector<uint8>* signature) { |
34 ScopedRSA rsa_key(EVP_PKEY_get1_RSA(key->key())); | 69 return SignUsingSpecifiedHash( |
| 70 key, SignatureCreator::SHA1, data, data_len, signature); |
| 71 } |
| 72 |
| 73 // static |
| 74 bool SignatureCreator::SignUsingSpecifiedHash(RSAPrivateKey* key, |
| 75 HashAlgorithm hash_alg, |
| 76 const uint8* data, |
| 77 int data_len, |
| 78 std::vector<uint8>* signature) { |
| 79 ScopedRSA rsa_key (EVP_PKEY_get1_RSA(key->key())); |
35 if (!rsa_key) | 80 if (!rsa_key) |
36 return false; | 81 return false; |
37 signature->resize(RSA_size(rsa_key.get())); | 82 signature->resize(RSA_size(rsa_key.get())); |
38 | 83 |
39 unsigned int len = 0; | 84 unsigned int len = 0; |
40 bool success = RSA_sign(NID_sha1, data, data_len, vector_as_array(signature), | 85 bool success = RSA_sign(ToOpenSSLDigestType(hash_alg), data, data_len, |
41 &len, rsa_key.get()); | 86 vector_as_array(signature), &len, rsa_key.get()); |
42 if (!success) { | 87 if (!success) { |
43 signature->clear(); | 88 signature->clear(); |
44 return false; | 89 return false; |
45 } | 90 } |
46 signature->resize(len); | 91 signature->resize(len); |
47 return true; | 92 return true; |
48 } | 93 } |
49 | 94 |
50 SignatureCreator::SignatureCreator() | 95 SignatureCreator::SignatureCreator() |
51 : sign_context_(EVP_MD_CTX_create()) { | 96 : sign_context_(EVP_MD_CTX_create()) { |
(...skipping 17 matching lines...) Expand all Loading... |
69 int rv = EVP_SignFinal(sign_context_, vector_as_array(signature), &len, key); | 114 int rv = EVP_SignFinal(sign_context_, vector_as_array(signature), &len, key); |
70 if (!rv) { | 115 if (!rv) { |
71 signature->clear(); | 116 signature->clear(); |
72 return false; | 117 return false; |
73 } | 118 } |
74 signature->resize(len); | 119 signature->resize(len); |
75 return true; | 120 return true; |
76 } | 121 } |
77 | 122 |
78 } // namespace crypto | 123 } // namespace crypto |
OLD | NEW |