Go to the documentation of this file.
32 #define TRACE_LEVEL IKE_TRACE_LEVEL
48 #if (IKE_SUPPORT == ENABLED)
85 #if (IKE_REAUTH_SUPPORT == ENABLED)
90 #if (IKE_DPD_SUPPORT == ENABLED)
95 #if (IKE_NAT_TRAVERSAL_SUPPORT == ENABLED)
100 #if (IKE_COOKIE_SUPPORT == ENABLED)
107 #if (IKE_CERT_AUTH_SUPPORT == ENABLED)
129 if(context == NULL || settings == NULL)
145 context->taskParams = settings->
task;
155 context->netContext = settings->
interface->netContext;
163 context->interface = settings->
interface;
166 context->prngAlgo = settings->
prngAlgo;
182 #if (IKE_REAUTH_SUPPORT == ENABLED)
187 #if (IKE_DPD_SUPPORT == ENABLED)
189 context->dpdPeriod = settings->
dpdPeriod;
192 #if (IKE_NAT_TRAVERSAL_SUPPORT == ENABLED)
197 #if (IKE_COOKIE_SUPPORT == ENABLED)
204 #if (IKE_CERT_AUTH_SUPPORT == ENABLED)
215 context->netContext->ikeContext = context;
269 if(context->socket == NULL)
289 #if (IKE_NAT_TRAVERSAL_SUPPORT == ENABLED)
294 if(context->altSocket == NULL)
316 context->stop =
FALSE;
317 context->running =
TRUE;
321 &context->taskParams);
338 context->running =
FALSE;
341 if(context->socket != NULL)
344 context->socket = NULL;
347 #if (IKE_NAT_TRAVERSAL_SUPPORT == ENABLED)
349 if(context->altSocket != NULL)
352 context->altSocket = NULL;
380 #if (NET_RTOS_SUPPORT == ENABLED)
382 context->stop =
TRUE;
387 while(context->running)
394 if(context->socket != NULL)
397 context->socket = NULL;
400 #if (IKE_NAT_TRAVERSAL_SUPPORT == ENABLED)
402 if(context->altSocket != NULL)
405 context->altSocket = NULL;
433 context->preferredGroupNum = groupNum;
453 if(context == NULL ||
id == NULL)
463 context->idLen = idLen;
480 #if (IKE_PSK_AUTH_SUPPORT == ENABLED)
482 if(context == NULL || psk == NULL)
490 osMemcpy(context->psk, psk, pskLen);
491 context->pskLen = pskLen;
517 size_t certChainLen,
const char_t *privateKey,
size_t privateKeyLen,
520 #if (IKE_CERT_AUTH_SUPPORT == ENABLED)
528 if(context == NULL || certChain == NULL || certChainLen == 0)
532 if(privateKey == NULL && privateKeyLen != 0)
606 context->certType = certType;
607 context->certChain = certChain;
608 context->certChainLen = certChainLen;
609 context->privateKey = privateKey;
610 context->privateKeyLen = privateKeyLen;
615 osStrcpy(context->password, password);
645 if(context == NULL || remoteIpAddr == NULL)
652 for(i = 0; i < context->numSaEntries; i++)
655 sa = &context->sa[i];
666 if(i >= context->numSaEntries)
675 sa->remoteIpAddr = *remoteIpAddr;
679 sa->originalInitiator =
TRUE;
682 sa->keContext.groupNum = context->preferredGroupNum;
703 #if (IKE_CREATE_CHILD_SA_SUPPORT == ENABLED)
717 context = sa->context;
720 sa->rekeyRequest =
TRUE;
756 context = sa->context;
759 sa->deleteRequest =
TRUE;
786 if(context == NULL || packet == NULL)
793 ipsecContext = context->netContext->ipsecContext;
795 if(ipsecContext == NULL)
854 childSa->remoteIpAddr = remoteIpAddr;
855 childSa->mode = spdEntry->
mode;
856 childSa->protocol = spdEntry->
protocol;
857 #if (IKE_CHILD_SA_PFS_SUPPORT == ENABLED)
858 childSa->pfs = spdEntry->
pfs;
860 childSa->initiator =
TRUE;
861 childSa->packetInfo = *packet;
862 childSa->selector = selector;
866 ipsecContext->sad[childSa->outboundSa].selector = selector;
886 #if (IKE_CREATE_CHILD_SA_SUPPORT == ENABLED)
900 context = childSa->context;
903 childSa->rekeyRequest =
TRUE;
939 context = childSa->context;
942 childSa->deleteRequest =
TRUE;
965 #if (NET_RTOS_SUPPORT == ENABLED)
977 eventDesc[
n].
socket = context->socket;
981 #if (IKE_NAT_TRAVERSAL_SUPPORT == ENABLED)
984 eventDesc[
n].
socket = context->altSocket;
996 context->running =
FALSE;
1003 #if (IKE_NAT_TRAVERSAL_SUPPORT == ENABLED)
1005 if(
n > 0 && eventDesc[0].eventFlags != 0)
1007 socket = context->socket;
1009 else if(
n > 1 && eventDesc[1].eventFlags != 0)
1011 socket = context->altSocket;
1019 if(
n > 0 && eventDesc[0].eventFlags != 0)
1021 socket = context->socket;
1034 msg.
data = context->message;
1044 context->messageLen = msg.
length;
1048 context->localInterface = msg.
interface;
1052 context->remotePort = msg.
srcPort;
1062 #if (NET_RTOS_SUPPORT == ENABLED)
1081 netContext = context->netContext;
1086 netContext->ikeContext = NULL;
error_t ikeSetPsk(IkeContext *context, const uint8_t *psk, size_t pskLen)
Set entity's pre-shared key.
IkeCertType
Certificate types.
OsTaskId osCreateTask(const char_t *name, OsTaskCode taskCode, void *arg, const OsTaskParameters *params)
Create a task.
X.509 certificate parsing.
error_t socketBind(Socket *socket, const IpAddr *localIpAddr, uint16_t localPort)
Associate a local address with a socket.
void netUnlock(NetContext *context)
Release exclusive access to the core of the TCP/IP stack.
Helper functions for IKEv2.
error_t ikeSetCertificate(IkeContext *context, const char_t *certChain, size_t certChainLen, const char_t *privateKey, size_t privateKeyLen, const char_t *password)
Load entity's certificate.
error_t ikeCreateChildSa(IkeContext *context, const IpsecPacketInfo *packet)
Create a new Child SA.
error_t ikeRekeySa(IkeSaEntry *sa)
Rekey an IKE SA.
#define IPSEC_PORT_START_OPAQUE
IpAddr remoteIpAddr
Remote IP address.
Message and ancillary data.
#define OS_INVALID_TASK_ID
void socketClose(Socket *socket)
Close an existing socket.
#define IKE_DEFAULT_SA_LIFETIME
uint_t numChildSaEntries
Number of Child SA entries.
void ikeGetDefaultSettings(IkeSettings *settings)
Initialize settings with default values.
error_t ikeDispatchMessage(IkeContext *context, uint8_t *message, size_t length)
Dispatch incoming IKE message.
IkeChildSaEntry * ikeCreateChildSaEntry(IkeContext *context)
Create a new Child Security Association.
void * data
Pointer to the payload.
error_t ikeGetCertificateType(const X509CertInfo *certInfo, IkeCertType *certType)
Retrieve the certificate type.
IKEv2 finite state machine.
Structure describing socket events.
@ IPSEC_POLICY_ACTION_PROTECT
Certificate parsing options.
IkeCertVerifyCallback certVerifyCallback
Certificate verification callback function.
void ikeChangeSaState(IkeSaEntry *sa, IkeSaState newState)
Update IKE SA state.
uint16_t destPort
Destination port.
uint16_t remotePort
Remote port.
uint_t numSaEntries
Number of IKE SA entries.
error_t ikeRekeyChildSa(IkeChildSaEntry *childSa)
Rekey a Child SA.
uint16_t ikeSelectDefaultGroup(void)
Get the default key exchange method.
void osDeleteTask(OsTaskId taskId)
Delete a task.
NetInterface * interface
Underlying network interface.
const SocketMsg SOCKET_DEFAULT_MSG
error_t pemImportCertificate(const char_t *input, size_t inputLen, uint8_t *output, size_t *outputLen, size_t *consumed)
Decode a PEM file containing a certificate.
size_t length
Actual length of the payload, in bytes.
PEM file import functions.
@ ERROR_INVALID_PARAMETER
Invalid parameter.
#define osMemcpy(dest, src, length)
uint8_t nextProtocol
Next layer protocol.
IkeSaEntry * ikeCreateSaEntry(IkeContext *context)
Create a new IKE Security Association.
Data logging functions for debugging purpose (IKEv2)
uint8_t nextProtocol
Next layer protocol.
uint16_t localPort
Local port.
const X509Options X509_DEFAULT_OPTIONS
systime_t dpdPeriod
Dead peer detection period.
bool_t ipCompAddr(const IpAddr *ipAddr1, const IpAddr *ipAddr2)
Compare IP addresses.
error_t ikeDeleteChildSa(IkeChildSaEntry *childSa)
Delete a Child SA.
void(* OsTaskCode)(void *arg)
Task routine.
error_t x509ParseCertificateEx(const uint8_t *data, size_t length, X509CertInfo *certInfo, const X509Options *options)
Parse a X.509 certificate.
IpsecPortRange remotePort
Remote port range.
bool_t ikeIsGroupSupported(uint16_t groupNum)
Check whether a given key exchange method is supported.
int_t socket(int_t family, int_t type, int_t protocol)
Create a socket that is bound to a specific transport service provider.
error_t ikeStop(IkeContext *context)
Stop IKE service.
@ ERROR_FAILURE
Generic error code.
systime_t natKeepaliveInterval
NAT keepalive interval.
NetInterface * interface
Underlying network interface.
Helper routines for IPsec.
const PrngAlgo * prngAlgo
Pseudo-random number generator to be used.
IpsecProtocol protocol
Security protocol (AH or ESP)
void osDeleteEvent(OsEvent *event)
Delete an event object.
#define IKE_MAX_PASSWORD_LEN
IpAddr srcIpAddr
Source IP address.
error_t socketReceiveMsg(Socket *socket, SocketMsg *message, uint_t flags)
Receive a message from a connectionless socket.
NetContext * netGetDefaultContext(void)
Get default TCP/IP stack context.
const OsTaskParameters OS_TASK_DEFAULT_PARAMS
@ IKE_CHILD_SA_STATE_INIT
IkeCookieGenerateCallback cookieGenerateCallback
Cookie generation callback function.
error_t ikeDeleteSa(IkeSaEntry *sa)
Delete an IKE SA.
void * prngContext
Pseudo-random number generator context.
bool_t pfs
Perfect forward secrecy.
IpsecMode mode
IPsec mode (tunnel or transport)
uint_t eventFlags
Returned events.
IpsecSpdEntry * ipsecFindSpdEntry(IpsecContext *context, IpsecPolicyAction policyAction, const IpsecSelector *selector, bool_t subset)
Search the SPD database for a matching entry.
void ikeChangeChildSaState(IkeChildSaEntry *childSa, IkeChildSaState newState)
Update Child SA state.
@ IKE_CHILD_SA_STATE_CLOSED
error_t socketPoll(SocketEventDesc *eventDesc, uint_t size, OsEvent *extEvent, systime_t timeout)
Wait for one of a set of sockets to become ready to perform I/O.
#define socketBindToInterface
NetContext * netContext
TCP/IP stack context.
IKEv2 (Internet Key Exchange Protocol)
#define IKE_TICK_INTERVAL
IpAddr destIpAddr
Destination IP address.
error_t ikeSetPreferredGroup(IkeContext *context, uint16_t groupNum)
Specify the preferred key exchange method.
#define IPSEC_PORT_END_OPAQUE
systime_t childSaLifetime
Lifetime of Child SAs.
IkeChildSaEntry * childSaEntries
Child SA entries.
Socket * socketOpenEx(NetContext *context, uint_t type, uint_t protocol)
Create a socket.
IpsecAddrRange localIpAddr
Local IP address range.
OsTaskParameters task
Task parameters.
void ikeDeinit(IkeContext *context)
Release IKE context.
IpAddr localIpAddr
Local IP address.
IpAddr remoteTunnelAddr
Remote tunnel IP address.
error_t ikeInit(IkeContext *context, const IkeSettings *settings)
IKE service initialization.
bool_t osCreateEvent(OsEvent *event)
Create an event object.
error_t ikeSetId(IkeContext *context, IkeIdType idType, const void *id, size_t idLen)
Set entity's ID.
X.509 certificate handling.
systime_t saLifetime
Lifetime of IKE SAs.
#define IKE_DEFAULT_NAT_KEEPALIVE_INTERVAL
size_t size
Size of the payload, in bytes.
IkeCookieVerifyCallback cookieVerifyCallback
Cookie verification callback function.
systime_t reauthPeriod
Reauthentication period.
void netLock(NetContext *context)
Get exclusive access to the core of the TCP/IP stack.
void osDelayTask(systime_t delay)
Delay routine.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
void ikeTask(IkeContext *context)
IKE task.
void ikeProcessEvents(IkeContext *context)
IKE event processing.
error_t ipsecDeriveSelector(const IpsecSpdEntry *spdEntry, const IpsecPacketInfo *packet, IpsecSelector *selector)
Derive SAD selector from SPD entry and triggering packet.
error_t ikeStart(IkeContext *context)
Start IKE service.
Security Policy Database (SPD) entry.
Socket * socket
Handle to a socket to monitor.
#define osMemset(p, value, length)
IkeSaEntry * saEntries
IKE SA entries.
error_t ikeCreateSa(IkeContext *context, const IpAddr *remoteIpAddr)
Create a new IKE SA.
uint16_t srcPort
Source port.
IpsecPortRange localPort
Local port range.
uint_t eventMask
Requested events.
IKEv2 algorithm negotiation.
IpsecAddrRange remoteIpAddr
Remote IP address range.
#define ikeAllocMem(size)
#define IKE_DEFAULT_CHILD_SA_LIFETIME