LCOV - code coverage report
Current view: top level - eap_common - ikev2_common.c (source / functions) Hit Total Coverage
Test: hostapd hwsim test run 1401872338 Lines: 270 370 73.0 %
Date: 2014-06-04 Functions: 16 16 100.0 %

          Line data    Source code
       1             : /*
       2             :  * IKEv2 common routines for initiator and responder
       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             : #include "includes.h"
      10             : 
      11             : #include "common.h"
      12             : #include "crypto/crypto.h"
      13             : #include "crypto/md5.h"
      14             : #include "crypto/sha1.h"
      15             : #include "crypto/random.h"
      16             : #include "ikev2_common.h"
      17             : 
      18             : 
      19             : static struct ikev2_integ_alg ikev2_integ_algs[] = {
      20             :         { AUTH_HMAC_SHA1_96, 20, 12 },
      21             :         { AUTH_HMAC_MD5_96, 16, 12 }
      22             : };
      23             : 
      24             : #define NUM_INTEG_ALGS ARRAY_SIZE(ikev2_integ_algs)
      25             : 
      26             : 
      27             : static struct ikev2_prf_alg ikev2_prf_algs[] = {
      28             :         { PRF_HMAC_SHA1, 20, 20 },
      29             :         { PRF_HMAC_MD5, 16, 16 }
      30             : };
      31             : 
      32             : #define NUM_PRF_ALGS ARRAY_SIZE(ikev2_prf_algs)
      33             : 
      34             : 
      35             : static struct ikev2_encr_alg ikev2_encr_algs[] = {
      36             :         { ENCR_AES_CBC, 16, 16 }, /* only 128-bit keys supported for now */
      37             :         { ENCR_3DES, 24, 8 }
      38             : };
      39             : 
      40             : #define NUM_ENCR_ALGS ARRAY_SIZE(ikev2_encr_algs)
      41             : 
      42             : 
      43          18 : const struct ikev2_integ_alg * ikev2_get_integ(int id)
      44             : {
      45             :         size_t i;
      46             : 
      47          18 :         for (i = 0; i < NUM_INTEG_ALGS; i++) {
      48          18 :                 if (ikev2_integ_algs[i].id == id)
      49          18 :                         return &ikev2_integ_algs[i];
      50             :         }
      51             : 
      52           0 :         return NULL;
      53             : }
      54             : 
      55             : 
      56          14 : int ikev2_integ_hash(int alg, const u8 *key, size_t key_len, const u8 *data,
      57             :                      size_t data_len, u8 *hash)
      58             : {
      59             :         u8 tmphash[IKEV2_MAX_HASH_LEN];
      60             : 
      61          14 :         switch (alg) {
      62             :         case AUTH_HMAC_SHA1_96:
      63          14 :                 if (key_len != 20)
      64           0 :                         return -1;
      65          14 :                 hmac_sha1(key, key_len, data, data_len, tmphash);
      66          14 :                 os_memcpy(hash, tmphash, 12);
      67          14 :                 break;
      68             :         case AUTH_HMAC_MD5_96:
      69           0 :                 if (key_len != 16)
      70           0 :                         return -1;
      71           0 :                 hmac_md5(key, key_len, data, data_len, tmphash);
      72           0 :                 os_memcpy(hash, tmphash, 12);
      73           0 :                 break;
      74             :         default:
      75           0 :                 return -1;
      76             :         }
      77             : 
      78          14 :         return 0;
      79             : }
      80             : 
      81             : 
      82          16 : const struct ikev2_prf_alg * ikev2_get_prf(int id)
      83             : {
      84             :         size_t i;
      85             : 
      86          16 :         for (i = 0; i < NUM_PRF_ALGS; i++) {
      87          16 :                 if (ikev2_prf_algs[i].id == id)
      88          16 :                         return &ikev2_prf_algs[i];
      89             :         }
      90             : 
      91           0 :         return NULL;
      92             : }
      93             : 
      94             : 
      95          42 : int ikev2_prf_hash(int alg, const u8 *key, size_t key_len,
      96             :                    size_t num_elem, const u8 *addr[], const size_t *len,
      97             :                    u8 *hash)
      98             : {
      99          42 :         switch (alg) {
     100             :         case PRF_HMAC_SHA1:
     101          42 :                 hmac_sha1_vector(key, key_len, num_elem, addr, len, hash);
     102          42 :                 break;
     103             :         case PRF_HMAC_MD5:
     104           0 :                 hmac_md5_vector(key, key_len, num_elem, addr, len, hash);
     105           0 :                 break;
     106             :         default:
     107           0 :                 return -1;
     108             :         }
     109             : 
     110          42 :         return 0;
     111             : }
     112             : 
     113             : 
     114           4 : int ikev2_prf_plus(int alg, const u8 *key, size_t key_len,
     115             :                    const u8 *data, size_t data_len,
     116             :                    u8 *out, size_t out_len)
     117             : {
     118             :         u8 hash[IKEV2_MAX_HASH_LEN];
     119             :         size_t hash_len;
     120             :         u8 iter, *pos, *end;
     121             :         const u8 *addr[3];
     122             :         size_t len[3];
     123             :         const struct ikev2_prf_alg *prf;
     124             :         int res;
     125             : 
     126           4 :         prf = ikev2_get_prf(alg);
     127           4 :         if (prf == NULL)
     128           0 :                 return -1;
     129           4 :         hash_len = prf->hash_len;
     130             : 
     131           4 :         addr[0] = hash;
     132           4 :         len[0] = hash_len;
     133           4 :         addr[1] = data;
     134           4 :         len[1] = data_len;
     135           4 :         addr[2] = &iter;
     136           4 :         len[2] = 1;
     137             : 
     138           4 :         pos = out;
     139           4 :         end = out + out_len;
     140           4 :         iter = 1;
     141          36 :         while (pos < end) {
     142             :                 size_t clen;
     143          28 :                 if (iter == 1)
     144           4 :                         res = ikev2_prf_hash(alg, key, key_len, 2, &addr[1],
     145             :                                              &len[1], hash);
     146             :                 else
     147          24 :                         res = ikev2_prf_hash(alg, key, key_len, 3, addr, len,
     148             :                                              hash);
     149          28 :                 if (res < 0)
     150           0 :                         return -1;
     151          28 :                 clen = hash_len;
     152          28 :                 if ((int) clen > end - pos)
     153           4 :                         clen = end - pos;
     154          28 :                 os_memcpy(pos, hash, clen);
     155          28 :                 pos += clen;
     156          28 :                 iter++;
     157             :         }
     158             : 
     159           4 :         return 0;
     160             : }
     161             : 
     162             : 
     163          10 : const struct ikev2_encr_alg * ikev2_get_encr(int id)
     164             : {
     165             :         size_t i;
     166             : 
     167          10 :         for (i = 0; i < NUM_ENCR_ALGS; i++) {
     168          10 :                 if (ikev2_encr_algs[i].id == id)
     169          10 :                         return &ikev2_encr_algs[i];
     170             :         }
     171             : 
     172           0 :         return NULL;
     173             : }
     174             : 
     175             : 
     176             : #ifdef CCNS_PL
     177             : /* from des.c */
     178             : struct des3_key_s {
     179             :         u32 ek[3][32];
     180             :         u32 dk[3][32];
     181             : };
     182             : 
     183             : void des3_key_setup(const u8 *key, struct des3_key_s *dkey);
     184             : void des3_encrypt(const u8 *plain, const struct des3_key_s *key, u8 *crypt);
     185             : void des3_decrypt(const u8 *crypt, const struct des3_key_s *key, u8 *plain);
     186             : #endif /* CCNS_PL */
     187             : 
     188             : 
     189           2 : int ikev2_encr_encrypt(int alg, const u8 *key, size_t key_len, const u8 *iv,
     190             :                        const u8 *plain, u8 *crypt, size_t len)
     191             : {
     192             :         struct crypto_cipher *cipher;
     193             :         int encr_alg;
     194             : 
     195             : #ifdef CCNS_PL
     196             :         if (alg == ENCR_3DES) {
     197             :                 struct des3_key_s des3key;
     198             :                 size_t i, blocks;
     199             :                 u8 *pos;
     200             : 
     201             :                 /* ECB mode is used incorrectly for 3DES!? */
     202             :                 if (key_len != 24) {
     203             :                         wpa_printf(MSG_INFO, "IKEV2: Invalid encr key length");
     204             :                         return -1;
     205             :                 }
     206             :                 des3_key_setup(key, &des3key);
     207             : 
     208             :                 blocks = len / 8;
     209             :                 pos = crypt;
     210             :                 for (i = 0; i < blocks; i++) {
     211             :                         des3_encrypt(pos, &des3key, pos);
     212             :                         pos += 8;
     213             :                 }
     214             :         } else {
     215             : #endif /* CCNS_PL */
     216           2 :         switch (alg) {
     217             :         case ENCR_3DES:
     218           0 :                 encr_alg = CRYPTO_CIPHER_ALG_3DES;
     219           0 :                 break;
     220             :         case ENCR_AES_CBC:
     221           2 :                 encr_alg = CRYPTO_CIPHER_ALG_AES;
     222           2 :                 break;
     223             :         default:
     224           0 :                 wpa_printf(MSG_DEBUG, "IKEV2: Unsupported encr alg %d", alg);
     225           0 :                 return -1;
     226             :         }
     227             : 
     228           2 :         cipher = crypto_cipher_init(encr_alg, iv, key, key_len);
     229           2 :         if (cipher == NULL) {
     230           0 :                 wpa_printf(MSG_INFO, "IKEV2: Failed to initialize cipher");
     231           0 :                 return -1;
     232             :         }
     233             : 
     234           2 :         if (crypto_cipher_encrypt(cipher, plain, crypt, len) < 0) {
     235           0 :                 wpa_printf(MSG_INFO, "IKEV2: Encryption failed");
     236           0 :                 crypto_cipher_deinit(cipher);
     237           0 :                 return -1;
     238             :         }
     239           2 :         crypto_cipher_deinit(cipher);
     240             : #ifdef CCNS_PL
     241             :         }
     242             : #endif /* CCNS_PL */
     243             : 
     244           2 :         return 0;
     245             : }
     246             : 
     247             : 
     248           4 : int ikev2_encr_decrypt(int alg, const u8 *key, size_t key_len, const u8 *iv,
     249             :                        const u8 *crypt, u8 *plain, size_t len)
     250             : {
     251             :         struct crypto_cipher *cipher;
     252             :         int encr_alg;
     253             : 
     254             : #ifdef CCNS_PL
     255             :         if (alg == ENCR_3DES) {
     256             :                 struct des3_key_s des3key;
     257             :                 size_t i, blocks;
     258             : 
     259             :                 /* ECB mode is used incorrectly for 3DES!? */
     260             :                 if (key_len != 24) {
     261             :                         wpa_printf(MSG_INFO, "IKEV2: Invalid encr key length");
     262             :                         return -1;
     263             :                 }
     264             :                 des3_key_setup(key, &des3key);
     265             : 
     266             :                 if (len % 8) {
     267             :                         wpa_printf(MSG_INFO, "IKEV2: Invalid encrypted "
     268             :                                    "length");
     269             :                         return -1;
     270             :                 }
     271             :                 blocks = len / 8;
     272             :                 for (i = 0; i < blocks; i++) {
     273             :                         des3_decrypt(crypt, &des3key, plain);
     274             :                         plain += 8;
     275             :                         crypt += 8;
     276             :                 }
     277             :         } else {
     278             : #endif /* CCNS_PL */
     279           4 :         switch (alg) {
     280             :         case ENCR_3DES:
     281           0 :                 encr_alg = CRYPTO_CIPHER_ALG_3DES;
     282           0 :                 break;
     283             :         case ENCR_AES_CBC:
     284           4 :                 encr_alg = CRYPTO_CIPHER_ALG_AES;
     285           4 :                 break;
     286             :         default:
     287           0 :                 wpa_printf(MSG_DEBUG, "IKEV2: Unsupported encr alg %d", alg);
     288           0 :                 return -1;
     289             :         }
     290             : 
     291           4 :         cipher = crypto_cipher_init(encr_alg, iv, key, key_len);
     292           4 :         if (cipher == NULL) {
     293           0 :                 wpa_printf(MSG_INFO, "IKEV2: Failed to initialize cipher");
     294           0 :                 return -1;
     295             :         }
     296             : 
     297           4 :         if (crypto_cipher_decrypt(cipher, crypt, plain, len) < 0) {
     298           0 :                 wpa_printf(MSG_INFO, "IKEV2: Decryption failed");
     299           0 :                 crypto_cipher_deinit(cipher);
     300           0 :                 return -1;
     301             :         }
     302           4 :         crypto_cipher_deinit(cipher);
     303             : #ifdef CCNS_PL
     304             :         }
     305             : #endif /* CCNS_PL */
     306             : 
     307           4 :         return 0;
     308             : }
     309             : 
     310             : 
     311           8 : int ikev2_parse_payloads(struct ikev2_payloads *payloads,
     312             :                          u8 next_payload, const u8 *pos, const u8 *end)
     313             : {
     314             :         const struct ikev2_payload_hdr *phdr;
     315             : 
     316           8 :         os_memset(payloads, 0, sizeof(*payloads));
     317             : 
     318          32 :         while (next_payload != IKEV2_PAYLOAD_NO_NEXT_PAYLOAD) {
     319             :                 int plen, pdatalen;
     320             :                 const u8 *pdata;
     321          16 :                 wpa_printf(MSG_DEBUG, "IKEV2: Processing payload %u",
     322             :                            next_payload);
     323          16 :                 if (end - pos < (int) sizeof(*phdr)) {
     324           0 :                         wpa_printf(MSG_INFO, "IKEV2:   Too short message for "
     325             :                                    "payload header (left=%ld)",
     326             :                                    (long) (end - pos));
     327             :                 }
     328          16 :                 phdr = (const struct ikev2_payload_hdr *) pos;
     329          16 :                 plen = WPA_GET_BE16(phdr->payload_length);
     330          16 :                 if (plen < (int) sizeof(*phdr) || pos + plen > end) {
     331           0 :                         wpa_printf(MSG_INFO, "IKEV2:   Invalid payload header "
     332             :                                    "length %d", plen);
     333           0 :                         return -1;
     334             :                 }
     335             : 
     336          32 :                 wpa_printf(MSG_DEBUG, "IKEV2:   Next Payload: %u  Flags: 0x%x"
     337             :                            "  Payload Length: %d",
     338          32 :                            phdr->next_payload, phdr->flags, plen);
     339             : 
     340          16 :                 pdata = (const u8 *) (phdr + 1);
     341          16 :                 pdatalen = plen - sizeof(*phdr);
     342             : 
     343          16 :                 switch (next_payload) {
     344             :                 case IKEV2_PAYLOAD_SA:
     345           2 :                         wpa_printf(MSG_DEBUG, "IKEV2:   Payload: Security "
     346             :                                    "Association");
     347           2 :                         payloads->sa = pdata;
     348           2 :                         payloads->sa_len = pdatalen;
     349           2 :                         break;
     350             :                 case IKEV2_PAYLOAD_KEY_EXCHANGE:
     351           2 :                         wpa_printf(MSG_DEBUG, "IKEV2:   Payload: Key "
     352             :                                    "Exchange");
     353           2 :                         payloads->ke = pdata;
     354           2 :                         payloads->ke_len = pdatalen;
     355           2 :                         break;
     356             :                 case IKEV2_PAYLOAD_IDi:
     357           0 :                         wpa_printf(MSG_DEBUG, "IKEV2:   Payload: IDi");
     358           0 :                         payloads->idi = pdata;
     359           0 :                         payloads->idi_len = pdatalen;
     360           0 :                         break;
     361             :                 case IKEV2_PAYLOAD_IDr:
     362           4 :                         wpa_printf(MSG_DEBUG, "IKEV2:   Payload: IDr");
     363           4 :                         payloads->idr = pdata;
     364           4 :                         payloads->idr_len = pdatalen;
     365           4 :                         break;
     366             :                 case IKEV2_PAYLOAD_CERTIFICATE:
     367           0 :                         wpa_printf(MSG_DEBUG, "IKEV2:   Payload: Certificate");
     368           0 :                         payloads->cert = pdata;
     369           0 :                         payloads->cert_len = pdatalen;
     370           0 :                         break;
     371             :                 case IKEV2_PAYLOAD_AUTHENTICATION:
     372           2 :                         wpa_printf(MSG_DEBUG, "IKEV2:   Payload: "
     373             :                                    "Authentication");
     374           2 :                         payloads->auth = pdata;
     375           2 :                         payloads->auth_len = pdatalen;
     376           2 :                         break;
     377             :                 case IKEV2_PAYLOAD_NONCE:
     378           2 :                         wpa_printf(MSG_DEBUG, "IKEV2:   Payload: Nonce");
     379           2 :                         payloads->nonce = pdata;
     380           2 :                         payloads->nonce_len = pdatalen;
     381           2 :                         break;
     382             :                 case IKEV2_PAYLOAD_ENCRYPTED:
     383           4 :                         wpa_printf(MSG_DEBUG, "IKEV2:   Payload: Encrypted");
     384           4 :                         payloads->encrypted = pdata;
     385           4 :                         payloads->encrypted_len = pdatalen;
     386           4 :                         break;
     387             :                 case IKEV2_PAYLOAD_NOTIFICATION:
     388           0 :                         wpa_printf(MSG_DEBUG, "IKEV2:   Payload: "
     389             :                                    "Notification");
     390           0 :                         payloads->notification = pdata;
     391           0 :                         payloads->notification_len = pdatalen;
     392           0 :                         break;
     393             :                 default:
     394           0 :                         if (phdr->flags & IKEV2_PAYLOAD_FLAGS_CRITICAL) {
     395           0 :                                 wpa_printf(MSG_INFO, "IKEV2:   Unsupported "
     396             :                                            "critical payload %u - reject the "
     397             :                                            "entire message", next_payload);
     398           0 :                                 return -1;
     399             :                         } else {
     400           0 :                                 wpa_printf(MSG_DEBUG, "IKEV2:   Skipped "
     401             :                                            "unsupported payload %u",
     402             :                                            next_payload);
     403             :                         }
     404             :                 }
     405             : 
     406          20 :                 if (next_payload == IKEV2_PAYLOAD_ENCRYPTED &&
     407           4 :                     pos + plen == end) {
     408             :                         /*
     409             :                          * Next Payload in the case of Encrypted Payload is
     410             :                          * actually the payload type for the first embedded
     411             :                          * payload.
     412             :                          */
     413           4 :                         payloads->encr_next_payload = phdr->next_payload;
     414           4 :                         next_payload = IKEV2_PAYLOAD_NO_NEXT_PAYLOAD;
     415             :                 } else
     416          12 :                         next_payload = phdr->next_payload;
     417             : 
     418          16 :                 pos += plen;
     419             :         }
     420             : 
     421           8 :         if (pos != end) {
     422           0 :                 wpa_printf(MSG_INFO, "IKEV2: Unexpected extra data after "
     423             :                            "payloads");
     424           0 :                 return -1;
     425             :         }
     426             : 
     427           8 :         return 0;
     428             : }
     429             : 
     430             : 
     431           4 : int ikev2_derive_auth_data(int prf_alg, const struct wpabuf *sign_msg,
     432             :                            const u8 *ID, size_t ID_len, u8 ID_type,
     433             :                            struct ikev2_keys *keys, int initiator,
     434             :                            const u8 *shared_secret, size_t shared_secret_len,
     435             :                            const u8 *nonce, size_t nonce_len,
     436             :                            const u8 *key_pad, size_t key_pad_len,
     437             :                            u8 *auth_data)
     438             : {
     439             :         size_t sign_len, buf_len;
     440             :         u8 *sign_data, *pos, *buf, hash[IKEV2_MAX_HASH_LEN];
     441             :         const struct ikev2_prf_alg *prf;
     442           4 :         const u8 *SK_p = initiator ? keys->SK_pi : keys->SK_pr;
     443             : 
     444           4 :         prf = ikev2_get_prf(prf_alg);
     445           4 :         if (sign_msg == NULL || ID == NULL || SK_p == NULL ||
     446           4 :             shared_secret == NULL || nonce == NULL || prf == NULL)
     447           0 :                 return -1;
     448             : 
     449             :         /* prf(SK_pi/r,IDi/r') */
     450           4 :         buf_len = 4 + ID_len;
     451           4 :         buf = os_zalloc(buf_len);
     452           4 :         if (buf == NULL)
     453           0 :                 return -1;
     454           4 :         buf[0] = ID_type;
     455           4 :         os_memcpy(buf + 4, ID, ID_len);
     456           4 :         if (ikev2_prf_hash(prf->id, SK_p, keys->SK_prf_len,
     457             :                            1, (const u8 **) &buf, &buf_len, hash) < 0) {
     458           0 :                 os_free(buf);
     459           0 :                 return -1;
     460             :         }
     461           4 :         os_free(buf);
     462             : 
     463             :         /* sign_data = msg | Nr/i | prf(SK_pi/r,IDi/r') */
     464           4 :         sign_len = wpabuf_len(sign_msg) + nonce_len + prf->hash_len;
     465           4 :         sign_data = os_malloc(sign_len);
     466           4 :         if (sign_data == NULL)
     467           0 :                 return -1;
     468           4 :         pos = sign_data;
     469           4 :         os_memcpy(pos, wpabuf_head(sign_msg), wpabuf_len(sign_msg));
     470           4 :         pos += wpabuf_len(sign_msg);
     471           4 :         os_memcpy(pos, nonce, nonce_len);
     472           4 :         pos += nonce_len;
     473           4 :         os_memcpy(pos, hash, prf->hash_len);
     474             : 
     475             :         /* AUTH = prf(prf(Shared Secret, key pad, sign_data) */
     476           4 :         if (ikev2_prf_hash(prf->id, shared_secret, shared_secret_len, 1,
     477           4 :                            &key_pad, &key_pad_len, hash) < 0 ||
     478           4 :             ikev2_prf_hash(prf->id, hash, prf->hash_len, 1,
     479             :                            (const u8 **) &sign_data, &sign_len, auth_data) < 0)
     480             :         {
     481           0 :                 os_free(sign_data);
     482           0 :                 return -1;
     483             :         }
     484           4 :         os_free(sign_data);
     485             : 
     486           4 :         return 0;
     487             : }
     488             : 
     489             : 
     490           4 : u8 * ikev2_decrypt_payload(int encr_id, int integ_id,
     491             :                            struct ikev2_keys *keys, int initiator,
     492             :                            const struct ikev2_hdr *hdr,
     493             :                            const u8 *encrypted, size_t encrypted_len,
     494             :                            size_t *res_len)
     495             : {
     496             :         size_t iv_len;
     497             :         const u8 *pos, *end, *iv, *integ;
     498             :         u8 hash[IKEV2_MAX_HASH_LEN], *decrypted;
     499             :         size_t decrypted_len, pad_len;
     500             :         const struct ikev2_integ_alg *integ_alg;
     501             :         const struct ikev2_encr_alg *encr_alg;
     502           4 :         const u8 *SK_e = initiator ? keys->SK_ei : keys->SK_er;
     503           4 :         const u8 *SK_a = initiator ? keys->SK_ai : keys->SK_ar;
     504             : 
     505           4 :         if (encrypted == NULL) {
     506           0 :                 wpa_printf(MSG_INFO, "IKEV2: No Encrypted payload in SA_AUTH");
     507           0 :                 return NULL;
     508             :         }
     509             : 
     510           4 :         encr_alg = ikev2_get_encr(encr_id);
     511           4 :         if (encr_alg == NULL) {
     512           0 :                 wpa_printf(MSG_INFO, "IKEV2: Unsupported encryption type");
     513           0 :                 return NULL;
     514             :         }
     515           4 :         iv_len = encr_alg->block_size;
     516             : 
     517           4 :         integ_alg = ikev2_get_integ(integ_id);
     518           4 :         if (integ_alg == NULL) {
     519           0 :                 wpa_printf(MSG_INFO, "IKEV2: Unsupported intergrity type");
     520           0 :                 return NULL;
     521             :         }
     522             : 
     523           4 :         if (encrypted_len < iv_len + 1 + integ_alg->hash_len) {
     524           0 :                 wpa_printf(MSG_INFO, "IKEV2: No room for IV or Integrity "
     525             :                           "Checksum");
     526           0 :                 return NULL;
     527             :         }
     528             : 
     529           4 :         iv = encrypted;
     530           4 :         pos = iv + iv_len;
     531           4 :         end = encrypted + encrypted_len;
     532           4 :         integ = end - integ_alg->hash_len;
     533             : 
     534           4 :         if (SK_a == NULL) {
     535           0 :                 wpa_printf(MSG_INFO, "IKEV2: No SK_a available");
     536           0 :                 return NULL;
     537             :         }
     538           4 :         if (ikev2_integ_hash(integ_id, SK_a, keys->SK_integ_len,
     539             :                              (const u8 *) hdr,
     540           4 :                              integ - (const u8 *) hdr, hash) < 0) {
     541           0 :                 wpa_printf(MSG_INFO, "IKEV2: Failed to calculate integrity "
     542             :                            "hash");
     543           0 :                 return NULL;
     544             :         }
     545           4 :         if (os_memcmp(integ, hash, integ_alg->hash_len) != 0) {
     546           0 :                 wpa_printf(MSG_INFO, "IKEV2: Incorrect Integrity Checksum "
     547             :                            "Data");
     548           0 :                 return NULL;
     549             :         }
     550             : 
     551           4 :         if (SK_e == NULL) {
     552           0 :                 wpa_printf(MSG_INFO, "IKEV2: No SK_e available");
     553           0 :                 return NULL;
     554             :         }
     555             : 
     556           4 :         decrypted_len = integ - pos;
     557           4 :         decrypted = os_malloc(decrypted_len);
     558           4 :         if (decrypted == NULL)
     559           0 :                 return NULL;
     560             : 
     561           4 :         if (ikev2_encr_decrypt(encr_alg->id, SK_e, keys->SK_encr_len, iv, pos,
     562             :                                decrypted, decrypted_len) < 0) {
     563           0 :                 os_free(decrypted);
     564           0 :                 return NULL;
     565             :         }
     566             : 
     567           4 :         pad_len = decrypted[decrypted_len - 1];
     568           4 :         if (decrypted_len < pad_len + 1) {
     569           0 :                 wpa_printf(MSG_INFO, "IKEV2: Invalid padding in encrypted "
     570             :                            "payload");
     571           0 :                 os_free(decrypted);
     572           0 :                 return NULL;
     573             :         }
     574             : 
     575           4 :         decrypted_len -= pad_len + 1;
     576             : 
     577           4 :         *res_len = decrypted_len;
     578           4 :         return decrypted;
     579             : }
     580             : 
     581             : 
     582           4 : void ikev2_update_hdr(struct wpabuf *msg)
     583             : {
     584             :         struct ikev2_hdr *hdr;
     585             : 
     586             :         /* Update lenth field in HDR */
     587           4 :         hdr = wpabuf_mhead(msg);
     588           4 :         WPA_PUT_BE32(hdr->length, wpabuf_len(msg));
     589           4 : }
     590             : 
     591             : 
     592           2 : int ikev2_build_encrypted(int encr_id, int integ_id, struct ikev2_keys *keys,
     593             :                           int initiator, struct wpabuf *msg,
     594             :                           struct wpabuf *plain, u8 next_payload)
     595             : {
     596             :         struct ikev2_payload_hdr *phdr;
     597             :         size_t plen;
     598             :         size_t iv_len, pad_len;
     599             :         u8 *icv, *iv;
     600             :         const struct ikev2_integ_alg *integ_alg;
     601             :         const struct ikev2_encr_alg *encr_alg;
     602           2 :         const u8 *SK_e = initiator ? keys->SK_ei : keys->SK_er;
     603           2 :         const u8 *SK_a = initiator ? keys->SK_ai : keys->SK_ar;
     604             : 
     605           2 :         wpa_printf(MSG_DEBUG, "IKEV2: Adding Encrypted payload");
     606             : 
     607             :         /* Encr - RFC 4306, Sect. 3.14 */
     608             : 
     609           2 :         encr_alg = ikev2_get_encr(encr_id);
     610           2 :         if (encr_alg == NULL) {
     611           0 :                 wpa_printf(MSG_INFO, "IKEV2: Unsupported encryption type");
     612           0 :                 return -1;
     613             :         }
     614           2 :         iv_len = encr_alg->block_size;
     615             : 
     616           2 :         integ_alg = ikev2_get_integ(integ_id);
     617           2 :         if (integ_alg == NULL) {
     618           0 :                 wpa_printf(MSG_INFO, "IKEV2: Unsupported intergrity type");
     619           0 :                 return -1;
     620             :         }
     621             : 
     622           2 :         if (SK_e == NULL) {
     623           0 :                 wpa_printf(MSG_INFO, "IKEV2: No SK_e available");
     624           0 :                 return -1;
     625             :         }
     626             : 
     627           2 :         if (SK_a == NULL) {
     628           0 :                 wpa_printf(MSG_INFO, "IKEV2: No SK_a available");
     629           0 :                 return -1;
     630             :         }
     631             : 
     632           2 :         phdr = wpabuf_put(msg, sizeof(*phdr));
     633           2 :         phdr->next_payload = next_payload;
     634           2 :         phdr->flags = 0;
     635             : 
     636           2 :         iv = wpabuf_put(msg, iv_len);
     637           2 :         if (random_get_bytes(iv, iv_len)) {
     638           0 :                 wpa_printf(MSG_INFO, "IKEV2: Could not generate IV");
     639           0 :                 return -1;
     640             :         }
     641             : 
     642           2 :         pad_len = iv_len - (wpabuf_len(plain) + 1) % iv_len;
     643           2 :         if (pad_len == iv_len)
     644           0 :                 pad_len = 0;
     645           2 :         wpabuf_put(plain, pad_len);
     646           2 :         wpabuf_put_u8(plain, pad_len);
     647             : 
     648           4 :         if (ikev2_encr_encrypt(encr_alg->id, SK_e, keys->SK_encr_len, iv,
     649           2 :                                wpabuf_head(plain), wpabuf_mhead(plain),
     650             :                                wpabuf_len(plain)) < 0)
     651           0 :                 return -1;
     652             : 
     653           2 :         wpabuf_put_buf(msg, plain);
     654             : 
     655             :         /* Need to update all headers (Length fields) prior to hash func */
     656           2 :         icv = wpabuf_put(msg, integ_alg->hash_len);
     657           2 :         plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;
     658           2 :         WPA_PUT_BE16(phdr->payload_length, plen);
     659             : 
     660           2 :         ikev2_update_hdr(msg);
     661             : 
     662           4 :         return ikev2_integ_hash(integ_id, SK_a, keys->SK_integ_len,
     663           2 :                                 wpabuf_head(msg),
     664           2 :                                 wpabuf_len(msg) - integ_alg->hash_len, icv);
     665             : 
     666             :         return 0;
     667             : }
     668             : 
     669             : 
     670           2 : int ikev2_keys_set(struct ikev2_keys *keys)
     671             : {
     672           8 :         return keys->SK_d && keys->SK_ai && keys->SK_ar && keys->SK_ei &&
     673           6 :                 keys->SK_er && keys->SK_pi && keys->SK_pr;
     674             : }
     675             : 
     676             : 
     677           4 : void ikev2_free_keys(struct ikev2_keys *keys)
     678             : {
     679           4 :         os_free(keys->SK_d);
     680           4 :         os_free(keys->SK_ai);
     681           4 :         os_free(keys->SK_ar);
     682           4 :         os_free(keys->SK_ei);
     683           4 :         os_free(keys->SK_er);
     684           4 :         os_free(keys->SK_pi);
     685           4 :         os_free(keys->SK_pr);
     686           4 :         keys->SK_d = keys->SK_ai = keys->SK_ar = keys->SK_ei = keys->SK_er =
     687           4 :                 keys->SK_pi = keys->SK_pr = NULL;
     688           4 : }
     689             : 
     690             : 
     691           2 : int ikev2_derive_sk_keys(const struct ikev2_prf_alg *prf,
     692             :                          const struct ikev2_integ_alg *integ,
     693             :                          const struct ikev2_encr_alg *encr,
     694             :                          const u8 *skeyseed, const u8 *data, size_t data_len,
     695             :                          struct ikev2_keys *keys)
     696             : {
     697             :         u8 *keybuf, *pos;
     698             :         size_t keybuf_len;
     699             : 
     700             :         /*
     701             :          * {SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr } =
     702             :          *      prf+(SKEYSEED, Ni | Nr | SPIi | SPIr )
     703             :          */
     704           2 :         ikev2_free_keys(keys);
     705           2 :         keys->SK_d_len = prf->key_len;
     706           2 :         keys->SK_integ_len = integ->key_len;
     707           2 :         keys->SK_encr_len = encr->key_len;
     708           2 :         keys->SK_prf_len = prf->key_len;
     709             : #ifdef CCNS_PL
     710             :         /* Uses encryption key length for SK_d; should be PRF length */
     711             :         keys->SK_d_len = keys->SK_encr_len;
     712             : #endif /* CCNS_PL */
     713             : 
     714           6 :         keybuf_len = keys->SK_d_len + 2 * keys->SK_integ_len +
     715           4 :                 2 * keys->SK_encr_len + 2 * keys->SK_prf_len;
     716           2 :         keybuf = os_malloc(keybuf_len);
     717           2 :         if (keybuf == NULL)
     718           0 :                 return -1;
     719             : 
     720           2 :         if (ikev2_prf_plus(prf->id, skeyseed, prf->hash_len,
     721             :                            data, data_len, keybuf, keybuf_len)) {
     722           0 :                 os_free(keybuf);
     723           0 :                 return -1;
     724             :         }
     725             : 
     726           2 :         pos = keybuf;
     727             : 
     728           2 :         keys->SK_d = os_malloc(keys->SK_d_len);
     729           2 :         if (keys->SK_d) {
     730           2 :                 os_memcpy(keys->SK_d, pos, keys->SK_d_len);
     731           4 :                 wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_d",
     732           2 :                                 keys->SK_d, keys->SK_d_len);
     733             :         }
     734           2 :         pos += keys->SK_d_len;
     735             : 
     736           2 :         keys->SK_ai = os_malloc(keys->SK_integ_len);
     737           2 :         if (keys->SK_ai) {
     738           2 :                 os_memcpy(keys->SK_ai, pos, keys->SK_integ_len);
     739           4 :                 wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_ai",
     740           2 :                                 keys->SK_ai, keys->SK_integ_len);
     741             :         }
     742           2 :         pos += keys->SK_integ_len;
     743             : 
     744           2 :         keys->SK_ar = os_malloc(keys->SK_integ_len);
     745           2 :         if (keys->SK_ar) {
     746           2 :                 os_memcpy(keys->SK_ar, pos, keys->SK_integ_len);
     747           4 :                 wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_ar",
     748           2 :                                 keys->SK_ar, keys->SK_integ_len);
     749             :         }
     750           2 :         pos += keys->SK_integ_len;
     751             : 
     752           2 :         keys->SK_ei = os_malloc(keys->SK_encr_len);
     753           2 :         if (keys->SK_ei) {
     754           2 :                 os_memcpy(keys->SK_ei, pos, keys->SK_encr_len);
     755           4 :                 wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_ei",
     756           2 :                                 keys->SK_ei, keys->SK_encr_len);
     757             :         }
     758           2 :         pos += keys->SK_encr_len;
     759             : 
     760           2 :         keys->SK_er = os_malloc(keys->SK_encr_len);
     761           2 :         if (keys->SK_er) {
     762           2 :                 os_memcpy(keys->SK_er, pos, keys->SK_encr_len);
     763           4 :                 wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_er",
     764           2 :                                 keys->SK_er, keys->SK_encr_len);
     765             :         }
     766           2 :         pos += keys->SK_encr_len;
     767             : 
     768           2 :         keys->SK_pi = os_malloc(keys->SK_prf_len);
     769           2 :         if (keys->SK_pi) {
     770           2 :                 os_memcpy(keys->SK_pi, pos, keys->SK_prf_len);
     771           4 :                 wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_pi",
     772           2 :                                 keys->SK_pi, keys->SK_prf_len);
     773             :         }
     774           2 :         pos += keys->SK_prf_len;
     775             : 
     776           2 :         keys->SK_pr = os_malloc(keys->SK_prf_len);
     777           2 :         if (keys->SK_pr) {
     778           2 :                 os_memcpy(keys->SK_pr, pos, keys->SK_prf_len);
     779           4 :                 wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_pr",
     780           2 :                                 keys->SK_pr, keys->SK_prf_len);
     781             :         }
     782             : 
     783           2 :         os_free(keybuf);
     784             : 
     785           2 :         if (!ikev2_keys_set(keys)) {
     786           0 :                 ikev2_free_keys(keys);
     787           0 :                 return -1;
     788             :         }
     789             : 
     790           2 :         return 0;
     791             : }

Generated by: LCOV version 1.10