aboutsummaryrefslogtreecommitdiffstats
path: root/src/tls/tls_handshake_state.cpp
blob: f2f6a2bafd7c3b4b34057e448e8fbfbaae0aaabf (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
/*
* TLS Handshaking
* (C) 2004-2006,2011 Jack Lloyd
*
* Released under the terms of the Botan license
*/

#include <botan/internal/tls_handshake_state.h>
#include <botan/internal/tls_messages.h>

namespace Botan {

namespace {

u32bit bitmask_for_handshake_type(Handshake_Type type)
   {
   switch(type)
      {
      case HELLO_REQUEST:
         return (1 << 0);

      /*
      * Same code point for both client hello styles
      */
      case CLIENT_HELLO:
      case CLIENT_HELLO_SSLV2:
         return (1 << 1);

      case SERVER_HELLO:
         return (1 << 2);

      case CERTIFICATE:
         return (1 << 3);

      case SERVER_KEX:
         return (1 << 4);

      case CERTIFICATE_REQUEST:
         return (1 << 5);

      case SERVER_HELLO_DONE:
         return (1 << 6);

      case CERTIFICATE_VERIFY:
         return (1 << 7);

      case CLIENT_KEX:
         return (1 << 8);

      case NEXT_PROTOCOL:
         return (1 << 9);

      case HANDSHAKE_CCS:
         return (1 << 10);

      case FINISHED:
         return (1 << 11);

      // allow explicitly disabling new handshakes
      case HANDSHAKE_NONE:
         return 0;

      default:
         throw Internal_Error("Unknown handshake type " + to_string(type));
      }

   return 0;
   }

}

/*
* Initialize the SSL/TLS Handshake State
*/
TLS_Handshake_State::TLS_Handshake_State()
   {
   client_hello = 0;
   server_hello = 0;
   server_certs = 0;
   server_kex = 0;
   cert_req = 0;
   server_hello_done = 0;
   next_protocol = 0;

   client_certs = 0;
   client_kex = 0;
   client_verify = 0;
   client_finished = 0;
   server_finished = 0;

   kex_pub = 0;
   kex_priv = 0;

   version = SSL_V3;

   hand_expecting_mask = 0;
   hand_received_mask = 0;
   }

void TLS_Handshake_State::confirm_transition_to(Handshake_Type handshake_msg)
   {
   const u32bit mask = bitmask_for_handshake_type(handshake_msg);

   hand_received_mask |= mask;

   const bool ok = (hand_expecting_mask & mask); // overlap?

   if(!ok)
      throw Unexpected_Message("Unexpected state transition in handshake, got " +
                               to_string(handshake_msg) + " mask is " +
                               to_string(hand_expecting_mask));

   /* We don't know what to expect next, so force a call to
      set_expected_next; if it doesn't happen, the next transition
      check will always fail which is what we want.
   */
   hand_expecting_mask = 0;
   }

void TLS_Handshake_State::set_expected_next(Handshake_Type handshake_msg)
   {
   hand_expecting_mask |= bitmask_for_handshake_type(handshake_msg);
   }

bool TLS_Handshake_State::received_handshake_msg(Handshake_Type handshake_msg) const
   {
   const u32bit mask = bitmask_for_handshake_type(handshake_msg);

   return (hand_received_mask & mask);
   }

std::pair<std::string, Signature_Format>
TLS_Handshake_State::choose_sig_format(const Public_Key* key, bool for_client_auth)
   {
   const std::string algo_name = key->algo_name();

   if(algo_name == "RSA")
      {
      std::string padding = "";

      if(for_client_auth && this->version == SSL_V3)
         padding = "EMSA3(Raw)";
      else
         padding = "EMSA3(TLS.Digest.0)";

      return std::make_pair(padding, IEEE_1363);
      }
   else if(algo_name == "DSA")
      {
      std::string padding = "";

      if(for_client_auth && this->version == SSL_V3)
         padding = "Raw";
      else
         padding = "EMSA1(SHA-1)";

      return std::make_pair(padding, DER_SEQUENCE);
      }

   throw Invalid_Argument(algo_name + " is invalid/unknown for TLS signatures");
   }

/*
* Destroy the SSL/TLS Handshake State
*/
TLS_Handshake_State::~TLS_Handshake_State()
   {
   delete client_hello;
   delete server_hello;
   delete server_certs;
   delete server_kex;
   delete cert_req;
   delete server_hello_done;
   delete next_protocol;

   delete client_certs;
   delete client_kex;
   delete client_verify;
   delete client_finished;
   delete server_finished;

   delete kex_pub;
   delete kex_priv;
   }

}