aboutsummaryrefslogtreecommitdiffstats
path: root/doc/tls.txt
blob: 247d6f7c99abdb6e3dcc4cdad69a5a78d4a33e4e (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526

Transport Layer Security (TLS)
========================================

.. versionadded:: 1.11.0

Botan supports both client and server implementations of the SSL/TLS
protocols, including SSL v3, TLS v1.0, TLS v1.1, and TLS v1.2 (the
insecure and obsolete SSL v2 protocol is not supported, beyond
processing SSL v2 client hellos which some clients still send for
backwards compatability with ancient servers).

The TLS implementation uses an event driven model. Information read
from the socket is passed to a TLS object using a function
``received_data``, which is processed and then (potentially) various
callback functions are called. For instance one callback is called
whenever a session is established or renegotiated, and another is
called whenevever a new plaintext record is available.

TLS Channels
----------------------------------------

TLS servers and clients share an interface called `TLS::Channel`. A
TLS channel (either client or server object) has these methods
available:

.. cpp:class:: TLS::Channel

   .. cpp:function size_t received_data(const byte buf[], size_t buf_size)

   This function is used to provide data sent by the counterparty (eg
   data that you read off the socket layer). Depending on the current
   protocol state and the amount of data provided this may result in one
   or more callback functions that were provided to the constructor being
   called.

   .. cpp:function void send(const byte buf[], size_t buf_size)

   If the connection has completed the initial handshake process, the
   data provided is sent to the counterparty as TLS
   traffic. Otherwise, an exception is thrown.

   .. cpp:function:: void close()

   A close notification is sent to the counterparty, and the internal
   state is cleared.

   .. cpp:function:: bool is_active()

   Returns true if and only if a handshake has been completed on this
   connection and the connection has not been subsequently closed.

   .. cpp:function:: bool is_closed()

   Returns true if and only if either a close notification or a fatal
   alert message have been either sent or received.

   .. cpp:function:: void renegotiate(bool force_full_renegotiation = false)

   Initiates a renegotiation. The counterparty is allowed by the
   protocol to ignore this request. If a successful renegotiation
   occurs, the *handshake_complete* callback will be called again.

   If *force_full_renegotiation* is false, then the client will
   attempt to simply renew the current session - this will refresh the
   symmetric keys but will not change the session master
   secret. Otherwise it will initiate a completely new session.

   For a server, if *force_full_renegotiation* is false, then a
   session resumption will be allowed if the client attempts
   it. Otherwise the server will prevent resumption and force the
   creation of a new session.

   .. cpp:function:: std::vector<X509_Certificate> peer_cert_chain()

   Returns the certificate chain of the server

.. _tls_client:

TLS Clients
----------------------------------------

.. cpp:class:: TLS::Client

   .. cpp:function:: TLS::Client( \
         std::function<void, const byte*, size_t> socket_output_fn, \
         std::function<void, const byte*, size_t, TLS::Alert> proc_fn, \
         std::function<bool, const TLS::Session&> handshake_complete, \
         TLS::Session_Manager& session_manager, \
         Credentials_Manager& credendials_manager, \
         const TLS::Policy& policy, \
         RandomNumberGenerator& rng, \
         const std::string& servername = "", \
         std::function<std::string, std::vector<std::string> > next_protocol)

   Initialize a new TLS client. The constructor will immediately
   initiate a new session.

   The *socket_output_fn* callback will be called with output that
   should be sent to the counterparty. For instance this will be
   called immediately from the constructor after the client hello
   message is constructed. An implementation of *socket_output_fn* is
   allowed to defer the write (for instance if writing when the
   callback occurs would block), but should eventually write the data
   to the counterparty *in order*.

   The *proc_fn* will be called with data sent by the counterparty
   after it has been processed. The byte array and size_t represent
   the plaintext; the :cpp:class:`TLS::Alert` value provides
   notification if the counterparty sent an alert via the TLS alert
   system. Possible values of alert data are included in the
   Alert_Type enum. Particularly relevant is the CLOSE_NOTIFY value.

   The *handshake_complete* function is called when a handshake
   (either initial or renegotiation) is completed. The return value of
   the callback specifies if the session should be cached for later
   resumption. If the function for some reason desires to prevent the
   connection from completing, it should throw an exception
   (preferably a TLS::Exception, which can provide more specific alert
   information to the counterparty). The :cpp:class:`TLS::Session`
   provides information about the session that was just established.

   The *session_manager* is an interface for storing TLS sessions,
   which allows for session resumption upon reconnecting to a server.
   In the absence of a need for persistent sessions, use
   :cpp:class:`TLS::Session_Manager_In_Memory` which caches
   connections for the lifetime of a single process. See
   :ref:`tls_session_managers` for more about session managers.

   The *credentials_manager* is an interface that will be called to
   retrieve any certificates, secret keys, pre-shared keys, or SRP
   intformation; see :doc:`credentials_manager` for more information.

   Use *servername* to specify the DNS name of the server you are
   attempting to connect to, if you know it. This helps the server
   select what certificate to use and helps the client validate the
   connection.

   The optional *next_protocol* callback is called if the server
   indicates it supports the next protocol notification extension.
   The callback wlil be called with a list of protocol names that the
   server advertises, and the client can select from them or return an
   unadvertised protocol.

A simple TLS client example:

.. literalinclude:: examples/tls_client.cpp

TLS Servers
----------------------------------------

.. cpp:class:: TLS::Server

   .. cpp:function:: TLS::Server( \
          std::function<void, const byte*, size_t> socket_output_fn, \
          std::function<void, const byte*, size_t, TLS::Alert> proc_fn, \
          std::function<bool, const TLS::Session&> handshake_complete, \
          TLS::Session_Manager& session_manager, \
          Credentials_Manager& creds, \
          const TLS::Policy& policy, \
          RandomNumberGenerator& rng, \
          const std::vector<std::string>& protocols)

The first 7 arguments are treated similiarly to the :ref:`client
<tls_client>`.  The final (optional) argument, protocols, specifies
the protocols the server is willing to advertise it supports.

A TLS server that can handle concurrent connections using asio:

.. literalinclude:: examples/asio_tls_server.cpp

.. _tls_sessions:

TLS Sessions
----------------------------------------

TLS allows clients and servers to support *session resumption*, where
the end point retains some information about an established session
and then reuse that information to bootstrap a new session in way that
is much cheaper computationally than a full handshake.

Every time your handshake callback is called, a new session has been
established, and a ``TLS::Session`` is included that provides
information about that session:

.. cpp:class:: TLS::Session

   .. cpp:function:: Protocol_Version version() const

       Returns the protocol version that was negotiated

   .. cpp:function:: Ciphersuite ciphersite() const

       Returns the :ref:`ciphersuite <tls_ciphersuite>` that was negotiated.

   .. cpp:function:: std::string sni_hostname() const

       Returns the hostname the client indicated in the hello message.

   .. cpp:function:: std::vector<X509_Certificate> peer_certs() const

       Returns the certificate chain of the peer

   .. cpp:function:: std::string srp_identifier() const

       If an SRP ciphersuite was used, then this is the identifier
       that was used for authentication.

   .. cpp:function:: bool secure_renegotiation() const

      Returns ``true`` if the connection was negotiated with the
      correct extensions to prevent the renegotiation attack.

There are also functions for serialization and deserializing sessions:

.. cpp:class:: TLS::Session

   .. cpp:function:: std::vector<byte> encrypt(const SymmetricKey& key, \
                                               RandomNumberGenerator& rng)

      Encrypts a session using a symmetric key *key* and returns a raw
      binary value that can later be passed to ``decrypt``.

   .. cpp:function:: static Session decrypt(const byte ciphertext[], \
                                            size_t length, \
                                            const SymmetricKey& key)

      Decrypts a session that was encrypted previously with
      ``encrypt`` and *key*, or throws an exception if decryption
      fails.

   .. cpp:function:: secure_vector<byte> DER_encode() const

       Returns a serialized version of the session.

       .. warning:: The return value contains the master secret for
                    the session, and an attacker who recovers it could
                    recover plaintext of previous sessions or
                    impersonate one side to the other.

.. _tls_session_managers:

TLS Session Managers
----------------------------------------

You may want sessions stored in a specific format or storage type. To
do so, implement the ``TLS::Session_Manager`` interface and pass your
implementation to the ``TLS::Client`` or ``TLS::Server`` constructor.

.. cpp:class:: TLS::Session_Mananger

 .. cpp:function:: void save(const Session& session)

     Save a new *session*. It is possible that this sessions session
     ID will replicate a session ID already stored, in which case the
     new session information should overwrite the previous information.

 .. cpp:function:: void remove_entry(const std::vector<byte>& session_id)

      Remove the session identified by *session_id*. Future attempts
      at resumption should fail for this session.

 .. cpp:function:: bool load_from_session_id(const std::vector<byte>& session_id, \
                                             Session& session)

      Attempt to resume a session identified by *session_id*. If
      located, *session* is set to the session data previously passed
      to *save*, and ``true`` is returned. Otherwise *session* is not
      modified and ``false`` is returned.

 .. cpp:function:: bool load_from_host_info(const std::string& hostname, u16bit port, \
                                            Session& session)

      Attempt to resume a session for *hostname* / *port*. If *port*
      is zero, try to find a session for *hostname* on any port. With
      the current client implementation, *port* is always zero.


 .. cpp:function:: std::chrono::seconds session_lifetime() const

      Returns the expected maximum lifetime of a session when using
      this session manager. Will return 0 if the lifetime is unknown
      or has no explicit expiration policy.

.. _tls_session_manager_inmem:

In Memory Session Manager
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The ``TLS::Session_Manager_In_Memory`` implementation saves sessions
in memory, with an upper bound on the maximum number of sessions and
the lifetime of a session.

.. cpp:class:: TLS::Session_Manager_In_Memory

 .. cpp:function:: Session_Manager_In_Memory(size_t max_sessions = 1000, \
                                             std::chrono::seconds session_lifetime = 7200)

    Limits the maximum number of saved sessions to *max_sessions*, and
    expires all sessions older than *session_lifetime*.

Noop Session Mananger
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The ``TLS::Session_Manager_Noop`` implementation does not save
sessions at all, and thus session resumption always fails. Its
constructor has no arguments.

SQLite3 Session Manager
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This session manager is only available if requiested at build time. If
the macro ``BOTAN_HAS_TLS_SQLITE3_SESSION_MANAGER`` is defined, then
``botan/tls_session_manager_sqlite.h`` contains
``TLS::Session_Manager_SQLite`` which stores sessions persistently to
a sqlite3 database. The session data is encrypted using a passphrase,
and stored in two tables, named ``tls_sessions`` (which holds the
actual session information) and ``tls_sessions_metadata`` (which holds
the PBKDF information).

.. warning:: The hostnames associated with the saved sessions are
             stored in the database in plaintext. This may be a
             serious privacy risk in some applications.

.. cpp:class:: TLS::Session_Manager_SQLite

 .. cpp:function:: Session_Manager_SQLite( \
       const std::string& passphrase, \
       RandomNumberGenerator& rng, \
       const std::string& db_filename, \
       size_t max_sessions = 1000, \
       std::chrono::seconds session_lifetime = 7200)

   Uses the sqlite3 database named by *db_filename*.

TLS Policies
----------------------------------------

``TLS::Policy`` is how an application can control details of what will
be negotiated during a handshake.

.. cpp:class:: TLS::Policy

 .. cpp:function:: Protocol_Version min_version() const

     Returns the minimum protocol version we are willing to negotiate.

     Default: SSL v3

 .. cpp:function:: Protocol_Version pref_version() const

     Return the protocol version we would prefer to negotiate. This is
     the version that clients will offer to servers.

     Default: TLS v1.2

 .. cpp:function:: std::vector<std::string> allowed_ciphers() const

     Returns the list of ciphers we are willing to negotiate, in order
     of preference.

     Default: returns "AES-256", "AES-128", "3DES", "ARC4".

     Also allowed: "Camellia-256", "Camellia-128", "SEED"

 .. cpp:function:: std::vector<std::string> allowed_hashes() const

     Returns the list of hash functions we are willing to use, in
     order of preference.

     Default: "SHA-512", "SHA-384", "SHA-256", "SHA-224", "SHA-1"

     Also allowed: "MD5"

 .. cpp:function:: std::vector<std::string> allowed_key_exchange_methods() const

     Returns the list of key exchange methods we are willing to use,
     in order of preference.

     Default: "ECDH", "DH", "RSA"

     Also allowed: "SRP_SHA", "ECDHE_PSK", "DHE_PSK", "PSK"

 .. cpp:function:: std::vector<std::string> allowed_signature_methods() const

     Default: "ECDSA", "RSA", "DSA"

     Also allowed: "" (meaning anonymous)

 .. cpp:function:: std::vector<std::string> allowed_ecc_curves() const

 .. cpp:function:: std::vector<byte> compression() const

 .. cpp:function:: bool allow_insecure_renegotiation() const

     If this function returns true, we will allow renegotiation attempts
     even if the counterparty does not support the RFC 5746 extensions.

     .. warning:: Returning true here could expose you to attacks

     Default: false

 .. cpp:function:: DL_Group dh_group() const

     For ephemeral Diffie-Hellman key exchange, the server sends a
     group parameter. Return the group parameter a server should
     use.

     Default: 2048 bit IETF IPsec group ("modp/ietf/2048")

 .. cpp:function:: size_t minimum_dh_group_size() const

     Return the minimum size in bits for a Diffie-Hellman group that a
     client will accept.

     Default: 1024 bits

 .. cpp:function:: bool hide_unknown_users() const

     The SRP and PSK suites work using an identifier along with a
     shared secret. If this function returns true, when an identifier
     that the server does not recognize is provided by a client, a
     random shared secret will be generated in such a way that a
     client should not be able to tell the difference between the
     identifier not being known and the secret being wrong.  This can
     help protect against some username probing attacks.  If it
     returns false, the server will instead send an
     ``unknown_psk_identity`` alert when an unknown identifier is
     used.

     Default: false

 .. cpp:function:: u32bit session_ticket_lifetime() const

     Return the lifetime of session tickets. Each session includes the
     start time. Sessions resumptions using tickets older than
     ``session_ticket_lifetime`` seconds will fail, forcing a full
     renegotiation.

     Default: 86400 seconds (1 day)


.. _tls_ciphersuite:

TLS Ciphersuites
----------------------------------------

.. cpp:class:: TLS::Ciphersuite

 .. cpp:function:: u16bit ciphersuite_code() const

     Return the numerical code for this ciphersuite

 .. cpp:function:: std::string to_string() const

     Return name of ciphersuite

 .. cpp:function:: std::string kex_algo() const

     Return the key exchange algorithm of this ciphersuite

 .. cpp:function:: std::string sig_algo() const

     Return the signature algorithm of this ciphersuite

 .. cpp:function:: std::string cipher_algo() const

     Return the cipher algorithm of this ciphersuite

 .. cpp:function:: std::string mac_algo() const

     Return the authentication algorithm of this ciphersuite

TLS Alerts
----------------------------------------

A ``TLS::Alert`` is passed to every invocation of a channel's
*proc_fn*. Most of the time, no alert was sent and so the alert will
be of type ``NULL_ALERT``.

.. cpp:class:: TLS::Alert

  .. cpp:function:: is_valid() const

       Return true if this alert is not a null alert

  .. cpp:function:: is_fatal() const

       Return if this alert is fatal or a warning alert

  .. cpp:function:: Type type() const

       Returns the type of the alert as an enum

  .. cpp:function:: std::string type_string()

       Returns the type of the alert as a string

TLS Protocol Version
----------------------------------------

TLS has several different versions with slightly different behaviors.
The ``TLS::Protocol_Version`` class represents a specific version:

.. cpp:class:: TLS::Protocol_Version

 .. cpp:type:: enum Version_Code

     ``SSL_V3``, ``TLS_V10``, ``TLS_V11``, ``TLS_V12``

 .. cpp:function:: Protocol_Version(Version_Code named_version)

      Create a specific version

 .. cpp:function:: byte major_version() const

      Returns major number of the protocol version

 .. cpp:function:: byte minor_version() const

      Returns minor number of the protocol version

 .. cpp:function:: std::string to_string() const

      Returns string description of the version, typically "SSL v3" or
      "TLS v1.n"