<commit_before>package com.xjeffrose.xio.SSL;

import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.SupportedCipherSuiteFilter;
import java.security.cert.*;
import javax.net.ssl.SSLException;

public class SslContextFactory {

  static private SslContextBuilder configure(TlsConfig config, SslContextBuilder builder) {
    return builder
      .applicationProtocolConfig(config.getAlpnConfig())
      .ciphers(config.getCiphers(), SupportedCipherSuiteFilter.INSTANCE)
      .clientAuth(config.getClientAuth())
      .enableOcsp(config.isEnableOcsp())
      .protocols(config.getProtocols())
      .sessionCacheSize(config.getSessionCacheSize())
      .sessionTimeout(config.getSessionTimeout())
      .sslProvider(config.getSslProvider())
      ;
  }

  static public SslContext buildServerContext(TlsConfig config) {
    try {
      SslContextBuilder builder = SslContextBuilder
        .forServer(config.getPrivateKey(), config.getCertificateAndTrustChain());
      return configure(config, builder)
        // servers will trust only certs in trust chain
        .trustManager(config.getTrustChain())
        .build();
    } catch (SSLException e) {
      return null;
    }
  }

  static public SslContext buildClientContext(TlsConfig config) {
    try {
      return configure(config, SslContextBuilder.forClient())
        .keyManager(config.getPrivateKey(), config.getCertificateAndTrustChain())
        // clients will trust only certs in trust chain
        .trustManager(config.getTrustChain())
        .build();
    } catch (SSLException e) {
      return null;
    }
  }
}
<commit_msg>Copy netty cert TrustManager, allow users to specify TrustManager<commit_after>package com.xjeffrose.xio.SSL;

import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.SupportedCipherSuiteFilter;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLException;
import javax.net.ssl.TrustManagerFactory;

public class SslContextFactory {

  static private SslContextBuilder configure(TlsConfig config, SslContextBuilder builder) {
    return builder
      .applicationProtocolConfig(config.getAlpnConfig())
      .ciphers(config.getCiphers(), SupportedCipherSuiteFilter.INSTANCE)
      .clientAuth(config.getClientAuth())
      .enableOcsp(config.isEnableOcsp())
      .protocols(config.getProtocols())
      .sessionCacheSize(config.getSessionCacheSize())
      .sessionTimeout(config.getSessionTimeout())
      .sslProvider(config.getSslProvider())
      ;
  }

  static public SslContext buildServerContext(TlsConfig config) {
    try {
      SslContextBuilder builder = SslContextBuilder
        .forServer(config.getPrivateKey(), config.getCertificateAndTrustChain());
      return configure(config, builder)
        // servers will trust only certs in trust chain
        .trustManager(config.getTrustChain())
        .build();
    } catch (SSLException e) {
      return null;
    }
  }

  static public SslContext buildClientContext(TlsConfig config, TrustManagerFactory trustManager) {
    try {
      return configure(config, SslContextBuilder.forClient())
        .keyManager(config.getPrivateKey(), config.getCertificateAndTrustChain())
        // clients will trust only certs in trust chain
        .trustManager(trustManager)
        .build();
    } catch (SSLException e) {
      return null;
    }
  }

  static TrustManagerFactory buildTrustManagerFactory(X509Certificate[] certCollection) {
    try {
      KeyStore ks = KeyStore.getInstance("JKS");
      ks.load(null, null);

      int i = 1;
      for (X509Certificate cert: certCollection) {
        String alias = Integer.toString(i);
        ks.setCertificateEntry(alias, cert);
        i++;
      }

      TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());

      trustManagerFactory.init(ks);

      return trustManagerFactory;
    } catch (NoSuchAlgorithmException | CertificateException | KeyStoreException | IOException e) {
      return null;
    }
  }

  static public SslContext buildClientContext(TlsConfig config) {
    return buildClientContext(config, buildTrustManagerFactory(config.getTrustChain()));
  }
}

{'fraction_non_alphanumeric': 0.09501661129568106, 'fraction_numerical': 0.0022148394241417496, 'mean_word_length': 3.8506981740064448, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '978387', 'n_tokens_mistral': 1269, 'n_tokens_neox': 1232, 'n_words': 245}