comparison ui/certificate_win.h @ 21:dc1e1e9e62ce

Add certificate class and use it
author Andre Heinecke <aheinecke@intevation.de>
date Thu, 20 Feb 2014 10:58:28 +0000
parents
children 17e1c8f37d72
comparison
equal deleted inserted replaced
20:fec1a610d43f 21:dc1e1e9e62ce
1 #include "certificate.h"
2
3 #ifdef Q_OS_WIN
4
5 #include <QDebug>
6
7 Certificate::Certificate(const QByteArray& asn1data) : mValid(false) {
8
9 // asn1data is expected to be \0 terminated as this is what
10 // qt does in ::fromBase64 so the actual asn1data is
11 // size() - 1 and not size()
12 Q_ASSERT (asn1data[asn1data.size()] == '\0');
13 DWORD sizeOfName = 0;
14
15 DWORD sizeOfCert = asn1data.size() - 1;
16
17 mPCertContext = CertCreateCertificateContext(X509_ASN_ENCODING,
18 (const BYTE *) asn1data.constData(), sizeOfCert);
19
20 if (!mPCertContext) {
21 qDebug() << "Error creating certificate context. Err: " <<
22 GetLastError();
23 return;
24 }
25
26 sizeOfName = CertGetNameStringW(pCertContext,
27 CERT_NAME_FRIENDLY_DISPLAY_TYPE,
28 0,
29 NULL,
30 NULL,
31 0);
32
33 if (sizeOfName <= 1) {
34 // Probably some fallbacks would be nice here? Let's see if this
35 // is a problem in testing.
36 qDebug() << "Failed to get friendly name. Don't know what to do!";
37 } else {
38 WCHAR certName[sizeOfName];
39 DWORD actSize = CertGetNameStringW(pCertContext,
40 CERT_NAME_FRIENDLY_DISPLAY_TYPE,
41 0,
42 NULL,
43 certName,
44 sizeOfName);
45 Q_ASSERT (actSize == sizeOfName);
46
47 mShortDescription = QString::fromWCharArray(certName, sizeOfName);
48 }
49
50 }
51
52 Certificate::~Certificate() {
53 if (mPCertContext) {
54 CertFreeCertificateContext(pCertContext);
55 }
56 }
57
58 QString Certificate::shortDescription() {
59 if (!isValid()) {
60 return QString::fromLatin1(tr("Invalid Certificate"));
61 }
62 return mShortDescription;
63 }
64
65 #endif

http://wald.intevation.org/projects/trustbridge/