KIO
ksslpkcs7.cpp
Go to the documentation of this file.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., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 */ 00020 00021 00022 #include <config.h> 00023 #include <ksslconfig.h> 00024 00025 #include <kopenssl.h> 00026 00027 #include <QtCore/QString> 00028 #include <QtCore/QFile> 00029 00030 #include <ksslall.h> 00031 #include <kdebug.h> 00032 #include <ktemporaryfile.h> 00033 #include <kcodecs.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(const QString &base64) { 00064 #ifdef KSSL_HAVE_SSL 00065 KTemporaryFile ktf; 00066 ktf.open(); 00067 00068 if (base64.isEmpty()) return NULL; 00069 QByteArray qba = QByteArray::fromBase64(base64.toLatin1()); 00070 ktf.write(qba); 00071 ktf.flush(); 00072 KSSLPKCS7* rc = loadCertFile(ktf.fileName()); 00073 return rc; 00074 #endif 00075 return NULL; 00076 } 00077 00078 00079 00080 KSSLPKCS7* KSSLPKCS7::loadCertFile(const QString &filename) { 00081 #ifdef KSSL_HAVE_SSL 00082 QFile qf(filename); 00083 PKCS7 *newpkcs = NULL; 00084 00085 if (!qf.open(QIODevice::ReadOnly)) 00086 return NULL; 00087 00088 FILE *fp = fdopen(qf.handle(), "r"); 00089 if (!fp) return NULL; 00090 00091 newpkcs = KOSSL::self()->d2i_PKCS7_fp(fp, &newpkcs); 00092 00093 if (!newpkcs) return NULL; 00094 00095 KSSLPKCS7 *c = new KSSLPKCS7; 00096 c->setCert(newpkcs); 00097 00098 return c; 00099 #endif 00100 return NULL; 00101 } 00102 00103 00104 void KSSLPKCS7::setCert(PKCS7 *c) { 00105 #ifdef KSSL_HAVE_SSL 00106 _pkcs = c; 00107 //STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7); 00108 //X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si); 00109 // set _chain and _cert here. 00110 #endif 00111 } 00112 00113 00114 KSSLCertificate *KSSLPKCS7::getCertificate() { 00115 return _cert; 00116 } 00117 00118 00119 KSSLCertChain *KSSLPKCS7::getChain() { 00120 return _chain; 00121 } 00122 00123 00124 QString KSSLPKCS7::toString() const { 00125 QString base64; 00126 #ifdef KSSL_HAVE_SSL 00127 unsigned char *p; 00128 int len; 00129 00130 len = kossl->i2d_PKCS7(_pkcs, NULL); 00131 if (len >= 0) { 00132 char *buf = new char[len]; 00133 p = (unsigned char *)buf; 00134 kossl->i2d_PKCS7(_pkcs, &p); 00135 base64 = QByteArray::fromRawData(buf,len).toBase64(); 00136 delete[] buf; 00137 } 00138 #endif 00139 return base64; 00140 } 00141 00142 00143 00144 bool KSSLPKCS7::toFile(const QString &filename) { 00145 #ifdef KSSL_HAVE_SSL 00146 QFile out(filename); 00147 00148 if (!out.open(QIODevice::WriteOnly)) return false; 00149 00150 int fd = out.handle(); 00151 FILE *fp = fdopen(fd, "w"); 00152 00153 if (!fp) { 00154 unlink(filename.toLatin1()); 00155 return false; 00156 } 00157 00158 kossl->i2d_PKCS7_fp(fp, _pkcs); 00159 00160 fclose(fp); 00161 return true; 00162 #endif 00163 return false; 00164 } 00165 00166 00167 KSSLCertificate::KSSLValidation KSSLPKCS7::validate() const { 00168 #ifdef KSSL_HAVE_SSL 00169 KSSLCertificate::KSSLValidation xx = _cert->validate(); 00170 return xx; 00171 #else 00172 return KSSLCertificate::NoSSL; 00173 #endif 00174 } 00175 00176 00177 KSSLCertificate::KSSLValidation KSSLPKCS7::revalidate() { 00178 if (_cert) 00179 return _cert->revalidate(); 00180 return KSSLCertificate::Unknown; 00181 } 00182 00183 00184 bool KSSLPKCS7::isValid() const { 00185 return (validate() == KSSLCertificate::Ok); 00186 } 00187 00188 00189 QString KSSLPKCS7::name() const { 00190 if (_cert) 00191 return _cert->getSubject(); 00192 return QString(); 00193 } 00194 00195 00196 #ifdef KSSL_HAVE_SSL 00197 #undef sk_new 00198 #undef sk_push 00199 #undef sk_free 00200 #undef sk_value 00201 #undef sk_num 00202 #undef sk_dup 00203 #endif 00204