]> git.ipfire.org Git - thirdparty/strongswan.git/blame - src/charon-tkm/src/tkm/tkm_nonceg.c
nonceg: Insert id mapping when allocating nonce
[thirdparty/strongswan.git] / src / charon-tkm / src / tkm / tkm_nonceg.c
CommitLineData
559fe48c
RB
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
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
23typedef struct private_tkm_nonceg_t private_tkm_nonceg_t;
24
25/**
26 * Private data of a tkm_nonceg_t object.
27 */
28struct private_tkm_nonceg_t {
29
30 /**
31 * Public tkm_nonceg_t interface.
32 */
33 tkm_nonceg_t public;
34
c38459d7
RB
35 /**
36 * Context id.
37 */
38 nc_id_type context_id;
39
559fe48c
RB
40};
41
42METHOD(nonce_gen_t, get_nonce, bool,
43 private_tkm_nonceg_t *this, size_t size, u_int8_t *buffer)
44{
8e95bf45 45 nonce_type nonce;
c38459d7 46 if (ike_nc_create(this->context_id, size, &nonce) != TKM_OK)
8e95bf45
RB
47 {
48 return FALSE;
49 }
50
51 memcpy(buffer, &nonce.data, size);
559fe48c
RB
52 return TRUE;
53}
54
55METHOD(nonce_gen_t, allocate_nonce, bool,
56 private_tkm_nonceg_t *this, size_t size, chunk_t *chunk)
57{
58 *chunk = chunk_alloc(size);
624178fe
AKR
59 if (get_nonce(this, chunk->len, chunk->ptr))
60 {
61 tkm->chunk_map->insert(tkm->chunk_map, chunk, this->context_id);
62 return TRUE;
63 }
64 return FALSE;
559fe48c
RB
65}
66
67METHOD(nonce_gen_t, destroy, void,
68 private_tkm_nonceg_t *this)
69{
70 free(this);
71}
72
601de9f3
AKR
73METHOD(tkm_nonceg_t, get_id, nc_id_type,
74 private_tkm_nonceg_t *this)
75{
76 return this->context_id;
77}
78
559fe48c
RB
79/*
80 * Described in header.
81 */
82tkm_nonceg_t *tkm_nonceg_create()
83{
84 private_tkm_nonceg_t *this;
85
86 INIT(this,
87 .public = {
88 .nonce_gen = {
89 .get_nonce = _get_nonce,
90 .allocate_nonce = _allocate_nonce,
91 .destroy = _destroy,
92 },
601de9f3 93 .get_id = _get_id,
559fe48c 94 },
c38459d7 95 .context_id = tkm->idmgr->acquire_id(tkm->idmgr, TKM_CTX_NONCE),
559fe48c
RB
96 );
97
c38459d7
RB
98 if (!this->context_id)
99 {
100 free(this);
101 return NULL;
102 }
103
559fe48c
RB
104 return &this->public;
105}