| 83 | } |
| 84 | |
| 85 | std::shared_ptr<X509> PkiUtility::FetchCert(const String& host, const String& port) |
| 86 | { |
| 87 | Shared<boost::asio::ssl::context>::Ptr sslContext; |
| 88 | |
| 89 | try { |
| 90 | sslContext = MakeAsioSslContext(); |
| 91 | } catch (const std::exception& ex) { |
| 92 | Log(LogCritical, "pki") |
| 93 | << "Cannot make SSL context."; |
| 94 | Log(LogDebug, "pki") |
| 95 | << "Cannot make SSL context:\n" << DiagnosticInformation(ex); |
| 96 | return std::shared_ptr<X509>(); |
| 97 | } |
| 98 | |
| 99 | auto stream (Shared<AsioTlsStream>::Make(IoEngine::Get().GetIoContext(), *sslContext, host)); |
| 100 | |
| 101 | try { |
| 102 | Connect(stream->lowest_layer(), host, port); |
| 103 | } catch (const std::exception& ex) { |
| 104 | Log(LogCritical, "pki") |
| 105 | << "Cannot connect to host '" << host << "' on port '" << port << "'"; |
| 106 | Log(LogDebug, "pki") |
| 107 | << "Cannot connect to host '" << host << "' on port '" << port << "':\n" << DiagnosticInformation(ex); |
| 108 | return std::shared_ptr<X509>(); |
| 109 | } |
| 110 | |
| 111 | auto& sslConn (stream->next_layer()); |
| 112 | |
| 113 | try { |
| 114 | sslConn.handshake(sslConn.client); |
| 115 | } catch (const std::exception& ex) { |
| 116 | Log(LogCritical, "pki") |
| 117 | << "Client TLS handshake failed. (" << ex.what() << ")"; |
| 118 | return std::shared_ptr<X509>(); |
| 119 | } |
| 120 | |
| 121 | Defer shutdown ([&sslConn]() { sslConn.shutdown(); }); |
| 122 | |
| 123 | return sslConn.GetPeerCertificate(); |
| 124 | } |
| 125 | |
| 126 | int PkiUtility::WriteCert(const std::shared_ptr<X509>& cert, const String& trustedfile) |
| 127 | { |
nothing calls this directly
no test coverage detected