inet6: only mark autoconf addresses tentative if detached
[dragonfly.git] / crypto / libressl / ssl / tls_content.c
blobede178f84cd0df5c2c544c34524f435891908a22
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>
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.
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.
18 #include <stdlib.h>
19 #include <string.h>
21 #include "tls_content.h"
23 /* Content from a TLS record. */
24 struct tls_content {
25 uint8_t type;
26 uint16_t epoch;
28 const uint8_t *data;
29 size_t len;
30 CBS cbs;
33 struct tls_content *
34 tls_content_new(void)
36 return calloc(1, sizeof(struct tls_content));
39 void
40 tls_content_clear(struct tls_content *content)
42 freezero((void *)content->data, content->len);
43 memset(content, 0, sizeof(*content));
46 void
47 tls_content_free(struct tls_content *content)
49 if (content == NULL)
50 return;
52 tls_content_clear(content);
54 freezero(content, sizeof(struct tls_content));
57 CBS *
58 tls_content_cbs(struct tls_content *content)
60 return &content->cbs;
63 int
64 tls_content_equal(struct tls_content *content, const uint8_t *buf, size_t n)
66 return CBS_mem_equal(&content->cbs, buf, n);
69 size_t
70 tls_content_remaining(struct tls_content *content)
72 return CBS_len(&content->cbs);
75 uint8_t
76 tls_content_type(struct tls_content *content)
78 return content->type;
81 int
82 tls_content_dup_data(struct tls_content *content, uint8_t type,
83 const uint8_t *data, size_t data_len)
85 uint8_t *dup;
87 if ((dup = calloc(1, data_len)) == NULL)
88 return 0;
89 memcpy(dup, data, data_len);
91 tls_content_set_data(content, type, dup, data_len);
93 return 1;
96 uint16_t
97 tls_content_epoch(struct tls_content *content)
99 return content->epoch;
102 void
103 tls_content_set_epoch(struct tls_content *content, uint16_t epoch)
105 content->epoch = epoch;
108 void
109 tls_content_set_data(struct tls_content *content, uint8_t type,
110 const uint8_t *data, size_t data_len)
112 tls_content_clear(content);
114 content->type = type;
115 content->data = data;
116 content->len = data_len;
118 CBS_init(&content->cbs, content->data, content->len);
121 static ssize_t
122 tls_content_read_internal(struct tls_content *content, uint8_t *buf, size_t n,
123 int peek)
125 if (n > CBS_len(&content->cbs))
126 n = CBS_len(&content->cbs);
128 /* XXX - CBS_memcpy? CBS_copy_bytes? */
129 memcpy(buf, CBS_data(&content->cbs), n);
131 if (!peek) {
132 if (!CBS_skip(&content->cbs, n))
133 return -1;
136 return n;
139 ssize_t
140 tls_content_peek(struct tls_content *content, uint8_t *buf, size_t n)
142 return tls_content_read_internal(content, buf, n, 1);
145 ssize_t
146 tls_content_read(struct tls_content *content, uint8_t *buf, size_t n)
148 return tls_content_read_internal(content, buf, n, 0);