Mercurial > trustbridge
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/certificate_win.cpp Thu Feb 20 10:58:28 2014 +0000 @@ -0,0 +1,58 @@ +#include "certificate.h" + +#ifdef Q_OS_WIN + +#include <QDebug> + +Certificate::Certificate(const QByteArray& asn1data) : mValid(false) { + + // asn1data is expected to be \0 terminated as this is what + // qt does in ::fromBase64 so the actual asn1data is + // size() - 1 and not size() + Q_ASSERT (asn1data[asn1data.size()] == '\0'); + DWORD sizeOfName = 0; + + DWORD sizeOfCert = asn1data.size() - 1; + + mPCertContext = CertCreateCertificateContext(X509_ASN_ENCODING, + (const BYTE *) asn1data.constData(), sizeOfCert); + + if (!mPCertContext) { + qDebug() << "Error creating certificate context. Err: " << + GetLastError(); + return; + } + + sizeOfName = CertGetNameStringW(mPCertContext, + CERT_NAME_FRIENDLY_DISPLAY_TYPE, + 0, + NULL, + NULL, + 0); + + if (sizeOfName <= 1) { + // Probably some fallbacks would be nice here? Let's see if this + // is a problem in testing. + qDebug() << "Failed to get friendly name. Don't know what to do!"; + } else { + WCHAR certName[sizeOfName]; + DWORD actSize = CertGetNameStringW(mPCertContext, + CERT_NAME_FRIENDLY_DISPLAY_TYPE, + 0, + NULL, + certName, + sizeOfName); + Q_ASSERT (actSize == sizeOfName); + + mShortDescription = QString::fromWCharArray(certName, sizeOfName); + } + +} + +Certificate::~Certificate() { + if (mPCertContext) { + CertFreeCertificateContext(mPCertContext); + } +} + +#endif