int ssl3_write_pending(SSL *s, int type, const unsigned char *buf, size_t len,
                       size_t *written)
{
    int i;
    SSL3_BUFFER *wb = s->rlayer.wbuf;
    size_t currbuf = 0;
    size_t tmpwrit = 0;
    if ((s->rlayer.wpend_tot > len)
        || ((s->rlayer.wpend_buf != buf) &&
            !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER))
        || (s->rlayer.wpend_type != type)) {
        SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BAD_WRITE_RETRY);
        return -1;
    }
    for (;;) {
        if (SSL3_BUFFER_get_left(&wb[currbuf]) == 0
            && currbuf < s->rlayer.numwpipes - 1) {
            currbuf++;
            continue;
        }
        clear_sys_error();
        if (s->wbio != NULL) {
            s->rwstate = SSL_WRITING;
            i = BIO_write(s->wbio, (char *)
                          &(SSL3_BUFFER_get_buf(&wb[currbuf])
                            [SSL3_BUFFER_get_offset(&wb[currbuf])]),
                          (unsigned int)SSL3_BUFFER_get_left(&wb[currbuf]));
            if (i >= 0)
                tmpwrit = i;
        } else {
            SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BIO_NOT_SET);
            i = -1;
        }
        if (i > 0 && tmpwrit == SSL3_BUFFER_get_left(&wb[currbuf])) {
            SSL3_BUFFER_set_left(&wb[currbuf], 0);
            SSL3_BUFFER_add_offset(&wb[currbuf], tmpwrit);
            if (currbuf + 1 < s->rlayer.numwpipes)
                continue;
            s->rwstate = SSL_NOTHING;
            *written = s->rlayer.wpend_ret;
            return 1;
        } else if (i <= 0) {
            if (SSL_IS_DTLS(s)) {
                SSL3_BUFFER_set_left(&wb[currbuf], 0);
            }
            return (i);
        }
        SSL3_BUFFER_add_offset(&wb[currbuf], tmpwrit);
        SSL3_BUFFER_sub_left(&wb[currbuf], tmpwrit);
    }
}
['static SUB_STATE_RETURN write_state_machine(SSL *s)\n{\n    OSSL_STATEM *st = &s->statem;\n    int ret;\n    WRITE_TRAN(*transition) (SSL *s);\n    WORK_STATE(*pre_work) (SSL *s, WORK_STATE wst);\n    WORK_STATE(*post_work) (SSL *s, WORK_STATE wst);\n    int (*get_construct_message_f) (SSL *s, WPACKET *pkt,\n                                    int (**confunc) (SSL *s, WPACKET *pkt),\n                                    int *mt);\n    void (*cb) (const SSL *ssl, int type, int val) = NULL;\n    int (*confunc) (SSL *s, WPACKET *pkt);\n    int mt;\n    WPACKET pkt;\n    cb = get_callback(s);\n    if (s->server) {\n        transition = ossl_statem_server_write_transition;\n        pre_work = ossl_statem_server_pre_work;\n        post_work = ossl_statem_server_post_work;\n        get_construct_message_f = ossl_statem_server_construct_message;\n    } else {\n        transition = ossl_statem_client_write_transition;\n        pre_work = ossl_statem_client_pre_work;\n        post_work = ossl_statem_client_post_work;\n        get_construct_message_f = ossl_statem_client_construct_message;\n    }\n    while (1) {\n        switch (st->write_state) {\n        case WRITE_STATE_TRANSITION:\n            if (cb != NULL) {\n                if (s->server)\n                    cb(s, SSL_CB_ACCEPT_LOOP, 1);\n                else\n                    cb(s, SSL_CB_CONNECT_LOOP, 1);\n            }\n            switch (transition(s)) {\n            case WRITE_TRAN_CONTINUE:\n                st->write_state = WRITE_STATE_PRE_WORK;\n                st->write_state_work = WORK_MORE_A;\n                break;\n            case WRITE_TRAN_FINISHED:\n                return SUB_STATE_FINISHED;\n                break;\n            case WRITE_TRAN_ERROR:\n                return SUB_STATE_ERROR;\n            }\n            break;\n        case WRITE_STATE_PRE_WORK:\n            switch (st->write_state_work = pre_work(s, st->write_state_work)) {\n            case WORK_ERROR:\n            case WORK_MORE_A:\n            case WORK_MORE_B:\n            case WORK_MORE_C:\n                return SUB_STATE_ERROR;\n            case WORK_FINISHED_CONTINUE:\n                st->write_state = WRITE_STATE_SEND;\n                break;\n            case WORK_FINISHED_STOP:\n                return SUB_STATE_END_HANDSHAKE;\n            }\n            if (!get_construct_message_f(s, &pkt, &confunc, &mt)) {\n                ossl_statem_set_error(s);\n                return SUB_STATE_ERROR;\n            }\n            if (mt == SSL3_MT_DUMMY) {\n                st->write_state = WRITE_STATE_POST_WORK;\n                st->write_state_work = WORK_MORE_A;\n                break;\n            }\n            if (!WPACKET_init(&pkt, s->init_buf)\n                    || !ssl_set_handshake_header(s, &pkt, mt)\n                    || (confunc != NULL && !confunc(s, &pkt))\n                    || !ssl_close_construct_packet(s, &pkt, mt)\n                    || !WPACKET_finish(&pkt)) {\n                WPACKET_cleanup(&pkt);\n                ossl_statem_set_error(s);\n                return SUB_STATE_ERROR;\n            }\n        case WRITE_STATE_SEND:\n            if (SSL_IS_DTLS(s) && st->use_timer) {\n                dtls1_start_timer(s);\n            }\n            ret = statem_do_write(s);\n            if (ret <= 0) {\n                return SUB_STATE_ERROR;\n            }\n            st->write_state = WRITE_STATE_POST_WORK;\n            st->write_state_work = WORK_MORE_A;\n        case WRITE_STATE_POST_WORK:\n            switch (st->write_state_work = post_work(s, st->write_state_work)) {\n            case WORK_ERROR:\n            case WORK_MORE_A:\n            case WORK_MORE_B:\n            case WORK_MORE_C:\n                return SUB_STATE_ERROR;\n            case WORK_FINISHED_CONTINUE:\n                st->write_state = WRITE_STATE_TRANSITION;\n                break;\n            case WORK_FINISHED_STOP:\n                return SUB_STATE_END_HANDSHAKE;\n            }\n            break;\n        default:\n            return SUB_STATE_ERROR;\n        }\n    }\n}', 'static int statem_do_write(SSL *s)\n{\n    OSSL_STATEM *st = &s->statem;\n    if (st->hand_state == TLS_ST_CW_CHANGE\n        || st->hand_state == TLS_ST_SW_CHANGE) {\n        if (SSL_IS_DTLS(s))\n            return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);\n        else\n            return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);\n    } else {\n        return ssl_do_write(s);\n    }\n}', 'int dtls1_do_write(SSL *s, int type)\n{\n    int ret;\n    size_t written;\n    size_t curr_mtu;\n    int retry = 1;\n    size_t len, frag_off, mac_size, blocksize, used_len;\n    if (!dtls1_query_mtu(s))\n        return -1;\n    if (s->d1->mtu < dtls1_min_mtu(s))\n        return -1;\n    if (s->init_off == 0 && type == SSL3_RT_HANDSHAKE) {\n        if (!ossl_assert(s->init_num ==\n                         s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH))\n            return -1;\n    }\n    if (s->write_hash) {\n        if (s->enc_write_ctx\n            && (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_write_ctx)) &\n                EVP_CIPH_FLAG_AEAD_CIPHER) != 0)\n            mac_size = 0;\n        else\n            mac_size = EVP_MD_CTX_size(s->write_hash);\n    } else\n        mac_size = 0;\n    if (s->enc_write_ctx &&\n        (EVP_CIPHER_CTX_mode(s->enc_write_ctx) == EVP_CIPH_CBC_MODE))\n        blocksize = 2 * EVP_CIPHER_CTX_block_size(s->enc_write_ctx);\n    else\n        blocksize = 0;\n    frag_off = 0;\n    s->rwstate = SSL_NOTHING;\n    while (s->init_num > 0) {\n        if (type == SSL3_RT_HANDSHAKE && s->init_off != 0) {\n            if (frag_off > 0) {\n                if (s->init_off <= DTLS1_HM_HEADER_LENGTH) {\n                    return -1;\n                }\n                s->init_off -= DTLS1_HM_HEADER_LENGTH;\n                s->init_num += DTLS1_HM_HEADER_LENGTH;\n            } else {\n                frag_off = s->d1->w_msg_hdr.frag_off;\n            }\n        }\n        used_len = BIO_wpending(s->wbio) + DTLS1_RT_HEADER_LENGTH\n            + mac_size + blocksize;\n        if (s->d1->mtu > used_len)\n            curr_mtu = s->d1->mtu - used_len;\n        else\n            curr_mtu = 0;\n        if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) {\n            ret = BIO_flush(s->wbio);\n            if (ret <= 0) {\n                s->rwstate = SSL_WRITING;\n                return ret;\n            }\n            used_len = DTLS1_RT_HEADER_LENGTH + mac_size + blocksize;\n            if (s->d1->mtu > used_len + DTLS1_HM_HEADER_LENGTH) {\n                curr_mtu = s->d1->mtu - used_len;\n            } else {\n                return -1;\n            }\n        }\n        if (((unsigned int)s->init_num) > curr_mtu)\n            len = curr_mtu;\n        else\n            len = s->init_num;\n        if (len > s->max_send_fragment)\n            len = s->max_send_fragment;\n        if (type == SSL3_RT_HANDSHAKE) {\n            if (len < DTLS1_HM_HEADER_LENGTH) {\n                return -1;\n            }\n            dtls1_fix_message_header(s, frag_off, len - DTLS1_HM_HEADER_LENGTH);\n            dtls1_write_message_header(s,\n                                       (unsigned char *)&s->init_buf->\n                                       data[s->init_off]);\n        }\n        ret = dtls1_write_bytes(s, type, &s->init_buf->data[s->init_off], len,\n                                &written);\n        if (ret < 0) {\n            if (retry && BIO_ctrl(SSL_get_wbio(s),\n                                  BIO_CTRL_DGRAM_MTU_EXCEEDED, 0, NULL) > 0) {\n                if (!(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {\n                    if (!dtls1_query_mtu(s))\n                        return -1;\n                    retry = 0;\n                } else\n                    return -1;\n            } else {\n                return -1;\n            }\n        } else {\n            if (!ossl_assert(len == written))\n                return -1;\n            if (type == SSL3_RT_HANDSHAKE && !s->d1->retransmitting) {\n                unsigned char *p =\n                    (unsigned char *)&s->init_buf->data[s->init_off];\n                const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;\n                size_t xlen;\n                if (frag_off == 0 && s->version != DTLS1_BAD_VER) {\n                    *p++ = msg_hdr->type;\n                    l2n3(msg_hdr->msg_len, p);\n                    s2n(msg_hdr->seq, p);\n                    l2n3(0, p);\n                    l2n3(msg_hdr->msg_len, p);\n                    p -= DTLS1_HM_HEADER_LENGTH;\n                    xlen = written;\n                } else {\n                    p += DTLS1_HM_HEADER_LENGTH;\n                    xlen = written - DTLS1_HM_HEADER_LENGTH;\n                }\n                if (!ssl3_finish_mac(s, p, xlen))\n                    return -1;\n            }\n            if (written == s->init_num) {\n                if (s->msg_callback)\n                    s->msg_callback(1, s->version, type, s->init_buf->data,\n                                    (size_t)(s->init_off + s->init_num), s,\n                                    s->msg_callback_arg);\n                s->init_off = 0;\n                s->init_num = 0;\n                return 1;\n            }\n            s->init_off += written;\n            s->init_num -= written;\n            written -= DTLS1_HM_HEADER_LENGTH;\n            frag_off += written;\n            dtls1_fix_message_header(s, frag_off, 0);\n        }\n    }\n    return 0;\n}', 'int dtls1_write_bytes(SSL *s, int type, const void *buf, size_t len,\n                      size_t *written)\n{\n    int i;\n    if (!ossl_assert(len <= SSL3_RT_MAX_PLAIN_LENGTH)) {\n        SSLerr(SSL_F_DTLS1_WRITE_BYTES, ERR_R_INTERNAL_ERROR);\n        return -1;\n    }\n    s->rwstate = SSL_NOTHING;\n    i = do_dtls1_write(s, type, buf, len, 0, written);\n    return i;\n}', 'int do_dtls1_write(SSL *s, int type, const unsigned char *buf,\n                   size_t len, int create_empty_fragment, size_t *written)\n{\n    unsigned char *p, *pseq;\n    int i, mac_size, clear = 0;\n    size_t prefix_len = 0;\n    int eivlen;\n    SSL3_RECORD wr;\n    SSL3_BUFFER *wb;\n    SSL_SESSION *sess;\n    wb = &s->rlayer.wbuf[0];\n    if (!ossl_assert(SSL3_BUFFER_get_left(wb) == 0)) {\n        SSLerr(SSL_F_DO_DTLS1_WRITE, ERR_R_INTERNAL_ERROR);\n        return 0;\n    }\n    if (s->s3->alert_dispatch) {\n        i = s->method->ssl_dispatch_alert(s);\n        if (i <= 0)\n            return i;\n    }\n    if (len == 0 && !create_empty_fragment)\n        return 0;\n    if (len > s->max_send_fragment) {\n        SSLerr(SSL_F_DO_DTLS1_WRITE, SSL_R_EXCEEDS_MAX_FRAGMENT_SIZE);\n        return 0;\n    }\n    sess = s->session;\n    if ((sess == NULL) ||\n        (s->enc_write_ctx == NULL) || (EVP_MD_CTX_md(s->write_hash) == NULL))\n        clear = 1;\n    if (clear)\n        mac_size = 0;\n    else {\n        mac_size = EVP_MD_CTX_size(s->write_hash);\n        if (mac_size < 0)\n            goto err;\n    }\n    p = SSL3_BUFFER_get_buf(wb) + prefix_len;\n    *(p++) = type & 0xff;\n    SSL3_RECORD_set_type(&wr, type);\n    if (s->method->version == DTLS_ANY_VERSION &&\n        s->max_proto_version != DTLS1_BAD_VER) {\n        *(p++) = DTLS1_VERSION >> 8;\n        *(p++) = DTLS1_VERSION & 0xff;\n    } else {\n        *(p++) = s->version >> 8;\n        *(p++) = s->version & 0xff;\n    }\n    pseq = p;\n    p += 10;\n    if (s->enc_write_ctx) {\n        int mode = EVP_CIPHER_CTX_mode(s->enc_write_ctx);\n        if (mode == EVP_CIPH_CBC_MODE) {\n            eivlen = EVP_CIPHER_CTX_iv_length(s->enc_write_ctx);\n            if (eivlen <= 1)\n                eivlen = 0;\n        }\n        else if (mode == EVP_CIPH_GCM_MODE)\n            eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;\n        else if (mode == EVP_CIPH_CCM_MODE)\n            eivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN;\n        else\n            eivlen = 0;\n    } else\n        eivlen = 0;\n    SSL3_RECORD_set_data(&wr, p + eivlen);\n    SSL3_RECORD_set_length(&wr, len);\n    SSL3_RECORD_set_input(&wr, (unsigned char *)buf);\n    if (s->compress != NULL) {\n        if (!ssl3_do_compress(s, &wr)) {\n            SSLerr(SSL_F_DO_DTLS1_WRITE, SSL_R_COMPRESSION_FAILURE);\n            goto err;\n        }\n    } else {\n        memcpy(SSL3_RECORD_get_data(&wr), SSL3_RECORD_get_input(&wr),\n               SSL3_RECORD_get_length(&wr));\n        SSL3_RECORD_reset_input(&wr);\n    }\n    if (!SSL_WRITE_ETM(s) && mac_size != 0) {\n        if (!s->method->ssl3_enc->mac(s, &wr,\n                                      &(p[SSL3_RECORD_get_length(&wr) + eivlen]),\n                                      1))\n            goto err;\n        SSL3_RECORD_add_length(&wr, mac_size);\n    }\n    SSL3_RECORD_set_data(&wr, p);\n    SSL3_RECORD_reset_input(&wr);\n    if (eivlen)\n        SSL3_RECORD_add_length(&wr, eivlen);\n    if (s->method->ssl3_enc->enc(s, &wr, 1, 1) < 1)\n        goto err;\n    if (SSL_WRITE_ETM(s) && mac_size != 0) {\n        if (!s->method->ssl3_enc->mac(s, &wr,\n                                      &(p[SSL3_RECORD_get_length(&wr)]), 1))\n            goto err;\n        SSL3_RECORD_add_length(&wr, mac_size);\n    }\n    s2n(s->rlayer.d->w_epoch, pseq);\n    memcpy(pseq, &(s->rlayer.write_sequence[2]), 6);\n    pseq += 6;\n    s2n(SSL3_RECORD_get_length(&wr), pseq);\n    if (s->msg_callback)\n        s->msg_callback(1, 0, SSL3_RT_HEADER, pseq - DTLS1_RT_HEADER_LENGTH,\n                        DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg);\n    SSL3_RECORD_set_type(&wr, type);\n    SSL3_RECORD_add_length(&wr, DTLS1_RT_HEADER_LENGTH);\n    ssl3_record_sequence_update(&(s->rlayer.write_sequence[0]));\n    if (create_empty_fragment) {\n        *written = wr.length;\n        return 1;\n    }\n    SSL3_BUFFER_set_left(wb, prefix_len + SSL3_RECORD_get_length(&wr));\n    SSL3_BUFFER_set_offset(wb, 0);\n    s->rlayer.wpend_tot = len;\n    s->rlayer.wpend_buf = buf;\n    s->rlayer.wpend_type = type;\n    s->rlayer.wpend_ret = len;\n    return ssl3_write_pending(s, type, buf, len, written);\n err:\n    return -1;\n}', 'int ssl3_write_pending(SSL *s, int type, const unsigned char *buf, size_t len,\n                       size_t *written)\n{\n    int i;\n    SSL3_BUFFER *wb = s->rlayer.wbuf;\n    size_t currbuf = 0;\n    size_t tmpwrit = 0;\n    if ((s->rlayer.wpend_tot > len)\n        || ((s->rlayer.wpend_buf != buf) &&\n            !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER))\n        || (s->rlayer.wpend_type != type)) {\n        SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BAD_WRITE_RETRY);\n        return -1;\n    }\n    for (;;) {\n        if (SSL3_BUFFER_get_left(&wb[currbuf]) == 0\n            && currbuf < s->rlayer.numwpipes - 1) {\n            currbuf++;\n            continue;\n        }\n        clear_sys_error();\n        if (s->wbio != NULL) {\n            s->rwstate = SSL_WRITING;\n            i = BIO_write(s->wbio, (char *)\n                          &(SSL3_BUFFER_get_buf(&wb[currbuf])\n                            [SSL3_BUFFER_get_offset(&wb[currbuf])]),\n                          (unsigned int)SSL3_BUFFER_get_left(&wb[currbuf]));\n            if (i >= 0)\n                tmpwrit = i;\n        } else {\n            SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BIO_NOT_SET);\n            i = -1;\n        }\n        if (i > 0 && tmpwrit == SSL3_BUFFER_get_left(&wb[currbuf])) {\n            SSL3_BUFFER_set_left(&wb[currbuf], 0);\n            SSL3_BUFFER_add_offset(&wb[currbuf], tmpwrit);\n            if (currbuf + 1 < s->rlayer.numwpipes)\n                continue;\n            s->rwstate = SSL_NOTHING;\n            *written = s->rlayer.wpend_ret;\n            return 1;\n        } else if (i <= 0) {\n            if (SSL_IS_DTLS(s)) {\n                SSL3_BUFFER_set_left(&wb[currbuf], 0);\n            }\n            return (i);\n        }\n        SSL3_BUFFER_add_offset(&wb[currbuf], tmpwrit);\n        SSL3_BUFFER_sub_left(&wb[currbuf], tmpwrit);\n    }\n}']