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