1/* $OpenBSD: tls_internal.h,v 1.10 2022/11/10 18:06:37 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 36enum ssl_encryption_level_t; 37 38struct tls13_secret; 39 40/* 41 * Callbacks. 42 */ 43typedef ssize_t (*tls_read_cb)(void *_buf, size_t _buflen, void *_cb_arg); 44typedef ssize_t (*tls_write_cb)(const void *_buf, size_t _buflen, 45 void *_cb_arg); 46typedef ssize_t (*tls_flush_cb)(void *_cb_arg); 47 48typedef ssize_t (*tls_handshake_read_cb)(void *_buf, size_t _buflen, 49 void *_cb_arg); 50typedef ssize_t (*tls_handshake_write_cb)(const void *_buf, size_t _buflen, 51 void *_cb_arg); 52typedef int (*tls_traffic_key_cb)(struct tls13_secret *key, 53 enum ssl_encryption_level_t level, void *_cb_arg); 54typedef int (*tls_alert_send_cb)(int _alert_desc, void *_cb_arg); 55 56/* 57 * Buffers. 58 */ 59struct tls_buffer; 60 61struct tls_buffer *tls_buffer_new(size_t init_size); 62void tls_buffer_clear(struct tls_buffer *buf); 63void tls_buffer_free(struct tls_buffer *buf); 64void tls_buffer_set_capacity_limit(struct tls_buffer *buf, size_t limit); 65ssize_t tls_buffer_extend(struct tls_buffer *buf, size_t len, 66 tls_read_cb read_cb, void *cb_arg); 67size_t tls_buffer_remaining(struct tls_buffer *buf); 68ssize_t tls_buffer_read(struct tls_buffer *buf, uint8_t *rbuf, size_t n); 69ssize_t tls_buffer_write(struct tls_buffer *buf, const uint8_t *wbuf, size_t n); 70int tls_buffer_append(struct tls_buffer *buf, const uint8_t *wbuf, size_t n); 71int tls_buffer_data(struct tls_buffer *buf, CBS *cbs); 72int tls_buffer_finish(struct tls_buffer *buf, uint8_t **out, size_t *out_len); 73 74/* 75 * Key shares. 76 */ 77struct tls_key_share; 78 79struct tls_key_share *tls_key_share_new(uint16_t group_id); 80struct tls_key_share *tls_key_share_new_nid(int nid); 81void tls_key_share_free(struct tls_key_share *ks); 82 83uint16_t tls_key_share_group(struct tls_key_share *ks); 84int tls_key_share_nid(struct tls_key_share *ks); 85void tls_key_share_set_key_bits(struct tls_key_share *ks, size_t key_bits); 86int tls_key_share_set_dh_params(struct tls_key_share *ks, DH *dh_params); 87int tls_key_share_peer_pkey(struct tls_key_share *ks, EVP_PKEY *pkey); 88int tls_key_share_generate(struct tls_key_share *ks); 89int tls_key_share_params(struct tls_key_share *ks, CBB *cbb); 90int tls_key_share_public(struct tls_key_share *ks, CBB *cbb); 91int tls_key_share_peer_params(struct tls_key_share *ks, CBS *cbs, 92 int *decode_error, int *invalid_params); 93int tls_key_share_peer_public(struct tls_key_share *ks, CBS *cbs, 94 int *decode_error, int *invalid_key); 95int tls_key_share_derive(struct tls_key_share *ks, uint8_t **shared_key, 96 size_t *shared_key_len); 97int tls_key_share_peer_security(const SSL *ssl, struct tls_key_share *ks); 98 99__END_HIDDEN_DECLS 100 101#endif 102