Chromium Code Reviews| 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.h" | 5 #include "crypto/ec_signature_creator.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> |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 41 std::vector<uint8> signature_data(signature_len); | 41 std::vector<uint8> signature_data(signature_len); |
| 42 SECItem sig = {siBuffer, &signature_data[0], signature_len}; | 42 SECItem sig = {siBuffer, &signature_data[0], signature_len}; |
| 43 rv = PK11_Sign(key, &sig, &hash); | 43 rv = PK11_Sign(key, &sig, &hash); |
| 44 if (rv != SECSuccess) | 44 if (rv != SECSuccess) |
| 45 return rv; | 45 return rv; |
| 46 | 46 |
| 47 // DER encode the signature. | 47 // DER encode the signature. |
| 48 return DSAU_EncodeDerSigWithLen(result, &sig, sig.len); | 48 return DSAU_EncodeDerSigWithLen(result, &sig, sig.len); |
| 49 } | 49 } |
| 50 | 50 |
| 51 } // namespace | 51 class ECSignatureCreatorNSS : public ECSignatureCreator { |
| 52 public: | |
| 53 explicit ECSignatureCreatorNSS(ECPrivateKey* key); | |
| 54 virtual ~ECSignatureCreatorNSS(); | |
| 52 | 55 |
| 53 // static | 56 virtual bool Sign(const uint8* data, |
| 54 ECSignatureCreator* ECSignatureCreator::Create(ECPrivateKey* key) { | 57 int data_len, |
| 55 return new ECSignatureCreator(key); | 58 std::vector<uint8>* signature) OVERRIDE; |
| 56 } | |
| 57 | 59 |
| 58 ECSignatureCreator::ECSignatureCreator(ECPrivateKey* key) | 60 private: |
| 61 ECPrivateKey* key_; | |
| 62 | |
| 63 DISALLOW_COPY_AND_ASSIGN(ECSignatureCreatorNSS); | |
| 64 }; | |
| 65 | |
| 66 ECSignatureCreatorNSS::ECSignatureCreatorNSS(ECPrivateKey* key) | |
| 59 : key_(key) { | 67 : key_(key) { |
| 60 EnsureNSSInit(); | 68 EnsureNSSInit(); |
| 61 } | 69 } |
| 62 | 70 |
| 63 ECSignatureCreator::~ECSignatureCreator() { } | 71 ECSignatureCreatorNSS::~ECSignatureCreatorNSS() { } |
|
wtc
2012/02/23 23:39:57
Nit: remove the space inside {}.
mattm
2012/02/24 01:05:28
Done.
| |
| 64 | 72 |
| 65 bool ECSignatureCreator::Sign(const uint8* data, | 73 bool ECSignatureCreatorNSS::Sign(const uint8* data, |
| 66 int data_len, | 74 int data_len, |
| 67 std::vector<uint8>* signature) { | 75 std::vector<uint8>* signature) { |
| 68 // Data to be signed | 76 // Data to be signed |
| 69 SECItem secret; | 77 SECItem secret; |
| 70 secret.type = siBuffer; | 78 secret.type = siBuffer; |
| 71 secret.len = data_len; | 79 secret.len = data_len; |
| 72 secret.data = const_cast<unsigned char*>(data); | 80 secret.data = const_cast<unsigned char*>(data); |
| 73 | 81 |
| 74 // SECItem to receive the output buffer. | 82 // SECItem to receive the output buffer. |
| 75 SECItem result; | 83 SECItem result; |
| 76 result.type = siBuffer; | 84 result.type = siBuffer; |
| 77 result.len = 0; | 85 result.len = 0; |
| 78 result.data = NULL; | 86 result.data = NULL; |
| 79 | 87 |
| 80 // Sign the secret data and save it to |result|. | 88 // Sign the secret data and save it to |result|. |
| 81 SECStatus rv = | 89 SECStatus rv = |
| 82 SignData(&result, &secret, key_->key(), HASH_AlgSHA1); | 90 SignData(&result, &secret, key_->key(), HASH_AlgSHA1); |
| 83 if (rv != SECSuccess) { | 91 if (rv != SECSuccess) { |
| 84 DLOG(ERROR) << "DerSignData: " << PORT_GetError(); | 92 DLOG(ERROR) << "DerSignData: " << PORT_GetError(); |
| 85 return false; | 93 return false; |
| 86 } | 94 } |
| 87 | 95 |
| 88 // Copy the signed data into the output vector. | 96 // Copy the signed data into the output vector. |
| 89 signature->assign(result.data, result.data + result.len); | 97 signature->assign(result.data, result.data + result.len); |
| 90 SECITEM_FreeItem(&result, PR_FALSE /* only free |result.data| */); | 98 SECITEM_FreeItem(&result, PR_FALSE /* only free |result.data| */); |
| 91 return true; | 99 return true; |
| 92 } | 100 } |
| 93 | 101 |
| 102 } // namespace | |
| 103 | |
| 104 // static | |
| 105 ECSignatureCreator* ECSignatureCreator::CreatePlatformImpl(ECPrivateKey* key) { | |
| 106 return new ECSignatureCreatorNSS(key); | |
| 107 } | |
| 108 | |
| 94 } // namespace crypto | 109 } // namespace crypto |
| OLD | NEW |