Use a compiler to marshall/unmarshall the sessions
[mod_auth_gssapi.git] / src / sessions.c
1 /* Copyright (C) 2014 mod_auth_gssapi authors - See COPYING for (C) terms */
2
3 #include "mod_auth_gssapi.h"
4 #include "asn1c/GSSSessionData.h"
5
6 APLOG_USE_MODULE(auth_gssapi);
7
8 static APR_OPTIONAL_FN_TYPE(ap_session_load) *mag_sess_load_fn = NULL;
9 static APR_OPTIONAL_FN_TYPE(ap_session_get) *mag_sess_get_fn = NULL;
10 static APR_OPTIONAL_FN_TYPE(ap_session_set) *mag_sess_set_fn = NULL;
11
12 void mag_post_config_session(void)
13 {
14     mag_sess_load_fn = APR_RETRIEVE_OPTIONAL_FN(ap_session_load);
15     mag_sess_get_fn = APR_RETRIEVE_OPTIONAL_FN(ap_session_get);
16     mag_sess_set_fn = APR_RETRIEVE_OPTIONAL_FN(ap_session_set);
17 }
18
19 static apr_status_t mag_session_load(request_rec *req, session_rec **sess)
20 {
21     if (mag_sess_load_fn) {
22         return mag_sess_load_fn(req, sess);
23     }
24     return DECLINED;
25 }
26
27 static apr_status_t mag_session_get(request_rec *req, session_rec *sess,
28                                     const char *key, const char **value)
29 {
30     if (mag_sess_get_fn) {
31         return mag_sess_get_fn(req, sess, key, value);
32     }
33     return DECLINED;
34 }
35
36 static apr_status_t mag_session_set(request_rec *req, session_rec *sess,
37                                     const char *key, const char *value)
38 {
39     if (mag_sess_set_fn) {
40         return mag_sess_set_fn(req, sess, key, value);
41     }
42     return DECLINED;
43 }
44
45 static bool encode_GSSSessionData(apr_pool_t *mempool,
46                                   GSSSessionData_t *gsessdata,
47                                   unsigned char **buf, int *len)
48 {
49     asn_enc_rval_t rval;
50     unsigned char *buffer = NULL;
51     size_t buflen;
52     bool ret = false;
53
54     /* dry run to compute the size */
55     rval = der_encode(&asn_DEF_GSSSessionData, gsessdata, NULL, NULL);
56     if (rval.encoded == -1) goto done;
57
58     buflen = rval.encoded;
59     buffer = apr_pcalloc(mempool, buflen);
60
61     /* now for real */
62     rval = der_encode_to_buffer(&asn_DEF_GSSSessionData,
63                                 gsessdata, buffer, buflen);
64     if (rval.encoded == -1) goto done;
65
66     *buf = buffer;
67     *len = buflen;
68     ret = true;
69
70 done:
71     return ret;
72 }
73
74 static GSSSessionData_t *decode_GSSSessionData(void *buf, size_t len)
75 {
76     GSSSessionData_t *gsessdata = NULL;
77     asn_dec_rval_t rval;
78
79     rval = ber_decode(NULL, &asn_DEF_GSSSessionData,
80                       (void **)&gsessdata, buf, len);
81     if (rval.code == RC_OK) {
82         return gsessdata;
83     }
84     return NULL;
85 }
86
87 #define MAG_BEARER_KEY "MagBearerToken"
88
89 void mag_check_session(request_rec *req,
90                        struct mag_config *cfg, struct mag_conn **conn)
91 {
92     struct mag_conn *mc;
93     apr_status_t rc;
94     session_rec *sess = NULL;
95     const char *sessval = NULL;
96     int declen;
97     struct databuf ctxbuf = { 0 };
98     struct databuf cipherbuf = { 0 };
99     GSSSessionData_t *gsessdata;
100     time_t expiration;
101
102     rc = mag_session_load(req, &sess);
103     if (rc != OK || sess == NULL) {
104         ap_log_rerror(APLOG_MARK, APLOG_INFO|APLOG_NOERRNO, 0, req,
105                       "Sessions not available, no cookies!");
106         return;
107     }
108
109     mc = *conn;
110     if (!mc) {
111         mc = apr_pcalloc(req->pool, sizeof(struct mag_conn));
112         if (!mc) return;
113
114         mc->parent = req->pool;
115         *conn = mc;
116     }
117
118     rc = mag_session_get(req, sess, MAG_BEARER_KEY, &sessval);
119     if (rc != OK) {
120         ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, req,
121                       "Failed to get session data!");
122         return;
123     }
124     if (!sessval) {
125         /* no session established, just return */
126         return;
127     }
128
129     if (!cfg->mag_skey) {
130         ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, req,
131                       "Session key not available, no cookies!");
132         /* we do not have a key, just return */
133         return;
134     }
135
136     /* decode it */
137     declen = apr_base64_decode_len(sessval);
138     cipherbuf.value = apr_palloc(req->pool, declen);
139     if (!cipherbuf.value) return;
140     cipherbuf.length = (int)apr_base64_decode((char *)cipherbuf.value, sessval);
141
142     rc = UNSEAL_BUFFER(req->pool, cfg->mag_skey, &cipherbuf, &ctxbuf);
143     if (rc != OK) {
144         ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, req,
145                       "Failed to unseal session data!");
146         return;
147     }
148
149     gsessdata = decode_GSSSessionData(ctxbuf.value, ctxbuf.length);
150     if (!gsessdata) {
151         ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, req,
152                       "Failed to unpack session data!");
153         return;
154     }
155
156     /* get time */
157     expiration = gsessdata->expiration;
158     if (expiration < time(NULL)) {
159         /* credentials fully expired, return nothing */
160         goto done;
161     }
162
163     /* user name */
164     mc->user_name = apr_pstrndup(mc->parent,
165                                  (char *)gsessdata->username.buf,
166                                  gsessdata->username.size);
167     if (!mc->user_name) goto done;
168
169     /* gssapi name */
170     mc->gss_name = apr_pstrndup(mc->parent,
171                                 (char *)gsessdata->gssname.buf,
172                                 gsessdata->gssname.size);
173     if (!mc->gss_name) goto done;
174
175     /* OK we have a valid token */
176     mc->established = true;
177
178 done:
179     ASN_STRUCT_FREE(asn_DEF_GSSSessionData, gsessdata);
180 }
181
182 void mag_attempt_session(request_rec *req,
183                          struct mag_config *cfg, struct mag_conn *mc)
184 {
185     session_rec *sess = NULL;
186     struct databuf plainbuf = { 0 };
187     struct databuf cipherbuf = { 0 };
188     struct databuf ctxbuf = { 0 };
189     GSSSessionData_t gsessdata = { 0 };
190     apr_status_t rc;
191     bool ret;
192
193     /* we save the session only if the authentication is established */
194
195     if (!mc->established) return;
196     rc = mag_session_load(req, &sess);
197     if (rc != OK || sess == NULL) {
198         ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, req,
199                       "Sessions not available, can't send cookies!");
200         return;
201     }
202
203     if (!cfg->mag_skey) {
204         ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, req,
205                       "Session key not available, generating new one.");
206         rc = SEAL_KEY_CREATE(cfg->pool, &cfg->mag_skey, NULL);
207         if (rc != OK) {
208             ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, req,
209                           "Failed to create sealing key!");
210             return;
211         }
212     }
213
214     gsessdata.expiration = mc->expiration;
215     if (OCTET_STRING_fromString(&gsessdata.username, mc->user_name) != 0)
216         goto done;
217     if (OCTET_STRING_fromString(&gsessdata.gssname, mc->gss_name) != 0)
218         goto done;
219     ret = encode_GSSSessionData(req->pool, &gsessdata,
220                                 &plainbuf.value, &plainbuf.length);
221     if (ret == false) {
222         ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, req,
223                       "Failed to pack session data!");
224         goto done;
225     }
226
227     rc = SEAL_BUFFER(req->pool, cfg->mag_skey, &plainbuf, &cipherbuf);
228     if (rc != OK) {
229         ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, req,
230                       "Failed to seal session data!");
231         goto done;
232     }
233
234     ctxbuf.length = apr_base64_encode_len(cipherbuf.length);
235     ctxbuf.value = apr_pcalloc(req->pool, ctxbuf.length);
236     if (!ctxbuf.value) goto done;
237
238     ctxbuf.length = apr_base64_encode((char *)ctxbuf.value,
239                                       (char *)cipherbuf.value,
240                                       cipherbuf.length);
241
242     rc = mag_session_set(req, sess, MAG_BEARER_KEY, (char *)ctxbuf.value);
243     if (rc != OK) {
244         ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, req,
245                       "Failed to set session data!");
246     }
247
248 done:
249     ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_GSSSessionData, &gsessdata);
250 }
251