/home/dko/projects/mobilec/tags/MobileC-v1.10.2/MobileC-v1.10.2/src/security/asm_node.c

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

Generated on Fri Jul 11 17:59:45 2008 for Mobile-C by  doxygen 1.5.4