ksslpkcs7.cc
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00109
00110
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
This file is part of the documentation for kdelibs Version 3.1.5.