ikev2: fix GCM cipher
[vpp.git] / src / plugins / ikev2 / ikev2_priv.h
1 /*
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 <plugins/ikev2/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
41 typedef enum
42 {
43   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
53 typedef struct
54 {
55   ikev2_auth_method_t method:8;
56   u8 *data;
57   u8 hex;                       /* hex encoding of the shared secret */
58   EVP_PKEY *key;
59 } ikev2_auth_t;
60
61 typedef enum
62 {
63   IKEV2_DH_GROUP_MODP = 0,
64   IKEV2_DH_GROUP_ECP = 1,
65 } ikev2_dh_group_t;
66
67 typedef struct
68 {
69   ikev2_transform_type_t type;
70   union
71   {
72     u16 transform_id;
73     ikev2_transform_encr_type_t encr_type:16;
74     ikev2_transform_prf_type_t prf_type:16;
75     ikev2_transform_integ_type_t integ_type:16;
76     ikev2_transform_dh_type_t dh_type:16;
77     ikev2_transform_esn_type_t esn_type:16;
78   };
79   u8 *attrs;
80   u16 key_len;
81   u16 key_trunc;
82   u16 block_size;
83   u8 dh_group;
84   int nid;
85   const char *dh_p;
86   const char *dh_g;
87   const void *md;
88   const void *cipher;
89 } ikev2_sa_transform_t;
90
91 typedef struct
92 {
93   u8 proposal_num;
94   ikev2_protocol_id_t protocol_id:8;
95   u32 spi;
96   ikev2_sa_transform_t *transforms;
97 } ikev2_sa_proposal_t;
98
99 typedef struct
100 {
101   u8 ts_type;
102   u8 protocol_id;
103   u16 selector_len;
104   u16 start_port;
105   u16 end_port;
106   ip4_address_t start_addr;
107   ip4_address_t end_addr;
108 } ikev2_ts_t;
109
110 typedef struct
111 {
112   u32 sw_if_index;
113   ip4_address_t ip4;
114 } ikev2_responder_t;
115
116 typedef struct
117 {
118   ikev2_transform_encr_type_t crypto_alg;
119   ikev2_transform_integ_type_t integ_alg;
120   ikev2_transform_dh_type_t dh_type;
121   u32 crypto_key_size;
122 } ikev2_transforms_set;
123
124
125 typedef struct
126 {
127   ikev2_id_type_t type:8;
128   u8 *data;
129 } ikev2_id_t;
130
131 typedef struct
132 {
133   /* sa proposals vectors */
134   ikev2_sa_proposal_t *i_proposals;
135   ikev2_sa_proposal_t *r_proposals;
136
137   /* Traffic Selectors */
138   ikev2_ts_t *tsi;
139   ikev2_ts_t *tsr;
140
141   /* keys */
142   u8 *sk_ai;
143   u8 *sk_ar;
144   u8 *sk_ei;
145   u8 *sk_er;
146   u32 salt_ei;
147   u32 salt_er;
148
149   /* installed data */
150   u32 sw_if_index;
151   u32 local_sa;
152   u32 remote_sa;
153
154   /* lifetime data */
155   f64 time_to_expiration;
156   u8 is_expired;
157   i8 rekey_retries;
158 } ikev2_child_sa_t;
159
160 typedef struct
161 {
162   u8 protocol_id;
163   u32 spi;                      /*for ESP and AH SPI size is 4, for IKE size is 0 */
164 } ikev2_delete_t;
165
166 typedef struct
167 {
168   u8 protocol_id;
169   u32 spi;
170   u32 ispi;
171   ikev2_sa_proposal_t *i_proposal;
172   ikev2_sa_proposal_t *r_proposal;
173   ikev2_ts_t *tsi;
174   ikev2_ts_t *tsr;
175 } ikev2_rekey_t;
176
177 typedef struct
178 {
179   u16 msg_type;
180   u8 protocol_id;
181   u32 spi;
182   u8 *data;
183 } ikev2_notify_t;
184
185 typedef struct
186 {
187   u8 *name;
188   u8 is_enabled;
189
190   ikev2_auth_t auth;
191   ikev2_id_t loc_id;
192   ikev2_id_t rem_id;
193   ikev2_ts_t loc_ts;
194   ikev2_ts_t rem_ts;
195   ikev2_responder_t responder;
196   ikev2_transforms_set ike_ts;
197   ikev2_transforms_set esp_ts;
198   u64 lifetime;
199   u64 lifetime_maxdata;
200   u32 lifetime_jitter;
201   u32 handover;
202 } ikev2_profile_t;
203
204 typedef struct
205 {
206   ikev2_state_t state;
207   u8 unsupported_cp;
208   u8 initial_contact;
209   ip4_address_t iaddr;
210   ip4_address_t raddr;
211   u64 ispi;
212   u64 rspi;
213   u8 *i_nonce;
214   u8 *r_nonce;
215
216   /* DH data */
217   u16 dh_group;
218   u8 *dh_shared_key;
219   u8 *dh_private_key;
220   u8 *i_dh_data;
221   u8 *r_dh_data;
222
223   /* sa proposals vectors */
224   ikev2_sa_proposal_t *i_proposals;
225   ikev2_sa_proposal_t *r_proposals;
226
227   /* keys */
228   u8 *sk_d;
229   u8 *sk_ai;
230   u8 *sk_ar;
231   u8 *sk_ei;
232   u8 *sk_er;
233   u8 *sk_pi;
234   u8 *sk_pr;
235
236   /* auth */
237   ikev2_auth_t i_auth;
238   ikev2_auth_t r_auth;
239
240   /* ID */
241   ikev2_id_t i_id;
242   ikev2_id_t r_id;
243
244   /* pending deletes */
245   ikev2_delete_t *del;
246
247   /* pending rekeyings */
248   ikev2_rekey_t *rekey;
249
250   /* packet data */
251   u8 *last_sa_init_req_packet_data;
252   u8 *last_sa_init_res_packet_data;
253
254   /* retransmit */
255   u32 last_msg_id;
256   u8 *last_res_packet_data;
257
258   u8 is_initiator;
259   u32 last_init_msg_id;
260   u8 is_profile_index_set;
261   u32 profile_index;
262
263   ikev2_child_sa_t *childs;
264 } ikev2_sa_t;
265
266
267 typedef struct
268 {
269   /* pool of IKEv2 Security Associations */
270   ikev2_sa_t *sas;
271
272   /* hash */
273   uword *sa_by_rspi;
274 } ikev2_main_per_thread_data_t;
275
276 typedef struct
277 {
278   /* pool of IKEv2 profiles */
279   ikev2_profile_t *profiles;
280
281   /* vector of supported transform types */
282   ikev2_sa_transform_t *supported_transforms;
283
284   /* hash */
285   mhash_t profile_index_by_name;
286
287   /* local private key */
288   EVP_PKEY *pkey;
289
290   /* convenience */
291   vlib_main_t *vlib_main;
292   vnet_main_t *vnet_main;
293
294   /* pool of IKEv2 Security Associations created in initiator mode */
295   ikev2_sa_t *sais;
296   /* hash */
297   uword *sa_by_ispi;
298
299   ikev2_main_per_thread_data_t *per_thread_data;
300
301   /* API message ID base */
302   u16 msg_id_base;
303 } ikev2_main_t;
304
305 extern ikev2_main_t ikev2_main;
306
307 void ikev2_sa_free_proposal_vector (ikev2_sa_proposal_t ** v);
308 ikev2_sa_transform_t *ikev2_sa_get_td_for_type (ikev2_sa_proposal_t * p,
309                                                 ikev2_transform_type_t type);
310
311 /* ikev2_crypto.c */
312 v8 *ikev2_calc_prf (ikev2_sa_transform_t * tr, v8 * key, v8 * data);
313 u8 *ikev2_calc_prfplus (ikev2_sa_transform_t * tr, u8 * key, u8 * seed,
314                         int len);
315 v8 *ikev2_calc_integr (ikev2_sa_transform_t * tr, v8 * key, u8 * data,
316                        int len);
317 v8 *ikev2_decrypt_data (ikev2_sa_t * sa, u8 * data, int len);
318 int ikev2_encrypt_data (ikev2_sa_t * sa, v8 * src, u8 * dst);
319 void ikev2_generate_dh (ikev2_sa_t * sa, ikev2_sa_transform_t * t);
320 void ikev2_complete_dh (ikev2_sa_t * sa, ikev2_sa_transform_t * t);
321 int ikev2_verify_sign (EVP_PKEY * pkey, u8 * sigbuf, u8 * data);
322 u8 *ikev2_calc_sign (EVP_PKEY * pkey, u8 * data);
323 EVP_PKEY *ikev2_load_cert_file (u8 * file);
324 EVP_PKEY *ikev2_load_key_file (u8 * file);
325 void ikev2_crypto_init (ikev2_main_t * km);
326
327 /* ikev2_payload.c */
328 typedef struct
329 {
330   u8 first_payload_type;
331   u16 last_hdr_off;
332   u8 *data;
333 } ikev2_payload_chain_t;
334
335 #define ikev2_payload_new_chain(V) vec_validate (V, 0)
336 #define ikev2_payload_destroy_chain(V) do { \
337   vec_free((V)->data);                 \
338   vec_free(V);                         \
339 } while (0)
340
341 void ikev2_payload_add_notify (ikev2_payload_chain_t * c, u16 msg_type,
342                                u8 * data);
343 void ikev2_payload_add_notify_2 (ikev2_payload_chain_t * c, u16 msg_type,
344                                  u8 * data, ikev2_notify_t * notify);
345 void ikev2_payload_add_sa (ikev2_payload_chain_t * c,
346                            ikev2_sa_proposal_t * proposals);
347 void ikev2_payload_add_ke (ikev2_payload_chain_t * c, u16 dh_group,
348                            u8 * dh_data);
349 void ikev2_payload_add_nonce (ikev2_payload_chain_t * c, u8 * nonce);
350 void ikev2_payload_add_id (ikev2_payload_chain_t * c, ikev2_id_t * id,
351                            u8 type);
352 void ikev2_payload_add_auth (ikev2_payload_chain_t * c, ikev2_auth_t * auth);
353 void ikev2_payload_add_ts (ikev2_payload_chain_t * c, ikev2_ts_t * ts,
354                            u8 type);
355 void ikev2_payload_add_delete (ikev2_payload_chain_t * c, ikev2_delete_t * d);
356 void ikev2_payload_chain_add_padding (ikev2_payload_chain_t * c, int bs);
357 void ikev2_parse_vendor_payload (ike_payload_header_t * ikep);
358 ikev2_sa_proposal_t *ikev2_parse_sa_payload (ike_payload_header_t * ikep);
359 ikev2_ts_t *ikev2_parse_ts_payload (ike_payload_header_t * ikep);
360 ikev2_delete_t *ikev2_parse_delete_payload (ike_payload_header_t * ikep);
361 ikev2_notify_t *ikev2_parse_notify_payload (ike_payload_header_t * ikep);
362
363 #endif /* __included_ikev2_priv_h__ */
364
365
366 /*
367  * fd.io coding-style-patch-verification: ON
368  *
369  * Local Variables:
370  * eval: (c-set-style "gnu")
371  * End:
372  */