~ubuntu-branches/ubuntu/karmic/psi/karmic

« back to all changes in this revision

Viewing changes to src/certutil.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-04-14 18:57:30 UTC
  • mfrom: (2.1.9 hardy)
  • Revision ID: james.westby@ubuntu.com-20080414185730-528re3zp0m2hdlhi
Tags: 0.11-8
* added CONFIG -= link_prl to .pro files and removed dependencies
  which are made unnecessary by this change
* Fix segfault when closing last chat tab with qt4.4
  (This is from upstream svn, rev. 1101) (Closes: Bug#476122)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <QtCrypto>
 
2
#include <QStringList>
 
3
#include <QDomDocument>
 
4
#include <QDebug>
 
5
#include <QDir>
 
6
#include <QFile>
 
7
 
 
8
#include "applicationinfo.h"
 
9
#include "certutil.h"
 
10
 
 
11
using namespace QCA;
 
12
 
 
13
/**
 
14
 * \class CertUtil
 
15
 * \brief A class providing utility functions for Certificates.
 
16
 */
 
17
 
 
18
/**
 
19
 * \brief Returns the list of directories with certificates.
 
20
 */
 
21
static QStringList certificateStores()
 
22
{
 
23
        QStringList l;
 
24
        l += ApplicationInfo::resourcesDir() + "/certs";
 
25
        l += ApplicationInfo::homeDir() + "/certs";
 
26
        return l;
 
27
}
 
28
 
 
29
/**
 
30
 * \brief Returns the collection of all available certificates.
 
31
 * This collection includes the system-wide certificates, as well as any
 
32
 * custom certificate in the Psi-specific cert dirs.
 
33
 */
 
34
CertificateCollection CertUtil::allCertificates()
 
35
{
 
36
        CertificateCollection certs(systemStore());
 
37
        QStringList stores = certificateStores();
 
38
        for (QStringList::ConstIterator s = stores.begin(); s != stores.end(); ++s) {
 
39
                QDir store(*s);
 
40
 
 
41
                // Read in PEM certificates
 
42
                store.setNameFilters(QStringList("*.crt") + QStringList("*.pem"));
 
43
                QStringList cert_files = store.entryList();
 
44
                for (QStringList::ConstIterator c = cert_files.begin(); c != cert_files.end(); ++c) {
 
45
                        //qDebug() << "certutil.cpp: Reading " << store.filePath(*c);
 
46
                        ConvertResult result;
 
47
                        Certificate cert = Certificate::fromPEMFile(store.filePath(*c),&result);
 
48
                        if (result == ConvertGood) {
 
49
                                certs.addCertificate(cert);
 
50
                        }
 
51
                        else {
 
52
                                qWarning(QString("certutil.cpp: Invalid PEM certificate: %1").arg(store.filePath(*c)));
 
53
                        }
 
54
                }
 
55
 
 
56
                // Read in old XML format certificates (DEPRECATED)
 
57
                store.setNameFilter("*.xml");
 
58
                cert_files = store.entryList();
 
59
                for(QStringList::ConstIterator it = cert_files.begin(); it != cert_files.end(); ++it) {
 
60
                        qWarning(QString("Loading certificate in obsolete XML format: %1").arg(store.filePath(*it)));
 
61
                        QFile f(store.filePath(*it));
 
62
                        if(!f.open(QIODevice::ReadOnly))
 
63
                                continue;
 
64
                        QDomDocument doc;
 
65
                        bool ok = doc.setContent(&f);
 
66
                        f.close();
 
67
                        if(!ok)
 
68
                                continue;
 
69
 
 
70
                        QDomElement base = doc.documentElement();
 
71
                        if(base.tagName() != "store")
 
72
                                continue;
 
73
                        QDomNodeList cl = base.elementsByTagName("certificate");
 
74
 
 
75
                        for(int n = 0; n < (int)cl.count(); ++n) {
 
76
                                QDomElement data = cl.item(n).toElement().elementsByTagName("data").item(0).toElement();
 
77
                                if(!data.isNull()) {
 
78
                                        ConvertResult result;
 
79
                                        Certificate cert = Certificate::fromDER(Base64().stringToArray(data.text()).toByteArray(),&result);
 
80
                                        if (result == ConvertGood) {
 
81
                                                certs.addCertificate(cert);
 
82
                                        }
 
83
                                        else {
 
84
                                                qWarning(QString("certutil.cpp: Invalid XML certificate: %1").arg(store.filePath(*it)));
 
85
                                        }
 
86
                                }
 
87
                        }
 
88
                }
 
89
        }
 
90
        return certs;
 
91
}
 
92
 
 
93
QString CertUtil::validityToString(QCA::Validity v)
 
94
{
 
95
        QString s;
 
96
        switch(v)
 
97
        {
 
98
                case QCA::ValidityGood:
 
99
                        s = "Validated";
 
100
                        break;
 
101
                case QCA::ErrorRejected:
 
102
                        s = "Root CA is marked to reject the specified purpose";
 
103
                        break;
 
104
                case QCA::ErrorUntrusted:
 
105
                        s = "Certificate not trusted for the required purpose";
 
106
                        break;
 
107
                case QCA::ErrorSignatureFailed:
 
108
                        s = "Invalid signature";
 
109
                        break;
 
110
                case QCA::ErrorInvalidCA:
 
111
                        s = "Invalid CA certificate";
 
112
                        break;
 
113
                case QCA::ErrorInvalidPurpose:
 
114
                        s = "Invalid certificate purpose";
 
115
                        break;
 
116
                case QCA::ErrorSelfSigned:
 
117
                        s = "Certificate is self-signed";
 
118
                        break;
 
119
                case QCA::ErrorRevoked:
 
120
                        s = "Certificate has been revoked";
 
121
                        break;
 
122
                case QCA::ErrorPathLengthExceeded:
 
123
                        s = "Maximum certificate chain length exceeded";
 
124
                        break;
 
125
                case QCA::ErrorExpired:
 
126
                        s = "Certificate has expired";
 
127
                        break;
 
128
                case QCA::ErrorExpiredCA:
 
129
                        s = "CA has expired";
 
130
                        break;
 
131
                case QCA::ErrorValidityUnknown:
 
132
                default:
 
133
                        s = "General certificate validation error";
 
134
                        break;
 
135
        }
 
136
        return s;
 
137
}
 
138
 
 
139
QString CertUtil::resultToString(int result, QCA::Validity validity)
 
140
{
 
141
        QString s;
 
142
        switch(result) {
 
143
                case QCA::TLS::NoCertificate:
 
144
                        s = QObject::tr("The server did not present a certificate.");
 
145
                        break;
 
146
                case QCA::TLS::Valid:
 
147
                        s = QObject::tr("Certificate is valid.");
 
148
                        break;
 
149
                case QCA::TLS::HostMismatch:
 
150
                        s = QObject::tr("The hostname does not match the one the certificate was issued to.");
 
151
                        break;
 
152
                case QCA::TLS::InvalidCertificate:
 
153
                        s = validityToString(validity);
 
154
                        break;
 
155
 
 
156
                default:
 
157
                        s = QObject::tr("General certificate validation error.");
 
158
                        break;
 
159
        }
 
160
        return s;
 
161
}