xref: /openbsd/lib/libtls/tls.h (revision fc61954a)
1 /* $OpenBSD: tls.h,v 1.40 2016/11/04 05:13:13 beck 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	20160904
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 /* RFC 6960 Section 2.3 */
45 #define TLS_OCSP_RESPONSE_SUCCESSFUL		0
46 #define TLS_OCSP_RESPONSE_MALFORMED		1
47 #define TLS_OCSP_RESPONSE_INTERNALERROR		2
48 #define TLS_OCSP_RESPONSE_TRYLATER		3
49 #define TLS_OCSP_RESPONSE_SIGREQUIRED		4
50 #define TLS_OCSP_RESPONSE_UNAUTHORIZED		5
51 
52 /* RFC 6960 Section 2.2 */
53 #define TLS_OCSP_CERT_GOOD			0
54 #define TLS_OCSP_CERT_REVOKED			1
55 #define TLS_OCSP_CERT_UNKNOWN			2
56 
57 /* RFC 5280 Section 5.3.1 */
58 #define TLS_CRL_REASON_UNSPECIFIED		0
59 #define TLS_CRL_REASON_KEY_COMPROMISE		1
60 #define TLS_CRL_REASON_CA_COMPROMISE		2
61 #define TLS_CRL_REASON_AFFILIATION_CHANGED	3
62 #define TLS_CRL_REASON_SUPERSEDED		4
63 #define TLS_CRL_REASON_CESSATION_OF_OPERATION	5
64 #define TLS_CRL_REASON_CERTIFICATE_HOLD		6
65 #define TLS_CRL_REASON_REMOVE_FROM_CRL		8
66 #define TLS_CRL_REASON_PRIVILEGE_WITHDRAWN	9
67 #define TLS_CRL_REASON_AA_COMPROMISE		10
68 
69 struct tls;
70 struct tls_config;
71 
72 typedef ssize_t (*tls_read_cb)(struct tls *_ctx, void *_buf, size_t _buflen,
73     void *_cb_arg);
74 typedef ssize_t (*tls_write_cb)(struct tls *_ctx, const void *_buf,
75     size_t _buflen, void *_cb_arg);
76 
77 int tls_init(void);
78 
79 const char *tls_config_error(struct tls_config *_config);
80 const char *tls_error(struct tls *_ctx);
81 
82 struct tls_config *tls_config_new(void);
83 void tls_config_free(struct tls_config *_config);
84 
85 int tls_config_add_keypair_file(struct tls_config *_config,
86     const char *_cert_file, const char *_key_file);
87 int tls_config_add_keypair_mem(struct tls_config *_config, const uint8_t *_cert,
88     size_t _cert_len, const uint8_t *_key, size_t _key_len);
89 
90 int tls_config_set_alpn(struct tls_config *_config, const char *_alpn);
91 int tls_config_set_ca_file(struct tls_config *_config, const char *_ca_file);
92 int tls_config_set_ca_path(struct tls_config *_config, const char *_ca_path);
93 int tls_config_set_ca_mem(struct tls_config *_config, const uint8_t *_ca,
94     size_t _len);
95 int tls_config_set_cert_file(struct tls_config *_config,
96     const char *_cert_file);
97 int tls_config_set_cert_mem(struct tls_config *_config, const uint8_t *_cert,
98     size_t _len);
99 int tls_config_set_ciphers(struct tls_config *_config, const char *_ciphers);
100 int tls_config_set_dheparams(struct tls_config *_config, const char *_params);
101 int tls_config_set_ecdhecurve(struct tls_config *_config, const char *_name);
102 int tls_config_set_key_file(struct tls_config *_config, const char *_key_file);
103 int tls_config_set_key_mem(struct tls_config *_config, const uint8_t *_key,
104     size_t _len);
105 int tls_config_set_keypair_file(struct tls_config *_config,
106     const char *_cert_file, const char *_key_file);
107 int tls_config_set_keypair_mem(struct tls_config *_config, const uint8_t *_cert,
108     size_t _cert_len, const uint8_t *_key, size_t _key_len);
109 void tls_config_set_protocols(struct tls_config *_config, uint32_t _protocols);
110 void tls_config_set_verify_depth(struct tls_config *_config, int _verify_depth);
111 
112 void tls_config_prefer_ciphers_client(struct tls_config *_config);
113 void tls_config_prefer_ciphers_server(struct tls_config *_config);
114 
115 void tls_config_insecure_noverifycert(struct tls_config *_config);
116 void tls_config_insecure_noverifyname(struct tls_config *_config);
117 void tls_config_insecure_noverifytime(struct tls_config *_config);
118 void tls_config_verify(struct tls_config *_config);
119 
120 void tls_config_ocsp_require_stapling(struct tls_config *_config);
121 void tls_config_verify_client(struct tls_config *_config);
122 void tls_config_verify_client_optional(struct tls_config *_config);
123 
124 void tls_config_clear_keys(struct tls_config *_config);
125 int tls_config_parse_protocols(uint32_t *_protocols, const char *_protostr);
126 
127 struct tls *tls_client(void);
128 struct tls *tls_server(void);
129 int tls_configure(struct tls *_ctx, struct tls_config *_config);
130 void tls_reset(struct tls *_ctx);
131 void tls_free(struct tls *_ctx);
132 
133 int tls_accept_fds(struct tls *_ctx, struct tls **_cctx, int _fd_read,
134     int _fd_write);
135 int tls_accept_socket(struct tls *_ctx, struct tls **_cctx, int _socket);
136 int tls_accept_cbs(struct tls *_ctx, struct tls **_cctx,
137     tls_read_cb _read_cb, tls_write_cb _write_cb, void *_cb_arg);
138 int tls_connect(struct tls *_ctx, const char *_host, const char *_port);
139 int tls_connect_fds(struct tls *_ctx, int _fd_read, int _fd_write,
140     const char *_servername);
141 int tls_connect_servername(struct tls *_ctx, const char *_host,
142     const char *_port, const char *_servername);
143 int tls_connect_socket(struct tls *_ctx, int _s, const char *_servername);
144 int tls_connect_cbs(struct tls *_ctx, tls_read_cb _read_cb,
145     tls_write_cb _write_cb, void *_cb_arg, const char *_servername);
146 int tls_handshake(struct tls *_ctx);
147 ssize_t tls_read(struct tls *_ctx, void *_buf, size_t _buflen);
148 ssize_t tls_write(struct tls *_ctx, const void *_buf, size_t _buflen);
149 int tls_close(struct tls *_ctx);
150 
151 int tls_peer_cert_provided(struct tls *_ctx);
152 int tls_peer_cert_contains_name(struct tls *_ctx, const char *_name);
153 
154 const char *tls_peer_cert_hash(struct tls *_ctx);
155 const char *tls_peer_cert_issuer(struct tls *_ctx);
156 const char *tls_peer_cert_subject(struct tls *_ctx);
157 time_t	tls_peer_cert_notbefore(struct tls *_ctx);
158 time_t	tls_peer_cert_notafter(struct tls *_ctx);
159 
160 const char *tls_conn_alpn_selected(struct tls *_ctx);
161 const char *tls_conn_cipher(struct tls *_ctx);
162 const char *tls_conn_servername(struct tls *_ctx);
163 const char *tls_conn_version(struct tls *_ctx);
164 
165 uint8_t *tls_load_file(const char *_file, size_t *_len, char *_password);
166 
167 int tls_ocsp_process_response(struct tls *_ctx, const unsigned char *_response, size_t _size);
168 int tls_peer_ocsp_cert_status(struct tls *_ctx);
169 int tls_peer_ocsp_crl_reason(struct tls *_ctx);
170 time_t tls_peer_ocsp_next_update(struct tls *_ctx);
171 int tls_peer_ocsp_response_status(struct tls *_ctx);
172 const char *tls_peer_ocsp_result(struct tls *_ctx);
173 time_t tls_peer_ocsp_revocation_time(struct tls *_ctx);
174 time_t tls_peer_ocsp_this_update(struct tls *_ctx);
175 const char *tls_peer_ocsp_url(struct tls *_ctx);
176 
177 #ifdef __cplusplus
178 }
179 #endif
180 
181 #endif /* HEADER_TLS_H */
182