Dactyloidae/security/nss/gtests/pk11_gtest/pk11_signature_test.h

115 lines
3.7 KiB
C
Raw Normal View History

2018-02-06 11:46:26 +01:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <memory>
#include "nss.h"
#include "pk11pub.h"
#include "sechash.h"
2018-12-15 01:42:53 +01:00
#include "nss_scoped_ptrs.h"
2018-02-23 11:04:39 +01:00
#include "databuffer.h"
2018-02-06 11:46:26 +01:00
#include "gtest/gtest.h"
namespace nss_test {
2018-02-23 11:04:39 +01:00
// For test vectors.
struct Pkcs11SignatureTestParams {
const DataBuffer pkcs8_;
const DataBuffer spki_;
const DataBuffer data_;
const DataBuffer signature_;
};
2018-02-06 11:46:26 +01:00
class Pk11SignatureTest : public ::testing::Test {
protected:
2026-06-29 21:29:25 +01:00
Pk11SignatureTest(CK_MECHANISM_TYPE mech, SECOidTag hash_oid,
CK_MECHANISM_TYPE combo)
: mechanism_(mech), hash_oid_(hash_oid), combo_(combo) {
skip_raw_ = false;
}
2018-02-06 11:46:26 +01:00
2018-02-23 11:04:39 +01:00
virtual const SECItem* parameters() const { return nullptr; }
CK_MECHANISM_TYPE mechanism() const { return mechanism_; }
2026-06-29 21:29:25 +01:00
void setSkipRaw(bool skip_raw) { skip_raw_ = true; }
2018-02-23 11:04:39 +01:00
2026-06-29 21:29:25 +01:00
bool ExportPrivateKey(ScopedSECKEYPrivateKey* key, DataBuffer& pkcs8) {
SECItem* pkcs8Item = PK11_ExportDERPrivateKeyInfo(key->get(), nullptr);
if (!pkcs8Item) {
return false;
2018-02-06 11:46:26 +01:00
}
2026-06-29 21:29:25 +01:00
pkcs8.Assign(pkcs8Item->data, pkcs8Item->len);
SECITEM_ZfreeItem(pkcs8Item, PR_TRUE);
return true;
2018-02-06 11:46:26 +01:00
}
2026-06-29 21:29:25 +01:00
ScopedSECKEYPrivateKey ImportPrivateKey(const DataBuffer& pkcs8);
ScopedSECKEYPublicKey ImportPublicKey(const DataBuffer& spki);
2018-02-06 11:46:26 +01:00
2018-02-23 11:04:39 +01:00
bool ComputeHash(const DataBuffer& data, DataBuffer* hash) {
hash->Allocate(static_cast<size_t>(HASH_ResultLenByOidTag(hash_oid_)));
SECStatus rv =
PK11_HashBuf(hash_oid_, hash->data(), data.data(), data.len());
return rv == SECSuccess;
2018-02-06 11:46:26 +01:00
}
2018-02-23 11:04:39 +01:00
bool SignHashedData(ScopedSECKEYPrivateKey& privKey, const DataBuffer& hash,
2026-06-29 21:29:25 +01:00
DataBuffer* sig);
bool SignData(ScopedSECKEYPrivateKey& privKey, const DataBuffer& data,
DataBuffer* sig);
2018-02-23 11:04:39 +01:00
bool ImportPrivateKeyAndSignHashedData(const DataBuffer& pkcs8,
const DataBuffer& data,
2026-06-29 21:29:25 +01:00
DataBuffer* sig, DataBuffer* sig2);
2018-02-06 11:46:26 +01:00
2026-06-29 21:29:25 +01:00
/* most primitive verify implemented in pk11_signature_test.cpp */
void Verify(ScopedSECKEYPublicKey& pubKey, const DataBuffer& data,
const DataBuffer& sig, bool valid);
/* quick helper functions that use the primitive verify */
void Verify(ScopedSECKEYPublicKey& pubKey, const DataBuffer& data,
const DataBuffer& sig) {
Verify(pubKey, data, sig, true);
2018-02-06 11:46:26 +01:00
}
2026-06-29 21:29:25 +01:00
void Verify(const Pkcs11SignatureTestParams& params, const DataBuffer& sig,
bool valid) {
2018-02-23 11:04:39 +01:00
ScopedSECKEYPublicKey pubKey(ImportPublicKey(params.spki_));
2018-02-06 11:46:26 +01:00
ASSERT_TRUE(pubKey);
2026-06-29 21:29:25 +01:00
Verify(pubKey, params.data_, sig, valid);
}
2018-02-06 11:46:26 +01:00
2026-06-29 21:29:25 +01:00
void Verify(const Pkcs11SignatureTestParams& params, bool valid) {
Verify(params, params.signature_, valid);
2018-02-06 11:46:26 +01:00
}
2018-02-23 11:04:39 +01:00
void Verify(const Pkcs11SignatureTestParams& params) {
2026-06-29 21:29:25 +01:00
Verify(params, params.signature_, true);
2018-02-23 11:04:39 +01:00
}
2018-02-06 11:46:26 +01:00
2018-02-23 11:04:39 +01:00
void SignAndVerify(const Pkcs11SignatureTestParams& params) {
DataBuffer sig;
2026-06-29 21:29:25 +01:00
DataBuffer sig2;
ASSERT_TRUE(ImportPrivateKeyAndSignHashedData(params.pkcs8_, params.data_,
&sig, &sig2));
Verify(params, sig, true);
Verify(params, sig2, true);
}
// Importing a private key in PKCS#8 format and reexporting it should
// result in the same binary representation.
void ImportExport(const DataBuffer& k) {
DataBuffer exported;
ScopedSECKEYPrivateKey key = ImportPrivateKey(k);
ExportPrivateKey(&key, exported);
EXPECT_EQ(k, exported);
2018-02-23 11:04:39 +01:00
}
2018-02-23 11:04:39 +01:00
private:
CK_MECHANISM_TYPE mechanism_;
SECOidTag hash_oid_;
2026-06-29 21:29:25 +01:00
CK_MECHANISM_TYPE combo_;
bool skip_raw_;
2018-02-23 11:04:39 +01:00
};
2018-02-06 11:46:26 +01:00
} // namespace nss_test