~ubuntu-branches/ubuntu/wily/psi/wily

« back to all changes in this revision

Viewing changes to src/sslcertdlg.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2009-09-25 17:49:51 UTC
  • mfrom: (6.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090925174951-lvm7kdap82o8xhn3
Tags: 0.13-1
* Updated to upstream version 0.13
* Set Standards-Version to 3.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * sslcertdlg.cpp
3
 
 * Copyright (C) 2003  Justin Karneges
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or
6
 
 * modify it under the terms of the GNU General Public License
7
 
 * as published by the Free Software Foundation; either version 2
8
 
 * of the License, or (at your option) any later version.
9
 
 *
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU General Public License
16
 
 * along with this library; if not, write to the Free Software
17
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 
 *
19
 
 */
20
 
 
21
 
#include <QtCrypto>
22
 
#include <QDateTime>
23
 
#include <QLabel>
24
 
#include <QPushButton>
25
 
 
26
 
#include "iconwidget.h"
27
 
#include "certutil.h"
28
 
#include "common.h"
29
 
#include "sslcertdlg.h"
30
 
 
31
 
 
32
 
SSLCertDlg::SSLCertDlg(QWidget *parent)
33
 
:QDialog(parent)
34
 
{
35
 
        ui_.setupUi(this);
36
 
        setModal(true);
37
 
        setWindowTitle(CAP(caption()));
38
 
 
39
 
        connect(ui_.pb_close, SIGNAL(clicked()), SLOT(close()));
40
 
        ui_.pb_close->setDefault(true);
41
 
        ui_.pb_close->setFocus();
42
 
}
43
 
 
44
 
QString SSLCertDlg::makePropTable(const QString &heading, const QCA::CertificateInfo &list)
45
 
{
46
 
        QString str;
47
 
        str += "<tr><td><i>" + heading + "</i><br>";
48
 
        str += "<table>";
49
 
        str += makePropEntry(QCA::Organization, tr("Organization:"), list);
50
 
        str += makePropEntry(QCA::OrganizationalUnit, tr("Organizational unit:"), list);
51
 
        str += makePropEntry(QCA::Locality, tr("Locality:"), list);
52
 
        str += makePropEntry(QCA::State, tr("State:"), list);
53
 
        str += makePropEntry(QCA::Country, tr("Country:"), list);
54
 
        str += makePropEntry(QCA::CommonName, tr("Common name:"), list);
55
 
        str += makePropEntry(QCA::DNS, tr("Domain name:"), list);
56
 
        str += makePropEntry(QCA::XMPP, tr("XMPP name:"), list);
57
 
        str += makePropEntry(QCA::Email, tr("Email:"), list);
58
 
        str += "</table></td></tr>";
59
 
        return str;
60
 
}
61
 
 
62
 
void SSLCertDlg::setCert(const QCA::Certificate &cert, int result, QCA::Validity validity)
63
 
{
64
 
        if(cert.isNull())
65
 
                return;
66
 
 
67
 
        if(result == QCA::TLS::Valid) {
68
 
                ui_.lb_valid->setText(tr("The certificate is valid."));
69
 
                setLabelStatus(*ui_.lb_valid,true);
70
 
        }
71
 
        else {
72
 
                ui_.lb_valid->setText(tr("The certificate is NOT valid!") + "\n" + QString(tr("Reason: %1.")).arg(CertUtil::resultToString(result, validity)));
73
 
                setLabelStatus(*ui_.lb_valid,false);
74
 
        }
75
 
 
76
 
        QDateTime now = QDateTime::currentDateTime();
77
 
        QDateTime notBefore = cert.notValidBefore();
78
 
        QDateTime notAfter = cert.notValidAfter();
79
 
        ui_.lb_notBefore->setText(cert.notValidBefore().toString());
80
 
        setLabelStatus(*ui_.lb_notBefore, now > notBefore);
81
 
        ui_.lb_notAfter->setText(cert.notValidAfter().toString());
82
 
        setLabelStatus(*ui_.lb_notAfter, now < notAfter);
83
 
 
84
 
        ui_.lb_sn->setText(cert.serialNumber().toString());
85
 
 
86
 
        QString str;
87
 
        str += "<table>";
88
 
        str += makePropTable(tr("Subject Details:"), cert.subjectInfo());
89
 
        str += makePropTable(tr("Issuer Details:"), cert.issuerInfo());
90
 
        str += "</table>";
91
 
        for (int i=0; i < 2; i++) {
92
 
                QString hashstr = QCA::Hash(i == 0 ? "md5" : "sha1").hashToString(cert.toDER())
93
 
                                        .toUpper().replace(QRegExp("(..)"), ":\\1").mid(1);
94
 
                str += QString("Fingerprint(%1): %2<br>").arg(i == 0 ? "MD5" : "SHA-1").arg(hashstr);
95
 
        }
96
 
        ui_.tb_cert->setText(str);
97
 
}
98
 
 
99
 
void SSLCertDlg::showCert(const QCA::Certificate &certificate, int result, QCA::Validity validity)
100
 
{
101
 
        SSLCertDlg *w = new SSLCertDlg(0);
102
 
        w->setCert(certificate, result, validity);
103
 
        w->exec();
104
 
        delete w;
105
 
}
106
 
 
107
 
void SSLCertDlg::setLabelStatus(QLabel& l, bool ok)
108
 
{
109
 
        l.setPaletteForegroundColor(ok ? QColor("#2A993B") : QColor("#810000"));
110
 
}
111
 
 
112
 
QString SSLCertDlg::makePropEntry(QCA::CertificateInfoType var, const QString &name, const QCA::CertificateInfo &list)
113
 
{
114
 
        QString val;
115
 
        QList<QString> values = list.values(var);
116
 
        for (int i = 0; i < values.size(); ++i)
117
 
                val += values.at(i) + "<br>";
118
 
 
119
 
        if(val.isEmpty())
120
 
                return "";
121
 
        else
122
 
                return QString("<tr><td><nobr><b>") + name + "</b></nobr></td><td>" + val + "</td></tr>";
123
 
}