Avoid proxy for bypass_socket, enable_udp_decap
[strongswan.git] / src / charon-tkm / src / tkm / tkm_diffie_hellman.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_utils.h"
22 #include "tkm_diffie_hellman.h"
23
24 #include <utils/debug.h>
25
26 typedef struct private_tkm_diffie_hellman_t private_tkm_diffie_hellman_t;
27
28 /**
29 * Private data of a tkm_diffie_hellman_t object.
30 */
31 struct private_tkm_diffie_hellman_t {
32
33 /**
34 * Public tkm_diffie_hellman_t interface.
35 */
36 tkm_diffie_hellman_t public;
37
38 /**
39 * Diffie Hellman group number.
40 */
41 u_int16_t group;
42
43 /**
44 * Diffie Hellman public value.
45 */
46 dh_pubvalue_type pubvalue;
47
48 /**
49 * Context id.
50 */
51 dh_id_type context_id;
52
53 };
54
55 METHOD(diffie_hellman_t, get_my_public_value, void,
56 private_tkm_diffie_hellman_t *this, chunk_t *value)
57 {
58 sequence_to_chunk(this->pubvalue.data, this->pubvalue.size, value);
59 }
60
61 METHOD(diffie_hellman_t, get_shared_secret, status_t,
62 private_tkm_diffie_hellman_t *this, chunk_t *secret)
63 {
64 dh_key_type shared_secret;
65 if (ike_dh_get_shared_secret(this->context_id, &shared_secret) != TKM_OK)
66 {
67 return FAILED;
68 }
69
70 sequence_to_chunk(&shared_secret.data[0], shared_secret.size, secret);
71 return SUCCESS;
72 }
73
74
75 METHOD(diffie_hellman_t, set_other_public_value, void,
76 private_tkm_diffie_hellman_t *this, chunk_t value)
77 {
78 // TODO: unvoid this function
79
80 dh_pubvalue_type othervalue;
81 othervalue.size = value.len;
82 memcpy(&othervalue.data, value.ptr, value.len);
83
84 ike_dh_generate_key(this->context_id, othervalue);
85 }
86
87 METHOD(diffie_hellman_t, get_dh_group, diffie_hellman_group_t,
88 private_tkm_diffie_hellman_t *this)
89 {
90 return this->group;
91 }
92
93 METHOD(diffie_hellman_t, destroy, void,
94 private_tkm_diffie_hellman_t *this)
95 {
96 if (ike_dh_reset(this->context_id) != TKM_OK)
97 {
98 DBG1(DBG_LIB, "failed to reset DH context %d", this->context_id);
99 }
100
101 tkm->idmgr->release_id(tkm->idmgr, TKM_CTX_DH, this->context_id);
102 free(this);
103 }
104
105 METHOD(tkm_diffie_hellman_t, get_id, dh_id_type,
106 private_tkm_diffie_hellman_t *this)
107 {
108 return this->context_id;
109 }
110
111 /*
112 * Described in header.
113 */
114 tkm_diffie_hellman_t *tkm_diffie_hellman_create(diffie_hellman_group_t group)
115 {
116 private_tkm_diffie_hellman_t *this;
117
118 INIT(this,
119 .public = {
120 .dh = {
121 .get_shared_secret = _get_shared_secret,
122 .set_other_public_value = _set_other_public_value,
123 .get_my_public_value = _get_my_public_value,
124 .get_dh_group = _get_dh_group,
125 .destroy = _destroy,
126 },
127 .get_id = _get_id,
128 },
129 .group = group,
130 .context_id = tkm->idmgr->acquire_id(tkm->idmgr, TKM_CTX_DH),
131 );
132
133 if (!this->context_id)
134 {
135 free(this);
136 return NULL;
137 }
138
139 if (ike_dh_create(this->context_id, group, &this->pubvalue) != TKM_OK)
140 {
141 free(this);
142 return NULL;
143 }
144
145 return &this->public;
146 }