Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
Rich Salz | aa8f3d7 | 2017-06-15 10:16:46 -0400 | [diff] [blame] | 3 | * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved |
Rich Salz | c80149d | 2017-06-20 10:14:36 -0400 | [diff] [blame] | 4 | * Copyright 2005 Nokia. All rights reserved. |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 5 | * |
Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 6 | * Licensed under the OpenSSL license (the "License"). You may not use |
| 7 | * this file except in compliance with the License. You can obtain a copy |
| 8 | * in the file LICENSE in the source distribution or at |
| 9 | * https://www.openssl.org/source/license.html |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 10 | */ |
Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 11 | |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 12 | #include <stdio.h> |
Matt Caswell | fc24f0b | 2017-01-17 10:43:37 +0000 | [diff] [blame] | 13 | #include <time.h> |
Matt Caswell | 8ba708e | 2015-09-11 10:48:59 +0100 | [diff] [blame] | 14 | #include "../ssl_locl.h" |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 15 | #include "statem_locl.h" |
Bodo Möller | ec57782 | 1999-04-23 22:13:45 +0000 | [diff] [blame] | 16 | #include <openssl/buffer.h> |
| 17 | #include <openssl/rand.h> |
| 18 | #include <openssl/objects.h> |
| 19 | #include <openssl/evp.h> |
Ben Laurie | dbad169 | 2001-07-30 23:57:25 +0000 | [diff] [blame] | 20 | #include <openssl/md5.h> |
Rich Salz | 3c27208 | 2016-03-18 14:30:20 -0400 | [diff] [blame] | 21 | #include <openssl/dh.h> |
Geoff Thorpe | d095b68 | 2004-05-17 18:53:47 +0000 | [diff] [blame] | 22 | #include <openssl/bn.h> |
Rich Salz | 3c27208 | 2016-03-18 14:30:20 -0400 | [diff] [blame] | 23 | #include <openssl/engine.h> |
Richard Levitte | f9b3bff | 2000-11-30 22:53:34 +0000 | [diff] [blame] | 24 | |
Matt Caswell | 3847d42 | 2017-02-01 13:31:27 +0000 | [diff] [blame] | 25 | static MSG_PROCESS_RETURN tls_process_hello_retry_request(SSL *s, PACKET *pkt); |
Matt Caswell | e46f233 | 2016-11-23 15:20:22 +0000 | [diff] [blame] | 26 | static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL *s, PACKET *pkt); |
| 27 | |
Matt Caswell | 7ab0963 | 2015-12-23 09:45:02 +0000 | [diff] [blame] | 28 | static ossl_inline int cert_req_allowed(SSL *s); |
Matt Caswell | a455d0f | 2015-09-14 15:06:37 +0100 | [diff] [blame] | 29 | static int key_exchange_expected(SSL *s); |
Matt Caswell | d45ba43 | 2015-04-24 15:05:27 +0100 | [diff] [blame] | 30 | static int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, |
Matt Caswell | ae2f7b3 | 2016-09-05 17:34:04 +0100 | [diff] [blame] | 31 | WPACKET *pkt); |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 32 | |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 33 | /* |
| 34 | * Is a CertificateRequest message allowed at the moment or not? |
| 35 | * |
| 36 | * Return values are: |
| 37 | * 1: Yes |
| 38 | * 0: No |
| 39 | */ |
Matt Caswell | 7ab0963 | 2015-12-23 09:45:02 +0000 | [diff] [blame] | 40 | static ossl_inline int cert_req_allowed(SSL *s) |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 41 | { |
| 42 | /* TLS does not like anon-DH with client cert */ |
Matt Caswell | b7fa1f9 | 2015-10-26 23:11:44 +0000 | [diff] [blame] | 43 | if ((s->version > SSL3_VERSION |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 44 | && (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)) |
| 45 | || (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aSRP | SSL_aPSK))) |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 46 | return 0; |
| 47 | |
| 48 | return 1; |
| 49 | } |
| 50 | |
| 51 | /* |
Matt Caswell | a455d0f | 2015-09-14 15:06:37 +0100 | [diff] [blame] | 52 | * Should we expect the ServerKeyExchange message or not? |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 53 | * |
| 54 | * Return values are: |
| 55 | * 1: Yes |
| 56 | * 0: No |
| 57 | */ |
Matt Caswell | a455d0f | 2015-09-14 15:06:37 +0100 | [diff] [blame] | 58 | static int key_exchange_expected(SSL *s) |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 59 | { |
| 60 | long alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 61 | |
| 62 | /* |
| 63 | * Can't skip server key exchange if this is an ephemeral |
Matt Caswell | a455d0f | 2015-09-14 15:06:37 +0100 | [diff] [blame] | 64 | * ciphersuite or for SRP |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 65 | */ |
Matt Caswell | a455d0f | 2015-09-14 15:06:37 +0100 | [diff] [blame] | 66 | if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK |
| 67 | | SSL_kSRP)) { |
| 68 | return 1; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 69 | } |
| 70 | |
Matt Caswell | a455d0f | 2015-09-14 15:06:37 +0100 | [diff] [blame] | 71 | return 0; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /* |
Matt Caswell | 8481f58 | 2015-10-26 11:54:17 +0000 | [diff] [blame] | 75 | * ossl_statem_client_read_transition() encapsulates the logic for the allowed |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 76 | * handshake state transitions when a TLS1.3 client is reading messages from the |
| 77 | * server. The message type that the server has sent is provided in |mt|. The |
| 78 | * current state is in |s->statem.hand_state|. |
| 79 | * |
Matt Caswell | 94ed2c6 | 2016-11-14 14:53:31 +0000 | [diff] [blame] | 80 | * Return values are 1 for success (transition allowed) and 0 on error |
| 81 | * (transition not allowed) |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 82 | */ |
| 83 | static int ossl_statem_client13_read_transition(SSL *s, int mt) |
| 84 | { |
| 85 | OSSL_STATEM *st = &s->statem; |
| 86 | |
| 87 | /* |
| 88 | * Note: There is no case for TLS_ST_CW_CLNT_HELLO, because we haven't |
| 89 | * yet negotiated TLSv1.3 at that point so that is handled by |
| 90 | * ossl_statem_client_read_transition() |
| 91 | */ |
| 92 | |
| 93 | switch (st->hand_state) { |
| 94 | default: |
| 95 | break; |
| 96 | |
Matt Caswell | 3847d42 | 2017-02-01 13:31:27 +0000 | [diff] [blame] | 97 | case TLS_ST_CW_CLNT_HELLO: |
| 98 | /* |
| 99 | * This must a ClientHello following a HelloRetryRequest, so the only |
| 100 | * thing we can get now is a ServerHello. |
| 101 | */ |
| 102 | if (mt == SSL3_MT_SERVER_HELLO) { |
| 103 | st->hand_state = TLS_ST_CR_SRVR_HELLO; |
| 104 | return 1; |
| 105 | } |
| 106 | break; |
| 107 | |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 108 | case TLS_ST_CR_SRVR_HELLO: |
Matt Caswell | e46f233 | 2016-11-23 15:20:22 +0000 | [diff] [blame] | 109 | if (mt == SSL3_MT_ENCRYPTED_EXTENSIONS) { |
| 110 | st->hand_state = TLS_ST_CR_ENCRYPTED_EXTENSIONS; |
| 111 | return 1; |
| 112 | } |
| 113 | break; |
| 114 | |
| 115 | case TLS_ST_CR_ENCRYPTED_EXTENSIONS: |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 116 | if (s->hit) { |
Matt Caswell | 92760c2 | 2016-11-09 14:06:12 +0000 | [diff] [blame] | 117 | if (mt == SSL3_MT_FINISHED) { |
| 118 | st->hand_state = TLS_ST_CR_FINISHED; |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 119 | return 1; |
| 120 | } |
| 121 | } else { |
Matt Caswell | 92760c2 | 2016-11-09 14:06:12 +0000 | [diff] [blame] | 122 | if (mt == SSL3_MT_CERTIFICATE_REQUEST) { |
| 123 | st->hand_state = TLS_ST_CR_CERT_REQ; |
| 124 | return 1; |
Matt Caswell | f5ca0b0 | 2016-11-21 12:10:35 +0000 | [diff] [blame] | 125 | } |
| 126 | if (mt == SSL3_MT_CERTIFICATE) { |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 127 | st->hand_state = TLS_ST_CR_CERT; |
| 128 | return 1; |
| 129 | } |
| 130 | } |
| 131 | break; |
| 132 | |
Matt Caswell | 92760c2 | 2016-11-09 14:06:12 +0000 | [diff] [blame] | 133 | case TLS_ST_CR_CERT_REQ: |
| 134 | if (mt == SSL3_MT_CERTIFICATE) { |
| 135 | st->hand_state = TLS_ST_CR_CERT; |
| 136 | return 1; |
| 137 | } |
| 138 | break; |
| 139 | |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 140 | case TLS_ST_CR_CERT: |
Matt Caswell | 2c5dfdc | 2016-12-05 17:04:51 +0000 | [diff] [blame] | 141 | if (mt == SSL3_MT_CERTIFICATE_VERIFY) { |
| 142 | st->hand_state = TLS_ST_CR_CERT_VRFY; |
| 143 | return 1; |
| 144 | } |
| 145 | break; |
| 146 | |
| 147 | case TLS_ST_CR_CERT_VRFY: |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 148 | if (mt == SSL3_MT_FINISHED) { |
| 149 | st->hand_state = TLS_ST_CR_FINISHED; |
| 150 | return 1; |
| 151 | } |
| 152 | break; |
Matt Caswell | cc2455b | 2017-01-11 17:18:19 +0000 | [diff] [blame] | 153 | |
| 154 | case TLS_ST_OK: |
| 155 | if (mt == SSL3_MT_NEWSESSION_TICKET) { |
| 156 | st->hand_state = TLS_ST_CR_SESSION_TICKET; |
| 157 | return 1; |
| 158 | } |
Matt Caswell | e1c3de4 | 2017-02-09 12:07:31 +0000 | [diff] [blame] | 159 | if (mt == SSL3_MT_KEY_UPDATE) { |
| 160 | st->hand_state = TLS_ST_CR_KEY_UPDATE; |
| 161 | return 1; |
| 162 | } |
Matt Caswell | cc2455b | 2017-01-11 17:18:19 +0000 | [diff] [blame] | 163 | break; |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 166 | /* No valid transition found */ |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * ossl_statem_client_read_transition() encapsulates the logic for the allowed |
Matt Caswell | 8481f58 | 2015-10-26 11:54:17 +0000 | [diff] [blame] | 172 | * handshake state transitions when the client is reading messages from the |
| 173 | * server. The message type that the server has sent is provided in |mt|. The |
| 174 | * current state is in |s->statem.hand_state|. |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 175 | * |
Matt Caswell | 94ed2c6 | 2016-11-14 14:53:31 +0000 | [diff] [blame] | 176 | * Return values are 1 for success (transition allowed) and 0 on error |
| 177 | * (transition not allowed) |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 178 | */ |
Matt Caswell | 8481f58 | 2015-10-26 11:54:17 +0000 | [diff] [blame] | 179 | int ossl_statem_client_read_transition(SSL *s, int mt) |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 180 | { |
Matt Caswell | d6f1a6e | 2015-10-05 10:58:52 +0100 | [diff] [blame] | 181 | OSSL_STATEM *st = &s->statem; |
Matt Caswell | a455d0f | 2015-09-14 15:06:37 +0100 | [diff] [blame] | 182 | int ske_expected; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 183 | |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 184 | /* |
Matt Caswell | 3847d42 | 2017-02-01 13:31:27 +0000 | [diff] [blame] | 185 | * Note that after writing the first ClientHello we don't know what version |
| 186 | * we are going to negotiate yet, so we don't take this branch until later. |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 187 | */ |
Matt Caswell | f5ca0b0 | 2016-11-21 12:10:35 +0000 | [diff] [blame] | 188 | if (SSL_IS_TLS13(s)) { |
Matt Caswell | 5abeaf3 | 2016-11-15 10:30:34 +0000 | [diff] [blame] | 189 | if (!ossl_statem_client13_read_transition(s, mt)) |
| 190 | goto err; |
| 191 | return 1; |
| 192 | } |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 193 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 194 | switch (st->hand_state) { |
Rich Salz | f3b3d7f | 2016-08-30 13:31:18 -0400 | [diff] [blame] | 195 | default: |
| 196 | break; |
| 197 | |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 198 | case TLS_ST_CW_CLNT_HELLO: |
| 199 | if (mt == SSL3_MT_SERVER_HELLO) { |
| 200 | st->hand_state = TLS_ST_CR_SRVR_HELLO; |
| 201 | return 1; |
| 202 | } |
| 203 | |
| 204 | if (SSL_IS_DTLS(s)) { |
| 205 | if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) { |
| 206 | st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST; |
| 207 | return 1; |
| 208 | } |
Matt Caswell | 3847d42 | 2017-02-01 13:31:27 +0000 | [diff] [blame] | 209 | } else { |
| 210 | if (mt == SSL3_MT_HELLO_RETRY_REQUEST) { |
| 211 | st->hand_state = TLS_ST_CR_HELLO_RETRY_REQUEST; |
| 212 | return 1; |
| 213 | } |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 214 | } |
| 215 | break; |
| 216 | |
Matt Caswell | d7f8783 | 2017-02-25 15:59:44 +0000 | [diff] [blame] | 217 | case TLS_ST_EARLY_DATA: |
Matt Caswell | 4004ce5 | 2017-02-25 00:06:49 +0000 | [diff] [blame] | 218 | /* |
| 219 | * We've not actually selected TLSv1.3 yet, but we have sent early |
| 220 | * data. The only thing allowed now is a ServerHello or a |
| 221 | * HelloRetryRequest. |
| 222 | */ |
| 223 | if (mt == SSL3_MT_SERVER_HELLO) { |
| 224 | st->hand_state = TLS_ST_CR_SRVR_HELLO; |
| 225 | return 1; |
| 226 | } |
| 227 | if (mt == SSL3_MT_HELLO_RETRY_REQUEST) { |
| 228 | st->hand_state = TLS_ST_CR_HELLO_RETRY_REQUEST; |
| 229 | return 1; |
| 230 | } |
| 231 | break; |
| 232 | |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 233 | case TLS_ST_CR_SRVR_HELLO: |
| 234 | if (s->hit) { |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 235 | if (s->ext.ticket_expected) { |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 236 | if (mt == SSL3_MT_NEWSESSION_TICKET) { |
| 237 | st->hand_state = TLS_ST_CR_SESSION_TICKET; |
| 238 | return 1; |
| 239 | } |
| 240 | } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { |
| 241 | st->hand_state = TLS_ST_CR_CHANGE; |
| 242 | return 1; |
| 243 | } |
| 244 | } else { |
| 245 | if (SSL_IS_DTLS(s) && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) { |
| 246 | st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST; |
| 247 | return 1; |
Matt Caswell | ad3819c | 2015-12-04 10:18:01 +0000 | [diff] [blame] | 248 | } else if (s->version >= TLS1_VERSION |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 249 | && s->ext.session_secret_cb != NULL |
| 250 | && s->session->ext.tick != NULL |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 251 | && mt == SSL3_MT_CHANGE_CIPHER_SPEC) { |
Matt Caswell | ad3819c | 2015-12-04 10:18:01 +0000 | [diff] [blame] | 252 | /* |
| 253 | * Normally, we can tell if the server is resuming the session |
| 254 | * from the session ID. EAP-FAST (RFC 4851), however, relies on |
| 255 | * the next server message after the ServerHello to determine if |
| 256 | * the server is resuming. |
| 257 | */ |
| 258 | s->hit = 1; |
| 259 | st->hand_state = TLS_ST_CR_CHANGE; |
| 260 | return 1; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 261 | } else if (!(s->s3->tmp.new_cipher->algorithm_auth |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 262 | & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) { |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 263 | if (mt == SSL3_MT_CERTIFICATE) { |
| 264 | st->hand_state = TLS_ST_CR_CERT; |
| 265 | return 1; |
| 266 | } |
| 267 | } else { |
Matt Caswell | a455d0f | 2015-09-14 15:06:37 +0100 | [diff] [blame] | 268 | ske_expected = key_exchange_expected(s); |
Matt Caswell | a455d0f | 2015-09-14 15:06:37 +0100 | [diff] [blame] | 269 | /* SKE is optional for some PSK ciphersuites */ |
| 270 | if (ske_expected |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 271 | || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK) |
| 272 | && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) { |
Matt Caswell | a455d0f | 2015-09-14 15:06:37 +0100 | [diff] [blame] | 273 | if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) { |
| 274 | st->hand_state = TLS_ST_CR_KEY_EXCH; |
| 275 | return 1; |
| 276 | } |
| 277 | } else if (mt == SSL3_MT_CERTIFICATE_REQUEST |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 278 | && cert_req_allowed(s)) { |
| 279 | st->hand_state = TLS_ST_CR_CERT_REQ; |
| 280 | return 1; |
Matt Caswell | a455d0f | 2015-09-14 15:06:37 +0100 | [diff] [blame] | 281 | } else if (mt == SSL3_MT_SERVER_DONE) { |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 282 | st->hand_state = TLS_ST_CR_SRVR_DONE; |
| 283 | return 1; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | } |
| 287 | break; |
| 288 | |
| 289 | case TLS_ST_CR_CERT: |
Matt Caswell | bb1aaab | 2015-11-05 14:31:11 +0000 | [diff] [blame] | 290 | /* |
| 291 | * The CertificateStatus message is optional even if |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 292 | * |ext.status_expected| is set |
Matt Caswell | bb1aaab | 2015-11-05 14:31:11 +0000 | [diff] [blame] | 293 | */ |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 294 | if (s->ext.status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) { |
Matt Caswell | bb1aaab | 2015-11-05 14:31:11 +0000 | [diff] [blame] | 295 | st->hand_state = TLS_ST_CR_CERT_STATUS; |
| 296 | return 1; |
Matt Caswell | a455d0f | 2015-09-14 15:06:37 +0100 | [diff] [blame] | 297 | } |
| 298 | /* Fall through */ |
| 299 | |
| 300 | case TLS_ST_CR_CERT_STATUS: |
| 301 | ske_expected = key_exchange_expected(s); |
Matt Caswell | a455d0f | 2015-09-14 15:06:37 +0100 | [diff] [blame] | 302 | /* SKE is optional for some PSK ciphersuites */ |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 303 | if (ske_expected || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK) |
| 304 | && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) { |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 305 | if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) { |
| 306 | st->hand_state = TLS_ST_CR_KEY_EXCH; |
| 307 | return 1; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 308 | } |
Matt Caswell | 672f333 | 2016-06-22 19:43:46 +0100 | [diff] [blame] | 309 | goto err; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 310 | } |
Matt Caswell | a455d0f | 2015-09-14 15:06:37 +0100 | [diff] [blame] | 311 | /* Fall through */ |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 312 | |
| 313 | case TLS_ST_CR_KEY_EXCH: |
Matt Caswell | a455d0f | 2015-09-14 15:06:37 +0100 | [diff] [blame] | 314 | if (mt == SSL3_MT_CERTIFICATE_REQUEST) { |
| 315 | if (cert_req_allowed(s)) { |
| 316 | st->hand_state = TLS_ST_CR_CERT_REQ; |
| 317 | return 1; |
| 318 | } |
Matt Caswell | 672f333 | 2016-06-22 19:43:46 +0100 | [diff] [blame] | 319 | goto err; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 320 | } |
Matt Caswell | a455d0f | 2015-09-14 15:06:37 +0100 | [diff] [blame] | 321 | /* Fall through */ |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 322 | |
| 323 | case TLS_ST_CR_CERT_REQ: |
| 324 | if (mt == SSL3_MT_SERVER_DONE) { |
| 325 | st->hand_state = TLS_ST_CR_SRVR_DONE; |
| 326 | return 1; |
| 327 | } |
| 328 | break; |
| 329 | |
| 330 | case TLS_ST_CW_FINISHED: |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 331 | if (s->ext.ticket_expected) { |
David Benjamin | c45d6b2 | 2016-03-05 19:35:52 -0500 | [diff] [blame] | 332 | if (mt == SSL3_MT_NEWSESSION_TICKET) { |
| 333 | st->hand_state = TLS_ST_CR_SESSION_TICKET; |
| 334 | return 1; |
| 335 | } |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 336 | } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { |
| 337 | st->hand_state = TLS_ST_CR_CHANGE; |
| 338 | return 1; |
| 339 | } |
| 340 | break; |
| 341 | |
| 342 | case TLS_ST_CR_SESSION_TICKET: |
| 343 | if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { |
| 344 | st->hand_state = TLS_ST_CR_CHANGE; |
| 345 | return 1; |
| 346 | } |
| 347 | break; |
| 348 | |
| 349 | case TLS_ST_CR_CHANGE: |
| 350 | if (mt == SSL3_MT_FINISHED) { |
| 351 | st->hand_state = TLS_ST_CR_FINISHED; |
| 352 | return 1; |
| 353 | } |
| 354 | break; |
Matt Caswell | c7f4778 | 2017-01-10 23:02:28 +0000 | [diff] [blame] | 355 | |
| 356 | case TLS_ST_OK: |
Matt Caswell | 4004ce5 | 2017-02-25 00:06:49 +0000 | [diff] [blame] | 357 | if (mt == SSL3_MT_HELLO_REQUEST) { |
Matt Caswell | c7f4778 | 2017-01-10 23:02:28 +0000 | [diff] [blame] | 358 | st->hand_state = TLS_ST_CR_HELLO_REQ; |
| 359 | return 1; |
| 360 | } |
| 361 | break; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 362 | } |
| 363 | |
Matt Caswell | 672f333 | 2016-06-22 19:43:46 +0100 | [diff] [blame] | 364 | err: |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 365 | /* No valid transition found */ |
Matt Caswell | 672f333 | 2016-06-22 19:43:46 +0100 | [diff] [blame] | 366 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE); |
Richard Levitte | 340a282 | 2016-07-19 11:50:31 +0200 | [diff] [blame] | 367 | SSLerr(SSL_F_OSSL_STATEM_CLIENT_READ_TRANSITION, SSL_R_UNEXPECTED_MESSAGE); |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | /* |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 372 | * ossl_statem_client13_write_transition() works out what handshake state to |
| 373 | * move to next when the TLSv1.3 client is writing messages to be sent to the |
| 374 | * server. |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 375 | */ |
| 376 | static WRITE_TRAN ossl_statem_client13_write_transition(SSL *s) |
| 377 | { |
| 378 | OSSL_STATEM *st = &s->statem; |
| 379 | |
| 380 | /* |
Matt Caswell | 3847d42 | 2017-02-01 13:31:27 +0000 | [diff] [blame] | 381 | * Note: There are no cases for TLS_ST_BEFORE because we haven't negotiated |
| 382 | * TLSv1.3 yet at that point. They are handled by |
| 383 | * ossl_statem_client_write_transition(). |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 384 | */ |
| 385 | switch (st->hand_state) { |
| 386 | default: |
| 387 | /* Shouldn't happen */ |
| 388 | return WRITE_TRAN_ERROR; |
| 389 | |
Matt Caswell | 3847d42 | 2017-02-01 13:31:27 +0000 | [diff] [blame] | 390 | case TLS_ST_CW_CLNT_HELLO: |
| 391 | /* We only hit this in the case of HelloRetryRequest */ |
| 392 | return WRITE_TRAN_FINISHED; |
| 393 | |
| 394 | case TLS_ST_CR_HELLO_RETRY_REQUEST: |
| 395 | st->hand_state = TLS_ST_CW_CLNT_HELLO; |
| 396 | return WRITE_TRAN_CONTINUE; |
| 397 | |
Matt Caswell | 92760c2 | 2016-11-09 14:06:12 +0000 | [diff] [blame] | 398 | case TLS_ST_CR_FINISHED: |
Matt Caswell | ef6c191 | 2017-03-09 15:03:07 +0000 | [diff] [blame] | 399 | if (s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY |
| 400 | || s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING) |
Matt Caswell | d7f8783 | 2017-02-25 15:59:44 +0000 | [diff] [blame] | 401 | st->hand_state = TLS_ST_PENDING_EARLY_DATA_END; |
Matt Caswell | 564547e | 2017-02-25 15:34:07 +0000 | [diff] [blame] | 402 | else |
| 403 | st->hand_state = (s->s3->tmp.cert_req != 0) ? TLS_ST_CW_CERT |
| 404 | : TLS_ST_CW_FINISHED; |
| 405 | return WRITE_TRAN_CONTINUE; |
| 406 | |
Matt Caswell | d7f8783 | 2017-02-25 15:59:44 +0000 | [diff] [blame] | 407 | case TLS_ST_PENDING_EARLY_DATA_END: |
Matt Caswell | ef6c191 | 2017-03-09 15:03:07 +0000 | [diff] [blame] | 408 | if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) { |
| 409 | st->hand_state = TLS_ST_CW_END_OF_EARLY_DATA; |
| 410 | return WRITE_TRAN_CONTINUE; |
| 411 | } |
| 412 | /* Fall through */ |
| 413 | |
| 414 | case TLS_ST_CW_END_OF_EARLY_DATA: |
Matt Caswell | 94ed2c6 | 2016-11-14 14:53:31 +0000 | [diff] [blame] | 415 | st->hand_state = (s->s3->tmp.cert_req != 0) ? TLS_ST_CW_CERT |
Matt Caswell | 92760c2 | 2016-11-09 14:06:12 +0000 | [diff] [blame] | 416 | : TLS_ST_CW_FINISHED; |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 417 | return WRITE_TRAN_CONTINUE; |
| 418 | |
| 419 | case TLS_ST_CW_CERT: |
| 420 | /* If a non-empty Certificate we also send CertificateVerify */ |
Matt Caswell | 94ed2c6 | 2016-11-14 14:53:31 +0000 | [diff] [blame] | 421 | st->hand_state = (s->s3->tmp.cert_req == 1) ? TLS_ST_CW_CERT_VRFY |
Matt Caswell | 92760c2 | 2016-11-09 14:06:12 +0000 | [diff] [blame] | 422 | : TLS_ST_CW_FINISHED; |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 423 | return WRITE_TRAN_CONTINUE; |
| 424 | |
| 425 | case TLS_ST_CW_CERT_VRFY: |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 426 | st->hand_state = TLS_ST_CW_FINISHED; |
| 427 | return WRITE_TRAN_CONTINUE; |
| 428 | |
Matt Caswell | e1c3de4 | 2017-02-09 12:07:31 +0000 | [diff] [blame] | 429 | case TLS_ST_CR_KEY_UPDATE: |
Matt Caswell | 5bf4793 | 2017-02-09 16:00:12 +0000 | [diff] [blame] | 430 | if (s->key_update != SSL_KEY_UPDATE_NONE) { |
| 431 | st->hand_state = TLS_ST_CW_KEY_UPDATE; |
| 432 | return WRITE_TRAN_CONTINUE; |
| 433 | } |
| 434 | /* Fall through */ |
| 435 | |
Matt Caswell | 9412b3a | 2017-02-09 13:12:00 +0000 | [diff] [blame] | 436 | case TLS_ST_CW_KEY_UPDATE: |
Matt Caswell | cc2455b | 2017-01-11 17:18:19 +0000 | [diff] [blame] | 437 | case TLS_ST_CR_SESSION_TICKET: |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 438 | case TLS_ST_CW_FINISHED: |
Matt Caswell | 94ed2c6 | 2016-11-14 14:53:31 +0000 | [diff] [blame] | 439 | st->hand_state = TLS_ST_OK; |
Matt Caswell | 94ed2c6 | 2016-11-14 14:53:31 +0000 | [diff] [blame] | 440 | return WRITE_TRAN_CONTINUE; |
Matt Caswell | cc2455b | 2017-01-11 17:18:19 +0000 | [diff] [blame] | 441 | |
| 442 | case TLS_ST_OK: |
Matt Caswell | 9412b3a | 2017-02-09 13:12:00 +0000 | [diff] [blame] | 443 | if (s->key_update != SSL_KEY_UPDATE_NONE) { |
| 444 | st->hand_state = TLS_ST_CW_KEY_UPDATE; |
| 445 | return WRITE_TRAN_CONTINUE; |
| 446 | } |
| 447 | |
| 448 | /* Try to read from the server instead */ |
Matt Caswell | cc2455b | 2017-01-11 17:18:19 +0000 | [diff] [blame] | 449 | return WRITE_TRAN_FINISHED; |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
| 453 | /* |
| 454 | * ossl_statem_client_write_transition() works out what handshake state to |
| 455 | * move to next when the client is writing messages to be sent to the server. |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 456 | */ |
Matt Caswell | 8481f58 | 2015-10-26 11:54:17 +0000 | [diff] [blame] | 457 | WRITE_TRAN ossl_statem_client_write_transition(SSL *s) |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 458 | { |
Matt Caswell | d6f1a6e | 2015-10-05 10:58:52 +0100 | [diff] [blame] | 459 | OSSL_STATEM *st = &s->statem; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 460 | |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 461 | /* |
| 462 | * Note that immediately before/after a ClientHello we don't know what |
| 463 | * version we are going to negotiate yet, so we don't take this branch until |
| 464 | * later |
| 465 | */ |
Matt Caswell | f5ca0b0 | 2016-11-21 12:10:35 +0000 | [diff] [blame] | 466 | if (SSL_IS_TLS13(s)) |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 467 | return ossl_statem_client13_write_transition(s); |
| 468 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 469 | switch (st->hand_state) { |
Rich Salz | f3b3d7f | 2016-08-30 13:31:18 -0400 | [diff] [blame] | 470 | default: |
| 471 | /* Shouldn't happen */ |
| 472 | return WRITE_TRAN_ERROR; |
| 473 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 474 | case TLS_ST_OK: |
Matt Caswell | c7f4778 | 2017-01-10 23:02:28 +0000 | [diff] [blame] | 475 | if (!s->renegotiate) { |
| 476 | /* |
| 477 | * We haven't requested a renegotiation ourselves so we must have |
| 478 | * received a message from the server. Better read it. |
| 479 | */ |
| 480 | return WRITE_TRAN_FINISHED; |
| 481 | } |
Bernd Edlinger | 018fcbe | 2017-05-11 16:21:37 +0200 | [diff] [blame] | 482 | /* Renegotiation */ |
| 483 | /* fall thru */ |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 484 | case TLS_ST_BEFORE: |
| 485 | st->hand_state = TLS_ST_CW_CLNT_HELLO; |
| 486 | return WRITE_TRAN_CONTINUE; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 487 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 488 | case TLS_ST_CW_CLNT_HELLO: |
Matt Caswell | 49e7fe1 | 2017-02-21 09:22:22 +0000 | [diff] [blame] | 489 | if (s->early_data_state == SSL_EARLY_DATA_CONNECTING) { |
| 490 | /* |
| 491 | * We are assuming this is a TLSv1.3 connection, although we haven't |
| 492 | * actually selected a version yet. |
| 493 | */ |
Matt Caswell | d7f8783 | 2017-02-25 15:59:44 +0000 | [diff] [blame] | 494 | st->hand_state = TLS_ST_EARLY_DATA; |
Matt Caswell | 49e7fe1 | 2017-02-21 09:22:22 +0000 | [diff] [blame] | 495 | return WRITE_TRAN_CONTINUE; |
| 496 | } |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 497 | /* |
| 498 | * No transition at the end of writing because we don't know what |
| 499 | * we will be sent |
| 500 | */ |
| 501 | return WRITE_TRAN_FINISHED; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 502 | |
Matt Caswell | d7f8783 | 2017-02-25 15:59:44 +0000 | [diff] [blame] | 503 | case TLS_ST_EARLY_DATA: |
Matt Caswell | 4004ce5 | 2017-02-25 00:06:49 +0000 | [diff] [blame] | 504 | return WRITE_TRAN_FINISHED; |
| 505 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 506 | case DTLS_ST_CR_HELLO_VERIFY_REQUEST: |
| 507 | st->hand_state = TLS_ST_CW_CLNT_HELLO; |
| 508 | return WRITE_TRAN_CONTINUE; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 509 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 510 | case TLS_ST_CR_SRVR_DONE: |
| 511 | if (s->s3->tmp.cert_req) |
| 512 | st->hand_state = TLS_ST_CW_CERT; |
| 513 | else |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 514 | st->hand_state = TLS_ST_CW_KEY_EXCH; |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 515 | return WRITE_TRAN_CONTINUE; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 516 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 517 | case TLS_ST_CW_CERT: |
| 518 | st->hand_state = TLS_ST_CW_KEY_EXCH; |
| 519 | return WRITE_TRAN_CONTINUE; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 520 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 521 | case TLS_ST_CW_KEY_EXCH: |
| 522 | /* |
| 523 | * For TLS, cert_req is set to 2, so a cert chain of nothing is |
| 524 | * sent, but no verify packet is sent |
| 525 | */ |
| 526 | /* |
| 527 | * XXX: For now, we do not support client authentication in ECDH |
| 528 | * cipher suites with ECDH (rather than ECDSA) certificates. We |
| 529 | * need to skip the certificate verify message when client's |
| 530 | * ECDH public key is sent inside the client certificate. |
| 531 | */ |
| 532 | if (s->s3->tmp.cert_req == 1) { |
| 533 | st->hand_state = TLS_ST_CW_CERT_VRFY; |
| 534 | } else { |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 535 | st->hand_state = TLS_ST_CW_CHANGE; |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 536 | } |
| 537 | if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) { |
| 538 | st->hand_state = TLS_ST_CW_CHANGE; |
| 539 | } |
| 540 | return WRITE_TRAN_CONTINUE; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 541 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 542 | case TLS_ST_CW_CERT_VRFY: |
| 543 | st->hand_state = TLS_ST_CW_CHANGE; |
| 544 | return WRITE_TRAN_CONTINUE; |
| 545 | |
| 546 | case TLS_ST_CW_CHANGE: |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 547 | #if defined(OPENSSL_NO_NEXTPROTONEG) |
Matt Caswell | 4004ce5 | 2017-02-25 00:06:49 +0000 | [diff] [blame] | 548 | st-> |
| 549 | hand_state = TLS_ST_CW_FINISHED; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 550 | #else |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 551 | if (!SSL_IS_DTLS(s) && s->s3->npn_seen) |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 552 | st->hand_state = TLS_ST_CW_NEXT_PROTO; |
| 553 | else |
| 554 | st->hand_state = TLS_ST_CW_FINISHED; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 555 | #endif |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 556 | return WRITE_TRAN_CONTINUE; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 557 | |
| 558 | #if !defined(OPENSSL_NO_NEXTPROTONEG) |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 559 | case TLS_ST_CW_NEXT_PROTO: |
| 560 | st->hand_state = TLS_ST_CW_FINISHED; |
| 561 | return WRITE_TRAN_CONTINUE; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 562 | #endif |
| 563 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 564 | case TLS_ST_CW_FINISHED: |
| 565 | if (s->hit) { |
| 566 | st->hand_state = TLS_ST_OK; |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 567 | return WRITE_TRAN_CONTINUE; |
| 568 | } else { |
| 569 | return WRITE_TRAN_FINISHED; |
| 570 | } |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 571 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 572 | case TLS_ST_CR_FINISHED: |
| 573 | if (s->hit) { |
| 574 | st->hand_state = TLS_ST_CW_CHANGE; |
| 575 | return WRITE_TRAN_CONTINUE; |
| 576 | } else { |
| 577 | st->hand_state = TLS_ST_OK; |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 578 | return WRITE_TRAN_CONTINUE; |
| 579 | } |
Matt Caswell | c7f4778 | 2017-01-10 23:02:28 +0000 | [diff] [blame] | 580 | |
| 581 | case TLS_ST_CR_HELLO_REQ: |
| 582 | /* |
| 583 | * If we can renegotiate now then do so, otherwise wait for a more |
| 584 | * convenient time. |
| 585 | */ |
| 586 | if (ssl3_renegotiate_check(s, 1)) { |
| 587 | if (!tls_setup_handshake(s)) { |
| 588 | ossl_statem_set_error(s); |
| 589 | return WRITE_TRAN_ERROR; |
| 590 | } |
| 591 | st->hand_state = TLS_ST_CW_CLNT_HELLO; |
| 592 | return WRITE_TRAN_CONTINUE; |
| 593 | } |
| 594 | st->hand_state = TLS_ST_OK; |
Matt Caswell | c7f4778 | 2017-01-10 23:02:28 +0000 | [diff] [blame] | 595 | return WRITE_TRAN_CONTINUE; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | |
| 599 | /* |
| 600 | * Perform any pre work that needs to be done prior to sending a message from |
| 601 | * the client to the server. |
| 602 | */ |
Matt Caswell | 8481f58 | 2015-10-26 11:54:17 +0000 | [diff] [blame] | 603 | WORK_STATE ossl_statem_client_pre_work(SSL *s, WORK_STATE wst) |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 604 | { |
Matt Caswell | d6f1a6e | 2015-10-05 10:58:52 +0100 | [diff] [blame] | 605 | OSSL_STATEM *st = &s->statem; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 606 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 607 | switch (st->hand_state) { |
Rich Salz | f3b3d7f | 2016-08-30 13:31:18 -0400 | [diff] [blame] | 608 | default: |
| 609 | /* No pre work to be done */ |
| 610 | break; |
| 611 | |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 612 | case TLS_ST_CW_CLNT_HELLO: |
| 613 | s->shutdown = 0; |
| 614 | if (SSL_IS_DTLS(s)) { |
| 615 | /* every DTLS ClientHello resets Finished MAC */ |
Matt Caswell | 2c4a056 | 2016-06-03 11:59:19 +0100 | [diff] [blame] | 616 | if (!ssl3_init_finished_mac(s)) { |
| 617 | ossl_statem_set_error(s); |
| 618 | return WORK_ERROR; |
| 619 | } |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 620 | } |
| 621 | break; |
| 622 | |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 623 | case TLS_ST_CW_CHANGE: |
| 624 | if (SSL_IS_DTLS(s)) { |
| 625 | if (s->hit) { |
| 626 | /* |
| 627 | * We're into the last flight so we don't retransmit these |
| 628 | * messages unless we need to. |
| 629 | */ |
| 630 | st->use_timer = 0; |
| 631 | } |
| 632 | #ifndef OPENSSL_NO_SCTP |
| 633 | if (BIO_dgram_is_sctp(SSL_get_wbio(s))) |
| 634 | return dtls_wait_for_dry(s); |
| 635 | #endif |
| 636 | } |
Rich Salz | f3b3d7f | 2016-08-30 13:31:18 -0400 | [diff] [blame] | 637 | break; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 638 | |
Matt Caswell | d7f8783 | 2017-02-25 15:59:44 +0000 | [diff] [blame] | 639 | case TLS_ST_PENDING_EARLY_DATA_END: |
Matt Caswell | ef6c191 | 2017-03-09 15:03:07 +0000 | [diff] [blame] | 640 | /* |
| 641 | * If we've been called by SSL_do_handshake()/SSL_write(), or we did not |
| 642 | * attempt to write early data before calling SSL_read() then we press |
| 643 | * on with the handshake. Otherwise we pause here. |
| 644 | */ |
| 645 | if (s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING |
| 646 | || s->early_data_state == SSL_EARLY_DATA_NONE) |
| 647 | return WORK_FINISHED_CONTINUE; |
| 648 | /* Fall through */ |
| 649 | |
| 650 | case TLS_ST_EARLY_DATA: |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 651 | case TLS_ST_OK: |
Matt Caswell | 30f05b1 | 2017-01-13 09:19:10 +0000 | [diff] [blame] | 652 | return tls_finish_handshake(s, wst, 1); |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | return WORK_FINISHED_CONTINUE; |
| 656 | } |
| 657 | |
| 658 | /* |
| 659 | * Perform any work that needs to be done after sending a message from the |
| 660 | * client to the server. |
| 661 | */ |
Matt Caswell | 8481f58 | 2015-10-26 11:54:17 +0000 | [diff] [blame] | 662 | WORK_STATE ossl_statem_client_post_work(SSL *s, WORK_STATE wst) |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 663 | { |
Matt Caswell | d6f1a6e | 2015-10-05 10:58:52 +0100 | [diff] [blame] | 664 | OSSL_STATEM *st = &s->statem; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 665 | |
| 666 | s->init_num = 0; |
| 667 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 668 | switch (st->hand_state) { |
Rich Salz | f3b3d7f | 2016-08-30 13:31:18 -0400 | [diff] [blame] | 669 | default: |
| 670 | /* No post work to be done */ |
| 671 | break; |
| 672 | |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 673 | case TLS_ST_CW_CLNT_HELLO: |
Matt Caswell | 4641756 | 2016-05-17 12:28:14 +0100 | [diff] [blame] | 674 | if (wst == WORK_MORE_A && statem_flush(s) != 1) |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 675 | return WORK_MORE_A; |
Matt Caswell | 4641756 | 2016-05-17 12:28:14 +0100 | [diff] [blame] | 676 | |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 677 | if (SSL_IS_DTLS(s)) { |
| 678 | /* Treat the next message as the first packet */ |
| 679 | s->first_packet = 1; |
| 680 | } |
Matt Caswell | 6cb4226 | 2017-02-21 16:40:16 +0000 | [diff] [blame] | 681 | |
| 682 | if (s->early_data_state == SSL_EARLY_DATA_CONNECTING |
| 683 | && s->max_early_data > 0) { |
| 684 | /* |
| 685 | * We haven't selected TLSv1.3 yet so we don't call the change |
| 686 | * cipher state function associated with the SSL_METHOD. Instead |
| 687 | * we call tls13_change_cipher_state() directly. |
| 688 | */ |
| 689 | if (!tls13_change_cipher_state(s, |
| 690 | SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) |
Matt Caswell | 1ea4d09 | 2017-02-22 13:01:48 +0000 | [diff] [blame] | 691 | return WORK_ERROR; |
Matt Caswell | 6cb4226 | 2017-02-21 16:40:16 +0000 | [diff] [blame] | 692 | } |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 693 | break; |
| 694 | |
Matt Caswell | ef6c191 | 2017-03-09 15:03:07 +0000 | [diff] [blame] | 695 | case TLS_ST_CW_END_OF_EARLY_DATA: |
| 696 | /* |
| 697 | * We set the enc_write_ctx back to NULL because we may end up writing |
| 698 | * in cleartext again if we get a HelloRetryRequest from the server. |
| 699 | */ |
| 700 | EVP_CIPHER_CTX_free(s->enc_write_ctx); |
| 701 | s->enc_write_ctx = NULL; |
| 702 | break; |
| 703 | |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 704 | case TLS_ST_CW_KEY_EXCH: |
| 705 | if (tls_client_key_exchange_post_work(s) == 0) |
| 706 | return WORK_ERROR; |
| 707 | break; |
| 708 | |
| 709 | case TLS_ST_CW_CHANGE: |
| 710 | s->session->cipher = s->s3->tmp.new_cipher; |
| 711 | #ifdef OPENSSL_NO_COMP |
| 712 | s->session->compress_meth = 0; |
| 713 | #else |
| 714 | if (s->s3->tmp.new_compression == NULL) |
| 715 | s->session->compress_meth = 0; |
| 716 | else |
| 717 | s->session->compress_meth = s->s3->tmp.new_compression->id; |
| 718 | #endif |
| 719 | if (!s->method->ssl3_enc->setup_key_block(s)) |
| 720 | return WORK_ERROR; |
| 721 | |
| 722 | if (!s->method->ssl3_enc->change_cipher_state(s, |
| 723 | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) |
| 724 | return WORK_ERROR; |
| 725 | |
| 726 | if (SSL_IS_DTLS(s)) { |
| 727 | #ifndef OPENSSL_NO_SCTP |
| 728 | if (s->hit) { |
| 729 | /* |
| 730 | * Change to new shared key of SCTP-Auth, will be ignored if |
| 731 | * no SCTP used. |
| 732 | */ |
| 733 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, |
| 734 | 0, NULL); |
| 735 | } |
| 736 | #endif |
| 737 | |
| 738 | dtls1_reset_seq_numbers(s, SSL3_CC_WRITE); |
| 739 | } |
| 740 | break; |
| 741 | |
| 742 | case TLS_ST_CW_FINISHED: |
| 743 | #ifndef OPENSSL_NO_SCTP |
| 744 | if (wst == WORK_MORE_A && SSL_IS_DTLS(s) && s->hit == 0) { |
| 745 | /* |
| 746 | * Change to new shared key of SCTP-Auth, will be ignored if |
| 747 | * no SCTP used. |
| 748 | */ |
| 749 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, |
| 750 | 0, NULL); |
| 751 | } |
| 752 | #endif |
| 753 | if (statem_flush(s) != 1) |
| 754 | return WORK_MORE_B; |
Matt Caswell | 92760c2 | 2016-11-09 14:06:12 +0000 | [diff] [blame] | 755 | |
| 756 | if (SSL_IS_TLS13(s)) { |
| 757 | if (!s->method->ssl3_enc->change_cipher_state(s, |
| 758 | SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) |
| 759 | return WORK_ERROR; |
| 760 | } |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 761 | break; |
Matt Caswell | 9412b3a | 2017-02-09 13:12:00 +0000 | [diff] [blame] | 762 | |
| 763 | case TLS_ST_CW_KEY_UPDATE: |
| 764 | if (statem_flush(s) != 1) |
| 765 | return WORK_MORE_A; |
Matt Caswell | 57389a3 | 2017-02-10 17:43:09 +0000 | [diff] [blame] | 766 | if (!tls13_update_key(s, 1)) |
| 767 | return WORK_ERROR; |
Matt Caswell | 9412b3a | 2017-02-09 13:12:00 +0000 | [diff] [blame] | 768 | break; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | return WORK_FINISHED_CONTINUE; |
| 772 | } |
| 773 | |
| 774 | /* |
Matt Caswell | 6392fb8 | 2016-09-30 11:17:57 +0100 | [diff] [blame] | 775 | * Get the message construction function and message type for sending from the |
| 776 | * client |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 777 | * |
| 778 | * Valid return values are: |
| 779 | * 1: Success |
| 780 | * 0: Error |
| 781 | */ |
Matt Caswell | 6392fb8 | 2016-09-30 11:17:57 +0100 | [diff] [blame] | 782 | int ossl_statem_client_construct_message(SSL *s, WPACKET *pkt, |
Matt Caswell | a15c953 | 2016-10-03 15:35:17 +0100 | [diff] [blame] | 783 | confunc_f *confunc, int *mt) |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 784 | { |
Matt Caswell | d6f1a6e | 2015-10-05 10:58:52 +0100 | [diff] [blame] | 785 | OSSL_STATEM *st = &s->statem; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 786 | |
Matt Caswell | 4a01c59 | 2016-09-30 10:38:32 +0100 | [diff] [blame] | 787 | switch (st->hand_state) { |
| 788 | default: |
| 789 | /* Shouldn't happen */ |
| 790 | return 0; |
| 791 | |
| 792 | case TLS_ST_CW_CHANGE: |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 793 | if (SSL_IS_DTLS(s)) |
Matt Caswell | 6392fb8 | 2016-09-30 11:17:57 +0100 | [diff] [blame] | 794 | *confunc = dtls_construct_change_cipher_spec; |
Matt Caswell | 4a01c59 | 2016-09-30 10:38:32 +0100 | [diff] [blame] | 795 | else |
Matt Caswell | 6392fb8 | 2016-09-30 11:17:57 +0100 | [diff] [blame] | 796 | *confunc = tls_construct_change_cipher_spec; |
| 797 | *mt = SSL3_MT_CHANGE_CIPHER_SPEC; |
Matt Caswell | 4a01c59 | 2016-09-30 10:38:32 +0100 | [diff] [blame] | 798 | break; |
Matt Caswell | 5923ad4 | 2016-09-30 00:27:40 +0100 | [diff] [blame] | 799 | |
Matt Caswell | 4a01c59 | 2016-09-30 10:38:32 +0100 | [diff] [blame] | 800 | case TLS_ST_CW_CLNT_HELLO: |
Matt Caswell | 6392fb8 | 2016-09-30 11:17:57 +0100 | [diff] [blame] | 801 | *confunc = tls_construct_client_hello; |
| 802 | *mt = SSL3_MT_CLIENT_HELLO; |
Matt Caswell | 4a01c59 | 2016-09-30 10:38:32 +0100 | [diff] [blame] | 803 | break; |
Matt Caswell | 5923ad4 | 2016-09-30 00:27:40 +0100 | [diff] [blame] | 804 | |
Matt Caswell | ef6c191 | 2017-03-09 15:03:07 +0000 | [diff] [blame] | 805 | case TLS_ST_CW_END_OF_EARLY_DATA: |
| 806 | *confunc = tls_construct_end_of_early_data; |
| 807 | *mt = SSL3_MT_END_OF_EARLY_DATA; |
| 808 | break; |
| 809 | |
| 810 | case TLS_ST_PENDING_EARLY_DATA_END: |
| 811 | *confunc = NULL; |
| 812 | *mt = SSL3_MT_DUMMY; |
| 813 | break; |
| 814 | |
Matt Caswell | 4a01c59 | 2016-09-30 10:38:32 +0100 | [diff] [blame] | 815 | case TLS_ST_CW_CERT: |
Matt Caswell | 6392fb8 | 2016-09-30 11:17:57 +0100 | [diff] [blame] | 816 | *confunc = tls_construct_client_certificate; |
| 817 | *mt = SSL3_MT_CERTIFICATE; |
Matt Caswell | 4a01c59 | 2016-09-30 10:38:32 +0100 | [diff] [blame] | 818 | break; |
Matt Caswell | 5923ad4 | 2016-09-30 00:27:40 +0100 | [diff] [blame] | 819 | |
Matt Caswell | 4a01c59 | 2016-09-30 10:38:32 +0100 | [diff] [blame] | 820 | case TLS_ST_CW_KEY_EXCH: |
Matt Caswell | 6392fb8 | 2016-09-30 11:17:57 +0100 | [diff] [blame] | 821 | *confunc = tls_construct_client_key_exchange; |
| 822 | *mt = SSL3_MT_CLIENT_KEY_EXCHANGE; |
Matt Caswell | 4a01c59 | 2016-09-30 10:38:32 +0100 | [diff] [blame] | 823 | break; |
Matt Caswell | 5923ad4 | 2016-09-30 00:27:40 +0100 | [diff] [blame] | 824 | |
Matt Caswell | 4a01c59 | 2016-09-30 10:38:32 +0100 | [diff] [blame] | 825 | case TLS_ST_CW_CERT_VRFY: |
Matt Caswell | d8bc139 | 2016-12-05 14:59:25 +0000 | [diff] [blame] | 826 | *confunc = tls_construct_cert_verify; |
Matt Caswell | 6392fb8 | 2016-09-30 11:17:57 +0100 | [diff] [blame] | 827 | *mt = SSL3_MT_CERTIFICATE_VERIFY; |
Matt Caswell | 4a01c59 | 2016-09-30 10:38:32 +0100 | [diff] [blame] | 828 | break; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 829 | |
| 830 | #if !defined(OPENSSL_NO_NEXTPROTONEG) |
Matt Caswell | 4a01c59 | 2016-09-30 10:38:32 +0100 | [diff] [blame] | 831 | case TLS_ST_CW_NEXT_PROTO: |
Matt Caswell | 6392fb8 | 2016-09-30 11:17:57 +0100 | [diff] [blame] | 832 | *confunc = tls_construct_next_proto; |
| 833 | *mt = SSL3_MT_NEXT_PROTO; |
Matt Caswell | 4a01c59 | 2016-09-30 10:38:32 +0100 | [diff] [blame] | 834 | break; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 835 | #endif |
Matt Caswell | 4a01c59 | 2016-09-30 10:38:32 +0100 | [diff] [blame] | 836 | case TLS_ST_CW_FINISHED: |
Matt Caswell | 6392fb8 | 2016-09-30 11:17:57 +0100 | [diff] [blame] | 837 | *confunc = tls_construct_finished; |
| 838 | *mt = SSL3_MT_FINISHED; |
Matt Caswell | 4a01c59 | 2016-09-30 10:38:32 +0100 | [diff] [blame] | 839 | break; |
Matt Caswell | 9412b3a | 2017-02-09 13:12:00 +0000 | [diff] [blame] | 840 | |
| 841 | case TLS_ST_CW_KEY_UPDATE: |
| 842 | *confunc = tls_construct_key_update; |
| 843 | *mt = SSL3_MT_KEY_UPDATE; |
| 844 | break; |
Matt Caswell | 4a01c59 | 2016-09-30 10:38:32 +0100 | [diff] [blame] | 845 | } |
Matt Caswell | 5923ad4 | 2016-09-30 00:27:40 +0100 | [diff] [blame] | 846 | |
Matt Caswell | 5923ad4 | 2016-09-30 00:27:40 +0100 | [diff] [blame] | 847 | return 1; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | /* |
| 851 | * Returns the maximum allowed length for the current message that we are |
| 852 | * reading. Excludes the message header. |
| 853 | */ |
Matt Caswell | eda7575 | 2016-09-06 12:05:25 +0100 | [diff] [blame] | 854 | size_t ossl_statem_client_max_message_size(SSL *s) |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 855 | { |
Matt Caswell | d6f1a6e | 2015-10-05 10:58:52 +0100 | [diff] [blame] | 856 | OSSL_STATEM *st = &s->statem; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 857 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 858 | switch (st->hand_state) { |
Rich Salz | f3b3d7f | 2016-08-30 13:31:18 -0400 | [diff] [blame] | 859 | default: |
| 860 | /* Shouldn't happen */ |
| 861 | return 0; |
| 862 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 863 | case TLS_ST_CR_SRVR_HELLO: |
| 864 | return SERVER_HELLO_MAX_LENGTH; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 865 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 866 | case DTLS_ST_CR_HELLO_VERIFY_REQUEST: |
| 867 | return HELLO_VERIFY_REQUEST_MAX_LENGTH; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 868 | |
Matt Caswell | 3847d42 | 2017-02-01 13:31:27 +0000 | [diff] [blame] | 869 | case TLS_ST_CR_HELLO_RETRY_REQUEST: |
| 870 | return HELLO_RETRY_REQUEST_MAX_LENGTH; |
| 871 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 872 | case TLS_ST_CR_CERT: |
| 873 | return s->max_cert_list; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 874 | |
Matt Caswell | 2c5dfdc | 2016-12-05 17:04:51 +0000 | [diff] [blame] | 875 | case TLS_ST_CR_CERT_VRFY: |
| 876 | return SSL3_RT_MAX_PLAIN_LENGTH; |
| 877 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 878 | case TLS_ST_CR_CERT_STATUS: |
| 879 | return SSL3_RT_MAX_PLAIN_LENGTH; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 880 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 881 | case TLS_ST_CR_KEY_EXCH: |
| 882 | return SERVER_KEY_EXCH_MAX_LENGTH; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 883 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 884 | case TLS_ST_CR_CERT_REQ: |
| 885 | /* |
| 886 | * Set to s->max_cert_list for compatibility with previous releases. In |
| 887 | * practice these messages can get quite long if servers are configured |
| 888 | * to provide a long list of acceptable CAs |
| 889 | */ |
| 890 | return s->max_cert_list; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 891 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 892 | case TLS_ST_CR_SRVR_DONE: |
| 893 | return SERVER_HELLO_DONE_MAX_LENGTH; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 894 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 895 | case TLS_ST_CR_CHANGE: |
| 896 | if (s->version == DTLS1_BAD_VER) |
| 897 | return 3; |
| 898 | return CCS_MAX_LENGTH; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 899 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 900 | case TLS_ST_CR_SESSION_TICKET: |
| 901 | return SSL3_RT_MAX_PLAIN_LENGTH; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 902 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 903 | case TLS_ST_CR_FINISHED: |
| 904 | return FINISHED_MAX_LENGTH; |
Matt Caswell | e46f233 | 2016-11-23 15:20:22 +0000 | [diff] [blame] | 905 | |
| 906 | case TLS_ST_CR_ENCRYPTED_EXTENSIONS: |
| 907 | return ENCRYPTED_EXTENSIONS_MAX_LENGTH; |
Matt Caswell | e1c3de4 | 2017-02-09 12:07:31 +0000 | [diff] [blame] | 908 | |
| 909 | case TLS_ST_CR_KEY_UPDATE: |
| 910 | return KEY_UPDATE_MAX_LENGTH; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 911 | } |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | /* |
| 915 | * Process a message that the client has been received from the server. |
| 916 | */ |
Matt Caswell | 8481f58 | 2015-10-26 11:54:17 +0000 | [diff] [blame] | 917 | MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt) |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 918 | { |
Matt Caswell | d6f1a6e | 2015-10-05 10:58:52 +0100 | [diff] [blame] | 919 | OSSL_STATEM *st = &s->statem; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 920 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 921 | switch (st->hand_state) { |
Rich Salz | f3b3d7f | 2016-08-30 13:31:18 -0400 | [diff] [blame] | 922 | default: |
| 923 | /* Shouldn't happen */ |
| 924 | return MSG_PROCESS_ERROR; |
| 925 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 926 | case TLS_ST_CR_SRVR_HELLO: |
| 927 | return tls_process_server_hello(s, pkt); |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 928 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 929 | case DTLS_ST_CR_HELLO_VERIFY_REQUEST: |
| 930 | return dtls_process_hello_verify(s, pkt); |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 931 | |
Matt Caswell | 3847d42 | 2017-02-01 13:31:27 +0000 | [diff] [blame] | 932 | case TLS_ST_CR_HELLO_RETRY_REQUEST: |
| 933 | return tls_process_hello_retry_request(s, pkt); |
| 934 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 935 | case TLS_ST_CR_CERT: |
| 936 | return tls_process_server_certificate(s, pkt); |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 937 | |
Matt Caswell | 2c5dfdc | 2016-12-05 17:04:51 +0000 | [diff] [blame] | 938 | case TLS_ST_CR_CERT_VRFY: |
| 939 | return tls_process_cert_verify(s, pkt); |
| 940 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 941 | case TLS_ST_CR_CERT_STATUS: |
| 942 | return tls_process_cert_status(s, pkt); |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 943 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 944 | case TLS_ST_CR_KEY_EXCH: |
| 945 | return tls_process_key_exchange(s, pkt); |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 946 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 947 | case TLS_ST_CR_CERT_REQ: |
| 948 | return tls_process_certificate_request(s, pkt); |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 949 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 950 | case TLS_ST_CR_SRVR_DONE: |
| 951 | return tls_process_server_done(s, pkt); |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 952 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 953 | case TLS_ST_CR_CHANGE: |
| 954 | return tls_process_change_cipher_spec(s, pkt); |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 955 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 956 | case TLS_ST_CR_SESSION_TICKET: |
| 957 | return tls_process_new_session_ticket(s, pkt); |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 958 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 959 | case TLS_ST_CR_FINISHED: |
| 960 | return tls_process_finished(s, pkt); |
Matt Caswell | e46f233 | 2016-11-23 15:20:22 +0000 | [diff] [blame] | 961 | |
Matt Caswell | c7f4778 | 2017-01-10 23:02:28 +0000 | [diff] [blame] | 962 | case TLS_ST_CR_HELLO_REQ: |
| 963 | return tls_process_hello_req(s, pkt); |
| 964 | |
Matt Caswell | e46f233 | 2016-11-23 15:20:22 +0000 | [diff] [blame] | 965 | case TLS_ST_CR_ENCRYPTED_EXTENSIONS: |
| 966 | return tls_process_encrypted_extensions(s, pkt); |
Matt Caswell | e1c3de4 | 2017-02-09 12:07:31 +0000 | [diff] [blame] | 967 | |
| 968 | case TLS_ST_CR_KEY_UPDATE: |
| 969 | return tls_process_key_update(s, pkt); |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 970 | } |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | /* |
| 974 | * Perform any further processing required following the receipt of a message |
| 975 | * from the server |
| 976 | */ |
Matt Caswell | 8481f58 | 2015-10-26 11:54:17 +0000 | [diff] [blame] | 977 | WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst) |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 978 | { |
Matt Caswell | d6f1a6e | 2015-10-05 10:58:52 +0100 | [diff] [blame] | 979 | OSSL_STATEM *st = &s->statem; |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 980 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 981 | switch (st->hand_state) { |
Rich Salz | f3b3d7f | 2016-08-30 13:31:18 -0400 | [diff] [blame] | 982 | default: |
| 983 | /* Shouldn't happen */ |
| 984 | return WORK_ERROR; |
| 985 | |
Matt Caswell | 05c4f1d | 2016-06-22 14:31:32 +0100 | [diff] [blame] | 986 | case TLS_ST_CR_CERT_REQ: |
| 987 | return tls_prepare_client_certificate(s, wst); |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 988 | } |
Matt Caswell | 61ae935 | 2015-09-11 11:23:20 +0100 | [diff] [blame] | 989 | } |
| 990 | |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 991 | int tls_construct_client_hello(SSL *s, WPACKET *pkt) |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 992 | { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 993 | unsigned char *p; |
Matt Caswell | ec60ccc | 2016-10-04 20:31:19 +0100 | [diff] [blame] | 994 | size_t sess_id_len; |
| 995 | int i, protverr; |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 996 | int al = SSL_AD_HANDSHAKE_FAILURE; |
Dr. Stephen Henson | 09b6c2e | 2005-09-30 23:35:33 +0000 | [diff] [blame] | 997 | #ifndef OPENSSL_NO_COMP |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 998 | SSL_COMP *comp; |
Dr. Stephen Henson | 09b6c2e | 2005-09-30 23:35:33 +0000 | [diff] [blame] | 999 | #endif |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1000 | SSL_SESSION *sess = s->session; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1001 | |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 1002 | if (!WPACKET_set_max_size(pkt, SSL3_RT_MAX_PLAIN_LENGTH)) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1003 | /* Should not happen */ |
| 1004 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 1005 | return 0; |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1006 | } |
Matt Caswell | 13c9bb3 | 2015-03-31 00:18:31 +0100 | [diff] [blame] | 1007 | |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1008 | /* Work out what SSL/TLS/DTLS version to use */ |
Viktor Dukhovni | 4fa5214 | 2015-12-29 03:24:17 -0500 | [diff] [blame] | 1009 | protverr = ssl_set_client_hello_version(s); |
| 1010 | if (protverr != 0) { |
| 1011 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, protverr); |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 1012 | return 0; |
Viktor Dukhovni | 4fa5214 | 2015-12-29 03:24:17 -0500 | [diff] [blame] | 1013 | } |
Matt Caswell | 13c9bb3 | 2015-03-31 00:18:31 +0100 | [diff] [blame] | 1014 | |
Matt Caswell | e586eac | 2017-03-21 13:50:31 +0000 | [diff] [blame] | 1015 | if (sess == NULL |
| 1016 | || !ssl_version_supported(s, sess->ssl_version) |
| 1017 | || !SSL_SESSION_is_resumable(sess)) { |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1018 | if (!ssl_get_new_session(s, 0)) |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 1019 | return 0; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1020 | } |
| 1021 | /* else use the pre-loaded session */ |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1022 | |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1023 | p = s->s3->client_random; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1024 | |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1025 | /* |
| 1026 | * for DTLS if client_random is initialized, reuse it, we are |
| 1027 | * required to use same upon reply to HelloVerify |
| 1028 | */ |
| 1029 | if (SSL_IS_DTLS(s)) { |
| 1030 | size_t idx; |
| 1031 | i = 1; |
| 1032 | for (idx = 0; idx < sizeof(s->s3->client_random); idx++) { |
| 1033 | if (p[idx]) { |
| 1034 | i = 0; |
| 1035 | break; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1036 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1037 | } |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1038 | } else |
| 1039 | i = 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1040 | |
Matt Caswell | f7f2a01 | 2017-03-22 08:52:54 +0000 | [diff] [blame] | 1041 | if (i && ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random), |
| 1042 | DOWNGRADE_NONE) <= 0) |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 1043 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1044 | |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1045 | /*- |
| 1046 | * version indicates the negotiated version: for example from |
| 1047 | * an SSLv2/v3 compatible client hello). The client_version |
| 1048 | * field is the maximum version we permit and it is also |
| 1049 | * used in RSA encrypted premaster secrets. Some servers can |
| 1050 | * choke if we initially report a higher version then |
| 1051 | * renegotiate to a lower one in the premaster secret. This |
| 1052 | * didn't happen with TLS 1.0 as most servers supported it |
| 1053 | * but it can with TLS 1.1 or later if the server only supports |
| 1054 | * 1.0. |
| 1055 | * |
| 1056 | * Possible scenario with previous logic: |
| 1057 | * 1. Client hello indicates TLS 1.2 |
| 1058 | * 2. Server hello says TLS 1.0 |
| 1059 | * 3. RSA encrypted premaster secret uses 1.2. |
FdaSilvaYY | 8483a00 | 2016-03-10 21:34:48 +0100 | [diff] [blame] | 1060 | * 4. Handshake proceeds using TLS 1.0. |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1061 | * 5. Server sends hello request to renegotiate. |
| 1062 | * 6. Client hello indicates TLS v1.0 as we now |
| 1063 | * know that is maximum server supports. |
| 1064 | * 7. Server chokes on RSA encrypted premaster secret |
| 1065 | * containing version 1.0. |
| 1066 | * |
| 1067 | * For interoperability it should be OK to always use the |
| 1068 | * maximum version we support in client hello and then rely |
| 1069 | * on the checking of version to ensure the servers isn't |
| 1070 | * being inconsistent: for example initially negotiating with |
| 1071 | * TLS 1.0 and renegotiating with TLS 1.2. We do this by using |
| 1072 | * client_version in client hello and not resetting it to |
| 1073 | * the negotiated version. |
Matt Caswell | cd99883 | 2016-10-23 00:41:11 +0100 | [diff] [blame] | 1074 | * |
| 1075 | * For TLS 1.3 we always set the ClientHello version to 1.2 and rely on the |
Matt Caswell | 16bce0e | 2016-10-31 17:05:20 +0000 | [diff] [blame] | 1076 | * supported_versions extension for the real supported versions. |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1077 | */ |
Matt Caswell | 7acb8b6 | 2016-11-23 13:56:15 +0000 | [diff] [blame] | 1078 | if (!WPACKET_put_bytes_u16(pkt, s->client_version) |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 1079 | || !WPACKET_memcpy(pkt, s->s3->client_random, SSL3_RANDOM_SIZE)) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1080 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 1081 | return 0; |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1082 | } |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1083 | |
| 1084 | /* Session ID */ |
Matt Caswell | f05bcf0 | 2017-01-13 13:49:44 +0000 | [diff] [blame] | 1085 | if (s->new_session || s->session->ssl_version == TLS1_3_VERSION) |
Matt Caswell | ec60ccc | 2016-10-04 20:31:19 +0100 | [diff] [blame] | 1086 | sess_id_len = 0; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1087 | else |
Matt Caswell | ec60ccc | 2016-10-04 20:31:19 +0100 | [diff] [blame] | 1088 | sess_id_len = s->session->session_id_length; |
| 1089 | if (sess_id_len > sizeof(s->session->session_id) |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 1090 | || !WPACKET_start_sub_packet_u8(pkt) |
Matt Caswell | ec60ccc | 2016-10-04 20:31:19 +0100 | [diff] [blame] | 1091 | || (sess_id_len != 0 && !WPACKET_memcpy(pkt, s->session->session_id, |
| 1092 | sess_id_len)) |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 1093 | || !WPACKET_close(pkt)) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1094 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 1095 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1098 | /* cookie stuff for DTLS */ |
| 1099 | if (SSL_IS_DTLS(s)) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1100 | if (s->d1->cookie_len > sizeof(s->d1->cookie) |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 1101 | || !WPACKET_sub_memcpy_u8(pkt, s->d1->cookie, |
Matt Caswell | b2b3024 | 2016-09-13 11:32:52 +0100 | [diff] [blame] | 1102 | s->d1->cookie_len)) { |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1103 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 1104 | return 0; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1105 | } |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | /* Ciphers supported */ |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 1109 | if (!WPACKET_start_sub_packet_u16(pkt)) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1110 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 1111 | return 0; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1112 | } |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1113 | /* ssl_cipher_list_to_bytes() raises SSLerr if appropriate */ |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 1114 | if (!ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), pkt)) |
| 1115 | return 0; |
| 1116 | if (!WPACKET_close(pkt)) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1117 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 1118 | return 0; |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1119 | } |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1120 | |
| 1121 | /* COMPRESSION */ |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 1122 | if (!WPACKET_start_sub_packet_u8(pkt)) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1123 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 1124 | return 0; |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1125 | } |
| 1126 | #ifndef OPENSSL_NO_COMP |
Matt Caswell | c19602b | 2017-03-01 10:36:38 +0000 | [diff] [blame] | 1127 | if (ssl_allow_compression(s) |
| 1128 | && s->ctx->comp_methods |
| 1129 | && (SSL_IS_DTLS(s) || s->s3->tmp.max_ver < TLS1_3_VERSION)) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1130 | int compnum = sk_SSL_COMP_num(s->ctx->comp_methods); |
| 1131 | for (i = 0; i < compnum; i++) { |
| 1132 | comp = sk_SSL_COMP_value(s->ctx->comp_methods, i); |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 1133 | if (!WPACKET_put_bytes_u8(pkt, comp->id)) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1134 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 1135 | return 0; |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1136 | } |
| 1137 | } |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1138 | } |
| 1139 | #endif |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1140 | /* Add the NULL method */ |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 1141 | if (!WPACKET_put_bytes_u8(pkt, 0) || !WPACKET_close(pkt)) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1142 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 1143 | return 0; |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1144 | } |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1145 | |
| 1146 | /* TLS extensions */ |
Matt Caswell | fe874d2 | 2017-04-04 11:40:02 +0100 | [diff] [blame] | 1147 | if (!tls_construct_extensions(s, pkt, SSL_EXT_CLIENT_HELLO, NULL, 0, &al)) { |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1148 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 1149 | return 0; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1150 | } |
| 1151 | |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1152 | return 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1153 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1154 | |
Matt Caswell | be3583f | 2015-10-26 11:46:33 +0000 | [diff] [blame] | 1155 | MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt) |
Matt Caswell | 8ba708e | 2015-09-11 10:48:59 +0100 | [diff] [blame] | 1156 | { |
| 1157 | int al; |
Matt Caswell | cb150cb | 2016-10-04 21:04:03 +0100 | [diff] [blame] | 1158 | size_t cookie_len; |
Matt Caswell | 8ba708e | 2015-09-11 10:48:59 +0100 | [diff] [blame] | 1159 | PACKET cookiepkt; |
| 1160 | |
| 1161 | if (!PACKET_forward(pkt, 2) |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1162 | || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) { |
Matt Caswell | 8ba708e | 2015-09-11 10:48:59 +0100 | [diff] [blame] | 1163 | al = SSL_AD_DECODE_ERROR; |
| 1164 | SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH); |
| 1165 | goto f_err; |
| 1166 | } |
| 1167 | |
| 1168 | cookie_len = PACKET_remaining(&cookiepkt); |
| 1169 | if (cookie_len > sizeof(s->d1->cookie)) { |
| 1170 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1171 | SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_TOO_LONG); |
| 1172 | goto f_err; |
| 1173 | } |
| 1174 | |
| 1175 | if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) { |
| 1176 | al = SSL_AD_DECODE_ERROR; |
| 1177 | SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH); |
| 1178 | goto f_err; |
| 1179 | } |
| 1180 | s->d1->cookie_len = cookie_len; |
| 1181 | |
| 1182 | return MSG_PROCESS_FINISHED_READING; |
| 1183 | f_err: |
| 1184 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
Matt Caswell | fe3a329 | 2015-10-05 10:39:54 +0100 | [diff] [blame] | 1185 | ossl_statem_set_error(s); |
Matt Caswell | 8ba708e | 2015-09-11 10:48:59 +0100 | [diff] [blame] | 1186 | return MSG_PROCESS_ERROR; |
| 1187 | } |
| 1188 | |
Matt Caswell | 11c67ee | 2017-03-13 15:21:15 +0000 | [diff] [blame] | 1189 | static int set_client_ciphersuite(SSL *s, const unsigned char *cipherchars) |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1190 | { |
| 1191 | STACK_OF(SSL_CIPHER) *sk; |
| 1192 | const SSL_CIPHER *c; |
Matt Caswell | 11c67ee | 2017-03-13 15:21:15 +0000 | [diff] [blame] | 1193 | int i; |
| 1194 | |
| 1195 | c = ssl_get_cipher_by_char(s, cipherchars, 0); |
| 1196 | if (c == NULL) { |
| 1197 | /* unknown cipher */ |
| 1198 | SSLerr(SSL_F_SET_CLIENT_CIPHERSUITE, SSL_R_UNKNOWN_CIPHER_RETURNED); |
| 1199 | return 0; |
| 1200 | } |
| 1201 | /* |
| 1202 | * If it is a disabled cipher we either didn't send it in client hello, |
| 1203 | * or it's not allowed for the selected protocol. So we return an error. |
| 1204 | */ |
Matt Caswell | 8af91fd | 2017-04-12 17:02:42 +0100 | [diff] [blame] | 1205 | if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK, 1)) { |
Matt Caswell | 11c67ee | 2017-03-13 15:21:15 +0000 | [diff] [blame] | 1206 | SSLerr(SSL_F_SET_CLIENT_CIPHERSUITE, SSL_R_WRONG_CIPHER_RETURNED); |
| 1207 | return 0; |
| 1208 | } |
| 1209 | |
| 1210 | sk = ssl_get_ciphers_by_id(s); |
| 1211 | i = sk_SSL_CIPHER_find(sk, c); |
| 1212 | if (i < 0) { |
| 1213 | /* we did not say we would use this cipher */ |
| 1214 | SSLerr(SSL_F_SET_CLIENT_CIPHERSUITE, SSL_R_WRONG_CIPHER_RETURNED); |
| 1215 | return 0; |
| 1216 | } |
| 1217 | |
| 1218 | if (SSL_IS_TLS13(s) && s->s3->tmp.new_cipher != NULL |
| 1219 | && s->s3->tmp.new_cipher->id != c->id) { |
| 1220 | /* ServerHello selected a different ciphersuite to that in the HRR */ |
| 1221 | SSLerr(SSL_F_SET_CLIENT_CIPHERSUITE, SSL_R_WRONG_CIPHER_RETURNED); |
| 1222 | return 0; |
| 1223 | } |
| 1224 | |
| 1225 | /* |
| 1226 | * Depending on the session caching (internal/external), the cipher |
| 1227 | * and/or cipher_id values may not be set. Make sure that cipher_id is |
| 1228 | * set and use it for comparison. |
| 1229 | */ |
| 1230 | if (s->session->cipher != NULL) |
| 1231 | s->session->cipher_id = s->session->cipher->id; |
| 1232 | if (s->hit && (s->session->cipher_id != c->id)) { |
Matt Caswell | a055a88 | 2017-05-26 17:59:34 +0100 | [diff] [blame] | 1233 | if (SSL_IS_TLS13(s)) { |
| 1234 | /* |
| 1235 | * In TLSv1.3 it is valid for the server to select a different |
| 1236 | * ciphersuite as long as the hash is the same. |
| 1237 | */ |
| 1238 | if (ssl_md(c->algorithm2) |
| 1239 | != ssl_md(s->session->cipher->algorithm2)) { |
| 1240 | SSLerr(SSL_F_SET_CLIENT_CIPHERSUITE, |
| 1241 | SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED); |
| 1242 | return 0; |
| 1243 | } |
| 1244 | } else { |
| 1245 | /* |
| 1246 | * Prior to TLSv1.3 resuming a session always meant using the same |
| 1247 | * ciphersuite. |
| 1248 | */ |
| 1249 | SSLerr(SSL_F_SET_CLIENT_CIPHERSUITE, |
| 1250 | SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); |
| 1251 | return 0; |
| 1252 | } |
Matt Caswell | 11c67ee | 2017-03-13 15:21:15 +0000 | [diff] [blame] | 1253 | } |
| 1254 | s->s3->tmp.new_cipher = c; |
| 1255 | |
| 1256 | return 1; |
| 1257 | } |
| 1258 | |
| 1259 | MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt) |
| 1260 | { |
Matt Caswell | 332eb39 | 2016-11-28 16:15:51 +0000 | [diff] [blame] | 1261 | PACKET session_id, extpkt; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1262 | size_t session_id_len; |
Emilia Kasper | b698174 | 2016-02-01 15:26:18 +0100 | [diff] [blame] | 1263 | const unsigned char *cipherchars; |
Matt Caswell | 11c67ee | 2017-03-13 15:21:15 +0000 | [diff] [blame] | 1264 | int al = SSL_AD_INTERNAL_ERROR; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1265 | unsigned int compression; |
Viktor Dukhovni | 4fa5214 | 2015-12-29 03:24:17 -0500 | [diff] [blame] | 1266 | unsigned int sversion; |
Matt Caswell | 3434f40 | 2016-11-28 16:45:52 +0000 | [diff] [blame] | 1267 | unsigned int context; |
Viktor Dukhovni | 4fa5214 | 2015-12-29 03:24:17 -0500 | [diff] [blame] | 1268 | int protverr; |
Matt Caswell | 332eb39 | 2016-11-28 16:15:51 +0000 | [diff] [blame] | 1269 | RAW_EXTENSION *extensions = NULL; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1270 | #ifndef OPENSSL_NO_COMP |
| 1271 | SSL_COMP *comp; |
| 1272 | #endif |
| 1273 | |
Viktor Dukhovni | 4fa5214 | 2015-12-29 03:24:17 -0500 | [diff] [blame] | 1274 | if (!PACKET_get_net_2(pkt, &sversion)) { |
| 1275 | al = SSL_AD_DECODE_ERROR; |
| 1276 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); |
| 1277 | goto f_err; |
| 1278 | } |
Kurt Roeckx | 7946ab3 | 2015-12-06 17:56:41 +0100 | [diff] [blame] | 1279 | |
Matt Caswell | c3043dc | 2017-03-22 11:50:32 +0000 | [diff] [blame] | 1280 | /* load the server random */ |
| 1281 | if (!PACKET_copy_bytes(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) { |
| 1282 | al = SSL_AD_DECODE_ERROR; |
| 1283 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); |
| 1284 | goto f_err; |
| 1285 | } |
| 1286 | |
| 1287 | /* |
| 1288 | * We do this immediately so we know what format the ServerHello is in. |
| 1289 | * Must be done after reading the random data so we can check for the |
| 1290 | * TLSv1.3 downgrade sentinels |
| 1291 | */ |
| 1292 | protverr = ssl_choose_client_version(s, sversion, 1, &al); |
Viktor Dukhovni | 4fa5214 | 2015-12-29 03:24:17 -0500 | [diff] [blame] | 1293 | if (protverr != 0) { |
Viktor Dukhovni | 4fa5214 | 2015-12-29 03:24:17 -0500 | [diff] [blame] | 1294 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, protverr); |
| 1295 | goto f_err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1296 | } |
Emilia Kasper | 7b3ba508 | 2014-11-19 15:56:27 +0100 | [diff] [blame] | 1297 | |
Matt Caswell | 524420d | 2017-03-07 10:21:58 +0000 | [diff] [blame] | 1298 | /* |
| 1299 | * In TLSv1.3 a ServerHello message signals a key change so the end of the |
| 1300 | * message must be on a record boundary. |
| 1301 | */ |
| 1302 | if (SSL_IS_TLS13(s) && RECORD_LAYER_processed_read_pending(&s->rlayer)) { |
| 1303 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 1304 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_NOT_ON_RECORD_BOUNDARY); |
| 1305 | goto f_err; |
| 1306 | } |
| 1307 | |
Emilia Kasper | fc5ce51 | 2015-09-18 14:09:37 +0200 | [diff] [blame] | 1308 | /* Get the session-id. */ |
Matt Caswell | 71728dd | 2016-11-07 13:50:43 +0000 | [diff] [blame] | 1309 | if (!SSL_IS_TLS13(s)) { |
| 1310 | if (!PACKET_get_length_prefixed_1(pkt, &session_id)) { |
| 1311 | al = SSL_AD_DECODE_ERROR; |
| 1312 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); |
| 1313 | goto f_err; |
| 1314 | } |
| 1315 | session_id_len = PACKET_remaining(&session_id); |
| 1316 | if (session_id_len > sizeof s->session->session_id |
| 1317 | || session_id_len > SSL3_SESSION_ID_SIZE) { |
| 1318 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1319 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
| 1320 | SSL_R_SSL3_SESSION_ID_TOO_LONG); |
| 1321 | goto f_err; |
| 1322 | } |
| 1323 | } else { |
Matt Caswell | 625b0d5 | 2016-12-08 09:48:29 +0000 | [diff] [blame] | 1324 | PACKET_null_init(&session_id); |
Matt Caswell | 71728dd | 2016-11-07 13:50:43 +0000 | [diff] [blame] | 1325 | session_id_len = 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1326 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1327 | |
Matt Caswell | 73999b6 | 2015-09-10 10:22:30 +0100 | [diff] [blame] | 1328 | if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) { |
Matt Caswell | f0659bd | 2015-10-22 14:02:46 +0100 | [diff] [blame] | 1329 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); |
Emilia Kasper | fc5ce51 | 2015-09-18 14:09:37 +0200 | [diff] [blame] | 1330 | al = SSL_AD_DECODE_ERROR; |
| 1331 | goto f_err; |
| 1332 | } |
| 1333 | |
Matt Caswell | 4ff65f7 | 2017-01-18 09:38:53 +0000 | [diff] [blame] | 1334 | if (!SSL_IS_TLS13(s)) { |
| 1335 | if (!PACKET_get_1(pkt, &compression)) { |
| 1336 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); |
| 1337 | al = SSL_AD_DECODE_ERROR; |
Emilia Kasper | 6e3d015 | 2015-04-21 18:12:58 +0200 | [diff] [blame] | 1338 | goto f_err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1339 | } |
Matt Caswell | 4ff65f7 | 2017-01-18 09:38:53 +0000 | [diff] [blame] | 1340 | } else { |
| 1341 | compression = 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1342 | } |
Dr. Stephen Henson | 12bf56c | 2008-11-15 17:18:12 +0000 | [diff] [blame] | 1343 | |
Matt Caswell | 4ff65f7 | 2017-01-18 09:38:53 +0000 | [diff] [blame] | 1344 | /* TLS extensions */ |
| 1345 | if (PACKET_remaining(pkt) == 0) { |
| 1346 | PACKET_null_init(&extpkt); |
Matt Caswell | 26b9172 | 2017-05-11 11:31:57 +0100 | [diff] [blame] | 1347 | } else if (!PACKET_as_length_prefixed_2(pkt, &extpkt) |
| 1348 | || PACKET_remaining(pkt) != 0) { |
Matt Caswell | 4ff65f7 | 2017-01-18 09:38:53 +0000 | [diff] [blame] | 1349 | al = SSL_AD_DECODE_ERROR; |
| 1350 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_LENGTH); |
| 1351 | goto f_err; |
| 1352 | } |
| 1353 | |
Matt Caswell | fe874d2 | 2017-04-04 11:40:02 +0100 | [diff] [blame] | 1354 | context = SSL_IS_TLS13(s) ? SSL_EXT_TLS1_3_SERVER_HELLO |
| 1355 | : SSL_EXT_TLS1_2_SERVER_HELLO; |
Tatsuhiro Tsujikawa | 735d5b5 | 2017-04-18 23:59:39 +0900 | [diff] [blame] | 1356 | if (!tls_collect_extensions(s, &extpkt, context, &extensions, &al, NULL, 1)) |
Matt Caswell | 4ff65f7 | 2017-01-18 09:38:53 +0000 | [diff] [blame] | 1357 | goto f_err; |
| 1358 | |
| 1359 | s->hit = 0; |
| 1360 | |
| 1361 | if (SSL_IS_TLS13(s)) { |
| 1362 | /* This will set s->hit if we are resuming */ |
| 1363 | if (!tls_parse_extension(s, TLSEXT_IDX_psk, |
Matt Caswell | fe874d2 | 2017-04-04 11:40:02 +0100 | [diff] [blame] | 1364 | SSL_EXT_TLS1_3_SERVER_HELLO, |
Matt Caswell | 4ff65f7 | 2017-01-18 09:38:53 +0000 | [diff] [blame] | 1365 | extensions, NULL, 0, &al)) |
| 1366 | goto f_err; |
| 1367 | } else { |
| 1368 | /* |
| 1369 | * Check if we can resume the session based on external pre-shared |
| 1370 | * secret. EAP-FAST (RFC 4851) supports two types of session resumption. |
| 1371 | * Resumption based on server-side state works with session IDs. |
| 1372 | * Resumption based on pre-shared Protected Access Credentials (PACs) |
| 1373 | * works by overriding the SessionTicket extension at the application |
| 1374 | * layer, and does not send a session ID. (We do not know whether |
| 1375 | * EAP-FAST servers would honour the session ID.) Therefore, the session |
| 1376 | * ID alone is not a reliable indicator of session resumption, so we |
| 1377 | * first check if we can resume, and later peek at the next handshake |
| 1378 | * message to see if the server wants to resume. |
| 1379 | */ |
| 1380 | if (s->version >= TLS1_VERSION |
| 1381 | && s->ext.session_secret_cb != NULL && s->session->ext.tick) { |
| 1382 | const SSL_CIPHER *pref_cipher = NULL; |
| 1383 | /* |
| 1384 | * s->session->master_key_length is a size_t, but this is an int for |
| 1385 | * backwards compat reasons |
| 1386 | */ |
| 1387 | int master_key_length; |
| 1388 | master_key_length = sizeof(s->session->master_key); |
| 1389 | if (s->ext.session_secret_cb(s, s->session->master_key, |
| 1390 | &master_key_length, |
| 1391 | NULL, &pref_cipher, |
| 1392 | s->ext.session_secret_cb_arg) |
| 1393 | && master_key_length > 0) { |
| 1394 | s->session->master_key_length = master_key_length; |
| 1395 | s->session->cipher = pref_cipher ? |
Benjamin Kaduk | 60d685d | 2017-02-06 11:30:16 -0600 | [diff] [blame] | 1396 | pref_cipher : ssl_get_cipher_by_char(s, cipherchars, 0); |
Matt Caswell | 4ff65f7 | 2017-01-18 09:38:53 +0000 | [diff] [blame] | 1397 | } else { |
| 1398 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR); |
| 1399 | al = SSL_AD_INTERNAL_ERROR; |
| 1400 | goto f_err; |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | if (session_id_len != 0 |
| 1405 | && session_id_len == s->session->session_id_length |
| 1406 | && memcmp(PACKET_data(&session_id), s->session->session_id, |
| 1407 | session_id_len) == 0) |
| 1408 | s->hit = 1; |
| 1409 | } |
| 1410 | |
| 1411 | if (s->hit) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1412 | if (s->sid_ctx_length != s->session->sid_ctx_length |
Matt Caswell | 4ff65f7 | 2017-01-18 09:38:53 +0000 | [diff] [blame] | 1413 | || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1414 | /* actually a client application bug */ |
| 1415 | al = SSL_AD_ILLEGAL_PARAMETER; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1416 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1417 | SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); |
| 1418 | goto f_err; |
| 1419 | } |
Emilia Kasper | 6e3d015 | 2015-04-21 18:12:58 +0200 | [diff] [blame] | 1420 | } else { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1421 | /* |
Emilia Kasper | 6e3d015 | 2015-04-21 18:12:58 +0200 | [diff] [blame] | 1422 | * If we were trying for session-id reuse but the server |
Matt Caswell | 4ff65f7 | 2017-01-18 09:38:53 +0000 | [diff] [blame] | 1423 | * didn't resume, make a new SSL_SESSION. |
Emilia Kasper | 6e3d015 | 2015-04-21 18:12:58 +0200 | [diff] [blame] | 1424 | * In the case of EAP-FAST and PAC, we do not send a session ID, |
| 1425 | * so the PAC-based session secret is always preserved. It'll be |
| 1426 | * overwritten if the server refuses resumption. |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1427 | */ |
Matt Caswell | 4ff65f7 | 2017-01-18 09:38:53 +0000 | [diff] [blame] | 1428 | if (s->session->session_id_length > 0 |
| 1429 | || (SSL_IS_TLS13(s) |
| 1430 | && s->session->ext.tick_identity |
| 1431 | != TLSEXT_PSK_BAD_IDENTITY)) { |
Laszlo Kovacs | 4f6eaa5 | 2015-02-20 14:35:57 -0500 | [diff] [blame] | 1432 | s->ctx->stats.sess_miss++; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1433 | if (!ssl_get_new_session(s, 0)) { |
| 1434 | goto f_err; |
| 1435 | } |
| 1436 | } |
Emilia Kasper | fc5ce51 | 2015-09-18 14:09:37 +0200 | [diff] [blame] | 1437 | |
Fedor Indutny | ccae4a1 | 2016-03-11 17:44:01 +0300 | [diff] [blame] | 1438 | s->session->ssl_version = s->version; |
Emilia Kasper | fc5ce51 | 2015-09-18 14:09:37 +0200 | [diff] [blame] | 1439 | s->session->session_id_length = session_id_len; |
| 1440 | /* session_id_len could be 0 */ |
Kurt Roeckx | a19fc66 | 2016-12-08 19:20:55 +0100 | [diff] [blame] | 1441 | if (session_id_len > 0) |
| 1442 | memcpy(s->session->session_id, PACKET_data(&session_id), |
| 1443 | session_id_len); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1444 | } |
Matt Caswell | 50932c4 | 2015-08-04 17:36:02 +0100 | [diff] [blame] | 1445 | |
Fedor Indutny | ccae4a1 | 2016-03-11 17:44:01 +0300 | [diff] [blame] | 1446 | /* Session version and negotiated protocol version should match */ |
| 1447 | if (s->version != s->session->ssl_version) { |
| 1448 | al = SSL_AD_PROTOCOL_VERSION; |
| 1449 | |
| 1450 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
| 1451 | SSL_R_SSL_SESSION_VERSION_MISMATCH); |
| 1452 | goto f_err; |
| 1453 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1454 | /* |
Kurt Roeckx | 3eb2aff | 2016-02-07 20:17:07 +0100 | [diff] [blame] | 1455 | * Now that we know the version, update the check to see if it's an allowed |
| 1456 | * version. |
| 1457 | */ |
| 1458 | s->s3->tmp.min_ver = s->version; |
| 1459 | s->s3->tmp.max_ver = s->version; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1460 | |
Matt Caswell | 11c67ee | 2017-03-13 15:21:15 +0000 | [diff] [blame] | 1461 | if (!set_client_ciphersuite(s, cipherchars)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1462 | al = SSL_AD_ILLEGAL_PARAMETER; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1463 | goto f_err; |
| 1464 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1465 | |
Dr. Stephen Henson | 09b6c2e | 2005-09-30 23:35:33 +0000 | [diff] [blame] | 1466 | #ifdef OPENSSL_NO_COMP |
Emilia Kasper | fc5ce51 | 2015-09-18 14:09:37 +0200 | [diff] [blame] | 1467 | if (compression != 0) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1468 | al = SSL_AD_ILLEGAL_PARAMETER; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1469 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1470 | SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
| 1471 | goto f_err; |
| 1472 | } |
| 1473 | /* |
| 1474 | * If compression is disabled we'd better not try to resume a session |
| 1475 | * using compression. |
| 1476 | */ |
| 1477 | if (s->session->compress_meth != 0) { |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1478 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1479 | goto f_err; |
| 1480 | } |
Dr. Stephen Henson | 09b6c2e | 2005-09-30 23:35:33 +0000 | [diff] [blame] | 1481 | #else |
Emilia Kasper | fc5ce51 | 2015-09-18 14:09:37 +0200 | [diff] [blame] | 1482 | if (s->hit && compression != s->session->compress_meth) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1483 | al = SSL_AD_ILLEGAL_PARAMETER; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1484 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1485 | SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED); |
| 1486 | goto f_err; |
| 1487 | } |
Emilia Kasper | fc5ce51 | 2015-09-18 14:09:37 +0200 | [diff] [blame] | 1488 | if (compression == 0) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1489 | comp = NULL; |
| 1490 | else if (!ssl_allow_compression(s)) { |
| 1491 | al = SSL_AD_ILLEGAL_PARAMETER; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1492 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1493 | goto f_err; |
Emilia Kasper | fc5ce51 | 2015-09-18 14:09:37 +0200 | [diff] [blame] | 1494 | } else { |
| 1495 | comp = ssl3_comp_find(s->ctx->comp_methods, compression); |
| 1496 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1497 | |
Emilia Kasper | fc5ce51 | 2015-09-18 14:09:37 +0200 | [diff] [blame] | 1498 | if (compression != 0 && comp == NULL) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1499 | al = SSL_AD_ILLEGAL_PARAMETER; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1500 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1501 | SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
| 1502 | goto f_err; |
| 1503 | } else { |
| 1504 | s->s3->tmp.new_compression = comp; |
| 1505 | } |
Dr. Stephen Henson | 09b6c2e | 2005-09-30 23:35:33 +0000 | [diff] [blame] | 1506 | #endif |
Bodo Möller | 761772d | 2007-09-21 06:54:24 +0000 | [diff] [blame] | 1507 | |
Tatsuhiro Tsujikawa | 735d5b5 | 2017-04-18 23:59:39 +0900 | [diff] [blame] | 1508 | if (!tls_parse_all_extensions(s, context, extensions, NULL, 0, &al, 1)) |
Matt Caswell | 332eb39 | 2016-11-28 16:15:51 +0000 | [diff] [blame] | 1509 | goto f_err; |
| 1510 | |
Matt Caswell | 8723588 | 2015-09-07 16:36:53 +0100 | [diff] [blame] | 1511 | #ifndef OPENSSL_NO_SCTP |
| 1512 | if (SSL_IS_DTLS(s) && s->hit) { |
| 1513 | unsigned char sctpauthkey[64]; |
| 1514 | char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)]; |
| 1515 | |
| 1516 | /* |
| 1517 | * Add new shared key for SCTP-Auth, will be ignored if |
| 1518 | * no SCTP used. |
| 1519 | */ |
Matt Caswell | 141eb8c | 2015-10-26 12:00:00 +0000 | [diff] [blame] | 1520 | memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL, |
| 1521 | sizeof(DTLS1_SCTP_AUTH_LABEL)); |
Matt Caswell | 8723588 | 2015-09-07 16:36:53 +0100 | [diff] [blame] | 1522 | |
| 1523 | if (SSL_export_keying_material(s, sctpauthkey, |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1524 | sizeof(sctpauthkey), |
| 1525 | labelbuffer, |
| 1526 | sizeof(labelbuffer), NULL, 0, 0) <= 0) |
Richard Levitte | c0aa6b8 | 2016-12-19 14:07:52 +0100 | [diff] [blame] | 1527 | goto f_err; |
Matt Caswell | 8723588 | 2015-09-07 16:36:53 +0100 | [diff] [blame] | 1528 | |
| 1529 | BIO_ctrl(SSL_get_wbio(s), |
| 1530 | BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY, |
| 1531 | sizeof(sctpauthkey), sctpauthkey); |
| 1532 | } |
| 1533 | #endif |
| 1534 | |
Matt Caswell | 92760c2 | 2016-11-09 14:06:12 +0000 | [diff] [blame] | 1535 | /* |
| 1536 | * In TLSv1.3 we have some post-processing to change cipher state, otherwise |
| 1537 | * we're done with this message |
| 1538 | */ |
| 1539 | if (SSL_IS_TLS13(s) |
| 1540 | && (!s->method->ssl3_enc->setup_key_block(s) |
| 1541 | || !s->method->ssl3_enc->change_cipher_state(s, |
Matt Caswell | 92760c2 | 2016-11-09 14:06:12 +0000 | [diff] [blame] | 1542 | SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_READ))) { |
| 1543 | al = SSL_AD_INTERNAL_ERROR; |
| 1544 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_CANNOT_CHANGE_CIPHER); |
| 1545 | goto f_err; |
| 1546 | } |
| 1547 | |
Matt Caswell | 1b0286a | 2016-12-05 17:31:37 +0000 | [diff] [blame] | 1548 | OPENSSL_free(extensions); |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1549 | return MSG_PROCESS_CONTINUE_READING; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1550 | f_err: |
| 1551 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
Matt Caswell | fe3a329 | 2015-10-05 10:39:54 +0100 | [diff] [blame] | 1552 | ossl_statem_set_error(s); |
Matt Caswell | 1b0286a | 2016-12-05 17:31:37 +0000 | [diff] [blame] | 1553 | OPENSSL_free(extensions); |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1554 | return MSG_PROCESS_ERROR; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1555 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1556 | |
Matt Caswell | 3847d42 | 2017-02-01 13:31:27 +0000 | [diff] [blame] | 1557 | static MSG_PROCESS_RETURN tls_process_hello_retry_request(SSL *s, PACKET *pkt) |
| 1558 | { |
| 1559 | unsigned int sversion; |
Matt Caswell | 2248dbe | 2017-02-06 16:47:29 +0000 | [diff] [blame] | 1560 | int errorcode; |
Matt Caswell | 11c67ee | 2017-03-13 15:21:15 +0000 | [diff] [blame] | 1561 | const unsigned char *cipherchars; |
Matt Caswell | 3847d42 | 2017-02-01 13:31:27 +0000 | [diff] [blame] | 1562 | RAW_EXTENSION *extensions = NULL; |
| 1563 | int al; |
| 1564 | PACKET extpkt; |
| 1565 | |
| 1566 | if (!PACKET_get_net_2(pkt, &sversion)) { |
| 1567 | al = SSL_AD_DECODE_ERROR; |
| 1568 | SSLerr(SSL_F_TLS_PROCESS_HELLO_RETRY_REQUEST, SSL_R_LENGTH_MISMATCH); |
| 1569 | goto f_err; |
| 1570 | } |
| 1571 | |
| 1572 | s->hello_retry_request = 1; |
| 1573 | |
| 1574 | /* This will fail if it doesn't choose TLSv1.3+ */ |
Matt Caswell | c3043dc | 2017-03-22 11:50:32 +0000 | [diff] [blame] | 1575 | errorcode = ssl_choose_client_version(s, sversion, 0, &al); |
Matt Caswell | 2248dbe | 2017-02-06 16:47:29 +0000 | [diff] [blame] | 1576 | if (errorcode != 0) { |
Matt Caswell | 2248dbe | 2017-02-06 16:47:29 +0000 | [diff] [blame] | 1577 | SSLerr(SSL_F_TLS_PROCESS_HELLO_RETRY_REQUEST, errorcode); |
Matt Caswell | 3847d42 | 2017-02-01 13:31:27 +0000 | [diff] [blame] | 1578 | goto f_err; |
| 1579 | } |
| 1580 | |
Matt Caswell | 11c67ee | 2017-03-13 15:21:15 +0000 | [diff] [blame] | 1581 | if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) { |
| 1582 | SSLerr(SSL_F_TLS_PROCESS_HELLO_RETRY_REQUEST, SSL_R_LENGTH_MISMATCH); |
| 1583 | al = SSL_AD_DECODE_ERROR; |
| 1584 | goto f_err; |
| 1585 | } |
| 1586 | |
| 1587 | if (!set_client_ciphersuite(s, cipherchars)) { |
| 1588 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1589 | goto f_err; |
| 1590 | } |
| 1591 | |
Matt Caswell | 66d4bf6 | 2017-05-08 16:05:16 +0100 | [diff] [blame] | 1592 | if (!PACKET_as_length_prefixed_2(pkt, &extpkt) |
| 1593 | /* Must have a non-empty extensions block */ |
| 1594 | || PACKET_remaining(&extpkt) == 0 |
| 1595 | /* Must be no trailing data after extensions */ |
| 1596 | || PACKET_remaining(pkt) != 0) { |
Matt Caswell | 3847d42 | 2017-02-01 13:31:27 +0000 | [diff] [blame] | 1597 | al = SSL_AD_DECODE_ERROR; |
| 1598 | SSLerr(SSL_F_TLS_PROCESS_HELLO_RETRY_REQUEST, SSL_R_BAD_LENGTH); |
| 1599 | goto f_err; |
| 1600 | } |
| 1601 | |
Matt Caswell | fe874d2 | 2017-04-04 11:40:02 +0100 | [diff] [blame] | 1602 | if (!tls_collect_extensions(s, &extpkt, SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST, |
Tatsuhiro Tsujikawa | 735d5b5 | 2017-04-18 23:59:39 +0900 | [diff] [blame] | 1603 | &extensions, &al, NULL, 1) |
Matt Caswell | fe874d2 | 2017-04-04 11:40:02 +0100 | [diff] [blame] | 1604 | || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST, |
Tatsuhiro Tsujikawa | 735d5b5 | 2017-04-18 23:59:39 +0900 | [diff] [blame] | 1605 | extensions, NULL, 0, &al, 1)) |
Matt Caswell | 3847d42 | 2017-02-01 13:31:27 +0000 | [diff] [blame] | 1606 | goto f_err; |
| 1607 | |
| 1608 | OPENSSL_free(extensions); |
Matt Caswell | 66d4bf6 | 2017-05-08 16:05:16 +0100 | [diff] [blame] | 1609 | extensions = NULL; |
| 1610 | |
| 1611 | if (s->ext.tls13_cookie_len == 0 && s->s3->tmp.pkey != NULL) { |
| 1612 | /* |
| 1613 | * We didn't receive a cookie or a new key_share so the next |
| 1614 | * ClientHello will not change |
| 1615 | */ |
| 1616 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1617 | SSLerr(SSL_F_TLS_PROCESS_HELLO_RETRY_REQUEST, |
| 1618 | SSL_R_NO_CHANGE_FOLLOWING_HRR); |
| 1619 | goto f_err; |
| 1620 | } |
Matt Caswell | 3847d42 | 2017-02-01 13:31:27 +0000 | [diff] [blame] | 1621 | |
Matt Caswell | 11c67ee | 2017-03-13 15:21:15 +0000 | [diff] [blame] | 1622 | /* |
| 1623 | * Re-initialise the Transcript Hash. We're going to prepopulate it with |
| 1624 | * a synthetic message_hash in place of ClientHello1. |
| 1625 | */ |
| 1626 | if (!create_synthetic_message_hash(s)) { |
| 1627 | al = SSL_AD_INTERNAL_ERROR; |
| 1628 | goto f_err; |
| 1629 | } |
| 1630 | |
| 1631 | /* |
| 1632 | * Add this message to the Transcript Hash. Normally this is done |
| 1633 | * automatically prior to the message processing stage. However due to the |
| 1634 | * need to create the synthetic message hash, we defer that step until now |
| 1635 | * for HRR messages. |
| 1636 | */ |
| 1637 | if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data, |
| 1638 | s->init_num + SSL3_HM_HEADER_LENGTH)) { |
| 1639 | al = SSL_AD_INTERNAL_ERROR; |
| 1640 | SSLerr(SSL_F_TLS_PROCESS_HELLO_RETRY_REQUEST, ERR_R_INTERNAL_ERROR); |
| 1641 | goto f_err; |
| 1642 | } |
| 1643 | |
Matt Caswell | 3847d42 | 2017-02-01 13:31:27 +0000 | [diff] [blame] | 1644 | return MSG_PROCESS_FINISHED_READING; |
| 1645 | f_err: |
| 1646 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 1647 | ossl_statem_set_error(s); |
| 1648 | OPENSSL_free(extensions); |
| 1649 | return MSG_PROCESS_ERROR; |
| 1650 | } |
| 1651 | |
Matt Caswell | be3583f | 2015-10-26 11:46:33 +0000 | [diff] [blame] | 1652 | MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt) |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1653 | { |
| 1654 | int al, i, ret = MSG_PROCESS_ERROR, exp_idx; |
| 1655 | unsigned long cert_list_len, cert_len; |
| 1656 | X509 *x = NULL; |
Emilia Kasper | b698174 | 2016-02-01 15:26:18 +0100 | [diff] [blame] | 1657 | const unsigned char *certstart, *certbytes; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1658 | STACK_OF(X509) *sk = NULL; |
| 1659 | EVP_PKEY *pkey = NULL; |
Matt Caswell | d805a57 | 2017-01-06 11:01:14 +0000 | [diff] [blame] | 1660 | size_t chainidx; |
Matt Caswell | e96e0f8 | 2016-12-02 09:14:15 +0000 | [diff] [blame] | 1661 | unsigned int context = 0; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1662 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1663 | if ((sk = sk_X509_new_null()) == NULL) { |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1664 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE); |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 1665 | goto err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1666 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1667 | |
Matt Caswell | e96e0f8 | 2016-12-02 09:14:15 +0000 | [diff] [blame] | 1668 | if ((SSL_IS_TLS13(s) && !PACKET_get_1(pkt, &context)) |
| 1669 | || context != 0 |
| 1670 | || !PACKET_get_net_3(pkt, &cert_list_len) |
Matt Caswell | 1a281aa | 2017-05-11 08:38:21 +0100 | [diff] [blame] | 1671 | || PACKET_remaining(pkt) != cert_list_len |
| 1672 | || PACKET_remaining(pkt) == 0) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1673 | al = SSL_AD_DECODE_ERROR; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1674 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1675 | goto f_err; |
| 1676 | } |
Matt Caswell | d805a57 | 2017-01-06 11:01:14 +0000 | [diff] [blame] | 1677 | for (chainidx = 0; PACKET_remaining(pkt); chainidx++) { |
Matt Caswell | 73999b6 | 2015-09-10 10:22:30 +0100 | [diff] [blame] | 1678 | if (!PACKET_get_net_3(pkt, &cert_len) |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1679 | || !PACKET_get_bytes(pkt, &certbytes, cert_len)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1680 | al = SSL_AD_DECODE_ERROR; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1681 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1682 | SSL_R_CERT_LENGTH_MISMATCH); |
| 1683 | goto f_err; |
| 1684 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1685 | |
Matt Caswell | df758a8 | 2015-08-04 20:10:06 +0100 | [diff] [blame] | 1686 | certstart = certbytes; |
| 1687 | x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1688 | if (x == NULL) { |
| 1689 | al = SSL_AD_BAD_CERTIFICATE; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1690 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_ASN1_LIB); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1691 | goto f_err; |
| 1692 | } |
Matt Caswell | df758a8 | 2015-08-04 20:10:06 +0100 | [diff] [blame] | 1693 | if (certbytes != (certstart + cert_len)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1694 | al = SSL_AD_DECODE_ERROR; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1695 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1696 | SSL_R_CERT_LENGTH_MISMATCH); |
| 1697 | goto f_err; |
| 1698 | } |
Matt Caswell | e96e0f8 | 2016-12-02 09:14:15 +0000 | [diff] [blame] | 1699 | |
| 1700 | if (SSL_IS_TLS13(s)) { |
| 1701 | RAW_EXTENSION *rawexts = NULL; |
| 1702 | PACKET extensions; |
| 1703 | |
| 1704 | if (!PACKET_get_length_prefixed_2(pkt, &extensions)) { |
| 1705 | al = SSL_AD_DECODE_ERROR; |
| 1706 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_BAD_LENGTH); |
| 1707 | goto f_err; |
| 1708 | } |
Matt Caswell | fe874d2 | 2017-04-04 11:40:02 +0100 | [diff] [blame] | 1709 | if (!tls_collect_extensions(s, &extensions, |
| 1710 | SSL_EXT_TLS1_3_CERTIFICATE, &rawexts, |
Tatsuhiro Tsujikawa | 735d5b5 | 2017-04-18 23:59:39 +0900 | [diff] [blame] | 1711 | &al, NULL, chainidx == 0) |
Tatsuhiro Tsujikawa | 8e1634e | 2017-04-21 22:10:32 +0900 | [diff] [blame] | 1712 | || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE, |
| 1713 | rawexts, x, chainidx, &al, |
| 1714 | PACKET_remaining(pkt) == 0)) { |
Matt Caswell | 5ee289e | 2017-01-25 14:45:12 +0000 | [diff] [blame] | 1715 | OPENSSL_free(rawexts); |
Matt Caswell | e96e0f8 | 2016-12-02 09:14:15 +0000 | [diff] [blame] | 1716 | goto f_err; |
Matt Caswell | 5ee289e | 2017-01-25 14:45:12 +0000 | [diff] [blame] | 1717 | } |
| 1718 | OPENSSL_free(rawexts); |
Matt Caswell | e96e0f8 | 2016-12-02 09:14:15 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1721 | if (!sk_X509_push(sk, x)) { |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1722 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE); |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 1723 | goto err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1724 | } |
| 1725 | x = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1726 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1727 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1728 | i = ssl_verify_cert_chain(s, sk); |
Matt Caswell | c8e2f98 | 2016-10-27 10:46:25 +0100 | [diff] [blame] | 1729 | /* |
| 1730 | * The documented interface is that SSL_VERIFY_PEER should be set in order |
| 1731 | * for client side verification of the server certificate to take place. |
| 1732 | * However, historically the code has only checked that *any* flag is set |
| 1733 | * to cause server verification to take place. Use of the other flags makes |
| 1734 | * no sense in client mode. An attempt to clean up the semantics was |
| 1735 | * reverted because at least one application *only* set |
| 1736 | * SSL_VERIFY_FAIL_IF_NO_PEER_CERT. Prior to the clean up this still caused |
| 1737 | * server verification to take place, after the clean up it silently did |
| 1738 | * nothing. SSL_CTX_set_verify()/SSL_set_verify() cannot validate the flags |
| 1739 | * sent to them because they are void functions. Therefore, we now use the |
| 1740 | * (less clean) historic behaviour of performing validation if any flag is |
| 1741 | * set. The *documented* interface remains the same. |
| 1742 | */ |
| 1743 | if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1744 | al = ssl_verify_alarm_type(s->verify_result); |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1745 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1746 | SSL_R_CERTIFICATE_VERIFY_FAILED); |
| 1747 | goto f_err; |
| 1748 | } |
| 1749 | ERR_clear_error(); /* but we keep s->verify_result */ |
| 1750 | if (i > 1) { |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1751 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1752 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1753 | goto f_err; |
| 1754 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1755 | |
Dr. Stephen Henson | c34b0f9 | 2015-06-21 19:34:33 +0100 | [diff] [blame] | 1756 | s->session->peer_chain = sk; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1757 | /* |
| 1758 | * Inconsistency alert: cert_chain does include the peer's certificate, |
Matt Caswell | d4d7894 | 2016-05-17 11:51:00 +0100 | [diff] [blame] | 1759 | * which we don't include in statem_srvr.c |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1760 | */ |
| 1761 | x = sk_X509_value(sk, 0); |
| 1762 | sk = NULL; |
| 1763 | /* |
| 1764 | * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end |
| 1765 | */ |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1766 | |
Dr. Stephen Henson | 8382fd3 | 2015-12-20 00:32:36 +0000 | [diff] [blame] | 1767 | pkey = X509_get0_pubkey(x); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1768 | |
Matt Caswell | 55a9a16 | 2015-05-12 10:27:53 +0100 | [diff] [blame] | 1769 | if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1770 | x = NULL; |
Matt Caswell | f69fe73 | 2017-05-10 16:47:24 +0100 | [diff] [blame] | 1771 | al = SSL_AD_INTERNAL_ERROR; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1772 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1773 | SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS); |
| 1774 | goto f_err; |
| 1775 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1776 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1777 | i = ssl_cert_type(x, pkey); |
Matt Caswell | 55a9a16 | 2015-05-12 10:27:53 +0100 | [diff] [blame] | 1778 | if (i < 0) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1779 | x = NULL; |
| 1780 | al = SSL3_AL_FATAL; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1781 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1782 | SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
| 1783 | goto f_err; |
| 1784 | } |
Dr. Stephen Henson | 05b8486 | 2017-01-30 15:34:25 +0000 | [diff] [blame] | 1785 | /* |
| 1786 | * Check certificate type is consistent with ciphersuite. For TLS 1.3 |
| 1787 | * skip check since TLS 1.3 ciphersuites can be used with any certificate |
| 1788 | * type. |
| 1789 | */ |
| 1790 | if (!SSL_IS_TLS13(s)) { |
| 1791 | exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher); |
| 1792 | if (exp_idx >= 0 && i != exp_idx |
Dr. Stephen Henson | b202155 | 2017-06-16 18:55:28 +0100 | [diff] [blame] | 1793 | && (exp_idx != SSL_PKEY_ECC || i != SSL_PKEY_ED25519) |
| 1794 | && (exp_idx != SSL_PKEY_GOST_EC || |
| 1795 | (i != SSL_PKEY_GOST12_512 && i != SSL_PKEY_GOST12_256 |
| 1796 | && i != SSL_PKEY_GOST01))) { |
Dr. Stephen Henson | 05b8486 | 2017-01-30 15:34:25 +0000 | [diff] [blame] | 1797 | x = NULL; |
| 1798 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1799 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
| 1800 | SSL_R_WRONG_CERTIFICATE_TYPE); |
| 1801 | goto f_err; |
| 1802 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1803 | } |
Dr. Stephen Henson | a273c6e | 2015-06-21 19:08:57 +0100 | [diff] [blame] | 1804 | s->session->peer_type = i; |
Matt Caswell | 55a9a16 | 2015-05-12 10:27:53 +0100 | [diff] [blame] | 1805 | |
| 1806 | X509_free(s->session->peer); |
Dr. Stephen Henson | 05f0fb9 | 2015-08-31 20:29:57 +0100 | [diff] [blame] | 1807 | X509_up_ref(x); |
Matt Caswell | 55a9a16 | 2015-05-12 10:27:53 +0100 | [diff] [blame] | 1808 | s->session->peer = x; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1809 | s->session->verify_result = s->verify_result; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1810 | x = NULL; |
Matt Caswell | 2c5dfdc | 2016-12-05 17:04:51 +0000 | [diff] [blame] | 1811 | |
| 1812 | /* Save the current hash state for when we receive the CertificateVerify */ |
| 1813 | if (SSL_IS_TLS13(s) |
| 1814 | && !ssl_handshake_hash(s, s->cert_verify_hash, |
| 1815 | sizeof(s->cert_verify_hash), |
| 1816 | &s->cert_verify_hash_len)) { |
| 1817 | al = SSL_AD_INTERNAL_ERROR; |
| 1818 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR); |
| 1819 | goto f_err; |
| 1820 | } |
| 1821 | |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1822 | ret = MSG_PROCESS_CONTINUE_READING; |
Rich Salz | 6669647 | 2015-05-01 14:29:48 -0400 | [diff] [blame] | 1823 | goto done; |
| 1824 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1825 | f_err: |
Rich Salz | 6669647 | 2015-05-01 14:29:48 -0400 | [diff] [blame] | 1826 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 1827 | err: |
Matt Caswell | fe3a329 | 2015-10-05 10:39:54 +0100 | [diff] [blame] | 1828 | ossl_statem_set_error(s); |
Rich Salz | 6669647 | 2015-05-01 14:29:48 -0400 | [diff] [blame] | 1829 | done: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1830 | X509_free(x); |
| 1831 | sk_X509_pop_free(sk, X509_free); |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 1832 | return ret; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1833 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1834 | |
Matt Caswell | 7dc1c64 | 2016-07-08 12:44:53 +0100 | [diff] [blame] | 1835 | static int tls_process_ske_psk_preamble(SSL *s, PACKET *pkt, int *al) |
Matt Caswell | 02a7459 | 2016-07-08 12:20:42 +0100 | [diff] [blame] | 1836 | { |
| 1837 | #ifndef OPENSSL_NO_PSK |
Matt Caswell | 7dc1c64 | 2016-07-08 12:44:53 +0100 | [diff] [blame] | 1838 | PACKET psk_identity_hint; |
Matt Caswell | 02a7459 | 2016-07-08 12:20:42 +0100 | [diff] [blame] | 1839 | |
Matt Caswell | 7dc1c64 | 2016-07-08 12:44:53 +0100 | [diff] [blame] | 1840 | /* PSK ciphersuites are preceded by an identity hint */ |
| 1841 | |
| 1842 | if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) { |
| 1843 | *al = SSL_AD_DECODE_ERROR; |
Matt Caswell | 4fa8886 | 2016-07-08 15:48:26 +0100 | [diff] [blame] | 1844 | SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, SSL_R_LENGTH_MISMATCH); |
Matt Caswell | 7dc1c64 | 2016-07-08 12:44:53 +0100 | [diff] [blame] | 1845 | return 0; |
| 1846 | } |
| 1847 | |
| 1848 | /* |
| 1849 | * Store PSK identity hint for later use, hint is used in |
| 1850 | * tls_construct_client_key_exchange. Assume that the maximum length of |
| 1851 | * a PSK identity hint can be as long as the maximum length of a PSK |
| 1852 | * identity. |
| 1853 | */ |
| 1854 | if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) { |
| 1855 | *al = SSL_AD_HANDSHAKE_FAILURE; |
Matt Caswell | 4fa8886 | 2016-07-08 15:48:26 +0100 | [diff] [blame] | 1856 | SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, SSL_R_DATA_LENGTH_TOO_LONG); |
Matt Caswell | 7dc1c64 | 2016-07-08 12:44:53 +0100 | [diff] [blame] | 1857 | return 0; |
| 1858 | } |
| 1859 | |
| 1860 | if (PACKET_remaining(&psk_identity_hint) == 0) { |
| 1861 | OPENSSL_free(s->session->psk_identity_hint); |
| 1862 | s->session->psk_identity_hint = NULL; |
| 1863 | } else if (!PACKET_strndup(&psk_identity_hint, |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1864 | &s->session->psk_identity_hint)) { |
Matt Caswell | 7dc1c64 | 2016-07-08 12:44:53 +0100 | [diff] [blame] | 1865 | *al = SSL_AD_INTERNAL_ERROR; |
| 1866 | return 0; |
| 1867 | } |
| 1868 | |
| 1869 | return 1; |
Matt Caswell | 02a7459 | 2016-07-08 12:20:42 +0100 | [diff] [blame] | 1870 | #else |
Matt Caswell | 4fa8886 | 2016-07-08 15:48:26 +0100 | [diff] [blame] | 1871 | SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 7dc1c64 | 2016-07-08 12:44:53 +0100 | [diff] [blame] | 1872 | *al = SSL_AD_INTERNAL_ERROR; |
| 1873 | return 0; |
Matt Caswell | 02a7459 | 2016-07-08 12:20:42 +0100 | [diff] [blame] | 1874 | #endif |
| 1875 | } |
| 1876 | |
Matt Caswell | 25c6c10 | 2016-07-08 14:55:56 +0100 | [diff] [blame] | 1877 | static int tls_process_ske_srp(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al) |
| 1878 | { |
| 1879 | #ifndef OPENSSL_NO_SRP |
| 1880 | PACKET prime, generator, salt, server_pub; |
| 1881 | |
| 1882 | if (!PACKET_get_length_prefixed_2(pkt, &prime) |
| 1883 | || !PACKET_get_length_prefixed_2(pkt, &generator) |
| 1884 | || !PACKET_get_length_prefixed_1(pkt, &salt) |
| 1885 | || !PACKET_get_length_prefixed_2(pkt, &server_pub)) { |
| 1886 | *al = SSL_AD_DECODE_ERROR; |
Matt Caswell | 4fa8886 | 2016-07-08 15:48:26 +0100 | [diff] [blame] | 1887 | SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, SSL_R_LENGTH_MISMATCH); |
Matt Caswell | 25c6c10 | 2016-07-08 14:55:56 +0100 | [diff] [blame] | 1888 | return 0; |
| 1889 | } |
| 1890 | |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1891 | /* TODO(size_t): Convert BN_bin2bn() calls */ |
Matt Caswell | 25c6c10 | 2016-07-08 14:55:56 +0100 | [diff] [blame] | 1892 | if ((s->srp_ctx.N = |
| 1893 | BN_bin2bn(PACKET_data(&prime), |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1894 | (int)PACKET_remaining(&prime), NULL)) == NULL |
Matt Caswell | 25c6c10 | 2016-07-08 14:55:56 +0100 | [diff] [blame] | 1895 | || (s->srp_ctx.g = |
| 1896 | BN_bin2bn(PACKET_data(&generator), |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1897 | (int)PACKET_remaining(&generator), NULL)) == NULL |
Matt Caswell | 25c6c10 | 2016-07-08 14:55:56 +0100 | [diff] [blame] | 1898 | || (s->srp_ctx.s = |
| 1899 | BN_bin2bn(PACKET_data(&salt), |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1900 | (int)PACKET_remaining(&salt), NULL)) == NULL |
Matt Caswell | 25c6c10 | 2016-07-08 14:55:56 +0100 | [diff] [blame] | 1901 | || (s->srp_ctx.B = |
| 1902 | BN_bin2bn(PACKET_data(&server_pub), |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1903 | (int)PACKET_remaining(&server_pub), NULL)) == NULL) { |
Matt Caswell | 25c6c10 | 2016-07-08 14:55:56 +0100 | [diff] [blame] | 1904 | *al = SSL_AD_INTERNAL_ERROR; |
Matt Caswell | 4fa8886 | 2016-07-08 15:48:26 +0100 | [diff] [blame] | 1905 | SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, ERR_R_BN_LIB); |
Matt Caswell | 25c6c10 | 2016-07-08 14:55:56 +0100 | [diff] [blame] | 1906 | return 0; |
| 1907 | } |
| 1908 | |
| 1909 | if (!srp_verify_server_param(s, al)) { |
Matt Caswell | 4fa8886 | 2016-07-08 15:48:26 +0100 | [diff] [blame] | 1910 | SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, SSL_R_BAD_SRP_PARAMETERS); |
Matt Caswell | 25c6c10 | 2016-07-08 14:55:56 +0100 | [diff] [blame] | 1911 | return 0; |
| 1912 | } |
| 1913 | |
| 1914 | /* We must check if there is a certificate */ |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1915 | if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS)) |
Matt Caswell | 25c6c10 | 2016-07-08 14:55:56 +0100 | [diff] [blame] | 1916 | *pkey = X509_get0_pubkey(s->session->peer); |
| 1917 | |
| 1918 | return 1; |
| 1919 | #else |
Matt Caswell | 4fa8886 | 2016-07-08 15:48:26 +0100 | [diff] [blame] | 1920 | SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 25c6c10 | 2016-07-08 14:55:56 +0100 | [diff] [blame] | 1921 | *al = SSL_AD_INTERNAL_ERROR; |
| 1922 | return 0; |
| 1923 | #endif |
| 1924 | } |
| 1925 | |
Matt Caswell | e01a610 | 2016-07-08 15:08:50 +0100 | [diff] [blame] | 1926 | static int tls_process_ske_dhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al) |
| 1927 | { |
| 1928 | #ifndef OPENSSL_NO_DH |
| 1929 | PACKET prime, generator, pub_key; |
| 1930 | EVP_PKEY *peer_tmp = NULL; |
| 1931 | |
| 1932 | DH *dh = NULL; |
| 1933 | BIGNUM *p = NULL, *g = NULL, *bnpub_key = NULL; |
| 1934 | |
Richard Levitte | 2650515 | 2016-12-30 21:57:28 +0100 | [diff] [blame] | 1935 | int check_bits = 0; |
| 1936 | |
Matt Caswell | e01a610 | 2016-07-08 15:08:50 +0100 | [diff] [blame] | 1937 | if (!PACKET_get_length_prefixed_2(pkt, &prime) |
| 1938 | || !PACKET_get_length_prefixed_2(pkt, &generator) |
| 1939 | || !PACKET_get_length_prefixed_2(pkt, &pub_key)) { |
| 1940 | *al = SSL_AD_DECODE_ERROR; |
Matt Caswell | 4fa8886 | 2016-07-08 15:48:26 +0100 | [diff] [blame] | 1941 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_LENGTH_MISMATCH); |
Matt Caswell | e01a610 | 2016-07-08 15:08:50 +0100 | [diff] [blame] | 1942 | return 0; |
| 1943 | } |
| 1944 | |
| 1945 | peer_tmp = EVP_PKEY_new(); |
| 1946 | dh = DH_new(); |
| 1947 | |
| 1948 | if (peer_tmp == NULL || dh == NULL) { |
| 1949 | *al = SSL_AD_INTERNAL_ERROR; |
Matt Caswell | 4fa8886 | 2016-07-08 15:48:26 +0100 | [diff] [blame] | 1950 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_MALLOC_FAILURE); |
Matt Caswell | e01a610 | 2016-07-08 15:08:50 +0100 | [diff] [blame] | 1951 | goto err; |
| 1952 | } |
| 1953 | |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1954 | /* TODO(size_t): Convert these calls */ |
| 1955 | p = BN_bin2bn(PACKET_data(&prime), (int)PACKET_remaining(&prime), NULL); |
| 1956 | g = BN_bin2bn(PACKET_data(&generator), (int)PACKET_remaining(&generator), |
| 1957 | NULL); |
| 1958 | bnpub_key = BN_bin2bn(PACKET_data(&pub_key), |
| 1959 | (int)PACKET_remaining(&pub_key), NULL); |
Matt Caswell | e01a610 | 2016-07-08 15:08:50 +0100 | [diff] [blame] | 1960 | if (p == NULL || g == NULL || bnpub_key == NULL) { |
| 1961 | *al = SSL_AD_INTERNAL_ERROR; |
Matt Caswell | 4fa8886 | 2016-07-08 15:48:26 +0100 | [diff] [blame] | 1962 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB); |
Matt Caswell | e01a610 | 2016-07-08 15:08:50 +0100 | [diff] [blame] | 1963 | goto err; |
| 1964 | } |
| 1965 | |
FdaSilvaYY | 69687aa | 2017-03-28 23:57:28 +0200 | [diff] [blame] | 1966 | /* test non-zero pubkey */ |
Richard Levitte | 2650515 | 2016-12-30 21:57:28 +0100 | [diff] [blame] | 1967 | if (BN_is_zero(bnpub_key)) { |
Matt Caswell | fb34a0f | 2017-05-16 17:28:23 +0100 | [diff] [blame] | 1968 | *al = SSL_AD_ILLEGAL_PARAMETER; |
Matt Caswell | 4fa8886 | 2016-07-08 15:48:26 +0100 | [diff] [blame] | 1969 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_BAD_DH_VALUE); |
Matt Caswell | e01a610 | 2016-07-08 15:08:50 +0100 | [diff] [blame] | 1970 | goto err; |
| 1971 | } |
| 1972 | |
| 1973 | if (!DH_set0_pqg(dh, p, NULL, g)) { |
| 1974 | *al = SSL_AD_INTERNAL_ERROR; |
Matt Caswell | 4fa8886 | 2016-07-08 15:48:26 +0100 | [diff] [blame] | 1975 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB); |
Matt Caswell | e01a610 | 2016-07-08 15:08:50 +0100 | [diff] [blame] | 1976 | goto err; |
| 1977 | } |
| 1978 | p = g = NULL; |
| 1979 | |
Richard Levitte | 2650515 | 2016-12-30 21:57:28 +0100 | [diff] [blame] | 1980 | if (DH_check_params(dh, &check_bits) == 0 || check_bits != 0) { |
Matt Caswell | fb34a0f | 2017-05-16 17:28:23 +0100 | [diff] [blame] | 1981 | *al = SSL_AD_ILLEGAL_PARAMETER; |
Richard Levitte | 2650515 | 2016-12-30 21:57:28 +0100 | [diff] [blame] | 1982 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_BAD_DH_VALUE); |
| 1983 | goto err; |
| 1984 | } |
| 1985 | |
Matt Caswell | e01a610 | 2016-07-08 15:08:50 +0100 | [diff] [blame] | 1986 | if (!DH_set0_key(dh, bnpub_key, NULL)) { |
| 1987 | *al = SSL_AD_INTERNAL_ERROR; |
Matt Caswell | 4fa8886 | 2016-07-08 15:48:26 +0100 | [diff] [blame] | 1988 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB); |
Matt Caswell | e01a610 | 2016-07-08 15:08:50 +0100 | [diff] [blame] | 1989 | goto err; |
| 1990 | } |
| 1991 | bnpub_key = NULL; |
| 1992 | |
| 1993 | if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) { |
| 1994 | *al = SSL_AD_HANDSHAKE_FAILURE; |
Matt Caswell | 4fa8886 | 2016-07-08 15:48:26 +0100 | [diff] [blame] | 1995 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_DH_KEY_TOO_SMALL); |
Matt Caswell | e01a610 | 2016-07-08 15:08:50 +0100 | [diff] [blame] | 1996 | goto err; |
| 1997 | } |
| 1998 | |
| 1999 | if (EVP_PKEY_assign_DH(peer_tmp, dh) == 0) { |
| 2000 | *al = SSL_AD_INTERNAL_ERROR; |
Matt Caswell | 4fa8886 | 2016-07-08 15:48:26 +0100 | [diff] [blame] | 2001 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_EVP_LIB); |
Matt Caswell | e01a610 | 2016-07-08 15:08:50 +0100 | [diff] [blame] | 2002 | goto err; |
| 2003 | } |
| 2004 | |
| 2005 | s->s3->peer_tmp = peer_tmp; |
| 2006 | |
| 2007 | /* |
| 2008 | * FIXME: This makes assumptions about which ciphersuites come with |
| 2009 | * public keys. We should have a less ad-hoc way of doing this |
| 2010 | */ |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 2011 | if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS)) |
Matt Caswell | e01a610 | 2016-07-08 15:08:50 +0100 | [diff] [blame] | 2012 | *pkey = X509_get0_pubkey(s->session->peer); |
| 2013 | /* else anonymous DH, so no certificate or pkey. */ |
| 2014 | |
| 2015 | return 1; |
| 2016 | |
| 2017 | err: |
| 2018 | BN_free(p); |
| 2019 | BN_free(g); |
| 2020 | BN_free(bnpub_key); |
| 2021 | DH_free(dh); |
| 2022 | EVP_PKEY_free(peer_tmp); |
| 2023 | |
| 2024 | return 0; |
| 2025 | #else |
Matt Caswell | 4fa8886 | 2016-07-08 15:48:26 +0100 | [diff] [blame] | 2026 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_INTERNAL_ERROR); |
Matt Caswell | e01a610 | 2016-07-08 15:08:50 +0100 | [diff] [blame] | 2027 | *al = SSL_AD_INTERNAL_ERROR; |
| 2028 | return 0; |
| 2029 | #endif |
| 2030 | } |
| 2031 | |
Matt Caswell | ff74aeb | 2016-07-08 15:26:13 +0100 | [diff] [blame] | 2032 | static int tls_process_ske_ecdhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al) |
| 2033 | { |
| 2034 | #ifndef OPENSSL_NO_EC |
| 2035 | PACKET encoded_pt; |
| 2036 | const unsigned char *ecparams; |
| 2037 | int curve_nid; |
Dr. Stephen Henson | ec24630 | 2016-08-11 15:41:49 +0100 | [diff] [blame] | 2038 | unsigned int curve_flags; |
Matt Caswell | ff74aeb | 2016-07-08 15:26:13 +0100 | [diff] [blame] | 2039 | EVP_PKEY_CTX *pctx = NULL; |
| 2040 | |
| 2041 | /* |
| 2042 | * Extract elliptic curve parameters and the server's ephemeral ECDH |
| 2043 | * public key. For now we only support named (not generic) curves and |
| 2044 | * ECParameters in this case is just three bytes. |
| 2045 | */ |
| 2046 | if (!PACKET_get_bytes(pkt, &ecparams, 3)) { |
| 2047 | *al = SSL_AD_DECODE_ERROR; |
Matt Caswell | 4fa8886 | 2016-07-08 15:48:26 +0100 | [diff] [blame] | 2048 | SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_LENGTH_TOO_SHORT); |
Matt Caswell | ff74aeb | 2016-07-08 15:26:13 +0100 | [diff] [blame] | 2049 | return 0; |
| 2050 | } |
| 2051 | /* |
| 2052 | * Check curve is one of our preferences, if not server has sent an |
| 2053 | * invalid curve. ECParameters is 3 bytes. |
| 2054 | */ |
| 2055 | if (!tls1_check_curve(s, ecparams, 3)) { |
Matt Caswell | fb34a0f | 2017-05-16 17:28:23 +0100 | [diff] [blame] | 2056 | *al = SSL_AD_ILLEGAL_PARAMETER; |
Matt Caswell | 4fa8886 | 2016-07-08 15:48:26 +0100 | [diff] [blame] | 2057 | SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_WRONG_CURVE); |
Matt Caswell | ff74aeb | 2016-07-08 15:26:13 +0100 | [diff] [blame] | 2058 | return 0; |
| 2059 | } |
| 2060 | |
Dr. Stephen Henson | ec24630 | 2016-08-11 15:41:49 +0100 | [diff] [blame] | 2061 | curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2), &curve_flags); |
| 2062 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 2063 | if (curve_nid == 0) { |
Matt Caswell | ff74aeb | 2016-07-08 15:26:13 +0100 | [diff] [blame] | 2064 | *al = SSL_AD_INTERNAL_ERROR; |
Matt Caswell | 4fa8886 | 2016-07-08 15:48:26 +0100 | [diff] [blame] | 2065 | SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, |
Matt Caswell | ff74aeb | 2016-07-08 15:26:13 +0100 | [diff] [blame] | 2066 | SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS); |
| 2067 | return 0; |
| 2068 | } |
| 2069 | |
Dr. Stephen Henson | ec24630 | 2016-08-11 15:41:49 +0100 | [diff] [blame] | 2070 | if ((curve_flags & TLS_CURVE_TYPE) == TLS_CURVE_CUSTOM) { |
| 2071 | EVP_PKEY *key = EVP_PKEY_new(); |
| 2072 | |
| 2073 | if (key == NULL || !EVP_PKEY_set_type(key, curve_nid)) { |
| 2074 | *al = SSL_AD_INTERNAL_ERROR; |
| 2075 | SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_EVP_LIB); |
| 2076 | EVP_PKEY_free(key); |
| 2077 | return 0; |
| 2078 | } |
| 2079 | s->s3->peer_tmp = key; |
| 2080 | } else { |
| 2081 | /* Set up EVP_PKEY with named curve as parameters */ |
| 2082 | pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL); |
| 2083 | if (pctx == NULL |
| 2084 | || EVP_PKEY_paramgen_init(pctx) <= 0 |
| 2085 | || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, curve_nid) <= 0 |
| 2086 | || EVP_PKEY_paramgen(pctx, &s->s3->peer_tmp) <= 0) { |
| 2087 | *al = SSL_AD_INTERNAL_ERROR; |
| 2088 | SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_EVP_LIB); |
| 2089 | EVP_PKEY_CTX_free(pctx); |
| 2090 | return 0; |
| 2091 | } |
Matt Caswell | ff74aeb | 2016-07-08 15:26:13 +0100 | [diff] [blame] | 2092 | EVP_PKEY_CTX_free(pctx); |
Dr. Stephen Henson | ec24630 | 2016-08-11 15:41:49 +0100 | [diff] [blame] | 2093 | pctx = NULL; |
Matt Caswell | ff74aeb | 2016-07-08 15:26:13 +0100 | [diff] [blame] | 2094 | } |
Matt Caswell | ff74aeb | 2016-07-08 15:26:13 +0100 | [diff] [blame] | 2095 | |
| 2096 | if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) { |
| 2097 | *al = SSL_AD_DECODE_ERROR; |
Matt Caswell | 4fa8886 | 2016-07-08 15:48:26 +0100 | [diff] [blame] | 2098 | SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_LENGTH_MISMATCH); |
Matt Caswell | ff74aeb | 2016-07-08 15:26:13 +0100 | [diff] [blame] | 2099 | return 0; |
| 2100 | } |
| 2101 | |
Dr. Stephen Henson | ec24630 | 2016-08-11 15:41:49 +0100 | [diff] [blame] | 2102 | if (!EVP_PKEY_set1_tls_encodedpoint(s->s3->peer_tmp, |
| 2103 | PACKET_data(&encoded_pt), |
| 2104 | PACKET_remaining(&encoded_pt))) { |
Matt Caswell | fb34a0f | 2017-05-16 17:28:23 +0100 | [diff] [blame] | 2105 | *al = SSL_AD_ILLEGAL_PARAMETER; |
Matt Caswell | 4fa8886 | 2016-07-08 15:48:26 +0100 | [diff] [blame] | 2106 | SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_BAD_ECPOINT); |
Matt Caswell | ff74aeb | 2016-07-08 15:26:13 +0100 | [diff] [blame] | 2107 | return 0; |
| 2108 | } |
| 2109 | |
| 2110 | /* |
| 2111 | * The ECC/TLS specification does not mention the use of DSA to sign |
| 2112 | * ECParameters in the server key exchange message. We do support RSA |
| 2113 | * and ECDSA. |
| 2114 | */ |
| 2115 | if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aECDSA) |
| 2116 | *pkey = X509_get0_pubkey(s->session->peer); |
| 2117 | else if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aRSA) |
| 2118 | *pkey = X509_get0_pubkey(s->session->peer); |
| 2119 | /* else anonymous ECDH, so no certificate or pkey. */ |
| 2120 | |
| 2121 | return 1; |
| 2122 | #else |
Matt Caswell | 4fa8886 | 2016-07-08 15:48:26 +0100 | [diff] [blame] | 2123 | SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_INTERNAL_ERROR); |
Matt Caswell | ff74aeb | 2016-07-08 15:26:13 +0100 | [diff] [blame] | 2124 | *al = SSL_AD_INTERNAL_ERROR; |
| 2125 | return 0; |
| 2126 | #endif |
| 2127 | } |
| 2128 | |
Matt Caswell | be3583f | 2015-10-26 11:46:33 +0000 | [diff] [blame] | 2129 | MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt) |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 2130 | { |
Dr. Stephen Henson | 5554fac | 2017-01-25 16:46:02 +0000 | [diff] [blame] | 2131 | int al = -1; |
Matt Caswell | e1e588a | 2016-07-08 15:41:36 +0100 | [diff] [blame] | 2132 | long alg_k; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2133 | EVP_PKEY *pkey = NULL; |
Matt Caswell | fe3066e | 2017-01-03 10:01:39 +0000 | [diff] [blame] | 2134 | EVP_MD_CTX *md_ctx = NULL; |
| 2135 | EVP_PKEY_CTX *pctx = NULL; |
Matt Caswell | 73999b6 | 2015-09-10 10:22:30 +0100 | [diff] [blame] | 2136 | PACKET save_param_start, signature; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 2137 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2138 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
Dr. Stephen Henson | b15f876 | 2014-10-24 12:30:33 +0100 | [diff] [blame] | 2139 | |
Matt Caswell | 73999b6 | 2015-09-10 10:22:30 +0100 | [diff] [blame] | 2140 | save_param_start = *pkt; |
Dr. Stephen Henson | 8d92c1f | 2015-06-21 16:26:08 +0100 | [diff] [blame] | 2141 | |
Ben Laurie | 3260adf | 2016-07-31 15:48:24 +0100 | [diff] [blame] | 2142 | #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) |
Dr. Stephen Henson | 61dd9f7 | 2015-12-14 00:33:33 +0000 | [diff] [blame] | 2143 | EVP_PKEY_free(s->s3->peer_tmp); |
| 2144 | s->s3->peer_tmp = NULL; |
Ben Laurie | 3260adf | 2016-07-31 15:48:24 +0100 | [diff] [blame] | 2145 | #endif |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 2146 | |
Dr. Stephen Henson | 7689082 | 2015-06-28 17:15:10 +0100 | [diff] [blame] | 2147 | if (alg_k & SSL_PSK) { |
Matt Caswell | 7dc1c64 | 2016-07-08 12:44:53 +0100 | [diff] [blame] | 2148 | if (!tls_process_ske_psk_preamble(s, pkt, &al)) |
| 2149 | goto err; |
Dr. Stephen Henson | 7689082 | 2015-06-28 17:15:10 +0100 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | /* Nothing else to do for plain PSK or RSAPSK */ |
| 2153 | if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) { |
Matt Caswell | 25c6c10 | 2016-07-08 14:55:56 +0100 | [diff] [blame] | 2154 | } else if (alg_k & SSL_kSRP) { |
| 2155 | if (!tls_process_ske_srp(s, pkt, &pkey, &al)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2156 | goto err; |
Matt Caswell | e01a610 | 2016-07-08 15:08:50 +0100 | [diff] [blame] | 2157 | } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) { |
| 2158 | if (!tls_process_ske_dhe(s, pkt, &pkey, &al)) |
| 2159 | goto err; |
Matt Caswell | ff74aeb | 2016-07-08 15:26:13 +0100 | [diff] [blame] | 2160 | } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) { |
| 2161 | if (!tls_process_ske_ecdhe(s, pkt, &pkey, &al)) |
| 2162 | goto err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2163 | } else if (alg_k) { |
| 2164 | al = SSL_AD_UNEXPECTED_MESSAGE; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 2165 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); |
Matt Caswell | e1e588a | 2016-07-08 15:41:36 +0100 | [diff] [blame] | 2166 | goto err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2167 | } |
Ralf S. Engelschall | dfeab06 | 1998-12-21 11:00:56 +0000 | [diff] [blame] | 2168 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2169 | /* if it was signed, check the signature */ |
| 2170 | if (pkey != NULL) { |
Emilia Kasper | 3294287 | 2015-10-02 14:40:30 +0200 | [diff] [blame] | 2171 | PACKET params; |
Matt Caswell | be8dba2 | 2016-07-08 12:18:18 +0100 | [diff] [blame] | 2172 | int maxsig; |
| 2173 | const EVP_MD *md = NULL; |
Dr. Stephen Henson | 72ceb6a | 2017-06-16 19:23:47 +0100 | [diff] [blame] | 2174 | unsigned char *tbs; |
| 2175 | size_t tbslen; |
| 2176 | int rv; |
Matt Caswell | e1e588a | 2016-07-08 15:41:36 +0100 | [diff] [blame] | 2177 | |
Emilia Kasper | 3294287 | 2015-10-02 14:40:30 +0200 | [diff] [blame] | 2178 | /* |
| 2179 | * |pkt| now points to the beginning of the signature, so the difference |
| 2180 | * equals the length of the parameters. |
| 2181 | */ |
| 2182 | if (!PACKET_get_sub_packet(&save_param_start, ¶ms, |
| 2183 | PACKET_remaining(&save_param_start) - |
Matt Caswell | 73999b6 | 2015-09-10 10:22:30 +0100 | [diff] [blame] | 2184 | PACKET_remaining(pkt))) { |
Matt Caswell | fb34a0f | 2017-05-16 17:28:23 +0100 | [diff] [blame] | 2185 | al = SSL_AD_DECODE_ERROR; |
Matt Caswell | f0659bd | 2015-10-22 14:02:46 +0100 | [diff] [blame] | 2186 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
Matt Caswell | e1e588a | 2016-07-08 15:41:36 +0100 | [diff] [blame] | 2187 | goto err; |
Emilia Kasper | 3294287 | 2015-10-02 14:40:30 +0200 | [diff] [blame] | 2188 | } |
| 2189 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2190 | if (SSL_USE_SIGALGS(s)) { |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 2191 | unsigned int sigalg; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 2192 | |
| 2193 | if (!PACKET_get_net_2(pkt, &sigalg)) { |
Matt Caswell | e1e588a | 2016-07-08 15:41:36 +0100 | [diff] [blame] | 2194 | al = SSL_AD_DECODE_ERROR; |
Matt Caswell | f0659bd | 2015-10-22 14:02:46 +0100 | [diff] [blame] | 2195 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); |
Matt Caswell | e1e588a | 2016-07-08 15:41:36 +0100 | [diff] [blame] | 2196 | goto err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2197 | } |
Dr. Stephen Henson | 5554fac | 2017-01-25 16:46:02 +0000 | [diff] [blame] | 2198 | rv = tls12_check_peer_sigalg(s, sigalg, pkey); |
Matt Caswell | e1e588a | 2016-07-08 15:41:36 +0100 | [diff] [blame] | 2199 | if (rv == -1) { |
| 2200 | al = SSL_AD_INTERNAL_ERROR; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2201 | goto err; |
Matt Caswell | e1e588a | 2016-07-08 15:41:36 +0100 | [diff] [blame] | 2202 | } else if (rv == 0) { |
| 2203 | al = SSL_AD_DECODE_ERROR; |
| 2204 | goto err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2205 | } |
Dr. Stephen Henson | a2f9200 | 2011-05-09 15:44:01 +0000 | [diff] [blame] | 2206 | #ifdef SSL_DEBUG |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2207 | fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md)); |
Dr. Stephen Henson | a2f9200 | 2011-05-09 15:44:01 +0000 | [diff] [blame] | 2208 | #endif |
Dr. Stephen Henson | f365a3e | 2017-02-13 16:32:06 +0000 | [diff] [blame] | 2209 | } else if (!tls1_set_peer_legacy_sigalg(s, pkey)) { |
| 2210 | al = SSL_AD_INTERNAL_ERROR; |
| 2211 | goto err; |
Emilia Kasper | 3294287 | 2015-10-02 14:40:30 +0200 | [diff] [blame] | 2212 | } |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 2213 | |
Dr. Stephen Henson | b202155 | 2017-06-16 18:55:28 +0100 | [diff] [blame] | 2214 | if (!tls1_lookup_md(s->s3->tmp.peer_sigalg, &md)) { |
| 2215 | al = SSL_AD_INTERNAL_ERROR; |
| 2216 | goto err; |
| 2217 | } |
Dr. Stephen Henson | f365a3e | 2017-02-13 16:32:06 +0000 | [diff] [blame] | 2218 | |
Matt Caswell | 73999b6 | 2015-09-10 10:22:30 +0100 | [diff] [blame] | 2219 | if (!PACKET_get_length_prefixed_2(pkt, &signature) |
| 2220 | || PACKET_remaining(pkt) != 0) { |
Matt Caswell | e1e588a | 2016-07-08 15:41:36 +0100 | [diff] [blame] | 2221 | al = SSL_AD_DECODE_ERROR; |
Matt Caswell | f0659bd | 2015-10-22 14:02:46 +0100 | [diff] [blame] | 2222 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH); |
Matt Caswell | e1e588a | 2016-07-08 15:41:36 +0100 | [diff] [blame] | 2223 | goto err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2224 | } |
Matt Caswell | be8dba2 | 2016-07-08 12:18:18 +0100 | [diff] [blame] | 2225 | maxsig = EVP_PKEY_size(pkey); |
| 2226 | if (maxsig < 0) { |
Matt Caswell | e1e588a | 2016-07-08 15:41:36 +0100 | [diff] [blame] | 2227 | al = SSL_AD_INTERNAL_ERROR; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 2228 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
Matt Caswell | e1e588a | 2016-07-08 15:41:36 +0100 | [diff] [blame] | 2229 | goto err; |
Matt Caswell | 8098fc5 | 2015-08-04 21:22:31 +0100 | [diff] [blame] | 2230 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 2231 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2232 | /* |
Matt Caswell | 8098fc5 | 2015-08-04 21:22:31 +0100 | [diff] [blame] | 2233 | * Check signature length |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2234 | */ |
Matt Caswell | be8dba2 | 2016-07-08 12:18:18 +0100 | [diff] [blame] | 2235 | if (PACKET_remaining(&signature) > (size_t)maxsig) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2236 | /* wrong packet length */ |
Matt Caswell | e1e588a | 2016-07-08 15:41:36 +0100 | [diff] [blame] | 2237 | al = SSL_AD_DECODE_ERROR; |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 2238 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, |
| 2239 | SSL_R_WRONG_SIGNATURE_LENGTH); |
Matt Caswell | e1e588a | 2016-07-08 15:41:36 +0100 | [diff] [blame] | 2240 | goto err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2241 | } |
Matt Caswell | e1e588a | 2016-07-08 15:41:36 +0100 | [diff] [blame] | 2242 | |
| 2243 | md_ctx = EVP_MD_CTX_new(); |
| 2244 | if (md_ctx == NULL) { |
| 2245 | al = SSL_AD_INTERNAL_ERROR; |
| 2246 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); |
| 2247 | goto err; |
| 2248 | } |
| 2249 | |
Matt Caswell | fe3066e | 2017-01-03 10:01:39 +0000 | [diff] [blame] | 2250 | if (EVP_DigestVerifyInit(md_ctx, &pctx, md, NULL, pkey) <= 0) { |
Dr. Stephen Henson | 192e4bb | 2015-11-21 03:56:52 +0000 | [diff] [blame] | 2251 | al = SSL_AD_INTERNAL_ERROR; |
| 2252 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB); |
Matt Caswell | e1e588a | 2016-07-08 15:41:36 +0100 | [diff] [blame] | 2253 | goto err; |
Dr. Stephen Henson | 192e4bb | 2015-11-21 03:56:52 +0000 | [diff] [blame] | 2254 | } |
Dr. Stephen Henson | 5554fac | 2017-01-25 16:46:02 +0000 | [diff] [blame] | 2255 | if (SSL_USE_PSS(s)) { |
Matt Caswell | fe3066e | 2017-01-03 10:01:39 +0000 | [diff] [blame] | 2256 | if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0 |
Dr. Stephen Henson | 91410d4 | 2017-01-29 13:38:55 +0000 | [diff] [blame] | 2257 | || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, |
Dr. Stephen Henson | 968ae5b | 2017-01-25 14:02:00 +0000 | [diff] [blame] | 2258 | RSA_PSS_SALTLEN_DIGEST) <= 0) { |
Matt Caswell | fe3066e | 2017-01-03 10:01:39 +0000 | [diff] [blame] | 2259 | al = SSL_AD_INTERNAL_ERROR; |
| 2260 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB); |
| 2261 | goto err; |
| 2262 | } |
| 2263 | } |
Dr. Stephen Henson | 72ceb6a | 2017-06-16 19:23:47 +0100 | [diff] [blame] | 2264 | tbslen = construct_key_exchange_tbs(s, &tbs, PACKET_data(¶ms), |
| 2265 | PACKET_remaining(¶ms)); |
| 2266 | if (tbslen == 0) { |
| 2267 | al = SSL_AD_INTERNAL_ERROR; |
| 2268 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); |
| 2269 | goto err; |
| 2270 | } |
| 2271 | |
| 2272 | rv = EVP_DigestVerify(md_ctx, PACKET_data(&signature), |
| 2273 | PACKET_remaining(&signature), tbs, tbslen); |
| 2274 | OPENSSL_free(tbs); |
| 2275 | if (rv < 0) { |
Matt Caswell | fe3066e | 2017-01-03 10:01:39 +0000 | [diff] [blame] | 2276 | al = SSL_AD_INTERNAL_ERROR; |
| 2277 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB); |
| 2278 | goto err; |
Dr. Stephen Henson | 72ceb6a | 2017-06-16 19:23:47 +0100 | [diff] [blame] | 2279 | } else if (rv == 0) { |
Dr. Stephen Henson | 192e4bb | 2015-11-21 03:56:52 +0000 | [diff] [blame] | 2280 | al = SSL_AD_DECRYPT_ERROR; |
| 2281 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE); |
Matt Caswell | e1e588a | 2016-07-08 15:41:36 +0100 | [diff] [blame] | 2282 | goto err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2283 | } |
Matt Caswell | e1e588a | 2016-07-08 15:41:36 +0100 | [diff] [blame] | 2284 | EVP_MD_CTX_free(md_ctx); |
Matt Caswell | fe3066e | 2017-01-03 10:01:39 +0000 | [diff] [blame] | 2285 | md_ctx = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2286 | } else { |
Dr. Stephen Henson | 7689082 | 2015-06-28 17:15:10 +0100 | [diff] [blame] | 2287 | /* aNULL, aSRP or PSK do not need public keys */ |
Matt Caswell | e1e588a | 2016-07-08 15:41:36 +0100 | [diff] [blame] | 2288 | if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 2289 | && !(alg_k & SSL_PSK)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2290 | /* Might be wrong key type, check it */ |
Matt Caswell | e1e588a | 2016-07-08 15:41:36 +0100 | [diff] [blame] | 2291 | if (ssl3_check_cert_and_algorithm(s)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2292 | /* Otherwise this shouldn't happen */ |
Matt Caswell | e1e588a | 2016-07-08 15:41:36 +0100 | [diff] [blame] | 2293 | al = SSL_AD_INTERNAL_ERROR; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 2294 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
Matt Caswell | e1e588a | 2016-07-08 15:41:36 +0100 | [diff] [blame] | 2295 | } else { |
| 2296 | al = SSL_AD_DECODE_ERROR; |
| 2297 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2298 | goto err; |
| 2299 | } |
| 2300 | /* still data left over */ |
Matt Caswell | 73999b6 | 2015-09-10 10:22:30 +0100 | [diff] [blame] | 2301 | if (PACKET_remaining(pkt) != 0) { |
Matt Caswell | e1e588a | 2016-07-08 15:41:36 +0100 | [diff] [blame] | 2302 | al = SSL_AD_DECODE_ERROR; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 2303 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE); |
Matt Caswell | e1e588a | 2016-07-08 15:41:36 +0100 | [diff] [blame] | 2304 | goto err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2305 | } |
| 2306 | } |
Matt Caswell | e1e588a | 2016-07-08 15:41:36 +0100 | [diff] [blame] | 2307 | |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 2308 | return MSG_PROCESS_CONTINUE_READING; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2309 | err: |
Matt Caswell | 7dc1c64 | 2016-07-08 12:44:53 +0100 | [diff] [blame] | 2310 | if (al != -1) |
| 2311 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
Matt Caswell | fe3a329 | 2015-10-05 10:39:54 +0100 | [diff] [blame] | 2312 | ossl_statem_set_error(s); |
Matt Caswell | fe3066e | 2017-01-03 10:01:39 +0000 | [diff] [blame] | 2313 | EVP_MD_CTX_free(md_ctx); |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 2314 | return MSG_PROCESS_ERROR; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2315 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 2316 | |
Matt Caswell | be3583f | 2015-10-26 11:46:33 +0000 | [diff] [blame] | 2317 | MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt) |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 2318 | { |
| 2319 | int ret = MSG_PROCESS_ERROR; |
Dr. Stephen Henson | 32f6610 | 2017-03-13 13:29:34 +0000 | [diff] [blame] | 2320 | int al = SSL_AD_DECODE_ERROR; |
| 2321 | size_t i; |
| 2322 | |
| 2323 | /* Clear certificate validity flags */ |
| 2324 | for (i = 0; i < SSL_PKEY_NUM; i++) |
| 2325 | s->s3->tmp.valid_flags[i] = 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2326 | |
Dr. Stephen Henson | 03f44b9 | 2017-02-22 17:26:44 +0000 | [diff] [blame] | 2327 | if (SSL_IS_TLS13(s)) { |
Dr. Stephen Henson | 32f6610 | 2017-03-13 13:29:34 +0000 | [diff] [blame] | 2328 | PACKET reqctx, extensions; |
| 2329 | RAW_EXTENSION *rawexts = NULL; |
Dr. Stephen Henson | 75c13e7 | 2017-02-23 22:12:28 +0000 | [diff] [blame] | 2330 | |
Dr. Stephen Henson | 03f44b9 | 2017-02-22 17:26:44 +0000 | [diff] [blame] | 2331 | /* Free and zero certificate types: it is not present in TLS 1.3 */ |
| 2332 | OPENSSL_free(s->s3->tmp.ctype); |
| 2333 | s->s3->tmp.ctype = NULL; |
| 2334 | s->s3->tmp.ctype_len = 0; |
Dr. Stephen Henson | 32f6610 | 2017-03-13 13:29:34 +0000 | [diff] [blame] | 2335 | |
Dr. Stephen Henson | 03f44b9 | 2017-02-22 17:26:44 +0000 | [diff] [blame] | 2336 | /* TODO(TLS1.3) need to process request context, for now ignore */ |
| 2337 | if (!PACKET_get_length_prefixed_1(pkt, &reqctx)) { |
| 2338 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, |
| 2339 | SSL_R_LENGTH_MISMATCH); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2340 | goto err; |
Dr. Stephen Henson | 03f44b9 | 2017-02-22 17:26:44 +0000 | [diff] [blame] | 2341 | } |
Dr. Stephen Henson | 32f6610 | 2017-03-13 13:29:34 +0000 | [diff] [blame] | 2342 | |
| 2343 | if (!PACKET_get_length_prefixed_2(pkt, &extensions)) { |
Dr. Stephen Henson | 45615c5 | 2017-03-10 16:31:20 +0000 | [diff] [blame] | 2344 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_BAD_LENGTH); |
Dr. Stephen Henson | 32f6610 | 2017-03-13 13:29:34 +0000 | [diff] [blame] | 2345 | goto err; |
| 2346 | } |
| 2347 | if (!tls_collect_extensions(s, &extensions, |
Matt Caswell | fe874d2 | 2017-04-04 11:40:02 +0100 | [diff] [blame] | 2348 | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, |
Tatsuhiro Tsujikawa | 735d5b5 | 2017-04-18 23:59:39 +0900 | [diff] [blame] | 2349 | &rawexts, &al, NULL, 1) |
Matt Caswell | fe874d2 | 2017-04-04 11:40:02 +0100 | [diff] [blame] | 2350 | || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, |
Tatsuhiro Tsujikawa | 735d5b5 | 2017-04-18 23:59:39 +0900 | [diff] [blame] | 2351 | rawexts, NULL, 0, &al, 1)) { |
Dr. Stephen Henson | 32f6610 | 2017-03-13 13:29:34 +0000 | [diff] [blame] | 2352 | OPENSSL_free(rawexts); |
| 2353 | goto err; |
| 2354 | } |
| 2355 | OPENSSL_free(rawexts); |
| 2356 | if (!tls1_process_sigalgs(s)) { |
| 2357 | al = SSL_AD_INTERNAL_ERROR; |
| 2358 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); |
| 2359 | goto err; |
| 2360 | } |
Dr. Stephen Henson | 03f44b9 | 2017-02-22 17:26:44 +0000 | [diff] [blame] | 2361 | } else { |
| 2362 | PACKET ctypes; |
| 2363 | |
| 2364 | /* get the certificate types */ |
| 2365 | if (!PACKET_get_length_prefixed_1(pkt, &ctypes)) { |
Dr. Stephen Henson | 03f44b9 | 2017-02-22 17:26:44 +0000 | [diff] [blame] | 2366 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, |
| 2367 | SSL_R_LENGTH_MISMATCH); |
| 2368 | goto err; |
| 2369 | } |
| 2370 | |
| 2371 | if (!PACKET_memdup(&ctypes, &s->s3->tmp.ctype, &s->s3->tmp.ctype_len)) { |
Dr. Stephen Henson | 32f6610 | 2017-03-13 13:29:34 +0000 | [diff] [blame] | 2372 | al = SSL_AD_INTERNAL_ERROR; |
Dr. Stephen Henson | 03f44b9 | 2017-02-22 17:26:44 +0000 | [diff] [blame] | 2373 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR); |
| 2374 | goto err; |
| 2375 | } |
Matt Caswell | ac11233 | 2015-08-04 22:12:53 +0100 | [diff] [blame] | 2376 | |
Dr. Stephen Henson | 32f6610 | 2017-03-13 13:29:34 +0000 | [diff] [blame] | 2377 | if (SSL_USE_SIGALGS(s)) { |
| 2378 | PACKET sigalgs; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 2379 | |
Dr. Stephen Henson | 32f6610 | 2017-03-13 13:29:34 +0000 | [diff] [blame] | 2380 | if (!PACKET_get_length_prefixed_2(pkt, &sigalgs)) { |
| 2381 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, |
| 2382 | SSL_R_LENGTH_MISMATCH); |
| 2383 | goto err; |
| 2384 | } |
| 2385 | |
| 2386 | if (!tls1_save_sigalgs(s, &sigalgs)) { |
| 2387 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, |
| 2388 | SSL_R_SIGNATURE_ALGORITHMS_ERROR); |
| 2389 | goto err; |
| 2390 | } |
| 2391 | if (!tls1_process_sigalgs(s)) { |
| 2392 | al = SSL_AD_INTERNAL_ERROR; |
| 2393 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, |
| 2394 | ERR_R_MALLOC_FAILURE); |
| 2395 | goto err; |
| 2396 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2397 | } |
Matt Caswell | ac11233 | 2015-08-04 22:12:53 +0100 | [diff] [blame] | 2398 | |
Dr. Stephen Henson | 32f6610 | 2017-03-13 13:29:34 +0000 | [diff] [blame] | 2399 | /* get the CA RDNs */ |
| 2400 | if (!parse_ca_names(s, pkt, &al)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2401 | goto err; |
Dr. Stephen Henson | 03f44b9 | 2017-02-22 17:26:44 +0000 | [diff] [blame] | 2402 | } |
| 2403 | |
| 2404 | if (PACKET_remaining(pkt) != 0) { |
Dr. Stephen Henson | 03f44b9 | 2017-02-22 17:26:44 +0000 | [diff] [blame] | 2405 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH); |
| 2406 | goto err; |
| 2407 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2408 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2409 | /* we should setup a certificate to return.... */ |
| 2410 | s->s3->tmp.cert_req = 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2411 | |
Matt Caswell | 05c4f1d | 2016-06-22 14:31:32 +0100 | [diff] [blame] | 2412 | ret = MSG_PROCESS_CONTINUE_PROCESSING; |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 2413 | goto done; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2414 | err: |
Dr. Stephen Henson | 32f6610 | 2017-03-13 13:29:34 +0000 | [diff] [blame] | 2415 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
Matt Caswell | fe3a329 | 2015-10-05 10:39:54 +0100 | [diff] [blame] | 2416 | ossl_statem_set_error(s); |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 2417 | done: |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 2418 | return ret; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
Matt Caswell | be3583f | 2015-10-26 11:46:33 +0000 | [diff] [blame] | 2421 | MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt) |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 2422 | { |
Matt Caswell | 6df55ca | 2017-01-20 13:53:38 +0000 | [diff] [blame] | 2423 | int al = SSL_AD_DECODE_ERROR; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 2424 | unsigned int ticklen; |
Matt Caswell | 9ac6244 | 2017-01-20 13:50:47 +0000 | [diff] [blame] | 2425 | unsigned long ticket_lifetime_hint, age_add = 0; |
Matt Caswell | ec60ccc | 2016-10-04 20:31:19 +0100 | [diff] [blame] | 2426 | unsigned int sess_len; |
Matt Caswell | de1df7e | 2017-01-13 13:32:11 +0000 | [diff] [blame] | 2427 | RAW_EXTENSION *exts = NULL; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 2428 | |
Matt Caswell | 73999b6 | 2015-09-10 10:22:30 +0100 | [diff] [blame] | 2429 | if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint) |
Matt Caswell | fc24f0b | 2017-01-17 10:43:37 +0000 | [diff] [blame] | 2430 | || (SSL_IS_TLS13(s) && !PACKET_get_net_4(pkt, &age_add)) |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 2431 | || !PACKET_get_net_2(pkt, &ticklen) |
Matt Caswell | de1df7e | 2017-01-13 13:32:11 +0000 | [diff] [blame] | 2432 | || (!SSL_IS_TLS13(s) && PACKET_remaining(pkt) != ticklen) |
Matt Caswell | 1f5b44e | 2017-01-20 16:02:07 +0000 | [diff] [blame] | 2433 | || (SSL_IS_TLS13(s) |
| 2434 | && (ticklen == 0 || PACKET_remaining(pkt) < ticklen))) { |
Matt Caswell | f0659bd | 2015-10-22 14:02:46 +0100 | [diff] [blame] | 2435 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH); |
Emilia Kasper | e711da7 | 2015-09-10 16:32:51 +0200 | [diff] [blame] | 2436 | goto f_err; |
| 2437 | } |
| 2438 | |
Matt Caswell | de1df7e | 2017-01-13 13:32:11 +0000 | [diff] [blame] | 2439 | /* |
| 2440 | * Server is allowed to change its mind (in <=TLSv1.2) and send an empty |
| 2441 | * ticket. We already checked this TLSv1.3 case above, so it should never |
| 2442 | * be 0 here in that instance |
| 2443 | */ |
Emilia Kasper | e711da7 | 2015-09-10 16:32:51 +0200 | [diff] [blame] | 2444 | if (ticklen == 0) |
Matt Caswell | c9de4a2 | 2015-10-22 15:02:14 +0100 | [diff] [blame] | 2445 | return MSG_PROCESS_CONTINUE_READING; |
Emilia Kasper | e711da7 | 2015-09-10 16:32:51 +0200 | [diff] [blame] | 2446 | |
Matt Caswell | 150840b | 2017-03-23 11:22:26 +0000 | [diff] [blame] | 2447 | /* |
| 2448 | * Sessions must be immutable once they go into the session cache. Otherwise |
| 2449 | * we can get multi-thread problems. Therefore we don't "update" sessions, |
| 2450 | * we replace them with a duplicate. In TLSv1.3 we need to do this every |
| 2451 | * time a NewSessionTicket arrives because those messages arrive |
| 2452 | * post-handshake and the session may have already gone into the session |
| 2453 | * cache. |
| 2454 | */ |
| 2455 | if (SSL_IS_TLS13(s) || s->session->session_id_length > 0) { |
Matt Caswell | 98ece4e | 2015-05-18 16:27:48 +0100 | [diff] [blame] | 2456 | int i = s->session_ctx->session_cache_mode; |
| 2457 | SSL_SESSION *new_sess; |
| 2458 | /* |
| 2459 | * We reused an existing session, so we need to replace it with a new |
| 2460 | * one |
| 2461 | */ |
| 2462 | if (i & SSL_SESS_CACHE_CLIENT) { |
| 2463 | /* |
Matt Caswell | e4612d0 | 2016-06-13 11:24:15 +0100 | [diff] [blame] | 2464 | * Remove the old session from the cache. We carry on if this fails |
Matt Caswell | 98ece4e | 2015-05-18 16:27:48 +0100 | [diff] [blame] | 2465 | */ |
Matt Caswell | e4612d0 | 2016-06-13 11:24:15 +0100 | [diff] [blame] | 2466 | SSL_CTX_remove_session(s->session_ctx, s->session); |
Matt Caswell | 98ece4e | 2015-05-18 16:27:48 +0100 | [diff] [blame] | 2467 | } |
| 2468 | |
| 2469 | if ((new_sess = ssl_session_dup(s->session, 0)) == 0) { |
| 2470 | al = SSL_AD_INTERNAL_ERROR; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 2471 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE); |
Matt Caswell | 98ece4e | 2015-05-18 16:27:48 +0100 | [diff] [blame] | 2472 | goto f_err; |
| 2473 | } |
| 2474 | |
| 2475 | SSL_SESSION_free(s->session); |
| 2476 | s->session = new_sess; |
| 2477 | } |
| 2478 | |
Matt Caswell | fc24f0b | 2017-01-17 10:43:37 +0000 | [diff] [blame] | 2479 | /* |
| 2480 | * Technically the cast to long here is not guaranteed by the C standard - |
| 2481 | * but we use it elsewhere, so this should be ok. |
| 2482 | */ |
| 2483 | s->session->time = (long)time(NULL); |
| 2484 | |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 2485 | OPENSSL_free(s->session->ext.tick); |
| 2486 | s->session->ext.tick = NULL; |
| 2487 | s->session->ext.ticklen = 0; |
Emilia Kasper | e711da7 | 2015-09-10 16:32:51 +0200 | [diff] [blame] | 2488 | |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 2489 | s->session->ext.tick = OPENSSL_malloc(ticklen); |
| 2490 | if (s->session->ext.tick == NULL) { |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 2491 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2492 | goto err; |
| 2493 | } |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 2494 | if (!PACKET_copy_bytes(pkt, s->session->ext.tick, ticklen)) { |
Matt Caswell | 561e12b | 2015-08-05 14:50:24 +0100 | [diff] [blame] | 2495 | al = SSL_AD_DECODE_ERROR; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 2496 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH); |
Matt Caswell | 561e12b | 2015-08-05 14:50:24 +0100 | [diff] [blame] | 2497 | goto f_err; |
| 2498 | } |
Emilia Kasper | e711da7 | 2015-09-10 16:32:51 +0200 | [diff] [blame] | 2499 | |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 2500 | s->session->ext.tick_lifetime_hint = ticket_lifetime_hint; |
Matt Caswell | fc24f0b | 2017-01-17 10:43:37 +0000 | [diff] [blame] | 2501 | s->session->ext.tick_age_add = age_add; |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 2502 | s->session->ext.ticklen = ticklen; |
Matt Caswell | de1df7e | 2017-01-13 13:32:11 +0000 | [diff] [blame] | 2503 | |
| 2504 | if (SSL_IS_TLS13(s)) { |
| 2505 | PACKET extpkt; |
| 2506 | |
| 2507 | if (!PACKET_as_length_prefixed_2(pkt, &extpkt) |
Matt Caswell | 26b9172 | 2017-05-11 11:31:57 +0100 | [diff] [blame] | 2508 | || PACKET_remaining(pkt) != 0 |
Matt Caswell | de1df7e | 2017-01-13 13:32:11 +0000 | [diff] [blame] | 2509 | || !tls_collect_extensions(s, &extpkt, |
Matt Caswell | fe874d2 | 2017-04-04 11:40:02 +0100 | [diff] [blame] | 2510 | SSL_EXT_TLS1_3_NEW_SESSION_TICKET, |
Tatsuhiro Tsujikawa | 735d5b5 | 2017-04-18 23:59:39 +0900 | [diff] [blame] | 2511 | &exts, &al, NULL, 1) |
Matt Caswell | fe874d2 | 2017-04-04 11:40:02 +0100 | [diff] [blame] | 2512 | || !tls_parse_all_extensions(s, |
| 2513 | SSL_EXT_TLS1_3_NEW_SESSION_TICKET, |
Tatsuhiro Tsujikawa | 735d5b5 | 2017-04-18 23:59:39 +0900 | [diff] [blame] | 2514 | exts, NULL, 0, &al, 1)) { |
Matt Caswell | de1df7e | 2017-01-13 13:32:11 +0000 | [diff] [blame] | 2515 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_BAD_EXTENSION); |
| 2516 | goto f_err; |
| 2517 | } |
| 2518 | } |
| 2519 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2520 | /* |
| 2521 | * There are two ways to detect a resumed ticket session. One is to set |
| 2522 | * an appropriate session ID and then the server must return a match in |
| 2523 | * ServerHello. This allows the normal client session ID matching to work |
| 2524 | * and we know much earlier that the ticket has been accepted. The |
| 2525 | * other way is to set zero length session ID when the ticket is |
| 2526 | * presented and rely on the handshake to determine session resumption. |
| 2527 | * We choose the former approach because this fits in with assumptions |
| 2528 | * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is |
| 2529 | * SHA256 is disabled) hash of the ticket. |
| 2530 | */ |
Matt Caswell | ec60ccc | 2016-10-04 20:31:19 +0100 | [diff] [blame] | 2531 | /* |
| 2532 | * TODO(size_t): we use sess_len here because EVP_Digest expects an int |
| 2533 | * but s->session->session_id_length is a size_t |
| 2534 | */ |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 2535 | if (!EVP_Digest(s->session->ext.tick, ticklen, |
Matt Caswell | ec60ccc | 2016-10-04 20:31:19 +0100 | [diff] [blame] | 2536 | s->session->session_id, &sess_len, |
Dr. Stephen Henson | d166ed8 | 2016-06-18 15:46:13 +0100 | [diff] [blame] | 2537 | EVP_sha256(), NULL)) { |
| 2538 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_EVP_LIB); |
| 2539 | goto err; |
| 2540 | } |
Matt Caswell | ec60ccc | 2016-10-04 20:31:19 +0100 | [diff] [blame] | 2541 | s->session->session_id_length = sess_len; |
Matt Caswell | de1df7e | 2017-01-13 13:32:11 +0000 | [diff] [blame] | 2542 | |
| 2543 | /* This is a standalone message in TLSv1.3, so there is no more to read */ |
| 2544 | if (SSL_IS_TLS13(s)) { |
Matt Caswell | 33d9341 | 2017-01-30 19:37:17 +0000 | [diff] [blame] | 2545 | OPENSSL_free(exts); |
Matt Caswell | de1df7e | 2017-01-13 13:32:11 +0000 | [diff] [blame] | 2546 | ssl_update_cache(s, SSL_SESS_CACHE_CLIENT); |
| 2547 | return MSG_PROCESS_FINISHED_READING; |
| 2548 | } |
| 2549 | |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 2550 | return MSG_PROCESS_CONTINUE_READING; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2551 | f_err: |
| 2552 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 2553 | err: |
Matt Caswell | fe3a329 | 2015-10-05 10:39:54 +0100 | [diff] [blame] | 2554 | ossl_statem_set_error(s); |
Matt Caswell | 33d9341 | 2017-01-30 19:37:17 +0000 | [diff] [blame] | 2555 | OPENSSL_free(exts); |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 2556 | return MSG_PROCESS_ERROR; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2557 | } |
| 2558 | |
Matt Caswell | f63e428 | 2016-12-02 14:46:54 +0000 | [diff] [blame] | 2559 | /* |
| 2560 | * In TLSv1.3 this is called from the extensions code, otherwise it is used to |
| 2561 | * parse a separate message. Returns 1 on success or 0 on failure. On failure |
| 2562 | * |*al| is populated with a suitable alert code. |
| 2563 | */ |
| 2564 | int tls_process_cert_status_body(SSL *s, PACKET *pkt, int *al) |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 2565 | { |
Matt Caswell | 8b0e934 | 2016-10-06 19:17:54 +0100 | [diff] [blame] | 2566 | size_t resplen; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 2567 | unsigned int type; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 2568 | |
Matt Caswell | 73999b6 | 2015-09-10 10:22:30 +0100 | [diff] [blame] | 2569 | if (!PACKET_get_1(pkt, &type) |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 2570 | || type != TLSEXT_STATUSTYPE_ocsp) { |
Matt Caswell | f63e428 | 2016-12-02 14:46:54 +0000 | [diff] [blame] | 2571 | *al = SSL_AD_DECODE_ERROR; |
| 2572 | SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS_BODY, |
| 2573 | SSL_R_UNSUPPORTED_STATUS_TYPE); |
| 2574 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2575 | } |
Matt Caswell | 56a26ce | 2016-10-19 16:28:12 +0100 | [diff] [blame] | 2576 | if (!PACKET_get_net_3_len(pkt, &resplen) |
| 2577 | || PACKET_remaining(pkt) != resplen) { |
Matt Caswell | f63e428 | 2016-12-02 14:46:54 +0000 | [diff] [blame] | 2578 | *al = SSL_AD_DECODE_ERROR; |
| 2579 | SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS_BODY, SSL_R_LENGTH_MISMATCH); |
| 2580 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2581 | } |
Rich Salz | 8cbfcc7 | 2016-12-11 15:01:28 -0500 | [diff] [blame] | 2582 | s->ext.ocsp.resp = OPENSSL_malloc(resplen); |
| 2583 | if (s->ext.ocsp.resp == NULL) { |
Matt Caswell | f63e428 | 2016-12-02 14:46:54 +0000 | [diff] [blame] | 2584 | *al = SSL_AD_INTERNAL_ERROR; |
| 2585 | SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS_BODY, ERR_R_MALLOC_FAILURE); |
| 2586 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2587 | } |
Rich Salz | 8cbfcc7 | 2016-12-11 15:01:28 -0500 | [diff] [blame] | 2588 | if (!PACKET_copy_bytes(pkt, s->ext.ocsp.resp, resplen)) { |
Matt Caswell | f63e428 | 2016-12-02 14:46:54 +0000 | [diff] [blame] | 2589 | *al = SSL_AD_DECODE_ERROR; |
| 2590 | SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS_BODY, SSL_R_LENGTH_MISMATCH); |
| 2591 | return 0; |
Matt Caswell | ac63710 | 2015-08-05 15:52:26 +0100 | [diff] [blame] | 2592 | } |
Rich Salz | 8cbfcc7 | 2016-12-11 15:01:28 -0500 | [diff] [blame] | 2593 | s->ext.ocsp.resp_len = resplen; |
Matt Caswell | f63e428 | 2016-12-02 14:46:54 +0000 | [diff] [blame] | 2594 | |
| 2595 | return 1; |
| 2596 | } |
Cory Benfield | 2faa1b4 | 2017-01-20 16:22:30 +0000 | [diff] [blame] | 2597 | |
Matt Caswell | f63e428 | 2016-12-02 14:46:54 +0000 | [diff] [blame] | 2598 | |
| 2599 | MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt) |
| 2600 | { |
| 2601 | int al; |
| 2602 | |
| 2603 | if (!tls_process_cert_status_body(s, pkt, &al)) { |
| 2604 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 2605 | ossl_statem_set_error(s); |
| 2606 | return MSG_PROCESS_ERROR; |
| 2607 | } |
| 2608 | |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 2609 | return MSG_PROCESS_CONTINUE_READING; |
Ralf S. Engelschall | dfeab06 | 1998-12-21 11:00:56 +0000 | [diff] [blame] | 2610 | } |
Ralf S. Engelschall | dfeab06 | 1998-12-21 11:00:56 +0000 | [diff] [blame] | 2611 | |
Matt Caswell | 7776a36 | 2016-11-15 10:13:09 +0000 | [diff] [blame] | 2612 | /* |
| 2613 | * Perform miscellaneous checks and processing after we have received the |
| 2614 | * server's initial flight. In TLS1.3 this is after the Server Finished message. |
Matt Caswell | 6530c49 | 2016-11-23 15:38:32 +0000 | [diff] [blame] | 2615 | * In <=TLS1.2 this is after the ServerDone message. Returns 1 on success or 0 |
| 2616 | * on failure. |
Matt Caswell | 7776a36 | 2016-11-15 10:13:09 +0000 | [diff] [blame] | 2617 | */ |
| 2618 | int tls_process_initial_server_flight(SSL *s, int *al) |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 2619 | { |
Matt Caswell | a455d0f | 2015-09-14 15:06:37 +0100 | [diff] [blame] | 2620 | /* |
| 2621 | * at this point we check that we have the required stuff from |
| 2622 | * the server |
| 2623 | */ |
| 2624 | if (!ssl3_check_cert_and_algorithm(s)) { |
Matt Caswell | 7776a36 | 2016-11-15 10:13:09 +0000 | [diff] [blame] | 2625 | *al = SSL_AD_HANDSHAKE_FAILURE; |
| 2626 | return 0; |
Matt Caswell | a455d0f | 2015-09-14 15:06:37 +0100 | [diff] [blame] | 2627 | } |
| 2628 | |
Matt Caswell | bb1aaab | 2015-11-05 14:31:11 +0000 | [diff] [blame] | 2629 | /* |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 2630 | * Call the ocsp status callback if needed. The |ext.ocsp.resp| and |
| 2631 | * |ext.ocsp.resp_len| values will be set if we actually received a status |
Matt Caswell | bb1aaab | 2015-11-05 14:31:11 +0000 | [diff] [blame] | 2632 | * message, or NULL and -1 otherwise |
| 2633 | */ |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 2634 | if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing |
| 2635 | && s->ctx->ext.status_cb != NULL) { |
| 2636 | int ret = s->ctx->ext.status_cb(s, s->ctx->ext.status_arg); |
| 2637 | |
Matt Caswell | bb1aaab | 2015-11-05 14:31:11 +0000 | [diff] [blame] | 2638 | if (ret == 0) { |
Matt Caswell | 7776a36 | 2016-11-15 10:13:09 +0000 | [diff] [blame] | 2639 | *al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; |
| 2640 | SSLerr(SSL_F_TLS_PROCESS_INITIAL_SERVER_FLIGHT, |
Matt Caswell | bb1aaab | 2015-11-05 14:31:11 +0000 | [diff] [blame] | 2641 | SSL_R_INVALID_STATUS_RESPONSE); |
Matt Caswell | 7776a36 | 2016-11-15 10:13:09 +0000 | [diff] [blame] | 2642 | return 0; |
Matt Caswell | bb1aaab | 2015-11-05 14:31:11 +0000 | [diff] [blame] | 2643 | } |
| 2644 | if (ret < 0) { |
Matt Caswell | 7776a36 | 2016-11-15 10:13:09 +0000 | [diff] [blame] | 2645 | *al = SSL_AD_INTERNAL_ERROR; |
| 2646 | SSLerr(SSL_F_TLS_PROCESS_INITIAL_SERVER_FLIGHT, |
| 2647 | ERR_R_MALLOC_FAILURE); |
| 2648 | return 0; |
Matt Caswell | bb1aaab | 2015-11-05 14:31:11 +0000 | [diff] [blame] | 2649 | } |
| 2650 | } |
Rob Percival | ed29e82 | 2016-03-03 16:19:23 +0000 | [diff] [blame] | 2651 | #ifndef OPENSSL_NO_CT |
| 2652 | if (s->ct_validation_callback != NULL) { |
Viktor Dukhovni | 4334143 | 2016-04-07 14:17:37 -0400 | [diff] [blame] | 2653 | /* Note we validate the SCTs whether or not we abort on error */ |
| 2654 | if (!ssl_validate_ct(s) && (s->verify_mode & SSL_VERIFY_PEER)) { |
Matt Caswell | 7776a36 | 2016-11-15 10:13:09 +0000 | [diff] [blame] | 2655 | *al = SSL_AD_HANDSHAKE_FAILURE; |
| 2656 | return 0; |
Rob Percival | ed29e82 | 2016-03-03 16:19:23 +0000 | [diff] [blame] | 2657 | } |
| 2658 | } |
| 2659 | #endif |
| 2660 | |
Matt Caswell | 7776a36 | 2016-11-15 10:13:09 +0000 | [diff] [blame] | 2661 | return 1; |
| 2662 | } |
| 2663 | |
| 2664 | MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt) |
| 2665 | { |
| 2666 | int al = SSL_AD_INTERNAL_ERROR; |
| 2667 | |
| 2668 | if (PACKET_remaining(pkt) > 0) { |
| 2669 | /* should contain no data */ |
| 2670 | al = SSL_AD_DECODE_ERROR; |
| 2671 | SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH); |
| 2672 | goto err; |
| 2673 | } |
| 2674 | #ifndef OPENSSL_NO_SRP |
| 2675 | if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) { |
| 2676 | if (SRP_Calc_A_param(s) <= 0) { |
| 2677 | SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC); |
| 2678 | goto err; |
| 2679 | } |
| 2680 | } |
| 2681 | #endif |
| 2682 | |
| 2683 | /* |
| 2684 | * Error queue messages are generated directly by this function |
| 2685 | */ |
| 2686 | if (!tls_process_initial_server_flight(s, &al)) |
| 2687 | goto err; |
| 2688 | |
Matt Caswell | bd79bcb | 2017-04-20 15:13:28 +0100 | [diff] [blame] | 2689 | return MSG_PROCESS_FINISHED_READING; |
Matt Caswell | 7776a36 | 2016-11-15 10:13:09 +0000 | [diff] [blame] | 2690 | |
| 2691 | err: |
| 2692 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 2693 | ossl_statem_set_error(s); |
| 2694 | return MSG_PROCESS_ERROR; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2695 | } |
Bodo Möller | 176f31d | 2003-02-28 15:37:10 +0000 | [diff] [blame] | 2696 | |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 2697 | static int tls_construct_cke_psk_preamble(SSL *s, WPACKET *pkt, int *al) |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 2698 | { |
| 2699 | #ifndef OPENSSL_NO_PSK |
| 2700 | int ret = 0; |
| 2701 | /* |
| 2702 | * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a |
| 2703 | * \0-terminated identity. The last byte is for us for simulating |
| 2704 | * strnlen. |
| 2705 | */ |
| 2706 | char identity[PSK_MAX_IDENTITY_LEN + 1]; |
| 2707 | size_t identitylen = 0; |
| 2708 | unsigned char psk[PSK_MAX_PSK_LEN]; |
| 2709 | unsigned char *tmppsk = NULL; |
| 2710 | char *tmpidentity = NULL; |
| 2711 | size_t psklen = 0; |
| 2712 | |
| 2713 | if (s->psk_client_callback == NULL) { |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 2714 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, SSL_R_PSK_NO_CLIENT_CB); |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 2715 | *al = SSL_AD_INTERNAL_ERROR; |
| 2716 | goto err; |
| 2717 | } |
| 2718 | |
| 2719 | memset(identity, 0, sizeof(identity)); |
| 2720 | |
| 2721 | psklen = s->psk_client_callback(s, s->session->psk_identity_hint, |
| 2722 | identity, sizeof(identity) - 1, |
| 2723 | psk, sizeof(psk)); |
| 2724 | |
| 2725 | if (psklen > PSK_MAX_PSK_LEN) { |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 2726 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 2727 | *al = SSL_AD_HANDSHAKE_FAILURE; |
| 2728 | goto err; |
| 2729 | } else if (psklen == 0) { |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 2730 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 2731 | SSL_R_PSK_IDENTITY_NOT_FOUND); |
| 2732 | *al = SSL_AD_HANDSHAKE_FAILURE; |
| 2733 | goto err; |
| 2734 | } |
| 2735 | |
| 2736 | identitylen = strlen(identity); |
| 2737 | if (identitylen > PSK_MAX_IDENTITY_LEN) { |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 2738 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR); |
Matt Caswell | fb34a0f | 2017-05-16 17:28:23 +0100 | [diff] [blame] | 2739 | *al = SSL_AD_INTERNAL_ERROR; |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 2740 | goto err; |
| 2741 | } |
| 2742 | |
| 2743 | tmppsk = OPENSSL_memdup(psk, psklen); |
| 2744 | tmpidentity = OPENSSL_strdup(identity); |
| 2745 | if (tmppsk == NULL || tmpidentity == NULL) { |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 2746 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_MALLOC_FAILURE); |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 2747 | *al = SSL_AD_INTERNAL_ERROR; |
| 2748 | goto err; |
| 2749 | } |
| 2750 | |
| 2751 | OPENSSL_free(s->s3->tmp.psk); |
| 2752 | s->s3->tmp.psk = tmppsk; |
| 2753 | s->s3->tmp.psklen = psklen; |
| 2754 | tmppsk = NULL; |
| 2755 | OPENSSL_free(s->session->psk_identity); |
| 2756 | s->session->psk_identity = tmpidentity; |
| 2757 | tmpidentity = NULL; |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 2758 | |
Matt Caswell | b2b3024 | 2016-09-13 11:32:52 +0100 | [diff] [blame] | 2759 | if (!WPACKET_sub_memcpy_u16(pkt, identity, identitylen)) { |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 2760 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR); |
| 2761 | *al = SSL_AD_INTERNAL_ERROR; |
| 2762 | goto err; |
| 2763 | } |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 2764 | |
| 2765 | ret = 1; |
| 2766 | |
| 2767 | err: |
| 2768 | OPENSSL_cleanse(psk, psklen); |
| 2769 | OPENSSL_cleanse(identity, sizeof(identity)); |
| 2770 | OPENSSL_clear_free(tmppsk, psklen); |
| 2771 | OPENSSL_clear_free(tmpidentity, identitylen); |
| 2772 | |
| 2773 | return ret; |
| 2774 | #else |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 2775 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 2776 | *al = SSL_AD_INTERNAL_ERROR; |
| 2777 | return 0; |
| 2778 | #endif |
| 2779 | } |
| 2780 | |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 2781 | static int tls_construct_cke_rsa(SSL *s, WPACKET *pkt, int *al) |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 2782 | { |
| 2783 | #ifndef OPENSSL_NO_RSA |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 2784 | unsigned char *encdata = NULL; |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 2785 | EVP_PKEY *pkey = NULL; |
| 2786 | EVP_PKEY_CTX *pctx = NULL; |
| 2787 | size_t enclen; |
| 2788 | unsigned char *pms = NULL; |
| 2789 | size_t pmslen = 0; |
| 2790 | |
| 2791 | if (s->session->peer == NULL) { |
| 2792 | /* |
| 2793 | * We should always have a server certificate with SSL_kRSA. |
| 2794 | */ |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 2795 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 2796 | return 0; |
| 2797 | } |
| 2798 | |
| 2799 | pkey = X509_get0_pubkey(s->session->peer); |
| 2800 | if (EVP_PKEY_get0_RSA(pkey) == NULL) { |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 2801 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 2802 | return 0; |
| 2803 | } |
| 2804 | |
| 2805 | pmslen = SSL_MAX_MASTER_KEY_LENGTH; |
| 2806 | pms = OPENSSL_malloc(pmslen); |
| 2807 | if (pms == NULL) { |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 2808 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_MALLOC_FAILURE); |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 2809 | *al = SSL_AD_INTERNAL_ERROR; |
| 2810 | return 0; |
| 2811 | } |
| 2812 | |
| 2813 | pms[0] = s->client_version >> 8; |
| 2814 | pms[1] = s->client_version & 0xff; |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 2815 | /* TODO(size_t): Convert this function */ |
| 2816 | if (RAND_bytes(pms + 2, (int)(pmslen - 2)) <= 0) { |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 2817 | goto err; |
| 2818 | } |
| 2819 | |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 2820 | /* Fix buf for TLS and beyond */ |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 2821 | if (s->version > SSL3_VERSION && !WPACKET_start_sub_packet_u16(pkt)) { |
| 2822 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); |
| 2823 | goto err; |
| 2824 | } |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 2825 | pctx = EVP_PKEY_CTX_new(pkey, NULL); |
| 2826 | if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0 |
| 2827 | || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) { |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 2828 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_EVP_LIB); |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 2829 | goto err; |
| 2830 | } |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 2831 | if (!WPACKET_allocate_bytes(pkt, enclen, &encdata) |
| 2832 | || EVP_PKEY_encrypt(pctx, encdata, &enclen, pms, pmslen) <= 0) { |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 2833 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, SSL_R_BAD_RSA_ENCRYPT); |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 2834 | goto err; |
| 2835 | } |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 2836 | EVP_PKEY_CTX_free(pctx); |
| 2837 | pctx = NULL; |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 2838 | |
| 2839 | /* Fix buf for TLS and beyond */ |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 2840 | if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) { |
| 2841 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); |
| 2842 | goto err; |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 2843 | } |
| 2844 | |
Cory Benfield | 2faa1b4 | 2017-01-20 16:22:30 +0000 | [diff] [blame] | 2845 | /* Log the premaster secret, if logging is enabled. */ |
| 2846 | if (!ssl_log_rsa_client_key_exchange(s, encdata, enclen, pms, pmslen)) |
| 2847 | goto err; |
| 2848 | |
Rich Salz | 26fb4b0 | 2017-04-24 09:41:51 -0400 | [diff] [blame] | 2849 | s->s3->tmp.pms = pms; |
| 2850 | s->s3->tmp.pmslen = pmslen; |
| 2851 | |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 2852 | return 1; |
| 2853 | err: |
| 2854 | OPENSSL_clear_free(pms, pmslen); |
| 2855 | EVP_PKEY_CTX_free(pctx); |
| 2856 | |
| 2857 | return 0; |
| 2858 | #else |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 2859 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 2860 | *al = SSL_AD_INTERNAL_ERROR; |
| 2861 | return 0; |
| 2862 | #endif |
| 2863 | } |
| 2864 | |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 2865 | static int tls_construct_cke_dhe(SSL *s, WPACKET *pkt, int *al) |
Matt Caswell | a8c1c70 | 2016-07-08 09:42:07 +0100 | [diff] [blame] | 2866 | { |
| 2867 | #ifndef OPENSSL_NO_DH |
| 2868 | DH *dh_clnt = NULL; |
| 2869 | const BIGNUM *pub_key; |
| 2870 | EVP_PKEY *ckey = NULL, *skey = NULL; |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 2871 | unsigned char *keybytes = NULL; |
Matt Caswell | a8c1c70 | 2016-07-08 09:42:07 +0100 | [diff] [blame] | 2872 | |
| 2873 | skey = s->s3->peer_tmp; |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 2874 | if (skey == NULL) |
| 2875 | goto err; |
| 2876 | |
Dr. Stephen Henson | 0a699a0 | 2016-08-15 14:07:33 +0100 | [diff] [blame] | 2877 | ckey = ssl_generate_pkey(skey); |
Matt Caswell | b599ce3 | 2016-11-23 22:12:40 +0000 | [diff] [blame] | 2878 | if (ckey == NULL) |
| 2879 | goto err; |
| 2880 | |
Matt Caswell | a8c1c70 | 2016-07-08 09:42:07 +0100 | [diff] [blame] | 2881 | dh_clnt = EVP_PKEY_get0_DH(ckey); |
| 2882 | |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 2883 | if (dh_clnt == NULL || ssl_derive(s, ckey, skey, 0) == 0) |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 2884 | goto err; |
Matt Caswell | a8c1c70 | 2016-07-08 09:42:07 +0100 | [diff] [blame] | 2885 | |
| 2886 | /* send off the data */ |
| 2887 | DH_get0_key(dh_clnt, &pub_key, NULL); |
Matt Caswell | b2b3024 | 2016-09-13 11:32:52 +0100 | [diff] [blame] | 2888 | if (!WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(pub_key), &keybytes)) |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 2889 | goto err; |
| 2890 | |
| 2891 | BN_bn2bin(pub_key, keybytes); |
Matt Caswell | a8c1c70 | 2016-07-08 09:42:07 +0100 | [diff] [blame] | 2892 | EVP_PKEY_free(ckey); |
| 2893 | |
| 2894 | return 1; |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 2895 | err: |
| 2896 | EVP_PKEY_free(ckey); |
| 2897 | #endif |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 2898 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_DHE, ERR_R_INTERNAL_ERROR); |
Matt Caswell | a8c1c70 | 2016-07-08 09:42:07 +0100 | [diff] [blame] | 2899 | *al = SSL_AD_INTERNAL_ERROR; |
| 2900 | return 0; |
Matt Caswell | a8c1c70 | 2016-07-08 09:42:07 +0100 | [diff] [blame] | 2901 | } |
| 2902 | |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 2903 | static int tls_construct_cke_ecdhe(SSL *s, WPACKET *pkt, int *al) |
Matt Caswell | 67ad5aa | 2016-07-08 09:51:02 +0100 | [diff] [blame] | 2904 | { |
| 2905 | #ifndef OPENSSL_NO_EC |
| 2906 | unsigned char *encodedPoint = NULL; |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 2907 | size_t encoded_pt_len = 0; |
Matt Caswell | 67ad5aa | 2016-07-08 09:51:02 +0100 | [diff] [blame] | 2908 | EVP_PKEY *ckey = NULL, *skey = NULL; |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 2909 | int ret = 0; |
Matt Caswell | 67ad5aa | 2016-07-08 09:51:02 +0100 | [diff] [blame] | 2910 | |
| 2911 | skey = s->s3->peer_tmp; |
Dr. Stephen Henson | ec24630 | 2016-08-11 15:41:49 +0100 | [diff] [blame] | 2912 | if (skey == NULL) { |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 2913 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 67ad5aa | 2016-07-08 09:51:02 +0100 | [diff] [blame] | 2914 | return 0; |
| 2915 | } |
| 2916 | |
Dr. Stephen Henson | 0a699a0 | 2016-08-15 14:07:33 +0100 | [diff] [blame] | 2917 | ckey = ssl_generate_pkey(skey); |
Matt Caswell | b599ce3 | 2016-11-23 22:12:40 +0000 | [diff] [blame] | 2918 | if (ckey == NULL) { |
| 2919 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_MALLOC_FAILURE); |
| 2920 | goto err; |
| 2921 | } |
Matt Caswell | 67ad5aa | 2016-07-08 09:51:02 +0100 | [diff] [blame] | 2922 | |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 2923 | if (ssl_derive(s, ckey, skey, 0) == 0) { |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 2924 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_EVP_LIB); |
Matt Caswell | 67ad5aa | 2016-07-08 09:51:02 +0100 | [diff] [blame] | 2925 | goto err; |
| 2926 | } |
| 2927 | |
| 2928 | /* Generate encoding of client key */ |
Dr. Stephen Henson | ec24630 | 2016-08-11 15:41:49 +0100 | [diff] [blame] | 2929 | encoded_pt_len = EVP_PKEY_get1_tls_encodedpoint(ckey, &encodedPoint); |
Matt Caswell | 67ad5aa | 2016-07-08 09:51:02 +0100 | [diff] [blame] | 2930 | |
| 2931 | if (encoded_pt_len == 0) { |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 2932 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_EC_LIB); |
Matt Caswell | 67ad5aa | 2016-07-08 09:51:02 +0100 | [diff] [blame] | 2933 | goto err; |
| 2934 | } |
| 2935 | |
Matt Caswell | b2b3024 | 2016-09-13 11:32:52 +0100 | [diff] [blame] | 2936 | if (!WPACKET_sub_memcpy_u8(pkt, encodedPoint, encoded_pt_len)) { |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 2937 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR); |
| 2938 | goto err; |
| 2939 | } |
Matt Caswell | 67ad5aa | 2016-07-08 09:51:02 +0100 | [diff] [blame] | 2940 | |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 2941 | ret = 1; |
Matt Caswell | 67ad5aa | 2016-07-08 09:51:02 +0100 | [diff] [blame] | 2942 | err: |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 2943 | OPENSSL_free(encodedPoint); |
Matt Caswell | 67ad5aa | 2016-07-08 09:51:02 +0100 | [diff] [blame] | 2944 | EVP_PKEY_free(ckey); |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 2945 | return ret; |
Matt Caswell | 67ad5aa | 2016-07-08 09:51:02 +0100 | [diff] [blame] | 2946 | #else |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 2947 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 67ad5aa | 2016-07-08 09:51:02 +0100 | [diff] [blame] | 2948 | *al = SSL_AD_INTERNAL_ERROR; |
| 2949 | return 0; |
| 2950 | #endif |
| 2951 | } |
| 2952 | |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 2953 | static int tls_construct_cke_gost(SSL *s, WPACKET *pkt, int *al) |
Matt Caswell | e00e0b3 | 2016-07-08 10:07:55 +0100 | [diff] [blame] | 2954 | { |
| 2955 | #ifndef OPENSSL_NO_GOST |
| 2956 | /* GOST key exchange message creation */ |
| 2957 | EVP_PKEY_CTX *pkey_ctx = NULL; |
| 2958 | X509 *peer_cert; |
| 2959 | size_t msglen; |
| 2960 | unsigned int md_len; |
| 2961 | unsigned char shared_ukm[32], tmp[256]; |
| 2962 | EVP_MD_CTX *ukm_hash = NULL; |
| 2963 | int dgst_nid = NID_id_GostR3411_94; |
| 2964 | unsigned char *pms = NULL; |
| 2965 | size_t pmslen = 0; |
| 2966 | |
| 2967 | if ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0) |
| 2968 | dgst_nid = NID_id_GostR3411_2012_256; |
| 2969 | |
| 2970 | /* |
FdaSilvaYY | 1ee4b98 | 2017-02-17 23:13:26 +0100 | [diff] [blame] | 2971 | * Get server certificate PKEY and create ctx from it |
Matt Caswell | e00e0b3 | 2016-07-08 10:07:55 +0100 | [diff] [blame] | 2972 | */ |
| 2973 | peer_cert = s->session->peer; |
| 2974 | if (!peer_cert) { |
| 2975 | *al = SSL_AD_HANDSHAKE_FAILURE; |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 2976 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, |
Matt Caswell | e00e0b3 | 2016-07-08 10:07:55 +0100 | [diff] [blame] | 2977 | SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER); |
| 2978 | return 0; |
| 2979 | } |
| 2980 | |
| 2981 | pkey_ctx = EVP_PKEY_CTX_new(X509_get0_pubkey(peer_cert), NULL); |
| 2982 | if (pkey_ctx == NULL) { |
| 2983 | *al = SSL_AD_INTERNAL_ERROR; |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 2984 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_MALLOC_FAILURE); |
Matt Caswell | e00e0b3 | 2016-07-08 10:07:55 +0100 | [diff] [blame] | 2985 | return 0; |
| 2986 | } |
| 2987 | /* |
| 2988 | * If we have send a certificate, and certificate key |
| 2989 | * parameters match those of server certificate, use |
| 2990 | * certificate key for key exchange |
| 2991 | */ |
| 2992 | |
| 2993 | /* Otherwise, generate ephemeral key pair */ |
| 2994 | pmslen = 32; |
| 2995 | pms = OPENSSL_malloc(pmslen); |
| 2996 | if (pms == NULL) { |
| 2997 | *al = SSL_AD_INTERNAL_ERROR; |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 2998 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_MALLOC_FAILURE); |
Matt Caswell | 2f3930b | 2016-08-22 22:17:20 +0100 | [diff] [blame] | 2999 | goto err; |
Matt Caswell | e00e0b3 | 2016-07-08 10:07:55 +0100 | [diff] [blame] | 3000 | } |
| 3001 | |
| 3002 | if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0 |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 3003 | /* Generate session key |
| 3004 | * TODO(size_t): Convert this function |
| 3005 | */ |
| 3006 | || RAND_bytes(pms, (int)pmslen) <= 0) { |
Matt Caswell | e00e0b3 | 2016-07-08 10:07:55 +0100 | [diff] [blame] | 3007 | *al = SSL_AD_INTERNAL_ERROR; |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 3008 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR); |
Matt Caswell | e00e0b3 | 2016-07-08 10:07:55 +0100 | [diff] [blame] | 3009 | goto err; |
| 3010 | }; |
| 3011 | /* |
Matt Caswell | e00e0b3 | 2016-07-08 10:07:55 +0100 | [diff] [blame] | 3012 | * Compute shared IV and store it in algorithm-specific context |
| 3013 | * data |
| 3014 | */ |
| 3015 | ukm_hash = EVP_MD_CTX_new(); |
| 3016 | if (ukm_hash == NULL |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 3017 | || EVP_DigestInit(ukm_hash, EVP_get_digestbynid(dgst_nid)) <= 0 |
| 3018 | || EVP_DigestUpdate(ukm_hash, s->s3->client_random, |
| 3019 | SSL3_RANDOM_SIZE) <= 0 |
| 3020 | || EVP_DigestUpdate(ukm_hash, s->s3->server_random, |
| 3021 | SSL3_RANDOM_SIZE) <= 0 |
| 3022 | || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) { |
Matt Caswell | e00e0b3 | 2016-07-08 10:07:55 +0100 | [diff] [blame] | 3023 | *al = SSL_AD_INTERNAL_ERROR; |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 3024 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR); |
Matt Caswell | e00e0b3 | 2016-07-08 10:07:55 +0100 | [diff] [blame] | 3025 | goto err; |
| 3026 | } |
| 3027 | EVP_MD_CTX_free(ukm_hash); |
| 3028 | ukm_hash = NULL; |
| 3029 | if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, |
| 3030 | EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) < 0) { |
| 3031 | *al = SSL_AD_INTERNAL_ERROR; |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 3032 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, SSL_R_LIBRARY_BUG); |
Matt Caswell | e00e0b3 | 2016-07-08 10:07:55 +0100 | [diff] [blame] | 3033 | goto err; |
| 3034 | } |
| 3035 | /* Make GOST keytransport blob message */ |
| 3036 | /* |
| 3037 | * Encapsulate it into sequence |
| 3038 | */ |
Matt Caswell | e00e0b3 | 2016-07-08 10:07:55 +0100 | [diff] [blame] | 3039 | msglen = 255; |
| 3040 | if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) { |
| 3041 | *al = SSL_AD_INTERNAL_ERROR; |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 3042 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, SSL_R_LIBRARY_BUG); |
Matt Caswell | e00e0b3 | 2016-07-08 10:07:55 +0100 | [diff] [blame] | 3043 | goto err; |
| 3044 | } |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 3045 | |
Matt Caswell | 08029df | 2016-09-20 14:47:44 +0100 | [diff] [blame] | 3046 | if (!WPACKET_put_bytes_u8(pkt, V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED) |
| 3047 | || (msglen >= 0x80 && !WPACKET_put_bytes_u8(pkt, 0x81)) |
Matt Caswell | b2b3024 | 2016-09-13 11:32:52 +0100 | [diff] [blame] | 3048 | || !WPACKET_sub_memcpy_u8(pkt, tmp, msglen)) { |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 3049 | *al = SSL_AD_INTERNAL_ERROR; |
| 3050 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR); |
| 3051 | goto err; |
Matt Caswell | e00e0b3 | 2016-07-08 10:07:55 +0100 | [diff] [blame] | 3052 | } |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 3053 | |
Matt Caswell | e00e0b3 | 2016-07-08 10:07:55 +0100 | [diff] [blame] | 3054 | EVP_PKEY_CTX_free(pkey_ctx); |
| 3055 | s->s3->tmp.pms = pms; |
| 3056 | s->s3->tmp.pmslen = pmslen; |
| 3057 | |
| 3058 | return 1; |
| 3059 | err: |
| 3060 | EVP_PKEY_CTX_free(pkey_ctx); |
| 3061 | OPENSSL_clear_free(pms, pmslen); |
| 3062 | EVP_MD_CTX_free(ukm_hash); |
| 3063 | return 0; |
| 3064 | #else |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 3065 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR); |
Matt Caswell | e00e0b3 | 2016-07-08 10:07:55 +0100 | [diff] [blame] | 3066 | *al = SSL_AD_INTERNAL_ERROR; |
| 3067 | return 0; |
| 3068 | #endif |
| 3069 | } |
| 3070 | |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 3071 | static int tls_construct_cke_srp(SSL *s, WPACKET *pkt, int *al) |
Matt Caswell | 840a2bf | 2016-07-08 10:43:59 +0100 | [diff] [blame] | 3072 | { |
Richard Levitte | 8b9546c | 2016-07-22 21:48:05 +0200 | [diff] [blame] | 3073 | #ifndef OPENSSL_NO_SRP |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 3074 | unsigned char *abytes = NULL; |
| 3075 | |
| 3076 | if (s->srp_ctx.A == NULL |
Matt Caswell | b2b3024 | 2016-09-13 11:32:52 +0100 | [diff] [blame] | 3077 | || !WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(s->srp_ctx.A), |
| 3078 | &abytes)) { |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 3079 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 840a2bf | 2016-07-08 10:43:59 +0100 | [diff] [blame] | 3080 | return 0; |
| 3081 | } |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 3082 | BN_bn2bin(s->srp_ctx.A, abytes); |
| 3083 | |
Matt Caswell | 840a2bf | 2016-07-08 10:43:59 +0100 | [diff] [blame] | 3084 | OPENSSL_free(s->session->srp_username); |
| 3085 | s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login); |
| 3086 | if (s->session->srp_username == NULL) { |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 3087 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_MALLOC_FAILURE); |
Matt Caswell | 840a2bf | 2016-07-08 10:43:59 +0100 | [diff] [blame] | 3088 | return 0; |
| 3089 | } |
| 3090 | |
| 3091 | return 1; |
| 3092 | #else |
Matt Caswell | 05ec6a2 | 2016-07-08 12:27:30 +0100 | [diff] [blame] | 3093 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 840a2bf | 2016-07-08 10:43:59 +0100 | [diff] [blame] | 3094 | *al = SSL_AD_INTERNAL_ERROR; |
| 3095 | return 0; |
| 3096 | #endif |
| 3097 | } |
| 3098 | |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 3099 | int tls_construct_client_key_exchange(SSL *s, WPACKET *pkt) |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3100 | { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3101 | unsigned long alg_k; |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 3102 | int al = -1; |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 3103 | |
Dr. Stephen Henson | 7689082 | 2015-06-28 17:15:10 +0100 | [diff] [blame] | 3104 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3105 | |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 3106 | if ((alg_k & SSL_PSK) |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 3107 | && !tls_construct_cke_psk_preamble(s, pkt, &al)) |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 3108 | goto err; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3109 | |
Matt Caswell | f1ec23c | 2016-09-13 11:01:04 +0100 | [diff] [blame] | 3110 | if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) { |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 3111 | if (!tls_construct_cke_rsa(s, pkt, &al)) |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 3112 | goto err; |
Matt Caswell | a8c1c70 | 2016-07-08 09:42:07 +0100 | [diff] [blame] | 3113 | } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) { |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 3114 | if (!tls_construct_cke_dhe(s, pkt, &al)) |
Dr. Stephen Henson | bc71f91 | 2015-12-15 23:57:18 +0000 | [diff] [blame] | 3115 | goto err; |
Matt Caswell | 67ad5aa | 2016-07-08 09:51:02 +0100 | [diff] [blame] | 3116 | } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) { |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 3117 | if (!tls_construct_cke_ecdhe(s, pkt, &al)) |
Matt Caswell | 67ad5aa | 2016-07-08 09:51:02 +0100 | [diff] [blame] | 3118 | goto err; |
Matt Caswell | e00e0b3 | 2016-07-08 10:07:55 +0100 | [diff] [blame] | 3119 | } else if (alg_k & SSL_kGOST) { |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 3120 | if (!tls_construct_cke_gost(s, pkt, &al)) |
Matt Caswell | e00e0b3 | 2016-07-08 10:07:55 +0100 | [diff] [blame] | 3121 | goto err; |
Matt Caswell | 840a2bf | 2016-07-08 10:43:59 +0100 | [diff] [blame] | 3122 | } else if (alg_k & SSL_kSRP) { |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 3123 | if (!tls_construct_cke_srp(s, pkt, &al)) |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3124 | goto err; |
Matt Caswell | 4a42454 | 2016-09-29 12:04:08 +0100 | [diff] [blame] | 3125 | } else if (!(alg_k & SSL_kPSK)) { |
Matt Caswell | fb34a0f | 2017-05-16 17:28:23 +0100 | [diff] [blame] | 3126 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3127 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
| 3128 | goto err; |
| 3129 | } |
| 3130 | |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3131 | return 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3132 | err: |
Matt Caswell | 13c0ec4 | 2016-07-07 14:42:27 +0100 | [diff] [blame] | 3133 | if (al != -1) |
| 3134 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
Matt Caswell | 0bce0b0 | 2016-07-07 12:47:07 +0100 | [diff] [blame] | 3135 | OPENSSL_clear_free(s->s3->tmp.pms, s->s3->tmp.pmslen); |
Dr. Stephen Henson | 76106e6 | 2015-05-12 17:17:37 +0100 | [diff] [blame] | 3136 | s->s3->tmp.pms = NULL; |
Dr. Stephen Henson | 7689082 | 2015-06-28 17:15:10 +0100 | [diff] [blame] | 3137 | #ifndef OPENSSL_NO_PSK |
| 3138 | OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen); |
| 3139 | s->s3->tmp.psk = NULL; |
| 3140 | #endif |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3141 | return 0; |
| 3142 | } |
| 3143 | |
| 3144 | int tls_client_key_exchange_post_work(SSL *s) |
| 3145 | { |
| 3146 | unsigned char *pms = NULL; |
| 3147 | size_t pmslen = 0; |
| 3148 | |
Matt Caswell | 6f13737 | 2016-04-28 15:12:37 +0100 | [diff] [blame] | 3149 | pms = s->s3->tmp.pms; |
| 3150 | pmslen = s->s3->tmp.pmslen; |
| 3151 | |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3152 | #ifndef OPENSSL_NO_SRP |
| 3153 | /* Check for SRP */ |
| 3154 | if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) { |
| 3155 | if (!srp_generate_client_master_secret(s)) { |
| 3156 | SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, |
| 3157 | ERR_R_INTERNAL_ERROR); |
| 3158 | goto err; |
| 3159 | } |
| 3160 | return 1; |
| 3161 | } |
| 3162 | #endif |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3163 | |
| 3164 | if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) { |
| 3165 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 3166 | SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE); |
| 3167 | goto err; |
| 3168 | } |
| 3169 | if (!ssl_generate_master_secret(s, pms, pmslen, 1)) { |
| 3170 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 3171 | SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 6f13737 | 2016-04-28 15:12:37 +0100 | [diff] [blame] | 3172 | /* ssl_generate_master_secret frees the pms even on error */ |
| 3173 | pms = NULL; |
| 3174 | pmslen = 0; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3175 | goto err; |
| 3176 | } |
Matt Caswell | 6f13737 | 2016-04-28 15:12:37 +0100 | [diff] [blame] | 3177 | pms = NULL; |
| 3178 | pmslen = 0; |
Matt Caswell | 473483d | 2015-09-07 22:00:36 +0100 | [diff] [blame] | 3179 | |
| 3180 | #ifndef OPENSSL_NO_SCTP |
| 3181 | if (SSL_IS_DTLS(s)) { |
| 3182 | unsigned char sctpauthkey[64]; |
| 3183 | char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)]; |
| 3184 | |
| 3185 | /* |
| 3186 | * Add new shared key for SCTP-Auth, will be ignored if no SCTP |
| 3187 | * used. |
| 3188 | */ |
Matt Caswell | 141eb8c | 2015-10-26 12:00:00 +0000 | [diff] [blame] | 3189 | memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL, |
| 3190 | sizeof(DTLS1_SCTP_AUTH_LABEL)); |
Matt Caswell | 473483d | 2015-09-07 22:00:36 +0100 | [diff] [blame] | 3191 | |
| 3192 | if (SSL_export_keying_material(s, sctpauthkey, |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 3193 | sizeof(sctpauthkey), labelbuffer, |
| 3194 | sizeof(labelbuffer), NULL, 0, 0) <= 0) |
Matt Caswell | 473483d | 2015-09-07 22:00:36 +0100 | [diff] [blame] | 3195 | goto err; |
| 3196 | |
| 3197 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY, |
| 3198 | sizeof(sctpauthkey), sctpauthkey); |
| 3199 | } |
| 3200 | #endif |
| 3201 | |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3202 | return 1; |
| 3203 | err: |
| 3204 | OPENSSL_clear_free(pms, pmslen); |
| 3205 | s->s3->tmp.pms = NULL; |
| 3206 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3207 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3208 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3209 | /* |
| 3210 | * Check a certificate can be used for client authentication. Currently check |
| 3211 | * cert exists, if we have a suitable digest for TLS 1.2 if static DH client |
| 3212 | * certificates can be used and optionally checks suitability for Suite B. |
Dr. Stephen Henson | 0d60939 | 2012-01-25 14:51:49 +0000 | [diff] [blame] | 3213 | */ |
| 3214 | static int ssl3_check_client_certificate(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3215 | { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3216 | /* If no suitable signature algorithm can't use certificate */ |
Dr. Stephen Henson | ad4dd36 | 2017-02-15 16:23:49 +0000 | [diff] [blame] | 3217 | if (!tls_choose_sigalg(s, NULL) || s->s3->tmp.sigalg == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3218 | return 0; |
| 3219 | /* |
| 3220 | * If strict mode check suitability of chain before using it. This also |
| 3221 | * adjusts suite B digest if necessary. |
| 3222 | */ |
| 3223 | if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT && |
| 3224 | !tls1_check_chain(s, NULL, NULL, NULL, -2)) |
| 3225 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3226 | return 1; |
| 3227 | } |
Dr. Stephen Henson | 0d60939 | 2012-01-25 14:51:49 +0000 | [diff] [blame] | 3228 | |
Matt Caswell | be3583f | 2015-10-26 11:46:33 +0000 | [diff] [blame] | 3229 | WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst) |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3230 | { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3231 | X509 *x509 = NULL; |
| 3232 | EVP_PKEY *pkey = NULL; |
| 3233 | int i; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3234 | |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3235 | if (wst == WORK_MORE_A) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3236 | /* Let cert callback update client certificates if required */ |
| 3237 | if (s->cert->cert_cb) { |
| 3238 | i = s->cert->cert_cb(s, s->cert->cert_cb_arg); |
| 3239 | if (i < 0) { |
| 3240 | s->rwstate = SSL_X509_LOOKUP; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3241 | return WORK_MORE_A; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3242 | } |
| 3243 | if (i == 0) { |
| 3244 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Matt Caswell | fe3a329 | 2015-10-05 10:39:54 +0100 | [diff] [blame] | 3245 | ossl_statem_set_error(s); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3246 | return 0; |
| 3247 | } |
| 3248 | s->rwstate = SSL_NOTHING; |
| 3249 | } |
| 3250 | if (ssl3_check_client_certificate(s)) |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3251 | return WORK_FINISHED_CONTINUE; |
| 3252 | |
| 3253 | /* Fall through to WORK_MORE_B */ |
| 3254 | wst = WORK_MORE_B; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3255 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3256 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3257 | /* We need to get a client cert */ |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3258 | if (wst == WORK_MORE_B) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3259 | /* |
| 3260 | * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP; |
| 3261 | * return(-1); We then get retied later |
| 3262 | */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3263 | i = ssl_do_client_cert_cb(s, &x509, &pkey); |
| 3264 | if (i < 0) { |
| 3265 | s->rwstate = SSL_X509_LOOKUP; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3266 | return WORK_MORE_B; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3267 | } |
| 3268 | s->rwstate = SSL_NOTHING; |
| 3269 | if ((i == 1) && (pkey != NULL) && (x509 != NULL)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3270 | if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) |
| 3271 | i = 0; |
| 3272 | } else if (i == 1) { |
| 3273 | i = 0; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3274 | SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3275 | SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); |
| 3276 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3277 | |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 3278 | X509_free(x509); |
Rich Salz | 25aaa98 | 2015-05-01 14:37:16 -0400 | [diff] [blame] | 3279 | EVP_PKEY_free(pkey); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3280 | if (i && !ssl3_check_client_certificate(s)) |
| 3281 | i = 0; |
| 3282 | if (i == 0) { |
| 3283 | if (s->version == SSL3_VERSION) { |
| 3284 | s->s3->tmp.cert_req = 0; |
| 3285 | ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE); |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3286 | return WORK_FINISHED_CONTINUE; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3287 | } else { |
| 3288 | s->s3->tmp.cert_req = 2; |
Dr. Stephen Henson | 124037f | 2015-06-16 14:44:29 +0100 | [diff] [blame] | 3289 | if (!ssl3_digest_cached_records(s, 0)) { |
Dr. Stephen Henson | dab18ab | 2015-05-07 00:04:48 +0100 | [diff] [blame] | 3290 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Matt Caswell | fe3a329 | 2015-10-05 10:39:54 +0100 | [diff] [blame] | 3291 | ossl_statem_set_error(s); |
Dr. Stephen Henson | dab18ab | 2015-05-07 00:04:48 +0100 | [diff] [blame] | 3292 | return 0; |
| 3293 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3294 | } |
| 3295 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3296 | |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3297 | return WORK_FINISHED_CONTINUE; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3298 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3299 | |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3300 | /* Shouldn't ever get here */ |
| 3301 | return WORK_ERROR; |
| 3302 | } |
| 3303 | |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 3304 | int tls_construct_client_certificate(SSL *s, WPACKET *pkt) |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3305 | { |
Matt Caswell | 0baed5e | 2017-01-02 11:16:37 +0000 | [diff] [blame] | 3306 | int al = SSL_AD_INTERNAL_ERROR; |
Matt Caswell | e96e0f8 | 2016-12-02 09:14:15 +0000 | [diff] [blame] | 3307 | |
| 3308 | /* |
| 3309 | * TODO(TLS1.3): For now we must put an empty context. Needs to be filled in |
| 3310 | * later |
| 3311 | */ |
| 3312 | if ((SSL_IS_TLS13(s) && !WPACKET_put_bytes_u8(pkt, 0)) |
| 3313 | || !ssl3_output_cert_chain(s, pkt, |
Matt Caswell | b90506e | 2016-10-03 15:37:47 +0100 | [diff] [blame] | 3314 | (s->s3->tmp.cert_req == 2) ? NULL |
Matt Caswell | e96e0f8 | 2016-12-02 09:14:15 +0000 | [diff] [blame] | 3315 | : s->cert->key, |
| 3316 | &al)) { |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3317 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR); |
Matt Caswell | f7e393b | 2017-02-27 11:19:57 +0000 | [diff] [blame] | 3318 | goto err; |
| 3319 | } |
| 3320 | |
| 3321 | if (SSL_IS_TLS13(s) |
| 3322 | && SSL_IS_FIRST_HANDSHAKE(s) |
| 3323 | && (!s->method->ssl3_enc->change_cipher_state(s, |
| 3324 | SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) { |
| 3325 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, |
| 3326 | SSL_R_CANNOT_CHANGE_CIPHER); |
| 3327 | goto err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3328 | } |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3329 | |
| 3330 | return 1; |
Matt Caswell | f7e393b | 2017-02-27 11:19:57 +0000 | [diff] [blame] | 3331 | err: |
| 3332 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 3333 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3334 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3335 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3336 | #define has_bits(i,m) (((i)&(m)) == (m)) |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3337 | |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 3338 | int ssl3_check_cert_and_algorithm(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3339 | { |
Richard Levitte | 60f43e9 | 2015-12-09 23:59:04 +0100 | [diff] [blame] | 3340 | int i; |
| 3341 | #ifndef OPENSSL_NO_EC |
| 3342 | int idx; |
| 3343 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3344 | long alg_k, alg_a; |
| 3345 | EVP_PKEY *pkey = NULL; |
Kurt Roeckx | 26c79d5 | 2015-04-18 12:23:12 +0200 | [diff] [blame] | 3346 | int al = SSL_AD_HANDSHAKE_FAILURE; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3347 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3348 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 3349 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3350 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3351 | /* we don't have a certificate */ |
Matt Caswell | 55a9a16 | 2015-05-12 10:27:53 +0100 | [diff] [blame] | 3352 | if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3353 | return (1); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3354 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3355 | /* This is the passed certificate */ |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3356 | |
Rich Salz | 10bf4fc | 2015-03-10 19:09:27 -0400 | [diff] [blame] | 3357 | #ifndef OPENSSL_NO_EC |
Richard Levitte | 60f43e9 | 2015-12-09 23:59:04 +0100 | [diff] [blame] | 3358 | idx = s->session->peer_type; |
Dr. Stephen Henson | b202155 | 2017-06-16 18:55:28 +0100 | [diff] [blame] | 3359 | if (idx == SSL_PKEY_ECC || idx == SSL_PKEY_ED25519) { |
Dr. Stephen Henson | a273c6e | 2015-06-21 19:08:57 +0100 | [diff] [blame] | 3360 | if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3361 | /* check failed */ |
| 3362 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT); |
| 3363 | goto f_err; |
| 3364 | } else { |
| 3365 | return 1; |
| 3366 | } |
| 3367 | } else if (alg_a & SSL_aECDSA) { |
| 3368 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, |
| 3369 | SSL_R_MISSING_ECDSA_SIGNING_CERT); |
| 3370 | goto f_err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3371 | } |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 3372 | #endif |
Dr. Stephen Henson | 8382fd3 | 2015-12-20 00:32:36 +0000 | [diff] [blame] | 3373 | pkey = X509_get0_pubkey(s->session->peer); |
Dr. Stephen Henson | a273c6e | 2015-06-21 19:08:57 +0100 | [diff] [blame] | 3374 | i = X509_certificate_type(s->session->peer, pkey); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3375 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3376 | /* Check that we have a certificate if we require one */ |
| 3377 | if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) { |
| 3378 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, |
| 3379 | SSL_R_MISSING_RSA_SIGNING_CERT); |
| 3380 | goto f_err; |
| 3381 | } |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 3382 | #ifndef OPENSSL_NO_DSA |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3383 | else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) { |
| 3384 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, |
| 3385 | SSL_R_MISSING_DSA_SIGNING_CERT); |
| 3386 | goto f_err; |
| 3387 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3388 | #endif |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 3389 | #ifndef OPENSSL_NO_RSA |
Kurt Roeckx | 361a119 | 2015-12-05 02:04:41 +0100 | [diff] [blame] | 3390 | if (alg_k & (SSL_kRSA | SSL_kRSAPSK) && |
| 3391 | !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) { |
| 3392 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, |
| 3393 | SSL_R_MISSING_RSA_ENCRYPTING_CERT); |
| 3394 | goto f_err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3395 | } |
Ulf Möller | 79df9d6 | 1999-04-27 03:19:12 +0000 | [diff] [blame] | 3396 | #endif |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 3397 | #ifndef OPENSSL_NO_DH |
Dr. Stephen Henson | fb79abe | 2015-12-17 01:07:46 +0000 | [diff] [blame] | 3398 | if ((alg_k & SSL_kDHE) && (s->s3->peer_tmp == NULL)) { |
Kurt Roeckx | 26c79d5 | 2015-04-18 12:23:12 +0200 | [diff] [blame] | 3399 | al = SSL_AD_INTERNAL_ERROR; |
| 3400 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3401 | goto f_err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3402 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3403 | #endif |
| 3404 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3405 | return (1); |
| 3406 | f_err: |
Kurt Roeckx | 26c79d5 | 2015-04-18 12:23:12 +0200 | [diff] [blame] | 3407 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3408 | return (0); |
| 3409 | } |
Dr. Stephen Henson | 6434abb | 2007-08-11 23:18:29 +0000 | [diff] [blame] | 3410 | |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 3411 | #ifndef OPENSSL_NO_NEXTPROTONEG |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 3412 | int tls_construct_next_proto(SSL *s, WPACKET *pkt) |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3413 | { |
Matt Caswell | 15e6be6 | 2016-09-14 12:10:33 +0100 | [diff] [blame] | 3414 | size_t len, padding_len; |
| 3415 | unsigned char *padding = NULL; |
Matt Caswell | 15e6be6 | 2016-09-14 12:10:33 +0100 | [diff] [blame] | 3416 | |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 3417 | len = s->ext.npn_len; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3418 | padding_len = 32 - ((len + 2) % 32); |
Matt Caswell | 15e6be6 | 2016-09-14 12:10:33 +0100 | [diff] [blame] | 3419 | |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 3420 | if (!WPACKET_sub_memcpy_u8(pkt, s->ext.npn, len) |
Matt Caswell | 7cea05d | 2016-09-29 23:28:29 +0100 | [diff] [blame] | 3421 | || !WPACKET_sub_allocate_bytes_u8(pkt, padding_len, &padding)) { |
Matt Caswell | 15e6be6 | 2016-09-14 12:10:33 +0100 | [diff] [blame] | 3422 | SSLerr(SSL_F_TLS_CONSTRUCT_NEXT_PROTO, ERR_R_INTERNAL_ERROR); |
| 3423 | goto err; |
| 3424 | } |
| 3425 | |
| 3426 | memset(padding, 0, padding_len); |
| 3427 | |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3428 | return 1; |
Matt Caswell | 15e6be6 | 2016-09-14 12:10:33 +0100 | [diff] [blame] | 3429 | err: |
Matt Caswell | 15e6be6 | 2016-09-14 12:10:33 +0100 | [diff] [blame] | 3430 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 3431 | return 0; |
Matt Caswell | b9908bf | 2015-07-29 14:08:49 +0100 | [diff] [blame] | 3432 | } |
Dr. Stephen Henson | 6434abb | 2007-08-11 23:18:29 +0000 | [diff] [blame] | 3433 | #endif |
Dr. Stephen Henson | 368888b | 2008-06-01 22:33:24 +0000 | [diff] [blame] | 3434 | |
Matt Caswell | c7f4778 | 2017-01-10 23:02:28 +0000 | [diff] [blame] | 3435 | MSG_PROCESS_RETURN tls_process_hello_req(SSL *s, PACKET *pkt) |
| 3436 | { |
| 3437 | if (PACKET_remaining(pkt) > 0) { |
| 3438 | /* should contain no data */ |
| 3439 | SSLerr(SSL_F_TLS_PROCESS_HELLO_REQ, SSL_R_LENGTH_MISMATCH); |
| 3440 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 3441 | ossl_statem_set_error(s); |
| 3442 | return MSG_PROCESS_ERROR; |
| 3443 | } |
| 3444 | |
Todd Short | db0f35d | 2017-05-10 16:46:14 -0400 | [diff] [blame] | 3445 | if ((s->options & SSL_OP_NO_RENEGOTIATION)) { |
| 3446 | ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION); |
| 3447 | return MSG_PROCESS_FINISHED_READING; |
| 3448 | } |
| 3449 | |
Matt Caswell | c7f4778 | 2017-01-10 23:02:28 +0000 | [diff] [blame] | 3450 | /* |
Matt Caswell | 1f04f23 | 2017-01-27 15:23:25 +0000 | [diff] [blame] | 3451 | * This is a historical discrepancy (not in the RFC) maintained for |
| 3452 | * compatibility reasons. If a TLS client receives a HelloRequest it will |
| 3453 | * attempt an abbreviated handshake. However if a DTLS client receives a |
| 3454 | * HelloRequest it will do a full handshake. Either behaviour is reasonable |
| 3455 | * but doing one for TLS and another for DTLS is odd. |
Matt Caswell | c7f4778 | 2017-01-10 23:02:28 +0000 | [diff] [blame] | 3456 | */ |
| 3457 | if (SSL_IS_DTLS(s)) |
| 3458 | SSL_renegotiate(s); |
| 3459 | else |
| 3460 | SSL_renegotiate_abbreviated(s); |
| 3461 | |
| 3462 | return MSG_PROCESS_FINISHED_READING; |
| 3463 | } |
| 3464 | |
Matt Caswell | e46f233 | 2016-11-23 15:20:22 +0000 | [diff] [blame] | 3465 | static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL *s, PACKET *pkt) |
| 3466 | { |
| 3467 | int al = SSL_AD_INTERNAL_ERROR; |
| 3468 | PACKET extensions; |
Matt Caswell | 3434f40 | 2016-11-28 16:45:52 +0000 | [diff] [blame] | 3469 | RAW_EXTENSION *rawexts = NULL; |
Matt Caswell | e46f233 | 2016-11-23 15:20:22 +0000 | [diff] [blame] | 3470 | |
Matt Caswell | 26b9172 | 2017-05-11 11:31:57 +0100 | [diff] [blame] | 3471 | if (!PACKET_as_length_prefixed_2(pkt, &extensions) |
| 3472 | || PACKET_remaining(pkt) != 0) { |
Matt Caswell | e46f233 | 2016-11-23 15:20:22 +0000 | [diff] [blame] | 3473 | al = SSL_AD_DECODE_ERROR; |
| 3474 | SSLerr(SSL_F_TLS_PROCESS_ENCRYPTED_EXTENSIONS, SSL_R_LENGTH_MISMATCH); |
| 3475 | goto err; |
| 3476 | } |
| 3477 | |
Matt Caswell | fe874d2 | 2017-04-04 11:40:02 +0100 | [diff] [blame] | 3478 | if (!tls_collect_extensions(s, &extensions, |
| 3479 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS, &rawexts, |
Tatsuhiro Tsujikawa | 735d5b5 | 2017-04-18 23:59:39 +0900 | [diff] [blame] | 3480 | &al, NULL, 1) |
Matt Caswell | fe874d2 | 2017-04-04 11:40:02 +0100 | [diff] [blame] | 3481 | || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS, |
Tatsuhiro Tsujikawa | 735d5b5 | 2017-04-18 23:59:39 +0900 | [diff] [blame] | 3482 | rawexts, NULL, 0, &al, 1)) |
Matt Caswell | 3434f40 | 2016-11-28 16:45:52 +0000 | [diff] [blame] | 3483 | goto err; |
| 3484 | |
Matt Caswell | 1b0286a | 2016-12-05 17:31:37 +0000 | [diff] [blame] | 3485 | OPENSSL_free(rawexts); |
Matt Caswell | e46f233 | 2016-11-23 15:20:22 +0000 | [diff] [blame] | 3486 | return MSG_PROCESS_CONTINUE_READING; |
| 3487 | |
| 3488 | err: |
| 3489 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 3490 | ossl_statem_set_error(s); |
Matt Caswell | 1b0286a | 2016-12-05 17:31:37 +0000 | [diff] [blame] | 3491 | OPENSSL_free(rawexts); |
Matt Caswell | e46f233 | 2016-11-23 15:20:22 +0000 | [diff] [blame] | 3492 | return MSG_PROCESS_ERROR; |
| 3493 | } |
| 3494 | |
Dr. Stephen Henson | 368888b | 2008-06-01 22:33:24 +0000 | [diff] [blame] | 3495 | int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3496 | { |
| 3497 | int i = 0; |
Dr. Stephen Henson | 368888b | 2008-06-01 22:33:24 +0000 | [diff] [blame] | 3498 | #ifndef OPENSSL_NO_ENGINE |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3499 | if (s->ctx->client_cert_engine) { |
| 3500 | i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s, |
| 3501 | SSL_get_client_CA_list(s), |
| 3502 | px509, ppkey, NULL, NULL, NULL); |
| 3503 | if (i != 0) |
| 3504 | return i; |
| 3505 | } |
Dr. Stephen Henson | 368888b | 2008-06-01 22:33:24 +0000 | [diff] [blame] | 3506 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3507 | if (s->ctx->client_cert_cb) |
| 3508 | i = s->ctx->client_cert_cb(s, px509, ppkey); |
| 3509 | return i; |
| 3510 | } |
Matt Caswell | d45ba43 | 2015-04-24 15:05:27 +0100 | [diff] [blame] | 3511 | |
Matt Caswell | ae2f7b3 | 2016-09-05 17:34:04 +0100 | [diff] [blame] | 3512 | int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, WPACKET *pkt) |
Matt Caswell | d45ba43 | 2015-04-24 15:05:27 +0100 | [diff] [blame] | 3513 | { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 3514 | int i; |
Matt Caswell | aafec89 | 2017-04-26 10:38:32 +0100 | [diff] [blame] | 3515 | size_t totlen = 0, len, maxlen, maxverok = 0; |
Matt Caswell | d45ba43 | 2015-04-24 15:05:27 +0100 | [diff] [blame] | 3516 | int empty_reneg_info_scsv = !s->renegotiate; |
| 3517 | /* Set disabled masks for this session */ |
| 3518 | ssl_set_client_disabled(s); |
| 3519 | |
| 3520 | if (sk == NULL) |
| 3521 | return (0); |
Matt Caswell | d45ba43 | 2015-04-24 15:05:27 +0100 | [diff] [blame] | 3522 | |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 3523 | #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH |
| 3524 | # if OPENSSL_MAX_TLS1_2_CIPHER_LENGTH < 6 |
| 3525 | # error Max cipher length too short |
| 3526 | # endif |
| 3527 | /* |
| 3528 | * Some servers hang if client hello > 256 bytes as hack workaround |
| 3529 | * chop number of supported ciphers to keep it well below this if we |
| 3530 | * use TLS v1.2 |
| 3531 | */ |
| 3532 | if (TLS1_get_version(s) >= TLS1_2_VERSION) |
| 3533 | maxlen = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1; |
| 3534 | else |
| 3535 | #endif |
| 3536 | /* Maximum length that can be stored in 2 bytes. Length must be even */ |
| 3537 | maxlen = 0xfffe; |
| 3538 | |
| 3539 | if (empty_reneg_info_scsv) |
| 3540 | maxlen -= 2; |
| 3541 | if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) |
| 3542 | maxlen -= 2; |
| 3543 | |
| 3544 | for (i = 0; i < sk_SSL_CIPHER_num(sk) && totlen < maxlen; i++) { |
| 3545 | const SSL_CIPHER *c; |
| 3546 | |
Matt Caswell | d45ba43 | 2015-04-24 15:05:27 +0100 | [diff] [blame] | 3547 | c = sk_SSL_CIPHER_value(sk, i); |
| 3548 | /* Skip disabled ciphers */ |
Matt Caswell | 8af91fd | 2017-04-12 17:02:42 +0100 | [diff] [blame] | 3549 | if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0)) |
Matt Caswell | d45ba43 | 2015-04-24 15:05:27 +0100 | [diff] [blame] | 3550 | continue; |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 3551 | |
| 3552 | if (!s->method->put_cipher_by_char(c, pkt, &len)) { |
| 3553 | SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR); |
| 3554 | return 0; |
| 3555 | } |
| 3556 | |
Matt Caswell | aafec89 | 2017-04-26 10:38:32 +0100 | [diff] [blame] | 3557 | /* Sanity check that the maximum version we offer has ciphers enabled */ |
| 3558 | if (!maxverok) { |
| 3559 | if (SSL_IS_DTLS(s)) { |
| 3560 | if (DTLS_VERSION_GE(c->max_dtls, s->s3->tmp.max_ver) |
| 3561 | && DTLS_VERSION_LE(c->min_dtls, s->s3->tmp.max_ver)) |
| 3562 | maxverok = 1; |
| 3563 | } else { |
| 3564 | if (c->max_tls >= s->s3->tmp.max_ver |
| 3565 | && c->min_tls <= s->s3->tmp.max_ver) |
| 3566 | maxverok = 1; |
| 3567 | } |
| 3568 | } |
| 3569 | |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 3570 | totlen += len; |
Matt Caswell | d45ba43 | 2015-04-24 15:05:27 +0100 | [diff] [blame] | 3571 | } |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 3572 | |
Matt Caswell | aafec89 | 2017-04-26 10:38:32 +0100 | [diff] [blame] | 3573 | if (totlen == 0 || !maxverok) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 3574 | SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, SSL_R_NO_CIPHERS_AVAILABLE); |
Matt Caswell | aafec89 | 2017-04-26 10:38:32 +0100 | [diff] [blame] | 3575 | |
| 3576 | if (!maxverok) |
| 3577 | ERR_add_error_data(1, "No ciphers enabled for max supported " |
| 3578 | "SSL/TLS version"); |
| 3579 | |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 3580 | return 0; |
| 3581 | } |
| 3582 | |
| 3583 | if (totlen != 0) { |
Matt Caswell | d45ba43 | 2015-04-24 15:05:27 +0100 | [diff] [blame] | 3584 | if (empty_reneg_info_scsv) { |
| 3585 | static SSL_CIPHER scsv = { |
| 3586 | 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 3587 | }; |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 3588 | if (!s->method->put_cipher_by_char(&scsv, pkt, &len)) { |
| 3589 | SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR); |
| 3590 | return 0; |
| 3591 | } |
Matt Caswell | d45ba43 | 2015-04-24 15:05:27 +0100 | [diff] [blame] | 3592 | } |
| 3593 | if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) { |
| 3594 | static SSL_CIPHER scsv = { |
| 3595 | 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 3596 | }; |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 3597 | if (!s->method->put_cipher_by_char(&scsv, pkt, &len)) { |
| 3598 | SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR); |
| 3599 | return 0; |
| 3600 | } |
Matt Caswell | d45ba43 | 2015-04-24 15:05:27 +0100 | [diff] [blame] | 3601 | } |
| 3602 | } |
| 3603 | |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 3604 | return 1; |
Matt Caswell | d45ba43 | 2015-04-24 15:05:27 +0100 | [diff] [blame] | 3605 | } |
Matt Caswell | ef6c191 | 2017-03-09 15:03:07 +0000 | [diff] [blame] | 3606 | |
| 3607 | int tls_construct_end_of_early_data(SSL *s, WPACKET *pkt) |
| 3608 | { |
| 3609 | if (s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY |
| 3610 | && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING) { |
| 3611 | SSLerr(SSL_F_TLS_CONSTRUCT_END_OF_EARLY_DATA, |
| 3612 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 3613 | return 0; |
| 3614 | } |
| 3615 | |
| 3616 | s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING; |
| 3617 | return 1; |
| 3618 | } |