xref: /src/crypto/openssl/include/internal/bio.h (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
1 /*
2  * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #ifndef OSSL_INTERNAL_BIO_H
11 #define OSSL_INTERNAL_BIO_H
12 #pragma once
13 
14 #include <openssl/core.h>
15 #include <openssl/bio.h>
16 
17 struct bio_method_st {
18     int type;
19     char *name;
20     int (*bwrite)(BIO *, const char *, size_t, size_t *);
21     int (*bwrite_old)(BIO *, const char *, int);
22     int (*bread)(BIO *, char *, size_t, size_t *);
23     int (*bread_old)(BIO *, char *, int);
24     int (*bputs)(BIO *, const char *);
25     int (*bgets)(BIO *, char *, int);
26     long (*ctrl)(BIO *, int, long, void *);
27     int (*create)(BIO *);
28     int (*destroy)(BIO *);
29     long (*callback_ctrl)(BIO *, int, BIO_info_cb *);
30     int (*bsendmmsg)(BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *);
31     int (*brecvmmsg)(BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *);
32 };
33 
34 void bio_free_ex_data(BIO *bio);
35 void bio_cleanup(void);
36 
37 /* Old style to new style BIO_METHOD conversion functions */
38 int bwrite_conv(BIO *bio, const char *data, size_t datal, size_t *written);
39 int bread_conv(BIO *bio, char *data, size_t datal, size_t *read);
40 
41 /* Changes to these internal BIOs must also update include/openssl/bio.h */
42 #define BIO_CTRL_SET_KTLS 72
43 #define BIO_CTRL_SET_KTLS_TX_SEND_CTRL_MSG 74
44 #define BIO_CTRL_CLEAR_KTLS_TX_CTRL_MSG 75
45 #define BIO_CTRL_SET_KTLS_TX_ZEROCOPY_SENDFILE 90
46 
47 /*
48  * This is used with socket BIOs:
49  * BIO_FLAGS_KTLS_TX means we are using ktls with this BIO for sending.
50  * BIO_FLAGS_KTLS_TX_CTRL_MSG means we are about to send a ctrl message next.
51  * BIO_FLAGS_KTLS_RX means we are using ktls with this BIO for receiving.
52  * BIO_FLAGS_KTLS_TX_ZEROCOPY_SENDFILE means we are using the zerocopy mode with
53  * this BIO for sending using sendfile.
54  */
55 #define BIO_FLAGS_KTLS_TX_CTRL_MSG 0x1000
56 #define BIO_FLAGS_KTLS_RX 0x2000
57 #define BIO_FLAGS_KTLS_TX 0x4000
58 #define BIO_FLAGS_KTLS_TX_ZEROCOPY_SENDFILE 0x8000
59 
60 /* KTLS related controls and flags */
61 #define BIO_set_ktls_flag(b, is_tx) \
62     BIO_set_flags(b, (is_tx) ? BIO_FLAGS_KTLS_TX : BIO_FLAGS_KTLS_RX)
63 #define BIO_should_ktls_flag(b, is_tx) \
64     BIO_test_flags(b, (is_tx) ? BIO_FLAGS_KTLS_TX : BIO_FLAGS_KTLS_RX)
65 #define BIO_set_ktls_ctrl_msg_flag(b) \
66     BIO_set_flags(b, BIO_FLAGS_KTLS_TX_CTRL_MSG)
67 #define BIO_should_ktls_ctrl_msg_flag(b) \
68     BIO_test_flags(b, BIO_FLAGS_KTLS_TX_CTRL_MSG)
69 #define BIO_clear_ktls_ctrl_msg_flag(b) \
70     BIO_clear_flags(b, BIO_FLAGS_KTLS_TX_CTRL_MSG)
71 #define BIO_set_ktls_zerocopy_sendfile_flag(b) \
72     BIO_set_flags(b, BIO_FLAGS_KTLS_TX_ZEROCOPY_SENDFILE)
73 
74 #define BIO_set_ktls(b, keyblob, is_tx) \
75     BIO_ctrl(b, BIO_CTRL_SET_KTLS, is_tx, keyblob)
76 #define BIO_set_ktls_ctrl_msg(b, record_type) \
77     BIO_ctrl(b, BIO_CTRL_SET_KTLS_TX_SEND_CTRL_MSG, record_type, NULL)
78 #define BIO_clear_ktls_ctrl_msg(b) \
79     BIO_ctrl(b, BIO_CTRL_CLEAR_KTLS_TX_CTRL_MSG, 0, NULL)
80 #define BIO_set_ktls_tx_zerocopy_sendfile(b) \
81     BIO_ctrl(b, BIO_CTRL_SET_KTLS_TX_ZEROCOPY_SENDFILE, 0, NULL)
82 
83 /* Functions to allow the core to offer the CORE_BIO type to providers */
84 OSSL_CORE_BIO *ossl_core_bio_new_from_bio(BIO *bio);
85 OSSL_CORE_BIO *ossl_core_bio_new_file(const char *filename, const char *mode);
86 OSSL_CORE_BIO *ossl_core_bio_new_mem_buf(const void *buf, int len);
87 int ossl_core_bio_read_ex(OSSL_CORE_BIO *cb, void *data, size_t dlen,
88     size_t *readbytes);
89 int ossl_core_bio_write_ex(OSSL_CORE_BIO *cb, const void *data, size_t dlen,
90     size_t *written);
91 int ossl_core_bio_gets(OSSL_CORE_BIO *cb, char *buf, int size);
92 int ossl_core_bio_puts(OSSL_CORE_BIO *cb, const char *buf);
93 long ossl_core_bio_ctrl(OSSL_CORE_BIO *cb, int cmd, long larg, void *parg);
94 int ossl_core_bio_up_ref(OSSL_CORE_BIO *cb);
95 int ossl_core_bio_free(OSSL_CORE_BIO *cb);
96 int ossl_core_bio_vprintf(OSSL_CORE_BIO *cb, const char *format, va_list args);
97 
98 int ossl_bio_init_core(OSSL_LIB_CTX *libctx, const OSSL_DISPATCH *fns);
99 
100 #endif
101