Refactor: tr_compute_dh_key manages its own buffer
authorSam Hartman <hartmans@debian.org>
Wed, 10 Apr 2013 19:27:05 +0000 (15:27 -0400)
committerSam Hartman <hartmans@debian.org>
Wed, 10 Apr 2013 19:27:05 +0000 (15:27 -0400)
common/dh_test/dh_test.c
common/tr_dh.c
include/tr_dh.h
tid/example/tids_main.c
tid/tidc.c

index a0d0a3d..875207b 100644 (file)
@@ -88,24 +88,14 @@ int main (int argc,
   /*** Would now send server's pub key to client ***/
 
   /* Compute key on client */
-  if (NULL == (c_keybuf = malloc(DH_size(c_dh)))) {
-    printf ("Error: Can't allocate client keybuf, exiting.\n");
-    exit(1);
-  }
-  if (0 > (c_keylen = tr_compute_dh_key(c_keybuf, 
-                                     DH_size(c_dh), 
+  if (0 > (c_keylen = tr_compute_dh_key(&c_keybuf, 
                                      s_dh->pub_key, 
                                      c_dh))) {
     
   }
   
   /* Compute key on server */
-  if (NULL == (s_keybuf = malloc(DH_size(s_dh)))) {
-    printf ("Error: Can't allocate server keybuf, exiting.\n");
-    exit(1);
-  }
-  if (0 > (s_keylen = tr_compute_dh_key(s_keybuf, 
-                                     DH_size(s_dh), 
+  if (0 > (s_keylen = tr_compute_dh_key(&s_keybuf, 
                                      c_dh->pub_key, 
                                      s_dh))) {
     printf("Error: Can't compute server key.\n");
index ec887db..f32c613 100644 (file)
@@ -163,22 +163,34 @@ void tr_destroy_dh_params(DH *dh) {
   }
 }
 
-int tr_compute_dh_key(unsigned char *buf, 
-                     size_t buflen, 
+int tr_compute_dh_key(unsigned char **pbuf, 
                      BIGNUM *pub_key, 
                      DH *priv_dh) {
-
+  size_t buflen;
+  unsigned char *buf = NULL;;
   int rc = 0;
-
+  
   if ((!buf) || 
       (!pub_key) || 
-      (!priv_dh) ||
-      (buflen < DH_size(priv_dh))) {
+      (!priv_dh)) {
     fprintf(stderr, "tr_compute_dh_key(): Invalid parameters.\n");
     return(-1);
   }
+  *pbuf = NULL;
+  buflen = DH_size(priv_dh);
+  buf = malloc(buflen);
+  if (buf == NULL) {
+    fprintf(stderr, "out of memory\n");
+    return -1;
+  }
 
+  
   rc = DH_compute_key(buf, pub_key, priv_dh);
+  if (0 <= rc) {
+    *pbuf = buf;
+  }else {
+    free(buf);
+  }
   return rc;
 }
 
index dd4d711..8965867 100644 (file)
@@ -42,7 +42,7 @@
 TR_EXPORT DH *tr_create_dh_params(unsigned char *key, size_t len);
 TR_EXPORT DH *tr_create_matching_dh(unsigned char *key, size_t len, DH *in_dh);
 TR_EXPORT void tr_destroy_dh_params(DH *dh);
-TR_EXPORT int tr_compute_dh_key(unsigned char *buf, size_t buflen, BIGNUM *pub_key, DH *priv_dh);
+TR_EXPORT int tr_compute_dh_key(unsigned char **pbuf,  BIGNUM *pub_key, DH *priv_dh);
 
 
 TR_EXPORT void tr_bin_to_hex(const unsigned char * bin, size_t binlen,
index 9a78ef3..dbd56ea 100644 (file)
@@ -123,13 +123,8 @@ static int tids_req_handler (TIDS_INSTANCE * tids,
 
   /* Generate the server key */
   printf("Generating the server key.\n");
-  if (NULL == (s_keybuf = malloc(DH_size((*resp)->servers->aaa_server_dh)))) {
-    printf ("tids_req_handler(): Can't allocate server keybuf.\n");
-    return -1;
-  }
 
-  if (0 > (s_keylen = tr_compute_dh_key(s_keybuf, 
-                                       DH_size((*resp)->servers->aaa_server_dh), 
+  if (0 > (s_keylen = tr_compute_dh_key(&s_keybuf, 
                                        req->tidc_dh->pub_key, 
                                        (*resp)->servers->aaa_server_dh))) {
     printf("tids_req_handler(): Key computation failed.");
index cf87bbc..19ff353 100644 (file)
@@ -195,12 +195,7 @@ int tidc_send_request (TIDC_INSTANCE *tidc,
     return -1;
   }
   
-  if (NULL == (c_keybuf = malloc(DH_size(tid_req->tidc_dh)))) {
-    fprintf (stderr, "Error: Can't allocate client keybuf, exiting.\n");
-    return -1;
-  }
-  if (0 > (c_keylen = tr_compute_dh_key(c_keybuf, 
-                                     DH_size(tid_req->tidc_dh), 
+  if (0 > (c_keylen = tr_compute_dh_key(&c_keybuf, 
                                      resp_msg->tid_resp->servers->aaa_server_dh->pub_key, 
                                      tid_req->tidc_dh))) {