blob: 7108975934cd7f84de5e9f95e582ceae94244e80 [file] [log] [blame]
[email protected]71a9f842009-09-24 01:21:121// 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
3// found in the LICENSE file.
4
5#include "base/crypto/signature_creator.h"
6
7#include <cryptohi.h>
8#include <keyhi.h>
9#include <stdlib.h>
10
11#include "base/logging.h"
12#include "base/nss_init.h"
13#include "base/scoped_ptr.h"
14
15namespace base {
16
17// static
18SignatureCreator* SignatureCreator::Create(RSAPrivateKey* key) {
19 scoped_ptr<SignatureCreator> result(new SignatureCreator);
20 result->key_ = key;
21
22 result->sign_context_ = SGN_NewContext(SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION,
23 key->key());
24 if (!result->sign_context_) {
25 NOTREACHED();
26 return NULL;
27 }
28
29 SECStatus rv = SGN_Begin(result->sign_context_);
30 if (rv != SECSuccess) {
31 NOTREACHED();
32 return NULL;
33 }
34
35 return result.release();
36}
37
38SignatureCreator::SignatureCreator() : sign_context_(NULL) {
39 EnsureNSSInit();
40}
41
42SignatureCreator::~SignatureCreator() {
43 if (sign_context_) {
44 SGN_DestroyContext(sign_context_, PR_TRUE);
45 sign_context_ = NULL;
46 }
47}
48
49bool SignatureCreator::Update(const uint8* data_part, int data_part_len) {
50 SECStatus rv = SGN_Update(sign_context_,
51 const_cast<unsigned char*>(data_part),
52 data_part_len);
53 if (rv != SECSuccess) {
54 NOTREACHED();
55 return false;
56 }
57
58 return true;
59}
60
61bool SignatureCreator::Final(std::vector<uint8>* signature) {
62 SECItem signature_item;
63 SECStatus rv = SGN_End(sign_context_, &signature_item);
64 if (rv != SECSuccess) {
65 NOTREACHED();
66 return false;
67 }
68 signature->assign(signature_item.data,
69 signature_item.data + signature_item.len);
70 SECITEM_FreeItem(&signature_item, PR_FALSE);
71 return true;
72}
73
74} // namespace base