diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libtls/Makefile | 3 | ||||
-rw-r--r-- | src/lib/libtls/tls.h | 4 | ||||
-rw-r--r-- | src/lib/libtls/tls_peer.c | 87 |
3 files changed, 92 insertions, 2 deletions
diff --git a/src/lib/libtls/Makefile b/src/lib/libtls/Makefile index 4b6b34c283..0e3329589e 100644 --- a/src/lib/libtls/Makefile +++ b/src/lib/libtls/Makefile | |||
@@ -1,4 +1,4 @@ | |||
1 | # $OpenBSD: Makefile,v 1.14 2015/09/11 07:07:23 jmc Exp $ | 1 | # $OpenBSD: Makefile,v 1.15 2015/09/11 11:28:01 jsing Exp $ |
2 | 2 | ||
3 | CFLAGS+= -Wall -Werror -Wimplicit | 3 | CFLAGS+= -Wall -Werror -Wimplicit |
4 | CFLAGS+= -DLIBRESSL_INTERNAL | 4 | CFLAGS+= -DLIBRESSL_INTERNAL |
@@ -15,6 +15,7 @@ HDRS= tls.h | |||
15 | SRCS= tls.c \ | 15 | SRCS= tls.c \ |
16 | tls_client.c \ | 16 | tls_client.c \ |
17 | tls_config.c \ | 17 | tls_config.c \ |
18 | tls_peer.c \ | ||
18 | tls_server.c \ | 19 | tls_server.c \ |
19 | tls_util.c \ | 20 | tls_util.c \ |
20 | tls_verify.c | 21 | tls_verify.c |
diff --git a/src/lib/libtls/tls.h b/src/lib/libtls/tls.h index f7c12920e0..3cb24f0ee5 100644 --- a/src/lib/libtls/tls.h +++ b/src/lib/libtls/tls.h | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: tls.h,v 1.18 2015/09/10 10:26:49 beck Exp $ */ | 1 | /* $OpenBSD: tls.h,v 1.19 2015/09/11 11:28:01 jsing Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> |
4 | * | 4 | * |
@@ -99,6 +99,8 @@ ssize_t tls_read(struct tls *_ctx, void *_buf, size_t _buflen); | |||
99 | ssize_t tls_write(struct tls *_ctx, const void *_buf, size_t _buflen); | 99 | ssize_t tls_write(struct tls *_ctx, const void *_buf, size_t _buflen); |
100 | int tls_close(struct tls *_ctx); | 100 | int tls_close(struct tls *_ctx); |
101 | 101 | ||
102 | int tls_peer_cert_hash(struct tls *_ctx, char **_hash); | ||
103 | |||
102 | uint8_t *tls_load_file(const char *_file, size_t *_len, char *_password); | 104 | uint8_t *tls_load_file(const char *_file, size_t *_len, char *_password); |
103 | 105 | ||
104 | #ifdef __cplusplus | 106 | #ifdef __cplusplus |
diff --git a/src/lib/libtls/tls_peer.c b/src/lib/libtls/tls_peer.c new file mode 100644 index 0000000000..e2a2c7b141 --- /dev/null +++ b/src/lib/libtls/tls_peer.c | |||
@@ -0,0 +1,87 @@ | |||
1 | /* $OpenBSD: tls_peer.c,v 1.1 2015/09/11 11:28:01 jsing Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2015 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 | #include <stdio.h> | ||
19 | |||
20 | #include <openssl/x509.h> | ||
21 | |||
22 | #include <tls.h> | ||
23 | #include "tls_internal.h" | ||
24 | |||
25 | static int | ||
26 | tls_hex_string(const unsigned char *in, size_t inlen, char **out, | ||
27 | size_t *outlen) | ||
28 | { | ||
29 | static const char hex[] = "0123456789abcdef"; | ||
30 | size_t i, len; | ||
31 | char *p; | ||
32 | |||
33 | if (outlen != NULL) | ||
34 | *outlen = 0; | ||
35 | |||
36 | if (inlen >= SIZE_MAX) | ||
37 | return (-1); | ||
38 | if ((*out = reallocarray(NULL, inlen + 1, 2)) == NULL) | ||
39 | return (-1); | ||
40 | |||
41 | p = *out; | ||
42 | len = 0; | ||
43 | for (i = 0; i < inlen; i++) { | ||
44 | p[len++] = hex[(in[i] >> 4) & 0x0f]; | ||
45 | p[len++] = hex[in[i] & 0x0f]; | ||
46 | } | ||
47 | p[len++] = 0; | ||
48 | |||
49 | if (outlen != NULL) | ||
50 | *outlen = len; | ||
51 | |||
52 | return (0); | ||
53 | } | ||
54 | |||
55 | int | ||
56 | tls_peer_cert_hash(struct tls *ctx, char **hash) | ||
57 | { | ||
58 | char d[EVP_MAX_MD_SIZE], *dhex = NULL; | ||
59 | int dlen, rv = -1; | ||
60 | |||
61 | *hash = NULL; | ||
62 | if (ctx->ssl_peer_cert == NULL) | ||
63 | return (0); | ||
64 | |||
65 | if (X509_digest(ctx->ssl_peer_cert, EVP_sha256(), d, &dlen) != 1) { | ||
66 | tls_set_errorx(ctx, "digest failed"); | ||
67 | goto err; | ||
68 | } | ||
69 | |||
70 | if (tls_hex_string(d, dlen, &dhex, NULL) != 0) { | ||
71 | tls_set_errorx(ctx, "digest hex string failed"); | ||
72 | goto err; | ||
73 | } | ||
74 | |||
75 | if (asprintf(hash, "SHA256:%s", dhex) == -1) { | ||
76 | tls_set_errorx(ctx, "out of memory"); | ||
77 | *hash = NULL; | ||
78 | goto err; | ||
79 | } | ||
80 | |||
81 | rv = 0; | ||
82 | |||
83 | err: | ||
84 | free(dhex); | ||
85 | |||
86 | return (rv); | ||
87 | } | ||