ike_misc.h
Go to the documentation of this file.
1 /**
2  * @file ike_misc.h
3  * @brief Helper functions for IKEv2
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2022-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneIPSEC Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.6
29  **/
30 
31 #ifndef _IKE_MISC_H
32 #define _IKE_MISC_H
33 
34 //Dependencies
35 #include "ike/ike.h"
36 
37 //C++ guard
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 //IKEv2 related constants
43 extern const uint8_t IKE_INVALID_SPI[8];
44 
45 //IKEv2 related functions
48 
50 IkeSaEntry *ikeFindSaEntry(IkeContext *context, const IkeHeader *ikeHeader);
51 
53  const IkeHeader *ikeHeader, const IkeNoncePayload *noncePayload);
54 
57 
59 
61  const uint8_t *spi);
62 
64 
65 void ikeInheritChildSas(IkeSaEntry *newSa, IkeSaEntry *oldSa);
66 
67 error_t ikeGenerateSaSpi(IkeSaEntry *sa, uint8_t *spi);
69 error_t ikeGenerateNonce(IkeContext *context, uint8_t *nonce, size_t *length);
70 
72 
73 void ikeSubstituteId(IkeSaEntry *sa);
74 
75 error_t ikeSelectTs(IkeChildSaEntry *childSa, const IkeTsPayload *tsiPayload,
76  const IkeTsPayload *tsrPayload);
77 
78 error_t ikeCheckTs(IkeChildSaEntry *childSa, const IkeTsPayload *tsiPayload,
79  const IkeTsPayload *tsrPayload, bool_t rekey);
80 
81 void ikeCopyLocalTsEntry(IkeChildSaEntry *childSa, IpsecSelector *selector,
82  const IkeTsEntry *tsEntry);
83 
84 void ikeCopyRemoteTsEntry(IkeChildSaEntry *childSa, IpsecSelector *selector,
85  const IkeTsEntry *tsEntry);
86 
87 error_t ikeCheckNonceLength(IkeSaEntry *sa, size_t nonceLen);
88 
89 int_t ikeCompareNonces(const uint8_t *nonce1, size_t nonceLen1,
90  const uint8_t *nonce2, size_t nonceLen2);
91 
93 
95  IkeChildSaEntry *childSa2);
96 
98 
100 
101 //C++ guard
102 #ifdef __cplusplus
103 }
104 #endif
105 
106 #endif
error_t ikeSelectTs(IkeChildSaEntry *childSa, const IkeTsPayload *tsiPayload, const IkeTsPayload *tsrPayload)
Traffic selector selection.
Definition: ike_misc.c:904
int bool_t
Definition: compiler_port.h:63
signed int int_t
Definition: compiler_port.h:56
IPsec selector.
Definition: ipsec.h:309
const uint8_t IKE_INVALID_SPI[8]
Definition: ike_misc.c:47
uint32_t spi
Definition: ah.h:175
Traffic selector entry.
Definition: ike.h:1801
error_t ikeRetransmitResponse(IkeSaEntry *sa)
Retransmit IKE response message.
Definition: ike_misc.c:117
IkeSaEntry * ikeFindSaEntry(IkeContext *context, const IkeHeader *ikeHeader)
Find an IKE SA that matches an incoming IKE message.
Definition: ike_misc.c:232
IkeChildSaEntry * ikeCreateChildSaEntry(IkeContext *context)
Create a new Child Security Association.
Definition: ike_misc.c:451
void ikeDeleteSaEntry(IkeSaEntry *sa)
Delete an IKE Security Association.
Definition: ike_misc.c:347
void ikeDeleteDuplicateSaEntries(IkeSaEntry *sa)
Delete an duplicate IKE Security Associations.
Definition: ike_misc.c:408
#define IkeContext
Definition: ike.h:832
IkeChildSaEntry * ikeFindChildSaEntry(IkeSaEntry *sa, uint8_t protocolId, const uint8_t *spi)
Find an Child SA that matches the specified SPI.
Definition: ike_misc.c:518
error_t
Error codes.
Definition: error.h:43
systime_t ikeRandomizeDelay(IkeContext *context, systime_t delay)
Apply random jitter to a time interval.
Definition: ike_misc.c:826
bool_t ikeIsInitialContact(IkeSaEntry *sa)
Test if the IKE SA is the only currently active with a given peer.
Definition: ike_misc.c:1625
uint8_t protocolId[]
IkeTsPayload
Definition: ike.h:1670
int_t ikeCompareNonces(const uint8_t *nonce1, size_t nonceLen1, const uint8_t *nonce2, size_t nonceLen2)
Compare nonces.
Definition: ike_misc.c:1334
void ikeCopyRemoteTsEntry(IkeChildSaEntry *childSa, IpsecSelector *selector, const IkeTsEntry *tsEntry)
Copy remote traffic selector.
Definition: ike_misc.c:1194
uint8_t length
Definition: tcp.h:375
IkeHeader
Definition: ike.h:1459
void ikeDeleteChildSaEntry(IkeChildSaEntry *childSa)
Delete a Child Security Association.
Definition: ike_misc.c:560
error_t ikeGenerateChildSaSpi(IkeChildSaEntry *childSa, uint8_t *spi)
Generate a new Child SA SPI.
Definition: ike_misc.c:718
IKEv2 (Internet Key Exchange Protocol)
uint32_t systime_t
System time.
int_t ikeCompareSaNonces(IkeSaEntry *sa1, IkeSaEntry *sa2)
Compare IKE SA nonces.
Definition: ike_misc.c:1376
void ikeCopyLocalTsEntry(IkeChildSaEntry *childSa, IpsecSelector *selector, const IkeTsEntry *tsEntry)
Copy local traffic selector.
Definition: ike_misc.c:1149
error_t ikeCreateIpsecSaPair(IkeChildSaEntry *childSa)
Create AH or ESP SA pair.
Definition: ike_misc.c:1467
#define IkeSaEntry
Definition: ike.h:836
error_t ikeCheckNonceLength(IkeSaEntry *sa, size_t nonceLen)
Check the length of the nonce.
Definition: ike_misc.c:1238
error_t ikeCheckTs(IkeChildSaEntry *childSa, const IkeTsPayload *tsiPayload, const IkeTsPayload *tsrPayload, bool_t rekey)
Check whether the selected traffic selectors are acceptable.
Definition: ike_misc.c:1046
void ikeInheritChildSas(IkeSaEntry *newSa, IkeSaEntry *oldSa)
Move inherited Child SAs.
Definition: ike_misc.c:597
error_t ikeGenerateSaSpi(IkeSaEntry *sa, uint8_t *spi)
Generate a new IKE SA SPI.
Definition: ike_misc.c:632
error_t ikeRetransmitRequest(IkeSaEntry *sa)
Retransmit IKE request message.
Definition: ike_misc.c:56
IkeSaEntry * ikeFindHalfOpenSaEntry(IkeContext *context, const IkeHeader *ikeHeader, const IkeNoncePayload *noncePayload)
Find an half-open IKE SA that matches an incoming IKE_SA_INIT request.
Definition: ike_misc.c:293
IkeNoncePayload
Definition: ike.h:1618
int_t ikeCompareChildSaNonces(IkeChildSaEntry *childSa1, IkeChildSaEntry *childSa2)
Compare Child SA nonces.
Definition: ike_misc.c:1422
void ikeSubstituteId(IkeSaEntry *sa)
Perform ID substitution.
Definition: ike_misc.c:860
#define IkeChildSaEntry
Definition: ike.h:840
uint8_t nonce[]
Definition: ntp_common.h:239
IkeSaEntry * ikeCreateSaEntry(IkeContext *context)
Create a new IKE Security Association.
Definition: ike_misc.c:185
error_t ikeGenerateNonce(IkeContext *context, uint8_t *nonce, size_t *length)
Generate a new nonce.
Definition: ike_misc.c:792