Skip to content

Commit

Permalink
split out HandshakeLogging
Browse files Browse the repository at this point in the history
Reviewed By: mingtaoy

Differential Revision: D66243048

fbshipit-source-id: e05755ced88b958681d153a41c7b5f46f482bf3c
  • Loading branch information
Yang Wang authored and facebook-github-bot committed Dec 5, 2024
1 parent c3ff89c commit 3af2d30
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 82 deletions.
4 changes: 3 additions & 1 deletion fizz/cmake/FizzSources.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @generated SignedSource<<6739c8027c4f0f51ab7bf5dcb36c5d96>>
# @generated SignedSource<<9637ef5067835d0574a6b268d63b71f1>>
#
# This file is generated file from `fizz/facebook/boilerplate.sh`.
# All manual changes will be lost.
Expand Down Expand Up @@ -91,6 +91,7 @@ set(
server/CookieCipher.cpp
server/FizzServer.cpp
server/FizzServerContext.cpp
server/HandshakeLogging.cpp
server/MultiServerExtensions.cpp
server/ReplayCache.cpp
server/ServerProtocol.cpp
Expand Down Expand Up @@ -252,6 +253,7 @@ set(
server/FizzServer-inl.h
server/FizzServer.h
server/FizzServerContext.h
server/HandshakeLogging.h
server/MultiServerExtensions.h
server/Negotiator.h
server/ReplayCache.h
Expand Down
14 changes: 14 additions & 0 deletions fizz/server/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ cpp_library(
],
exported_deps = [
":fizz_server_context",
":handshake_logging",
":resumption_state",
":server_extensions",
"//fizz/protocol:actions",
Expand Down Expand Up @@ -385,3 +386,16 @@ cpp_library(
":server_extensions",
],
)

cpp_library(
name = "handshake_logging",
srcs = [
"HandshakeLogging.cpp",
],
headers = [
"HandshakeLogging.h",
],
exported_deps = [
"//fizz/record:record",
],
)
75 changes: 75 additions & 0 deletions fizz/server/HandshakeLogging.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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/server/HandshakeLogging.h>

namespace fizz {
namespace server {

void HandshakeLogging::populateFromClientHello(const ClientHello& chlo) {
clientLegacyVersion = chlo.legacy_version;
auto supportedVersions = getExtension<SupportedVersions>(chlo.extensions);
if (supportedVersions) {
clientSupportedVersions = supportedVersions->versions;
}
clientCiphers = chlo.cipher_suites;
clientExtensions.clear();
for (const auto& extension : chlo.extensions) {
clientExtensions.push_back(extension.extension_type);
if (extension.extension_type == ExtensionType::test_extension &&
extension.extension_data->length() == 1) {
// Special extension we want to log the byte for
testExtensionByte = *extension.extension_data->data();
}
}
clientAlpns.clear();
auto alpn = getExtension<ProtocolNameList>(chlo.extensions);
if (alpn) {
for (auto& protocol : alpn->protocol_name_list) {
clientAlpns.push_back(protocol.name->to<std::string>());
}
}
auto sni = getExtension<ServerNameList>(chlo.extensions);
if (sni && !sni->server_name_list.empty()) {
clientSni = sni->server_name_list.front().hostname->to<std::string>();
}
auto supportedGroups = getExtension<SupportedGroups>(chlo.extensions);
if (supportedGroups) {
clientSupportedGroups = std::move(supportedGroups->named_group_list);
}

auto keyShare = getExtension<ClientKeyShare>(chlo.extensions);
if (keyShare && !clientKeyShares) {
std::vector<NamedGroup> shares;
for (const auto& entry : keyShare->client_shares) {
shares.push_back(entry.group);
}
clientKeyShares = std::move(shares);
}

auto exchangeModes = getExtension<PskKeyExchangeModes>(chlo.extensions);
if (exchangeModes) {
clientKeyExchangeModes = std::move(exchangeModes->modes);
}

auto clientSigSchemes = getExtension<SignatureAlgorithms>(chlo.extensions);
if (clientSigSchemes) {
clientSignatureAlgorithms =
std::move(clientSigSchemes->supported_signature_algorithms);
}

clientSessionIdSent =
chlo.legacy_session_id && !chlo.legacy_session_id->empty();
clientRandom = chlo.random;

if (chlo.originalEncoding.hasValue()) {
originalChloSize = chlo.originalEncoding.value()->computeChainDataLength();
}
}
} // namespace server
} // namespace fizz
36 changes: 36 additions & 0 deletions fizz/server/HandshakeLogging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.
*/

#pragma once

#include <fizz/record/Extensions.h>

namespace fizz {
namespace server {

struct HandshakeLogging {
folly::Optional<ProtocolVersion> clientLegacyVersion;
std::vector<ProtocolVersion> clientSupportedVersions;
std::vector<CipherSuite> clientCiphers;
std::vector<ExtensionType> clientExtensions;
folly::Optional<ProtocolVersion> clientRecordVersion;
folly::Optional<std::string> clientSni;
std::vector<NamedGroup> clientSupportedGroups;
folly::Optional<std::vector<NamedGroup>> clientKeyShares;
std::vector<PskKeyExchangeMode> clientKeyExchangeModes;
std::vector<SignatureScheme> clientSignatureAlgorithms;
folly::Optional<bool> clientSessionIdSent;
folly::Optional<Random> clientRandom;
folly::Optional<uint8_t> testExtensionByte;
std::vector<std::string> clientAlpns;
size_t originalChloSize{0};

void populateFromClientHello(const ClientHello& chlo);
};
} // namespace server
} // namespace fizz
61 changes: 0 additions & 61 deletions fizz/server/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,67 +11,6 @@
namespace fizz {
namespace server {

void HandshakeLogging::populateFromClientHello(const ClientHello& chlo) {
clientLegacyVersion = chlo.legacy_version;
auto supportedVersions = getExtension<SupportedVersions>(chlo.extensions);
if (supportedVersions) {
clientSupportedVersions = supportedVersions->versions;
}
clientCiphers = chlo.cipher_suites;
clientExtensions.clear();
for (const auto& extension : chlo.extensions) {
clientExtensions.push_back(extension.extension_type);
if (extension.extension_type == ExtensionType::test_extension &&
extension.extension_data->length() == 1) {
// Special extension we want to log the byte for
testExtensionByte = *extension.extension_data->data();
}
}
clientAlpns.clear();
auto alpn = getExtension<ProtocolNameList>(chlo.extensions);
if (alpn) {
for (auto& protocol : alpn->protocol_name_list) {
clientAlpns.push_back(protocol.name->to<std::string>());
}
}
auto sni = getExtension<ServerNameList>(chlo.extensions);
if (sni && !sni->server_name_list.empty()) {
clientSni = sni->server_name_list.front().hostname->to<std::string>();
}
auto supportedGroups = getExtension<SupportedGroups>(chlo.extensions);
if (supportedGroups) {
clientSupportedGroups = std::move(supportedGroups->named_group_list);
}

auto keyShare = getExtension<ClientKeyShare>(chlo.extensions);
if (keyShare && !clientKeyShares) {
std::vector<NamedGroup> shares;
for (const auto& entry : keyShare->client_shares) {
shares.push_back(entry.group);
}
clientKeyShares = std::move(shares);
}

auto exchangeModes = getExtension<PskKeyExchangeModes>(chlo.extensions);
if (exchangeModes) {
clientKeyExchangeModes = std::move(exchangeModes->modes);
}

auto clientSigSchemes = getExtension<SignatureAlgorithms>(chlo.extensions);
if (clientSigSchemes) {
clientSignatureAlgorithms =
std::move(clientSigSchemes->supported_signature_algorithms);
}

clientSessionIdSent =
chlo.legacy_session_id && !chlo.legacy_session_id->empty();
clientRandom = chlo.random;

if (chlo.originalEncoding.hasValue()) {
originalChloSize = chlo.originalEncoding.value()->computeChainDataLength();
}
}

folly::StringPiece toString(StateEnum state) {
switch (state) {
case StateEnum::Uninitialized:
Expand Down
21 changes: 1 addition & 20 deletions fizz/server/State.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <fizz/record/RecordLayer.h>
#include <fizz/server/Actions.h>
#include <fizz/server/FizzServerContext.h>
#include <fizz/server/HandshakeLogging.h>
#include <fizz/server/ResumptionState.h>
#include <fizz/server/ServerExtensions.h>

Expand Down Expand Up @@ -51,26 +52,6 @@ struct ECHState {
folly::Optional<std::string> outerSni;
};

struct HandshakeLogging {
folly::Optional<ProtocolVersion> clientLegacyVersion;
std::vector<ProtocolVersion> clientSupportedVersions;
std::vector<CipherSuite> clientCiphers;
std::vector<ExtensionType> clientExtensions;
folly::Optional<ProtocolVersion> clientRecordVersion;
folly::Optional<std::string> clientSni;
std::vector<NamedGroup> clientSupportedGroups;
folly::Optional<std::vector<NamedGroup>> clientKeyShares;
std::vector<PskKeyExchangeMode> clientKeyExchangeModes;
std::vector<SignatureScheme> clientSignatureAlgorithms;
folly::Optional<bool> clientSessionIdSent;
folly::Optional<Random> clientRandom;
folly::Optional<uint8_t> testExtensionByte;
std::vector<std::string> clientAlpns;
size_t originalChloSize{0};

void populateFromClientHello(const ClientHello& chlo);
};

/**
* Validator interface that application can set to check app token.
*/
Expand Down

0 comments on commit 3af2d30

Please sign in to comment.