2016-05-29 12 views
2

libcurlとsslを使用して問題が発生しました。C++でのLibCurlの使用と自己署名付き証明書

私は、次のcurlコマンドを使用して、私のサイトに接続しようとした場合:

カール-q --certクライアント2048.crt --keyクライアント2048.keyます。https:// ***** * "ユーザ名= &パスワード=" -H "X-アプリケーション:curlCommandLineTest" -d

すべてがうまく機能

私は同じことをするにはどうすればよい(証明書が途中で自己署名があります) libcurlを使う?

私はlibcurl sslサンプルに従いましたが、証明書と秘密鍵 には異なる拡張子がありますので、どこから起動するのかわかりません。

これまでのところ、私は次のことを試してみました(および他の多くの組み合わせ):

static const char *pCertFile = "client-2048.crt"; 
static const char *pCACertFile = "client-2048.pem"; 
static const char *pKeyName = "client-2048.key"; 

curl_global_init(CURL_GLOBAL_DEFAULT); 

curl = curl_easy_init(); 
if (curl) { 
    /* what call to write: */ 
    curl_easy_setopt(curl, CURLOPT_URL, "https://*****"); 
    curl_easy_setopt(curl, CURLOPT_HEADERDATA, headerfile); 

     /* cert is stored PEM coded in file... */ 
     /* since PEM is default, we needn't set it for PEM */ 
     curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, "PEM"); 

     /* set the cert for client authentication */ 
     curl_easy_setopt(curl, CURLOPT_SSLCERT, pCertFile); 

     /* set the private key (file or ID in engine) */ 
     curl_easy_setopt(curl, CURLOPT_SSLKEY, pKeyName); 

     /* disconnect if we can't validate server's cert */ 
     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L); 

     /* Perform the request, res will get the return code */ 
     res = curl_easy_perform(curl); 
     /* Check for errors */ 
     if (res != CURLE_OK) 
      fprintf(stderr, "curl_easy_perform() failed: %s\n", 
       curl_easy_strerror(res)); 

     /* we are done... */ 
    } while (0); 
    /* always cleanup */ 
    curl_easy_cleanup(curl); 
    return 0; 

しかし、私はメッセージを取得:

curl_easy_perform() failed: Peer certificate cannot be authenticated with 
given CA certificates 

だから、というコールをミラーリングするlibcurlのコード何でしょうし成功?

おかげ

+0

ここでは、curlがそれ以上のことをしているのは間違いないかもしれませんが、まず第一に、それは自己署名証明書なので、ピア検証を無効にする必要があります。 – user1583007

+0

'--libcurl source.c'を使ってcurlコマンドラインをlibcurlソースコードに変換 - 最初のステップとして –

答えて

1

自己署名証明書:

curl_easy_setopt(カール、CURLOPT_SSL_VERIFYPEER、0L);

+0

ありがとうございます –

関連する問題