Handle another ENOMEM case.
[radsecproxy.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 (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     else
37       rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__, NULL);
38
39   return c;
40 }
41
42 int
43 rs_tls_init (struct rs_connection *conn)
44 {
45   struct rs_context *ctx;
46   struct tls *tlsconf;
47   SSL_CTX *ssl_ctx;
48   SSL *ssl;
49   assert (conn->ctx);
50   ctx = conn->ctx;
51
52   tlsconf = _get_tlsconf (ctx, conn->active_peer->realm);
53   if (!tlsconf)
54     return -1;
55   ssl_ctx = tlsgetctx (RADPROT_TLS, tlsconf);
56   if (!ssl_ctx)
57     {
58       /* TODO: check radsecproxy error  */
59       return rs_err_conn_push_fl (conn, RSE_SOME_ERROR, __FILE__, __LINE__,
60                                   NULL);
61     }
62
63   ssl = SSL_new (ssl_ctx);
64   if (!ssl)
65     {
66       /* TODO: check and report SSL error  */
67       /* TODO: free ssl_ctx  */
68       return rs_err_conn_push_fl (conn, RSE_SOME_ERROR, __FILE__, __LINE__,
69                                   NULL);
70     }
71
72   conn->tls_ctx = ssl_ctx;
73   conn->tls_ssl = ssl;
74   rs_free (ctx, tlsconf);
75   return RSE_OK;
76 }