/home/dko/projects/mobilec/trunk/src/security/asm_node.c

Go to the documentation of this file.
00001 #ifndef _WIN32
00002 #include <netdb.h>
00003 #include "config.h"
00004 #else
00005 #include <windows.h>
00006 #include "../winconfig.h"
00007 #endif
00008 #include "asm.h"
00009 #include "asm_message_parser.h"
00010 #include "asm_node.h"
00011 #include "xyssl-0.7/include/xyssl/havege.h"
00012 #include "xyssl-0.7/include/xyssl/bignum.h"
00013   
00014 #ifdef MC_SECURITY
00015 
00016   int
00017 asm_node_Destroy(asm_node_p asm_node)
00018 {
00019   if (asm_node == NULL) {
00020     return MC_SUCCESS;
00021   }
00022 
00023   if (asm_node->xml_encrypt_root != NULL) {
00024     mxmlDelete(asm_node->xml_encrypt_root);
00025   }
00026 
00027   if (asm_node->remote_addr != NULL) {
00028     free(asm_node->remote_addr);
00029   }
00030 
00031   if (asm_node->data.dh_data) {
00032     dh_data_Destroy(asm_node->data.dh_data);
00033   }
00034 
00035   free(asm_node);
00036   return MC_SUCCESS;
00037 }
00038 
00039   asm_node_p
00040 asm_node_Initialize(message_p message, mc_asm_p security_manager)
00041 {
00042   int n = 0;
00043   unsigned char *buf;
00044   char* hostname;
00045   char* port_str;
00046   int port;
00047 #ifndef _WIN32
00048   char* save_ptr; /* For re-entrant strtok */
00049 #endif
00050   asm_node_p asm_node;
00051   havege_state hs;
00052   havege_init(&hs);
00053   buf = (unsigned char*)malloc(sizeof(char) * 1024);
00054   CHECK_NULL(buf, exit(0););
00055   asm_node = (asm_node_p)malloc(sizeof(asm_node_t));
00056   CHECK_NULL(asm_node, exit(0););
00057   memset(asm_node, 0, sizeof(asm_node_t));
00058   asm_node->data.dh_data = (dh_data_p)malloc(sizeof(dh_data_t));
00059   CHECK_NULL(asm_node->data.dh_data, exit(0););
00060   memset(asm_node->data.dh_data, 0, sizeof(dh_data_t));
00061   asm_node->remote_addr = (struct sockaddr_in*)malloc
00062     (
00063      sizeof(struct sockaddr_in)
00064     );
00065   CHECK_NULL(asm_node->remote_addr, exit(0););
00066   hostname = strtok_r
00067     (
00068      message->from_address,
00069      ":",
00070      &save_ptr
00071     );
00072   port_str = strtok_r
00073     (
00074      NULL,
00075      ":",
00076      &save_ptr
00077     );
00078   port = atoi(port_str);
00079 
00080   *(asm_node->remote_addr) = *(message->addr);
00081   asm_node->remote_addr->sin_port = htons(port);
00082   asm_node->xml_encrypt_root = message->xml_payload;
00083   if(asm_message_parse(asm_node)) {
00084     fprintf(stderr, "ASM Parse Error %s:%d\n", __FILE__, __LINE__);
00085     goto err_cleanup;
00086   } else {
00087     /* At this point all the dh data should be initialized. Here, 
00088      * we will determine the secret shared key, and set up the aes. */
00089     if
00090       (
00091        mpi_copy
00092        (
00093         &(asm_node->data.dh_data->dhm.X),
00094         &(security_manager->home_encryption_info->data.dh_data->dhm.X)
00095        )
00096       )
00097       {
00098         fprintf(stderr, "Error. %s:%d\n", __FILE__,__LINE__);
00099       }
00100 
00101     if 
00102       ( 
00103        dhm_calc_secret
00104        (
00105         &(asm_node->data.dh_data->dhm),
00106         buf,
00107         &n
00108        )
00109       )
00110       {
00111         fprintf(stderr, "Error. %s:%d\n", __FILE__, __LINE__);
00112         goto err_cleanup;
00113       }
00114     aes_set_key
00115       (
00116        &(asm_node->data.dh_data->aes),
00117        buf,
00118        256
00119       );
00120     free(buf);
00121     return asm_node;
00122   }
00123 err_cleanup:
00124   free(asm_node);
00125   free(buf);
00126   return NULL;
00127 }
00128 
00129 #endif /*MC_SECURITY*/

Generated on Fri May 16 14:49:55 2008 for Mobile-C by  doxygen 1.5.4