kio Library API Documentation

ksslpkcs7.cc

00001 /* This file is part of the KDE project
00002  *
00003  * Copyright (C) 2001 George Staikos <staikos@kde.org>
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Library General Public License
00016  * along with this library; see the file COPYING.LIB.  If not, write to
00017  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018  * Boston, MA 02111-1307, USA.
00019  */
00020 
00021 
00022 #ifdef HAVE_CONFIG_H
00023 #include <config.h>
00024 #endif
00025 
00026 #include <kopenssl.h>
00027 
00028 #include <qstring.h>
00029 #include <qfile.h>
00030 #include <ksslall.h>
00031 #include <kdebug.h>
00032 #include <ktempfile.h>
00033 #include <kmdcodec.h>
00034 
00035 #include <assert.h>
00036 
00037 #ifdef KSSL_HAVE_SSL
00038 #define sk_new kossl->sk_new
00039 #define sk_push kossl->sk_push
00040 #define sk_free kossl->sk_free
00041 #define sk_value kossl->sk_value
00042 #define sk_num kossl->sk_num
00043 #define sk_dup kossl->sk_dup
00044 #endif
00045 
00046 
00047 KSSLPKCS7::KSSLPKCS7() {
00048    _pkcs = NULL;
00049    _cert = NULL;
00050    kossl = KOSSL::self();
00051 }
00052 
00053 
00054 
00055 KSSLPKCS7::~KSSLPKCS7() {
00056 #ifdef KSSL_HAVE_SSL
00057    if (_pkcs) kossl->PKCS7_free(_pkcs);
00058 #endif
00059    if (_cert) delete _cert;
00060 }
00061 
00062 
00063 KSSLPKCS7* KSSLPKCS7::fromString(QString base64) {
00064 #ifdef KSSL_HAVE_SSL
00065 KTempFile ktf;
00066 
00067     if (base64.isEmpty()) return NULL;
00068     QByteArray qba, qbb = QCString(base64.latin1()).copy();
00069     KCodecs::base64Decode(qbb, qba);
00070     ktf.file()->writeBlock(qba);
00071     ktf.close();
00072     KSSLPKCS7* rc = loadCertFile(ktf.name());
00073     ktf.unlink();
00074     return rc;
00075 #endif
00076 return NULL;
00077 }
00078 
00079 
00080 
00081 KSSLPKCS7* KSSLPKCS7::loadCertFile(QString filename) {
00082 #ifdef KSSL_HAVE_SSL
00083 QFile qf(filename);
00084 PKCS7 *newpkcs = NULL;
00085 
00086   if (!qf.open(IO_ReadOnly))
00087     return NULL;
00088 
00089   FILE *fp = fdopen(qf.handle(), "r");
00090   if (!fp) return NULL;
00091 
00092   newpkcs = KOSSL::self()->d2i_PKCS7_fp(fp, &newpkcs);
00093 
00094   if (!newpkcs) return NULL;
00095 
00096   KSSLPKCS7 *c = new KSSLPKCS7;
00097   c->setCert(newpkcs);
00098 
00099   return c;
00100 #endif
00101 return NULL;
00102 }
00103 
00104 
00105 void KSSLPKCS7::setCert(PKCS7 *c) {
00106 #ifdef KSSL_HAVE_SSL
00107    _pkcs = c;
00108    //STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7);
00109    //X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si);
00110    // set _chain and _cert here.
00111 #endif
00112 }
00113 
00114 
00115 KSSLCertificate *KSSLPKCS7::getCertificate() {
00116    return _cert;
00117 }
00118 
00119 
00120 KSSLCertChain *KSSLPKCS7::getChain() {
00121    return _chain;
00122 }
00123 
00124 
00125 QString KSSLPKCS7::toString() {
00126 QString base64;
00127 #ifdef KSSL_HAVE_SSL
00128 unsigned char *p;
00129 int len;
00130 
00131    len = kossl->i2d_PKCS7(_pkcs, NULL);
00132    char *buf = new char[len];
00133    p = (unsigned char *)buf;
00134    kossl->i2d_PKCS7(_pkcs, &p);
00135    QByteArray qba;
00136    qba.setRawData(buf, len);
00137    base64 = KCodecs::base64Encode(qba);
00138    qba.resetRawData(buf, len);
00139    delete[] buf;
00140 #endif
00141 return base64;
00142 }
00143 
00144 
00145 
00146 bool KSSLPKCS7::toFile(QString filename) {
00147 #ifdef KSSL_HAVE_SSL
00148 QFile out(filename);
00149 
00150    if (!out.open(IO_WriteOnly)) return false;
00151 
00152    int fd = out.handle();
00153    FILE *fp = fdopen(fd, "w");
00154 
00155    if (!fp) {
00156       unlink(filename.latin1());
00157       return false;
00158    }
00159 
00160    kossl->i2d_PKCS7_fp(fp, _pkcs);
00161 
00162    fclose(fp);
00163    return true;
00164 #endif
00165 return false;
00166 }
00167 
00168 
00169 KSSLCertificate::KSSLValidation KSSLPKCS7::validate() {
00170 #ifdef KSSL_HAVE_SSL
00171 KSSLCertificate::KSSLValidation xx = _cert->validate();
00172 return xx;
00173 #else
00174 return KSSLCertificate::NoSSL;
00175 #endif
00176 }
00177 
00178 
00179 KSSLCertificate::KSSLValidation KSSLPKCS7::revalidate() {
00180    if (_cert)
00181       return _cert->revalidate();
00182    return KSSLCertificate::Unknown;
00183 }
00184 
00185 
00186 bool KSSLPKCS7::isValid() {
00187 return (validate() == KSSLCertificate::Ok);
00188 }
00189 
00190 
00191 QString KSSLPKCS7::name() {
00192    if (_cert)
00193       return _cert->getSubject();
00194    return QString();
00195 }
00196 
00197  
00198 #ifdef KSSL_HAVE_SSL
00199 #undef sk_new
00200 #undef sk_push
00201 #undef sk_free
00202 #undef sk_value
00203 #undef sk_num
00204 #undef sk_dup
00205 #endif
00206 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:14:27 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001