blob: 3a30efba480a3be7f76cd5a2715add5aa85d937e [file] [log] [blame]
[email protected]5ee44d42012-02-08 00:14:541// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]71a9f842009-09-24 01:21:122// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4b559b4d2011-04-14 17:37:145#include "crypto/signature_creator.h"
[email protected]71a9f842009-09-24 01:21:126
7#include <cryptohi.h>
8#include <keyhi.h>
9#include <stdlib.h>
10
11#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:1512#include "base/memory/scoped_ptr.h"
[email protected]4b559b4d2011-04-14 17:37:1413#include "crypto/nss_util.h"
[email protected]5ee44d42012-02-08 00:14:5414#include "crypto/rsa_private_key.h"
[email protected]71a9f842009-09-24 01:21:1215
[email protected]4b559b4d2011-04-14 17:37:1416namespace crypto {
[email protected]71a9f842009-09-24 01:21:1217
[email protected]eae9c062011-01-11 00:50:5918SignatureCreator::~SignatureCreator() {
19 if (sign_context_) {
20 SGN_DestroyContext(sign_context_, PR_TRUE);
21 sign_context_ = NULL;
22 }
23}
24
[email protected]71a9f842009-09-24 01:21:1225// static
26SignatureCreator* SignatureCreator::Create(RSAPrivateKey* key) {
27 scoped_ptr<SignatureCreator> result(new SignatureCreator);
28 result->key_ = key;
29
30 result->sign_context_ = SGN_NewContext(SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION,
31 key->key());
32 if (!result->sign_context_) {
33 NOTREACHED();
34 return NULL;
35 }
36
37 SECStatus rv = SGN_Begin(result->sign_context_);
38 if (rv != SECSuccess) {
39 NOTREACHED();
40 return NULL;
41 }
42
43 return result.release();
44}
45
[email protected]71a9f842009-09-24 01:21:1246bool SignatureCreator::Update(const uint8* data_part, int data_part_len) {
[email protected]1e33d6c2009-09-25 01:30:3947 // 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
[email protected]71a9f842009-09-24 01:21:1249 SECStatus rv = SGN_Update(sign_context_,
50 const_cast<unsigned char*>(data_part),
51 data_part_len);
52 if (rv != SECSuccess) {
53 NOTREACHED();
54 return false;
55 }
56
57 return true;
58}
59
60bool SignatureCreator::Final(std::vector<uint8>* signature) {
61 SECItem signature_item;
62 SECStatus rv = SGN_End(sign_context_, &signature_item);
63 if (rv != SECSuccess) {
64 NOTREACHED();
65 return false;
66 }
67 signature->assign(signature_item.data,
68 signature_item.data + signature_item.len);
69 SECITEM_FreeItem(&signature_item, PR_FALSE);
70 return true;
71}
72
[email protected]335b0dc2012-03-30 21:37:1973SignatureCreator::SignatureCreator()
74 : key_(NULL),
75 sign_context_(NULL) {
[email protected]eae9c062011-01-11 00:50:5976 EnsureNSSInit();
77}
78
[email protected]4b559b4d2011-04-14 17:37:1479} // namespace crypto