0

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

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);
4

3 回答 3

2

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

SSL_CTX_use_certificate_file(sslctx, certFile.c_str(), SSL_FILETYPE_PEM);
于 2012-07-24T13:04:05.503 回答
1

改变

SSL_CTX_use_certificate_file(sslctx, certFile, SSL_FILETYPE_PEM);

进入

SSL_CTX_use_certificate_file(sslctx, certFile.c_str(), SSL_FILETYPE_PEM);
于 2012-07-24T13:03:53.413 回答
1

尝试使用c_str()

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

而是char*string

于 2012-07-24T13:04:08.843 回答