cmake_minimum_required(VERSION 3.13)

project(gss-opaque
    VERSION 0.1.0
    DESCRIPTION "OPAQUE (RFC 9807) as a GSSAPI mechanism plug-in"
    LANGUAGES C)

include(GNUInstallDirs)

set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)

# Static libs participate in SHARED/MODULE links, so PIC everywhere.
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

add_compile_options(-Wall -Wextra -Wpedantic)

enable_testing()

option(BUILD_GSSAPI_PLUGIN
    "Build the GSSAPI mechanism plug-in (requires MIT Kerberos GSSAPI dev headers)."
    OFF)

option(OPAQUE_MEASUREMENT_TIMESTAMPS
    "Measurement build only: the plug-in appends CLOCK_REALTIME entry and exit readings of gss_accept_sec_context to /var/lib/opaque-measurement/accept-timestamps."
    OFF)

# ---------------------------------------------------------------------------
# Dependencies — direct only; transitive deps come via pkg-config
# Requires.private and the dynamic linker's DT_NEEDED entries.
# ---------------------------------------------------------------------------

find_package(PkgConfig REQUIRED)

pkg_check_modules(LIBOPAQUE IMPORTED_TARGET libopaque)
pkg_check_modules(SODIUM    REQUIRED IMPORTED_TARGET libsodium)
pkg_check_modules(ARGON2    REQUIRED IMPORTED_TARGET libargon2)

# Debian's libopaque-dev (1.0.1-4) installs opaque.h and libopaque.so but no
# libopaque.pc, so pkg-config cannot find it there. Locate the header and the
# library directly instead, and give the result the target name pkg-config would
# have created, so no consumer changes. This reproduces upstream's libopaque.pc,
# "Libs: -lopaque". liboprf's include path is deliberately not taken from
# liboprf.pc: Debian's copy declares -I/usr/local/include/oprf, a directory the
# package does not install, while opaque.h includes <oprf/toprf.h>, which the
# default include path already resolves.
if(NOT LIBOPAQUE_FOUND)
    find_path(LIBOPAQUE_HEADER_DIR opaque.h REQUIRED)
    find_library(LIBOPAQUE_LIBRARY opaque REQUIRED)
    add_library(PkgConfig::LIBOPAQUE INTERFACE IMPORTED)
    target_include_directories(PkgConfig::LIBOPAQUE INTERFACE ${LIBOPAQUE_HEADER_DIR})
    target_link_libraries(PkgConfig::LIBOPAQUE INTERFACE ${LIBOPAQUE_LIBRARY})
    set(LIBOPAQUE_INCLUDE_DIRS ${LIBOPAQUE_HEADER_DIR})
    set(LIBOPAQUE_VERSION "unknown: found without libopaque.pc")
endif()
message(STATUS "libopaque: ${LIBOPAQUE_VERSION}")
message(STATUS "libsodium: ${SODIUM_VERSION}")
message(STATUS "libargon2: ${ARGON2_VERSION}")

if(BUILD_GSSAPI_PLUGIN)
    pkg_check_modules(GSSAPI REQUIRED IMPORTED_TARGET krb5-gssapi)
    message(STATUS "GSSAPI (MIT): ${GSSAPI_LIBRARIES}")
endif()

# ---------------------------------------------------------------------------
# Subprojects
# ---------------------------------------------------------------------------

add_subdirectory(libopaque)
add_subdirectory(driver)
add_subdirectory(compliance)
if(BUILD_GSSAPI_PLUGIN)
    add_subdirectory(gss-opaque)
    add_subdirectory(opaque-keytab)
    add_subdirectory(opaque-register)
    add_subdirectory(opaque-ssh)
    add_subdirectory(opaque-unregister)
    add_subdirectory(opaque-passwd)

    # T-14: the installed binaries import none of libopaque's one-lane Argon2 or
    # server-side-password entry points (RFC 9807 section 7).
    set(INSTALLED_BINARIES
        "$<TARGET_FILE:gssopaque>|$<TARGET_FILE:opaque-keytab>|$<TARGET_FILE:opaque-register>|$<TARGET_FILE:opaque-ssh>|$<TARGET_FILE:opaque-unregister>|$<TARGET_FILE:opaque-passwd>")
    add_test(NAME plugin_uses_only_rfc9807_key_stretching_entry_points
        COMMAND ${CMAKE_COMMAND} -DNM=${CMAKE_NM} -DPLUGIN=$<TARGET_FILE:gssopaque>
                -DBINARIES=${INSTALLED_BINARIES}
                -P ${CMAKE_SOURCE_DIR}/driver/check-libopaque-entry-points.cmake)

    # Manual pages in nroff source form (Debian Policy 12.1); the packaging compresses them.
    install(FILES man/opaque-ssh.1 man/opaque-register.1 man/opaque-passwd.1 man/opaque-unregister.1
        DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
    install(FILES man/opaque-files.5 DESTINATION ${CMAKE_INSTALL_MANDIR}/man5)
    install(FILES man/opaque-keytab.8 man/gssopaque.8 DESTINATION ${CMAKE_INSTALL_MANDIR}/man8)
endif()
