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.
5 #include "crypto/signature_creator.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "crypto/nss_util.h"
14 #include "crypto/rsa_private_key.h"
18 SignatureCreator::~SignatureCreator() {
20 SGN_DestroyContext(sign_context_
, PR_TRUE
);
26 SignatureCreator
* SignatureCreator::Create(RSAPrivateKey
* key
) {
27 scoped_ptr
<SignatureCreator
> result(new SignatureCreator
);
30 result
->sign_context_
= SGN_NewContext(SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION
,
32 if (!result
->sign_context_
) {
37 SECStatus rv
= SGN_Begin(result
->sign_context_
);
38 if (rv
!= SECSuccess
) {
43 return result
.release();
46 bool SignatureCreator::Update(const uint8
* data_part
, int data_part_len
) {
47 // TODO(wtc): Remove this const_cast when we require NSS 3.12.5.
48 // See NSS bug https://bugzilla.mozilla.org/show_bug.cgi?id=518255
49 SECStatus rv
= SGN_Update(sign_context_
,
50 const_cast<unsigned char*>(data_part
),
52 if (rv
!= SECSuccess
) {
60 bool SignatureCreator::Final(std::vector
<uint8
>* signature
) {
61 SECItem signature_item
;
62 SECStatus rv
= SGN_End(sign_context_
, &signature_item
);
63 if (rv
!= SECSuccess
) {
67 signature
->assign(signature_item
.data
,
68 signature_item
.data
+ signature_item
.len
);
69 SECITEM_FreeItem(&signature_item
, PR_FALSE
);
73 SignatureCreator::SignatureCreator()