OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "base/crypto/signature_creator.h" | 5 #include "base/crypto/signature_creator.h" |
6 | 6 |
7 #include <cryptohi.h> | 7 #include <cryptohi.h> |
8 #include <keyhi.h> | 8 #include <keyhi.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/nss_util.h" | 12 #include "base/nss_util.h" |
13 #include "base/scoped_ptr.h" | 13 #include "base/scoped_ptr.h" |
14 | 14 |
15 namespace base { | 15 namespace base { |
16 | 16 |
| 17 SignatureCreator::~SignatureCreator() { |
| 18 if (sign_context_) { |
| 19 SGN_DestroyContext(sign_context_, PR_TRUE); |
| 20 sign_context_ = NULL; |
| 21 } |
| 22 } |
| 23 |
17 // static | 24 // static |
18 SignatureCreator* SignatureCreator::Create(RSAPrivateKey* key) { | 25 SignatureCreator* SignatureCreator::Create(RSAPrivateKey* key) { |
19 scoped_ptr<SignatureCreator> result(new SignatureCreator); | 26 scoped_ptr<SignatureCreator> result(new SignatureCreator); |
20 result->key_ = key; | 27 result->key_ = key; |
21 | 28 |
22 result->sign_context_ = SGN_NewContext(SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION, | 29 result->sign_context_ = SGN_NewContext(SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION, |
23 key->key()); | 30 key->key()); |
24 if (!result->sign_context_) { | 31 if (!result->sign_context_) { |
25 NOTREACHED(); | 32 NOTREACHED(); |
26 return NULL; | 33 return NULL; |
27 } | 34 } |
28 | 35 |
29 SECStatus rv = SGN_Begin(result->sign_context_); | 36 SECStatus rv = SGN_Begin(result->sign_context_); |
30 if (rv != SECSuccess) { | 37 if (rv != SECSuccess) { |
31 NOTREACHED(); | 38 NOTREACHED(); |
32 return NULL; | 39 return NULL; |
33 } | 40 } |
34 | 41 |
35 return result.release(); | 42 return result.release(); |
36 } | 43 } |
37 | 44 |
38 SignatureCreator::SignatureCreator() : sign_context_(NULL) { | |
39 EnsureNSSInit(); | |
40 } | |
41 | |
42 SignatureCreator::~SignatureCreator() { | |
43 if (sign_context_) { | |
44 SGN_DestroyContext(sign_context_, PR_TRUE); | |
45 sign_context_ = NULL; | |
46 } | |
47 } | |
48 | |
49 bool SignatureCreator::Update(const uint8* data_part, int data_part_len) { | 45 bool SignatureCreator::Update(const uint8* data_part, int data_part_len) { |
50 // TODO(wtc): Remove this const_cast when we require NSS 3.12.5. | 46 // TODO(wtc): Remove this const_cast when we require NSS 3.12.5. |
51 // See NSS bug https://bugzilla.mozilla.org/show_bug.cgi?id=518255 | 47 // See NSS bug https://bugzilla.mozilla.org/show_bug.cgi?id=518255 |
52 SECStatus rv = SGN_Update(sign_context_, | 48 SECStatus rv = SGN_Update(sign_context_, |
53 const_cast<unsigned char*>(data_part), | 49 const_cast<unsigned char*>(data_part), |
54 data_part_len); | 50 data_part_len); |
55 if (rv != SECSuccess) { | 51 if (rv != SECSuccess) { |
56 NOTREACHED(); | 52 NOTREACHED(); |
57 return false; | 53 return false; |
58 } | 54 } |
59 | 55 |
60 return true; | 56 return true; |
61 } | 57 } |
62 | 58 |
63 bool SignatureCreator::Final(std::vector<uint8>* signature) { | 59 bool SignatureCreator::Final(std::vector<uint8>* signature) { |
64 SECItem signature_item; | 60 SECItem signature_item; |
65 SECStatus rv = SGN_End(sign_context_, &signature_item); | 61 SECStatus rv = SGN_End(sign_context_, &signature_item); |
66 if (rv != SECSuccess) { | 62 if (rv != SECSuccess) { |
67 NOTREACHED(); | 63 NOTREACHED(); |
68 return false; | 64 return false; |
69 } | 65 } |
70 signature->assign(signature_item.data, | 66 signature->assign(signature_item.data, |
71 signature_item.data + signature_item.len); | 67 signature_item.data + signature_item.len); |
72 SECITEM_FreeItem(&signature_item, PR_FALSE); | 68 SECITEM_FreeItem(&signature_item, PR_FALSE); |
73 return true; | 69 return true; |
74 } | 70 } |
75 | 71 |
| 72 SignatureCreator::SignatureCreator() : sign_context_(NULL) { |
| 73 EnsureNSSInit(); |
| 74 } |
| 75 |
76 } // namespace base | 76 } // namespace base |
OLD | NEW |