1 1.3 christos /* $NetBSD: tls.h,v 1.3 2018/02/08 17:45:29 christos 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.3 christos #include <openssl/dh.h> 49 1.1 christos 50 1.1 christos /* initial size for TLS inbuf, minimum prefix + linelength 51 1.1 christos * guaranteed to be accepted */ 52 1.2 minskim #define TLS_MIN_LINELENGTH (2048 + 5) 53 1.1 christos /* usually the inbuf is enlarged as needed and then kept. 54 1.1 christos * if bigger than TLS_PERSIST_LINELENGTH, then shrink 55 1.1 christos * to TLS_LARGE_LINELENGTH immediately */ 56 1.1 christos #define TLS_LARGE_LINELENGTH 8192 57 1.1 christos #define TLS_PERSIST_LINELENGTH 32768 58 1.1 christos 59 1.1 christos /* timeout to call non-blocking TLS operations again */ 60 1.1 christos #define TLS_RETRY_EVENT_USEC 20000 61 1.1 christos 62 1.1 christos /* reconnect to lost server after n sec (initial value) */ 63 1.1 christos #define TLS_RECONNECT_SEC 10 64 1.1 christos /* backoff connection attempts */ 65 1.1 christos #define TLS_RECONNECT_BACKOFF_FACTOR 15/10 66 1.1 christos #define TLS_RECONNECT_BACKOFF(x) (x) = (x) * TLS_RECONNECT_BACKOFF_FACTOR 67 1.1 christos /* abandon connection attempts after n sec 68 1.1 christos * This has to be <= 5h (with 10sec initial interval), 69 1.1 christos * otherwise a daily SIGHUP from newsylog will reset 70 1.1 christos * all timers and the giveup time will never be reached 71 1.2 minskim * 72 1.2 minskim * set here: 2h, reached after ca. 7h of reconnecting 73 1.1 christos */ 74 1.1 christos #define TLS_RECONNECT_GIVEUP 60*60*2 75 1.1 christos 76 1.1 christos /* default algorithm for certificate fingerprints */ 77 1.1 christos #define DEFAULT_FINGERPRINT_ALG "sha-1" 78 1.1 christos 79 1.1 christos /* default X.509 files */ 80 1.1 christos #define DEFAULT_X509_CERTFILE "/etc/openssl/default.crt" 81 1.1 christos #define DEFAULT_X509_KEYFILE "/etc/openssl/default.key" 82 1.1 christos 83 1.1 christos /* options for peer certificate verification */ 84 1.1 christos #define X509VERIFY_ALWAYS 0 85 1.1 christos #define X509VERIFY_IFPRESENT 1 86 1.1 christos #define X509VERIFY_NONE 2 87 1.1 christos 88 1.1 christos /* attributes for self-generated keys/certificates */ 89 1.1 christos #define TLS_GENCERT_BITS 1024 90 1.1 christos #define TLS_GENCERT_SERIAL 1 91 1.1 christos #define TLS_GENCERT_DAYS 5*365 92 1.1 christos 93 1.1 christos /* TLS connection states */ 94 1.1 christos #define ST_NONE 0 95 1.1 christos #define ST_TLS_EST 1 96 1.1 christos #define ST_TCP_EST 2 97 1.1 christos #define ST_CONNECTING 3 98 1.2 minskim #define ST_ACCEPTING 4 99 1.1 christos #define ST_READING 5 100 1.1 christos #define ST_WRITING 6 101 1.1 christos #define ST_EOF 7 102 1.1 christos #define ST_CLOSING0 8 103 1.1 christos #define ST_CLOSING1 9 104 1.1 christos #define ST_CLOSING2 10 105 1.1 christos 106 1.1 christos /* backlog for listen */ 107 1.1 christos #define TLSBACKLOG 4 108 1.1 christos /* close TLS connection after multiple 'soft' errors */ 109 1.1 christos #define TLS_MAXERRORCOUNT 4 110 1.1 christos 111 1.1 christos /* 112 1.1 christos * holds TLS related settings for one connection to be 113 1.1 christos * included in the SSL object and available in callbacks 114 1.2 minskim * 115 1.1 christos * Many fields have a slightly different semantic for 116 1.1 christos * incoming and outgoing connections: 117 1.1 christos * - for outgoing connections it contains the values from syslog.conf and 118 1.1 christos * the server's cert is checked against these values by check_peer_cert() 119 1.1 christos * - for incoming connections it is not used for checking, instead 120 1.1 christos * dispatch_tls_accept() fills in the connected hostname/port and 121 1.1 christos * check_peer_cert() fills in subject and fingerprint from the peer cert 122 1.1 christos */ 123 1.1 christos struct tls_conn_settings { 124 1.1 christos unsigned send_queue:1, /* currently sending buffer */ 125 1.1 christos errorcount:4, /* counter [0;TLS_MAXERRORCOUNT] */ 126 1.1 christos accepted:1, /* workaround cf. check_peer_cert*/ 127 1.2 minskim shutdown:1, /* fast connection close on exit */ 128 1.1 christos x509verify:2, /* kind of validation needed */ 129 1.1 christos incoming:1, /* set if we are server */ 130 1.1 christos state:4; /* outgoing connection state */ 131 1.1 christos struct event *event; /* event for read/write activity */ 132 1.1 christos struct event *retryevent; /* event for retries */ 133 1.1 christos SSL *sslptr; /* active SSL object */ 134 1.1 christos char *hostname; /* hostname or IP we connect to */ 135 1.1 christos char *port; /* service name or port number */ 136 1.1 christos char *subject; /* configured hostname in cert */ 137 1.1 christos char *fingerprint; /* fingerprint of peer cert */ 138 1.1 christos char *certfile; /* filename of peer cert */ 139 1.1 christos unsigned reconnect; /* seconds between reconnects */ 140 1.1 christos }; 141 1.1 christos 142 1.1 christos /* argument struct only used for tls_send() */ 143 1.1 christos struct tls_send_msg { 144 1.1 christos struct filed *f; 145 1.1 christos struct buf_queue *qentry; 146 1.1 christos char *line; /* formatted message */ 147 1.1 christos size_t linelen; 148 1.1 christos size_t offset; /* in case of partial writes */ 149 1.1 christos }; 150 1.1 christos 151 1.1 christos /* return values for TLS_examine_error() */ 152 1.1 christos #define TLS_OK 0 /* no real problem, just ignore */ 153 1.1 christos #define TLS_RETRY_READ 1 /* just retry, non-blocking operation not finished yet */ 154 1.1 christos #define TLS_RETRY_WRITE 2 /* just retry, non-blocking operation not finished yet */ 155 1.1 christos #define TLS_TEMP_ERROR 4 /* recoverable error condition, but try again */ 156 1.1 christos #define TLS_PERM_ERROR 8 /* non-recoverable error condition, closed TLS and socket */ 157 1.1 christos 158 1.1 christos /* global TLS setup and utility */ 159 1.1 christos char *init_global_TLS_CTX(void); 160 1.1 christos struct socketEvent *socksetup_tls(const int, const char *, const char *); 161 1.1 christos int check_peer_cert(int, X509_STORE_CTX *); 162 1.1 christos int accept_cert(const char* , struct tls_conn_settings *, char *, char *); 163 1.1 christos int deny_cert(struct tls_conn_settings *, char *, char *); 164 1.1 christos bool read_certfile(X509 **, const char *); 165 1.1 christos bool write_x509files(EVP_PKEY *, X509 *, const char *, const char *); 166 1.1 christos bool mk_x509_cert(X509 **, EVP_PKEY **, int, int, int); 167 1.1 christos bool x509_cert_add_subjectAltName(X509 *, X509V3_CTX *); 168 1.1 christos int tls_examine_error(const char *, const SSL *, struct tls_conn_settings *, const int); 169 1.1 christos 170 1.1 christos bool get_fingerprint(const X509 *, char **, const char *); 171 1.1 christos bool get_commonname(X509 *, char **); 172 1.1 christos bool match_hostnames(X509 *, const char *, const char *); 173 1.1 christos bool match_fingerprint(const X509 *, const char *); 174 1.1 christos bool match_certfile(const X509 *, const char *); 175 1.1 christos 176 1.1 christos /* configuration & parsing */ 177 1.1 christos bool parse_tls_destination(const char *, struct filed *, size_t); 178 1.1 christos /* event callbacks */ 179 1.1 christos void dispatch_socket_accept(int, short, void *); 180 1.1 christos void dispatch_tls_accept(int, short, void *); 181 1.1 christos void dispatch_tls_read(int, short, void *); 182 1.1 christos void dispatch_tls_send(int, short, void *); 183 1.1 christos void dispatch_tls_eof(int, short, void *); 184 1.1 christos void dispatch_SSL_connect(int, short, void *); 185 1.1 christos void dispatch_SSL_shutdown(int, short, void *); 186 1.1 christos void dispatch_force_tls_reconnect(int, short, void *); 187 1.1 christos 188 1.1 christos bool tls_connect(struct tls_conn_settings *); 189 1.1 christos void tls_reconnect(int, short, void *); 190 1.1 christos bool tls_send(struct filed *, char *, size_t, struct buf_queue*); 191 1.1 christos void tls_split_messages(struct TLS_Incoming_Conn *); 192 1.1 christos 193 1.1 christos void free_tls_conn(struct tls_conn_settings *); 194 1.1 christos void free_tls_sslptr(struct tls_conn_settings *); 195 1.1 christos void free_tls_send_msg(struct tls_send_msg *); 196 1.1 christos 197 1.1 christos #endif /* !_TLS_H */ 198