簡體   English   中英

如果我為 linux 或 windows 10 或更高版本申請,如何強制 OpenSSL 使用我系統的根 CA?

[英]how I can enforce OpenSSL to use my system's root CA if I make my application for linux or for windows 10 or later?

我正在制作以下代碼:

#include <stdio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>

#include <shlwapi.h>

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)

#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS


#include <winsock2.h>
#include <ws2tcpip.h>
#include <shlwapi.h>

// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#pragma comment (lib, "Ws2_32.lib")
//#pragma comment (lib, "Mswsock.lib")
//#pragma comment (lib, "AdvApi32.lib")


#define WIN32_LEAN_AND_MEAN


int verifyCerts( SSL_CTX* ctx )
{
    // directory where executable is
    char path[MAX_PATH] = "";
    
    memset(path, 0, MAX_PATH);
    GetModuleFileName(0, path, MAX_PATH);
    PathRemoveFileSpec(path);

    sprintf(path,"%s\\%s",path,"certificates");
    printf("\nCert path %s\n",path);
    sprintf(path,"%s\\%s",path,"certificates");
    printf("\nCert path %s\n",path);
    int value = SSL_CTX_load_verify_locations(ctx,NULL,path);
}

#else // By default use system's CA root

int verifyCerts( SSL_CTX* ctx )
{
       
}

#endif

SSL_CTX* InitCTX(void)
{
    OpenSSL_add_all_algorithms(); 
    SSL_load_error_strings();
    const SSL_METHOD* method = SSLv23_method();
    SSL_CTX* ctx = SSL_CTX_new(method);
    SSL_CTX_set_options(ctx, SSL_OP_ALL | SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1);
    
    if (ctx == NULL)
    {
        ERR_print_errors_fp(stderr);
        abort();
    }
   
    int value = verifyCerts( ctx );
    if(value == 0) {
        printf("Certificate error\n");
        exit(1);
    }

    SSL_CTX_set_verify(ctx,SSL_VERIFY_PEER,NULL);

    return ctx;
}

就我而言,我想做以下事情:

  • 對於 windows XP(舊版應用程序)在我的應用程序旁邊提供證書,以便我可以盡可能安全地制作我的應用程序。
  • 在任何其他情況下(對於 linux 系統或 windows 10 或更高版本),我將使用操作系統的默認 CA 證書(無需麻煩提供我自己的證書)。

那么我如何確保后一種情況也適用呢?

#else部分只需放置以下代碼:

int verifyCerts( SSL_CTX* ctx )
{    
    const char *path = getenv(X509_get_default_cert_dir_env());

    if (!path){
        path = X509_get_default_cert_dir();
    }

    return SSL_CTX_load_verify_locations(ctx,NULL,path);
}

這將允許 linux 系統使用默認證書路徑進行驗證。 因此,對於 Wonders XP,我們只能使用自定義 mingw 標志。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM