Openssl SSL_CTX_new 成功,但將 errno 設定為 ENOSYS(功能未實現)

Openssl SSL_CTX_new 成功,但將 errno 設定為 ENOSYS(功能未實現)

SSL_CTX_new() 函數運作正常,但有些奇怪,它將 errno 設為 ENOSYS。

文件對此沒有任何說明: https://www.openssl.org/docs/man1.1.1/man3/SSL_CTX_new.html

#include <errno.h>
#include <openssl/ssl.h>

int main(int argc, char * argv[]){

    SSL_library_init();
    SSL_load_error_strings();

    perror("A");

    SSL_CTX * sslContext = SSL_CTX_new(TLS_server_method());

    if (sslContext == NULL){
        return EXIT_FAILURE;
    }

    perror("B");

    return 0;
}

此程式碼返回此:

A: Undefined error: 0
B: Function not implemented

我的 openssl 版本是:OpenSSL 1.1.1b 2019 年 2 月 26 日

答案1

並非所有函式庫都使用 errno 來報告狀態。如果文件沒有明確說明此函數設定 errno,則最好將該值視為不明確的:該函數可能會保留原始值,或將其設為 0,或在其中留下一些垃圾。

事實上,OpenSSL 文件並沒有提及任何有關 errno 的資訊;相反,該庫有自己的“錯誤堆疊”,您可以使用它來訪問ERR_get_error(),這比 libc 提供的非常有限的集合提供了更精確的錯誤代碼和訊息。

因此,您在 errno 中找到的值只是 SSL_CTX_new() 使用的某些或其他 libc 函數的剩餘部分內部。 (例如,它可能嘗試透過呼叫 libc getrandom() 來為 RNG 播種,然後由於缺乏核心支援而傳回 ENOSYS。)

相關內容