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 #include <stddef.h> | 9 #include <stddef.h> |
10 #include <stdint.h> | 10 #include <stdint.h> |
11 | 11 |
12 #include "base/logging.h" | 12 #include "base/logging.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" | |
16 | 15 |
17 namespace crypto { | 16 namespace crypto { |
18 | 17 |
19 namespace { | 18 namespace { |
20 | 19 |
21 const EVP_MD* ToOpenSSLDigest(SignatureCreator::HashAlgorithm hash_alg) { | 20 const EVP_MD* ToOpenSSLDigest(SignatureCreator::HashAlgorithm hash_alg) { |
22 switch (hash_alg) { | 21 switch (hash_alg) { |
23 case SignatureCreator::SHA1: | 22 case SignatureCreator::SHA1: |
24 return EVP_sha1(); | 23 return EVP_sha1(); |
25 case SignatureCreator::SHA256: | 24 case SignatureCreator::SHA256: |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 } | 60 } |
62 return result; | 61 return result; |
63 } | 62 } |
64 | 63 |
65 // static | 64 // static |
66 bool SignatureCreator::Sign(RSAPrivateKey* key, | 65 bool SignatureCreator::Sign(RSAPrivateKey* key, |
67 HashAlgorithm hash_alg, | 66 HashAlgorithm hash_alg, |
68 const uint8_t* data, | 67 const uint8_t* data, |
69 int data_len, | 68 int data_len, |
70 std::vector<uint8_t>* signature) { | 69 std::vector<uint8_t>* signature) { |
71 ScopedRSA rsa_key(EVP_PKEY_get1_RSA(key->key())); | 70 bssl::UniquePtr<RSA> rsa_key(EVP_PKEY_get1_RSA(key->key())); |
72 if (!rsa_key) | 71 if (!rsa_key) |
73 return false; | 72 return false; |
74 signature->resize(RSA_size(rsa_key.get())); | 73 signature->resize(RSA_size(rsa_key.get())); |
75 | 74 |
76 unsigned int len = 0; | 75 unsigned int len = 0; |
77 if (!RSA_sign(ToOpenSSLDigestType(hash_alg), data, data_len, | 76 if (!RSA_sign(ToOpenSSLDigestType(hash_alg), data, data_len, |
78 signature->data(), &len, rsa_key.get())) { | 77 signature->data(), &len, rsa_key.get())) { |
79 signature->clear(); | 78 signature->clear(); |
80 return false; | 79 return false; |
81 } | 80 } |
(...skipping 22 matching lines...) Expand all Loading... |
104 signature->clear(); | 103 signature->clear(); |
105 return false; | 104 return false; |
106 } | 105 } |
107 signature->resize(len); | 106 signature->resize(len); |
108 return true; | 107 return true; |
109 } | 108 } |
110 | 109 |
111 SignatureCreator::SignatureCreator() : sign_context_(EVP_MD_CTX_create()) {} | 110 SignatureCreator::SignatureCreator() : sign_context_(EVP_MD_CTX_create()) {} |
112 | 111 |
113 } // namespace crypto | 112 } // namespace crypto |
OLD | NEW |