ses_prv.h

00001 /*
00002  * Copyright (c) 2005-2012 by KoanLogic s.r.l. <http://www.koanlogic.com>
00003  * All rights reserved.
00004  *
00005  * This file is part of KLone, and as such it is subject to the license stated
00006  * in the LICENSE file which you have received as part of this distribution.
00007  *
00008  * $Id: ses_prv.h,v 1.19 2009/05/31 18:50:27 tho Exp $
00009  */
00010 
00011 #ifndef _KLONE_SESPRV_H_
00012 #define _KLONE_SESPRV_H_
00013 
00014 #include "klone_conf.h"
00015 #ifdef SSL_OPENSSL
00016 #include <openssl/hmac.h>
00017 #include <openssl/evp.h>
00018 #include <openssl/rand.h>
00019 #endif
00020 #include <u/libu.h>
00021 #include <klone/session.h>
00022 #include <klone/request.h>
00023 #include <klone/response.h>
00024 #include <klone/vars.h>
00025 #include <klone/http.h>
00026 #include <klone/atom.h>
00027 #include <klone/md5.h>
00028 #ifdef SSL_CYASSL
00029 #include <config.h>
00030 #include <types.h>
00031 #include <ctc_hmac.h>
00032 #include <openssl/evp.h>
00033 #endif
00034 
00035 #ifdef __cplusplus
00036 extern "C" {
00037 #endif
00038 
00039 #define SESSION_KEY_VAR      "KLONE_CIPHER_KEY"
00040 
00041 typedef int (*session_load_t)(session_t*);
00042 typedef int (*session_save_t)(session_t*);
00043 typedef int (*session_remove_t)(session_t*);
00044 typedef int (*session_term_t)(session_t*);
00045 
00046 /* session type */
00047 enum { 
00048     SESSION_TYPE_UNKNOWN, 
00049     SESSION_TYPE_FILE, 
00050     SESSION_TYPE_MEMORY, 
00051     SESSION_TYPE_CLIENT
00052 };
00053 
00054 enum { 
00055     SESSION_ID_LENGTH = MD5_DIGEST_LEN,         /* sid length       */
00056     SESSION_ID_BUFSZ = 1 + SESSION_ID_LENGTH    /* sid buffer size  */
00057 };
00058 
00059 /* hmac and cipher key size */
00060 enum { 
00061     HMAC_KEY_LEN = 64, 
00062     #ifdef SSL_OPENSSL
00063     CIPHER_KEY_LEN = EVP_MAX_KEY_LENGTH, 
00064     CIPHER_KEY_BUFSZ = 2* EVP_MAX_KEY_LENGTH,  /* key + padding */
00065     CIPHER_IV_LEN = EVP_MAX_IV_LENGTH
00066     #else
00067     CIPHER_KEY_LEN = 32, CIPHER_KEY_BUFSZ = 64, CIPHER_IV_LEN = 16
00068     #endif
00069  };
00070 
00071 #ifdef SSL_CYASSL
00072 typedef Hmac HMAC_CTX;
00073 #endif
00074 
00075 /* session runtime parameters */
00076 typedef struct session_opt_s
00077 {
00078     /* common session options */
00079     int type;       /* type of sessions (file, memory, client-side)  */
00080     int max_age;    /* max allowed age of sessions                   */
00081     int encrypt;    /* >0 when client-side session encryption is on  */
00082     int compress;   /* >0 when client-side session compression is on */
00083     char name[128]; /* cookie name                                   */
00084 
00085     /* file session options/struct                                   */
00086     char path[U_FILENAME_MAX]; /* session save path                  */
00087     unsigned char session_key[CIPHER_KEY_BUFSZ]; /* session secret key */
00088     unsigned char session_iv[CIPHER_IV_LEN];   /* session init vect */
00089 
00090     /* in-memory session options/struct                              */
00091     atoms_t *atoms; /* atom list used to store in-memory sessions    */
00092     size_t max_count;   /* max # of in-memory sessions               */
00093     size_t mem_limit;   /* max (total) size of in-memory sessions    */
00094 
00095     #ifdef SSL_ON
00096     char keyvar[128]; /* name of the session variable w/ the description key */
00097     const EVP_CIPHER *cipher; /* encryption cipher algorithm         */
00098     unsigned char cipher_key[CIPHER_KEY_BUFSZ]; /* cipher secret key  */
00099     unsigned char cipher_iv[CIPHER_IV_LEN];   /* cipher Init Vector */
00100     /* client-side options/structs                                   */
00101     HMAC_CTX hmac_ctx;  /* openssl HMAC context                      */
00102     const EVP_MD *hash; /* client-side session HMAC hash algorithm   */
00103     char hmac_key[HMAC_KEY_LEN]; /* session HMAC secret key         */
00104     #endif
00105 } session_opt_t;
00106 
00107 struct session_s
00108 {
00109     vars_t *vars;               /* variable list                              */
00110     request_t *rq;              /* request bound to this session              */
00111     response_t *rs;             /* response bound to this session             */
00112     char filename[U_FILENAME_MAX];/* session filename                         */
00113     char id[SESSION_ID_BUFSZ];  /* session ID                                 */
00114     int removed;                /* >0 if the calling session has been deleted */
00115     int mtime;                  /* last modified time                         */
00116     session_load_t load;        /* ptr to the driver load function            */
00117     session_save_t save;        /* ptr to the driver save function            */
00118     session_remove_t remove;    /* ptr to the driver remove function          */
00119     session_term_t term;        /* ptr to the driver term function            */
00120     session_opt_t *so;          /* runtime option                             */
00121 };
00122 
00123 /* main c'tor */
00124 int session_create(session_opt_t*, request_t*, response_t*, session_t**);
00125 
00126 /* driver c'tor */
00127 int session_client_create(session_opt_t*, request_t*, response_t*, session_t**);
00128 int session_file_create(session_opt_t*, request_t*, response_t*, session_t**);
00129 int session_mem_create(session_opt_t*, request_t*, response_t*, session_t**);
00130 
00131 /* private functions */
00132 int session_prv_init(session_t *, request_t *, response_t *);
00133 int session_prv_load_from_io(session_t *, io_t *);
00134 int session_prv_save_to_io(session_t*, io_t *);
00135 int session_prv_save_var(var_t *, void*);
00136 int session_prv_calc_maxsize(var_t *v, void *p);
00137 int session_prv_save_to_buf(session_t *ss, char **pbuf, size_t *psz);
00138 int session_prv_load_from_buf(session_t *ss, char *buf, size_t size);
00139 int session_prv_set_id(session_t *ss, const char *sid);
00140 int session_priv_set_id(session_t *ss, const char *sid); /* backward comp. */
00141 
00142 /* init/term funcs */
00143 int session_module_init(u_config_t *config, session_opt_t **pso);
00144 int session_file_module_init(u_config_t *config, session_opt_t *pso);
00145 int session_mem_module_init(u_config_t *config, session_opt_t *pso);
00146 int session_client_module_init(u_config_t *config, session_opt_t *pso);
00147 int session_module_term(session_opt_t *so);
00148 int session_module_term(session_opt_t *so);
00149 
00150 #ifdef __cplusplus
00151 }
00152 #endif 
00153 
00154 #endif

←Products
Copyright © 2005-2012 - KoanLogic S.r.l. - All rights reserved