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

« back to all changes in this revision

Viewing changes to src/srv/tls/shl/TlsConnect.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
// - TlsConnect.cpp                                                           -
 
3
// - afnix:tls service - tls connect 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 "TlsShake.hpp"
 
19
#include "Runnable.hpp"
 
20
#include "QuarkZone.hpp"
 
21
#include "Exception.hpp"
 
22
#include "TlsConnect.hpp"
 
23
 
 
24
namespace afnix {
 
25
 
 
26
  // -------------------------------------------------------------------------
 
27
  // - class section                                                         -
 
28
  // -------------------------------------------------------------------------
 
29
 
 
30
  // create a tls connect by flag
 
31
 
 
32
  TlsConnect::TlsConnect (const bool sflg) {
 
33
    d_sflg = sflg;
 
34
  }
 
35
 
 
36
  // return the class name
 
37
  
 
38
  String TlsConnect::repr (void) const {
 
39
    return "TlsConnect";
 
40
  }
 
41
 
 
42
  // reset the tls connection object
 
43
 
 
44
  void TlsConnect::reset (void) {
 
45
    wrlock ();
 
46
    try {
 
47
      d_info.reset ();
 
48
      unlock ();
 
49
    } catch (...) {
 
50
      unlock ();
 
51
      throw;
 
52
    }
 
53
  }
 
54
 
 
55
  // get the connection info list
 
56
 
 
57
  Plist TlsConnect::getinfo (void) const {
 
58
    rdlock ();
 
59
    try {
 
60
      Plist result = d_info;
 
61
      unlock ();
 
62
      return result;
 
63
    } catch (...) {
 
64
      unlock ();
 
65
      throw;
 
66
    }
 
67
  }
 
68
 
 
69
  // perform a handshake operation
 
70
 
 
71
  TlsState* TlsConnect::connect (InputStream* is, OutputStream* os) {
 
72
    // check for nil
 
73
    if ((is == nilp) || (os == nilp)) return nilp;
 
74
    // connect and lock
 
75
    wrlock ();
 
76
    try {
 
77
      // reset the object
 
78
      reset ();
 
79
      // connect the tls socket
 
80
      TlsState* result = d_sflg ? getss (is, os) : nilp;
 
81
      unlock ();
 
82
      return result;
 
83
    } catch (...) {
 
84
      unlock ();
 
85
      throw;
 
86
    }
 
87
  }
 
88
 
 
89
  // -------------------------------------------------------------------------
 
90
  // - protected section                                                     -
 
91
  // -------------------------------------------------------------------------
 
92
 
 
93
  // get a server state
 
94
 
 
95
  TlsState* TlsConnect::getss (InputStream* is, OutputStream* os) {
 
96
    // check for nil
 
97
    if ((is == nilp) || (os == nilp)) return nilp;
 
98
    // lock and connect
 
99
    wrlock ();
 
100
    TlsState*   ssta = nilp;
 
101
    TlsProto*   tlsp = nilp;
 
102
    TlsChello*  chlo = nilp;
 
103
    TlsMessage* shlo = nilp;
 
104
    try {
 
105
      // check for consistency
 
106
      if (d_sflg == false) {
 
107
        throw Exception ("tls-error", "invalid call to client hello get");
 
108
      }
 
109
      // create a server state by flag
 
110
      ssta = new TlsState (d_sflg, d_prms);
 
111
      Object::iref (ssta);
 
112
      // get the client hello message
 
113
      chlo = dynamic_cast <TlsChello*> (TlsProto().getchlo (is));
 
114
      if (chlo == nilp) {
 
115
        throw Exception ("tls-error", "cannot get client hello message");
 
116
      }
 
117
      // update the info list
 
118
      d_info += chlo->getinfo ();
 
119
      // update the state version
 
120
      ssta->setvers (chlo->getvmaj (), chlo->getvmin ());
 
121
      // update the selected cipher
 
122
      ssta->setcifr (chlo->getcifr ());
 
123
      // create a tls protocol by state
 
124
      tlsp = TlsProto::create (ssta);
 
125
      // create a server hello message and encode it
 
126
      shlo = tlsp->getshlo (ssta);
 
127
      if (shlo == nilp) {
 
128
        throw Exception ("tls-error", "cannot get server hello message");
 
129
      }
 
130
      tlsp->encode (os, shlo);
 
131
      // clean and unlock
 
132
      delete chlo;
 
133
      delete shlo;
 
134
      delete tlsp;
 
135
      Object::tref (ssta);
 
136
      unlock ();
 
137
      return ssta;
 
138
    } catch (...) {
 
139
      delete ssta;
 
140
      delete tlsp;
 
141
      delete chlo;
 
142
      delete shlo;
 
143
      unlock ();
 
144
      throw;
 
145
    }
 
146
  }
 
147
 
 
148
  // -------------------------------------------------------------------------
 
149
  // - object section                                                        -
 
150
  // -------------------------------------------------------------------------
 
151
 
 
152
  // the quark zone
 
153
  static const long QUARK_ZONE_LENGTH = 1;
 
154
  static QuarkZone  zone (QUARK_ZONE_LENGTH);
 
155
 
 
156
  // the object supported quarks
 
157
  static const long QUARK_CONNECT = zone.intern ("connect");
 
158
 
 
159
  // create a new object in a generic way
 
160
 
 
161
  Object* TlsConnect::mknew (Vector* argv) {
 
162
    // get the number of arguments
 
163
    long argc = (argv == nilp) ? 0 : argv->length ();
 
164
 
 
165
    // check for 1 argument
 
166
    if (argc == 1) {
 
167
      bool sflg = argv->getbool (0);
 
168
      return new TlsConnect (sflg);
 
169
    }
 
170
    // too many arguments
 
171
    throw Exception ("argument-error", 
 
172
                     "too many argument with tls connect constructor");
 
173
  }
 
174
  
 
175
  // return true if the given quark is defined
 
176
 
 
177
  bool TlsConnect::isquark (const long quark, const bool hflg) const {
 
178
    rdlock ();
 
179
    try {
 
180
      if (zone.exists (quark) == true) {
 
181
        unlock ();
 
182
        return true;
 
183
      }
 
184
      bool result = hflg ? TlsInfos::isquark (quark, hflg) : false;
 
185
      unlock ();
 
186
      return result;
 
187
    } catch (...) {
 
188
      unlock ();
 
189
      throw;
 
190
    }
 
191
  }
 
192
 
 
193
  // apply this object with a set of arguments and a quark
 
194
  
 
195
  Object* TlsConnect::apply (Runnable* robj, Nameset* nset, const long quark,
 
196
                            Vector* argv) {
 
197
    // get the number of arguments
 
198
    long argc = (argv == nilp) ? 0 : argv->length ();
 
199
    
 
200
    // dispatch 2 arguments
 
201
    if (argc == 2) {
 
202
      if (quark == QUARK_CONNECT) {
 
203
        Object* obj = argv->get (0);
 
204
        InputStream* is = dynamic_cast <InputStream*> (obj);
 
205
        if (is == nilp) {
 
206
          throw Exception ("type-error", "invalid object as input stream",
 
207
                           Object::repr (obj));
 
208
        }
 
209
        obj = argv->get (1);
 
210
        OutputStream* os = dynamic_cast <OutputStream*> (obj);
 
211
        if (os == nilp) {
 
212
          throw Exception ("type-error", "invalid object as output stream",
 
213
                           Object::repr (obj));
 
214
        }
 
215
        return connect (is, os);
 
216
      }
 
217
    }
 
218
    // call the info method
 
219
    return TlsInfos::apply (robj, nset, quark, argv);
 
220
  }
 
221
}