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., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 */ 00020 00021 00022 #ifdef HAVE_CONFIG_H 00023 #include <config.h> 00024 #endif 00025 00026 #include <kopenssl.h> 00027 00028 #include <tqstring.h> 00029 #include <tqfile.h> 00030 #include <ksslall.h> 00031 #include <kdebug.h> 00032 #include <ktempfile.h> 00033 #include <kmdcodec.h> 00034 00035 #include <assert.h> 00036 00037 00038 KSSLPKCS7::KSSLPKCS7() { 00039 _pkcs = NULL; 00040 _cert = NULL; 00041 kossl = KOSSL::self(); 00042 } 00043 00044 00045 00046 KSSLPKCS7::~KSSLPKCS7() { 00047 #ifdef KSSL_HAVE_SSL 00048 if (_pkcs) kossl->PKCS7_free(_pkcs); 00049 #endif 00050 if (_cert) delete _cert; 00051 } 00052 00053 00054 KSSLPKCS7* KSSLPKCS7::fromString(TQString base64) { 00055 #ifdef KSSL_HAVE_SSL 00056 KTempFile ktf; 00057 00058 if (base64.isEmpty()) return NULL; 00059 TQByteArray qba, qbb = TQCString(base64.latin1()).copy(); 00060 KCodecs::base64Decode(qbb, qba); 00061 ktf.file()->writeBlock(qba); 00062 ktf.close(); 00063 KSSLPKCS7* rc = loadCertFile(ktf.name()); 00064 ktf.unlink(); 00065 return rc; 00066 #endif 00067 return NULL; 00068 } 00069 00070 00071 00072 KSSLPKCS7* KSSLPKCS7::loadCertFile(TQString filename) { 00073 #ifdef KSSL_HAVE_SSL 00074 TQFile qf(filename); 00075 PKCS7 *newpkcs = NULL; 00076 00077 if (!qf.open(IO_ReadOnly)) 00078 return NULL; 00079 00080 FILE *fp = fdopen(qf.handle(), "r"); 00081 if (!fp) return NULL; 00082 00083 newpkcs = KOSSL::self()->d2i_PKCS7_fp(fp, &newpkcs); 00084 00085 if (!newpkcs) return NULL; 00086 00087 KSSLPKCS7 *c = new KSSLPKCS7; 00088 c->setCert(newpkcs); 00089 00090 return c; 00091 #endif 00092 return NULL; 00093 } 00094 00095 00096 void KSSLPKCS7::setCert(PKCS7 *c) { 00097 #ifdef KSSL_HAVE_SSL 00098 _pkcs = c; 00099 //STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7); 00100 //X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si); 00101 // set _chain and _cert here. 00102 #endif 00103 } 00104 00105 00106 KSSLCertificate *KSSLPKCS7::getCertificate() { 00107 return _cert; 00108 } 00109 00110 00111 KSSLCertChain *KSSLPKCS7::getChain() { 00112 return _chain; 00113 } 00114 00115 00116 TQString KSSLPKCS7::toString() { 00117 TQString base64; 00118 #ifdef KSSL_HAVE_SSL 00119 unsigned char *p; 00120 int len; 00121 00122 len = kossl->i2d_PKCS7(_pkcs, NULL); 00123 if (len >= 0) { 00124 char *buf = new char[len]; 00125 p = (unsigned char *)buf; 00126 kossl->i2d_PKCS7(_pkcs, &p); 00127 TQByteArray qba; 00128 qba.setRawData(buf, len); 00129 base64 = KCodecs::base64Encode(qba); 00130 qba.resetRawData(buf, len); 00131 delete[] buf; 00132 } 00133 #endif 00134 return base64; 00135 } 00136 00137 00138 00139 bool KSSLPKCS7::toFile(TQString filename) { 00140 #ifdef KSSL_HAVE_SSL 00141 TQFile out(filename); 00142 00143 if (!out.open(IO_WriteOnly)) return false; 00144 00145 int fd = out.handle(); 00146 FILE *fp = fdopen(fd, "w"); 00147 00148 if (!fp) { 00149 unlink(filename.latin1()); 00150 return false; 00151 } 00152 00153 kossl->i2d_PKCS7_fp(fp, _pkcs); 00154 00155 fclose(fp); 00156 return true; 00157 #endif 00158 return false; 00159 } 00160 00161 00162 KSSLCertificate::KSSLValidation KSSLPKCS7::validate() { 00163 #ifdef KSSL_HAVE_SSL 00164 KSSLCertificate::KSSLValidation xx = _cert->validate(); 00165 return xx; 00166 #else 00167 return KSSLCertificate::NoSSL; 00168 #endif 00169 } 00170 00171 00172 KSSLCertificate::KSSLValidation KSSLPKCS7::revalidate() { 00173 if (_cert) 00174 return _cert->revalidate(); 00175 return KSSLCertificate::Unknown; 00176 } 00177 00178 00179 bool KSSLPKCS7::isValid() { 00180 return (validate() == KSSLCertificate::Ok); 00181 } 00182 00183 00184 TQString KSSLPKCS7::name() { 00185 if (_cert) 00186 return _cert->getSubject(); 00187 return TQString(); 00188 } 00189