blob: 9f67ad2ae6f59f6f6b18a7c3a9ac3fb4bc76f8e5 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef __included_ikev2_priv_h__
16#define __included_ikev2_priv_h__
17
18#include <vnet/vnet.h>
19#include <vnet/ip/ip.h>
20#include <vnet/ethernet/ethernet.h>
21
22#include <vnet/ipsec/ikev2.h>
23
24#include <vppinfra/hash.h>
25#include <vppinfra/elog.h>
26#include <vppinfra/error.h>
27
28#include <openssl/rand.h>
29#include <openssl/dh.h>
30#include <openssl/hmac.h>
31#include <openssl/evp.h>
32
33#define IKEV2_DEBUG_PAYLOAD 1
34
35#if IKEV2_DEBUG_PAYLOAD == 1
36#define DBG_PLD(my_args...) clib_warning(my_args)
37#else
38#define DBG_PLD(my_args...)
39#endif
40
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070041typedef enum
42{
Ed Warnickecb9cada2015-12-08 15:45:58 -070043 IKEV2_STATE_UNKNOWN,
44 IKEV2_STATE_SA_INIT,
45 IKEV2_STATE_DELETED,
46 IKEV2_STATE_AUTH_FAILED,
47 IKEV2_STATE_AUTHENTICATED,
48 IKEV2_STATE_NOTIFY_AND_DELETE,
49 IKEV2_STATE_TS_UNACCEPTABLE,
50 IKEV2_STATE_NO_PROPOSAL_CHOSEN,
51} ikev2_state_t;
52
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070053typedef struct
54{
Ed Warnickecb9cada2015-12-08 15:45:58 -070055 ikev2_auth_method_t method:8;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070056 u8 *data;
57 u8 hex; /* hex encoding of the shared secret */
58 EVP_PKEY *key;
Ed Warnickecb9cada2015-12-08 15:45:58 -070059} ikev2_auth_t;
60
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070061typedef enum
62{
Ed Warnickecb9cada2015-12-08 15:45:58 -070063 IKEV2_DH_GROUP_MODP = 0,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070064 IKEV2_DH_GROUP_ECP = 1,
Ed Warnickecb9cada2015-12-08 15:45:58 -070065} ikev2_dh_group_t;
66
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070067typedef struct
68{
Ed Warnickecb9cada2015-12-08 15:45:58 -070069 ikev2_transform_type_t type;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070070 union
71 {
72 u16 transform_id;
73 ikev2_transform_encr_type_t encr_type:16;
74 ikev2_transform_prf_type_t prf_type:16;
Ed Warnickecb9cada2015-12-08 15:45:58 -070075 ikev2_transform_integ_type_t integ_type:16;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070076 ikev2_transform_dh_type_t dh_type:16;
77 ikev2_transform_esn_type_t esn_type:16;
Ed Warnickecb9cada2015-12-08 15:45:58 -070078 };
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070079 u8 *attrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -070080 u16 key_len;
81 u16 key_trunc;
82 u16 block_size;
83 u8 dh_group;
84 int nid;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070085 const char *dh_p;
86 const char *dh_g;
87 const void *md;
88 const void *cipher;
Ed Warnickecb9cada2015-12-08 15:45:58 -070089} ikev2_sa_transform_t;
90
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070091typedef struct
92{
Ed Warnickecb9cada2015-12-08 15:45:58 -070093 u8 proposal_num;
94 ikev2_protocol_id_t protocol_id:8;
95 u32 spi;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070096 ikev2_sa_transform_t *transforms;
Ed Warnickecb9cada2015-12-08 15:45:58 -070097} ikev2_sa_proposal_t;
98
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070099typedef struct
100{
101 u8 ts_type;
102 u8 protocol_id;
103 u16 selector_len;
104 u16 start_port;
105 u16 end_port;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106 ip4_address_t start_addr;
107 ip4_address_t end_addr;
108} ikev2_ts_t;
109
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700110typedef struct
111{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112 ikev2_id_type_t type:8;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700113 u8 *data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114} ikev2_id_t;
115
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700116typedef struct
117{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118 /* sa proposals vectors */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700119 ikev2_sa_proposal_t *i_proposals;
120 ikev2_sa_proposal_t *r_proposals;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121
122 /* Traffic Selectors */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700123 ikev2_ts_t *tsi;
124 ikev2_ts_t *tsr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125
126 /* keys */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700127 u8 *sk_ai;
128 u8 *sk_ar;
129 u8 *sk_ei;
130 u8 *sk_er;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131} ikev2_child_sa_t;
132
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700133typedef struct
134{
135 u8 protocol_id;
136 u32 spi; /*for ESP and AH SPI size is 4, for IKE size is 0 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137} ikev2_delete_t;
138
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700139typedef struct
140{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141 u8 protocol_id;
142 u32 spi;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700143 ikev2_sa_proposal_t *i_proposal;
144 ikev2_sa_proposal_t *r_proposal;
145 ikev2_ts_t *tsi;
146 ikev2_ts_t *tsr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147} ikev2_rekey_t;
148
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700149typedef struct
150{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151 u16 msg_type;
152 u8 protocol_id;
153 u32 spi;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700154 u8 *data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155} ikev2_notify_t;
156
157
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700158typedef struct
159{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160 ikev2_state_t state;
161 u8 unsupported_cp;
162 u8 initial_contact;
163 ip4_address_t iaddr;
164 ip4_address_t raddr;
165 u64 ispi;
166 u64 rspi;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700167 u8 *i_nonce;
168 u8 *r_nonce;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169
170 /* DH data */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700171 u16 dh_group;
172 u8 *dh_shared_key;
173 u8 *i_dh_data;
174 u8 *r_dh_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175
176 /* sa proposals vectors */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700177 ikev2_sa_proposal_t *i_proposals;
178 ikev2_sa_proposal_t *r_proposals;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179
180 /* keys */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700181 u8 *sk_d;
182 u8 *sk_ai;
183 u8 *sk_ar;
184 u8 *sk_ei;
185 u8 *sk_er;
186 u8 *sk_pi;
187 u8 *sk_pr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188
189 /* auth */
190 ikev2_auth_t i_auth;
191 ikev2_auth_t r_auth;
192
193 /* ID */
194 ikev2_id_t i_id;
195 ikev2_id_t r_id;
196
197 /* pending deletes */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700198 ikev2_delete_t *del;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199
200 /* pending rekeyings */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700201 ikev2_rekey_t *rekey;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202
203 /* packet data */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700204 u8 *last_sa_init_req_packet_data;
205 u8 *last_sa_init_res_packet_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206
207 /* retransmit */
208 u32 last_msg_id;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700209 u8 *last_res_packet_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700211 ikev2_child_sa_t *childs;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212} ikev2_sa_t;
213
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700214typedef struct
215{
216 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217 u8 is_enabled;
218
219 ikev2_auth_t auth;
220 ikev2_id_t loc_id;
221 ikev2_id_t rem_id;
222 ikev2_ts_t loc_ts;
223 ikev2_ts_t rem_ts;
224} ikev2_profile_t;
225
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700226typedef struct
227{
228 /* pool of IKEv2 Security Associations */
229 ikev2_sa_t *sas;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700230
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700231 /* hash */
232 uword *sa_by_rspi;
Matthew Smith2838a232016-06-21 16:05:09 -0500233} ikev2_main_per_thread_data_t;
234
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700235typedef struct
236{
237 /* pool of IKEv2 profiles */
238 ikev2_profile_t *profiles;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700240 /* vector of supported transform types */
241 ikev2_sa_transform_t *supported_transforms;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700243 /* hash */
244 mhash_t profile_index_by_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700246 /* local private key */
247 EVP_PKEY *pkey;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700249 /* convenience */
250 vlib_main_t *vlib_main;
251 vnet_main_t *vnet_main;
Matthew Smith2838a232016-06-21 16:05:09 -0500252
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700253 ikev2_main_per_thread_data_t *per_thread_data;
Matthew Smith2838a232016-06-21 16:05:09 -0500254
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255} ikev2_main_t;
256
257ikev2_main_t ikev2_main;
258
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700259void ikev2_sa_free_proposal_vector (ikev2_sa_proposal_t ** v);
260ikev2_sa_transform_t *ikev2_sa_get_td_for_type (ikev2_sa_proposal_t * p,
261 ikev2_transform_type_t type);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262
263/* ikev2_crypto.c */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700264v8 *ikev2_calc_prf (ikev2_sa_transform_t * tr, v8 * key, v8 * data);
265u8 *ikev2_calc_prfplus (ikev2_sa_transform_t * tr, u8 * key, u8 * seed,
266 int len);
267v8 *ikev2_calc_integr (ikev2_sa_transform_t * tr, v8 * key, u8 * data,
268 int len);
269v8 *ikev2_decrypt_data (ikev2_sa_t * sa, u8 * data, int len);
270int ikev2_encrypt_data (ikev2_sa_t * sa, v8 * src, u8 * dst);
271void ikev2_generate_dh (ikev2_sa_t * sa, ikev2_sa_transform_t * t);
272int ikev2_verify_sign (EVP_PKEY * pkey, u8 * sigbuf, u8 * data);
273u8 *ikev2_calc_sign (EVP_PKEY * pkey, u8 * data);
274EVP_PKEY *ikev2_load_cert_file (u8 * file);
275EVP_PKEY *ikev2_load_key_file (u8 * file);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276void ikev2_crypto_init (ikev2_main_t * km);
277
278/* ikev2_payload.c */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700279typedef struct
280{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700281 u8 first_payload_type;
282 u16 last_hdr_off;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700283 u8 *data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700284} ikev2_payload_chain_t;
285
286#define ikev2_payload_new_chain(V) vec_validate (V, 0)
287#define ikev2_payload_destroy_chain(V) do { \
288 vec_free((V)->data); \
289 vec_free(V); \
290} while (0)
291
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700292void ikev2_payload_add_notify (ikev2_payload_chain_t * c, u16 msg_type,
293 u8 * data);
294void ikev2_payload_add_sa (ikev2_payload_chain_t * c,
295 ikev2_sa_proposal_t * proposals);
296void ikev2_payload_add_ke (ikev2_payload_chain_t * c, u16 dh_group,
297 u8 * dh_data);
298void ikev2_payload_add_nonce (ikev2_payload_chain_t * c, u8 * nonce);
299void ikev2_payload_add_id (ikev2_payload_chain_t * c, ikev2_id_t * id,
300 u8 type);
301void ikev2_payload_add_auth (ikev2_payload_chain_t * c, ikev2_auth_t * auth);
302void ikev2_payload_add_ts (ikev2_payload_chain_t * c, ikev2_ts_t * ts,
303 u8 type);
304void ikev2_payload_add_delete (ikev2_payload_chain_t * c, ikev2_delete_t * d);
305void ikev2_payload_chain_add_padding (ikev2_payload_chain_t * c, int bs);
306void ikev2_parse_vendor_payload (ike_payload_header_t * ikep);
307ikev2_sa_proposal_t *ikev2_parse_sa_payload (ike_payload_header_t * ikep);
308ikev2_ts_t *ikev2_parse_ts_payload (ike_payload_header_t * ikep);
309ikev2_delete_t *ikev2_parse_delete_payload (ike_payload_header_t * ikep);
310ikev2_notify_t *ikev2_parse_notify_payload (ike_payload_header_t * ikep);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311
312#endif /* __included_ikev2_priv_h__ */
313
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700314
315/*
316 * fd.io coding-style-patch-verification: ON
317 *
318 * Local Variables:
319 * eval: (c-set-style "gnu")
320 * End:
321 */