~ubuntu-branches/ubuntu/wily/afnix/wily

« back to all changes in this revision

Viewing changes to src/srv/tls/shl/TlsProto.cpp

  • Committer: Package Import Robot
  • Author(s): Nobuhiro Iwamatsu
  • Date: 2015-07-11 02:00:35 UTC
  • mfrom: (10.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20150711020035-2nhpztq7s15qyc0v
Tags: 2.5.1-1
* New upstream release. (Closes: #789968)
* Update debian/control.
  - Update Standards-Version to 3.9.6.
* Add support mips64(el) and ppc64el. (Closes: #741508, #748146)
* Add patches/support-gcc-5.x.patch. (Closes: #777767)
  - Fix build with gcc-5.x.
* Add patches/Disable-NET0001.als.patch.
  - Disable test of NET0001.als.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ---------------------------------------------------------------------------
 
2
// - TlsProto.cpp                                                            -
 
3
// - afnix:tls service - tls protocol class implementation                   -
 
4
// ---------------------------------------------------------------------------
 
5
// - This program is free software;  you can redistribute it  and/or  modify -
 
6
// - it provided that this copyright notice is kept intact.                  -
 
7
// -                                                                         -
 
8
// - This program  is  distributed in  the hope  that it will be useful, but -
 
9
// - without  any  warranty;  without  even   the   implied    warranty   of -
 
10
// - merchantability or fitness for a particular purpose.  In no event shall -
 
11
// - the copyright holder be liable for any  direct, indirect, incidental or -
 
12
// - special damages arising in any way out of the use of this software.     -
 
13
// ---------------------------------------------------------------------------
 
14
// - copyright (c) 1999-2015 amaury darsch                                   -
 
15
// ---------------------------------------------------------------------------
 
16
 
 
17
#include "Vector.hpp"
 
18
#include "Integer.hpp"
 
19
#include "TlsTypes.hxx"
 
20
#include "TlsShake.hpp"
 
21
#include "TlsProto.hpp"
 
22
#include "TlsChello.hpp"
 
23
#include "TlsShello.hpp"
 
24
#include "QuarkZone.hpp"
 
25
#include "Exception.hpp"
 
26
 
 
27
namespace afnix {
 
28
 
 
29
  // -------------------------------------------------------------------------
 
30
  // - public section                                                        -
 
31
  // -------------------------------------------------------------------------
 
32
 
 
33
  // create a tls protocol by version
 
34
 
 
35
  TlsProto* TlsProto::create (const t_byte vmaj, const t_byte vmin) {
 
36
    // check version
 
37
    String vers = tls_vers_tostring (vmaj, vmin);
 
38
    if (tls_vers_isvalid (vmaj, vmin) == false) {
 
39
      throw Exception ("tls-error", "invalid tls version", vers);
 
40
    }
 
41
    // process major version
 
42
    if (vmaj == TLS_VMAJ_3XX) {
 
43
      if (vmin == TLS_VMIN_301) return new TlsProto;
 
44
    }
 
45
    throw Exception ("tls-error", "cannot create tls protocol version", vers);
 
46
  }
 
47
 
 
48
  // create a tls protocol by state
 
49
 
 
50
  TlsProto* TlsProto::create (TlsState* sta) {
 
51
    // check for nil
 
52
    if (sta == nilp) return nilp;
 
53
    // get version and check
 
54
    t_byte vmaj = sta->getvmaj ();
 
55
    t_byte vmin = sta->getvmin ();
 
56
    // create the protocol
 
57
    return TlsProto::create (vmaj, vmin);
 
58
  }
 
59
 
 
60
  // -------------------------------------------------------------------------
 
61
  // - class section                                                         -
 
62
  // -------------------------------------------------------------------------
 
63
 
 
64
  // create a default decoder
 
65
 
 
66
  TlsProto::TlsProto (void) {
 
67
  }
 
68
 
 
69
  // return the class name
 
70
  
 
71
  String TlsProto::repr (void) const {
 
72
    return "TlsProto";
 
73
  }
 
74
 
 
75
  // get a record by input stream
 
76
 
 
77
  TlsRecord* TlsProto::getrcd (InputStream* is) const {
 
78
    rdlock ();
 
79
    try {
 
80
      TlsRecord* result = new TlsRecord (is);
 
81
      unlock ();
 
82
      return result;
 
83
    } catch (...) {
 
84
      unlock ();
 
85
      throw;
 
86
    }
 
87
  }
 
88
 
 
89
  // get a message a by input stream
 
90
 
 
91
  TlsMessage* TlsProto::getmsg (InputStream* is) const {
 
92
    rdlock ();
 
93
    TlsRecord* rcd = nilp;
 
94
    try {
 
95
      // get the next available record
 
96
      rcd = getrcd (is);
 
97
      // decode the record
 
98
      TlsMessage* msg = getmsg (rcd);
 
99
      unlock ();
 
100
      return msg;
 
101
    } catch (...) {
 
102
      delete rcd;
 
103
      unlock ();
 
104
      throw;
 
105
    }
 
106
  }
 
107
 
 
108
  // decode a record into a message
 
109
  
 
110
  TlsMessage* TlsProto::getmsg (TlsRecord* rcd) const {
 
111
    // check for nil
 
112
    if (rcd == nilp) return nilp;
 
113
    // lock and decode
 
114
    rdlock ();
 
115
    try {
 
116
      // get the record type and check
 
117
      t_byte type = rcd->gettype ();
 
118
      // prepare result
 
119
      TlsMessage* result = nilp;
 
120
      // map the record
 
121
      switch (type) {
 
122
      case TLS_TYPE_HSK:
 
123
        result = new TlsShake (rcd);
 
124
        break;
 
125
      default:
 
126
        throw Exception ("tls-error", "cannot decode record into a message");
 
127
        break;
 
128
      }
 
129
      unlock ();
 
130
      return result;
 
131
    } catch (...) {
 
132
      unlock ();
 
133
      throw;
 
134
    }
 
135
  }
 
136
 
 
137
  // encode a tls message
 
138
 
 
139
  void TlsProto::encode (OutputStream* os, TlsMessage* mesg) const {
 
140
    // check for nil
 
141
    if ((mesg == nilp) || (os == nilp)) return;
 
142
    rdlock ();
 
143
    try {
 
144
      // check for an alert
 
145
      //TlsAlert* alt = dynamic_cast <TlsAlert*> (mesg);
 
146
      //if (alt != nilp) alt->encode (os);
 
147
      // check for a handshake
 
148
      TlsShake* hsk = dynamic_cast <TlsShake*> (mesg);
 
149
      if (hsk != nilp) hsk->write (os);
 
150
      unlock ();
 
151
    } catch (...) {
 
152
      unlock ();
 
153
      throw;
 
154
    }
 
155
  }
 
156
 
 
157
  // decode a handshake block
 
158
 
 
159
  TlsInfos* TlsProto::decode (TlsHblock* hblk) const {
 
160
    // check for nil
 
161
    if (hblk == nilp) return nilp;
 
162
    // lock and decode
 
163
    rdlock ();
 
164
    try {
 
165
      // get the block type and check
 
166
      t_byte type = hblk->gettype ();
 
167
      // prepare result
 
168
      TlsInfos* result = nilp;
 
169
      // map the record
 
170
      switch (type) {
 
171
      case TLS_HSHK_CLH:
 
172
        result = new TlsChello (hblk);
 
173
        break;
 
174
      default:
 
175
        throw Exception ("tls-error", "cannot decode handshake block");
 
176
        break;
 
177
      }
 
178
      unlock ();
 
179
      return result;
 
180
    } catch (...) {
 
181
      unlock ();
 
182
      throw;
 
183
    }
 
184
  }
 
185
 
 
186
  // get a client hello by input stream
 
187
 
 
188
  TlsInfos* TlsProto::getchlo (InputStream* is) const {
 
189
    rdlock ();
 
190
    TlsHblock* blk = nilp;
 
191
    try {
 
192
      // get the next available message
 
193
      TlsMessage* msg = getmsg (is);
 
194
      if (msg == nilp) {
 
195
        unlock ();
 
196
        return nilp;
 
197
      }
 
198
      // map it to a handshake message
 
199
      TlsShake* shk = dynamic_cast <TlsShake*> (msg);
 
200
      if (shk == nilp) {
 
201
        throw Exception ("tls-error", "cannot get handshake message");
 
202
      }                  
 
203
      // create a handshake iterator
 
204
      TlsShakeit sit (shk);
 
205
      // get the handshake block
 
206
      blk = dynamic_cast <TlsHblock*> (sit.getobj ());
 
207
      if (blk == nilp) {
 
208
        throw Exception ("tls-error", "cannot get handshake block");
 
209
      }
 
210
      // move to the end and check
 
211
      sit.next ();
 
212
      if (sit.isend () == false) {
 
213
        throw Exception ("tls-error", "inconsistent handshake message");
 
214
      }
 
215
      // get the client block
 
216
      TlsInfos* hlo = decode (blk);
 
217
      if (hlo == nilp) {
 
218
        throw Exception ("tls-error", "cannot decode client hello block");
 
219
      }
 
220
      delete blk;
 
221
      unlock ();
 
222
      return hlo;
 
223
    } catch (...) {
 
224
      delete blk;
 
225
      unlock ();
 
226
      throw;
 
227
    }
 
228
  }
 
229
 
 
230
  // map a server hello by state
 
231
 
 
232
  TlsMessage* TlsProto::getshlo (TlsState* sta) const {
 
233
    // check for nil first
 
234
    if (sta == nilp) return nilp;
 
235
    // lock and map
 
236
    TlsShake* hsk = nilp;
 
237
    rdlock ();
 
238
    try {
 
239
      // create a tls handshake by state
 
240
      hsk = new TlsShake (sta->getvmaj (), sta->getvmin());
 
241
      // get the  server hello chunk by state
 
242
      TlsChunk chk = toshlo (sta);
 
243
      // add the chunk block to the record
 
244
      hsk->add (TLS_HSHK_SRH, chk);
 
245
      unlock ();
 
246
      return hsk;
 
247
    } catch (...) {
 
248
      delete hsk;
 
249
      unlock ();
 
250
      throw;
 
251
    }
 
252
  }
 
253
 
 
254
  // get a server hello chunk by state
 
255
 
 
256
  TlsChunk TlsProto::toshlo (TlsState* sta) const {
 
257
    // lock and generate
 
258
    rdlock ();
 
259
    try {
 
260
      // check for nil first
 
261
      if (sta == nilp) {
 
262
        throw Exception ("tls-error", "cannot generate server hello chunk");
 
263
      }
 
264
      // gather the server hello information
 
265
      t_byte vmaj = sta->getvmaj ();
 
266
      t_byte vmin = sta->getvmin ();
 
267
      t_word cifr = sta->getcifr ();
 
268
      // create a server hello
 
269
      TlsShello shlo (vmaj, vmin, cifr);
 
270
      // map it to an info block
 
271
      TlsChunk result = shlo.tochunk ();
 
272
      unlock ();
 
273
      return result;
 
274
    } catch (...) {
 
275
      unlock ();
 
276
      throw;
 
277
    }
 
278
  }
 
279
 
 
280
  // -------------------------------------------------------------------------
 
281
  // - object section                                                        -
 
282
  // -------------------------------------------------------------------------
 
283
 
 
284
  // the quark zone
 
285
  static const long QUARK_ZONE_LENGTH = 2;
 
286
  static QuarkZone  zone (QUARK_ZONE_LENGTH);
 
287
 
 
288
  // the object supported quarks
 
289
  static const long QUARK_DECODE = zone.intern ("decode");
 
290
  static const long QUARK_GETMSG = zone.intern ("get-message");
 
291
 
 
292
  // create a new object in a generic way
 
293
 
 
294
  Object* TlsProto::mknew (Vector* argv) {
 
295
    // get the number of arguments
 
296
    long argc = (argv == nilp) ? 0 : argv->length ();
 
297
 
 
298
    // check for 0 argument
 
299
    if (argc == 0) return new TlsProto;
 
300
    // too many arguments
 
301
    throw Exception ("argument-error", 
 
302
                     "too many argument with tls decoder constructor");
 
303
  }
 
304
 
 
305
  // return true if the given quark is defined
 
306
 
 
307
  bool TlsProto::isquark (const long quark, const bool hflg) const {
 
308
    rdlock ();
 
309
    try {
 
310
      if (zone.exists (quark) == true) {
 
311
        unlock ();
 
312
        return true;
 
313
      }
 
314
      bool result = hflg ? Object::isquark (quark, hflg) : false;
 
315
      unlock ();
 
316
      return result;
 
317
    } catch (...) {
 
318
      unlock ();
 
319
      throw;
 
320
    }
 
321
  }
 
322
 
 
323
  // apply this object with a set of arguments and a quark
 
324
  
 
325
  Object* TlsProto::apply (Runnable* robj, Nameset* nset, const long quark,
 
326
                           Vector* argv) {
 
327
    // get the number of arguments
 
328
    long argc = (argv == nilp) ? 0 : argv->length ();
 
329
    
 
330
    // dispatch 1 argument
 
331
    if (argc == 1) {
 
332
      if (quark == QUARK_DECODE) {
 
333
        Object*     obj = argv->get (0);
 
334
        TlsHblock* hblk = dynamic_cast<TlsHblock*> (obj);
 
335
        if (hblk == nilp) {
 
336
          throw Exception ("type-error", "invalid object as handshake block",
 
337
                           Object::repr (obj));
 
338
        }
 
339
        return decode (hblk);
 
340
      }
 
341
      if (quark == QUARK_GETMSG) {
 
342
        Object*    obj = argv->get (0);
 
343
        TlsRecord* rcd = dynamic_cast<TlsRecord*> (obj);
 
344
        if (rcd == nilp) {
 
345
          throw Exception ("type-error", "invalid object as record",
 
346
                           Object::repr (obj));
 
347
        }
 
348
        return getmsg (rcd);
 
349
      }
 
350
    }
 
351
    // call the object method
 
352
    return Object::apply (robj, nset, quark, argv);
 
353
  }
 
354
}