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
149 #include <openssl/ssl.h>
177 int ssl3_accept(SSL *ssl) {
180 void (*cb)(const SSL *ssl, int type, int value) = NULL;
184 assert(ssl->handshake_func == ssl3_accept);
185 assert(ssl->server);
186 assert(!SSL_IS_DTLS(ssl));
191 if (ssl->info_callback != NULL) {
192 cb = ssl->info_callback;
193 } else if (ssl->ctx->info_callback != NULL) {
194 cb = ssl->ctx->info_callback;
197 ssl->in_handshake++;
199 if (ssl->cert == NULL) {
200 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
205 state = ssl->state;
207 switch (ssl->state) {
210 cb(ssl, SSL_CB_HANDSHAKE_START, 1);
213 if (ssl->init_buf == NULL) {
219 ssl->init_buf = buf;
222 ssl->init_num = 0;
226 if (!ssl_init_wbio_buffer(ssl, 1)) {
231 if (!ssl3_init_handshake_buffer(ssl)) {
232 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
237 if (!ssl->s3->have_version) {
238 ssl->state = SSL3_ST_SR_INITIAL_BYTES;
240 ssl->state = SSL3_ST_SR_CLNT_HELLO_A;
245 ret = ssl3_get_initial_bytes(ssl);
249 /* ssl3_get_initial_bytes sets ssl->state to one of
254 ret = ssl3_get_v2_client_hello(ssl);
258 ssl->state = SSL3_ST_SR_CLNT_HELLO_A;
265 ssl->shutdown = 0;
266 ret = ssl3_get_client_hello(ssl);
270 ssl->state = SSL3_ST_SW_SRVR_HELLO_A;
271 ssl->init_num = 0;
276 ret = ssl3_send_server_hello(ssl);
280 if (ssl->hit) {
281 if (ssl->tlsext_ticket_expected) {
282 ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
284 ssl->state = SSL3_ST_SW_CHANGE_A;
287 ssl->state = SSL3_ST_SW_CERT_A;
289 ssl->init_num = 0;
294 if (ssl_cipher_has_server_public_key(ssl->s3->tmp.new_cipher)) {
295 ret = ssl3_send_server_certificate(ssl);
299 if (ssl->s3->tmp.certificate_status_expected) {
300 ssl->state = SSL3_ST_SW_CERT_STATUS_A;
302 ssl->state = SSL3_ST_SW_KEY_EXCH_A;
306 ssl->state = SSL3_ST_SW_KEY_EXCH_A;
308 ssl->init_num = 0;
313 ret = ssl3_send_certificate_status(ssl);
317 ssl->state = SSL3_ST_SW_KEY_EXCH_A;
318 ssl->init_num = 0;
324 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
333 if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) ||
334 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
335 ret = ssl3_send_server_key_exchange(ssl);
343 ssl->state = SSL3_ST_SW_CERT_REQ_A;
344 ssl->init_num = 0;
349 if (ssl->s3->tmp.cert_request) {
350 ret = ssl3_send_certificate_request(ssl);
357 ssl->state = SSL3_ST_SW_SRVR_DONE_A;
358 ssl->init_num = 0;
363 ret = ssl3_send_server_done(ssl);
367 ssl->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
368 ssl->state = SSL3_ST_SW_FLUSH;
369 ssl->init_num = 0;
378 ssl->rwstate = SSL_WRITING;
379 if (BIO_flush(ssl->wbio) <= 0) {
383 ssl->rwstate = SSL_NOTHING;
385 ssl->state = ssl->s3->tmp.next_state;
390 if (ssl->s3->tmp.cert_request) {
391 ret = ssl3_get_client_certificate(ssl);
396 ssl->init_num = 0;
397 ssl->state = SSL3_ST_SR_KEY_EXCH_A;
403 ret = ssl3_get_client_key_exchange(ssl);
407 ssl->state = SSL3_ST_SR_CERT_VRFY_A;
408 ssl->init_num = 0;
413 ret = ssl3_get_cert_verify(ssl);
418 ssl->state = SSL3_ST_SR_CHANGE;
419 ssl->init_num = 0;
423 ret = ssl->method->ssl_read_change_cipher_spec(ssl);
428 if (!ssl3_do_change_cipher_spec(ssl)) {
433 if (ssl->s3->next_proto_neg_seen) {
434 ssl->state = SSL3_ST_SR_NEXT_PROTO_A;
435 } else if (ssl->s3->tlsext_channel_id_valid) {
436 ssl->state = SSL3_ST_SR_CHANNEL_ID_A;
438 ssl->state = SSL3_ST_SR_FINISHED_A;
444 ret = ssl3_get_next_proto(ssl);
448 ssl->init_num = 0;
449 if (ssl->s3->tlsext_channel_id_valid) {
450 ssl->state = SSL3_ST_SR_CHANNEL_ID_A;
452 ssl->state = SSL3_ST_SR_FINISHED_A;
458 ret = ssl3_get_channel_id(ssl);
462 ssl->init_num = 0;
463 ssl->state = SSL3_ST_SR_FINISHED_A;
468 ret = ssl3_get_finished(ssl, SSL3_ST_SR_FINISHED_A,
474 if (ssl->hit) {
475 ssl->state = SSL_ST_OK;
476 } else if (ssl
477 ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
479 ssl->state = SSL3_ST_SW_CHANGE_A;
482 * hashes in |ssl->session| in case we need them to verify a ChannelID
484 if (!ssl->hit && ssl->s3->tlsext_channel_id_valid) {
485 ret = tls1_record_handshake_hashes_for_channel_id(ssl);
490 ssl->init_num = 0;
495 ret = ssl3_send_new_session_ticket(ssl);
499 ssl->state = SSL3_ST_SW_CHANGE_A;
500 ssl->init_num = 0;
505 ssl->session->cipher = ssl->s3->tmp.new_cipher;
506 if (!ssl->enc_method->setup_key_block(ssl)) {
511 ret = ssl3_send_change_cipher_spec(ssl, SSL3_ST_SW_CHANGE_A,
516 ssl->state = SSL3_ST_SW_FINISHED_A;
517 ssl->init_num = 0;
519 if (!ssl->enc_method->change_cipher_state(
520 ssl, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
528 ret = ssl3_send_finished(ssl, SSL3_ST_SW_FINISHED_A,
530 ssl->enc_method->server_finished_label,
531 ssl->enc_method->server_finished_label_len);
535 ssl->state = SSL3_ST_SW_FLUSH;
536 if (ssl->hit) {
537 ssl->s3->tmp.next_state = SSL3_ST_SR_CHANGE;
539 ssl->s3->tmp.next_state = SSL_ST_OK;
541 ssl->init_num = 0;
546 ssl3_cleanup_key_block(ssl);
548 BUF_MEM_free(ssl->init_buf);
549 ssl->init_buf = NULL;
552 ssl_free_wbio_buffer(ssl);
554 ssl->init_num = 0;
558 if (ssl->ctx->retain_only_sha256_of_client_certs) {
559 X509_free(ssl->session->peer);
560 ssl->session->peer = NULL;
561 sk_X509_pop_free(ssl->session->cert_chain, X509_free);
562 ssl->session->cert_chain = NULL;
565 ssl->s3->initial_handshake_complete = 1;
567 ssl_update_cache(ssl, SSL_SESS_CACHE_SERVER);
570 cb(ssl, SSL_CB_HANDSHAKE_DONE, 1);
577 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
582 if (!ssl->s3->tmp.reuse_message && !skip && cb != NULL &&
583 ssl->state != state) {
584 new_state = ssl->state;
585 ssl->state = state;
586 cb(ssl, SSL_CB_ACCEPT_LOOP, 1);
587 ssl->state = new_state;
593 ssl->in_handshake--;
596 cb(ssl, SSL_CB_ACCEPT_EXIT, ret);
601 int ssl3_get_initial_bytes(SSL *ssl) {
605 int ret = ssl_read_buffer_extend_to(ssl, SSL3_RT_HEADER_LENGTH);
609 assert(ssl_read_buffer_len(ssl) == SSL3_RT_HEADER_LENGTH);
610 const uint8_t *p = ssl_read_buffer(ssl);
619 OPENSSL_PUT_ERROR(SSL, SSL_R_HTTP_REQUEST);
623 OPENSSL_PUT_ERROR(SSL, SSL_R_HTTPS_PROXY_REQUEST);
631 ssl->state = SSL3_ST_SR_V2_CLIENT_HELLO;
636 ssl->state = SSL3_ST_SR_CLNT_HELLO_A;
640 int ssl3_get_v2_client_hello(SSL *ssl) {
651 assert(ssl_read_buffer_len(ssl) >= SSL3_RT_HEADER_LENGTH);
652 p = ssl_read_buffer(ssl);
655 OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
662 OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_LENGTH_MISMATCH);
667 ret = ssl_read_buffer_extend_to(ssl, 2 + msg_length);
671 assert(ssl_read_buffer_len(ssl) == msg_length + 2);
672 CBS_init(&v2_client_hello, ssl_read_buffer(ssl) + 2, msg_length);
676 if (!ssl3_update_handshake_hash(ssl, CBS_data(&v2_client_hello),
680 if (ssl->msg_callback) {
681 ssl->msg_callback(0, SSL2_VERSION, 0, CBS_data(&v2_client_hello),
682 CBS_len(&v2_client_hello), ssl, ssl->msg_callback_arg);
694 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
713 if (!CBB_init_fixed(&client_hello, (uint8_t *)ssl->init_buf->data,
714 ssl->init_buf->max) ||
723 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
732 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
742 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
751 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
756 ssl->s3->tmp.reuse_message = 1;
757 ssl->s3->tmp.message_type = SSL3_MT_CLIENT_HELLO;
759 ssl->s3->tmp.message_size = len - 4;
762 ssl_read_buffer_consume(ssl, 2 + msg_length);
763 ssl_read_buffer_discard(ssl);
768 int ssl3_get_client_hello(SSL *ssl) {
783 switch (ssl->state) {
786 n = ssl->method->ssl_get_message(
787 ssl, SSL3_ST_SR_CLNT_HELLO_A, SSL3_ST_SR_CLNT_HELLO_B,
795 ssl->state = SSL3_ST_SR_CLNT_HELLO_C;
802 n = ssl->init_num;
805 early_ctx.ssl = ssl;
806 early_ctx.client_hello = ssl->init_msg;
810 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
814 if (ssl->state == SSL3_ST_SR_CLNT_HELLO_C &&
815 ssl->ctx->select_certificate_cb != NULL) {
816 ssl->state = SSL3_ST_SR_CLNT_HELLO_D;
817 switch (ssl->ctx->select_certificate_cb(&early_ctx)) {
819 ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
825 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
832 ssl->state = SSL3_ST_SR_CLNT_HELLO_D;
836 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
840 CBS_init(&client_hello, ssl->init_msg, n);
846 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
852 ssl->client_version = client_version;
855 memcpy(ssl->s3->client_random, CBS_data(&client_random), SSL3_RANDOM_SIZE);
857 if (SSL_IS_DTLS(ssl)) {
863 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
873 if (!ssl->s3->have_version) {
875 uint16_t version = ssl3_get_mutual_version(ssl, client_version);
877 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
878 ssl->version = ssl->client_version;
882 ssl->version = version;
883 ssl->enc_method = ssl3_get_enc_method(version);
884 assert(ssl->enc_method != NULL);
885 /* At this point, the connection's version is known and |ssl->version| is
887 ssl->s3->have_version = 1;
888 } else if (SSL_IS_DTLS(ssl) ? (ssl->client_version > ssl->version)
889 : (ssl->client_version < ssl->version)) {
890 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER);
895 ssl->hit = 0;
897 switch (ssl_get_prev_session(ssl, &session, &send_new_ticket, &early_ctx)) {
903 ssl->rwstate = SSL_PENDING_SESSION;
906 ssl->tlsext_ticket_expected = send_new_ticket;
915 ssl->version != SSL3_VERSION &&
927 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
931 ssl->hit =
934 ssl->version == session->ssl_version &&
940 if (ssl->hit) {
942 SSL_SESSION_free(ssl->session);
943 ssl->session = session;
946 ssl->verify_result = ssl->session->verify_result;
948 if (!ssl_get_new_session(ssl, 1 /* server */)) {
953 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
954 ssl->session->session_id_length = 0;
958 if (ssl->ctx->dos_protection_cb != NULL &&
959 ssl->ctx->dos_protection_cb(&early_ctx) == 0) {
962 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
972 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
976 ciphers = ssl_bytes_to_cipher_list(ssl, &cipher_suites);
982 if (ssl->hit) {
985 uint32_t id = ssl->session->cipher->id;
999 OPENSSL_PUT_ERROR(SSL, SSL_R_REQUIRED_CIPHER_MISSING);
1008 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMPRESSION_SPECIFIED);
1013 if (ssl->version >= SSL3_VERSION &&
1014 !ssl_parse_clienthello_tlsext(ssl, &client_hello)) {
1015 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1023 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
1027 if (have_extended_master_secret != ssl->s3->tmp.extended_master_secret) {
1029 OPENSSL_PUT_ERROR(SSL, SSL_R_EMS_STATE_INCONSISTENT);
1034 if (!ssl->hit) {
1037 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_PASSED);
1042 if (ssl->cert->cert_cb) {
1043 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
1046 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
1050 ssl->rwstate = SSL_X509_LOOKUP;
1053 ssl->rwstate = SSL_NOTHING;
1055 c = ssl3_choose_cipher(ssl, ciphers, ssl_get_cipher_preferences(ssl));
1059 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
1062 ssl->s3->tmp.new_cipher = c;
1065 ssl->s3->tmp.cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
1067 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
1068 ssl->s3->tlsext_channel_id_valid) {
1069 ssl->s3->tmp.cert_request = 0;
1072 if (ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK) {
1073 ssl->s3->tmp.cert_request = 0;
1077 ssl->s3->tmp.new_cipher = ssl->session->cipher;
1078 ssl->s3->tmp.cert_request = 0;
1082 if (!ssl3_init_handshake_hash(ssl)) {
1088 if (!SSL_USE_SIGALGS(ssl) || !ssl->s3->tmp.cert_request) {
1089 ssl3_free_handshake_buffer(ssl);
1097 * ssl version is set - sslv3
1098 * ssl->session - The ssl session has been setup.
1099 * ssl->hit - session reuse flag
1100 * ssl->tmp.new_cipher - the new cipher to use. */
1106 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
1115 int ssl3_send_server_hello(SSL *ssl) {
1116 if (ssl->state == SSL3_ST_SW_SRVR_HELLO_B) {
1117 return ssl_do_write(ssl);
1120 assert(ssl->state == SSL3_ST_SW_SRVR_HELLO_A);
1124 if (ssl->s3->tlsext_channel_id_valid &&
1125 (ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
1126 ssl->s3->tlsext_channel_id_valid = 0;
1132 if (ssl->hit && ssl->session->original_handshake_hash_len == 0) {
1133 ssl->s3->tlsext_channel_id_valid = 0;
1136 if (!ssl_fill_hello_random(ssl->s3->server_random, SSL3_RANDOM_SIZE,
1138 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1145 if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
1146 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
1147 !CBB_add_u16(&cbb, ssl->version) ||
1148 !CBB_add_bytes(&cbb, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1150 !CBB_add_bytes(&session_id, ssl->session->session_id,
1151 ssl->session->session_id_length) ||
1152 !CBB_add_u16(&cbb, ssl_cipher_get_value(ssl->s3->tmp.new_cipher)) ||
1154 !ssl_add_serverhello_tlsext(ssl, &cbb) ||
1156 !ssl_set_handshake_header(ssl, SSL3_MT_SERVER_HELLO, length)) {
1157 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1162 ssl->state = SSL3_ST_SW_SRVR_HELLO_B;
1163 return ssl_do_write(ssl);
1166 int ssl3_send_certificate_status(SSL *ssl) {
1167 if (ssl->state == SSL3_ST_SW_CERT_STATUS_A) {
1172 if (!CBB_init_fixed(&out, ssl_handshake_start(ssl),
1173 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
1176 !CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
1177 ssl->ctx->ocsp_response_length) ||
1179 !ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE_STATUS, length)) {
1180 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1185 ssl->state = SSL3_ST_SW_CERT_STATUS_B;
1189 return ssl_do_write(ssl);
1192 int ssl3_send_server_done(SSL *ssl) {
1193 if (ssl->state == SSL3_ST_SW_SRVR_DONE_A) {
1194 if (!ssl_set_handshake_header(ssl, SSL3_MT_SERVER_DONE, 0)) {
1197 ssl->state = SSL3_ST_SW_SRVR_DONE_B;
1201 return ssl_do_write(ssl);
1204 int ssl3_send_server_key_exchange(SSL *ssl) {
1205 if (ssl->state == SSL3_ST_SW_KEY_EXCH_C) {
1206 return ssl_do_write(ssl);
1210 if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
1211 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl))) {
1215 if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) {
1217 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1218 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
1223 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
1225 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
1233 DH *params = ssl->cert->dh_tmp;
1234 if (params == NULL && ssl->cert->dh_tmp_cb != NULL) {
1235 params = ssl->cert->dh_tmp_cb(ssl, 0, 1024);
1238 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY);
1239 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1242 ssl->session->key_exchange_info = DH_num_bits(params);
1250 SSL_ECDH_CTX_init_for_dhe(&ssl->s3->tmp.ecdh_ctx, dh);
1256 !SSL_ECDH_CTX_generate_keypair(&ssl->s3->tmp.ecdh_ctx, &child)) {
1262 if (!tls1_get_shared_curve(ssl, &curve_id)) {
1263 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY);
1264 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1267 ssl->session->key_exchange_info = curve_id;
1270 if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, curve_id) ||
1274 !SSL_ECDH_CTX_generate_keypair(&ssl->s3->tmp.ecdh_ctx, &child)) {
1282 * TODO(davidben): When |ssl->init_buf| is gone, come up with a simpler
1284 } else if (!CBB_did_write(&cbb, ssl->init_num - SSL_HM_HEADER_LENGTH(ssl))) {
1289 if (ssl_cipher_has_server_public_key(ssl->s3->tmp.new_cipher)) {
1290 if (!ssl_has_private_key(ssl)) {
1291 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1295 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
1298 if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) {
1308 if (SSL_USE_SIGALGS(ssl)) {
1309 md = tls1_choose_signing_digest(ssl);
1310 if (!tls12_add_sigandhash(ssl, &cbb, md)) {
1311 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1312 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1315 } else if (ssl_private_key_type(ssl) == EVP_PKEY_RSA) {
1328 EVP_DigestUpdate(&md_ctx, ssl->s3->client_random, SSL3_RANDOM_SIZE) &&
1329 EVP_DigestUpdate(&md_ctx, ssl->s3->server_random, SSL3_RANDOM_SIZE) &&
1339 sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len, md,
1342 assert(ssl->state == SSL3_ST_SW_KEY_EXCH_B);
1351 ssl_private_key_sign_complete(ssl, ptr, &sig_len, max_sig_len);
1356 ssl->rwstate = SSL_NOTHING;
1362 ssl->rwstate = SSL_NOTHING;
1368 ssl->init_num = SSL_HM_HEADER_LENGTH(ssl) + CBB_len(&cbb);
1369 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
1370 ssl->state = SSL3_ST_SW_KEY_EXCH_B;
1377 !ssl_set_handshake_header(ssl, SSL3_MT_SERVER_KEY_EXCHANGE, length)) {
1380 ssl->state = SSL3_ST_SW_KEY_EXCH_C;
1381 return ssl_do_write(ssl);
1388 int ssl3_send_certificate_request(SSL *ssl) {
1396 if (ssl->state == SSL3_ST_SW_CERT_REQ_A) {
1397 buf = ssl->init_buf;
1399 d = p = ssl_handshake_start(ssl);
1403 n = ssl3_get_req_cert_type(ssl, p);
1408 if (SSL_USE_SIGALGS(ssl)) {
1410 nl = tls12_get_psigalgs(ssl, &psigs);
1421 sk = SSL_get_client_CA_list(ssl);
1427 if (!BUF_MEM_grow_clean(buf, SSL_HM_HEADER_LENGTH(ssl) + n + j + 2)) {
1428 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
1431 p = ssl_handshake_start(ssl) + n;
1440 p = ssl_handshake_start(ssl) + off;
1443 if (!ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE_REQUEST, n)) {
1446 ssl->state = SSL3_ST_SW_CERT_REQ_B;
1450 return ssl_do_write(ssl);
1456 int ssl3_get_client_key_exchange(SSL *ssl) {
1468 if (ssl->state == SSL3_ST_SR_KEY_EXCH_A ||
1469 ssl->state == SSL3_ST_SR_KEY_EXCH_B) {
1471 const long n = ssl->method->ssl_get_message(
1472 ssl, SSL3_ST_SR_KEY_EXCH_A, SSL3_ST_SR_KEY_EXCH_B,
1479 CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num);
1480 alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1481 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
1491 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1496 if (ssl->psk_server_callback == NULL) {
1497 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB);
1504 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
1509 if (!CBS_strdup(&psk_identity, &ssl->session->psk_identity)) {
1511 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1516 psk_len = ssl->psk_server_callback(ssl, ssl->session->psk_identity, psk,
1519 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1524 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1534 const size_t rsa_size = ssl_private_key_max_signature_len(ssl);
1537 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1543 if (ssl->state == SSL3_ST_SR_KEY_EXCH_B) {
1544 if (!ssl_has_private_key(ssl) ||
1545 ssl_private_key_type(ssl) != EVP_PKEY_RSA) {
1547 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE);
1551 if (ssl->version > SSL3_VERSION) {
1556 OPENSSL_PUT_ERROR(SSL,
1567 ssl, decrypt_buf, &decrypt_len, rsa_size,
1571 assert(ssl->state == SSL3_ST_SR_KEY_EXCH_C);
1574 ssl, decrypt_buf, &decrypt_len, rsa_size);
1579 ssl->rwstate = SSL_NOTHING;
1582 ssl->rwstate = SSL_NOTHING;
1585 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
1586 ssl->state = SSL3_ST_SR_KEY_EXCH_C;
1597 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1608 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1625 (unsigned)(ssl->client_version >> 8));
1627 (unsigned)(ssl->client_version & 0xff));
1652 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1658 if (!SSL_ECDH_CTX_compute_secret(&ssl->s3->tmp.ecdh_ctx, &premaster_secret,
1666 SSL_ECDH_CTX_cleanup(&ssl->s3->tmp.ecdh_ctx);
1673 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1679 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE);
1697 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1709 ssl->session->master_key_length = ssl->enc_method->generate_master_secret(
1710 ssl, ssl->session->master_key, premaster_secret, premaster_secret_len);
1711 if (ssl->session->master_key_length == 0) {
1714 ssl->session->extended_master_secret = ssl->s3->tmp.extended_master_secret;
1721 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
1732 int ssl3_get_cert_verify(SSL *ssl) {
1736 X509 *peer = ssl->session->peer;
1747 ssl3_free_handshake_buffer(ssl);
1751 n = ssl->method->ssl_get_message(
1752 ssl, SSL3_ST_SR_CERT_VRFY_A, SSL3_ST_SR_CERT_VRFY_B,
1768 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1772 CBS_init(&certificate_verify, ssl->init_msg, n);
1775 if (SSL_USE_SIGALGS(ssl)) {
1780 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1783 if (!tls12_check_peer_sigalg(ssl, &md, &al, hash, signature_type, pkey)) {
1789 if (!ssl3_cert_verify_hash(ssl, digest, &digest_length, &md, pkey->type)) {
1795 ssl3_free_handshake_buffer(ssl);
1796 if (!ssl3_hash_current_message(ssl)) {
1804 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1817 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
1825 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
1835 int ssl3_get_client_certificate(SSL *ssl) {
1844 n = ssl->method->ssl_get_message(ssl, SSL3_ST_SR_CERT_A, SSL3_ST_SR_CERT_B,
1845 -1, (long)ssl->max_cert_list,
1852 if (ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
1853 if ((ssl->verify_mode & SSL_VERIFY_PEER) &&
1854 (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
1855 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1861 if (ssl->version > SSL3_VERSION && ssl->s3->tmp.cert_request) {
1862 OPENSSL_PUT_ERROR(SSL,
1867 ssl->s3->tmp.reuse_message = 1;
1872 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1874 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_MESSAGE_TYPE);
1878 CBS_init(&certificate_msg, ssl->init_msg, n);
1882 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1889 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1899 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1903 if (is_first_certificate && ssl->ctx->retain_only_sha256_of_client_certs) {
1908 SHA256_Final(ssl->session->peer_sha256, &sha256);
1909 ssl->session->peer_sha256_valid = 1;
1918 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
1923 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
1927 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1935 ssl3_free_handshake_buffer(ssl);
1938 if (ssl->version == SSL3_VERSION) {
1940 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
1942 } else if ((ssl->verify_mode & SSL_VERIFY_PEER) &&
1943 (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
1945 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1950 i = ssl_verify_cert_chain(ssl, sk);
1952 al = ssl_verify_alarm_type(ssl->verify_result);
1953 OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
1958 X509_free(ssl->session->peer);
1959 ssl->session->peer = sk_X509_shift(sk);
1960 ssl->session->verify_result = ssl->verify_result;
1962 sk_X509_pop_free(ssl->session->cert_chain, X509_free);
1963 ssl->session->cert_chain = sk;
1973 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
1982 int ssl3_send_server_certificate(SSL *ssl) {
1983 if (ssl->state == SSL3_ST_SW_CERT_A) {
1984 if (!ssl3_output_cert_chain(ssl)) {
1987 ssl->state = SSL3_ST_SW_CERT_B;
1991 return ssl_do_write(ssl);
1995 int ssl3_send_new_session_ticket(SSL *ssl) {
2005 if (ssl->state == SSL3_ST_SW_SESSION_TICKET_A) {
2009 SSL_CTX *tctx = ssl->initial_ctx;
2018 if (!SSL_SESSION_to_bytes_for_ticket(ssl->session, &session,
2032 p = ssl_handshake_start(ssl);
2040 len = p - ssl_handshake_start(ssl);
2041 if (!ssl_set_handshake_header(ssl, SSL3_MT_NEWSESSION_TICKET, len)) {
2044 ssl->state = SSL3_ST_SW_SESSION_TICKET_B;
2045 return ssl_do_write(ssl);
2051 if (!BUF_MEM_grow(ssl->init_buf, SSL_HM_HEADER_LENGTH(ssl) + 6 +
2055 p = ssl_handshake_start(ssl);
2059 if (tctx->tlsext_ticket_key_cb(ssl, key_name, iv, &ctx, &hctx,
2077 l2n(ssl->hit ? 0 : ssl->session->timeout, p);
2106 len = p - ssl_handshake_start(ssl);
2108 p = ssl_handshake_start(ssl) + 4;
2110 if (!ssl_set_handshake_header(ssl, SSL3_MT_NEWSESSION_TICKET, len)) {
2113 ssl->state = SSL3_ST_SW_SESSION_TICKET_B;
2117 ret = ssl_do_write(ssl);
2128 int ssl3_get_next_proto(SSL *ssl) {
2135 if (!ssl->s3->next_proto_neg_seen) {
2136 OPENSSL_PUT_ERROR(SSL, SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
2140 n = ssl->method->ssl_get_message(ssl, SSL3_ST_SR_NEXT_PROTO_A,
2149 CBS_init(&next_protocol, ssl->init_msg, n);
2159 !CBS_stow(&selected_protocol, &ssl->next_proto_negotiated,
2160 &ssl->next_proto_negotiated_len)) {
2168 int ssl3_get_channel_id(SSL *ssl) {
2182 n = ssl->method->ssl_get_message(
2183 ssl, SSL3_ST_SR_CHANNEL_ID_A, SSL3_ST_SR_CHANNEL_ID_B,
2193 if (!tls1_channel_id_hash(ssl, channel_id_hash, &channel_id_hash_len)) {
2198 if (!ssl3_hash_current_message(ssl)) {
2202 CBS_init(&encrypted_extensions, ssl->init_msg, n);
2221 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_MESSAGE);
2227 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
2262 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
2263 ssl->s3->tlsext_channel_id_valid = 0;
2267 memcpy(ssl->s3->tlsext_channel_id, p, 64);