Bringing up TLS connections working.
[libradsec.git] / lib / tls.c
1 /* See the file COPYING for licensing information.  */
2
3 #if defined HAVE_CONFIG_H
4 #include <config.h>
5 #endif
6
7 #include <assert.h>
8 #include <openssl/ssl.h>
9 #include <radsec/radsec.h>
10 #include <radsec/radsec-impl.h>
11
12 #include <regex.h>
13 #include "rsp_list.h"
14 #include "../radsecproxy.h"
15
16 static struct tls *
17 _get_tlsconf (const struct rs_context *ctx, const struct rs_realm *realm)
18 {
19   struct tls *c = rs_malloc (ctx, sizeof (struct tls));
20
21   if (c)
22     {
23       memset (c, 0, sizeof (struct tls));
24       /* TODO: Make sure old radsecproxy code doesn't free these all
25          of a sudden, or strdup them.  */
26       c->name = realm->name;
27       c->cacertfile = realm->cacertfile;
28       c->cacertpath = NULL;     /* NYI */
29       c->certfile = realm->certfile;
30       c->certkeyfile = realm->certkeyfile;
31       c->certkeypwd = NULL;     /* NYI */
32       c->cacheexpiry = 0;       /* NYI */
33       c->crlcheck = 0;          /* NYI */
34       c->policyoids = (char **) NULL; /* NYI */
35     }
36
37   return c;
38 }
39
40 int
41 rs_tls_init (struct rs_connection *conn)
42 {
43   struct rs_context *ctx;
44   struct tls *tlsconf;
45   SSL_CTX *ssl_ctx;
46   SSL *ssl;
47   assert (conn->ctx);
48   ctx = conn->ctx;
49
50   tlsconf = _get_tlsconf (ctx, conn->active_peer->realm);
51   assert (tlsconf);
52   ssl_ctx = tlsgetctx (RADPROT_TLS, tlsconf);
53   if (!ssl_ctx)
54     {
55       /* TODO: check radsecproxy error  */
56       return rs_err_conn_push_fl (conn, RSE_SOME_ERROR, __FILE__, __LINE__,
57                                   NULL);
58     }
59
60   ssl = SSL_new (ssl_ctx);
61   if (!ssl)
62     {
63       /* TODO: check and report SSL error  */
64       /* TODO: free ssl_ctx  */
65       return rs_err_conn_push_fl (conn, RSE_SOME_ERROR, __FILE__, __LINE__,
66                                   NULL);
67     }
68
69   conn->tls_ctx = ssl_ctx;
70   conn->tls_ssl = ssl;
71   rs_free (ctx, tlsconf);
72   return RSE_OK;
73 }