KIO
ksslcertificatehome.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE project 00002 * 00003 * Copyright (C) 2000-2005 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 #include <ksslcertificatehome.h> 00022 #include <ksslcertificate.h> 00023 #include <ksslpkcs12.h> 00024 00025 #include <kconfiggroup.h> 00026 #include <kconfig.h> 00027 00028 QStringList KSSLCertificateHome::getCertificateList() 00029 { 00030 KConfig cfg("ksslcertificates", KConfig::SimpleConfig); 00031 return cfg.groupList(); 00032 } 00033 00034 00035 void KSSLCertificateHome::setDefaultCertificate(const QString & name, const QString &host, bool send, bool prompt) 00036 { 00037 KConfig file("ksslauthmap", KConfig::SimpleConfig); 00038 KConfigGroup cfg(&file, QString::fromLatin1(QUrl::toAce(host))); 00039 00040 cfg.writeEntry("certificate", name); 00041 cfg.writeEntry("send", send); 00042 cfg.writeEntry("prompt", prompt); 00043 cfg.sync(); 00044 } 00045 00046 00047 void KSSLCertificateHome::setDefaultCertificate(KSSLPKCS12 *cert, const QString &host, bool send, bool prompt) { 00048 if (cert) 00049 KSSLCertificateHome::setDefaultCertificate(cert->name(), host, send, prompt); 00050 } 00051 00052 00053 bool KSSLCertificateHome::addCertificate(const QString &filename, const QString &password, bool storePass) { 00054 KSSLPKCS12 *pkcs = KSSLPKCS12::loadCertFile(filename, password); 00055 00056 if (!pkcs) return false; 00057 00058 KSSLCertificateHome::addCertificate(pkcs, storePass?password:QString("")); 00059 delete pkcs; 00060 00061 return true; 00062 } 00063 00064 00065 bool KSSLCertificateHome::addCertificate(KSSLPKCS12 *cert, const QString &passToStore) { 00066 if (!cert) return false; 00067 00068 KConfig file("ksslcertificates", KConfig::SimpleConfig); 00069 KConfigGroup cfg = file.group(cert->name().toLatin1()); 00070 00071 cfg.writeEntry("PKCS12Base64", cert->toString()); 00072 cfg.writeEntry("Password", passToStore); 00073 cfg.sync(); 00074 return true; 00075 } 00076 00077 bool KSSLCertificateHome::deleteCertificate(const QString &filename, const QString &password) { 00078 KSSLPKCS12 *pkcs = KSSLPKCS12::loadCertFile(filename, password); 00079 00080 if (!pkcs) return false; 00081 00082 bool ok = deleteCertificate(pkcs); 00083 delete pkcs; 00084 00085 return ok; 00086 } 00087 00088 bool KSSLCertificateHome::deleteCertificate(KSSLPKCS12 *cert) { 00089 if (!cert) return false; 00090 00091 return deleteCertificateByName(cert->name()); 00092 } 00093 00094 bool KSSLCertificateHome::deleteCertificateByName(const QString &name) { 00095 if (name.isEmpty()) return false; 00096 00097 KConfig cfg("ksslcertificates", KConfig::SimpleConfig); 00098 00099 cfg.deleteGroup(name); 00100 cfg.sync(); 00101 00102 return true; 00103 } 00104 00105 KSSLPKCS12* KSSLCertificateHome::getCertificateByName(const QString &name, const QString &password) 00106 { 00107 KConfig cfg("ksslcertificates", KConfig::SimpleConfig); 00108 if (!cfg.hasGroup(name)) return NULL; 00109 00110 KConfigGroup cg(&cfg, name); 00111 00112 return KSSLPKCS12::fromString(cg.readEntry("PKCS12Base64", ""), password); 00113 } 00114 00115 00116 KSSLPKCS12* KSSLCertificateHome::getCertificateByName(const QString &name) 00117 { 00118 KConfig cfg("ksslcertificates", KConfig::SimpleConfig); 00119 if (!cfg.hasGroup(name)) return NULL; 00120 00121 KConfigGroup cg(&cfg, name); 00122 00123 return KSSLPKCS12::fromString(cg.readEntry("PKCS12Base64", ""), cg.readEntry("Password", "")); 00124 } 00125 00126 00127 bool KSSLCertificateHome::hasCertificateByName(const QString &name) { 00128 KConfig cfg("ksslcertificates", KConfig::SimpleConfig); 00129 if (!cfg.hasGroup(name)) return false; 00130 return true; 00131 } 00132 00133 KSSLPKCS12* KSSLCertificateHome::getCertificateByHost(const QString &host, 00134 const QString &password, KSSLAuthAction *aa) 00135 { 00136 return KSSLCertificateHome::getCertificateByName(KSSLCertificateHome::getDefaultCertificateName(host, aa), password); 00137 } 00138 00139 00140 QString KSSLCertificateHome::getDefaultCertificateName(const QString &host, KSSLAuthAction *aa) 00141 { 00142 KConfig file("ksslauthmap", KConfig::SimpleConfig); 00143 KConfigGroup cfg = file.group(QString::fromLatin1(QUrl::toAce(host))); 00144 00145 if (!cfg.exists()) { 00146 if (aa) *aa = AuthNone; 00147 return QString(); 00148 } else { 00149 if (aa) { 00150 bool tmp = cfg.readEntry("send", false); 00151 *aa = AuthSend; 00152 if (!tmp) { 00153 tmp = cfg.readEntry("prompt", false); 00154 *aa = AuthPrompt; 00155 if (!tmp) { 00156 *aa = AuthDont; 00157 } 00158 } 00159 } 00160 return cfg.readEntry("certificate", ""); 00161 } 00162 } 00163 00164 00165 QString KSSLCertificateHome::getDefaultCertificateName(KSSLAuthAction *aa) 00166 { 00167 KConfig _cfg("cryptodefaults", KConfig::NoGlobals); 00168 KConfigGroup cfg(&_cfg, "Auth"); 00169 if (aa) { 00170 QString am = cfg.readEntry("AuthMethod", ""); 00171 if (am == "send") 00172 *aa = AuthSend; 00173 else if (am == "prompt") 00174 *aa = AuthPrompt; 00175 else 00176 *aa = AuthDont; 00177 } 00178 00179 return cfg.readEntry("DefaultCert", ""); 00180 } 00181 00182 00183 KSSLPKCS12* KSSLCertificateHome::getDefaultCertificate(const QString &password, KSSLAuthAction *aa) { 00184 QString name = KSSLCertificateHome::getDefaultCertificateName(aa); 00185 KConfig cfg("ksslcertificates", KConfig::SimpleConfig); 00186 00187 if (name.isEmpty()) return NULL; 00188 00189 KConfigGroup cg(&cfg, name); 00190 return KSSLPKCS12::fromString(cg.readEntry("PKCS12Base64", ""), password); 00191 } 00192 00193 00194 00195 KSSLPKCS12* KSSLCertificateHome::getDefaultCertificate(KSSLAuthAction *aa) { 00196 QString name = KSSLCertificateHome::getDefaultCertificateName(aa); 00197 KConfig cfg("ksslcertificates", KConfig::SimpleConfig); 00198 00199 if (name.isEmpty()) return NULL; 00200 00201 KConfigGroup cg(&cfg, name); 00202 00203 return KSSLPKCS12::fromString(cg.readEntry("PKCS12Base64", ""), 00204 cg.readEntry("Password", "")); 00205 } 00206 00207 00208 void KSSLCertificateHome::setDefaultCertificate(const QString &name, bool send, bool prompt) 00209 { 00210 KConfig cfg("ksslauthmap", KConfig::SimpleConfig); 00211 KConfigGroup cg(&cfg, "<default>"); 00212 cg.writeEntry("defaultCertificate", name); 00213 cg.writeEntry("send", send); 00214 cg.writeEntry("prompt", prompt); 00215 } 00216 00217 00218 void KSSLCertificateHome::setDefaultCertificate(KSSLPKCS12 *cert, bool send, bool prompt) { 00219 if (cert) 00220 KSSLCertificateHome::setDefaultCertificate(cert->name(), send, prompt); 00221 } 00222
KDE 4.6 API Reference