00001
00016 #ifndef IKEV2_COMMON_H
00017 #define IKEV2_COMMON_H
00018
00019
00020
00021
00022
00023 #define IKEV2_NONCE_MIN_LEN 16
00024 #define IKEV2_NONCE_MAX_LEN 256
00025
00026
00027 #ifdef _MSC_VER
00028 #pragma pack(push, 1)
00029 #endif
00030
00031 #define IKEV2_SPI_LEN 8
00032
00033 struct ikev2_hdr {
00034 u8 i_spi[IKEV2_SPI_LEN];
00035 u8 r_spi[IKEV2_SPI_LEN];
00036 u8 next_payload;
00037 u8 version;
00038 u8 exchange_type;
00039 u8 flags;
00040 u8 message_id[4];
00041 u8 length[4];
00042 } STRUCT_PACKED;
00043
00044 struct ikev2_payload_hdr {
00045 u8 next_payload;
00046 u8 flags;
00047 u8 payload_length[2];
00048 } STRUCT_PACKED;
00049
00050 struct ikev2_proposal {
00051 u8 type;
00052 u8 reserved;
00053 u8 proposal_length[2];
00054 u8 proposal_num;
00055 u8 protocol_id;
00056 u8 spi_size;
00057 u8 num_transforms;
00058
00059
00060 } STRUCT_PACKED;
00061
00062 struct ikev2_transform {
00063 u8 type;
00064 u8 reserved;
00065 u8 transform_length[2];
00066 u8 transform_type;
00067 u8 reserved2;
00068 u8 transform_id[2];
00069
00070 } STRUCT_PACKED;
00071
00072 #ifdef _MSC_VER
00073 #pragma pack(pop)
00074 #endif
00075
00076
00077
00078 #define IKEV2_MjVer 2
00079 #define IKEV2_MnVer 0
00080 #ifdef CCNS_PL
00081 #define IKEV2_VERSION ((IKEV2_MjVer) | ((IKEV2_MnVer) << 4))
00082 #else
00083 #define IKEV2_VERSION (((IKEV2_MjVer) << 4) | (IKEV2_MnVer))
00084 #endif
00085
00086
00087 enum {
00088
00089 IKE_SA_INIT = 34,
00090 IKE_SA_AUTH = 35,
00091 CREATE_CHILD_SA = 36,
00092 INFORMATION = 37
00093
00094
00095 };
00096
00097
00098 #define IKEV2_HDR_INITIATOR 0x08
00099 #define IKEV2_HDR_VERSION 0x10
00100 #define IKEV2_HDR_RESPONSE 0x20
00101
00102
00103 #define IKEV2_PAYLOAD_FLAGS_CRITICAL 0x01
00104
00105
00106
00107
00108 enum {
00109 IKEV2_PAYLOAD_NO_NEXT_PAYLOAD = 0,
00110 IKEV2_PAYLOAD_SA = 33,
00111 IKEV2_PAYLOAD_KEY_EXCHANGE = 34,
00112 IKEV2_PAYLOAD_IDi = 35,
00113 IKEV2_PAYLOAD_IDr = 36,
00114 IKEV2_PAYLOAD_CERTIFICATE = 37,
00115 IKEV2_PAYLOAD_CERT_REQ = 38,
00116 IKEV2_PAYLOAD_AUTHENTICATION = 39,
00117 IKEV2_PAYLOAD_NONCE = 40,
00118 IKEV2_PAYLOAD_NOTIFICATION = 41,
00119 IKEV2_PAYLOAD_VENDOD_ID = 43,
00120 IKEV2_PAYLOAD_ENCRYPTED = 46,
00121 IKEV2_PAYLOAD_NEXT_FAST_ID = 121
00122 };
00123
00124
00125
00126 enum {
00127 IKEV2_PROTOCOL_RESERVED = 0,
00128 IKEV2_PROTOCOL_IKE = 1,
00129 IKEV2_PROTOCOL_AH = 2,
00130 IKEV2_PROTOCOL_ESP = 3
00131 };
00132
00133
00134
00135 enum {
00136 IKEV2_TRANSFORM_ENCR = 1,
00137 IKEV2_TRANSFORM_PRF = 2,
00138 IKEV2_TRANSFORM_INTEG = 3,
00139 IKEV2_TRANSFORM_DH = 4,
00140 IKEV2_TRANSFORM_ESN = 5
00141 };
00142
00143
00144 enum {
00145 ENCR_DES_IV64 = 1,
00146 ENCR_DES = 2,
00147 ENCR_3DES = 3,
00148 ENCR_RC5 = 4,
00149 ENCR_IDEA = 5,
00150 ENCR_CAST = 6,
00151 ENCR_BLOWFISH = 7,
00152 ENCR_3IDEA = 8,
00153 ENCR_DES_IV32 = 9,
00154 ENCR_NULL = 11,
00155 ENCR_AES_CBC = 12,
00156 ENCR_AES_CTR = 13
00157 };
00158
00159
00160 enum {
00161 PRF_HMAC_MD5 = 1,
00162 PRF_HMAC_SHA1 = 2,
00163 PRF_HMAC_TIGER = 3,
00164 PRF_AES128_XCBC = 4
00165 };
00166
00167
00168 enum {
00169 AUTH_HMAC_MD5_96 = 1,
00170 AUTH_HMAC_SHA1_96 = 2,
00171 AUTH_DES_MAC = 3,
00172 AUTH_KPDK_MD5 = 4,
00173 AUTH_AES_XCBC_96 = 5
00174 };
00175
00176
00177 enum {
00178 DH_GROUP1_768BIT_MODP = 1,
00179 DH_GROUP2_1024BIT_MODP = 2,
00180 DH_GROUP5_1536BIT_MODP = 5,
00181 DH_GROUP5_2048BIT_MODP = 14,
00182 DH_GROUP5_3072BIT_MODP = 15,
00183 DH_GROUP5_4096BIT_MODP = 16,
00184 DH_GROUP5_6144BIT_MODP = 17,
00185 DH_GROUP5_8192BIT_MODP = 18
00186 };
00187
00188
00189
00190 enum {
00191 ID_IPV4_ADDR = 1,
00192 ID_FQDN = 2,
00193 ID_RFC822_ADDR = 3,
00194 ID_IPV6_ADDR = 5,
00195 ID_DER_ASN1_DN = 9,
00196 ID_DER_ASN1_GN= 10,
00197 ID_KEY_ID = 11
00198 };
00199
00200
00201
00202 enum {
00203 CERT_ENCODING_PKCS7_X509 = 1,
00204 CERT_ENCODING_PGP_CERT = 2,
00205 CERT_ENCODING_DNS_SIGNED_KEY = 3,
00206
00207
00208 CERT_ENCODING_X509_CERT_SIGN = 4,
00209 CERT_ENCODING_KERBEROS_TOKEN = 6,
00210
00211 CERT_ENCODING_CRL = 7,
00212 CERT_ENCODING_ARL = 8,
00213 CERT_ENCODING_SPKI_CERT = 9,
00214 CERT_ENCODING_X509_CERT_ATTR = 10,
00215
00216 CERT_ENCODING_RAW_RSA_KEY = 11,
00217 CERT_ENCODING_HASH_AND_URL_X509_CERT = 12,
00218 CERT_ENCODING_HASH_AND_URL_X509_BUNDLE = 13
00219 };
00220
00221
00222
00223 enum {
00224 AUTH_RSA_SIGN = 1,
00225 AUTH_SHARED_KEY_MIC = 2,
00226 AUTH_DSS_SIGN = 3
00227 };
00228
00229
00230
00231 enum {
00232 UNSUPPORTED_CRITICAL_PAYLOAD = 1,
00233 INVALID_IKE_SPI = 4,
00234 INVALID_MAJOR_VERSION = 5,
00235 INVALID_SYNTAX = 7,
00236 INVALID_MESSAGE_ID = 9,
00237 INVALID_SPI = 11,
00238 NO_PROPOSAL_CHOSEN = 14,
00239 INVALID_KE_PAYLOAD = 17,
00240 AUTHENTICATION_FAILED = 24,
00241 SINGLE_PAIR_REQUIRED = 34,
00242 NO_ADDITIONAL_SAS = 35,
00243 INTERNAL_ADDRESS_FAILURE = 36,
00244 FAILED_CP_REQUIRED = 37,
00245 TS_UNACCEPTABLE = 38,
00246 INVALID_SELECTORS = 39
00247 };
00248
00249
00250 struct ikev2_keys {
00251 u8 *SK_d, *SK_ai, *SK_ar, *SK_ei, *SK_er, *SK_pi, *SK_pr;
00252 size_t SK_d_len, SK_integ_len, SK_encr_len, SK_prf_len;
00253 };
00254
00255
00256 int ikev2_keys_set(struct ikev2_keys *keys);
00257 void ikev2_free_keys(struct ikev2_keys *keys);
00258
00259
00260
00261 #define IKEV2_MAX_HASH_LEN 20
00262
00263 struct ikev2_integ_alg {
00264 int id;
00265 size_t key_len;
00266 size_t hash_len;
00267 };
00268
00269 struct ikev2_prf_alg {
00270 int id;
00271 size_t key_len;
00272 size_t hash_len;
00273 };
00274
00275 struct ikev2_encr_alg {
00276 int id;
00277 size_t key_len;
00278 size_t block_size;
00279 };
00280
00281 const struct ikev2_integ_alg * ikev2_get_integ(int id);
00282 int ikev2_integ_hash(int alg, const u8 *key, size_t key_len, const u8 *data,
00283 size_t data_len, u8 *hash);
00284 const struct ikev2_prf_alg * ikev2_get_prf(int id);
00285 int ikev2_prf_hash(int alg, const u8 *key, size_t key_len,
00286 size_t num_elem, const u8 *addr[], const size_t *len,
00287 u8 *hash);
00288 int ikev2_prf_plus(int alg, const u8 *key, size_t key_len,
00289 const u8 *data, size_t data_len,
00290 u8 *out, size_t out_len);
00291 const struct ikev2_encr_alg * ikev2_get_encr(int id);
00292 int ikev2_encr_encrypt(int alg, const u8 *key, size_t key_len, const u8 *iv,
00293 const u8 *plain, u8 *crypt, size_t len);
00294 int ikev2_encr_decrypt(int alg, const u8 *key, size_t key_len, const u8 *iv,
00295 const u8 *crypt, u8 *plain, size_t len);
00296
00297 int ikev2_derive_auth_data(int prf_alg, const struct wpabuf *sign_msg,
00298 const u8 *ID, size_t ID_len, u8 ID_type,
00299 struct ikev2_keys *keys, int initiator,
00300 const u8 *shared_secret, size_t shared_secret_len,
00301 const u8 *nonce, size_t nonce_len,
00302 const u8 *key_pad, size_t key_pad_len,
00303 u8 *auth_data);
00304
00305
00306 struct ikev2_payloads {
00307 const u8 *sa;
00308 size_t sa_len;
00309 const u8 *ke;
00310 size_t ke_len;
00311 const u8 *idi;
00312 size_t idi_len;
00313 const u8 *idr;
00314 size_t idr_len;
00315 const u8 *cert;
00316 size_t cert_len;
00317 const u8 *auth;
00318 size_t auth_len;
00319 const u8 *nonce;
00320 size_t nonce_len;
00321 const u8 *encrypted;
00322 size_t encrypted_len;
00323 u8 encr_next_payload;
00324 const u8 *notification;
00325 size_t notification_len;
00326 };
00327
00328 int ikev2_parse_payloads(struct ikev2_payloads *payloads,
00329 u8 next_payload, const u8 *pos, const u8 *end);
00330
00331 u8 * ikev2_decrypt_payload(int encr_id, int integ_id, struct ikev2_keys *keys,
00332 int initiator, const struct ikev2_hdr *hdr,
00333 const u8 *encrypted, size_t encrypted_len,
00334 size_t *res_len);
00335 void ikev2_update_hdr(struct wpabuf *msg);
00336 int ikev2_build_encrypted(int encr_id, int integ_id, struct ikev2_keys *keys,
00337 int initiator, struct wpabuf *msg,
00338 struct wpabuf *plain, u8 next_payload);
00339 int ikev2_derive_sk_keys(const struct ikev2_prf_alg *prf,
00340 const struct ikev2_integ_alg *integ,
00341 const struct ikev2_encr_alg *encr,
00342 const u8 *skeyseed, const u8 *data, size_t data_len,
00343 struct ikev2_keys *keys);
00344
00345 #endif
00346