Skip to content

Commit

Permalink
move prepareSignData out of the openssl backend
Browse files Browse the repository at this point in the history
Summary: this is a generic function that other backends will use. Moving it to protocol/Certificate.h

Reviewed By: mingtaoy

Differential Revision: D64883829

fbshipit-source-id: ef2f5a0df3ba6aaf755a1a1052c938c3eb325737
  • Loading branch information
Zale Young authored and facebook-github-bot committed Jan 28, 2025
1 parent 6e56859 commit 27e4bc5
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 71 deletions.
10 changes: 5 additions & 5 deletions fizz/backend/openssl/certificate/CertUtils-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ inline void CertUtils::verify<KeyType::P256>(
CertificateVerifyContext context,
folly::ByteRange toBeSigned,
folly::ByteRange signature) {
auto signData = CertUtils::prepareSignData(context, toBeSigned);
auto signData = fizz::certverify::prepareSignData(context, toBeSigned);
switch (scheme) {
case SignatureScheme::ecdsa_secp256r1_sha256:
return certSignature.verify<SignatureScheme::ecdsa_secp256r1_sha256>(
Expand All @@ -63,7 +63,7 @@ inline void CertUtils::verify<KeyType::P384>(
CertificateVerifyContext context,
folly::ByteRange toBeSigned,
folly::ByteRange signature) {
auto signData = CertUtils::prepareSignData(context, toBeSigned);
auto signData = fizz::certverify::prepareSignData(context, toBeSigned);
switch (scheme) {
case SignatureScheme::ecdsa_secp384r1_sha384:
return certSignature.verify<SignatureScheme::ecdsa_secp384r1_sha384>(
Expand All @@ -80,7 +80,7 @@ inline void CertUtils::verify<KeyType::P521>(
CertificateVerifyContext context,
folly::ByteRange toBeSigned,
folly::ByteRange signature) {
auto signData = CertUtils::prepareSignData(context, toBeSigned);
auto signData = fizz::certverify::prepareSignData(context, toBeSigned);
switch (scheme) {
case SignatureScheme::ecdsa_secp521r1_sha512:
return certSignature.verify<SignatureScheme::ecdsa_secp521r1_sha512>(
Expand All @@ -97,7 +97,7 @@ inline void CertUtils::verify<KeyType::ED25519>(
CertificateVerifyContext context,
folly::ByteRange toBeSigned,
folly::ByteRange signature) {
auto signData = CertUtils::prepareSignData(context, toBeSigned);
auto signData = fizz::certverify::prepareSignData(context, toBeSigned);
switch (scheme) {
case SignatureScheme::ed25519:
return certSignature.verify<SignatureScheme::ed25519>(
Expand All @@ -114,7 +114,7 @@ inline void CertUtils::verify<KeyType::RSA>(
CertificateVerifyContext context,
folly::ByteRange toBeSigned,
folly::ByteRange signature) {
auto signData = CertUtils::prepareSignData(context, toBeSigned);
auto signData = fizz::certverify::prepareSignData(context, toBeSigned);
switch (scheme) {
case SignatureScheme::rsa_pss_sha256:
return certSignature.verify<SignatureScheme::rsa_pss_sha256>(
Expand Down
50 changes: 0 additions & 50 deletions fizz/backend/openssl/certificate/CertUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,56 +38,6 @@ folly::Optional<std::string> getIdentityFromX509(X509* x) {
}
} // namespace detail

Buf CertUtils::prepareSignData(
CertificateVerifyContext context,
folly::ByteRange toBeSigned) {
static constexpr folly::StringPiece kServerLabel =
"TLS 1.3, server CertificateVerify";
static constexpr folly::StringPiece kClientLabel =
"TLS 1.3, client CertificateVerify";
static constexpr folly::StringPiece kAuthLabel = "Exported Authenticator";
static constexpr folly::StringPiece kServerDelegatedCredLabel =
"TLS, server delegated credentials";
static constexpr folly::StringPiece kClientDelegatedCredLabel =
"TLS, client delegated credentials";
static constexpr size_t kSigPrefixLen = 64;
static constexpr uint8_t kSigPrefix = 32;

folly::StringPiece label;
switch (context) {
case CertificateVerifyContext::Server:
label = kServerLabel;
break;
case CertificateVerifyContext::Client:
label = kClientLabel;
break;
case CertificateVerifyContext::Authenticator:
label = kAuthLabel;
break;
case CertificateVerifyContext::ClientDelegatedCredential:
label = kClientDelegatedCredLabel;
break;
case CertificateVerifyContext::ServerDelegatedCredential:
label = kServerDelegatedCredLabel;
break;
}

size_t sigDataLen = kSigPrefixLen + label.size() + 1 + toBeSigned.size();
auto buf = folly::IOBuf::create(sigDataLen);
buf->append(sigDataLen);

// Place bytes in the right order.
size_t offset = 0;
memset(buf->writableData(), kSigPrefix, kSigPrefixLen);
offset += kSigPrefixLen;
memcpy(buf->writableData() + offset, label.data(), label.size());
offset += label.size();
memset(buf->writableData() + offset, 0, 1);
offset += 1;
memcpy(buf->writableData() + offset, toBeSigned.data(), toBeSigned.size());
return buf;
}

CertificateMsg CertUtils::getCertMessage(
const std::vector<folly::ssl::X509UniquePtr>& certs,
Buf certificateRequestContext) {
Expand Down
8 changes: 0 additions & 8 deletions fizz/backend/openssl/certificate/CertUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ namespace openssl {

class CertUtils {
public:
/**
* Adds the appropriate context data to prepare toBeSigned for a signature
* scheme's signing function.
*/
static Buf prepareSignData(
CertificateVerifyContext context,
folly::ByteRange toBeSigned);

static CertificateMsg getCertMessage(
const std::vector<folly::ssl::X509UniquePtr>& certs,
Buf certificateRequestContext);
Expand Down
10 changes: 5 additions & 5 deletions fizz/backend/openssl/certificate/OpenSSLSelfCertImpl-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ inline Buf OpenSSLSelfCertImpl<KeyType::P256>::sign(
SignatureScheme scheme,
CertificateVerifyContext context,
folly::ByteRange toBeSigned) const {
auto signData = CertUtils::prepareSignData(context, toBeSigned);
auto signData = fizz::certverify::prepareSignData(context, toBeSigned);
switch (scheme) {
case SignatureScheme::ecdsa_secp256r1_sha256:
return signature_.sign<SignatureScheme::ecdsa_secp256r1_sha256>(
Expand All @@ -94,7 +94,7 @@ inline Buf OpenSSLSelfCertImpl<KeyType::P384>::sign(
SignatureScheme scheme,
CertificateVerifyContext context,
folly::ByteRange toBeSigned) const {
auto signData = CertUtils::prepareSignData(context, toBeSigned);
auto signData = fizz::certverify::prepareSignData(context, toBeSigned);
switch (scheme) {
case SignatureScheme::ecdsa_secp384r1_sha384:
return signature_.sign<SignatureScheme::ecdsa_secp384r1_sha384>(
Expand All @@ -109,7 +109,7 @@ inline Buf OpenSSLSelfCertImpl<KeyType::P521>::sign(
SignatureScheme scheme,
CertificateVerifyContext context,
folly::ByteRange toBeSigned) const {
auto signData = CertUtils::prepareSignData(context, toBeSigned);
auto signData = fizz::certverify::prepareSignData(context, toBeSigned);
switch (scheme) {
case SignatureScheme::ecdsa_secp521r1_sha512:
return signature_.sign<SignatureScheme::ecdsa_secp521r1_sha512>(
Expand All @@ -124,7 +124,7 @@ inline Buf OpenSSLSelfCertImpl<KeyType::ED25519>::sign(
SignatureScheme scheme,
CertificateVerifyContext context,
folly::ByteRange toBeSigned) const {
auto signData = CertUtils::prepareSignData(context, toBeSigned);
auto signData = fizz::certverify::prepareSignData(context, toBeSigned);
switch (scheme) {
case SignatureScheme::ed25519:
return signature_.sign<SignatureScheme::ed25519>(signData->coalesce());
Expand All @@ -138,7 +138,7 @@ inline Buf OpenSSLSelfCertImpl<KeyType::RSA>::sign(
SignatureScheme scheme,
CertificateVerifyContext context,
folly::ByteRange toBeSigned) const {
auto signData = CertUtils::prepareSignData(context, toBeSigned);
auto signData = fizz::certverify::prepareSignData(context, toBeSigned);
switch (scheme) {
case SignatureScheme::rsa_pss_sha256:
return signature_.sign<SignatureScheme::rsa_pss_sha256>(
Expand Down
4 changes: 2 additions & 2 deletions fizz/extensions/javacrypto/JavaCryptoPeerCert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* LICENSE file in the root directory of this source tree.
*/

#include <fizz/backend/openssl/certificate/CertUtils.h>
#include <fizz/extensions/javacrypto/JavaCryptoPeerCert.h>
#include <fizz/extensions/javacrypto/JniUtils.h>
#include <fizz/protocol/Certificate.h>

namespace fizz {

Expand Down Expand Up @@ -71,7 +71,7 @@ void JavaCryptoPeerCert::verify(
throw std::runtime_error("Unsupported signature scheme");
}
auto jAlgorithm = env->NewStringUTF(algorithm.c_str());
auto signData = openssl::CertUtils::prepareSignData(context, toBeSigned);
auto signData = fizz::certverify::prepareSignData(context, toBeSigned);
auto jSignData = jni::createByteArray(env, std::move(signData));
auto jSignature = jni::createByteArray(env, signature);

Expand Down
53 changes: 53 additions & 0 deletions fizz/protocol/Certificate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,57 @@ std::string IdentityCert::getIdentity() const {
std::optional<std::string> IdentityCert::getDER() const {
return std::nullopt;
}

namespace certverify {

Buf prepareSignData(
CertificateVerifyContext context,
folly::ByteRange toBeSigned) {
static constexpr folly::StringPiece kServerLabel =
"TLS 1.3, server CertificateVerify";
static constexpr folly::StringPiece kClientLabel =
"TLS 1.3, client CertificateVerify";
static constexpr folly::StringPiece kAuthLabel = "Exported Authenticator";
static constexpr folly::StringPiece kServerDelegatedCredLabel =
"TLS, server delegated credentials";
static constexpr folly::StringPiece kClientDelegatedCredLabel =
"TLS, client delegated credentials";
static constexpr size_t kSigPrefixLen = 64;
static constexpr uint8_t kSigPrefix = 32;

folly::StringPiece label;
switch (context) {
case CertificateVerifyContext::Server:
label = kServerLabel;
break;
case CertificateVerifyContext::Client:
label = kClientLabel;
break;
case CertificateVerifyContext::Authenticator:
label = kAuthLabel;
break;
case CertificateVerifyContext::ClientDelegatedCredential:
label = kClientDelegatedCredLabel;
break;
case CertificateVerifyContext::ServerDelegatedCredential:
label = kServerDelegatedCredLabel;
break;
}

size_t sigDataLen = kSigPrefixLen + label.size() + 1 + toBeSigned.size();
auto buf = folly::IOBuf::create(sigDataLen);
buf->append(sigDataLen);

// Place bytes in the right order.
size_t offset = 0;
memset(buf->writableData(), kSigPrefix, kSigPrefixLen);
offset += kSigPrefixLen;
memcpy(buf->writableData() + offset, label.data(), label.size());
offset += label.size();
memset(buf->writableData() + offset, 0, 1);
offset += 1;
memcpy(buf->writableData() + offset, toBeSigned.data(), toBeSigned.size());
return buf;
}
} // namespace certverify
} // namespace fizz
12 changes: 12 additions & 0 deletions fizz/protocol/Certificate.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ enum class CertificateVerifyContext {
ServerDelegatedCredential
};

namespace certverify {

/**
* Adds the appropriate context data to prepare toBeSigned for a signature
* scheme's signing function.
*/
Buf prepareSignData(
CertificateVerifyContext context,
folly::ByteRange toBeSigned);

} // namespace certverify

class IdentityCert : public IdentityCertBase {
public:
explicit IdentityCert(std::string identity);
Expand Down
2 changes: 1 addition & 1 deletion fizz/protocol/test/CertTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ TEST(CertTest, GetCertMessage) {
TEST(CertTest, PrepareSignData) {
std::array<uint8_t, 32> toBeSigned;
memset(toBeSigned.data(), 1, toBeSigned.size());
auto out = openssl::CertUtils::prepareSignData(
auto out = fizz::certverify::prepareSignData(
CertificateVerifyContext::Server, folly::range(toBeSigned));
auto hex = hexlify(out->moveToFbString());
std::string expected =
Expand Down

0 comments on commit 27e4bc5

Please sign in to comment.