<commit_before>/*
 *  Copyright (c) 2017, 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. An additional grant
 *  of patent rights can be found in the PATENTS file in the same directory.
 *
 */
#include <wangle/acceptor/SSLAcceptorHandshakeHelper.h>

#include <string>
#include <wangle/acceptor/Acceptor.h>
#include <wangle/acceptor/SecureTransportType.h>

namespace wangle {

static const std::string empty_string;

using namespace folly;

void SSLAcceptorHandshakeHelper::start(
    folly::AsyncSSLSocket::UniquePtr sock,
    AcceptorHandshakeHelper::Callback* callback) noexcept {
  socket_ = std::move(sock);
  callback_ = callback;

  if (acceptor_->getParseClientHello()) {
    socket_->enableClientHelloParsing();
  }

  socket_->forceCacheAddrOnFailure(true);
  socket_->sslAccept(this);
}

void SSLAcceptorHandshakeHelper::fillSSLTransportInfoFields(
    AsyncSSLSocket* sock, TransportInfo& tinfo) {
  tinfo.secure = true;
  tinfo.securityType = sock->getSecurityProtocol();
  tinfo.sslSetupBytesRead = sock->getRawBytesReceived();
  tinfo.sslSetupBytesWritten = sock->getRawBytesWritten();
  tinfo.sslServerName = sock->getSSLServerName() ?
    std::make_shared<std::string>(sock->getSSLServerName()) : nullptr;
  tinfo.sslCipher = sock->getNegotiatedCipherName() ?
    std::make_shared<std::string>(sock->getNegotiatedCipherName()) : nullptr;
  tinfo.sslVersion = sock->getSSLVersion();
  const char* sigAlgName = sock->getSSLCertSigAlgName();
  tinfo.sslCertSigAlgName =
    std::make_shared<std::string>(sigAlgName ? sigAlgName : "");
  tinfo.sslCertSize = sock->getSSLCertSize();
  tinfo.sslResume = SSLUtil::getResumeState(sock);
  tinfo.sslClientCiphers = std::make_shared<std::string>();
  sock->getSSLClientCiphers(*tinfo.sslClientCiphers);
  tinfo.sslClientCiphersHex = std::make_shared<std::string>();
  sock->getSSLClientCiphers(
      *tinfo.sslClientCiphersHex, /* convertToString = */ false);
  tinfo.sslServerCiphers = std::make_shared<std::string>();
  sock->getSSLServerCiphers(*tinfo.sslServerCiphers);
  tinfo.sslClientComprMethods =
      std::make_shared<std::string>(sock->getSSLClientComprMethods());
  tinfo.sslClientExts =
      std::make_shared<std::string>(sock->getSSLClientExts());
  tinfo.sslClientSigAlgs =
      std::make_shared<std::string>(sock->getSSLClientSigAlgs());
  tinfo.sslClientSupportedVersions =
      std::make_shared<std::string>(sock->getSSLClientSupportedVersions());
}

void SSLAcceptorHandshakeHelper::handshakeSuc(AsyncSSLSocket* sock) noexcept {
  const unsigned char* nextProto = nullptr;
  unsigned nextProtoLength = 0;
  sock->getSelectedNextProtocol(&nextProto, &nextProtoLength);
  if (VLOG_IS_ON(3)) {
    if (nextProto) {
      VLOG(3) << "Client selected next protocol " <<
          std::string((const char*)nextProto, nextProtoLength);
    } else {
      VLOG(3) << "Client did not select a next protocol";
    }<commit_msg>Support older versions of OpenSSL<commit_after>/*
 *  Copyright (c) 2017, 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. An additional grant
 *  of patent rights can be found in the PATENTS file in the same directory.
 *
 */
#include <wangle/acceptor/SSLAcceptorHandshakeHelper.h>

#include <string>
#include <wangle/acceptor/Acceptor.h>
#include <wangle/acceptor/SecureTransportType.h>

namespace wangle {

static const std::string empty_string;

using namespace folly;

void SSLAcceptorHandshakeHelper::start(
    folly::AsyncSSLSocket::UniquePtr sock,
    AcceptorHandshakeHelper::Callback* callback) noexcept {
  socket_ = std::move(sock);
  callback_ = callback;

  if (acceptor_->getParseClientHello()) {
    socket_->enableClientHelloParsing();
  }

  socket_->forceCacheAddrOnFailure(true);
  socket_->sslAccept(this);
}

void SSLAcceptorHandshakeHelper::fillSSLTransportInfoFields(
    AsyncSSLSocket* sock, TransportInfo& tinfo) {
  tinfo.secure = true;
  tinfo.securityType = sock->getSecurityProtocol();
  tinfo.sslSetupBytesRead = sock->getRawBytesReceived();
  tinfo.sslSetupBytesWritten = sock->getRawBytesWritten();
  tinfo.sslServerName = sock->getSSLServerName() ?
    std::make_shared<std::string>(sock->getSSLServerName()) : nullptr;
  tinfo.sslCipher = sock->getNegotiatedCipherName() ?
    std::make_shared<std::string>(sock->getNegotiatedCipherName()) : nullptr;
  tinfo.sslVersion = sock->getSSLVersion();
  const char* sigAlgName = sock->getSSLCertSigAlgName();
  tinfo.sslCertSigAlgName =
    std::make_shared<std::string>(sigAlgName ? sigAlgName : "");
  tinfo.sslCertSize = sock->getSSLCertSize();
  tinfo.sslResume = SSLUtil::getResumeState(sock);
  tinfo.sslClientCiphers = std::make_shared<std::string>();
  sock->getSSLClientCiphers(*tinfo.sslClientCiphers);
  tinfo.sslClientCiphersHex = std::make_shared<std::string>();
  sock->getSSLClientCiphers(
      *tinfo.sslClientCiphersHex, /* convertToString = */ false);
  tinfo.sslServerCiphers = std::make_shared<std::string>();
  sock->getSSLServerCiphers(*tinfo.sslServerCiphers);
  tinfo.sslClientComprMethods =
      std::make_shared<std::string>(sock->getSSLClientComprMethods());
  tinfo.sslClientExts =
      std::make_shared<std::string>(sock->getSSLClientExts());
  tinfo.sslClientSigAlgs =
      std::make_shared<std::string>(sock->getSSLClientSigAlgs());
  tinfo.sslClientSupportedVersions =
      std::make_shared<std::string>(sock->getSSLClientSupportedVersions());
}

void SSLAcceptorHandshakeHelper::handshakeSuc(AsyncSSLSocket* sock) noexcept {
  const unsigned char* nextProto = nullptr;
  unsigned nextProtoLength = 0;
  sock->getSelectedNextProtocolNoThrow(&nextProto, &nextProtoLength);
  if (VLOG_IS_ON(3)) {
    if (nextProto) {
      VLOG(3) << "Client selected next protocol " <<
          std::string((const char*)nextProto, nextProtoLength);
    } else {
      VLOG(3) << "Client did not select a next protocol";
    }
{'fraction_non_alphanumeric': 0.12695280381516197, 'fraction_numerical': 0.002631146193060352, 'mean_word_length': 5.5327604726100965, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '970191', 'n_tokens_mistral': 1952, 'n_tokens_neox': 1936, 'n_words': 346}