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