diff options
| author | jsing <> | 2015-08-27 15:26:50 +0000 |
|---|---|---|
| committer | jsing <> | 2015-08-27 15:26:50 +0000 |
| commit | d621690a38473c22a65b4be34c24680f5148cd42 (patch) | |
| tree | d2c2a8c6fc1b8da3c4117997a97ab03f0ac74f21 | |
| parent | ec56fcd75da47203f2a92e4a7ac2df5ec3da32be (diff) | |
| download | openbsd-d621690a38473c22a65b4be34c24680f5148cd42.tar.gz openbsd-d621690a38473c22a65b4be34c24680f5148cd42.tar.bz2 openbsd-d621690a38473c22a65b4be34c24680f5148cd42.zip | |
Improve libtls error messages.
The tls_set_error() function previously stored the errno but did nothing
with it. Change tls_set_error() to append the strerror(3) of the stored
errno so that we include useful information regarding failures.
Provide a tls_set_errorx() function that does not store the errno or
include strerror(3) in the error message. Call this function instead of
tls_set_error() for errors where the errno value has no useful meaning.
With feedback from and ok doug@
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libtls/tls.c | 87 | ||||
| -rw-r--r-- | src/lib/libtls/tls_client.c | 34 | ||||
| -rw-r--r-- | src/lib/libtls/tls_internal.h | 9 | ||||
| -rw-r--r-- | src/lib/libtls/tls_server.c | 18 | ||||
| -rw-r--r-- | src/lib/libtls/tls_verify.c | 8 |
5 files changed, 101 insertions, 55 deletions
diff --git a/src/lib/libtls/tls.c b/src/lib/libtls/tls.c index c79191ee15..445933d176 100644 --- a/src/lib/libtls/tls.c +++ b/src/lib/libtls/tls.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: tls.c,v 1.14 2015/08/27 14:34:46 jsing Exp $ */ | 1 | /* $OpenBSD: tls.c,v 1.15 2015/08/27 15:26:49 jsing Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> |
| 4 | * | 4 | * |
| @@ -58,18 +58,61 @@ tls_error(struct tls *ctx) | |||
| 58 | return ctx->errmsg; | 58 | return ctx->errmsg; |
| 59 | } | 59 | } |
| 60 | 60 | ||
| 61 | static int | ||
| 62 | tls_set_verror(struct tls *ctx, int errnum, const char *fmt, va_list ap) | ||
| 63 | { | ||
| 64 | char *errmsg = NULL; | ||
| 65 | int rv = -1; | ||
| 66 | |||
| 67 | free(ctx->errmsg); | ||
| 68 | ctx->errmsg = NULL; | ||
| 69 | |||
| 70 | if (vasprintf(&errmsg, fmt, ap) == -1) { | ||
| 71 | errmsg = NULL; | ||
| 72 | goto err; | ||
| 73 | } | ||
| 74 | |||
| 75 | if (errnum == -1) { | ||
| 76 | ctx->errmsg = errmsg; | ||
| 77 | return (0); | ||
| 78 | } | ||
| 79 | |||
| 80 | if (asprintf(&ctx->errmsg, "%s: %s", errmsg, strerror(errnum)) == -1) { | ||
| 81 | ctx->errmsg = NULL; | ||
| 82 | goto err; | ||
| 83 | } | ||
| 84 | |||
| 85 | rv = 0; | ||
| 86 | |||
| 87 | err: | ||
| 88 | free(errmsg); | ||
| 89 | |||
| 90 | return (rv); | ||
| 91 | } | ||
| 92 | |||
| 61 | int | 93 | int |
| 62 | tls_set_error(struct tls *ctx, char *fmt, ...) | 94 | tls_set_error(struct tls *ctx, const char *fmt, ...) |
| 63 | { | 95 | { |
| 64 | va_list ap; | 96 | va_list ap; |
| 65 | int rv; | 97 | int rv; |
| 66 | 98 | ||
| 67 | ctx->err = errno; | 99 | ctx->errnum = errno; |
| 68 | free(ctx->errmsg); | 100 | |
| 69 | ctx->errmsg = NULL; | 101 | va_start(ap, fmt); |
| 102 | rv = tls_set_verror(ctx, ctx->errnum, fmt, ap); | ||
| 103 | va_end(ap); | ||
| 104 | |||
| 105 | return (rv); | ||
| 106 | } | ||
| 107 | |||
| 108 | int | ||
| 109 | tls_set_errorx(struct tls *ctx, const char *fmt, ...) | ||
| 110 | { | ||
| 111 | va_list ap; | ||
| 112 | int rv; | ||
| 70 | 113 | ||
| 71 | va_start(ap, fmt); | 114 | va_start(ap, fmt); |
| 72 | rv = vasprintf(&ctx->errmsg, fmt, ap); | 115 | rv = tls_set_verror(ctx, -1, fmt, ap); |
| 73 | va_end(ap); | 116 | va_end(ap); |
| 74 | 117 | ||
| 75 | return (rv); | 118 | return (rv); |
| @@ -113,35 +156,35 @@ tls_configure_keypair(struct tls *ctx) | |||
| 113 | 156 | ||
| 114 | if (ctx->config->cert_mem != NULL) { | 157 | if (ctx->config->cert_mem != NULL) { |
| 115 | if (ctx->config->cert_len > INT_MAX) { | 158 | if (ctx->config->cert_len > INT_MAX) { |
| 116 | tls_set_error(ctx, "certificate too long"); | 159 | tls_set_errorx(ctx, "certificate too long"); |
| 117 | goto err; | 160 | goto err; |
| 118 | } | 161 | } |
| 119 | 162 | ||
| 120 | if (SSL_CTX_use_certificate_chain_mem(ctx->ssl_ctx, | 163 | if (SSL_CTX_use_certificate_chain_mem(ctx->ssl_ctx, |
| 121 | ctx->config->cert_mem, ctx->config->cert_len) != 1) { | 164 | ctx->config->cert_mem, ctx->config->cert_len) != 1) { |
| 122 | tls_set_error(ctx, "failed to load certificate"); | 165 | tls_set_errorx(ctx, "failed to load certificate"); |
| 123 | goto err; | 166 | goto err; |
| 124 | } | 167 | } |
| 125 | cert = NULL; | 168 | cert = NULL; |
| 126 | } | 169 | } |
| 127 | if (ctx->config->key_mem != NULL) { | 170 | if (ctx->config->key_mem != NULL) { |
| 128 | if (ctx->config->key_len > INT_MAX) { | 171 | if (ctx->config->key_len > INT_MAX) { |
| 129 | tls_set_error(ctx, "key too long"); | 172 | tls_set_errorx(ctx, "key too long"); |
| 130 | goto err; | 173 | goto err; |
| 131 | } | 174 | } |
| 132 | 175 | ||
| 133 | if ((bio = BIO_new_mem_buf(ctx->config->key_mem, | 176 | if ((bio = BIO_new_mem_buf(ctx->config->key_mem, |
| 134 | ctx->config->key_len)) == NULL) { | 177 | ctx->config->key_len)) == NULL) { |
| 135 | tls_set_error(ctx, "failed to create buffer"); | 178 | tls_set_errorx(ctx, "failed to create buffer"); |
| 136 | goto err; | 179 | goto err; |
| 137 | } | 180 | } |
| 138 | if ((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, | 181 | if ((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, |
| 139 | NULL)) == NULL) { | 182 | NULL)) == NULL) { |
| 140 | tls_set_error(ctx, "failed to read private key"); | 183 | tls_set_errorx(ctx, "failed to read private key"); |
| 141 | goto err; | 184 | goto err; |
| 142 | } | 185 | } |
| 143 | if (SSL_CTX_use_PrivateKey(ctx->ssl_ctx, pkey) != 1) { | 186 | if (SSL_CTX_use_PrivateKey(ctx->ssl_ctx, pkey) != 1) { |
| 144 | tls_set_error(ctx, "failed to load private key"); | 187 | tls_set_errorx(ctx, "failed to load private key"); |
| 145 | goto err; | 188 | goto err; |
| 146 | } | 189 | } |
| 147 | BIO_free(bio); | 190 | BIO_free(bio); |
| @@ -153,20 +196,20 @@ tls_configure_keypair(struct tls *ctx) | |||
| 153 | if (ctx->config->cert_file != NULL) { | 196 | if (ctx->config->cert_file != NULL) { |
| 154 | if (SSL_CTX_use_certificate_chain_file(ctx->ssl_ctx, | 197 | if (SSL_CTX_use_certificate_chain_file(ctx->ssl_ctx, |
| 155 | ctx->config->cert_file) != 1) { | 198 | ctx->config->cert_file) != 1) { |
| 156 | tls_set_error(ctx, "failed to load certificate file"); | 199 | tls_set_errorx(ctx, "failed to load certificate file"); |
| 157 | goto err; | 200 | goto err; |
| 158 | } | 201 | } |
| 159 | } | 202 | } |
| 160 | if (ctx->config->key_file != NULL) { | 203 | if (ctx->config->key_file != NULL) { |
| 161 | if (SSL_CTX_use_PrivateKey_file(ctx->ssl_ctx, | 204 | if (SSL_CTX_use_PrivateKey_file(ctx->ssl_ctx, |
| 162 | ctx->config->key_file, SSL_FILETYPE_PEM) != 1) { | 205 | ctx->config->key_file, SSL_FILETYPE_PEM) != 1) { |
| 163 | tls_set_error(ctx, "failed to load private key file"); | 206 | tls_set_errorx(ctx, "failed to load private key file"); |
| 164 | goto err; | 207 | goto err; |
| 165 | } | 208 | } |
| 166 | } | 209 | } |
| 167 | 210 | ||
| 168 | if (SSL_CTX_check_private_key(ctx->ssl_ctx) != 1) { | 211 | if (SSL_CTX_check_private_key(ctx->ssl_ctx) != 1) { |
| 169 | tls_set_error(ctx, "private/public key mismatch"); | 212 | tls_set_errorx(ctx, "private/public key mismatch"); |
| 170 | goto err; | 213 | goto err; |
| 171 | } | 214 | } |
| 172 | 215 | ||
| @@ -203,7 +246,7 @@ tls_configure_ssl(struct tls *ctx) | |||
| 203 | if (ctx->config->ciphers != NULL) { | 246 | if (ctx->config->ciphers != NULL) { |
| 204 | if (SSL_CTX_set_cipher_list(ctx->ssl_ctx, | 247 | if (SSL_CTX_set_cipher_list(ctx->ssl_ctx, |
| 205 | ctx->config->ciphers) != 1) { | 248 | ctx->config->ciphers) != 1) { |
| 206 | tls_set_error(ctx, "failed to set ciphers"); | 249 | tls_set_errorx(ctx, "failed to set ciphers"); |
| 207 | goto err; | 250 | goto err; |
| 208 | } | 251 | } |
| 209 | } | 252 | } |
| @@ -235,9 +278,9 @@ tls_reset(struct tls *ctx) | |||
| 235 | ctx->socket = -1; | 278 | ctx->socket = -1; |
| 236 | ctx->state = 0; | 279 | ctx->state = 0; |
| 237 | 280 | ||
| 238 | ctx->err = 0; | ||
| 239 | free(ctx->errmsg); | 281 | free(ctx->errmsg); |
| 240 | ctx->errmsg = NULL; | 282 | ctx->errmsg = NULL; |
| 283 | ctx->errnum = 0; | ||
| 241 | } | 284 | } |
| 242 | 285 | ||
| 243 | int | 286 | int |
| @@ -267,21 +310,21 @@ tls_ssl_error(struct tls *ctx, SSL *ssl_conn, int ssl_ret, const char *prefix) | |||
| 267 | } else if (ssl_ret == -1) { | 310 | } else if (ssl_ret == -1) { |
| 268 | errstr = strerror(errno); | 311 | errstr = strerror(errno); |
| 269 | } | 312 | } |
| 270 | tls_set_error(ctx, "%s failed: %s", prefix, errstr); | 313 | tls_set_errorx(ctx, "%s failed: %s", prefix, errstr); |
| 271 | return (-1); | 314 | return (-1); |
| 272 | 315 | ||
| 273 | case SSL_ERROR_SSL: | 316 | case SSL_ERROR_SSL: |
| 274 | if ((err = ERR_peek_error()) != 0) { | 317 | if ((err = ERR_peek_error()) != 0) { |
| 275 | errstr = ERR_error_string(err, NULL); | 318 | errstr = ERR_error_string(err, NULL); |
| 276 | } | 319 | } |
| 277 | tls_set_error(ctx, "%s failed: %s", prefix, errstr); | 320 | tls_set_errorx(ctx, "%s failed: %s", prefix, errstr); |
| 278 | return (-1); | 321 | return (-1); |
| 279 | 322 | ||
| 280 | case SSL_ERROR_WANT_CONNECT: | 323 | case SSL_ERROR_WANT_CONNECT: |
| 281 | case SSL_ERROR_WANT_ACCEPT: | 324 | case SSL_ERROR_WANT_ACCEPT: |
| 282 | case SSL_ERROR_WANT_X509_LOOKUP: | 325 | case SSL_ERROR_WANT_X509_LOOKUP: |
| 283 | default: | 326 | default: |
| 284 | tls_set_error(ctx, "%s failed (%i)", prefix, ssl_err); | 327 | tls_set_errorx(ctx, "%s failed (%i)", prefix, ssl_err); |
| 285 | return (-1); | 328 | return (-1); |
| 286 | } | 329 | } |
| 287 | } | 330 | } |
| @@ -294,7 +337,7 @@ tls_read(struct tls *ctx, void *buf, size_t buflen, size_t *outlen) | |||
| 294 | *outlen = 0; | 337 | *outlen = 0; |
| 295 | 338 | ||
| 296 | if (buflen > INT_MAX) { | 339 | if (buflen > INT_MAX) { |
| 297 | tls_set_error(ctx, "buflen too long"); | 340 | tls_set_errorx(ctx, "buflen too long"); |
| 298 | return (-1); | 341 | return (-1); |
| 299 | } | 342 | } |
| 300 | 343 | ||
| @@ -315,7 +358,7 @@ tls_write(struct tls *ctx, const void *buf, size_t buflen, size_t *outlen) | |||
| 315 | *outlen = 0; | 358 | *outlen = 0; |
| 316 | 359 | ||
| 317 | if (buflen > INT_MAX) { | 360 | if (buflen > INT_MAX) { |
| 318 | tls_set_error(ctx, "buflen too long"); | 361 | tls_set_errorx(ctx, "buflen too long"); |
| 319 | return (-1); | 362 | return (-1); |
| 320 | } | 363 | } |
| 321 | 364 | ||
diff --git a/src/lib/libtls/tls_client.c b/src/lib/libtls/tls_client.c index 241c506676..168a7089fc 100644 --- a/src/lib/libtls/tls_client.c +++ b/src/lib/libtls/tls_client.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: tls_client.c,v 1.20 2015/08/27 14:34:46 jsing Exp $ */ | 1 | /* $OpenBSD: tls_client.c,v 1.21 2015/08/27 15:26:50 jsing Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> |
| 4 | * | 4 | * |
| @@ -95,12 +95,12 @@ tls_connect_servername(struct tls *ctx, const char *host, const char *port, | |||
| 95 | int rv = -1, s = -1, ret; | 95 | int rv = -1, s = -1, ret; |
| 96 | 96 | ||
| 97 | if ((ctx->flags & TLS_CLIENT) == 0) { | 97 | if ((ctx->flags & TLS_CLIENT) == 0) { |
| 98 | tls_set_error(ctx, "not a client context"); | 98 | tls_set_errorx(ctx, "not a client context"); |
| 99 | goto err; | 99 | goto err; |
| 100 | } | 100 | } |
| 101 | 101 | ||
| 102 | if (host == NULL) { | 102 | if (host == NULL) { |
| 103 | tls_set_error(ctx, "host not specified"); | 103 | tls_set_errorx(ctx, "host not specified"); |
| 104 | goto err; | 104 | goto err; |
| 105 | } | 105 | } |
| 106 | 106 | ||
| @@ -111,7 +111,7 @@ tls_connect_servername(struct tls *ctx, const char *host, const char *port, | |||
| 111 | if ((p = (char *)port) == NULL) { | 111 | if ((p = (char *)port) == NULL) { |
| 112 | ret = tls_host_port(host, &hs, &ps); | 112 | ret = tls_host_port(host, &hs, &ps); |
| 113 | if (ret == -1) { | 113 | if (ret == -1) { |
| 114 | tls_set_error(ctx, "memory allocation failure"); | 114 | tls_set_errorx(ctx, "memory allocation failure"); |
| 115 | goto err; | 115 | goto err; |
| 116 | } | 116 | } |
| 117 | if (ret != 0) | 117 | if (ret != 0) |
| @@ -169,7 +169,7 @@ tls_connect_fds(struct tls *ctx, int fd_read, int fd_write, | |||
| 169 | int ret, err; | 169 | int ret, err; |
| 170 | 170 | ||
| 171 | if ((ctx->flags & TLS_CLIENT) == 0) { | 171 | if ((ctx->flags & TLS_CLIENT) == 0) { |
| 172 | tls_set_error(ctx, "not a client context"); | 172 | tls_set_errorx(ctx, "not a client context"); |
| 173 | goto err; | 173 | goto err; |
| 174 | } | 174 | } |
| 175 | 175 | ||
| @@ -177,12 +177,12 @@ tls_connect_fds(struct tls *ctx, int fd_read, int fd_write, | |||
| 177 | goto connecting; | 177 | goto connecting; |
| 178 | 178 | ||
| 179 | if (fd_read < 0 || fd_write < 0) { | 179 | if (fd_read < 0 || fd_write < 0) { |
| 180 | tls_set_error(ctx, "invalid file descriptors"); | 180 | tls_set_errorx(ctx, "invalid file descriptors"); |
| 181 | return (-1); | 181 | return (-1); |
| 182 | } | 182 | } |
| 183 | 183 | ||
| 184 | if ((ctx->ssl_ctx = SSL_CTX_new(SSLv23_client_method())) == NULL) { | 184 | if ((ctx->ssl_ctx = SSL_CTX_new(SSLv23_client_method())) == NULL) { |
| 185 | tls_set_error(ctx, "ssl context failure"); | 185 | tls_set_errorx(ctx, "ssl context failure"); |
| 186 | goto err; | 186 | goto err; |
| 187 | } | 187 | } |
| 188 | 188 | ||
| @@ -191,7 +191,7 @@ tls_connect_fds(struct tls *ctx, int fd_read, int fd_write, | |||
| 191 | 191 | ||
| 192 | if (ctx->config->verify_name) { | 192 | if (ctx->config->verify_name) { |
| 193 | if (servername == NULL) { | 193 | if (servername == NULL) { |
| 194 | tls_set_error(ctx, "server name not specified"); | 194 | tls_set_errorx(ctx, "server name not specified"); |
| 195 | goto err; | 195 | goto err; |
| 196 | } | 196 | } |
| 197 | } | 197 | } |
| @@ -201,19 +201,19 @@ tls_connect_fds(struct tls *ctx, int fd_read, int fd_write, | |||
| 201 | 201 | ||
| 202 | if (ctx->config->ca_mem != NULL) { | 202 | if (ctx->config->ca_mem != NULL) { |
| 203 | if (ctx->config->ca_len > INT_MAX) { | 203 | if (ctx->config->ca_len > INT_MAX) { |
| 204 | tls_set_error(ctx, "ca too long"); | 204 | tls_set_errorx(ctx, "ca too long"); |
| 205 | goto err; | 205 | goto err; |
| 206 | } | 206 | } |
| 207 | 207 | ||
| 208 | if (SSL_CTX_load_verify_mem(ctx->ssl_ctx, | 208 | if (SSL_CTX_load_verify_mem(ctx->ssl_ctx, |
| 209 | ctx->config->ca_mem, ctx->config->ca_len) != 1) { | 209 | ctx->config->ca_mem, ctx->config->ca_len) != 1) { |
| 210 | tls_set_error(ctx, | 210 | tls_set_errorx(ctx, |
| 211 | "ssl verify memory setup failure"); | 211 | "ssl verify memory setup failure"); |
| 212 | goto err; | 212 | goto err; |
| 213 | } | 213 | } |
| 214 | } else if (SSL_CTX_load_verify_locations(ctx->ssl_ctx, | 214 | } else if (SSL_CTX_load_verify_locations(ctx->ssl_ctx, |
| 215 | ctx->config->ca_file, ctx->config->ca_path) != 1) { | 215 | ctx->config->ca_file, ctx->config->ca_path) != 1) { |
| 216 | tls_set_error(ctx, "ssl verify setup failure"); | 216 | tls_set_errorx(ctx, "ssl verify setup failure"); |
| 217 | goto err; | 217 | goto err; |
| 218 | } | 218 | } |
| 219 | if (ctx->config->verify_depth >= 0) | 219 | if (ctx->config->verify_depth >= 0) |
| @@ -222,16 +222,16 @@ tls_connect_fds(struct tls *ctx, int fd_read, int fd_write, | |||
| 222 | } | 222 | } |
| 223 | 223 | ||
| 224 | if ((ctx->ssl_conn = SSL_new(ctx->ssl_ctx)) == NULL) { | 224 | if ((ctx->ssl_conn = SSL_new(ctx->ssl_ctx)) == NULL) { |
| 225 | tls_set_error(ctx, "ssl connection failure"); | 225 | tls_set_errorx(ctx, "ssl connection failure"); |
| 226 | goto err; | 226 | goto err; |
| 227 | } | 227 | } |
| 228 | if (SSL_set_app_data(ctx->ssl_conn, ctx) != 1) { | 228 | if (SSL_set_app_data(ctx->ssl_conn, ctx) != 1) { |
| 229 | tls_set_error(ctx, "ssl application data failure"); | 229 | tls_set_errorx(ctx, "ssl application data failure"); |
| 230 | goto err; | 230 | goto err; |
| 231 | } | 231 | } |
| 232 | if (SSL_set_rfd(ctx->ssl_conn, fd_read) != 1 || | 232 | if (SSL_set_rfd(ctx->ssl_conn, fd_read) != 1 || |
| 233 | SSL_set_wfd(ctx->ssl_conn, fd_write) != 1) { | 233 | SSL_set_wfd(ctx->ssl_conn, fd_write) != 1) { |
| 234 | tls_set_error(ctx, "ssl file descriptor failure"); | 234 | tls_set_errorx(ctx, "ssl file descriptor failure"); |
| 235 | goto err; | 235 | goto err; |
| 236 | } | 236 | } |
| 237 | 237 | ||
| @@ -243,7 +243,7 @@ tls_connect_fds(struct tls *ctx, int fd_read, int fd_write, | |||
| 243 | inet_pton(AF_INET, servername, &addrbuf) != 1 && | 243 | inet_pton(AF_INET, servername, &addrbuf) != 1 && |
| 244 | inet_pton(AF_INET6, servername, &addrbuf) != 1) { | 244 | inet_pton(AF_INET6, servername, &addrbuf) != 1) { |
| 245 | if (SSL_set_tlsext_host_name(ctx->ssl_conn, servername) == 0) { | 245 | if (SSL_set_tlsext_host_name(ctx->ssl_conn, servername) == 0) { |
| 246 | tls_set_error(ctx, "server name indication failure"); | 246 | tls_set_errorx(ctx, "server name indication failure"); |
| 247 | goto err; | 247 | goto err; |
| 248 | } | 248 | } |
| 249 | } | 249 | } |
| @@ -262,12 +262,12 @@ connecting: | |||
| 262 | if (ctx->config->verify_name) { | 262 | if (ctx->config->verify_name) { |
| 263 | cert = SSL_get_peer_certificate(ctx->ssl_conn); | 263 | cert = SSL_get_peer_certificate(ctx->ssl_conn); |
| 264 | if (cert == NULL) { | 264 | if (cert == NULL) { |
| 265 | tls_set_error(ctx, "no server certificate"); | 265 | tls_set_errorx(ctx, "no server certificate"); |
| 266 | goto err; | 266 | goto err; |
| 267 | } | 267 | } |
| 268 | if ((ret = tls_check_servername(ctx, cert, servername)) != 0) { | 268 | if ((ret = tls_check_servername(ctx, cert, servername)) != 0) { |
| 269 | if (ret != -2) | 269 | if (ret != -2) |
| 270 | tls_set_error(ctx, "name `%s' not present in" | 270 | tls_set_errorx(ctx, "name `%s' not present in" |
| 271 | " server certificate", servername); | 271 | " server certificate", servername); |
| 272 | goto err; | 272 | goto err; |
| 273 | } | 273 | } |
diff --git a/src/lib/libtls/tls_internal.h b/src/lib/libtls/tls_internal.h index cf4a8e28ad..4503c20ab7 100644 --- a/src/lib/libtls/tls_internal.h +++ b/src/lib/libtls/tls_internal.h | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: tls_internal.h,v 1.13 2015/08/27 14:34:46 jsing Exp $ */ | 1 | /* $OpenBSD: tls_internal.h,v 1.14 2015/08/27 15:26:50 jsing Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2014 Jeremie Courreges-Anglas <jca@openbsd.org> | 3 | * Copyright (c) 2014 Jeremie Courreges-Anglas <jca@openbsd.org> |
| 4 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> | 4 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> |
| @@ -59,8 +59,8 @@ struct tls { | |||
| 59 | uint32_t flags; | 59 | uint32_t flags; |
| 60 | uint32_t state; | 60 | uint32_t state; |
| 61 | 61 | ||
| 62 | int err; | ||
| 63 | char *errmsg; | 62 | char *errmsg; |
| 63 | int errnum; | ||
| 64 | 64 | ||
| 65 | int socket; | 65 | int socket; |
| 66 | 66 | ||
| @@ -76,7 +76,10 @@ int tls_configure_keypair(struct tls *ctx); | |||
| 76 | int tls_configure_server(struct tls *ctx); | 76 | int tls_configure_server(struct tls *ctx); |
| 77 | int tls_configure_ssl(struct tls *ctx); | 77 | int tls_configure_ssl(struct tls *ctx); |
| 78 | int tls_host_port(const char *hostport, char **host, char **port); | 78 | int tls_host_port(const char *hostport, char **host, char **port); |
| 79 | int tls_set_error(struct tls *ctx, char *fmt, ...) | 79 | int tls_set_error(struct tls *ctx, const char *fmt, ...) |
| 80 | __attribute__((__format__ (printf, 2, 3))) | ||
| 81 | __attribute__((__nonnull__ (2))); | ||
| 82 | int tls_set_errorx(struct tls *ctx, const char *fmt, ...) | ||
| 80 | __attribute__((__format__ (printf, 2, 3))) | 83 | __attribute__((__format__ (printf, 2, 3))) |
| 81 | __attribute__((__nonnull__ (2))); | 84 | __attribute__((__nonnull__ (2))); |
| 82 | int tls_ssl_error(struct tls *ctx, SSL *ssl_conn, int ssl_ret, | 85 | int tls_ssl_error(struct tls *ctx, SSL *ssl_conn, int ssl_ret, |
diff --git a/src/lib/libtls/tls_server.c b/src/lib/libtls/tls_server.c index 605ab69219..bb29c7ce42 100644 --- a/src/lib/libtls/tls_server.c +++ b/src/lib/libtls/tls_server.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: tls_server.c,v 1.9 2015/08/22 14:52:39 jsing Exp $ */ | 1 | /* $OpenBSD: tls_server.c,v 1.10 2015/08/27 15:26:50 jsing Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> |
| 4 | * | 4 | * |
| @@ -54,7 +54,7 @@ tls_configure_server(struct tls *ctx) | |||
| 54 | unsigned char sid[SSL_MAX_SSL_SESSION_ID_LENGTH]; | 54 | unsigned char sid[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 55 | 55 | ||
| 56 | if ((ctx->ssl_ctx = SSL_CTX_new(SSLv23_server_method())) == NULL) { | 56 | if ((ctx->ssl_ctx = SSL_CTX_new(SSLv23_server_method())) == NULL) { |
| 57 | tls_set_error(ctx, "ssl context failure"); | 57 | tls_set_errorx(ctx, "ssl context failure"); |
| 58 | goto err; | 58 | goto err; |
| 59 | } | 59 | } |
| 60 | 60 | ||
| @@ -73,7 +73,7 @@ tls_configure_server(struct tls *ctx) | |||
| 73 | } else if (ctx->config->ecdhecurve != NID_undef) { | 73 | } else if (ctx->config->ecdhecurve != NID_undef) { |
| 74 | if ((ecdh_key = EC_KEY_new_by_curve_name( | 74 | if ((ecdh_key = EC_KEY_new_by_curve_name( |
| 75 | ctx->config->ecdhecurve)) == NULL) { | 75 | ctx->config->ecdhecurve)) == NULL) { |
| 76 | tls_set_error(ctx, "failed to set ECDHE curve"); | 76 | tls_set_errorx(ctx, "failed to set ECDHE curve"); |
| 77 | goto err; | 77 | goto err; |
| 78 | } | 78 | } |
| 79 | SSL_CTX_set_options(ctx->ssl_ctx, SSL_OP_SINGLE_ECDH_USE); | 79 | SSL_CTX_set_options(ctx->ssl_ctx, SSL_OP_SINGLE_ECDH_USE); |
| @@ -88,7 +88,7 @@ tls_configure_server(struct tls *ctx) | |||
| 88 | */ | 88 | */ |
| 89 | arc4random_buf(sid, sizeof(sid)); | 89 | arc4random_buf(sid, sizeof(sid)); |
| 90 | if (!SSL_CTX_set_session_id_context(ctx->ssl_ctx, sid, sizeof(sid))) { | 90 | if (!SSL_CTX_set_session_id_context(ctx->ssl_ctx, sid, sizeof(sid))) { |
| 91 | tls_set_error(ctx, "failed to set session id context"); | 91 | tls_set_errorx(ctx, "failed to set session id context"); |
| 92 | goto err; | 92 | goto err; |
| 93 | } | 93 | } |
| 94 | 94 | ||
| @@ -105,28 +105,28 @@ tls_accept_fds(struct tls *ctx, struct tls **cctx, int fd_read, int fd_write) | |||
| 105 | int ret, err; | 105 | int ret, err; |
| 106 | 106 | ||
| 107 | if ((ctx->flags & TLS_SERVER) == 0) { | 107 | if ((ctx->flags & TLS_SERVER) == 0) { |
| 108 | tls_set_error(ctx, "not a server context"); | 108 | tls_set_errorx(ctx, "not a server context"); |
| 109 | goto err; | 109 | goto err; |
| 110 | } | 110 | } |
| 111 | 111 | ||
| 112 | if (conn_ctx == NULL) { | 112 | if (conn_ctx == NULL) { |
| 113 | if ((conn_ctx = tls_server_conn(ctx)) == NULL) { | 113 | if ((conn_ctx = tls_server_conn(ctx)) == NULL) { |
| 114 | tls_set_error(ctx, "connection context failure"); | 114 | tls_set_errorx(ctx, "connection context failure"); |
| 115 | goto err; | 115 | goto err; |
| 116 | } | 116 | } |
| 117 | *cctx = conn_ctx; | 117 | *cctx = conn_ctx; |
| 118 | 118 | ||
| 119 | if ((conn_ctx->ssl_conn = SSL_new(ctx->ssl_ctx)) == NULL) { | 119 | if ((conn_ctx->ssl_conn = SSL_new(ctx->ssl_ctx)) == NULL) { |
| 120 | tls_set_error(ctx, "ssl failure"); | 120 | tls_set_errorx(ctx, "ssl failure"); |
| 121 | goto err; | 121 | goto err; |
| 122 | } | 122 | } |
| 123 | if (SSL_set_app_data(conn_ctx->ssl_conn, conn_ctx) != 1) { | 123 | if (SSL_set_app_data(conn_ctx->ssl_conn, conn_ctx) != 1) { |
| 124 | tls_set_error(ctx, "ssl application data failure"); | 124 | tls_set_errorx(ctx, "ssl application data failure"); |
| 125 | goto err; | 125 | goto err; |
| 126 | } | 126 | } |
| 127 | if (SSL_set_rfd(conn_ctx->ssl_conn, fd_read) != 1 || | 127 | if (SSL_set_rfd(conn_ctx->ssl_conn, fd_read) != 1 || |
| 128 | SSL_set_wfd(conn_ctx->ssl_conn, fd_write) != 1) { | 128 | SSL_set_wfd(conn_ctx->ssl_conn, fd_write) != 1) { |
| 129 | tls_set_error(ctx, "ssl file descriptor failure"); | 129 | tls_set_errorx(ctx, "ssl file descriptor failure"); |
| 130 | goto err; | 130 | goto err; |
| 131 | } | 131 | } |
| 132 | } | 132 | } |
diff --git a/src/lib/libtls/tls_verify.c b/src/lib/libtls/tls_verify.c index 8ddc68a8f1..c603ca8f73 100644 --- a/src/lib/libtls/tls_verify.c +++ b/src/lib/libtls/tls_verify.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: tls_verify.c,v 1.9 2015/08/27 07:15:39 jsing Exp $ */ | 1 | /* $OpenBSD: tls_verify.c,v 1.10 2015/08/27 15:26:50 jsing Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2014 Jeremie Courreges-Anglas <jca@openbsd.org> | 3 | * Copyright (c) 2014 Jeremie Courreges-Anglas <jca@openbsd.org> |
| 4 | * | 4 | * |
| @@ -125,7 +125,7 @@ tls_check_subject_altname(struct tls *ctx, X509 *cert, const char *name) | |||
| 125 | len = ASN1_STRING_length(altname->d.dNSName); | 125 | len = ASN1_STRING_length(altname->d.dNSName); |
| 126 | 126 | ||
| 127 | if (len < 0 || len != strlen(data)) { | 127 | if (len < 0 || len != strlen(data)) { |
| 128 | tls_set_error(ctx, | 128 | tls_set_errorx(ctx, |
| 129 | "error verifying name '%s': " | 129 | "error verifying name '%s': " |
| 130 | "NUL byte in subjectAltName, " | 130 | "NUL byte in subjectAltName, " |
| 131 | "probably a malicious certificate", | 131 | "probably a malicious certificate", |
| @@ -168,7 +168,7 @@ tls_check_subject_altname(struct tls *ctx, X509 *cert, const char *name) | |||
| 168 | data = ASN1_STRING_data(altname->d.iPAddress); | 168 | data = ASN1_STRING_data(altname->d.iPAddress); |
| 169 | 169 | ||
| 170 | if (datalen < 0) { | 170 | if (datalen < 0) { |
| 171 | tls_set_error(ctx, | 171 | tls_set_errorx(ctx, |
| 172 | "Unexpected negative length for an " | 172 | "Unexpected negative length for an " |
| 173 | "IP address: %d", datalen); | 173 | "IP address: %d", datalen); |
| 174 | rv = -2; | 174 | rv = -2; |
| @@ -218,7 +218,7 @@ tls_check_common_name(struct tls *ctx, X509 *cert, const char *name) | |||
| 218 | 218 | ||
| 219 | /* NUL bytes in CN? */ | 219 | /* NUL bytes in CN? */ |
| 220 | if (common_name_len != strlen(common_name)) { | 220 | if (common_name_len != strlen(common_name)) { |
| 221 | tls_set_error(ctx, "error verifying name '%s': " | 221 | tls_set_errorx(ctx, "error verifying name '%s': " |
| 222 | "NUL byte in Common Name field, " | 222 | "NUL byte in Common Name field, " |
| 223 | "probably a malicious certificate", name); | 223 | "probably a malicious certificate", name); |
| 224 | rv = -2; | 224 | rv = -2; |
