1 /* Copyright (C) 2014 mod_auth_gssapi authors - See COPYING for (C) terms */
3 #include "mod_auth_gssapi.h"
4 #include "asn1c/GSSSessionData.h"
6 APLOG_USE_MODULE(auth_gssapi);
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;
12 void mag_post_config_session(void)
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);
19 static apr_status_t mag_session_load(request_rec *req, session_rec **sess)
21 if (mag_sess_load_fn) {
22 return mag_sess_load_fn(req, sess);
27 static apr_status_t mag_session_get(request_rec *req, session_rec *sess,
28 const char *key, const char **value)
30 if (mag_sess_get_fn) {
31 return mag_sess_get_fn(req, sess, key, value);
36 static apr_status_t mag_session_set(request_rec *req, session_rec *sess,
37 const char *key, const char *value)
39 if (mag_sess_set_fn) {
40 return mag_sess_set_fn(req, sess, key, value);
45 static bool encode_GSSSessionData(apr_pool_t *mempool,
46 GSSSessionData_t *gsessdata,
47 unsigned char **buf, int *len)
50 unsigned char *buffer = NULL;
54 /* dry run to compute the size */
55 rval = der_encode(&asn_DEF_GSSSessionData, gsessdata, NULL, NULL);
56 if (rval.encoded == -1) goto done;
58 buflen = rval.encoded;
59 buffer = apr_pcalloc(mempool, buflen);
62 rval = der_encode_to_buffer(&asn_DEF_GSSSessionData,
63 gsessdata, buffer, buflen);
64 if (rval.encoded == -1) goto done;
74 static GSSSessionData_t *decode_GSSSessionData(void *buf, size_t len)
76 GSSSessionData_t *gsessdata = NULL;
79 rval = ber_decode(NULL, &asn_DEF_GSSSessionData,
80 (void **)&gsessdata, buf, len);
81 if (rval.code == RC_OK) {
87 #define MAG_BEARER_KEY "MagBearerToken"
89 void mag_check_session(request_rec *req,
90 struct mag_config *cfg, struct mag_conn **conn)
94 session_rec *sess = NULL;
95 const char *sessval = NULL;
97 struct databuf ctxbuf = { 0 };
98 struct databuf cipherbuf = { 0 };
99 GSSSessionData_t *gsessdata;
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!");
111 mc = apr_pcalloc(req->pool, sizeof(struct mag_conn));
114 mc->parent = req->pool;
118 rc = mag_session_get(req, sess, MAG_BEARER_KEY, &sessval);
120 ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, req,
121 "Failed to get session data!");
125 /* no session established, just return */
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 */
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);
142 rc = UNSEAL_BUFFER(req->pool, cfg->mag_skey, &cipherbuf, &ctxbuf);
144 ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, req,
145 "Failed to unseal session data!");
149 gsessdata = decode_GSSSessionData(ctxbuf.value, ctxbuf.length);
151 ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, req,
152 "Failed to unpack session data!");
157 if (gsessdata->established != 0) mc->established = true;
158 if (gsessdata->delegated != 0) mc->delegated = true;
161 expiration = gsessdata->expiration;
162 if (expiration < time(NULL)) {
163 /* credentials fully expired, return nothing */
168 mc->user_name = apr_pstrndup(mc->parent,
169 (char *)gsessdata->username.buf,
170 gsessdata->username.size);
171 if (!mc->user_name) goto done;
174 mc->gss_name = apr_pstrndup(mc->parent,
175 (char *)gsessdata->gssname.buf,
176 gsessdata->gssname.size);
177 if (!mc->gss_name) goto done;
179 /* OK we have a valid token */
180 mc->established = true;
183 ASN_STRUCT_FREE(asn_DEF_GSSSessionData, gsessdata);
186 void mag_attempt_session(request_rec *req,
187 struct mag_config *cfg, struct mag_conn *mc)
189 session_rec *sess = NULL;
190 struct databuf plainbuf = { 0 };
191 struct databuf cipherbuf = { 0 };
192 struct databuf ctxbuf = { 0 };
193 GSSSessionData_t gsessdata = { 0 };
197 /* we save the session only if the authentication is established */
199 if (!mc->established) return;
200 rc = mag_session_load(req, &sess);
201 if (rc != OK || sess == NULL) {
202 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, req,
203 "Sessions not available, can't send cookies!");
207 if (!cfg->mag_skey) {
208 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, req,
209 "Session key not available, generating new one.");
210 rc = SEAL_KEY_CREATE(cfg->pool, &cfg->mag_skey, NULL);
212 ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, req,
213 "Failed to create sealing key!");
218 gsessdata.established = mc->established?1:0;
219 gsessdata.delegated = mc->delegated?1:0;
220 gsessdata.expiration = mc->expiration;
221 if (OCTET_STRING_fromString(&gsessdata.username, mc->user_name) != 0)
223 if (OCTET_STRING_fromString(&gsessdata.gssname, mc->gss_name) != 0)
225 ret = encode_GSSSessionData(req->pool, &gsessdata,
226 &plainbuf.value, &plainbuf.length);
228 ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, req,
229 "Failed to pack session data!");
233 rc = SEAL_BUFFER(req->pool, cfg->mag_skey, &plainbuf, &cipherbuf);
235 ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, req,
236 "Failed to seal session data!");
240 ctxbuf.length = apr_base64_encode_len(cipherbuf.length);
241 ctxbuf.value = apr_pcalloc(req->pool, ctxbuf.length);
242 if (!ctxbuf.value) goto done;
244 ctxbuf.length = apr_base64_encode((char *)ctxbuf.value,
245 (char *)cipherbuf.value,
248 rc = mag_session_set(req, sess, MAG_BEARER_KEY, (char *)ctxbuf.value);
250 ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, req,
251 "Failed to set session data!");
255 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_GSSSessionData, &gsessdata);