summaryrefslogtreecommitdiffstats
path: root/tdeio/kssl/ksslcertchain.cc
blob: 4f14e4be15ce36755ee3c4dcd1ee6185688fef16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* This file is part of the KDE project
 *
 * Copyright (C) 2001 George Staikos <staikos@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
#ifdef HAVE_CONFIG_H 
#include <config.h>
#endif

#include "kssldefs.h"
#include "ksslcertificate.h"
#include "ksslcertchain.h"

// this hack provided by Malte Starostik to avoid glibc/openssl bug
// on some systems
#ifdef KSSL_HAVE_SSL
#define crypt _openssl_crypt
#include <openssl/ssl.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/x509_vfy.h>
#include <openssl/pem.h>
#include <openssl/stack.h>
#include <openssl/safestack.h>
#undef crypt
#endif

#include <kopenssl.h>
#include <kdebug.h>
#include <tqstringlist.h>


class KSSLCertChainPrivate {
public:
  KSSLCertChainPrivate() {
     kossl = KOSSL::self();
  }

  ~KSSLCertChainPrivate() {
  }

  KOSSL *kossl;
};

KSSLCertChain::KSSLCertChain() {
  d = new KSSLCertChainPrivate;
  _chain = NULL;
}


KSSLCertChain::~KSSLCertChain() {
#ifdef KSSL_HAVE_SSL
  if (_chain) {
    STACK_OF(X509) *x = (STACK_OF(X509) *)_chain;

    for (;;) {
      X509* x5 = reinterpret_cast<X509*>(d->kossl->OPENSSL_sk_pop(x));
      if (!x5) break;
      d->kossl->X509_free(x5);
    }
    d->kossl->OPENSSL_sk_free(x);
  }
#endif
  delete d;
}


bool KSSLCertChain::isValid() {
  return (_chain && depth() > 0);
}


KSSLCertChain *KSSLCertChain::replicate() {
KSSLCertChain *x = new KSSLCertChain;
TQPtrList<KSSLCertificate> ch = getChain();

  x->setChain(ch);   // this will do a deep copy for us
  ch.setAutoDelete(true);
return x;
}


int KSSLCertChain::depth() {
#ifdef KSSL_HAVE_SSL
  return d->kossl->OPENSSL_sk_num((STACK_OF(X509)*)_chain);
#endif
return 0;
}


TQPtrList<KSSLCertificate> KSSLCertChain::getChain() {
TQPtrList<KSSLCertificate> cl;
if (!_chain) return cl;
#ifdef KSSL_HAVE_SSL
STACK_OF(X509) *x = (STACK_OF(X509) *)_chain;

   for (int i = 0; i < d->kossl->OPENSSL_sk_num(x); i++) {
     X509* x5 = reinterpret_cast<X509*>(d->kossl->OPENSSL_sk_value(x, i));
     if (!x5) continue;
     KSSLCertificate *nc = new KSSLCertificate;
     nc->setCert(d->kossl->X509_dup(x5));
     cl.append(nc);
   }

#endif
return cl;
}


void KSSLCertChain::setChain(TQPtrList<KSSLCertificate>& chain) {
#ifdef KSSL_HAVE_SSL
if (_chain) {
    STACK_OF(X509) *x = (STACK_OF(X509) *)_chain;

    for (;;) {
      X509* x5 = reinterpret_cast<X509*>(d->kossl->OPENSSL_sk_pop(x));
      if (!x5) break;
      d->kossl->X509_free(x5);
    }
    d->kossl->OPENSSL_sk_free(x);
    _chain = NULL;
}

  if (chain.count() == 0) return;
  _chain = reinterpret_cast<STACK_OF(X509)*>(d->kossl->OPENSSL_sk_new(NULL));
  for (KSSLCertificate *x = chain.first(); x != 0; x = chain.next()) {
    d->kossl->OPENSSL_sk_push((STACK_OF(X509) *)_chain, d->kossl->X509_dup(x->getCert()));
  }

#endif
}

 
void KSSLCertChain::setChain(void *stack_of_x509) {
#ifdef KSSL_HAVE_SSL
    if (_chain) {
        STACK_OF(X509) *x = (STACK_OF(X509) *)_chain;

        for (;;) {
          X509* x5 = reinterpret_cast<X509*>(d->kossl->OPENSSL_sk_pop(x));
          if (!x5) break;
          d->kossl->X509_free(x5);
        }
        d->kossl->OPENSSL_sk_free(x);
        _chain = NULL;
    }

    if (!stack_of_x509) return;

    _chain = reinterpret_cast<STACK_OF(X509)*>(d->kossl->OPENSSL_sk_new(NULL));
    STACK_OF(X509) *x = (STACK_OF(X509) *)stack_of_x509;

   for (int i = 0; i < d->kossl->OPENSSL_sk_num(x); i++) {
     X509* x5 = reinterpret_cast<X509*>(d->kossl->OPENSSL_sk_value(x, i));
     if (!x5) continue;
     d->kossl->OPENSSL_sk_push((STACK_OF(X509)*)_chain,d->kossl->X509_dup(x5));
   }

#else
    _chain = NULL;
#endif
}


void KSSLCertChain::setChain(TQStringList chain) {
	setCertChain(chain);
}

void KSSLCertChain::setCertChain(const TQStringList& chain) {
    TQPtrList<KSSLCertificate> cl;
    cl.setAutoDelete(true);
    for (TQStringList::ConstIterator s = chain.begin(); s != chain.end(); ++s) {
       KSSLCertificate *c = KSSLCertificate::fromString((*s).local8Bit());
       if (c) {
          cl.append(c);
       }
    }
    setChain(cl);
}