1 /* $OpenBSD: tls_content.c,v 1.2 2022/11/11 17:15:27 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. */
24 struct tls_content {
25 uint8_t type;
26 uint16_t epoch;
27
28 const uint8_t *data;
29 size_t data_len;
30 CBS cbs;
31 };
32
33 struct tls_content *
tls_content_new(void)34 tls_content_new(void)
35 {
36 return calloc(1, sizeof(struct tls_content));
37 }
38
39 void
tls_content_clear(struct tls_content * content)40 tls_content_clear(struct tls_content *content)
41 {
42 freezero((void *)content->data, content->data_len);
43 memset(content, 0, sizeof(*content));
44 }
45
46 void
tls_content_free(struct tls_content * content)47 tls_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
57 CBS *
tls_content_cbs(struct tls_content * content)58 tls_content_cbs(struct tls_content *content)
59 {
60 return &content->cbs;
61 }
62
63 int
tls_content_equal(struct tls_content * content,const uint8_t * buf,size_t n)64 tls_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
69 size_t
tls_content_remaining(struct tls_content * content)70 tls_content_remaining(struct tls_content *content)
71 {
72 return CBS_len(&content->cbs);
73 }
74
75 uint8_t
tls_content_type(struct tls_content * content)76 tls_content_type(struct tls_content *content)
77 {
78 return content->type;
79 }
80
81 int
tls_content_dup_data(struct tls_content * content,uint8_t type,const uint8_t * data,size_t data_len)82 tls_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
96 uint16_t
tls_content_epoch(struct tls_content * content)97 tls_content_epoch(struct tls_content *content)
98 {
99 return content->epoch;
100 }
101
102 void
tls_content_set_epoch(struct tls_content * content,uint16_t epoch)103 tls_content_set_epoch(struct tls_content *content, uint16_t epoch)
104 {
105 content->epoch = epoch;
106 }
107
108 void
tls_content_set_data(struct tls_content * content,uint8_t type,const uint8_t * data,size_t data_len)109 tls_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->data_len = data_len;
117
118 CBS_init(&content->cbs, content->data, content->data_len);
119 }
120
121 int
tls_content_set_bounds(struct tls_content * content,size_t offset,size_t len)122 tls_content_set_bounds(struct tls_content *content, size_t offset, size_t len)
123 {
124 size_t content_len;
125
126 content_len = offset + len;
127 if (content_len < len)
128 return 0;
129 if (content_len > content->data_len)
130 return 0;
131
132 CBS_init(&content->cbs, content->data, content_len);
133 return CBS_skip(&content->cbs, offset);
134 }
135
136 static ssize_t
tls_content_read_internal(struct tls_content * content,uint8_t * buf,size_t n,int peek)137 tls_content_read_internal(struct tls_content *content, uint8_t *buf, size_t n,
138 int peek)
139 {
140 if (n > CBS_len(&content->cbs))
141 n = CBS_len(&content->cbs);
142
143 /* XXX - CBS_memcpy? CBS_copy_bytes? */
144 memcpy(buf, CBS_data(&content->cbs), n);
145
146 if (!peek) {
147 if (!CBS_skip(&content->cbs, n))
148 return -1;
149 }
150
151 return n;
152 }
153
154 ssize_t
tls_content_peek(struct tls_content * content,uint8_t * buf,size_t n)155 tls_content_peek(struct tls_content *content, uint8_t *buf, size_t n)
156 {
157 return tls_content_read_internal(content, buf, n, 1);
158 }
159
160 ssize_t
tls_content_read(struct tls_content * content,uint8_t * buf,size_t n)161 tls_content_read(struct tls_content *content, uint8_t *buf, size_t n)
162 {
163 return tls_content_read_internal(content, buf, n, 0);
164 }
165