c++ - 错误 : cannot convert ‘std::string’ to ‘X509*’ for argument ‘1’ to ‘EVP_PKEY* X509_get_pubkey(X509*)’

标签 c++ ssl openssl ssl-certificate

我想验证证书的格式是否有效,所以我写了这段代码,但似乎有错误

if (validConfigFile()) {    
    INIReader reader(CONFIG_FILE);
    string certFile= reader.Get("Server", "cert-file", "None");
    if (certFile == "None") {
        syslog(LOG_ERR, "cert-file in configuration file is not properly adjusted");
        exit(EXIT_FAILURE); }
    else {
        std::string fileName(certFile);
        ifstream fin(fileName.c_str());
        if(fin.fail()) {
            syslog(LOG_ERR, "cert-file not found in its file path");
            exit(EXIT_FAILURE); }
        else {
            OpenSSL_add_all_digests();
            SSL_CTX *sslctx = SSL_CTX_new(SSLv23_server_method());
            SSL_CTX_use_certificate_file(sslctx, certFile, SSL_FILETYPE_PEM);
            SSL *ssl = SSL_new(sslctx);
            X509 *CERT = SSL_get_certificate(ssl);
            if (X509_verify(CERT,X509_get_pubkey(CERT)) == -1) {
                syslog(LOG_ERR, "cert-file not valid");
                exit(EXIT_FAILURE); }
        }
    }
}

编译时出现这个错误

/main.cpp:68: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘2’ to ‘int SSL_CTX_use_certificate_file(SSL_CTX*, const char*, int)’

还有

main.cpp:70: error: expected unqualified-id before ‘char’ for that line  : X509 *CERT = SSL_get_certificate(ssl);

最佳答案

std::string 没有隐式转换为 char*(C 风格的字符串),因此请改用方法 c_str():

SSL_CTX_use_certificate_file(sslctx, certFile.c_str(), SSL_FILETYPE_PEM);

关于c++ - 错误 : cannot convert ‘std::string’ to ‘X509*’ for argument ‘1’ to ‘EVP_PKEY* X509_get_pubkey(X509*)’ ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11631428/

相关文章:

c++ - 考虑什么类型的宏?

python - C++ - Python 与 ctypes 的绑定(bind) - 在函数中返回多个值

c++ - vector 中的重复元素

iis - IIS 5.1 中的 HTTPS

windows - 构建 OpenSSL x64 静态库时出现错误 "error A2009: syntax error in expression"

c++ - 使用 QTcpSocket 和 QTcpServer 的控制台聊天

asp.net - Chrome ERR_HTTP2_PROTOCOL_ERROR + Firefox 安全连接失败

java - 如何使用java从现有 keystore 生成csr

node.js - Nodejs createCipher 与 createCipheriv

Android NDK 链接 OpenSSL