]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/charon-tkm/src/tkm/tkm_nonceg.c
Merge branch 'tkm'
[thirdparty/strongswan.git] / src / charon-tkm / src / tkm / tkm_nonceg.c
1 /*
2 * Copyrigth (C) 2012 Reto Buerki
3 * Copyright (C) 2012 Adrian-Ken Rueegsegger
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <tkm/client.h>
18 #include <tkm/constants.h>
19
20 #include "tkm.h"
21 #include "tkm_nonceg.h"
22
23 typedef struct private_tkm_nonceg_t private_tkm_nonceg_t;
24
25 /**
26 * Private data of a tkm_nonceg_t object.
27 */
28 struct private_tkm_nonceg_t {
29
30 /**
31 * Public tkm_nonceg_t interface.
32 */
33 tkm_nonceg_t public;
34
35 /**
36 * Context id.
37 */
38 nc_id_type context_id;
39
40 };
41
42 METHOD(nonce_gen_t, get_nonce, bool,
43 private_tkm_nonceg_t *this, size_t size, u_int8_t *buffer)
44 {
45 nonce_type nonce;
46
47 if (ike_nc_create(this->context_id, size, &nonce) != TKM_OK)
48 {
49 return FALSE;
50 }
51
52 memcpy(buffer, &nonce.data, size);
53 return TRUE;
54 }
55
56 METHOD(nonce_gen_t, allocate_nonce, bool,
57 private_tkm_nonceg_t *this, size_t size, chunk_t *chunk)
58 {
59 *chunk = chunk_alloc(size);
60 if (get_nonce(this, chunk->len, chunk->ptr))
61 {
62 tkm->chunk_map->insert(tkm->chunk_map, chunk, this->context_id);
63 return TRUE;
64 }
65 return FALSE;
66 }
67
68 METHOD(nonce_gen_t, destroy, void,
69 private_tkm_nonceg_t *this)
70 {
71 free(this);
72 }
73
74 METHOD(tkm_nonceg_t, get_id, nc_id_type,
75 private_tkm_nonceg_t *this)
76 {
77 return this->context_id;
78 }
79
80 /*
81 * Described in header.
82 */
83 tkm_nonceg_t *tkm_nonceg_create()
84 {
85 private_tkm_nonceg_t *this;
86
87 INIT(this,
88 .public = {
89 .nonce_gen = {
90 .get_nonce = _get_nonce,
91 .allocate_nonce = _allocate_nonce,
92 .destroy = _destroy,
93 },
94 .get_id = _get_id,
95 },
96 .context_id = tkm->idmgr->acquire_id(tkm->idmgr, TKM_CTX_NONCE),
97 );
98
99 if (!this->context_id)
100 {
101 free(this);
102 return NULL;
103 }
104
105 return &this->public;
106 }