xref: /dragonfly/crypto/libressl/ssl/tls_content.c (revision 6f5ec8b5)
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. */
24 struct 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 
33 struct tls_content *
34 tls_content_new(void)
35 {
36 	return calloc(1, sizeof(struct tls_content));
37 }
38 
39 void
40 tls_content_clear(struct tls_content *content)
41 {
42 	freezero((void *)content->data, content->len);
43 	memset(content, 0, sizeof(*content));
44 }
45 
46 void
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 *
58 tls_content_cbs(struct tls_content *content)
59 {
60 	return &content->cbs;
61 }
62 
63 int
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
70 tls_content_remaining(struct tls_content *content)
71 {
72 	return CBS_len(&content->cbs);
73 }
74 
75 uint8_t
76 tls_content_type(struct tls_content *content)
77 {
78 	return content->type;
79 }
80 
81 int
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
97 tls_content_epoch(struct tls_content *content)
98 {
99 	return content->epoch;
100 }
101 
102 void
103 tls_content_set_epoch(struct tls_content *content, uint16_t epoch)
104 {
105 	content->epoch = epoch;
106 }
107 
108 void
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->len = data_len;
117 
118 	CBS_init(&content->cbs, content->data, content->len);
119 }
120 
121 static ssize_t
122 tls_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 
139 ssize_t
140 tls_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 
145 ssize_t
146 tls_content_read(struct tls_content *content, uint8_t *buf, size_t n)
147 {
148 	return tls_content_read_internal(content, buf, n, 0);
149 }
150