kio
ksslpkcs7.cc
Go to the documentation of this file.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 if (len >= 0) {
00133 char *buf = new char[len];
00134 p = (unsigned char *)buf;
00135 kossl->i2d_PKCS7(_pkcs, &p);
00136 QByteArray qba;
00137 qba.setRawData(buf, len);
00138 base64 = KCodecs::base64Encode(qba);
00139 qba.resetRawData(buf, len);
00140 delete[] buf;
00141 }
00142 #endif
00143 return base64;
00144 }
00145
00146
00147
00148 bool KSSLPKCS7::toFile(QString filename) {
00149 #ifdef KSSL_HAVE_SSL
00150 QFile out(filename);
00151
00152 if (!out.open(IO_WriteOnly)) return false;
00153
00154 int fd = out.handle();
00155 FILE *fp = fdopen(fd, "w");
00156
00157 if (!fp) {
00158 unlink(filename.latin1());
00159 return false;
00160 }
00161
00162 kossl->i2d_PKCS7_fp(fp, _pkcs);
00163
00164 fclose(fp);
00165 return true;
00166 #endif
00167 return false;
00168 }
00169
00170
00171 KSSLCertificate::KSSLValidation KSSLPKCS7::validate() {
00172 #ifdef KSSL_HAVE_SSL
00173 KSSLCertificate::KSSLValidation xx = _cert->validate();
00174 return xx;
00175 #else
00176 return KSSLCertificate::NoSSL;
00177 #endif
00178 }
00179
00180
00181 KSSLCertificate::KSSLValidation KSSLPKCS7::revalidate() {
00182 if (_cert)
00183 return _cert->revalidate();
00184 return KSSLCertificate::Unknown;
00185 }
00186
00187
00188 bool KSSLPKCS7::isValid() {
00189 return (validate() == KSSLCertificate::Ok);
00190 }
00191
00192
00193 QString KSSLPKCS7::name() {
00194 if (_cert)
00195 return _cert->getSubject();
00196 return QString();
00197 }
00198
00199
00200 #ifdef KSSL_HAVE_SSL
00201 #undef sk_new
00202 #undef sk_push
00203 #undef sk_free
00204 #undef sk_value
00205 #undef sk_num
00206 #undef sk_dup
00207 #endif
00208