1 /* $OpenBSD: tls_config.c,v 1.21 2016/07/07 14:09:03 jsing Exp $ */
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
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.
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.
23 #include "tls_internal.h"
26 set_string(const char **dest
, const char *src
)
31 if ((*dest
= strdup(src
)) == NULL
)
37 memdup(const void *in
, size_t len
)
41 if ((out
= malloc(len
)) == NULL
)
48 set_mem(char **dest
, size_t *destlen
, const void *src
, size_t srclen
)
54 if ((*dest
= memdup(src
, srclen
)) == NULL
)
60 static struct tls_keypair
*
63 return calloc(1, sizeof(struct tls_keypair
));
67 tls_keypair_set_cert_file(struct tls_keypair
*keypair
, const char *cert_file
)
69 return set_string(&keypair
->cert_file
, cert_file
);
73 tls_keypair_set_cert_mem(struct tls_keypair
*keypair
, const uint8_t *cert
,
76 return set_mem(&keypair
->cert_mem
, &keypair
->cert_len
, cert
, len
);
80 tls_keypair_set_key_file(struct tls_keypair
*keypair
, const char *key_file
)
82 return set_string(&keypair
->key_file
, key_file
);
86 tls_keypair_set_key_mem(struct tls_keypair
*keypair
, const uint8_t *key
,
89 if (keypair
->key_mem
!= NULL
)
90 explicit_bzero(keypair
->key_mem
, keypair
->key_len
);
91 return set_mem(&keypair
->key_mem
, &keypair
->key_len
, key
, len
);
95 tls_keypair_clear(struct tls_keypair
*keypair
)
97 tls_keypair_set_cert_mem(keypair
, NULL
, 0);
98 tls_keypair_set_key_mem(keypair
, NULL
, 0);
102 tls_keypair_free(struct tls_keypair
*keypair
)
107 tls_keypair_clear(keypair
);
109 free((char *)keypair
->cert_file
);
110 free(keypair
->cert_mem
);
111 free((char *)keypair
->key_file
);
112 free(keypair
->key_mem
);
120 struct tls_config
*config
;
122 if ((config
= calloc(1, sizeof(*config
))) == NULL
)
125 if ((config
->keypair
= tls_keypair_new()) == NULL
)
129 * Default configuration.
131 if (tls_config_set_ca_file(config
, _PATH_SSL_CA_FILE
) != 0)
133 if (tls_config_set_dheparams(config
, "none") != 0)
135 if (tls_config_set_ecdhecurve(config
, "auto") != 0)
137 if (tls_config_set_ciphers(config
, "secure") != 0)
140 tls_config_set_protocols(config
, TLS_PROTOCOLS_DEFAULT
);
141 tls_config_set_verify_depth(config
, 6);
143 tls_config_prefer_ciphers_server(config
);
145 tls_config_verify(config
);
150 tls_config_free(config
);
155 tls_config_free(struct tls_config
*config
)
157 struct tls_keypair
*kp
, *nkp
;
162 for (kp
= config
->keypair
; kp
!= NULL
; kp
= nkp
) {
164 tls_keypair_free(kp
);
167 free(config
->error
.msg
);
169 free((char *)config
->ca_file
);
170 free((char *)config
->ca_mem
);
171 free((char *)config
->ca_path
);
172 free((char *)config
->ciphers
);
178 tls_config_error(struct tls_config
*config
)
180 return config
->error
.msg
;
184 tls_config_clear_keys(struct tls_config
*config
)
186 struct tls_keypair
*kp
;
188 for (kp
= config
->keypair
; kp
!= NULL
; kp
= kp
->next
)
189 tls_keypair_clear(kp
);
191 tls_config_set_ca_mem(config
, NULL
, 0);
195 tls_config_parse_protocols(uint32_t *protocols
, const char *protostr
)
197 uint32_t proto
, protos
= 0;
201 if ((s
= strdup(protostr
)) == NULL
)
205 while ((p
= strsep(&q
, ",:")) != NULL
) {
206 while (*p
== ' ' || *p
== '\t')
215 if (negate
&& protos
== 0)
216 protos
= TLS_PROTOCOLS_ALL
;
219 if (strcasecmp(p
, "all") == 0 ||
220 strcasecmp(p
, "legacy") == 0)
221 proto
= TLS_PROTOCOLS_ALL
;
222 else if (strcasecmp(p
, "default") == 0 ||
223 strcasecmp(p
, "secure") == 0)
224 proto
= TLS_PROTOCOLS_DEFAULT
;
225 if (strcasecmp(p
, "tlsv1") == 0)
226 proto
= TLS_PROTOCOL_TLSv1
;
227 else if (strcasecmp(p
, "tlsv1.0") == 0)
228 proto
= TLS_PROTOCOL_TLSv1_0
;
229 else if (strcasecmp(p
, "tlsv1.1") == 0)
230 proto
= TLS_PROTOCOL_TLSv1_1
;
231 else if (strcasecmp(p
, "tlsv1.2") == 0)
232 proto
= TLS_PROTOCOL_TLSv1_2
;
253 tls_config_set_ca_file(struct tls_config
*config
, const char *ca_file
)
255 return set_string(&config
->ca_file
, ca_file
);
259 tls_config_set_ca_path(struct tls_config
*config
, const char *ca_path
)
261 return set_string(&config
->ca_path
, ca_path
);
265 tls_config_set_ca_mem(struct tls_config
*config
, const uint8_t *ca
, size_t len
)
267 return set_mem(&config
->ca_mem
, &config
->ca_len
, ca
, len
);
271 tls_config_set_cert_file(struct tls_config
*config
, const char *cert_file
)
273 return tls_keypair_set_cert_file(config
->keypair
, cert_file
);
277 tls_config_set_cert_mem(struct tls_config
*config
, const uint8_t *cert
,
280 return tls_keypair_set_cert_mem(config
->keypair
, cert
, len
);
284 tls_config_set_ciphers(struct tls_config
*config
, const char *ciphers
)
286 SSL_CTX
*ssl_ctx
= NULL
;
288 if (ciphers
== NULL
||
289 strcasecmp(ciphers
, "default") == 0 ||
290 strcasecmp(ciphers
, "secure") == 0)
291 ciphers
= TLS_CIPHERS_DEFAULT
;
292 else if (strcasecmp(ciphers
, "compat") == 0)
293 ciphers
= TLS_CIPHERS_COMPAT
;
294 else if (strcasecmp(ciphers
, "legacy") == 0)
295 ciphers
= TLS_CIPHERS_LEGACY
;
296 else if (strcasecmp(ciphers
, "all") == 0 ||
297 strcasecmp(ciphers
, "insecure") == 0)
298 ciphers
= TLS_CIPHERS_ALL
;
300 if ((ssl_ctx
= SSL_CTX_new(SSLv23_method())) == NULL
) {
301 tls_config_set_errorx(config
, "out of memory");
304 if (SSL_CTX_set_cipher_list(ssl_ctx
, ciphers
) != 1) {
305 tls_config_set_errorx(config
, "no ciphers for '%s'", ciphers
);
309 SSL_CTX_free(ssl_ctx
);
310 return set_string(&config
->ciphers
, ciphers
);
313 SSL_CTX_free(ssl_ctx
);
318 tls_config_set_dheparams(struct tls_config
*config
, const char *params
)
322 if (params
== NULL
|| strcasecmp(params
, "none") == 0)
324 else if (strcasecmp(params
, "auto") == 0)
326 else if (strcasecmp(params
, "legacy") == 0)
329 tls_config_set_errorx(config
, "invalid dhe param '%s'", params
);
333 config
->dheparams
= keylen
;
339 tls_config_set_ecdhecurve(struct tls_config
*config
, const char *name
)
343 if (name
== NULL
|| strcasecmp(name
, "none") == 0)
345 else if (strcasecmp(name
, "auto") == 0)
347 else if ((nid
= OBJ_txt2nid(name
)) == NID_undef
) {
348 tls_config_set_errorx(config
, "invalid ecdhe curve '%s'", name
);
352 config
->ecdhecurve
= nid
;
358 tls_config_set_key_file(struct tls_config
*config
, const char *key_file
)
360 return tls_keypair_set_key_file(config
->keypair
, key_file
);
364 tls_config_set_key_mem(struct tls_config
*config
, const uint8_t *key
,
367 return tls_keypair_set_key_mem(config
->keypair
, key
, len
);
371 tls_config_set_keypair_file(struct tls_config
*config
,
372 const char *cert_file
, const char *key_file
)
374 if (tls_config_set_cert_file(config
, cert_file
) != 0)
376 if (tls_config_set_key_file(config
, key_file
) != 0)
383 tls_config_set_keypair_mem(struct tls_config
*config
, const uint8_t *cert
,
384 size_t cert_len
, const uint8_t *key
, size_t key_len
)
386 if (tls_config_set_cert_mem(config
, cert
, cert_len
) != 0)
388 if (tls_config_set_key_mem(config
, key
, key_len
) != 0)
395 tls_config_set_protocols(struct tls_config
*config
, uint32_t protocols
)
397 config
->protocols
= protocols
;
401 tls_config_set_verify_depth(struct tls_config
*config
, int verify_depth
)
403 config
->verify_depth
= verify_depth
;
407 tls_config_prefer_ciphers_client(struct tls_config
*config
)
409 config
->ciphers_server
= 0;
413 tls_config_prefer_ciphers_server(struct tls_config
*config
)
415 config
->ciphers_server
= 1;
419 tls_config_insecure_noverifycert(struct tls_config
*config
)
421 config
->verify_cert
= 0;
425 tls_config_insecure_noverifyname(struct tls_config
*config
)
427 config
->verify_name
= 0;
431 tls_config_insecure_noverifytime(struct tls_config
*config
)
433 config
->verify_time
= 0;
437 tls_config_verify(struct tls_config
*config
)
439 config
->verify_cert
= 1;
440 config
->verify_name
= 1;
441 config
->verify_time
= 1;
445 tls_config_verify_client(struct tls_config
*config
)
447 config
->verify_client
= 1;
451 tls_config_verify_client_optional(struct tls_config
*config
)
453 config
->verify_client
= 2;