Top Level Namespace

Defined Under Namespace

Modules: JWT

Constant Summary collapse

LIBOQS_VERSION =
"0.15.0"
LIBOQS_SHA256 =
"3983f7cd1247f37fb76a040e6fd684894d44a84cecdcfbdb90559b3216684b5c"
LIBOQS_URL =
"https://github.com/open-quantum-safe/liboqs/archive/refs/tags/#{LIBOQS_VERSION}.tar.gz"
PACKAGE_ROOT_DIR =
File.expand_path("../../..", __dir__)
VENDOR_DIR =
File.join(PACKAGE_ROOT_DIR, "lib", "jwt", "pq", "vendor")

Instance Method Summary collapse

Instance Method Details

#build_liboqs(source_dir) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'ext/jwt/pq/extconf.rb', line 119

def build_liboqs(source_dir)
  build_dir = File.join(source_dir, "build")
  FileUtils.mkdir_p(build_dir)
  FileUtils.mkdir_p(VENDOR_DIR)

  nproc = begin
    Etc.nprocessors
  rescue StandardError
    2
  end

  # Semicolons are cmake list separators (safe here — system() uses execvp, no shell)
  cmake_args = %W[
    -DBUILD_SHARED_LIBS=ON
    -DOQS_MINIMAL_BUILD=SIG_ml_dsa_44;SIG_ml_dsa_65;SIG_ml_dsa_87
    -DOQS_BUILD_ONLY_LIB=ON
    -DOQS_USE_OPENSSL=OFF
    -DCMAKE_BUILD_TYPE=Release
    -DCMAKE_INSTALL_PREFIX=#{VENDOR_DIR}
    -DCMAKE_POSITION_INDEPENDENT_CODE=ON
  ]

  # Use Ninja if available (faster), fall back to Make
  if find_executable("ninja")
    cmake_args << "-GNinja"
  end

  $stdout.puts "jwt-pq: configuring liboqs #{LIBOQS_VERSION} (ML-DSA only)..."
  unless system("cmake", "-S", source_dir, "-B", build_dir, *cmake_args)
    abort "ERROR: cmake configure failed for liboqs"
  end

  $stdout.puts "jwt-pq: compiling liboqs (using #{nproc} cores)..."
  unless system("cmake", "--build", build_dir, "--parallel", nproc.to_s)
    abort "ERROR: cmake build failed for liboqs"
  end

  $stdout.puts "jwt-pq: installing liboqs to vendor directory..."
  unless system("cmake", "--install", build_dir)
    abort "ERROR: cmake install failed for liboqs"
  end
end

#check_cmake!Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'ext/jwt/pq/extconf.rb', line 36

def check_cmake!
  cmake = find_executable("cmake")
  unless cmake
    abort <<~MSG
      ERROR: cmake is required to compile liboqs for jwt-pq.

      Install it with:
        macOS:  brew install cmake
        Ubuntu: sudo apt-get install cmake

      Alternatively, install liboqs manually and use:
        gem install jwt-pq -- --use-system-libraries
    MSG
  end
  cmake
end

#download_source(dest_dir) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'ext/jwt/pq/extconf.rb', line 69

def download_source(dest_dir)
  tarball_path = File.join(dest_dir, "liboqs-#{LIBOQS_VERSION}.tar.gz")

  # Support local tarball via env var (for air-gapped environments)
  source = ENV.fetch("JWT_PQ_LIBOQS_SOURCE", LIBOQS_URL)

  $stdout.puts "jwt-pq: downloading liboqs #{LIBOQS_VERSION}..."
  if source.start_with?("http")
    download_via_http(source, tarball_path)
  else
    FileUtils.cp(source, tarball_path)
  end

  # Verify checksum
  actual = Digest::SHA256.file(tarball_path).hexdigest
  unless actual == LIBOQS_SHA256
    abort "ERROR: SHA-256 mismatch for liboqs tarball.\n" \
          "  Expected: #{LIBOQS_SHA256}\n" \
          "  Got:      #{actual}"
  end

  tarball_path
end

#download_via_http(url, dest_path, redirect_limit = 5) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'ext/jwt/pq/extconf.rb', line 53

def download_via_http(url, dest_path, redirect_limit = 5)
  abort "ERROR: too many redirects downloading liboqs" if redirect_limit.zero?

  uri = URI.parse(url)
  response = Net::HTTP.get_response(uri)

  case response
  when Net::HTTPSuccess
    File.binwrite(dest_path, response.body)
  when Net::HTTPRedirection
    download_via_http(response["location"], dest_path, redirect_limit - 1)
  else
    abort "ERROR: failed to download liboqs (HTTP #{response.code}): #{uri}"
  end
end

#extract_tarball(tarball_path, dest_dir) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'ext/jwt/pq/extconf.rb', line 93

def extract_tarball(tarball_path, dest_dir)
  $stdout.puts "jwt-pq: extracting liboqs #{LIBOQS_VERSION}..."
  File.open(tarball_path, "rb") do |file|
    Zlib::GzipReader.wrap(file) do |gz|
      Gem::Package::TarReader.new(gz) do |tar|
        tar.each do |entry|
          dest = File.expand_path(File.join(dest_dir, entry.full_name))
          unless dest.start_with?("#{File.expand_path(dest_dir)}/")
            abort "ERROR: path traversal detected in tarball: #{entry.full_name}"
          end

          if entry.directory?
            FileUtils.mkdir_p(dest)
          elsif entry.file?
            FileUtils.mkdir_p(File.dirname(dest))
            File.binwrite(dest, entry.read)
            File.chmod(entry.header.mode, dest) if entry.header.mode && entry.header.mode > 0
          end
        end
      end
    end
  end

  File.join(dest_dir, "liboqs-#{LIBOQS_VERSION}")
end

#find_vendored_libraryObject



162
163
164
165
166
167
168
# File 'ext/jwt/pq/extconf.rb', line 162

def find_vendored_library
  %w[dylib so].each do |ext|
    path = File.join(VENDOR_DIR, "lib", "liboqs.#{ext}")
    return path if File.exist?(path)
  end
  nil
end

#use_system_libraries?Boolean

Returns:

  • (Boolean)


31
32
33
34
# File 'ext/jwt/pq/extconf.rb', line 31

def use_system_libraries?
  ENV.key?("JWT_PQ_USE_SYSTEM_LIBRARIES") ||
    enable_config("system-libraries", false)
end

#write_dummy_makefileObject



20
21
22
23
24
25
26
27
28
29
# File 'ext/jwt/pq/extconf.rb', line 20

def write_dummy_makefile
  File.write("Makefile", <<~MAKEFILE)
    all:
    \t@echo "jwt-pq: nothing to compile"
    install:
    \t@echo "jwt-pq: nothing to install"
    clean:
    \t@echo "jwt-pq: nothing to clean"
  MAKEFILE
end