126 #define RSMBLY_BITMASK_SIZE(msg_len) (((msg_len) + 7) / 8)
128 #define RSMBLY_BITMASK_MARK(bitmask, start, end) { \
129 if ((end) - (start) <= 8) { \
131 for (ii = (start); ii < (end); ii++) bitmask[((ii) >> 3)] |= (1 << ((ii) & 7)); \
134 bitmask[((start) >> 3)] |= bitmask_start_values[((start) & 7)]; \
135 for (ii = (((start) >> 3) + 1); ii < ((((end) - 1)) >> 3); ii++) bitmask[ii] = 0xff; \
136 bitmask[(((end) - 1) >> 3)] |= bitmask_end_values[((end) & 7)]; \
139 #define RSMBLY_BITMASK_IS_COMPLETE(bitmask, msg_len, is_complete) { \
141 OPENSSL_assert((msg_len) > 0); \
143 if (bitmask[(((msg_len) - 1) >> 3)] != bitmask_end_values[((msg_len) & 7)]) is_complete = 0; \
144 if (is_complete) for (ii = (((msg_len) - 1) >> 3) - 1; ii >= 0 ; ii--) \
145 if (bitmask[ii] != 0xff) { is_complete = 0; break; } }
148 #define RSMBLY_BITMASK_PRINT(bitmask, msg_len) { \
150 printf("bitmask: "); for (ii = 0; ii < (msg_len); ii++) \
151 printf("%d ", (bitmask[ii >> 3] & (1 << (ii & 7))) >> (ii & 7)); \
155 static unsigned char bitmask_start_values[] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80};
156 static unsigned char bitmask_end_values[] = {0xff, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f};
159 static unsigned int g_probable_mtu[] = {1500 - 28, 512 - 28, 256 - 28};
161 static unsigned int dtls1_guess_mtu(
unsigned int curr_mtu);
162 static void dtls1_fix_message_header(
SSL *s,
unsigned long frag_off,
163 unsigned long frag_len);
164 static unsigned char *dtls1_write_message_header(
SSL *s,
166 static void dtls1_set_message_header_int(
SSL *s,
unsigned char mt,
167 unsigned long len,
unsigned short seq_num,
unsigned long frag_off,
168 unsigned long frag_len);
169 static long dtls1_get_message_fragment(
SSL *s,
int st1,
int stn,
173 dtls1_hm_fragment_new(
unsigned long frag_len,
int reassembly)
176 unsigned char *buf = NULL;
177 unsigned char *bitmask = NULL;
227 unsigned int len, frag_off, mac_size, blocksize;
248 fprintf(stderr,
"using MTU = %d\n", mtu);
298 mac_size - blocksize;
321 dtls1_fix_message_header(s, frag_off,
365 *p++ = msg_hdr->type;
366 l2n3(msg_hdr->msg_len,p);
367 s2n (msg_hdr->seq,p);
369 l2n3(msg_hdr->msg_len,p);
436 i = dtls1_get_message_fragment(s, st1, stn, max, ok);
440 else if ( i <= 0 && !*ok)
447 *(p++) = msg_hdr->
type;
488 if ( (frag_off+frag_len) > msg_len)
494 if ( (frag_off+frag_len) > (
unsigned long)max)
529 dtls1_retrieve_buffered_fragment(
SSL *s,
long max,
int *ok)
556 al=dtls1_preprocess_fragment(s,&frag->
msg_header,max);
565 dtls1_hm_fragment_free(frag);
585 dtls1_reassemble_fragment(
SSL *s,
struct hm_header_st* msg_hdr,
int *ok)
589 int i = -1, is_complete;
590 unsigned char seq64be[8];
591 unsigned long frag_len = msg_hdr->
frag_len, max_len;
604 if ((msg_hdr->
frag_off+frag_len) > max_len)
608 memset(seq64be,0,
sizeof(seq64be));
609 seq64be[6] = (
unsigned char) (msg_hdr->
seq>>8);
610 seq64be[7] = (
unsigned char) msg_hdr->
seq;
615 frag = dtls1_hm_fragment_new(msg_hdr->
msg_len, 1);
618 memcpy(&(frag->
msg_header), msg_hdr,
sizeof(*msg_hdr));
630 unsigned char devnull [256];
636 frag_len>
sizeof(devnull)?
sizeof(devnull):frag_len,0);
646 if (i<=0 || (
unsigned long)i!=frag_len)
650 (
long)(msg_hdr->
frag_off + frag_len));
663 memset(seq64be,0,
sizeof(seq64be));
664 seq64be[6] = (
unsigned char)(msg_hdr->
seq>>8);
665 seq64be[7] = (
unsigned char)(msg_hdr->
seq);
680 if (frag != NULL) dtls1_hm_fragment_free(frag);
688 dtls1_process_out_of_seq_message(
SSL *s,
struct hm_header_st* msg_hdr,
int *ok)
693 unsigned char seq64be[8];
694 unsigned long frag_len = msg_hdr->
frag_len;
700 memset(seq64be,0,
sizeof(seq64be));
701 seq64be[6] = (
unsigned char) (msg_hdr->
seq>>8);
702 seq64be[7] = (
unsigned char) msg_hdr->
seq;
708 if (item != NULL && frag_len < msg_hdr->msg_len)
720 unsigned char devnull [256];
726 frag_len>
sizeof(devnull)?
sizeof(devnull):frag_len,0);
733 if (frag_len && frag_len < msg_hdr->msg_len)
734 return dtls1_reassemble_fragment(s, msg_hdr, ok);
736 frag = dtls1_hm_fragment_new(frag_len, 0);
740 memcpy(&(frag->
msg_header), msg_hdr,
sizeof(*msg_hdr));
747 if (i<=0 || (
unsigned long)i!=frag_len)
751 memset(seq64be,0,
sizeof(seq64be));
752 seq64be[6] = (
unsigned char)(msg_hdr->
seq>>8);
753 seq64be[7] = (
unsigned char)(msg_hdr->
seq);
765 if ( frag != NULL) dtls1_hm_fragment_free(frag);
773 dtls1_get_message_fragment(
SSL *s,
int st1,
int stn,
long max,
int *ok)
781 if ((frag_len = dtls1_retrieve_buffered_fragment(s,max,ok)) || *ok)
814 return dtls1_process_out_of_seq_message(s, &msg_hdr, ok);
820 if (frag_len && frag_len < len)
821 return dtls1_reassemble_fragment(s, &msg_hdr, ok);
830 if (wire[1] == 0 && wire[2] == 0 && wire[3] == 0)
838 return dtls1_get_message_fragment(s, st1, stn,
849 if ((al=dtls1_preprocess_fragment(s,&msg_hdr,max)))
860 &p[frag_off],frag_len,0);
874 if (i != (
int)frag_len)
934 #ifdef OPENSSL_SYS_WIN16
942 s->
init_num=(int)l+DTLS1_HM_HEADER_LENGTH;
995 static int dtls1_add_cert_to_buf(
BUF_MEM *buf,
unsigned long *l,
X509 *x)
1006 p=(
unsigned char *)&(buf->
data[*l]);
1044 if (!dtls1_add_cert_to_buf(buf, &l, x))
1056 if (!dtls1_add_cert_to_buf(buf, &l, x))
1065 p=(
unsigned char *)&(buf->
data[0]);
1076 fprintf( stderr,
"invalid state reached %s:%d", __FILE__, __LINE__);
1087 #ifndef OPENSSL_NO_HEARTBEATS
1109 DTLS1_AD_MISSING_HANDSHAKE_MESSAGE);
1145 0, &found) <= 0 && found)
1147 fprintf(stderr,
"dtls1_retransmit_message() failed\n");
1160 unsigned char seq64be[8];
1166 frag = dtls1_hm_fragment_new(s->
init_num, 0);
1195 memset(seq64be,0,
sizeof(seq64be));
1204 dtls1_hm_fragment_free(frag);
1209 fprintf( stderr,
"buffered messge: \ttype = %xx\n", msg_buf->type);
1210 fprintf( stderr,
"\t\t\t\t\tlen = %d\n", msg_buf->len);
1211 fprintf( stderr,
"\t\t\t\t\tseq_num = %d\n", msg_buf->seq_num);
1226 unsigned long header_length;
1227 unsigned char seq64be[8];
1229 unsigned char save_write_sequence[8];
1237 memset(seq64be,0,
sizeof(seq64be));
1238 seq64be[6] = (
unsigned char)(seq>>8);
1239 seq64be[7] = (
unsigned char)seq;
1244 fprintf(stderr,
"retransmit: message %d non-existant\n", seq);
1327 unsigned long len,
unsigned long frag_off,
unsigned long frag_len)
1330 if (frag_off == 0 && !s->
d1->
listen)
1337 frag_off, frag_len);
1345 dtls1_set_message_header_int(
SSL *s,
unsigned char mt,
1346 unsigned long len,
unsigned short seq_num,
unsigned long frag_off,
1347 unsigned long frag_len)
1353 msg_hdr->
seq = seq_num;
1359 dtls1_fix_message_header(
SSL *s,
unsigned long frag_off,
1360 unsigned long frag_len)
1368 static unsigned char *
1369 dtls1_write_message_header(
SSL *s,
unsigned char *p)
1373 *p++ = msg_hdr->
type;
1386 return (g_probable_mtu[(
sizeof(g_probable_mtu) /
1387 sizeof(g_probable_mtu[0])) - 1]);
1391 dtls1_guess_mtu(
unsigned int curr_mtu)
1395 if ( curr_mtu == 0 )
1396 return g_probable_mtu[0] ;
1398 for ( i = 0; i <
sizeof(g_probable_mtu)/
sizeof(g_probable_mtu[0]); i++)
1399 if ( curr_mtu > g_probable_mtu[i])
1400 return g_probable_mtu[i];
1409 msg_hdr->
type = *(data++);
1422 ccs_hdr->
type = *(data++);
1428 #ifndef OPENSSL_NO_SCTP
1433 if (ret < 0)
return -1;
1440 #ifndef OPENSSL_NO_SCTP
1446 #ifndef OPENSSL_NO_HEARTBEATS
1450 unsigned char *p = &s->
s3->
rrec.
data[0], *pl;
1451 unsigned short hbtype;
1452 unsigned int payload;
1453 unsigned int padding = 16;
1467 unsigned char *buffer, *
bp;
1480 memcpy(bp, pl, payload);
1489 buffer, 3 + payload + padding,
1520 unsigned char *buf, *
p;
1522 unsigned int payload = 18;
1523 unsigned int padding = 16;
1580 buf, 3 + payload + padding,