1 | /*
|
---|
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | *
|
---|
4 | * Licensed under the OpenSSL license (the "License"). You may not use
|
---|
5 | * this file except in compliance with the License. You can obtain a copy
|
---|
6 | * in the file LICENSE in the source distribution or at
|
---|
7 | * https://www.openssl.org/source/license.html
|
---|
8 | */
|
---|
9 |
|
---|
10 | #include "../ssl_locl.h"
|
---|
11 | #include "record_locl.h"
|
---|
12 |
|
---|
13 | void SSL3_BUFFER_set_data(SSL3_BUFFER *b, const unsigned char *d, int n)
|
---|
14 | {
|
---|
15 | if (d != NULL)
|
---|
16 | memcpy(b->buf, d, n);
|
---|
17 | b->left = n;
|
---|
18 | b->offset = 0;
|
---|
19 | }
|
---|
20 |
|
---|
21 | /*
|
---|
22 | * Clear the contents of an SSL3_BUFFER but retain any memory allocated. Also
|
---|
23 | * retains the default_len setting
|
---|
24 | */
|
---|
25 | void SSL3_BUFFER_clear(SSL3_BUFFER *b)
|
---|
26 | {
|
---|
27 | b->offset = 0;
|
---|
28 | b->left = 0;
|
---|
29 | }
|
---|
30 |
|
---|
31 | void SSL3_BUFFER_release(SSL3_BUFFER *b)
|
---|
32 | {
|
---|
33 | OPENSSL_free(b->buf);
|
---|
34 | b->buf = NULL;
|
---|
35 | }
|
---|
36 |
|
---|
37 | int ssl3_setup_read_buffer(SSL *s)
|
---|
38 | {
|
---|
39 | unsigned char *p;
|
---|
40 | size_t len, align = 0, headerlen;
|
---|
41 | SSL3_BUFFER *b;
|
---|
42 |
|
---|
43 | b = RECORD_LAYER_get_rbuf(&s->rlayer);
|
---|
44 |
|
---|
45 | if (SSL_IS_DTLS(s))
|
---|
46 | headerlen = DTLS1_RT_HEADER_LENGTH;
|
---|
47 | else
|
---|
48 | headerlen = SSL3_RT_HEADER_LENGTH;
|
---|
49 |
|
---|
50 | #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
|
---|
51 | align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1);
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | if (b->buf == NULL) {
|
---|
55 | len = SSL3_RT_MAX_PLAIN_LENGTH
|
---|
56 | + SSL3_RT_MAX_ENCRYPTED_OVERHEAD + headerlen + align;
|
---|
57 | #ifndef OPENSSL_NO_COMP
|
---|
58 | if (ssl_allow_compression(s))
|
---|
59 | len += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
|
---|
60 | #endif
|
---|
61 | if (b->default_len > len)
|
---|
62 | len = b->default_len;
|
---|
63 | if ((p = OPENSSL_malloc(len)) == NULL)
|
---|
64 | goto err;
|
---|
65 | b->buf = p;
|
---|
66 | b->len = len;
|
---|
67 | }
|
---|
68 |
|
---|
69 | RECORD_LAYER_set_packet(&s->rlayer, &(b->buf[0]));
|
---|
70 | return 1;
|
---|
71 |
|
---|
72 | err:
|
---|
73 | SSLerr(SSL_F_SSL3_SETUP_READ_BUFFER, ERR_R_MALLOC_FAILURE);
|
---|
74 | return 0;
|
---|
75 | }
|
---|
76 |
|
---|
77 | int ssl3_setup_write_buffer(SSL *s, unsigned int numwpipes, size_t len)
|
---|
78 | {
|
---|
79 | unsigned char *p;
|
---|
80 | size_t align = 0, headerlen;
|
---|
81 | SSL3_BUFFER *wb;
|
---|
82 | unsigned int currpipe;
|
---|
83 |
|
---|
84 | s->rlayer.numwpipes = numwpipes;
|
---|
85 |
|
---|
86 | if (len == 0) {
|
---|
87 | if (SSL_IS_DTLS(s))
|
---|
88 | headerlen = DTLS1_RT_HEADER_LENGTH + 1;
|
---|
89 | else
|
---|
90 | headerlen = SSL3_RT_HEADER_LENGTH;
|
---|
91 |
|
---|
92 | #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
|
---|
93 | align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1);
|
---|
94 | #endif
|
---|
95 |
|
---|
96 | len = s->max_send_fragment
|
---|
97 | + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD + headerlen + align;
|
---|
98 | #ifndef OPENSSL_NO_COMP
|
---|
99 | if (ssl_allow_compression(s))
|
---|
100 | len += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
|
---|
101 | #endif
|
---|
102 | if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))
|
---|
103 | len += headerlen + align + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD;
|
---|
104 | }
|
---|
105 |
|
---|
106 | wb = RECORD_LAYER_get_wbuf(&s->rlayer);
|
---|
107 | for (currpipe = 0; currpipe < numwpipes; currpipe++) {
|
---|
108 | SSL3_BUFFER *thiswb = &wb[currpipe];
|
---|
109 |
|
---|
110 | if (thiswb->buf == NULL) {
|
---|
111 | p = OPENSSL_malloc(len);
|
---|
112 | if (p == NULL) {
|
---|
113 | s->rlayer.numwpipes = currpipe;
|
---|
114 | goto err;
|
---|
115 | }
|
---|
116 | memset(thiswb, 0, sizeof(SSL3_BUFFER));
|
---|
117 | thiswb->buf = p;
|
---|
118 | thiswb->len = len;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | return 1;
|
---|
123 |
|
---|
124 | err:
|
---|
125 | SSLerr(SSL_F_SSL3_SETUP_WRITE_BUFFER, ERR_R_MALLOC_FAILURE);
|
---|
126 | return 0;
|
---|
127 | }
|
---|
128 |
|
---|
129 | int ssl3_setup_buffers(SSL *s)
|
---|
130 | {
|
---|
131 | if (!ssl3_setup_read_buffer(s))
|
---|
132 | return 0;
|
---|
133 | if (!ssl3_setup_write_buffer(s, 1, 0))
|
---|
134 | return 0;
|
---|
135 | return 1;
|
---|
136 | }
|
---|
137 |
|
---|
138 | int ssl3_release_write_buffer(SSL *s)
|
---|
139 | {
|
---|
140 | SSL3_BUFFER *wb;
|
---|
141 | unsigned int pipes;
|
---|
142 |
|
---|
143 | pipes = s->rlayer.numwpipes;
|
---|
144 | while (pipes > 0) {
|
---|
145 | wb = &RECORD_LAYER_get_wbuf(&s->rlayer)[pipes - 1];
|
---|
146 |
|
---|
147 | OPENSSL_free(wb->buf);
|
---|
148 | wb->buf = NULL;
|
---|
149 | pipes--;
|
---|
150 | }
|
---|
151 | s->rlayer.numwpipes = 0;
|
---|
152 | return 1;
|
---|
153 | }
|
---|
154 |
|
---|
155 | int ssl3_release_read_buffer(SSL *s)
|
---|
156 | {
|
---|
157 | SSL3_BUFFER *b;
|
---|
158 |
|
---|
159 | b = RECORD_LAYER_get_rbuf(&s->rlayer);
|
---|
160 | OPENSSL_free(b->buf);
|
---|
161 | b->buf = NULL;
|
---|
162 | return 1;
|
---|
163 | }
|
---|