comparison ui/certificate_win.cpp @ 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 1dd8e91972a8
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(mPCertContext,
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(mPCertContext,
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(mPCertContext);
55 }
56 }
57
58 #endif

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