|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# This file can be loaded by RubyGems test-unit files and Bundler rspec files. |
| 4 | +# Don't add test-unit or rspec dependent logic in this file. |
| 5 | + |
| 6 | +require "socket" |
| 7 | +require "openssl" |
| 8 | + |
| 9 | +module Gem::LocalSSLServerUtilities |
| 10 | + CERTS_DIR = __dir__ |
| 11 | + |
| 12 | + def certs_dir |
| 13 | + CERTS_DIR |
| 14 | + end |
| 15 | + |
| 16 | + def initialize_ssl_server |
| 17 | + @ssl_server_thread = nil |
| 18 | + @ssl_server = nil |
| 19 | + end |
| 20 | + |
| 21 | + def stop_ssl_server |
| 22 | + if @ssl_server_thread |
| 23 | + @ssl_server_thread.kill.join |
| 24 | + @ssl_server_thread = nil |
| 25 | + end |
| 26 | + if @ssl_server |
| 27 | + @ssl_server.close |
| 28 | + @ssl_server = nil |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + # mode: |
| 33 | + # :non_pqc - Run single server with PQC-unsupported RSA (default) |
| 34 | + # :pqc - Run single server with PQC-supported key exchange, |
| 35 | + # X25519MLKEM768, and PQC-supported certificate, ML-DSA-65 |
| 36 | + def start_ssl_server(config = {}) |
| 37 | + mode = config.fetch(:mode, :non_pqc) |
| 38 | + server = TCPServer.new(0) |
| 39 | + ctx = OpenSSL::SSL::SSLContext.new |
| 40 | + |
| 41 | + case mode |
| 42 | + when :non_pqc |
| 43 | + ctx.cert = cert("ssl_cert.pem") |
| 44 | + ctx.key = key("ssl_key.pem") |
| 45 | + ctx.ca_file = File.join(certs_dir, "ca_cert.pem") |
| 46 | + when :pqc |
| 47 | + ctx.cert = cert("mldsa65_ssl_cert.pem") |
| 48 | + ctx.key = key("mldsa65_ssl_key.pem") |
| 49 | + ctx.ca_file = File.join(certs_dir, "mldsa65_ca_cert.pem") |
| 50 | + ctx.groups = "X25519MLKEM768" |
| 51 | + end |
| 52 | + |
| 53 | + ctx.verify_mode = config[:verify_mode] if config[:verify_mode] |
| 54 | + @ssl_server = OpenSSL::SSL::SSLServer.new(server, ctx) |
| 55 | + @ssl_server_thread = Thread.new do |
| 56 | + loop do |
| 57 | + ssl_client = @ssl_server.accept |
| 58 | + Thread.new(ssl_client) do |client| |
| 59 | + handle_request(client) |
| 60 | + ensure |
| 61 | + client.close |
| 62 | + end |
| 63 | + rescue OpenSSL::SSL::SSLError |
| 64 | + # Ignore SSL errors because we're testing them implicitly |
| 65 | + end |
| 66 | + end |
| 67 | + @ssl_server |
| 68 | + end |
| 69 | + |
| 70 | + def handle_request(client) |
| 71 | + request = client.gets |
| 72 | + if request&.start_with?("GET /yaml") |
| 73 | + client.print "HTTP/1.1 200 OK\r\nContent-Type: text/yaml\r\n\r\n--- true\n" |
| 74 | + elsif request&.start_with?("GET /insecure_redirect") |
| 75 | + location = request.match(/to=([^ ]+)/)[1] |
| 76 | + client.print "HTTP/1.1 301 Moved Permanently\r\nLocation: #{location}\r\n\r\n" |
| 77 | + else |
| 78 | + client.print "HTTP/1.1 404 Not Found\r\n\r\n" |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + def cert(filename) |
| 83 | + OpenSSL::X509::Certificate.new(File.read(File.join(certs_dir, filename))) |
| 84 | + end |
| 85 | + |
| 86 | + def key(filename) |
| 87 | + OpenSSL::PKey.read(File.read(File.join(certs_dir, filename))) |
| 88 | + end |
| 89 | + |
| 90 | + def without_pqc_support(&block) |
| 91 | + # PQC algorithms ML-KEM and ML-DSA require OpenSSL >= 3.5. |
| 92 | + # https://openssl-library.org/post/2025-04-08-openssl-35-final-release/ |
| 93 | + unless OpenSSL::OPENSSL_VERSION_NUMBER >= 0x30500000 |
| 94 | + yield "PQC algorithms require OpenSSL >= 3.5" |
| 95 | + return |
| 96 | + end |
| 97 | + # ctx.groups (OpenSSL::SSL::SSLContext#groups) used in start_ssl_server |
| 98 | + # mode :pqc requires Ruby OpenSSL >= 4.0. |
| 99 | + unless Gem::Version.new(OpenSSL::VERSION) >= Gem::Version.new("4.0") |
| 100 | + yield "PQC test requires Ruby OpenSSL >= 4.0" |
| 101 | + return |
| 102 | + end |
| 103 | + # Even with a new enough OpenSSL, the runtime may keep PQC groups and |
| 104 | + # signature algorithms out of its default negotiation lists (for example |
| 105 | + # RHEL's system-wide crypto policies). The PQC server forces both, while |
| 106 | + # the gem fetcher connects with the default client configuration, so a |
| 107 | + # real loopback handshake is the only reliable way to tell whether this |
| 108 | + # environment can negotiate PQC at all. |
| 109 | + unless Gem::LocalSSLServerUtilities.support_pqc_handshake? |
| 110 | + yield "PQC handshake is not available in this OpenSSL configuration" |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + # Probe an actual PQC handshake between a forced-PQC server and a |
| 115 | + # default-configured client, mirroring what the integration tests exercise. |
| 116 | + # Memoized so the probe runs at most once per process. |
| 117 | + def self.support_pqc_handshake? |
| 118 | + return @support_pqc_handshake unless @support_pqc_handshake.nil? |
| 119 | + |
| 120 | + @support_pqc_handshake = probe_pqc_handshake |
| 121 | + end |
| 122 | + |
| 123 | + def self.probe_pqc_handshake |
| 124 | + server = TCPServer.new("127.0.0.1", 0) |
| 125 | + ctx = OpenSSL::SSL::SSLContext.new |
| 126 | + ctx.cert = OpenSSL::X509::Certificate.new(File.read(File.join(CERTS_DIR, "mldsa65_ssl_cert.pem"))) |
| 127 | + ctx.key = OpenSSL::PKey.read(File.read(File.join(CERTS_DIR, "mldsa65_ssl_key.pem"))) |
| 128 | + ctx.groups = "X25519MLKEM768" |
| 129 | + ssl_server = OpenSSL::SSL::SSLServer.new(server, ctx) |
| 130 | + |
| 131 | + port = server.addr[1] |
| 132 | + server_thread = Thread.new do |
| 133 | + client = ssl_server.accept |
| 134 | + client.close |
| 135 | + rescue OpenSSL::OpenSSLError |
| 136 | + nil |
| 137 | + end |
| 138 | + |
| 139 | + client_ctx = OpenSSL::SSL::SSLContext.new |
| 140 | + client_ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE |
| 141 | + socket = TCPSocket.new("127.0.0.1", port) |
| 142 | + ssl = OpenSSL::SSL::SSLSocket.new(socket, client_ctx) |
| 143 | + ssl.connect |
| 144 | + ssl.close |
| 145 | + true |
| 146 | + rescue OpenSSL::OpenSSLError, SystemCallError |
| 147 | + false |
| 148 | + ensure |
| 149 | + server_thread&.join(5) |
| 150 | + server_thread&.kill if server_thread&.alive? |
| 151 | + ssl_server&.close |
| 152 | + server&.close |
| 153 | + end |
| 154 | +end |
0 commit comments