annotate 3rdparty/vmime/examples/example6_certificateVerifier.hpp @ 0:a4671277546c tip

created the repository for the thymian project
author ferencd
date Tue, 17 Aug 2021 11:19:54 +0200
parents
children
rev   line source
ferencd@0 1
ferencd@0 2
ferencd@0 3 #if VMIME_HAVE_TLS_SUPPORT
ferencd@0 4
ferencd@0 5 // Certificate verifier (TLS/SSL)
ferencd@0 6 class interactiveCertificateVerifier : public vmime::security::cert::defaultCertificateVerifier
ferencd@0 7 {
ferencd@0 8 public:
ferencd@0 9
ferencd@0 10 void verify(vmime::shared_ptr <vmime::security::cert::certificateChain> chain, const vmime::string& hostname)
ferencd@0 11 {
ferencd@0 12 try
ferencd@0 13 {
ferencd@0 14 setX509TrustedCerts(m_trustedCerts);
ferencd@0 15
ferencd@0 16 defaultCertificateVerifier::verify(chain, hostname);
ferencd@0 17 }
ferencd@0 18 catch (vmime::security::cert::certificateException&)
ferencd@0 19 {
ferencd@0 20 // Obtain subject's certificate
ferencd@0 21 vmime::shared_ptr <vmime::security::cert::certificate> cert = chain->getAt(0);
ferencd@0 22
ferencd@0 23 std::cout << std::endl;
ferencd@0 24 std::cout << "Server sent a '" << cert->getType() << "'" << " certificate." << std::endl;
ferencd@0 25 std::cout << "Do you want to accept this certificate? (Y/n) ";
ferencd@0 26 std::cout.flush();
ferencd@0 27
ferencd@0 28 std::string answer;
ferencd@0 29 std::getline(std::cin, answer);
ferencd@0 30
ferencd@0 31 if (answer.length() != 0 &&
ferencd@0 32 (answer[0] == 'Y' || answer[0] == 'y'))
ferencd@0 33 {
ferencd@0 34 // Accept it, and remember user's choice for later
ferencd@0 35 if (cert->getType() == "X.509")
ferencd@0 36 {
ferencd@0 37 m_trustedCerts.push_back(vmime::dynamicCast
ferencd@0 38 <vmime::security::cert::X509Certificate>(cert));
ferencd@0 39
ferencd@0 40 setX509TrustedCerts(m_trustedCerts);
ferencd@0 41 defaultCertificateVerifier::verify(chain, hostname);
ferencd@0 42 }
ferencd@0 43
ferencd@0 44 return;
ferencd@0 45 }
ferencd@0 46
ferencd@0 47 throw vmime::security::cert::certificateException
ferencd@0 48 ("User did not accept the certificate.");
ferencd@0 49 }
ferencd@0 50 }
ferencd@0 51
ferencd@0 52 private:
ferencd@0 53
ferencd@0 54 static std::vector <vmime::shared_ptr <vmime::security::cert::X509Certificate> > m_trustedCerts;
ferencd@0 55 };
ferencd@0 56
ferencd@0 57
ferencd@0 58 std::vector <vmime::shared_ptr <vmime::security::cert::X509Certificate> >
ferencd@0 59 interactiveCertificateVerifier::m_trustedCerts;
ferencd@0 60
ferencd@0 61 #endif // VMIME_HAVE_TLS_SUPPORT
ferencd@0 62