1 /* Copyright (c) 2007-2009, UNINETT AS
2 * Copyright (c) 2010-2011, NORDUnet A/S */
3 /* See LICENSE for licensing information. */
5 #if defined HAVE_CONFIG_H
11 #include <sys/socket.h>
12 #include <netinet/in.h>
21 #include <sys/select.h>
24 #include <arpa/inet.h>
28 #include <openssl/ssl.h>
29 #include <openssl/rand.h>
30 #include <openssl/err.h>
31 #include <openssl/md5.h>
32 #include <openssl/x509v3.h>
37 #include "hostport_types.h"
38 #include "radsecproxy.h"
40 static struct hash *tlsconfs = NULL;
42 static int pem_passwd_cb(char *buf, int size, int rwflag, void *userdata) {
43 int pwdlen = strlen(userdata);
44 if (rwflag != 0 || pwdlen > size) /* not for decryption or too large */
46 memcpy(buf, userdata, pwdlen);
50 static int verify_cb(int ok, X509_STORE_CTX *ctx) {
55 err_cert = X509_STORE_CTX_get_current_cert(ctx);
56 err = X509_STORE_CTX_get_error(ctx);
57 depth = X509_STORE_CTX_get_error_depth(ctx);
59 if (depth > MAX_CERT_DEPTH) {
61 err = X509_V_ERR_CERT_CHAIN_TOO_LONG;
62 X509_STORE_CTX_set_error(ctx, err);
67 buf = X509_NAME_oneline(X509_get_subject_name(err_cert), NULL, 0);
68 debug(DBG_WARN, "verify error: num=%d:%s:depth=%d:%s", err, X509_verify_cert_error_string(err), depth, buf ? buf : "");
73 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
75 buf = X509_NAME_oneline(X509_get_issuer_name(err_cert), NULL, 0);
77 debug(DBG_WARN, "\tIssuer=%s", buf);
83 case X509_V_ERR_CERT_NOT_YET_VALID:
84 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
85 debug(DBG_WARN, "\tCertificate not yet valid");
87 case X509_V_ERR_CERT_HAS_EXPIRED:
88 debug(DBG_WARN, "Certificate has expired");
90 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
91 debug(DBG_WARN, "Certificate no longer valid (after notAfter)");
93 case X509_V_ERR_NO_EXPLICIT_POLICY:
94 debug(DBG_WARN, "No Explicit Certificate Policy");
102 static void ssl_info_callback(const SSL *ssl, int where, int ret) {
106 w = where & ~SSL_ST_MASK;
108 if (w & SSL_ST_CONNECT)
110 else if (w & SSL_ST_ACCEPT)
115 if (where & SSL_CB_LOOP)
116 debug(DBG_DBG, "%s:%s\n", s, SSL_state_string_long(ssl));
117 else if (where & SSL_CB_ALERT) {
118 s = (where & SSL_CB_READ) ? "read" : "write";
119 debug(DBG_DBG, "SSL3 alert %s:%s:%s\n", s, SSL_alert_type_string_long(ret), SSL_alert_desc_string_long(ret));
121 else if (where & SSL_CB_EXIT) {
123 debug(DBG_DBG, "%s:failed in %s\n", s, SSL_state_string_long(ssl));
125 debug(DBG_DBG, "%s:error in %s\n", s, SSL_state_string_long(ssl));
130 static X509_VERIFY_PARAM *createverifyparams(char **poids) {
131 X509_VERIFY_PARAM *pm;
132 ASN1_OBJECT *pobject;
135 pm = X509_VERIFY_PARAM_new();
139 for (i = 0; poids[i]; i++) {
140 pobject = OBJ_txt2obj(poids[i], 0);
142 X509_VERIFY_PARAM_free(pm);
145 X509_VERIFY_PARAM_add0_policy(pm, pobject);
148 X509_VERIFY_PARAM_set_flags(pm, X509_V_FLAG_POLICY_CHECK | X509_V_FLAG_EXPLICIT_POLICY);
152 static int tlsaddcacrl(SSL_CTX *ctx, struct tls *conf) {
153 STACK_OF(X509_NAME) *calist;
157 if (!SSL_CTX_load_verify_locations(ctx, conf->cacertfile, conf->cacertpath)) {
158 while ((error = ERR_get_error()))
159 debug(DBG_ERR, "SSL: %s", ERR_error_string(error, NULL));
160 debug(DBG_ERR, "tlsaddcacrl: Error updating TLS context %s", conf->name);
164 calist = conf->cacertfile ? SSL_load_client_CA_file(conf->cacertfile) : NULL;
166 if (!conf->cacertfile || calist) {
167 if (conf->cacertpath) {
169 calist = sk_X509_NAME_new_null();
170 if (!SSL_add_dir_cert_subjects_to_stack(calist, conf->cacertpath)) {
171 sk_X509_NAME_free(calist);
177 while ((error = ERR_get_error()))
178 debug(DBG_ERR, "SSL: %s", ERR_error_string(error, NULL));
179 debug(DBG_ERR, "tlsaddcacrl: Error adding CA subjects in TLS context %s", conf->name);
182 ERR_clear_error(); /* add_dir_cert_subj returns errors on success */
183 SSL_CTX_set_client_CA_list(ctx, calist);
185 SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, verify_cb);
186 SSL_CTX_set_verify_depth(ctx, MAX_CERT_DEPTH + 1);
188 if (conf->crlcheck || conf->vpm) {
189 x509_s = SSL_CTX_get_cert_store(ctx);
191 X509_STORE_set_flags(x509_s, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
193 X509_STORE_set1_param(x509_s, conf->vpm);
196 debug(DBG_DBG, "tlsaddcacrl: updated TLS context %s", conf->name);
200 static SSL_CTX *tlscreatectx(uint8_t type, struct tls *conf) {
207 ctx = SSL_CTX_new(TLSv1_method());
212 ctx = SSL_CTX_new(DTLSv1_method());
213 SSL_CTX_set_read_ahead(ctx, 1);
218 debug(DBG_ERR, "tlscreatectx: Error initialising SSL/TLS in TLS context %s", conf->name);
219 while ((error = ERR_get_error()))
220 debug(DBG_ERR, "SSL: %s", ERR_error_string(error, NULL));
224 SSL_CTX_set_info_callback(ctx, ssl_info_callback);
227 if (conf->certkeypwd) {
228 SSL_CTX_set_default_passwd_cb_userdata(ctx, conf->certkeypwd);
229 SSL_CTX_set_default_passwd_cb(ctx, pem_passwd_cb);
231 if (conf->certfile || conf->certkeyfile) {
232 if (!SSL_CTX_use_certificate_chain_file(ctx, conf->certfile) ||
233 !SSL_CTX_use_PrivateKey_file(ctx, conf->certkeyfile, SSL_FILETYPE_PEM) ||
234 !SSL_CTX_check_private_key(ctx)) {
235 while ((error = ERR_get_error()))
236 debug(DBG_ERR, "SSL: %s", ERR_error_string(error, NULL));
237 debug(DBG_ERR, "tlscreatectx: Error initialising SSL/TLS (certfile issues) in TLS context %s", conf->name);
243 if (conf->policyoids) {
245 conf->vpm = createverifyparams(conf->policyoids);
247 debug(DBG_ERR, "tlscreatectx: Failed to add policyOIDs in TLS context %s", conf->name);
254 if (conf->cacertfile != NULL || conf->cacertpath != NULL)
255 if (!tlsaddcacrl(ctx, conf)) {
257 X509_VERIFY_PARAM_free(conf->vpm);
264 debug(DBG_DBG, "tlscreatectx: created TLS context %s", conf->name);
268 struct tls *tlsgettls(char *alt1, char *alt2) {
271 t = hash_read(tlsconfs, alt1, strlen(alt1));
273 t = hash_read(tlsconfs, alt2, strlen(alt2));
277 SSL_CTX *tlsgetctx(uint8_t type, struct tls *t) {
282 gettimeofday(&now, NULL);
287 if (t->tlsexpiry && t->tlsctx) {
288 if (t->tlsexpiry < now.tv_sec) {
289 t->tlsexpiry = now.tv_sec + t->cacheexpiry;
290 tlsaddcacrl(t->tlsctx, t);
294 t->tlsctx = tlscreatectx(RAD_TLS, t);
296 t->tlsexpiry = now.tv_sec + t->cacheexpiry;
302 if (t->dtlsexpiry && t->dtlsctx) {
303 if (t->dtlsexpiry < now.tv_sec) {
304 t->dtlsexpiry = now.tv_sec + t->cacheexpiry;
305 tlsaddcacrl(t->dtlsctx, t);
309 t->dtlsctx = tlscreatectx(RAD_DTLS, t);
311 t->dtlsexpiry = now.tv_sec + t->cacheexpiry;
319 X509 *verifytlscert(SSL *ssl) {
323 if (SSL_get_verify_result(ssl) != X509_V_OK) {
324 debug(DBG_ERR, "verifytlscert: basic validation failed");
325 while ((error = ERR_get_error()))
326 debug(DBG_ERR, "verifytlscert: TLS: %s", ERR_error_string(error, NULL));
330 cert = SSL_get_peer_certificate(ssl);
332 debug(DBG_ERR, "verifytlscert: failed to obtain certificate");
336 int subjectaltnameaddr(X509 *cert, int family, const struct in6_addr *addr) {
337 int loc, i, l, n, r = 0;
340 STACK_OF(GENERAL_NAME) *alt;
343 debug(DBG_DBG, "subjectaltnameaddr");
345 loc = X509_get_ext_by_NID(cert, NID_subject_alt_name, -1);
349 ex = X509_get_ext(cert, loc);
350 alt = X509V3_EXT_d2i(ex);
354 n = sk_GENERAL_NAME_num(alt);
355 for (i = 0; i < n; i++) {
356 gn = sk_GENERAL_NAME_value(alt, i);
357 if (gn->type != GEN_IPADD)
360 v = (char *)ASN1_STRING_data(gn->d.ia5);
361 l = ASN1_STRING_length(gn->d.ia5);
362 if (((family == AF_INET && l == sizeof(struct in_addr)) || (family == AF_INET6 && l == sizeof(struct in6_addr)))
363 && !memcmp(v, &addr, l)) {
368 GENERAL_NAMES_free(alt);
372 int subjectaltnameregexp(X509 *cert, int type, const char *exact, const regex_t *regex) {
373 int loc, i, l, n, r = 0;
376 STACK_OF(GENERAL_NAME) *alt;
379 debug(DBG_DBG, "subjectaltnameregexp");
381 loc = X509_get_ext_by_NID(cert, NID_subject_alt_name, -1);
385 ex = X509_get_ext(cert, loc);
386 alt = X509V3_EXT_d2i(ex);
390 n = sk_GENERAL_NAME_num(alt);
391 for (i = 0; i < n; i++) {
392 gn = sk_GENERAL_NAME_value(alt, i);
393 if (gn->type != type)
396 v = (char *)ASN1_STRING_data(gn->d.ia5);
397 l = ASN1_STRING_length(gn->d.ia5);
401 printfchars(NULL, gn->type == GEN_DNS ? "dns" : "uri", NULL, v, l);
404 if (memcmp(v, exact, l))
407 s = stringcopy((char *)v, l);
409 debug(DBG_ERR, "malloc failed");
412 if (regexec(regex, s, 0, NULL, 0)) {
421 GENERAL_NAMES_free(alt);
425 int cnregexp(X509 *cert, const char *exact, const regex_t *regex) {
432 nm = X509_get_subject_name(cert);
435 loc = X509_NAME_get_index_by_NID(nm, NID_commonName, loc);
438 e = X509_NAME_get_entry(nm, loc);
439 t = X509_NAME_ENTRY_get_data(e);
440 v = (char *) ASN1_STRING_data(t);
441 l = ASN1_STRING_length(t);
445 if (l == strlen(exact) && !strncasecmp(exact, v, l))
448 s = stringcopy((char *)v, l);
450 debug(DBG_ERR, "malloc failed");
453 if (regexec(regex, s, 0, NULL, 0)) {
464 /* this is a bit sloppy, should not always accept match to any */
465 int certnamecheck(X509 *cert, struct list *hostports) {
466 struct list_node *entry;
467 struct hostportres *hp;
469 uint8_t type = 0; /* 0 for DNS, AF_INET for IPv4, AF_INET6 for IPv6 */
470 struct in6_addr addr;
472 for (entry = list_first(hostports); entry; entry = list_next(entry)) {
473 hp = (struct hostportres *)entry->data;
474 if (hp->prefixlen != 255) {
475 /* we disable the check for prefixes */
478 if (inet_pton(AF_INET, hp->host, &addr))
480 else if (inet_pton(AF_INET6, hp->host, &addr))
485 r = type ? subjectaltnameaddr(cert, type, &addr) : subjectaltnameregexp(cert, GEN_DNS, hp->host, NULL);
488 debug(DBG_DBG, "certnamecheck: Found subjectaltname matching %s %s", type ? "address" : "host", hp->host);
491 debug(DBG_WARN, "certnamecheck: No subjectaltname matching %s %s", type ? "address" : "host", hp->host);
493 if (cnregexp(cert, hp->host, NULL)) {
494 debug(DBG_DBG, "certnamecheck: Found cn matching host %s", hp->host);
497 debug(DBG_WARN, "certnamecheck: cn not matching host %s", hp->host);
503 int verifyconfcert(X509 *cert, struct clsrvconf *conf) {
504 if (conf->certnamecheck) {
505 if (!certnamecheck(cert, conf->hostports)) {
506 debug(DBG_WARN, "verifyconfcert: certificate name check failed");
509 debug(DBG_WARN, "verifyconfcert: certificate name check ok");
511 if (conf->certcnregex) {
512 if (cnregexp(cert, NULL, conf->certcnregex) < 1) {
513 debug(DBG_WARN, "verifyconfcert: CN not matching regex");
516 debug(DBG_DBG, "verifyconfcert: CN matching regex");
518 if (conf->certuriregex) {
519 if (subjectaltnameregexp(cert, GEN_URI, NULL, conf->certuriregex) < 1) {
520 debug(DBG_WARN, "verifyconfcert: subjectaltname URI not matching regex");
523 debug(DBG_DBG, "verifyconfcert: subjectaltname URI matching regex");
528 /* Local Variables: */
529 /* c-file-style: "stroustrup" */