e2cf11e114dff1bc868f2502e9a204e36c0a598a
[openssl.git] / ssl / statem.c
1 /* ssl/statem.c */
2 /*
3  * Written by Matt Caswell for the OpenSSL project.
4  */
5 /* ====================================================================
6  * Copyright (c) 1998-2015 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    openssl-core@openssl.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 #include <openssl/rand.h>
60 #include "ssl_locl.h"
61
62 /*
63  * This file implements the SSL/TLS/DTLS state machines.
64  *
65  * There are two primary state machines:
66  *
67  * 1) Message flow state machine
68  * 2) Handshake state machine
69  *
70  * The Message flow state machine controls the reading and sending of messages
71  * including handling of non-blocking IO events, flushing of the underlying
72  * write BIO, handling unexpected messages, etc. It is itself broken into two
73  * separate sub-state machines which control reading and writing respectively.
74  *
75  * The Handshake state machine keeps track of the current SSL/TLS handshake
76  * state. Transitions of the handshake state are the result of events that
77  * occur within the Message flow state machine.
78  *
79  * Overall it looks like this:
80  *
81  * ---------------------------------------------            -------------------
82  * |                                           |            |                 |
83  * | Message flow state machine                |            |                 |
84  * |                                           |            |                 |
85  * | -------------------- -------------------- | Transition | Handshake state |
86  * | | MSG_FLOW_READING    | | MSG_FLOW_WRITING    | | Event      | machine         |
87  * | | sub-state        | | sub-state        | |----------->|                 |
88  * | | machine for      | | machine for      | |            |                 |
89  * | | reading messages | | writing messages | |            |                 |
90  * | -------------------- -------------------- |            |                 |
91  * |                                           |            |                 |
92  * ---------------------------------------------            -------------------
93  *
94  */
95
96 /* Sub state machine return values */
97 enum SUB_STATE_RETURN {
98     /* Something bad happened or NBIO */
99     SUB_STATE_ERROR,
100     /* Sub state finished go to the next sub state */
101     SUB_STATE_FINISHED,
102     /* Sub state finished and handshake was completed */
103     SUB_STATE_END_HANDSHAKE
104 };
105
106 static int state_machine(SSL *s, int server);
107 static void init_read_state_machine(SSL *s);
108 static enum SUB_STATE_RETURN read_state_machine(SSL *s);
109 static void init_write_state_machine(SSL *s);
110 static enum SUB_STATE_RETURN write_state_machine(SSL *s);
111 static inline int cert_req_allowed(SSL *s);
112 static inline int key_exchange_skip_allowed(SSL *s);
113 static int client_read_transition(SSL *s, int mt);
114 static enum WRITE_TRAN client_write_transition(SSL *s);
115 static enum WORK_STATE client_pre_work(SSL *s, enum WORK_STATE wst);
116 static enum WORK_STATE client_post_work(SSL *s, enum WORK_STATE wst);
117 static int client_construct_message(SSL *s);
118 static unsigned long client_max_message_size(SSL *s);
119 static enum MSG_PROCESS_RETURN client_process_message(SSL *s,
120                                                       unsigned long len);
121 static enum WORK_STATE client_post_process_message(SSL *s, enum WORK_STATE wst);
122 static int server_read_transition(SSL *s, int mt);
123 static inline int send_server_key_exchange(SSL *s);
124 static inline int send_certificate_request(SSL *s);
125 static enum WRITE_TRAN server_write_transition(SSL *s);
126 static enum WORK_STATE server_pre_work(SSL *s, enum WORK_STATE wst);
127 static enum WORK_STATE server_post_work(SSL *s, enum WORK_STATE wst);
128 static int server_construct_message(SSL *s);
129 static unsigned long server_max_message_size(SSL *s);
130 static enum MSG_PROCESS_RETURN server_process_message(SSL *s, unsigned long len);
131 static enum WORK_STATE server_post_process_message(SSL *s, enum WORK_STATE wst);
132
133 /*
134  * Clear the state machine state and reset back to MSG_FLOW_UNINITED
135  */
136 void statem_clear(SSL *s)
137 {
138     s->statem.state = MSG_FLOW_UNINITED;
139 }
140
141 /*
142  * Set the state machine up ready for a renegotiation handshake
143  */
144 void statem_set_renegotiate(SSL *s)
145 {
146     s->statem.state = MSG_FLOW_RENEGOTIATE;
147 }
148
149 /*
150  * Put the state machine into an error state. This is a permanent error for
151  * the current connection.
152  */
153 void statem_set_error(SSL *s)
154 {
155     s->statem.state = MSG_FLOW_ERROR;
156     /* TODO: This is temporary - remove me */
157     s->state = SSL_ST_ERR;
158 }
159
160 int ssl3_connect(SSL *s) {
161     return state_machine(s, 0);
162 }
163
164 int dtls1_connect(SSL *s)
165 {
166     return state_machine(s, 0);
167 }
168
169 int ssl3_accept(SSL *s)
170 {
171     return state_machine(s, 1);
172 }
173
174 /*
175  * The main message flow state machine. We start in the MSG_FLOW_UNINITED or
176  * MSG_FLOW_RENEGOTIATE state and finish in MSG_FLOW_FINISHED. Valid states and
177  * transitions are as follows:
178  *
179  * MSG_FLOW_UNINITED     MSG_FLOW_RENEGOTIATE
180  *        |                       |
181  *        +-----------------------+
182  *        v
183  * MSG_FLOW_WRITING <---> MSG_FLOW_READING
184  *        |
185  *        V
186  * MSG_FLOW_FINISHED
187  *        |
188  *        V
189  *    [SUCCESS]
190  *
191  * We may exit at any point due to an error or NBIO event. If an NBIO event
192  * occurs then we restart at the point we left off when we are recalled.
193  * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them.
194  *
195  * In addition to the above there is also the MSG_FLOW_ERROR state. We can move
196  * into that state at any point in the event that an irrecoverable error occurs.
197  *
198  * Valid return values are:
199  *   1: Success
200  * <=0: NBIO or error
201  */
202 static int state_machine(SSL *s, int server) {
203     BUF_MEM *buf = NULL;
204     unsigned long Time = (unsigned long)time(NULL);
205     void (*cb) (const SSL *ssl, int type, int val) = NULL;
206     STATEM *st = &s->statem;
207     int ret = -1;
208     int ssret;
209
210     if (st->state == MSG_FLOW_ERROR) {
211         /* Shouldn't have been called if we're already in the error state */
212         return -1;
213     }
214
215     RAND_add(&Time, sizeof(Time), 0);
216     ERR_clear_error();
217     clear_sys_error();
218
219     if (s->info_callback != NULL)
220         cb = s->info_callback;
221     else if (s->ctx->info_callback != NULL)
222         cb = s->ctx->info_callback;
223
224     s->in_handshake++;
225     if (!SSL_in_init(s) || SSL_in_before(s)) {
226         if (!SSL_clear(s))
227             return -1;
228     }
229
230 #ifndef OPENSSL_NO_SCTP
231     if (SSL_IS_DTLS(s)) {
232         /*
233          * Notify SCTP BIO socket to enter handshake mode and prevent stream
234          * identifier other than 0. Will be ignored if no SCTP is used.
235          */
236         BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
237                  s->in_handshake, NULL);
238     }
239 #endif
240
241 #ifndef OPENSSL_NO_HEARTBEATS
242     /*
243      * If we're awaiting a HeartbeatResponse, pretend we already got and
244      * don't await it anymore, because Heartbeats don't make sense during
245      * handshakes anyway.
246      */
247     if (s->tlsext_hb_pending) {
248         if (SSL_IS_DTLS(s))
249             dtls1_stop_timer(s);
250         s->tlsext_hb_pending = 0;
251         s->tlsext_hb_seq++;
252     }
253 #endif
254
255     /* Initialise state machine */
256
257     if (st->state == MSG_FLOW_RENEGOTIATE) {
258         s->renegotiate = 1;
259         if (!server)
260             s->ctx->stats.sess_connect_renegotiate++;
261     }
262
263     if (st->state == MSG_FLOW_UNINITED || st->state == MSG_FLOW_RENEGOTIATE) {
264         /* TODO: Temporary - fix this */
265         if (server)
266             s->state = SSL_ST_ACCEPT;
267         else
268             s->state = SSL_ST_CONNECT;
269
270         if (st->state == MSG_FLOW_UNINITED) {
271             st->hand_state = TLS_ST_BEFORE;
272         }
273
274         s->server = server;
275         if (cb != NULL)
276             cb(s, SSL_CB_HANDSHAKE_START, 1);
277
278         if (SSL_IS_DTLS(s)) {
279             if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) &&
280                     (server
281                     || (s->version & 0xff00) != (DTLS1_BAD_VER & 0xff00))) {
282                 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
283                 goto end;
284             }
285         } else {
286             if ((s->version >> 8) != SSL3_VERSION_MAJOR
287                     && s->version != TLS_ANY_VERSION) {
288                 SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
289                 goto end;
290             }
291         }
292
293         if (!SSL_IS_DTLS(s)) {
294             if (s->version != TLS_ANY_VERSION &&
295                     !ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
296                 SSLerr(SSL_F_STATE_MACHINE, SSL_R_VERSION_TOO_LOW);
297                 goto end;
298             }
299         }
300
301         if (server)
302             s->type = SSL_ST_ACCEPT;
303         else
304             s->type = SSL_ST_CONNECT;
305
306         if (s->init_buf == NULL) {
307             if ((buf = BUF_MEM_new()) == NULL) {
308                 goto end;
309             }
310             if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
311                 goto end;
312             }
313             s->init_buf = buf;
314             buf = NULL;
315         }
316
317         if (!ssl3_setup_buffers(s)) {
318             goto end;
319         }
320         s->init_num = 0;
321
322         /*
323          * Should have been reset by tls_process_finished, too.
324          */
325         s->s3->change_cipher_spec = 0;
326
327         if (!server || st->state != MSG_FLOW_RENEGOTIATE) {
328                 /*
329                  * Ok, we now need to push on a buffering BIO ...but not with
330                  * SCTP
331                  */
332 #ifndef OPENSSL_NO_SCTP
333                 if (!SSL_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(s)))
334 #endif
335                     if (!ssl_init_wbio_buffer(s, server ? 1 : 0)) {
336                         goto end;
337                     }
338
339             ssl3_init_finished_mac(s);
340         }
341
342         if (server) {
343             if (st->state != MSG_FLOW_RENEGOTIATE) {
344                 s->ctx->stats.sess_accept++;
345             } else if (!s->s3->send_connection_binding &&
346                        !(s->options &
347                          SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
348                 /*
349                  * Server attempting to renegotiate with client that doesn't
350                  * support secure renegotiation.
351                  */
352                 SSLerr(SSL_F_STATE_MACHINE,
353                        SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
354                 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
355                 statem_set_error(s);
356                 goto end;
357             } else {
358                 /*
359                  * s->state == SSL_ST_RENEGOTIATE, we will just send a
360                  * HelloRequest
361                  */
362                 s->ctx->stats.sess_accept_renegotiate++;
363             }
364         } else {
365             s->ctx->stats.sess_connect++;
366
367             /* mark client_random uninitialized */
368             memset(s->s3->client_random, 0, sizeof(s->s3->client_random));
369             s->hit = 0;
370
371             s->s3->tmp.cert_request = 0;
372
373             if (SSL_IS_DTLS(s)) {
374                 st->use_timer = 1;
375             }
376         }
377
378         st->state = MSG_FLOW_WRITING;
379         init_write_state_machine(s);
380         st->read_state_first_init = 1;
381     }
382
383     while(st->state != MSG_FLOW_FINISHED) {
384         if(st->state == MSG_FLOW_READING) {
385             ssret = read_state_machine(s);
386             if (ssret == SUB_STATE_FINISHED) {
387                 st->state = MSG_FLOW_WRITING;
388                 init_write_state_machine(s);
389             } else {
390                 /* NBIO or error */
391                 goto end;
392             }
393         } else if (st->state == MSG_FLOW_WRITING) {
394             ssret = write_state_machine(s);
395             if (ssret == SUB_STATE_FINISHED) {
396                 st->state = MSG_FLOW_READING;
397                 init_read_state_machine(s);
398             } else if (ssret == SUB_STATE_END_HANDSHAKE) {
399                 st->state = MSG_FLOW_FINISHED;
400             } else {
401                 /* NBIO or error */
402                 goto end;
403             }
404         } else {
405             /* Error */
406             statem_set_error(s);
407             goto end;
408         }
409     }
410
411     st->state = MSG_FLOW_UNINITED;
412     ret = 1;
413
414  end:
415     s->in_handshake--;
416
417 #ifndef OPENSSL_NO_SCTP
418     if (SSL_IS_DTLS(s)) {
419         /*
420          * Notify SCTP BIO socket to leave handshake mode and allow stream
421          * identifier other than 0. Will be ignored if no SCTP is used.
422          */
423         BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
424                  s->in_handshake, NULL);
425     }
426 #endif
427
428     BUF_MEM_free(buf);
429     if (cb != NULL) {
430         if (server)
431             cb(s, SSL_CB_ACCEPT_EXIT, ret);
432         else
433             cb(s, SSL_CB_CONNECT_EXIT, ret);
434     }
435     return ret;
436 }
437
438 /*
439  * Initialise the MSG_FLOW_READING sub-state machine
440  */
441 static void init_read_state_machine(SSL *s)
442 {
443     STATEM *st = &s->statem;
444
445     st->read_state = READ_STATE_HEADER;
446 }
447
448 /*
449  * This function implements the sub-state machine when the message flow is in
450  * MSG_FLOW_READING. The valid sub-states and transitions are:
451  *
452  * READ_STATE_HEADER <--+<-------------+
453  *        |             |              |
454  *        v             |              |
455  * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS
456  *        |                            |
457  *        +----------------------------+
458  *        v
459  * [SUB_STATE_FINISHED]
460  *
461  * READ_STATE_HEADER has the responsibility for reading in the message header
462  * and transitioning the state of the handshake state machine.
463  *
464  * READ_STATE_BODY reads in the rest of the message and then subsequently
465  * processes it.
466  *
467  * READ_STATE_POST_PROCESS is an optional step that may occur if some post
468  * processing activity performed on the message may block.
469  *
470  * Any of the above states could result in an NBIO event occuring in which case
471  * control returns to the calling application. When this function is recalled we
472  * will resume in the same state where we left off.
473  */
474 static enum SUB_STATE_RETURN read_state_machine(SSL *s) {
475     STATEM *st = &s->statem;
476     int ret, mt;
477     unsigned long len;
478     int (*transition)(SSL *s, int mt);
479     enum MSG_PROCESS_RETURN (*process_message)(SSL *s, unsigned long n);
480     enum WORK_STATE (*post_process_message)(SSL *s, enum WORK_STATE wst);
481     unsigned long (*max_message_size)(SSL *s);
482     void (*cb) (const SSL *ssl, int type, int val) = NULL;
483
484     if (s->info_callback != NULL)
485         cb = s->info_callback;
486     else if (s->ctx->info_callback != NULL)
487         cb = s->ctx->info_callback;
488
489     if(s->server) {
490         transition = server_read_transition;
491         process_message = server_process_message;
492         max_message_size = server_max_message_size;
493         post_process_message = server_post_process_message;
494     } else {
495         transition = client_read_transition;
496         process_message = client_process_message;
497         max_message_size = client_max_message_size;
498         post_process_message = client_post_process_message;
499     }
500
501     if (st->read_state_first_init) {
502         s->first_packet = 1;
503         st->read_state_first_init = 0;
504     }
505
506     while(1) {
507         switch(st->read_state) {
508         case READ_STATE_HEADER:
509             s->init_num = 0;
510             /* Get the state the peer wants to move to */
511             if (SSL_IS_DTLS(s)) {
512                 /*
513                  * In DTLS we get the whole message in one go - header and body
514                  */
515                 ret = dtls_get_message(s, &mt, &len);
516             } else {
517                 ret = tls_get_message_header(s, &mt);
518             }
519
520             if (ret == 0) {
521                 /* Could be non-blocking IO */
522                 return SUB_STATE_ERROR;
523             }
524
525             if (cb != NULL) {
526                 /* Notify callback of an impending state change */
527                 if (s->server)
528                     cb(s, SSL_CB_ACCEPT_LOOP, 1);
529                 else
530                     cb(s, SSL_CB_CONNECT_LOOP, 1);
531             }
532             /*
533              * Validate that we are allowed to move to the new state and move
534              * to that state if so
535              */
536             if(!transition(s, mt)) {
537                 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
538                 SSLerr(SSL_F_READ_STATE_MACHINE, SSL_R_UNEXPECTED_MESSAGE);
539                 return SUB_STATE_ERROR;
540             }
541
542             if (s->s3->tmp.message_size > max_message_size(s)) {
543                 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
544                 SSLerr(SSL_F_READ_STATE_MACHINE, SSL_R_EXCESSIVE_MESSAGE_SIZE);
545                 return SUB_STATE_ERROR;
546             }
547
548             st->read_state = READ_STATE_BODY;
549             /* Fall through */
550
551         case READ_STATE_BODY:
552             if (!SSL_IS_DTLS(s)) {
553                 /* We already got this above for DTLS */
554                 ret = tls_get_message_body(s, &len);
555                 if (ret == 0) {
556                     /* Could be non-blocking IO */
557                     return SUB_STATE_ERROR;
558                 }
559             }
560
561             s->first_packet = 0;
562             ret = process_message(s, len);
563             if (ret == MSG_PROCESS_ERROR) {
564                 return SUB_STATE_ERROR;
565             }
566
567             if (ret == MSG_PROCESS_FINISHED_READING) {
568                 if (SSL_IS_DTLS(s)) {
569                     dtls1_stop_timer(s);
570                 }
571                 return SUB_STATE_FINISHED;
572             }
573
574             if (ret == MSG_PROCESS_CONTINUE_PROCESSING) {
575                 st->read_state = READ_STATE_POST_PROCESS;
576                 st->read_state_work = WORK_MORE_A;
577             } else {
578                 st->read_state = READ_STATE_HEADER;
579             }
580             break;
581
582         case READ_STATE_POST_PROCESS:
583             st->read_state_work = post_process_message(s, st->read_state_work);
584             switch(st->read_state_work) {
585             default:
586                 return SUB_STATE_ERROR;
587
588             case WORK_FINISHED_CONTINUE:
589                 st->read_state = READ_STATE_HEADER;
590                 break;
591
592             case WORK_FINISHED_STOP:
593                 if (SSL_IS_DTLS(s)) {
594                     dtls1_stop_timer(s);
595                 }
596                 return SUB_STATE_FINISHED;
597             }
598             break;
599
600         default:
601             /* Shouldn't happen */
602             ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
603             SSLerr(SSL_F_READ_STATE_MACHINE, ERR_R_INTERNAL_ERROR);
604             statem_set_error(s);
605             return SUB_STATE_ERROR;
606         }
607     }
608 }
609
610 /*
611  * Send a previously constructed message to the peer.
612  */
613 static int statem_do_write(SSL *s)
614 {
615     STATEM *st = &s->statem;
616
617     if (st->hand_state == TLS_ST_CW_CHANGE
618             || st->hand_state == TLS_ST_SW_CHANGE) {
619         if (SSL_IS_DTLS(s))
620             return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
621         else
622             return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
623     } else {
624         return ssl_do_write(s);
625     }
626 }
627
628 /*
629  * Initialise the MSG_FLOW_WRITING sub-state machine
630  */
631 static void init_write_state_machine(SSL *s)
632 {
633     STATEM *st = &s->statem;
634
635     st->write_state = WRITE_STATE_TRANSITION;
636 }
637
638 /*
639  * This function implements the sub-state machine when the message flow is in
640  * MSG_FLOW_WRITING. The valid sub-states and transitions are:
641  *
642  * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED]
643  * |             |
644  * |             v
645  * |      WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE]
646  * |             |
647  * |             v
648  * |       WRITE_STATE_SEND
649  * |             |
650  * |             v
651  * |     WRITE_STATE_POST_WORK
652  * |             |
653  * +-------------+
654  *
655  * WRITE_STATE_TRANSITION transitions the state of the handshake state machine
656
657  * WRITE_STATE_PRE_WORK performs any work necessary to prepare the later
658  * sending of the message. This could result in an NBIO event occuring in
659  * which case control returns to the calling application. When this function
660  * is recalled we will resume in the same state where we left off.
661  *
662  * WRITE_STATE_SEND sends the message and performs any work to be done after
663  * sending.
664  *
665  * WRITE_STATE_POST_WORK performs any work necessary after the sending of the
666  * message has been completed. As for WRITE_STATE_PRE_WORK this could also
667  * result in an NBIO event.
668  */
669 static enum SUB_STATE_RETURN write_state_machine(SSL *s)
670 {
671     STATEM *st = &s->statem;
672     int ret;
673     enum WRITE_TRAN (*transition)(SSL *s);
674     enum WORK_STATE (*pre_work)(SSL *s, enum WORK_STATE wst);
675     enum WORK_STATE (*post_work)(SSL *s, enum WORK_STATE wst);
676     int (*construct_message)(SSL *s);
677     void (*cb) (const SSL *ssl, int type, int val) = NULL;
678
679     if (s->info_callback != NULL)
680         cb = s->info_callback;
681     else if (s->ctx->info_callback != NULL)
682         cb = s->ctx->info_callback;
683
684     if(s->server) {
685         transition = server_write_transition;
686         pre_work = server_pre_work;
687         post_work = server_post_work;
688         construct_message = server_construct_message;
689     } else {
690         transition = client_write_transition;
691         pre_work = client_pre_work;
692         post_work = client_post_work;
693         construct_message = client_construct_message;
694     }
695
696     while(1) {
697         switch(st->write_state) {
698         case WRITE_STATE_TRANSITION:
699             if (cb != NULL) {
700                 /* Notify callback of an impending state change */
701                 if (s->server)
702                     cb(s, SSL_CB_ACCEPT_LOOP, 1);
703                 else
704                     cb(s, SSL_CB_CONNECT_LOOP, 1);
705             }
706             switch(transition(s)) {
707             case WRITE_TRAN_CONTINUE:
708                 st->write_state = WRITE_STATE_PRE_WORK;
709                 st->write_state_work = WORK_MORE_A;
710                 break;
711
712             case WRITE_TRAN_FINISHED:
713                 return SUB_STATE_FINISHED;
714                 break;
715
716             default:
717                 return SUB_STATE_ERROR;
718             }
719             break;
720
721         case WRITE_STATE_PRE_WORK:
722             switch(st->write_state_work = pre_work(s, st->write_state_work)) {
723             default:
724                 return SUB_STATE_ERROR;
725
726             case WORK_FINISHED_CONTINUE:
727                 st->write_state = WRITE_STATE_SEND;
728                 break;
729
730             case WORK_FINISHED_STOP:
731                 return SUB_STATE_END_HANDSHAKE;
732             }
733             if(construct_message(s) == 0)
734                 return SUB_STATE_ERROR;
735
736             /* Fall through */
737
738         case WRITE_STATE_SEND:
739             if (SSL_IS_DTLS(s) && st->use_timer) {
740                 dtls1_start_timer(s);
741             }
742             ret = statem_do_write(s);
743             if (ret <= 0) {
744                 return SUB_STATE_ERROR;
745             }
746             st->write_state = WRITE_STATE_POST_WORK;
747             st->write_state_work = WORK_MORE_A;
748             /* Fall through */
749
750         case WRITE_STATE_POST_WORK:
751             switch(st->write_state_work = post_work(s, st->write_state_work)) {
752             default:
753                 return SUB_STATE_ERROR;
754
755             case WORK_FINISHED_CONTINUE:
756                 st->write_state = WRITE_STATE_TRANSITION;
757                 break;
758
759             case WORK_FINISHED_STOP:
760                 return SUB_STATE_END_HANDSHAKE;
761             }
762             break;
763
764         default:
765             return SUB_STATE_ERROR;
766         }
767     }
768 }
769
770 /*
771  * Flush the write BIO
772  */
773 static int statem_flush(SSL *s)
774 {
775     s->rwstate = SSL_WRITING;
776     if (BIO_flush(s->wbio) <= 0) {
777         return 0;
778     }
779     s->rwstate = SSL_NOTHING;
780
781     return 1;
782 }
783
784 /*
785  * Called by the record layer to determine whether application data is
786  * allowed to be sent in the current handshake state or not.
787  *
788  * Return values are:
789  *   1: Yes (application data allowed)
790  *   0: No (application data not allowed)
791  */
792 int statem_app_data_allowed(SSL *s)
793 {
794     STATEM *st = &s->statem;
795
796     if (st->state == MSG_FLOW_UNINITED || st->state == MSG_FLOW_RENEGOTIATE)
797         return 0;
798
799     if (!s->s3->in_read_app_data || (s->s3->total_renegotiations == 0))
800         return 0;
801
802     if (s->server) {
803         /*
804          * If we're a server and we haven't got as far as writing our
805          * ServerHello yet then we allow app data
806          */
807         if (st->hand_state == TLS_ST_BEFORE
808                 || st->hand_state == TLS_ST_SR_CLNT_HELLO)
809             return 1;
810     } else {
811         /*
812          * If we're a client and we haven't read the ServerHello yet then we
813          * allow app data
814          */
815         if (st->hand_state == TLS_ST_CW_CLNT_HELLO)
816             return 1;
817     }
818
819     return 0;
820 }
821
822
823 #ifndef OPENSSL_NO_SCTP
824 /*
825  * Set flag used by SCTP to determine whether we are in the read sock state
826  */
827 void statem_set_sctp_read_sock(SSL *s, int read_sock)
828 {
829     s->statem.in_sctp_read_sock = read_sock;
830 }
831
832 /*
833  * Called by the record layer to determine whether we are in the read sock
834  * state or not.
835  *
836  * Return values are:
837  *   1: Yes (we are in the read sock state)
838  *   0: No (we are not in the read sock state)
839  */
840 int statem_in_sctp_read_sock(SSL *s)
841 {
842     return s->statem.in_sctp_read_sock;
843 }
844 #endif
845
846 /*
847  * Is a CertificateRequest message allowed at the moment or not?
848  *
849  *  Return values are:
850  *  1: Yes
851  *  0: No
852  */
853 static inline int cert_req_allowed(SSL *s)
854 {
855     /* TLS does not like anon-DH with client cert */
856     if (s->version > SSL3_VERSION
857             && (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL))
858         return 0;
859
860     return 1;
861 }
862
863 /*
864  * Are we allowed to skip the ServerKeyExchange message?
865  *
866  *  Return values are:
867  *  1: Yes
868  *  0: No
869  */
870 static inline int key_exchange_skip_allowed(SSL *s)
871 {
872     long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
873
874     /*
875      * Can't skip server key exchange if this is an ephemeral
876      * ciphersuite.
877      */
878     if (alg_k & (SSL_kDHE | SSL_kECDHE)) {
879         return 0;
880     }
881
882     return 1;
883 }
884
885 /*
886  * client_read_transition() encapsulates the logic for the allowed handshake
887  * state transitions when the client is reading messages from the server. The
888  * message type that the server has sent is provided in |mt|. The current state
889  * is in |s->statem.hand_state|.
890  *
891  *  Return values are:
892  *  1: Success (transition allowed)
893  *  0: Error (transition not allowed)
894  */
895 static int client_read_transition(SSL *s, int mt)
896 {
897     STATEM *st = &s->statem;
898
899     switch(st->hand_state) {
900     case TLS_ST_CW_CLNT_HELLO:
901         if (mt == SSL3_MT_SERVER_HELLO) {
902             st->hand_state = TLS_ST_CR_SRVR_HELLO;
903             return 1;
904         }
905
906         if (SSL_IS_DTLS(s)) {
907             if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
908                 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
909                 return 1;
910             }
911         }
912         break;
913
914     case TLS_ST_CR_SRVR_HELLO:
915         if (s->hit) {
916             if (s->tlsext_ticket_expected) {
917                 if (mt == SSL3_MT_NEWSESSION_TICKET) {
918                     st->hand_state = TLS_ST_CR_SESSION_TICKET;
919                     return 1;
920                 }
921             } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
922                 st->hand_state = TLS_ST_CR_CHANGE;
923                 return 1;
924             }
925         } else {
926             if (SSL_IS_DTLS(s) && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
927                 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
928                 return 1;
929             } else if (!(s->s3->tmp.new_cipher->algorithm_auth
930                         & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
931                 if (mt == SSL3_MT_CERTIFICATE) {
932                     st->hand_state = TLS_ST_CR_CERT;
933                     return 1;
934                 }
935             } else {
936                 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
937                     st->hand_state = TLS_ST_CR_KEY_EXCH;
938                     return 1;
939                 } else if (key_exchange_skip_allowed(s)) {
940                     if (mt == SSL3_MT_CERTIFICATE_REQUEST
941                             && cert_req_allowed(s)) {
942                         st->hand_state = TLS_ST_CR_CERT_REQ;
943                         return 1;
944                     } else if (mt == SSL3_MT_SERVER_DONE) {
945                         st->hand_state = TLS_ST_CR_SRVR_DONE;
946                         return 1;
947                     }
948                 }
949             }
950         }
951         break;
952
953     case TLS_ST_CR_CERT:
954         if (s->tlsext_status_expected) {
955             if (mt == SSL3_MT_CERTIFICATE_STATUS) {
956                 st->hand_state = TLS_ST_CR_CERT_STATUS;
957                 return 1;
958             }
959         } else {
960             if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
961                 st->hand_state = TLS_ST_CR_KEY_EXCH;
962                 return 1;
963             } else if (key_exchange_skip_allowed(s)) {
964                 if (mt == SSL3_MT_CERTIFICATE_REQUEST && cert_req_allowed(s)) {
965                     st->hand_state = TLS_ST_CR_CERT_REQ;
966                     return 1;
967                 } else if (mt == SSL3_MT_SERVER_DONE) {
968                     st->hand_state = TLS_ST_CR_SRVR_DONE;
969                     return 1;
970                 }
971             }
972         }
973         break;
974
975     case TLS_ST_CR_CERT_STATUS:
976         if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
977             st->hand_state = TLS_ST_CR_KEY_EXCH;
978             return 1;
979         } else if (key_exchange_skip_allowed(s)) {
980             if (mt == SSL3_MT_CERTIFICATE_REQUEST && cert_req_allowed(s)) {
981                 st->hand_state = TLS_ST_CR_CERT_REQ;
982                 return 1;
983             } else if (mt == SSL3_MT_SERVER_DONE) {
984                 st->hand_state = TLS_ST_CR_SRVR_DONE;
985                 return 1;
986             }
987         }
988         break;
989
990     case TLS_ST_CR_KEY_EXCH:
991         if (mt == SSL3_MT_CERTIFICATE_REQUEST && cert_req_allowed(s)) {
992             st->hand_state = TLS_ST_CR_CERT_REQ;
993             return 1;
994         } else if (mt == SSL3_MT_SERVER_DONE) {
995             st->hand_state = TLS_ST_CR_SRVR_DONE;
996             return 1;
997         }
998         break;
999
1000     case TLS_ST_CR_CERT_REQ:
1001         if (mt == SSL3_MT_SERVER_DONE) {
1002             st->hand_state = TLS_ST_CR_SRVR_DONE;
1003             return 1;
1004         }
1005         break;
1006
1007     case TLS_ST_CW_FINISHED:
1008         if (mt == SSL3_MT_NEWSESSION_TICKET && s->tlsext_ticket_expected) {
1009             st->hand_state = TLS_ST_CR_SESSION_TICKET;
1010             return 1;
1011         } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
1012             st->hand_state = TLS_ST_CR_CHANGE;
1013             return 1;
1014         }
1015         break;
1016
1017     case TLS_ST_CR_SESSION_TICKET:
1018         if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
1019             st->hand_state = TLS_ST_CR_CHANGE;
1020             return 1;
1021         }
1022         break;
1023
1024     case TLS_ST_CR_CHANGE:
1025         if (mt == SSL3_MT_FINISHED) {
1026             st->hand_state = TLS_ST_CR_FINISHED;
1027             return 1;
1028         }
1029         break;
1030
1031     default:
1032         break;
1033     }
1034
1035     /* No valid transition found */
1036     return 0;
1037 }
1038
1039 /*
1040  * client_write_transition() works out what handshake state to move to next
1041  * when the client is writing messages to be sent to the server.
1042  */
1043 static enum WRITE_TRAN client_write_transition(SSL *s)
1044 {
1045     STATEM *st = &s->statem;
1046
1047     switch(st->hand_state) {
1048         case TLS_ST_OK:
1049             /* Renegotiation - fall through */
1050         case TLS_ST_BEFORE:
1051             st->hand_state = TLS_ST_CW_CLNT_HELLO;
1052             return WRITE_TRAN_CONTINUE;
1053
1054         case TLS_ST_CW_CLNT_HELLO:
1055             /*
1056              * No transition at the end of writing because we don't know what
1057              * we will be sent
1058              */
1059             return WRITE_TRAN_FINISHED;
1060
1061         case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
1062             st->hand_state = TLS_ST_CW_CLNT_HELLO;
1063             return WRITE_TRAN_CONTINUE;
1064
1065         case TLS_ST_CR_SRVR_DONE:
1066             if (s->s3->tmp.cert_req)
1067                 st->hand_state = TLS_ST_CW_CERT;
1068             else
1069                 st->hand_state = TLS_ST_CW_KEY_EXCH;
1070             return WRITE_TRAN_CONTINUE;
1071
1072         case TLS_ST_CW_CERT:
1073             st->hand_state = TLS_ST_CW_KEY_EXCH;
1074             return WRITE_TRAN_CONTINUE;
1075
1076         case TLS_ST_CW_KEY_EXCH:
1077             /*
1078              * For TLS, cert_req is set to 2, so a cert chain of nothing is
1079              * sent, but no verify packet is sent
1080              */
1081             /*
1082              * XXX: For now, we do not support client authentication in ECDH
1083              * cipher suites with ECDH (rather than ECDSA) certificates. We
1084              * need to skip the certificate verify message when client's
1085              * ECDH public key is sent inside the client certificate.
1086              */
1087             if (s->s3->tmp.cert_req == 1) {
1088                 st->hand_state = TLS_ST_CW_CERT_VRFY;
1089             } else {
1090                 st->hand_state = TLS_ST_CW_CHANGE;
1091             }
1092             if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
1093                 st->hand_state = TLS_ST_CW_CHANGE;
1094             }
1095             return WRITE_TRAN_CONTINUE;
1096
1097         case TLS_ST_CW_CERT_VRFY:
1098             st->hand_state = TLS_ST_CW_CHANGE;
1099             return WRITE_TRAN_CONTINUE;
1100
1101         case TLS_ST_CW_CHANGE:
1102 #if defined(OPENSSL_NO_NEXTPROTONEG)
1103             st->hand_state = TLS_ST_CW_FINISHED;
1104 #else
1105             if (!SSL_IS_DTLS(s) && s->s3->next_proto_neg_seen)
1106                 st->hand_state = TLS_ST_CW_NEXT_PROTO;
1107             else
1108                 st->hand_state = TLS_ST_CW_FINISHED;
1109 #endif
1110             return WRITE_TRAN_CONTINUE;
1111
1112 #if !defined(OPENSSL_NO_NEXTPROTONEG)
1113         case TLS_ST_CW_NEXT_PROTO:
1114             st->hand_state = TLS_ST_CW_FINISHED;
1115             return WRITE_TRAN_CONTINUE;
1116 #endif
1117
1118         case TLS_ST_CW_FINISHED:
1119             if (s->hit) {
1120                 st->hand_state = TLS_ST_OK;
1121                 /* TODO: This needs removing */
1122                 s->state = SSL_ST_OK;
1123                 return WRITE_TRAN_CONTINUE;
1124             } else {
1125                 return WRITE_TRAN_FINISHED;
1126             }
1127
1128         case TLS_ST_CR_FINISHED:
1129             if (s->hit) {
1130                 st->hand_state = TLS_ST_CW_CHANGE;
1131                 return WRITE_TRAN_CONTINUE;
1132             } else {
1133                 st->hand_state = TLS_ST_OK;
1134                 /* TODO: This needs removing */
1135                 s->state = SSL_ST_OK;
1136                 return WRITE_TRAN_CONTINUE;
1137             }
1138
1139         default:
1140             /* Shouldn't happen */
1141             return WRITE_TRAN_ERROR;
1142     }
1143 }
1144
1145 /*
1146  * Perform any pre work that needs to be done prior to sending a message from
1147  * the client to the server.
1148  */
1149 static enum WORK_STATE client_pre_work(SSL *s, enum WORK_STATE wst)
1150 {
1151     STATEM *st = &s->statem;
1152
1153     switch(st->hand_state) {
1154     case TLS_ST_CW_CLNT_HELLO:
1155         s->shutdown = 0;
1156         if (SSL_IS_DTLS(s)) {
1157             /* every DTLS ClientHello resets Finished MAC */
1158             ssl3_init_finished_mac(s);
1159         }
1160         break;
1161
1162     case TLS_ST_CW_CERT:
1163         return tls_prepare_client_certificate(s, wst);
1164
1165     case TLS_ST_CW_CHANGE:
1166         if (SSL_IS_DTLS(s)) {
1167             if (s->hit) {
1168                 /*
1169                  * We're into the last flight so we don't retransmit these
1170                  * messages unless we need to.
1171                  */
1172                 st->use_timer = 0;
1173             }
1174 #ifndef OPENSSL_NO_SCTP
1175             if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
1176                 return dtls_wait_for_dry(s);
1177 #endif
1178         }
1179         return WORK_FINISHED_CONTINUE;
1180
1181     case TLS_ST_OK:
1182         return tls_finish_handshake(s, wst);
1183
1184     default:
1185         /* No pre work to be done */
1186         break;
1187     }
1188
1189     return WORK_FINISHED_CONTINUE;
1190 }
1191
1192 /*
1193  * Perform any work that needs to be done after sending a message from the
1194  * client to the server.
1195  */
1196 static enum WORK_STATE client_post_work(SSL *s, enum WORK_STATE wst)
1197 {
1198     STATEM *st = &s->statem;
1199
1200     s->init_num = 0;
1201
1202     switch(st->hand_state) {
1203     case TLS_ST_CW_CLNT_HELLO:
1204         if (SSL_IS_DTLS(s) && s->d1->cookie_len > 0 && statem_flush(s) != 1)
1205             return WORK_MORE_A;
1206 #ifndef OPENSSL_NO_SCTP
1207         /* Disable buffering for SCTP */
1208         if (!SSL_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(s))) {
1209 #endif
1210             /*
1211              * turn on buffering for the next lot of output
1212              */
1213             if (s->bbio != s->wbio)
1214                 s->wbio = BIO_push(s->bbio, s->wbio);
1215 #ifndef OPENSSL_NO_SCTP
1216             }
1217 #endif
1218         if (SSL_IS_DTLS(s)) {
1219             /* Treat the next message as the first packet */
1220             s->first_packet = 1;
1221         }
1222         break;
1223
1224     case TLS_ST_CW_KEY_EXCH:
1225         if (tls_client_key_exchange_post_work(s) == 0)
1226             return WORK_ERROR;
1227         break;
1228
1229     case TLS_ST_CW_CHANGE:
1230         s->session->cipher = s->s3->tmp.new_cipher;
1231 #ifdef OPENSSL_NO_COMP
1232         s->session->compress_meth = 0;
1233 #else
1234         if (s->s3->tmp.new_compression == NULL)
1235             s->session->compress_meth = 0;
1236         else
1237             s->session->compress_meth = s->s3->tmp.new_compression->id;
1238 #endif
1239         if (!s->method->ssl3_enc->setup_key_block(s))
1240             return WORK_ERROR;
1241
1242         if (!s->method->ssl3_enc->change_cipher_state(s,
1243                                                       SSL3_CHANGE_CIPHER_CLIENT_WRITE))
1244             return WORK_ERROR;
1245
1246         if (SSL_IS_DTLS(s)) {
1247 #ifndef OPENSSL_NO_SCTP
1248             if (s->hit) {
1249                 /*
1250                  * Change to new shared key of SCTP-Auth, will be ignored if
1251                  * no SCTP used.
1252                  */
1253                 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
1254                          0, NULL);
1255             }
1256 #endif
1257
1258             dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
1259         }
1260         break;
1261
1262     case TLS_ST_CW_FINISHED:
1263 #ifndef OPENSSL_NO_SCTP
1264         if (wst == WORK_MORE_A && SSL_IS_DTLS(s) && s->hit == 0) {
1265             /*
1266              * Change to new shared key of SCTP-Auth, will be ignored if
1267              * no SCTP used.
1268              */
1269             BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
1270                      0, NULL);
1271         }
1272 #endif
1273         if (statem_flush(s) != 1)
1274             return WORK_MORE_B;
1275
1276         if (s->hit && tls_finish_handshake(s, WORK_MORE_A) != 1)
1277                 return WORK_ERROR;
1278         break;
1279
1280     default:
1281         /* No post work to be done */
1282         break;
1283     }
1284
1285     return WORK_FINISHED_CONTINUE;
1286 }
1287
1288 /*
1289  * Construct a message to be sent from the client to the server.
1290  *
1291  * Valid return values are:
1292  *   1: Success
1293  *   0: Error
1294  */
1295 static int client_construct_message(SSL *s)
1296 {
1297     STATEM *st = &s->statem;
1298
1299     switch(st->hand_state) {
1300     case TLS_ST_CW_CLNT_HELLO:
1301         return tls_construct_client_hello(s);
1302
1303     case TLS_ST_CW_CERT:
1304         return tls_construct_client_certificate(s);
1305
1306     case TLS_ST_CW_KEY_EXCH:
1307         return tls_construct_client_key_exchange(s);
1308
1309     case TLS_ST_CW_CERT_VRFY:
1310         return tls_construct_client_verify(s);
1311
1312     case TLS_ST_CW_CHANGE:
1313         if (SSL_IS_DTLS(s))
1314             return dtls_construct_change_cipher_spec(s);
1315         else
1316             return tls_construct_change_cipher_spec(s);
1317
1318 #if !defined(OPENSSL_NO_NEXTPROTONEG)
1319     case TLS_ST_CW_NEXT_PROTO:
1320         return tls_construct_next_proto(s);
1321 #endif
1322     case TLS_ST_CW_FINISHED:
1323         return tls_construct_finished(s,
1324                                       s->method->
1325                                       ssl3_enc->client_finished_label,
1326                                       s->method->
1327                                       ssl3_enc->client_finished_label_len);
1328
1329     default:
1330         /* Shouldn't happen */
1331         break;
1332     }
1333
1334     return 0;
1335 }
1336
1337 /* The spec allows for a longer length than this, but we limit it */
1338 #define HELLO_VERIFY_REQUEST_MAX_LENGTH 258
1339 #define SERVER_HELLO_MAX_LENGTH         20000
1340 #define SERVER_KEY_EXCH_MAX_LENGTH      102400
1341 #define SERVER_HELLO_DONE_MAX_LENGTH    0
1342 #define CCS_MAX_LENGTH                  1
1343 /* Max should actually be 36 but we are generous */
1344 #define FINISHED_MAX_LENGTH             64
1345
1346 /*
1347  * Returns the maximum allowed length for the current message that we are
1348  * reading. Excludes the message header.
1349  */
1350 static unsigned long client_max_message_size(SSL *s)
1351 {
1352     STATEM *st = &s->statem;
1353
1354     switch(st->hand_state) {
1355         case TLS_ST_CR_SRVR_HELLO:
1356             return SERVER_HELLO_MAX_LENGTH;
1357
1358         case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
1359             return HELLO_VERIFY_REQUEST_MAX_LENGTH;
1360
1361         case TLS_ST_CR_CERT:
1362             return s->max_cert_list;
1363
1364         case TLS_ST_CR_CERT_STATUS:
1365             return SSL3_RT_MAX_PLAIN_LENGTH;
1366
1367         case TLS_ST_CR_KEY_EXCH:
1368             return SERVER_KEY_EXCH_MAX_LENGTH;
1369
1370         case TLS_ST_CR_CERT_REQ:
1371             return SSL3_RT_MAX_PLAIN_LENGTH;
1372
1373         case TLS_ST_CR_SRVR_DONE:
1374             return SERVER_HELLO_DONE_MAX_LENGTH;
1375
1376         case TLS_ST_CR_CHANGE:
1377             return CCS_MAX_LENGTH;
1378
1379         case TLS_ST_CR_SESSION_TICKET:
1380             return SSL3_RT_MAX_PLAIN_LENGTH;
1381
1382         case TLS_ST_CR_FINISHED:
1383             return FINISHED_MAX_LENGTH;
1384
1385         default:
1386             /* Shouldn't happen */
1387             break;
1388     }
1389
1390     return 0;
1391 }
1392
1393 /*
1394  * Process a message that the client has been received from the server.
1395  */
1396 static enum MSG_PROCESS_RETURN client_process_message(SSL *s, unsigned long len)
1397 {
1398     STATEM *st = &s->statem;
1399
1400     switch(st->hand_state) {
1401         case TLS_ST_CR_SRVR_HELLO:
1402             return tls_process_server_hello(s, len);
1403
1404         case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
1405             return dtls_process_hello_verify(s, len);
1406
1407         case TLS_ST_CR_CERT:
1408             return tls_process_server_certificate(s, len);
1409
1410         case TLS_ST_CR_CERT_STATUS:
1411             return tls_process_cert_status(s, len);
1412
1413         case TLS_ST_CR_KEY_EXCH:
1414             return tls_process_key_exchange(s, len);
1415
1416         case TLS_ST_CR_CERT_REQ:
1417             return tls_process_certificate_request(s, len);
1418
1419         case TLS_ST_CR_SRVR_DONE:
1420             return tls_process_server_done(s, len);
1421
1422         case TLS_ST_CR_CHANGE:
1423             return tls_process_change_cipher_spec(s, len);
1424
1425         case TLS_ST_CR_SESSION_TICKET:
1426             return tls_process_new_session_ticket(s, len);
1427
1428         case TLS_ST_CR_FINISHED:
1429             return tls_process_finished(s, len);
1430
1431         default:
1432             /* Shouldn't happen */
1433             break;
1434     }
1435
1436     return MSG_PROCESS_ERROR;
1437 }
1438
1439 /*
1440  * Perform any further processing required following the receipt of a message
1441  * from the server
1442  */
1443 static enum WORK_STATE client_post_process_message(SSL *s, enum WORK_STATE wst)
1444 {
1445     STATEM *st = &s->statem;
1446
1447     switch(st->hand_state) {
1448 #ifndef OPENSSL_NO_SCTP
1449     case TLS_ST_CR_SRVR_DONE:
1450         /* We only get here if we are using SCTP and we are renegotiating */
1451         if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
1452             s->s3->in_read_app_data = 2;
1453             s->rwstate = SSL_READING;
1454             BIO_clear_retry_flags(SSL_get_rbio(s));
1455             BIO_set_retry_read(SSL_get_rbio(s));
1456             statem_set_sctp_read_sock(s, 1);
1457             return WORK_MORE_A;
1458         }
1459         statem_set_sctp_read_sock(s, 0);
1460         return WORK_FINISHED_STOP;
1461 #endif
1462
1463     case TLS_ST_CR_FINISHED:
1464         if (!s->hit)
1465             return tls_finish_handshake(s, wst);
1466         else
1467             return WORK_FINISHED_STOP;
1468     default:
1469         break;
1470     }
1471
1472     /* Shouldn't happen */
1473     return WORK_ERROR;
1474 }
1475
1476
1477 /*
1478  * server_read_transition() encapsulates the logic for the allowed handshake
1479  * state transitions when the server is reading messages from the client. The
1480  * message type that the client has sent is provided in |mt|. The current state
1481  * is in |s->statem.hand_state|.
1482  *
1483  *  Valid return values are:
1484  *  1: Success (transition allowed)
1485  *  0: Error (transition not allowed)
1486  */
1487 static int server_read_transition(SSL *s, int mt)
1488 {
1489     STATEM *st = &s->statem;
1490
1491     switch(st->hand_state) {
1492     case TLS_ST_BEFORE:
1493         if (mt == SSL3_MT_CLIENT_HELLO) {
1494             st->hand_state = TLS_ST_SR_CLNT_HELLO;
1495             return 1;
1496         }
1497         break;
1498
1499     case TLS_ST_SW_SRVR_DONE:
1500         /*
1501          * If we get a CKE message after a ServerDone then either
1502          * 1) We didn't request a Certificate
1503          * OR
1504          * 2) If we did request one then
1505          *      a) We allow no Certificate to be returned
1506          *      AND
1507          *      b) We are running SSL3 (in TLS1.0+ the client must return a 0
1508          *         list if we requested a certificate)
1509          */
1510         if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE
1511                 && (!s->s3->tmp.cert_request
1512                     || (!((s->verify_mode & SSL_VERIFY_PEER) &&
1513                           (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
1514                         && (s->version == SSL3_VERSION)))) {
1515             st->hand_state = TLS_ST_SR_KEY_EXCH;
1516             return 1;
1517         } else if (s->s3->tmp.cert_request) {
1518             if (mt == SSL3_MT_CERTIFICATE) {
1519                 st->hand_state = TLS_ST_SR_CERT;
1520                 return 1;
1521             } 
1522         }
1523         break;
1524
1525     case TLS_ST_SR_CERT:
1526         if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
1527             st->hand_state = TLS_ST_SR_KEY_EXCH;
1528             return 1;
1529         }
1530         break;
1531
1532     case TLS_ST_SR_KEY_EXCH:
1533         /*
1534          * We should only process a CertificateVerify message if we have
1535          * received a Certificate from the client. If so then |s->session->peer|
1536          * will be non NULL. In some instances a CertificateVerify message is
1537          * not required even if the peer has sent a Certificate (e.g. such as in
1538          * the case of static DH). In that case |s->no_cert_verify| should be
1539          * set.
1540          */
1541         if (s->session->peer == NULL || s->no_cert_verify) {
1542             if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
1543                 /*
1544                  * For the ECDH ciphersuites when the client sends its ECDH
1545                  * pub key in a certificate, the CertificateVerify message is
1546                  * not sent. Also for GOST ciphersuites when the client uses
1547                  * its key from the certificate for key exchange.
1548                  */
1549                 st->hand_state = TLS_ST_SR_CHANGE;
1550                 return 1;
1551             }
1552         } else {
1553             if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
1554                 st->hand_state = TLS_ST_SR_CERT_VRFY;
1555                 return 1;
1556             }
1557         }
1558         break;
1559
1560     case TLS_ST_SR_CERT_VRFY:
1561         if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
1562             st->hand_state = TLS_ST_SR_CHANGE;
1563             return 1;
1564         }
1565         break;
1566
1567     case TLS_ST_SR_CHANGE:
1568 #ifndef OPENSSL_NO_NEXTPROTONEG
1569         if (s->s3->next_proto_neg_seen) {
1570             if (mt == SSL3_MT_NEXT_PROTO) {
1571                 st->hand_state = TLS_ST_SR_NEXT_PROTO;
1572                 return 1;
1573             }
1574         } else {
1575 #endif
1576             if (mt == SSL3_MT_FINISHED) {
1577                 st->hand_state = TLS_ST_SR_FINISHED;
1578                 return 1;
1579             }
1580 #ifndef OPENSSL_NO_NEXTPROTONEG
1581         }
1582 #endif
1583         break;
1584
1585 #ifndef OPENSSL_NO_NEXTPROTONEG
1586     case TLS_ST_SR_NEXT_PROTO:
1587         if (mt == SSL3_MT_FINISHED) {
1588             st->hand_state = TLS_ST_SR_FINISHED;
1589             return 1;
1590         }
1591         break;
1592 #endif
1593
1594     case TLS_ST_SW_FINISHED:
1595         if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
1596             st->hand_state = TLS_ST_SR_CHANGE;
1597             return 1;
1598         }
1599         break;
1600
1601     default:
1602         break;
1603     }
1604
1605     /* No valid transition found */
1606     return 0;
1607 }
1608
1609 /*
1610  * Should we send a ServerKeyExchange message?
1611  *
1612  * Valid return values are:
1613  *   1: Yes
1614  *   0: No
1615  */
1616 static inline int send_server_key_exchange(SSL *s)
1617 {
1618     unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1619
1620     /*
1621      * only send a ServerKeyExchange if DH, fortezza or RSA but we have a
1622      * sign only certificate PSK: may send PSK identity hints For
1623      * ECC ciphersuites, we send a serverKeyExchange message only if
1624      * the cipher suite is either ECDH-anon or ECDHE. In other cases,
1625      * the server certificate contains the server's public key for
1626      * key exchange.
1627      */
1628     if (   (alg_k & SSL_kDHE)
1629         || (alg_k & SSL_kECDHE)
1630         || ((alg_k & SSL_kRSA)
1631             && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
1632                 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
1633                     && EVP_PKEY_size(s->cert->pkeys
1634                                      [SSL_PKEY_RSA_ENC].privatekey) *
1635                     8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
1636                    )
1637                )
1638            )
1639         /*
1640          * PSK: send ServerKeyExchange if PSK identity hint if
1641          * provided
1642          */
1643 #ifndef OPENSSL_NO_PSK
1644         /* Only send SKE if we have identity hint for plain PSK */
1645         || ((alg_k & (SSL_kPSK | SSL_kRSAPSK))
1646             && s->cert->psk_identity_hint)
1647         /* For other PSK always send SKE */
1648         || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK)))
1649 #endif
1650 #ifndef OPENSSL_NO_SRP
1651         /* SRP: send ServerKeyExchange */
1652         || (alg_k & SSL_kSRP)
1653 #endif
1654        ) {
1655         return 1;
1656     }
1657
1658     return 0;
1659 }
1660
1661 /*
1662  * Should we send a CertificateRequest message?
1663  *
1664  * Valid return values are:
1665  *   1: Yes
1666  *   0: No
1667  */
1668 static inline int send_certificate_request(SSL *s)
1669 {
1670     if (
1671            /* don't request cert unless asked for it: */
1672            s->verify_mode & SSL_VERIFY_PEER
1673            /*
1674             * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
1675             * during re-negotiation:
1676             */
1677            && ((s->session->peer == NULL) ||
1678                !(s->verify_mode & SSL_VERIFY_CLIENT_ONCE))
1679            /*
1680             * never request cert in anonymous ciphersuites (see
1681             * section "Certificate request" in SSL 3 drafts and in
1682             * RFC 2246):
1683             */
1684            && (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
1685            /*
1686             * ... except when the application insists on
1687             * verification (against the specs, but s3_clnt.c accepts
1688             * this for SSL 3)
1689             */
1690                || (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
1691            /* don't request certificate for SRP auth */
1692            && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aSRP)
1693            /*
1694             * With normal PSK Certificates and Certificate Requests
1695             * are omitted
1696             */
1697            && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)) {
1698         return 1;
1699     }
1700
1701     return 0;
1702 }
1703
1704 /*
1705  * server_write_transition() works out what handshake state to move to next
1706  * when the server is writing messages to be sent to the client.
1707  */
1708 static enum WRITE_TRAN server_write_transition(SSL *s)
1709 {
1710     STATEM *st = &s->statem;
1711
1712     switch(st->hand_state) {
1713         case TLS_ST_BEFORE:
1714             /* Just go straight to trying to read from the client */;
1715             return WRITE_TRAN_FINISHED;
1716
1717         case TLS_ST_OK:
1718             /* We must be trying to renegotiate */
1719             st->hand_state = TLS_ST_SW_HELLO_REQ;
1720             return WRITE_TRAN_CONTINUE;
1721
1722         case TLS_ST_SW_HELLO_REQ:
1723             st->hand_state = TLS_ST_OK;
1724             /* TODO: This needs removing */
1725             s->state = SSL_ST_OK;
1726             return WRITE_TRAN_CONTINUE;
1727
1728         case TLS_ST_SR_CLNT_HELLO:
1729             st->hand_state = TLS_ST_SW_SRVR_HELLO;
1730             return WRITE_TRAN_CONTINUE;
1731
1732         case TLS_ST_SW_SRVR_HELLO:
1733             if (s->hit) {
1734                 if (s->tlsext_ticket_expected)
1735                     st->hand_state = TLS_ST_SW_SESSION_TICKET;
1736                 else
1737                     st->hand_state = TLS_ST_SW_CHANGE;
1738             } else {
1739                 /* Check if it is anon DH or anon ECDH, */
1740                 /* normal PSK or SRP */
1741                 if (!(s->s3->tmp.new_cipher->algorithm_auth &
1742                      (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
1743                     st->hand_state = TLS_ST_SW_CERT;
1744                 } else if (send_server_key_exchange(s)) {
1745                     st->hand_state = TLS_ST_SW_KEY_EXCH;
1746                 } else if (send_certificate_request(s)) {
1747                     st->hand_state = TLS_ST_SW_CERT_REQ;
1748                 } else {
1749                     st->hand_state = TLS_ST_SW_SRVR_DONE;
1750                 }
1751             }
1752             return WRITE_TRAN_CONTINUE;
1753
1754         case TLS_ST_SW_CERT:
1755             if (s->tlsext_status_expected) {
1756                 st->hand_state = TLS_ST_SW_CERT_STATUS;
1757                 return WRITE_TRAN_CONTINUE;
1758             }
1759             /* Fall through */
1760
1761         case TLS_ST_SW_CERT_STATUS:
1762             if (send_server_key_exchange(s)) {
1763                 st->hand_state = TLS_ST_SW_KEY_EXCH;
1764                 return WRITE_TRAN_CONTINUE;
1765             }
1766             /* Fall through */
1767
1768         case TLS_ST_SW_KEY_EXCH:
1769             if (send_certificate_request(s)) {
1770                 st->hand_state = TLS_ST_SW_CERT_REQ;
1771                 return WRITE_TRAN_CONTINUE;
1772             }
1773             /* Fall through */
1774
1775         case TLS_ST_SW_CERT_REQ:
1776             st->hand_state = TLS_ST_SW_SRVR_DONE;
1777             return WRITE_TRAN_CONTINUE;
1778
1779         case TLS_ST_SW_SRVR_DONE:
1780             return WRITE_TRAN_FINISHED;
1781
1782         case TLS_ST_SR_FINISHED:
1783             if (s->hit) {
1784                 st->hand_state = TLS_ST_OK;
1785                 /* TODO: This needs removing */
1786                 s->state = SSL_ST_OK;
1787                 return WRITE_TRAN_CONTINUE;
1788             } else if (s->tlsext_ticket_expected) {
1789                 st->hand_state = TLS_ST_SW_SESSION_TICKET;
1790             } else {
1791                 st->hand_state = TLS_ST_SW_CHANGE;
1792             }
1793             return WRITE_TRAN_CONTINUE;
1794
1795         case TLS_ST_SW_SESSION_TICKET:
1796             st->hand_state = TLS_ST_SW_CHANGE;
1797             return WRITE_TRAN_CONTINUE;
1798
1799         case TLS_ST_SW_CHANGE:
1800             st->hand_state = TLS_ST_SW_FINISHED;
1801             return WRITE_TRAN_CONTINUE;
1802
1803         case TLS_ST_SW_FINISHED:
1804             if (s->hit) {
1805                 return WRITE_TRAN_FINISHED;
1806             }
1807             st->hand_state = TLS_ST_OK;
1808             /* TODO: This needs removing */
1809             s->state = SSL_ST_OK;
1810             return WRITE_TRAN_CONTINUE;
1811
1812         default:
1813             /* Shouldn't happen */
1814             return WRITE_TRAN_ERROR;
1815     }
1816 }
1817
1818 /*
1819  * Perform any pre work that needs to be done prior to sending a message from
1820  * the server to the client.
1821  */
1822 static enum WORK_STATE server_pre_work(SSL *s, enum WORK_STATE wst)
1823 {
1824     STATEM *st = &s->statem;
1825
1826     switch(st->hand_state) {
1827     case TLS_ST_SW_HELLO_REQ:
1828         s->shutdown = 0;
1829         break;
1830
1831     case TLS_ST_SW_CHANGE:
1832         s->session->cipher = s->s3->tmp.new_cipher;
1833         if (!s->method->ssl3_enc->setup_key_block(s)) {
1834             statem_set_error(s);
1835             return WORK_ERROR;
1836         }
1837         return WORK_FINISHED_CONTINUE;
1838
1839     case TLS_ST_OK:
1840         return tls_finish_handshake(s, wst);
1841
1842     default:
1843         /* No pre work to be done */
1844         break;
1845     }
1846
1847     return WORK_FINISHED_CONTINUE;
1848 }
1849
1850 /*
1851  * Perform any work that needs to be done after sending a message from the
1852  * server to the client.
1853  */
1854 static enum WORK_STATE server_post_work(SSL *s, enum WORK_STATE wst)
1855 {
1856     STATEM *st = &s->statem;
1857
1858     s->init_num = 0;
1859
1860     switch(st->hand_state) {
1861     case TLS_ST_SW_HELLO_REQ:
1862         if (statem_flush(s) != 1)
1863             return WORK_MORE_A;
1864         ssl3_init_finished_mac(s);
1865         break;
1866
1867     case TLS_ST_SW_CHANGE:
1868         if (!s->method->ssl3_enc->change_cipher_state(s,
1869                 SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
1870             statem_set_error(s);
1871             return WORK_ERROR;
1872         }
1873         break;
1874
1875     case TLS_ST_SW_SRVR_DONE:
1876         if (statem_flush(s) != 1)
1877             return WORK_MORE_A;
1878         break;
1879
1880     case TLS_ST_SW_FINISHED:
1881         if (statem_flush(s) != 1)
1882             return WORK_MORE_A;
1883         break;
1884
1885     default:
1886         /* No post work to be done */
1887         break;
1888     }
1889
1890     return WORK_FINISHED_CONTINUE;
1891 }
1892
1893 /*
1894  * Construct a message to be sent from the server to the client.
1895  *
1896  * Valid return values are:
1897  *   1: Success
1898  *   0: Error
1899  */
1900 static int server_construct_message(SSL *s)
1901 {
1902     STATEM *st = &s->statem;
1903
1904     switch(st->hand_state) {
1905     case TLS_ST_SW_HELLO_REQ:
1906         return tls_construct_hello_request(s);
1907
1908     case TLS_ST_SW_SRVR_HELLO:
1909         return tls_construct_server_hello(s);
1910
1911     case TLS_ST_SW_CERT:
1912         return tls_construct_server_certificate(s);
1913
1914     case TLS_ST_SW_KEY_EXCH:
1915         return tls_construct_server_key_exchange(s);
1916
1917     case TLS_ST_SW_CERT_REQ:
1918         return tls_construct_certificate_request(s);
1919
1920     case TLS_ST_SW_SRVR_DONE:
1921         return tls_construct_server_done(s);
1922
1923     case TLS_ST_SW_SESSION_TICKET:
1924         return tls_construct_new_session_ticket(s);
1925
1926     case TLS_ST_SW_CERT_STATUS:
1927         return tls_construct_cert_status(s);
1928
1929     case TLS_ST_SW_CHANGE:
1930         if (SSL_IS_DTLS(s))
1931             return dtls_construct_change_cipher_spec(s);
1932         else
1933             return tls_construct_change_cipher_spec(s);
1934
1935     case TLS_ST_SW_FINISHED:
1936         return tls_construct_finished(s,
1937                                       s->method->
1938                                       ssl3_enc->server_finished_label,
1939                                       s->method->
1940                                       ssl3_enc->server_finished_label_len);
1941
1942     default:
1943         /* Shouldn't happen */
1944         break;
1945     }
1946
1947     return 0;
1948 }
1949
1950 #define CLIENT_KEY_EXCH_MAX_LENGTH      2048
1951 #define NEXT_PROTO_MAX_LENGTH           514
1952
1953 /*
1954  * Returns the maximum allowed length for the current message that we are
1955  * reading. Excludes the message header.
1956  */
1957 static unsigned long server_max_message_size(SSL *s)
1958 {
1959     STATEM *st = &s->statem;
1960
1961     switch(st->hand_state) {
1962     case TLS_ST_SR_CLNT_HELLO:
1963         return SSL3_RT_MAX_PLAIN_LENGTH;
1964
1965     case TLS_ST_SR_CERT:
1966         return s->max_cert_list;
1967
1968     case TLS_ST_SR_KEY_EXCH:
1969         return CLIENT_KEY_EXCH_MAX_LENGTH;
1970
1971     case TLS_ST_SR_CERT_VRFY:
1972         return SSL3_RT_MAX_PLAIN_LENGTH;
1973
1974 #ifndef OPENSSL_NO_NEXTPROTONEG
1975     case TLS_ST_SR_NEXT_PROTO:
1976         return NEXT_PROTO_MAX_LENGTH;
1977 #endif
1978
1979     case TLS_ST_SR_CHANGE:
1980         return CCS_MAX_LENGTH;
1981
1982     case TLS_ST_SR_FINISHED:
1983         return FINISHED_MAX_LENGTH;
1984
1985     default:
1986         /* Shouldn't happen */
1987         break;
1988     }
1989
1990     return 0;
1991 }
1992
1993 /*
1994  * Process a message that the server has received from the client.
1995  */
1996 static enum MSG_PROCESS_RETURN  server_process_message(SSL *s,
1997                                                        unsigned long len)
1998 {
1999     STATEM *st = &s->statem;
2000
2001     switch(st->hand_state) {
2002     case TLS_ST_SR_CLNT_HELLO:
2003         return tls_process_client_hello(s, len);
2004
2005     case TLS_ST_SR_CERT:
2006         return tls_process_client_certificate(s, len);
2007
2008     case TLS_ST_SR_KEY_EXCH:
2009         return tls_process_client_key_exchange(s, len);
2010
2011     case TLS_ST_SR_CERT_VRFY:
2012         return tls_process_cert_verify(s, len);
2013
2014 #ifndef OPENSSL_NO_NEXTPROTONEG
2015     case TLS_ST_SR_NEXT_PROTO:
2016         return tls_process_next_proto(s, len);
2017 #endif
2018
2019     case TLS_ST_SR_CHANGE:
2020         return tls_process_change_cipher_spec(s, len);
2021
2022     case TLS_ST_SR_FINISHED:
2023         return tls_process_finished(s, len);
2024
2025     default:
2026         /* Shouldn't happen */
2027         break;
2028     }
2029
2030     return MSG_PROCESS_ERROR;
2031 }
2032
2033 /*
2034  * Perform any further processing required following the receipt of a message
2035  * from the client
2036  */
2037 static enum WORK_STATE server_post_process_message(SSL *s, enum WORK_STATE wst)
2038 {
2039     STATEM *st = &s->statem;
2040
2041     switch(st->hand_state) {
2042     case TLS_ST_SR_CLNT_HELLO:
2043         return tls_post_process_client_hello(s, wst);
2044
2045     case TLS_ST_SR_KEY_EXCH:
2046         return tls_post_process_client_key_exchange(s, wst);
2047
2048     case TLS_ST_SR_FINISHED:
2049         if (s->hit)
2050             return tls_finish_handshake(s, wst);
2051         else
2052             return WORK_FINISHED_STOP;
2053     default:
2054         break;
2055     }
2056
2057     /* Shouldn't happen */
2058     return WORK_ERROR;
2059 }