diff ui/certificate.cpp @ 355:5f1494fab517

merged.
author Raimund Renkert <rrenkert@intevation.de>
date Thu, 10 Apr 2014 16:03:23 +0200
parents b0a274f4f9e2 a49766196a7d
children 67b471c4d1fc
line wrap: on
line diff
--- a/ui/certificate.cpp	Thu Apr 10 15:57:26 2014 +0200
+++ b/ui/certificate.cpp	Thu Apr 10 16:03:23 2014 +0200
@@ -1,9 +1,11 @@
 #include "certificate.h"
 #include <QDebug>
+#include <QFile>
 #include <QStringList>
 #include <QObject>
 
 #include "certhelp.h"
+#include "listutil.h"
 
 /* Qt wrapper around certhelp functions. */
 QString getX509Value(x509_name *namebuf, unsigned char *oid) {
@@ -62,6 +64,20 @@
             .arg(QLocale::system().toString(mValidTo));
 }
 
+Certificate::Certificate(const QByteArray& derData) :
+    mValid(false)
+{
+    if (derData.isEmpty()) {
+        return;
+    }
+
+    parseDetails(derData);
+
+    mValid = !mSubjectCN.isEmpty();
+
+    mBaseLine = derData.toBase64();
+}
+
 Certificate::Certificate(const QString& b64Line) :
     mValid(false)
 {
@@ -70,10 +86,10 @@
     }
 
     /* Cut of the first two chars (e.g. I: and decode) */
-    QByteArray asn1data = QByteArray::fromBase64(
+    QByteArray derData = QByteArray::fromBase64(
             b64Line.right(b64Line.size() - 2).toLatin1());
 
-    parseDetails(asn1data);
+    parseDetails(derData);
 
     /* If the subject CN is set then at least one x509parse
      * in polarssl was successfull. And a root certificate
@@ -98,6 +114,57 @@
     return ret;
 }
 
+QList<Certificate> Certificate::fromFileName(const QString& file_name) {
+    /* We read the file using Qt to avoid filename encoding problems
+     * on Windows */
+
+    /* TODO change qDebug errors into messageboxes */
+    QFile certificateFile(file_name);
+    QByteArray fileContent;
+    QList<Certificate> retval;
+    x509_crt chain;
+    int ret = 0;
+    if (!certificateFile.open(QIODevice::ReadOnly)) {
+        qDebug() << "Failed to read file.";
+        return retval;
+    }
+
+    if (certificateFile.size() > MAX_LINE_LENGTH * MAX_LINES) {
+        qDebug() << "File too large";
+        return retval;
+    }
+
+    fileContent = certificateFile.readAll();
+
+    x509_crt_init(&chain);
+
+    ret = x509_crt_parse(&chain,
+            reinterpret_cast<const unsigned char*>(fileContent.constData()),
+            fileContent.size());
+
+    if (ret < 0) {
+        qDebug() << "Failed to parse certificates.";
+        return retval;
+    }
+
+    if (ret > 0) {
+        qDebug() << "Some certificates could not be parsed.";
+        /* Maybe return here? */
+    }
+
+    x509_crt *iter = &chain;
+
+    while (iter) {
+        QByteArray derData(reinterpret_cast<const char*>(iter->raw.p),
+                static_cast<int>(iter->raw.len));
+        retval << Certificate(derData);
+        iter = iter->next;
+    }
+    x509_crt_free(&chain);
+
+    return retval;
+}
+
 void Certificate::setInstallCert(bool install)
 {
     if (install && mBaseLine.startsWith("R:")) {

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