MCPcopy Create free account
hub / github.com/Icinga/icinga2 / FetchCert

Method FetchCert

lib/remote/pkiutility.cpp:85–124  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

83}
84
85std::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
126int PkiUtility::WriteCert(const std::shared_ptr<X509>& cert, const String& trustedfile)
127{

Callers

nothing calls this directly

Calls 6

MakeAsioSslContextFunction · 0.85
LogClass · 0.85
handshakeMethod · 0.80
GetPeerCertificateMethod · 0.80
ConnectFunction · 0.50
whatMethod · 0.45

Tested by

no test coverage detected