1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "crypto/signature_creator.h"
#include <cryptohi.h>
#include <keyhi.h>
#include <stdlib.h>
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "crypto/nss_util.h"
#include "crypto/rsa_private_key.h"
namespace crypto {
namespace {
SECOidTag ToNSSSigOid(SignatureCreator::HashAlgorithm hash_alg) {
switch (hash_alg) {
case SignatureCreator::SHA1:
return SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION;
case SignatureCreator::SHA256:
return SEC_OID_PKCS1_SHA256_WITH_RSA_ENCRYPTION;
}
return SEC_OID_UNKNOWN;
}
SECOidTag ToNSSHashOid(SignatureCreator::HashAlgorithm hash_alg) {
switch (hash_alg) {
case SignatureCreator::SHA1:
return SEC_OID_SHA1;
case SignatureCreator::SHA256:
return SEC_OID_SHA256;
}
return SEC_OID_UNKNOWN;
}
} // namespace
SignatureCreator::~SignatureCreator() {
if (sign_context_) {
SGN_DestroyContext(sign_context_, PR_TRUE);
sign_context_ = NULL;
}
}
// static
SignatureCreator* SignatureCreator::Create(RSAPrivateKey* key,
HashAlgorithm hash_alg) {
scoped_ptr<SignatureCreator> result(new SignatureCreator);
result->sign_context_ = SGN_NewContext(ToNSSSigOid(hash_alg), key->key());
if (!result->sign_context_) {
NOTREACHED();
return NULL;
}
SECStatus rv = SGN_Begin(result->sign_context_);
if (rv != SECSuccess) {
NOTREACHED();
return NULL;
}
return result.release();
}
// static
bool SignatureCreator::Sign(RSAPrivateKey* key,
HashAlgorithm hash_alg,
const uint8* data,
int data_len,
std::vector<uint8>* signature) {
SECItem data_item;
data_item.type = siBuffer;
data_item.data = const_cast<unsigned char*>(data);
data_item.len = data_len;
SECItem signature_item;
SECStatus rv = SGN_Digest(key->key(), ToNSSHashOid(hash_alg), &signature_item,
&data_item);
if (rv != SECSuccess) {
NOTREACHED();
return false;
}
signature->assign(signature_item.data,
signature_item.data + signature_item.len);
SECITEM_FreeItem(&signature_item, PR_FALSE);
return true;
}
bool SignatureCreator::Update(const uint8* data_part, int data_part_len) {
SECStatus rv = SGN_Update(sign_context_, data_part, data_part_len);
if (rv != SECSuccess) {
NOTREACHED();
return false;
}
return true;
}
bool SignatureCreator::Final(std::vector<uint8>* signature) {
SECItem signature_item;
SECStatus rv = SGN_End(sign_context_, &signature_item);
if (rv != SECSuccess) {
return false;
}
signature->assign(signature_item.data,
signature_item.data + signature_item.len);
SECITEM_FreeItem(&signature_item, PR_FALSE);
return true;
}
SignatureCreator::SignatureCreator() : sign_context_(NULL) {
EnsureNSSInit();
}
} // namespace crypto
|