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 | 16 |
16 namespace crypto { | 17 namespace crypto { |
17 | 18 |
18 // static | 19 // static |
19 SignatureCreator* SignatureCreator::Create(RSAPrivateKey* key) { | 20 SignatureCreator* SignatureCreator::Create(RSAPrivateKey* key) { |
20 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 21 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
21 scoped_ptr<SignatureCreator> result(new SignatureCreator); | 22 scoped_ptr<SignatureCreator> result(new SignatureCreator); |
22 result->key_ = key; | 23 result->key_ = key; |
23 if (!EVP_SignInit_ex(result->sign_context_, EVP_sha1(), NULL)) | 24 if (!EVP_SignInit_ex(result->sign_context_, EVP_sha1(), NULL)) |
24 return NULL; | 25 return NULL; |
25 return result.release(); | 26 return result.release(); |
26 } | 27 } |
27 | 28 |
28 // static | 29 // static |
29 bool SignatureCreator::Sign(RSAPrivateKey* key, | 30 bool SignatureCreator::Sign(RSAPrivateKey* key, |
30 const uint8* data, | 31 const uint8* data, |
31 int data_len, | 32 int data_len, |
32 std::vector<uint8>* signature) { | 33 std::vector<uint8>* signature) { |
33 RSA* rsa_key = EVP_PKEY_get1_RSA(key->key()); | 34 ScopedRSA rsa_key(EVP_PKEY_get1_RSA(key->key())); |
34 if (!rsa_key) | 35 if (!rsa_key) |
35 return false; | 36 return false; |
36 signature->resize(RSA_size(rsa_key)); | 37 signature->resize(RSA_size(rsa_key.get())); |
37 | 38 |
38 unsigned int len = 0; | 39 unsigned int len = 0; |
39 bool success = RSA_sign(NID_sha1, data, data_len, vector_as_array(signature), | 40 bool success = RSA_sign(NID_sha1, data, data_len, vector_as_array(signature), |
40 &len, rsa_key); | 41 &len, rsa_key.get()); |
41 if (!success) { | 42 if (!success) { |
42 signature->clear(); | 43 signature->clear(); |
43 return false; | 44 return false; |
44 } | 45 } |
45 signature->resize(len); | 46 signature->resize(len); |
46 return true; | 47 return true; |
47 } | 48 } |
48 | 49 |
49 SignatureCreator::SignatureCreator() | 50 SignatureCreator::SignatureCreator() |
50 : sign_context_(EVP_MD_CTX_create()) { | 51 : sign_context_(EVP_MD_CTX_create()) { |
(...skipping 17 matching lines...) Expand all Loading... |
68 int rv = EVP_SignFinal(sign_context_, vector_as_array(signature), &len, key); | 69 int rv = EVP_SignFinal(sign_context_, vector_as_array(signature), &len, key); |
69 if (!rv) { | 70 if (!rv) { |
70 signature->clear(); | 71 signature->clear(); |
71 return false; | 72 return false; |
72 } | 73 } |
73 signature->resize(len); | 74 signature->resize(len); |
74 return true; | 75 return true; |
75 } | 76 } |
76 | 77 |
77 } // namespace crypto | 78 } // namespace crypto |
OLD | NEW |