rust-libteec 0.6.4

Rust implementation of TEE Client API for secure communication with Trusted Applications.
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2026 KylinSoft Co., Ltd. <https://www.kylinos.cn/>
// See LICENSES for license details.

// 通过 dlopen 动态加载 libcc_teec,调用 cc_check_enable 检查机密通信服务端是否可用。
//
// 用法:
//   ./cc-check-enable [库路径]
// 库路径缺省为 libcc_teec.so(通过 LD_LIBRARY_PATH 指定搜索目录)。
//
// 运行环境说明:
//   cc_check_enable 先读取 /opt/x-kernel/.enable(内容须为 "1"),
//   文件缺失或内容非 1 时直接返回 -1,不发起连接。

#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>

typedef int (*cc_check_enable_fn)(void);

int main(int argc, char **argv)
{
    const char *lib_path = argc > 1 ? argv[1] : "libcc_teec.so";

    void *handle = dlopen(lib_path, RTLD_NOW);
    if (handle == NULL) {
        fprintf(stderr, "dlopen(%s) 失败: %s\n", lib_path, dlerror());
        return 2;
    }
    dlerror();
    cc_check_enable_fn cc_check_enable =
        (cc_check_enable_fn)dlsym(handle, "cc_check_enable");
    const char *sym_err = dlerror();
    if (sym_err != NULL) {
        fprintf(stderr, "dlsym(cc_check_enable) 失败: %s\n", sym_err);
        dlclose(handle);
        return 2;
    }

    int rc = cc_check_enable();
    const char *desc;
    switch (rc) {
    case 1:
        desc = "机密通信可用(服务端在,TLS 握手成功)";
        break;
    case 0:
        desc = "可信子系统存在但通信失败";
        break;
    default:
        desc = "可信子系统不可用(/opt/x-kernel/.enable 缺失或内容非 1)";
        break;
    }
    printf("cc_check_enable = %d  (%s)\n", rc, desc);

    dlclose(handle);
    return rc < 0 ? 1 : rc;
}