ksslpkcs12.cc
/* This file is part of the KDE project
*
* Copyright (C) 2001 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
#include <kopenssl.h>
#include <qstring.h>
#include <qfile.h>
#include <ksslall.h>
#include <kdebug.h>
#include <ktempfile.h>
#include <kmdcodec.h>
#include <assert.h>
#ifdef KSSL_HAVE_SSL
#define sk_new kossl->sk_new
#define sk_push kossl->sk_push
#define sk_free kossl->sk_free
#define sk_value kossl->sk_value
#define sk_num kossl->sk_num
#define sk_dup kossl->sk_dup
#define sk_pop kossl->sk_pop
#endif
KSSLPKCS12::KSSLPKCS12() {
_pkcs = NULL;
_pkey = NULL;
_cert = NULL;
_caStack = NULL;
kossl = KOSSL::self();
}
KSSLPKCS12::~KSSLPKCS12() {
#ifdef KSSL_HAVE_SSL
if (_pkey) kossl->EVP_PKEY_free(_pkey);
if (_caStack) {
for (;;) {
X509* x5 = sk_X509_pop(_caStack);
if (!x5) break;
kossl->X509_free(x5);
}
sk_X509_free(_caStack);
}
if (_pkcs) kossl->PKCS12_free(_pkcs);
#endif
if (_cert) delete _cert;
}
KSSLPKCS12* KSSLPKCS12::fromString(QString base64, QString password) {
#ifdef KSSL_HAVE_SSL
KTempFile ktf;
if (base64.isEmpty()) return NULL;
QByteArray qba, qbb = QCString(base64.latin1()).copy();
KCodecs::base64Decode(qbb, qba);
ktf.file()->writeBlock(qba);
ktf.close();
KSSLPKCS12* rc = loadCertFile(ktf.name(), password);
ktf.unlink();
return rc;
#endif
return NULL;
}
KSSLPKCS12* KSSLPKCS12::loadCertFile(QString filename, QString password) {
#ifdef KSSL_HAVE_SSL
QFile qf(filename);
PKCS12 *newpkcs = NULL;
if (!qf.open(IO_ReadOnly))
return NULL;
FILE *fp = fdopen(qf.handle(), "r");
if (!fp) return NULL;
newpkcs = KOSSL::self()->d2i_PKCS12_fp(fp, &newpkcs);
fclose(fp);
if (!newpkcs) {
KOSSL::self()->ERR_clear_error();
return NULL;
}
KSSLPKCS12 *c = new KSSLPKCS12;
c->setCert(newpkcs);
// Now we parse it to see if we can decrypt it and interpret it
if (!c->parse(password)) {
delete c; c = NULL;
}
return c;
#endif
return NULL;
}
void KSSLPKCS12::setCert(PKCS12 *c) {
#ifdef KSSL_HAVE_SSL
_pkcs = c;
#endif
}
bool KSSLPKCS12::changePassword(QString pold, QString pnew) {
#ifdef KSSL_HAVE_SSL
// OpenSSL makes me cast away the const here. argh
return (0 == kossl->PKCS12_newpass(_pkcs,
pold.isNull() ? (char *)"" : (char *)pold.latin1(),
pnew.isNull() ? (char *)"" : (char *)pnew.latin1()));
#endif
return false;
}
bool KSSLPKCS12::parse(QString pass) {
#ifdef KSSL_HAVE_SSL
X509 *x = NULL;
assert(_pkcs); // if you're calling this before pkcs gets set, it's a BUG!
if (_cert) delete _cert;
if (_pkey) kossl->EVP_PKEY_free(_pkey);
if (_caStack) {
for (;;) {
X509* x5 = sk_X509_pop(_caStack);
if (!x5) break;
kossl->X509_free(x5);
}
sk_X509_free(_caStack);
}
_pkey = NULL;
_caStack = NULL;
_cert = NULL;
int rc = kossl->PKCS12_parse(_pkcs, pass.latin1(), &_pkey, &x, &_caStack);
if (rc == 1) {
// kdDebug(7029) << "PKCS12_parse success" << endl;
if (x) {
_cert = new KSSLCertificate;
_cert->setCert(x);
if (_caStack) {
_cert->setChain(_caStack);
}
return true;
}
} else {
_caStack = NULL;
_pkey = NULL;
kossl->ERR_clear_error();
}
#endif
return false;
}
EVP_PKEY *KSSLPKCS12::getPrivateKey() {
return _pkey;
}
KSSLCertificate *KSSLPKCS12::getCertificate() {
return _cert;
}
QString KSSLPKCS12::toString() {
QString base64;
#ifdef KSSL_HAVE_SSL
unsigned char *p;
int len;
len = kossl->i2d_PKCS12(_pkcs, NULL);
if (len >= 0) {
char *buf = new char[len];
p = (unsigned char *)buf;
kossl->i2d_PKCS12(_pkcs, &p);
QByteArray qba;
qba.setRawData(buf, len);
base64 = KCodecs::base64Encode(qba);
qba.resetRawData(buf, len);
delete[] buf;
}
#endif
return base64;
}
bool KSSLPKCS12::toFile(QString filename) {
#ifdef KSSL_HAVE_SSL
QFile out(filename);
if (!out.open(IO_WriteOnly)) return false;
int fd = out.handle();
FILE *fp = fdopen(fd, "w");
if (!fp) {
unlink(filename.latin1());
return false;
}
kossl->i2d_PKCS12_fp(fp, _pkcs);
fclose(fp);
return true;
#endif
return false;
}
KSSLCertificate::KSSLValidation KSSLPKCS12::validate() {
return validate(KSSLCertificate::SSLServer);
}
KSSLCertificate::KSSLValidation KSSLPKCS12::validate(KSSLCertificate::KSSLPurpose p) {
#ifdef KSSL_HAVE_SSL
KSSLCertificate::KSSLValidation xx = _cert->validate(p);
if (1 != kossl->X509_check_private_key(_cert->getCert(), _pkey)) {
xx = KSSLCertificate::PrivateKeyFailed;
}
return xx;
#else
return KSSLCertificate::NoSSL;
#endif
}
KSSLCertificate::KSSLValidation KSSLPKCS12::revalidate() {
return revalidate(KSSLCertificate::SSLServer);
}
KSSLCertificate::KSSLValidation KSSLPKCS12::revalidate(KSSLCertificate::KSSLPurpose p) {
return _cert->revalidate(p);
}
bool KSSLPKCS12::isValid() {
return isValid(KSSLCertificate::SSLServer);
}
bool KSSLPKCS12::isValid(KSSLCertificate::KSSLPurpose p) {
return (validate(p) == KSSLCertificate::Ok);
}
QString KSSLPKCS12::name() {
return _cert->getSubject();
}
#ifdef KSSL_HAVE_SSL
#undef sk_new
#undef sk_push
#undef sk_free
#undef sk_value
#undef sk_num
#undef sk_pop
#undef sk_dup
#endif
KSSLPKCS12::setCert
void setCert(PKCS12 *c)
Raw set the PKCS12 object.
Definition: ksslpkcs12.cc:126
KSSLCertificate::setCert
bool setCert(QString &cert)
Re-set the certificate from a base64 string.
Definition: ksslcertificate.cc:1058
KSSLPKCS12::~KSSLPKCS12
virtual ~KSSLPKCS12()
Destroy this PKCS#12 certificate.
Definition: ksslpkcs12.cc:58
KSSLCertificate::validate
KSSLValidation validate()
Check if this is a valid certificate.
Definition: ksslcertificate.cc:590
KSSLPKCS12::changePassword
bool changePassword(QString pold, QString pnew)
Change the password of the PKCS#12 in memory.
Definition: ksslpkcs12.cc:133
KSSLCertificate
KDE X.509 Certificate.
Definition: ksslcertificate.h:75
KSSLCertificate::revalidate
KSSLValidation revalidate()
Check if this is a valid certificate.
Definition: ksslcertificate.cc:753
KSSLPKCS12::toString
QString toString()
Convert to a Base64 string.
Definition: ksslpkcs12.cc:196
KSSLPKCS12::toFile
bool toFile(QString filename)
Write the PKCS#12 to a file in raw mode.
Definition: ksslpkcs12.cc:219
KSSLCertificate::getSubject
QString getSubject() const
Get the subject of the certificate (X.509 map).
Definition: ksslcertificate.cc:166
KSSLPKCS12::name
QString name()
The name of this certificate.
Definition: ksslpkcs12.cc:281
KSSLPKCS12
KDE PKCS#12 Certificate.
Definition: ksslpkcs12.h:61
KSSLPKCS12::revalidate
KSSLCertificate::KSSLValidation revalidate()
Check the X.509 and private key to make sure they're valid.
Definition: ksslpkcs12.cc:261
KSSLCertificate::KSSLValidation
KSSLValidation
A CA certificate can be validated as Irrelevant when it was not used to sign any other relevant certi...
Definition: ksslcertificate.h:113
KSSLPKCS12::validate
KSSLCertificate::KSSLValidation validate()
Check the X.509 and private key to make sure they're valid.
Definition: ksslpkcs12.cc:242
KSSLPKCS12::loadCertFile
static KSSLPKCS12 * loadCertFile(QString filename, QString password="")
Create a KSSLPKCS12 object by reading a PKCS#12 file.
Definition: ksslpkcs12.cc:93
KSSLPKCS12::getPrivateKey
EVP_PKEY * getPrivateKey()
Get the private key.
Definition: ksslpkcs12.cc:186
KSSLPKCS12::isValid
bool isValid()
Check if the X.509 and private key are valid.
Definition: ksslpkcs12.cc:271
KSSLPKCS12::fromString
static KSSLPKCS12 * fromString(QString base64, QString password="")
Create a KSSLPKCS12 object from a Base64 in a QString.
Definition: ksslpkcs12.cc:75
KSSLPKCS12::getCertificate
KSSLCertificate * getCertificate()
Get the X.509 certificate.
Definition: ksslpkcs12.cc:191

KDE Home | KDE Accessibility Home | Description of Access Keys