00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "CryptExternalMethods.hpp"
00021 #include "Utils.hpp"
00022 #include "string_t.hpp"
00023 #include <regex.h>
00024
00025 #define GEN_ERROR(l,a) iostream_error << a << iostream_endl << iostream_flush ;
00026
00027
00028
00029 extern char *stristr (const char *s1, const char *s2) ;
00030 extern int createAuthHeaderMD5(char * user, char * password, char * method,
00031 char * uri, char * msgbody, char * auth,
00032 char * algo, char * result);
00033 extern int createAuthHeaderAKAv1MD5(char * user, char * OP,
00034 char * AMF,
00035 char * K,
00036 char * method,
00037 char * uri, char * msgbody, char * auth, char *algo,
00038 char * result);
00039
00040
00041 char* external_find_text_value (char *P_buf, char *P_field) {
00042
00043 char *L_value = NULL ;
00044
00045 regex_t L_reg_expr ;
00046 int L_status ;
00047 char L_buffer[100];
00048 regmatch_t L_pmatch[3] ;
00049 size_t L_size = 0 ;
00050
00051 string_t L_string = "" ;
00052
00053 L_string = "([[:blank:]]*" ;
00054 L_string += P_field ;
00055 L_string += "[[:blank:]]*=[[:blank:]]*)([^;]+)";
00056
00057 L_status = regcomp (&L_reg_expr,
00058 L_string.c_str(),
00059 REG_EXTENDED) ;
00060
00061 if (L_status != 0) {
00062 regerror(L_status, &L_reg_expr, L_buffer, 100);
00063 regfree (&L_reg_expr) ;
00064 } else {
00065
00066 L_status = regexec (&L_reg_expr, P_buf, 3, L_pmatch, 0) ;
00067 regfree (&L_reg_expr) ;
00068 if (L_status == 0) {
00069 L_size = L_pmatch[2].rm_eo - L_pmatch[2].rm_so ;
00070 ALLOC_TABLE(L_value, char*, sizeof(char), L_size+1);
00071 memcpy(L_value, &(P_buf[L_pmatch[2].rm_so]), L_size);
00072 L_value[L_size]='\0' ;
00073 }
00074 }
00075 return (L_value);
00076 }
00077
00078 typedef struct _crypto_args_string {
00079 char * m_user;
00080 char * m_password;
00081 char * m_method;
00082 char * m_uri;
00083 char * m_auth;
00084 int m_algo_id;
00085 char * m_algo ;
00086 char * m_aka_k ;
00087 char * m_aka_op ;
00088 char * m_aka_amf ;
00089 } T_CryptoArgsStr, *T_pCryptoArgsStr ;
00090
00091
00092 int check_algorithm(char * auth) {
00093
00094 char algo[32]="MD5";
00095 char *start, *end;
00096
00097 if ((start = stristr(auth, "Digest")) == NULL) {
00098 return (-1);
00099 }
00100
00101 if ((start = stristr(auth, "algorithm=")) != NULL) {
00102 start = start + strlen("algorithm=");
00103 if (*start == '"') { start++; }
00104 end = start + strcspn(start, " ,\"\r\n");
00105 strncpy(algo, start, end - start);
00106 algo[end - start] ='\0';
00107 }
00108
00109 if (strncasecmp(algo, "MD5", 3)==0) {
00110 return (0);
00111 } else if (strncasecmp(algo, "AKAv1-MD5", 9)==0) {
00112 return (1);
00113 } else {
00114 return (-1) ;
00115 }
00116 }
00117
00118 int crypto_args_analysis (T_pValueData P_args, T_pCryptoArgsStr P_result) {
00119
00120 int L_ret = 0 ;
00121
00122 P_result->m_user = NULL ;
00123 P_result->m_password = NULL ;
00124 P_result->m_method = NULL ;
00125 P_result->m_uri = NULL ;
00126 P_result->m_auth = NULL ;
00127 P_result->m_algo_id = -1 ;
00128 P_result->m_algo = NULL ;
00129 P_result->m_aka_k = NULL ;
00130 P_result->m_aka_op = NULL ;
00131 P_result->m_aka_amf = NULL ;
00132
00133 P_result->m_user = external_find_text_value((char*)P_args->m_value.m_val_binary.m_value,
00134 (char*)"username") ;
00135 if (P_result->m_user == NULL ) {
00136 GEN_ERROR(E_GEN_FATAL_ERROR,
00137 "user no defined in format of the action: set-value format=\"username=.. ");
00138 L_ret = -1;
00139 return (L_ret);
00140 }
00141
00142 P_result->m_method = external_find_text_value((char*)P_args->m_value.m_val_binary.m_value,
00143 (char*)"method") ;
00144 if (P_result->m_method == NULL ) {
00145 GEN_ERROR(E_GEN_FATAL_ERROR,
00146 "method no defined in format of the action: set-value format=\"method=.. ");
00147 L_ret = -1;
00148 return (L_ret);
00149 }
00150
00151 P_result->m_uri = external_find_text_value((char*)P_args->m_value.m_val_binary.m_value,
00152 (char*)"uri") ;
00153 if (P_result->m_uri == NULL ) {
00154 GEN_ERROR(E_GEN_FATAL_ERROR,
00155 "uri no defined in format of the action: set-value format=\"uri=.. ");
00156 L_ret = -1;
00157 return (L_ret);
00158 }
00159
00160 P_result->m_auth = external_find_text_value((char*)P_args->m_value.m_val_binary.m_value,
00161 (char*)"auth") ;
00162 if (P_result->m_auth == NULL ) {
00163 GEN_ERROR(E_GEN_FATAL_ERROR,
00164 "auth no defined in format of the action: set-value format=\"auth=.. ");
00165 L_ret = -1;
00166 return (L_ret);
00167 }
00168
00169 P_result->m_algo_id = check_algorithm(P_result->m_auth);
00170 if (P_result->m_algo_id == -1 ) {
00171 GEN_ERROR(E_GEN_FATAL_ERROR,
00172 "algorithm not defined (MD5 or AKA)");
00173 L_ret = -1;
00174 return (L_ret);
00175 }
00176
00177
00178 if (P_result->m_algo_id == 0) {
00179
00180 ALLOC_TABLE(P_result->m_algo, char*, sizeof(char), 4);
00181 strcpy(P_result->m_algo, (char*)"MD5");
00182
00183 P_result->m_password = external_find_text_value((char*)P_args->m_value.m_val_binary.m_value,
00184 (char*)"password") ;
00185 if (P_result->m_password == NULL ) {
00186 GEN_ERROR(E_GEN_FATAL_ERROR,
00187 "password no defined in format of the action: set-value format=\"password=...");
00188 L_ret = -1;
00189 return (L_ret);
00190 }
00191
00192
00193 } else {
00194
00195 ALLOC_TABLE(P_result->m_algo, char*, sizeof(char), 10);
00196 strcpy(P_result->m_algo, (char*)"AKAv1-MD5");
00197
00198
00199 P_result->m_aka_op = external_find_text_value((char*)P_args->m_value.m_val_binary.m_value,
00200 (char*)"aka_op") ;
00201 if (P_result->m_aka_op == NULL ) {
00202 GEN_ERROR(E_GEN_FATAL_ERROR,
00203 "aka_op no defined in format of the action: set-value format=\"aka_op=...");
00204 L_ret = -1;
00205 return (L_ret);
00206 }
00207
00208 P_result->m_aka_amf = external_find_text_value((char*)P_args->m_value.m_val_binary.m_value,
00209 (char*)"aka_amf") ;
00210 if (P_result->m_aka_amf == NULL ) {
00211 GEN_ERROR(E_GEN_FATAL_ERROR,
00212 "aka_amf no defined in format of the action: set-value format=\"aka_amf=...");
00213 L_ret = -1;
00214 return (L_ret);
00215 }
00216
00217 P_result->m_aka_k = external_find_text_value((char*)P_args->m_value.m_val_binary.m_value,
00218 (char*)"aka_k") ;
00219 if (P_result->m_aka_k == NULL ) {
00220 GEN_ERROR(E_GEN_FATAL_ERROR,
00221 "aka_k no defined in format of the action: set-value format=\"aka_k=...");
00222 L_ret = -1;
00223 return (L_ret);
00224 }
00225
00226 }
00227
00228
00229 return (L_ret);
00230 }
00231
00232
00233
00234
00235 int crypto_method (T_pValueData P_msgPart,
00236 T_pValueData P_args,
00237 T_pValueData P_result) {
00238
00239 int L_ret = 0 ;
00240 T_CryptoArgsStr L_crypto ;
00241 char L_result [2049] ;
00242
00243
00244 L_ret = crypto_args_analysis(P_args, &L_crypto);
00245 if (L_ret != -1) {
00246 if (L_crypto.m_algo_id == 0) {
00247 L_ret = createAuthHeaderMD5(L_crypto.m_user,
00248 L_crypto.m_password,
00249 L_crypto.m_method,
00250 L_crypto.m_uri,
00251 (char*)P_msgPart->m_value.m_val_binary.m_value,
00252 L_crypto.m_auth,
00253 L_crypto.m_algo,
00254 L_result);
00255 } else {
00256 L_ret = createAuthHeaderAKAv1MD5(L_crypto.m_user,
00257 L_crypto.m_aka_op,
00258 L_crypto.m_aka_amf,
00259 L_crypto.m_aka_k,
00260 L_crypto.m_method,
00261 L_crypto.m_uri,
00262 (char*)P_msgPart->m_value.m_val_binary.m_value,
00263 L_crypto.m_auth,
00264 L_crypto.m_algo,
00265 L_result);
00266 }
00267 if (L_ret == 1) {
00268 P_result->m_type = E_TYPE_STRING ;
00269 ALLOC_TABLE(P_result->m_value.m_val_binary.m_value,
00270 unsigned char*,
00271 sizeof(unsigned char),
00272 strlen(L_result));
00273 P_result->m_value.m_val_binary.m_size = strlen(L_result);
00274 memcpy(P_result->m_value.m_val_binary.m_value, L_result, strlen(L_result));
00275 } else {
00276 L_ret = -1 ;
00277 }
00278 }
00279
00280 FREE_TABLE(L_crypto.m_user);
00281 FREE_TABLE(L_crypto.m_password);
00282 FREE_TABLE(L_crypto.m_method);
00283 FREE_TABLE(L_crypto.m_uri);
00284 FREE_TABLE(L_crypto.m_auth);
00285 FREE_TABLE(L_crypto.m_algo );
00286 FREE_TABLE(L_crypto.m_aka_k );
00287 FREE_TABLE(L_crypto.m_aka_op );
00288 FREE_TABLE(L_crypto.m_aka_amf );
00289
00290 return (L_ret);
00291 }
00292