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