• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KIO

  • sources
  • kde-4.12
  • kdelibs
  • kio
  • kssl
ksslcertificate.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE project
2  *
3  * Copyright (C) 2000-2003 George Staikos <staikos@kde.org>
4  * 2008 Richard Hartmann <richih-kde@net.in.tum.de>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #include "ksslcertificate.h"
23 
24 #include <config.h>
25 #include <ksslconfig.h>
26 
27 
28 
29 #include <unistd.h>
30 #include <QtCore/QString>
31 #include <QtCore/QStringList>
32 #include <QtCore/QFile>
33 
34 #include "kssldefs.h"
35 #include "ksslcertchain.h"
36 #include "ksslutils.h"
37 
38 #include <kstandarddirs.h>
39 #include <kcodecs.h>
40 #include <kde_file.h>
41 #include <klocale.h>
42 #include <QtCore/QDate>
43 #include <ktemporaryfile.h>
44 
45 #include <sys/types.h>
46 
47 #ifdef HAVE_SYS_STAT_H
48 #include <sys/stat.h>
49 #endif
50 
51 // this hack provided by Malte Starostik to avoid glibc/openssl bug
52 // on some systems
53 #ifdef KSSL_HAVE_SSL
54 #define crypt _openssl_crypt
55 #include <openssl/ssl.h>
56 #include <openssl/x509.h>
57 #include <openssl/x509v3.h>
58 #include <openssl/x509_vfy.h>
59 #include <openssl/pem.h>
60 #undef crypt
61 #endif
62 
63 #include <kopenssl.h>
64 #include <kdebug.h>
65 #include "ksslx509v3.h"
66 
67 
68 
69 static char hv[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
70 
71 
72 class KSSLCertificatePrivate {
73 public:
74  KSSLCertificatePrivate() {
75  kossl = KOSSL::self();
76  _lastPurpose = KSSLCertificate::None;
77  }
78 
79  ~KSSLCertificatePrivate() {
80  }
81 
82  KSSLCertificate::KSSLValidation m_stateCache;
83  bool m_stateCached;
84  #ifdef KSSL_HAVE_SSL
85  X509 *m_cert;
86  #endif
87  KOSSL *kossl;
88  KSSLCertChain _chain;
89  KSSLX509V3 _extensions;
90  KSSLCertificate::KSSLPurpose _lastPurpose;
91 };
92 
93 KSSLCertificate::KSSLCertificate() {
94  d = new KSSLCertificatePrivate;
95  d->m_stateCached = false;
96  KGlobal::dirs()->addResourceType("kssl", "data", "kssl");
97  #ifdef KSSL_HAVE_SSL
98  d->m_cert = NULL;
99  #endif
100 }
101 
102 
103 KSSLCertificate::KSSLCertificate(const KSSLCertificate& x) {
104  d = new KSSLCertificatePrivate;
105  d->m_stateCached = false;
106  KGlobal::dirs()->addResourceType("kssl", "data", "kssl");
107  #ifdef KSSL_HAVE_SSL
108  d->m_cert = NULL;
109  setCert(KOSSL::self()->X509_dup(const_cast<KSSLCertificate&>(x).getCert()));
110  KSSLCertChain *c = x.d->_chain.replicate();
111  setChain(c->rawChain());
112  delete c;
113  #endif
114 }
115 
116 
117 
118 KSSLCertificate::~KSSLCertificate() {
119 #ifdef KSSL_HAVE_SSL
120  if (d->m_cert) {
121  d->kossl->X509_free(d->m_cert);
122  }
123 #endif
124  delete d;
125 }
126 
127 
128 KSSLCertChain& KSSLCertificate::chain() {
129  return d->_chain;
130 }
131 
132 
133 KSSLCertificate *KSSLCertificate::fromX509(X509 *x5) {
134  KSSLCertificate *n = NULL;
135 #ifdef KSSL_HAVE_SSL
136  if (x5) {
137  n = new KSSLCertificate;
138  n->setCert(KOSSL::self()->X509_dup(x5));
139  }
140 #endif
141  return n;
142 }
143 
144 
145 KSSLCertificate *KSSLCertificate::fromString(const QByteArray &cert) {
146  KSSLCertificate *n = NULL;
147 #ifdef KSSL_HAVE_SSL
148  if (cert.isEmpty()) {
149  return NULL;
150  }
151 
152  QByteArray qba = QByteArray::fromBase64(cert);
153  unsigned char *qbap = reinterpret_cast<unsigned char *>(qba.data());
154  X509 *x5c = KOSSL::self()->d2i_X509(NULL, &qbap, qba.size());
155  if (!x5c) {
156  return NULL;
157  }
158 
159  n = new KSSLCertificate;
160  n->setCert(x5c);
161 #endif
162  return n;
163 }
164 
165 
166 
167 QString KSSLCertificate::getSubject() const {
168  QString rc = "";
169 
170 #ifdef KSSL_HAVE_SSL
171  char *t = d->kossl->X509_NAME_oneline(d->kossl->X509_get_subject_name(d->m_cert), 0, 0);
172  if (!t) {
173  return rc;
174  }
175  rc = t;
176  d->kossl->OPENSSL_free(t);
177 #endif
178  return rc;
179 }
180 
181 
182 QString KSSLCertificate::getSerialNumber() const {
183  QString rc = "";
184 
185 #ifdef KSSL_HAVE_SSL
186  ASN1_INTEGER *aint = d->kossl->X509_get_serialNumber(d->m_cert);
187  if (aint) {
188  rc = ASN1_INTEGER_QString(aint);
189  // d->kossl->ASN1_INTEGER_free(aint); this makes the sig test fail
190  }
191 #endif
192  return rc;
193 }
194 
195 
196 QString KSSLCertificate::getSignatureText() const {
197  QString rc = "";
198 
199 #ifdef KSSL_HAVE_SSL
200  char *s;
201  int n, i;
202 
203  i = d->kossl->OBJ_obj2nid(d->m_cert->sig_alg->algorithm);
204  rc = i18n("Signature Algorithm: ");
205  rc += (i == NID_undef)?i18n("Unknown"):QString(d->kossl->OBJ_nid2ln(i));
206 
207  rc += '\n';
208  rc += i18n("Signature Contents:");
209  n = d->m_cert->signature->length;
210  s = (char *)d->m_cert->signature->data;
211  for (i = 0; i < n; ++i) {
212  if (i%20 != 0) {
213  rc += ':';
214  }
215  else {
216  rc += '\n';
217  }
218  rc.append(QChar(hv[(s[i]&0xf0)>>4]));
219  rc.append(QChar(hv[s[i]&0x0f]));
220  }
221 
222 #endif
223 
224  return rc;
225 }
226 
227 
228 void KSSLCertificate::getEmails(QStringList &to) const {
229  to.clear();
230 #ifdef KSSL_HAVE_SSL
231  if (!d->m_cert) {
232  return;
233  }
234 
235  STACK *s = d->kossl->X509_get1_email(d->m_cert);
236  if (s) {
237  for(int n=0; n < s->num; n++) {
238  to.append(d->kossl->sk_value(s,n));
239  }
240  d->kossl->X509_email_free(s);
241  }
242 #endif
243 }
244 
245 
246 QString KSSLCertificate::getKDEKey() const {
247  return getSubject() + " (" + getMD5DigestText() + ')';
248 }
249 
250 
251 QString KSSLCertificate::getMD5DigestFromKDEKey(const QString &k) {
252  QString rc;
253  int pos = k.lastIndexOf('(');
254  if (pos != -1) {
255  unsigned int len = k.length();
256  if (k.at(len-1) == ')') {
257  rc = k.mid(pos+1, len-pos-2);
258  }
259  }
260  return rc;
261 }
262 
263 
264 QString KSSLCertificate::getMD5DigestText() const {
265 QString rc = "";
266 
267 #ifdef KSSL_HAVE_SSL
268  unsigned int n;
269  unsigned char md[EVP_MAX_MD_SIZE];
270 
271  if (!d->kossl->X509_digest(d->m_cert, d->kossl->EVP_md5(), md, &n)) {
272  return rc;
273  }
274 
275  for (unsigned int j = 0; j < n; j++) {
276  if (j > 0) {
277  rc += ':';
278  }
279  rc.append(QChar(hv[(md[j]&0xf0)>>4]));
280  rc.append(QChar(hv[md[j]&0x0f]));
281  }
282 
283 #endif
284 
285  return rc;
286 }
287 
288 
289 
290 QString KSSLCertificate::getMD5Digest() const {
291 QString rc = "";
292 
293 #ifdef KSSL_HAVE_SSL
294  unsigned int n;
295  unsigned char md[EVP_MAX_MD_SIZE];
296 
297  if (!d->kossl->X509_digest(d->m_cert, d->kossl->EVP_md5(), md, &n)) {
298  return rc;
299  }
300 
301  for (unsigned int j = 0; j < n; j++) {
302  rc.append(QLatin1Char(hv[(md[j]&0xf0)>>4]));
303  rc.append(QLatin1Char(hv[md[j]&0x0f]));
304  }
305 
306 #endif
307 
308  return rc;
309 }
310 
311 
312 
313 QString KSSLCertificate::getKeyType() const {
314 QString rc = "";
315 
316 #ifdef KSSL_HAVE_SSL
317  EVP_PKEY *pkey = d->kossl->X509_get_pubkey(d->m_cert);
318  if (pkey) {
319  #ifndef NO_RSA
320  if (pkey->type == EVP_PKEY_RSA) {
321  rc = "RSA";
322  }
323  else
324  #endif
325  #ifndef NO_DSA
326  if (pkey->type == EVP_PKEY_DSA) {
327  rc = "DSA";
328  }
329  else
330  #endif
331  rc = "Unknown";
332  d->kossl->EVP_PKEY_free(pkey);
333  }
334 #endif
335 
336  return rc;
337 }
338 
339 
340 
341 QString KSSLCertificate::getPublicKeyText() const {
342 QString rc = "";
343 char *x = NULL;
344 
345 #ifdef KSSL_HAVE_SSL
346  EVP_PKEY *pkey = d->kossl->X509_get_pubkey(d->m_cert);
347  if (pkey) {
348  rc = i18nc("Unknown", "Unknown key algorithm");
349  #ifndef NO_RSA
350  if (pkey->type == EVP_PKEY_RSA) {
351  x = d->kossl->BN_bn2hex(pkey->pkey.rsa->n);
352  rc = i18n("Key type: RSA (%1 bit)", strlen(x)*4) + '\n';
353 
354  rc += i18n("Modulus: ");
355  for (unsigned int i = 0; i < strlen(x); i++) {
356  if (i%40 != 0 && i%2 == 0) {
357  rc += ':';
358  }
359  else if (i%40 == 0) {
360  rc += '\n';
361  }
362  rc += x[i];
363  }
364  rc += '\n';
365  d->kossl->OPENSSL_free(x);
366 
367  x = d->kossl->BN_bn2hex(pkey->pkey.rsa->e);
368  rc += i18n("Exponent: 0x") + QLatin1String(x) +
369  QLatin1String("\n");
370  d->kossl->OPENSSL_free(x);
371  }
372  #endif
373  #ifndef NO_DSA
374  if (pkey->type == EVP_PKEY_DSA) {
375  x = d->kossl->BN_bn2hex(pkey->pkey.dsa->p);
376  // hack - this may not be always accurate
377  rc = i18n("Key type: DSA (%1 bit)", strlen(x)*4) + '\n';
378 
379  rc += i18n("Prime: ");
380  for (unsigned int i = 0; i < strlen(x); i++) {
381  if (i%40 != 0 && i%2 == 0) {
382  rc += ':';
383  }
384  else if (i%40 == 0) {
385  rc += '\n';
386  }
387  rc += x[i];
388  }
389  rc += '\n';
390  d->kossl->OPENSSL_free(x);
391 
392  x = d->kossl->BN_bn2hex(pkey->pkey.dsa->q);
393  rc += i18n("160 bit prime factor: ");
394  for (unsigned int i = 0; i < strlen(x); i++) {
395  if (i%40 != 0 && i%2 == 0) {
396  rc += ':';
397  }
398  else if (i%40 == 0) {
399  rc += '\n';
400  }
401  rc += x[i];
402  }
403  rc += '\n';
404  d->kossl->OPENSSL_free(x);
405 
406  x = d->kossl->BN_bn2hex(pkey->pkey.dsa->g);
407  rc += QString("g: ");
408  for (unsigned int i = 0; i < strlen(x); i++) {
409  if (i%40 != 0 && i%2 == 0) {
410  rc += ':';
411  }
412  else if (i%40 == 0) {
413  rc += '\n';
414  }
415  rc += x[i];
416  }
417  rc += '\n';
418  d->kossl->OPENSSL_free(x);
419 
420  x = d->kossl->BN_bn2hex(pkey->pkey.dsa->pub_key);
421  rc += i18n("Public key: ");
422  for (unsigned int i = 0; i < strlen(x); i++) {
423  if (i%40 != 0 && i%2 == 0) {
424  rc += ':';
425  }
426  else if (i%40 == 0) {
427  rc += '\n';
428  }
429  rc += x[i];
430  }
431  rc += '\n';
432  d->kossl->OPENSSL_free(x);
433  }
434  #endif
435  d->kossl->EVP_PKEY_free(pkey);
436  }
437 #endif
438 
439  return rc;
440 }
441 
442 
443 
444 QString KSSLCertificate::getIssuer() const {
445 QString rc = "";
446 
447 #ifdef KSSL_HAVE_SSL
448  char *t = d->kossl->X509_NAME_oneline(d->kossl->X509_get_issuer_name(d->m_cert), 0, 0);
449 
450  if (!t) {
451  return rc;
452  }
453 
454  rc = t;
455  d->kossl->OPENSSL_free(t);
456 #endif
457 
458  return rc;
459 }
460 
461 void KSSLCertificate::setChain(void *c) {
462 #ifdef KSSL_HAVE_SSL
463  d->_chain.setChain(c);
464 #endif
465  d->m_stateCached = false;
466  d->m_stateCache = KSSLCertificate::Unknown;
467 }
468 
469 void KSSLCertificate::setCert(X509 *c) {
470 #ifdef KSSL_HAVE_SSL
471  d->m_cert = c;
472  if (c) {
473  d->_extensions.flags = 0;
474  d->kossl->X509_check_purpose(c, -1, 0); // setup the fields (!!)
475 
476 #if 0
477  kDebug(7029) << "---------------- Certificate ------------------"
478  << endl;
479  kDebug(7029) << getSubject();
480 #endif
481 
482  for (int j = 0; j < d->kossl->X509_PURPOSE_get_count(); j++) {
483  X509_PURPOSE *ptmp = d->kossl->X509_PURPOSE_get0(j);
484  int id = d->kossl->X509_PURPOSE_get_id(ptmp);
485  for (int ca = 0; ca < 2; ca++) {
486  int idret = d->kossl->X509_check_purpose(c, id, ca);
487  if (idret == 1 || idret == 2) { // have it
488  // kDebug() << "PURPOSE: " << id << (ca?" CA":"");
489  if (!ca) {
490  d->_extensions.flags |= (1L <<(id-1));
491  }
492  else d->_extensions.flags |= (1L <<(16+id-1));
493  } else {
494  if (!ca) {
495  d->_extensions.flags &= ~(1L <<(id-1));
496  }
497  else d->_extensions.flags &= ~(1L <<(16+id-1));
498  }
499  }
500  }
501 
502 #if 0
503  kDebug(7029) << "flags: " << QString::number(c->ex_flags, 2)
504  << "\nkeyusage: " << QString::number(c->ex_kusage, 2)
505  << "\nxkeyusage: " << QString::number(c->ex_xkusage, 2)
506  << "\nnscert: " << QString::number(c->ex_nscert, 2)
507  << endl;
508  if (c->ex_flags & EXFLAG_KUSAGE)
509  kDebug(7029) << " --- Key Usage extensions found";
510  else kDebug(7029) << " --- Key Usage extensions NOT found";
511 
512  if (c->ex_flags & EXFLAG_XKUSAGE)
513  kDebug(7029) << " --- Extended key usage extensions found";
514  else kDebug(7029) << " --- Extended key usage extensions NOT found";
515 
516  if (c->ex_flags & EXFLAG_NSCERT)
517  kDebug(7029) << " --- NS extensions found";
518  else kDebug(7029) << " --- NS extensions NOT found";
519 
520  if (d->_extensions.certTypeSSLCA())
521  kDebug(7029) << "NOTE: this is an SSL CA file.";
522  else kDebug(7029) << "NOTE: this is NOT an SSL CA file.";
523 
524  if (d->_extensions.certTypeEmailCA())
525  kDebug(7029) << "NOTE: this is an EMAIL CA file.";
526  else kDebug(7029) << "NOTE: this is NOT an EMAIL CA file.";
527 
528  if (d->_extensions.certTypeCodeCA())
529  kDebug(7029) << "NOTE: this is a CODE CA file.";
530  else kDebug(7029) << "NOTE: this is NOT a CODE CA file.";
531 
532  if (d->_extensions.certTypeSSLClient())
533  kDebug(7029) << "NOTE: this is an SSL client.";
534  else kDebug(7029) << "NOTE: this is NOT an SSL client.";
535 
536  if (d->_extensions.certTypeSSLServer())
537  kDebug(7029) << "NOTE: this is an SSL server.";
538  else kDebug(7029) << "NOTE: this is NOT an SSL server.";
539 
540  if (d->_extensions.certTypeNSSSLServer())
541  kDebug(7029) << "NOTE: this is a NETSCAPE SSL server.";
542  else kDebug(7029) << "NOTE: this is NOT a NETSCAPE SSL server.";
543 
544  if (d->_extensions.certTypeSMIME())
545  kDebug(7029) << "NOTE: this is an SMIME certificate.";
546  else kDebug(7029) << "NOTE: this is NOT an SMIME certificate.";
547 
548  if (d->_extensions.certTypeSMIMEEncrypt())
549  kDebug(7029) << "NOTE: this is an SMIME encrypt cert.";
550  else kDebug(7029) << "NOTE: this is NOT an SMIME encrypt cert.";
551 
552  if (d->_extensions.certTypeSMIMESign())
553  kDebug(7029) << "NOTE: this is an SMIME sign cert.";
554  else kDebug(7029) << "NOTE: this is NOT an SMIME sign cert.";
555 
556  if (d->_extensions.certTypeCRLSign())
557  kDebug(7029) << "NOTE: this is a CRL signer.";
558  else kDebug(7029) << "NOTE: this is NOT a CRL signer.";
559 
560  kDebug(7029) << "-----------------------------------------------"
561  << endl;
562 #endif
563  }
564 #endif
565  d->m_stateCached = false;
566  d->m_stateCache = KSSLCertificate::Unknown;
567 }
568 
569 X509 *KSSLCertificate::getCert() {
570 #ifdef KSSL_HAVE_SSL
571  return d->m_cert;
572 #endif
573  return 0;
574 }
575 
576 // pull in the callback. It's common across multiple files but we want
577 // it to be hidden.
578 
579 #include "ksslcallback.c"
580 
581 
582 bool KSSLCertificate::isValid(KSSLCertificate::KSSLPurpose p) {
583  return (validate(p) == KSSLCertificate::Ok);
584 }
585 
586 
587 bool KSSLCertificate::isValid() {
588  return isValid(KSSLCertificate::SSLServer);
589 }
590 
591 
592 int KSSLCertificate::purposeToOpenSSL(KSSLCertificate::KSSLPurpose p) const {
593  int rc = 0;
594 #ifdef KSSL_HAVE_SSL
595  if (p == KSSLCertificate::SSLServer) {
596  rc = X509_PURPOSE_SSL_SERVER;
597  } else if (p == KSSLCertificate::SSLClient) {
598  rc = X509_PURPOSE_SSL_CLIENT;
599  } else if (p == KSSLCertificate::SMIMEEncrypt) {
600  rc = X509_PURPOSE_SMIME_ENCRYPT;
601  } else if (p == KSSLCertificate::SMIMESign) {
602  rc = X509_PURPOSE_SMIME_SIGN;
603  } else if (p == KSSLCertificate::Any) {
604  rc = X509_PURPOSE_ANY;
605  }
606 #endif
607  return rc;
608 }
609 
610 
611 // For backward compatibility
612 KSSLCertificate::KSSLValidation KSSLCertificate::validate() {
613  return validate(KSSLCertificate::SSLServer);
614 }
615 
616 KSSLCertificate::KSSLValidation KSSLCertificate::validate(KSSLCertificate::KSSLPurpose purpose)
617 {
618  KSSLValidationList result = validateVerbose(purpose);
619  if (result.isEmpty()) {
620  return KSSLCertificate::Ok;
621  }
622  else
623  return result.first();
624 }
625 
626 //
627 // See apps/verify.c in OpenSSL for the source of most of this logic.
628 //
629 
630 // CRL files? we don't do that yet
631 KSSLCertificate::KSSLValidationList KSSLCertificate::validateVerbose(KSSLCertificate::KSSLPurpose purpose)
632 {
633  return validateVerbose(purpose, 0);
634 }
635 
636 KSSLCertificate::KSSLValidationList KSSLCertificate::validateVerbose(KSSLCertificate::KSSLPurpose purpose, KSSLCertificate *ca)
637 {
638  KSSLValidationList errors;
639  if (ca || (d->_lastPurpose != purpose)) {
640  d->m_stateCached = false;
641  }
642 
643  if (!d->m_stateCached) {
644  d->_lastPurpose = purpose;
645  }
646 
647 #ifdef KSSL_HAVE_SSL
648  X509_STORE *certStore;
649  X509_LOOKUP *certLookup;
650  X509_STORE_CTX *certStoreCTX;
651  int rc = 0;
652 
653  if (!d->m_cert) {
654  errors << KSSLCertificate::Unknown;
655  return errors;
656  }
657 
658  if (d->m_stateCached) {
659  errors << d->m_stateCache;
660  return errors;
661  }
662 
663  const QStringList qsl = KGlobal::dirs()->resourceDirs("kssl");
664 
665  if (qsl.isEmpty()) {
666  errors << KSSLCertificate::NoCARoot;
667  return errors;
668  }
669 
670  KSSLCertificate::KSSLValidation ksslv = Unknown;
671 
672  for (QStringList::ConstIterator j = qsl.begin(); j != qsl.end(); ++j) {
673  KDE_struct_stat sb;
674  QString _j = (*j) + "ca-bundle.crt";
675  if (-1 == KDE_stat(_j.toLatin1().constData(), &sb)) {
676  continue;
677  }
678 
679  certStore = d->kossl->X509_STORE_new();
680  if (!certStore) {
681  errors << KSSLCertificate::Unknown;
682  return errors;
683  }
684 
685  X509_STORE_set_verify_cb_func(certStore, X509Callback);
686 
687  certLookup = d->kossl->X509_STORE_add_lookup(certStore, d->kossl->X509_LOOKUP_file());
688  if (!certLookup) {
689  ksslv = KSSLCertificate::Unknown;
690  d->kossl->X509_STORE_free(certStore);
691  continue;
692  }
693 
694  if (!d->kossl->X509_LOOKUP_load_file(certLookup, _j.toLatin1().constData(), X509_FILETYPE_PEM)) {
695  // error accessing directory and loading pems
696  kDebug(7029) << "KSSL couldn't read CA root: "
697  << _j << endl;
698  ksslv = KSSLCertificate::ErrorReadingRoot;
699  d->kossl->X509_STORE_free(certStore);
700  continue;
701  }
702 
703  // This is the checking code
704  certStoreCTX = d->kossl->X509_STORE_CTX_new();
705 
706  // this is a bad error - could mean no free memory.
707  // This may be the wrong thing to do here
708  if (!certStoreCTX) {
709  kDebug(7029) << "KSSL couldn't create an X509 store context.";
710  d->kossl->X509_STORE_free(certStore);
711  continue;
712  }
713 
714  d->kossl->X509_STORE_CTX_init(certStoreCTX, certStore, d->m_cert, NULL);
715  if (d->_chain.isValid()) {
716  d->kossl->X509_STORE_CTX_set_chain(certStoreCTX, (STACK_OF(X509)*)d->_chain.rawChain());
717  }
718 
719  //kDebug(7029) << "KSSL setting CRL..............";
720  // int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x);
721 
722  d->kossl->X509_STORE_CTX_set_purpose(certStoreCTX, purposeToOpenSSL(purpose));
723 
724  KSSL_X509CallBack_ca = ca ? ca->d->m_cert : 0;
725  KSSL_X509CallBack_ca_found = false;
726 
727  certStoreCTX->error = X509_V_OK;
728  rc = d->kossl->X509_verify_cert(certStoreCTX);
729  int errcode = certStoreCTX->error;
730  if (ca && !KSSL_X509CallBack_ca_found) {
731  ksslv = KSSLCertificate::Irrelevant;
732  } else {
733  ksslv = processError(errcode);
734  }
735  // For servers, we can try NS_SSL_SERVER too
736  if ((ksslv != KSSLCertificate::Ok) &&
737  (ksslv != KSSLCertificate::Irrelevant) &&
738  purpose == KSSLCertificate::SSLServer) {
739  d->kossl->X509_STORE_CTX_set_purpose(certStoreCTX,
740  X509_PURPOSE_NS_SSL_SERVER);
741 
742  certStoreCTX->error = X509_V_OK;
743  rc = d->kossl->X509_verify_cert(certStoreCTX);
744  errcode = certStoreCTX->error;
745  ksslv = processError(errcode);
746  }
747  d->kossl->X509_STORE_CTX_free(certStoreCTX);
748  d->kossl->X509_STORE_free(certStore);
749  // end of checking code
750  //
751 
752  //kDebug(7029) << "KSSL Validation procedure RC: "
753  // << rc << endl;
754  //kDebug(7029) << "KSSL Validation procedure errcode: "
755  // << errcode << endl;
756  //kDebug(7029) << "KSSL Validation procedure RESULTS: "
757  // << ksslv << endl;
758 
759  if (ksslv != NoCARoot && ksslv != InvalidCA && ksslv != GetIssuerCertFailed && ksslv != DecodeIssuerPublicKeyFailed && ksslv != GetIssuerCertLocallyFailed ) {
760  d->m_stateCached = true;
761  d->m_stateCache = ksslv;
762  }
763  break;
764  }
765 
766  if (ksslv != KSSLCertificate::Ok) {
767  errors << ksslv;
768  }
769 #else
770  errors << KSSLCertificate::NoSSL;
771 #endif
772  return errors;
773 }
774 
775 
776 
777 KSSLCertificate::KSSLValidation KSSLCertificate::revalidate() {
778  return revalidate(KSSLCertificate::SSLServer);
779 }
780 
781 
782 KSSLCertificate::KSSLValidation KSSLCertificate::revalidate(KSSLCertificate::KSSLPurpose p) {
783  d->m_stateCached = false;
784  return validate(p);
785 }
786 
787 
788 KSSLCertificate::KSSLValidation KSSLCertificate::processError(int ec) {
789  KSSLCertificate::KSSLValidation rc;
790 
791  rc = KSSLCertificate::Unknown;
792 #ifdef KSSL_HAVE_SSL
793  switch (ec) {
794 
795  // see man 1 verify for a detailed listing of all error codes
796 
797  // error 0
798  case X509_V_OK:
799  rc = KSSLCertificate::Ok;
800  break;
801 
802 
803  // error 2
804  case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
805  rc = KSSLCertificate::GetIssuerCertFailed;
806  break;
807 
808  // error 3
809  case X509_V_ERR_UNABLE_TO_GET_CRL:
810  rc = KSSLCertificate::GetCRLFailed;
811  break;
812 
813  // error 4
814  case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
815  rc = KSSLCertificate::DecryptCertificateSignatureFailed;
816  break;
817 
818  // error 5
819  case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
820  rc = KSSLCertificate::DecryptCRLSignatureFailed;
821  break;
822 
823  // error 6
824  case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
825  rc = KSSLCertificate::DecodeIssuerPublicKeyFailed;
826  break;
827 
828  // error 7
829  case X509_V_ERR_CERT_SIGNATURE_FAILURE:
830  rc = KSSLCertificate::CertificateSignatureFailed;
831  break;
832 
833  // error 8
834  case X509_V_ERR_CRL_SIGNATURE_FAILURE:
835  rc = KSSLCertificate::CRLSignatureFailed;
836  break;
837 
838  // error 9
839  case X509_V_ERR_CERT_NOT_YET_VALID:
840  rc = KSSLCertificate::CertificateNotYetValid;
841  break;
842 
843  // error 10
844  case X509_V_ERR_CERT_HAS_EXPIRED:
845  rc = KSSLCertificate::CertificateHasExpired;
846  kDebug(7029) << "KSSL apparently this is expired. Not after: "
847  << getNotAfter() << endl;
848  break;
849 
850  // error 11
851  case X509_V_ERR_CRL_NOT_YET_VALID:
852  rc = KSSLCertificate::CRLNotYetValid;
853  break;
854 
855  // error 12
856  case X509_V_ERR_CRL_HAS_EXPIRED:
857  rc = KSSLCertificate::CRLHasExpired;
858  break;
859 
860  // error 13
861  case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
862  rc = KSSLCertificate::CertificateFieldNotBeforeErroneous;
863  break;
864 
865  // error 14
866  case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
867  rc = KSSLCertificate::CertificateFieldNotAfterErroneous;
868  break;
869 
870  // error 15 - unused as of OpenSSL 0.9.8g
871  case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
872  rc = KSSLCertificate::CRLFieldLastUpdateErroneous;
873  break;
874 
875  // error 16 - unused as of OpenSSL 0.9.8g
876  case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
877  rc = KSSLCertificate::CRLFieldNextUpdateErroneous;
878  break;
879 
880  // error 17
881  case X509_V_ERR_OUT_OF_MEM:
882  rc = KSSLCertificate::OutOfMemory;
883  break;
884 
885  // error 18
886  case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
887  rc = KSSLCertificate::SelfSigned;
888  break;
889 
890  // error 19
891  case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
892  rc = KSSLCertificate::SelfSignedInChain;
893  break;
894 
895  // error 20
896  case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
897  rc = KSSLCertificate::GetIssuerCertLocallyFailed;
898  break;
899 
900  // error 21
901  case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
902  rc = KSSLCertificate::VerifyLeafSignatureFailed;
903  break;
904 
905  // error 22 - unused as of OpenSSL 0.9.8g
906  case X509_V_ERR_CERT_CHAIN_TOO_LONG:
907  rc = KSSLCertificate::CertificateChainTooLong;
908  break;
909 
910  // error 23 - unused as of OpenSSL 0.9.8g
911  case X509_V_ERR_CERT_REVOKED:
912  rc = KSSLCertificate::CertificateRevoked;
913  break;
914 
915  // error 24
916  case X509_V_ERR_INVALID_CA:
917  rc = KSSLCertificate::InvalidCA;
918  break;
919 
920  // error 25
921  case X509_V_ERR_PATH_LENGTH_EXCEEDED:
922  rc = KSSLCertificate::PathLengthExceeded;
923  break;
924 
925  // error 26
926  case X509_V_ERR_INVALID_PURPOSE:
927  rc = KSSLCertificate::InvalidPurpose;
928  break;
929 
930  // error 27
931  case X509_V_ERR_CERT_UNTRUSTED:
932  rc = KSSLCertificate::CertificateUntrusted;
933  break;
934 
935  // error 28
936  case X509_V_ERR_CERT_REJECTED:
937  rc = KSSLCertificate::CertificateRejected;
938  break;
939 
940  // error 29 - only used with -issuer_checks
941  case X509_V_ERR_SUBJECT_ISSUER_MISMATCH:
942  rc = KSSLCertificate::IssuerSubjectMismatched;
943  break;
944 
945  // error 30 - only used with -issuer_checks
946  case X509_V_ERR_AKID_SKID_MISMATCH:
947  rc = KSSLCertificate::AuthAndSubjectKeyIDMismatched;
948  break;
949 
950  // error 31 - only used with -issuer_checks
951  case X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH:
952  rc = KSSLCertificate::AuthAndSubjectKeyIDAndNameMismatched;
953  break;
954 
955  // error 32
956  case X509_V_ERR_KEYUSAGE_NO_CERTSIGN:
957  rc = KSSLCertificate::KeyMayNotSignCertificate;
958  break;
959 
960 
961  // error 50 - unused as of OpenSSL 0.9.8g
962  case X509_V_ERR_APPLICATION_VERIFICATION:
963  rc = KSSLCertificate::ApplicationVerificationFailed;
964  break;
965 
966 
967  default:
968  rc = KSSLCertificate::Unknown;
969  break;
970  }
971 
972  d->m_stateCache = rc;
973  d->m_stateCached = true;
974 #endif
975  return rc;
976 }
977 
978 
979 QString KSSLCertificate::getNotBefore() const {
980 #ifdef KSSL_HAVE_SSL
981  return ASN1_UTCTIME_QString(X509_get_notBefore(d->m_cert));
982 #else
983  return QString();
984 #endif
985 }
986 
987 
988 QString KSSLCertificate::getNotAfter() const {
989 #ifdef KSSL_HAVE_SSL
990  return ASN1_UTCTIME_QString(X509_get_notAfter(d->m_cert));
991 #else
992  return QString();
993 #endif
994 }
995 
996 
997 QDateTime KSSLCertificate::getQDTNotBefore() const {
998 #ifdef KSSL_HAVE_SSL
999  return ASN1_UTCTIME_QDateTime(X509_get_notBefore(d->m_cert), NULL);
1000 #else
1001  return QDateTime::currentDateTime();
1002 #endif
1003 }
1004 
1005 
1006 QDateTime KSSLCertificate::getQDTNotAfter() const {
1007 #ifdef KSSL_HAVE_SSL
1008  return ASN1_UTCTIME_QDateTime(X509_get_notAfter(d->m_cert), NULL);
1009 #else
1010  return QDateTime::currentDateTime();
1011 #endif
1012 }
1013 
1014 
1015 int operator==(KSSLCertificate &x, KSSLCertificate &y) {
1016 #ifndef KSSL_HAVE_SSL
1017  return 1;
1018 #else
1019  if (!KOSSL::self()->X509_cmp(x.getCert(), y.getCert())) {
1020  return 1;
1021  }
1022  return 0;
1023 #endif
1024 }
1025 
1026 
1027 KSSLCertificate *KSSLCertificate::replicate() {
1028  // The new certificate doesn't have the cached value. It's probably
1029  // better this way. We can't anticipate every reason for doing this.
1030  KSSLCertificate *newOne = new KSSLCertificate();
1031 #ifdef KSSL_HAVE_SSL
1032  newOne->setCert(d->kossl->X509_dup(getCert()));
1033  KSSLCertChain *c = d->_chain.replicate();
1034  newOne->setChain(c->rawChain());
1035  delete c;
1036 #endif
1037  return newOne;
1038 }
1039 
1040 
1041 QString KSSLCertificate::toString()
1042 {
1043  return toDer().toBase64();
1044 }
1045 
1046 
1047 QString KSSLCertificate::verifyText(KSSLValidation x) {
1048  switch (x) {
1049  // messages for errors defined in verify(1)
1050  case KSSLCertificate::Ok:
1051  return i18n("The certificate is valid.");
1052  case KSSLCertificate::GetIssuerCertFailed:
1053  return i18n("Retrieval of the issuer certificate failed. This means the CA's (Certificate Authority) certificate can not be found.");
1054  case KSSLCertificate::GetCRLFailed:
1055  return i18n("Retrieval of the CRL (Certificate Revocation List) failed. This means the CA's (Certificate Authority) CRL can not be found.");
1056  case KSSLCertificate::DecryptCertificateSignatureFailed:
1057  return i18n("The decryption of the certificate's signature failed. This means it could not even be calculated as opposed to just not matching the expected result.");
1058  case KSSLCertificate::DecryptCRLSignatureFailed:
1059  return i18n("The decryption of the CRL's (Certificate Revocation List) signature failed. This means it could not even be calculated as opposed to just not matching the expected result.");
1060  case KSSLCertificate::DecodeIssuerPublicKeyFailed:
1061  return i18n("The decoding of the public key of the issuer failed. This means that the CA's (Certificate Authority) certificate can not be used to verify the certificate you wanted to use.");
1062  case KSSLCertificate::CertificateSignatureFailed:
1063  return i18n("The certificate's signature is invalid. This means that the certificate can not be verified.");
1064  case KSSLCertificate::CRLSignatureFailed:
1065  return i18n("The CRL's (Certificate Revocation List) signature is invalid. This means that the CRL can not be verified.");
1066  case KSSLCertificate::CertificateNotYetValid:
1067  return i18n("The certificate is not valid, yet.");
1068  case KSSLCertificate::CertificateHasExpired:
1069  return i18n("The certificate is not valid, any more.");
1070  case KSSLCertificate::CRLNotYetValid:
1071  return i18n("The CRL (Certificate Revocation List) is not valid, yet.");
1072  case KSSLCertificate::CRLHasExpired:
1073  return i18n("The CRL (Certificate Revocation List) is not valid, yet.");
1074  case KSSLCertificate::CertificateFieldNotBeforeErroneous:
1075  return i18n("The time format of the certificate's 'notBefore' field is invalid.");
1076  case KSSLCertificate::CertificateFieldNotAfterErroneous:
1077  return i18n("The time format of the certificate's 'notAfter' field is invalid.");
1078  case KSSLCertificate::CRLFieldLastUpdateErroneous:
1079  return i18n("The time format of the CRL's (Certificate Revocation List) 'lastUpdate' field is invalid.");
1080  case KSSLCertificate::CRLFieldNextUpdateErroneous:
1081  return i18n("The time format of the CRL's (Certificate Revocation List) 'nextUpdate' field is invalid.");
1082  case KSSLCertificate::OutOfMemory:
1083  return i18n("The OpenSSL process ran out of memory.");
1084  case KSSLCertificate::SelfSigned:
1085  return i18n("The certificate is self-signed and not in the list of trusted certificates. If you want to accept this certificate, import it into the list of trusted certificates.");
1086  case KSSLCertificate::SelfSignedChain: // this is obsolete and kept around for backwards compatibility, only
1087  case KSSLCertificate::SelfSignedInChain:
1088  return i18n("The certificate is self-signed. While the trust chain could be built up, the root CA's (Certificate Authority) certificate can not be found.");
1089  case KSSLCertificate::GetIssuerCertLocallyFailed:
1090  return i18n("The CA's (Certificate Authority) certificate can not be found. Most likely, your trust chain is broken.");
1091  case KSSLCertificate::VerifyLeafSignatureFailed:
1092  return i18n("The certificate can not be verified as it is the only certificate in the trust chain and not self-signed. If you self-sign the certificate, make sure to import it into the list of trusted certificates.");
1093  case KSSLCertificate::CertificateChainTooLong:
1094  return i18n("The certificate chain is longer than the maximum depth specified.");
1095  case KSSLCertificate::Revoked: // this is obsolete and kept around for backwards compatibility, only
1096  case KSSLCertificate::CertificateRevoked:
1097  return i18n("The certificate has been revoked.");
1098  case KSSLCertificate::InvalidCA:
1099  return i18n("The certificate's CA (Certificate Authority) is invalid.");
1100  case KSSLCertificate::PathLengthExceeded:
1101  return i18n("The length of the trust chain exceeded one of the CA's (Certificate Authority) 'pathlength' parameters, making all subsequent signatures invalid.");
1102  case KSSLCertificate::InvalidPurpose:
1103  return i18n("The certificate has not been signed for the purpose you tried to use it for. This means the CA (Certificate Authority) does not allow this usage.");
1104  case KSSLCertificate::Untrusted: // this is obsolete and kept around for backwards compatibility, only
1105  case KSSLCertificate::CertificateUntrusted:
1106  return i18n("The root CA (Certificate Authority) is not trusted for the purpose you tried to use this certificate for.");
1107  case KSSLCertificate::Rejected: // this is obsolete and kept around for backwards compatibility, only // this is obsolete and kept around for backwards compatibility, onle
1108  case KSSLCertificate::CertificateRejected:
1109  return i18n("The root CA (Certificate Authority) has been marked to be rejected for the purpose you tried to use it for.");
1110  case KSSLCertificate::IssuerSubjectMismatched:
1111  return i18n("The certificate's CA (Certificate Authority) does not match the CA name of the certificate.");
1112  case KSSLCertificate::AuthAndSubjectKeyIDMismatched:
1113  return i18n("The CA (Certificate Authority) certificate's key ID does not match the key ID in the 'Issuer' section of the certificate you are trying to use.");
1114  case KSSLCertificate::AuthAndSubjectKeyIDAndNameMismatched:
1115  return i18n("The CA (Certificate Authority) certificate's key ID and name do not match the key ID and name in the 'Issuer' section of the certificate you are trying to use.");
1116  case KSSLCertificate::KeyMayNotSignCertificate:
1117  return i18n("The certificate's CA (Certificate Authority) is not allowed to sign certificates.");
1118  case KSSLCertificate::ApplicationVerificationFailed:
1119  return i18n("OpenSSL could not be verified.");
1120 
1121 
1122  // this is obsolete and kept around for backwards compatibility, only
1123  case KSSLCertificate::SignatureFailed:
1124  return i18n("The signature test for this certificate failed. This could mean that the signature of this certificate or any in its trust path are invalid, could not be decoded or that the CRL (Certificate Revocation List) could not be verified. If you see this message, please let the author of the software you are using know that he or she should use the new, more specific error messages.");
1125  case KSSLCertificate::Expired:
1126  return i18n("This certificate, any in its trust path or its CA's (Certificate Authority) CRL (Certificate Revocation List) is not valid. Any of them could not be valid yet or not valid any more. If you see this message, please let the author of the software you are using know that he or she should use the new, more specific error messages.");
1127  // continue 'useful' messages
1128 
1129  // other error messages
1130  case KSSLCertificate::ErrorReadingRoot:
1131  case KSSLCertificate::NoCARoot:
1132  return i18n("Certificate signing authority root files could not be found so the certificate is not verified.");
1133  case KSSLCertificate::NoSSL:
1134  return i18n("SSL support was not found.");
1135  case KSSLCertificate::PrivateKeyFailed:
1136  return i18n("Private key test failed.");
1137  case KSSLCertificate::InvalidHost:
1138  return i18n("The certificate has not been issued for this host.");
1139  case KSSLCertificate::Irrelevant:
1140  return i18n("This certificate is not relevant.");
1141  default:
1142  break;
1143  }
1144 
1145  return i18n("The certificate is invalid.");
1146 }
1147 
1148 
1149 QByteArray KSSLCertificate::toDer() {
1150  QByteArray qba;
1151 #ifdef KSSL_HAVE_SSL
1152  int certlen = d->kossl->i2d_X509(getCert(), NULL);
1153  if (certlen >= 0) {
1154  // These should technically be unsigned char * but it doesn't matter
1155  // for our purposes
1156  char *cert = new char[certlen];
1157  unsigned char *p = (unsigned char *)cert;
1158  // FIXME: return code!
1159  d->kossl->i2d_X509(getCert(), &p);
1160 
1161  // encode it into a QString
1162  qba = QByteArray(cert, certlen);
1163  delete[] cert;
1164  }
1165 #endif
1166  return qba;
1167 }
1168 
1169 
1170 
1171 QByteArray KSSLCertificate::toPem() {
1172 QByteArray qba;
1173 QString thecert = toString();
1174 const char *header = "-----BEGIN CERTIFICATE-----\n";
1175 const char *footer = "-----END CERTIFICATE-----\n";
1176 
1177  // We just do base64 on the ASN1
1178  // 64 character lines (unpadded)
1179  unsigned int xx = thecert.length() - 1;
1180  for (unsigned int i = 0; i < xx/64; i++) {
1181  thecert.insert(64*(i+1)+i, '\n');
1182  }
1183 
1184  thecert.prepend(header);
1185 
1186  if (thecert[thecert.length()-1] != '\n') {
1187  thecert += '\n';
1188  }
1189 
1190  thecert.append(footer);
1191 
1192  qba = thecert.toLocal8Bit();
1193  return qba;
1194 }
1195 
1196 
1197 #define NETSCAPE_CERT_HDR "certificate"
1198 
1199 #ifdef KSSL_HAVE_SSL
1200 #if OPENSSL_VERSION_NUMBER < 0x00909000L
1201 
1202 typedef struct NETSCAPE_X509_st
1203 {
1204  ASN1_OCTET_STRING *header;
1205  X509 *cert;
1206 } NETSCAPE_X509;
1207 #endif
1208 #endif
1209 
1210 // what a piece of crap this is
1211 QByteArray KSSLCertificate::toNetscape() {
1212  QByteArray qba;
1213 #ifdef KSSL_HAVE_SSL
1214  NETSCAPE_X509 nx;
1215  ASN1_OCTET_STRING hdr;
1216  KTemporaryFile ktf;
1217  ktf.open();
1218  FILE *ktf_fs = fopen(ktf.fileName().toLatin1(), "r+");
1219 
1220  hdr.data = (unsigned char *)NETSCAPE_CERT_HDR;
1221  hdr.length = strlen(NETSCAPE_CERT_HDR);
1222  nx.header = &hdr;
1223  nx.cert = getCert();
1224 
1225  d->kossl->ASN1_item_i2d_fp(ktf_fs,(unsigned char *)&nx);
1226  fclose(ktf_fs);
1227 
1228  QFile qf(ktf.fileName());
1229  if (qf.open(QIODevice::ReadOnly)) {
1230  qba = qf.readAll();
1231  }
1232 #endif
1233 return qba;
1234 }
1235 
1236 
1237 
1238 QString KSSLCertificate::toText() {
1239  QString text;
1240 #ifdef KSSL_HAVE_SSL
1241  KTemporaryFile ktf;
1242  ktf.open();
1243  FILE *ktf_fs = fopen(ktf.fileName().toLatin1(), "r+");
1244 
1245  d->kossl->X509_print(ktf_fs, getCert());
1246  fclose(ktf_fs);
1247 
1248  QFile qf(ktf.fileName());
1249  if (!qf.open(QIODevice::ReadOnly) )
1250  return text;
1251  char *buf = new char[qf.size()+1];
1252  qf.read(buf, qf.size());
1253  buf[qf.size()] = 0;
1254  text = buf;
1255  delete[] buf;
1256  qf.close();
1257 #endif
1258 return text;
1259 }
1260 
1261 bool KSSLCertificate::setCert(const QString& cert) {
1262 #ifdef KSSL_HAVE_SSL
1263  QByteArray qba, qbb = cert.toLocal8Bit();
1264  qba = QByteArray::fromBase64(qbb);
1265  unsigned char *qbap = reinterpret_cast<unsigned char *>(qba.data());
1266  X509 *x5c = KOSSL::self()->d2i_X509(NULL, &qbap, qba.size());
1267  if (x5c) {
1268  setCert(x5c);
1269  return true;
1270  }
1271 #endif
1272  return false;
1273 }
1274 
1275 
1276 KSSLX509V3& KSSLCertificate::x509V3Extensions() {
1277  return d->_extensions;
1278 }
1279 
1280 
1281 bool KSSLCertificate::isSigner() {
1282  return d->_extensions.certTypeCA();
1283 }
1284 
1285 
1286 QStringList KSSLCertificate::subjAltNames() const {
1287  QStringList rc;
1288 #ifdef KSSL_HAVE_SSL
1289  STACK_OF(GENERAL_NAME) *names;
1290  names = (STACK_OF(GENERAL_NAME)*)d->kossl->X509_get_ext_d2i(d->m_cert, NID_subject_alt_name, 0, 0);
1291 
1292  if (!names) {
1293  return rc;
1294  }
1295 
1296  int cnt = d->kossl->sk_GENERAL_NAME_num(names);
1297 
1298  for (int i = 0; i < cnt; i++) {
1299  const GENERAL_NAME *val = (const GENERAL_NAME *)d->kossl->sk_value(names, i);
1300  if (val->type != GEN_DNS) {
1301  continue;
1302  }
1303 
1304  QString s = (const char *)d->kossl->ASN1_STRING_data(val->d.ia5);
1305  if (!s.isEmpty() &&
1306  /* skip subjectAltNames with embedded NULs */
1307  s.length() == d->kossl->ASN1_STRING_length(val->d.ia5)) {
1308  rc += s;
1309  }
1310  }
1311  d->kossl->sk_free(names);
1312 #endif
1313  return rc;
1314 }
1315 
1316 
1317 QDataStream& operator<<(QDataStream& s, const KSSLCertificate& r) {
1318  QStringList qsl;
1319  QList<KSSLCertificate *> cl = const_cast<KSSLCertificate&>(r).chain().getChain();
1320 
1321  foreach(KSSLCertificate *c, cl) {
1322  qsl << c->toString();
1323  }
1324 
1325  qDeleteAll(cl);
1326  s << const_cast<KSSLCertificate&>(r).toString() << qsl;
1327 
1328  return s;
1329 }
1330 
1331 
1332 QDataStream& operator>>(QDataStream& s, KSSLCertificate& r) {
1333  QStringList qsl;
1334  QString cert;
1335 
1336  s >> cert >> qsl;
1337 
1338  if (r.setCert(cert) && !qsl.isEmpty()) {
1339  r.chain().setCertChain(qsl);
1340  }
1341 
1342  return s;
1343 }
1344 
1345 
1346 
operator>>
QDataStream & operator>>(QDataStream &s, KSSLCertificate &r)
Definition: ksslcertificate.cpp:1332
KSSLCertificate::Expired
Definition: ksslcertificate.h:120
i18n
QString i18n(const char *text)
KSSLCertificate::DecryptCRLSignatureFailed
Definition: ksslcertificate.h:137
KSSLCertificate::replicate
KSSLCertificate * replicate()
Explicitly make a copy of this certificate.
Definition: ksslcertificate.cpp:1027
KSSLCertificate::PathLengthExceeded
Definition: ksslcertificate.h:120
KSSLCertificate::Unknown
Definition: ksslcertificate.h:119
header
const char header[]
KSSLCertificate::CRLNotYetValid
Definition: ksslcertificate.h:128
kssldefs.h
KSSLCertificate::AuthAndSubjectKeyIDMismatched
Definition: ksslcertificate.h:140
KSSLCertificate::CertificateUntrusted
Definition: ksslcertificate.h:134
kdebug.h
KSSLCertificate::Untrusted
Definition: ksslcertificate.h:122
NETSCAPE_CERT_HDR
#define NETSCAPE_CERT_HDR
Definition: ksslcertificate.cpp:1197
KSSLCertificate::NoCARoot
Definition: ksslcertificate.h:119
KStandardDirs::addResourceType
bool addResourceType(const char *type, const QString &relativename, bool priority=true)
KSSLCertificate::validate
KSSLValidation validate()
Check if this is a valid certificate.
Definition: ksslcertificate.cpp:612
KSSLCertificate::getSerialNumber
QString getSerialNumber() const
Get the serial number of the certificate.
Definition: ksslcertificate.cpp:182
KSSLCertificate::DecodeIssuerPublicKeyFailed
Definition: ksslcertificate.h:125
KSSLCertificate::InvalidPurpose
Definition: ksslcertificate.h:119
KSSLCertificate::Any
Definition: ksslcertificate.h:147
KSSLCertificate::x509V3Extensions
KSSLX509V3 & x509V3Extensions()
Access the X.509v3 parameters.
Definition: ksslcertificate.cpp:1276
KSSLCertificate::CertificateNotYetValid
Definition: ksslcertificate.h:127
KSSLCertificate::PrivateKeyFailed
Definition: ksslcertificate.h:123
KSSLCertificate::chain
KSSLCertChain & chain()
Get a reference to the certificate chain.
Definition: ksslcertificate.cpp:128
KSSLCertificate::Revoked
Definition: ksslcertificate.h:122
KSSLCertificate::toNetscape
QByteArray toNetscape()
Convert the certificate to Netscape format.
Definition: ksslcertificate.cpp:1211
ksslcertificate.h
KSSLCertificate::CertificateHasExpired
Definition: ksslcertificate.h:127
KSSLCertificate::SignatureFailed
Definition: ksslcertificate.h:122
KSSLCertificate::KeyMayNotSignCertificate
Definition: ksslcertificate.h:142
KSSLCertificate::OutOfMemory
Definition: ksslcertificate.h:140
KSSLCertificate::VerifyLeafSignatureFailed
Definition: ksslcertificate.h:134
KSSLCertificate::CRLHasExpired
Definition: ksslcertificate.h:128
KGlobal::dirs
KStandardDirs * dirs()
KSSLCertificate::CertificateFieldNotAfterErroneous
Definition: ksslcertificate.h:130
KSSLCertificate
KDE X.509 Certificate.
Definition: ksslcertificate.h:74
KSSLCertificate::revalidate
KSSLValidation revalidate()
Check if this is a valid certificate.
Definition: ksslcertificate.cpp:777
KSSLCertificate::IssuerSubjectMismatched
Definition: ksslcertificate.h:143
KSSLCertificate::getIssuer
QString getIssuer() const
Get the issuer of the certificate (X.509 map).
Definition: ksslcertificate.cpp:444
QString
KTemporaryFile
KSSLCertificate::getPublicKeyText
QString getPublicKeyText() const
Get the public key.
Definition: ksslcertificate.cpp:341
KSSLCertChain::rawChain
void * rawChain()
Read the raw chain in OpenSSL format.
Definition: ksslcertchain.cpp:114
KSSLCertificate::SelfSigned
Definition: ksslcertificate.h:121
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KSSLCertificate::getSubject
QString getSubject() const
Get the subject of the certificate (X.509 map).
Definition: ksslcertificate.cpp:167
KSSLCertificate::getMD5DigestText
QString getMD5DigestText() const
Get the MD5 digest of the certificate.
Definition: ksslcertificate.cpp:264
klocale.h
KSSLCertificate::getQDTNotAfter
QDateTime getQDTNotAfter() const
Get the date that the certificate is valid until.
Definition: ksslcertificate.cpp:1006
KSSLCertificate::ErrorReadingRoot
Definition: ksslcertificate.h:121
KSSLCertificate::Ok
Definition: ksslcertificate.h:119
i18nc
QString i18nc(const char *ctxt, const char *text)
KSSLCertificate::GetCRLFailed
Definition: ksslcertificate.h:141
KSSLCertificate::Rejected
Definition: ksslcertificate.h:123
KSSLCertificate::None
Definition: ksslcertificate.h:146
KSSLCertChain
KDE Certificate Chain Representation Class.
Definition: ksslcertchain.h:43
KSSLCertificate::getQDTNotBefore
QDateTime getQDTNotBefore() const
Get the date that the certificate becomes valid on.
Definition: ksslcertificate.cpp:997
KSSLCertificate::verifyText
static QString verifyText(KSSLValidation x)
Obtain the localized message that corresponds to a validation result.
Definition: ksslcertificate.cpp:1047
KSSLCertificate::getCert
X509 * getCert()
Definition: ksslcertificate.cpp:569
KSSLCertificate::ApplicationVerificationFailed
Definition: ksslcertificate.h:138
STACK_OF
#define STACK_OF(x)
Definition: ksslpkcs12.h:46
KSSLCertificate::InvalidCA
Definition: ksslcertificate.h:120
KSSLCertChain::setCertChain
void setCertChain(const QStringList &chain)
Set the certificate chain as a list of base64 encoded X.509 certificates.
Definition: ksslcertchain.cpp:194
KSSLCertificate::~KSSLCertificate
~KSSLCertificate()
Destroy this X.509 certificate.
Definition: ksslcertificate.cpp:118
ksslx509v3.h
KSSLCertificate::KSSLValidation
KSSLValidation
Result of the validate() call.
Definition: ksslcertificate.h:119
KSSLCertificate::CertificateRevoked
Definition: ksslcertificate.h:133
KSSLCertificate::KSSLPurpose
KSSLPurpose
Definition: ksslcertificate.h:146
KSSLCertificate::toString
QString toString()
Convert this certificate to a string.
Definition: ksslcertificate.cpp:1041
QStringList
KOSSL
#define KOSSL
Definition: kopenssl.h:25
KSSLCertificate::CRLSignatureFailed
Definition: ksslcertificate.h:135
operator<<
QDataStream & operator<<(QDataStream &s, const KSSLCertificate &r)
Definition: ksslcertificate.cpp:1317
KSSLCertificate::CRLFieldLastUpdateErroneous
Definition: ksslcertificate.h:131
KSSLX509V3
KDE X509v3 Flag Class.
Definition: ksslx509v3.h:37
KSSLCertificate::getKeyType
QString getKeyType() const
Get the key type (RSA, DSA, etc).
Definition: ksslcertificate.cpp:313
KSSLCertificate::getKDEKey
QString getKDEKey() const
KDEKey is a concatenation "Subject (MD5)", mostly needed for SMIME.
Definition: ksslcertificate.cpp:246
KSSLCertificate::NoSSL
Definition: ksslcertificate.h:121
KSSLCertificate::getMD5DigestFromKDEKey
static QString getMD5DigestFromKDEKey(const QString &k)
Aegypten semantics force us to search by MD5Digest only.
Definition: ksslcertificate.cpp:251
KSSLCertificate::isSigner
bool isSigner()
Check if this is a signer certificate.
Definition: ksslcertificate.cpp:1281
KSSLCertificate::KSSLCertificate
KSSLCertificate()
Definition: ksslcertificate.cpp:93
KSSLCertificate::isValid
bool isValid()
Check if this is a valid certificate.
Definition: ksslcertificate.cpp:587
KSSLCertificate::fromX509
static KSSLCertificate * fromX509(X509 *x5)
Create an X.509 certificate from the internal representation.
Definition: ksslcertificate.cpp:133
ksslutils.h
KSSLCertificate::CertificateSignatureFailed
Definition: ksslcertificate.h:135
KStandardDirs::resourceDirs
QStringList resourceDirs(const char *type) const
KSSLCertificate::DecryptCertificateSignatureFailed
Definition: ksslcertificate.h:136
KSSLCertificate::toText
QString toText()
Convert the certificate to OpenSSL plain text format.
Definition: ksslcertificate.cpp:1238
QDateTime
KSSLCertificate::getNotBefore
QString getNotBefore() const
Get the date that the certificate becomes valid on.
Definition: ksslcertificate.cpp:979
KSSLCertificate::fromString
static KSSLCertificate * fromString(const QByteArray &cert)
Create an X.509 certificate from a base64 encoded string.
Definition: ksslcertificate.cpp:145
KSSLCertificate::AuthAndSubjectKeyIDAndNameMismatched
Definition: ksslcertificate.h:139
KSSLCertificate::CertificateChainTooLong
Definition: ksslcertificate.h:141
KSSLCertificate::Irrelevant
Definition: ksslcertificate.h:124
KSSLCertificate::getNotAfter
QString getNotAfter() const
Get the date that the certificate is valid until.
Definition: ksslcertificate.cpp:988
ktemporaryfile.h
KSSLCertificate::InvalidHost
Definition: ksslcertificate.h:123
KSSLCertificate::setChain
void setChain(void *c)
Definition: ksslcertificate.cpp:461
KSSLCertificate::CertificateFieldNotBeforeErroneous
Definition: ksslcertificate.h:129
kstandarddirs.h
KSSLCertificate::processError
KSSLValidation processError(int ec)
Definition: ksslcertificate.cpp:788
KSSLCertificate::SMIMEEncrypt
Definition: ksslcertificate.h:147
KSSLCertificate::subjAltNames
QStringList subjAltNames() const
The alternate subject name.
Definition: ksslcertificate.cpp:1286
KSSLCertificate::toDer
QByteArray toDer()
Convert the certificate to DER (ASN.1) format.
Definition: ksslcertificate.cpp:1149
fopen
FILE * fopen(const QString &pathname, const char *mode)
KSSLCertificate::getSignatureText
QString getSignatureText() const
Get the signature.
Definition: ksslcertificate.cpp:196
KSSLCertificate::GetIssuerCertLocallyFailed
Definition: ksslcertificate.h:126
KSSLCertificate::toPem
QByteArray toPem()
Convert the certificate to PEM (base64) format.
Definition: ksslcertificate.cpp:1171
KSSLCertificate::getMD5Digest
QString getMD5Digest() const
Get the MD5 digest of the certificate.
Definition: ksslcertificate.cpp:290
kcodecs.h
kopenssl.h
KSSLCertificate::SelfSignedInChain
Definition: ksslcertificate.h:138
KSSLCertificate::validateVerbose
KSSLValidationList validateVerbose(KSSLPurpose p)
Check if this is a valid certificate.
Definition: ksslcertificate.cpp:631
KSSLCertificate::GetIssuerCertFailed
Definition: ksslcertificate.h:125
KSSLCertificate::SSLClient
Definition: ksslcertificate.h:146
KSSLCertificate::CRLFieldNextUpdateErroneous
Definition: ksslcertificate.h:132
KSSLCertificate::CertificateRejected
Definition: ksslcertificate.h:137
ksslcertchain.h
operator==
int operator==(KSSLCertificate &x, KSSLCertificate &y)
Definition: ksslcertificate.cpp:1015
hv
static char hv[]
Definition: ksslcertificate.cpp:69
KSSLCertificate::setCert
bool setCert(const QString &cert)
Re-set the certificate from a base64 string.
Definition: ksslcertificate.cpp:1261
KSSLCertificate::SelfSignedChain
Definition: ksslcertificate.h:124
QList
KIO::number
QString number(KIO::filesize_t size)
Converts a size to a string representation Not unlike QString::number(...)
Definition: global.cpp:63
KSSLCertificate::getEmails
void getEmails(QStringList &to) const
FIXME: document.
Definition: ksslcertificate.cpp:228
KSSLCertificate::SSLServer
Definition: ksslcertificate.h:146
KSSLCertificate::SMIMESign
Definition: ksslcertificate.h:147
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:50:02 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal