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();
47 bool SignatureCreator::Sign(RSAPrivateKey
* key
,
50 std::vector
<uint8
>* signature
) {
52 data_item
.type
= siBuffer
;
53 data_item
.data
= const_cast<unsigned char*>(data
);
54 data_item
.len
= data_len
;
56 SECItem signature_item
;
57 SECStatus rv
= SGN_Digest(key
->key(), SEC_OID_SHA1
, &signature_item
,
59 if (rv
!= SECSuccess
) {
63 signature
->assign(signature_item
.data
,
64 signature_item
.data
+ signature_item
.len
);
65 SECITEM_FreeItem(&signature_item
, PR_FALSE
);
69 bool SignatureCreator::Update(const uint8
* data_part
, int data_part_len
) {
70 SECStatus rv
= SGN_Update(sign_context_
, data_part
, data_part_len
);
71 if (rv
!= SECSuccess
) {
79 bool SignatureCreator::Final(std::vector
<uint8
>* signature
) {
80 SECItem signature_item
;
81 SECStatus rv
= SGN_End(sign_context_
, &signature_item
);
82 if (rv
!= SECSuccess
) {
85 signature
->assign(signature_item
.data
,
86 signature_item
.data
+ signature_item
.len
);
87 SECITEM_FreeItem(&signature_item
, PR_FALSE
);
91 SignatureCreator::SignatureCreator()