2016-09-22 11 views
2

自己署名入りの証明書(自分のCAを使用)で生成しましたが、今では昇格ASIOクライアントでサーバーのIDを確認しようとしています。私はopensslでこれらを検証し、検証がうまくいくように思えます。自己署名入り証明書を昇格で検証できません

サーバーコードとクライアントコードはそれぞれherehereです。

私は、次の部分修正:しかし、これは動作していないと、クライアントは、私は

Verifying /C=IT/ST=Italy/L=Milan/O=MyCompanyLtd/OU=MyCompanyLtd Auth/CN=mywebsite.net/[email protected] 
preverified: false 
Handshake failed: certificate verify failed 

を返して

bool verify_certificate(bool preverified, 
     boost::asio::ssl::verify_context& ctx) 
    { 
    // The verify callback can be used to check whether the certificate that is 
    // being presented is valid for the peer. For example, RFC 2818 describes 
    // the steps involved in doing this for HTTPS. Consult the OpenSSL 
    // documentation for more details. Note that the callback is called once 
    // for each certificate in the certificate chain, starting from the root 
    // certificate authority. 

    // In this example we will simply print the certificate's subject name. 
    char subject_name[256]; 
    X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle()); 
    X509_NAME_oneline(X509_get_subject_name(cert), subject_name, 256); 
    std::cout << "Verifying " << subject_name << "\n"; 
    std::cout << "preverified: " << std::boolalpha << preverified << "\n"; 

    return preverified; 
    } 

... 

int main(int argc, char* argv[]) 
{ 
    try 
    { 
    boost::asio::io_service io_service; 

    boost::asio::ip::tcp::resolver resolver(io_service); 
    boost::asio::ip::tcp::resolver::query query("localhost", "3232"); 
    boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query); 

    boost::asio::ssl::context ctx(boost::asio::ssl::context::tlsv12_client); 


    std::ifstream ca_file("/home/paul/ca/certs/ca.cert.pem", std::ios::binary | std::ios::ate); 
    std::vector<char> data; 
    auto size = ca_file.tellg(); 
    data.resize(size); 
    ca_file.seekg(0, std::ios::beg); 
    ca_file.read(data.data(), size); 
    ca_file.close(); 

    // Have my own CA added to the list of known CAs 
    ctx.add_certificate_authority(boost::asio::buffer(data, data.size())); 

    // Not sure if I need something here, the CA should be enough to 
    // validate the server's certificate prompted (even if signed by the 
    // intermediate CA) 
    //ctx.load_verify_file("/home/paul/ca/private/ca.key.pem"); 

    //ctx.load_verify_file("/home/paul/ca/intermediate/private/intermediate.key.pem"); 

    client c(io_service, ctx, iterator); 

class server 
{ 
public: 
    server(boost::asio::io_service& io_service, unsigned short port) 
    : io_service_(io_service), 
     acceptor_(io_service, 
      boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)), 
     context_(boost::asio::ssl::context::tlsv12_server) 
    { 
    context_.set_options(
     boost::asio::ssl::context::default_workarounds 
     | boost::asio::ssl::context::no_sslv2 
     | boost::asio::ssl::context::single_dh_use); 
    context_.set_password_callback(boost::bind(&server::get_password, this)); 

    // Use the certificate for my website that I had generated  context_.use_certificate_file("/home/paul/ca/intermediate/certs/mywebsite.net.cert.pem", boost::asio::ssl::context::pem); 

    // Not sure if I need this, probably not. I do have an intermediate CA though   
    //context_.use_certificate_chain_file("/home/paul/ca/intermediate/certs/ca-chain.cert.pem"); 

    // Use website private key  context_.use_private_key_file("/home/paul/ca/intermediate/private/mywebsite.net.key.pem", boost::asio::ssl::context::pem); 
    context_.use_tmp_dh_file("/home/paul/SSLTest/dh512.pem"); 

    start_accept(); 
    } 

とクライアントでのコールバックで検証が実行されていないことを認識していますが、に設定されたコールバックは事前検証後に呼び出される(したがってpreverifiedパラメータ)。

どこが間違っていますか?

+2

「自己割り当て」とは、[自己署名](https://en.wikipedia.org/wiki/Self-signed_certificate)証明書を意味しますか? –

+0

@DanMašekはい、正確に。ごめんなさい。 – Dean

+2

@Deanタイトルを修正するには19時間足りませんか? – sehe

答えて

0

あなたが提供したデータだけではうまくいかないでしょう。証明書を検証するのに十分な情報がありません。一般的に、これらの2つを呼び出す必要があります。

ctx.use_certificate_chain_file("path"); 
ctx.use_private_key_file("path", boost::asio::ssl::context::pem); 
関連する問題