Lines Matching refs:ssl
4 * This package is an SSL implementation written
6 * The implementation was written so as to conform with Netscapes SSL.
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
150 #include <openssl/ssl.h>
175 int ssl3_connect(SSL *ssl) {
177 void (*cb)(const SSL *ssl, int type, int value) = NULL;
181 assert(ssl->handshake_func == ssl3_connect);
182 assert(!ssl->server);
183 assert(!SSL_IS_DTLS(ssl));
188 if (ssl->info_callback != NULL) {
189 cb = ssl->info_callback;
190 } else if (ssl->ctx->info_callback != NULL) {
191 cb = ssl->ctx->info_callback;
194 ssl->in_handshake++;
197 state = ssl->state;
199 switch (ssl->state) {
202 cb(ssl, SSL_CB_HANDSHAKE_START, 1);
205 if (ssl->init_buf == NULL) {
213 ssl->init_buf = buf;
217 if (!ssl_init_wbio_buffer(ssl, 0)) {
224 if (!ssl3_init_handshake_buffer(ssl)) {
225 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
230 ssl->state = SSL3_ST_CW_CLNT_HELLO_A;
231 ssl->init_num = 0;
236 ssl->shutdown = 0;
237 ret = ssl3_send_client_hello(ssl);
241 ssl->state = SSL3_ST_CR_SRVR_HELLO_A;
242 ssl->init_num = 0;
245 if (ssl->bbio != ssl->wbio) {
246 ssl->wbio = BIO_push(ssl->bbio, ssl->wbio);
253 ret = ssl3_get_server_hello(ssl);
258 if (ssl->hit) {
259 ssl->state = SSL3_ST_CR_CHANGE;
260 if (ssl->tlsext_ticket_expected) {
262 ssl->state = SSL3_ST_CR_SESSION_TICKET_A;
265 ssl->state = SSL3_ST_CR_CERT_A;
267 ssl->init_num = 0;
272 if (ssl_cipher_has_server_public_key(ssl->s3->tmp.new_cipher)) {
273 ret = ssl3_get_server_certificate(ssl);
277 if (ssl->s3->tmp.certificate_status_expected) {
278 ssl->state = SSL3_ST_CR_CERT_STATUS_A;
280 ssl->state = SSL3_ST_VERIFY_SERVER_CERT;
284 ssl->state = SSL3_ST_CR_KEY_EXCH_A;
286 ssl->init_num = 0;
290 ret = ssl3_verify_server_cert(ssl);
295 ssl->state = SSL3_ST_CR_KEY_EXCH_A;
296 ssl->init_num = 0;
301 ret = ssl3_get_server_key_exchange(ssl);
305 ssl->state = SSL3_ST_CR_CERT_REQ_A;
306 ssl->init_num = 0;
311 ret = ssl3_get_certificate_request(ssl);
315 ssl->state = SSL3_ST_CR_SRVR_DONE_A;
316 ssl->init_num = 0;
321 ret = ssl3_get_server_done(ssl);
325 if (ssl->s3->tmp.cert_req) {
326 ssl->state = SSL3_ST_CW_CERT_A;
328 ssl->state = SSL3_ST_CW_KEY_EXCH_A;
330 ssl->init_num = 0;
338 ret = ssl3_send_client_certificate(ssl);
342 ssl->state = SSL3_ST_CW_KEY_EXCH_A;
343 ssl->init_num = 0;
348 ret = ssl3_send_client_key_exchange(ssl);
354 if (ssl->s3->tmp.cert_req == 1) {
355 ssl->state = SSL3_ST_CW_CERT_VRFY_A;
357 ssl->state = SSL3_ST_CW_CHANGE_A;
360 ssl->init_num = 0;
366 ret = ssl3_send_cert_verify(ssl);
370 ssl->state = SSL3_ST_CW_CHANGE_A;
371 ssl->init_num = 0;
376 ret = ssl3_send_change_cipher_spec(ssl, SSL3_ST_CW_CHANGE_A,
382 ssl->state = SSL3_ST_CW_FINISHED_A;
383 if (ssl->s3->tlsext_channel_id_valid) {
384 ssl->state = SSL3_ST_CW_CHANNEL_ID_A;
386 if (ssl->s3->next_proto_neg_seen) {
387 ssl->state = SSL3_ST_CW_NEXT_PROTO_A;
389 ssl->init_num = 0;
391 ssl->session->cipher = ssl->s3->tmp.new_cipher;
392 if (!ssl->enc_method->setup_key_block(ssl) ||
393 !ssl->enc_method->change_cipher_state(
394 ssl, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
403 ret = ssl3_send_next_proto(ssl);
408 if (ssl->s3->tlsext_channel_id_valid) {
409 ssl->state = SSL3_ST_CW_CHANNEL_ID_A;
411 ssl->state = SSL3_ST_CW_FINISHED_A;
417 ret = ssl3_send_channel_id(ssl);
421 ssl->state = SSL3_ST_CW_FINISHED_A;
426 ret = ssl3_send_finished(ssl, SSL3_ST_CW_FINISHED_A,
428 ssl->enc_method->client_finished_label,
429 ssl->enc_method->client_finished_label_len);
433 ssl->state = SSL3_ST_CW_FLUSH;
435 if (ssl->hit) {
436 ssl->s3->tmp.next_state = SSL_ST_OK;
442 ret = tls1_record_handshake_hashes_for_channel_id(ssl);
446 if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
447 ssl3_can_false_start(ssl) &&
450 !ssl->s3->initial_handshake_complete) {
451 ssl->s3->tmp.next_state = SSL3_ST_FALSE_START;
454 if (ssl->tlsext_ticket_expected) {
455 ssl->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
457 ssl->s3->tmp.next_state = SSL3_ST_CR_CHANGE;
461 ssl->init_num = 0;
466 ret = ssl3_get_new_session_ticket(ssl);
470 ssl->state = SSL3_ST_CR_CHANGE;
471 ssl->init_num = 0;
476 ret = ssl3_get_cert_status(ssl);
480 ssl->state = SSL3_ST_VERIFY_SERVER_CERT;
481 ssl->init_num = 0;
485 ret = ssl->method->ssl_read_change_cipher_spec(ssl);
490 if (!ssl3_do_change_cipher_spec(ssl)) {
494 ssl->state = SSL3_ST_CR_FINISHED_A;
499 ret = ssl3_get_finished(ssl, SSL3_ST_CR_FINISHED_A,
505 if (ssl->hit) {
506 ssl->state = SSL3_ST_CW_CHANGE_A;
508 ssl->state = SSL_ST_OK;
510 ssl->init_num = 0;
514 ssl->rwstate = SSL_WRITING;
515 if (BIO_flush(ssl->wbio) <= 0) {
519 ssl->rwstate = SSL_NOTHING;
520 ssl->state = ssl->s3->tmp.next_state;
525 if (ssl->tlsext_ticket_expected) {
526 ssl->state = SSL3_ST_CR_SESSION_TICKET_A;
528 ssl->state = SSL3_ST_CR_CHANGE;
530 ssl->s3->tmp.in_false_start = 1;
532 ssl_free_wbio_buffer(ssl);
538 ssl3_cleanup_key_block(ssl);
540 BUF_MEM_free(ssl->init_buf);
541 ssl->init_buf = NULL;
544 ssl_free_wbio_buffer(ssl);
546 const int is_initial_handshake = !ssl->s3->initial_handshake_complete;
548 ssl->init_num = 0;
549 ssl->s3->tmp.in_false_start = 0;
550 ssl->s3->initial_handshake_complete = 1;
554 ssl_update_cache(ssl, SSL_SESS_CACHE_CLIENT);
558 /* ssl->server=0; */
561 cb(ssl, SSL_CB_HANDSHAKE_DONE, 1);
567 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
572 if (!ssl->s3->tmp.reuse_message && !skip) {
573 if (cb != NULL && ssl->state != state) {
574 new_state = ssl->state;
575 ssl->state = state;
576 cb(ssl, SSL_CB_CONNECT_LOOP, 1);
577 ssl->state = new_state;
584 ssl->in_handshake--;
587 cb(ssl, SSL_CB_CONNECT_EXIT, ret);
592 static int ssl3_write_client_cipher_list(SSL *ssl, CBB *out) {
594 ssl_set_client_disabled(ssl);
601 STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl);
608 if ((cipher->algorithm_mkey & ssl->cert->mask_k) ||
609 (cipher->algorithm_auth & ssl->cert->mask_a)) {
613 ssl3_version_from_wire(ssl, ssl->client_version)) {
624 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
630 if (ssl->client_version == SSL3_VERSION &&
631 !ssl->s3->initial_handshake_complete) {
636 ssl->s3->tmp.extensions.sent |= (1u << 0);
639 if ((ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) &&
647 int ssl3_send_client_hello(SSL *ssl) {
648 if (ssl->state == SSL3_ST_CW_CLNT_HELLO_B) {
649 return ssl_do_write(ssl);
654 if (SSL_IS_DTLS(ssl) && !ssl3_init_handshake_buffer(ssl)) {
655 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
662 assert(ssl->state == SSL3_ST_CW_CLNT_HELLO_A);
663 if (!ssl->s3->have_version) {
664 uint16_t max_version = ssl3_get_max_client_version(ssl);
667 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
671 ssl->version = max_version;
672 /* Only set |ssl->client_version| on the initial handshake. Renegotiations,
676 ssl->client_version = max_version;
681 if (ssl->session != NULL &&
682 (ssl->session->session_id_length == 0 || ssl->session->not_resumable ||
683 ssl->session->timeout < (long)(time(NULL) - ssl->session->time) ||
684 (!SSL_IS_DTLS(ssl) && ssl->session->ssl_version > ssl->version) ||
685 (SSL_IS_DTLS(ssl) && ssl->session->ssl_version < ssl->version))) {
686 SSL_set_session(ssl, NULL);
691 if ((!SSL_IS_DTLS(ssl) || !ssl->d1->send_cookie) &&
692 !ssl_fill_hello_random(ssl->s3->client_random,
693 sizeof(ssl->s3->client_random), 0 /* client */)) {
698 int has_session = ssl->session != NULL &&
699 !ssl->s3->initial_handshake_complete;
702 if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
703 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
704 !CBB_add_u16(&cbb, ssl->client_version) ||
705 !CBB_add_bytes(&cbb, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
708 !CBB_add_bytes(&child, ssl->session->session_id,
709 ssl->session->session_id_length))) {
713 if (SSL_IS_DTLS(ssl)) {
715 !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
721 if (!ssl3_write_client_cipher_list(ssl, &cbb) ||
724 !ssl_add_clienthello_tlsext(ssl, &cbb,
725 CBB_len(&cbb) + SSL_HM_HEADER_LENGTH(ssl)) ||
727 !ssl_set_handshake_header(ssl, SSL3_MT_CLIENT_HELLO, length)) {
731 ssl->state = SSL3_ST_CW_CLNT_HELLO_B;
732 return ssl_do_write(ssl);
739 int ssl3_get_server_hello(SSL *ssl) {
742 CERT *ct = ssl->cert;
749 n = ssl->method->ssl_get_message(ssl, SSL3_ST_CR_SRVR_HELLO_A,
764 OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
769 CBS_init(&server_hello, ssl->init_msg, n);
778 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
782 assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
783 if (!ssl->s3->have_version) {
784 if (!ssl3_is_version_enabled(ssl, server_version)) {
785 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
786 ssl->version = server_version;
789 ssl->s3->have_version = 1;
793 ssl->version = server_version;
794 ssl->enc_method = ssl3_get_enc_method(server_version);
795 assert(ssl->enc_method != NULL);
796 /* At this point, the connection's version is known and ssl->version is
798 ssl->s3->have_version = 1;
799 } else if (server_version != ssl->version) {
800 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
806 memcpy(ssl->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
808 assert(ssl->session == NULL || ssl->session->session_id_length > 0);
809 if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
810 CBS_mem_equal(&session_id, ssl->session->session_id,
811 ssl->session->session_id_length)) {
812 if (ssl->sid_ctx_length != ssl->session->sid_ctx_length ||
813 memcmp(ssl->session->sid_ctx, ssl->sid_ctx, ssl->sid_ctx_length)) {
816 OPENSSL_PUT_ERROR(SSL,
820 ssl->hit = 1;
824 ssl->hit = 0;
825 if (!ssl_get_new_session(ssl, 0 /* client */)) {
829 ssl->session->session_id_length = CBS_len(&session_id);
830 memcpy(ssl->session->session_id, CBS_data(&session_id),
838 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
845 ssl3_version_from_wire(ssl, ssl->version)) {
847 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
851 sk = ssl_get_ciphers_by_id(ssl);
855 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
859 if (ssl->hit) {
860 if (ssl->session->cipher != c) {
862 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
865 if (ssl->session->ssl_version != ssl->version) {
867 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
871 ssl->s3->tmp.new_cipher = c;
874 if (!ssl3_init_handshake_hash(ssl)) {
881 if (!SSL_USE_SIGALGS(ssl) || ssl->hit) {
882 ssl3_free_handshake_buffer(ssl);
888 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
893 if (!ssl_parse_serverhello_tlsext(ssl, &server_hello)) {
894 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
902 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
906 if (ssl->hit &&
907 ssl->s3->tmp.extended_master_secret !=
908 ssl->session->extended_master_secret) {
910 if (ssl->session->extended_master_secret) {
911 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
913 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
921 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
927 * certificate for |ssl|. Otherwise, it returns zero and pushes an error on the
929 static int ssl3_check_leaf_certificate(SSL *ssl, X509 *leaf) {
937 const SSL_CIPHER *cipher = ssl->s3->tmp.new_cipher;
941 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE);
952 OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
956 if (!tls1_check_ec_cert(ssl, leaf)) {
957 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
969 int ssl3_get_server_certificate(SSL *ssl) {
978 n = ssl->method->ssl_get_message(ssl, SSL3_ST_CR_CERT_A, SSL3_ST_CR_CERT_B,
979 SSL3_MT_CERTIFICATE, (long)ssl->max_cert_list,
986 CBS_init(&cbs, ssl->init_msg, n);
990 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
998 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1006 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
1014 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
1019 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
1023 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1030 if (!ssl3_check_leaf_certificate(ssl, leaf)) {
1037 sk_X509_pop_free(ssl->session->cert_chain, X509_free);
1038 ssl->session->cert_chain = sk;
1041 X509_free(ssl->session->peer);
1042 ssl->session->peer = X509_up_ref(leaf);
1044 ssl->session->verify_result = ssl->verify_result;
1050 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
1060 int ssl3_get_server_key_exchange(SSL *ssl) {
1073 n = ssl->method->ssl_get_message(ssl, SSL3_ST_CR_KEY_EXCH_A,
1074 SSL3_ST_CR_KEY_EXCH_B, -1, ssl->max_cert_list,
1080 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1081 if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher)) {
1082 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
1083 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1089 if (ssl->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
1092 OPENSSL_free(ssl->s3->tmp.peer_psk_identity_hint);
1093 ssl->s3->tmp.peer_psk_identity_hint = NULL;
1095 ssl->s3->tmp.reuse_message = 1;
1100 CBS_init(&server_key_exchange, ssl->init_msg, n);
1103 alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1104 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
1114 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1128 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
1133 if (!CBS_strdup(&psk_identity_hint, &ssl->s3->tmp.peer_psk_identity_hint)) {
1135 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1149 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1164 ssl->session->key_exchange_info = DH_num_bits(dh);
1165 if (ssl->session->key_exchange_info < 1024) {
1166 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DH_P_LENGTH);
1168 } else if (ssl->session->key_exchange_info > 4096) {
1172 OPENSSL_PUT_ERROR(SSL, SSL_R_DH_P_TOO_LONG);
1176 SSL_ECDH_CTX_init_for_dhe(&ssl->s3->tmp.ecdh_ctx, dh);
1181 if (!CBS_stow(&dh_Ys, &ssl->s3->tmp.peer_key, &peer_key_len)) {
1185 assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff);
1186 ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len;
1197 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1200 ssl->session->key_exchange_info = curve_id;
1203 if (!tls1_check_curve_id(ssl, curve_id)) {
1205 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
1211 if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, curve_id) ||
1212 !CBS_stow(&point, &ssl->s3->tmp.peer_key, &peer_key_len)) {
1216 assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff);
1217 ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len;
1220 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
1231 if (ssl_cipher_has_server_public_key(ssl->s3->tmp.new_cipher)) {
1232 pkey = X509_get_pubkey(ssl->session->peer);
1237 if (SSL_USE_SIGALGS(ssl)) {
1242 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1245 if (!tls12_check_peer_sigalg(ssl, &md, &al, hash, signature, pkey)) {
1248 ssl->s3->tmp.server_key_exchange_hash = hash;
1260 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1265 !EVP_DigestVerifyUpdate(&md_ctx, ssl->s3->client_random,
1267 !EVP_DigestVerifyUpdate(&md_ctx, ssl->s3->server_random,
1275 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
1284 OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
1293 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
1307 int ssl3_get_certificate_request(SSL *ssl) {
1317 n = ssl->method->ssl_get_message(ssl, SSL3_ST_CR_CERT_REQ_A,
1318 SSL3_ST_CR_CERT_REQ_B, -1, ssl->max_cert_list,
1325 ssl->s3->tmp.cert_req = 0;
1327 if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1328 ssl->s3->tmp.reuse_message = 1;
1331 ssl3_free_handshake_buffer(ssl);
1335 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1336 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1337 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_MESSAGE_TYPE);
1341 CBS_init(&cbs, ssl->init_msg, n);
1345 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1351 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1352 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1356 if (!CBS_stow(&certificate_types, &ssl->s3->tmp.certificate_types,
1357 &ssl->s3->tmp.num_certificate_types)) {
1358 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1362 if (SSL_USE_SIGALGS(ssl)) {
1365 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
1366 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1367 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1374 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1375 OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
1383 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1384 OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
1393 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1394 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
1399 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1400 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1405 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1406 OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_LENGTH_MISMATCH);
1411 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1417 ssl->s3->tmp.cert_req = 1;
1418 sk_X509_NAME_pop_free(ssl->s3->tmp.ca_names, X509_NAME_free);
1419 ssl->s3->tmp.ca_names = ca_sk;
1429 int ssl3_get_new_session_ticket(SSL *ssl) {
1431 long n = ssl->method->ssl_get_message(
1432 ssl, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B,
1441 CBS_init(&new_session_ticket, ssl->init_msg, n);
1446 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1455 ssl->tlsext_ticket_expected = 0;
1459 if (ssl->hit) {
1465 if (!SSL_SESSION_to_bytes_for_ticket(ssl->session, &bytes, &bytes_len)) {
1472 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1476 SSL_SESSION_free(ssl->session);
1477 ssl->session = new_session;
1480 if (!CBS_stow(&ticket, &ssl->session->tlsext_tick,
1481 &ssl->session->tlsext_ticklen)) {
1482 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1485 ssl->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
1490 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), ssl->session->session_id,
1491 &ssl->session->session_id_length, EVP_sha256(), NULL)) {
1498 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
1503 int ssl3_get_cert_status(SSL *ssl) {
1509 n = ssl->method->ssl_get_message(
1510 ssl, SSL3_ST_CR_CERT_STATUS_A, SSL3_ST_CR_CERT_STATUS_B,
1517 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1520 ssl->s3->tmp.reuse_message = 1;
1524 CBS_init(&certificate_status, ssl->init_msg, n);
1531 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1535 if (!CBS_stow(&ocsp_response, &ssl->session->ocsp_response,
1536 &ssl->session->ocsp_response_length)) {
1538 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1544 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
1548 int ssl3_get_server_done(SSL *ssl) {
1552 n = ssl->method->ssl_get_message(ssl, SSL3_ST_CR_SRVR_DONE_A,
1563 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1564 OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
1574 int ssl3_send_client_key_exchange(SSL *ssl) {
1575 if (ssl->state == SSL3_ST_CW_KEY_EXCH_B) {
1576 return ssl_do_write(ssl);
1578 assert(ssl->state == SSL3_ST_CW_KEY_EXCH_A);
1583 if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
1584 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl))) {
1588 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1589 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
1595 if (ssl->psk_client_callback == NULL) {
1596 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
1602 psk_len = ssl->psk_client_callback(
1603 ssl, ssl->s3->tmp.peer_psk_identity_hint, identity, sizeof(identity),
1606 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1607 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1612 OPENSSL_free(ssl->session->psk_identity);
1613 ssl->session->psk_identity = BUF_strdup(identity);
1614 if (ssl->session->psk_identity == NULL) {
1615 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1634 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1638 EVP_PKEY *pkey = X509_get_pubkey(ssl->session->peer);
1645 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1650 ssl->session->key_exchange_info = EVP_PKEY_bits(pkey);
1653 pms[0] = ssl->client_version >> 8;
1654 pms[1] = ssl->client_version & 0xff;
1662 if (ssl->version > SSL3_VERSION) {
1674 !ssl_log_rsa_client_key_exchange(ssl, ptr, enc_pms_len, pms, pms_len) ||
1691 !SSL_ECDH_CTX_generate_keypair(&ssl->s3->tmp.ecdh_ctx, &child) ||
1698 if (!SSL_ECDH_CTX_compute_secret(&ssl->s3->tmp.ecdh_ctx, &pms, &pms_len,
1699 &alert, ssl->s3->tmp.peer_key,
1700 ssl->s3->tmp.peer_key_len)) {
1701 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1706 SSL_ECDH_CTX_cleanup(&ssl->s3->tmp.ecdh_ctx);
1707 OPENSSL_free(ssl->s3->tmp.peer_key);
1708 ssl->s3->tmp.peer_key = NULL;
1715 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1720 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1721 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1740 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1753 !ssl_set_handshake_header(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE, length)) {
1756 ssl->state = SSL3_ST_CW_KEY_EXCH_B;
1758 ssl->session->master_key_length = ssl->enc_method->generate_master_secret(
1759 ssl, ssl->session->master_key, pms, pms_len);
1760 if (ssl->session->master_key_length == 0) {
1763 ssl->session->extended_master_secret = ssl->s3->tmp.extended_master_secret;
1768 return ssl_do_write(ssl);
1778 int ssl3_send_cert_verify(SSL *ssl) {
1779 if (ssl->state == SSL3_ST_CW_CERT_VRFY_C) {
1780 return ssl_do_write(ssl);
1784 if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
1785 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl))) {
1789 assert(ssl_has_private_key(ssl));
1791 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
1794 if (ssl->state == SSL3_ST_CW_CERT_VRFY_A) {
1797 if (SSL_USE_SIGALGS(ssl)) {
1798 md = tls1_choose_signing_digest(ssl);
1799 if (!tls12_add_sigandhash(ssl, &cbb, md)) {
1800 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1809 if (!ssl3_cert_verify_hash(ssl, digest, &digest_len, &md,
1810 ssl_private_key_type(ssl))) {
1815 ssl3_free_handshake_buffer(ssl);
1823 sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len, md,
1826 assert(ssl->state == SSL3_ST_CW_CERT_VRFY_B);
1831 if ((SSL_USE_SIGALGS(ssl) && !CBB_did_write(&cbb, 2)) ||
1837 ssl_private_key_sign_complete(ssl, ptr, &sig_len, max_sig_len);
1842 ssl->rwstate = SSL_NOTHING;
1845 ssl->rwstate = SSL_NOTHING;
1848 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
1849 ssl->state = SSL3_ST_CW_CERT_VRFY_B;
1856 !ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE_VERIFY, length)) {
1860 ssl->state = SSL3_ST_CW_CERT_VRFY_C;
1861 return ssl_do_write(ssl);
1870 static int ssl3_has_client_certificate(SSL *ssl) {
1871 return ssl->cert && ssl->cert->x509 && ssl_has_private_key(ssl);
1874 int ssl3_send_client_certificate(SSL *ssl) {
1879 if (ssl->state == SSL3_ST_CW_CERT_A) {
1881 if (ssl->cert->cert_cb) {
1882 i = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
1884 ssl->rwstate = SSL_X509_LOOKUP;
1888 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1891 ssl->rwstate = SSL_NOTHING;
1894 if (ssl3_has_client_certificate(ssl)) {
1895 ssl->state = SSL3_ST_CW_CERT_C;
1897 ssl->state = SSL3_ST_CW_CERT_B;
1902 if (ssl->state == SSL3_ST_CW_CERT_B) {
1904 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
1906 i = ssl_do_client_cert_cb(ssl, &x509, &pkey);
1908 ssl->rwstate = SSL_X509_LOOKUP;
1911 ssl->rwstate = SSL_NOTHING;
1913 ssl->state = SSL3_ST_CW_CERT_B;
1914 if (!SSL_use_certificate(ssl, x509) || !SSL_use_PrivateKey(ssl, pkey)) {
1919 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
1924 if (i && !ssl3_has_client_certificate(ssl)) {
1928 if (ssl->version == SSL3_VERSION) {
1929 ssl->s3->tmp.cert_req = 0;
1930 ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
1933 ssl->s3->tmp.cert_req = 2;
1936 ssl3_free_handshake_buffer(ssl);
1941 ssl->state = SSL3_ST_CW_CERT_C;
1944 if (ssl->state == SSL3_ST_CW_CERT_C) {
1945 if (ssl->s3->tmp.cert_req == 2) {
1947 uint8_t *p = ssl_handshake_start(ssl);
1949 if (!ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE, 3)) {
1952 } else if (!ssl3_output_cert_chain(ssl)) {
1955 ssl->state = SSL3_ST_CW_CERT_D;
1959 return ssl_do_write(ssl);
1962 int ssl3_send_next_proto(SSL *ssl) {
1963 if (ssl->state == SSL3_ST_CW_NEXT_PROTO_B) {
1964 return ssl_do_write(ssl);
1967 assert(ssl->state == SSL3_ST_CW_NEXT_PROTO_A);
1970 size_t padding_len = 32 - ((ssl->next_proto_negotiated_len + 2) % 32);
1975 if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
1976 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
1978 !CBB_add_bytes(&child, ssl->next_proto_negotiated,
1979 ssl->next_proto_negotiated_len) ||
1983 !ssl_set_handshake_header(ssl, SSL3_MT_NEXT_PROTO, length)) {
1984 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1989 ssl->state = SSL3_ST_CW_NEXT_PROTO_B;
1990 return ssl_do_write(ssl);
1993 int ssl3_send_channel_id(SSL *ssl) {
1994 if (ssl->state == SSL3_ST_CW_CHANNEL_ID_B) {
1995 return ssl_do_write(ssl);
1998 assert(ssl->state == SSL3_ST_CW_CHANNEL_ID_A);
2000 if (ssl->tlsext_channel_id_private == NULL &&
2001 ssl->ctx->channel_id_cb != NULL) {
2003 ssl->ctx->channel_id_cb(ssl, &key);
2005 !SSL_set1_tls_channel_id(ssl, key)) {
2012 if (ssl->tlsext_channel_id_private == NULL) {
2013 ssl->rwstate = SSL_CHANNEL_ID_LOOKUP;
2016 ssl->rwstate = SSL_NOTHING;
2018 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
2020 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2037 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
2049 if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
2050 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
2058 !ssl_set_handshake_header(ssl, SSL3_MT_ENCRYPTED_EXTENSIONS, length)) {
2059 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2064 ssl->state = SSL3_ST_CW_CHANNEL_ID_B;
2065 ret = ssl_do_write(ssl);
2074 int ssl_do_client_cert_cb(SSL *ssl, X509 **out_x509, EVP_PKEY **out_pkey) {
2075 if (ssl->ctx->client_cert_cb == NULL) {
2078 return ssl->ctx->client_cert_cb(ssl, out_x509, out_pkey);
2081 int ssl3_verify_server_cert(SSL *ssl) {
2082 ssl, ssl->session->cert_chain);
2083 if (ssl->verify_mode != SSL_VERIFY_NONE && ret <= 0) {
2084 int al = ssl_verify_alarm_type(ssl->verify_result);
2085 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
2086 OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
2089 ERR_clear_error(); /* but we keep ssl->verify_result */