139beb93cSSam Leffler /*
239beb93cSSam Leffler * IKEv2 common routines for initiator and responder
339beb93cSSam Leffler * Copyright (c) 2007, Jouni Malinen <j@w1.fi>
439beb93cSSam Leffler *
5f05cddf9SRui Paulo * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo * See README for more details.
739beb93cSSam Leffler */
839beb93cSSam Leffler
939beb93cSSam Leffler #include "includes.h"
1039beb93cSSam Leffler
1139beb93cSSam Leffler #include "common.h"
12e28a4053SRui Paulo #include "crypto/crypto.h"
13e28a4053SRui Paulo #include "crypto/md5.h"
14e28a4053SRui Paulo #include "crypto/sha1.h"
15f05cddf9SRui Paulo #include "crypto/random.h"
1639beb93cSSam Leffler #include "ikev2_common.h"
1739beb93cSSam Leffler
1839beb93cSSam Leffler
19325151a3SRui Paulo static const struct ikev2_integ_alg ikev2_integ_algs[] = {
2039beb93cSSam Leffler { AUTH_HMAC_SHA1_96, 20, 12 },
2139beb93cSSam Leffler { AUTH_HMAC_MD5_96, 16, 12 }
2239beb93cSSam Leffler };
2339beb93cSSam Leffler
245b9c547cSRui Paulo #define NUM_INTEG_ALGS ARRAY_SIZE(ikev2_integ_algs)
2539beb93cSSam Leffler
2639beb93cSSam Leffler
27325151a3SRui Paulo static const struct ikev2_prf_alg ikev2_prf_algs[] = {
2839beb93cSSam Leffler { PRF_HMAC_SHA1, 20, 20 },
2939beb93cSSam Leffler { PRF_HMAC_MD5, 16, 16 }
3039beb93cSSam Leffler };
3139beb93cSSam Leffler
325b9c547cSRui Paulo #define NUM_PRF_ALGS ARRAY_SIZE(ikev2_prf_algs)
3339beb93cSSam Leffler
3439beb93cSSam Leffler
35325151a3SRui Paulo static const struct ikev2_encr_alg ikev2_encr_algs[] = {
3639beb93cSSam Leffler { ENCR_AES_CBC, 16, 16 }, /* only 128-bit keys supported for now */
3739beb93cSSam Leffler { ENCR_3DES, 24, 8 }
3839beb93cSSam Leffler };
3939beb93cSSam Leffler
405b9c547cSRui Paulo #define NUM_ENCR_ALGS ARRAY_SIZE(ikev2_encr_algs)
4139beb93cSSam Leffler
4239beb93cSSam Leffler
ikev2_get_integ(int id)4339beb93cSSam Leffler const struct ikev2_integ_alg * ikev2_get_integ(int id)
4439beb93cSSam Leffler {
4539beb93cSSam Leffler size_t i;
4639beb93cSSam Leffler
4739beb93cSSam Leffler for (i = 0; i < NUM_INTEG_ALGS; i++) {
4839beb93cSSam Leffler if (ikev2_integ_algs[i].id == id)
4939beb93cSSam Leffler return &ikev2_integ_algs[i];
5039beb93cSSam Leffler }
5139beb93cSSam Leffler
5239beb93cSSam Leffler return NULL;
5339beb93cSSam Leffler }
5439beb93cSSam Leffler
5539beb93cSSam Leffler
ikev2_integ_hash(int alg,const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * hash)5639beb93cSSam Leffler int ikev2_integ_hash(int alg, const u8 *key, size_t key_len, const u8 *data,
5739beb93cSSam Leffler size_t data_len, u8 *hash)
5839beb93cSSam Leffler {
5939beb93cSSam Leffler u8 tmphash[IKEV2_MAX_HASH_LEN];
6039beb93cSSam Leffler
6139beb93cSSam Leffler switch (alg) {
6239beb93cSSam Leffler case AUTH_HMAC_SHA1_96:
6339beb93cSSam Leffler if (key_len != 20)
6439beb93cSSam Leffler return -1;
65*780fb4a2SCy Schubert if (hmac_sha1(key, key_len, data, data_len, tmphash) < 0)
66*780fb4a2SCy Schubert return -1;
6739beb93cSSam Leffler os_memcpy(hash, tmphash, 12);
6839beb93cSSam Leffler break;
6939beb93cSSam Leffler case AUTH_HMAC_MD5_96:
7039beb93cSSam Leffler if (key_len != 16)
7139beb93cSSam Leffler return -1;
72*780fb4a2SCy Schubert if (hmac_md5(key, key_len, data, data_len, tmphash) < 0)
73*780fb4a2SCy Schubert return -1;
7439beb93cSSam Leffler os_memcpy(hash, tmphash, 12);
7539beb93cSSam Leffler break;
7639beb93cSSam Leffler default:
7739beb93cSSam Leffler return -1;
7839beb93cSSam Leffler }
7939beb93cSSam Leffler
8039beb93cSSam Leffler return 0;
8139beb93cSSam Leffler }
8239beb93cSSam Leffler
8339beb93cSSam Leffler
ikev2_get_prf(int id)8439beb93cSSam Leffler const struct ikev2_prf_alg * ikev2_get_prf(int id)
8539beb93cSSam Leffler {
8639beb93cSSam Leffler size_t i;
8739beb93cSSam Leffler
8839beb93cSSam Leffler for (i = 0; i < NUM_PRF_ALGS; i++) {
8939beb93cSSam Leffler if (ikev2_prf_algs[i].id == id)
9039beb93cSSam Leffler return &ikev2_prf_algs[i];
9139beb93cSSam Leffler }
9239beb93cSSam Leffler
9339beb93cSSam Leffler return NULL;
9439beb93cSSam Leffler }
9539beb93cSSam Leffler
9639beb93cSSam Leffler
ikev2_prf_hash(int alg,const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * hash)9739beb93cSSam Leffler int ikev2_prf_hash(int alg, const u8 *key, size_t key_len,
9839beb93cSSam Leffler size_t num_elem, const u8 *addr[], const size_t *len,
9939beb93cSSam Leffler u8 *hash)
10039beb93cSSam Leffler {
10139beb93cSSam Leffler switch (alg) {
10239beb93cSSam Leffler case PRF_HMAC_SHA1:
103*780fb4a2SCy Schubert return hmac_sha1_vector(key, key_len, num_elem, addr, len,
104*780fb4a2SCy Schubert hash);
10539beb93cSSam Leffler case PRF_HMAC_MD5:
106*780fb4a2SCy Schubert return hmac_md5_vector(key, key_len, num_elem, addr, len, hash);
10739beb93cSSam Leffler default:
10839beb93cSSam Leffler return -1;
10939beb93cSSam Leffler }
11039beb93cSSam Leffler }
11139beb93cSSam Leffler
11239beb93cSSam Leffler
ikev2_prf_plus(int alg,const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * out,size_t out_len)11339beb93cSSam Leffler int ikev2_prf_plus(int alg, const u8 *key, size_t key_len,
11439beb93cSSam Leffler const u8 *data, size_t data_len,
11539beb93cSSam Leffler u8 *out, size_t out_len)
11639beb93cSSam Leffler {
11739beb93cSSam Leffler u8 hash[IKEV2_MAX_HASH_LEN];
11839beb93cSSam Leffler size_t hash_len;
11939beb93cSSam Leffler u8 iter, *pos, *end;
12039beb93cSSam Leffler const u8 *addr[3];
12139beb93cSSam Leffler size_t len[3];
12239beb93cSSam Leffler const struct ikev2_prf_alg *prf;
12339beb93cSSam Leffler int res;
12439beb93cSSam Leffler
12539beb93cSSam Leffler prf = ikev2_get_prf(alg);
12639beb93cSSam Leffler if (prf == NULL)
12739beb93cSSam Leffler return -1;
12839beb93cSSam Leffler hash_len = prf->hash_len;
12939beb93cSSam Leffler
13039beb93cSSam Leffler addr[0] = hash;
13139beb93cSSam Leffler len[0] = hash_len;
13239beb93cSSam Leffler addr[1] = data;
13339beb93cSSam Leffler len[1] = data_len;
13439beb93cSSam Leffler addr[2] = &iter;
13539beb93cSSam Leffler len[2] = 1;
13639beb93cSSam Leffler
13739beb93cSSam Leffler pos = out;
13839beb93cSSam Leffler end = out + out_len;
13939beb93cSSam Leffler iter = 1;
14039beb93cSSam Leffler while (pos < end) {
14139beb93cSSam Leffler size_t clen;
14239beb93cSSam Leffler if (iter == 1)
14339beb93cSSam Leffler res = ikev2_prf_hash(alg, key, key_len, 2, &addr[1],
14439beb93cSSam Leffler &len[1], hash);
14539beb93cSSam Leffler else
14639beb93cSSam Leffler res = ikev2_prf_hash(alg, key, key_len, 3, addr, len,
14739beb93cSSam Leffler hash);
14839beb93cSSam Leffler if (res < 0)
14939beb93cSSam Leffler return -1;
15039beb93cSSam Leffler clen = hash_len;
15139beb93cSSam Leffler if ((int) clen > end - pos)
15239beb93cSSam Leffler clen = end - pos;
15339beb93cSSam Leffler os_memcpy(pos, hash, clen);
15439beb93cSSam Leffler pos += clen;
15539beb93cSSam Leffler iter++;
15639beb93cSSam Leffler }
15739beb93cSSam Leffler
15839beb93cSSam Leffler return 0;
15939beb93cSSam Leffler }
16039beb93cSSam Leffler
16139beb93cSSam Leffler
ikev2_get_encr(int id)16239beb93cSSam Leffler const struct ikev2_encr_alg * ikev2_get_encr(int id)
16339beb93cSSam Leffler {
16439beb93cSSam Leffler size_t i;
16539beb93cSSam Leffler
16639beb93cSSam Leffler for (i = 0; i < NUM_ENCR_ALGS; i++) {
16739beb93cSSam Leffler if (ikev2_encr_algs[i].id == id)
16839beb93cSSam Leffler return &ikev2_encr_algs[i];
16939beb93cSSam Leffler }
17039beb93cSSam Leffler
17139beb93cSSam Leffler return NULL;
17239beb93cSSam Leffler }
17339beb93cSSam Leffler
17439beb93cSSam Leffler
ikev2_encr_encrypt(int alg,const u8 * key,size_t key_len,const u8 * iv,const u8 * plain,u8 * crypt,size_t len)17539beb93cSSam Leffler int ikev2_encr_encrypt(int alg, const u8 *key, size_t key_len, const u8 *iv,
17639beb93cSSam Leffler const u8 *plain, u8 *crypt, size_t len)
17739beb93cSSam Leffler {
17839beb93cSSam Leffler struct crypto_cipher *cipher;
17939beb93cSSam Leffler int encr_alg;
18039beb93cSSam Leffler
18139beb93cSSam Leffler switch (alg) {
18239beb93cSSam Leffler case ENCR_3DES:
18339beb93cSSam Leffler encr_alg = CRYPTO_CIPHER_ALG_3DES;
18439beb93cSSam Leffler break;
18539beb93cSSam Leffler case ENCR_AES_CBC:
18639beb93cSSam Leffler encr_alg = CRYPTO_CIPHER_ALG_AES;
18739beb93cSSam Leffler break;
18839beb93cSSam Leffler default:
18939beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Unsupported encr alg %d", alg);
19039beb93cSSam Leffler return -1;
19139beb93cSSam Leffler }
19239beb93cSSam Leffler
19339beb93cSSam Leffler cipher = crypto_cipher_init(encr_alg, iv, key, key_len);
19439beb93cSSam Leffler if (cipher == NULL) {
19539beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Failed to initialize cipher");
19639beb93cSSam Leffler return -1;
19739beb93cSSam Leffler }
19839beb93cSSam Leffler
19939beb93cSSam Leffler if (crypto_cipher_encrypt(cipher, plain, crypt, len) < 0) {
20039beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Encryption failed");
20139beb93cSSam Leffler crypto_cipher_deinit(cipher);
20239beb93cSSam Leffler return -1;
20339beb93cSSam Leffler }
20439beb93cSSam Leffler crypto_cipher_deinit(cipher);
20539beb93cSSam Leffler
20639beb93cSSam Leffler return 0;
20739beb93cSSam Leffler }
20839beb93cSSam Leffler
20939beb93cSSam Leffler
ikev2_encr_decrypt(int alg,const u8 * key,size_t key_len,const u8 * iv,const u8 * crypt,u8 * plain,size_t len)21039beb93cSSam Leffler int ikev2_encr_decrypt(int alg, const u8 *key, size_t key_len, const u8 *iv,
21139beb93cSSam Leffler const u8 *crypt, u8 *plain, size_t len)
21239beb93cSSam Leffler {
21339beb93cSSam Leffler struct crypto_cipher *cipher;
21439beb93cSSam Leffler int encr_alg;
21539beb93cSSam Leffler
21639beb93cSSam Leffler switch (alg) {
21739beb93cSSam Leffler case ENCR_3DES:
21839beb93cSSam Leffler encr_alg = CRYPTO_CIPHER_ALG_3DES;
21939beb93cSSam Leffler break;
22039beb93cSSam Leffler case ENCR_AES_CBC:
22139beb93cSSam Leffler encr_alg = CRYPTO_CIPHER_ALG_AES;
22239beb93cSSam Leffler break;
22339beb93cSSam Leffler default:
22439beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Unsupported encr alg %d", alg);
22539beb93cSSam Leffler return -1;
22639beb93cSSam Leffler }
22739beb93cSSam Leffler
22839beb93cSSam Leffler cipher = crypto_cipher_init(encr_alg, iv, key, key_len);
22939beb93cSSam Leffler if (cipher == NULL) {
23039beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Failed to initialize cipher");
23139beb93cSSam Leffler return -1;
23239beb93cSSam Leffler }
23339beb93cSSam Leffler
23439beb93cSSam Leffler if (crypto_cipher_decrypt(cipher, crypt, plain, len) < 0) {
23539beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Decryption failed");
23639beb93cSSam Leffler crypto_cipher_deinit(cipher);
23739beb93cSSam Leffler return -1;
23839beb93cSSam Leffler }
23939beb93cSSam Leffler crypto_cipher_deinit(cipher);
24039beb93cSSam Leffler
24139beb93cSSam Leffler return 0;
24239beb93cSSam Leffler }
24339beb93cSSam Leffler
24439beb93cSSam Leffler
ikev2_parse_payloads(struct ikev2_payloads * payloads,u8 next_payload,const u8 * pos,const u8 * end)24539beb93cSSam Leffler int ikev2_parse_payloads(struct ikev2_payloads *payloads,
24639beb93cSSam Leffler u8 next_payload, const u8 *pos, const u8 *end)
24739beb93cSSam Leffler {
24839beb93cSSam Leffler const struct ikev2_payload_hdr *phdr;
24939beb93cSSam Leffler
25039beb93cSSam Leffler os_memset(payloads, 0, sizeof(*payloads));
25139beb93cSSam Leffler
25239beb93cSSam Leffler while (next_payload != IKEV2_PAYLOAD_NO_NEXT_PAYLOAD) {
2535b9c547cSRui Paulo unsigned int plen, pdatalen, left;
25439beb93cSSam Leffler const u8 *pdata;
25539beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Processing payload %u",
25639beb93cSSam Leffler next_payload);
2575b9c547cSRui Paulo if (end < pos)
2585b9c547cSRui Paulo return -1;
2595b9c547cSRui Paulo left = end - pos;
2605b9c547cSRui Paulo if (left < sizeof(*phdr)) {
26139beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Too short message for "
26239beb93cSSam Leffler "payload header (left=%ld)",
26339beb93cSSam Leffler (long) (end - pos));
2645b9c547cSRui Paulo return -1;
26539beb93cSSam Leffler }
26639beb93cSSam Leffler phdr = (const struct ikev2_payload_hdr *) pos;
26739beb93cSSam Leffler plen = WPA_GET_BE16(phdr->payload_length);
2685b9c547cSRui Paulo if (plen < sizeof(*phdr) || plen > left) {
26939beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Invalid payload header "
27039beb93cSSam Leffler "length %d", plen);
27139beb93cSSam Leffler return -1;
27239beb93cSSam Leffler }
27339beb93cSSam Leffler
27439beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Next Payload: %u Flags: 0x%x"
2755b9c547cSRui Paulo " Payload Length: %u",
27639beb93cSSam Leffler phdr->next_payload, phdr->flags, plen);
27739beb93cSSam Leffler
27839beb93cSSam Leffler pdata = (const u8 *) (phdr + 1);
27939beb93cSSam Leffler pdatalen = plen - sizeof(*phdr);
28039beb93cSSam Leffler
28139beb93cSSam Leffler switch (next_payload) {
28239beb93cSSam Leffler case IKEV2_PAYLOAD_SA:
28339beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Payload: Security "
28439beb93cSSam Leffler "Association");
28539beb93cSSam Leffler payloads->sa = pdata;
28639beb93cSSam Leffler payloads->sa_len = pdatalen;
28739beb93cSSam Leffler break;
28839beb93cSSam Leffler case IKEV2_PAYLOAD_KEY_EXCHANGE:
28939beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Payload: Key "
29039beb93cSSam Leffler "Exchange");
29139beb93cSSam Leffler payloads->ke = pdata;
29239beb93cSSam Leffler payloads->ke_len = pdatalen;
29339beb93cSSam Leffler break;
29439beb93cSSam Leffler case IKEV2_PAYLOAD_IDi:
29539beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Payload: IDi");
29639beb93cSSam Leffler payloads->idi = pdata;
29739beb93cSSam Leffler payloads->idi_len = pdatalen;
29839beb93cSSam Leffler break;
29939beb93cSSam Leffler case IKEV2_PAYLOAD_IDr:
30039beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Payload: IDr");
30139beb93cSSam Leffler payloads->idr = pdata;
30239beb93cSSam Leffler payloads->idr_len = pdatalen;
30339beb93cSSam Leffler break;
30439beb93cSSam Leffler case IKEV2_PAYLOAD_CERTIFICATE:
30539beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Payload: Certificate");
30639beb93cSSam Leffler payloads->cert = pdata;
30739beb93cSSam Leffler payloads->cert_len = pdatalen;
30839beb93cSSam Leffler break;
30939beb93cSSam Leffler case IKEV2_PAYLOAD_AUTHENTICATION:
31039beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Payload: "
31139beb93cSSam Leffler "Authentication");
31239beb93cSSam Leffler payloads->auth = pdata;
31339beb93cSSam Leffler payloads->auth_len = pdatalen;
31439beb93cSSam Leffler break;
31539beb93cSSam Leffler case IKEV2_PAYLOAD_NONCE:
31639beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Payload: Nonce");
31739beb93cSSam Leffler payloads->nonce = pdata;
31839beb93cSSam Leffler payloads->nonce_len = pdatalen;
31939beb93cSSam Leffler break;
32039beb93cSSam Leffler case IKEV2_PAYLOAD_ENCRYPTED:
32139beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Payload: Encrypted");
32239beb93cSSam Leffler payloads->encrypted = pdata;
32339beb93cSSam Leffler payloads->encrypted_len = pdatalen;
32439beb93cSSam Leffler break;
32539beb93cSSam Leffler case IKEV2_PAYLOAD_NOTIFICATION:
32639beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Payload: "
32739beb93cSSam Leffler "Notification");
32839beb93cSSam Leffler payloads->notification = pdata;
32939beb93cSSam Leffler payloads->notification_len = pdatalen;
33039beb93cSSam Leffler break;
33139beb93cSSam Leffler default:
33239beb93cSSam Leffler if (phdr->flags & IKEV2_PAYLOAD_FLAGS_CRITICAL) {
33339beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unsupported "
33439beb93cSSam Leffler "critical payload %u - reject the "
33539beb93cSSam Leffler "entire message", next_payload);
33639beb93cSSam Leffler return -1;
33739beb93cSSam Leffler } else {
33839beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Skipped "
33939beb93cSSam Leffler "unsupported payload %u",
34039beb93cSSam Leffler next_payload);
34139beb93cSSam Leffler }
34239beb93cSSam Leffler }
34339beb93cSSam Leffler
34439beb93cSSam Leffler if (next_payload == IKEV2_PAYLOAD_ENCRYPTED &&
34539beb93cSSam Leffler pos + plen == end) {
34639beb93cSSam Leffler /*
34739beb93cSSam Leffler * Next Payload in the case of Encrypted Payload is
34839beb93cSSam Leffler * actually the payload type for the first embedded
34939beb93cSSam Leffler * payload.
35039beb93cSSam Leffler */
35139beb93cSSam Leffler payloads->encr_next_payload = phdr->next_payload;
35239beb93cSSam Leffler next_payload = IKEV2_PAYLOAD_NO_NEXT_PAYLOAD;
35339beb93cSSam Leffler } else
35439beb93cSSam Leffler next_payload = phdr->next_payload;
35539beb93cSSam Leffler
35639beb93cSSam Leffler pos += plen;
35739beb93cSSam Leffler }
35839beb93cSSam Leffler
35939beb93cSSam Leffler if (pos != end) {
36039beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unexpected extra data after "
36139beb93cSSam Leffler "payloads");
36239beb93cSSam Leffler return -1;
36339beb93cSSam Leffler }
36439beb93cSSam Leffler
36539beb93cSSam Leffler return 0;
36639beb93cSSam Leffler }
36739beb93cSSam Leffler
36839beb93cSSam Leffler
ikev2_derive_auth_data(int prf_alg,const struct wpabuf * sign_msg,const u8 * ID,size_t ID_len,u8 ID_type,struct ikev2_keys * keys,int initiator,const u8 * shared_secret,size_t shared_secret_len,const u8 * nonce,size_t nonce_len,const u8 * key_pad,size_t key_pad_len,u8 * auth_data)36939beb93cSSam Leffler int ikev2_derive_auth_data(int prf_alg, const struct wpabuf *sign_msg,
37039beb93cSSam Leffler const u8 *ID, size_t ID_len, u8 ID_type,
37139beb93cSSam Leffler struct ikev2_keys *keys, int initiator,
37239beb93cSSam Leffler const u8 *shared_secret, size_t shared_secret_len,
37339beb93cSSam Leffler const u8 *nonce, size_t nonce_len,
37439beb93cSSam Leffler const u8 *key_pad, size_t key_pad_len,
37539beb93cSSam Leffler u8 *auth_data)
37639beb93cSSam Leffler {
37739beb93cSSam Leffler size_t sign_len, buf_len;
37839beb93cSSam Leffler u8 *sign_data, *pos, *buf, hash[IKEV2_MAX_HASH_LEN];
37939beb93cSSam Leffler const struct ikev2_prf_alg *prf;
38039beb93cSSam Leffler const u8 *SK_p = initiator ? keys->SK_pi : keys->SK_pr;
38139beb93cSSam Leffler
38239beb93cSSam Leffler prf = ikev2_get_prf(prf_alg);
38339beb93cSSam Leffler if (sign_msg == NULL || ID == NULL || SK_p == NULL ||
38439beb93cSSam Leffler shared_secret == NULL || nonce == NULL || prf == NULL)
38539beb93cSSam Leffler return -1;
38639beb93cSSam Leffler
38739beb93cSSam Leffler /* prf(SK_pi/r,IDi/r') */
38839beb93cSSam Leffler buf_len = 4 + ID_len;
38939beb93cSSam Leffler buf = os_zalloc(buf_len);
39039beb93cSSam Leffler if (buf == NULL)
39139beb93cSSam Leffler return -1;
39239beb93cSSam Leffler buf[0] = ID_type;
39339beb93cSSam Leffler os_memcpy(buf + 4, ID, ID_len);
39439beb93cSSam Leffler if (ikev2_prf_hash(prf->id, SK_p, keys->SK_prf_len,
39539beb93cSSam Leffler 1, (const u8 **) &buf, &buf_len, hash) < 0) {
39639beb93cSSam Leffler os_free(buf);
39739beb93cSSam Leffler return -1;
39839beb93cSSam Leffler }
39939beb93cSSam Leffler os_free(buf);
40039beb93cSSam Leffler
40139beb93cSSam Leffler /* sign_data = msg | Nr/i | prf(SK_pi/r,IDi/r') */
40239beb93cSSam Leffler sign_len = wpabuf_len(sign_msg) + nonce_len + prf->hash_len;
40339beb93cSSam Leffler sign_data = os_malloc(sign_len);
40439beb93cSSam Leffler if (sign_data == NULL)
40539beb93cSSam Leffler return -1;
40639beb93cSSam Leffler pos = sign_data;
40739beb93cSSam Leffler os_memcpy(pos, wpabuf_head(sign_msg), wpabuf_len(sign_msg));
40839beb93cSSam Leffler pos += wpabuf_len(sign_msg);
40939beb93cSSam Leffler os_memcpy(pos, nonce, nonce_len);
41039beb93cSSam Leffler pos += nonce_len;
41139beb93cSSam Leffler os_memcpy(pos, hash, prf->hash_len);
41239beb93cSSam Leffler
41339beb93cSSam Leffler /* AUTH = prf(prf(Shared Secret, key pad, sign_data) */
41439beb93cSSam Leffler if (ikev2_prf_hash(prf->id, shared_secret, shared_secret_len, 1,
41539beb93cSSam Leffler &key_pad, &key_pad_len, hash) < 0 ||
41639beb93cSSam Leffler ikev2_prf_hash(prf->id, hash, prf->hash_len, 1,
41739beb93cSSam Leffler (const u8 **) &sign_data, &sign_len, auth_data) < 0)
41839beb93cSSam Leffler {
41939beb93cSSam Leffler os_free(sign_data);
42039beb93cSSam Leffler return -1;
42139beb93cSSam Leffler }
42239beb93cSSam Leffler os_free(sign_data);
42339beb93cSSam Leffler
42439beb93cSSam Leffler return 0;
42539beb93cSSam Leffler }
42639beb93cSSam Leffler
42739beb93cSSam Leffler
ikev2_decrypt_payload(int encr_id,int integ_id,struct ikev2_keys * keys,int initiator,const struct ikev2_hdr * hdr,const u8 * encrypted,size_t encrypted_len,size_t * res_len)42839beb93cSSam Leffler u8 * ikev2_decrypt_payload(int encr_id, int integ_id,
42939beb93cSSam Leffler struct ikev2_keys *keys, int initiator,
43039beb93cSSam Leffler const struct ikev2_hdr *hdr,
43139beb93cSSam Leffler const u8 *encrypted, size_t encrypted_len,
43239beb93cSSam Leffler size_t *res_len)
43339beb93cSSam Leffler {
43439beb93cSSam Leffler size_t iv_len;
43539beb93cSSam Leffler const u8 *pos, *end, *iv, *integ;
43639beb93cSSam Leffler u8 hash[IKEV2_MAX_HASH_LEN], *decrypted;
43739beb93cSSam Leffler size_t decrypted_len, pad_len;
43839beb93cSSam Leffler const struct ikev2_integ_alg *integ_alg;
43939beb93cSSam Leffler const struct ikev2_encr_alg *encr_alg;
44039beb93cSSam Leffler const u8 *SK_e = initiator ? keys->SK_ei : keys->SK_er;
44139beb93cSSam Leffler const u8 *SK_a = initiator ? keys->SK_ai : keys->SK_ar;
44239beb93cSSam Leffler
44339beb93cSSam Leffler if (encrypted == NULL) {
44439beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: No Encrypted payload in SA_AUTH");
44539beb93cSSam Leffler return NULL;
44639beb93cSSam Leffler }
44739beb93cSSam Leffler
44839beb93cSSam Leffler encr_alg = ikev2_get_encr(encr_id);
44939beb93cSSam Leffler if (encr_alg == NULL) {
45039beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unsupported encryption type");
45139beb93cSSam Leffler return NULL;
45239beb93cSSam Leffler }
45339beb93cSSam Leffler iv_len = encr_alg->block_size;
45439beb93cSSam Leffler
45539beb93cSSam Leffler integ_alg = ikev2_get_integ(integ_id);
45639beb93cSSam Leffler if (integ_alg == NULL) {
45739beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unsupported intergrity type");
45839beb93cSSam Leffler return NULL;
45939beb93cSSam Leffler }
46039beb93cSSam Leffler
46139beb93cSSam Leffler if (encrypted_len < iv_len + 1 + integ_alg->hash_len) {
46239beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: No room for IV or Integrity "
46339beb93cSSam Leffler "Checksum");
46439beb93cSSam Leffler return NULL;
46539beb93cSSam Leffler }
46639beb93cSSam Leffler
46739beb93cSSam Leffler iv = encrypted;
46839beb93cSSam Leffler pos = iv + iv_len;
46939beb93cSSam Leffler end = encrypted + encrypted_len;
47039beb93cSSam Leffler integ = end - integ_alg->hash_len;
47139beb93cSSam Leffler
47239beb93cSSam Leffler if (SK_a == NULL) {
47339beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: No SK_a available");
47439beb93cSSam Leffler return NULL;
47539beb93cSSam Leffler }
47639beb93cSSam Leffler if (ikev2_integ_hash(integ_id, SK_a, keys->SK_integ_len,
47739beb93cSSam Leffler (const u8 *) hdr,
47839beb93cSSam Leffler integ - (const u8 *) hdr, hash) < 0) {
47939beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Failed to calculate integrity "
48039beb93cSSam Leffler "hash");
48139beb93cSSam Leffler return NULL;
48239beb93cSSam Leffler }
4835b9c547cSRui Paulo if (os_memcmp_const(integ, hash, integ_alg->hash_len) != 0) {
48439beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Incorrect Integrity Checksum "
48539beb93cSSam Leffler "Data");
48639beb93cSSam Leffler return NULL;
48739beb93cSSam Leffler }
48839beb93cSSam Leffler
48939beb93cSSam Leffler if (SK_e == NULL) {
49039beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: No SK_e available");
49139beb93cSSam Leffler return NULL;
49239beb93cSSam Leffler }
49339beb93cSSam Leffler
49439beb93cSSam Leffler decrypted_len = integ - pos;
49539beb93cSSam Leffler decrypted = os_malloc(decrypted_len);
49639beb93cSSam Leffler if (decrypted == NULL)
49739beb93cSSam Leffler return NULL;
49839beb93cSSam Leffler
49939beb93cSSam Leffler if (ikev2_encr_decrypt(encr_alg->id, SK_e, keys->SK_encr_len, iv, pos,
50039beb93cSSam Leffler decrypted, decrypted_len) < 0) {
50139beb93cSSam Leffler os_free(decrypted);
50239beb93cSSam Leffler return NULL;
50339beb93cSSam Leffler }
50439beb93cSSam Leffler
50539beb93cSSam Leffler pad_len = decrypted[decrypted_len - 1];
50639beb93cSSam Leffler if (decrypted_len < pad_len + 1) {
50739beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Invalid padding in encrypted "
50839beb93cSSam Leffler "payload");
50939beb93cSSam Leffler os_free(decrypted);
51039beb93cSSam Leffler return NULL;
51139beb93cSSam Leffler }
51239beb93cSSam Leffler
51339beb93cSSam Leffler decrypted_len -= pad_len + 1;
51439beb93cSSam Leffler
51539beb93cSSam Leffler *res_len = decrypted_len;
51639beb93cSSam Leffler return decrypted;
51739beb93cSSam Leffler }
51839beb93cSSam Leffler
51939beb93cSSam Leffler
ikev2_update_hdr(struct wpabuf * msg)52039beb93cSSam Leffler void ikev2_update_hdr(struct wpabuf *msg)
52139beb93cSSam Leffler {
52239beb93cSSam Leffler struct ikev2_hdr *hdr;
52339beb93cSSam Leffler
52439beb93cSSam Leffler /* Update lenth field in HDR */
52539beb93cSSam Leffler hdr = wpabuf_mhead(msg);
52639beb93cSSam Leffler WPA_PUT_BE32(hdr->length, wpabuf_len(msg));
52739beb93cSSam Leffler }
52839beb93cSSam Leffler
52939beb93cSSam Leffler
ikev2_build_encrypted(int encr_id,int integ_id,struct ikev2_keys * keys,int initiator,struct wpabuf * msg,struct wpabuf * plain,u8 next_payload)53039beb93cSSam Leffler int ikev2_build_encrypted(int encr_id, int integ_id, struct ikev2_keys *keys,
53139beb93cSSam Leffler int initiator, struct wpabuf *msg,
53239beb93cSSam Leffler struct wpabuf *plain, u8 next_payload)
53339beb93cSSam Leffler {
53439beb93cSSam Leffler struct ikev2_payload_hdr *phdr;
53539beb93cSSam Leffler size_t plen;
53639beb93cSSam Leffler size_t iv_len, pad_len;
53739beb93cSSam Leffler u8 *icv, *iv;
53839beb93cSSam Leffler const struct ikev2_integ_alg *integ_alg;
53939beb93cSSam Leffler const struct ikev2_encr_alg *encr_alg;
54039beb93cSSam Leffler const u8 *SK_e = initiator ? keys->SK_ei : keys->SK_er;
54139beb93cSSam Leffler const u8 *SK_a = initiator ? keys->SK_ai : keys->SK_ar;
54239beb93cSSam Leffler
54339beb93cSSam Leffler wpa_printf(MSG_DEBUG, "IKEV2: Adding Encrypted payload");
54439beb93cSSam Leffler
54539beb93cSSam Leffler /* Encr - RFC 4306, Sect. 3.14 */
54639beb93cSSam Leffler
54739beb93cSSam Leffler encr_alg = ikev2_get_encr(encr_id);
54839beb93cSSam Leffler if (encr_alg == NULL) {
54939beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unsupported encryption type");
55039beb93cSSam Leffler return -1;
55139beb93cSSam Leffler }
55239beb93cSSam Leffler iv_len = encr_alg->block_size;
55339beb93cSSam Leffler
55439beb93cSSam Leffler integ_alg = ikev2_get_integ(integ_id);
55539beb93cSSam Leffler if (integ_alg == NULL) {
55639beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Unsupported intergrity type");
55739beb93cSSam Leffler return -1;
55839beb93cSSam Leffler }
55939beb93cSSam Leffler
56039beb93cSSam Leffler if (SK_e == NULL) {
56139beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: No SK_e available");
56239beb93cSSam Leffler return -1;
56339beb93cSSam Leffler }
56439beb93cSSam Leffler
56539beb93cSSam Leffler if (SK_a == NULL) {
56639beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: No SK_a available");
56739beb93cSSam Leffler return -1;
56839beb93cSSam Leffler }
56939beb93cSSam Leffler
57039beb93cSSam Leffler phdr = wpabuf_put(msg, sizeof(*phdr));
57139beb93cSSam Leffler phdr->next_payload = next_payload;
57239beb93cSSam Leffler phdr->flags = 0;
57339beb93cSSam Leffler
57439beb93cSSam Leffler iv = wpabuf_put(msg, iv_len);
575f05cddf9SRui Paulo if (random_get_bytes(iv, iv_len)) {
57639beb93cSSam Leffler wpa_printf(MSG_INFO, "IKEV2: Could not generate IV");
57739beb93cSSam Leffler return -1;
57839beb93cSSam Leffler }
57939beb93cSSam Leffler
58039beb93cSSam Leffler pad_len = iv_len - (wpabuf_len(plain) + 1) % iv_len;
58139beb93cSSam Leffler if (pad_len == iv_len)
58239beb93cSSam Leffler pad_len = 0;
58339beb93cSSam Leffler wpabuf_put(plain, pad_len);
58439beb93cSSam Leffler wpabuf_put_u8(plain, pad_len);
58539beb93cSSam Leffler
58639beb93cSSam Leffler if (ikev2_encr_encrypt(encr_alg->id, SK_e, keys->SK_encr_len, iv,
58739beb93cSSam Leffler wpabuf_head(plain), wpabuf_mhead(plain),
58839beb93cSSam Leffler wpabuf_len(plain)) < 0)
58939beb93cSSam Leffler return -1;
59039beb93cSSam Leffler
59139beb93cSSam Leffler wpabuf_put_buf(msg, plain);
59239beb93cSSam Leffler
59339beb93cSSam Leffler /* Need to update all headers (Length fields) prior to hash func */
59439beb93cSSam Leffler icv = wpabuf_put(msg, integ_alg->hash_len);
59539beb93cSSam Leffler plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
59639beb93cSSam Leffler WPA_PUT_BE16(phdr->payload_length, plen);
59739beb93cSSam Leffler
59839beb93cSSam Leffler ikev2_update_hdr(msg);
59939beb93cSSam Leffler
60039beb93cSSam Leffler return ikev2_integ_hash(integ_id, SK_a, keys->SK_integ_len,
60139beb93cSSam Leffler wpabuf_head(msg),
60239beb93cSSam Leffler wpabuf_len(msg) - integ_alg->hash_len, icv);
60339beb93cSSam Leffler
60439beb93cSSam Leffler return 0;
60539beb93cSSam Leffler }
60639beb93cSSam Leffler
60739beb93cSSam Leffler
ikev2_keys_set(struct ikev2_keys * keys)60839beb93cSSam Leffler int ikev2_keys_set(struct ikev2_keys *keys)
60939beb93cSSam Leffler {
61039beb93cSSam Leffler return keys->SK_d && keys->SK_ai && keys->SK_ar && keys->SK_ei &&
61139beb93cSSam Leffler keys->SK_er && keys->SK_pi && keys->SK_pr;
61239beb93cSSam Leffler }
61339beb93cSSam Leffler
61439beb93cSSam Leffler
ikev2_free_keys(struct ikev2_keys * keys)61539beb93cSSam Leffler void ikev2_free_keys(struct ikev2_keys *keys)
61639beb93cSSam Leffler {
61739beb93cSSam Leffler os_free(keys->SK_d);
61839beb93cSSam Leffler os_free(keys->SK_ai);
61939beb93cSSam Leffler os_free(keys->SK_ar);
62039beb93cSSam Leffler os_free(keys->SK_ei);
62139beb93cSSam Leffler os_free(keys->SK_er);
62239beb93cSSam Leffler os_free(keys->SK_pi);
62339beb93cSSam Leffler os_free(keys->SK_pr);
62439beb93cSSam Leffler keys->SK_d = keys->SK_ai = keys->SK_ar = keys->SK_ei = keys->SK_er =
62539beb93cSSam Leffler keys->SK_pi = keys->SK_pr = NULL;
62639beb93cSSam Leffler }
62739beb93cSSam Leffler
62839beb93cSSam Leffler
ikev2_derive_sk_keys(const struct ikev2_prf_alg * prf,const struct ikev2_integ_alg * integ,const struct ikev2_encr_alg * encr,const u8 * skeyseed,const u8 * data,size_t data_len,struct ikev2_keys * keys)62939beb93cSSam Leffler int ikev2_derive_sk_keys(const struct ikev2_prf_alg *prf,
63039beb93cSSam Leffler const struct ikev2_integ_alg *integ,
63139beb93cSSam Leffler const struct ikev2_encr_alg *encr,
63239beb93cSSam Leffler const u8 *skeyseed, const u8 *data, size_t data_len,
63339beb93cSSam Leffler struct ikev2_keys *keys)
63439beb93cSSam Leffler {
63539beb93cSSam Leffler u8 *keybuf, *pos;
63639beb93cSSam Leffler size_t keybuf_len;
63739beb93cSSam Leffler
63839beb93cSSam Leffler /*
63939beb93cSSam Leffler * {SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr } =
64039beb93cSSam Leffler * prf+(SKEYSEED, Ni | Nr | SPIi | SPIr )
64139beb93cSSam Leffler */
64239beb93cSSam Leffler ikev2_free_keys(keys);
64339beb93cSSam Leffler keys->SK_d_len = prf->key_len;
64439beb93cSSam Leffler keys->SK_integ_len = integ->key_len;
64539beb93cSSam Leffler keys->SK_encr_len = encr->key_len;
64639beb93cSSam Leffler keys->SK_prf_len = prf->key_len;
64739beb93cSSam Leffler
64839beb93cSSam Leffler keybuf_len = keys->SK_d_len + 2 * keys->SK_integ_len +
64939beb93cSSam Leffler 2 * keys->SK_encr_len + 2 * keys->SK_prf_len;
65039beb93cSSam Leffler keybuf = os_malloc(keybuf_len);
65139beb93cSSam Leffler if (keybuf == NULL)
65239beb93cSSam Leffler return -1;
65339beb93cSSam Leffler
65439beb93cSSam Leffler if (ikev2_prf_plus(prf->id, skeyseed, prf->hash_len,
65539beb93cSSam Leffler data, data_len, keybuf, keybuf_len)) {
65639beb93cSSam Leffler os_free(keybuf);
65739beb93cSSam Leffler return -1;
65839beb93cSSam Leffler }
65939beb93cSSam Leffler
66039beb93cSSam Leffler pos = keybuf;
66139beb93cSSam Leffler
66239beb93cSSam Leffler keys->SK_d = os_malloc(keys->SK_d_len);
66339beb93cSSam Leffler if (keys->SK_d) {
66439beb93cSSam Leffler os_memcpy(keys->SK_d, pos, keys->SK_d_len);
66539beb93cSSam Leffler wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_d",
66639beb93cSSam Leffler keys->SK_d, keys->SK_d_len);
66739beb93cSSam Leffler }
66839beb93cSSam Leffler pos += keys->SK_d_len;
66939beb93cSSam Leffler
67039beb93cSSam Leffler keys->SK_ai = os_malloc(keys->SK_integ_len);
67139beb93cSSam Leffler if (keys->SK_ai) {
67239beb93cSSam Leffler os_memcpy(keys->SK_ai, pos, keys->SK_integ_len);
67339beb93cSSam Leffler wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_ai",
67439beb93cSSam Leffler keys->SK_ai, keys->SK_integ_len);
67539beb93cSSam Leffler }
67639beb93cSSam Leffler pos += keys->SK_integ_len;
67739beb93cSSam Leffler
67839beb93cSSam Leffler keys->SK_ar = os_malloc(keys->SK_integ_len);
67939beb93cSSam Leffler if (keys->SK_ar) {
68039beb93cSSam Leffler os_memcpy(keys->SK_ar, pos, keys->SK_integ_len);
68139beb93cSSam Leffler wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_ar",
68239beb93cSSam Leffler keys->SK_ar, keys->SK_integ_len);
68339beb93cSSam Leffler }
68439beb93cSSam Leffler pos += keys->SK_integ_len;
68539beb93cSSam Leffler
68639beb93cSSam Leffler keys->SK_ei = os_malloc(keys->SK_encr_len);
68739beb93cSSam Leffler if (keys->SK_ei) {
68839beb93cSSam Leffler os_memcpy(keys->SK_ei, pos, keys->SK_encr_len);
68939beb93cSSam Leffler wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_ei",
69039beb93cSSam Leffler keys->SK_ei, keys->SK_encr_len);
69139beb93cSSam Leffler }
69239beb93cSSam Leffler pos += keys->SK_encr_len;
69339beb93cSSam Leffler
69439beb93cSSam Leffler keys->SK_er = os_malloc(keys->SK_encr_len);
69539beb93cSSam Leffler if (keys->SK_er) {
69639beb93cSSam Leffler os_memcpy(keys->SK_er, pos, keys->SK_encr_len);
69739beb93cSSam Leffler wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_er",
69839beb93cSSam Leffler keys->SK_er, keys->SK_encr_len);
69939beb93cSSam Leffler }
70039beb93cSSam Leffler pos += keys->SK_encr_len;
70139beb93cSSam Leffler
70239beb93cSSam Leffler keys->SK_pi = os_malloc(keys->SK_prf_len);
70339beb93cSSam Leffler if (keys->SK_pi) {
70439beb93cSSam Leffler os_memcpy(keys->SK_pi, pos, keys->SK_prf_len);
70539beb93cSSam Leffler wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_pi",
70639beb93cSSam Leffler keys->SK_pi, keys->SK_prf_len);
70739beb93cSSam Leffler }
70839beb93cSSam Leffler pos += keys->SK_prf_len;
70939beb93cSSam Leffler
71039beb93cSSam Leffler keys->SK_pr = os_malloc(keys->SK_prf_len);
71139beb93cSSam Leffler if (keys->SK_pr) {
71239beb93cSSam Leffler os_memcpy(keys->SK_pr, pos, keys->SK_prf_len);
71339beb93cSSam Leffler wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_pr",
71439beb93cSSam Leffler keys->SK_pr, keys->SK_prf_len);
71539beb93cSSam Leffler }
71639beb93cSSam Leffler
71739beb93cSSam Leffler os_free(keybuf);
71839beb93cSSam Leffler
71939beb93cSSam Leffler if (!ikev2_keys_set(keys)) {
72039beb93cSSam Leffler ikev2_free_keys(keys);
72139beb93cSSam Leffler return -1;
72239beb93cSSam Leffler }
72339beb93cSSam Leffler
72439beb93cSSam Leffler return 0;
72539beb93cSSam Leffler }
726