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.
162 lines
6.5 KiB
162 lines
6.5 KiB
15 years ago
|
/***************************************************************************
|
||
|
* Copyright (C) 2004 by Leonid Zeitlin *
|
||
|
* lz@europe.com *
|
||
|
* *
|
||
|
* This program is free software; you can redistribute it and/or modify *
|
||
|
* it under the terms of the GNU General Public License as published by *
|
||
|
* the Free Software Foundation; either version 2 of the License, or *
|
||
|
* (at your option) any later version. *
|
||
|
* *
|
||
|
* This program 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 General Public License for more details. *
|
||
|
* *
|
||
|
* You should have received a copy of the GNU General Public License *
|
||
|
* along with this program; if not, write to the *
|
||
|
* Free Software Foundation, Inc., *
|
||
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
|
||
|
***************************************************************************/
|
||
|
|
||
|
#include <config.h>
|
||
12 years ago
|
#include "tdefile_cert.h"
|
||
15 years ago
|
|
||
|
#include <kgenericfactory.h>
|
||
|
#include <ksslcertificate.h>
|
||
|
#include <ksslx509map.h>
|
||
|
#include <kopenssl.h>
|
||
|
//#include <kstandarddirs.h>
|
||
|
//#include <kdebug.h>
|
||
12 years ago
|
//#include <tdeio/global.h>
|
||
15 years ago
|
|
||
15 years ago
|
#include <tqdatetime.h>
|
||
|
#include <tqfile.h>
|
||
|
#include <tqcstring.h>
|
||
|
//#include <tqfileinfo.h>
|
||
|
//#include <tqdir.h>
|
||
15 years ago
|
|
||
|
typedef KGenericFactory<CertPlugin> CertFactory;
|
||
|
|
||
12 years ago
|
K_EXPORT_COMPONENT_FACTORY(tdefile_cert, CertFactory("tdefile-cert"))
|
||
15 years ago
|
|
||
13 years ago
|
CertPlugin::CertPlugin(TQObject *parent, const char *name, const TQStringList &args)
|
||
|
: KFilePlugin(parent, name, args)
|
||
15 years ago
|
{
|
||
|
//add the mimetype here - example:
|
||
|
//KFileMimeTypeInfo* info = addMimeTypeInfo( "text/html" );
|
||
|
KFileMimeTypeInfo* info = addMimeTypeInfo( "application/x-x509-ca-cert" );
|
||
|
|
||
|
// our new group
|
||
|
KFileMimeTypeInfo::GroupInfo* group = 0L;
|
||
|
KFileMimeTypeInfo::ItemInfo* item;
|
||
|
|
||
|
group = addGroupInfo(info, "certInfo", i18n("Certificate Information"));
|
||
15 years ago
|
item = addItemInfo(group, "ValidFrom", i18n("Valid From"), TQVariant::DateTime);
|
||
|
item = addItemInfo(group, "ValidUntil", i18n("Valid Until"), TQVariant::DateTime);
|
||
|
item = addItemInfo(group, "State", i18n("State"), TQVariant::String);
|
||
|
item = addItemInfo(group, "SerialNo", i18n("Serial Number"), TQVariant::String);
|
||
15 years ago
|
|
||
|
group = addGroupInfo(info, "certSubjectInfo", i18n("Subject"));
|
||
15 years ago
|
item = addItemInfo(group, "O", i18n("Organization"), TQVariant::String);
|
||
|
item = addItemInfo(group, "OU", i18n("Organizational Unit"), TQVariant::String);
|
||
|
item = addItemInfo(group, "L", i18n("Locality"), TQVariant::String);
|
||
|
item = addItemInfo(group, "C", i18n("Country"), TQVariant::String);
|
||
|
item = addItemInfo(group, "CN", i18n("Common Name"), TQVariant::String);
|
||
|
item = addItemInfo(group, "E", i18n("Email"), TQVariant::String);
|
||
15 years ago
|
|
||
|
group = addGroupInfo(info, "certIssuerInfo", i18n("Issuer"));
|
||
15 years ago
|
item = addItemInfo(group, "O", i18n("Organization"), TQVariant::String);
|
||
|
item = addItemInfo(group, "OU", i18n("Organizational Unit"), TQVariant::String);
|
||
|
item = addItemInfo(group, "L", i18n("Locality"), TQVariant::String);
|
||
|
item = addItemInfo(group, "C", i18n("Country"), TQVariant::String);
|
||
|
item = addItemInfo(group, "CN", i18n("Common Name"), TQVariant::String);
|
||
|
item = addItemInfo(group, "E", i18n("Email"), TQVariant::String);
|
||
15 years ago
|
|
||
|
//setUnit(item, KFileMimeTypeInfo::KiloBytes);
|
||
|
|
||
|
// strings are possible, too:
|
||
15 years ago
|
//addItemInfo(group, "Text", i18n("Document Type"), TQVariant::String);
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
void CertPlugin::appendDNItems(KFileMetaInfoGroup &group, const TQString &DN)
|
||
15 years ago
|
{
|
||
|
KSSLX509Map map(DN);
|
||
15 years ago
|
TQString value;
|
||
|
//TQString dbg;
|
||
|
TQStringList keys = group.supportedKeys();
|
||
|
TQStringList::ConstIterator end = keys.end();
|
||
|
for (TQStringList::ConstIterator it = keys.begin(); it != end; ++it) {
|
||
15 years ago
|
value = map.getValue(*it);
|
||
|
//dbg += *it + " = " + value + "; ";
|
||
|
if (!value.isNull()) appendItem(group, *it, value);
|
||
|
//appendItem(group, "CN", dbg);
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
static KSSLCertificate *readCertFromFile(const TQString &path)
|
||
15 years ago
|
{
|
||
|
KSSLCertificate *ret = NULL;
|
||
|
|
||
15 years ago
|
TQFile file(path);
|
||
15 years ago
|
if (!file.open(IO_ReadOnly)) return NULL;
|
||
15 years ago
|
TQByteArray file_data = file.readAll();
|
||
15 years ago
|
file.close();
|
||
|
|
||
15 years ago
|
TQCString file_string = TQCString(file_data.data(), file_data.size());
|
||
15 years ago
|
// try as is:
|
||
|
ret = KSSLCertificate::fromString(file_string);
|
||
|
if (ret) return ret;
|
||
|
// didn't work. Let's see if begin/end lines are there:
|
||
|
KOSSL::self()->ERR_clear_error();
|
||
|
const char *begin_line = "-----BEGIN CERTIFICATE-----\n";
|
||
|
const char *end_line = "\n-----END CERTIFICATE-----";
|
||
13 years ago
|
int begin_pos = file_string.find(begin_line);
|
||
15 years ago
|
if (begin_pos >= 0) {
|
||
|
begin_pos += strlen(begin_line);
|
||
13 years ago
|
int end_pos = file_string.find(end_line, begin_pos);
|
||
15 years ago
|
if (end_pos >= 0) {
|
||
|
// read the data between begin and end lines
|
||
15 years ago
|
TQCString body = file_string.mid(begin_pos, end_pos - begin_pos);
|
||
15 years ago
|
ret = KSSLCertificate::fromString(body);
|
||
|
return ret; // even if it's NULL, we can't help it
|
||
|
}
|
||
|
}
|
||
|
// still didn't work. Assume the file was in DER (binary) encoding
|
||
|
unsigned char *p = (unsigned char*) file_data.data();
|
||
|
KOSSL::self()->ERR_clear_error();
|
||
|
X509 *x = KOSSL::self()->d2i_X509(NULL, &p, file_data.size());
|
||
|
if (x) {
|
||
|
ret = KSSLCertificate::fromX509(x);
|
||
|
KOSSL::self()->X509_free(x);
|
||
|
return ret;
|
||
|
}
|
||
|
else return NULL;
|
||
|
}
|
||
|
|
||
|
bool CertPlugin::readInfo(KFileMetaInfo& info, uint /*what*/)
|
||
|
{
|
||
|
KSSLCertificate *cert = readCertFromFile(info.path());
|
||
|
if (cert) {
|
||
|
KFileMetaInfoGroup group = appendGroup(info, "certInfo");
|
||
|
appendItem(group, "ValidFrom", cert->getQDTNotBefore());
|
||
|
appendItem(group, "ValidUntil", cert->getQDTNotAfter());
|
||
|
appendItem(group, "State", KSSLCertificate::verifyText(cert->validate()));
|
||
|
appendItem(group, "SerialNo", cert->getSerialNumber());
|
||
|
|
||
|
group = appendGroup(info, "certSubjectInfo");
|
||
|
appendDNItems(group, cert->getSubject());
|
||
|
|
||
|
group = appendGroup(info, "certIssuerInfo");
|
||
|
appendDNItems(group, cert->getIssuer());
|
||
|
|
||
|
delete cert;
|
||
|
return true;
|
||
|
}
|
||
|
else {
|
||
|
KOSSL::self()->ERR_clear_error(); // don't leave errors behind
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
#include "tdefile_cert.moc"
|