tls.h revision 1.2 1 1.2 minskim /* $NetBSD: tls.h,v 1.2 2008/11/07 07:36:38 minskim Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1 christos * by Martin Schtte.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos * 3. All advertising materials mentioning features or use of this software
19 1.1 christos * must display the following acknowledgement:
20 1.1 christos * This product includes software developed by the NetBSD
21 1.1 christos * Foundation, Inc. and its contributors.
22 1.1 christos * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 christos * contributors may be used to endorse or promote products derived
24 1.1 christos * from this software without specific prior written permission.
25 1.1 christos *
26 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
37 1.1 christos */
38 1.1 christos /*
39 1.1 christos * tls.h
40 1.2 minskim *
41 1.1 christos */
42 1.1 christos #ifndef _TLS_H
43 1.1 christos #define _TLS_H
44 1.1 christos #include <openssl/x509v3.h>
45 1.1 christos #include <openssl/err.h>
46 1.1 christos #include <openssl/rand.h>
47 1.1 christos #include <openssl/pem.h>
48 1.1 christos
49 1.1 christos /* initial size for TLS inbuf, minimum prefix + linelength
50 1.1 christos * guaranteed to be accepted */
51 1.2 minskim #define TLS_MIN_LINELENGTH (2048 + 5)
52 1.1 christos /* usually the inbuf is enlarged as needed and then kept.
53 1.1 christos * if bigger than TLS_PERSIST_LINELENGTH, then shrink
54 1.1 christos * to TLS_LARGE_LINELENGTH immediately */
55 1.1 christos #define TLS_LARGE_LINELENGTH 8192
56 1.1 christos #define TLS_PERSIST_LINELENGTH 32768
57 1.1 christos
58 1.1 christos /* timeout to call non-blocking TLS operations again */
59 1.1 christos #define TLS_RETRY_EVENT_USEC 20000
60 1.1 christos
61 1.1 christos /* reconnect to lost server after n sec (initial value) */
62 1.1 christos #define TLS_RECONNECT_SEC 10
63 1.1 christos /* backoff connection attempts */
64 1.1 christos #define TLS_RECONNECT_BACKOFF_FACTOR 15/10
65 1.1 christos #define TLS_RECONNECT_BACKOFF(x) (x) = (x) * TLS_RECONNECT_BACKOFF_FACTOR
66 1.1 christos /* abandon connection attempts after n sec
67 1.1 christos * This has to be <= 5h (with 10sec initial interval),
68 1.1 christos * otherwise a daily SIGHUP from newsylog will reset
69 1.1 christos * all timers and the giveup time will never be reached
70 1.2 minskim *
71 1.2 minskim * set here: 2h, reached after ca. 7h of reconnecting
72 1.1 christos */
73 1.1 christos #define TLS_RECONNECT_GIVEUP 60*60*2
74 1.1 christos
75 1.1 christos /* default algorithm for certificate fingerprints */
76 1.1 christos #define DEFAULT_FINGERPRINT_ALG "sha-1"
77 1.1 christos
78 1.1 christos /* default X.509 files */
79 1.1 christos #define DEFAULT_X509_CERTFILE "/etc/openssl/default.crt"
80 1.1 christos #define DEFAULT_X509_KEYFILE "/etc/openssl/default.key"
81 1.1 christos
82 1.1 christos /* options for peer certificate verification */
83 1.1 christos #define X509VERIFY_ALWAYS 0
84 1.1 christos #define X509VERIFY_IFPRESENT 1
85 1.1 christos #define X509VERIFY_NONE 2
86 1.1 christos
87 1.1 christos /* attributes for self-generated keys/certificates */
88 1.1 christos #define TLS_GENCERT_BITS 1024
89 1.1 christos #define TLS_GENCERT_SERIAL 1
90 1.1 christos #define TLS_GENCERT_DAYS 5*365
91 1.1 christos
92 1.1 christos /* TLS connection states */
93 1.1 christos #define ST_NONE 0
94 1.1 christos #define ST_TLS_EST 1
95 1.1 christos #define ST_TCP_EST 2
96 1.1 christos #define ST_CONNECTING 3
97 1.2 minskim #define ST_ACCEPTING 4
98 1.1 christos #define ST_READING 5
99 1.1 christos #define ST_WRITING 6
100 1.1 christos #define ST_EOF 7
101 1.1 christos #define ST_CLOSING0 8
102 1.1 christos #define ST_CLOSING1 9
103 1.1 christos #define ST_CLOSING2 10
104 1.1 christos
105 1.1 christos /* backlog for listen */
106 1.1 christos #define TLSBACKLOG 4
107 1.1 christos /* close TLS connection after multiple 'soft' errors */
108 1.1 christos #define TLS_MAXERRORCOUNT 4
109 1.1 christos
110 1.1 christos /*
111 1.1 christos * holds TLS related settings for one connection to be
112 1.1 christos * included in the SSL object and available in callbacks
113 1.2 minskim *
114 1.1 christos * Many fields have a slightly different semantic for
115 1.1 christos * incoming and outgoing connections:
116 1.1 christos * - for outgoing connections it contains the values from syslog.conf and
117 1.1 christos * the server's cert is checked against these values by check_peer_cert()
118 1.1 christos * - for incoming connections it is not used for checking, instead
119 1.1 christos * dispatch_tls_accept() fills in the connected hostname/port and
120 1.1 christos * check_peer_cert() fills in subject and fingerprint from the peer cert
121 1.1 christos */
122 1.1 christos struct tls_conn_settings {
123 1.1 christos unsigned send_queue:1, /* currently sending buffer */
124 1.1 christos errorcount:4, /* counter [0;TLS_MAXERRORCOUNT] */
125 1.1 christos accepted:1, /* workaround cf. check_peer_cert*/
126 1.2 minskim shutdown:1, /* fast connection close on exit */
127 1.1 christos x509verify:2, /* kind of validation needed */
128 1.1 christos incoming:1, /* set if we are server */
129 1.1 christos state:4; /* outgoing connection state */
130 1.1 christos struct event *event; /* event for read/write activity */
131 1.1 christos struct event *retryevent; /* event for retries */
132 1.1 christos SSL *sslptr; /* active SSL object */
133 1.1 christos char *hostname; /* hostname or IP we connect to */
134 1.1 christos char *port; /* service name or port number */
135 1.1 christos char *subject; /* configured hostname in cert */
136 1.1 christos char *fingerprint; /* fingerprint of peer cert */
137 1.1 christos char *certfile; /* filename of peer cert */
138 1.1 christos unsigned reconnect; /* seconds between reconnects */
139 1.1 christos };
140 1.1 christos
141 1.1 christos /* argument struct only used for tls_send() */
142 1.1 christos struct tls_send_msg {
143 1.1 christos struct filed *f;
144 1.1 christos struct buf_queue *qentry;
145 1.1 christos char *line; /* formatted message */
146 1.1 christos size_t linelen;
147 1.1 christos size_t offset; /* in case of partial writes */
148 1.1 christos };
149 1.1 christos
150 1.1 christos /* return values for TLS_examine_error() */
151 1.1 christos #define TLS_OK 0 /* no real problem, just ignore */
152 1.1 christos #define TLS_RETRY_READ 1 /* just retry, non-blocking operation not finished yet */
153 1.1 christos #define TLS_RETRY_WRITE 2 /* just retry, non-blocking operation not finished yet */
154 1.1 christos #define TLS_TEMP_ERROR 4 /* recoverable error condition, but try again */
155 1.1 christos #define TLS_PERM_ERROR 8 /* non-recoverable error condition, closed TLS and socket */
156 1.1 christos
157 1.1 christos /* global TLS setup and utility */
158 1.1 christos char *init_global_TLS_CTX(void);
159 1.1 christos struct socketEvent *socksetup_tls(const int, const char *, const char *);
160 1.1 christos int check_peer_cert(int, X509_STORE_CTX *);
161 1.1 christos int accept_cert(const char* , struct tls_conn_settings *, char *, char *);
162 1.1 christos int deny_cert(struct tls_conn_settings *, char *, char *);
163 1.1 christos bool read_certfile(X509 **, const char *);
164 1.1 christos bool write_x509files(EVP_PKEY *, X509 *, const char *, const char *);
165 1.1 christos bool mk_x509_cert(X509 **, EVP_PKEY **, int, int, int);
166 1.1 christos bool x509_cert_add_subjectAltName(X509 *, X509V3_CTX *);
167 1.1 christos int tls_examine_error(const char *, const SSL *, struct tls_conn_settings *, const int);
168 1.1 christos
169 1.1 christos bool get_fingerprint(const X509 *, char **, const char *);
170 1.1 christos bool get_commonname(X509 *, char **);
171 1.1 christos bool match_hostnames(X509 *, const char *, const char *);
172 1.1 christos bool match_fingerprint(const X509 *, const char *);
173 1.1 christos bool match_certfile(const X509 *, const char *);
174 1.1 christos
175 1.1 christos /* configuration & parsing */
176 1.1 christos bool parse_tls_destination(const char *, struct filed *, size_t);
177 1.1 christos /* event callbacks */
178 1.1 christos void dispatch_socket_accept(int, short, void *);
179 1.1 christos void dispatch_tls_accept(int, short, void *);
180 1.1 christos void dispatch_tls_read(int, short, void *);
181 1.1 christos void dispatch_tls_send(int, short, void *);
182 1.1 christos void dispatch_tls_eof(int, short, void *);
183 1.1 christos void dispatch_SSL_connect(int, short, void *);
184 1.1 christos void dispatch_SSL_shutdown(int, short, void *);
185 1.1 christos void dispatch_force_tls_reconnect(int, short, void *);
186 1.1 christos
187 1.1 christos bool tls_connect(struct tls_conn_settings *);
188 1.1 christos void tls_reconnect(int, short, void *);
189 1.1 christos bool tls_send(struct filed *, char *, size_t, struct buf_queue*);
190 1.1 christos void tls_split_messages(struct TLS_Incoming_Conn *);
191 1.1 christos
192 1.1 christos void free_tls_conn(struct tls_conn_settings *);
193 1.1 christos void free_tls_sslptr(struct tls_conn_settings *);
194 1.1 christos void free_tls_send_msg(struct tls_send_msg *);
195 1.1 christos
196 1.1 christos #endif /* !_TLS_H */
197