9a032c54fa9b23e47d0f99bf62065f2bf6c87a5d
[strongswan.git] / src / libstrongswan / plugins / openssl / openssl_diffie_hellman.c
1 /*
2 * Copyright (C) 2008-2010 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
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 <openssl/dh.h>
18
19 #include "openssl_diffie_hellman.h"
20
21 #include <debug.h>
22
23 typedef struct private_openssl_diffie_hellman_t private_openssl_diffie_hellman_t;
24
25 /**
26 * Private data of an openssl_diffie_hellman_t object.
27 */
28 struct private_openssl_diffie_hellman_t {
29 /**
30 * Public openssl_diffie_hellman_t interface.
31 */
32 openssl_diffie_hellman_t public;
33
34 /**
35 * Diffie Hellman group number.
36 */
37 u_int16_t group;
38
39 /**
40 * Diffie Hellman object
41 */
42 DH *dh;
43
44 /**
45 * Other public value
46 */
47 BIGNUM *pub_key;
48
49 /**
50 * Shared secret
51 */
52 chunk_t shared_secret;
53
54 /**
55 * True if shared secret is computed
56 */
57 bool computed;
58 };
59
60 /**
61 * Implementation of openssl_diffie_hellman_t.get_my_public_value.
62 */
63 static void get_my_public_value(private_openssl_diffie_hellman_t *this,
64 chunk_t *value)
65 {
66 *value = chunk_alloc(DH_size(this->dh));
67 memset(value->ptr, 0, value->len);
68 BN_bn2bin(this->dh->pub_key,
69 value->ptr + value->len - BN_num_bytes(this->dh->pub_key));
70 }
71
72 /**
73 * Implementation of openssl_diffie_hellman_t.get_shared_secret.
74 */
75 static status_t get_shared_secret(private_openssl_diffie_hellman_t *this,
76 chunk_t *secret)
77 {
78 if (!this->computed)
79 {
80 return FAILED;
81 }
82 /* shared secret should requires a len according the DH group */
83 *secret = chunk_alloc(DH_size(this->dh));
84 memset(secret->ptr, 0, secret->len);
85 memcpy(secret->ptr + secret->len - this->shared_secret.len,
86 this->shared_secret.ptr, this->shared_secret.len);
87 return SUCCESS;
88 }
89
90
91 /**
92 * Implementation of openssl_diffie_hellman_t.set_other_public_value.
93 */
94 static void set_other_public_value(private_openssl_diffie_hellman_t *this,
95 chunk_t value)
96 {
97 int len;
98
99 BN_bin2bn(value.ptr, value.len, this->pub_key);
100 chunk_clear(&this->shared_secret);
101 this->shared_secret.ptr = malloc(DH_size(this->dh));
102 memset(this->shared_secret.ptr, 0xFF, this->shared_secret.len);
103 len = DH_compute_key(this->shared_secret.ptr, this->pub_key, this->dh);
104 if (len < 0)
105 {
106 DBG1(DBG_LIB, "DH shared secret computation failed");
107 return;
108 }
109 this->shared_secret.len = len;
110 this->computed = TRUE;
111 }
112
113 /**
114 * Implementation of openssl_diffie_hellman_t.get_dh_group.
115 */
116 static diffie_hellman_group_t get_dh_group(private_openssl_diffie_hellman_t *this)
117 {
118 return this->group;
119 }
120
121 /**
122 * Lookup the modulus in modulo table
123 */
124 static status_t set_modulus(private_openssl_diffie_hellman_t *this)
125 {
126 diffie_hellman_params_t *params = diffie_hellman_get_params(this->group);
127 if (!params)
128 {
129 return NOT_FOUND;
130 }
131 this->dh->p = BN_bin2bn(params->prime.ptr, params->prime.len, NULL);
132 this->dh->g = BN_bin2bn(params->generator.ptr, params->generator.len, NULL);
133 if (params->exp_len != params->prime.len)
134 {
135 this->dh->length = params->exp_len * 8;
136 }
137 return SUCCESS;
138 }
139
140 /**
141 * Implementation of openssl_diffie_hellman_t.destroy.
142 */
143 static void destroy(private_openssl_diffie_hellman_t *this)
144 {
145 BN_clear_free(this->pub_key);
146 DH_free(this->dh);
147 chunk_clear(&this->shared_secret);
148 free(this);
149 }
150
151 /*
152 * Described in header.
153 */
154 openssl_diffie_hellman_t *openssl_diffie_hellman_create(diffie_hellman_group_t group)
155 {
156 private_openssl_diffie_hellman_t *this = malloc_thing(private_openssl_diffie_hellman_t);
157
158 this->public.dh.get_shared_secret = (status_t (*)(diffie_hellman_t *, chunk_t *)) get_shared_secret;
159 this->public.dh.set_other_public_value = (void (*)(diffie_hellman_t *, chunk_t )) set_other_public_value;
160 this->public.dh.get_my_public_value = (void (*)(diffie_hellman_t *, chunk_t *)) get_my_public_value;
161 this->public.dh.get_dh_group = (diffie_hellman_group_t (*)(diffie_hellman_t *)) get_dh_group;
162 this->public.dh.destroy = (void (*)(diffie_hellman_t *)) destroy;
163
164 this->dh = DH_new();
165 if (!this->dh)
166 {
167 free(this);
168 return NULL;
169 }
170
171 this->group = group;
172 this->computed = FALSE;
173 this->pub_key = BN_new();
174 this->shared_secret = chunk_empty;
175
176 /* find a modulus according to group */
177 if (set_modulus(this) != SUCCESS)
178 {
179 destroy(this);
180 return NULL;
181 }
182
183 /* generate my public and private values */
184 if (!DH_generate_key(this->dh))
185 {
186 destroy(this);
187 return NULL;
188 }
189 DBG2(DBG_LIB, "size of DH secret exponent: %d bits",
190 BN_num_bits(this->dh->priv_key));
191
192 return &this->public;
193 }