summaryrefslogtreecommitdiff
path: root/src/lib/libssl/tls_content.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libssl/tls_content.c')
-rw-r--r--src/lib/libssl/tls_content.c149
1 files changed, 149 insertions, 0 deletions
diff --git a/src/lib/libssl/tls_content.c b/src/lib/libssl/tls_content.c
new file mode 100644
index 0000000000..ede178f84c
--- /dev/null
+++ b/src/lib/libssl/tls_content.c
@@ -0,0 +1,149 @@
1/* $OpenBSD: tls_content.c,v 1.1 2021/09/04 16:26:12 jsing Exp $ */
2/*
3 * Copyright (c) 2020 Joel Sing <jsing@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <stdlib.h>
19#include <string.h>
20
21#include "tls_content.h"
22
23/* Content from a TLS record. */
24struct tls_content {
25 uint8_t type;
26 uint16_t epoch;
27
28 const uint8_t *data;
29 size_t len;
30 CBS cbs;
31};
32
33struct tls_content *
34tls_content_new(void)
35{
36 return calloc(1, sizeof(struct tls_content));
37}
38
39void
40tls_content_clear(struct tls_content *content)
41{
42 freezero((void *)content->data, content->len);
43 memset(content, 0, sizeof(*content));
44}
45
46void
47tls_content_free(struct tls_content *content)
48{
49 if (content == NULL)
50 return;
51
52 tls_content_clear(content);
53
54 freezero(content, sizeof(struct tls_content));
55}
56
57CBS *
58tls_content_cbs(struct tls_content *content)
59{
60 return &content->cbs;
61}
62
63int
64tls_content_equal(struct tls_content *content, const uint8_t *buf, size_t n)
65{
66 return CBS_mem_equal(&content->cbs, buf, n);
67}
68
69size_t
70tls_content_remaining(struct tls_content *content)
71{
72 return CBS_len(&content->cbs);
73}
74
75uint8_t
76tls_content_type(struct tls_content *content)
77{
78 return content->type;
79}
80
81int
82tls_content_dup_data(struct tls_content *content, uint8_t type,
83 const uint8_t *data, size_t data_len)
84{
85 uint8_t *dup;
86
87 if ((dup = calloc(1, data_len)) == NULL)
88 return 0;
89 memcpy(dup, data, data_len);
90
91 tls_content_set_data(content, type, dup, data_len);
92
93 return 1;
94}
95
96uint16_t
97tls_content_epoch(struct tls_content *content)
98{
99 return content->epoch;
100}
101
102void
103tls_content_set_epoch(struct tls_content *content, uint16_t epoch)
104{
105 content->epoch = epoch;
106}
107
108void
109tls_content_set_data(struct tls_content *content, uint8_t type,
110 const uint8_t *data, size_t data_len)
111{
112 tls_content_clear(content);
113
114 content->type = type;
115 content->data = data;
116 content->len = data_len;
117
118 CBS_init(&content->cbs, content->data, content->len);
119}
120
121static ssize_t
122tls_content_read_internal(struct tls_content *content, uint8_t *buf, size_t n,
123 int peek)
124{
125 if (n > CBS_len(&content->cbs))
126 n = CBS_len(&content->cbs);
127
128 /* XXX - CBS_memcpy? CBS_copy_bytes? */
129 memcpy(buf, CBS_data(&content->cbs), n);
130
131 if (!peek) {
132 if (!CBS_skip(&content->cbs, n))
133 return -1;
134 }
135
136 return n;
137}
138
139ssize_t
140tls_content_peek(struct tls_content *content, uint8_t *buf, size_t n)
141{
142 return tls_content_read_internal(content, buf, n, 1);
143}
144
145ssize_t
146tls_content_read(struct tls_content *content, uint8_t *buf, size_t n)
147{
148 return tls_content_read_internal(content, buf, n, 0);
149}