You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
172 lines
4.2 KiB
172 lines
4.2 KiB
15 years ago
|
/* This file is part of the KDE project
|
||
|
*
|
||
|
* Copyright (C) 2000-2003 George Staikos <staikos@kde.org>
|
||
|
*
|
||
|
* This library is free software; you can redistribute it and/or
|
||
|
* modify it under the terms of the GNU Library General Public
|
||
|
* License as published by the Free Software Foundation; either
|
||
|
* version 2 of the License, or (at your option) any later version.
|
||
|
*
|
||
|
* This library is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
* Library General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Library General Public License
|
||
|
* along with this library; see the file COPYING.LIB. If not, write to
|
||
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
* Boston, MA 02110-1301, USA.
|
||
|
*/
|
||
|
|
||
|
#ifdef HAVE_CONFIG_H
|
||
|
#include <config.h>
|
||
|
#endif
|
||
|
|
||
15 years ago
|
#include <tqregexp.h>
|
||
15 years ago
|
|
||
|
#include "ksslpeerinfo.h"
|
||
|
#include <kdebug.h>
|
||
|
|
||
|
#include <ksockaddr.h>
|
||
|
#include <kextsock.h>
|
||
|
#include <netsupp.h>
|
||
|
#ifndef Q_WS_WIN //TODO kresolver not ported
|
||
|
#include "kresolver.h"
|
||
|
#endif
|
||
|
|
||
|
#include "ksslx509map.h"
|
||
|
|
||
|
class KSSLPeerInfoPrivate {
|
||
|
public:
|
||
|
KSSLPeerInfoPrivate() {}
|
||
|
~KSSLPeerInfoPrivate() { }
|
||
15 years ago
|
TQString peerHost;
|
||
15 years ago
|
};
|
||
|
|
||
|
|
||
|
|
||
|
KSSLPeerInfo::KSSLPeerInfo() {
|
||
|
d = new KSSLPeerInfoPrivate;
|
||
|
}
|
||
|
|
||
|
KSSLPeerInfo::~KSSLPeerInfo() {
|
||
|
delete d;
|
||
|
}
|
||
|
|
||
|
KSSLCertificate& KSSLPeerInfo::getPeerCertificate() {
|
||
|
return m_cert;
|
||
|
}
|
||
|
|
||
15 years ago
|
void KSSLPeerInfo::setPeerHost(TQString realHost) {
|
||
15 years ago
|
d->peerHost = realHost.stripWhiteSpace();
|
||
|
while(d->peerHost.endsWith("."))
|
||
|
d->peerHost.truncate(d->peerHost.length()-1);
|
||
|
|
||
|
#ifdef Q_WS_WIN //TODO kresolver not ported
|
||
|
d->peerHost = d->peerHost.lower();
|
||
|
#else
|
||
13 years ago
|
d->peerHost = TQString::fromLatin1(KNetwork::KResolver::domainToAscii(d->peerHost));
|
||
15 years ago
|
#endif
|
||
|
}
|
||
|
|
||
|
bool KSSLPeerInfo::certMatchesAddress() {
|
||
|
#ifdef KSSL_HAVE_SSL
|
||
|
KSSLX509Map certinfo(m_cert.getSubject());
|
||
15 years ago
|
TQStringList cns = TQStringList::split(TQRegExp("[ \n\r]"), certinfo.getValue("CN"));
|
||
15 years ago
|
cns += m_cert.subjAltNames();
|
||
|
|
||
15 years ago
|
for (TQStringList::Iterator cn = cns.begin(); cn != cns.end(); ++cn) {
|
||
15 years ago
|
if (cnMatchesAddress((*cn).stripWhiteSpace().lower()))
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
#endif
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
|
||
15 years ago
|
bool KSSLPeerInfo::cnMatchesAddress(TQString cn) {
|
||
15 years ago
|
#ifdef KSSL_HAVE_SSL
|
||
15 years ago
|
TQRegExp rx;
|
||
15 years ago
|
|
||
|
kdDebug(7029) << "Matching CN=[" << cn << "] to ["
|
||
|
<< d->peerHost << "]" << endl;
|
||
|
|
||
|
// Check for invalid characters
|
||
15 years ago
|
if (TQRegExp("[^a-zA-Z0-9\\.\\*\\-]").search(cn) >= 0) {
|
||
14 years ago
|
kdDebug(7029) << "CN contains invalid characters! Failing." << endl;
|
||
15 years ago
|
return false;
|
||
|
}
|
||
|
|
||
|
// Domains can legally end with '.'s. We don't need them though.
|
||
|
while(cn.endsWith("."))
|
||
|
cn.truncate(cn.length()-1);
|
||
|
|
||
|
// Do not let empty CN's get by!!
|
||
|
if (cn.isEmpty())
|
||
|
return false;
|
||
|
|
||
|
// Check for IPv4 address
|
||
|
rx.setPattern("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}");
|
||
|
if (rx.exactMatch(d->peerHost))
|
||
|
return d->peerHost == cn;
|
||
|
|
||
|
// Check for IPv6 address here...
|
||
|
rx.setPattern("^\\[.*\\]$");
|
||
|
if (rx.exactMatch(d->peerHost))
|
||
|
return d->peerHost == cn;
|
||
|
|
||
14 years ago
|
if (cn.contains('*')) {
|
||
15 years ago
|
// First make sure that there are at least two valid parts
|
||
|
// after the wildcard (*).
|
||
15 years ago
|
TQStringList parts = TQStringList::split('.', cn, false);
|
||
15 years ago
|
|
||
|
while (parts.count() > 2)
|
||
|
parts.remove(parts.begin());
|
||
|
|
||
|
if (parts.count() != 2) {
|
||
|
return false; // we don't allow *.root - that's bad
|
||
|
}
|
||
|
|
||
14 years ago
|
if (parts[0].contains('*') || parts[1].contains('*')) {
|
||
15 years ago
|
return false;
|
||
|
}
|
||
|
|
||
|
// RFC2818 says that *.example.com should match against
|
||
|
// foo.example.com but not bar.foo.example.com
|
||
|
// (ie. they must have the same number of parts)
|
||
15 years ago
|
if (TQRegExp(cn, false, true).exactMatch(d->peerHost) &&
|
||
|
TQStringList::split('.', cn, false).count() ==
|
||
|
TQStringList::split('.', d->peerHost, false).count())
|
||
15 years ago
|
return true;
|
||
|
|
||
|
// *.example.com must match example.com also. Sigh..
|
||
|
if (cn.startsWith("*.")) {
|
||
15 years ago
|
TQString chopped = cn.mid(2);
|
||
15 years ago
|
if (chopped == d->peerHost) {
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
// We must have an exact match in this case (insensitive though)
|
||
|
// (note we already did .lower())
|
||
|
if (cn == d->peerHost)
|
||
|
return true;
|
||
|
#endif
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
|
||
|
void KSSLPeerInfo::reset() {
|
||
15 years ago
|
d->peerHost = TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
|
|
||
15 years ago
|
const TQString& KSSLPeerInfo::peerHost() const {
|
||
15 years ago
|
return d->peerHost;
|
||
|
}
|
||
|
|