summaryrefslogtreecommitdiff
path: root/lib/libtls/tls_config.c
diff options
context:
space:
mode:
authorJoel Sing <jsing@cvs.openbsd.org>2015-02-12 04:35:18 +0000
committerJoel Sing <jsing@cvs.openbsd.org>2015-02-12 04:35:18 +0000
commit496ad4078aa0f09a35229e5d6d5bcddbf15954ac (patch)
tree3338712700577ac921ddf03596edf6d58db9fbaf /lib/libtls/tls_config.c
parent4c15799ca50e480e007a58833fada9f74e231e68 (diff)
Add a tls_config_parse_protocols() function that allows a protocols string
to be converted into a libtls protocols value. This allows for things like: "tlsv1.0,tlsv1.1" (TLSv1.0 and TLSv1.1) "all,!tlsv1.0" (all protocols except TLSv1.0) Discussed with tedu@ and reyk@
Diffstat (limited to 'lib/libtls/tls_config.c')
-rw-r--r--lib/libtls/tls_config.c61
1 files changed, 60 insertions, 1 deletions
diff --git a/lib/libtls/tls_config.c b/lib/libtls/tls_config.c
index 4342b5a565d..bec7afcb1b1 100644
--- a/lib/libtls/tls_config.c
+++ b/lib/libtls/tls_config.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: tls_config.c,v 1.5 2015/02/12 04:31:27 jsing Exp $ */
+/* $OpenBSD: tls_config.c,v 1.6 2015/02/12 04:35:17 jsing Exp $ */
/*
* Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
*
@@ -15,6 +15,7 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
@@ -109,6 +110,64 @@ tls_config_clear_keys(struct tls_config *config)
}
int
+tls_config_parse_protocols(uint32_t *protocols, const char *protostr)
+{
+ uint32_t proto, protos = 0;
+ char *s, *p, *q;
+ int negate;
+
+ if ((s = strdup(protostr)) == NULL)
+ return (-1);
+
+ q = s;
+ while ((p = strsep(&q, ",:")) != NULL) {
+ while (*p == ' ' || *p == '\t')
+ p++;
+
+ negate = 0;
+ if (*p == '!') {
+ negate = 1;
+ p++;
+ }
+
+ if (negate && protos == 0)
+ protos = TLS_PROTOCOLS_ALL;
+
+ proto = 0;
+ if (strcasecmp(p, "all") == 0 ||
+ strcasecmp(p, "legacy") == 0)
+ proto = TLS_PROTOCOLS_ALL;
+ else if (strcasecmp(p, "default") == 0 ||
+ strcasecmp(p, "secure") == 0)
+ proto = TLS_PROTOCOLS_DEFAULT;
+ if (strcasecmp(p, "tlsv1") == 0)
+ proto = TLS_PROTOCOL_TLSv1;
+ else if (strcasecmp(p, "tlsv1.0") == 0)
+ proto = TLS_PROTOCOL_TLSv1_0;
+ else if (strcasecmp(p, "tlsv1.1") == 0)
+ proto = TLS_PROTOCOL_TLSv1_1;
+ else if (strcasecmp(p, "tlsv1.2") == 0)
+ proto = TLS_PROTOCOL_TLSv1_2;
+
+ if (proto == 0) {
+ free(s);
+ return (-1);
+ }
+
+ if (negate)
+ protos &= ~proto;
+ else
+ protos |= proto;
+ }
+
+ *protocols = protos;
+
+ free(s);
+
+ return (0);
+}
+
+int
tls_config_set_ca_file(struct tls_config *config, const char *ca_file)
{
return set_string(&config->ca_file, ca_file);