123456789_123456789_123456789_123456789_123456789_

Class: Puma::MiniSSL::SSLContext

Relationships & Source Files
Inherits: Object
Defined in: ext/puma_http11/mini_ssl.c

Class Method Summary

Constructor Details

.new(mini_ssl_ctx)

[ GitHub ]

  
# File 'ext/puma_http11/mini_ssl.c', line 202

VALUE
sslctx_initialize(VALUE self, VALUE mini_ssl_ctx) {
  SSL_CTX* ctx;

#ifdef HAVE_SSL_CTX_SET_MIN_PROTO_VERSION
  int min;
#endif
  int ssl_options;
  VALUE key, cert, ca, verify_mode, ssl_cipher_filter, no_tlsv1, no_tlsv1_1,
    verification_flags;
  DH *dh;

#if OPENSSL_VERSION_NUMBER < 0x10002000L
  EC_KEY *ecdh;
#endif

  TypedData_Get_Struct(self, SSL_CTX, &sslctx_type, ctx);

  key = rb_funcall(mini_ssl_ctx, rb_intern_const("key"), 0);
  StringValue(key);

  cert = rb_funcall(mini_ssl_ctx, rb_intern_const("cert"), 0);
  StringValue(cert);

  ca = rb_funcall(mini_ssl_ctx, rb_intern_const("ca"), 0);

  verify_mode = rb_funcall(mini_ssl_ctx, rb_intern_const("verify_mode"), 0);

  ssl_cipher_filter = rb_funcall(mini_ssl_ctx, rb_intern_const("ssl_cipher_filter"), 0);

  no_tlsv1 = rb_funcall(mini_ssl_ctx, rb_intern_const("no_tlsv1"), 0);

  no_tlsv1_1 = rb_funcall(mini_ssl_ctx, rb_intern_const("no_tlsv1_1"), 0);

  SSL_CTX_use_certificate_chain_file(ctx, RSTRING_PTR(cert));
  SSL_CTX_use_PrivateKey_file(ctx, RSTRING_PTR(key), SSL_FILETYPE_PEM);

  verification_flags = rb_funcall(mini_ssl_ctx, rb_intern_const("verification_flags"), 0);

  if (!NIL_P(verification_flags)) {
    X509_VERIFY_PARAM *param = SSL_CTX_get0_param(ctx);
    X509_VERIFY_PARAM_set_flags(param, NUM2INT(verification_flags));
    SSL_CTX_set1_param(ctx, param);
  }

  if (!NIL_P(ca)) {
    StringValue(ca);
    SSL_CTX_load_verify_locations(ctx, RSTRING_PTR(ca), NULL);
  }

  ssl_options = SSL_OP_CIPHER_SERVER_PREFERENCE | SSL_OP_SINGLE_ECDH_USE | SSL_OP_NO_COMPRESSION;

#ifdef HAVE_SSL_CTX_SET_MIN_PROTO_VERSION
  if (RTEST(no_tlsv1_1)) {
    min = TLS1_2_VERSION;
  }
  else if (RTEST(no_tlsv1)) {
    min = TLS1_1_VERSION;
  }
  else {
    min = TLS1_VERSION;
  }

  SSL_CTX_set_min_proto_version(ctx, min);

  SSL_CTX_set_options(ctx, ssl_options);

#else
  /* As of 1.0.2f, SSL_OP_SINGLE_DH_USE key use is always on */
  ssl_options |= SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_SINGLE_DH_USE;

  if (RTEST(no_tlsv1)) {
    ssl_options |= SSL_OP_NO_TLSv1;
  }
  if(RTEST(no_tlsv1_1)) {
    ssl_options |= SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1;
  }
  SSL_CTX_set_options(ctx, ssl_options);
#endif

  SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);

  if (!NIL_P(ssl_cipher_filter)) {
    StringValue(ssl_cipher_filter);
    SSL_CTX_set_cipher_list(ctx, RSTRING_PTR(ssl_cipher_filter));
  }
  else {
    SSL_CTX_set_cipher_list(ctx, "HIGH:!aNULL@STRENGTH");
  }

  dh = get_dh2048();
  SSL_CTX_set_tmp_dh(ctx, dh);

#if OPENSSL_VERSION_NUMBER < 0x10002000L
  // Remove this case if OpenSSL 1.0.1 (now EOL) support is no
  // longer needed.
  ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  if (ecdh) {
    SSL_CTX_set_tmp_ecdh(ctx, ecdh);
    EC_KEY_free(ecdh);
  }
#elif OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
  SSL_CTX_set_ecdh_auto(ctx, 1);
#endif

  if (NIL_P(verify_mode)) {
    /* SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL); */
  } else {
    SSL_CTX_set_verify(ctx, NUM2INT(verify_mode), engine_verify_callback);
  }
  // printf("\ninitialize end security_level %d\n", SSL_CTX_get_security_level(ctx));
  rb_obj_freeze(self);
  return self;
}