简体   繁体   English

设置SSL-初始步骤

[英]Setting Up SSL - Initial steps

I'm trying to convert my sockets program over to SSL. 我正在尝试将套接字程序转换为SSL。 I'm just laying out the initial setup, and have come upon some run-time errors that I don't know how to resolve. 我只是在布局初始设置,遇到了一些我不知道如何解决的运行时错误。 Note: there is a file called 'my_server.pem' in the same directory as the c file. 注意:在与c文件相同的目录中有一个名为“ my_server.pem”的文件。

Can anyone help me? 谁能帮我? Thanks! 谢谢!

#include <openssl/bio.h> // BIO objects for I/O
#include <openssl/ssl.h> // SSL and SSL_CTX for SSL connections
#include <openssl/err.h> // Error reporting

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

    // data structures for SSL
    BIO *bio;
    SSL *ssl;
    SSL_CTX *ctx;

    // initialize openSSL
    SSL_load_error_strings();
    ERR_load_BIO_strings();
    OpenSSL_add_all_algorithms();

    // set up the SSL context
    ctx = SSL_CTX_new(SSLv23_client_method());

    if (ctx == NULL)
        fprintf(stderr, "context is null\n");

    // load the trust store
    if (! SSL_CTX_load_verify_locations(ctx, "my_server.pem", NULL)) {
        fprintf(stderr, "Error loading trust store\n");
        ERR_print_errors_fp(stderr);
        SSL_CTX_free(ctx);
        return 0;
    }
    fprintf(stderr, "made it\n");

    //...

    return 0;
}

These are the error messages: 这些是错误消息:

140735285130080:error:02001002:system library:fopen:No such file or directory:bss_file.c:169:fopen('my_server.pem','r')
140735285130080:error:2006D080:BIO routines:BIO_new_file:no such file:bss_file.c:172:
140735285130080:error:0B084002:x509 certificate routines:X509_load_cert_crl_file:system lib:by_file.c:274:

From the error, it looks like it is unable to locate my_server.pem file. 根据该错误,似乎无法找到my_server.pem文件。 Even though it is in the same location of your C-code, it will be accessible to your program as it is by its name when it will be present in the working directory. 即使它位于C代码的相同位置,当程序出现在工作目录中时,也可以按其名称对其进行访问。

So, either move the file to working directory of the application or give a relative or absolute path to the file which points to this file. 因此,可以将文件移动到应用程序的工作目录中,或者提供指向该文件的相对或绝对路径。

To diagnose further, open your file with fopen in read mode and see the last error if it fails. 为了进一步诊断,请在读取模式下使用fopen打开文件,如果失败,请查看最后一个错误。

If your fopen is able to open the file, then there is some other problem. 如果您的fopen能够打开文件,则还有其他问题。

Try provide full path instead of "my_server.pem" example here . 尝试在此处提供完整路径,而不是"my_server.pem" 示例

Or alternatively, try put null instead of "my_server.pem" and put path to the directory instead of null refernce here . 或者,尝试将null而不是"my_server.pem"放在这里, 并在目录中放置路径而不是null 引用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM