Skip to content

Commit

Permalink
add testCertVerify shared test for use in openssl and other backends
Browse files Browse the repository at this point in the history
Reviewed By: mingtaoy

Differential Revision: D66143703

fbshipit-source-id: bf6e42ca47a6bfb81ecbeca88e796d5d54d3c53a
  • Loading branch information
Zale Young authored and facebook-github-bot committed Jan 28, 2025
1 parent f8b7ddb commit 88cb93d
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
35 changes: 35 additions & 0 deletions fizz/backend/openssl/crypto/signature/test/PeerCertVerify.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2018-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <folly/portability/GTest.h>

#include <fizz/backend/openssl/certificate/CertUtils.h>
#include <fizz/crypto/test/Signature.h>
#include <fizz/crypto/test/SignatureTestData.h>

using namespace testing;

namespace fizz {
namespace openssl {
namespace test {

class VerifyTest : public Test,
public WithParamInterface<fizz::test::SignatureTestData> {};

TEST_P(VerifyTest, PeerCertVerify) {
fizz::test::testCertVerify(GetParam(), openssl::CertUtils::makePeerCert);
}

INSTANTIATE_TEST_SUITE_P(
SignatureTestVectors,
VerifyTest,
ValuesIn(fizz::test::kSignatureTestVectors));

} // namespace test
} // namespace openssl
} // namespace fizz
72 changes: 72 additions & 0 deletions fizz/crypto/test/Signature.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2018-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <fizz/crypto/test/Signature.h>

#include <folly/Range.h>
#include <folly/io/IOBuf.h>
#include <folly/portability/GTest.h>

using namespace testing;

namespace fizz {
namespace test {

std::unique_ptr<folly::IOBuf> makeCertBuf(std::string certDer) {
return folly::IOBuf::copyBuffer(certDer.data(), certDer.size());
}

void testCertVerify(
SignatureTestData testCase,
std::unique_ptr<PeerCert> (*makePeerCert)(Buf)) {
std::string certDer = folly::unhexlify(testCase.certDer);
std::string msg = folly::unhexlify(testCase.msg);
std::string sig = folly::unhexlify(testCase.sig);

std::unique_ptr<folly::IOBuf> certBuf = makeCertBuf(certDer);

if (!testCase.validCert) {
EXPECT_THROW(makePeerCert(makeCertBuf(certDer)), std::runtime_error);
return;
}

// make sure move works
auto tempPeerCert = makePeerCert(makeCertBuf(certDer));

auto peerCert = std::move(tempPeerCert);

// test getDER()
auto retDer = peerCert->getDER();
ASSERT_TRUE(retDer.has_value());

ASSERT_EQ(memcmp(certDer.c_str(), retDer.value().c_str(), certDer.size()), 0);

if (!testCase.validSig) {
EXPECT_THROW(
peerCert->verify(
testCase.sigScheme,
fizz::CertificateVerifyContext::Server,
folly::ByteRange(
reinterpret_cast<const unsigned char*>(msg.c_str()),
msg.size()),
folly::ByteRange(
reinterpret_cast<const unsigned char*>(sig.c_str()),
sig.size())),
std::runtime_error);
} else {
EXPECT_NO_THROW(peerCert->verify(
testCase.sigScheme,
fizz::CertificateVerifyContext::Server,
folly::ByteRange(
reinterpret_cast<const unsigned char*>(msg.c_str()), msg.size()),
folly::ByteRange(
reinterpret_cast<const unsigned char*>(sig.c_str()), sig.size())));
}
}
} // namespace test
} // namespace fizz
21 changes: 21 additions & 0 deletions fizz/crypto/test/Signature.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2018-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <folly/portability/GTest.h>

#include <fizz/crypto/test/SignatureTestData.h>
#include <fizz/protocol/Certificate.h>

namespace fizz {
namespace test {

void testCertVerify(
SignatureTestData testCase,
std::unique_ptr<PeerCert> (*makePeerCert)(Buf));
} // namespace test
} // namespace fizz
2 changes: 2 additions & 0 deletions fizz/crypto/test/SignatureTestData.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once

#include <fizz/record/Types.h>

namespace fizz::test {
Expand Down

0 comments on commit 88cb93d

Please sign in to comment.