• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * IKEv2 definitions
3  * Copyright (c) 2007, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #ifndef IKEV2_COMMON_H
10 #define IKEV2_COMMON_H
11 
12 /*
13  * Nonce length must be at least 16 octets. It must also be at least half the
14  * key size of the negotiated PRF.
15  */
16 #define IKEV2_NONCE_MIN_LEN 16
17 #define IKEV2_NONCE_MAX_LEN 256
18 
19 /* IKE Header - RFC 4306, Sect. 3.1 */
20 #ifdef _MSC_VER
21 #pragma pack(push, 1)
22 #endif /* _MSC_VER */
23 
24 #define IKEV2_SPI_LEN 8
25 
26 struct ikev2_hdr {
27 	u8 i_spi[IKEV2_SPI_LEN]; /* IKE_SA Initiator's SPI */
28 	u8 r_spi[IKEV2_SPI_LEN]; /* IKE_SA Responder's SPI */
29 	u8 next_payload;
30 	u8 version; /* MjVer | MnVer */
31 	u8 exchange_type;
32 	u8 flags;
33 	u8 message_id[4];
34 	u8 length[4]; /* total length of HDR + payloads */
35 } STRUCT_PACKED;
36 
37 struct ikev2_payload_hdr {
38 	u8 next_payload;
39 	u8 flags;
40 	u8 payload_length[2]; /* this payload, including the payload header */
41 } STRUCT_PACKED;
42 
43 struct ikev2_proposal {
44 	u8 type; /* 0 (last) or 2 (more) */
45 	u8 reserved;
46 	u8 proposal_length[2]; /* including all transform and attributes */
47 	u8 proposal_num;
48 	u8 protocol_id; /* IKEV2_PROTOCOL_* */
49 	u8 spi_size;
50 	u8 num_transforms;
51 	/* SPI of spi_size octets */
52 	/* Transforms */
53 } STRUCT_PACKED;
54 
55 struct ikev2_transform {
56 	u8 type; /* 0 (last) or 3 (more) */
57 	u8 reserved;
58 	u8 transform_length[2]; /* including Header and Attributes */
59 	u8 transform_type;
60 	u8 reserved2;
61 	u8 transform_id[2];
62 	/* Transform Attributes */
63 } STRUCT_PACKED;
64 
65 #ifdef _MSC_VER
66 #pragma pack(pop)
67 #endif /* _MSC_VER */
68 
69 
70 /* Current IKEv2 version from RFC 4306 */
71 #define IKEV2_MjVer 2
72 #define IKEV2_MnVer 0
73 #define IKEV2_VERSION (((IKEV2_MjVer) << 4) | (IKEV2_MnVer))
74 
75 /* IKEv2 Exchange Types */
76 enum {
77 	/* 0-33 RESERVED */
78 	IKE_SA_INIT = 34,
79 	IKE_SA_AUTH = 35,
80 	CREATE_CHILD_SA = 36,
81 	INFORMATION = 37
82 	/* 38-239 RESERVED TO IANA */
83 	/* 240-255 Reserved for private use */
84 };
85 
86 /* IKEv2 Flags */
87 #define IKEV2_HDR_INITIATOR	0x08
88 #define IKEV2_HDR_VERSION	0x10
89 #define IKEV2_HDR_RESPONSE	0x20
90 
91 /* Payload Header Flags */
92 #define IKEV2_PAYLOAD_FLAGS_CRITICAL 0x01
93 
94 
95 /* EAP-IKEv2 Payload Types (in Next Payload Type field)
96  * http://www.iana.org/assignments/eap-ikev2-payloads */
97 enum {
98 	IKEV2_PAYLOAD_NO_NEXT_PAYLOAD = 0,
99 	IKEV2_PAYLOAD_SA = 33,
100 	IKEV2_PAYLOAD_KEY_EXCHANGE = 34,
101 	IKEV2_PAYLOAD_IDi = 35,
102 	IKEV2_PAYLOAD_IDr = 36,
103 	IKEV2_PAYLOAD_CERTIFICATE = 37,
104 	IKEV2_PAYLOAD_CERT_REQ = 38,
105 	IKEV2_PAYLOAD_AUTHENTICATION = 39,
106 	IKEV2_PAYLOAD_NONCE = 40,
107 	IKEV2_PAYLOAD_NOTIFICATION = 41,
108 	IKEV2_PAYLOAD_VENDOD_ID = 43,
109 	IKEV2_PAYLOAD_ENCRYPTED = 46,
110 	IKEV2_PAYLOAD_NEXT_FAST_ID = 121
111 };
112 
113 
114 /* IKEv2 Proposal - Protocol ID */
115 enum {
116 	IKEV2_PROTOCOL_RESERVED = 0,
117 	IKEV2_PROTOCOL_IKE = 1, /* IKE is the only one allowed for EAP-IKEv2 */
118 	IKEV2_PROTOCOL_AH = 2,
119 	IKEV2_PROTOCOL_ESP = 3
120 };
121 
122 
123 /* IKEv2 Transform Types */
124 enum {
125 	IKEV2_TRANSFORM_ENCR = 1,
126 	IKEV2_TRANSFORM_PRF = 2,
127 	IKEV2_TRANSFORM_INTEG = 3,
128 	IKEV2_TRANSFORM_DH = 4,
129 	IKEV2_TRANSFORM_ESN = 5
130 };
131 
132 /* IKEv2 Transform Type 1 (Encryption Algorithm) */
133 enum {
134 	ENCR_DES_IV64 = 1,
135 	ENCR_DES = 2,
136 	ENCR_3DES = 3,
137 	ENCR_RC5 = 4,
138 	ENCR_IDEA = 5,
139 	ENCR_CAST = 6,
140 	ENCR_BLOWFISH = 7,
141 	ENCR_3IDEA = 8,
142 	ENCR_DES_IV32 = 9,
143 	ENCR_NULL = 11,
144 	ENCR_AES_CBC = 12,
145 	ENCR_AES_CTR = 13
146 };
147 
148 /* IKEv2 Transform Type 2 (Pseudo-random Function) */
149 enum {
150 	PRF_HMAC_MD5 = 1,
151 	PRF_HMAC_SHA1 = 2,
152 	PRF_HMAC_TIGER = 3,
153 	PRF_AES128_XCBC = 4
154 };
155 
156 /* IKEv2 Transform Type 3 (Integrity Algorithm) */
157 enum {
158 	AUTH_HMAC_MD5_96 = 1,
159 	AUTH_HMAC_SHA1_96 = 2,
160 	AUTH_DES_MAC = 3,
161 	AUTH_KPDK_MD5 = 4,
162 	AUTH_AES_XCBC_96 = 5
163 };
164 
165 /* IKEv2 Transform Type 4 (Diffie-Hellman Group) */
166 enum {
167 	DH_GROUP1_768BIT_MODP = 1, /* RFC 4306 */
168 	DH_GROUP2_1024BIT_MODP = 2, /* RFC 4306 */
169 	DH_GROUP5_1536BIT_MODP = 5, /* RFC 3526 */
170 	DH_GROUP5_2048BIT_MODP = 14, /* RFC 3526 */
171 	DH_GROUP5_3072BIT_MODP = 15, /* RFC 3526 */
172 	DH_GROUP5_4096BIT_MODP = 16, /* RFC 3526 */
173 	DH_GROUP5_6144BIT_MODP = 17, /* RFC 3526 */
174 	DH_GROUP5_8192BIT_MODP = 18 /* RFC 3526 */
175 };
176 
177 
178 /* Identification Data Types (RFC 4306, Sect. 3.5) */
179 enum {
180 	ID_IPV4_ADDR = 1,
181 	ID_FQDN = 2,
182 	ID_RFC822_ADDR = 3,
183 	ID_IPV6_ADDR = 5,
184 	ID_DER_ASN1_DN = 9,
185 	ID_DER_ASN1_GN= 10,
186 	ID_KEY_ID = 11
187 };
188 
189 
190 /* Certificate Encoding (RFC 4306, Sect. 3.6) */
191 enum {
192 	CERT_ENCODING_PKCS7_X509 = 1,
193 	CERT_ENCODING_PGP_CERT = 2,
194 	CERT_ENCODING_DNS_SIGNED_KEY = 3,
195 	/* X.509 Certificate - Signature: DER encoded X.509 certificate whose
196 	 * public key is used to validate the sender's AUTH payload */
197 	CERT_ENCODING_X509_CERT_SIGN = 4,
198 	CERT_ENCODING_KERBEROS_TOKEN = 6,
199 	/* DER encoded X.509 certificate revocation list */
200 	CERT_ENCODING_CRL = 7,
201 	CERT_ENCODING_ARL = 8,
202 	CERT_ENCODING_SPKI_CERT = 9,
203 	CERT_ENCODING_X509_CERT_ATTR = 10,
204 	/* PKCS #1 encoded RSA key */
205 	CERT_ENCODING_RAW_RSA_KEY = 11,
206 	CERT_ENCODING_HASH_AND_URL_X509_CERT = 12,
207 	CERT_ENCODING_HASH_AND_URL_X509_BUNDLE = 13
208 };
209 
210 
211 /* Authentication Method (RFC 4306, Sect. 3.8) */
212 enum {
213 	AUTH_RSA_SIGN = 1,
214 	AUTH_SHARED_KEY_MIC = 2,
215 	AUTH_DSS_SIGN = 3
216 };
217 
218 
219 /* Notify Message Types (RFC 4306, Sect. 3.10.1) */
220 enum {
221 	UNSUPPORTED_CRITICAL_PAYLOAD = 1,
222 	INVALID_IKE_SPI = 4,
223 	INVALID_MAJOR_VERSION = 5,
224 	INVALID_SYNTAX = 7,
225 	INVALID_MESSAGE_ID = 9,
226 	INVALID_SPI = 11,
227 	NO_PROPOSAL_CHOSEN = 14,
228 	INVALID_KE_PAYLOAD = 17,
229 	AUTHENTICATION_FAILED = 24,
230 	SINGLE_PAIR_REQUIRED = 34,
231 	NO_ADDITIONAL_SAS = 35,
232 	INTERNAL_ADDRESS_FAILURE = 36,
233 	FAILED_CP_REQUIRED = 37,
234 	TS_UNACCEPTABLE = 38,
235 	INVALID_SELECTORS = 39
236 };
237 
238 
239 struct ikev2_keys {
240 	u8 *SK_d, *SK_ai, *SK_ar, *SK_ei, *SK_er, *SK_pi, *SK_pr;
241 	size_t SK_d_len, SK_integ_len, SK_encr_len, SK_prf_len;
242 };
243 
244 
245 int ikev2_keys_set(struct ikev2_keys *keys);
246 void ikev2_free_keys(struct ikev2_keys *keys);
247 
248 
249 /* Maximum hash length for supported hash algorithms */
250 #define IKEV2_MAX_HASH_LEN 20
251 
252 struct ikev2_integ_alg {
253 	int id;
254 	size_t key_len;
255 	size_t hash_len;
256 };
257 
258 struct ikev2_prf_alg {
259 	int id;
260 	size_t key_len;
261 	size_t hash_len;
262 };
263 
264 struct ikev2_encr_alg {
265 	int id;
266 	size_t key_len;
267 	size_t block_size;
268 };
269 
270 const struct ikev2_integ_alg * ikev2_get_integ(int id);
271 int ikev2_integ_hash(int alg, const u8 *key, size_t key_len, const u8 *data,
272 		     size_t data_len, u8 *hash);
273 const struct ikev2_prf_alg * ikev2_get_prf(int id);
274 int ikev2_prf_hash(int alg, const u8 *key, size_t key_len,
275 		   size_t num_elem, const u8 *addr[], const size_t *len,
276 		   u8 *hash);
277 int ikev2_prf_plus(int alg, const u8 *key, size_t key_len,
278 		   const u8 *data, size_t data_len,
279 		   u8 *out, size_t out_len);
280 const struct ikev2_encr_alg * ikev2_get_encr(int id);
281 int ikev2_encr_encrypt(int alg, const u8 *key, size_t key_len, const u8 *iv,
282 		       const u8 *plain, u8 *crypt, size_t len);
283 int ikev2_encr_decrypt(int alg, const u8 *key, size_t key_len, const u8 *iv,
284 		       const u8 *crypt, u8 *plain, size_t len);
285 
286 int ikev2_derive_auth_data(int prf_alg, const struct wpabuf *sign_msg,
287 			   const u8 *ID, size_t ID_len, u8 ID_type,
288 			   struct ikev2_keys *keys, int initiator,
289 			   const u8 *shared_secret, size_t shared_secret_len,
290 			   const u8 *nonce, size_t nonce_len,
291 			   const u8 *key_pad, size_t key_pad_len,
292 			   u8 *auth_data);
293 
294 
295 struct ikev2_payloads {
296 	const u8 *sa;
297 	size_t sa_len;
298 	const u8 *ke;
299 	size_t ke_len;
300 	const u8 *idi;
301 	size_t idi_len;
302 	const u8 *idr;
303 	size_t idr_len;
304 	const u8 *cert;
305 	size_t cert_len;
306 	const u8 *auth;
307 	size_t auth_len;
308 	const u8 *nonce;
309 	size_t nonce_len;
310 	const u8 *encrypted;
311 	size_t encrypted_len;
312 	u8 encr_next_payload;
313 	const u8 *notification;
314 	size_t notification_len;
315 };
316 
317 int ikev2_parse_payloads(struct ikev2_payloads *payloads,
318 			 u8 next_payload, const u8 *pos, const u8 *end);
319 
320 u8 * ikev2_decrypt_payload(int encr_id, int integ_id, struct ikev2_keys *keys,
321 			   int initiator, const struct ikev2_hdr *hdr,
322 			   const u8 *encrypted, size_t encrypted_len,
323 			   size_t *res_len);
324 void ikev2_update_hdr(struct wpabuf *msg);
325 int ikev2_build_encrypted(int encr_id, int integ_id, struct ikev2_keys *keys,
326 			  int initiator, struct wpabuf *msg,
327 			  struct wpabuf *plain, u8 next_payload);
328 int ikev2_derive_sk_keys(const struct ikev2_prf_alg *prf,
329 			 const struct ikev2_integ_alg *integ,
330 			 const struct ikev2_encr_alg *encr,
331 			 const u8 *skeyseed, const u8 *data, size_t data_len,
332 			 struct ikev2_keys *keys);
333 
334 #endif /* IKEV2_COMMON_H */
335