]>
Commit | Line | Data |
---|---|---|
846e33c7 RS |
1 | /* |
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. | |
8c74b5e5 | 3 | * |
846e33c7 RS |
4 | * Licensed under the OpenSSL license (the "License"). You may not use |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
8c74b5e5 | 8 | */ |
846e33c7 | 9 | |
ea262260 BM |
10 | /* ==================================================================== |
11 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | |
12 | * | |
0f113f3e | 13 | * Portions of the attached software ("Contribution") are developed by |
ea262260 BM |
14 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
15 | * | |
16 | * The Contribution is licensed pursuant to the OpenSSL open source | |
17 | * license provided above. | |
18 | * | |
ea262260 BM |
19 | * ECC cipher suite support in OpenSSL originally written by |
20 | * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. | |
21 | * | |
22 | */ | |
ddac1974 NL |
23 | /* ==================================================================== |
24 | * Copyright 2005 Nokia. All rights reserved. | |
25 | * | |
26 | * The portions of the attached software ("Contribution") is developed by | |
27 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source | |
28 | * license. | |
29 | * | |
30 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of | |
31 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites | |
32 | * support (see RFC 4279) to OpenSSL. | |
33 | * | |
34 | * No patent licenses or other rights except those expressly stated in | |
35 | * the OpenSSL open source license shall be deemed granted or received | |
36 | * expressly, by implication, estoppel, or otherwise. | |
37 | * | |
38 | * No assurances are provided by Nokia that the Contribution does not | |
39 | * infringe the patent or other intellectual property rights of any third | |
40 | * party or that the license provides you with all the necessary rights | |
41 | * to make use of the Contribution. | |
42 | * | |
43 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN | |
44 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA | |
45 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY | |
46 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR | |
47 | * OTHERWISE. | |
48 | */ | |
d02b48c6 RE |
49 | |
50 | #include <stdio.h> | |
8ba708e5 | 51 | #include "../ssl_locl.h" |
61ae935a | 52 | #include "statem_locl.h" |
ec577822 BM |
53 | #include <openssl/buffer.h> |
54 | #include <openssl/rand.h> | |
55 | #include <openssl/objects.h> | |
56 | #include <openssl/evp.h> | |
dbad1690 | 57 | #include <openssl/md5.h> |
3c27208f | 58 | #include <openssl/dh.h> |
d095b68d | 59 | #include <openssl/bn.h> |
3c27208f | 60 | #include <openssl/engine.h> |
f9b3bff6 | 61 | |
e46f2334 MC |
62 | static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL *s, PACKET *pkt); |
63 | ||
7ab09630 | 64 | static ossl_inline int cert_req_allowed(SSL *s); |
a455d0f6 | 65 | static int key_exchange_expected(SSL *s); |
0f113f3e | 66 | static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b); |
d45ba43d | 67 | static int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, |
ae2f7b37 | 68 | WPACKET *pkt); |
ea262260 | 69 | |
61ae935a MC |
70 | /* |
71 | * Is a CertificateRequest message allowed at the moment or not? | |
72 | * | |
73 | * Return values are: | |
74 | * 1: Yes | |
75 | * 0: No | |
76 | */ | |
7ab09630 | 77 | static ossl_inline int cert_req_allowed(SSL *s) |
61ae935a MC |
78 | { |
79 | /* TLS does not like anon-DH with client cert */ | |
b7fa1f98 | 80 | if ((s->version > SSL3_VERSION |
a230b26e EK |
81 | && (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)) |
82 | || (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aSRP | SSL_aPSK))) | |
61ae935a MC |
83 | return 0; |
84 | ||
85 | return 1; | |
86 | } | |
87 | ||
88 | /* | |
a455d0f6 | 89 | * Should we expect the ServerKeyExchange message or not? |
61ae935a MC |
90 | * |
91 | * Return values are: | |
92 | * 1: Yes | |
93 | * 0: No | |
94 | */ | |
a455d0f6 | 95 | static int key_exchange_expected(SSL *s) |
61ae935a MC |
96 | { |
97 | long alg_k = s->s3->tmp.new_cipher->algorithm_mkey; | |
98 | ||
99 | /* | |
100 | * Can't skip server key exchange if this is an ephemeral | |
a455d0f6 | 101 | * ciphersuite or for SRP |
61ae935a | 102 | */ |
a455d0f6 MC |
103 | if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK |
104 | | SSL_kSRP)) { | |
105 | return 1; | |
61ae935a MC |
106 | } |
107 | ||
a455d0f6 | 108 | return 0; |
61ae935a MC |
109 | } |
110 | ||
0f1e51ea MC |
111 | /* |
112 | * ossl_statem_client_read_transition() encapsulates the logic for the allowed | |
113 | * handshake state transitions when a TLS1.3 client is reading messages from the | |
114 | * server. The message type that the server has sent is provided in |mt|. The | |
115 | * current state is in |s->statem.hand_state|. | |
116 | * | |
94ed2c67 MC |
117 | * Return values are 1 for success (transition allowed) and 0 on error |
118 | * (transition not allowed) | |
0f1e51ea MC |
119 | */ |
120 | static int ossl_statem_client13_read_transition(SSL *s, int mt) | |
121 | { | |
122 | OSSL_STATEM *st = &s->statem; | |
123 | ||
94ed2c67 MC |
124 | /* |
125 | * TODO(TLS1.3): This is still based on the TLSv1.2 state machine. Over time | |
126 | * we will update this to look more like real TLSv1.3 | |
127 | */ | |
128 | ||
0f1e51ea MC |
129 | /* |
130 | * Note: There is no case for TLS_ST_CW_CLNT_HELLO, because we haven't | |
131 | * yet negotiated TLSv1.3 at that point so that is handled by | |
132 | * ossl_statem_client_read_transition() | |
133 | */ | |
134 | ||
135 | switch (st->hand_state) { | |
136 | default: | |
137 | break; | |
138 | ||
139 | case TLS_ST_CR_SRVR_HELLO: | |
e46f2334 MC |
140 | if (mt == SSL3_MT_ENCRYPTED_EXTENSIONS) { |
141 | st->hand_state = TLS_ST_CR_ENCRYPTED_EXTENSIONS; | |
142 | return 1; | |
143 | } | |
144 | break; | |
145 | ||
146 | case TLS_ST_CR_ENCRYPTED_EXTENSIONS: | |
0f1e51ea | 147 | if (s->hit) { |
92760c21 MC |
148 | if (mt == SSL3_MT_FINISHED) { |
149 | st->hand_state = TLS_ST_CR_FINISHED; | |
0f1e51ea MC |
150 | return 1; |
151 | } | |
152 | } else { | |
92760c21 MC |
153 | if (mt == SSL3_MT_CERTIFICATE_REQUEST) { |
154 | st->hand_state = TLS_ST_CR_CERT_REQ; | |
155 | return 1; | |
f5ca0b04 MC |
156 | } |
157 | if (mt == SSL3_MT_CERTIFICATE) { | |
0f1e51ea MC |
158 | st->hand_state = TLS_ST_CR_CERT; |
159 | return 1; | |
160 | } | |
161 | } | |
162 | break; | |
163 | ||
92760c21 MC |
164 | case TLS_ST_CR_CERT_REQ: |
165 | if (mt == SSL3_MT_CERTIFICATE) { | |
166 | st->hand_state = TLS_ST_CR_CERT; | |
167 | return 1; | |
168 | } | |
169 | break; | |
170 | ||
0f1e51ea | 171 | case TLS_ST_CR_CERT: |
0f1e51ea MC |
172 | if (mt == SSL3_MT_FINISHED) { |
173 | st->hand_state = TLS_ST_CR_FINISHED; | |
174 | return 1; | |
175 | } | |
176 | break; | |
92760c21 | 177 | |
0f1e51ea MC |
178 | } |
179 | ||
0f1e51ea | 180 | /* No valid transition found */ |
0f1e51ea MC |
181 | return 0; |
182 | } | |
183 | ||
61ae935a | 184 | /* |
8481f583 MC |
185 | * ossl_statem_client_read_transition() encapsulates the logic for the allowed |
186 | * handshake state transitions when the client is reading messages from the | |
187 | * server. The message type that the server has sent is provided in |mt|. The | |
188 | * current state is in |s->statem.hand_state|. | |
61ae935a | 189 | * |
94ed2c67 MC |
190 | * Return values are 1 for success (transition allowed) and 0 on error |
191 | * (transition not allowed) | |
61ae935a | 192 | */ |
8481f583 | 193 | int ossl_statem_client_read_transition(SSL *s, int mt) |
61ae935a | 194 | { |
d6f1a6e9 | 195 | OSSL_STATEM *st = &s->statem; |
a455d0f6 | 196 | int ske_expected; |
61ae935a | 197 | |
0f1e51ea MC |
198 | /* |
199 | * Note that after a ClientHello we don't know what version we are going | |
200 | * to negotiate yet, so we don't take this branch until later | |
201 | */ | |
f5ca0b04 | 202 | if (SSL_IS_TLS13(s)) { |
5abeaf35 MC |
203 | if (!ossl_statem_client13_read_transition(s, mt)) |
204 | goto err; | |
205 | return 1; | |
206 | } | |
0f1e51ea | 207 | |
a230b26e | 208 | switch (st->hand_state) { |
f3b3d7f0 RS |
209 | default: |
210 | break; | |
211 | ||
61ae935a MC |
212 | case TLS_ST_CW_CLNT_HELLO: |
213 | if (mt == SSL3_MT_SERVER_HELLO) { | |
214 | st->hand_state = TLS_ST_CR_SRVR_HELLO; | |
215 | return 1; | |
216 | } | |
217 | ||
218 | if (SSL_IS_DTLS(s)) { | |
219 | if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) { | |
220 | st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST; | |
221 | return 1; | |
222 | } | |
223 | } | |
224 | break; | |
225 | ||
226 | case TLS_ST_CR_SRVR_HELLO: | |
227 | if (s->hit) { | |
aff8c126 | 228 | if (s->ext.ticket_expected) { |
61ae935a MC |
229 | if (mt == SSL3_MT_NEWSESSION_TICKET) { |
230 | st->hand_state = TLS_ST_CR_SESSION_TICKET; | |
231 | return 1; | |
232 | } | |
233 | } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { | |
234 | st->hand_state = TLS_ST_CR_CHANGE; | |
235 | return 1; | |
236 | } | |
237 | } else { | |
238 | if (SSL_IS_DTLS(s) && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) { | |
239 | st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST; | |
240 | return 1; | |
ad3819c2 | 241 | } else if (s->version >= TLS1_VERSION |
aff8c126 RS |
242 | && s->ext.session_secret_cb != NULL |
243 | && s->session->ext.tick != NULL | |
a230b26e | 244 | && mt == SSL3_MT_CHANGE_CIPHER_SPEC) { |
ad3819c2 MC |
245 | /* |
246 | * Normally, we can tell if the server is resuming the session | |
247 | * from the session ID. EAP-FAST (RFC 4851), however, relies on | |
248 | * the next server message after the ServerHello to determine if | |
249 | * the server is resuming. | |
250 | */ | |
251 | s->hit = 1; | |
252 | st->hand_state = TLS_ST_CR_CHANGE; | |
253 | return 1; | |
61ae935a | 254 | } else if (!(s->s3->tmp.new_cipher->algorithm_auth |
a230b26e | 255 | & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) { |
61ae935a MC |
256 | if (mt == SSL3_MT_CERTIFICATE) { |
257 | st->hand_state = TLS_ST_CR_CERT; | |
258 | return 1; | |
259 | } | |
260 | } else { | |
a455d0f6 | 261 | ske_expected = key_exchange_expected(s); |
a455d0f6 MC |
262 | /* SKE is optional for some PSK ciphersuites */ |
263 | if (ske_expected | |
a230b26e EK |
264 | || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK) |
265 | && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) { | |
a455d0f6 MC |
266 | if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) { |
267 | st->hand_state = TLS_ST_CR_KEY_EXCH; | |
268 | return 1; | |
269 | } | |
270 | } else if (mt == SSL3_MT_CERTIFICATE_REQUEST | |
a230b26e EK |
271 | && cert_req_allowed(s)) { |
272 | st->hand_state = TLS_ST_CR_CERT_REQ; | |
273 | return 1; | |
a455d0f6 | 274 | } else if (mt == SSL3_MT_SERVER_DONE) { |
a230b26e EK |
275 | st->hand_state = TLS_ST_CR_SRVR_DONE; |
276 | return 1; | |
61ae935a MC |
277 | } |
278 | } | |
279 | } | |
280 | break; | |
281 | ||
282 | case TLS_ST_CR_CERT: | |
bb1aaab4 MC |
283 | /* |
284 | * The CertificateStatus message is optional even if | |
aff8c126 | 285 | * |ext.status_expected| is set |
bb1aaab4 | 286 | */ |
aff8c126 | 287 | if (s->ext.status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) { |
bb1aaab4 MC |
288 | st->hand_state = TLS_ST_CR_CERT_STATUS; |
289 | return 1; | |
a455d0f6 MC |
290 | } |
291 | /* Fall through */ | |
292 | ||
293 | case TLS_ST_CR_CERT_STATUS: | |
294 | ske_expected = key_exchange_expected(s); | |
a455d0f6 | 295 | /* SKE is optional for some PSK ciphersuites */ |
a230b26e EK |
296 | if (ske_expected || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK) |
297 | && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) { | |
61ae935a MC |
298 | if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) { |
299 | st->hand_state = TLS_ST_CR_KEY_EXCH; | |
300 | return 1; | |
61ae935a | 301 | } |
672f3337 | 302 | goto err; |
61ae935a | 303 | } |
a455d0f6 | 304 | /* Fall through */ |
61ae935a | 305 | |
a455d0f6 MC |
306 | case TLS_ST_CR_KEY_EXCH: |
307 | if (mt == SSL3_MT_CERTIFICATE_REQUEST) { | |
308 | if (cert_req_allowed(s)) { | |
61ae935a MC |
309 | st->hand_state = TLS_ST_CR_CERT_REQ; |
310 | return 1; | |
61ae935a | 311 | } |
672f3337 | 312 | goto err; |
61ae935a | 313 | } |
a455d0f6 | 314 | /* Fall through */ |
61ae935a MC |
315 | |
316 | case TLS_ST_CR_CERT_REQ: | |
317 | if (mt == SSL3_MT_SERVER_DONE) { | |
318 | st->hand_state = TLS_ST_CR_SRVR_DONE; | |
319 | return 1; | |
320 | } | |
321 | break; | |
322 | ||
323 | case TLS_ST_CW_FINISHED: | |
aff8c126 | 324 | if (s->ext.ticket_expected) { |
c45d6b2b DB |
325 | if (mt == SSL3_MT_NEWSESSION_TICKET) { |
326 | st->hand_state = TLS_ST_CR_SESSION_TICKET; | |
327 | return 1; | |
328 | } | |
61ae935a MC |
329 | } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { |
330 | st->hand_state = TLS_ST_CR_CHANGE; | |
331 | return 1; | |
332 | } | |
333 | break; | |
334 | ||
335 | case TLS_ST_CR_SESSION_TICKET: | |
336 | 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_CHANGE: | |
343 | if (mt == SSL3_MT_FINISHED) { | |
344 | st->hand_state = TLS_ST_CR_FINISHED; | |
345 | return 1; | |
346 | } | |
347 | break; | |
61ae935a MC |
348 | } |
349 | ||
672f3337 | 350 | err: |
61ae935a | 351 | /* No valid transition found */ |
672f3337 | 352 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE); |
340a2828 | 353 | SSLerr(SSL_F_OSSL_STATEM_CLIENT_READ_TRANSITION, SSL_R_UNEXPECTED_MESSAGE); |
61ae935a MC |
354 | return 0; |
355 | } | |
356 | ||
357 | /* | |
0f1e51ea MC |
358 | * ossl_statem_client13_write_transition() works out what handshake state to |
359 | * move to next when the TLSv1.3 client is writing messages to be sent to the | |
360 | * server. | |
0f1e51ea MC |
361 | */ |
362 | static WRITE_TRAN ossl_statem_client13_write_transition(SSL *s) | |
363 | { | |
364 | OSSL_STATEM *st = &s->statem; | |
365 | ||
94ed2c67 MC |
366 | /* |
367 | * TODO(TLS1.3): This is still based on the TLSv1.2 state machine. Over time | |
368 | * we will update this to look more like real TLSv1.3 | |
369 | */ | |
370 | ||
0f1e51ea MC |
371 | /* |
372 | * Note: There are no cases for TLS_ST_BEFORE or TLS_ST_CW_CLNT_HELLO, | |
373 | * because we haven't negotiated TLSv1.3 yet at that point. They are | |
374 | * handled by ossl_statem_client_write_transition(). | |
375 | */ | |
376 | switch (st->hand_state) { | |
377 | default: | |
378 | /* Shouldn't happen */ | |
379 | return WRITE_TRAN_ERROR; | |
380 | ||
92760c21 | 381 | case TLS_ST_CR_FINISHED: |
94ed2c67 | 382 | st->hand_state = (s->s3->tmp.cert_req != 0) ? TLS_ST_CW_CERT |
92760c21 | 383 | : TLS_ST_CW_FINISHED; |
0f1e51ea MC |
384 | return WRITE_TRAN_CONTINUE; |
385 | ||
386 | case TLS_ST_CW_CERT: | |
387 | /* If a non-empty Certificate we also send CertificateVerify */ | |
94ed2c67 | 388 | st->hand_state = (s->s3->tmp.cert_req == 1) ? TLS_ST_CW_CERT_VRFY |
92760c21 | 389 | : TLS_ST_CW_FINISHED; |
0f1e51ea MC |
390 | return WRITE_TRAN_CONTINUE; |
391 | ||
392 | case TLS_ST_CW_CERT_VRFY: | |
0f1e51ea MC |
393 | st->hand_state = TLS_ST_CW_FINISHED; |
394 | return WRITE_TRAN_CONTINUE; | |
395 | ||
396 | case TLS_ST_CW_FINISHED: | |
94ed2c67 MC |
397 | st->hand_state = TLS_ST_OK; |
398 | ossl_statem_set_in_init(s, 0); | |
399 | return WRITE_TRAN_CONTINUE; | |
0f1e51ea MC |
400 | } |
401 | } | |
402 | ||
403 | /* | |
404 | * ossl_statem_client_write_transition() works out what handshake state to | |
405 | * move to next when the client is writing messages to be sent to the server. | |
61ae935a | 406 | */ |
8481f583 | 407 | WRITE_TRAN ossl_statem_client_write_transition(SSL *s) |
61ae935a | 408 | { |
d6f1a6e9 | 409 | OSSL_STATEM *st = &s->statem; |
61ae935a | 410 | |
0f1e51ea MC |
411 | /* |
412 | * Note that immediately before/after a ClientHello we don't know what | |
413 | * version we are going to negotiate yet, so we don't take this branch until | |
414 | * later | |
415 | */ | |
f5ca0b04 | 416 | if (SSL_IS_TLS13(s)) |
0f1e51ea MC |
417 | return ossl_statem_client13_write_transition(s); |
418 | ||
a230b26e | 419 | switch (st->hand_state) { |
f3b3d7f0 RS |
420 | default: |
421 | /* Shouldn't happen */ | |
422 | return WRITE_TRAN_ERROR; | |
423 | ||
a230b26e EK |
424 | case TLS_ST_OK: |
425 | /* Renegotiation - fall through */ | |
426 | case TLS_ST_BEFORE: | |
427 | st->hand_state = TLS_ST_CW_CLNT_HELLO; | |
428 | return WRITE_TRAN_CONTINUE; | |
61ae935a | 429 | |
a230b26e EK |
430 | case TLS_ST_CW_CLNT_HELLO: |
431 | /* | |
432 | * No transition at the end of writing because we don't know what | |
433 | * we will be sent | |
434 | */ | |
435 | return WRITE_TRAN_FINISHED; | |
61ae935a | 436 | |
a230b26e EK |
437 | case DTLS_ST_CR_HELLO_VERIFY_REQUEST: |
438 | st->hand_state = TLS_ST_CW_CLNT_HELLO; | |
439 | return WRITE_TRAN_CONTINUE; | |
61ae935a | 440 | |
a230b26e EK |
441 | case TLS_ST_CR_SRVR_DONE: |
442 | if (s->s3->tmp.cert_req) | |
443 | st->hand_state = TLS_ST_CW_CERT; | |
444 | else | |
61ae935a | 445 | st->hand_state = TLS_ST_CW_KEY_EXCH; |
a230b26e | 446 | return WRITE_TRAN_CONTINUE; |
61ae935a | 447 | |
a230b26e EK |
448 | case TLS_ST_CW_CERT: |
449 | st->hand_state = TLS_ST_CW_KEY_EXCH; | |
450 | return WRITE_TRAN_CONTINUE; | |
61ae935a | 451 | |
a230b26e EK |
452 | case TLS_ST_CW_KEY_EXCH: |
453 | /* | |
454 | * For TLS, cert_req is set to 2, so a cert chain of nothing is | |
455 | * sent, but no verify packet is sent | |
456 | */ | |
457 | /* | |
458 | * XXX: For now, we do not support client authentication in ECDH | |
459 | * cipher suites with ECDH (rather than ECDSA) certificates. We | |
460 | * need to skip the certificate verify message when client's | |
461 | * ECDH public key is sent inside the client certificate. | |
462 | */ | |
463 | if (s->s3->tmp.cert_req == 1) { | |
464 | st->hand_state = TLS_ST_CW_CERT_VRFY; | |
465 | } else { | |
61ae935a | 466 | st->hand_state = TLS_ST_CW_CHANGE; |
a230b26e EK |
467 | } |
468 | if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) { | |
469 | st->hand_state = TLS_ST_CW_CHANGE; | |
470 | } | |
471 | return WRITE_TRAN_CONTINUE; | |
61ae935a | 472 | |
a230b26e EK |
473 | case TLS_ST_CW_CERT_VRFY: |
474 | st->hand_state = TLS_ST_CW_CHANGE; | |
475 | return WRITE_TRAN_CONTINUE; | |
476 | ||
477 | case TLS_ST_CW_CHANGE: | |
61ae935a | 478 | #if defined(OPENSSL_NO_NEXTPROTONEG) |
a230b26e | 479 | st->hand_state = TLS_ST_CW_FINISHED; |
61ae935a | 480 | #else |
aff8c126 | 481 | if (!SSL_IS_DTLS(s) && s->s3->npn_seen) |
a230b26e EK |
482 | st->hand_state = TLS_ST_CW_NEXT_PROTO; |
483 | else | |
484 | st->hand_state = TLS_ST_CW_FINISHED; | |
61ae935a | 485 | #endif |
a230b26e | 486 | return WRITE_TRAN_CONTINUE; |
61ae935a MC |
487 | |
488 | #if !defined(OPENSSL_NO_NEXTPROTONEG) | |
a230b26e EK |
489 | case TLS_ST_CW_NEXT_PROTO: |
490 | st->hand_state = TLS_ST_CW_FINISHED; | |
491 | return WRITE_TRAN_CONTINUE; | |
61ae935a MC |
492 | #endif |
493 | ||
a230b26e EK |
494 | case TLS_ST_CW_FINISHED: |
495 | if (s->hit) { | |
496 | st->hand_state = TLS_ST_OK; | |
497 | ossl_statem_set_in_init(s, 0); | |
498 | return WRITE_TRAN_CONTINUE; | |
499 | } else { | |
500 | return WRITE_TRAN_FINISHED; | |
501 | } | |
61ae935a | 502 | |
a230b26e EK |
503 | case TLS_ST_CR_FINISHED: |
504 | if (s->hit) { | |
505 | st->hand_state = TLS_ST_CW_CHANGE; | |
506 | return WRITE_TRAN_CONTINUE; | |
507 | } else { | |
508 | st->hand_state = TLS_ST_OK; | |
509 | ossl_statem_set_in_init(s, 0); | |
510 | return WRITE_TRAN_CONTINUE; | |
511 | } | |
61ae935a MC |
512 | } |
513 | } | |
514 | ||
515 | /* | |
516 | * Perform any pre work that needs to be done prior to sending a message from | |
517 | * the client to the server. | |
518 | */ | |
8481f583 | 519 | WORK_STATE ossl_statem_client_pre_work(SSL *s, WORK_STATE wst) |
61ae935a | 520 | { |
d6f1a6e9 | 521 | OSSL_STATEM *st = &s->statem; |
61ae935a | 522 | |
a230b26e | 523 | switch (st->hand_state) { |
f3b3d7f0 RS |
524 | default: |
525 | /* No pre work to be done */ | |
526 | break; | |
527 | ||
61ae935a MC |
528 | case TLS_ST_CW_CLNT_HELLO: |
529 | s->shutdown = 0; | |
530 | if (SSL_IS_DTLS(s)) { | |
531 | /* every DTLS ClientHello resets Finished MAC */ | |
2c4a056f MC |
532 | if (!ssl3_init_finished_mac(s)) { |
533 | ossl_statem_set_error(s); | |
534 | return WORK_ERROR; | |
535 | } | |
61ae935a MC |
536 | } |
537 | break; | |
538 | ||
61ae935a MC |
539 | case TLS_ST_CW_CHANGE: |
540 | if (SSL_IS_DTLS(s)) { | |
541 | if (s->hit) { | |
542 | /* | |
543 | * We're into the last flight so we don't retransmit these | |
544 | * messages unless we need to. | |
545 | */ | |
546 | st->use_timer = 0; | |
547 | } | |
548 | #ifndef OPENSSL_NO_SCTP | |
549 | if (BIO_dgram_is_sctp(SSL_get_wbio(s))) | |
550 | return dtls_wait_for_dry(s); | |
551 | #endif | |
552 | } | |
f3b3d7f0 | 553 | break; |
61ae935a MC |
554 | |
555 | case TLS_ST_OK: | |
556 | return tls_finish_handshake(s, wst); | |
61ae935a MC |
557 | } |
558 | ||
559 | return WORK_FINISHED_CONTINUE; | |
560 | } | |
561 | ||
562 | /* | |
563 | * Perform any work that needs to be done after sending a message from the | |
564 | * client to the server. | |
565 | */ | |
8481f583 | 566 | WORK_STATE ossl_statem_client_post_work(SSL *s, WORK_STATE wst) |
61ae935a | 567 | { |
d6f1a6e9 | 568 | OSSL_STATEM *st = &s->statem; |
61ae935a MC |
569 | |
570 | s->init_num = 0; | |
571 | ||
a230b26e | 572 | switch (st->hand_state) { |
f3b3d7f0 RS |
573 | default: |
574 | /* No post work to be done */ | |
575 | break; | |
576 | ||
61ae935a | 577 | case TLS_ST_CW_CLNT_HELLO: |
46417569 | 578 | if (wst == WORK_MORE_A && statem_flush(s) != 1) |
61ae935a | 579 | return WORK_MORE_A; |
46417569 | 580 | |
61ae935a MC |
581 | if (SSL_IS_DTLS(s)) { |
582 | /* Treat the next message as the first packet */ | |
583 | s->first_packet = 1; | |
584 | } | |
585 | break; | |
586 | ||
587 | case TLS_ST_CW_KEY_EXCH: | |
588 | if (tls_client_key_exchange_post_work(s) == 0) | |
589 | return WORK_ERROR; | |
590 | break; | |
591 | ||
592 | case TLS_ST_CW_CHANGE: | |
593 | s->session->cipher = s->s3->tmp.new_cipher; | |
594 | #ifdef OPENSSL_NO_COMP | |
595 | s->session->compress_meth = 0; | |
596 | #else | |
597 | if (s->s3->tmp.new_compression == NULL) | |
598 | s->session->compress_meth = 0; | |
599 | else | |
600 | s->session->compress_meth = s->s3->tmp.new_compression->id; | |
601 | #endif | |
602 | if (!s->method->ssl3_enc->setup_key_block(s)) | |
603 | return WORK_ERROR; | |
604 | ||
605 | if (!s->method->ssl3_enc->change_cipher_state(s, | |
606 | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) | |
607 | return WORK_ERROR; | |
608 | ||
609 | if (SSL_IS_DTLS(s)) { | |
610 | #ifndef OPENSSL_NO_SCTP | |
611 | if (s->hit) { | |
612 | /* | |
613 | * Change to new shared key of SCTP-Auth, will be ignored if | |
614 | * no SCTP used. | |
615 | */ | |
616 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, | |
617 | 0, NULL); | |
618 | } | |
619 | #endif | |
620 | ||
621 | dtls1_reset_seq_numbers(s, SSL3_CC_WRITE); | |
622 | } | |
623 | break; | |
624 | ||
625 | case TLS_ST_CW_FINISHED: | |
626 | #ifndef OPENSSL_NO_SCTP | |
627 | if (wst == WORK_MORE_A && SSL_IS_DTLS(s) && s->hit == 0) { | |
628 | /* | |
629 | * Change to new shared key of SCTP-Auth, will be ignored if | |
630 | * no SCTP used. | |
631 | */ | |
632 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, | |
633 | 0, NULL); | |
634 | } | |
635 | #endif | |
636 | if (statem_flush(s) != 1) | |
637 | return WORK_MORE_B; | |
92760c21 MC |
638 | |
639 | if (SSL_IS_TLS13(s)) { | |
640 | if (!s->method->ssl3_enc->change_cipher_state(s, | |
641 | SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) | |
642 | return WORK_ERROR; | |
643 | } | |
61ae935a | 644 | break; |
61ae935a MC |
645 | } |
646 | ||
647 | return WORK_FINISHED_CONTINUE; | |
648 | } | |
649 | ||
650 | /* | |
6392fb8e MC |
651 | * Get the message construction function and message type for sending from the |
652 | * client | |
61ae935a MC |
653 | * |
654 | * Valid return values are: | |
655 | * 1: Success | |
656 | * 0: Error | |
657 | */ | |
6392fb8e | 658 | int ossl_statem_client_construct_message(SSL *s, WPACKET *pkt, |
a15c953f | 659 | confunc_f *confunc, int *mt) |
61ae935a | 660 | { |
d6f1a6e9 | 661 | OSSL_STATEM *st = &s->statem; |
61ae935a | 662 | |
4a01c59f MC |
663 | switch (st->hand_state) { |
664 | default: | |
665 | /* Shouldn't happen */ | |
666 | return 0; | |
667 | ||
668 | case TLS_ST_CW_CHANGE: | |
5923ad4b | 669 | if (SSL_IS_DTLS(s)) |
6392fb8e | 670 | *confunc = dtls_construct_change_cipher_spec; |
4a01c59f | 671 | else |
6392fb8e MC |
672 | *confunc = tls_construct_change_cipher_spec; |
673 | *mt = SSL3_MT_CHANGE_CIPHER_SPEC; | |
4a01c59f MC |
674 | break; |
675 | ||
676 | case TLS_ST_CW_CLNT_HELLO: | |
6392fb8e MC |
677 | *confunc = tls_construct_client_hello; |
678 | *mt = SSL3_MT_CLIENT_HELLO; | |
4a01c59f MC |
679 | break; |
680 | ||
681 | case TLS_ST_CW_CERT: | |
6392fb8e MC |
682 | *confunc = tls_construct_client_certificate; |
683 | *mt = SSL3_MT_CERTIFICATE; | |
4a01c59f MC |
684 | break; |
685 | ||
686 | case TLS_ST_CW_KEY_EXCH: | |
6392fb8e MC |
687 | *confunc = tls_construct_client_key_exchange; |
688 | *mt = SSL3_MT_CLIENT_KEY_EXCHANGE; | |
4a01c59f MC |
689 | break; |
690 | ||
691 | case TLS_ST_CW_CERT_VRFY: | |
6392fb8e MC |
692 | *confunc = tls_construct_client_verify; |
693 | *mt = SSL3_MT_CERTIFICATE_VERIFY; | |
4a01c59f | 694 | break; |
61ae935a MC |
695 | |
696 | #if !defined(OPENSSL_NO_NEXTPROTONEG) | |
4a01c59f | 697 | case TLS_ST_CW_NEXT_PROTO: |
6392fb8e MC |
698 | *confunc = tls_construct_next_proto; |
699 | *mt = SSL3_MT_NEXT_PROTO; | |
4a01c59f | 700 | break; |
61ae935a | 701 | #endif |
4a01c59f | 702 | case TLS_ST_CW_FINISHED: |
6392fb8e MC |
703 | *confunc = tls_construct_finished; |
704 | *mt = SSL3_MT_FINISHED; | |
4a01c59f MC |
705 | break; |
706 | } | |
5923ad4b | 707 | |
5923ad4b | 708 | return 1; |
61ae935a MC |
709 | } |
710 | ||
711 | /* | |
712 | * Returns the maximum allowed length for the current message that we are | |
713 | * reading. Excludes the message header. | |
714 | */ | |
eda75751 | 715 | size_t ossl_statem_client_max_message_size(SSL *s) |
61ae935a | 716 | { |
d6f1a6e9 | 717 | OSSL_STATEM *st = &s->statem; |
61ae935a | 718 | |
a230b26e | 719 | switch (st->hand_state) { |
f3b3d7f0 RS |
720 | default: |
721 | /* Shouldn't happen */ | |
722 | return 0; | |
723 | ||
a230b26e EK |
724 | case TLS_ST_CR_SRVR_HELLO: |
725 | return SERVER_HELLO_MAX_LENGTH; | |
61ae935a | 726 | |
a230b26e EK |
727 | case DTLS_ST_CR_HELLO_VERIFY_REQUEST: |
728 | return HELLO_VERIFY_REQUEST_MAX_LENGTH; | |
61ae935a | 729 | |
a230b26e EK |
730 | case TLS_ST_CR_CERT: |
731 | return s->max_cert_list; | |
61ae935a | 732 | |
a230b26e EK |
733 | case TLS_ST_CR_CERT_STATUS: |
734 | return SSL3_RT_MAX_PLAIN_LENGTH; | |
61ae935a | 735 | |
a230b26e EK |
736 | case TLS_ST_CR_KEY_EXCH: |
737 | return SERVER_KEY_EXCH_MAX_LENGTH; | |
61ae935a | 738 | |
a230b26e EK |
739 | case TLS_ST_CR_CERT_REQ: |
740 | /* | |
741 | * Set to s->max_cert_list for compatibility with previous releases. In | |
742 | * practice these messages can get quite long if servers are configured | |
743 | * to provide a long list of acceptable CAs | |
744 | */ | |
745 | return s->max_cert_list; | |
61ae935a | 746 | |
a230b26e EK |
747 | case TLS_ST_CR_SRVR_DONE: |
748 | return SERVER_HELLO_DONE_MAX_LENGTH; | |
61ae935a | 749 | |
a230b26e EK |
750 | case TLS_ST_CR_CHANGE: |
751 | if (s->version == DTLS1_BAD_VER) | |
752 | return 3; | |
753 | return CCS_MAX_LENGTH; | |
61ae935a | 754 | |
a230b26e EK |
755 | case TLS_ST_CR_SESSION_TICKET: |
756 | return SSL3_RT_MAX_PLAIN_LENGTH; | |
61ae935a | 757 | |
a230b26e EK |
758 | case TLS_ST_CR_FINISHED: |
759 | return FINISHED_MAX_LENGTH; | |
e46f2334 MC |
760 | |
761 | case TLS_ST_CR_ENCRYPTED_EXTENSIONS: | |
762 | return ENCRYPTED_EXTENSIONS_MAX_LENGTH; | |
61ae935a | 763 | } |
61ae935a MC |
764 | } |
765 | ||
766 | /* | |
767 | * Process a message that the client has been received from the server. | |
768 | */ | |
8481f583 | 769 | MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt) |
61ae935a | 770 | { |
d6f1a6e9 | 771 | OSSL_STATEM *st = &s->statem; |
61ae935a | 772 | |
a230b26e | 773 | switch (st->hand_state) { |
f3b3d7f0 RS |
774 | default: |
775 | /* Shouldn't happen */ | |
776 | return MSG_PROCESS_ERROR; | |
777 | ||
a230b26e EK |
778 | case TLS_ST_CR_SRVR_HELLO: |
779 | return tls_process_server_hello(s, pkt); | |
61ae935a | 780 | |
a230b26e EK |
781 | case DTLS_ST_CR_HELLO_VERIFY_REQUEST: |
782 | return dtls_process_hello_verify(s, pkt); | |
61ae935a | 783 | |
a230b26e EK |
784 | case TLS_ST_CR_CERT: |
785 | return tls_process_server_certificate(s, pkt); | |
61ae935a | 786 | |
a230b26e EK |
787 | case TLS_ST_CR_CERT_STATUS: |
788 | return tls_process_cert_status(s, pkt); | |
61ae935a | 789 | |
a230b26e EK |
790 | case TLS_ST_CR_KEY_EXCH: |
791 | return tls_process_key_exchange(s, pkt); | |
61ae935a | 792 | |
a230b26e EK |
793 | case TLS_ST_CR_CERT_REQ: |
794 | return tls_process_certificate_request(s, pkt); | |
61ae935a | 795 | |
a230b26e EK |
796 | case TLS_ST_CR_SRVR_DONE: |
797 | return tls_process_server_done(s, pkt); | |
61ae935a | 798 | |
a230b26e EK |
799 | case TLS_ST_CR_CHANGE: |
800 | return tls_process_change_cipher_spec(s, pkt); | |
61ae935a | 801 | |
a230b26e EK |
802 | case TLS_ST_CR_SESSION_TICKET: |
803 | return tls_process_new_session_ticket(s, pkt); | |
61ae935a | 804 | |
a230b26e EK |
805 | case TLS_ST_CR_FINISHED: |
806 | return tls_process_finished(s, pkt); | |
e46f2334 MC |
807 | |
808 | case TLS_ST_CR_ENCRYPTED_EXTENSIONS: | |
809 | return tls_process_encrypted_extensions(s, pkt); | |
61ae935a | 810 | } |
61ae935a MC |
811 | } |
812 | ||
813 | /* | |
814 | * Perform any further processing required following the receipt of a message | |
815 | * from the server | |
816 | */ | |
8481f583 | 817 | WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst) |
61ae935a | 818 | { |
d6f1a6e9 | 819 | OSSL_STATEM *st = &s->statem; |
61ae935a | 820 | |
a230b26e | 821 | switch (st->hand_state) { |
f3b3d7f0 RS |
822 | default: |
823 | /* Shouldn't happen */ | |
824 | return WORK_ERROR; | |
825 | ||
05c4f1d5 MC |
826 | case TLS_ST_CR_CERT_REQ: |
827 | return tls_prepare_client_certificate(s, wst); | |
828 | ||
61ae935a MC |
829 | #ifndef OPENSSL_NO_SCTP |
830 | case TLS_ST_CR_SRVR_DONE: | |
831 | /* We only get here if we are using SCTP and we are renegotiating */ | |
832 | if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) { | |
833 | s->s3->in_read_app_data = 2; | |
834 | s->rwstate = SSL_READING; | |
835 | BIO_clear_retry_flags(SSL_get_rbio(s)); | |
836 | BIO_set_retry_read(SSL_get_rbio(s)); | |
fe3a3291 | 837 | ossl_statem_set_sctp_read_sock(s, 1); |
61ae935a MC |
838 | return WORK_MORE_A; |
839 | } | |
fe3a3291 | 840 | ossl_statem_set_sctp_read_sock(s, 0); |
61ae935a MC |
841 | return WORK_FINISHED_STOP; |
842 | #endif | |
61ae935a | 843 | } |
61ae935a MC |
844 | } |
845 | ||
7cea05dc | 846 | int tls_construct_client_hello(SSL *s, WPACKET *pkt) |
0f113f3e | 847 | { |
2c7b4dbc | 848 | unsigned char *p; |
ec60ccc1 MC |
849 | size_t sess_id_len; |
850 | int i, protverr; | |
2c7b4dbc | 851 | int al = SSL_AD_HANDSHAKE_FAILURE; |
09b6c2ef | 852 | #ifndef OPENSSL_NO_COMP |
0f113f3e MC |
853 | SSL_COMP *comp; |
854 | #endif | |
b9908bf9 | 855 | SSL_SESSION *sess = s->session; |
0f113f3e | 856 | |
7cea05dc | 857 | if (!WPACKET_set_max_size(pkt, SSL3_RT_MAX_PLAIN_LENGTH)) { |
2c7b4dbc MC |
858 | /* Should not happen */ |
859 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); | |
7cea05dc | 860 | return 0; |
2c7b4dbc | 861 | } |
0f113f3e | 862 | |
b9908bf9 | 863 | /* Work out what SSL/TLS/DTLS version to use */ |
4fa52141 VD |
864 | protverr = ssl_set_client_hello_version(s); |
865 | if (protverr != 0) { | |
866 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, protverr); | |
7cea05dc | 867 | return 0; |
4fa52141 | 868 | } |
0f113f3e | 869 | |
a230b26e | 870 | if ((sess == NULL) || !ssl_version_supported(s, sess->ssl_version) || |
0f113f3e | 871 | /* |
b9908bf9 MC |
872 | * In the case of EAP-FAST, we can have a pre-shared |
873 | * "ticket" without a session ID. | |
0f113f3e | 874 | */ |
aff8c126 | 875 | (!sess->session_id_length && !sess->ext.tick) || |
b9908bf9 MC |
876 | (sess->not_resumable)) { |
877 | if (!ssl_get_new_session(s, 0)) | |
7cea05dc | 878 | return 0; |
b9908bf9 MC |
879 | } |
880 | /* else use the pre-loaded session */ | |
0f113f3e | 881 | |
b9908bf9 | 882 | p = s->s3->client_random; |
0f113f3e | 883 | |
b9908bf9 MC |
884 | /* |
885 | * for DTLS if client_random is initialized, reuse it, we are | |
886 | * required to use same upon reply to HelloVerify | |
887 | */ | |
888 | if (SSL_IS_DTLS(s)) { | |
889 | size_t idx; | |
890 | i = 1; | |
891 | for (idx = 0; idx < sizeof(s->s3->client_random); idx++) { | |
892 | if (p[idx]) { | |
893 | i = 0; | |
894 | break; | |
0f113f3e | 895 | } |
0f113f3e | 896 | } |
b9908bf9 MC |
897 | } else |
898 | i = 1; | |
0f113f3e | 899 | |
a230b26e | 900 | if (i && ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random)) <= 0) |
7cea05dc | 901 | return 0; |
b9908bf9 | 902 | |
b9908bf9 MC |
903 | /*- |
904 | * version indicates the negotiated version: for example from | |
905 | * an SSLv2/v3 compatible client hello). The client_version | |
906 | * field is the maximum version we permit and it is also | |
907 | * used in RSA encrypted premaster secrets. Some servers can | |
908 | * choke if we initially report a higher version then | |
909 | * renegotiate to a lower one in the premaster secret. This | |
910 | * didn't happen with TLS 1.0 as most servers supported it | |
911 | * but it can with TLS 1.1 or later if the server only supports | |
912 | * 1.0. | |
913 | * | |
914 | * Possible scenario with previous logic: | |
915 | * 1. Client hello indicates TLS 1.2 | |
916 | * 2. Server hello says TLS 1.0 | |
917 | * 3. RSA encrypted premaster secret uses 1.2. | |
8483a003 | 918 | * 4. Handshake proceeds using TLS 1.0. |
b9908bf9 MC |
919 | * 5. Server sends hello request to renegotiate. |
920 | * 6. Client hello indicates TLS v1.0 as we now | |
921 | * know that is maximum server supports. | |
922 | * 7. Server chokes on RSA encrypted premaster secret | |
923 | * containing version 1.0. | |
924 | * | |
925 | * For interoperability it should be OK to always use the | |
926 | * maximum version we support in client hello and then rely | |
927 | * on the checking of version to ensure the servers isn't | |
928 | * being inconsistent: for example initially negotiating with | |
929 | * TLS 1.0 and renegotiating with TLS 1.2. We do this by using | |
930 | * client_version in client hello and not resetting it to | |
931 | * the negotiated version. | |
cd998837 MC |
932 | * |
933 | * For TLS 1.3 we always set the ClientHello version to 1.2 and rely on the | |
16bce0e0 | 934 | * supported_versions extension for the real supported versions. |
b9908bf9 | 935 | */ |
7acb8b64 | 936 | if (!WPACKET_put_bytes_u16(pkt, s->client_version) |
7cea05dc | 937 | || !WPACKET_memcpy(pkt, s->s3->client_random, SSL3_RANDOM_SIZE)) { |
2c7b4dbc | 938 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
7cea05dc | 939 | return 0; |
2c7b4dbc | 940 | } |
b9908bf9 MC |
941 | |
942 | /* Session ID */ | |
943 | if (s->new_session) | |
ec60ccc1 | 944 | sess_id_len = 0; |
b9908bf9 | 945 | else |
ec60ccc1 MC |
946 | sess_id_len = s->session->session_id_length; |
947 | if (sess_id_len > sizeof(s->session->session_id) | |
7cea05dc | 948 | || !WPACKET_start_sub_packet_u8(pkt) |
ec60ccc1 MC |
949 | || (sess_id_len != 0 && !WPACKET_memcpy(pkt, s->session->session_id, |
950 | sess_id_len)) | |
7cea05dc | 951 | || !WPACKET_close(pkt)) { |
2c7b4dbc | 952 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
7cea05dc | 953 | return 0; |
b9908bf9 | 954 | } |
0f113f3e | 955 | |
b9908bf9 MC |
956 | /* cookie stuff for DTLS */ |
957 | if (SSL_IS_DTLS(s)) { | |
2c7b4dbc | 958 | if (s->d1->cookie_len > sizeof(s->d1->cookie) |
7cea05dc | 959 | || !WPACKET_sub_memcpy_u8(pkt, s->d1->cookie, |
b2b3024e | 960 | s->d1->cookie_len)) { |
b9908bf9 | 961 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
7cea05dc | 962 | return 0; |
0f113f3e | 963 | } |
b9908bf9 MC |
964 | } |
965 | ||
966 | /* Ciphers supported */ | |
7cea05dc | 967 | if (!WPACKET_start_sub_packet_u16(pkt)) { |
2c7b4dbc | 968 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
7cea05dc | 969 | return 0; |
2c7b4dbc MC |
970 | } |
971 | /* ssl_cipher_list_to_bytes() raises SSLerr if appropriate */ | |
7cea05dc MC |
972 | if (!ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), pkt)) |
973 | return 0; | |
974 | if (!WPACKET_close(pkt)) { | |
2c7b4dbc | 975 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
7cea05dc | 976 | return 0; |
b9908bf9 | 977 | } |
0f113f3e | 978 | |
b9908bf9 | 979 | /* COMPRESSION */ |
7cea05dc | 980 | if (!WPACKET_start_sub_packet_u8(pkt)) { |
2c7b4dbc | 981 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
7cea05dc | 982 | return 0; |
2c7b4dbc MC |
983 | } |
984 | #ifndef OPENSSL_NO_COMP | |
985 | if (ssl_allow_compression(s) && s->ctx->comp_methods) { | |
986 | int compnum = sk_SSL_COMP_num(s->ctx->comp_methods); | |
987 | for (i = 0; i < compnum; i++) { | |
988 | comp = sk_SSL_COMP_value(s->ctx->comp_methods, i); | |
7cea05dc | 989 | if (!WPACKET_put_bytes_u8(pkt, comp->id)) { |
2c7b4dbc | 990 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
7cea05dc | 991 | return 0; |
2c7b4dbc MC |
992 | } |
993 | } | |
b9908bf9 | 994 | } |
09b6c2ef | 995 | #endif |
2c7b4dbc | 996 | /* Add the NULL method */ |
7cea05dc | 997 | if (!WPACKET_put_bytes_u8(pkt, 0) || !WPACKET_close(pkt)) { |
2c7b4dbc | 998 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
7cea05dc | 999 | return 0; |
2c7b4dbc | 1000 | } |
761772d7 | 1001 | |
b9908bf9 | 1002 | /* TLS extensions */ |
30aeba43 | 1003 | if (!tls_construct_extensions(s, pkt, EXT_CLIENT_HELLO, NULL, 0, &al)) { |
b9908bf9 MC |
1004 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
1005 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); | |
7cea05dc | 1006 | return 0; |
b9908bf9 | 1007 | } |
0f113f3e | 1008 | |
b9908bf9 | 1009 | return 1; |
0f113f3e | 1010 | } |
d02b48c6 | 1011 | |
be3583fa | 1012 | MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt) |
8ba708e5 MC |
1013 | { |
1014 | int al; | |
cb150cbc | 1015 | size_t cookie_len; |
8ba708e5 MC |
1016 | PACKET cookiepkt; |
1017 | ||
1018 | if (!PACKET_forward(pkt, 2) | |
a230b26e | 1019 | || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) { |
8ba708e5 MC |
1020 | al = SSL_AD_DECODE_ERROR; |
1021 | SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH); | |
1022 | goto f_err; | |
1023 | } | |
1024 | ||
1025 | cookie_len = PACKET_remaining(&cookiepkt); | |
1026 | if (cookie_len > sizeof(s->d1->cookie)) { | |
1027 | al = SSL_AD_ILLEGAL_PARAMETER; | |
1028 | SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_TOO_LONG); | |
1029 | goto f_err; | |
1030 | } | |
1031 | ||
1032 | if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) { | |
1033 | al = SSL_AD_DECODE_ERROR; | |
1034 | SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH); | |
1035 | goto f_err; | |
1036 | } | |
1037 | s->d1->cookie_len = cookie_len; | |
1038 | ||
1039 | return MSG_PROCESS_FINISHED_READING; | |
1040 | f_err: | |
1041 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
fe3a3291 | 1042 | ossl_statem_set_error(s); |
8ba708e5 MC |
1043 | return MSG_PROCESS_ERROR; |
1044 | } | |
1045 | ||
be3583fa | 1046 | MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt) |
b9908bf9 MC |
1047 | { |
1048 | STACK_OF(SSL_CIPHER) *sk; | |
1049 | const SSL_CIPHER *c; | |
332eb390 | 1050 | PACKET session_id, extpkt; |
b9908bf9 | 1051 | size_t session_id_len; |
b6981744 | 1052 | const unsigned char *cipherchars; |
b9908bf9 MC |
1053 | int i, al = SSL_AD_INTERNAL_ERROR; |
1054 | unsigned int compression; | |
4fa52141 | 1055 | unsigned int sversion; |
3434f40b | 1056 | unsigned int context; |
4fa52141 | 1057 | int protverr; |
332eb390 | 1058 | RAW_EXTENSION *extensions = NULL; |
b9908bf9 MC |
1059 | #ifndef OPENSSL_NO_COMP |
1060 | SSL_COMP *comp; | |
1061 | #endif | |
1062 | ||
4fa52141 VD |
1063 | if (!PACKET_get_net_2(pkt, &sversion)) { |
1064 | al = SSL_AD_DECODE_ERROR; | |
1065 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); | |
1066 | goto f_err; | |
1067 | } | |
50932c4a | 1068 | |
4fa52141 VD |
1069 | protverr = ssl_choose_client_version(s, sversion); |
1070 | if (protverr != 0) { | |
1071 | al = SSL_AD_PROTOCOL_VERSION; | |
1072 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, protverr); | |
1073 | goto f_err; | |
0f113f3e | 1074 | } |
0f113f3e MC |
1075 | |
1076 | /* load the server hello data */ | |
1077 | /* load the server random */ | |
73999b62 | 1078 | if (!PACKET_copy_bytes(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) { |
50932c4a | 1079 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 1080 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); |
50932c4a MC |
1081 | goto f_err; |
1082 | } | |
0f113f3e MC |
1083 | |
1084 | s->hit = 0; | |
1085 | ||
fc5ce51d | 1086 | /* Get the session-id. */ |
71728dd8 MC |
1087 | if (!SSL_IS_TLS13(s)) { |
1088 | if (!PACKET_get_length_prefixed_1(pkt, &session_id)) { | |
1089 | al = SSL_AD_DECODE_ERROR; | |
1090 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); | |
1091 | goto f_err; | |
1092 | } | |
1093 | session_id_len = PACKET_remaining(&session_id); | |
1094 | if (session_id_len > sizeof s->session->session_id | |
1095 | || session_id_len > SSL3_SESSION_ID_SIZE) { | |
1096 | al = SSL_AD_ILLEGAL_PARAMETER; | |
1097 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, | |
1098 | SSL_R_SSL3_SESSION_ID_TOO_LONG); | |
1099 | goto f_err; | |
1100 | } | |
1101 | } else { | |
625b0d51 | 1102 | PACKET_null_init(&session_id); |
71728dd8 | 1103 | session_id_len = 0; |
0f113f3e | 1104 | } |
e481f9b9 | 1105 | |
73999b62 | 1106 | if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) { |
f0659bdb | 1107 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); |
fc5ce51d EK |
1108 | al = SSL_AD_DECODE_ERROR; |
1109 | goto f_err; | |
1110 | } | |
1111 | ||
0f113f3e | 1112 | /* |
6e3d0153 EK |
1113 | * Check if we can resume the session based on external pre-shared secret. |
1114 | * EAP-FAST (RFC 4851) supports two types of session resumption. | |
1115 | * Resumption based on server-side state works with session IDs. | |
1116 | * Resumption based on pre-shared Protected Access Credentials (PACs) | |
1117 | * works by overriding the SessionTicket extension at the application | |
1118 | * layer, and does not send a session ID. (We do not know whether EAP-FAST | |
1119 | * servers would honour the session ID.) Therefore, the session ID alone | |
1120 | * is not a reliable indicator of session resumption, so we first check if | |
1121 | * we can resume, and later peek at the next handshake message to see if the | |
1122 | * server wants to resume. | |
0f113f3e | 1123 | */ |
71728dd8 | 1124 | if (s->version >= TLS1_VERSION && !SSL_IS_TLS13(s) |
aff8c126 | 1125 | && s->ext.session_secret_cb != NULL && s->session->ext.tick) { |
4a640fb6 | 1126 | const SSL_CIPHER *pref_cipher = NULL; |
8c1a5343 MC |
1127 | /* |
1128 | * s->session->master_key_length is a size_t, but this is an int for | |
1129 | * backwards compat reasons | |
1130 | */ | |
1131 | int master_key_length; | |
1132 | master_key_length = sizeof(s->session->master_key); | |
aff8c126 | 1133 | if (s->ext.session_secret_cb(s, s->session->master_key, |
8c1a5343 | 1134 | &master_key_length, |
0f113f3e | 1135 | NULL, &pref_cipher, |
aff8c126 | 1136 | s->ext.session_secret_cb_arg) |
8c1a5343 MC |
1137 | && master_key_length > 0) { |
1138 | s->session->master_key_length = master_key_length; | |
0f113f3e | 1139 | s->session->cipher = pref_cipher ? |
50932c4a | 1140 | pref_cipher : ssl_get_cipher_by_char(s, cipherchars); |
6e3d0153 | 1141 | } else { |
b9908bf9 | 1142 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR); |
6e3d0153 EK |
1143 | al = SSL_AD_INTERNAL_ERROR; |
1144 | goto f_err; | |
0f113f3e | 1145 | } |
50932c4a MC |
1146 | } |
1147 | ||
fc5ce51d EK |
1148 | if (session_id_len != 0 && session_id_len == s->session->session_id_length |
1149 | && memcmp(PACKET_data(&session_id), s->session->session_id, | |
1150 | session_id_len) == 0) { | |
0f113f3e MC |
1151 | if (s->sid_ctx_length != s->session->sid_ctx_length |
1152 | || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) { | |
1153 | /* actually a client application bug */ | |
1154 | al = SSL_AD_ILLEGAL_PARAMETER; | |
b9908bf9 | 1155 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
0f113f3e MC |
1156 | SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); |
1157 | goto f_err; | |
1158 | } | |
1159 | s->hit = 1; | |
6e3d0153 | 1160 | } else { |
0f113f3e | 1161 | /* |
6e3d0153 EK |
1162 | * If we were trying for session-id reuse but the server |
1163 | * didn't echo the ID, make a new SSL_SESSION. | |
1164 | * In the case of EAP-FAST and PAC, we do not send a session ID, | |
1165 | * so the PAC-based session secret is always preserved. It'll be | |
1166 | * overwritten if the server refuses resumption. | |
0f113f3e MC |
1167 | */ |
1168 | if (s->session->session_id_length > 0) { | |
4f6eaa59 | 1169 | s->ctx->stats.sess_miss++; |
0f113f3e MC |
1170 | if (!ssl_get_new_session(s, 0)) { |
1171 | goto f_err; | |
1172 | } | |
1173 | } | |
50932c4a | 1174 | |
ccae4a15 | 1175 | s->session->ssl_version = s->version; |
fc5ce51d EK |
1176 | s->session->session_id_length = session_id_len; |
1177 | /* session_id_len could be 0 */ | |
a19fc66a KR |
1178 | if (session_id_len > 0) |
1179 | memcpy(s->session->session_id, PACKET_data(&session_id), | |
1180 | session_id_len); | |
0f113f3e | 1181 | } |
fc5ce51d | 1182 | |
ccae4a15 FI |
1183 | /* Session version and negotiated protocol version should match */ |
1184 | if (s->version != s->session->ssl_version) { | |
1185 | al = SSL_AD_PROTOCOL_VERSION; | |
1186 | ||
1187 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, | |
1188 | SSL_R_SSL_SESSION_VERSION_MISMATCH); | |
1189 | goto f_err; | |
1190 | } | |
1191 | ||
50932c4a | 1192 | c = ssl_get_cipher_by_char(s, cipherchars); |
0f113f3e MC |
1193 | if (c == NULL) { |
1194 | /* unknown cipher */ | |
1195 | al = SSL_AD_ILLEGAL_PARAMETER; | |
b9908bf9 | 1196 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED); |
0f113f3e MC |
1197 | goto f_err; |
1198 | } | |
0f113f3e | 1199 | /* |
3eb2aff4 KR |
1200 | * Now that we know the version, update the check to see if it's an allowed |
1201 | * version. | |
1202 | */ | |
1203 | s->s3->tmp.min_ver = s->version; | |
1204 | s->s3->tmp.max_ver = s->version; | |
1205 | /* | |
1206 | * If it is a disabled cipher we either didn't send it in client hello, | |
1207 | * or it's not allowed for the selected protocol. So we return an error. | |
0f113f3e MC |
1208 | */ |
1209 | if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) { | |
1210 | al = SSL_AD_ILLEGAL_PARAMETER; | |
b9908bf9 | 1211 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED); |
0f113f3e MC |
1212 | goto f_err; |
1213 | } | |
0f113f3e MC |
1214 | |
1215 | sk = ssl_get_ciphers_by_id(s); | |
1216 | i = sk_SSL_CIPHER_find(sk, c); | |
1217 | if (i < 0) { | |
1218 | /* we did not say we would use this cipher */ | |
1219 | al = SSL_AD_ILLEGAL_PARAMETER; | |
b9908bf9 | 1220 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED); |
0f113f3e MC |
1221 | goto f_err; |
1222 | } | |
1223 | ||
1224 | /* | |
1225 | * Depending on the session caching (internal/external), the cipher | |
1226 | * and/or cipher_id values may not be set. Make sure that cipher_id is | |
1227 | * set and use it for comparison. | |
1228 | */ | |
1229 | if (s->session->cipher) | |
1230 | s->session->cipher_id = s->session->cipher->id; | |
1231 | if (s->hit && (s->session->cipher_id != c->id)) { | |
9e9858d1 | 1232 | al = SSL_AD_ILLEGAL_PARAMETER; |
b9908bf9 | 1233 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
9e9858d1 RS |
1234 | SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); |
1235 | goto f_err; | |
0f113f3e MC |
1236 | } |
1237 | s->s3->tmp.new_cipher = c; | |
0f113f3e MC |
1238 | /* lets get the compression algorithm */ |
1239 | /* COMPRESSION */ | |
71728dd8 MC |
1240 | if (!SSL_IS_TLS13(s)) { |
1241 | if (!PACKET_get_1(pkt, &compression)) { | |
1242 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); | |
1243 | al = SSL_AD_DECODE_ERROR; | |
1244 | goto f_err; | |
1245 | } | |
1246 | } else { | |
1247 | compression = 0; | |
50932c4a | 1248 | } |
71728dd8 | 1249 | |
09b6c2ef | 1250 | #ifdef OPENSSL_NO_COMP |
fc5ce51d | 1251 | if (compression != 0) { |
0f113f3e | 1252 | al = SSL_AD_ILLEGAL_PARAMETER; |
b9908bf9 | 1253 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
0f113f3e MC |
1254 | SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
1255 | goto f_err; | |
1256 | } | |
1257 | /* | |
1258 | * If compression is disabled we'd better not try to resume a session | |
1259 | * using compression. | |
1260 | */ | |
1261 | if (s->session->compress_meth != 0) { | |
b9908bf9 | 1262 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION); |
0f113f3e MC |
1263 | goto f_err; |
1264 | } | |
09b6c2ef | 1265 | #else |
fc5ce51d | 1266 | if (s->hit && compression != s->session->compress_meth) { |
0f113f3e | 1267 | al = SSL_AD_ILLEGAL_PARAMETER; |
b9908bf9 | 1268 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
0f113f3e MC |
1269 | SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED); |
1270 | goto f_err; | |
1271 | } | |
fc5ce51d | 1272 | if (compression == 0) |
0f113f3e MC |
1273 | comp = NULL; |
1274 | else if (!ssl_allow_compression(s)) { | |
1275 | al = SSL_AD_ILLEGAL_PARAMETER; | |
b9908bf9 | 1276 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED); |
0f113f3e | 1277 | goto f_err; |
fc5ce51d EK |
1278 | } else { |
1279 | comp = ssl3_comp_find(s->ctx->comp_methods, compression); | |
1280 | } | |
0f113f3e | 1281 | |
fc5ce51d | 1282 | if (compression != 0 && comp == NULL) { |
0f113f3e | 1283 | al = SSL_AD_ILLEGAL_PARAMETER; |
b9908bf9 | 1284 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
0f113f3e MC |
1285 | SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
1286 | goto f_err; | |
1287 | } else { | |
1288 | s->s3->tmp.new_compression = comp; | |
1289 | } | |
09b6c2ef | 1290 | #endif |
761772d7 | 1291 | |
0f113f3e | 1292 | /* TLS extensions */ |
332eb390 MC |
1293 | if (PACKET_remaining(pkt) == 0) { |
1294 | PACKET_null_init(&extpkt); | |
1295 | } else if (!PACKET_as_length_prefixed_2(pkt, &extpkt)) { | |
0f113f3e | 1296 | al = SSL_AD_DECODE_ERROR; |
332eb390 | 1297 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_LENGTH); |
0f113f3e MC |
1298 | goto f_err; |
1299 | } | |
332eb390 | 1300 | |
3434f40b MC |
1301 | context = SSL_IS_TLS13(s) ? EXT_TLS1_3_SERVER_HELLO |
1302 | : EXT_TLS1_2_SERVER_HELLO; | |
1303 | if (!tls_collect_extensions(s, &extpkt, context, &extensions, &al) | |
f97d4c37 | 1304 | || !tls_parse_all_extensions(s, context, extensions, NULL, 0, &al)) |
332eb390 MC |
1305 | goto f_err; |
1306 | ||
8723588e MC |
1307 | #ifndef OPENSSL_NO_SCTP |
1308 | if (SSL_IS_DTLS(s) && s->hit) { | |
1309 | unsigned char sctpauthkey[64]; | |
1310 | char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)]; | |
1311 | ||
1312 | /* | |
1313 | * Add new shared key for SCTP-Auth, will be ignored if | |
1314 | * no SCTP used. | |
1315 | */ | |
141eb8c6 MC |
1316 | memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL, |
1317 | sizeof(DTLS1_SCTP_AUTH_LABEL)); | |
8723588e MC |
1318 | |
1319 | if (SSL_export_keying_material(s, sctpauthkey, | |
a230b26e EK |
1320 | sizeof(sctpauthkey), |
1321 | labelbuffer, | |
1322 | sizeof(labelbuffer), NULL, 0, 0) <= 0) | |
c0aa6b81 | 1323 | goto f_err; |
8723588e MC |
1324 | |
1325 | BIO_ctrl(SSL_get_wbio(s), | |
1326 | BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY, | |
1327 | sizeof(sctpauthkey), sctpauthkey); | |
1328 | } | |
1329 | #endif | |
1330 | ||
92760c21 MC |
1331 | /* |
1332 | * In TLSv1.3 we have some post-processing to change cipher state, otherwise | |
1333 | * we're done with this message | |
1334 | */ | |
1335 | if (SSL_IS_TLS13(s) | |
1336 | && (!s->method->ssl3_enc->setup_key_block(s) | |
1337 | || !s->method->ssl3_enc->change_cipher_state(s, | |
1338 | SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE) | |
1339 | || !s->method->ssl3_enc->change_cipher_state(s, | |
1340 | SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_READ))) { | |
1341 | al = SSL_AD_INTERNAL_ERROR; | |
1342 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_CANNOT_CHANGE_CIPHER); | |
1343 | goto f_err; | |
1344 | } | |
1345 | ||
1b0286a3 | 1346 | OPENSSL_free(extensions); |
b9908bf9 | 1347 | return MSG_PROCESS_CONTINUE_READING; |
0f113f3e MC |
1348 | f_err: |
1349 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
fe3a3291 | 1350 | ossl_statem_set_error(s); |
1b0286a3 | 1351 | OPENSSL_free(extensions); |
b9908bf9 | 1352 | return MSG_PROCESS_ERROR; |
0f113f3e | 1353 | } |
d02b48c6 | 1354 | |
be3583fa | 1355 | MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt) |
b9908bf9 MC |
1356 | { |
1357 | int al, i, ret = MSG_PROCESS_ERROR, exp_idx; | |
1358 | unsigned long cert_list_len, cert_len; | |
1359 | X509 *x = NULL; | |
b6981744 | 1360 | const unsigned char *certstart, *certbytes; |
b9908bf9 MC |
1361 | STACK_OF(X509) *sk = NULL; |
1362 | EVP_PKEY *pkey = NULL; | |
d805a57b | 1363 | size_t chainidx; |
e96e0f8e | 1364 | unsigned int context = 0; |
0f113f3e MC |
1365 | |
1366 | if ((sk = sk_X509_new_null()) == NULL) { | |
b9908bf9 | 1367 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE); |
cc273a93 | 1368 | goto err; |
0f113f3e MC |
1369 | } |
1370 | ||
e96e0f8e MC |
1371 | if ((SSL_IS_TLS13(s) && !PACKET_get_1(pkt, &context)) |
1372 | || context != 0 | |
1373 | || !PACKET_get_net_3(pkt, &cert_list_len) | |
1374 | || PACKET_remaining(pkt) != cert_list_len) { | |
0f113f3e | 1375 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 1376 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH); |
0f113f3e MC |
1377 | goto f_err; |
1378 | } | |
d805a57b | 1379 | for (chainidx = 0; PACKET_remaining(pkt); chainidx++) { |
73999b62 | 1380 | if (!PACKET_get_net_3(pkt, &cert_len) |
a230b26e | 1381 | || !PACKET_get_bytes(pkt, &certbytes, cert_len)) { |
0f113f3e | 1382 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 1383 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
0f113f3e MC |
1384 | SSL_R_CERT_LENGTH_MISMATCH); |
1385 | goto f_err; | |
1386 | } | |
1387 | ||
df758a85 MC |
1388 | certstart = certbytes; |
1389 | x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len); | |
0f113f3e MC |
1390 | if (x == NULL) { |
1391 | al = SSL_AD_BAD_CERTIFICATE; | |
b9908bf9 | 1392 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_ASN1_LIB); |
0f113f3e MC |
1393 | goto f_err; |
1394 | } | |
df758a85 | 1395 | if (certbytes != (certstart + cert_len)) { |
0f113f3e | 1396 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 1397 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
0f113f3e MC |
1398 | SSL_R_CERT_LENGTH_MISMATCH); |
1399 | goto f_err; | |
1400 | } | |
e96e0f8e MC |
1401 | |
1402 | if (SSL_IS_TLS13(s)) { | |
1403 | RAW_EXTENSION *rawexts = NULL; | |
1404 | PACKET extensions; | |
1405 | ||
1406 | if (!PACKET_get_length_prefixed_2(pkt, &extensions)) { | |
1407 | al = SSL_AD_DECODE_ERROR; | |
1408 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_BAD_LENGTH); | |
1409 | goto f_err; | |
1410 | } | |
1411 | if (!tls_collect_extensions(s, &extensions, EXT_TLS1_3_CERTIFICATE, | |
1412 | &rawexts, &al) | |
1413 | || !tls_parse_all_extensions(s, EXT_TLS1_3_CERTIFICATE, | |
d805a57b | 1414 | rawexts, x, chainidx, &al)) |
e96e0f8e MC |
1415 | goto f_err; |
1416 | } | |
1417 | ||
0f113f3e | 1418 | if (!sk_X509_push(sk, x)) { |
b9908bf9 | 1419 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE); |
cc273a93 | 1420 | goto err; |
0f113f3e MC |
1421 | } |
1422 | x = NULL; | |
0f113f3e MC |
1423 | } |
1424 | ||
1425 | i = ssl_verify_cert_chain(s, sk); | |
c8e2f98c MC |
1426 | /* |
1427 | * The documented interface is that SSL_VERIFY_PEER should be set in order | |
1428 | * for client side verification of the server certificate to take place. | |
1429 | * However, historically the code has only checked that *any* flag is set | |
1430 | * to cause server verification to take place. Use of the other flags makes | |
1431 | * no sense in client mode. An attempt to clean up the semantics was | |
1432 | * reverted because at least one application *only* set | |
1433 | * SSL_VERIFY_FAIL_IF_NO_PEER_CERT. Prior to the clean up this still caused | |
1434 | * server verification to take place, after the clean up it silently did | |
1435 | * nothing. SSL_CTX_set_verify()/SSL_set_verify() cannot validate the flags | |
1436 | * sent to them because they are void functions. Therefore, we now use the | |
1437 | * (less clean) historic behaviour of performing validation if any flag is | |
1438 | * set. The *documented* interface remains the same. | |
1439 | */ | |
1440 | if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) { | |
0f113f3e | 1441 | al = ssl_verify_alarm_type(s->verify_result); |
b9908bf9 | 1442 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
0f113f3e MC |
1443 | SSL_R_CERTIFICATE_VERIFY_FAILED); |
1444 | goto f_err; | |
1445 | } | |
1446 | ERR_clear_error(); /* but we keep s->verify_result */ | |
1447 | if (i > 1) { | |
b9908bf9 | 1448 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i); |
0f113f3e MC |
1449 | al = SSL_AD_HANDSHAKE_FAILURE; |
1450 | goto f_err; | |
1451 | } | |
1452 | ||
c34b0f99 | 1453 | s->session->peer_chain = sk; |
0f113f3e MC |
1454 | /* |
1455 | * Inconsistency alert: cert_chain does include the peer's certificate, | |
d4d78943 | 1456 | * which we don't include in statem_srvr.c |
0f113f3e MC |
1457 | */ |
1458 | x = sk_X509_value(sk, 0); | |
1459 | sk = NULL; | |
1460 | /* | |
1461 | * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end | |
1462 | */ | |
1463 | ||
8382fd3a | 1464 | pkey = X509_get0_pubkey(x); |
0f113f3e | 1465 | |
55a9a16f | 1466 | if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) { |
0f113f3e MC |
1467 | x = NULL; |
1468 | al = SSL3_AL_FATAL; | |
b9908bf9 | 1469 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
0f113f3e MC |
1470 | SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS); |
1471 | goto f_err; | |
1472 | } | |
1473 | ||
1474 | i = ssl_cert_type(x, pkey); | |
55a9a16f | 1475 | if (i < 0) { |
0f113f3e MC |
1476 | x = NULL; |
1477 | al = SSL3_AL_FATAL; | |
b9908bf9 | 1478 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
0f113f3e MC |
1479 | SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
1480 | goto f_err; | |
1481 | } | |
1482 | ||
55a9a16f | 1483 | exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher); |
e44380a9 | 1484 | if (exp_idx >= 0 && i != exp_idx |
a230b26e EK |
1485 | && (exp_idx != SSL_PKEY_GOST_EC || |
1486 | (i != SSL_PKEY_GOST12_512 && i != SSL_PKEY_GOST12_256 | |
1487 | && i != SSL_PKEY_GOST01))) { | |
55a9a16f MC |
1488 | x = NULL; |
1489 | al = SSL_AD_ILLEGAL_PARAMETER; | |
b9908bf9 | 1490 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
55a9a16f MC |
1491 | SSL_R_WRONG_CERTIFICATE_TYPE); |
1492 | goto f_err; | |
0f113f3e | 1493 | } |
a273c6ee | 1494 | s->session->peer_type = i; |
55a9a16f MC |
1495 | |
1496 | X509_free(s->session->peer); | |
05f0fb9f | 1497 | X509_up_ref(x); |
55a9a16f | 1498 | s->session->peer = x; |
0f113f3e MC |
1499 | s->session->verify_result = s->verify_result; |
1500 | ||
1501 | x = NULL; | |
b9908bf9 | 1502 | ret = MSG_PROCESS_CONTINUE_READING; |
66696478 RS |
1503 | goto done; |
1504 | ||
0f113f3e | 1505 | f_err: |
66696478 | 1506 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
cc273a93 | 1507 | err: |
fe3a3291 | 1508 | ossl_statem_set_error(s); |
66696478 | 1509 | done: |
0f113f3e MC |
1510 | X509_free(x); |
1511 | sk_X509_pop_free(sk, X509_free); | |
b9908bf9 | 1512 | return ret; |
0f113f3e | 1513 | } |
d02b48c6 | 1514 | |
7dc1c647 | 1515 | static int tls_process_ske_psk_preamble(SSL *s, PACKET *pkt, int *al) |
02a74590 MC |
1516 | { |
1517 | #ifndef OPENSSL_NO_PSK | |
7dc1c647 | 1518 | PACKET psk_identity_hint; |
02a74590 | 1519 | |
7dc1c647 MC |
1520 | /* PSK ciphersuites are preceded by an identity hint */ |
1521 | ||
1522 | if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) { | |
1523 | *al = SSL_AD_DECODE_ERROR; | |
4fa88861 | 1524 | SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, SSL_R_LENGTH_MISMATCH); |
7dc1c647 MC |
1525 | return 0; |
1526 | } | |
1527 | ||
1528 | /* | |
1529 | * Store PSK identity hint for later use, hint is used in | |
1530 | * tls_construct_client_key_exchange. Assume that the maximum length of | |
1531 | * a PSK identity hint can be as long as the maximum length of a PSK | |
1532 | * identity. | |
1533 | */ | |
1534 | if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) { | |
1535 | *al = SSL_AD_HANDSHAKE_FAILURE; | |
4fa88861 | 1536 | SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, SSL_R_DATA_LENGTH_TOO_LONG); |
7dc1c647 MC |
1537 | return 0; |
1538 | } | |
02a74590 | 1539 | |
7dc1c647 MC |
1540 | if (PACKET_remaining(&psk_identity_hint) == 0) { |
1541 | OPENSSL_free(s->session->psk_identity_hint); | |
1542 | s->session->psk_identity_hint = NULL; | |
1543 | } else if (!PACKET_strndup(&psk_identity_hint, | |
a230b26e | 1544 | &s->session->psk_identity_hint)) { |
7dc1c647 MC |
1545 | *al = SSL_AD_INTERNAL_ERROR; |
1546 | return 0; | |
1547 | } | |
1548 | ||
1549 | return 1; | |
1550 | #else | |
4fa88861 | 1551 | SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR); |
7dc1c647 MC |
1552 | *al = SSL_AD_INTERNAL_ERROR; |
1553 | return 0; | |
02a74590 MC |
1554 | #endif |
1555 | } | |
1556 | ||
25c6c10c MC |
1557 | static int tls_process_ske_srp(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al) |
1558 | { | |
1559 | #ifndef OPENSSL_NO_SRP | |
1560 | PACKET prime, generator, salt, server_pub; | |
1561 | ||
1562 | if (!PACKET_get_length_prefixed_2(pkt, &prime) | |
1563 | || !PACKET_get_length_prefixed_2(pkt, &generator) | |
1564 | || !PACKET_get_length_prefixed_1(pkt, &salt) | |
1565 | || !PACKET_get_length_prefixed_2(pkt, &server_pub)) { | |
1566 | *al = SSL_AD_DECODE_ERROR; | |
4fa88861 | 1567 | SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, SSL_R_LENGTH_MISMATCH); |
25c6c10c MC |
1568 | return 0; |
1569 | } | |
1570 | ||
348240c6 | 1571 | /* TODO(size_t): Convert BN_bin2bn() calls */ |
25c6c10c MC |
1572 | if ((s->srp_ctx.N = |
1573 | BN_bin2bn(PACKET_data(&prime), | |
348240c6 | 1574 | (int)PACKET_remaining(&prime), NULL)) == NULL |
25c6c10c MC |
1575 | || (s->srp_ctx.g = |
1576 | BN_bin2bn(PACKET_data(&generator), | |
348240c6 | 1577 | (int)PACKET_remaining(&generator), NULL)) == NULL |
25c6c10c MC |
1578 | || (s->srp_ctx.s = |
1579 | BN_bin2bn(PACKET_data(&salt), | |
348240c6 | 1580 | (int)PACKET_remaining(&salt), NULL)) == NULL |
25c6c10c MC |
1581 | || (s->srp_ctx.B = |
1582 | BN_bin2bn(PACKET_data(&server_pub), | |
348240c6 | 1583 | (int)PACKET_remaining(&server_pub), NULL)) == NULL) { |
25c6c10c | 1584 | *al = SSL_AD_INTERNAL_ERROR; |
4fa88861 | 1585 | SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, ERR_R_BN_LIB); |
25c6c10c MC |
1586 | return 0; |
1587 | } | |
1588 | ||
1589 | if (!srp_verify_server_param(s, al)) { | |
1590 | *al = SSL_AD_DECODE_ERROR; | |
4fa88861 | 1591 | SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, SSL_R_BAD_SRP_PARAMETERS); |
25c6c10c MC |
1592 | return 0; |
1593 | } | |
1594 | ||
1595 | /* We must check if there is a certificate */ | |
a230b26e | 1596 | if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS)) |
25c6c10c MC |
1597 | *pkey = X509_get0_pubkey(s->session->peer); |
1598 | ||
1599 | return 1; | |
1600 | #else | |
4fa88861 | 1601 | SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, ERR_R_INTERNAL_ERROR); |
25c6c10c MC |
1602 | *al = SSL_AD_INTERNAL_ERROR; |
1603 | return 0; | |
1604 | #endif | |
1605 | } | |
1606 | ||
e01a610d MC |
1607 | static int tls_process_ske_dhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al) |
1608 | { | |
1609 | #ifndef OPENSSL_NO_DH | |
1610 | PACKET prime, generator, pub_key; | |
1611 | EVP_PKEY *peer_tmp = NULL; | |
1612 | ||
1613 | DH *dh = NULL; | |
1614 | BIGNUM *p = NULL, *g = NULL, *bnpub_key = NULL; | |
1615 | ||
1616 | if (!PACKET_get_length_prefixed_2(pkt, &prime) | |
1617 | || !PACKET_get_length_prefixed_2(pkt, &generator) | |
1618 | || !PACKET_get_length_prefixed_2(pkt, &pub_key)) { | |
1619 | *al = SSL_AD_DECODE_ERROR; | |
4fa88861 | 1620 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_LENGTH_MISMATCH); |
e01a610d MC |
1621 | return 0; |
1622 | } | |
1623 | ||
1624 | peer_tmp = EVP_PKEY_new(); | |
1625 | dh = DH_new(); | |
1626 | ||
1627 | if (peer_tmp == NULL || dh == NULL) { | |
1628 | *al = SSL_AD_INTERNAL_ERROR; | |
4fa88861 | 1629 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_MALLOC_FAILURE); |
e01a610d MC |
1630 | goto err; |
1631 | } | |
1632 | ||
348240c6 MC |
1633 | /* TODO(size_t): Convert these calls */ |
1634 | p = BN_bin2bn(PACKET_data(&prime), (int)PACKET_remaining(&prime), NULL); | |
1635 | g = BN_bin2bn(PACKET_data(&generator), (int)PACKET_remaining(&generator), | |
1636 | NULL); | |
1637 | bnpub_key = BN_bin2bn(PACKET_data(&pub_key), | |
1638 | (int)PACKET_remaining(&pub_key), NULL); | |
e01a610d MC |
1639 | if (p == NULL || g == NULL || bnpub_key == NULL) { |
1640 | *al = SSL_AD_INTERNAL_ERROR; | |
4fa88861 | 1641 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB); |
e01a610d MC |
1642 | goto err; |
1643 | } | |
1644 | ||
1645 | if (BN_is_zero(p) || BN_is_zero(g) || BN_is_zero(bnpub_key)) { | |
1646 | *al = SSL_AD_DECODE_ERROR; | |
4fa88861 | 1647 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_BAD_DH_VALUE); |
e01a610d MC |
1648 | goto err; |
1649 | } | |
1650 | ||
1651 | if (!DH_set0_pqg(dh, p, NULL, g)) { | |
1652 | *al = SSL_AD_INTERNAL_ERROR; | |
4fa88861 | 1653 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB); |
e01a610d MC |
1654 | goto err; |
1655 | } | |
1656 | p = g = NULL; | |
1657 | ||
1658 | if (!DH_set0_key(dh, bnpub_key, NULL)) { | |
1659 | *al = SSL_AD_INTERNAL_ERROR; | |
4fa88861 | 1660 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB); |
e01a610d MC |
1661 | goto err; |
1662 | } | |
1663 | bnpub_key = NULL; | |
1664 | ||
1665 | if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) { | |
1666 | *al = SSL_AD_HANDSHAKE_FAILURE; | |
4fa88861 | 1667 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_DH_KEY_TOO_SMALL); |
e01a610d MC |
1668 | goto err; |
1669 | } | |
1670 | ||
1671 | if (EVP_PKEY_assign_DH(peer_tmp, dh) == 0) { | |
1672 | *al = SSL_AD_INTERNAL_ERROR; | |
4fa88861 | 1673 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_EVP_LIB); |
e01a610d MC |
1674 | goto err; |
1675 | } | |
1676 | ||
1677 | s->s3->peer_tmp = peer_tmp; | |
1678 | ||
1679 | /* | |
1680 | * FIXME: This makes assumptions about which ciphersuites come with | |
1681 | * public keys. We should have a less ad-hoc way of doing this | |
1682 | */ | |
a230b26e | 1683 | if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS)) |
e01a610d MC |
1684 | *pkey = X509_get0_pubkey(s->session->peer); |
1685 | /* else anonymous DH, so no certificate or pkey. */ | |
1686 | ||
1687 | return 1; | |
1688 | ||
1689 | err: | |
1690 | BN_free(p); | |
1691 | BN_free(g); | |
1692 | BN_free(bnpub_key); | |
1693 | DH_free(dh); | |
1694 | EVP_PKEY_free(peer_tmp); | |
1695 | ||
1696 | return 0; | |
1697 | #else | |
4fa88861 | 1698 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_INTERNAL_ERROR); |
e01a610d MC |
1699 | *al = SSL_AD_INTERNAL_ERROR; |
1700 | return 0; | |
1701 | #endif | |
1702 | } | |
1703 | ||
ff74aeb1 MC |
1704 | static int tls_process_ske_ecdhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al) |
1705 | { | |
1706 | #ifndef OPENSSL_NO_EC | |
1707 | PACKET encoded_pt; | |
1708 | const unsigned char *ecparams; | |
1709 | int curve_nid; | |
ec24630a | 1710 | unsigned int curve_flags; |
ff74aeb1 MC |
1711 | EVP_PKEY_CTX *pctx = NULL; |
1712 | ||
1713 | /* | |
1714 | * Extract elliptic curve parameters and the server's ephemeral ECDH | |
1715 | * public key. For now we only support named (not generic) curves and | |
1716 | * ECParameters in this case is just three bytes. | |
1717 | */ | |
1718 | if (!PACKET_get_bytes(pkt, &ecparams, 3)) { | |
1719 | *al = SSL_AD_DECODE_ERROR; | |
4fa88861 | 1720 | SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_LENGTH_TOO_SHORT); |
ff74aeb1 MC |
1721 | return 0; |
1722 | } | |
1723 | /* | |
1724 | * Check curve is one of our preferences, if not server has sent an | |
1725 | * invalid curve. ECParameters is 3 bytes. | |
1726 | */ | |
1727 | if (!tls1_check_curve(s, ecparams, 3)) { | |
1728 | *al = SSL_AD_DECODE_ERROR; | |
4fa88861 | 1729 | SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_WRONG_CURVE); |
ff74aeb1 MC |
1730 | return 0; |
1731 | } | |
1732 | ||
ec24630a DSH |
1733 | curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2), &curve_flags); |
1734 | ||
a230b26e | 1735 | if (curve_nid == 0) { |
ff74aeb1 | 1736 | *al = SSL_AD_INTERNAL_ERROR; |
4fa88861 | 1737 | SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, |
ff74aeb1 MC |
1738 | SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS); |
1739 | return 0; | |
1740 | } | |
1741 | ||
ec24630a DSH |
1742 | if ((curve_flags & TLS_CURVE_TYPE) == TLS_CURVE_CUSTOM) { |
1743 | EVP_PKEY *key = EVP_PKEY_new(); | |
1744 | ||
1745 | if (key == NULL || !EVP_PKEY_set_type(key, curve_nid)) { | |
1746 | *al = SSL_AD_INTERNAL_ERROR; | |
1747 | SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_EVP_LIB); | |
1748 | EVP_PKEY_free(key); | |
1749 | return 0; | |
1750 | } | |
1751 | s->s3->peer_tmp = key; | |
1752 | } else { | |
1753 | /* Set up EVP_PKEY with named curve as parameters */ | |
1754 | pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL); | |
1755 | if (pctx == NULL | |
1756 | || EVP_PKEY_paramgen_init(pctx) <= 0 | |
1757 | || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, curve_nid) <= 0 | |
1758 | || EVP_PKEY_paramgen(pctx, &s->s3->peer_tmp) <= 0) { | |
1759 | *al = SSL_AD_INTERNAL_ERROR; | |
1760 | SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_EVP_LIB); | |
1761 | EVP_PKEY_CTX_free(pctx); | |
1762 | return 0; | |
1763 | } | |
ff74aeb1 | 1764 | EVP_PKEY_CTX_free(pctx); |
ec24630a | 1765 | pctx = NULL; |
ff74aeb1 | 1766 | } |
ff74aeb1 MC |
1767 | |
1768 | if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) { | |
1769 | *al = SSL_AD_DECODE_ERROR; | |
4fa88861 | 1770 | SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_LENGTH_MISMATCH); |
ff74aeb1 MC |
1771 | return 0; |
1772 | } | |
1773 | ||
ec24630a DSH |
1774 | if (!EVP_PKEY_set1_tls_encodedpoint(s->s3->peer_tmp, |
1775 | PACKET_data(&encoded_pt), | |
1776 | PACKET_remaining(&encoded_pt))) { | |
ff74aeb1 | 1777 | *al = SSL_AD_DECODE_ERROR; |
4fa88861 | 1778 | SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_BAD_ECPOINT); |
ff74aeb1 MC |
1779 | return 0; |
1780 | } | |
1781 | ||
1782 | /* | |
1783 | * The ECC/TLS specification does not mention the use of DSA to sign | |
1784 | * ECParameters in the server key exchange message. We do support RSA | |
1785 | * and ECDSA. | |
1786 | */ | |
1787 | if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aECDSA) | |
1788 | *pkey = X509_get0_pubkey(s->session->peer); | |
1789 | else if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aRSA) | |
1790 | *pkey = X509_get0_pubkey(s->session->peer); | |
1791 | /* else anonymous ECDH, so no certificate or pkey. */ | |
1792 | ||
1793 | return 1; | |
1794 | #else | |
4fa88861 | 1795 | SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_INTERNAL_ERROR); |
ff74aeb1 MC |
1796 | *al = SSL_AD_INTERNAL_ERROR; |
1797 | return 0; | |
1798 | #endif | |
1799 | } | |
1800 | ||
be3583fa | 1801 | MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt) |
b9908bf9 | 1802 | { |
7dc1c647 | 1803 | int al = -1; |
e1e588ac | 1804 | long alg_k; |
b9908bf9 | 1805 | EVP_PKEY *pkey = NULL; |
73999b62 | 1806 | PACKET save_param_start, signature; |
b9908bf9 | 1807 | |
b9908bf9 MC |
1808 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
1809 | ||
73999b62 | 1810 | save_param_start = *pkt; |
8d92c1f8 | 1811 | |
3260adf1 | 1812 | #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) |
61dd9f7a DSH |
1813 | EVP_PKEY_free(s->s3->peer_tmp); |
1814 | s->s3->peer_tmp = NULL; | |
3260adf1 | 1815 | #endif |
d02b48c6 | 1816 | |
7689082b | 1817 | if (alg_k & SSL_PSK) { |
7dc1c647 MC |
1818 | if (!tls_process_ske_psk_preamble(s, pkt, &al)) |
1819 | goto err; | |
7689082b DSH |
1820 | } |
1821 | ||
1822 | /* Nothing else to do for plain PSK or RSAPSK */ | |
1823 | if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) { | |
25c6c10c MC |
1824 | } else if (alg_k & SSL_kSRP) { |
1825 | if (!tls_process_ske_srp(s, pkt, &pkey, &al)) | |
0f113f3e | 1826 | goto err; |
e01a610d MC |
1827 | } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) { |
1828 | if (!tls_process_ske_dhe(s, pkt, &pkey, &al)) | |
1829 | goto err; | |
ff74aeb1 MC |
1830 | } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) { |
1831 | if (!tls_process_ske_ecdhe(s, pkt, &pkey, &al)) | |
1832 | goto err; | |
0f113f3e MC |
1833 | } else if (alg_k) { |
1834 | al = SSL_AD_UNEXPECTED_MESSAGE; | |
b9908bf9 | 1835 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); |
e1e588ac | 1836 | goto err; |
0f113f3e | 1837 | } |
0f113f3e | 1838 | |
0f113f3e MC |
1839 | /* if it was signed, check the signature */ |
1840 | if (pkey != NULL) { | |
32942870 | 1841 | PACKET params; |
be8dba2c MC |
1842 | int maxsig; |
1843 | const EVP_MD *md = NULL; | |
e1e588ac MC |
1844 | EVP_MD_CTX *md_ctx; |
1845 | ||
32942870 EK |
1846 | /* |
1847 | * |pkt| now points to the beginning of the signature, so the difference | |
1848 | * equals the length of the parameters. | |
1849 | */ | |
1850 | if (!PACKET_get_sub_packet(&save_param_start, ¶ms, | |
1851 | PACKET_remaining(&save_param_start) - | |
73999b62 | 1852 | PACKET_remaining(pkt))) { |
32942870 | 1853 | al = SSL_AD_INTERNAL_ERROR; |
f0659bdb | 1854 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
e1e588ac | 1855 | goto err; |
32942870 EK |
1856 | } |
1857 | ||
0f113f3e | 1858 | if (SSL_USE_SIGALGS(s)) { |
b6981744 | 1859 | const unsigned char *sigalgs; |
0f113f3e | 1860 | int rv; |
73999b62 | 1861 | if (!PACKET_get_bytes(pkt, &sigalgs, 2)) { |
e1e588ac | 1862 | al = SSL_AD_DECODE_ERROR; |
f0659bdb | 1863 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); |
e1e588ac | 1864 | goto err; |
0f113f3e | 1865 | } |
32942870 | 1866 | rv = tls12_check_peer_sigalg(&md, s, sigalgs, pkey); |
e1e588ac MC |
1867 | if (rv == -1) { |
1868 | al = SSL_AD_INTERNAL_ERROR; | |
1869 | goto err; | |
1870 | } else if (rv == 0) { | |
1871 | al = SSL_AD_DECODE_ERROR; | |
0f113f3e | 1872 | goto err; |
0f113f3e | 1873 | } |
a2f9200f | 1874 | #ifdef SSL_DEBUG |
0f113f3e MC |
1875 | fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md)); |
1876 | #endif | |
3aeb9348 | 1877 | } else if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) { |
192e4bbb | 1878 | md = EVP_md5_sha1(); |
32942870 | 1879 | } else { |
0f113f3e | 1880 | md = EVP_sha1(); |
32942870 | 1881 | } |
0f113f3e | 1882 | |
73999b62 MC |
1883 | if (!PACKET_get_length_prefixed_2(pkt, &signature) |
1884 | || PACKET_remaining(pkt) != 0) { | |
e1e588ac | 1885 | al = SSL_AD_DECODE_ERROR; |
f0659bdb | 1886 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH); |
e1e588ac | 1887 | goto err; |
0f113f3e | 1888 | } |
be8dba2c MC |
1889 | maxsig = EVP_PKEY_size(pkey); |
1890 | if (maxsig < 0) { | |
e1e588ac | 1891 | al = SSL_AD_INTERNAL_ERROR; |
b9908bf9 | 1892 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
e1e588ac | 1893 | goto err; |
8098fc56 | 1894 | } |
0f113f3e MC |
1895 | |
1896 | /* | |
8098fc56 | 1897 | * Check signature length |
0f113f3e | 1898 | */ |
be8dba2c | 1899 | if (PACKET_remaining(&signature) > (size_t)maxsig) { |
0f113f3e | 1900 | /* wrong packet length */ |
e1e588ac | 1901 | al = SSL_AD_DECODE_ERROR; |
a230b26e EK |
1902 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, |
1903 | SSL_R_WRONG_SIGNATURE_LENGTH); | |
e1e588ac MC |
1904 | goto err; |
1905 | } | |
1906 | ||
1907 | md_ctx = EVP_MD_CTX_new(); | |
1908 | if (md_ctx == NULL) { | |
1909 | al = SSL_AD_INTERNAL_ERROR; | |
1910 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); | |
1911 | goto err; | |
0f113f3e | 1912 | } |
e1e588ac | 1913 | |
6e59a892 | 1914 | if (EVP_VerifyInit_ex(md_ctx, md, NULL) <= 0 |
a230b26e EK |
1915 | || EVP_VerifyUpdate(md_ctx, &(s->s3->client_random[0]), |
1916 | SSL3_RANDOM_SIZE) <= 0 | |
1917 | || EVP_VerifyUpdate(md_ctx, &(s->s3->server_random[0]), | |
1918 | SSL3_RANDOM_SIZE) <= 0 | |
1919 | || EVP_VerifyUpdate(md_ctx, PACKET_data(¶ms), | |
1920 | PACKET_remaining(¶ms)) <= 0) { | |
e1e588ac | 1921 | EVP_MD_CTX_free(md_ctx); |
192e4bbb DSH |
1922 | al = SSL_AD_INTERNAL_ERROR; |
1923 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB); | |
e1e588ac | 1924 | goto err; |
192e4bbb | 1925 | } |
348240c6 | 1926 | /* TODO(size_t): Convert this call */ |
6e59a892 | 1927 | if (EVP_VerifyFinal(md_ctx, PACKET_data(&signature), |
348240c6 MC |
1928 | (unsigned int)PACKET_remaining(&signature), |
1929 | pkey) <= 0) { | |
192e4bbb | 1930 | /* bad signature */ |
e1e588ac | 1931 | EVP_MD_CTX_free(md_ctx); |
192e4bbb DSH |
1932 | al = SSL_AD_DECRYPT_ERROR; |
1933 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE); | |
e1e588ac | 1934 | goto err; |
0f113f3e | 1935 | } |
e1e588ac | 1936 | EVP_MD_CTX_free(md_ctx); |
0f113f3e | 1937 | } else { |
7689082b | 1938 | /* aNULL, aSRP or PSK do not need public keys */ |
e1e588ac | 1939 | if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) |
a230b26e | 1940 | && !(alg_k & SSL_PSK)) { |
0f113f3e | 1941 | /* Might be wrong key type, check it */ |
e1e588ac | 1942 | if (ssl3_check_cert_and_algorithm(s)) { |
0f113f3e | 1943 | /* Otherwise this shouldn't happen */ |
e1e588ac | 1944 | al = SSL_AD_INTERNAL_ERROR; |
b9908bf9 | 1945 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
e1e588ac MC |
1946 | } else { |
1947 | al = SSL_AD_DECODE_ERROR; | |
1948 | } | |
0f113f3e MC |
1949 | goto err; |
1950 | } | |
1951 | /* still data left over */ | |
73999b62 | 1952 | if (PACKET_remaining(pkt) != 0) { |
e1e588ac | 1953 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 1954 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE); |
e1e588ac | 1955 | goto err; |
0f113f3e MC |
1956 | } |
1957 | } | |
e1e588ac | 1958 | |
b9908bf9 | 1959 | return MSG_PROCESS_CONTINUE_READING; |
0f113f3e | 1960 | err: |
7dc1c647 MC |
1961 | if (al != -1) |
1962 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
fe3a3291 | 1963 | ossl_statem_set_error(s); |
b9908bf9 | 1964 | return MSG_PROCESS_ERROR; |
0f113f3e | 1965 | } |
d02b48c6 | 1966 | |
be3583fa | 1967 | MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt) |
b9908bf9 MC |
1968 | { |
1969 | int ret = MSG_PROCESS_ERROR; | |
1970 | unsigned int list_len, ctype_num, i, name_len; | |
1971 | X509_NAME *xn = NULL; | |
b6981744 EK |
1972 | const unsigned char *data; |
1973 | const unsigned char *namestart, *namebytes; | |
b9908bf9 | 1974 | STACK_OF(X509_NAME) *ca_sk = NULL; |
0f113f3e MC |
1975 | |
1976 | if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) { | |
b9908bf9 | 1977 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
1978 | goto err; |
1979 | } | |
1980 | ||
1981 | /* get the certificate types */ | |
73999b62 | 1982 | if (!PACKET_get_1(pkt, &ctype_num) |
a230b26e | 1983 | || !PACKET_get_bytes(pkt, &data, ctype_num)) { |
ac112332 | 1984 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 | 1985 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH); |
ac112332 MC |
1986 | goto err; |
1987 | } | |
b548a1f1 RS |
1988 | OPENSSL_free(s->cert->ctypes); |
1989 | s->cert->ctypes = NULL; | |
0f113f3e MC |
1990 | if (ctype_num > SSL3_CT_NUMBER) { |
1991 | /* If we exceed static buffer copy all to cert structure */ | |
1992 | s->cert->ctypes = OPENSSL_malloc(ctype_num); | |
1993 | if (s->cert->ctypes == NULL) { | |
b9908bf9 | 1994 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
1995 | goto err; |
1996 | } | |
ac112332 | 1997 | memcpy(s->cert->ctypes, data, ctype_num); |
d736bc1a | 1998 | s->cert->ctype_num = ctype_num; |
0f113f3e MC |
1999 | ctype_num = SSL3_CT_NUMBER; |
2000 | } | |
2001 | for (i = 0; i < ctype_num; i++) | |
ac112332 MC |
2002 | s->s3->tmp.ctype[i] = data[i]; |
2003 | ||
0f113f3e | 2004 | if (SSL_USE_SIGALGS(s)) { |
73999b62 | 2005 | if (!PACKET_get_net_2(pkt, &list_len) |
a230b26e | 2006 | || !PACKET_get_bytes(pkt, &data, list_len)) { |
0f113f3e | 2007 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 MC |
2008 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, |
2009 | SSL_R_LENGTH_MISMATCH); | |
0f113f3e MC |
2010 | goto err; |
2011 | } | |
ac112332 | 2012 | |
0f113f3e MC |
2013 | /* Clear certificate digests and validity flags */ |
2014 | for (i = 0; i < SSL_PKEY_NUM; i++) { | |
d376e57d | 2015 | s->s3->tmp.md[i] = NULL; |
6383d316 | 2016 | s->s3->tmp.valid_flags[i] = 0; |
0f113f3e | 2017 | } |
ac112332 | 2018 | if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) { |
0f113f3e | 2019 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 | 2020 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, |
0f113f3e MC |
2021 | SSL_R_SIGNATURE_ALGORITHMS_ERROR); |
2022 | goto err; | |
2023 | } | |
2024 | if (!tls1_process_sigalgs(s)) { | |
2025 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
b9908bf9 | 2026 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
2027 | goto err; |
2028 | } | |
a0f63828 DSH |
2029 | } else { |
2030 | ssl_set_default_md(s); | |
0f113f3e MC |
2031 | } |
2032 | ||
2033 | /* get the CA RDNs */ | |
73999b62 | 2034 | if (!PACKET_get_net_2(pkt, &list_len) |
a230b26e | 2035 | || PACKET_remaining(pkt) != list_len) { |
0f113f3e | 2036 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 | 2037 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH); |
0f113f3e MC |
2038 | goto err; |
2039 | } | |
2040 | ||
73999b62 MC |
2041 | while (PACKET_remaining(pkt)) { |
2042 | if (!PACKET_get_net_2(pkt, &name_len) | |
a230b26e | 2043 | || !PACKET_get_bytes(pkt, &namebytes, name_len)) { |
0f113f3e | 2044 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 MC |
2045 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, |
2046 | SSL_R_LENGTH_MISMATCH); | |
0f113f3e MC |
2047 | goto err; |
2048 | } | |
2049 | ||
ac112332 | 2050 | namestart = namebytes; |
0f113f3e | 2051 | |
ac112332 MC |
2052 | if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes, |
2053 | name_len)) == NULL) { | |
3c33c6f6 | 2054 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 | 2055 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB); |
3c33c6f6 | 2056 | goto err; |
0f113f3e MC |
2057 | } |
2058 | ||
ac112332 | 2059 | if (namebytes != (namestart + name_len)) { |
0f113f3e | 2060 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 | 2061 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, |
0f113f3e MC |
2062 | SSL_R_CA_DN_LENGTH_MISMATCH); |
2063 | goto err; | |
2064 | } | |
2065 | if (!sk_X509_NAME_push(ca_sk, xn)) { | |
b9908bf9 | 2066 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
2067 | goto err; |
2068 | } | |
6afef8b1 | 2069 | xn = NULL; |
0f113f3e MC |
2070 | } |
2071 | ||
0f113f3e MC |
2072 | /* we should setup a certificate to return.... */ |
2073 | s->s3->tmp.cert_req = 1; | |
2074 | s->s3->tmp.ctype_num = ctype_num; | |
222561fe | 2075 | sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free); |
0f113f3e MC |
2076 | s->s3->tmp.ca_names = ca_sk; |
2077 | ca_sk = NULL; | |
2078 | ||
05c4f1d5 | 2079 | ret = MSG_PROCESS_CONTINUE_PROCESSING; |
cc273a93 | 2080 | goto done; |
0f113f3e | 2081 | err: |
fe3a3291 | 2082 | ossl_statem_set_error(s); |
cc273a93 | 2083 | done: |
6afef8b1 | 2084 | X509_NAME_free(xn); |
222561fe | 2085 | sk_X509_NAME_pop_free(ca_sk, X509_NAME_free); |
b9908bf9 | 2086 | return ret; |
0f113f3e MC |
2087 | } |
2088 | ||
2089 | static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b) | |
dfeab068 | 2090 | { |
0f113f3e | 2091 | return (X509_NAME_cmp(*a, *b)); |
dfeab068 | 2092 | } |
dfeab068 | 2093 | |
be3583fa | 2094 | MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt) |
b9908bf9 MC |
2095 | { |
2096 | int al; | |
2097 | unsigned int ticklen; | |
2098 | unsigned long ticket_lifetime_hint; | |
ec60ccc1 | 2099 | unsigned int sess_len; |
b9908bf9 | 2100 | |
73999b62 | 2101 | if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint) |
a230b26e EK |
2102 | || !PACKET_get_net_2(pkt, &ticklen) |
2103 | || PACKET_remaining(pkt) != ticklen) { | |
e711da71 | 2104 | al = SSL_AD_DECODE_ERROR; |
f0659bdb | 2105 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH); |
e711da71 EK |
2106 | goto f_err; |
2107 | } | |
2108 | ||
2109 | /* Server is allowed to change its mind and send an empty ticket. */ | |
2110 | if (ticklen == 0) | |
c9de4a20 | 2111 | return MSG_PROCESS_CONTINUE_READING; |
e711da71 | 2112 | |
98ece4ee MC |
2113 | if (s->session->session_id_length > 0) { |
2114 | int i = s->session_ctx->session_cache_mode; | |
2115 | SSL_SESSION *new_sess; | |
2116 | /* | |
2117 | * We reused an existing session, so we need to replace it with a new | |
2118 | * one | |
2119 | */ | |
2120 | if (i & SSL_SESS_CACHE_CLIENT) { | |
2121 | /* | |
e4612d02 | 2122 | * Remove the old session from the cache. We carry on if this fails |
98ece4ee | 2123 | */ |
e4612d02 | 2124 | SSL_CTX_remove_session(s->session_ctx, s->session); |
98ece4ee MC |
2125 | } |
2126 | ||
2127 | if ((new_sess = ssl_session_dup(s->session, 0)) == 0) { | |
2128 | al = SSL_AD_INTERNAL_ERROR; | |
b9908bf9 | 2129 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE); |
98ece4ee MC |
2130 | goto f_err; |
2131 | } | |
2132 | ||
2133 | SSL_SESSION_free(s->session); | |
2134 | s->session = new_sess; | |
2135 | } | |
2136 | ||
aff8c126 RS |
2137 | OPENSSL_free(s->session->ext.tick); |
2138 | s->session->ext.tick = NULL; | |
2139 | s->session->ext.ticklen = 0; | |
e711da71 | 2140 | |
aff8c126 RS |
2141 | s->session->ext.tick = OPENSSL_malloc(ticklen); |
2142 | if (s->session->ext.tick == NULL) { | |
b9908bf9 | 2143 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
2144 | goto err; |
2145 | } | |
aff8c126 | 2146 | if (!PACKET_copy_bytes(pkt, s->session->ext.tick, ticklen)) { |
561e12bb | 2147 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 2148 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH); |
561e12bb MC |
2149 | goto f_err; |
2150 | } | |
e711da71 | 2151 | |
aff8c126 RS |
2152 | s->session->ext.tick_lifetime_hint = ticket_lifetime_hint; |
2153 | s->session->ext.ticklen = ticklen; | |
0f113f3e MC |
2154 | /* |
2155 | * There are two ways to detect a resumed ticket session. One is to set | |
2156 | * an appropriate session ID and then the server must return a match in | |
2157 | * ServerHello. This allows the normal client session ID matching to work | |
2158 | * and we know much earlier that the ticket has been accepted. The | |
2159 | * other way is to set zero length session ID when the ticket is | |
2160 | * presented and rely on the handshake to determine session resumption. | |
2161 | * We choose the former approach because this fits in with assumptions | |
2162 | * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is | |
2163 | * SHA256 is disabled) hash of the ticket. | |
2164 | */ | |
ec60ccc1 MC |
2165 | /* |
2166 | * TODO(size_t): we use sess_len here because EVP_Digest expects an int | |
2167 | * but s->session->session_id_length is a size_t | |
2168 | */ | |
aff8c126 | 2169 | if (!EVP_Digest(s->session->ext.tick, ticklen, |
ec60ccc1 | 2170 | s->session->session_id, &sess_len, |
d166ed8c DSH |
2171 | EVP_sha256(), NULL)) { |
2172 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_EVP_LIB); | |
2173 | goto err; | |
2174 | } | |
ec60ccc1 | 2175 | s->session->session_id_length = sess_len; |
b9908bf9 | 2176 | return MSG_PROCESS_CONTINUE_READING; |
0f113f3e MC |
2177 | f_err: |
2178 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
2179 | err: | |
fe3a3291 | 2180 | ossl_statem_set_error(s); |
b9908bf9 | 2181 | return MSG_PROCESS_ERROR; |
0f113f3e | 2182 | } |
67c8e7f4 | 2183 | |
f63e4288 MC |
2184 | /* |
2185 | * In TLSv1.3 this is called from the extensions code, otherwise it is used to | |
2186 | * parse a separate message. Returns 1 on success or 0 on failure. On failure | |
2187 | * |*al| is populated with a suitable alert code. | |
2188 | */ | |
2189 | int tls_process_cert_status_body(SSL *s, PACKET *pkt, int *al) | |
b9908bf9 | 2190 | { |
8b0e934a | 2191 | size_t resplen; |
b9908bf9 | 2192 | unsigned int type; |
b9908bf9 | 2193 | |
73999b62 | 2194 | if (!PACKET_get_1(pkt, &type) |
a230b26e | 2195 | || type != TLSEXT_STATUSTYPE_ocsp) { |
f63e4288 MC |
2196 | *al = SSL_AD_DECODE_ERROR; |
2197 | SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS_BODY, | |
2198 | SSL_R_UNSUPPORTED_STATUS_TYPE); | |
2199 | return 0; | |
0f113f3e | 2200 | } |
56a26ce3 MC |
2201 | if (!PACKET_get_net_3_len(pkt, &resplen) |
2202 | || PACKET_remaining(pkt) != resplen) { | |
f63e4288 MC |
2203 | *al = SSL_AD_DECODE_ERROR; |
2204 | SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS_BODY, SSL_R_LENGTH_MISMATCH); | |
2205 | return 0; | |
0f113f3e | 2206 | } |
8cbfcc70 RS |
2207 | s->ext.ocsp.resp = OPENSSL_malloc(resplen); |
2208 | if (s->ext.ocsp.resp == NULL) { | |
f63e4288 MC |
2209 | *al = SSL_AD_INTERNAL_ERROR; |
2210 | SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS_BODY, ERR_R_MALLOC_FAILURE); | |
2211 | return 0; | |
0f113f3e | 2212 | } |
8cbfcc70 | 2213 | if (!PACKET_copy_bytes(pkt, s->ext.ocsp.resp, resplen)) { |
f63e4288 MC |
2214 | *al = SSL_AD_DECODE_ERROR; |
2215 | SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS_BODY, SSL_R_LENGTH_MISMATCH); | |
2216 | return 0; | |
ac63710a | 2217 | } |
8cbfcc70 | 2218 | s->ext.ocsp.resp_len = resplen; |
f63e4288 MC |
2219 | |
2220 | return 1; | |
2221 | } | |
2222 | ||
2223 | ||
2224 | MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt) | |
2225 | { | |
2226 | int al; | |
2227 | ||
2228 | if (!tls_process_cert_status_body(s, pkt, &al)) { | |
2229 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
2230 | ossl_statem_set_error(s); | |
2231 | return MSG_PROCESS_ERROR; | |
2232 | } | |
2233 | ||
b9908bf9 | 2234 | return MSG_PROCESS_CONTINUE_READING; |
0f113f3e | 2235 | } |
d02b48c6 | 2236 | |
7776a36c MC |
2237 | /* |
2238 | * Perform miscellaneous checks and processing after we have received the | |
2239 | * server's initial flight. In TLS1.3 this is after the Server Finished message. | |
6530c490 MC |
2240 | * In <=TLS1.2 this is after the ServerDone message. Returns 1 on success or 0 |
2241 | * on failure. | |
7776a36c MC |
2242 | */ |
2243 | int tls_process_initial_server_flight(SSL *s, int *al) | |
b9908bf9 | 2244 | { |
a455d0f6 MC |
2245 | /* |
2246 | * at this point we check that we have the required stuff from | |
2247 | * the server | |
2248 | */ | |
2249 | if (!ssl3_check_cert_and_algorithm(s)) { | |
7776a36c MC |
2250 | *al = SSL_AD_HANDSHAKE_FAILURE; |
2251 | return 0; | |
a455d0f6 MC |
2252 | } |
2253 | ||
bb1aaab4 | 2254 | /* |
aff8c126 RS |
2255 | * Call the ocsp status callback if needed. The |ext.ocsp.resp| and |
2256 | * |ext.ocsp.resp_len| values will be set if we actually received a status | |
bb1aaab4 MC |
2257 | * message, or NULL and -1 otherwise |
2258 | */ | |
aff8c126 RS |
2259 | if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing |
2260 | && s->ctx->ext.status_cb != NULL) { | |
2261 | int ret = s->ctx->ext.status_cb(s, s->ctx->ext.status_arg); | |
2262 | ||
bb1aaab4 | 2263 | if (ret == 0) { |
7776a36c MC |
2264 | *al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; |
2265 | SSLerr(SSL_F_TLS_PROCESS_INITIAL_SERVER_FLIGHT, | |
bb1aaab4 | 2266 | SSL_R_INVALID_STATUS_RESPONSE); |
7776a36c | 2267 | return 0; |
bb1aaab4 MC |
2268 | } |
2269 | if (ret < 0) { | |
7776a36c MC |
2270 | *al = SSL_AD_INTERNAL_ERROR; |
2271 | SSLerr(SSL_F_TLS_PROCESS_INITIAL_SERVER_FLIGHT, | |
2272 | ERR_R_MALLOC_FAILURE); | |
2273 | return 0; | |
bb1aaab4 MC |
2274 | } |
2275 | } | |
ed29e82a RP |
2276 | #ifndef OPENSSL_NO_CT |
2277 | if (s->ct_validation_callback != NULL) { | |
43341433 VD |
2278 | /* Note we validate the SCTs whether or not we abort on error */ |
2279 | if (!ssl_validate_ct(s) && (s->verify_mode & SSL_VERIFY_PEER)) { | |
7776a36c MC |
2280 | *al = SSL_AD_HANDSHAKE_FAILURE; |
2281 | return 0; | |
ed29e82a RP |
2282 | } |
2283 | } | |
2284 | #endif | |
2285 | ||
7776a36c MC |
2286 | return 1; |
2287 | } | |
2288 | ||
2289 | MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt) | |
2290 | { | |
2291 | int al = SSL_AD_INTERNAL_ERROR; | |
2292 | ||
2293 | if (PACKET_remaining(pkt) > 0) { | |
2294 | /* should contain no data */ | |
2295 | al = SSL_AD_DECODE_ERROR; | |
2296 | SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH); | |
2297 | goto err; | |
2298 | } | |
2299 | #ifndef OPENSSL_NO_SRP | |
2300 | if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) { | |
2301 | if (SRP_Calc_A_param(s) <= 0) { | |
2302 | SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC); | |
2303 | goto err; | |
2304 | } | |
2305 | } | |
2306 | #endif | |
2307 | ||
2308 | /* | |
2309 | * Error queue messages are generated directly by this function | |
2310 | */ | |
2311 | if (!tls_process_initial_server_flight(s, &al)) | |
2312 | goto err; | |
2313 | ||
473483d4 MC |
2314 | #ifndef OPENSSL_NO_SCTP |
2315 | /* Only applies to renegotiation */ | |
2316 | if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s)) | |
a230b26e | 2317 | && s->renegotiate != 0) |
473483d4 MC |
2318 | return MSG_PROCESS_CONTINUE_PROCESSING; |
2319 | else | |
2320 | #endif | |
2321 | return MSG_PROCESS_FINISHED_READING; | |
7776a36c MC |
2322 | |
2323 | err: | |
2324 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
2325 | ossl_statem_set_error(s); | |
2326 | return MSG_PROCESS_ERROR; | |
0f113f3e | 2327 | } |
176f31dd | 2328 | |
f1ec23c0 | 2329 | static int tls_construct_cke_psk_preamble(SSL *s, WPACKET *pkt, int *al) |
0f113f3e | 2330 | { |
7689082b | 2331 | #ifndef OPENSSL_NO_PSK |
13c0ec4a MC |
2332 | int ret = 0; |
2333 | /* | |
2334 | * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a | |
2335 | * \0-terminated identity. The last byte is for us for simulating | |
2336 | * strnlen. | |
2337 | */ | |
2338 | char identity[PSK_MAX_IDENTITY_LEN + 1]; | |
2339 | size_t identitylen = 0; | |
2340 | unsigned char psk[PSK_MAX_PSK_LEN]; | |
2341 | unsigned char *tmppsk = NULL; | |
2342 | char *tmpidentity = NULL; | |
2343 | size_t psklen = 0; | |
2344 | ||
2345 | if (s->psk_client_callback == NULL) { | |
05ec6a25 | 2346 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, SSL_R_PSK_NO_CLIENT_CB); |
13c0ec4a MC |
2347 | *al = SSL_AD_INTERNAL_ERROR; |
2348 | goto err; | |
2349 | } | |
d02b48c6 | 2350 | |
13c0ec4a | 2351 | memset(identity, 0, sizeof(identity)); |
d02b48c6 | 2352 | |
13c0ec4a MC |
2353 | psklen = s->psk_client_callback(s, s->session->psk_identity_hint, |
2354 | identity, sizeof(identity) - 1, | |
2355 | psk, sizeof(psk)); | |
7689082b | 2356 | |
13c0ec4a | 2357 | if (psklen > PSK_MAX_PSK_LEN) { |
05ec6a25 | 2358 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR); |
13c0ec4a MC |
2359 | *al = SSL_AD_HANDSHAKE_FAILURE; |
2360 | goto err; | |
2361 | } else if (psklen == 0) { | |
05ec6a25 | 2362 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, |
13c0ec4a MC |
2363 | SSL_R_PSK_IDENTITY_NOT_FOUND); |
2364 | *al = SSL_AD_HANDSHAKE_FAILURE; | |
2365 | goto err; | |
2366 | } | |
7689082b | 2367 | |
13c0ec4a MC |
2368 | identitylen = strlen(identity); |
2369 | if (identitylen > PSK_MAX_IDENTITY_LEN) { | |
05ec6a25 | 2370 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR); |
13c0ec4a MC |
2371 | *al = SSL_AD_HANDSHAKE_FAILURE; |
2372 | goto err; | |
2373 | } | |
7689082b | 2374 | |
13c0ec4a MC |
2375 | tmppsk = OPENSSL_memdup(psk, psklen); |
2376 | tmpidentity = OPENSSL_strdup(identity); | |
2377 | if (tmppsk == NULL || tmpidentity == NULL) { | |
05ec6a25 | 2378 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_MALLOC_FAILURE); |
13c0ec4a MC |
2379 | *al = SSL_AD_INTERNAL_ERROR; |
2380 | goto err; | |
2381 | } | |
7689082b | 2382 | |
13c0ec4a MC |
2383 | OPENSSL_free(s->s3->tmp.psk); |
2384 | s->s3->tmp.psk = tmppsk; | |
2385 | s->s3->tmp.psklen = psklen; | |
2386 | tmppsk = NULL; | |
2387 | OPENSSL_free(s->session->psk_identity); | |
2388 | s->session->psk_identity = tmpidentity; | |
2389 | tmpidentity = NULL; | |
f1ec23c0 | 2390 | |
b2b3024e | 2391 | if (!WPACKET_sub_memcpy_u16(pkt, identity, identitylen)) { |
f1ec23c0 MC |
2392 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR); |
2393 | *al = SSL_AD_INTERNAL_ERROR; | |
2394 | goto err; | |
2395 | } | |
7689082b | 2396 | |
13c0ec4a | 2397 | ret = 1; |
0bce0b02 | 2398 | |
13c0ec4a MC |
2399 | err: |
2400 | OPENSSL_cleanse(psk, psklen); | |
2401 | OPENSSL_cleanse(identity, sizeof(identity)); | |
2402 | OPENSSL_clear_free(tmppsk, psklen); | |
2403 | OPENSSL_clear_free(tmpidentity, identitylen); | |
d02b48c6 | 2404 | |
13c0ec4a MC |
2405 | return ret; |
2406 | #else | |
05ec6a25 | 2407 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR); |
13c0ec4a MC |
2408 | *al = SSL_AD_INTERNAL_ERROR; |
2409 | return 0; | |
b9908bf9 | 2410 | #endif |
13c0ec4a | 2411 | } |
b9908bf9 | 2412 | |
f1ec23c0 | 2413 | static int tls_construct_cke_rsa(SSL *s, WPACKET *pkt, int *al) |
13c0ec4a | 2414 | { |
bc36ee62 | 2415 | #ifndef OPENSSL_NO_RSA |
f1ec23c0 | 2416 | unsigned char *encdata = NULL; |
13c0ec4a MC |
2417 | EVP_PKEY *pkey = NULL; |
2418 | EVP_PKEY_CTX *pctx = NULL; | |
2419 | size_t enclen; | |
2420 | unsigned char *pms = NULL; | |
2421 | size_t pmslen = 0; | |
b9908bf9 | 2422 | |
13c0ec4a MC |
2423 | if (s->session->peer == NULL) { |
2424 | /* | |
2425 | * We should always have a server certificate with SSL_kRSA. | |
2426 | */ | |
05ec6a25 | 2427 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); |
13c0ec4a MC |
2428 | return 0; |
2429 | } | |
0f113f3e | 2430 | |
13c0ec4a MC |
2431 | pkey = X509_get0_pubkey(s->session->peer); |
2432 | if (EVP_PKEY_get0_RSA(pkey) == NULL) { | |
05ec6a25 | 2433 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); |
13c0ec4a MC |
2434 | return 0; |
2435 | } | |
0f113f3e | 2436 | |
13c0ec4a MC |
2437 | pmslen = SSL_MAX_MASTER_KEY_LENGTH; |
2438 | pms = OPENSSL_malloc(pmslen); | |
2439 | if (pms == NULL) { | |
05ec6a25 | 2440 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_MALLOC_FAILURE); |
13c0ec4a MC |
2441 | *al = SSL_AD_INTERNAL_ERROR; |
2442 | return 0; | |
2443 | } | |
0bce0b02 | 2444 | |
13c0ec4a MC |
2445 | pms[0] = s->client_version >> 8; |
2446 | pms[1] = s->client_version & 0xff; | |
348240c6 MC |
2447 | /* TODO(size_t): Convert this function */ |
2448 | if (RAND_bytes(pms + 2, (int)(pmslen - 2)) <= 0) { | |
13c0ec4a MC |
2449 | goto err; |
2450 | } | |
0f113f3e | 2451 | |
13c0ec4a | 2452 | /* Fix buf for TLS and beyond */ |
f1ec23c0 MC |
2453 | if (s->version > SSL3_VERSION && !WPACKET_start_sub_packet_u16(pkt)) { |
2454 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); | |
2455 | goto err; | |
2456 | } | |
13c0ec4a MC |
2457 | pctx = EVP_PKEY_CTX_new(pkey, NULL); |
2458 | if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0 | |
2459 | || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) { | |
05ec6a25 | 2460 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_EVP_LIB); |
13c0ec4a MC |
2461 | goto err; |
2462 | } | |
f1ec23c0 MC |
2463 | if (!WPACKET_allocate_bytes(pkt, enclen, &encdata) |
2464 | || EVP_PKEY_encrypt(pctx, encdata, &enclen, pms, pmslen) <= 0) { | |
05ec6a25 | 2465 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, SSL_R_BAD_RSA_ENCRYPT); |
13c0ec4a MC |
2466 | goto err; |
2467 | } | |
13c0ec4a MC |
2468 | EVP_PKEY_CTX_free(pctx); |
2469 | pctx = NULL; | |
0f113f3e | 2470 | # ifdef PKCS1_CHECK |
13c0ec4a MC |
2471 | if (s->options & SSL_OP_PKCS1_CHECK_1) |
2472 | (*p)[1]++; | |
2473 | if (s->options & SSL_OP_PKCS1_CHECK_2) | |
2474 | tmp_buf[0] = 0x70; | |
0f113f3e | 2475 | # endif |
0f113f3e | 2476 | |
13c0ec4a | 2477 | /* Fix buf for TLS and beyond */ |
f1ec23c0 MC |
2478 | if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) { |
2479 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); | |
2480 | goto err; | |
b9908bf9 | 2481 | } |
13c0ec4a MC |
2482 | |
2483 | s->s3->tmp.pms = pms; | |
2484 | s->s3->tmp.pmslen = pmslen; | |
2485 | ||
2486 | return 1; | |
2487 | err: | |
2488 | OPENSSL_clear_free(pms, pmslen); | |
2489 | EVP_PKEY_CTX_free(pctx); | |
2490 | ||
2491 | return 0; | |
2492 | #else | |
05ec6a25 | 2493 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); |
13c0ec4a MC |
2494 | *al = SSL_AD_INTERNAL_ERROR; |
2495 | return 0; | |
f9b3bff6 | 2496 | #endif |
13c0ec4a MC |
2497 | } |
2498 | ||
f1ec23c0 | 2499 | static int tls_construct_cke_dhe(SSL *s, WPACKET *pkt, int *al) |
a8c1c704 MC |
2500 | { |
2501 | #ifndef OPENSSL_NO_DH | |
2502 | DH *dh_clnt = NULL; | |
2503 | const BIGNUM *pub_key; | |
2504 | EVP_PKEY *ckey = NULL, *skey = NULL; | |
f1ec23c0 | 2505 | unsigned char *keybytes = NULL; |
a8c1c704 MC |
2506 | |
2507 | skey = s->s3->peer_tmp; | |
f1ec23c0 MC |
2508 | if (skey == NULL) |
2509 | goto err; | |
2510 | ||
0a699a07 | 2511 | ckey = ssl_generate_pkey(skey); |
b599ce3b MC |
2512 | if (ckey == NULL) |
2513 | goto err; | |
2514 | ||
a8c1c704 MC |
2515 | dh_clnt = EVP_PKEY_get0_DH(ckey); |
2516 | ||
0f1e51ea | 2517 | if (dh_clnt == NULL || ssl_derive(s, ckey, skey, 0) == 0) |
f1ec23c0 | 2518 | goto err; |
a8c1c704 MC |
2519 | |
2520 | /* send off the data */ | |
2521 | DH_get0_key(dh_clnt, &pub_key, NULL); | |
b2b3024e | 2522 | if (!WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(pub_key), &keybytes)) |
f1ec23c0 MC |
2523 | goto err; |
2524 | ||
2525 | BN_bn2bin(pub_key, keybytes); | |
a8c1c704 MC |
2526 | EVP_PKEY_free(ckey); |
2527 | ||
2528 | return 1; | |
f1ec23c0 MC |
2529 | err: |
2530 | EVP_PKEY_free(ckey); | |
2531 | #endif | |
05ec6a25 | 2532 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_DHE, ERR_R_INTERNAL_ERROR); |
a8c1c704 MC |
2533 | *al = SSL_AD_INTERNAL_ERROR; |
2534 | return 0; | |
a8c1c704 MC |
2535 | } |
2536 | ||
f1ec23c0 | 2537 | static int tls_construct_cke_ecdhe(SSL *s, WPACKET *pkt, int *al) |
67ad5aab MC |
2538 | { |
2539 | #ifndef OPENSSL_NO_EC | |
2540 | unsigned char *encodedPoint = NULL; | |
348240c6 | 2541 | size_t encoded_pt_len = 0; |
67ad5aab | 2542 | EVP_PKEY *ckey = NULL, *skey = NULL; |
f1ec23c0 | 2543 | int ret = 0; |
67ad5aab MC |
2544 | |
2545 | skey = s->s3->peer_tmp; | |
ec24630a | 2546 | if (skey == NULL) { |
05ec6a25 | 2547 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR); |
67ad5aab MC |
2548 | return 0; |
2549 | } | |
2550 | ||
0a699a07 | 2551 | ckey = ssl_generate_pkey(skey); |
b599ce3b MC |
2552 | if (ckey == NULL) { |
2553 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_MALLOC_FAILURE); | |
2554 | goto err; | |
2555 | } | |
67ad5aab | 2556 | |
0f1e51ea | 2557 | if (ssl_derive(s, ckey, skey, 0) == 0) { |
05ec6a25 | 2558 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_EVP_LIB); |
67ad5aab MC |
2559 | goto err; |
2560 | } | |
2561 | ||
2562 | /* Generate encoding of client key */ | |
ec24630a | 2563 | encoded_pt_len = EVP_PKEY_get1_tls_encodedpoint(ckey, &encodedPoint); |
67ad5aab MC |
2564 | |
2565 | if (encoded_pt_len == 0) { | |
05ec6a25 | 2566 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_EC_LIB); |
67ad5aab MC |
2567 | goto err; |
2568 | } | |
2569 | ||
b2b3024e | 2570 | if (!WPACKET_sub_memcpy_u8(pkt, encodedPoint, encoded_pt_len)) { |
f1ec23c0 MC |
2571 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR); |
2572 | goto err; | |
2573 | } | |
67ad5aab | 2574 | |
f1ec23c0 | 2575 | ret = 1; |
67ad5aab | 2576 | err: |
f1ec23c0 | 2577 | OPENSSL_free(encodedPoint); |
67ad5aab | 2578 | EVP_PKEY_free(ckey); |
f1ec23c0 | 2579 | return ret; |
67ad5aab | 2580 | #else |
05ec6a25 | 2581 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR); |
67ad5aab MC |
2582 | *al = SSL_AD_INTERNAL_ERROR; |
2583 | return 0; | |
2584 | #endif | |
2585 | } | |
2586 | ||
f1ec23c0 | 2587 | static int tls_construct_cke_gost(SSL *s, WPACKET *pkt, int *al) |
e00e0b3d MC |
2588 | { |
2589 | #ifndef OPENSSL_NO_GOST | |
2590 | /* GOST key exchange message creation */ | |
2591 | EVP_PKEY_CTX *pkey_ctx = NULL; | |
2592 | X509 *peer_cert; | |
2593 | size_t msglen; | |
2594 | unsigned int md_len; | |
2595 | unsigned char shared_ukm[32], tmp[256]; | |
2596 | EVP_MD_CTX *ukm_hash = NULL; | |
2597 | int dgst_nid = NID_id_GostR3411_94; | |
2598 | unsigned char *pms = NULL; | |
2599 | size_t pmslen = 0; | |
2600 | ||
2601 | if ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0) | |
2602 | dgst_nid = NID_id_GostR3411_2012_256; | |
2603 | ||
2604 | /* | |
2605 | * Get server sertificate PKEY and create ctx from it | |
2606 | */ | |
2607 | peer_cert = s->session->peer; | |
2608 | if (!peer_cert) { | |
2609 | *al = SSL_AD_HANDSHAKE_FAILURE; | |
05ec6a25 | 2610 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, |
e00e0b3d MC |
2611 | SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER); |
2612 | return 0; | |
2613 | } | |
2614 | ||
2615 | pkey_ctx = EVP_PKEY_CTX_new(X509_get0_pubkey(peer_cert), NULL); | |
2616 | if (pkey_ctx == NULL) { | |
2617 | *al = SSL_AD_INTERNAL_ERROR; | |
05ec6a25 | 2618 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_MALLOC_FAILURE); |
e00e0b3d MC |
2619 | return 0; |
2620 | } | |
2621 | /* | |
2622 | * If we have send a certificate, and certificate key | |
2623 | * parameters match those of server certificate, use | |
2624 | * certificate key for key exchange | |
2625 | */ | |
2626 | ||
2627 | /* Otherwise, generate ephemeral key pair */ | |
2628 | pmslen = 32; | |
2629 | pms = OPENSSL_malloc(pmslen); | |
2630 | if (pms == NULL) { | |
2631 | *al = SSL_AD_INTERNAL_ERROR; | |
05ec6a25 | 2632 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_MALLOC_FAILURE); |
2f3930bc | 2633 | goto err; |
e00e0b3d MC |
2634 | } |
2635 | ||
2636 | if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0 | |
348240c6 MC |
2637 | /* Generate session key |
2638 | * TODO(size_t): Convert this function | |
2639 | */ | |
2640 | || RAND_bytes(pms, (int)pmslen) <= 0) { | |
e00e0b3d | 2641 | *al = SSL_AD_INTERNAL_ERROR; |
05ec6a25 | 2642 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR); |
e00e0b3d MC |
2643 | goto err; |
2644 | }; | |
e00e0b3d MC |
2645 | /* |
2646 | * Compute shared IV and store it in algorithm-specific context | |
2647 | * data | |
2648 | */ | |
2649 | ukm_hash = EVP_MD_CTX_new(); | |
2650 | if (ukm_hash == NULL | |
a230b26e EK |
2651 | || EVP_DigestInit(ukm_hash, EVP_get_digestbynid(dgst_nid)) <= 0 |
2652 | || EVP_DigestUpdate(ukm_hash, s->s3->client_random, | |
2653 | SSL3_RANDOM_SIZE) <= 0 | |
2654 | || EVP_DigestUpdate(ukm_hash, s->s3->server_random, | |
2655 | SSL3_RANDOM_SIZE) <= 0 | |
2656 | || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) { | |
e00e0b3d | 2657 | *al = SSL_AD_INTERNAL_ERROR; |
05ec6a25 | 2658 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR); |
e00e0b3d MC |
2659 | goto err; |
2660 | } | |
2661 | EVP_MD_CTX_free(ukm_hash); | |
2662 | ukm_hash = NULL; | |
2663 | if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, | |
2664 | EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) < 0) { | |
2665 | *al = SSL_AD_INTERNAL_ERROR; | |
05ec6a25 | 2666 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, SSL_R_LIBRARY_BUG); |
e00e0b3d MC |
2667 | goto err; |
2668 | } | |
2669 | /* Make GOST keytransport blob message */ | |
2670 | /* | |
2671 | * Encapsulate it into sequence | |
2672 | */ | |
e00e0b3d MC |
2673 | msglen = 255; |
2674 | if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) { | |
2675 | *al = SSL_AD_INTERNAL_ERROR; | |
05ec6a25 | 2676 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, SSL_R_LIBRARY_BUG); |
e00e0b3d MC |
2677 | goto err; |
2678 | } | |
f1ec23c0 | 2679 | |
08029dfa MC |
2680 | if (!WPACKET_put_bytes_u8(pkt, V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED) |
2681 | || (msglen >= 0x80 && !WPACKET_put_bytes_u8(pkt, 0x81)) | |
b2b3024e | 2682 | || !WPACKET_sub_memcpy_u8(pkt, tmp, msglen)) { |
f1ec23c0 MC |
2683 | *al = SSL_AD_INTERNAL_ERROR; |
2684 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR); | |
2685 | goto err; | |
e00e0b3d | 2686 | } |
f1ec23c0 | 2687 | |
e00e0b3d MC |
2688 | EVP_PKEY_CTX_free(pkey_ctx); |
2689 | s->s3->tmp.pms = pms; | |
2690 | s->s3->tmp.pmslen = pmslen; | |
2691 | ||
2692 | return 1; | |
2693 | err: | |
2694 | EVP_PKEY_CTX_free(pkey_ctx); | |
2695 | OPENSSL_clear_free(pms, pmslen); | |
2696 | EVP_MD_CTX_free(ukm_hash); | |
2697 | return 0; | |
2698 | #else | |
05ec6a25 | 2699 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR); |
e00e0b3d MC |
2700 | *al = SSL_AD_INTERNAL_ERROR; |
2701 | return 0; | |
2702 | #endif | |
2703 | } | |
2704 | ||
f1ec23c0 | 2705 | static int tls_construct_cke_srp(SSL *s, WPACKET *pkt, int *al) |
840a2bf8 | 2706 | { |
8b9546c7 | 2707 | #ifndef OPENSSL_NO_SRP |
f1ec23c0 MC |
2708 | unsigned char *abytes = NULL; |
2709 | ||
2710 | if (s->srp_ctx.A == NULL | |
b2b3024e MC |
2711 | || !WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(s->srp_ctx.A), |
2712 | &abytes)) { | |
05ec6a25 | 2713 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_INTERNAL_ERROR); |
840a2bf8 MC |
2714 | return 0; |
2715 | } | |
f1ec23c0 MC |
2716 | BN_bn2bin(s->srp_ctx.A, abytes); |
2717 | ||
840a2bf8 MC |
2718 | OPENSSL_free(s->session->srp_username); |
2719 | s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login); | |
2720 | if (s->session->srp_username == NULL) { | |
05ec6a25 | 2721 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_MALLOC_FAILURE); |
840a2bf8 MC |
2722 | return 0; |
2723 | } | |
2724 | ||
2725 | return 1; | |
2726 | #else | |
05ec6a25 | 2727 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_INTERNAL_ERROR); |
840a2bf8 MC |
2728 | *al = SSL_AD_INTERNAL_ERROR; |
2729 | return 0; | |
2730 | #endif | |
2731 | } | |
2732 | ||
7cea05dc | 2733 | int tls_construct_client_key_exchange(SSL *s, WPACKET *pkt) |
13c0ec4a | 2734 | { |
13c0ec4a MC |
2735 | unsigned long alg_k; |
2736 | int al = -1; | |
2737 | ||
f1ec23c0 | 2738 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
13c0ec4a | 2739 | |
13c0ec4a | 2740 | if ((alg_k & SSL_PSK) |
7cea05dc | 2741 | && !tls_construct_cke_psk_preamble(s, pkt, &al)) |
13c0ec4a MC |
2742 | goto err; |
2743 | ||
f1ec23c0 | 2744 | if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) { |
7cea05dc | 2745 | if (!tls_construct_cke_rsa(s, pkt, &al)) |
13c0ec4a | 2746 | goto err; |
a8c1c704 | 2747 | } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) { |
7cea05dc | 2748 | if (!tls_construct_cke_dhe(s, pkt, &al)) |
b9908bf9 | 2749 | goto err; |
67ad5aab | 2750 | } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) { |
7cea05dc | 2751 | if (!tls_construct_cke_ecdhe(s, pkt, &al)) |
ce0c1f2b | 2752 | goto err; |
e00e0b3d | 2753 | } else if (alg_k & SSL_kGOST) { |
7cea05dc | 2754 | if (!tls_construct_cke_gost(s, pkt, &al)) |
a71edf3b | 2755 | goto err; |
840a2bf8 | 2756 | } else if (alg_k & SSL_kSRP) { |
7cea05dc | 2757 | if (!tls_construct_cke_srp(s, pkt, &al)) |
69f68237 | 2758 | goto err; |
4a424545 | 2759 | } else if (!(alg_k & SSL_kPSK)) { |
b9908bf9 MC |
2760 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
2761 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); | |
2762 | goto err; | |
2763 | } | |
2764 | ||
b9908bf9 | 2765 | return 1; |
0f113f3e | 2766 | err: |
13c0ec4a MC |
2767 | if (al != -1) |
2768 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
0bce0b02 | 2769 | OPENSSL_clear_free(s->s3->tmp.pms, s->s3->tmp.pmslen); |
76106e60 | 2770 | s->s3->tmp.pms = NULL; |
7689082b DSH |
2771 | #ifndef OPENSSL_NO_PSK |
2772 | OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen); | |
2773 | s->s3->tmp.psk = NULL; | |
0f113f3e | 2774 | #endif |
b9908bf9 MC |
2775 | return 0; |
2776 | } | |
2777 | ||
2778 | int tls_client_key_exchange_post_work(SSL *s) | |
2779 | { | |
2780 | unsigned char *pms = NULL; | |
2781 | size_t pmslen = 0; | |
2782 | ||
6f137370 MC |
2783 | pms = s->s3->tmp.pms; |
2784 | pmslen = s->s3->tmp.pmslen; | |
2785 | ||
b9908bf9 MC |
2786 | #ifndef OPENSSL_NO_SRP |
2787 | /* Check for SRP */ | |
2788 | if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) { | |
2789 | if (!srp_generate_client_master_secret(s)) { | |
2790 | SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, | |
2791 | ERR_R_INTERNAL_ERROR); | |
2792 | goto err; | |
2793 | } | |
2794 | return 1; | |
2795 | } | |
2796 | #endif | |
b9908bf9 MC |
2797 | |
2798 | if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) { | |
2799 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
2800 | SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE); | |
2801 | goto err; | |
2802 | } | |
2803 | if (!ssl_generate_master_secret(s, pms, pmslen, 1)) { | |
2804 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
2805 | SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR); | |
6f137370 MC |
2806 | /* ssl_generate_master_secret frees the pms even on error */ |
2807 | pms = NULL; | |
2808 | pmslen = 0; | |
b9908bf9 MC |
2809 | goto err; |
2810 | } | |
6f137370 MC |
2811 | pms = NULL; |
2812 | pmslen = 0; | |
473483d4 MC |
2813 | |
2814 | #ifndef OPENSSL_NO_SCTP | |
2815 | if (SSL_IS_DTLS(s)) { | |
2816 | unsigned char sctpauthkey[64]; | |
2817 | char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)]; | |
2818 | ||
2819 | /* | |
2820 | * Add new shared key for SCTP-Auth, will be ignored if no SCTP | |
2821 | * used. | |
2822 | */ | |
141eb8c6 MC |
2823 | memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL, |
2824 | sizeof(DTLS1_SCTP_AUTH_LABEL)); | |
473483d4 MC |
2825 | |
2826 | if (SSL_export_keying_material(s, sctpauthkey, | |
a230b26e EK |
2827 | sizeof(sctpauthkey), labelbuffer, |
2828 | sizeof(labelbuffer), NULL, 0, 0) <= 0) | |
473483d4 MC |
2829 | goto err; |
2830 | ||
2831 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY, | |
2832 | sizeof(sctpauthkey), sctpauthkey); | |
2833 | } | |
2834 | #endif | |
2835 | ||
b9908bf9 MC |
2836 | return 1; |
2837 | err: | |
2838 | OPENSSL_clear_free(pms, pmslen); | |
2839 | s->s3->tmp.pms = NULL; | |
2840 | return 0; | |
0f113f3e | 2841 | } |
d02b48c6 | 2842 | |
7cea05dc | 2843 | int tls_construct_client_verify(SSL *s, WPACKET *pkt) |
0f113f3e | 2844 | { |
0f113f3e | 2845 | EVP_PKEY *pkey; |
a0f63828 | 2846 | const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys]; |
5a008ff6 | 2847 | EVP_MD_CTX *mctx = NULL; |
0f113f3e | 2848 | unsigned u = 0; |
a0f63828 DSH |
2849 | long hdatalen = 0; |
2850 | void *hdata; | |
6400f338 | 2851 | unsigned char *sig = NULL; |
6400f338 | 2852 | |
bfb0641f | 2853 | mctx = EVP_MD_CTX_new(); |
6e59a892 RL |
2854 | if (mctx == NULL) { |
2855 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_MALLOC_FAILURE); | |
2856 | goto err; | |
2857 | } | |
b9908bf9 | 2858 | pkey = s->cert->key->privatekey; |
a0f63828 DSH |
2859 | |
2860 | hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata); | |
2861 | if (hdatalen <= 0) { | |
5f3d93e4 MC |
2862 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); |
2863 | goto err; | |
2864 | } | |
0f1e51ea | 2865 | |
7cea05dc | 2866 | if (SSL_USE_SIGALGS(s)&& !tls12_get_sigandhash(pkt, pkey, md)) { |
6400f338 MC |
2867 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); |
2868 | goto err; | |
a0f63828 | 2869 | } |
855a54a9 | 2870 | #ifdef SSL_DEBUG |
a0f63828 | 2871 | fprintf(stderr, "Using client alg %s\n", EVP_MD_name(md)); |
b9908bf9 | 2872 | #endif |
6400f338 MC |
2873 | sig = OPENSSL_malloc(EVP_PKEY_size(pkey)); |
2874 | if (sig == NULL) { | |
2875 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_MALLOC_FAILURE); | |
2876 | goto err; | |
2877 | } | |
6e59a892 RL |
2878 | if (!EVP_SignInit_ex(mctx, md, NULL) |
2879 | || !EVP_SignUpdate(mctx, hdata, hdatalen) | |
a0f63828 | 2880 | || (s->version == SSL3_VERSION |
6e59a892 | 2881 | && !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET, |
348240c6 | 2882 | (int)s->session->master_key_length, |
a0f63828 | 2883 | s->session->master_key)) |
6400f338 | 2884 | || !EVP_SignFinal(mctx, sig, &u, pkey)) { |
a0f63828 DSH |
2885 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB); |
2886 | goto err; | |
2887 | } | |
2a9b9654 | 2888 | #ifndef OPENSSL_NO_GOST |
3aeb9348 DSH |
2889 | { |
2890 | int pktype = EVP_PKEY_id(pkey); | |
2891 | if (pktype == NID_id_GostR3410_2001 | |
2892 | || pktype == NID_id_GostR3410_2012_256 | |
2893 | || pktype == NID_id_GostR3410_2012_512) | |
6400f338 | 2894 | BUF_reverse(sig, NULL, u); |
b9908bf9 | 2895 | } |
2a9b9654 | 2896 | #endif |
a0f63828 | 2897 | |
7cea05dc | 2898 | if (!WPACKET_sub_memcpy_u16(pkt, sig, u)) { |
6400f338 MC |
2899 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); |
2900 | goto err; | |
2901 | } | |
2902 | ||
a0f63828 DSH |
2903 | /* Digest cached records and discard handshake buffer */ |
2904 | if (!ssl3_digest_cached_records(s, 0)) | |
2905 | goto err; | |
6400f338 | 2906 | |
6400f338 | 2907 | OPENSSL_free(sig); |
bfb0641f | 2908 | EVP_MD_CTX_free(mctx); |
b9908bf9 | 2909 | return 1; |
0f113f3e | 2910 | err: |
6400f338 | 2911 | OPENSSL_free(sig); |
bfb0641f | 2912 | EVP_MD_CTX_free(mctx); |
6400f338 | 2913 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
b9908bf9 | 2914 | return 0; |
0f113f3e MC |
2915 | } |
2916 | ||
2917 | /* | |
2918 | * Check a certificate can be used for client authentication. Currently check | |
2919 | * cert exists, if we have a suitable digest for TLS 1.2 if static DH client | |
2920 | * certificates can be used and optionally checks suitability for Suite B. | |
0d609395 DSH |
2921 | */ |
2922 | static int ssl3_check_client_certificate(SSL *s) | |
0f113f3e | 2923 | { |
0f113f3e MC |
2924 | if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey) |
2925 | return 0; | |
2926 | /* If no suitable signature algorithm can't use certificate */ | |
d376e57d | 2927 | if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys]) |
0f113f3e MC |
2928 | return 0; |
2929 | /* | |
2930 | * If strict mode check suitability of chain before using it. This also | |
2931 | * adjusts suite B digest if necessary. | |
2932 | */ | |
2933 | if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT && | |
2934 | !tls1_check_chain(s, NULL, NULL, NULL, -2)) | |
2935 | return 0; | |
0f113f3e MC |
2936 | return 1; |
2937 | } | |
0d609395 | 2938 | |
be3583fa | 2939 | WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst) |
0f113f3e MC |
2940 | { |
2941 | X509 *x509 = NULL; | |
2942 | EVP_PKEY *pkey = NULL; | |
2943 | int i; | |
2944 | ||
b9908bf9 | 2945 | if (wst == WORK_MORE_A) { |
0f113f3e MC |
2946 | /* Let cert callback update client certificates if required */ |
2947 | if (s->cert->cert_cb) { | |
2948 | i = s->cert->cert_cb(s, s->cert->cert_cb_arg); | |
2949 | if (i < 0) { | |
2950 | s->rwstate = SSL_X509_LOOKUP; | |
b9908bf9 | 2951 | return WORK_MORE_A; |
0f113f3e MC |
2952 | } |
2953 | if (i == 0) { | |
2954 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
fe3a3291 | 2955 | ossl_statem_set_error(s); |
0f113f3e MC |
2956 | return 0; |
2957 | } | |
2958 | s->rwstate = SSL_NOTHING; | |
2959 | } | |
2960 | if (ssl3_check_client_certificate(s)) | |
b9908bf9 MC |
2961 | return WORK_FINISHED_CONTINUE; |
2962 | ||
2963 | /* Fall through to WORK_MORE_B */ | |
2964 | wst = WORK_MORE_B; | |
0f113f3e MC |
2965 | } |
2966 | ||
2967 | /* We need to get a client cert */ | |
b9908bf9 | 2968 | if (wst == WORK_MORE_B) { |
0f113f3e MC |
2969 | /* |
2970 | * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP; | |
2971 | * return(-1); We then get retied later | |
2972 | */ | |
0f113f3e MC |
2973 | i = ssl_do_client_cert_cb(s, &x509, &pkey); |
2974 | if (i < 0) { | |
2975 | s->rwstate = SSL_X509_LOOKUP; | |
b9908bf9 | 2976 | return WORK_MORE_B; |
0f113f3e MC |
2977 | } |
2978 | s->rwstate = SSL_NOTHING; | |
2979 | if ((i == 1) && (pkey != NULL) && (x509 != NULL)) { | |
0f113f3e MC |
2980 | if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) |
2981 | i = 0; | |
2982 | } else if (i == 1) { | |
2983 | i = 0; | |
b9908bf9 | 2984 | SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE, |
0f113f3e MC |
2985 | SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); |
2986 | } | |
2987 | ||
222561fe | 2988 | X509_free(x509); |
25aaa98a | 2989 | EVP_PKEY_free(pkey); |
0f113f3e MC |
2990 | if (i && !ssl3_check_client_certificate(s)) |
2991 | i = 0; | |
2992 | if (i == 0) { | |
2993 | if (s->version == SSL3_VERSION) { | |
2994 | s->s3->tmp.cert_req = 0; | |
2995 | ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE); | |
b9908bf9 | 2996 | return WORK_FINISHED_CONTINUE; |
0f113f3e MC |
2997 | } else { |
2998 | s->s3->tmp.cert_req = 2; | |
124037fd | 2999 | if (!ssl3_digest_cached_records(s, 0)) { |
dab18ab5 | 3000 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
fe3a3291 | 3001 | ossl_statem_set_error(s); |
dab18ab5 DSH |
3002 | return 0; |
3003 | } | |
0f113f3e MC |
3004 | } |
3005 | } | |
3006 | ||
b9908bf9 | 3007 | return WORK_FINISHED_CONTINUE; |
0f113f3e MC |
3008 | } |
3009 | ||
b9908bf9 MC |
3010 | /* Shouldn't ever get here */ |
3011 | return WORK_ERROR; | |
3012 | } | |
3013 | ||
7cea05dc | 3014 | int tls_construct_client_certificate(SSL *s, WPACKET *pkt) |
b9908bf9 | 3015 | { |
0baed5e9 | 3016 | int al = SSL_AD_INTERNAL_ERROR; |
e96e0f8e MC |
3017 | |
3018 | /* | |
3019 | * TODO(TLS1.3): For now we must put an empty context. Needs to be filled in | |
3020 | * later | |
3021 | */ | |
3022 | if ((SSL_IS_TLS13(s) && !WPACKET_put_bytes_u8(pkt, 0)) | |
3023 | || !ssl3_output_cert_chain(s, pkt, | |
b90506e9 | 3024 | (s->s3->tmp.cert_req == 2) ? NULL |
e96e0f8e MC |
3025 | : s->cert->key, |
3026 | &al)) { | |
b9908bf9 | 3027 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR); |
e96e0f8e | 3028 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
b9908bf9 | 3029 | return 0; |
0f113f3e | 3030 | } |
b9908bf9 MC |
3031 | |
3032 | return 1; | |
0f113f3e MC |
3033 | } |
3034 | ||
3035 | #define has_bits(i,m) (((i)&(m)) == (m)) | |
d02b48c6 | 3036 | |
36d16f8e | 3037 | int ssl3_check_cert_and_algorithm(SSL *s) |
0f113f3e | 3038 | { |
60f43e9e RL |
3039 | int i; |
3040 | #ifndef OPENSSL_NO_EC | |
3041 | int idx; | |
3042 | #endif | |
0f113f3e MC |
3043 | long alg_k, alg_a; |
3044 | EVP_PKEY *pkey = NULL; | |
26c79d56 | 3045 | int al = SSL_AD_HANDSHAKE_FAILURE; |
d02b48c6 | 3046 | |
0f113f3e MC |
3047 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
3048 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; | |
d02b48c6 | 3049 | |
0f113f3e | 3050 | /* we don't have a certificate */ |
55a9a16f | 3051 | if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK)) |
0f113f3e | 3052 | return (1); |
d02b48c6 | 3053 | |
0f113f3e | 3054 | /* This is the passed certificate */ |
d02b48c6 | 3055 | |
10bf4fc2 | 3056 | #ifndef OPENSSL_NO_EC |
60f43e9e | 3057 | idx = s->session->peer_type; |
0f113f3e | 3058 | if (idx == SSL_PKEY_ECC) { |
a273c6ee | 3059 | if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) { |
0f113f3e MC |
3060 | /* check failed */ |
3061 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT); | |
3062 | goto f_err; | |
3063 | } else { | |
3064 | return 1; | |
3065 | } | |
3066 | } else if (alg_a & SSL_aECDSA) { | |
3067 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3068 | SSL_R_MISSING_ECDSA_SIGNING_CERT); | |
3069 | goto f_err; | |
0f113f3e MC |
3070 | } |
3071 | #endif | |
8382fd3a | 3072 | pkey = X509_get0_pubkey(s->session->peer); |
a273c6ee | 3073 | i = X509_certificate_type(s->session->peer, pkey); |
0f113f3e MC |
3074 | |
3075 | /* Check that we have a certificate if we require one */ | |
3076 | if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) { | |
3077 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3078 | SSL_R_MISSING_RSA_SIGNING_CERT); | |
3079 | goto f_err; | |
3080 | } | |
bc36ee62 | 3081 | #ifndef OPENSSL_NO_DSA |
0f113f3e MC |
3082 | else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) { |
3083 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3084 | SSL_R_MISSING_DSA_SIGNING_CERT); | |
3085 | goto f_err; | |
3086 | } | |
d02b48c6 | 3087 | #endif |
bc36ee62 | 3088 | #ifndef OPENSSL_NO_RSA |
361a1191 KR |
3089 | if (alg_k & (SSL_kRSA | SSL_kRSAPSK) && |
3090 | !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) { | |
3091 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3092 | SSL_R_MISSING_RSA_ENCRYPTING_CERT); | |
3093 | goto f_err; | |
0f113f3e | 3094 | } |
79df9d62 | 3095 | #endif |
bc36ee62 | 3096 | #ifndef OPENSSL_NO_DH |
fb79abe3 | 3097 | if ((alg_k & SSL_kDHE) && (s->s3->peer_tmp == NULL)) { |
26c79d56 KR |
3098 | al = SSL_AD_INTERNAL_ERROR; |
3099 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR); | |
0f113f3e | 3100 | goto f_err; |
0f113f3e | 3101 | } |
d02b48c6 RE |
3102 | #endif |
3103 | ||
0f113f3e MC |
3104 | return (1); |
3105 | f_err: | |
26c79d56 | 3106 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
0f113f3e MC |
3107 | return (0); |
3108 | } | |
3109 | ||
e481f9b9 | 3110 | #ifndef OPENSSL_NO_NEXTPROTONEG |
7cea05dc | 3111 | int tls_construct_next_proto(SSL *s, WPACKET *pkt) |
b9908bf9 | 3112 | { |
15e6be6c MC |
3113 | size_t len, padding_len; |
3114 | unsigned char *padding = NULL; | |
15e6be6c | 3115 | |
aff8c126 | 3116 | len = s->ext.npn_len; |
b9908bf9 | 3117 | padding_len = 32 - ((len + 2) % 32); |
15e6be6c | 3118 | |
aff8c126 | 3119 | if (!WPACKET_sub_memcpy_u8(pkt, s->ext.npn, len) |
7cea05dc | 3120 | || !WPACKET_sub_allocate_bytes_u8(pkt, padding_len, &padding)) { |
15e6be6c MC |
3121 | SSLerr(SSL_F_TLS_CONSTRUCT_NEXT_PROTO, ERR_R_INTERNAL_ERROR); |
3122 | goto err; | |
3123 | } | |
3124 | ||
3125 | memset(padding, 0, padding_len); | |
3126 | ||
b9908bf9 | 3127 | return 1; |
15e6be6c | 3128 | err: |
15e6be6c MC |
3129 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
3130 | return 0; | |
b9908bf9 | 3131 | } |
6434abbf | 3132 | #endif |
368888bc | 3133 | |
e46f2334 MC |
3134 | static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL *s, PACKET *pkt) |
3135 | { | |
3136 | int al = SSL_AD_INTERNAL_ERROR; | |
3137 | PACKET extensions; | |
3434f40b | 3138 | RAW_EXTENSION *rawexts = NULL; |
e46f2334 | 3139 | |
e46f2334 MC |
3140 | if (!PACKET_as_length_prefixed_2(pkt, &extensions)) { |
3141 | al = SSL_AD_DECODE_ERROR; | |
3142 | SSLerr(SSL_F_TLS_PROCESS_ENCRYPTED_EXTENSIONS, SSL_R_LENGTH_MISMATCH); | |
3143 | goto err; | |
3144 | } | |
3145 | ||
e96e0f8e | 3146 | if (!tls_collect_extensions(s, &extensions, EXT_TLS1_3_ENCRYPTED_EXTENSIONS, |
1266eefd | 3147 | &rawexts, &al) |
e96e0f8e | 3148 | || !tls_parse_all_extensions(s, EXT_TLS1_3_ENCRYPTED_EXTENSIONS, |
f97d4c37 | 3149 | rawexts, NULL, 0, &al)) |
3434f40b MC |
3150 | goto err; |
3151 | ||
1b0286a3 | 3152 | OPENSSL_free(rawexts); |
e46f2334 MC |
3153 | return MSG_PROCESS_CONTINUE_READING; |
3154 | ||
3155 | err: | |
3156 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
3157 | ossl_statem_set_error(s); | |
1b0286a3 | 3158 | OPENSSL_free(rawexts); |
e46f2334 MC |
3159 | return MSG_PROCESS_ERROR; |
3160 | } | |
3161 | ||
368888bc | 3162 | int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) |
0f113f3e MC |
3163 | { |
3164 | int i = 0; | |
368888bc | 3165 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
3166 | if (s->ctx->client_cert_engine) { |
3167 | i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s, | |
3168 | SSL_get_client_CA_list(s), | |
3169 | px509, ppkey, NULL, NULL, NULL); | |
3170 | if (i != 0) | |
3171 | return i; | |
3172 | } | |
3173 | #endif | |
3174 | if (s->ctx->client_cert_cb) | |
3175 | i = s->ctx->client_cert_cb(s, px509, ppkey); | |
3176 | return i; | |
3177 | } | |
d45ba43d | 3178 | |
ae2f7b37 | 3179 | int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, WPACKET *pkt) |
d45ba43d | 3180 | { |
2c7b4dbc MC |
3181 | int i; |
3182 | size_t totlen = 0, len, maxlen; | |
d45ba43d MC |
3183 | int empty_reneg_info_scsv = !s->renegotiate; |
3184 | /* Set disabled masks for this session */ | |
3185 | ssl_set_client_disabled(s); | |
3186 | ||
3187 | if (sk == NULL) | |
3188 | return (0); | |
d45ba43d | 3189 | |
2c7b4dbc MC |
3190 | #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH |
3191 | # if OPENSSL_MAX_TLS1_2_CIPHER_LENGTH < 6 | |
3192 | # error Max cipher length too short | |
3193 | # endif | |
3194 | /* | |
3195 | * Some servers hang if client hello > 256 bytes as hack workaround | |
3196 | * chop number of supported ciphers to keep it well below this if we | |
3197 | * use TLS v1.2 | |
3198 | */ | |
3199 | if (TLS1_get_version(s) >= TLS1_2_VERSION) | |
3200 | maxlen = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1; | |
3201 | else | |
3202 | #endif | |
3203 | /* Maximum length that can be stored in 2 bytes. Length must be even */ | |
3204 | maxlen = 0xfffe; | |
3205 | ||
3206 | if (empty_reneg_info_scsv) | |
3207 | maxlen -= 2; | |
3208 | if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) | |
3209 | maxlen -= 2; | |
3210 | ||
3211 | for (i = 0; i < sk_SSL_CIPHER_num(sk) && totlen < maxlen; i++) { | |
3212 | const SSL_CIPHER *c; | |
3213 | ||
d45ba43d MC |
3214 | c = sk_SSL_CIPHER_value(sk, i); |
3215 | /* Skip disabled ciphers */ | |
3216 | if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED)) | |
3217 | continue; | |
2c7b4dbc MC |
3218 | |
3219 | if (!s->method->put_cipher_by_char(c, pkt, &len)) { | |
3220 | SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR); | |
3221 | return 0; | |
3222 | } | |
3223 | ||
3224 | totlen += len; | |
d45ba43d | 3225 | } |
2c7b4dbc MC |
3226 | |
3227 | if (totlen == 0) { | |
3228 | SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, SSL_R_NO_CIPHERS_AVAILABLE); | |
3229 | return 0; | |
3230 | } | |
3231 | ||
3232 | if (totlen != 0) { | |
d45ba43d MC |
3233 | if (empty_reneg_info_scsv) { |
3234 | static SSL_CIPHER scsv = { | |
3235 | 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
3236 | }; | |
2c7b4dbc MC |
3237 | if (!s->method->put_cipher_by_char(&scsv, pkt, &len)) { |
3238 | SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR); | |
3239 | return 0; | |
3240 | } | |
d45ba43d MC |
3241 | } |
3242 | if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) { | |
3243 | static SSL_CIPHER scsv = { | |
3244 | 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
3245 | }; | |
2c7b4dbc MC |
3246 | if (!s->method->put_cipher_by_char(&scsv, pkt, &len)) { |
3247 | SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR); | |
3248 | return 0; | |
3249 | } | |
d45ba43d MC |
3250 | } |
3251 | } | |
3252 | ||
2c7b4dbc | 3253 | return 1; |
d45ba43d | 3254 | } |