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.

ksslpkcs7.cc 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* This file is part of the KDE project
  2. *
  3. * Copyright (C) 2001 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. #ifdef HAVE_CONFIG_H
  21. #include <config.h>
  22. #endif
  23. #include <kopenssl.h>
  24. #include <tqstring.h>
  25. #include <tqfile.h>
  26. #include <ksslall.h>
  27. #include <kdebug.h>
  28. #include <tdetempfile.h>
  29. #include <kmdcodec.h>
  30. #include <assert.h>
  31. KSSLPKCS7::KSSLPKCS7() {
  32. _pkcs = NULL;
  33. _cert = NULL;
  34. kossl = KOSSL::self();
  35. }
  36. KSSLPKCS7::~KSSLPKCS7() {
  37. #ifdef KSSL_HAVE_SSL
  38. if (_pkcs) kossl->PKCS7_free(_pkcs);
  39. #endif
  40. if (_cert) delete _cert;
  41. }
  42. KSSLPKCS7* KSSLPKCS7::fromString(TQString base64) {
  43. #ifdef KSSL_HAVE_SSL
  44. KTempFile ktf;
  45. if (base64.isEmpty()) return NULL;
  46. TQByteArray qba, qbb = TQCString(base64.latin1()).copy();
  47. KCodecs::base64Decode(qbb, qba);
  48. ktf.file()->writeBlock(qba);
  49. ktf.close();
  50. KSSLPKCS7* rc = loadCertFile(ktf.name());
  51. ktf.unlink();
  52. return rc;
  53. #endif
  54. return NULL;
  55. }
  56. KSSLPKCS7* KSSLPKCS7::loadCertFile(TQString filename) {
  57. #ifdef KSSL_HAVE_SSL
  58. TQFile qf(filename);
  59. PKCS7 *newpkcs = NULL;
  60. if (!qf.open(IO_ReadOnly))
  61. return NULL;
  62. FILE *fp = fdopen(qf.handle(), "r");
  63. if (!fp) return NULL;
  64. newpkcs = KOSSL::self()->d2i_PKCS7_fp(fp, &newpkcs);
  65. if (!newpkcs) return NULL;
  66. KSSLPKCS7 *c = new KSSLPKCS7;
  67. c->setCert(newpkcs);
  68. return c;
  69. #endif
  70. return NULL;
  71. }
  72. void KSSLPKCS7::setCert(PKCS7 *c) {
  73. #ifdef KSSL_HAVE_SSL
  74. _pkcs = c;
  75. //STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7);
  76. //X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si);
  77. // set _chain and _cert here.
  78. #endif
  79. }
  80. KSSLCertificate *KSSLPKCS7::getCertificate() {
  81. return _cert;
  82. }
  83. KSSLCertChain *KSSLPKCS7::getChain() {
  84. return _chain;
  85. }
  86. TQString KSSLPKCS7::toString() {
  87. TQString base64;
  88. #ifdef KSSL_HAVE_SSL
  89. unsigned char *p;
  90. int len;
  91. len = kossl->i2d_PKCS7(_pkcs, NULL);
  92. if (len >= 0) {
  93. char *buf = new char[len];
  94. p = (unsigned char *)buf;
  95. kossl->i2d_PKCS7(_pkcs, &p);
  96. TQByteArray qba;
  97. qba.setRawData(buf, len);
  98. base64 = KCodecs::base64Encode(qba);
  99. qba.resetRawData(buf, len);
  100. delete[] buf;
  101. }
  102. #endif
  103. return base64;
  104. }
  105. bool KSSLPKCS7::toFile(TQString filename) {
  106. #ifdef KSSL_HAVE_SSL
  107. TQFile out(filename);
  108. if (!out.open(IO_WriteOnly)) return false;
  109. int fd = out.handle();
  110. FILE *fp = fdopen(fd, "w");
  111. if (!fp) {
  112. unlink(filename.latin1());
  113. return false;
  114. }
  115. kossl->i2d_PKCS7_fp(fp, _pkcs);
  116. fclose(fp);
  117. return true;
  118. #endif
  119. return false;
  120. }
  121. KSSLCertificate::KSSLValidation KSSLPKCS7::validate() {
  122. #ifdef KSSL_HAVE_SSL
  123. KSSLCertificate::KSSLValidation xx = _cert->validate();
  124. return xx;
  125. #else
  126. return KSSLCertificate::NoSSL;
  127. #endif
  128. }
  129. KSSLCertificate::KSSLValidation KSSLPKCS7::revalidate() {
  130. if (_cert)
  131. return _cert->revalidate();
  132. return KSSLCertificate::Unknown;
  133. }
  134. bool KSSLPKCS7::isValid() {
  135. return (validate() == KSSLCertificate::Ok);
  136. }
  137. TQString KSSLPKCS7::name() {
  138. if (_cert)
  139. return _cert->getSubject();
  140. return TQString();
  141. }