KIO
ksslpeerinfo.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "ksslpeerinfo.h"
00022
00023 #include <config.h>
00024 #include <ksslconfig.h>
00025
00026 #include <QtCore/QRegExp>
00027 #include <QtCore/QUrl>
00028
00029 #include <kdebug.h>
00030
00031 #include "ksslx509map.h"
00032
00033 class KSSLPeerInfoPrivate {
00034 public:
00035 KSSLPeerInfoPrivate() {}
00036 ~KSSLPeerInfoPrivate() { }
00037 QString peerHost;
00038 };
00039
00040
00041
00042 KSSLPeerInfo::KSSLPeerInfo()
00043 :d(new KSSLPeerInfoPrivate)
00044 {
00045 }
00046
00047 KSSLPeerInfo::~KSSLPeerInfo() {
00048 delete d;
00049 }
00050
00051 KSSLCertificate& KSSLPeerInfo::getPeerCertificate() {
00052 return m_cert;
00053 }
00054
00055 void KSSLPeerInfo::setPeerHost(const QString &realHost) {
00056 d->peerHost = realHost.trimmed();
00057 while(d->peerHost.endsWith('.'))
00058 d->peerHost.truncate(d->peerHost.length()-1);
00059
00060 d->peerHost = QString::fromLatin1(QUrl::toAce(d->peerHost));
00061 }
00062
00063 bool KSSLPeerInfo::certMatchesAddress() {
00064 #ifdef KSSL_HAVE_SSL
00065 KSSLX509Map certinfo(m_cert.getSubject());
00066 QStringList cns = certinfo.getValue("CN").split(QRegExp("[ \n\r]"), QString::SkipEmptyParts);
00067 cns += m_cert.subjAltNames();
00068
00069 for (QStringList::const_iterator cn = cns.constBegin(); cn != cns.constEnd(); ++cn) {
00070 if (cnMatchesAddress((*cn).trimmed().toLower()))
00071 return true;
00072 }
00073
00074 #endif
00075
00076 return false;
00077 }
00078
00079
00080 bool KSSLPeerInfo::cnMatchesAddress(QString cn) {
00081 #ifdef KSSL_HAVE_SSL
00082 QRegExp rx;
00083
00084 kDebug(7029) << "Matching CN=[" << cn << "] to ["
00085 << d->peerHost << "]" << endl;
00086
00087
00088 if (QRegExp("[^a-zA-Z0-9\\.\\*\\-]").indexIn(cn) >= 0) {
00089 kDebug(7029) << "CN contains invalid characters! Failing.";
00090 return false;
00091 }
00092
00093
00094 while(cn.endsWith('.'))
00095 cn.truncate(cn.length()-1);
00096
00097
00098 if (cn.isEmpty())
00099 return false;
00100
00101
00102 rx.setPattern("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}");
00103 if (rx.exactMatch(d->peerHost))
00104 return d->peerHost == cn;
00105
00106
00107 rx.setPattern("^\\[.*\\]$");
00108 if (rx.exactMatch(d->peerHost))
00109 return d->peerHost == cn;
00110
00111 if (cn.contains('*')) {
00112
00113
00114 QStringList parts = cn.split('.', QString::SkipEmptyParts);
00115
00116 while (parts.count() > 2)
00117 parts.removeFirst();
00118
00119 if (parts.count() != 2) {
00120 return false;
00121 }
00122
00123 if (parts[0].contains('*') || parts[1].contains('*')) {
00124 return false;
00125 }
00126
00127
00128
00129
00130 if (QRegExp(cn, Qt::CaseInsensitive, QRegExp::Wildcard).exactMatch(d->peerHost) &&
00131 cn.split('.', QString::SkipEmptyParts).count() ==
00132 d->peerHost.split('.', QString::SkipEmptyParts).count())
00133 return true;
00134
00135
00136 if (cn.startsWith("*.")) {
00137 QString chopped = cn.mid(2);
00138 if (chopped == d->peerHost) {
00139 return true;
00140 }
00141 }
00142 return false;
00143 }
00144
00145
00146
00147 if (cn == d->peerHost)
00148 return true;
00149 #endif
00150 return false;
00151 }
00152
00153
00154 void KSSLPeerInfo::reset() {
00155 d->peerHost.clear();
00156 }
00157
00158
00159 const QString& KSSLPeerInfo::peerHost() const {
00160 return d->peerHost;
00161 }
00162