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