]>
Commit | Line | Data |
---|---|---|
8ba708e5 | 1 | /* ssl/statem/statem_clnt.c */ |
58964a49 | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
d02b48c6 RE |
3 | * All rights reserved. |
4 | * | |
5 | * This package is an SSL implementation written | |
6 | * by Eric Young (eay@cryptsoft.com). | |
7 | * The implementation was written so as to conform with Netscapes SSL. | |
0f113f3e | 8 | * |
d02b48c6 RE |
9 | * This library is free for commercial and non-commercial use as long as |
10 | * the following conditions are aheared to. The following conditions | |
11 | * apply to all code found in this distribution, be it the RC4, RSA, | |
12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | |
13 | * included with this distribution is covered by the same copyright terms | |
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | |
0f113f3e | 15 | * |
d02b48c6 RE |
16 | * Copyright remains Eric Young's, and as such any Copyright notices in |
17 | * the code are not to be removed. | |
18 | * If this package is used in a product, Eric Young should be given attribution | |
19 | * as the author of the parts of the library used. | |
20 | * This can be in the form of a textual message at program startup or | |
21 | * in documentation (online or textual) provided with the package. | |
0f113f3e | 22 | * |
d02b48c6 RE |
23 | * Redistribution and use in source and binary forms, with or without |
24 | * modification, are permitted provided that the following conditions | |
25 | * are met: | |
26 | * 1. Redistributions of source code must retain the copyright | |
27 | * notice, this list of conditions and the following disclaimer. | |
28 | * 2. Redistributions in binary form must reproduce the above copyright | |
29 | * notice, this list of conditions and the following disclaimer in the | |
30 | * documentation and/or other materials provided with the distribution. | |
31 | * 3. All advertising materials mentioning features or use of this software | |
32 | * must display the following acknowledgement: | |
33 | * "This product includes cryptographic software written by | |
34 | * Eric Young (eay@cryptsoft.com)" | |
35 | * The word 'cryptographic' can be left out if the rouines from the library | |
36 | * being used are not cryptographic related :-). | |
0f113f3e | 37 | * 4. If you include any Windows specific code (or a derivative thereof) from |
d02b48c6 RE |
38 | * the apps directory (application code) you must include an acknowledgement: |
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
0f113f3e | 40 | * |
d02b48c6 RE |
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
51 | * SUCH DAMAGE. | |
0f113f3e | 52 | * |
d02b48c6 RE |
53 | * The licence and distribution terms for any publically available version or |
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | |
55 | * copied and put under another distribution licence | |
56 | * [including the GNU Public Licence.] | |
57 | */ | |
8c74b5e5 | 58 | /* ==================================================================== |
52b8dad8 | 59 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
8c74b5e5 BM |
60 | * |
61 | * Redistribution and use in source and binary forms, with or without | |
62 | * modification, are permitted provided that the following conditions | |
63 | * are met: | |
64 | * | |
65 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 66 | * notice, this list of conditions and the following disclaimer. |
8c74b5e5 BM |
67 | * |
68 | * 2. Redistributions in binary form must reproduce the above copyright | |
69 | * notice, this list of conditions and the following disclaimer in | |
70 | * the documentation and/or other materials provided with the | |
71 | * distribution. | |
72 | * | |
73 | * 3. All advertising materials mentioning features or use of this | |
74 | * software must display the following acknowledgment: | |
75 | * "This product includes software developed by the OpenSSL Project | |
76 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | |
77 | * | |
78 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
79 | * endorse or promote products derived from this software without | |
80 | * prior written permission. For written permission, please contact | |
81 | * openssl-core@openssl.org. | |
82 | * | |
83 | * 5. Products derived from this software may not be called "OpenSSL" | |
84 | * nor may "OpenSSL" appear in their names without prior written | |
85 | * permission of the OpenSSL Project. | |
86 | * | |
87 | * 6. Redistributions of any form whatsoever must retain the following | |
88 | * acknowledgment: | |
89 | * "This product includes software developed by the OpenSSL Project | |
90 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | |
91 | * | |
92 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
93 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
94 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
95 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
96 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
97 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
98 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
99 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
100 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
101 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
102 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
103 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
104 | * ==================================================================== | |
105 | * | |
106 | * This product includes cryptographic software written by Eric Young | |
107 | * (eay@cryptsoft.com). This product includes software written by Tim | |
108 | * Hudson (tjh@cryptsoft.com). | |
109 | * | |
110 | */ | |
ea262260 BM |
111 | /* ==================================================================== |
112 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | |
113 | * | |
0f113f3e | 114 | * Portions of the attached software ("Contribution") are developed by |
ea262260 BM |
115 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
116 | * | |
117 | * The Contribution is licensed pursuant to the OpenSSL open source | |
118 | * license provided above. | |
119 | * | |
ea262260 BM |
120 | * ECC cipher suite support in OpenSSL originally written by |
121 | * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. | |
122 | * | |
123 | */ | |
ddac1974 NL |
124 | /* ==================================================================== |
125 | * Copyright 2005 Nokia. All rights reserved. | |
126 | * | |
127 | * The portions of the attached software ("Contribution") is developed by | |
128 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source | |
129 | * license. | |
130 | * | |
131 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of | |
132 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites | |
133 | * support (see RFC 4279) to OpenSSL. | |
134 | * | |
135 | * No patent licenses or other rights except those expressly stated in | |
136 | * the OpenSSL open source license shall be deemed granted or received | |
137 | * expressly, by implication, estoppel, or otherwise. | |
138 | * | |
139 | * No assurances are provided by Nokia that the Contribution does not | |
140 | * infringe the patent or other intellectual property rights of any third | |
141 | * party or that the license provides you with all the necessary rights | |
142 | * to make use of the Contribution. | |
143 | * | |
144 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN | |
145 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA | |
146 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY | |
147 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR | |
148 | * OTHERWISE. | |
149 | */ | |
d02b48c6 RE |
150 | |
151 | #include <stdio.h> | |
8ba708e5 | 152 | #include "../ssl_locl.h" |
61ae935a | 153 | #include "statem_locl.h" |
ec577822 BM |
154 | #include <openssl/buffer.h> |
155 | #include <openssl/rand.h> | |
156 | #include <openssl/objects.h> | |
157 | #include <openssl/evp.h> | |
dbad1690 | 158 | #include <openssl/md5.h> |
3eeaab4b | 159 | #ifndef OPENSSL_NO_DH |
0f113f3e | 160 | # include <openssl/dh.h> |
3eeaab4b | 161 | #endif |
d095b68d | 162 | #include <openssl/bn.h> |
368888bc | 163 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e | 164 | # include <openssl/engine.h> |
368888bc | 165 | #endif |
f9b3bff6 | 166 | |
61ae935a | 167 | static inline int cert_req_allowed(SSL *s); |
a455d0f6 | 168 | static int key_exchange_expected(SSL *s); |
d45ba43d | 169 | static int ssl_set_version(SSL *s); |
0f113f3e | 170 | static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b); |
d45ba43d | 171 | static int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, |
e9fa092e | 172 | unsigned char *p); |
ea262260 | 173 | |
61ae935a MC |
174 | |
175 | /* | |
176 | * Is a CertificateRequest message allowed at the moment or not? | |
177 | * | |
178 | * Return values are: | |
179 | * 1: Yes | |
180 | * 0: No | |
181 | */ | |
182 | static inline int cert_req_allowed(SSL *s) | |
183 | { | |
184 | /* TLS does not like anon-DH with client cert */ | |
b7fa1f98 MC |
185 | if ((s->version > SSL3_VERSION |
186 | && (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)) | |
187 | || (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aSRP | SSL_aPSK))) | |
61ae935a MC |
188 | return 0; |
189 | ||
190 | return 1; | |
191 | } | |
192 | ||
193 | /* | |
a455d0f6 | 194 | * Should we expect the ServerKeyExchange message or not? |
61ae935a MC |
195 | * |
196 | * Return values are: | |
197 | * 1: Yes | |
198 | * 0: No | |
a455d0f6 | 199 | * -1: Error |
61ae935a | 200 | */ |
a455d0f6 | 201 | static int key_exchange_expected(SSL *s) |
61ae935a MC |
202 | { |
203 | long alg_k = s->s3->tmp.new_cipher->algorithm_mkey; | |
204 | ||
205 | /* | |
206 | * Can't skip server key exchange if this is an ephemeral | |
a455d0f6 | 207 | * ciphersuite or for SRP |
61ae935a | 208 | */ |
a455d0f6 MC |
209 | if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK |
210 | | SSL_kSRP)) { | |
211 | return 1; | |
61ae935a MC |
212 | } |
213 | ||
a455d0f6 MC |
214 | /* |
215 | * Export ciphersuites may have temporary RSA keys if the public key in the | |
216 | * server certificate is longer than the maximum export strength | |
217 | */ | |
218 | if ((alg_k & SSL_kRSA) && SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) { | |
219 | EVP_PKEY *pkey; | |
220 | ||
221 | pkey = X509_get_pubkey(s->session->peer); | |
222 | if (pkey == NULL) | |
223 | return -1; | |
224 | ||
225 | /* | |
226 | * If the public key in the certificate is shorter than or equal to the | |
227 | * maximum export strength then a temporary RSA key is not allowed | |
228 | */ | |
229 | if (EVP_PKEY_bits(pkey) | |
230 | <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) | |
231 | return 0; | |
232 | ||
233 | EVP_PKEY_free(pkey); | |
234 | ||
235 | return 1; | |
236 | } | |
237 | ||
238 | return 0; | |
61ae935a MC |
239 | } |
240 | ||
241 | /* | |
8481f583 MC |
242 | * ossl_statem_client_read_transition() encapsulates the logic for the allowed |
243 | * handshake state transitions when the client is reading messages from the | |
244 | * server. The message type that the server has sent is provided in |mt|. The | |
245 | * current state is in |s->statem.hand_state|. | |
61ae935a MC |
246 | * |
247 | * Return values are: | |
248 | * 1: Success (transition allowed) | |
249 | * 0: Error (transition not allowed) | |
250 | */ | |
8481f583 | 251 | int ossl_statem_client_read_transition(SSL *s, int mt) |
61ae935a | 252 | { |
d6f1a6e9 | 253 | OSSL_STATEM *st = &s->statem; |
a455d0f6 | 254 | int ske_expected; |
61ae935a MC |
255 | |
256 | switch(st->hand_state) { | |
257 | case TLS_ST_CW_CLNT_HELLO: | |
258 | if (mt == SSL3_MT_SERVER_HELLO) { | |
259 | st->hand_state = TLS_ST_CR_SRVR_HELLO; | |
260 | return 1; | |
261 | } | |
262 | ||
263 | if (SSL_IS_DTLS(s)) { | |
264 | if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) { | |
265 | st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST; | |
266 | return 1; | |
267 | } | |
268 | } | |
269 | break; | |
270 | ||
271 | case TLS_ST_CR_SRVR_HELLO: | |
272 | if (s->hit) { | |
273 | if (s->tlsext_ticket_expected) { | |
274 | if (mt == SSL3_MT_NEWSESSION_TICKET) { | |
275 | st->hand_state = TLS_ST_CR_SESSION_TICKET; | |
276 | return 1; | |
277 | } | |
278 | } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { | |
279 | st->hand_state = TLS_ST_CR_CHANGE; | |
280 | return 1; | |
281 | } | |
282 | } else { | |
283 | if (SSL_IS_DTLS(s) && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) { | |
284 | st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST; | |
285 | return 1; | |
286 | } else if (!(s->s3->tmp.new_cipher->algorithm_auth | |
287 | & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) { | |
288 | if (mt == SSL3_MT_CERTIFICATE) { | |
289 | st->hand_state = TLS_ST_CR_CERT; | |
290 | return 1; | |
291 | } | |
292 | } else { | |
a455d0f6 MC |
293 | ske_expected = key_exchange_expected(s); |
294 | if (ske_expected < 0) | |
295 | return 0; | |
296 | /* SKE is optional for some PSK ciphersuites */ | |
297 | if (ske_expected | |
298 | || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK) | |
299 | && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) { | |
300 | if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) { | |
301 | st->hand_state = TLS_ST_CR_KEY_EXCH; | |
302 | return 1; | |
303 | } | |
304 | } else if (mt == SSL3_MT_CERTIFICATE_REQUEST | |
61ae935a MC |
305 | && cert_req_allowed(s)) { |
306 | st->hand_state = TLS_ST_CR_CERT_REQ; | |
307 | return 1; | |
a455d0f6 | 308 | } else if (mt == SSL3_MT_SERVER_DONE) { |
61ae935a MC |
309 | st->hand_state = TLS_ST_CR_SRVR_DONE; |
310 | return 1; | |
61ae935a MC |
311 | } |
312 | } | |
313 | } | |
314 | break; | |
315 | ||
316 | case TLS_ST_CR_CERT: | |
317 | if (s->tlsext_status_expected) { | |
318 | if (mt == SSL3_MT_CERTIFICATE_STATUS) { | |
319 | st->hand_state = TLS_ST_CR_CERT_STATUS; | |
320 | return 1; | |
321 | } | |
a455d0f6 MC |
322 | return 0; |
323 | } | |
324 | /* Fall through */ | |
325 | ||
326 | case TLS_ST_CR_CERT_STATUS: | |
327 | ske_expected = key_exchange_expected(s); | |
328 | if (ske_expected < 0) | |
329 | return 0; | |
330 | /* SKE is optional for some PSK ciphersuites */ | |
331 | if (ske_expected | |
332 | || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK) | |
333 | && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) { | |
61ae935a MC |
334 | if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) { |
335 | st->hand_state = TLS_ST_CR_KEY_EXCH; | |
336 | return 1; | |
61ae935a | 337 | } |
a455d0f6 | 338 | return 0; |
61ae935a | 339 | } |
a455d0f6 | 340 | /* Fall through */ |
61ae935a | 341 | |
a455d0f6 MC |
342 | case TLS_ST_CR_KEY_EXCH: |
343 | if (mt == SSL3_MT_CERTIFICATE_REQUEST) { | |
344 | if (cert_req_allowed(s)) { | |
61ae935a MC |
345 | st->hand_state = TLS_ST_CR_CERT_REQ; |
346 | return 1; | |
61ae935a | 347 | } |
a455d0f6 | 348 | return 0; |
61ae935a | 349 | } |
a455d0f6 | 350 | /* Fall through */ |
61ae935a MC |
351 | |
352 | case TLS_ST_CR_CERT_REQ: | |
353 | if (mt == SSL3_MT_SERVER_DONE) { | |
354 | st->hand_state = TLS_ST_CR_SRVR_DONE; | |
355 | return 1; | |
356 | } | |
357 | break; | |
358 | ||
359 | case TLS_ST_CW_FINISHED: | |
360 | if (mt == SSL3_MT_NEWSESSION_TICKET && s->tlsext_ticket_expected) { | |
361 | st->hand_state = TLS_ST_CR_SESSION_TICKET; | |
362 | return 1; | |
363 | } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { | |
364 | st->hand_state = TLS_ST_CR_CHANGE; | |
365 | return 1; | |
366 | } | |
367 | break; | |
368 | ||
369 | case TLS_ST_CR_SESSION_TICKET: | |
370 | if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { | |
371 | st->hand_state = TLS_ST_CR_CHANGE; | |
372 | return 1; | |
373 | } | |
374 | break; | |
375 | ||
376 | case TLS_ST_CR_CHANGE: | |
377 | if (mt == SSL3_MT_FINISHED) { | |
378 | st->hand_state = TLS_ST_CR_FINISHED; | |
379 | return 1; | |
380 | } | |
381 | break; | |
382 | ||
383 | default: | |
384 | break; | |
385 | } | |
386 | ||
387 | /* No valid transition found */ | |
388 | return 0; | |
389 | } | |
390 | ||
391 | /* | |
392 | * client_write_transition() works out what handshake state to move to next | |
393 | * when the client is writing messages to be sent to the server. | |
394 | */ | |
8481f583 | 395 | WRITE_TRAN ossl_statem_client_write_transition(SSL *s) |
61ae935a | 396 | { |
d6f1a6e9 | 397 | OSSL_STATEM *st = &s->statem; |
61ae935a MC |
398 | |
399 | switch(st->hand_state) { | |
400 | case TLS_ST_OK: | |
401 | /* Renegotiation - fall through */ | |
402 | case TLS_ST_BEFORE: | |
403 | st->hand_state = TLS_ST_CW_CLNT_HELLO; | |
404 | return WRITE_TRAN_CONTINUE; | |
405 | ||
406 | case TLS_ST_CW_CLNT_HELLO: | |
407 | /* | |
408 | * No transition at the end of writing because we don't know what | |
409 | * we will be sent | |
410 | */ | |
411 | return WRITE_TRAN_FINISHED; | |
412 | ||
413 | case DTLS_ST_CR_HELLO_VERIFY_REQUEST: | |
414 | st->hand_state = TLS_ST_CW_CLNT_HELLO; | |
415 | return WRITE_TRAN_CONTINUE; | |
416 | ||
417 | case TLS_ST_CR_SRVR_DONE: | |
418 | if (s->s3->tmp.cert_req) | |
419 | st->hand_state = TLS_ST_CW_CERT; | |
420 | else | |
421 | st->hand_state = TLS_ST_CW_KEY_EXCH; | |
422 | return WRITE_TRAN_CONTINUE; | |
423 | ||
424 | case TLS_ST_CW_CERT: | |
425 | st->hand_state = TLS_ST_CW_KEY_EXCH; | |
426 | return WRITE_TRAN_CONTINUE; | |
427 | ||
428 | case TLS_ST_CW_KEY_EXCH: | |
429 | /* | |
430 | * For TLS, cert_req is set to 2, so a cert chain of nothing is | |
431 | * sent, but no verify packet is sent | |
432 | */ | |
433 | /* | |
434 | * XXX: For now, we do not support client authentication in ECDH | |
435 | * cipher suites with ECDH (rather than ECDSA) certificates. We | |
436 | * need to skip the certificate verify message when client's | |
437 | * ECDH public key is sent inside the client certificate. | |
438 | */ | |
439 | if (s->s3->tmp.cert_req == 1) { | |
440 | st->hand_state = TLS_ST_CW_CERT_VRFY; | |
441 | } else { | |
442 | st->hand_state = TLS_ST_CW_CHANGE; | |
443 | } | |
444 | if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) { | |
445 | st->hand_state = TLS_ST_CW_CHANGE; | |
446 | } | |
447 | return WRITE_TRAN_CONTINUE; | |
448 | ||
449 | case TLS_ST_CW_CERT_VRFY: | |
450 | st->hand_state = TLS_ST_CW_CHANGE; | |
451 | return WRITE_TRAN_CONTINUE; | |
452 | ||
453 | case TLS_ST_CW_CHANGE: | |
454 | #if defined(OPENSSL_NO_NEXTPROTONEG) | |
455 | st->hand_state = TLS_ST_CW_FINISHED; | |
456 | #else | |
457 | if (!SSL_IS_DTLS(s) && s->s3->next_proto_neg_seen) | |
458 | st->hand_state = TLS_ST_CW_NEXT_PROTO; | |
459 | else | |
460 | st->hand_state = TLS_ST_CW_FINISHED; | |
461 | #endif | |
462 | return WRITE_TRAN_CONTINUE; | |
463 | ||
464 | #if !defined(OPENSSL_NO_NEXTPROTONEG) | |
465 | case TLS_ST_CW_NEXT_PROTO: | |
466 | st->hand_state = TLS_ST_CW_FINISHED; | |
467 | return WRITE_TRAN_CONTINUE; | |
468 | #endif | |
469 | ||
470 | case TLS_ST_CW_FINISHED: | |
471 | if (s->hit) { | |
472 | st->hand_state = TLS_ST_OK; | |
fe3a3291 | 473 | ossl_statem_set_in_init(s, 0); |
61ae935a MC |
474 | return WRITE_TRAN_CONTINUE; |
475 | } else { | |
476 | return WRITE_TRAN_FINISHED; | |
477 | } | |
478 | ||
479 | case TLS_ST_CR_FINISHED: | |
480 | if (s->hit) { | |
481 | st->hand_state = TLS_ST_CW_CHANGE; | |
482 | return WRITE_TRAN_CONTINUE; | |
483 | } else { | |
484 | st->hand_state = TLS_ST_OK; | |
fe3a3291 | 485 | ossl_statem_set_in_init(s, 0); |
61ae935a MC |
486 | return WRITE_TRAN_CONTINUE; |
487 | } | |
488 | ||
489 | default: | |
490 | /* Shouldn't happen */ | |
491 | return WRITE_TRAN_ERROR; | |
492 | } | |
493 | } | |
494 | ||
495 | /* | |
496 | * Perform any pre work that needs to be done prior to sending a message from | |
497 | * the client to the server. | |
498 | */ | |
8481f583 | 499 | WORK_STATE ossl_statem_client_pre_work(SSL *s, WORK_STATE wst) |
61ae935a | 500 | { |
d6f1a6e9 | 501 | OSSL_STATEM *st = &s->statem; |
61ae935a MC |
502 | |
503 | switch(st->hand_state) { | |
504 | case TLS_ST_CW_CLNT_HELLO: | |
505 | s->shutdown = 0; | |
506 | if (SSL_IS_DTLS(s)) { | |
507 | /* every DTLS ClientHello resets Finished MAC */ | |
508 | ssl3_init_finished_mac(s); | |
509 | } | |
510 | break; | |
511 | ||
512 | case TLS_ST_CW_CERT: | |
513 | return tls_prepare_client_certificate(s, wst); | |
514 | ||
515 | case TLS_ST_CW_CHANGE: | |
516 | if (SSL_IS_DTLS(s)) { | |
517 | if (s->hit) { | |
518 | /* | |
519 | * We're into the last flight so we don't retransmit these | |
520 | * messages unless we need to. | |
521 | */ | |
522 | st->use_timer = 0; | |
523 | } | |
524 | #ifndef OPENSSL_NO_SCTP | |
525 | if (BIO_dgram_is_sctp(SSL_get_wbio(s))) | |
526 | return dtls_wait_for_dry(s); | |
527 | #endif | |
528 | } | |
529 | return WORK_FINISHED_CONTINUE; | |
530 | ||
531 | case TLS_ST_OK: | |
532 | return tls_finish_handshake(s, wst); | |
533 | ||
534 | default: | |
535 | /* No pre work to be done */ | |
536 | break; | |
537 | } | |
538 | ||
539 | return WORK_FINISHED_CONTINUE; | |
540 | } | |
541 | ||
542 | /* | |
543 | * Perform any work that needs to be done after sending a message from the | |
544 | * client to the server. | |
545 | */ | |
8481f583 | 546 | WORK_STATE ossl_statem_client_post_work(SSL *s, WORK_STATE wst) |
61ae935a | 547 | { |
d6f1a6e9 | 548 | OSSL_STATEM *st = &s->statem; |
61ae935a MC |
549 | |
550 | s->init_num = 0; | |
551 | ||
552 | switch(st->hand_state) { | |
553 | case TLS_ST_CW_CLNT_HELLO: | |
554 | if (SSL_IS_DTLS(s) && s->d1->cookie_len > 0 && statem_flush(s) != 1) | |
555 | return WORK_MORE_A; | |
556 | #ifndef OPENSSL_NO_SCTP | |
557 | /* Disable buffering for SCTP */ | |
558 | if (!SSL_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(s))) { | |
559 | #endif | |
560 | /* | |
561 | * turn on buffering for the next lot of output | |
562 | */ | |
563 | if (s->bbio != s->wbio) | |
564 | s->wbio = BIO_push(s->bbio, s->wbio); | |
565 | #ifndef OPENSSL_NO_SCTP | |
566 | } | |
567 | #endif | |
568 | if (SSL_IS_DTLS(s)) { | |
569 | /* Treat the next message as the first packet */ | |
570 | s->first_packet = 1; | |
571 | } | |
572 | break; | |
573 | ||
574 | case TLS_ST_CW_KEY_EXCH: | |
575 | if (tls_client_key_exchange_post_work(s) == 0) | |
576 | return WORK_ERROR; | |
577 | break; | |
578 | ||
579 | case TLS_ST_CW_CHANGE: | |
580 | s->session->cipher = s->s3->tmp.new_cipher; | |
581 | #ifdef OPENSSL_NO_COMP | |
582 | s->session->compress_meth = 0; | |
583 | #else | |
584 | if (s->s3->tmp.new_compression == NULL) | |
585 | s->session->compress_meth = 0; | |
586 | else | |
587 | s->session->compress_meth = s->s3->tmp.new_compression->id; | |
588 | #endif | |
589 | if (!s->method->ssl3_enc->setup_key_block(s)) | |
590 | return WORK_ERROR; | |
591 | ||
592 | if (!s->method->ssl3_enc->change_cipher_state(s, | |
593 | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) | |
594 | return WORK_ERROR; | |
595 | ||
596 | if (SSL_IS_DTLS(s)) { | |
597 | #ifndef OPENSSL_NO_SCTP | |
598 | if (s->hit) { | |
599 | /* | |
600 | * Change to new shared key of SCTP-Auth, will be ignored if | |
601 | * no SCTP used. | |
602 | */ | |
603 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, | |
604 | 0, NULL); | |
605 | } | |
606 | #endif | |
607 | ||
608 | dtls1_reset_seq_numbers(s, SSL3_CC_WRITE); | |
609 | } | |
610 | break; | |
611 | ||
612 | case TLS_ST_CW_FINISHED: | |
613 | #ifndef OPENSSL_NO_SCTP | |
614 | if (wst == WORK_MORE_A && SSL_IS_DTLS(s) && s->hit == 0) { | |
615 | /* | |
616 | * Change to new shared key of SCTP-Auth, will be ignored if | |
617 | * no SCTP used. | |
618 | */ | |
619 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, | |
620 | 0, NULL); | |
621 | } | |
622 | #endif | |
623 | if (statem_flush(s) != 1) | |
624 | return WORK_MORE_B; | |
61ae935a MC |
625 | break; |
626 | ||
627 | default: | |
628 | /* No post work to be done */ | |
629 | break; | |
630 | } | |
631 | ||
632 | return WORK_FINISHED_CONTINUE; | |
633 | } | |
634 | ||
635 | /* | |
636 | * Construct a message to be sent from the client to the server. | |
637 | * | |
638 | * Valid return values are: | |
639 | * 1: Success | |
640 | * 0: Error | |
641 | */ | |
8481f583 | 642 | int ossl_statem_client_construct_message(SSL *s) |
61ae935a | 643 | { |
d6f1a6e9 | 644 | OSSL_STATEM *st = &s->statem; |
61ae935a MC |
645 | |
646 | switch(st->hand_state) { | |
647 | case TLS_ST_CW_CLNT_HELLO: | |
648 | return tls_construct_client_hello(s); | |
649 | ||
650 | case TLS_ST_CW_CERT: | |
651 | return tls_construct_client_certificate(s); | |
652 | ||
653 | case TLS_ST_CW_KEY_EXCH: | |
654 | return tls_construct_client_key_exchange(s); | |
655 | ||
656 | case TLS_ST_CW_CERT_VRFY: | |
657 | return tls_construct_client_verify(s); | |
658 | ||
659 | case TLS_ST_CW_CHANGE: | |
660 | if (SSL_IS_DTLS(s)) | |
661 | return dtls_construct_change_cipher_spec(s); | |
662 | else | |
663 | return tls_construct_change_cipher_spec(s); | |
664 | ||
665 | #if !defined(OPENSSL_NO_NEXTPROTONEG) | |
666 | case TLS_ST_CW_NEXT_PROTO: | |
667 | return tls_construct_next_proto(s); | |
668 | #endif | |
669 | case TLS_ST_CW_FINISHED: | |
670 | return tls_construct_finished(s, | |
671 | s->method-> | |
672 | ssl3_enc->client_finished_label, | |
673 | s->method-> | |
674 | ssl3_enc->client_finished_label_len); | |
675 | ||
676 | default: | |
677 | /* Shouldn't happen */ | |
678 | break; | |
679 | } | |
680 | ||
681 | return 0; | |
682 | } | |
683 | ||
684 | /* | |
685 | * Returns the maximum allowed length for the current message that we are | |
686 | * reading. Excludes the message header. | |
687 | */ | |
8481f583 | 688 | unsigned long ossl_statem_client_max_message_size(SSL *s) |
61ae935a | 689 | { |
d6f1a6e9 | 690 | OSSL_STATEM *st = &s->statem; |
61ae935a MC |
691 | |
692 | switch(st->hand_state) { | |
693 | case TLS_ST_CR_SRVR_HELLO: | |
694 | return SERVER_HELLO_MAX_LENGTH; | |
695 | ||
696 | case DTLS_ST_CR_HELLO_VERIFY_REQUEST: | |
697 | return HELLO_VERIFY_REQUEST_MAX_LENGTH; | |
698 | ||
699 | case TLS_ST_CR_CERT: | |
700 | return s->max_cert_list; | |
701 | ||
702 | case TLS_ST_CR_CERT_STATUS: | |
703 | return SSL3_RT_MAX_PLAIN_LENGTH; | |
704 | ||
705 | case TLS_ST_CR_KEY_EXCH: | |
706 | return SERVER_KEY_EXCH_MAX_LENGTH; | |
707 | ||
708 | case TLS_ST_CR_CERT_REQ: | |
709 | return SSL3_RT_MAX_PLAIN_LENGTH; | |
710 | ||
711 | case TLS_ST_CR_SRVR_DONE: | |
712 | return SERVER_HELLO_DONE_MAX_LENGTH; | |
713 | ||
714 | case TLS_ST_CR_CHANGE: | |
715 | return CCS_MAX_LENGTH; | |
716 | ||
717 | case TLS_ST_CR_SESSION_TICKET: | |
718 | return SSL3_RT_MAX_PLAIN_LENGTH; | |
719 | ||
720 | case TLS_ST_CR_FINISHED: | |
721 | return FINISHED_MAX_LENGTH; | |
722 | ||
723 | default: | |
724 | /* Shouldn't happen */ | |
725 | break; | |
726 | } | |
727 | ||
728 | return 0; | |
729 | } | |
730 | ||
731 | /* | |
732 | * Process a message that the client has been received from the server. | |
733 | */ | |
8481f583 | 734 | MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt) |
61ae935a | 735 | { |
d6f1a6e9 | 736 | OSSL_STATEM *st = &s->statem; |
61ae935a MC |
737 | |
738 | switch(st->hand_state) { | |
739 | case TLS_ST_CR_SRVR_HELLO: | |
740 | return tls_process_server_hello(s, pkt); | |
741 | ||
742 | case DTLS_ST_CR_HELLO_VERIFY_REQUEST: | |
743 | return dtls_process_hello_verify(s, pkt); | |
744 | ||
745 | case TLS_ST_CR_CERT: | |
746 | return tls_process_server_certificate(s, pkt); | |
747 | ||
748 | case TLS_ST_CR_CERT_STATUS: | |
749 | return tls_process_cert_status(s, pkt); | |
750 | ||
751 | case TLS_ST_CR_KEY_EXCH: | |
752 | return tls_process_key_exchange(s, pkt); | |
753 | ||
754 | case TLS_ST_CR_CERT_REQ: | |
755 | return tls_process_certificate_request(s, pkt); | |
756 | ||
757 | case TLS_ST_CR_SRVR_DONE: | |
758 | return tls_process_server_done(s, pkt); | |
759 | ||
760 | case TLS_ST_CR_CHANGE: | |
761 | return tls_process_change_cipher_spec(s, pkt); | |
762 | ||
763 | case TLS_ST_CR_SESSION_TICKET: | |
764 | return tls_process_new_session_ticket(s, pkt); | |
765 | ||
766 | case TLS_ST_CR_FINISHED: | |
767 | return tls_process_finished(s, pkt); | |
768 | ||
769 | default: | |
770 | /* Shouldn't happen */ | |
771 | break; | |
772 | } | |
773 | ||
774 | return MSG_PROCESS_ERROR; | |
775 | } | |
776 | ||
777 | /* | |
778 | * Perform any further processing required following the receipt of a message | |
779 | * from the server | |
780 | */ | |
8481f583 | 781 | WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst) |
61ae935a | 782 | { |
d6f1a6e9 | 783 | OSSL_STATEM *st = &s->statem; |
61ae935a MC |
784 | |
785 | switch(st->hand_state) { | |
786 | #ifndef OPENSSL_NO_SCTP | |
787 | case TLS_ST_CR_SRVR_DONE: | |
788 | /* We only get here if we are using SCTP and we are renegotiating */ | |
789 | if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) { | |
790 | s->s3->in_read_app_data = 2; | |
791 | s->rwstate = SSL_READING; | |
792 | BIO_clear_retry_flags(SSL_get_rbio(s)); | |
793 | BIO_set_retry_read(SSL_get_rbio(s)); | |
fe3a3291 | 794 | ossl_statem_set_sctp_read_sock(s, 1); |
61ae935a MC |
795 | return WORK_MORE_A; |
796 | } | |
fe3a3291 | 797 | ossl_statem_set_sctp_read_sock(s, 0); |
61ae935a MC |
798 | return WORK_FINISHED_STOP; |
799 | #endif | |
800 | ||
61ae935a MC |
801 | default: |
802 | break; | |
803 | } | |
804 | ||
805 | /* Shouldn't happen */ | |
806 | return WORK_ERROR; | |
807 | } | |
808 | ||
d45ba43d MC |
809 | /* |
810 | * Work out what version we should be using for the initial ClientHello if | |
811 | * the version is currently set to (D)TLS_ANY_VERSION. | |
812 | * Returns 1 on success | |
813 | * Returns 0 on error | |
814 | */ | |
815 | static int ssl_set_version(SSL *s) | |
816 | { | |
817 | unsigned long mask, options = s->options; | |
818 | ||
819 | if (s->method->version == TLS_ANY_VERSION) { | |
820 | /* | |
821 | * SSL_OP_NO_X disables all protocols above X *if* there are | |
822 | * some protocols below X enabled. This is required in order | |
823 | * to maintain "version capability" vector contiguous. So | |
824 | * that if application wants to disable TLS1.0 in favour of | |
825 | * TLS1>=1, it would be insufficient to pass SSL_NO_TLSv1, the | |
826 | * answer is SSL_OP_NO_TLSv1|SSL_OP_NO_SSLv3. | |
827 | */ | |
828 | mask = SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1 | |
829 | #if !defined(OPENSSL_NO_SSL3) | |
830 | | SSL_OP_NO_SSLv3 | |
831 | #endif | |
832 | ; | |
833 | #if !defined(OPENSSL_NO_TLS1_2_CLIENT) | |
834 | if (options & SSL_OP_NO_TLSv1_2) { | |
835 | if ((options & mask) != mask) { | |
836 | s->version = TLS1_1_VERSION; | |
837 | } else { | |
838 | SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE); | |
839 | return 0; | |
840 | } | |
841 | } else { | |
842 | s->version = TLS1_2_VERSION; | |
843 | } | |
844 | #else | |
845 | if ((options & mask) == mask) { | |
846 | SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE); | |
847 | return 0; | |
848 | } | |
849 | s->version = TLS1_1_VERSION; | |
850 | #endif | |
851 | ||
852 | mask &= ~SSL_OP_NO_TLSv1_1; | |
853 | if ((options & SSL_OP_NO_TLSv1_1) && (options & mask) != mask) | |
854 | s->version = TLS1_VERSION; | |
855 | mask &= ~SSL_OP_NO_TLSv1; | |
856 | #if !defined(OPENSSL_NO_SSL3) | |
857 | if ((options & SSL_OP_NO_TLSv1) && (options & mask) != mask) | |
858 | s->version = SSL3_VERSION; | |
859 | #endif | |
860 | ||
861 | if (s->version != TLS1_2_VERSION && tls1_suiteb(s)) { | |
862 | SSLerr(SSL_F_SSL_SET_VERSION, | |
863 | SSL_R_ONLY_TLS_1_2_ALLOWED_IN_SUITEB_MODE); | |
864 | return 0; | |
865 | } | |
866 | ||
867 | if (s->version == SSL3_VERSION && FIPS_mode()) { | |
868 | SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE); | |
869 | return 0; | |
870 | } | |
871 | ||
872 | } else if (s->method->version == DTLS_ANY_VERSION) { | |
873 | /* Determine which DTLS version to use */ | |
874 | /* If DTLS 1.2 disabled correct the version number */ | |
875 | if (options & SSL_OP_NO_DTLSv1_2) { | |
876 | if (tls1_suiteb(s)) { | |
877 | SSLerr(SSL_F_SSL_SET_VERSION, | |
878 | SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE); | |
879 | return 0; | |
880 | } | |
881 | /* | |
882 | * Disabling all versions is silly: return an error. | |
883 | */ | |
884 | if (options & SSL_OP_NO_DTLSv1) { | |
885 | SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_WRONG_SSL_VERSION); | |
886 | return 0; | |
887 | } | |
888 | /* | |
889 | * Update method so we don't use any DTLS 1.2 features. | |
890 | */ | |
891 | s->method = DTLSv1_client_method(); | |
892 | s->version = DTLS1_VERSION; | |
893 | } else { | |
894 | /* | |
895 | * We only support one version: update method | |
896 | */ | |
897 | if (options & SSL_OP_NO_DTLSv1) | |
898 | s->method = DTLSv1_2_client_method(); | |
899 | s->version = DTLS1_2_VERSION; | |
900 | } | |
901 | } | |
902 | ||
903 | s->client_version = s->version; | |
904 | ||
905 | return 1; | |
906 | } | |
907 | ||
b9908bf9 | 908 | int tls_construct_client_hello(SSL *s) |
0f113f3e MC |
909 | { |
910 | unsigned char *buf; | |
911 | unsigned char *p, *d; | |
912 | int i; | |
913 | unsigned long l; | |
914 | int al = 0; | |
09b6c2ef | 915 | #ifndef OPENSSL_NO_COMP |
0f113f3e MC |
916 | int j; |
917 | SSL_COMP *comp; | |
918 | #endif | |
b9908bf9 | 919 | SSL_SESSION *sess = s->session; |
0f113f3e MC |
920 | |
921 | buf = (unsigned char *)s->init_buf->data; | |
0f113f3e | 922 | |
b9908bf9 MC |
923 | /* Work out what SSL/TLS/DTLS version to use */ |
924 | if (ssl_set_version(s) == 0) | |
925 | goto err; | |
0f113f3e | 926 | |
b9908bf9 | 927 | if ((sess == NULL) || (sess->ssl_version != s->version) || |
0f113f3e | 928 | /* |
b9908bf9 MC |
929 | * In the case of EAP-FAST, we can have a pre-shared |
930 | * "ticket" without a session ID. | |
0f113f3e | 931 | */ |
b9908bf9 MC |
932 | (!sess->session_id_length && !sess->tlsext_tick) || |
933 | (sess->not_resumable)) { | |
934 | if (!ssl_get_new_session(s, 0)) | |
e1b568dd | 935 | goto err; |
b9908bf9 MC |
936 | } |
937 | /* else use the pre-loaded session */ | |
0f113f3e | 938 | |
b9908bf9 | 939 | p = s->s3->client_random; |
0f113f3e | 940 | |
b9908bf9 MC |
941 | /* |
942 | * for DTLS if client_random is initialized, reuse it, we are | |
943 | * required to use same upon reply to HelloVerify | |
944 | */ | |
945 | if (SSL_IS_DTLS(s)) { | |
946 | size_t idx; | |
947 | i = 1; | |
948 | for (idx = 0; idx < sizeof(s->s3->client_random); idx++) { | |
949 | if (p[idx]) { | |
950 | i = 0; | |
951 | break; | |
0f113f3e | 952 | } |
0f113f3e | 953 | } |
b9908bf9 MC |
954 | } else |
955 | i = 1; | |
0f113f3e | 956 | |
b9908bf9 MC |
957 | if (i && ssl_fill_hello_random(s, 0, p, |
958 | sizeof(s->s3->client_random)) <= 0) | |
959 | goto err; | |
960 | ||
961 | /* Do the message type and length last */ | |
962 | d = p = ssl_handshake_start(s); | |
963 | ||
964 | /*- | |
965 | * version indicates the negotiated version: for example from | |
966 | * an SSLv2/v3 compatible client hello). The client_version | |
967 | * field is the maximum version we permit and it is also | |
968 | * used in RSA encrypted premaster secrets. Some servers can | |
969 | * choke if we initially report a higher version then | |
970 | * renegotiate to a lower one in the premaster secret. This | |
971 | * didn't happen with TLS 1.0 as most servers supported it | |
972 | * but it can with TLS 1.1 or later if the server only supports | |
973 | * 1.0. | |
974 | * | |
975 | * Possible scenario with previous logic: | |
976 | * 1. Client hello indicates TLS 1.2 | |
977 | * 2. Server hello says TLS 1.0 | |
978 | * 3. RSA encrypted premaster secret uses 1.2. | |
979 | * 4. Handhaked proceeds using TLS 1.0. | |
980 | * 5. Server sends hello request to renegotiate. | |
981 | * 6. Client hello indicates TLS v1.0 as we now | |
982 | * know that is maximum server supports. | |
983 | * 7. Server chokes on RSA encrypted premaster secret | |
984 | * containing version 1.0. | |
985 | * | |
986 | * For interoperability it should be OK to always use the | |
987 | * maximum version we support in client hello and then rely | |
988 | * on the checking of version to ensure the servers isn't | |
989 | * being inconsistent: for example initially negotiating with | |
990 | * TLS 1.0 and renegotiating with TLS 1.2. We do this by using | |
991 | * client_version in client hello and not resetting it to | |
992 | * the negotiated version. | |
993 | */ | |
994 | *(p++) = s->client_version >> 8; | |
995 | *(p++) = s->client_version & 0xff; | |
996 | ||
997 | /* Random stuff */ | |
998 | memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE); | |
999 | p += SSL3_RANDOM_SIZE; | |
1000 | ||
1001 | /* Session ID */ | |
1002 | if (s->new_session) | |
1003 | i = 0; | |
1004 | else | |
1005 | i = s->session->session_id_length; | |
1006 | *(p++) = i; | |
1007 | if (i != 0) { | |
1008 | if (i > (int)sizeof(s->session->session_id)) { | |
1009 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); | |
1010 | goto err; | |
0f113f3e | 1011 | } |
b9908bf9 MC |
1012 | memcpy(p, s->session->session_id, i); |
1013 | p += i; | |
1014 | } | |
0f113f3e | 1015 | |
b9908bf9 MC |
1016 | /* cookie stuff for DTLS */ |
1017 | if (SSL_IS_DTLS(s)) { | |
1018 | if (s->d1->cookie_len > sizeof(s->d1->cookie)) { | |
1019 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); | |
0f113f3e MC |
1020 | goto err; |
1021 | } | |
b9908bf9 MC |
1022 | *(p++) = s->d1->cookie_len; |
1023 | memcpy(p, s->d1->cookie, s->d1->cookie_len); | |
1024 | p += s->d1->cookie_len; | |
1025 | } | |
1026 | ||
1027 | /* Ciphers supported */ | |
1028 | i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2])); | |
1029 | if (i == 0) { | |
1030 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE); | |
1031 | goto err; | |
1032 | } | |
800e1cd9 | 1033 | #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH |
b9908bf9 MC |
1034 | /* |
1035 | * Some servers hang if client hello > 256 bytes as hack workaround | |
1036 | * chop number of supported ciphers to keep it well below this if we | |
1037 | * use TLS v1.2 | |
1038 | */ | |
1039 | if (TLS1_get_version(s) >= TLS1_2_VERSION | |
1040 | && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH) | |
1041 | i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1; | |
0f113f3e | 1042 | #endif |
b9908bf9 MC |
1043 | s2n(i, p); |
1044 | p += i; | |
0f113f3e | 1045 | |
b9908bf9 | 1046 | /* COMPRESSION */ |
09b6c2ef | 1047 | #ifdef OPENSSL_NO_COMP |
b9908bf9 | 1048 | *(p++) = 1; |
09b6c2ef | 1049 | #else |
566dda07 | 1050 | |
b9908bf9 MC |
1051 | if (!ssl_allow_compression(s) || !s->ctx->comp_methods) |
1052 | j = 0; | |
1053 | else | |
1054 | j = sk_SSL_COMP_num(s->ctx->comp_methods); | |
1055 | *(p++) = 1 + j; | |
1056 | for (i = 0; i < j; i++) { | |
1057 | comp = sk_SSL_COMP_value(s->ctx->comp_methods, i); | |
1058 | *(p++) = comp->id; | |
1059 | } | |
09b6c2ef | 1060 | #endif |
b9908bf9 | 1061 | *(p++) = 0; /* Add the NULL method */ |
761772d7 | 1062 | |
b9908bf9 MC |
1063 | /* TLS extensions */ |
1064 | if (ssl_prepare_clienthello_tlsext(s) <= 0) { | |
1065 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT); | |
1066 | goto err; | |
1067 | } | |
1068 | if ((p = | |
1069 | ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH, | |
1070 | &al)) == NULL) { | |
1071 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
1072 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); | |
1073 | goto err; | |
1074 | } | |
0f113f3e | 1075 | |
b9908bf9 MC |
1076 | l = p - d; |
1077 | if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) { | |
1078 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | |
1079 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); | |
1080 | goto err; | |
0f113f3e MC |
1081 | } |
1082 | ||
b9908bf9 | 1083 | return 1; |
0f113f3e | 1084 | err: |
fe3a3291 | 1085 | ossl_statem_set_error(s); |
b9908bf9 | 1086 | return 0; |
0f113f3e | 1087 | } |
d02b48c6 | 1088 | |
be3583fa | 1089 | MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt) |
8ba708e5 MC |
1090 | { |
1091 | int al; | |
1092 | unsigned int cookie_len; | |
1093 | PACKET cookiepkt; | |
1094 | ||
1095 | if (!PACKET_forward(pkt, 2) | |
1096 | || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) { | |
1097 | al = SSL_AD_DECODE_ERROR; | |
1098 | SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH); | |
1099 | goto f_err; | |
1100 | } | |
1101 | ||
1102 | cookie_len = PACKET_remaining(&cookiepkt); | |
1103 | if (cookie_len > sizeof(s->d1->cookie)) { | |
1104 | al = SSL_AD_ILLEGAL_PARAMETER; | |
1105 | SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_TOO_LONG); | |
1106 | goto f_err; | |
1107 | } | |
1108 | ||
1109 | if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) { | |
1110 | al = SSL_AD_DECODE_ERROR; | |
1111 | SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH); | |
1112 | goto f_err; | |
1113 | } | |
1114 | s->d1->cookie_len = cookie_len; | |
1115 | ||
1116 | return MSG_PROCESS_FINISHED_READING; | |
1117 | f_err: | |
1118 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
fe3a3291 | 1119 | ossl_statem_set_error(s); |
8ba708e5 MC |
1120 | return MSG_PROCESS_ERROR; |
1121 | } | |
1122 | ||
be3583fa | 1123 | MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt) |
b9908bf9 MC |
1124 | { |
1125 | STACK_OF(SSL_CIPHER) *sk; | |
1126 | const SSL_CIPHER *c; | |
73999b62 | 1127 | PACKET session_id; |
b9908bf9 MC |
1128 | size_t session_id_len; |
1129 | unsigned char *cipherchars; | |
1130 | int i, al = SSL_AD_INTERNAL_ERROR; | |
1131 | unsigned int compression; | |
1132 | #ifndef OPENSSL_NO_COMP | |
1133 | SSL_COMP *comp; | |
1134 | #endif | |
1135 | ||
13c9bb3e | 1136 | if (s->method->version == TLS_ANY_VERSION) { |
50932c4a MC |
1137 | unsigned int sversion; |
1138 | ||
73999b62 | 1139 | if (!PACKET_get_net_2(pkt, &sversion)) { |
50932c4a | 1140 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 1141 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); |
50932c4a MC |
1142 | goto f_err; |
1143 | } | |
13c9bb3e MC |
1144 | |
1145 | #if TLS_MAX_VERSION != TLS1_2_VERSION | |
1146 | #error Code needs updating for new TLS version | |
1147 | #endif | |
1148 | #ifndef OPENSSL_NO_SSL3 | |
1149 | if ((sversion == SSL3_VERSION) && !(s->options & SSL_OP_NO_SSLv3)) { | |
1150 | if (FIPS_mode()) { | |
b9908bf9 | 1151 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
13c9bb3e | 1152 | SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE); |
d45ba43d MC |
1153 | al = SSL_AD_PROTOCOL_VERSION; |
1154 | goto f_err; | |
13c9bb3e MC |
1155 | } |
1156 | s->method = SSLv3_client_method(); | |
1157 | } else | |
1158 | #endif | |
1159 | if ((sversion == TLS1_VERSION) && !(s->options & SSL_OP_NO_TLSv1)) { | |
1160 | s->method = TLSv1_client_method(); | |
1161 | } else if ((sversion == TLS1_1_VERSION) && | |
1162 | !(s->options & SSL_OP_NO_TLSv1_1)) { | |
1163 | s->method = TLSv1_1_client_method(); | |
1164 | } else if ((sversion == TLS1_2_VERSION) && | |
1165 | !(s->options & SSL_OP_NO_TLSv1_2)) { | |
1166 | s->method = TLSv1_2_client_method(); | |
1167 | } else { | |
b9908bf9 | 1168 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL); |
d45ba43d MC |
1169 | al = SSL_AD_PROTOCOL_VERSION; |
1170 | goto f_err; | |
13c9bb3e MC |
1171 | } |
1172 | s->session->ssl_version = s->version = s->method->version; | |
1173 | ||
1174 | if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) { | |
b9908bf9 | 1175 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_VERSION_TOO_LOW); |
d45ba43d MC |
1176 | al = SSL_AD_PROTOCOL_VERSION; |
1177 | goto f_err; | |
13c9bb3e MC |
1178 | } |
1179 | } else if (s->method->version == DTLS_ANY_VERSION) { | |
0f113f3e | 1180 | /* Work out correct protocol version to use */ |
50932c4a MC |
1181 | unsigned int hversion; |
1182 | int options; | |
1183 | ||
73999b62 | 1184 | if (!PACKET_get_net_2(pkt, &hversion)) { |
50932c4a | 1185 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 1186 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); |
50932c4a MC |
1187 | goto f_err; |
1188 | } | |
1189 | ||
1190 | options = s->options; | |
0f113f3e MC |
1191 | if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2)) |
1192 | s->method = DTLSv1_2_client_method(); | |
1193 | else if (tls1_suiteb(s)) { | |
b9908bf9 | 1194 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
0f113f3e MC |
1195 | SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE); |
1196 | s->version = hversion; | |
1197 | al = SSL_AD_PROTOCOL_VERSION; | |
1198 | goto f_err; | |
1199 | } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1)) | |
1200 | s->method = DTLSv1_client_method(); | |
1201 | else { | |
b9908bf9 | 1202 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION); |
0f113f3e MC |
1203 | s->version = hversion; |
1204 | al = SSL_AD_PROTOCOL_VERSION; | |
1205 | goto f_err; | |
1206 | } | |
7322abf5 | 1207 | s->session->ssl_version = s->version = s->method->version; |
50932c4a MC |
1208 | } else { |
1209 | unsigned char *vers; | |
1210 | ||
73999b62 | 1211 | if (!PACKET_get_bytes(pkt, &vers, 2)) { |
50932c4a | 1212 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 1213 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); |
50932c4a MC |
1214 | goto f_err; |
1215 | } | |
1216 | if ((vers[0] != (s->version >> 8)) | |
1217 | || (vers[1] != (s->version & 0xff))) { | |
b9908bf9 | 1218 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION); |
50932c4a MC |
1219 | s->version = (s->version & 0xff00) | vers[1]; |
1220 | al = SSL_AD_PROTOCOL_VERSION; | |
1221 | goto f_err; | |
1222 | } | |
0f113f3e | 1223 | } |
0f113f3e MC |
1224 | |
1225 | /* load the server hello data */ | |
1226 | /* load the server random */ | |
73999b62 | 1227 | if (!PACKET_copy_bytes(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) { |
50932c4a | 1228 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 1229 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); |
50932c4a MC |
1230 | goto f_err; |
1231 | } | |
0f113f3e MC |
1232 | |
1233 | s->hit = 0; | |
1234 | ||
fc5ce51d | 1235 | /* Get the session-id. */ |
73999b62 | 1236 | if (!PACKET_get_length_prefixed_1(pkt, &session_id)) { |
fc5ce51d | 1237 | al = SSL_AD_DECODE_ERROR; |
f0659bdb | 1238 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); |
fc5ce51d EK |
1239 | goto f_err; |
1240 | } | |
1241 | session_id_len = PACKET_remaining(&session_id); | |
1242 | if (session_id_len > sizeof s->session->session_id | |
1243 | || session_id_len > SSL3_SESSION_ID_SIZE) { | |
0f113f3e | 1244 | al = SSL_AD_ILLEGAL_PARAMETER; |
b9908bf9 | 1245 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG); |
0f113f3e MC |
1246 | goto f_err; |
1247 | } | |
e481f9b9 | 1248 | |
73999b62 | 1249 | if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) { |
f0659bdb | 1250 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); |
fc5ce51d EK |
1251 | al = SSL_AD_DECODE_ERROR; |
1252 | goto f_err; | |
1253 | } | |
1254 | ||
0f113f3e | 1255 | /* |
6e3d0153 EK |
1256 | * Check if we can resume the session based on external pre-shared secret. |
1257 | * EAP-FAST (RFC 4851) supports two types of session resumption. | |
1258 | * Resumption based on server-side state works with session IDs. | |
1259 | * Resumption based on pre-shared Protected Access Credentials (PACs) | |
1260 | * works by overriding the SessionTicket extension at the application | |
1261 | * layer, and does not send a session ID. (We do not know whether EAP-FAST | |
1262 | * servers would honour the session ID.) Therefore, the session ID alone | |
1263 | * is not a reliable indicator of session resumption, so we first check if | |
1264 | * we can resume, and later peek at the next handshake message to see if the | |
1265 | * server wants to resume. | |
0f113f3e | 1266 | */ |
6e3d0153 EK |
1267 | if (s->version >= TLS1_VERSION && s->tls_session_secret_cb && |
1268 | s->session->tlsext_tick) { | |
0f113f3e MC |
1269 | SSL_CIPHER *pref_cipher = NULL; |
1270 | s->session->master_key_length = sizeof(s->session->master_key); | |
1271 | if (s->tls_session_secret_cb(s, s->session->master_key, | |
1272 | &s->session->master_key_length, | |
1273 | NULL, &pref_cipher, | |
1274 | s->tls_session_secret_cb_arg)) { | |
1275 | s->session->cipher = pref_cipher ? | |
50932c4a | 1276 | pref_cipher : ssl_get_cipher_by_char(s, cipherchars); |
6e3d0153 | 1277 | } else { |
b9908bf9 | 1278 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR); |
6e3d0153 EK |
1279 | al = SSL_AD_INTERNAL_ERROR; |
1280 | goto f_err; | |
0f113f3e | 1281 | } |
50932c4a MC |
1282 | } |
1283 | ||
fc5ce51d EK |
1284 | if (session_id_len != 0 && session_id_len == s->session->session_id_length |
1285 | && memcmp(PACKET_data(&session_id), s->session->session_id, | |
1286 | session_id_len) == 0) { | |
0f113f3e MC |
1287 | if (s->sid_ctx_length != s->session->sid_ctx_length |
1288 | || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) { | |
1289 | /* actually a client application bug */ | |
1290 | al = SSL_AD_ILLEGAL_PARAMETER; | |
b9908bf9 | 1291 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
0f113f3e MC |
1292 | SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); |
1293 | goto f_err; | |
1294 | } | |
1295 | s->hit = 1; | |
6e3d0153 | 1296 | } else { |
0f113f3e | 1297 | /* |
6e3d0153 EK |
1298 | * If we were trying for session-id reuse but the server |
1299 | * didn't echo the ID, make a new SSL_SESSION. | |
1300 | * In the case of EAP-FAST and PAC, we do not send a session ID, | |
1301 | * so the PAC-based session secret is always preserved. It'll be | |
1302 | * overwritten if the server refuses resumption. | |
0f113f3e MC |
1303 | */ |
1304 | if (s->session->session_id_length > 0) { | |
1305 | if (!ssl_get_new_session(s, 0)) { | |
1306 | goto f_err; | |
1307 | } | |
1308 | } | |
50932c4a | 1309 | |
fc5ce51d EK |
1310 | s->session->session_id_length = session_id_len; |
1311 | /* session_id_len could be 0 */ | |
1312 | memcpy(s->session->session_id, PACKET_data(&session_id), | |
1313 | session_id_len); | |
0f113f3e | 1314 | } |
fc5ce51d | 1315 | |
50932c4a | 1316 | c = ssl_get_cipher_by_char(s, cipherchars); |
0f113f3e MC |
1317 | if (c == NULL) { |
1318 | /* unknown cipher */ | |
1319 | al = SSL_AD_ILLEGAL_PARAMETER; | |
b9908bf9 | 1320 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED); |
0f113f3e MC |
1321 | goto f_err; |
1322 | } | |
1323 | /* Set version disabled mask now we know version */ | |
1324 | if (!SSL_USE_TLS1_2_CIPHERS(s)) | |
4d69f9e6 | 1325 | s->s3->tmp.mask_ssl = SSL_TLSV1_2; |
0f113f3e | 1326 | else |
4d69f9e6 | 1327 | s->s3->tmp.mask_ssl = 0; |
0f113f3e MC |
1328 | /* |
1329 | * If it is a disabled cipher we didn't send it in client hello, so | |
1330 | * return an error. | |
1331 | */ | |
1332 | if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) { | |
1333 | al = SSL_AD_ILLEGAL_PARAMETER; | |
b9908bf9 | 1334 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED); |
0f113f3e MC |
1335 | goto f_err; |
1336 | } | |
0f113f3e MC |
1337 | |
1338 | sk = ssl_get_ciphers_by_id(s); | |
1339 | i = sk_SSL_CIPHER_find(sk, c); | |
1340 | if (i < 0) { | |
1341 | /* we did not say we would use this cipher */ | |
1342 | al = SSL_AD_ILLEGAL_PARAMETER; | |
b9908bf9 | 1343 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED); |
0f113f3e MC |
1344 | goto f_err; |
1345 | } | |
1346 | ||
1347 | /* | |
1348 | * Depending on the session caching (internal/external), the cipher | |
1349 | * and/or cipher_id values may not be set. Make sure that cipher_id is | |
1350 | * set and use it for comparison. | |
1351 | */ | |
1352 | if (s->session->cipher) | |
1353 | s->session->cipher_id = s->session->cipher->id; | |
1354 | if (s->hit && (s->session->cipher_id != c->id)) { | |
9e9858d1 | 1355 | al = SSL_AD_ILLEGAL_PARAMETER; |
b9908bf9 | 1356 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
9e9858d1 RS |
1357 | SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); |
1358 | goto f_err; | |
0f113f3e MC |
1359 | } |
1360 | s->s3->tmp.new_cipher = c; | |
1361 | /* | |
1362 | * Don't digest cached records if no sigalgs: we may need them for client | |
1363 | * authentication. | |
1364 | */ | |
124037fd | 1365 | if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s, 0)) |
0f113f3e MC |
1366 | goto f_err; |
1367 | /* lets get the compression algorithm */ | |
1368 | /* COMPRESSION */ | |
73999b62 | 1369 | if (!PACKET_get_1(pkt, &compression)) { |
f0659bdb | 1370 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); |
50932c4a MC |
1371 | al = SSL_AD_DECODE_ERROR; |
1372 | goto f_err; | |
1373 | } | |
09b6c2ef | 1374 | #ifdef OPENSSL_NO_COMP |
fc5ce51d | 1375 | if (compression != 0) { |
0f113f3e | 1376 | al = SSL_AD_ILLEGAL_PARAMETER; |
b9908bf9 | 1377 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
0f113f3e MC |
1378 | SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
1379 | goto f_err; | |
1380 | } | |
1381 | /* | |
1382 | * If compression is disabled we'd better not try to resume a session | |
1383 | * using compression. | |
1384 | */ | |
1385 | if (s->session->compress_meth != 0) { | |
b9908bf9 | 1386 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION); |
0f113f3e MC |
1387 | goto f_err; |
1388 | } | |
09b6c2ef | 1389 | #else |
fc5ce51d | 1390 | if (s->hit && compression != s->session->compress_meth) { |
0f113f3e | 1391 | al = SSL_AD_ILLEGAL_PARAMETER; |
b9908bf9 | 1392 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
0f113f3e MC |
1393 | SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED); |
1394 | goto f_err; | |
1395 | } | |
fc5ce51d | 1396 | if (compression == 0) |
0f113f3e MC |
1397 | comp = NULL; |
1398 | else if (!ssl_allow_compression(s)) { | |
1399 | al = SSL_AD_ILLEGAL_PARAMETER; | |
b9908bf9 | 1400 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED); |
0f113f3e | 1401 | goto f_err; |
fc5ce51d EK |
1402 | } else { |
1403 | comp = ssl3_comp_find(s->ctx->comp_methods, compression); | |
1404 | } | |
0f113f3e | 1405 | |
fc5ce51d | 1406 | if (compression != 0 && comp == NULL) { |
0f113f3e | 1407 | al = SSL_AD_ILLEGAL_PARAMETER; |
b9908bf9 | 1408 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
0f113f3e MC |
1409 | SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
1410 | goto f_err; | |
1411 | } else { | |
1412 | s->s3->tmp.new_compression = comp; | |
1413 | } | |
09b6c2ef | 1414 | #endif |
761772d7 | 1415 | |
0f113f3e | 1416 | /* TLS extensions */ |
73999b62 | 1417 | if (!ssl_parse_serverhello_tlsext(s, pkt)) { |
b9908bf9 | 1418 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_PARSE_TLSEXT); |
0f113f3e MC |
1419 | goto err; |
1420 | } | |
0f113f3e | 1421 | |
73999b62 | 1422 | if (PACKET_remaining(pkt) != 0) { |
0f113f3e MC |
1423 | /* wrong packet length */ |
1424 | al = SSL_AD_DECODE_ERROR; | |
b9908bf9 | 1425 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH); |
0f113f3e MC |
1426 | goto f_err; |
1427 | } | |
1428 | ||
8723588e MC |
1429 | #ifndef OPENSSL_NO_SCTP |
1430 | if (SSL_IS_DTLS(s) && s->hit) { | |
1431 | unsigned char sctpauthkey[64]; | |
1432 | char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)]; | |
1433 | ||
1434 | /* | |
1435 | * Add new shared key for SCTP-Auth, will be ignored if | |
1436 | * no SCTP used. | |
1437 | */ | |
141eb8c6 MC |
1438 | memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL, |
1439 | sizeof(DTLS1_SCTP_AUTH_LABEL)); | |
8723588e MC |
1440 | |
1441 | if (SSL_export_keying_material(s, sctpauthkey, | |
1442 | sizeof(sctpauthkey), | |
1443 | labelbuffer, | |
1444 | sizeof(labelbuffer), NULL, 0, | |
1445 | 0) <= 0) | |
1446 | goto err; | |
1447 | ||
1448 | BIO_ctrl(SSL_get_wbio(s), | |
1449 | BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY, | |
1450 | sizeof(sctpauthkey), sctpauthkey); | |
1451 | } | |
1452 | #endif | |
1453 | ||
b9908bf9 | 1454 | return MSG_PROCESS_CONTINUE_READING; |
0f113f3e MC |
1455 | f_err: |
1456 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
1457 | err: | |
fe3a3291 | 1458 | ossl_statem_set_error(s); |
b9908bf9 | 1459 | return MSG_PROCESS_ERROR; |
0f113f3e | 1460 | } |
d02b48c6 | 1461 | |
be3583fa | 1462 | MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt) |
b9908bf9 MC |
1463 | { |
1464 | int al, i, ret = MSG_PROCESS_ERROR, exp_idx; | |
1465 | unsigned long cert_list_len, cert_len; | |
1466 | X509 *x = NULL; | |
1467 | unsigned char *certstart, *certbytes; | |
1468 | STACK_OF(X509) *sk = NULL; | |
1469 | EVP_PKEY *pkey = NULL; | |
0f113f3e MC |
1470 | |
1471 | if ((sk = sk_X509_new_null()) == NULL) { | |
b9908bf9 | 1472 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE); |
cc273a93 | 1473 | goto err; |
0f113f3e MC |
1474 | } |
1475 | ||
73999b62 MC |
1476 | if (!PACKET_get_net_3(pkt, &cert_list_len) |
1477 | || PACKET_remaining(pkt) != cert_list_len) { | |
0f113f3e | 1478 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 1479 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH); |
0f113f3e MC |
1480 | goto f_err; |
1481 | } | |
73999b62 MC |
1482 | while (PACKET_remaining(pkt)) { |
1483 | if (!PACKET_get_net_3(pkt, &cert_len) | |
1484 | || !PACKET_get_bytes(pkt, &certbytes, cert_len)) { | |
0f113f3e | 1485 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 1486 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
0f113f3e MC |
1487 | SSL_R_CERT_LENGTH_MISMATCH); |
1488 | goto f_err; | |
1489 | } | |
1490 | ||
df758a85 MC |
1491 | certstart = certbytes; |
1492 | x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len); | |
0f113f3e MC |
1493 | if (x == NULL) { |
1494 | al = SSL_AD_BAD_CERTIFICATE; | |
b9908bf9 | 1495 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_ASN1_LIB); |
0f113f3e MC |
1496 | goto f_err; |
1497 | } | |
df758a85 | 1498 | if (certbytes != (certstart + cert_len)) { |
0f113f3e | 1499 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 1500 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
0f113f3e MC |
1501 | SSL_R_CERT_LENGTH_MISMATCH); |
1502 | goto f_err; | |
1503 | } | |
1504 | if (!sk_X509_push(sk, x)) { | |
b9908bf9 | 1505 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE); |
cc273a93 | 1506 | goto err; |
0f113f3e MC |
1507 | } |
1508 | x = NULL; | |
0f113f3e MC |
1509 | } |
1510 | ||
1511 | i = ssl_verify_cert_chain(s, sk); | |
55a9a16f | 1512 | if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) { |
0f113f3e | 1513 | al = ssl_verify_alarm_type(s->verify_result); |
b9908bf9 | 1514 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
0f113f3e MC |
1515 | SSL_R_CERTIFICATE_VERIFY_FAILED); |
1516 | goto f_err; | |
1517 | } | |
1518 | ERR_clear_error(); /* but we keep s->verify_result */ | |
1519 | if (i > 1) { | |
b9908bf9 | 1520 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i); |
0f113f3e MC |
1521 | al = SSL_AD_HANDSHAKE_FAILURE; |
1522 | goto f_err; | |
1523 | } | |
1524 | ||
c34b0f99 | 1525 | s->session->peer_chain = sk; |
0f113f3e MC |
1526 | /* |
1527 | * Inconsistency alert: cert_chain does include the peer's certificate, | |
1528 | * which we don't include in s3_srvr.c | |
1529 | */ | |
1530 | x = sk_X509_value(sk, 0); | |
1531 | sk = NULL; | |
1532 | /* | |
1533 | * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end | |
1534 | */ | |
1535 | ||
1536 | pkey = X509_get_pubkey(x); | |
1537 | ||
55a9a16f | 1538 | if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) { |
0f113f3e MC |
1539 | x = NULL; |
1540 | al = SSL3_AL_FATAL; | |
b9908bf9 | 1541 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
0f113f3e MC |
1542 | SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS); |
1543 | goto f_err; | |
1544 | } | |
1545 | ||
1546 | i = ssl_cert_type(x, pkey); | |
55a9a16f | 1547 | if (i < 0) { |
0f113f3e MC |
1548 | x = NULL; |
1549 | al = SSL3_AL_FATAL; | |
b9908bf9 | 1550 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
0f113f3e MC |
1551 | SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
1552 | goto f_err; | |
1553 | } | |
1554 | ||
55a9a16f MC |
1555 | exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher); |
1556 | if (exp_idx >= 0 && i != exp_idx) { | |
1557 | x = NULL; | |
1558 | al = SSL_AD_ILLEGAL_PARAMETER; | |
b9908bf9 | 1559 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
55a9a16f MC |
1560 | SSL_R_WRONG_CERTIFICATE_TYPE); |
1561 | goto f_err; | |
0f113f3e | 1562 | } |
a273c6ee | 1563 | s->session->peer_type = i; |
55a9a16f MC |
1564 | |
1565 | X509_free(s->session->peer); | |
05f0fb9f | 1566 | X509_up_ref(x); |
55a9a16f | 1567 | s->session->peer = x; |
0f113f3e MC |
1568 | s->session->verify_result = s->verify_result; |
1569 | ||
1570 | x = NULL; | |
b9908bf9 | 1571 | ret = MSG_PROCESS_CONTINUE_READING; |
66696478 RS |
1572 | goto done; |
1573 | ||
0f113f3e | 1574 | f_err: |
66696478 | 1575 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
cc273a93 | 1576 | err: |
fe3a3291 | 1577 | ossl_statem_set_error(s); |
66696478 | 1578 | done: |
0f113f3e MC |
1579 | EVP_PKEY_free(pkey); |
1580 | X509_free(x); | |
1581 | sk_X509_pop_free(sk, X509_free); | |
b9908bf9 | 1582 | return ret; |
0f113f3e | 1583 | } |
d02b48c6 | 1584 | |
be3583fa | 1585 | MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt) |
b9908bf9 MC |
1586 | { |
1587 | #ifndef OPENSSL_NO_RSA | |
1588 | unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2]; | |
1589 | #endif | |
1590 | EVP_MD_CTX md_ctx; | |
1591 | int al, j, verify_ret; | |
1592 | long alg_k, alg_a; | |
1593 | EVP_PKEY *pkey = NULL; | |
1594 | const EVP_MD *md = NULL; | |
1595 | #ifndef OPENSSL_NO_RSA | |
1596 | RSA *rsa = NULL; | |
1597 | #endif | |
1598 | #ifndef OPENSSL_NO_DH | |
1599 | DH *dh = NULL; | |
1600 | #endif | |
1601 | #ifndef OPENSSL_NO_EC | |
1602 | EC_KEY *ecdh = NULL; | |
1603 | BN_CTX *bn_ctx = NULL; | |
1604 | EC_POINT *srvr_ecpoint = NULL; | |
1605 | int curve_nid = 0; | |
1606 | #endif | |
73999b62 | 1607 | PACKET save_param_start, signature; |
b9908bf9 MC |
1608 | |
1609 | EVP_MD_CTX_init(&md_ctx); | |
1610 | ||
1611 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; | |
1612 | ||
73999b62 | 1613 | save_param_start = *pkt; |
8d92c1f8 | 1614 | |
bc36ee62 | 1615 | #ifndef OPENSSL_NO_RSA |
8d92c1f8 DSH |
1616 | RSA_free(s->s3->peer_rsa_tmp); |
1617 | s->s3->peer_rsa_tmp = NULL; | |
d02b48c6 | 1618 | #endif |
bc36ee62 | 1619 | #ifndef OPENSSL_NO_DH |
8d92c1f8 DSH |
1620 | DH_free(s->s3->peer_dh_tmp); |
1621 | s->s3->peer_dh_tmp = NULL; | |
ea262260 | 1622 | #endif |
10bf4fc2 | 1623 | #ifndef OPENSSL_NO_EC |
8d92c1f8 DSH |
1624 | EC_KEY_free(s->s3->peer_ecdh_tmp); |
1625 | s->s3->peer_ecdh_tmp = NULL; | |
d02b48c6 | 1626 | #endif |
8d92c1f8 | 1627 | |
0f113f3e | 1628 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
d02b48c6 | 1629 | |
0f113f3e | 1630 | al = SSL_AD_DECODE_ERROR; |
f2be92b9 | 1631 | |
ddac1974 | 1632 | #ifndef OPENSSL_NO_PSK |
7689082b DSH |
1633 | /* PSK ciphersuites are preceded by an identity hint */ |
1634 | if (alg_k & SSL_PSK) { | |
32942870 | 1635 | PACKET psk_identity_hint; |
73999b62 | 1636 | if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) { |
f0659bdb | 1637 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH); |
0f113f3e MC |
1638 | goto f_err; |
1639 | } | |
0f113f3e MC |
1640 | |
1641 | /* | |
1642 | * Store PSK identity hint for later use, hint is used in | |
1643 | * ssl3_send_client_key_exchange. Assume that the maximum length of | |
1644 | * a PSK identity hint can be as long as the maximum length of a PSK | |
1645 | * identity. | |
1646 | */ | |
32942870 | 1647 | if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) { |
0f113f3e | 1648 | al = SSL_AD_HANDSHAKE_FAILURE; |
b9908bf9 | 1649 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG); |
0f113f3e MC |
1650 | goto f_err; |
1651 | } | |
8098fc56 | 1652 | |
32942870 EK |
1653 | if (!PACKET_strndup(&psk_identity_hint, |
1654 | &s->session->psk_identity_hint)) { | |
1655 | al = SSL_AD_INTERNAL_ERROR; | |
1656 | goto f_err; | |
0f113f3e | 1657 | } |
7689082b DSH |
1658 | } |
1659 | ||
1660 | /* Nothing else to do for plain PSK or RSAPSK */ | |
1661 | if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) { | |
0f113f3e MC |
1662 | } else |
1663 | #endif /* !OPENSSL_NO_PSK */ | |
edc032b5 | 1664 | #ifndef OPENSSL_NO_SRP |
0f113f3e | 1665 | if (alg_k & SSL_kSRP) { |
32942870 | 1666 | PACKET prime, generator, salt, server_pub; |
73999b62 MC |
1667 | if (!PACKET_get_length_prefixed_2(pkt, &prime) |
1668 | || !PACKET_get_length_prefixed_2(pkt, &generator) | |
1669 | || !PACKET_get_length_prefixed_1(pkt, &salt) | |
1670 | || !PACKET_get_length_prefixed_2(pkt, &server_pub)) { | |
f0659bdb | 1671 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH); |
0f113f3e MC |
1672 | goto f_err; |
1673 | } | |
0f113f3e | 1674 | |
32942870 EK |
1675 | if ((s->srp_ctx.N = |
1676 | BN_bin2bn(PACKET_data(&prime), | |
1677 | PACKET_remaining(&prime), NULL)) == NULL | |
1678 | || (s->srp_ctx.g = | |
1679 | BN_bin2bn(PACKET_data(&generator), | |
1680 | PACKET_remaining(&generator), NULL)) == NULL | |
1681 | || (s->srp_ctx.s = | |
1682 | BN_bin2bn(PACKET_data(&salt), | |
1683 | PACKET_remaining(&salt), NULL)) == NULL | |
1684 | || (s->srp_ctx.B = | |
1685 | BN_bin2bn(PACKET_data(&server_pub), | |
1686 | PACKET_remaining(&server_pub), NULL)) == NULL) { | |
f0659bdb | 1687 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB); |
0f113f3e MC |
1688 | goto err; |
1689 | } | |
0f113f3e MC |
1690 | |
1691 | if (!srp_verify_server_param(s, &al)) { | |
b9908bf9 | 1692 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS); |
0f113f3e MC |
1693 | goto f_err; |
1694 | } | |
0989790b | 1695 | |
edc032b5 | 1696 | /* We must check if there is a certificate */ |
8df53b7a | 1697 | if (alg_a & (SSL_aRSA|SSL_aDSS)) |
a273c6ee | 1698 | pkey = X509_get_pubkey(s->session->peer); |
0f113f3e MC |
1699 | } else |
1700 | #endif /* !OPENSSL_NO_SRP */ | |
edc032b5 | 1701 | #ifndef OPENSSL_NO_RSA |
0f113f3e | 1702 | if (alg_k & SSL_kRSA) { |
32942870 | 1703 | PACKET mod, exp; |
0f113f3e MC |
1704 | /* Temporary RSA keys only allowed in export ciphersuites */ |
1705 | if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) { | |
1706 | al = SSL_AD_UNEXPECTED_MESSAGE; | |
b9908bf9 | 1707 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); |
0f113f3e MC |
1708 | goto f_err; |
1709 | } | |
0f113f3e | 1710 | |
73999b62 MC |
1711 | if (!PACKET_get_length_prefixed_2(pkt, &mod) |
1712 | || !PACKET_get_length_prefixed_2(pkt, &exp)) { | |
f0659bdb | 1713 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH); |
0f113f3e MC |
1714 | goto f_err; |
1715 | } | |
0f113f3e | 1716 | |
32942870 | 1717 | if ((rsa = RSA_new()) == NULL) { |
f0659bdb | 1718 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
1719 | goto err; |
1720 | } | |
0f113f3e | 1721 | |
32942870 EK |
1722 | if ((rsa->n = BN_bin2bn(PACKET_data(&mod), PACKET_remaining(&mod), |
1723 | rsa->n)) == NULL | |
1724 | || (rsa->e = BN_bin2bn(PACKET_data(&exp), PACKET_remaining(&exp), | |
1725 | rsa->e)) == NULL) { | |
f0659bdb | 1726 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB); |
0f113f3e MC |
1727 | goto err; |
1728 | } | |
0f113f3e MC |
1729 | |
1730 | /* this should be because we are using an export cipher */ | |
1731 | if (alg_a & SSL_aRSA) | |
a273c6ee | 1732 | pkey = X509_get_pubkey(s->session->peer); |
0f113f3e | 1733 | else { |
b9908bf9 | 1734 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
0f113f3e MC |
1735 | goto err; |
1736 | } | |
1dece951 | 1737 | |
8d92c1f8 | 1738 | s->s3->peer_rsa_tmp = rsa; |
0f113f3e MC |
1739 | rsa = NULL; |
1740 | } | |
1741 | #else /* OPENSSL_NO_RSA */ | |
1742 | if (0) ; | |
d02b48c6 | 1743 | #endif |
bc36ee62 | 1744 | #ifndef OPENSSL_NO_DH |
7689082b | 1745 | else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) { |
32942870 | 1746 | PACKET prime, generator, pub_key; |
0f113f3e | 1747 | |
73999b62 MC |
1748 | if (!PACKET_get_length_prefixed_2(pkt, &prime) |
1749 | || !PACKET_get_length_prefixed_2(pkt, &generator) | |
1750 | || !PACKET_get_length_prefixed_2(pkt, &pub_key)) { | |
f0659bdb | 1751 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH); |
0f113f3e MC |
1752 | goto f_err; |
1753 | } | |
0f113f3e | 1754 | |
32942870 | 1755 | if ((dh = DH_new()) == NULL) { |
f0659bdb | 1756 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_DH_LIB); |
0f113f3e MC |
1757 | goto err; |
1758 | } | |
0f113f3e | 1759 | |
32942870 EK |
1760 | if ((dh->p = BN_bin2bn(PACKET_data(&prime), |
1761 | PACKET_remaining(&prime), NULL)) == NULL | |
1762 | || (dh->g = BN_bin2bn(PACKET_data(&generator), | |
1763 | PACKET_remaining(&generator), NULL)) == NULL | |
1764 | || (dh->pub_key = | |
1765 | BN_bin2bn(PACKET_data(&pub_key), | |
1766 | PACKET_remaining(&pub_key), NULL)) == NULL) { | |
f0659bdb | 1767 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB); |
0f113f3e MC |
1768 | goto err; |
1769 | } | |
0f113f3e | 1770 | |
32942870 | 1771 | if (BN_is_zero(dh->p) || BN_is_zero(dh->g) || BN_is_zero(dh->pub_key)) { |
f0659bdb | 1772 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_DH_VALUE); |
61e72d76 GL |
1773 | goto f_err; |
1774 | } | |
1775 | ||
0f113f3e MC |
1776 | if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) { |
1777 | al = SSL_AD_HANDSHAKE_FAILURE; | |
b9908bf9 | 1778 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL); |
0f113f3e MC |
1779 | goto f_err; |
1780 | } | |
8df53b7a | 1781 | if (alg_a & (SSL_aRSA|SSL_aDSS)) |
a273c6ee | 1782 | pkey = X509_get_pubkey(s->session->peer); |
0f113f3e MC |
1783 | /* else anonymous DH, so no certificate or pkey. */ |
1784 | ||
8d92c1f8 | 1785 | s->s3->peer_dh_tmp = dh; |
0f113f3e | 1786 | dh = NULL; |
0f113f3e MC |
1787 | } |
1788 | #endif /* !OPENSSL_NO_DH */ | |
ea262260 | 1789 | |
10bf4fc2 | 1790 | #ifndef OPENSSL_NO_EC |
7689082b | 1791 | else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) { |
0f113f3e MC |
1792 | EC_GROUP *ngroup; |
1793 | const EC_GROUP *group; | |
32942870 EK |
1794 | PACKET encoded_pt; |
1795 | unsigned char *ecparams; | |
0f113f3e MC |
1796 | |
1797 | if ((ecdh = EC_KEY_new()) == NULL) { | |
b9908bf9 | 1798 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
1799 | goto err; |
1800 | } | |
1801 | ||
1802 | /* | |
1803 | * Extract elliptic curve parameters and the server's ephemeral ECDH | |
32942870 | 1804 | * public key. For now we only support named (not generic) curves and |
8098fc56 | 1805 | * ECParameters in this case is just three bytes. |
0f113f3e | 1806 | */ |
73999b62 | 1807 | if (!PACKET_get_bytes(pkt, &ecparams, 3)) { |
f0659bdb | 1808 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); |
0f113f3e MC |
1809 | goto f_err; |
1810 | } | |
1811 | /* | |
1812 | * Check curve is one of our preferences, if not server has sent an | |
1813 | * invalid curve. ECParameters is 3 bytes. | |
1814 | */ | |
32942870 | 1815 | if (!tls1_check_curve(s, ecparams, 3)) { |
f0659bdb | 1816 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_CURVE); |
0f113f3e MC |
1817 | goto f_err; |
1818 | } | |
1819 | ||
32942870 | 1820 | if ((curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2))) == 0) { |
0f113f3e | 1821 | al = SSL_AD_INTERNAL_ERROR; |
b9908bf9 | 1822 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, |
0f113f3e MC |
1823 | SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS); |
1824 | goto f_err; | |
1825 | } | |
1826 | ||
1827 | ngroup = EC_GROUP_new_by_curve_name(curve_nid); | |
1828 | if (ngroup == NULL) { | |
b9908bf9 | 1829 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EC_LIB); |
0f113f3e MC |
1830 | goto err; |
1831 | } | |
1832 | if (EC_KEY_set_group(ecdh, ngroup) == 0) { | |
b9908bf9 | 1833 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EC_LIB); |
0f113f3e MC |
1834 | goto err; |
1835 | } | |
1836 | EC_GROUP_free(ngroup); | |
1837 | ||
1838 | group = EC_KEY_get0_group(ecdh); | |
1839 | ||
1840 | if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && | |
1841 | (EC_GROUP_get_degree(group) > 163)) { | |
1842 | al = SSL_AD_EXPORT_RESTRICTION; | |
b9908bf9 | 1843 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, |
0f113f3e MC |
1844 | SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER); |
1845 | goto f_err; | |
1846 | } | |
1847 | ||
0f113f3e MC |
1848 | /* Next, get the encoded ECPoint */ |
1849 | if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) || | |
1850 | ((bn_ctx = BN_CTX_new()) == NULL)) { | |
b9908bf9 | 1851 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
1852 | goto err; |
1853 | } | |
1854 | ||
73999b62 | 1855 | if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) { |
f0659bdb | 1856 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH); |
8098fc56 MC |
1857 | goto f_err; |
1858 | } | |
0f113f3e | 1859 | |
32942870 EK |
1860 | if (EC_POINT_oct2point(group, srvr_ecpoint, PACKET_data(&encoded_pt), |
1861 | PACKET_remaining(&encoded_pt), bn_ctx) == 0) { | |
f0659bdb | 1862 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_ECPOINT); |
0f113f3e MC |
1863 | goto f_err; |
1864 | } | |
0f113f3e MC |
1865 | |
1866 | /* | |
1867 | * The ECC/TLS specification does not mention the use of DSA to sign | |
1868 | * ECParameters in the server key exchange message. We do support RSA | |
1869 | * and ECDSA. | |
1870 | */ | |
1871 | if (0) ; | |
1872 | # ifndef OPENSSL_NO_RSA | |
1873 | else if (alg_a & SSL_aRSA) | |
a273c6ee | 1874 | pkey = X509_get_pubkey(s->session->peer); |
0f113f3e | 1875 | # endif |
10bf4fc2 | 1876 | # ifndef OPENSSL_NO_EC |
0f113f3e | 1877 | else if (alg_a & SSL_aECDSA) |
a273c6ee | 1878 | pkey = X509_get_pubkey(s->session->peer); |
0f113f3e MC |
1879 | # endif |
1880 | /* else anonymous ECDH, so no certificate or pkey. */ | |
1881 | EC_KEY_set_public_key(ecdh, srvr_ecpoint); | |
8d92c1f8 | 1882 | s->s3->peer_ecdh_tmp = ecdh; |
0f113f3e MC |
1883 | ecdh = NULL; |
1884 | BN_CTX_free(bn_ctx); | |
1885 | bn_ctx = NULL; | |
1886 | EC_POINT_free(srvr_ecpoint); | |
1887 | srvr_ecpoint = NULL; | |
1888 | } else if (alg_k) { | |
1889 | al = SSL_AD_UNEXPECTED_MESSAGE; | |
b9908bf9 | 1890 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); |
0f113f3e MC |
1891 | goto f_err; |
1892 | } | |
10bf4fc2 | 1893 | #endif /* !OPENSSL_NO_EC */ |
0f113f3e | 1894 | |
0f113f3e MC |
1895 | /* if it was signed, check the signature */ |
1896 | if (pkey != NULL) { | |
32942870 EK |
1897 | PACKET params; |
1898 | /* | |
1899 | * |pkt| now points to the beginning of the signature, so the difference | |
1900 | * equals the length of the parameters. | |
1901 | */ | |
1902 | if (!PACKET_get_sub_packet(&save_param_start, ¶ms, | |
1903 | PACKET_remaining(&save_param_start) - | |
73999b62 | 1904 | PACKET_remaining(pkt))) { |
32942870 | 1905 | al = SSL_AD_INTERNAL_ERROR; |
f0659bdb | 1906 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
32942870 EK |
1907 | goto f_err; |
1908 | } | |
1909 | ||
0f113f3e | 1910 | if (SSL_USE_SIGALGS(s)) { |
32942870 | 1911 | unsigned char *sigalgs; |
0f113f3e | 1912 | int rv; |
73999b62 | 1913 | if (!PACKET_get_bytes(pkt, &sigalgs, 2)) { |
f0659bdb | 1914 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); |
0f113f3e MC |
1915 | goto f_err; |
1916 | } | |
32942870 | 1917 | rv = tls12_check_peer_sigalg(&md, s, sigalgs, pkey); |
0f113f3e MC |
1918 | if (rv == -1) |
1919 | goto err; | |
1920 | else if (rv == 0) { | |
1921 | goto f_err; | |
1922 | } | |
a2f9200f | 1923 | #ifdef SSL_DEBUG |
0f113f3e MC |
1924 | fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md)); |
1925 | #endif | |
32942870 | 1926 | } else { |
0f113f3e | 1927 | md = EVP_sha1(); |
32942870 | 1928 | } |
0f113f3e | 1929 | |
73999b62 MC |
1930 | if (!PACKET_get_length_prefixed_2(pkt, &signature) |
1931 | || PACKET_remaining(pkt) != 0) { | |
f0659bdb | 1932 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH); |
0f113f3e MC |
1933 | goto f_err; |
1934 | } | |
0f113f3e | 1935 | j = EVP_PKEY_size(pkey); |
8098fc56 | 1936 | if (j < 0) { |
b9908bf9 | 1937 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
8098fc56 MC |
1938 | goto f_err; |
1939 | } | |
0f113f3e MC |
1940 | |
1941 | /* | |
8098fc56 | 1942 | * Check signature length |
0f113f3e | 1943 | */ |
32942870 | 1944 | if (PACKET_remaining(&signature) > (size_t)j) { |
0f113f3e | 1945 | /* wrong packet length */ |
b9908bf9 | 1946 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH); |
0f113f3e MC |
1947 | goto f_err; |
1948 | } | |
bc36ee62 | 1949 | #ifndef OPENSSL_NO_RSA |
0f113f3e MC |
1950 | if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) { |
1951 | int num; | |
1952 | unsigned int size; | |
1953 | ||
1954 | j = 0; | |
1955 | q = md_buf; | |
1956 | for (num = 2; num > 0; num--) { | |
1957 | EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); | |
1958 | EVP_DigestInit_ex(&md_ctx, (num == 2) | |
1959 | ? s->ctx->md5 : s->ctx->sha1, NULL); | |
1960 | EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]), | |
1961 | SSL3_RANDOM_SIZE); | |
1962 | EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]), | |
1963 | SSL3_RANDOM_SIZE); | |
32942870 EK |
1964 | EVP_DigestUpdate(&md_ctx, PACKET_data(¶ms), |
1965 | PACKET_remaining(¶ms)); | |
0f113f3e MC |
1966 | EVP_DigestFinal_ex(&md_ctx, q, &size); |
1967 | q += size; | |
1968 | j += size; | |
1969 | } | |
8098fc56 | 1970 | verify_ret = |
32942870 EK |
1971 | RSA_verify(NID_md5_sha1, md_buf, j, PACKET_data(&signature), |
1972 | PACKET_remaining(&signature), pkey->pkey.rsa); | |
8098fc56 | 1973 | if (verify_ret < 0) { |
0f113f3e | 1974 | al = SSL_AD_DECRYPT_ERROR; |
b9908bf9 | 1975 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT); |
0f113f3e MC |
1976 | goto f_err; |
1977 | } | |
8098fc56 | 1978 | if (verify_ret == 0) { |
0f113f3e MC |
1979 | /* bad signature */ |
1980 | al = SSL_AD_DECRYPT_ERROR; | |
b9908bf9 | 1981 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE); |
0f113f3e MC |
1982 | goto f_err; |
1983 | } | |
1984 | } else | |
1985 | #endif | |
1986 | { | |
1987 | EVP_VerifyInit_ex(&md_ctx, md, NULL); | |
1988 | EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]), | |
1989 | SSL3_RANDOM_SIZE); | |
1990 | EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]), | |
1991 | SSL3_RANDOM_SIZE); | |
32942870 EK |
1992 | EVP_VerifyUpdate(&md_ctx, PACKET_data(¶ms), |
1993 | PACKET_remaining(¶ms)); | |
1994 | if (EVP_VerifyFinal(&md_ctx, PACKET_data(&signature), | |
1995 | PACKET_remaining(&signature), pkey) <= 0) { | |
0f113f3e MC |
1996 | /* bad signature */ |
1997 | al = SSL_AD_DECRYPT_ERROR; | |
b9908bf9 | 1998 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE); |
0f113f3e MC |
1999 | goto f_err; |
2000 | } | |
2001 | } | |
2002 | } else { | |
7689082b DSH |
2003 | /* aNULL, aSRP or PSK do not need public keys */ |
2004 | if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_PSK)) { | |
0f113f3e MC |
2005 | /* Might be wrong key type, check it */ |
2006 | if (ssl3_check_cert_and_algorithm(s)) | |
2007 | /* Otherwise this shouldn't happen */ | |
b9908bf9 | 2008 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
0f113f3e MC |
2009 | goto err; |
2010 | } | |
2011 | /* still data left over */ | |
73999b62 | 2012 | if (PACKET_remaining(pkt) != 0) { |
b9908bf9 | 2013 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE); |
0f113f3e MC |
2014 | goto f_err; |
2015 | } | |
2016 | } | |
2017 | EVP_PKEY_free(pkey); | |
2018 | EVP_MD_CTX_cleanup(&md_ctx); | |
b9908bf9 | 2019 | return MSG_PROCESS_CONTINUE_READING; |
0f113f3e MC |
2020 | f_err: |
2021 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
2022 | err: | |
2023 | EVP_PKEY_free(pkey); | |
bc36ee62 | 2024 | #ifndef OPENSSL_NO_RSA |
d6407083 | 2025 | RSA_free(rsa); |
6b521df3 | 2026 | #endif |
bc36ee62 | 2027 | #ifndef OPENSSL_NO_DH |
d6407083 | 2028 | DH_free(dh); |
ea262260 | 2029 | #endif |
10bf4fc2 | 2030 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
2031 | BN_CTX_free(bn_ctx); |
2032 | EC_POINT_free(srvr_ecpoint); | |
8fdc3734 | 2033 | EC_KEY_free(ecdh); |
6b521df3 | 2034 | #endif |
0f113f3e | 2035 | EVP_MD_CTX_cleanup(&md_ctx); |
fe3a3291 | 2036 | ossl_statem_set_error(s); |
b9908bf9 | 2037 | return MSG_PROCESS_ERROR; |
0f113f3e | 2038 | } |
d02b48c6 | 2039 | |
be3583fa | 2040 | MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt) |
b9908bf9 MC |
2041 | { |
2042 | int ret = MSG_PROCESS_ERROR; | |
2043 | unsigned int list_len, ctype_num, i, name_len; | |
2044 | X509_NAME *xn = NULL; | |
2045 | unsigned char *data; | |
2046 | unsigned char *namestart, *namebytes; | |
2047 | STACK_OF(X509_NAME) *ca_sk = NULL; | |
0f113f3e MC |
2048 | |
2049 | if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) { | |
b9908bf9 | 2050 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
2051 | goto err; |
2052 | } | |
2053 | ||
2054 | /* get the certificate types */ | |
73999b62 MC |
2055 | if (!PACKET_get_1(pkt, &ctype_num) |
2056 | || !PACKET_get_bytes(pkt, &data, ctype_num)) { | |
ac112332 | 2057 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 | 2058 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH); |
ac112332 MC |
2059 | goto err; |
2060 | } | |
b548a1f1 RS |
2061 | OPENSSL_free(s->cert->ctypes); |
2062 | s->cert->ctypes = NULL; | |
0f113f3e MC |
2063 | if (ctype_num > SSL3_CT_NUMBER) { |
2064 | /* If we exceed static buffer copy all to cert structure */ | |
2065 | s->cert->ctypes = OPENSSL_malloc(ctype_num); | |
2066 | if (s->cert->ctypes == NULL) { | |
b9908bf9 | 2067 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
2068 | goto err; |
2069 | } | |
ac112332 | 2070 | memcpy(s->cert->ctypes, data, ctype_num); |
0f113f3e MC |
2071 | s->cert->ctype_num = (size_t)ctype_num; |
2072 | ctype_num = SSL3_CT_NUMBER; | |
2073 | } | |
2074 | for (i = 0; i < ctype_num; i++) | |
ac112332 MC |
2075 | s->s3->tmp.ctype[i] = data[i]; |
2076 | ||
0f113f3e | 2077 | if (SSL_USE_SIGALGS(s)) { |
73999b62 MC |
2078 | if (!PACKET_get_net_2(pkt, &list_len) |
2079 | || !PACKET_get_bytes(pkt, &data, list_len)) { | |
0f113f3e | 2080 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 MC |
2081 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, |
2082 | SSL_R_LENGTH_MISMATCH); | |
0f113f3e MC |
2083 | goto err; |
2084 | } | |
ac112332 | 2085 | |
0f113f3e MC |
2086 | /* Clear certificate digests and validity flags */ |
2087 | for (i = 0; i < SSL_PKEY_NUM; i++) { | |
d376e57d | 2088 | s->s3->tmp.md[i] = NULL; |
6383d316 | 2089 | s->s3->tmp.valid_flags[i] = 0; |
0f113f3e | 2090 | } |
ac112332 | 2091 | if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) { |
0f113f3e | 2092 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 | 2093 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, |
0f113f3e MC |
2094 | SSL_R_SIGNATURE_ALGORITHMS_ERROR); |
2095 | goto err; | |
2096 | } | |
2097 | if (!tls1_process_sigalgs(s)) { | |
2098 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
b9908bf9 | 2099 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
2100 | goto err; |
2101 | } | |
0f113f3e MC |
2102 | } |
2103 | ||
2104 | /* get the CA RDNs */ | |
73999b62 MC |
2105 | if (!PACKET_get_net_2(pkt, &list_len) |
2106 | || PACKET_remaining(pkt) != list_len) { | |
0f113f3e | 2107 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 | 2108 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH); |
0f113f3e MC |
2109 | goto err; |
2110 | } | |
2111 | ||
73999b62 MC |
2112 | while (PACKET_remaining(pkt)) { |
2113 | if (!PACKET_get_net_2(pkt, &name_len) | |
2114 | || !PACKET_get_bytes(pkt, &namebytes, name_len)) { | |
0f113f3e | 2115 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 MC |
2116 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, |
2117 | SSL_R_LENGTH_MISMATCH); | |
0f113f3e MC |
2118 | goto err; |
2119 | } | |
2120 | ||
ac112332 | 2121 | namestart = namebytes; |
0f113f3e | 2122 | |
ac112332 MC |
2123 | if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes, |
2124 | name_len)) == NULL) { | |
3c33c6f6 | 2125 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 | 2126 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB); |
3c33c6f6 | 2127 | goto err; |
0f113f3e MC |
2128 | } |
2129 | ||
ac112332 | 2130 | if (namebytes != (namestart + name_len)) { |
0f113f3e | 2131 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 | 2132 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, |
0f113f3e MC |
2133 | SSL_R_CA_DN_LENGTH_MISMATCH); |
2134 | goto err; | |
2135 | } | |
2136 | if (!sk_X509_NAME_push(ca_sk, xn)) { | |
b9908bf9 | 2137 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
2138 | goto err; |
2139 | } | |
0f113f3e MC |
2140 | } |
2141 | ||
0f113f3e MC |
2142 | /* we should setup a certificate to return.... */ |
2143 | s->s3->tmp.cert_req = 1; | |
2144 | s->s3->tmp.ctype_num = ctype_num; | |
222561fe | 2145 | sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free); |
0f113f3e MC |
2146 | s->s3->tmp.ca_names = ca_sk; |
2147 | ca_sk = NULL; | |
2148 | ||
b9908bf9 | 2149 | ret = MSG_PROCESS_CONTINUE_READING; |
cc273a93 | 2150 | goto done; |
0f113f3e | 2151 | err: |
fe3a3291 | 2152 | ossl_statem_set_error(s); |
cc273a93 | 2153 | done: |
222561fe | 2154 | sk_X509_NAME_pop_free(ca_sk, X509_NAME_free); |
b9908bf9 | 2155 | return ret; |
0f113f3e MC |
2156 | } |
2157 | ||
2158 | static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b) | |
dfeab068 | 2159 | { |
0f113f3e | 2160 | return (X509_NAME_cmp(*a, *b)); |
dfeab068 | 2161 | } |
dfeab068 | 2162 | |
be3583fa | 2163 | MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt) |
b9908bf9 MC |
2164 | { |
2165 | int al; | |
2166 | unsigned int ticklen; | |
2167 | unsigned long ticket_lifetime_hint; | |
b9908bf9 | 2168 | |
73999b62 MC |
2169 | if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint) |
2170 | || !PACKET_get_net_2(pkt, &ticklen) | |
2171 | || PACKET_remaining(pkt) != ticklen) { | |
e711da71 | 2172 | al = SSL_AD_DECODE_ERROR; |
f0659bdb | 2173 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH); |
e711da71 EK |
2174 | goto f_err; |
2175 | } | |
2176 | ||
2177 | /* Server is allowed to change its mind and send an empty ticket. */ | |
2178 | if (ticklen == 0) | |
c9de4a20 | 2179 | return MSG_PROCESS_CONTINUE_READING; |
e711da71 | 2180 | |
98ece4ee MC |
2181 | if (s->session->session_id_length > 0) { |
2182 | int i = s->session_ctx->session_cache_mode; | |
2183 | SSL_SESSION *new_sess; | |
2184 | /* | |
2185 | * We reused an existing session, so we need to replace it with a new | |
2186 | * one | |
2187 | */ | |
2188 | if (i & SSL_SESS_CACHE_CLIENT) { | |
2189 | /* | |
2190 | * Remove the old session from the cache | |
2191 | */ | |
2192 | if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) { | |
2193 | if (s->session_ctx->remove_session_cb != NULL) | |
2194 | s->session_ctx->remove_session_cb(s->session_ctx, | |
2195 | s->session); | |
2196 | } else { | |
2197 | /* We carry on if this fails */ | |
2198 | SSL_CTX_remove_session(s->session_ctx, s->session); | |
2199 | } | |
2200 | } | |
2201 | ||
2202 | if ((new_sess = ssl_session_dup(s->session, 0)) == 0) { | |
2203 | al = SSL_AD_INTERNAL_ERROR; | |
b9908bf9 | 2204 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE); |
98ece4ee MC |
2205 | goto f_err; |
2206 | } | |
2207 | ||
2208 | SSL_SESSION_free(s->session); | |
2209 | s->session = new_sess; | |
2210 | } | |
2211 | ||
b548a1f1 RS |
2212 | OPENSSL_free(s->session->tlsext_tick); |
2213 | s->session->tlsext_ticklen = 0; | |
e711da71 | 2214 | |
0f113f3e MC |
2215 | s->session->tlsext_tick = OPENSSL_malloc(ticklen); |
2216 | if (!s->session->tlsext_tick) { | |
b9908bf9 | 2217 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
2218 | goto err; |
2219 | } | |
73999b62 | 2220 | if (!PACKET_copy_bytes(pkt, s->session->tlsext_tick, ticklen)) { |
561e12bb | 2221 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 2222 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH); |
561e12bb MC |
2223 | goto f_err; |
2224 | } | |
e711da71 EK |
2225 | |
2226 | s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint; | |
0f113f3e MC |
2227 | s->session->tlsext_ticklen = ticklen; |
2228 | /* | |
2229 | * There are two ways to detect a resumed ticket session. One is to set | |
2230 | * an appropriate session ID and then the server must return a match in | |
2231 | * ServerHello. This allows the normal client session ID matching to work | |
2232 | * and we know much earlier that the ticket has been accepted. The | |
2233 | * other way is to set zero length session ID when the ticket is | |
2234 | * presented and rely on the handshake to determine session resumption. | |
2235 | * We choose the former approach because this fits in with assumptions | |
2236 | * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is | |
2237 | * SHA256 is disabled) hash of the ticket. | |
2238 | */ | |
561e12bb | 2239 | EVP_Digest(s->session->tlsext_tick, ticklen, |
0f113f3e | 2240 | s->session->session_id, &s->session->session_id_length, |
0f113f3e | 2241 | EVP_sha256(), NULL); |
b9908bf9 | 2242 | return MSG_PROCESS_CONTINUE_READING; |
0f113f3e MC |
2243 | f_err: |
2244 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
2245 | err: | |
fe3a3291 | 2246 | ossl_statem_set_error(s); |
b9908bf9 | 2247 | return MSG_PROCESS_ERROR; |
0f113f3e | 2248 | } |
67c8e7f4 | 2249 | |
be3583fa | 2250 | MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt) |
b9908bf9 MC |
2251 | { |
2252 | int al; | |
2253 | unsigned long resplen; | |
2254 | unsigned int type; | |
b9908bf9 | 2255 | |
73999b62 | 2256 | if (!PACKET_get_1(pkt, &type) |
ac63710a | 2257 | || type != TLSEXT_STATUSTYPE_ocsp) { |
0f113f3e | 2258 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 2259 | SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE); |
0f113f3e MC |
2260 | goto f_err; |
2261 | } | |
73999b62 MC |
2262 | if (!PACKET_get_net_3(pkt, &resplen) |
2263 | || PACKET_remaining(pkt) != resplen) { | |
0f113f3e | 2264 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 2265 | SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH); |
0f113f3e MC |
2266 | goto f_err; |
2267 | } | |
b548a1f1 | 2268 | OPENSSL_free(s->tlsext_ocsp_resp); |
ac63710a | 2269 | s->tlsext_ocsp_resp = OPENSSL_malloc(resplen); |
0f113f3e MC |
2270 | if (!s->tlsext_ocsp_resp) { |
2271 | al = SSL_AD_INTERNAL_ERROR; | |
b9908bf9 | 2272 | SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
2273 | goto f_err; |
2274 | } | |
73999b62 | 2275 | if (!PACKET_copy_bytes(pkt, s->tlsext_ocsp_resp, resplen)) { |
ac63710a | 2276 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 2277 | SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH); |
ac63710a MC |
2278 | goto f_err; |
2279 | } | |
0f113f3e MC |
2280 | s->tlsext_ocsp_resplen = resplen; |
2281 | if (s->ctx->tlsext_status_cb) { | |
2282 | int ret; | |
2283 | ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg); | |
2284 | if (ret == 0) { | |
2285 | al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; | |
b9908bf9 | 2286 | SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE); |
0f113f3e MC |
2287 | goto f_err; |
2288 | } | |
2289 | if (ret < 0) { | |
2290 | al = SSL_AD_INTERNAL_ERROR; | |
b9908bf9 | 2291 | SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
2292 | goto f_err; |
2293 | } | |
2294 | } | |
b9908bf9 | 2295 | return MSG_PROCESS_CONTINUE_READING; |
0f113f3e MC |
2296 | f_err: |
2297 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
fe3a3291 | 2298 | ossl_statem_set_error(s); |
b9908bf9 | 2299 | return MSG_PROCESS_ERROR; |
0f113f3e | 2300 | } |
d02b48c6 | 2301 | |
be3583fa | 2302 | MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt) |
b9908bf9 | 2303 | { |
73999b62 | 2304 | if (PACKET_remaining(pkt) > 0) { |
0f113f3e MC |
2305 | /* should contain no data */ |
2306 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | |
b9908bf9 | 2307 | SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH); |
fe3a3291 | 2308 | ossl_statem_set_error(s); |
b9908bf9 | 2309 | return MSG_PROCESS_ERROR; |
0f113f3e | 2310 | } |
b9908bf9 MC |
2311 | |
2312 | #ifndef OPENSSL_NO_SRP | |
2313 | if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) { | |
2314 | if (SRP_Calc_A_param(s) <= 0) { | |
2315 | SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC); | |
2316 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
fe3a3291 | 2317 | ossl_statem_set_error(s); |
b9908bf9 MC |
2318 | return MSG_PROCESS_ERROR; |
2319 | } | |
2320 | } | |
2321 | #endif | |
2322 | ||
a455d0f6 MC |
2323 | /* |
2324 | * at this point we check that we have the required stuff from | |
2325 | * the server | |
2326 | */ | |
2327 | if (!ssl3_check_cert_and_algorithm(s)) { | |
2328 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | |
fe3a3291 | 2329 | ossl_statem_set_error(s); |
a455d0f6 MC |
2330 | return MSG_PROCESS_ERROR; |
2331 | } | |
2332 | ||
473483d4 MC |
2333 | #ifndef OPENSSL_NO_SCTP |
2334 | /* Only applies to renegotiation */ | |
2335 | if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s)) | |
2336 | && s->renegotiate != 0) | |
2337 | return MSG_PROCESS_CONTINUE_PROCESSING; | |
2338 | else | |
2339 | #endif | |
2340 | return MSG_PROCESS_FINISHED_READING; | |
0f113f3e | 2341 | } |
176f31dd | 2342 | |
b9908bf9 | 2343 | int tls_construct_client_key_exchange(SSL *s) |
0f113f3e MC |
2344 | { |
2345 | unsigned char *p; | |
2346 | int n; | |
7689082b DSH |
2347 | #ifndef OPENSSL_NO_PSK |
2348 | size_t pskhdrlen = 0; | |
2349 | #endif | |
0f113f3e | 2350 | unsigned long alg_k; |
bc36ee62 | 2351 | #ifndef OPENSSL_NO_RSA |
0f113f3e MC |
2352 | unsigned char *q; |
2353 | EVP_PKEY *pkey = NULL; | |
79df9d62 | 2354 | #endif |
10bf4fc2 | 2355 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
2356 | EC_KEY *clnt_ecdh = NULL; |
2357 | const EC_POINT *srvr_ecpoint = NULL; | |
2358 | EVP_PKEY *srvr_pub_pkey = NULL; | |
2359 | unsigned char *encodedPoint = NULL; | |
2360 | int encoded_pt_len = 0; | |
2361 | BN_CTX *bn_ctx = NULL; | |
ea262260 | 2362 | #endif |
c660ec63 DSH |
2363 | unsigned char *pms = NULL; |
2364 | size_t pmslen = 0; | |
7689082b | 2365 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
d02b48c6 | 2366 | |
b9908bf9 | 2367 | p = ssl_handshake_start(s); |
d02b48c6 | 2368 | |
7689082b DSH |
2369 | |
2370 | #ifndef OPENSSL_NO_PSK | |
b9908bf9 MC |
2371 | if (alg_k & SSL_PSK) { |
2372 | int psk_err = 1; | |
2373 | /* | |
2374 | * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a | |
2375 | * \0-terminated identity. The last byte is for us for simulating | |
2376 | * strnlen. | |
2377 | */ | |
2378 | char identity[PSK_MAX_IDENTITY_LEN + 1]; | |
2379 | size_t identitylen; | |
2380 | unsigned char psk[PSK_MAX_PSK_LEN]; | |
2381 | size_t psklen; | |
2382 | ||
2383 | if (s->psk_client_callback == NULL) { | |
2384 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2385 | SSL_R_PSK_NO_CLIENT_CB); | |
2386 | goto err; | |
2387 | } | |
7689082b | 2388 | |
b9908bf9 | 2389 | memset(identity, 0, sizeof(identity)); |
7689082b | 2390 | |
b9908bf9 MC |
2391 | psklen = s->psk_client_callback(s, s->session->psk_identity_hint, |
2392 | identity, sizeof(identity) - 1, | |
2393 | psk, sizeof(psk)); | |
7689082b | 2394 | |
b9908bf9 MC |
2395 | if (psklen > PSK_MAX_PSK_LEN) { |
2396 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2397 | ERR_R_INTERNAL_ERROR); | |
2398 | goto psk_err; | |
2399 | } else if (psklen == 0) { | |
2400 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2401 | SSL_R_PSK_IDENTITY_NOT_FOUND); | |
2402 | goto psk_err; | |
2403 | } | |
7689082b | 2404 | |
b9908bf9 MC |
2405 | OPENSSL_free(s->s3->tmp.psk); |
2406 | s->s3->tmp.psk = BUF_memdup(psk, psklen); | |
2407 | OPENSSL_cleanse(psk, psklen); | |
7689082b | 2408 | |
b9908bf9 MC |
2409 | if (s->s3->tmp.psk == NULL) { |
2410 | OPENSSL_cleanse(identity, sizeof(identity)); | |
2411 | goto memerr; | |
2412 | } | |
7689082b | 2413 | |
b9908bf9 | 2414 | s->s3->tmp.psklen = psklen; |
7689082b | 2415 | |
b9908bf9 MC |
2416 | identitylen = strlen(identity); |
2417 | if (identitylen > PSK_MAX_IDENTITY_LEN) { | |
2418 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2419 | ERR_R_INTERNAL_ERROR); | |
2420 | goto psk_err; | |
2421 | } | |
2422 | OPENSSL_free(s->session->psk_identity); | |
2423 | s->session->psk_identity = BUF_strdup(identity); | |
2424 | if (s->session->psk_identity == NULL) { | |
7689082b | 2425 | OPENSSL_cleanse(identity, sizeof(identity)); |
b9908bf9 | 2426 | goto memerr; |
7689082b | 2427 | } |
d02b48c6 | 2428 | |
b9908bf9 MC |
2429 | s2n(identitylen, p); |
2430 | memcpy(p, identity, identitylen); | |
2431 | pskhdrlen = 2 + identitylen; | |
2432 | p += identitylen; | |
2433 | psk_err = 0; | |
2434 | psk_err: | |
2435 | OPENSSL_cleanse(identity, sizeof(identity)); | |
2436 | if (psk_err != 0) { | |
2437 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | |
2438 | goto err; | |
0f113f3e | 2439 | } |
b9908bf9 MC |
2440 | } |
2441 | if (alg_k & SSL_kPSK) { | |
2442 | n = 0; | |
2443 | } else | |
2444 | #endif | |
2445 | ||
2446 | /* Fool emacs indentation */ | |
2447 | if (0) { | |
2448 | } | |
bc36ee62 | 2449 | #ifndef OPENSSL_NO_RSA |
b9908bf9 MC |
2450 | else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) { |
2451 | RSA *rsa; | |
2452 | pmslen = SSL_MAX_MASTER_KEY_LENGTH; | |
2453 | pms = OPENSSL_malloc(pmslen); | |
2454 | if (!pms) | |
2455 | goto memerr; | |
2456 | ||
2457 | if (s->session->peer == NULL) { | |
2458 | /* | |
2459 | * We should always have a server certificate with SSL_kRSA. | |
2460 | */ | |
2461 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2462 | ERR_R_INTERNAL_ERROR); | |
2463 | goto err; | |
2464 | } | |
0f113f3e | 2465 | |
b9908bf9 MC |
2466 | if (s->s3->peer_rsa_tmp != NULL) |
2467 | rsa = s->s3->peer_rsa_tmp; | |
2468 | else { | |
2469 | pkey = X509_get_pubkey(s->session->peer); | |
2470 | if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA) | |
2471 | || (pkey->pkey.rsa == NULL)) { | |
2472 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2473 | ERR_R_INTERNAL_ERROR); | |
0f113f3e | 2474 | EVP_PKEY_free(pkey); |
b9908bf9 | 2475 | goto err; |
0f113f3e | 2476 | } |
b9908bf9 MC |
2477 | rsa = pkey->pkey.rsa; |
2478 | EVP_PKEY_free(pkey); | |
2479 | } | |
0f113f3e | 2480 | |
b9908bf9 MC |
2481 | pms[0] = s->client_version >> 8; |
2482 | pms[1] = s->client_version & 0xff; | |
2483 | if (RAND_bytes(pms + 2, pmslen - 2) <= 0) | |
2484 | goto err; | |
0f113f3e | 2485 | |
b9908bf9 MC |
2486 | q = p; |
2487 | /* Fix buf for TLS and beyond */ | |
2488 | if (s->version > SSL3_VERSION) | |
2489 | p += 2; | |
2490 | n = RSA_public_encrypt(pmslen, pms, p, rsa, RSA_PKCS1_PADDING); | |
0f113f3e | 2491 | # ifdef PKCS1_CHECK |
b9908bf9 MC |
2492 | if (s->options & SSL_OP_PKCS1_CHECK_1) |
2493 | p[1]++; | |
2494 | if (s->options & SSL_OP_PKCS1_CHECK_2) | |
2495 | tmp_buf[0] = 0x70; | |
0f113f3e | 2496 | # endif |
b9908bf9 MC |
2497 | if (n <= 0) { |
2498 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2499 | SSL_R_BAD_RSA_ENCRYPT); | |
2500 | goto err; | |
2501 | } | |
0f113f3e | 2502 | |
b9908bf9 MC |
2503 | /* Fix buf for TLS and beyond */ |
2504 | if (s->version > SSL3_VERSION) { | |
2505 | s2n(n, q); | |
2506 | n += 2; | |
0f113f3e | 2507 | } |
b9908bf9 | 2508 | } |
f9b3bff6 | 2509 | #endif |
bc36ee62 | 2510 | #ifndef OPENSSL_NO_DH |
b9908bf9 MC |
2511 | else if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd | SSL_kDHEPSK)) { |
2512 | DH *dh_srvr, *dh_clnt; | |
2513 | if (s->s3->peer_dh_tmp != NULL) | |
2514 | dh_srvr = s->s3->peer_dh_tmp; | |
2515 | else { | |
2516 | /* we get them from the cert */ | |
2517 | EVP_PKEY *spkey = NULL; | |
2518 | dh_srvr = NULL; | |
2519 | spkey = X509_get_pubkey(s->session->peer); | |
2520 | if (spkey) { | |
2521 | dh_srvr = EVP_PKEY_get1_DH(spkey); | |
2522 | EVP_PKEY_free(spkey); | |
0f113f3e | 2523 | } |
b9908bf9 MC |
2524 | if (dh_srvr == NULL) { |
2525 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2526 | ERR_R_INTERNAL_ERROR); | |
0f113f3e MC |
2527 | goto err; |
2528 | } | |
0f113f3e | 2529 | } |
b9908bf9 MC |
2530 | if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) { |
2531 | /* Use client certificate key */ | |
2532 | EVP_PKEY *clkey = s->cert->key->privatekey; | |
2533 | dh_clnt = NULL; | |
2534 | if (clkey) | |
2535 | dh_clnt = EVP_PKEY_get1_DH(clkey); | |
2536 | if (dh_clnt == NULL) { | |
2537 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
0f113f3e MC |
2538 | ERR_R_INTERNAL_ERROR); |
2539 | goto err; | |
2540 | } | |
b9908bf9 MC |
2541 | } else { |
2542 | /* generate a new random key */ | |
2543 | if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) { | |
2544 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB); | |
0f113f3e MC |
2545 | goto err; |
2546 | } | |
b9908bf9 MC |
2547 | if (!DH_generate_key(dh_clnt)) { |
2548 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB); | |
2549 | DH_free(dh_clnt); | |
0f113f3e MC |
2550 | goto err; |
2551 | } | |
b9908bf9 | 2552 | } |
0f113f3e | 2553 | |
b9908bf9 MC |
2554 | pmslen = DH_size(dh_clnt); |
2555 | pms = OPENSSL_malloc(pmslen); | |
2556 | if (!pms) | |
2557 | goto memerr; | |
0f113f3e | 2558 | |
b9908bf9 MC |
2559 | /* |
2560 | * use the 'p' output buffer for the DH key, but make sure to | |
2561 | * clear it out afterwards | |
2562 | */ | |
0f113f3e | 2563 | |
b9908bf9 MC |
2564 | n = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt); |
2565 | if (s->s3->peer_dh_tmp == NULL) | |
2566 | DH_free(dh_srvr); | |
0f113f3e | 2567 | |
b9908bf9 MC |
2568 | if (n <= 0) { |
2569 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB); | |
2570 | DH_free(dh_clnt); | |
2571 | goto err; | |
2572 | } | |
2573 | pmslen = n; | |
0f113f3e | 2574 | |
b9908bf9 MC |
2575 | if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) |
2576 | n = 0; | |
2577 | else { | |
2578 | /* send off the data */ | |
2579 | n = BN_num_bytes(dh_clnt->pub_key); | |
2580 | s2n(n, p); | |
2581 | BN_bn2bin(dh_clnt->pub_key, p); | |
2582 | n += 2; | |
0f113f3e | 2583 | } |
c660ec63 | 2584 | |
b9908bf9 MC |
2585 | DH_free(dh_clnt); |
2586 | } | |
2587 | #endif | |
0f113f3e | 2588 | |
b9908bf9 MC |
2589 | #ifndef OPENSSL_NO_EC |
2590 | else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe | SSL_kECDHEPSK)) { | |
2591 | const EC_GROUP *srvr_group = NULL; | |
2592 | EC_KEY *tkey; | |
2593 | int ecdh_clnt_cert = 0; | |
2594 | int field_size = 0; | |
2595 | /* | |
2596 | * Did we send out the client's ECDH share for use in premaster | |
2597 | * computation as part of client certificate? If so, set | |
2598 | * ecdh_clnt_cert to 1. | |
2599 | */ | |
2600 | if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) { | |
2601 | /*- | |
2602 | * XXX: For now, we do not support client | |
2603 | * authentication using ECDH certificates. | |
2604 | * To add such support, one needs to add | |
2605 | * code that checks for appropriate | |
2606 | * conditions and sets ecdh_clnt_cert to 1. | |
2607 | * For example, the cert have an ECC | |
2608 | * key on the same curve as the server's | |
2609 | * and the key should be authorized for | |
2610 | * key agreement. | |
0f113f3e | 2611 | * |
b9908bf9 MC |
2612 | * One also needs to add code in ssl3_connect |
2613 | * to skip sending the certificate verify | |
2614 | * message. | |
2615 | * | |
2616 | * if ((s->cert->key->privatekey != NULL) && | |
2617 | * (s->cert->key->privatekey->type == | |
2618 | * EVP_PKEY_EC) && ...) | |
2619 | * ecdh_clnt_cert = 1; | |
0f113f3e | 2620 | */ |
b9908bf9 | 2621 | } |
0f113f3e | 2622 | |
b9908bf9 MC |
2623 | if (s->s3->peer_ecdh_tmp != NULL) { |
2624 | tkey = s->s3->peer_ecdh_tmp; | |
2625 | } else { | |
2626 | /* Get the Server Public Key from Cert */ | |
2627 | srvr_pub_pkey = X509_get_pubkey(s->session->peer); | |
2628 | if ((srvr_pub_pkey == NULL) | |
2629 | || (srvr_pub_pkey->type != EVP_PKEY_EC) | |
2630 | || (srvr_pub_pkey->pkey.ec == NULL)) { | |
2631 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
266483d2 MC |
2632 | ERR_R_INTERNAL_ERROR); |
2633 | goto err; | |
0f113f3e | 2634 | } |
b9908bf9 MC |
2635 | |
2636 | tkey = srvr_pub_pkey->pkey.ec; | |
2637 | } | |
2638 | ||
2639 | srvr_group = EC_KEY_get0_group(tkey); | |
2640 | srvr_ecpoint = EC_KEY_get0_public_key(tkey); | |
2641 | ||
2642 | if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) { | |
2643 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2644 | ERR_R_INTERNAL_ERROR); | |
2645 | goto err; | |
2646 | } | |
2647 | ||
2648 | if ((clnt_ecdh = EC_KEY_new()) == NULL) { | |
2649 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2650 | ERR_R_MALLOC_FAILURE); | |
2651 | goto err; | |
2652 | } | |
2653 | ||
2654 | if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) { | |
2655 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB); | |
2656 | goto err; | |
2657 | } | |
2658 | if (ecdh_clnt_cert) { | |
0f113f3e | 2659 | /* |
b9908bf9 MC |
2660 | * Reuse key info from our certificate We only need our |
2661 | * private key to perform the ECDH computation. | |
0f113f3e | 2662 | */ |
b9908bf9 MC |
2663 | const BIGNUM *priv_key; |
2664 | tkey = s->cert->key->privatekey->pkey.ec; | |
2665 | priv_key = EC_KEY_get0_private_key(tkey); | |
2666 | if (priv_key == NULL) { | |
2667 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2668 | ERR_R_MALLOC_FAILURE); | |
0f113f3e MC |
2669 | goto err; |
2670 | } | |
b9908bf9 MC |
2671 | if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) { |
2672 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB); | |
0f113f3e MC |
2673 | goto err; |
2674 | } | |
b9908bf9 MC |
2675 | } else { |
2676 | /* Generate a new ECDH key pair */ | |
2677 | if (!(EC_KEY_generate_key(clnt_ecdh))) { | |
2678 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2679 | ERR_R_ECDH_LIB); | |
2680 | goto err; | |
0f113f3e | 2681 | } |
b9908bf9 MC |
2682 | } |
2683 | ||
2684 | /* | |
2685 | * use the 'p' output buffer for the ECDH key, but make sure to | |
2686 | * clear it out afterwards | |
2687 | */ | |
0f113f3e | 2688 | |
b9908bf9 MC |
2689 | field_size = EC_GROUP_get_degree(srvr_group); |
2690 | if (field_size <= 0) { | |
2691 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB); | |
2692 | goto err; | |
0f113f3e | 2693 | } |
b9908bf9 MC |
2694 | pmslen = (field_size + 7) / 8; |
2695 | pms = OPENSSL_malloc(pmslen); | |
2696 | if (!pms) | |
2697 | goto memerr; | |
2698 | n = ECDH_compute_key(pms, pmslen, srvr_ecpoint, clnt_ecdh, NULL); | |
2699 | if (n <= 0 || pmslen != (size_t)n) { | |
2700 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB); | |
2701 | goto err; | |
2702 | } | |
2703 | ||
2704 | if (ecdh_clnt_cert) { | |
2705 | /* Send empty client key exch message */ | |
2706 | n = 0; | |
2707 | } else { | |
2708 | /* | |
2709 | * First check the size of encoding and allocate memory | |
2710 | * accordingly. | |
2711 | */ | |
2712 | encoded_pt_len = | |
2713 | EC_POINT_point2oct(srvr_group, | |
2714 | EC_KEY_get0_public_key(clnt_ecdh), | |
2715 | POINT_CONVERSION_UNCOMPRESSED, | |
2716 | NULL, 0, NULL); | |
2717 | ||
2718 | encodedPoint = (unsigned char *) | |
2719 | OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char)); | |
2720 | bn_ctx = BN_CTX_new(); | |
2721 | if ((encodedPoint == NULL) || (bn_ctx == NULL)) { | |
2722 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
0f113f3e MC |
2723 | ERR_R_MALLOC_FAILURE); |
2724 | goto err; | |
2725 | } | |
b9908bf9 MC |
2726 | |
2727 | /* Encode the public key */ | |
2728 | n = EC_POINT_point2oct(srvr_group, | |
2729 | EC_KEY_get0_public_key(clnt_ecdh), | |
2730 | POINT_CONVERSION_UNCOMPRESSED, | |
2731 | encodedPoint, encoded_pt_len, bn_ctx); | |
2732 | ||
2733 | *p = n; /* length of encoded point */ | |
2734 | /* Encoded point will be copied here */ | |
2735 | p += 1; | |
2736 | /* copy the point */ | |
2737 | memcpy(p, encodedPoint, n); | |
2738 | /* increment n to account for length field */ | |
2739 | n += 1; | |
0f113f3e MC |
2740 | } |
2741 | ||
b9908bf9 MC |
2742 | /* Free allocated memory */ |
2743 | BN_CTX_free(bn_ctx); | |
2744 | OPENSSL_free(encodedPoint); | |
2745 | EC_KEY_free(clnt_ecdh); | |
2746 | EVP_PKEY_free(srvr_pub_pkey); | |
2747 | } | |
2748 | #endif /* !OPENSSL_NO_EC */ | |
2749 | else if (alg_k & SSL_kGOST) { | |
2750 | /* GOST key exchange message creation */ | |
2751 | EVP_PKEY_CTX *pkey_ctx; | |
2752 | X509 *peer_cert; | |
2753 | size_t msglen; | |
2754 | unsigned int md_len; | |
2755 | unsigned char shared_ukm[32], tmp[256]; | |
2756 | EVP_MD_CTX *ukm_hash; | |
2757 | EVP_PKEY *pub_key; | |
2758 | ||
2759 | pmslen = 32; | |
2760 | pms = OPENSSL_malloc(pmslen); | |
2761 | if (!pms) | |
2762 | goto memerr; | |
7689082b | 2763 | |
b9908bf9 MC |
2764 | /* |
2765 | * Get server sertificate PKEY and create ctx from it | |
2766 | */ | |
2767 | peer_cert = s->session->peer; | |
2768 | if (!peer_cert) { | |
2769 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2770 | SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER); | |
77d514c5 MC |
2771 | goto err; |
2772 | } | |
2773 | ||
b9908bf9 MC |
2774 | pkey_ctx = EVP_PKEY_CTX_new(pub_key = |
2775 | X509_get_pubkey(peer_cert), NULL); | |
2776 | /* | |
2777 | * If we have send a certificate, and certificate key | |
2778 | * | |
2779 | * * parameters match those of server certificate, use | |
2780 | * certificate key for key exchange | |
2781 | */ | |
0f113f3e | 2782 | |
b9908bf9 MC |
2783 | /* Otherwise, generate ephemeral key pair */ |
2784 | ||
2785 | EVP_PKEY_encrypt_init(pkey_ctx); | |
2786 | /* Generate session key */ | |
2787 | if (RAND_bytes(pms, pmslen) <= 0) { | |
2788 | EVP_PKEY_CTX_free(pkey_ctx); | |
2789 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2790 | ERR_R_INTERNAL_ERROR); | |
2791 | goto err; | |
2792 | }; | |
c660ec63 | 2793 | /* |
b9908bf9 | 2794 | * If we have client certificate, use its secret as peer key |
c660ec63 | 2795 | */ |
b9908bf9 MC |
2796 | if (s->s3->tmp.cert_req && s->cert->key->privatekey) { |
2797 | if (EVP_PKEY_derive_set_peer | |
2798 | (pkey_ctx, s->cert->key->privatekey) <= 0) { | |
2799 | /* | |
2800 | * If there was an error - just ignore it. Ephemeral key | |
2801 | * * would be used | |
2802 | */ | |
2803 | ERR_clear_error(); | |
c660ec63 DSH |
2804 | } |
2805 | } | |
b9908bf9 MC |
2806 | /* |
2807 | * Compute shared IV and store it in algorithm-specific context | |
2808 | * data | |
2809 | */ | |
2810 | ukm_hash = EVP_MD_CTX_create(); | |
2811 | EVP_DigestInit(ukm_hash, | |
2812 | EVP_get_digestbynid(NID_id_GostR3411_94)); | |
2813 | EVP_DigestUpdate(ukm_hash, s->s3->client_random, | |
2814 | SSL3_RANDOM_SIZE); | |
2815 | EVP_DigestUpdate(ukm_hash, s->s3->server_random, | |
2816 | SSL3_RANDOM_SIZE); | |
2817 | EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len); | |
2818 | EVP_MD_CTX_destroy(ukm_hash); | |
2819 | if (EVP_PKEY_CTX_ctrl | |
2820 | (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8, | |
2821 | shared_ukm) < 0) { | |
2822 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2823 | SSL_R_LIBRARY_BUG); | |
2824 | goto err; | |
c660ec63 | 2825 | } |
b9908bf9 MC |
2826 | /* Make GOST keytransport blob message */ |
2827 | /* | |
2828 | * Encapsulate it into sequence | |
2829 | */ | |
2830 | *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED; | |
2831 | msglen = 255; | |
2832 | if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) < 0) { | |
2833 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2834 | SSL_R_LIBRARY_BUG); | |
c660ec63 DSH |
2835 | goto err; |
2836 | } | |
b9908bf9 MC |
2837 | if (msglen >= 0x80) { |
2838 | *(p++) = 0x81; | |
2839 | *(p++) = msglen & 0xff; | |
2840 | n = msglen + 3; | |
2841 | } else { | |
2842 | *(p++) = msglen & 0xff; | |
2843 | n = msglen + 2; | |
2844 | } | |
2845 | memcpy(p, tmp, msglen); | |
2846 | /* Check if pubkey from client certificate was used */ | |
2847 | if (EVP_PKEY_CTX_ctrl | |
2848 | (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) { | |
2849 | /* Set flag "skip certificate verify" */ | |
2850 | s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY; | |
2851 | } | |
2852 | EVP_PKEY_CTX_free(pkey_ctx); | |
2853 | EVP_PKEY_free(pub_key); | |
2854 | ||
2855 | } | |
2856 | #ifndef OPENSSL_NO_SRP | |
2857 | else if (alg_k & SSL_kSRP) { | |
2858 | if (s->srp_ctx.A != NULL) { | |
2859 | /* send off the data */ | |
2860 | n = BN_num_bytes(s->srp_ctx.A); | |
2861 | s2n(n, p); | |
2862 | BN_bn2bin(s->srp_ctx.A, p); | |
2863 | n += 2; | |
2864 | } else { | |
2865 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2866 | ERR_R_INTERNAL_ERROR); | |
2867 | goto err; | |
2868 | } | |
2869 | OPENSSL_free(s->session->srp_username); | |
2870 | s->session->srp_username = BUF_strdup(s->srp_ctx.login); | |
2871 | if (s->session->srp_username == NULL) { | |
2872 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2873 | ERR_R_MALLOC_FAILURE); | |
69f68237 MC |
2874 | goto err; |
2875 | } | |
c660ec63 | 2876 | } |
b9908bf9 MC |
2877 | #endif |
2878 | else { | |
2879 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | |
2880 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); | |
2881 | goto err; | |
2882 | } | |
2883 | ||
2884 | #ifndef OPENSSL_NO_PSK | |
2885 | n += pskhdrlen; | |
2886 | #endif | |
2887 | ||
2888 | if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) { | |
2889 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | |
2890 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); | |
2891 | goto err; | |
2892 | } | |
2893 | ||
2894 | s->s3->tmp.pms = pms; | |
2895 | s->s3->tmp.pmslen = pmslen; | |
2896 | ||
2897 | return 1; | |
c660ec63 DSH |
2898 | memerr: |
2899 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
b9908bf9 | 2900 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); |
0f113f3e | 2901 | err: |
4b45c6e5 | 2902 | OPENSSL_clear_free(pms, pmslen); |
76106e60 | 2903 | s->s3->tmp.pms = NULL; |
10bf4fc2 | 2904 | #ifndef OPENSSL_NO_EC |
0f113f3e | 2905 | BN_CTX_free(bn_ctx); |
b548a1f1 | 2906 | OPENSSL_free(encodedPoint); |
8fdc3734 | 2907 | EC_KEY_free(clnt_ecdh); |
0f113f3e | 2908 | EVP_PKEY_free(srvr_pub_pkey); |
7689082b DSH |
2909 | #endif |
2910 | #ifndef OPENSSL_NO_PSK | |
2911 | OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen); | |
2912 | s->s3->tmp.psk = NULL; | |
0f113f3e | 2913 | #endif |
fe3a3291 | 2914 | ossl_statem_set_error(s); |
b9908bf9 MC |
2915 | return 0; |
2916 | } | |
2917 | ||
2918 | int tls_client_key_exchange_post_work(SSL *s) | |
2919 | { | |
2920 | unsigned char *pms = NULL; | |
2921 | size_t pmslen = 0; | |
2922 | ||
2923 | #ifndef OPENSSL_NO_SRP | |
2924 | /* Check for SRP */ | |
2925 | if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) { | |
2926 | if (!srp_generate_client_master_secret(s)) { | |
2927 | SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, | |
2928 | ERR_R_INTERNAL_ERROR); | |
2929 | goto err; | |
2930 | } | |
2931 | return 1; | |
2932 | } | |
2933 | #endif | |
2934 | pms = s->s3->tmp.pms; | |
2935 | pmslen = s->s3->tmp.pmslen; | |
2936 | ||
2937 | if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) { | |
2938 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
2939 | SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE); | |
2940 | goto err; | |
2941 | } | |
2942 | if (!ssl_generate_master_secret(s, pms, pmslen, 1)) { | |
2943 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
2944 | SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR); | |
2945 | goto err; | |
2946 | } | |
473483d4 MC |
2947 | |
2948 | #ifndef OPENSSL_NO_SCTP | |
2949 | if (SSL_IS_DTLS(s)) { | |
2950 | unsigned char sctpauthkey[64]; | |
2951 | char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)]; | |
2952 | ||
2953 | /* | |
2954 | * Add new shared key for SCTP-Auth, will be ignored if no SCTP | |
2955 | * used. | |
2956 | */ | |
141eb8c6 MC |
2957 | memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL, |
2958 | sizeof(DTLS1_SCTP_AUTH_LABEL)); | |
473483d4 MC |
2959 | |
2960 | if (SSL_export_keying_material(s, sctpauthkey, | |
2961 | sizeof(sctpauthkey), labelbuffer, | |
2962 | sizeof(labelbuffer), NULL, 0, 0) <= 0) | |
2963 | goto err; | |
2964 | ||
2965 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY, | |
2966 | sizeof(sctpauthkey), sctpauthkey); | |
2967 | } | |
2968 | #endif | |
2969 | ||
b9908bf9 MC |
2970 | return 1; |
2971 | err: | |
2972 | OPENSSL_clear_free(pms, pmslen); | |
2973 | s->s3->tmp.pms = NULL; | |
2974 | return 0; | |
0f113f3e | 2975 | } |
d02b48c6 | 2976 | |
b9908bf9 | 2977 | int tls_construct_client_verify(SSL *s) |
0f113f3e MC |
2978 | { |
2979 | unsigned char *p; | |
2980 | unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH]; | |
2981 | EVP_PKEY *pkey; | |
2982 | EVP_PKEY_CTX *pctx = NULL; | |
2983 | EVP_MD_CTX mctx; | |
2984 | unsigned u = 0; | |
2985 | unsigned long n; | |
2986 | int j; | |
2987 | ||
2988 | EVP_MD_CTX_init(&mctx); | |
2989 | ||
b9908bf9 MC |
2990 | p = ssl_handshake_start(s); |
2991 | pkey = s->cert->key->privatekey; | |
0e1dba93 | 2992 | /* Create context from key and test if sha1 is allowed as digest */ |
b9908bf9 MC |
2993 | pctx = EVP_PKEY_CTX_new(pkey, NULL); |
2994 | EVP_PKEY_sign_init(pctx); | |
2995 | if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) { | |
2996 | if (!SSL_USE_SIGALGS(s)) | |
2997 | s->method->ssl3_enc->cert_verify_mac(s, | |
2998 | NID_sha1, | |
2999 | &(data | |
3000 | [MD5_DIGEST_LENGTH])); | |
3001 | } else { | |
3002 | ERR_clear_error(); | |
3003 | } | |
3004 | /* | |
3005 | * For TLS v1.2 send signature algorithm and signature using agreed | |
3006 | * digest and cached handshake records. | |
3007 | */ | |
3008 | if (SSL_USE_SIGALGS(s)) { | |
3009 | long hdatalen = 0; | |
3010 | void *hdata; | |
3011 | const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys]; | |
3012 | hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata); | |
3013 | if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) { | |
3014 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); | |
3015 | goto err; | |
0f113f3e | 3016 | } |
b9908bf9 | 3017 | p += 2; |
855a54a9 | 3018 | #ifdef SSL_DEBUG |
b9908bf9 MC |
3019 | fprintf(stderr, "Using TLS 1.2 with client alg %s\n", |
3020 | EVP_MD_name(md)); | |
0f113f3e | 3021 | #endif |
b9908bf9 MC |
3022 | if (!EVP_SignInit_ex(&mctx, md, NULL) |
3023 | || !EVP_SignUpdate(&mctx, hdata, hdatalen) | |
3024 | || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) { | |
3025 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB); | |
3026 | goto err; | |
3027 | } | |
3028 | s2n(u, p); | |
3029 | n = u + 4; | |
3030 | /* Digest cached records and discard handshake buffer */ | |
3031 | if (!ssl3_digest_cached_records(s, 0)) | |
3032 | goto err; | |
3033 | } else | |
bc36ee62 | 3034 | #ifndef OPENSSL_NO_RSA |
b9908bf9 MC |
3035 | if (pkey->type == EVP_PKEY_RSA) { |
3036 | s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0])); | |
3037 | if (RSA_sign(NID_md5_sha1, data, | |
3038 | MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, | |
3039 | &(p[2]), &u, pkey->pkey.rsa) <= 0) { | |
3040 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_RSA_LIB); | |
3041 | goto err; | |
3042 | } | |
3043 | s2n(u, p); | |
3044 | n = u + 2; | |
3045 | } else | |
d02b48c6 | 3046 | #endif |
bc36ee62 | 3047 | #ifndef OPENSSL_NO_DSA |
b9908bf9 MC |
3048 | if (pkey->type == EVP_PKEY_DSA) { |
3049 | if (!DSA_sign(pkey->save_type, | |
3050 | &(data[MD5_DIGEST_LENGTH]), | |
3051 | SHA_DIGEST_LENGTH, &(p[2]), | |
3052 | (unsigned int *)&j, pkey->pkey.dsa)) { | |
3053 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_DSA_LIB); | |
3054 | goto err; | |
3055 | } | |
3056 | s2n(j, p); | |
3057 | n = j + 2; | |
3058 | } else | |
ea262260 | 3059 | #endif |
10bf4fc2 | 3060 | #ifndef OPENSSL_NO_EC |
b9908bf9 MC |
3061 | if (pkey->type == EVP_PKEY_EC) { |
3062 | if (!ECDSA_sign(pkey->save_type, | |
3063 | &(data[MD5_DIGEST_LENGTH]), | |
3064 | SHA_DIGEST_LENGTH, &(p[2]), | |
3065 | (unsigned int *)&j, pkey->pkey.ec)) { | |
3066 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_ECDSA_LIB); | |
0f113f3e MC |
3067 | goto err; |
3068 | } | |
b9908bf9 MC |
3069 | s2n(j, p); |
3070 | n = j + 2; | |
3071 | } else | |
3072 | #endif | |
3073 | if (pkey->type == NID_id_GostR3410_2001) { | |
3074 | unsigned char signbuf[64]; | |
3075 | int i; | |
3076 | size_t sigsize = 64; | |
3077 | s->method->ssl3_enc->cert_verify_mac(s, | |
3078 | NID_id_GostR3411_94, data); | |
3079 | if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) { | |
3080 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); | |
77d514c5 MC |
3081 | goto err; |
3082 | } | |
b9908bf9 MC |
3083 | for (i = 63, j = 0; i >= 0; j++, i--) { |
3084 | p[2 + j] = signbuf[i]; | |
3085 | } | |
3086 | s2n(j, p); | |
3087 | n = j + 2; | |
3088 | } else { | |
3089 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); | |
3090 | goto err; | |
3091 | } | |
3092 | if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) { | |
3093 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); | |
3094 | goto err; | |
0f113f3e | 3095 | } |
b9908bf9 | 3096 | |
0f113f3e MC |
3097 | EVP_MD_CTX_cleanup(&mctx); |
3098 | EVP_PKEY_CTX_free(pctx); | |
b9908bf9 | 3099 | return 1; |
0f113f3e MC |
3100 | err: |
3101 | EVP_MD_CTX_cleanup(&mctx); | |
3102 | EVP_PKEY_CTX_free(pctx); | |
b9908bf9 | 3103 | return 0; |
0f113f3e MC |
3104 | } |
3105 | ||
3106 | /* | |
3107 | * Check a certificate can be used for client authentication. Currently check | |
3108 | * cert exists, if we have a suitable digest for TLS 1.2 if static DH client | |
3109 | * certificates can be used and optionally checks suitability for Suite B. | |
0d609395 DSH |
3110 | */ |
3111 | static int ssl3_check_client_certificate(SSL *s) | |
0f113f3e MC |
3112 | { |
3113 | unsigned long alg_k; | |
3114 | if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey) | |
3115 | return 0; | |
3116 | /* If no suitable signature algorithm can't use certificate */ | |
d376e57d | 3117 | if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys]) |
0f113f3e MC |
3118 | return 0; |
3119 | /* | |
3120 | * If strict mode check suitability of chain before using it. This also | |
3121 | * adjusts suite B digest if necessary. | |
3122 | */ | |
3123 | if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT && | |
3124 | !tls1_check_chain(s, NULL, NULL, NULL, -2)) | |
3125 | return 0; | |
3126 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; | |
3127 | /* See if we can use client certificate for fixed DH */ | |
3128 | if (alg_k & (SSL_kDHr | SSL_kDHd)) { | |
a273c6ee | 3129 | int i = s->session->peer_type; |
0f113f3e MC |
3130 | EVP_PKEY *clkey = NULL, *spkey = NULL; |
3131 | clkey = s->cert->key->privatekey; | |
3132 | /* If client key not DH assume it can be used */ | |
3133 | if (EVP_PKEY_id(clkey) != EVP_PKEY_DH) | |
3134 | return 1; | |
3135 | if (i >= 0) | |
a273c6ee | 3136 | spkey = X509_get_pubkey(s->session->peer); |
0f113f3e MC |
3137 | if (spkey) { |
3138 | /* Compare server and client parameters */ | |
3139 | i = EVP_PKEY_cmp_parameters(clkey, spkey); | |
3140 | EVP_PKEY_free(spkey); | |
3141 | if (i != 1) | |
3142 | return 0; | |
3143 | } | |
3144 | s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY; | |
3145 | } | |
3146 | return 1; | |
3147 | } | |
0d609395 | 3148 | |
be3583fa | 3149 | WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst) |
0f113f3e MC |
3150 | { |
3151 | X509 *x509 = NULL; | |
3152 | EVP_PKEY *pkey = NULL; | |
3153 | int i; | |
3154 | ||
b9908bf9 | 3155 | if (wst == WORK_MORE_A) { |
0f113f3e MC |
3156 | /* Let cert callback update client certificates if required */ |
3157 | if (s->cert->cert_cb) { | |
3158 | i = s->cert->cert_cb(s, s->cert->cert_cb_arg); | |
3159 | if (i < 0) { | |
3160 | s->rwstate = SSL_X509_LOOKUP; | |
b9908bf9 | 3161 | return WORK_MORE_A; |
0f113f3e MC |
3162 | } |
3163 | if (i == 0) { | |
3164 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
fe3a3291 | 3165 | ossl_statem_set_error(s); |
0f113f3e MC |
3166 | return 0; |
3167 | } | |
3168 | s->rwstate = SSL_NOTHING; | |
3169 | } | |
3170 | if (ssl3_check_client_certificate(s)) | |
b9908bf9 MC |
3171 | return WORK_FINISHED_CONTINUE; |
3172 | ||
3173 | /* Fall through to WORK_MORE_B */ | |
3174 | wst = WORK_MORE_B; | |
0f113f3e MC |
3175 | } |
3176 | ||
3177 | /* We need to get a client cert */ | |
b9908bf9 | 3178 | if (wst == WORK_MORE_B) { |
0f113f3e MC |
3179 | /* |
3180 | * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP; | |
3181 | * return(-1); We then get retied later | |
3182 | */ | |
0f113f3e MC |
3183 | i = ssl_do_client_cert_cb(s, &x509, &pkey); |
3184 | if (i < 0) { | |
3185 | s->rwstate = SSL_X509_LOOKUP; | |
b9908bf9 | 3186 | return WORK_MORE_B; |
0f113f3e MC |
3187 | } |
3188 | s->rwstate = SSL_NOTHING; | |
3189 | if ((i == 1) && (pkey != NULL) && (x509 != NULL)) { | |
0f113f3e MC |
3190 | if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) |
3191 | i = 0; | |
3192 | } else if (i == 1) { | |
3193 | i = 0; | |
b9908bf9 | 3194 | SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE, |
0f113f3e MC |
3195 | SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); |
3196 | } | |
3197 | ||
222561fe | 3198 | X509_free(x509); |
25aaa98a | 3199 | EVP_PKEY_free(pkey); |
0f113f3e MC |
3200 | if (i && !ssl3_check_client_certificate(s)) |
3201 | i = 0; | |
3202 | if (i == 0) { | |
3203 | if (s->version == SSL3_VERSION) { | |
3204 | s->s3->tmp.cert_req = 0; | |
3205 | ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE); | |
b9908bf9 | 3206 | return WORK_FINISHED_CONTINUE; |
0f113f3e MC |
3207 | } else { |
3208 | s->s3->tmp.cert_req = 2; | |
124037fd | 3209 | if (!ssl3_digest_cached_records(s, 0)) { |
dab18ab5 | 3210 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
fe3a3291 | 3211 | ossl_statem_set_error(s); |
dab18ab5 DSH |
3212 | return 0; |
3213 | } | |
0f113f3e MC |
3214 | } |
3215 | } | |
3216 | ||
b9908bf9 | 3217 | return WORK_FINISHED_CONTINUE; |
0f113f3e MC |
3218 | } |
3219 | ||
b9908bf9 MC |
3220 | /* Shouldn't ever get here */ |
3221 | return WORK_ERROR; | |
3222 | } | |
3223 | ||
3224 | int tls_construct_client_certificate(SSL *s) | |
3225 | { | |
3226 | if (!ssl3_output_cert_chain(s, | |
3227 | (s->s3->tmp.cert_req == | |
3228 | 2) ? NULL : s->cert->key)) { | |
3229 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR); | |
3230 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
fe3a3291 | 3231 | ossl_statem_set_error(s); |
b9908bf9 | 3232 | return 0; |
0f113f3e | 3233 | } |
b9908bf9 MC |
3234 | |
3235 | return 1; | |
0f113f3e MC |
3236 | } |
3237 | ||
3238 | #define has_bits(i,m) (((i)&(m)) == (m)) | |
d02b48c6 | 3239 | |
36d16f8e | 3240 | int ssl3_check_cert_and_algorithm(SSL *s) |
0f113f3e MC |
3241 | { |
3242 | int i, idx; | |
3243 | long alg_k, alg_a; | |
3244 | EVP_PKEY *pkey = NULL; | |
ac38115c | 3245 | int pkey_bits; |
bc36ee62 | 3246 | #ifndef OPENSSL_NO_RSA |
0f113f3e | 3247 | RSA *rsa; |
79df9d62 | 3248 | #endif |
bc36ee62 | 3249 | #ifndef OPENSSL_NO_DH |
0f113f3e | 3250 | DH *dh; |
79df9d62 | 3251 | #endif |
26c79d56 | 3252 | int al = SSL_AD_HANDSHAKE_FAILURE; |
d02b48c6 | 3253 | |
0f113f3e MC |
3254 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
3255 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; | |
d02b48c6 | 3256 | |
0f113f3e | 3257 | /* we don't have a certificate */ |
55a9a16f | 3258 | if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK)) |
0f113f3e | 3259 | return (1); |
bc36ee62 | 3260 | #ifndef OPENSSL_NO_RSA |
8d92c1f8 | 3261 | rsa = s->s3->peer_rsa_tmp; |
79df9d62 | 3262 | #endif |
bc36ee62 | 3263 | #ifndef OPENSSL_NO_DH |
8d92c1f8 | 3264 | dh = s->s3->peer_dh_tmp; |
79df9d62 | 3265 | #endif |
d02b48c6 | 3266 | |
0f113f3e | 3267 | /* This is the passed certificate */ |
d02b48c6 | 3268 | |
a273c6ee | 3269 | idx = s->session->peer_type; |
10bf4fc2 | 3270 | #ifndef OPENSSL_NO_EC |
0f113f3e | 3271 | if (idx == SSL_PKEY_ECC) { |
a273c6ee | 3272 | if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) { |
0f113f3e MC |
3273 | /* check failed */ |
3274 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT); | |
3275 | goto f_err; | |
3276 | } else { | |
3277 | return 1; | |
3278 | } | |
3279 | } else if (alg_a & SSL_aECDSA) { | |
3280 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3281 | SSL_R_MISSING_ECDSA_SIGNING_CERT); | |
3282 | goto f_err; | |
3283 | } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) { | |
3284 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT); | |
3285 | goto f_err; | |
3286 | } | |
3287 | #endif | |
a273c6ee | 3288 | pkey = X509_get_pubkey(s->session->peer); |
ac38115c | 3289 | pkey_bits = EVP_PKEY_bits(pkey); |
a273c6ee | 3290 | i = X509_certificate_type(s->session->peer, pkey); |
0f113f3e MC |
3291 | EVP_PKEY_free(pkey); |
3292 | ||
3293 | /* Check that we have a certificate if we require one */ | |
3294 | if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) { | |
3295 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3296 | SSL_R_MISSING_RSA_SIGNING_CERT); | |
3297 | goto f_err; | |
3298 | } | |
bc36ee62 | 3299 | #ifndef OPENSSL_NO_DSA |
0f113f3e MC |
3300 | else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) { |
3301 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3302 | SSL_R_MISSING_DSA_SIGNING_CERT); | |
3303 | goto f_err; | |
3304 | } | |
d02b48c6 | 3305 | #endif |
bc36ee62 | 3306 | #ifndef OPENSSL_NO_RSA |
7689082b | 3307 | if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) { |
26c79d56 KR |
3308 | if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && |
3309 | !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) { | |
3310 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3311 | SSL_R_MISSING_RSA_ENCRYPTING_CERT); | |
3312 | goto f_err; | |
3313 | } else if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) { | |
3314 | if (pkey_bits <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) { | |
3315 | if (!has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) { | |
3316 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3317 | SSL_R_MISSING_RSA_ENCRYPTING_CERT); | |
3318 | goto f_err; | |
3319 | } | |
3320 | if (rsa != NULL) { | |
3321 | /* server key exchange is not allowed. */ | |
3322 | al = SSL_AD_INTERNAL_ERROR; | |
3323 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR); | |
3324 | goto f_err; | |
3325 | } | |
3326 | } | |
3327 | } | |
0f113f3e | 3328 | } |
79df9d62 | 3329 | #endif |
bc36ee62 | 3330 | #ifndef OPENSSL_NO_DH |
26c79d56 KR |
3331 | if ((alg_k & SSL_kDHE) && (dh == NULL)) { |
3332 | al = SSL_AD_INTERNAL_ERROR; | |
3333 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR); | |
0f113f3e MC |
3334 | goto f_err; |
3335 | } else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) && | |
3336 | !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) { | |
3337 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3338 | SSL_R_MISSING_DH_RSA_CERT); | |
3339 | goto f_err; | |
3340 | } | |
3341 | # ifndef OPENSSL_NO_DSA | |
3342 | else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) && | |
3343 | !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) { | |
3344 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3345 | SSL_R_MISSING_DH_DSA_CERT); | |
3346 | goto f_err; | |
3347 | } | |
3348 | # endif | |
d02b48c6 RE |
3349 | #endif |
3350 | ||
ac38115c KR |
3351 | if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && |
3352 | pkey_bits > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) { | |
bc36ee62 | 3353 | #ifndef OPENSSL_NO_RSA |
0f113f3e | 3354 | if (alg_k & SSL_kRSA) { |
26c79d56 KR |
3355 | if (rsa == NULL) { |
3356 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3357 | SSL_R_MISSING_EXPORT_TMP_RSA_KEY); | |
3358 | goto f_err; | |
3359 | } else if (RSA_bits(rsa) > | |
0f113f3e | 3360 | SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) { |
26c79d56 KR |
3361 | /* We have a temporary RSA key but it's too large. */ |
3362 | al = SSL_AD_EXPORT_RESTRICTION; | |
0f113f3e MC |
3363 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, |
3364 | SSL_R_MISSING_EXPORT_TMP_RSA_KEY); | |
3365 | goto f_err; | |
3366 | } | |
3367 | } else | |
d02b48c6 | 3368 | #endif |
bc36ee62 | 3369 | #ifndef OPENSSL_NO_DH |
26c79d56 KR |
3370 | if (alg_k & SSL_kDHE) { |
3371 | if (DH_bits(dh) > | |
0f113f3e | 3372 | SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) { |
26c79d56 KR |
3373 | /* We have a temporary DH key but it's too large. */ |
3374 | al = SSL_AD_EXPORT_RESTRICTION; | |
0f113f3e MC |
3375 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, |
3376 | SSL_R_MISSING_EXPORT_TMP_DH_KEY); | |
3377 | goto f_err; | |
3378 | } | |
26c79d56 KR |
3379 | } else if (alg_k & (SSL_kDHr | SSL_kDHd)) { |
3380 | /* The cert should have had an export DH key. */ | |
3381 | al = SSL_AD_EXPORT_RESTRICTION; | |
3382 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3383 | SSL_R_MISSING_EXPORT_TMP_DH_KEY); | |
3384 | goto f_err; | |
0f113f3e MC |
3385 | } else |
3386 | #endif | |
3387 | { | |
3388 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3389 | SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); | |
3390 | goto f_err; | |
3391 | } | |
3392 | } | |
3393 | return (1); | |
3394 | f_err: | |
26c79d56 | 3395 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
0f113f3e MC |
3396 | return (0); |
3397 | } | |
3398 | ||
e481f9b9 | 3399 | #ifndef OPENSSL_NO_NEXTPROTONEG |
b9908bf9 MC |
3400 | int tls_construct_next_proto(SSL *s) |
3401 | { | |
3402 | unsigned int len, padding_len; | |
3403 | unsigned char *d; | |
3404 | ||
3405 | len = s->next_proto_negotiated_len; | |
3406 | padding_len = 32 - ((len + 2) % 32); | |
3407 | d = (unsigned char *)s->init_buf->data; | |
3408 | d[4] = len; | |
3409 | memcpy(d + 5, s->next_proto_negotiated, len); | |
3410 | d[5 + len] = padding_len; | |
3411 | memset(d + 6 + len, 0, padding_len); | |
3412 | *(d++) = SSL3_MT_NEXT_PROTO; | |
3413 | l2n3(2 + len + padding_len, d); | |
3414 | s->init_num = 4 + 2 + len + padding_len; | |
3415 | s->init_off = 0; | |
3416 | ||
3417 | return 1; | |
3418 | } | |
6434abbf | 3419 | #endif |
368888bc DSH |
3420 | |
3421 | int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) | |
0f113f3e MC |
3422 | { |
3423 | int i = 0; | |
368888bc | 3424 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
3425 | if (s->ctx->client_cert_engine) { |
3426 | i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s, | |
3427 | SSL_get_client_CA_list(s), | |
3428 | px509, ppkey, NULL, NULL, NULL); | |
3429 | if (i != 0) | |
3430 | return i; | |
3431 | } | |
3432 | #endif | |
3433 | if (s->ctx->client_cert_cb) | |
3434 | i = s->ctx->client_cert_cb(s, px509, ppkey); | |
3435 | return i; | |
3436 | } | |
d45ba43d MC |
3437 | |
3438 | int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, | |
e9fa092e | 3439 | unsigned char *p) |
d45ba43d MC |
3440 | { |
3441 | int i, j = 0; | |
3442 | SSL_CIPHER *c; | |
3443 | unsigned char *q; | |
3444 | int empty_reneg_info_scsv = !s->renegotiate; | |
3445 | /* Set disabled masks for this session */ | |
3446 | ssl_set_client_disabled(s); | |
3447 | ||
3448 | if (sk == NULL) | |
3449 | return (0); | |
3450 | q = p; | |
d45ba43d MC |
3451 | |
3452 | for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) { | |
3453 | c = sk_SSL_CIPHER_value(sk, i); | |
3454 | /* Skip disabled ciphers */ | |
3455 | if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED)) | |
3456 | continue; | |
3457 | #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL | |
3458 | if (c->id == SSL3_CK_SCSV) { | |
3459 | if (!empty_reneg_info_scsv) | |
3460 | continue; | |
3461 | else | |
3462 | empty_reneg_info_scsv = 0; | |
3463 | } | |
3464 | #endif | |
e9fa092e | 3465 | j = s->method->put_cipher_by_char(c, p); |
d45ba43d MC |
3466 | p += j; |
3467 | } | |
3468 | /* | |
3469 | * If p == q, no ciphers; caller indicates an error. Otherwise, add | |
3470 | * applicable SCSVs. | |
3471 | */ | |
3472 | if (p != q) { | |
3473 | if (empty_reneg_info_scsv) { | |
3474 | static SSL_CIPHER scsv = { | |
3475 | 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
3476 | }; | |
e9fa092e | 3477 | j = s->method->put_cipher_by_char(&scsv, p); |
d45ba43d MC |
3478 | p += j; |
3479 | #ifdef OPENSSL_RI_DEBUG | |
3480 | fprintf(stderr, | |
3481 | "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n"); | |
3482 | #endif | |
3483 | } | |
3484 | if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) { | |
3485 | static SSL_CIPHER scsv = { | |
3486 | 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
3487 | }; | |
e9fa092e | 3488 | j = s->method->put_cipher_by_char(&scsv, p); |
d45ba43d MC |
3489 | p += j; |
3490 | } | |
3491 | } | |
3492 | ||
3493 | return (p - q); | |
3494 | } |