TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ksslcertificatehome.cc 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* This file is part of the KDE project
  2. *
  3. * Copyright (C) 2000-2005 George Staikos <staikos@kde.org>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public License
  16. * along with this library; see the file COPYING.LIB. If not, write to
  17. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. * Boston, MA 02110-1301, USA.
  19. */
  20. #include <ksslcertificatehome.h>
  21. #include <ksslcertificate.h>
  22. #include <ksslpkcs12.h>
  23. #include <kresolver.h>
  24. #include <ksimpleconfig.h>
  25. using namespace KNetwork;
  26. TQStringList KSSLCertificateHome::getCertificateList() {
  27. KSimpleConfig cfg("ksslcertificates", false);
  28. TQStringList list = cfg.groupList();
  29. TQString defaultstr("<default>");
  30. TQString blankstr("");
  31. list.remove(defaultstr);
  32. list.remove(blankstr);
  33. return list;
  34. }
  35. // KDE 4: make it const TQString &
  36. void KSSLCertificateHome::setDefaultCertificate(TQString name, TQString host, bool send, bool prompt) {
  37. KSimpleConfig cfg("ksslauthmap", false);
  38. #ifdef Q_WS_WIN //temporary
  39. cfg.setGroup(host);
  40. #else
  41. cfg.setGroup(KResolver::domainToAscii(host));
  42. #endif
  43. cfg.writeEntry("certificate", name);
  44. cfg.writeEntry("send", send);
  45. cfg.writeEntry("prompt", prompt);
  46. cfg.sync();
  47. }
  48. // KDE 4: make it const TQString &
  49. void KSSLCertificateHome::setDefaultCertificate(KSSLPKCS12 *cert, TQString host, bool send, bool prompt) {
  50. if (cert)
  51. KSSLCertificateHome::setDefaultCertificate(cert->name(), host, send, prompt);
  52. }
  53. // KDE 4: make it const TQString &
  54. bool KSSLCertificateHome::addCertificate(TQString filename, TQString password, bool storePass) {
  55. KSSLPKCS12 *pkcs = KSSLPKCS12::loadCertFile(filename, password);
  56. if (!pkcs) return false;
  57. KSSLCertificateHome::addCertificate(pkcs, storePass?password:TQString(""));
  58. delete pkcs;
  59. return true;
  60. }
  61. // KDE 4: make it const TQString &
  62. bool KSSLCertificateHome::addCertificate(KSSLPKCS12 *cert, TQString passToStore) {
  63. if (!cert) return false;
  64. KSimpleConfig cfg("ksslcertificates", false);
  65. cfg.setGroup(cert->name());
  66. cfg.writeEntry("PKCS12Base64", cert->toString());
  67. cfg.writeEntry("Password", passToStore);
  68. cfg.sync();
  69. return true;
  70. }
  71. bool KSSLCertificateHome::deleteCertificate(const TQString &filename, const TQString &password) {
  72. KSSLPKCS12 *pkcs = KSSLPKCS12::loadCertFile(filename, password);
  73. if (!pkcs) return false;
  74. bool ok = deleteCertificate(pkcs);
  75. delete pkcs;
  76. return ok;
  77. }
  78. bool KSSLCertificateHome::deleteCertificate(KSSLPKCS12 *cert) {
  79. if (!cert) return false;
  80. return deleteCertificateByName(cert->name());
  81. }
  82. bool KSSLCertificateHome::deleteCertificateByName(const TQString &name) {
  83. if (name.isEmpty()) return false;
  84. KSimpleConfig cfg("ksslcertificates", false);
  85. bool ok = cfg.deleteGroup(name);
  86. cfg.sync();
  87. return ok;
  88. }
  89. // KDE 4: make it const TQString &
  90. KSSLPKCS12* KSSLCertificateHome::getCertificateByName(TQString name, TQString password) {
  91. KSimpleConfig cfg("ksslcertificates", false);
  92. if (!cfg.hasGroup(name)) return NULL;
  93. cfg.setGroup(name);
  94. return KSSLPKCS12::fromString(cfg.readEntry("PKCS12Base64", ""), password);
  95. }
  96. // KDE 4: make it const TQString &
  97. KSSLPKCS12* KSSLCertificateHome::getCertificateByName(TQString name) {
  98. KSimpleConfig cfg("ksslcertificates", false);
  99. if (!cfg.hasGroup(name)) return NULL;
  100. cfg.setGroup(name);
  101. return KSSLPKCS12::fromString(cfg.readEntry("PKCS12Base64", ""), cfg.readEntry("Password", ""));
  102. }
  103. // KDE 4: make it const TQString &
  104. bool KSSLCertificateHome::hasCertificateByName(TQString name) {
  105. KSimpleConfig cfg("ksslcertificates", false);
  106. if (!cfg.hasGroup(name)) return false;
  107. return true;
  108. }
  109. // KDE 4: make it const TQString &
  110. KSSLPKCS12* KSSLCertificateHome::getCertificateByHost(TQString host, TQString password, KSSLAuthAction *aa) {
  111. return KSSLCertificateHome::getCertificateByName(KSSLCertificateHome::getDefaultCertificateName(host, aa), password);
  112. }
  113. // KDE 4: make it const TQString &
  114. TQString KSSLCertificateHome::getDefaultCertificateName(TQString host, KSSLAuthAction *aa) {
  115. KSimpleConfig cfg("ksslauthmap", false);
  116. #ifdef Q_WS_WIN //temporary
  117. if (!cfg.hasGroup(host)) {
  118. #else
  119. if (!cfg.hasGroup(KResolver::domainToAscii(host))) {
  120. #endif
  121. if (aa) *aa = AuthNone;
  122. return TQString::null;
  123. } else {
  124. #ifdef Q_WS_WIN //temporary
  125. cfg.setGroup(host);
  126. #else
  127. cfg.setGroup(KResolver::domainToAscii(host));
  128. #endif
  129. if (aa) {
  130. bool tmp = cfg.readBoolEntry("send", false);
  131. *aa = AuthSend;
  132. if (!tmp) {
  133. tmp = cfg.readBoolEntry("prompt", false);
  134. *aa = AuthPrompt;
  135. if (!tmp) {
  136. *aa = AuthDont;
  137. }
  138. }
  139. }
  140. return cfg.readEntry("certificate", "");
  141. }
  142. }
  143. TQString KSSLCertificateHome::getDefaultCertificateName(KSSLAuthAction *aa) {
  144. TDEConfig cfg("cryptodefaults", false);
  145. cfg.setGroup("Auth");
  146. if (aa) {
  147. TQString am = cfg.readEntry("AuthMethod", "");
  148. if (am == "send")
  149. *aa = AuthSend;
  150. else if (am == "prompt")
  151. *aa = AuthPrompt;
  152. else
  153. *aa = AuthDont;
  154. }
  155. return cfg.readEntry("DefaultCert", "");
  156. }
  157. // KDE 4: make it const TQString &
  158. KSSLPKCS12* KSSLCertificateHome::getDefaultCertificate(TQString password, KSSLAuthAction *aa) {
  159. TQString name = KSSLCertificateHome::getDefaultCertificateName(aa);
  160. KSimpleConfig cfg("ksslcertificates", false);
  161. if (name.isEmpty()) return NULL;
  162. cfg.setGroup(name);
  163. return KSSLPKCS12::fromString(cfg.readEntry("PKCS12Base64", ""), password);
  164. }
  165. KSSLPKCS12* KSSLCertificateHome::getDefaultCertificate(KSSLAuthAction *aa) {
  166. TQString name = KSSLCertificateHome::getDefaultCertificateName(aa);
  167. KSimpleConfig cfg("ksslcertificates", false);
  168. if (name.isEmpty()) return NULL;
  169. cfg.setGroup(name);
  170. return KSSLPKCS12::fromString(cfg.readEntry("PKCS12Base64", ""),
  171. cfg.readEntry("Password", ""));
  172. }
  173. // KDE 4: make it const TQString &
  174. void KSSLCertificateHome::setDefaultCertificate(TQString name, bool send, bool prompt) {
  175. KSimpleConfig cfg("ksslauthmap", false);
  176. cfg.setGroup("<default>");
  177. cfg.writeEntry("defaultCertificate", name);
  178. cfg.writeEntry("send", send);
  179. cfg.writeEntry("prompt", prompt);
  180. }
  181. void KSSLCertificateHome::setDefaultCertificate(KSSLPKCS12 *cert, bool send, bool prompt) {
  182. if (cert)
  183. KSSLCertificateHome::setDefaultCertificate(cert->name(), send, prompt);
  184. }