File indexing completed on 2024-04-28 04:43:37

0001 /*
0002  Copyright (C) 2007 Justin Karneges <justin@affinix.com>
0003 
0004  Permission is hereby granted, free of charge, to any person obtaining a copy
0005  of this software and associated documentation files (the "Software"), to deal
0006  in the Software without restriction, including without limitation the rights
0007  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0008  copies of the Software, and to permit persons to whom the Software is
0009  furnished to do so, subject to the following conditions:
0010 
0011  The above copyright notice and this permission notice shall be included in
0012  all copies or substantial portions of the Software.
0013 
0014  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
0017  AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
0018  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0019  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0020 */
0021 
0022 #include "certviewdlg.h"
0023 
0024 #include "ui_certview.h"
0025 #include <QtCore>
0026 #include <QtCrypto>
0027 #include <QtGui>
0028 
0029 // from qcatool
0030 class InfoType
0031 {
0032 public:
0033     QCA::CertificateInfoType type;
0034     QString                  varname;
0035     QString                  shortname;
0036     QString                  name;
0037     QString                  desc;
0038 
0039     InfoType()
0040     {
0041     }
0042 
0043     InfoType(QCA::CertificateInfoType _type,
0044              const QString           &_varname,
0045              const QString           &_shortname,
0046              const QString           &_name,
0047              const QString           &_desc)
0048         : type(_type)
0049         , varname(_varname)
0050         , shortname(_shortname)
0051         , name(_name)
0052         , desc(_desc)
0053     {
0054     }
0055 };
0056 
0057 static QList<InfoType> makeInfoTypeList(bool legacyEmail = false)
0058 {
0059     QList<InfoType> out;
0060     out += InfoType(
0061         QCA::CommonName, "CommonName", "CN", CertViewDlg::tr("Common Name (CN)"), "Full name, domain, anything");
0062     out += InfoType(QCA::Email, "Email", "", CertViewDlg::tr("Email Address"), "");
0063     if (legacyEmail)
0064         out += InfoType(QCA::EmailLegacy, "EmailLegacy", "", CertViewDlg::tr("PKCS#9 Email Address"), "");
0065     out += InfoType(QCA::Organization, "Organization", "O", CertViewDlg::tr("Organization (O)"), "Company, group, etc");
0066     out += InfoType(QCA::OrganizationalUnit,
0067                     "OrganizationalUnit",
0068                     "OU",
0069                     CertViewDlg::tr("Organizational Unit (OU)"),
0070                     "Division/branch of organization");
0071     out += InfoType(QCA::Locality, "Locality", "", CertViewDlg::tr("Locality (L)"), "City, shire, part of a state");
0072     out += InfoType(QCA::State, "State", "", CertViewDlg::tr("State (ST)"), "State within the country");
0073     out += InfoType(QCA::Country, "Country", "C", CertViewDlg::tr("Country Code (C)"), "2-letter code");
0074     out += InfoType(QCA::IncorporationLocality,
0075                     "IncorporationLocality",
0076                     "",
0077                     CertViewDlg::tr("Incorporation Locality"),
0078                     "For EV certificates");
0079     out += InfoType(QCA::IncorporationState,
0080                     "IncorporationState",
0081                     "",
0082                     CertViewDlg::tr("Incorporation State"),
0083                     "For EV certificates");
0084     out += InfoType(QCA::IncorporationCountry,
0085                     "IncorporationCountry",
0086                     "",
0087                     CertViewDlg::tr("Incorporation Country"),
0088                     "For EV certificates");
0089     out += InfoType(QCA::URI, "URI", "", CertViewDlg::tr("URI"), "");
0090     out += InfoType(QCA::DNS, "DNS", "", CertViewDlg::tr("Domain Name"), "Domain (dnsName)");
0091     out += InfoType(QCA::IPAddress, "IPAddress", "", CertViewDlg::tr("IP Adddress"), "");
0092     out += InfoType(QCA::XMPP, "XMPP", "", CertViewDlg::tr("XMPP Address (JID)"), "From RFC 3920 (id-on-xmppAddr)");
0093     return out;
0094 }
0095 
0096 static QString try_print_info(const QString &name, const QStringList &values)
0097 {
0098     QString out;
0099     if (!values.isEmpty()) {
0100         QString value = values.join(", ");
0101         out           = QString("   ") + CertViewDlg::tr("%1: %2").arg(name, value) + '\n';
0102     }
0103     return out;
0104 }
0105 
0106 static QString print_info(const QString &title, const QCA::CertificateInfo &info)
0107 {
0108     QString         out;
0109     QList<InfoType> list = makeInfoTypeList();
0110     out += title + '\n';
0111     foreach (const InfoType &t, list)
0112         out += try_print_info(t.name, info.values(t.type));
0113     return out;
0114 }
0115 
0116 static QString cert_info_string(const QCA::Certificate &cert)
0117 {
0118     QString out;
0119     out += CertViewDlg::tr("Serial Number: %1").arg(cert.serialNumber().toString()) + '\n';
0120     out += print_info(CertViewDlg::tr("Subject"), cert.subjectInfo());
0121     out += print_info(CertViewDlg::tr("Issuer"), cert.issuerInfo());
0122     out += CertViewDlg::tr("Validity") + '\n';
0123     out += QString("   ") + CertViewDlg::tr("Not before: %1").arg(cert.notValidBefore().toString()) + '\n';
0124     out += QString("   ") + CertViewDlg::tr("Not after:  %1").arg(cert.notValidAfter().toString()) + '\n';
0125     return out;
0126 }
0127 
0128 class CertViewDlg::Private : public QObject
0129 {
0130     Q_OBJECT
0131 public:
0132     CertViewDlg          *q;
0133     Ui_CertView           ui;
0134     QCA::CertificateChain chain;
0135 
0136     Private(CertViewDlg *_q)
0137         : QObject(_q)
0138         , q(_q)
0139     {
0140         ui.setupUi(q);
0141         connect(ui.cb_chain, SIGNAL(activated(int)), SLOT(cb_activated(int)));
0142         ui.lb_info->setTextInteractionFlags(Qt::TextSelectableByMouse);
0143     }
0144 
0145     void update()
0146     {
0147         QStringList names = QCA::makeFriendlyNames(chain);
0148         ui.cb_chain->clear();
0149         foreach (const QString &s, names)
0150             ui.cb_chain->insertItem(ui.cb_chain->count(), s);
0151         updateInfo();
0152     }
0153 
0154     void updateInfo()
0155     {
0156         int x = ui.cb_chain->currentIndex();
0157         if (x == -1) {
0158             ui.lb_info->setText("");
0159             return;
0160         }
0161 
0162         ui.lb_info->setText(cert_info_string(chain[x]));
0163     }
0164 
0165 private Q_SLOTS:
0166     void cb_activated(int)
0167     {
0168         updateInfo();
0169     }
0170 };
0171 
0172 CertViewDlg::CertViewDlg(const QCA::CertificateChain &chain, QWidget *parent)
0173     : QDialog(parent)
0174 {
0175     d        = new Private(this);
0176     d->chain = chain;
0177     d->update();
0178 }
0179 
0180 CertViewDlg::~CertViewDlg()
0181 {
0182     delete d;
0183 }
0184 
0185 #include "certviewdlg.moc"