1 /* $OpenBSD: tls_internal.h,v 1.9 2022/07/24 14:28:16 jsing Exp $ */
2 /*
3  * Copyright (c) 2018, 2019, 2021 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 #ifndef HEADER_TLS_INTERNAL_H
19 #define HEADER_TLS_INTERNAL_H
20 
21 #include <openssl/dh.h>
22 #include <openssl/evp.h>
23 
24 #include "bytestring.h"
25 
26 __BEGIN_HIDDEN_DECLS
27 
28 #define TLS_IO_SUCCESS			 1
29 #define TLS_IO_EOF			 0
30 #define TLS_IO_FAILURE			-1
31 #define TLS_IO_ALERT			-2
32 #define TLS_IO_WANT_POLLIN		-3
33 #define TLS_IO_WANT_POLLOUT		-4
34 #define TLS_IO_WANT_RETRY		-5 /* Retry the previous call immediately. */
35 
36 enum ssl_encryption_level_t;
37 
38 struct tls13_secret;
39 
40 /*
41  * Callbacks.
42  */
43 typedef ssize_t (*tls_read_cb)(void *_buf, size_t _buflen, void *_cb_arg);
44 typedef ssize_t (*tls_write_cb)(const void *_buf, size_t _buflen,
45     void *_cb_arg);
46 typedef ssize_t (*tls_flush_cb)(void *_cb_arg);
47 
48 typedef ssize_t (*tls_handshake_read_cb)(void *_buf, size_t _buflen,
49     void *_cb_arg);
50 typedef ssize_t (*tls_handshake_write_cb)(const void *_buf, size_t _buflen,
51     void *_cb_arg);
52 typedef int (*tls_traffic_key_cb)(struct tls13_secret *key,
53     enum ssl_encryption_level_t level, void *_cb_arg);
54 typedef int (*tls_alert_send_cb)(int _alert_desc, void *_cb_arg);
55 
56 /*
57  * Buffers.
58  */
59 struct tls_buffer;
60 
61 struct tls_buffer *tls_buffer_new(size_t init_size);
62 void tls_buffer_clear(struct tls_buffer *buf);
63 void tls_buffer_free(struct tls_buffer *buf);
64 void tls_buffer_set_capacity_limit(struct tls_buffer *buf, size_t limit);
65 ssize_t tls_buffer_extend(struct tls_buffer *buf, size_t len,
66     tls_read_cb read_cb, void *cb_arg);
67 ssize_t tls_buffer_read(struct tls_buffer *buf, uint8_t *rbuf, size_t n);
68 ssize_t tls_buffer_write(struct tls_buffer *buf, const uint8_t *wbuf, size_t n);
69 int tls_buffer_append(struct tls_buffer *buf, const uint8_t *wbuf, size_t n);
70 int tls_buffer_data(struct tls_buffer *buf, CBS *cbs);
71 int tls_buffer_finish(struct tls_buffer *buf, uint8_t **out, size_t *out_len);
72 
73 /*
74  * Key shares.
75  */
76 struct tls_key_share;
77 
78 struct tls_key_share *tls_key_share_new(uint16_t group_id);
79 struct tls_key_share *tls_key_share_new_nid(int nid);
80 void tls_key_share_free(struct tls_key_share *ks);
81 
82 uint16_t tls_key_share_group(struct tls_key_share *ks);
83 int tls_key_share_nid(struct tls_key_share *ks);
84 void tls_key_share_set_key_bits(struct tls_key_share *ks, size_t key_bits);
85 int tls_key_share_set_dh_params(struct tls_key_share *ks, DH *dh_params);
86 int tls_key_share_peer_pkey(struct tls_key_share *ks, EVP_PKEY *pkey);
87 int tls_key_share_generate(struct tls_key_share *ks);
88 int tls_key_share_params(struct tls_key_share *ks, CBB *cbb);
89 int tls_key_share_public(struct tls_key_share *ks, CBB *cbb);
90 int tls_key_share_peer_params(struct tls_key_share *ks, CBS *cbs,
91     int *decode_error, int *invalid_params);
92 int tls_key_share_peer_public(struct tls_key_share *ks, CBS *cbs,
93     int *decode_error, int *invalid_key);
94 int tls_key_share_derive(struct tls_key_share *ks, uint8_t **shared_key,
95     size_t *shared_key_len);
96 int tls_key_share_peer_security(const SSL *ssl, struct tls_key_share *ks);
97 
98 __END_HIDDEN_DECLS
99 
100 #endif
101