1 /* $OpenBSD: tls_content.c,v 1.1 2021/09/04 16:26:12 jsing Exp $ */
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.
21 #include "tls_content.h"
23 /* Content from a TLS record. */
36 return calloc(1, sizeof(struct tls_content
));
40 tls_content_clear(struct tls_content
*content
)
42 freezero((void *)content
->data
, content
->len
);
43 memset(content
, 0, sizeof(*content
));
47 tls_content_free(struct tls_content
*content
)
52 tls_content_clear(content
);
54 freezero(content
, sizeof(struct tls_content
));
58 tls_content_cbs(struct tls_content
*content
)
64 tls_content_equal(struct tls_content
*content
, const uint8_t *buf
, size_t n
)
66 return CBS_mem_equal(&content
->cbs
, buf
, n
);
70 tls_content_remaining(struct tls_content
*content
)
72 return CBS_len(&content
->cbs
);
76 tls_content_type(struct tls_content
*content
)
82 tls_content_dup_data(struct tls_content
*content
, uint8_t type
,
83 const uint8_t *data
, size_t data_len
)
87 if ((dup
= calloc(1, data_len
)) == NULL
)
89 memcpy(dup
, data
, data_len
);
91 tls_content_set_data(content
, type
, dup
, data_len
);
97 tls_content_epoch(struct tls_content
*content
)
99 return content
->epoch
;
103 tls_content_set_epoch(struct tls_content
*content
, uint16_t epoch
)
105 content
->epoch
= epoch
;
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
);
122 tls_content_read_internal(struct tls_content
*content
, uint8_t *buf
, size_t n
,
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
);
132 if (!CBS_skip(&content
->cbs
, n
))
140 tls_content_peek(struct tls_content
*content
, uint8_t *buf
, size_t n
)
142 return tls_content_read_internal(content
, buf
, n
, 1);
146 tls_content_read(struct tls_content
*content
, uint8_t *buf
, size_t n
)
148 return tls_content_read_internal(content
, buf
, n
, 0);