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 // Private constructor. Use the Create() method instead. | |
| 54 explicit ECSignatureCreatorNSS(ECPrivateKey* key); | |
|
Ryan Hamilton
2012/02/23 04:36:12
The comment says private, but the access modifier
mattm
2012/02/23 04:44:57
Ah, comment was moved over from the now-abstract E
| |
| 55 virtual ~ECSignatureCreatorNSS(); | |
| 52 | 56 |
| 53 // static | 57 virtual bool Sign(const uint8* data, |
| 54 ECSignatureCreator* ECSignatureCreator::Create(ECPrivateKey* key) { | 58 int data_len, |
| 55 return new ECSignatureCreator(key); | 59 std::vector<uint8>* signature); |
| 56 } | |
| 57 | 60 |
| 58 ECSignatureCreator::ECSignatureCreator(ECPrivateKey* key) | 61 private: |
| 62 ECPrivateKey* key_; | |
| 63 | |
| 64 DISALLOW_COPY_AND_ASSIGN(ECSignatureCreatorNSS); | |
| 65 }; | |
| 66 | |
| 67 ECSignatureCreatorNSS::ECSignatureCreatorNSS(ECPrivateKey* key) | |
| 59 : key_(key) { | 68 : key_(key) { |
| 60 EnsureNSSInit(); | 69 EnsureNSSInit(); |
| 61 } | 70 } |
| 62 | 71 |
| 63 ECSignatureCreator::~ECSignatureCreator() { } | 72 ECSignatureCreatorNSS::~ECSignatureCreatorNSS() { } |
| 64 | 73 |
| 65 bool ECSignatureCreator::Sign(const uint8* data, | 74 bool ECSignatureCreatorNSS::Sign(const uint8* data, |
| 66 int data_len, | 75 int data_len, |
| 67 std::vector<uint8>* signature) { | 76 std::vector<uint8>* signature) { |
| 68 // Data to be signed | 77 // Data to be signed |
| 69 SECItem secret; | 78 SECItem secret; |
| 70 secret.type = siBuffer; | 79 secret.type = siBuffer; |
| 71 secret.len = data_len; | 80 secret.len = data_len; |
| 72 secret.data = const_cast<unsigned char*>(data); | 81 secret.data = const_cast<unsigned char*>(data); |
| 73 | 82 |
| 74 // SECItem to receive the output buffer. | 83 // SECItem to receive the output buffer. |
| 75 SECItem result; | 84 SECItem result; |
| 76 result.type = siBuffer; | 85 result.type = siBuffer; |
| 77 result.len = 0; | 86 result.len = 0; |
| 78 result.data = NULL; | 87 result.data = NULL; |
| 79 | 88 |
| 80 // Sign the secret data and save it to |result|. | 89 // Sign the secret data and save it to |result|. |
| 81 SECStatus rv = | 90 SECStatus rv = |
| 82 SignData(&result, &secret, key_->key(), HASH_AlgSHA1); | 91 SignData(&result, &secret, key_->key(), HASH_AlgSHA1); |
| 83 if (rv != SECSuccess) { | 92 if (rv != SECSuccess) { |
| 84 DLOG(ERROR) << "DerSignData: " << PORT_GetError(); | 93 DLOG(ERROR) << "DerSignData: " << PORT_GetError(); |
| 85 return false; | 94 return false; |
| 86 } | 95 } |
| 87 | 96 |
| 88 // Copy the signed data into the output vector. | 97 // Copy the signed data into the output vector. |
| 89 signature->assign(result.data, result.data + result.len); | 98 signature->assign(result.data, result.data + result.len); |
| 90 SECITEM_FreeItem(&result, PR_FALSE /* only free |result.data| */); | 99 SECITEM_FreeItem(&result, PR_FALSE /* only free |result.data| */); |
| 91 return true; | 100 return true; |
| 92 } | 101 } |
| 93 | 102 |
| 103 } // namespace | |
| 104 | |
| 105 // static | |
| 106 ECSignatureCreator* ECSignatureCreator::CreatePlatformImpl(ECPrivateKey* key) { | |
| 107 return new ECSignatureCreatorNSS(key); | |
| 108 } | |
| 109 | |
| 94 } // namespace crypto | 110 } // namespace crypto |
| OLD | NEW |