C/C++ クライアントが SSL 経由でサーバーを認証するようにしたいと考えています。最初にサーバーから証明書ファイルをダウンロードしました
openssl s_client -showcerts -connect www.openssl.org:443 </dev/null 2>/dev/null | openssl x509 -outform PEM > mycertfile.pem
次に、アプリケーションで次の API 呼び出し (疑似コード) を実行します。
// Register the error strings for libcrypto & libssl
SSL_load_error_strings();
// Register the available ciphers and digests
SSL_library_init();
// New context saying we are a client, and using SSL 2 or 3
ctx = SSL_CTX_new(SSLv23_client_method());
// load the certificate
if(!SSL_CTX_load_verify_locations(ctx, "mycertfile.pem", 0))
...
// Create an SSL struct for the connection
ssl = SSL_new(ctx);
// Connect the SSL struct to our pre-existing TCP/IP socket connection
if (!SSL_set_fd(ssl, sd))
...
// Initiate SSL handshake
if(SSL_connect(ssl) != 1)
...
// form this point onwards the SSL connection is established and works
// perfectly, I would be able to send and receive encrypted data
// **Crucial point now**
// Get certificate (it works)
X509 *cert = SSL_get_peer_certificate(ssl);
if(cert) {
// the below API returns code 19
const long cert_res = SSL_get_verify_result(ssl);
if(cert_res == X509_V_OK) {
printf("Certificate verified!\n");
}
X509_free(cert);
}
上記のコードは、証明書を確認することを気にせず、暗号化された接続に関心があるだけであれば問題なく動作します。
問題は、サーバーの信頼性を検証しようとすると、証明書を取得できますが、5 分前に証明書をダウンロードしただけでもSSL_get_peer_certificate
結果の検証が機能しないことです。
私は何を間違っていますか?
これはすべて、 gcc およびopensslを使用した Ubuntu 12.04.03 x86-64 上にあります。
ありがとう、エマ