OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "crypto/ec_signature_creator_impl.h" | 5 #include "crypto/ec_signature_creator_impl.h" |
6 | 6 |
7 #include <cryptohi.h> | 7 #include <cryptohi.h> |
8 #include <pk11pub.h> | 8 #include <pk11pub.h> |
9 #include <secerr.h> | 9 #include <secerr.h> |
10 #include <sechash.h> | 10 #include <sechash.h> |
11 #if defined(OS_POSIX) | 11 #if defined(OS_POSIX) |
12 #include <unistd.h> | 12 #include <unistd.h> |
13 #endif | 13 #endif |
14 | 14 |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "crypto/ec_private_key.h" | 16 #include "crypto/ec_private_key.h" |
17 #include "crypto/nss_util.h" | 17 #include "crypto/nss_util.h" |
18 #include "crypto/scoped_nss_types.h" | 18 #include "crypto/scoped_nss_types.h" |
19 | 19 |
20 namespace crypto { | 20 namespace crypto { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 SECStatus SignData(SECItem* result, | 24 SECStatus SignData(SECItem* result, |
25 SECItem* input, | 25 SECItem* input, |
26 SECKEYPrivateKey* key, | 26 SECKEYPrivateKey* key, |
27 HASH_HashType hash_type) { | 27 HASH_HashType hash_type, |
| 28 size_t* out_signature_len) { |
28 if (key->keyType != ecKey) { | 29 if (key->keyType != ecKey) { |
29 DLOG(FATAL) << "Should be using an EC key."; | 30 DLOG(FATAL) << "Should be using an EC key."; |
30 PORT_SetError(SEC_ERROR_INVALID_ARGS); | 31 PORT_SetError(SEC_ERROR_INVALID_ARGS); |
31 return SECFailure; | 32 return SECFailure; |
32 } | 33 } |
33 | 34 |
34 // Hash the input. | 35 // Hash the input. |
35 std::vector<uint8> hash_data(HASH_ResultLen(hash_type)); | 36 std::vector<uint8> hash_data(HASH_ResultLen(hash_type)); |
36 SECStatus rv = HASH_HashBuf( | 37 SECStatus rv = HASH_HashBuf( |
37 hash_type, &hash_data[0], input->data, input->len); | 38 hash_type, &hash_data[0], input->data, input->len); |
38 if (rv != SECSuccess) | 39 if (rv != SECSuccess) |
39 return rv; | 40 return rv; |
40 SECItem hash = {siBuffer, &hash_data[0], | 41 SECItem hash = {siBuffer, &hash_data[0], |
41 static_cast<unsigned int>(hash_data.size())}; | 42 static_cast<unsigned int>(hash_data.size())}; |
42 | 43 |
43 // Compute signature of hash. | 44 // Compute signature of hash. |
44 int signature_len = PK11_SignatureLen(key); | 45 int signature_len = PK11_SignatureLen(key); |
45 std::vector<uint8> signature_data(signature_len); | 46 std::vector<uint8> signature_data(signature_len); |
46 SECItem sig = {siBuffer, &signature_data[0], | 47 SECItem sig = {siBuffer, &signature_data[0], |
47 static_cast<unsigned int>(signature_len)}; | 48 static_cast<unsigned int>(signature_len)}; |
48 rv = PK11_Sign(key, &sig, &hash); | 49 rv = PK11_Sign(key, &sig, &hash); |
49 if (rv != SECSuccess) | 50 if (rv != SECSuccess) |
50 return rv; | 51 return rv; |
51 | 52 |
| 53 *out_signature_len = sig.len; |
| 54 |
52 // DER encode the signature. | 55 // DER encode the signature. |
53 return DSAU_EncodeDerSigWithLen(result, &sig, sig.len); | 56 return DSAU_EncodeDerSigWithLen(result, &sig, sig.len); |
54 } | 57 } |
55 | 58 |
56 } // namespace | 59 } // namespace |
57 | 60 |
58 ECSignatureCreatorImpl::ECSignatureCreatorImpl(ECPrivateKey* key) | 61 ECSignatureCreatorImpl::ECSignatureCreatorImpl(ECPrivateKey* key) |
59 : key_(key) { | 62 : key_(key), |
| 63 signature_len_(0) { |
60 EnsureNSSInit(); | 64 EnsureNSSInit(); |
61 } | 65 } |
62 | 66 |
63 ECSignatureCreatorImpl::~ECSignatureCreatorImpl() {} | 67 ECSignatureCreatorImpl::~ECSignatureCreatorImpl() {} |
64 | 68 |
65 bool ECSignatureCreatorImpl::Sign(const uint8* data, | 69 bool ECSignatureCreatorImpl::Sign(const uint8* data, |
66 int data_len, | 70 int data_len, |
67 std::vector<uint8>* signature) { | 71 std::vector<uint8>* signature) { |
68 // Data to be signed | 72 // Data to be signed |
69 SECItem secret; | 73 SECItem secret; |
70 secret.type = siBuffer; | 74 secret.type = siBuffer; |
71 secret.len = data_len; | 75 secret.len = data_len; |
72 secret.data = const_cast<unsigned char*>(data); | 76 secret.data = const_cast<unsigned char*>(data); |
73 | 77 |
74 // SECItem to receive the output buffer. | 78 // SECItem to receive the output buffer. |
75 SECItem result; | 79 SECItem result; |
76 result.type = siBuffer; | 80 result.type = siBuffer; |
77 result.len = 0; | 81 result.len = 0; |
78 result.data = NULL; | 82 result.data = NULL; |
79 | 83 |
80 // Sign the secret data and save it to |result|. | 84 // Sign the secret data and save it to |result|. |
81 SECStatus rv = | 85 SECStatus rv = |
82 SignData(&result, &secret, key_->key(), HASH_AlgSHA1); | 86 SignData(&result, &secret, key_->key(), HASH_AlgSHA256, &signature_len_); |
83 if (rv != SECSuccess) { | 87 if (rv != SECSuccess) { |
84 DLOG(ERROR) << "DerSignData: " << PORT_GetError(); | 88 DLOG(ERROR) << "DerSignData: " << PORT_GetError(); |
85 return false; | 89 return false; |
86 } | 90 } |
87 | 91 |
88 // Copy the signed data into the output vector. | 92 // Copy the signed data into the output vector. |
89 signature->assign(result.data, result.data + result.len); | 93 signature->assign(result.data, result.data + result.len); |
90 SECITEM_FreeItem(&result, PR_FALSE /* only free |result.data| */); | 94 SECITEM_FreeItem(&result, PR_FALSE /* only free |result.data| */); |
91 return true; | 95 return true; |
92 } | 96 } |
93 | 97 |
| 98 bool ECSignatureCreatorImpl::DecodeSignature( |
| 99 const std::vector<uint8>& der_sig, |
| 100 std::vector<uint8>* out_raw_sig) { |
| 101 SECItem der_sig_item; |
| 102 der_sig_item.type = siBuffer; |
| 103 der_sig_item.len = der_sig.size(); |
| 104 der_sig_item.data = const_cast<uint8*>(&der_sig[0]); |
| 105 |
| 106 SECItem* raw_sig = DSAU_DecodeDerSigToLen(&der_sig_item, signature_len_); |
| 107 if (!raw_sig) |
| 108 return false; |
| 109 out_raw_sig->assign(raw_sig->data, raw_sig->data + raw_sig->len); |
| 110 SECITEM_FreeItem(raw_sig, PR_TRUE /* free SECItem structure itself. */); |
| 111 return true; |
| 112 } |
| 113 |
94 } // namespace crypto | 114 } // namespace crypto |
OLD | NEW |