]> git.ipfire.org Git - thirdparty/strongswan.git/blobdiff - src/charon-tkm/src/tkm/tkm_nonceg.c
Fixed some typos, courtesy of codespell
[thirdparty/strongswan.git] / src / charon-tkm / src / tkm / tkm_nonceg.c
index 9ac32a7c7f61bbbe09d71d879b53486cc93af4b3..2b3e66d2deb422a1a67e267a42b56cd604733c61 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyrigth (C) 2012 Reto Buerki
+ * Copyright (C) 2012 Reto Buerki
  * Copyright (C) 2012 Adrian-Ken Rueegsegger
  * Hochschule fuer Technik Rapperswil
  *
  * for more details.
  */
 
-#include "tkm_nonceg.h"
-
 #include <tkm/client.h>
 #include <tkm/constants.h>
 
+#include "tkm.h"
+#include "tkm_nonceg.h"
+
 typedef struct private_tkm_nonceg_t private_tkm_nonceg_t;
 
 /**
@@ -31,21 +32,33 @@ struct private_tkm_nonceg_t {
         */
        tkm_nonceg_t public;
 
+       /**
+        * Nonce chunk.
+        */
+       chunk_t nonce;
 };
 
 METHOD(nonce_gen_t, get_nonce, bool,
-       private_tkm_nonceg_t *this, size_t size, u_int8_t *buffer)
+       private_tkm_nonceg_t *this, size_t size, uint8_t *buffer)
 {
        nonce_type nonce;
+       uint64_t nc_id;
+
+       nc_id = tkm->idmgr->acquire_id(tkm->idmgr, TKM_CTX_NONCE);
+       if (!nc_id)
+       {
+               return FALSE;
+       }
 
-       /* request nonce from TKM, the context is not yet used */
-       const result_type result = ike_nc_create(1, size, &nonce);
-       if (result != TKM_OK)
+       if (ike_nc_create(nc_id, size, &nonce) != TKM_OK)
        {
+               tkm->idmgr->release_id(tkm->idmgr, TKM_CTX_NONCE, nc_id);
                return FALSE;
        }
 
        memcpy(buffer, &nonce.data, size);
+       this->nonce = chunk_clone(chunk_create(buffer, size));
+       tkm->chunk_map->insert(tkm->chunk_map, &this->nonce, nc_id);
        return TRUE;
 }
 
@@ -59,6 +72,21 @@ METHOD(nonce_gen_t, allocate_nonce, bool,
 METHOD(nonce_gen_t, destroy, void,
        private_tkm_nonceg_t *this)
 {
+       uint64_t nc_id;
+
+       nc_id = tkm->chunk_map->get_id(tkm->chunk_map, &this->nonce);
+       if (nc_id)
+       {
+               DBG1(DBG_IKE, "resetting stale nonce context %llu", nc_id);
+
+               if (ike_nc_reset(nc_id) != TKM_OK)
+               {
+                       DBG1(DBG_IKE, "failed to reset nonce context %llu", nc_id);
+               }
+               tkm->idmgr->release_id(tkm->idmgr, TKM_CTX_NONCE, nc_id);
+               tkm->chunk_map->remove(tkm->chunk_map, &this->nonce);
+       }
+       chunk_free(&this->nonce);
        free(this);
 }