libpktlab - The PacketLab Utility Library
|
The pktlab pktctrl module. More...
#include <pktlab.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <poll.h>
#include <sys/select.h>
Go to the source code of this file.
Macros | |
#define | PKTCTRL_SNIKEY_LEN_MIN 1 |
Minimum pktlab snikey length. | |
#define | PKTCTRL_SNIKEY_LEN_MAX 63 |
Maximum pktlab snikey length. More... | |
Enumerations | |
enum | pktctrl_err { PKTCTRL_SUCCESS = 0, PKTCTRL_INVAL = 1, PKTCTRL_MAP_ERR = 2, PKTCTRL_KEY_NOT_FOUND = 3, PKTCTRL_SSL_FAILURE = 4, PKTCTRL_BAD_CERT = 5, PKTCTRL_BAD_CERTKEY = 6, PKTCTRL_IN_PROGRESS = 7, PKTCTRL_UNKNOWN_FAULT = 127 } |
pktctrl module defined error values. | |
Functions | |
struct pktctrl_obj * | pktctrl_create_obj (void) |
Get initialized pktctrl session object struct. More... | |
struct pktctrl_keycert_store * | pktctrl_create_keycert_store (void) |
Get initialized pktctrl key certificate storage struct. More... | |
int | pktctrl_set_server_keycert (struct pktctrl_keycert_store *store, const char *snikey, struct pktlab_certificate **cert_ls, uint_fast32_t certnum, struct pktlab_publickey **root_ls, uint_fast32_t rootnum) |
Load server certificate chain and trusted roots for some snikey into pktctrl key certificate storage struct. More... | |
int | pktctrl_get_server_keycert (struct pktctrl_keycert_store *store, const char *snikey, struct pktlab_certificate ***cert_ls, uint_fast32_t *certnum, struct pktlab_publickey ***root_ls, uint_fast32_t *rootnum) |
Get loaded server certificate chain and trusted roots for some snikey from server pktctrl key certificate storage struct. More... | |
int | pktctrl_clear_server_keycert (struct pktctrl_keycert_store *store, const char *snikey) |
Delete server certificate chain and trusted roots entry for some snikey from server pktctrl key certificate storage struct. More... | |
int | pktctrl_set_server_keycert_default (struct pktctrl_keycert_store *store, const char *snikey) |
Set server pktctrl key certificate storage default entry. More... | |
int | pktctrl_clear_server_keycert_default (struct pktctrl_keycert_store *store) |
Clear server pktctrl key certificate storage default entry. More... | |
int | pktctrl_set_client_keycert (struct pktctrl_keycert_store *store, struct pktlab_certificate **cert_ls, uint_fast32_t certnum, struct pktlab_publickey **root_ls, uint_fast32_t rootnum) |
Load client certificate chain and trusted roots into pktctrl key certificate storage struct. More... | |
int | pktctrl_get_client_keycert (struct pktctrl_keycert_store *store, struct pktlab_certificate ***cert_ls, uint_fast32_t *certnum, struct pktlab_publickey ***root_ls, uint_fast32_t *rootnum) |
Get loaded client certificate chain and trusted roots from client pktctrl key certificate storage struct. More... | |
int | pktctrl_clear_client_keycert (struct pktctrl_keycert_store *store) |
Delete client certificate chain and trusted roots from client pktctrl key certificate storage struct. More... | |
int | pktctrl_accept (int fd, int auth_mode, struct pktlab_privatekey *serverkey, struct pktctrl_keycert_store *store, struct pktctrl_obj *ctrlobj, char **client_snikey, struct pktlab_certificate ***peer_cert_ls, uint_fast32_t *peer_cert_num) |
Perform TLS server-side handshake to establish TLS pktctrl session. More... | |
int | pktctrl_connect (int fd, int auth_mode, const char *snikey, struct pktlab_privatekey *clientkey, struct pktctrl_keycert_store *store, struct pktctrl_obj *ctrlobj, struct pktlab_certificate ***peer_cert_ls, uint_fast32_t *peer_cert_num) |
Perform TLS client-side handshake to establish TLS pktctrl session. More... | |
int | pktctrl_raw_session (int fd, struct pktctrl_obj *ctrlobj) |
Set up pktctrl session object struct to establish barebone TCP pktctrl session. More... | |
int | pktctrl_prepare_select (int *nfds, fd_set *restrict rset, fd_set *restrict wset, bool want_read, bool want_write, struct pktctrl_obj *ctrlobj) |
Set up arguments to select() for read/write operation on established pktctrl session based on intention. More... | |
int | pktctrl_process_select (const fd_set *restrict rset, const fd_set *restrict wset, struct pktctrl_obj *ctrlobj, bool *readable, bool *writable) |
Process select() results to identify if read/write operation on established pktctrl session can be performed. More... | |
int | pktctrl_prepare_poll (struct pollfd *restrict pfd, bool want_read, bool want_write, struct pktctrl_obj *ctrlobj) |
int | pktctrl_process_poll (const struct pollfd *restrict pfd, struct pktctrl_obj *ctrlobj, bool *readable, bool *writable, bool *err) |
int | pktctrl_read_message (struct pktctrl_obj *ctrlobj, struct pktlab_message **msgptr) |
Read pktctrl message from established pktctrl session. More... | |
int | pktctrl_write_message (struct pktctrl_obj *ctrlobj, const struct pktlab_message *msg) |
Write pktctrl message to established pktctrl session. More... | |
int | pktctrl_flush_write (struct pktctrl_obj *ctrlobj) |
Flush established pktctrl session internal writer buffer. More... | |
size_t | pktctrl_write_unsent (const struct pktctrl_obj *ctrlobj) |
Get established pktctrl session internal writer buffered content length. More... | |
ssize_t | pktctrl_read (struct pktctrl_obj *obj, void *buf, size_t nbyte) |
Read bytes from established pktctrl session. More... | |
ssize_t | pktctrl_write (struct pktctrl_obj *obj, const void *buf, size_t nbyte) |
Write bytes to established pktctrl session. More... | |
int | pktctrl_close (struct pktctrl_obj *obj) |
Close and free an allocated pktctrl session object struct. More... | |
int | pktctrl_cleanup_keycert_store (struct pktctrl_keycert_store *store) |
Free an allocated pktctrl key certificate storage struct. More... | |
bool | pktctrl_is_established (struct pktctrl_obj *obj) |
Check if a pktctrl session object is established. More... | |
bool | pktctrl_is_valid_snikey (const char *snikey) |
Check if '\0'-terminated string is a valid snikey. More... | |
int | pktctrl_gen_snikey (char **buf) |
Generate random '\0'-terminated valid snikey of PKTCTRL_SNIKEY_LEN_MAX length. More... | |
The pktlab pktctrl module.
For pktlab protocol message communication among pktlab entities. Note snikey is the same as SNI in normal TLS context.
#define PKTCTRL_SNIKEY_LEN_MAX 63 |
Maximum pktlab snikey length.
Defined to be 63 for TLS library compatibility, as 63 is the maximum domain name label length.
int pktctrl_accept | ( | int | fd, |
int | auth_mode, | ||
struct pktlab_privatekey * | serverkey, | ||
struct pktctrl_keycert_store * | store, | ||
struct pktctrl_obj * | ctrlobj, | ||
char ** | client_snikey, | ||
struct pktlab_certificate *** | peer_cert_ls, | ||
uint_fast32_t * | peer_cert_num | ||
) |
Perform TLS server-side handshake to establish TLS pktctrl session.
[in] | fd | File descriptor for an established TCP connection. |
[in] | auth_mode | The certificate chain verification mode to use. One of enum pktlab_auth_mode. |
[in] | serverkey | Pointer to a loaded pktlab private key struct to be used as TLS server key. |
[in] | store | Pointer to a loaded server pktctrl key certificate storage struct. |
[in,out] | ctrlobj | Pointer to an initialized pktctrl session object struct. |
[out] | client_snikey | Pointer to return the client-supplied snikey. |
[out] | peer_cert_ls | Pointer to return client certificate chain. |
[out] | peer_cert_num | The number of returned client certificates. |
int pktctrl_cleanup_keycert_store | ( | struct pktctrl_keycert_store * | store | ) |
Free an allocated pktctrl key certificate storage struct.
[in,out] | store | Pointer to an allocated pktctrl key certificate storage struct to free. |
int pktctrl_clear_client_keycert | ( | struct pktctrl_keycert_store * | store | ) |
Delete client certificate chain and trusted roots from client pktctrl key certificate storage struct.
[in,out] | store | Pointer to a client pktctrl key certificate storage struct. |
int pktctrl_clear_server_keycert | ( | struct pktctrl_keycert_store * | store, |
const char * | snikey | ||
) |
Delete server certificate chain and trusted roots entry for some snikey from server pktctrl key certificate storage struct.
[in,out] | store | Pointer to a server pktctrl key certificate storage struct. |
[in] | snikey | Pointer to the target '\0'-terminated snikey. |
int pktctrl_clear_server_keycert_default | ( | struct pktctrl_keycert_store * | store | ) |
Clear server pktctrl key certificate storage default entry.
[in,out] | store | Pointer to a server pktctrl key certificate storage struct. |
int pktctrl_close | ( | struct pktctrl_obj * | obj | ) |
Close and free an allocated pktctrl session object struct.
[in,out] | obj | Pointer to an allocated pktctrl session object struct to free. |
int pktctrl_connect | ( | int | fd, |
int | auth_mode, | ||
const char * | snikey, | ||
struct pktlab_privatekey * | clientkey, | ||
struct pktctrl_keycert_store * | store, | ||
struct pktctrl_obj * | ctrlobj, | ||
struct pktlab_certificate *** | peer_cert_ls, | ||
uint_fast32_t * | peer_cert_num | ||
) |
Perform TLS client-side handshake to establish TLS pktctrl session.
[in] | fd | File descriptor for an established TCP connection. |
[in] | auth_mode | The certificate chain verification mode to use. One of enum pktlab_auth_mode. |
[in] | snikey | Pointer to a '\0'-terminated snikey or NULL. |
[in] | clientkey | Pointer to a loaded pktlab private key struct to be used as TLS client key. |
[in] | store | Pointer to a loaded client pktctrl key certificate storage struct. |
[in,out] | ctrlobj | Pointer to an initialized pktctrl session object struct. |
[out] | peer_cert_ls | Pointer to return client certificate chain. |
[out] | peer_cert_num | The number of returned client certificates. |
struct pktctrl_keycert_store* pktctrl_create_keycert_store | ( | void | ) |
Get initialized pktctrl key certificate storage struct.
struct pktctrl_obj* pktctrl_create_obj | ( | void | ) |
Get initialized pktctrl session object struct.
int pktctrl_flush_write | ( | struct pktctrl_obj * | ctrlobj | ) |
Flush established pktctrl session internal writer buffer.
[in,out] | ctrlobj | Pointer to an established pktctrl session object struct. |
int pktctrl_gen_snikey | ( | char ** | buf | ) |
Generate random '\0'-terminated valid snikey of PKTCTRL_SNIKEY_LEN_MAX length.
[in,out] | buf | Pointer to return allocated snikey. |
int pktctrl_get_client_keycert | ( | struct pktctrl_keycert_store * | store, |
struct pktlab_certificate *** | cert_ls, | ||
uint_fast32_t * | certnum, | ||
struct pktlab_publickey *** | root_ls, | ||
uint_fast32_t * | rootnum | ||
) |
Get loaded client certificate chain and trusted roots from client pktctrl key certificate storage struct.
[in] | store | Pointer to a client pktctrl key certificate storage struct. |
[out] | cert_ls | Pointer to return the loaded client certificate chain. |
[out] | certnum | Pointer to return the number of client certificates loaded. |
[out] | root_ls | Pointer to return the loaded trusted roots. |
[out] | rootnum | Pointer to return the number of trusted roots loaded. |
int pktctrl_get_server_keycert | ( | struct pktctrl_keycert_store * | store, |
const char * | snikey, | ||
struct pktlab_certificate *** | cert_ls, | ||
uint_fast32_t * | certnum, | ||
struct pktlab_publickey *** | root_ls, | ||
uint_fast32_t * | rootnum | ||
) |
Get loaded server certificate chain and trusted roots for some snikey from server pktctrl key certificate storage struct.
[in] | store | Pointer to a server pktctrl key certificate storage struct. |
[in] | snikey | Pointer to the target '\0'-terminated snikey. |
[out] | cert_ls | Pointer to return the loaded server certificate chain. |
[out] | certnum | Pointer to return the number of server certificates loaded. |
[out] | root_ls | Pointer to return the loaded trusted roots. |
[out] | rootnum | Pointer to return the number of trusted roots loaded. |
bool pktctrl_is_established | ( | struct pktctrl_obj * | obj | ) |
Check if a pktctrl session object is established.
[in] | obj | Pointer to a pktctrl session object struct. |
bool pktctrl_is_valid_snikey | ( | const char * | snikey | ) |
Check if '\0'-terminated string is a valid snikey.
[in] | snikey | Pointer to a '\0'-terminated string. |
int pktctrl_prepare_select | ( | int * | nfds, |
fd_set *restrict | rset, | ||
fd_set *restrict | wset, | ||
bool | want_read, | ||
bool | want_write, | ||
struct pktctrl_obj * | ctrlobj | ||
) |
Set up arguments to select() for read/write operation on established pktctrl session based on intention.
[in,out] | nfds | Pointer to update the nfds value to select on the ctrlobj fd. |
[in,out] | rset | Read fdset. May be set for ctrlobj fd. |
[in,out] | wset | Write fdset. May be set for ctrlobj fd. |
[in] | want_read | Intention on wanting to read from ctrlobj. |
[in] | want_write | Intention on wanting to write to ctrlobj. |
[in] | ctrlobj | Pointer to an established pktctrl session object struct. |
int pktctrl_process_select | ( | const fd_set *restrict | rset, |
const fd_set *restrict | wset, | ||
struct pktctrl_obj * | ctrlobj, | ||
bool * | readable, | ||
bool * | writable | ||
) |
Process select() results to identify if read/write operation on established pktctrl session can be performed.
[in] | rset | select()-set read fdset. |
[in] | wset | select()-set write fdset. |
[in] | ctrlobj | Pointer to an established pktctrl session object struct. |
[out] | readable | Pointer to return if read operation can be performed on ctrlobj. |
[out] | writable | Pointer to return if write operation can be performed on ctrlobj. |
int pktctrl_raw_session | ( | int | fd, |
struct pktctrl_obj * | ctrlobj | ||
) |
Set up pktctrl session object struct to establish barebone TCP pktctrl session.
[in] | fd | File descriptor for an established TCP connection. |
[in,out] | ctrlobj | Pointer to an initialized pktctrl session object struct. |
ssize_t pktctrl_read | ( | struct pktctrl_obj * | obj, |
void * | buf, | ||
size_t | nbyte | ||
) |
Read bytes from established pktctrl session.
[in,out] | obj | Pointer to an established pktctrl session object struct. |
[out] | buf | Pointer to buffer for storing the read bytes. |
[in] | buflen | Attempted read length (must be > 0). |
int pktctrl_read_message | ( | struct pktctrl_obj * | ctrlobj, |
struct pktlab_message ** | msgptr | ||
) |
Read pktctrl message from established pktctrl session.
[in,out] | ctrlobj | Pointer to an established pktctrl session object struct. |
[out] | msgptr | Pointer to return an allocated fields-set msg that was read. |
int pktctrl_set_client_keycert | ( | struct pktctrl_keycert_store * | store, |
struct pktlab_certificate ** | cert_ls, | ||
uint_fast32_t | certnum, | ||
struct pktlab_publickey ** | root_ls, | ||
uint_fast32_t | rootnum | ||
) |
Load client certificate chain and trusted roots into pktctrl key certificate storage struct.
[in,out] | store | Pointer to an initialized/client pktctrl key certificate storage struct. |
[in] | cert_ls | Array of pointers to loaded pktlab certificate structs to be loaded as the client certificate chain. |
[in] | certnum | The number of certificates being loaded. |
[in] | root_ls | Array of pointers to loaded pktlab public key structs to be loaded as the remote party certificate chain trusted roots. |
[in] | rootnum | The number of public keys being loaded. |
int pktctrl_set_server_keycert | ( | struct pktctrl_keycert_store * | store, |
const char * | snikey, | ||
struct pktlab_certificate ** | cert_ls, | ||
uint_fast32_t | certnum, | ||
struct pktlab_publickey ** | root_ls, | ||
uint_fast32_t | rootnum | ||
) |
Load server certificate chain and trusted roots for some snikey into pktctrl key certificate storage struct.
[in,out] | store | Pointer to an initialized/server pktctrl key certificate storage struct. |
[in] | snikey | Pointer to the target '\0'-terminated snikey. |
[in] | cert_ls | Array of pointers to loaded pktlab certificate structs to be loaded as the server certificate chain. |
[in] | certnum | The number of certificates being loaded. |
[in] | root_ls | Array of pointers to loaded pktlab public key structs to be loaded as the remote party certificate chain trusted roots. |
[in] | rootnum | The number of public keys being loaded. |
int pktctrl_set_server_keycert_default | ( | struct pktctrl_keycert_store * | store, |
const char * | snikey | ||
) |
Set server pktctrl key certificate storage default entry.
[in,out] | store | Pointer to a server pktctrl key certificate storage struct. |
[in] | snikey | Pointer to the target '\0'-terminated snikey. |
ssize_t pktctrl_write | ( | struct pktctrl_obj * | obj, |
const void * | buf, | ||
size_t | nbyte | ||
) |
Write bytes to established pktctrl session.
[in,out] | obj | Pointer to an established pktctrl session object struct. |
[in] | buf | Target write bytes. |
[in] | buflen | Attempted write length (must be > 0). |
int pktctrl_write_message | ( | struct pktctrl_obj * | ctrlobj, |
const struct pktlab_message * | msg | ||
) |
Write pktctrl message to established pktctrl session.
[in,out] | ctrlobj | Pointer to an established pktctrl session object struct. |
[in] | msg | Pointer to a fields-set msg struct. |
size_t pktctrl_write_unsent | ( | const struct pktctrl_obj * | ctrlobj | ) |
Get established pktctrl session internal writer buffered content length.
[in] | ctrlobj | Pointer to an established pktctrl session object struct. |