File: certviewdlg.cpp

package info (click to toggle)
qca2 2.3.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,884 kB
  • sloc: cpp: 59,224; ansic: 814; perl: 133; sh: 89; makefile: 34
file content (185 lines) | stat: -rw-r--r-- 6,389 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 Copyright (C) 2007 Justin Karneges <justin@affinix.com>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "certviewdlg.h"

#include "ui_certview.h"
#include <QtCore>
#include <QtCrypto>
#include <QtGui>

// from qcatool
class InfoType
{
public:
    QCA::CertificateInfoType type;
    QString                  varname;
    QString                  shortname;
    QString                  name;
    QString                  desc;

    InfoType()
    {
    }

    InfoType(QCA::CertificateInfoType _type,
             const QString           &_varname,
             const QString           &_shortname,
             const QString           &_name,
             const QString           &_desc)
        : type(_type)
        , varname(_varname)
        , shortname(_shortname)
        , name(_name)
        , desc(_desc)
    {
    }
};

static QList<InfoType> makeInfoTypeList(bool legacyEmail = false)
{
    QList<InfoType> out;
    out += InfoType(
        QCA::CommonName, "CommonName", "CN", CertViewDlg::tr("Common Name (CN)"), "Full name, domain, anything");
    out += InfoType(QCA::Email, "Email", "", CertViewDlg::tr("Email Address"), "");
    if (legacyEmail)
        out += InfoType(QCA::EmailLegacy, "EmailLegacy", "", CertViewDlg::tr("PKCS#9 Email Address"), "");
    out += InfoType(QCA::Organization, "Organization", "O", CertViewDlg::tr("Organization (O)"), "Company, group, etc");
    out += InfoType(QCA::OrganizationalUnit,
                    "OrganizationalUnit",
                    "OU",
                    CertViewDlg::tr("Organizational Unit (OU)"),
                    "Division/branch of organization");
    out += InfoType(QCA::Locality, "Locality", "", CertViewDlg::tr("Locality (L)"), "City, shire, part of a state");
    out += InfoType(QCA::State, "State", "", CertViewDlg::tr("State (ST)"), "State within the country");
    out += InfoType(QCA::Country, "Country", "C", CertViewDlg::tr("Country Code (C)"), "2-letter code");
    out += InfoType(QCA::IncorporationLocality,
                    "IncorporationLocality",
                    "",
                    CertViewDlg::tr("Incorporation Locality"),
                    "For EV certificates");
    out += InfoType(QCA::IncorporationState,
                    "IncorporationState",
                    "",
                    CertViewDlg::tr("Incorporation State"),
                    "For EV certificates");
    out += InfoType(QCA::IncorporationCountry,
                    "IncorporationCountry",
                    "",
                    CertViewDlg::tr("Incorporation Country"),
                    "For EV certificates");
    out += InfoType(QCA::URI, "URI", "", CertViewDlg::tr("URI"), "");
    out += InfoType(QCA::DNS, "DNS", "", CertViewDlg::tr("Domain Name"), "Domain (dnsName)");
    out += InfoType(QCA::IPAddress, "IPAddress", "", CertViewDlg::tr("IP Adddress"), "");
    out += InfoType(QCA::XMPP, "XMPP", "", CertViewDlg::tr("XMPP Address (JID)"), "From RFC 3920 (id-on-xmppAddr)");
    return out;
}

static QString try_print_info(const QString &name, const QStringList &values)
{
    QString out;
    if (!values.isEmpty()) {
        QString value = values.join(", ");
        out           = QString("   ") + CertViewDlg::tr("%1: %2").arg(name, value) + '\n';
    }
    return out;
}

static QString print_info(const QString &title, const QCA::CertificateInfo &info)
{
    QString         out;
    QList<InfoType> list = makeInfoTypeList();
    out += title + '\n';
    foreach (const InfoType &t, list)
        out += try_print_info(t.name, info.values(t.type));
    return out;
}

static QString cert_info_string(const QCA::Certificate &cert)
{
    QString out;
    out += CertViewDlg::tr("Serial Number: %1").arg(cert.serialNumber().toString()) + '\n';
    out += print_info(CertViewDlg::tr("Subject"), cert.subjectInfo());
    out += print_info(CertViewDlg::tr("Issuer"), cert.issuerInfo());
    out += CertViewDlg::tr("Validity") + '\n';
    out += QString("   ") + CertViewDlg::tr("Not before: %1").arg(cert.notValidBefore().toString()) + '\n';
    out += QString("   ") + CertViewDlg::tr("Not after:  %1").arg(cert.notValidAfter().toString()) + '\n';
    return out;
}

class CertViewDlg::Private : public QObject
{
    Q_OBJECT
public:
    CertViewDlg          *q;
    Ui_CertView           ui;
    QCA::CertificateChain chain;

    Private(CertViewDlg *_q)
        : QObject(_q)
        , q(_q)
    {
        ui.setupUi(q);
        connect(ui.cb_chain, SIGNAL(activated(int)), SLOT(cb_activated(int)));
        ui.lb_info->setTextInteractionFlags(Qt::TextSelectableByMouse);
    }

    void update()
    {
        QStringList names = QCA::makeFriendlyNames(chain);
        ui.cb_chain->clear();
        foreach (const QString &s, names)
            ui.cb_chain->insertItem(ui.cb_chain->count(), s);
        updateInfo();
    }

    void updateInfo()
    {
        int x = ui.cb_chain->currentIndex();
        if (x == -1) {
            ui.lb_info->setText("");
            return;
        }

        ui.lb_info->setText(cert_info_string(chain[x]));
    }

private Q_SLOTS:
    void cb_activated(int)
    {
        updateInfo();
    }
};

CertViewDlg::CertViewDlg(const QCA::CertificateChain &chain, QWidget *parent)
    : QDialog(parent)
{
    d        = new Private(this);
    d->chain = chain;
    d->update();
}

CertViewDlg::~CertViewDlg()
{
    delete d;
}

#include "certviewdlg.moc"