]>
Commit | Line | Data |
---|---|---|
559fe48c | 1 | /* |
2db6d5b8 | 2 | * Copyright (C) 2012 Reto Buerki |
559fe48c | 3 | * Copyright (C) 2012 Adrian-Ken Rueegsegger |
1b671669 | 4 | * HSR Hochschule fuer Technik Rapperswil |
559fe48c RB |
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 | ||
8e95bf45 RB |
17 | #include <tkm/client.h> |
18 | #include <tkm/constants.h> | |
19 | ||
c38459d7 RB |
20 | #include "tkm.h" |
21 | #include "tkm_nonceg.h" | |
22 | ||
559fe48c RB |
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 | ||
c38459d7 | 35 | /** |
8cdc5632 | 36 | * Nonce chunk. |
c38459d7 | 37 | */ |
8cdc5632 | 38 | chunk_t nonce; |
559fe48c RB |
39 | }; |
40 | ||
41 | METHOD(nonce_gen_t, get_nonce, bool, | |
b12c53ce | 42 | private_tkm_nonceg_t *this, size_t size, uint8_t *buffer) |
559fe48c | 43 | { |
8e95bf45 | 44 | nonce_type nonce; |
8cdc5632 | 45 | uint64_t nc_id; |
7cc6fa1a | 46 | |
8cdc5632 RB |
47 | nc_id = tkm->idmgr->acquire_id(tkm->idmgr, TKM_CTX_NONCE); |
48 | if (!nc_id) | |
8e95bf45 RB |
49 | { |
50 | return FALSE; | |
51 | } | |
52 | ||
8cdc5632 RB |
53 | if (ike_nc_create(nc_id, size, &nonce) != TKM_OK) |
54 | { | |
55 | tkm->idmgr->release_id(tkm->idmgr, TKM_CTX_NONCE, nc_id); | |
56 | return FALSE; | |
57 | } | |
58 | ||
8e95bf45 | 59 | memcpy(buffer, &nonce.data, size); |
8cdc5632 RB |
60 | this->nonce = chunk_clone(chunk_create(buffer, size)); |
61 | tkm->chunk_map->insert(tkm->chunk_map, &this->nonce, nc_id); | |
559fe48c RB |
62 | return TRUE; |
63 | } | |
64 | ||
65 | METHOD(nonce_gen_t, allocate_nonce, bool, | |
66 | private_tkm_nonceg_t *this, size_t size, chunk_t *chunk) | |
67 | { | |
68 | *chunk = chunk_alloc(size); | |
8cdc5632 | 69 | return get_nonce(this, chunk->len, chunk->ptr); |
559fe48c RB |
70 | } |
71 | ||
72 | METHOD(nonce_gen_t, destroy, void, | |
73 | private_tkm_nonceg_t *this) | |
74 | { | |
8cdc5632 RB |
75 | uint64_t nc_id; |
76 | ||
77 | nc_id = tkm->chunk_map->get_id(tkm->chunk_map, &this->nonce); | |
78 | if (nc_id) | |
79 | { | |
80 | DBG1(DBG_IKE, "resetting stale nonce context %llu", nc_id); | |
81 | ||
82 | if (ike_nc_reset(nc_id) != TKM_OK) | |
83 | { | |
84 | DBG1(DBG_IKE, "failed to reset nonce context %llu", nc_id); | |
85 | } | |
86 | tkm->idmgr->release_id(tkm->idmgr, TKM_CTX_NONCE, nc_id); | |
87 | tkm->chunk_map->remove(tkm->chunk_map, &this->nonce); | |
88 | } | |
89 | chunk_free(&this->nonce); | |
559fe48c RB |
90 | free(this); |
91 | } | |
92 | ||
93 | /* | |
94 | * Described in header. | |
95 | */ | |
96 | tkm_nonceg_t *tkm_nonceg_create() | |
97 | { | |
98 | private_tkm_nonceg_t *this; | |
99 | ||
100 | INIT(this, | |
101 | .public = { | |
102 | .nonce_gen = { | |
103 | .get_nonce = _get_nonce, | |
104 | .allocate_nonce = _allocate_nonce, | |
105 | .destroy = _destroy, | |
106 | }, | |
107 | }, | |
108 | ); | |
109 | ||
110 | return &this->public; | |
111 | } |