vmime/examples/example6_certificateVerifier.hpp

65 lines
1.6 KiB
C++
Raw Normal View History

2014-03-16 21:52:40 +00:00
#if VMIME_HAVE_TLS_SUPPORT
// Certificate verifier (TLS/SSL)
2018-09-05 21:54:48 +00:00
class interactiveCertificateVerifier : public vmime::security::cert::defaultCertificateVerifier {
2014-03-16 21:52:40 +00:00
public:
2018-09-05 21:54:48 +00:00
void verify(
const vmime::shared_ptr <vmime::security::cert::certificateChain>& chain,
const vmime::string& hostname
) {
try {
2014-03-16 21:52:40 +00:00
setX509TrustedCerts(m_trustedCerts);
defaultCertificateVerifier::verify(chain, hostname);
2018-09-05 21:54:48 +00:00
} catch (vmime::security::cert::certificateException&) {
2014-03-16 21:52:40 +00:00
// Obtain subject's certificate
vmime::shared_ptr <vmime::security::cert::certificate> cert = chain->getAt(0);
std::cout << std::endl;
std::cout << "Server sent a '" << cert->getType() << "'" << " certificate." << std::endl;
std::cout << "Do you want to accept this certificate? (Y/n) ";
std::cout.flush();
std::string answer;
std::getline(std::cin, answer);
if (answer.length() != 0 &&
2018-09-05 21:54:48 +00:00
(answer[0] == 'Y' || answer[0] == 'y')) {
2014-03-16 21:52:40 +00:00
// Accept it, and remember user's choice for later
2018-09-05 21:54:48 +00:00
if (cert->getType() == "X.509") {
m_trustedCerts.push_back(
vmime::dynamicCast <vmime::security::cert::X509Certificate>(cert)
);
2014-03-16 21:52:40 +00:00
setX509TrustedCerts(m_trustedCerts);
defaultCertificateVerifier::verify(chain, hostname);
}
return;
}
2018-09-05 21:54:48 +00:00
throw vmime::security::cert::certificateException("User did not accept the certificate.");
2014-03-16 21:52:40 +00:00
}
}
private:
static std::vector <vmime::shared_ptr <vmime::security::cert::X509Certificate> > m_trustedCerts;
};
std::vector <vmime::shared_ptr <vmime::security::cert::X509Certificate> >
interactiveCertificateVerifier::m_trustedCerts;
#endif // VMIME_HAVE_TLS_SUPPORT