1 /* $OpenBSD: tls.h,v 1.35 2016/08/22 14:58:26 jsing Exp $ */ 2 /* 3 * Copyright (c) 2014 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_H 19 #define HEADER_TLS_H 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif 24 25 #include <sys/types.h> 26 27 #include <stddef.h> 28 #include <stdint.h> 29 30 #define TLS_API 20160822 31 32 #define TLS_PROTOCOL_TLSv1_0 (1 << 1) 33 #define TLS_PROTOCOL_TLSv1_1 (1 << 2) 34 #define TLS_PROTOCOL_TLSv1_2 (1 << 3) 35 #define TLS_PROTOCOL_TLSv1 \ 36 (TLS_PROTOCOL_TLSv1_0|TLS_PROTOCOL_TLSv1_1|TLS_PROTOCOL_TLSv1_2) 37 38 #define TLS_PROTOCOLS_ALL TLS_PROTOCOL_TLSv1 39 #define TLS_PROTOCOLS_DEFAULT TLS_PROTOCOL_TLSv1_2 40 41 #define TLS_WANT_POLLIN -2 42 #define TLS_WANT_POLLOUT -3 43 44 struct tls; 45 struct tls_config; 46 47 int tls_init(void); 48 49 const char *tls_config_error(struct tls_config *_config); 50 const char *tls_error(struct tls *_ctx); 51 52 struct tls_config *tls_config_new(void); 53 void tls_config_free(struct tls_config *_config); 54 55 int tls_config_add_keypair_file(struct tls_config *_config, 56 const char *_cert_file, const char *_key_file); 57 int tls_config_add_keypair_mem(struct tls_config *_config, const uint8_t *_cert, 58 size_t _cert_len, const uint8_t *_key, size_t _key_len); 59 60 int tls_config_set_alpn(struct tls_config *_config, const char *_alpn); 61 int tls_config_set_ca_file(struct tls_config *_config, const char *_ca_file); 62 int tls_config_set_ca_path(struct tls_config *_config, const char *_ca_path); 63 int tls_config_set_ca_mem(struct tls_config *_config, const uint8_t *_ca, 64 size_t _len); 65 int tls_config_set_cert_file(struct tls_config *_config, 66 const char *_cert_file); 67 int tls_config_set_cert_mem(struct tls_config *_config, const uint8_t *_cert, 68 size_t _len); 69 int tls_config_set_ciphers(struct tls_config *_config, const char *_ciphers); 70 int tls_config_set_dheparams(struct tls_config *_config, const char *_params); 71 int tls_config_set_ecdhecurve(struct tls_config *_config, const char *_name); 72 int tls_config_set_key_file(struct tls_config *_config, const char *_key_file); 73 int tls_config_set_key_mem(struct tls_config *_config, const uint8_t *_key, 74 size_t _len); 75 int tls_config_set_keypair_file(struct tls_config *_config, 76 const char *_cert_file, const char *_key_file); 77 int tls_config_set_keypair_mem(struct tls_config *_config, const uint8_t *_cert, 78 size_t _cert_len, const uint8_t *_key, size_t _key_len); 79 void tls_config_set_protocols(struct tls_config *_config, uint32_t _protocols); 80 void tls_config_set_verify_depth(struct tls_config *_config, int _verify_depth); 81 82 void tls_config_prefer_ciphers_client(struct tls_config *_config); 83 void tls_config_prefer_ciphers_server(struct tls_config *_config); 84 85 void tls_config_insecure_noverifycert(struct tls_config *_config); 86 void tls_config_insecure_noverifyname(struct tls_config *_config); 87 void tls_config_insecure_noverifytime(struct tls_config *_config); 88 void tls_config_verify(struct tls_config *_config); 89 90 void tls_config_verify_client(struct tls_config *_config); 91 void tls_config_verify_client_optional(struct tls_config *_config); 92 93 void tls_config_clear_keys(struct tls_config *_config); 94 int tls_config_parse_protocols(uint32_t *_protocols, const char *_protostr); 95 96 struct tls *tls_client(void); 97 struct tls *tls_server(void); 98 int tls_configure(struct tls *_ctx, struct tls_config *_config); 99 void tls_reset(struct tls *_ctx); 100 void tls_free(struct tls *_ctx); 101 102 int tls_accept_fds(struct tls *_ctx, struct tls **_cctx, int _fd_read, 103 int _fd_write); 104 int tls_accept_socket(struct tls *_ctx, struct tls **_cctx, int _socket); 105 int tls_connect(struct tls *_ctx, const char *_host, const char *_port); 106 int tls_connect_fds(struct tls *_ctx, int _fd_read, int _fd_write, 107 const char *_servername); 108 int tls_connect_servername(struct tls *_ctx, const char *_host, 109 const char *_port, const char *_servername); 110 int tls_connect_socket(struct tls *_ctx, int _s, const char *_servername); 111 int tls_handshake(struct tls *_ctx); 112 ssize_t tls_read(struct tls *_ctx, void *_buf, size_t _buflen); 113 ssize_t tls_write(struct tls *_ctx, const void *_buf, size_t _buflen); 114 int tls_close(struct tls *_ctx); 115 116 int tls_peer_cert_provided(struct tls *_ctx); 117 int tls_peer_cert_contains_name(struct tls *_ctx, const char *_name); 118 119 const char *tls_peer_cert_hash(struct tls *_ctx); 120 const char *tls_peer_cert_issuer(struct tls *_ctx); 121 const char *tls_peer_cert_subject(struct tls *_ctx); 122 time_t tls_peer_cert_notbefore(struct tls *_ctx); 123 time_t tls_peer_cert_notafter(struct tls *_ctx); 124 125 const char *tls_conn_alpn_selected(struct tls *_ctx); 126 const char *tls_conn_cipher(struct tls *_ctx); 127 const char *tls_conn_servername(struct tls *_ctx); 128 const char *tls_conn_version(struct tls *_ctx); 129 130 uint8_t *tls_load_file(const char *_file, size_t *_len, char *_password); 131 132 #ifdef __cplusplus 133 } 134 #endif 135 136 #endif /* HEADER_TLS_H */ 137