Support SSL_OP_CLEANSE_PLAINTEXT on QUIC streams
[openssl.git] / include / internal / ring_buf.h
1 /*
2  * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 #ifndef OSSL_INTERNAL_RING_BUF_H
11 # define OSSL_INTERNAL_RING_BUF_H
12 # pragma once
13
14 # include <openssl/e_os2.h>              /* For 'ossl_inline' */
15
16 /*
17  * ==================================================================
18  * Byte-wise ring buffer which supports pushing and popping blocks of multiple
19  * bytes at a time. The logical offset of each byte for the purposes of a QUIC
20  * stream is tracked. Bytes can be popped from the ring buffer in two stages;
21  * first they are popped, and then they are culled. Bytes which have been popped
22  * but not yet culled will not be overwritten, and can be restored.
23  */
24 struct ring_buf {
25     void       *start;
26     size_t      alloc;        /* size of buffer allocation in bytes */
27
28     /*
29      * Logical offset of the head (where we append to). This is the current size
30      * of the QUIC stream. This increases monotonically.
31      */
32     uint64_t    head_offset;
33
34     /*
35      * Logical offset of the cull tail. Data is no longer needed and is
36      * deallocated as the cull tail advances, which occurs as data is
37      * acknowledged. This increases monotonically.
38      */
39     uint64_t    ctail_offset;
40 };
41
42 static ossl_inline int ring_buf_init(struct ring_buf *r)
43 {
44     r->start = NULL;
45     r->alloc = 0;
46     r->head_offset = r->ctail_offset = 0;
47     return 1;
48 }
49
50 static ossl_inline void ring_buf_destroy(struct ring_buf *r)
51 {
52     OPENSSL_free(r->start);
53     r->start = NULL;
54     r->alloc = 0;
55 }
56
57 static ossl_inline size_t ring_buf_used(struct ring_buf *r)
58 {
59     return (size_t)(r->head_offset - r->ctail_offset);
60 }
61
62 static ossl_inline size_t ring_buf_avail(struct ring_buf *r)
63 {
64     return r->alloc - ring_buf_used(r);
65 }
66
67 static ossl_inline int ring_buf_write_at(struct ring_buf *r,
68                                          uint64_t logical_offset,
69                                          const unsigned char *buf,
70                                          size_t buf_len)
71 {
72     size_t avail, idx, l;
73     unsigned char *start = r->start;
74     int i;
75
76     avail = ring_buf_avail(r);
77     if (logical_offset < r->ctail_offset
78         || logical_offset + buf_len > r->head_offset + avail)
79         return 0;
80
81     for (i = 0; buf_len > 0 && i < 2; ++i) {
82         idx = logical_offset % r->alloc;
83         l = r->alloc - idx;
84         if (buf_len < l)
85             l = buf_len;
86
87         memcpy(start + idx, buf, l);
88         if (r->head_offset < logical_offset + l)
89             r->head_offset = logical_offset + l;
90
91         logical_offset += l;
92         buf += l;
93         buf_len -= l;
94     }
95
96     assert(buf_len == 0);
97
98     return 1;
99 }
100
101 static ossl_inline size_t ring_buf_push(struct ring_buf *r,
102                                         const unsigned char *buf,
103                                         size_t buf_len)
104 {
105     size_t pushed = 0, avail, idx, l;
106     unsigned char *start = r->start;
107
108     for (;;) {
109         avail = ring_buf_avail(r);
110         if (buf_len > avail)
111             buf_len = avail;
112
113         if (buf_len == 0)
114             break;
115
116         idx = r->head_offset % r->alloc;
117         l = r->alloc - idx;
118         if (buf_len < l)
119             l = buf_len;
120
121         memcpy(start + idx, buf, l);
122         r->head_offset  += l;
123         buf             += l;
124         buf_len         -= l;
125         pushed          += l;
126     }
127
128     return pushed;
129 }
130
131 static ossl_inline const unsigned char *ring_buf_get_ptr(const struct ring_buf *r,
132                                                          uint64_t logical_offset,
133                                                          size_t *max_len)
134 {
135     unsigned char *start = r->start;
136     size_t idx;
137
138     if (logical_offset >= r->head_offset || logical_offset < r->ctail_offset)
139         return NULL;
140     idx = logical_offset % r->alloc;
141     *max_len = r->alloc - idx;
142     return start + idx;
143 }
144
145 /*
146  * Retrieves data out of the read side of the ring buffer starting at the given
147  * logical offset. *buf is set to point to a contiguous span of bytes and
148  * *buf_len is set to the number of contiguous bytes. After this function
149  * returns, there may or may not be more bytes available at the logical offset
150  * of (logical_offset + *buf_len) by calling this function again. If the logical
151  * offset is out of the range retained by the ring buffer, returns 0, else
152  * returns 1. A logical offset at the end of the range retained by the ring
153  * buffer is not considered an error and is returned with a *buf_len of 0.
154  *
155  * The ring buffer state is not changed.
156  */
157 static ossl_inline int ring_buf_get_buf_at(const struct ring_buf *r,
158                                            uint64_t logical_offset,
159                                            const unsigned char **buf,
160                                            size_t *buf_len)
161 {
162     const unsigned char *start = r->start;
163     size_t idx, l;
164
165     if (logical_offset > r->head_offset || logical_offset < r->ctail_offset)
166         return 0;
167
168     if (r->alloc == 0) {
169         *buf        = NULL;
170         *buf_len    = 0;
171         return 1;
172     }
173
174     idx = logical_offset % r->alloc;
175     l   = (size_t)(r->head_offset - logical_offset);
176     if (l > r->alloc - idx)
177         l = r->alloc - idx;
178
179     *buf        = start + idx;
180     *buf_len    = l;
181     return 1;
182 }
183
184 static ossl_inline void ring_buf_cpop_range(struct ring_buf *r,
185                                             uint64_t start, uint64_t end,
186                                             int cleanse)
187 {
188     assert(end >= start);
189
190     if (start > r->ctail_offset)
191         return;
192
193     if (cleanse && r->alloc > 0 && end > r->ctail_offset) {
194         size_t idx = r->ctail_offset % r->alloc;
195         uint64_t cleanse_end = end + 1;
196         size_t l;
197
198         if (cleanse_end > r->head_offset)
199             cleanse_end = r->head_offset;
200         l = (size_t)(cleanse_end - r->ctail_offset);
201         if (l > r->alloc - idx) {
202             OPENSSL_cleanse((unsigned char *)r->start + idx, r->alloc - idx);
203             l -= r->alloc - idx;
204             idx = 0;
205         }
206         if (l > 0)
207             OPENSSL_cleanse((unsigned char *)r->start + idx, l);
208     }
209
210     r->ctail_offset = end + 1;
211     /* Allow culling unpushed data */
212     if (r->head_offset < r->ctail_offset)
213         r->head_offset = r->ctail_offset;
214 }
215
216 static ossl_inline int ring_buf_resize(struct ring_buf *r, size_t num_bytes)
217 {
218     struct ring_buf rnew = {0};
219     const unsigned char *src = NULL;
220     size_t src_len = 0, copied = 0;
221
222     if (num_bytes == r->alloc)
223         return 1;
224
225     if (num_bytes < ring_buf_used(r))
226         return 0;
227
228     rnew.start = OPENSSL_malloc(num_bytes);
229     if (rnew.start == NULL)
230         return 0;
231
232     rnew.alloc          = num_bytes;
233     rnew.head_offset    = r->head_offset - ring_buf_used(r);
234     rnew.ctail_offset   = rnew.head_offset;
235
236     for (;;) {
237         if (!ring_buf_get_buf_at(r, r->ctail_offset + copied, &src, &src_len)) {
238             OPENSSL_free(rnew.start);
239             return 0;
240         }
241
242         if (src_len == 0)
243             break;
244
245         if (ring_buf_push(&rnew, src, src_len) != src_len) {
246             OPENSSL_free(rnew.start);
247             return 0;
248         }
249
250         copied += src_len;
251     }
252
253     assert(rnew.head_offset == r->head_offset);
254     rnew.ctail_offset   = r->ctail_offset;
255
256     OPENSSL_free(r->start);
257     memcpy(r, &rnew, sizeof(*r));
258     return 1;
259 }
260
261 #endif                          /* OSSL_INTERNAL_RING_BUF_H */