2 The Common operations used by IKE Exchange Process.
4 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php.
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include "IpSecDebug.h"
19 #include "IkeService.h"
20 #include "IpSecConfigImpl.h"
22 UINT16 mIkev2EncryptAlgorithmList
[IKEV2_SUPPORT_ENCRYPT_ALGORITHM_NUM
] = {
23 IKEV2_TRANSFORM_ID_ENCR_3DES
,
24 IKEV2_TRANSFORM_ID_ENCR_AES_CBC
,
27 UINT16 mIkev2PrfAlgorithmList
[IKEV2_SUPPORT_PRF_ALGORITHM_NUM
] = {
28 IKEV2_TRANSFORM_ID_PRF_HMAC_SHA1
,
31 UINT16 mIkev2DhGroupAlgorithmList
[IKEV2_SUPPORT_DH_ALGORITHM_NUM
] = {
32 IKEV2_TRANSFORM_ID_DH_1024MODP
,
33 IKEV2_TRANSFORM_ID_DH_2048MODP
,
36 UINT16 mIkev2AuthAlgorithmList
[IKEV2_SUPPORT_AUTH_ALGORITHM_NUM
] = {
37 IKEV2_TRANSFORM_ID_AUTH_HMAC_SHA1_96
,
41 Allocate buffer for IKEV2_SA_SESSION and initialize it.
43 @param[in] Private Pointer to IPSEC_PRIVATE_DATA.
44 @param[in] UdpService Pointer to IKE_UDP_SERVICE related to this IKE SA Session.
46 @return Pointer to IKEV2_SA_SESSION or NULL.
51 IN IPSEC_PRIVATE_DATA
*Private
,
52 IN IKE_UDP_SERVICE
*UdpService
56 IKEV2_SESSION_COMMON
*SessionCommon
;
57 IKEV2_SA_SESSION
*IkeSaSession
;
59 IkeSaSession
= AllocateZeroPool (sizeof (IKEV2_SA_SESSION
));
60 if (IkeSaSession
== NULL
) {
65 // Initialize the fields of IkeSaSession and its SessionCommon.
67 IkeSaSession
->NCookie
= NULL
;
68 IkeSaSession
->Signature
= IKEV2_SA_SESSION_SIGNATURE
;
69 IkeSaSession
->InitiatorCookie
= IkeGenerateCookie ();
70 IkeSaSession
->ResponderCookie
= 0;
72 // BUGBUG: Message ID starts from 2 is to match the OpenSwan requirement, but it
73 // might not match the IPv6 Logo. In its test specification, it mentions that
74 // the Message ID should start from zero after the IKE_SA_INIT exchange.
76 IkeSaSession
->MessageId
= 2;
77 SessionCommon
= &IkeSaSession
->SessionCommon
;
78 SessionCommon
->UdpService
= UdpService
;
79 SessionCommon
->Private
= Private
;
80 SessionCommon
->IkeSessionType
= IkeSessionTypeIkeSa
;
81 SessionCommon
->IkeVer
= 2;
82 SessionCommon
->AfterEncodePayload
= NULL
;
83 SessionCommon
->BeforeDecodePayload
= NULL
;
86 // Create a resend notfiy event for retry.
88 Status
= gBS
->CreateEvent (
89 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
93 &SessionCommon
->TimeoutEvent
96 if (EFI_ERROR (Status
)) {
97 FreePool (IkeSaSession
);
102 // Initialize the lists in IkeSaSession.
104 InitializeListHead (&IkeSaSession
->ChildSaSessionList
);
105 InitializeListHead (&IkeSaSession
->ChildSaEstablishSessionList
);
106 InitializeListHead (&IkeSaSession
->InfoMIDList
);
107 InitializeListHead (&IkeSaSession
->DeleteSaList
);
113 Register the established IKEv2 SA into Private->Ikev2EstablishedList. If there is
114 IKEV2_SA_SESSION with same remote peer IP, remove the old one then register the
117 @param[in] IkeSaSession Pointer to IKEV2_SA_SESSION to be registered.
118 @param[in] Private Pointer to IPSEC_PRAVATE_DATA.
123 IN IKEV2_SA_SESSION
*IkeSaSession
,
124 IN IPSEC_PRIVATE_DATA
*Private
127 IKEV2_SESSION_COMMON
*SessionCommon
;
128 IKEV2_SA_SESSION
*OldIkeSaSession
;
133 // Keep IKE SA exclusive to remote ip address.
135 SessionCommon
= &IkeSaSession
->SessionCommon
;
136 OldIkeSaSession
= Ikev2SaSessionRemove (&Private
->Ikev2EstablishedList
, &SessionCommon
->RemotePeerIp
);
137 if (OldIkeSaSession
!= NULL
) {
139 // TODO: It should delete all child SAs if rekey the IKE SA.
141 Ikev2SaSessionFree (OldIkeSaSession
);
145 // Cleanup the fields of SessionCommon for processing.
147 Ikev2SessionCommonRefresh (SessionCommon
);
150 // Insert the ready IKE SA session into established list.
152 Ikev2SaSessionInsert (&Private
->Ikev2EstablishedList
, IkeSaSession
, &SessionCommon
->RemotePeerIp
);
155 // Create a notfiy event for the IKE SA life time counting.
157 Status
= gBS
->CreateEvent (
158 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
162 &SessionCommon
->TimeoutEvent
164 if (EFI_ERROR(Status
)){
166 // If TimerEvent creation failed, the SA will be alive untill user disable it or
167 // receiving a Delete Payload from peer.
173 // Start to count the lifetime of the IKE SA.
175 if (IkeSaSession
->Spd
->Data
->ProcessingPolicy
->SaLifetime
.HardLifetime
== 0) {
176 Lifetime
= IKE_SA_DEFAULT_LIFETIME
;
178 Lifetime
= IkeSaSession
->Spd
->Data
->ProcessingPolicy
->SaLifetime
.HardLifetime
;
181 Status
= gBS
->SetTimer (
182 SessionCommon
->TimeoutEvent
,
184 MultU64x32(Lifetime
, 10000000) // ms->100ns
186 if (EFI_ERROR(Status
)){
188 // If SetTimer failed, the SA will be alive untill user disable it or
189 // receiving a Delete Payload from peer.
196 "\n------IkeSa established and start to count down %d seconds lifetime\n",
204 Find a IKEV2_SA_SESSION by the remote peer IP.
206 @param[in] SaSessionList SaSession List to be searched.
207 @param[in] RemotePeerIp Pointer to specified IP address.
209 @return Pointer to IKEV2_SA_SESSION if find one or NULL.
213 Ikev2SaSessionLookup (
214 IN LIST_ENTRY
*SaSessionList
,
215 IN EFI_IP_ADDRESS
*RemotePeerIp
219 IKEV2_SA_SESSION
*IkeSaSession
;
221 NET_LIST_FOR_EACH (Entry
, SaSessionList
) {
222 IkeSaSession
= IKEV2_SA_SESSION_BY_SESSION (Entry
);
225 &IkeSaSession
->SessionCommon
.RemotePeerIp
,
227 sizeof (EFI_IP_ADDRESS
)
238 Insert a IKE_SA_SESSION into IkeSaSession list. The IkeSaSession list is either
239 Private->Ikev2SaSession list or Private->Ikev2EstablishedList list.
241 @param[in] SaSessionList Pointer to list to be inserted into.
242 @param[in] IkeSaSession Pointer to IKEV2_SA_SESSION to be inserted.
243 @param[in] RemotePeerIp Pointer to EFI_IP_ADDRESSS to indicate the
244 unique IKEV2_SA_SESSION.
248 Ikev2SaSessionInsert (
249 IN LIST_ENTRY
*SaSessionList
,
250 IN IKEV2_SA_SESSION
*IkeSaSession
,
251 IN EFI_IP_ADDRESS
*RemotePeerIp
254 Ikev2SaSessionRemove (SaSessionList
, RemotePeerIp
);
255 InsertTailList (SaSessionList
, &IkeSaSession
->BySessionTable
);
259 Remove the SA Session by Remote Peer IP.
261 @param[in] SaSessionList Pointer to list to be searched.
262 @param[in] RemotePeerIp Pointer to EFI_IP_ADDRESS to use for SA Session search.
264 @retval Pointer to IKEV2_SA_SESSION with the specified remote IP address or NULL.
268 Ikev2SaSessionRemove (
269 IN LIST_ENTRY
*SaSessionList
,
270 IN EFI_IP_ADDRESS
*RemotePeerIp
274 IKEV2_SA_SESSION
*IkeSaSession
;
276 NET_LIST_FOR_EACH (Entry
, SaSessionList
) {
277 IkeSaSession
= IKEV2_SA_SESSION_BY_SESSION (Entry
);
280 &IkeSaSession
->SessionCommon
.RemotePeerIp
,
282 sizeof (EFI_IP_ADDRESS
)
285 RemoveEntryList (Entry
);
295 Free specified Seession Common. The session common would belong to a IKE SA or
298 @param[in] SessionCommon Pointer to a Session Common.
302 Ikev2SaSessionCommonFree (
303 IN IKEV2_SESSION_COMMON
*SessionCommon
307 ASSERT (SessionCommon
!= NULL
);
309 if (SessionCommon
->LastSentPacket
!= NULL
) {
310 IkePacketFree (SessionCommon
->LastSentPacket
);
313 if (SessionCommon
->SaParams
!= NULL
) {
314 FreePool (SessionCommon
->SaParams
);
316 if (SessionCommon
->TimeoutEvent
!= NULL
) {
317 gBS
->CloseEvent (SessionCommon
->TimeoutEvent
);
322 After IKE/Child SA is estiblished, close the time event and free sent packet.
324 @param[in] SessionCommon Pointer to a Session Common.
328 Ikev2SessionCommonRefresh (
329 IN IKEV2_SESSION_COMMON
*SessionCommon
332 ASSERT (SessionCommon
!= NULL
);
334 gBS
->CloseEvent (SessionCommon
->TimeoutEvent
);
335 SessionCommon
->TimeoutEvent
= NULL
;
336 SessionCommon
->TimeoutInterval
= 0;
337 SessionCommon
->RetryCount
= 0;
338 if (SessionCommon
->LastSentPacket
!= NULL
) {
339 IkePacketFree (SessionCommon
->LastSentPacket
);
340 SessionCommon
->LastSentPacket
= NULL
;
346 Free specified IKEV2 SA Session.
348 @param[in] IkeSaSession Pointer to IKEV2_SA_SESSION to be freed.
353 IN IKEV2_SA_SESSION
*IkeSaSession
356 IKEV2_SESSION_KEYS
*IkeKeys
;
358 IKEV2_CHILD_SA_SESSION
*ChildSa
;
359 IKEV2_DH_BUFFER
*DhBuffer
;
361 ASSERT (IkeSaSession
!= NULL
);
364 // Delete Common Session
366 Ikev2SaSessionCommonFree (&IkeSaSession
->SessionCommon
);
369 // Delete ChildSaEstablish List and SAD
371 for (Entry
= IkeSaSession
->ChildSaEstablishSessionList
.ForwardLink
;
372 Entry
!= &IkeSaSession
->ChildSaEstablishSessionList
;
375 ChildSa
= IKEV2_CHILD_SA_SESSION_BY_IKE_SA (Entry
);
376 Entry
= Entry
->ForwardLink
;
377 Ikev2ChildSaSilentDelete (ChildSa
->IkeSaSession
, ChildSa
->LocalPeerSpi
);
382 // Delete ChildSaSessionList
384 for ( Entry
= IkeSaSession
->ChildSaSessionList
.ForwardLink
;
385 Entry
!= &IkeSaSession
->ChildSaSessionList
;
387 ChildSa
= IKEV2_CHILD_SA_SESSION_BY_IKE_SA (Entry
);
388 Entry
= Entry
->ForwardLink
;
389 RemoveEntryList (Entry
->BackLink
);
390 Ikev2ChildSaSessionFree (ChildSa
);
394 // Delete DhBuffer and Keys
396 if (IkeSaSession
->IkeKeys
!= NULL
) {
397 IkeKeys
= IkeSaSession
->IkeKeys
;
398 DhBuffer
= IkeKeys
->DhBuffer
;
403 Ikev2DhBufferFree (DhBuffer
);
408 if (IkeKeys
->SkAiKey
!= NULL
) {
409 FreePool (IkeKeys
->SkAiKey
);
411 if (IkeKeys
->SkArKey
!= NULL
) {
412 FreePool (IkeKeys
->SkArKey
);
414 if (IkeKeys
->SkdKey
!= NULL
) {
415 FreePool (IkeKeys
->SkdKey
);
417 if (IkeKeys
->SkEiKey
!= NULL
) {
418 FreePool (IkeKeys
->SkEiKey
);
420 if (IkeKeys
->SkErKey
!= NULL
) {
421 FreePool (IkeKeys
->SkErKey
);
423 if (IkeKeys
->SkPiKey
!= NULL
) {
424 FreePool (IkeKeys
->SkPiKey
);
426 if (IkeKeys
->SkPrKey
!= NULL
) {
427 FreePool (IkeKeys
->SkPrKey
);
432 if (IkeSaSession
->SaData
!= NULL
) {
433 FreePool (IkeSaSession
->SaData
);
436 if (IkeSaSession
->NiBlock
!= NULL
) {
437 FreePool (IkeSaSession
->NiBlock
);
440 if (IkeSaSession
->NrBlock
!= NULL
) {
441 FreePool (IkeSaSession
->NrBlock
);
444 if (IkeSaSession
->NCookie
!= NULL
) {
445 FreePool (IkeSaSession
->NCookie
);
448 if (IkeSaSession
->InitPacket
!= NULL
) {
449 FreePool (IkeSaSession
->InitPacket
);
452 if (IkeSaSession
->RespPacket
!= NULL
) {
453 FreePool (IkeSaSession
->RespPacket
);
456 FreePool (IkeSaSession
);
462 Increase the MessageID in IkeSaSession.
464 @param[in] IkeSaSession Pointer to a specified IKEV2_SA_SESSION.
468 Ikev2SaSessionIncreaseMessageId (
469 IN IKEV2_SA_SESSION
*IkeSaSession
472 if (IkeSaSession
->MessageId
< 0xffffffff) {
473 IkeSaSession
->MessageId
++;
476 // TODO: Trigger Rekey process.
482 Allocate memory for IKEV2 Child SA Session.
484 @param[in] UdpService Pointer to IKE_UDP_SERVICE.
485 @param[in] IkeSaSession Pointer to IKEV2_SA_SESSION related to this Child SA
488 @retval Pointer of a new created IKEV2 Child SA Session or NULL.
491 IKEV2_CHILD_SA_SESSION
*
492 Ikev2ChildSaSessionAlloc (
493 IN IKE_UDP_SERVICE
*UdpService
,
494 IN IKEV2_SA_SESSION
*IkeSaSession
498 IKEV2_CHILD_SA_SESSION
*ChildSaSession
;
499 IKEV2_SESSION_COMMON
*ChildSaCommon
;
500 IKEV2_SESSION_COMMON
*SaCommon
;
502 ChildSaSession
= AllocateZeroPool (sizeof (IKEV2_CHILD_SA_SESSION
));
503 if (ChildSaSession
== NULL
) {
508 // Initialize the fields of ChildSaSession and its SessionCommon.
510 ChildSaSession
->Signature
= IKEV2_CHILD_SA_SESSION_SIGNATURE
;
511 ChildSaSession
->IkeSaSession
= IkeSaSession
;
512 ChildSaSession
->MessageId
= IkeSaSession
->MessageId
;
515 // Generate an new SPI.
517 Status
= IkeGenerateSpi (IkeSaSession
, &(ChildSaSession
->LocalPeerSpi
));
518 if (EFI_ERROR (Status
)) {
519 FreePool (ChildSaSession
);
523 ChildSaCommon
= &ChildSaSession
->SessionCommon
;
524 ChildSaCommon
->UdpService
= UdpService
;
525 ChildSaCommon
->Private
= IkeSaSession
->SessionCommon
.Private
;
526 ChildSaCommon
->IkeSessionType
= IkeSessionTypeChildSa
;
527 ChildSaCommon
->IkeVer
= 2;
528 ChildSaCommon
->AfterEncodePayload
= Ikev2ChildSaAfterEncodePayload
;
529 ChildSaCommon
->BeforeDecodePayload
= Ikev2ChildSaBeforeDecodePayload
;
530 SaCommon
= &ChildSaSession
->IkeSaSession
->SessionCommon
;
533 // Create a resend notfiy event for retry.
535 Status
= gBS
->CreateEvent (
536 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
540 &ChildSaCommon
->TimeoutEvent
542 if (EFI_ERROR (Status
)) {
543 FreePool (ChildSaSession
);
547 CopyMem (&ChildSaCommon
->LocalPeerIp
, &SaCommon
->LocalPeerIp
, sizeof (EFI_IP_ADDRESS
));
548 CopyMem (&ChildSaCommon
->RemotePeerIp
, &SaCommon
->RemotePeerIp
, sizeof (EFI_IP_ADDRESS
));
550 return ChildSaSession
;
554 Register a established IKEv2 Child SA into IkeSaSession->ChildSaEstablishSessionList.
555 If the there is IKEV2_CHILD_SA_SESSION with same remote peer IP, remove the old one
556 then register the new one.
558 @param[in] ChildSaSession Pointer to IKEV2_CHILD_SA_SESSION to be registered.
559 @param[in] Private Pointer to IPSEC_PRAVATE_DATA.
563 Ikev2ChildSaSessionReg (
564 IN IKEV2_CHILD_SA_SESSION
*ChildSaSession
,
565 IN IPSEC_PRIVATE_DATA
*Private
568 IKEV2_SESSION_COMMON
*SessionCommon
;
569 IKEV2_CHILD_SA_SESSION
*OldChildSaSession
;
570 IKEV2_SA_SESSION
*IkeSaSession
;
575 // Keep the IKE SA exclusive.
577 SessionCommon
= &ChildSaSession
->SessionCommon
;
578 IkeSaSession
= ChildSaSession
->IkeSaSession
;
579 OldChildSaSession
= Ikev2ChildSaSessionRemove (
580 &IkeSaSession
->ChildSaEstablishSessionList
,
581 ChildSaSession
->LocalPeerSpi
,
582 IKEV2_ESTABLISHED_CHILDSA_LIST
584 if (OldChildSaSession
!= NULL
) {
588 Ikev2ChildSaSessionFree (OldChildSaSession
);
592 // Store the ready child SA into SAD.
594 Ikev2StoreSaData (ChildSaSession
);
597 // Cleanup the fields of SessionCommon for processing.
599 Ikev2SessionCommonRefresh (SessionCommon
);
602 // Insert the ready child SA session into established list.
604 Ikev2ChildSaSessionInsert (&IkeSaSession
->ChildSaEstablishSessionList
, ChildSaSession
);
607 // Create a Notify event for the IKE SA life time counting.
609 Status
= gBS
->CreateEvent (
610 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
614 &SessionCommon
->TimeoutEvent
616 if (EFI_ERROR(Status
)){
621 // Start to count the lifetime of the IKE SA.
623 if (ChildSaSession
->Spd
->Data
->ProcessingPolicy
->SaLifetime
.HardLifetime
!= 0){
624 Lifetime
= ChildSaSession
->Spd
->Data
->ProcessingPolicy
->SaLifetime
.HardLifetime
;
626 Lifetime
= CHILD_SA_DEFAULT_LIFETIME
;
629 Status
= gBS
->SetTimer (
630 SessionCommon
->TimeoutEvent
,
632 MultU64x32(Lifetime
, 10000000) // ms->100ns
634 if (EFI_ERROR(Status
)){
640 "\n------ChildSa established and start to count down %d seconds lifetime\n",
649 This function find the Child SA by the specified SPI.
651 This functin find a ChildSA session by searching the ChildSaSessionlist of
652 the input IKEV2_SA_SESSION by specified MessageID.
654 @param[in] SaSessionList Pointer to List to be searched.
655 @param[in] Spi Specified SPI.
657 @return Pointer to IKEV2_CHILD_SA_SESSION or NULL.
660 IKEV2_CHILD_SA_SESSION
*
661 Ikev2ChildSaSessionLookupBySpi (
662 IN LIST_ENTRY
*SaSessionList
,
667 IKEV2_CHILD_SA_SESSION
*ChildSaSession
;
669 NET_LIST_FOR_EACH (Entry
, SaSessionList
) {
670 ChildSaSession
= IKEV2_CHILD_SA_SESSION_BY_IKE_SA (Entry
);
672 if (ChildSaSession
->RemotePeerSpi
== Spi
|| ChildSaSession
->LocalPeerSpi
== Spi
) {
673 return ChildSaSession
;
681 Insert a Child SA Session into the specified ChildSa list.
683 @param[in] SaSessionList Pointer to list to be inserted in.
684 @param[in] ChildSaSession Pointer to IKEV2_CHILD_SA_SESSION to be inserted.
688 Ikev2ChildSaSessionInsert (
689 IN LIST_ENTRY
*SaSessionList
,
690 IN IKEV2_CHILD_SA_SESSION
*ChildSaSession
693 InsertTailList (SaSessionList
, &ChildSaSession
->ByIkeSa
);
697 Remove the IKEV2_CHILD_SA_SESSION from IkeSaSessionList.
699 @param[in] SaSessionList The SA Session List to be iterated.
700 @param[in] Spi Spi used to identified the IKEV2_CHILD_SA_SESSION.
701 @param[in] ListType The type of the List to indicate whether it is a
704 @return The point to IKEV2_CHILD_SA_SESSION or NULL.
707 IKEV2_CHILD_SA_SESSION
*
708 Ikev2ChildSaSessionRemove (
709 IN LIST_ENTRY
*SaSessionList
,
715 LIST_ENTRY
*NextEntry
;
716 IKEV2_CHILD_SA_SESSION
*ChildSaSession
;
718 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, SaSessionList
) {
720 if (ListType
== IKEV2_ESTABLISHED_CHILDSA_LIST
|| ListType
== IKEV2_ESTABLISHING_CHILDSA_LIST
) {
721 ChildSaSession
= IKEV2_CHILD_SA_SESSION_BY_IKE_SA (Entry
);
722 } else if (ListType
== IKEV2_DELET_CHILDSA_LIST
) {
723 ChildSaSession
= IKEV2_CHILD_SA_SESSION_BY_DEL_SA (Entry
);
728 if (ChildSaSession
->RemotePeerSpi
== Spi
|| ChildSaSession
->LocalPeerSpi
== Spi
) {
729 RemoveEntryList (Entry
);
730 return ChildSaSession
;
738 Free the memory located for the specified IKEV2_CHILD_SA_SESSION.
740 @param[in] ChildSaSession Pointer to IKEV2_CHILD_SA_SESSION.
744 Ikev2ChildSaSessionFree (
745 IN IKEV2_CHILD_SA_SESSION
*ChildSaSession
748 IKEV2_SESSION_COMMON
*SessionCommon
;
750 SessionCommon
= &ChildSaSession
->SessionCommon
;
751 if (ChildSaSession
->SaData
!= NULL
) {
752 FreePool (ChildSaSession
->SaData
);
755 if (ChildSaSession
->NiBlock
!= NULL
) {
756 FreePool (ChildSaSession
->NiBlock
);
759 if (ChildSaSession
->NrBlock
!= NULL
) {
760 FreePool (ChildSaSession
->NrBlock
);
763 if (ChildSaSession
->ChildKeymats
.LocalPeerInfo
.EspAlgoInfo
.AuthKey
!= NULL
) {
764 FreePool (ChildSaSession
->ChildKeymats
.LocalPeerInfo
.EspAlgoInfo
.AuthKey
);
767 if (ChildSaSession
->ChildKeymats
.LocalPeerInfo
.EspAlgoInfo
.EncKey
!= NULL
) {
768 FreePool (ChildSaSession
->ChildKeymats
.LocalPeerInfo
.EspAlgoInfo
.EncKey
);
771 if (ChildSaSession
->ChildKeymats
.RemotePeerInfo
.EspAlgoInfo
.AuthKey
!= NULL
) {
772 FreePool (ChildSaSession
->ChildKeymats
.RemotePeerInfo
.EspAlgoInfo
.AuthKey
);
775 if (ChildSaSession
->ChildKeymats
.RemotePeerInfo
.EspAlgoInfo
.EncKey
!= NULL
) {
776 FreePool (ChildSaSession
->ChildKeymats
.RemotePeerInfo
.EspAlgoInfo
.EncKey
);
782 Ikev2DhBufferFree (ChildSaSession
->DhBuffer
);
785 // Delete SpdSelector
787 if (ChildSaSession
->SpdSelector
!= NULL
) {
788 if (ChildSaSession
->SpdSelector
->LocalAddress
!= NULL
) {
789 FreePool (ChildSaSession
->SpdSelector
->LocalAddress
);
791 if (ChildSaSession
->SpdSelector
->RemoteAddress
!= NULL
) {
792 FreePool (ChildSaSession
->SpdSelector
->RemoteAddress
);
794 FreePool (ChildSaSession
->SpdSelector
);
796 Ikev2SaSessionCommonFree (SessionCommon
);
797 FreePool (ChildSaSession
);
803 Delete the specified established Child SA.
805 This function delete the Child SA directly and don't send the Information Packet to
808 @param[in] IkeSaSession Pointer to a IKE SA Session used to be searched for.
809 @param[in] Spi SPI used to find the Child SA.
811 @retval EFI_NOT_FOUND Pointer of IKE SA Session is NULL.
812 @retval EFI_NOT_FOUND There is no specified Child SA related with the input
813 SPI under this IKE SA Session.
814 @retval EFI_SUCCESS Delete the Child SA successfully.
818 Ikev2ChildSaSilentDelete (
819 IN IKEV2_SA_SESSION
*IkeSaSession
,
824 EFI_IPSEC_CONFIG_SELECTOR
*Selector
;
826 BOOLEAN IsLocalFound
;
827 BOOLEAN IsRemoteFound
;
830 IKEV2_CHILD_SA_SESSION
*ChildSession
;
831 EFI_IPSEC_CONFIG_SELECTOR
*LocalSelector
;
832 EFI_IPSEC_CONFIG_SELECTOR
*RemoteSelector
;
833 IPSEC_PRIVATE_DATA
*Private
;
835 if (IkeSaSession
== NULL
) {
836 return EFI_NOT_FOUND
;
839 IsLocalFound
= FALSE
;
840 IsRemoteFound
= FALSE
;
842 LocalSelector
= NULL
;
843 RemoteSelector
= NULL
;
845 Private
= IkeSaSession
->SessionCommon
.Private
;
848 // Remove the Established SA from ChildSaEstablishlist.
850 ChildSession
= Ikev2ChildSaSessionRemove(
851 &(IkeSaSession
->ChildSaEstablishSessionList
),
853 IKEV2_ESTABLISHED_CHILDSA_LIST
855 if (ChildSession
== NULL
) {
856 return EFI_NOT_FOUND
;
859 LocalSpi
= ChildSession
->LocalPeerSpi
;
860 RemoteSpi
= ChildSession
->RemotePeerSpi
;
862 SelectorSize
= sizeof (EFI_IPSEC_CONFIG_SELECTOR
);
863 Selector
= AllocateZeroPool (SelectorSize
);
864 if (Selector
== NULL
) {
865 return EFI_OUT_OF_RESOURCES
;
869 Status
= EfiIpSecConfigGetNextSelector (
870 &Private
->IpSecConfig
,
871 IPsecConfigDataTypeSad
,
875 if (Status
== EFI_BUFFER_TOO_SMALL
) {
878 Selector
= AllocateZeroPool (SelectorSize
);
879 if (Selector
== NULL
) {
880 Status
= EFI_OUT_OF_RESOURCES
;
884 Status
= EfiIpSecConfigGetNextSelector (
885 &Private
->IpSecConfig
,
886 IPsecConfigDataTypeSad
,
892 if (EFI_ERROR (Status
)) {
896 if (Selector
->SaId
.Spi
== RemoteSpi
) {
898 // SPI is unique. There is only one SAD whose SPI is
899 // same with RemoteSpi.
901 IsRemoteFound
= TRUE
;
902 RemoteSelector
= AllocateZeroPool (SelectorSize
);
903 if (RemoteSelector
== NULL
) {
904 Status
= EFI_OUT_OF_RESOURCES
;
908 CopyMem (RemoteSelector
, Selector
, SelectorSize
);
911 if (Selector
->SaId
.Spi
== LocalSpi
) {
913 // SPI is unique. There is only one SAD whose SPI is
914 // same with LocalSpi.
917 LocalSelector
= AllocateZeroPool (SelectorSize
);
918 if (LocalSelector
== NULL
) {
919 Status
= EFI_OUT_OF_RESOURCES
;
923 CopyMem (LocalSelector
, Selector
, SelectorSize
);
927 // Delete SA from the Variable.
930 Status
= EfiIpSecConfigSetData (
931 &Private
->IpSecConfig
,
932 IPsecConfigDataTypeSad
,
940 Status
= EfiIpSecConfigSetData (
941 &Private
->IpSecConfig
,
942 IPsecConfigDataTypeSad
,
952 "\n------IKEV2 deleted ChildSa(local spi, remote spi):(0x%x, 0x%x)------\n",
956 Ikev2ChildSaSessionFree (ChildSession
);
958 if (RemoteSelector
!= NULL
) {
959 FreePool (RemoteSelector
);
962 if (LocalSelector
!= NULL
) {
963 FreePool (LocalSelector
);
966 if (Selector
!= NULL
) {
974 Free the specified DhBuffer.
976 @param[in] DhBuffer Pointer to IKEV2_DH_BUFFER to be freed.
981 IKEV2_DH_BUFFER
*DhBuffer
984 if (DhBuffer
!= NULL
) {
985 if (DhBuffer
->GxBuffer
!= NULL
) {
986 FreePool (DhBuffer
->GxBuffer
);
988 if (DhBuffer
->GyBuffer
!= NULL
) {
989 FreePool (DhBuffer
->GyBuffer
);
991 if (DhBuffer
->GxyBuffer
!= NULL
) {
992 FreePool (DhBuffer
->GxyBuffer
);
994 if (DhBuffer
->DhContext
!= NULL
) {
995 IpSecCryptoIoFreeDh (&DhBuffer
->DhContext
);
1002 This function is to parse a request IKE packet and return its request type.
1003 The request type is one of IKE CHILD SA creation, IKE SA rekeying and
1004 IKE CHILD SA rekeying.
1006 @param[in] IkePacket IKE packet to be prased.
1008 return the type of the IKE packet.
1011 IKEV2_CREATE_CHILD_REQUEST_TYPE
1012 Ikev2ChildExchangeRequestType(
1013 IN IKE_PACKET
*IkePacket
1018 IKE_PAYLOAD
*IkePayload
;
1022 NET_LIST_FOR_EACH (Entry
, &(IkePacket
)->PayloadList
) {
1023 IkePayload
= IKE_PAYLOAD_BY_PACKET (Entry
);
1024 if (IkePayload
->PayloadType
== IKEV2_PAYLOAD_TYPE_TS_INIT
) {
1026 // Packet with Ts Payload means it is for either CHILD_SA_CREATE or CHILD_SA_REKEY.
1030 if (IkePayload
->PayloadType
== IKEV2_PAYLOAD_TYPE_NOTIFY
) {
1031 if (((IKEV2_NOTIFY
*)IkePayload
)->MessageType
== IKEV2_NOTIFICATION_REKEY_SA
) {
1033 // If notify payload with REKEY_SA message type, the IkePacket is for
1034 // rekeying Child SA.
1036 return IkeRequestTypeRekeyChildSa
;
1043 // The Create Child Exchange is for IKE SA rekeying.
1045 return IkeRequestTypeRekeyIkeSa
;
1048 // If the Notify payloaad with transport mode message type, the IkePacket is
1049 // for create Child SA.
1051 return IkeRequestTypeCreateChildSa
;
1056 Associate a SPD selector to the Child SA Session.
1058 This function is called when the Child SA is not the first child SA of its
1059 IKE SA. It associate a SPD to this Child SA.
1061 @param[in, out] ChildSaSession Pointer to the Child SA Session to be associated to
1064 @retval EFI_SUCCESS Associate one SPD selector to this Child SA Session successfully.
1065 @retval EFI_NOT_FOUND Can't find the related SPD selector.
1069 Ikev2ChildSaAssociateSpdEntry (
1070 IN OUT IKEV2_CHILD_SA_SESSION
*ChildSaSession
1073 IpSecVisitConfigData (IPsecConfigDataTypeSpd
, Ikev2MatchSpdEntry
, ChildSaSession
);
1074 if (ChildSaSession
->Spd
!= NULL
) {
1077 return EFI_NOT_FOUND
;
1084 Validate the IKE header of received IKE packet.
1086 @param[in] IkeSaSession Pointer to IKEV2_SA_SESSION related to this IKE packet.
1087 @param[in] IkeHdr Pointer to IKE header of received IKE packet.
1089 @retval TRUE If the IKE header is valid.
1090 @retval FALSE If the IKE header is invalid.
1094 Ikev2ValidateHeader (
1095 IN IKEV2_SA_SESSION
*IkeSaSession
,
1096 IN IKE_HEADER
*IkeHdr
1100 IKEV2_SESSION_STATE State
;
1102 State
= IkeSaSession
->SessionCommon
.State
;
1103 if (State
== IkeStateInit
) {
1105 // For the IKE Initial Exchange, the MessagId should be zero.
1107 if (IkeHdr
->MessageId
!= 0) {
1111 if (State
== IkeStateAuth
) {
1112 if (IkeHdr
->MessageId
!= 1) {
1116 if (IkeHdr
->InitiatorCookie
!= IkeSaSession
->InitiatorCookie
||
1117 IkeHdr
->ResponderCookie
!= IkeSaSession
->ResponderCookie
1120 // TODO: send notification INVALID-COOKIE
1127 // Information Exchagne and Create Child Exchange can be started from each part.
1129 if (IkeHdr
->ExchangeType
!= IKEV2_EXCHANGE_TYPE_INFO
&&
1130 IkeHdr
->ExchangeType
!= IKEV2_EXCHANGE_TYPE_CREATE_CHILD
1132 if (IkeSaSession
->SessionCommon
.IsInitiator
) {
1133 if (IkeHdr
->InitiatorCookie
!= IkeSaSession
->InitiatorCookie
) {
1135 // TODO: send notification INVALID-COOKIE
1139 if (IkeHdr
->Flags
!= IKE_HEADER_FLAGS_RESPOND
) {
1143 if (IkeHdr
->Flags
!= IKE_HEADER_FLAGS_INIT
) {
1153 Create and intialize IKEV2_SA_DATA for speicifed IKEV2_SESSION_COMMON.
1155 This function will be only called by the initiator. The responder's IKEV2_SA_DATA
1156 will be generated during parsed the initiator packet.
1158 @param[in] SessionCommon Pointer to IKEV2_SESSION_COMMON related to.
1160 @retval a Pointer to a new IKEV2_SA_DATA or NULL.
1164 Ikev2InitializeSaData (
1165 IN IKEV2_SESSION_COMMON
*SessionCommon
1168 IKEV2_CHILD_SA_SESSION
*ChildSaSession
;
1169 IKEV2_SA_DATA
*SaData
;
1170 IKEV2_PROPOSAL_DATA
*ProposalData
;
1171 IKEV2_TRANSFORM_DATA
*TransformData
;
1172 IKE_SA_ATTRIBUTE
*Attribute
;
1174 ASSERT (SessionCommon
!= NULL
);
1176 // TODO: Remove the hard code of the support Alogrithm. Those data should be
1177 // get from the SPD/PAD data.
1179 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1180 SaData
= AllocateZeroPool (
1181 sizeof (IKEV2_SA_DATA
) +
1182 sizeof (IKEV2_PROPOSAL_DATA
) * 2 +
1183 sizeof (IKEV2_TRANSFORM_DATA
) * 4 * 2
1186 SaData
= AllocateZeroPool (
1187 sizeof (IKEV2_SA_DATA
) +
1188 sizeof (IKEV2_PROPOSAL_DATA
) * 2 +
1189 sizeof (IKEV2_TRANSFORM_DATA
) * 3 * 2
1192 if (SaData
== NULL
) {
1197 // First proposal payload: 3DES + SHA1 + DH
1199 SaData
->NumProposals
= 2;
1200 ProposalData
= (IKEV2_PROPOSAL_DATA
*) (SaData
+ 1);
1201 ProposalData
->ProposalIndex
= 1;
1204 // If SA data for IKE_SA_INIT exchage, contains 4 transforms. If SA data for
1205 // IKE_AUTH exchange contains 3 transforms.
1207 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1208 ProposalData
->NumTransforms
= 4;
1210 ProposalData
->NumTransforms
= 3;
1214 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1215 ProposalData
->ProtocolId
= IPSEC_PROTO_ISAKMP
;
1217 ChildSaSession
= IKEV2_CHILD_SA_SESSION_FROM_COMMON (SessionCommon
);
1218 ProposalData
->ProtocolId
= IPSEC_PROTO_IPSEC_ESP
;
1219 ProposalData
->Spi
= AllocateZeroPool (sizeof (ChildSaSession
->LocalPeerSpi
));
1220 if (ProposalData
->Spi
== NULL
) {
1227 &ChildSaSession
->LocalPeerSpi
,
1228 sizeof(ChildSaSession
->LocalPeerSpi
)
1233 // Set transform attribute for Encryption Algorithm - 3DES
1235 TransformData
= (IKEV2_TRANSFORM_DATA
*) (ProposalData
+ 1);
1236 TransformData
->TransformIndex
= 0;
1237 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_ENCR
;
1238 TransformData
->TransformId
= IKEV2_TRANSFORM_ID_ENCR_3DES
;
1241 // Set transform attribute for Integrity Algorithm - SHA1_96
1243 TransformData
= (IKEV2_TRANSFORM_DATA
*) (TransformData
+ 1);
1244 TransformData
->TransformIndex
= 1;
1245 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_INTEG
;
1246 TransformData
->TransformId
= IKEV2_TRANSFORM_ID_AUTH_HMAC_SHA1_96
;
1248 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1250 // Set transform attribute for Pseduo-Random Function - HAMC_SHA1
1252 TransformData
= (IKEV2_TRANSFORM_DATA
*) (TransformData
+ 1);
1253 TransformData
->TransformIndex
= 2;
1254 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_PRF
;
1255 TransformData
->TransformId
= IKEV2_TRANSFORM_ID_PRF_HMAC_SHA1
;
1258 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1260 // Set transform attribute for DH Group - DH 1024
1262 TransformData
= (IKEV2_TRANSFORM_DATA
*) (TransformData
+ 1);
1263 TransformData
->TransformIndex
= 3;
1264 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_DH
;
1265 TransformData
->TransformId
= IKEV2_TRANSFORM_ID_DH_1024MODP
;
1268 // Transform type for Extended Sequence Numbers. Currently not support Extended
1271 TransformData
= (IKEV2_TRANSFORM_DATA
*) (TransformData
+ 1);
1272 TransformData
->TransformIndex
= 2;
1273 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_ESN
;
1274 TransformData
->TransformId
= 0;
1278 // Second proposal payload: 3DES + SHA1 + DH
1280 ProposalData
= (IKEV2_PROPOSAL_DATA
*) (TransformData
+ 1);
1281 ProposalData
->ProposalIndex
= 2;
1283 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1284 ProposalData
->ProtocolId
= IPSEC_PROTO_ISAKMP
;
1285 ProposalData
->NumTransforms
= 4;
1288 ChildSaSession
= IKEV2_CHILD_SA_SESSION_FROM_COMMON (SessionCommon
);
1289 ProposalData
->ProtocolId
= IPSEC_PROTO_IPSEC_ESP
;
1290 ProposalData
->NumTransforms
= 3;
1291 ProposalData
->Spi
= AllocateZeroPool (sizeof (ChildSaSession
->LocalPeerSpi
));
1292 if (ProposalData
->Spi
== NULL
) {
1293 FreePool (((IKEV2_PROPOSAL_DATA
*) (SaData
+ 1))->Spi
);
1300 &ChildSaSession
->LocalPeerSpi
,
1301 sizeof(ChildSaSession
->LocalPeerSpi
)
1306 // Set transform attribute for Encryption Algorithm - AES-CBC
1308 TransformData
= (IKEV2_TRANSFORM_DATA
*) (ProposalData
+ 1);
1309 TransformData
->TransformIndex
= 0;
1310 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_ENCR
;
1311 TransformData
->TransformId
= IKEV2_TRANSFORM_ID_ENCR_AES_CBC
;
1312 Attribute
= &TransformData
->Attribute
;
1313 Attribute
->AttrType
= IKEV2_ATTRIBUTE_TYPE_KEYLEN
;
1314 Attribute
->Attr
.AttrLength
= (UINT16
) (8 * IpSecGetEncryptKeyLength (IKEV2_TRANSFORM_ID_ENCR_AES_CBC
));
1317 // Set transform attribute for Integrity Algorithm - SHA1_96
1319 TransformData
= (IKEV2_TRANSFORM_DATA
*) (TransformData
+ 1);
1320 TransformData
->TransformIndex
= 1;
1321 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_INTEG
;
1322 TransformData
->TransformId
= IKEV2_TRANSFORM_ID_AUTH_HMAC_SHA1_96
;
1324 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1326 // Set transform attribute for Pseduo-Random Function - HAMC_SHA1
1328 TransformData
= (IKEV2_TRANSFORM_DATA
*) (TransformData
+ 1);
1329 TransformData
->TransformIndex
= 2;
1330 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_PRF
;
1331 TransformData
->TransformId
= IKEV2_TRANSFORM_ID_PRF_HMAC_SHA1
;
1334 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1336 // Set transform attrbiute for DH Group - DH-1024
1338 TransformData
= (IKEV2_TRANSFORM_DATA
*) (TransformData
+ 1);
1339 TransformData
->TransformIndex
= 3;
1340 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_DH
;
1341 TransformData
->TransformId
= IKEV2_TRANSFORM_ID_DH_1024MODP
;
1344 // Transform type for Extended Sequence Numbers. Currently not support Extended
1347 TransformData
= (IKEV2_TRANSFORM_DATA
*) (TransformData
+ 1);
1348 TransformData
->TransformIndex
= 2;
1349 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_ESN
;
1350 TransformData
->TransformId
= 0;
1357 Store the SA into SAD.
1359 @param[in] ChildSaSession Pointer to IKEV2_CHILD_SA_SESSION.
1364 IN IKEV2_CHILD_SA_SESSION
*ChildSaSession
1368 EFI_IPSEC_SA_ID SaId
;
1369 EFI_IPSEC_SA_DATA2 SaData
;
1370 IKEV2_SESSION_COMMON
*SessionCommon
;
1371 IPSEC_PRIVATE_DATA
*Private
;
1372 UINT32 TempAddressCount
;
1373 EFI_IP_ADDRESS_INFO
*TempAddressInfo
;
1375 SessionCommon
= &ChildSaSession
->SessionCommon
;
1376 Private
= SessionCommon
->Private
;
1378 ZeroMem (&SaId
, sizeof (EFI_IPSEC_SA_ID
));
1379 ZeroMem (&SaData
, sizeof (EFI_IPSEC_SA_DATA2
));
1382 // Create a SpdSelector. In this implementation, one SPD represents
1383 // 2 direction traffic, so in here, there needs to reverse the local address
1384 // and remote address for Remote Peer's SA, then reverse again for the locate
1387 TempAddressCount
= ChildSaSession
->SpdSelector
->LocalAddressCount
;
1388 TempAddressInfo
= ChildSaSession
->SpdSelector
->LocalAddress
;
1390 ChildSaSession
->SpdSelector
->LocalAddressCount
= ChildSaSession
->SpdSelector
->RemoteAddressCount
;
1391 ChildSaSession
->SpdSelector
->LocalAddress
= ChildSaSession
->SpdSelector
->RemoteAddress
;
1393 ChildSaSession
->SpdSelector
->RemoteAddress
= TempAddressInfo
;
1394 ChildSaSession
->SpdSelector
->RemoteAddressCount
= TempAddressCount
;
1397 // Set the SaId and SaData.
1399 SaId
.Spi
= ChildSaSession
->LocalPeerSpi
;
1400 SaId
.Proto
= EfiIPsecESP
;
1401 SaData
.AntiReplayWindows
= 16;
1403 SaData
.Mode
= ChildSaSession
->Spd
->Data
->ProcessingPolicy
->Mode
;
1406 // If it is tunnel mode, should add the TunnelDest and TunnelSource for SaData.
1408 if (SaData
.Mode
== EfiIPsecTunnel
) {
1410 &SaData
.TunnelSourceAddress
,
1411 &ChildSaSession
->Spd
->Data
->ProcessingPolicy
->TunnelOption
->RemoteTunnelAddress
,
1412 sizeof (EFI_IP_ADDRESS
)
1415 &SaData
.TunnelDestinationAddress
,
1416 &ChildSaSession
->Spd
->Data
->ProcessingPolicy
->TunnelOption
->LocalTunnelAddress
,
1417 sizeof (EFI_IP_ADDRESS
)
1421 CopyMem (&SaId
.DestAddress
, &ChildSaSession
->SessionCommon
.LocalPeerIp
, sizeof (EFI_IP_ADDRESS
));
1422 CopyMem (&SaData
.AlgoInfo
, &ChildSaSession
->ChildKeymats
.LocalPeerInfo
, sizeof (EFI_IPSEC_ALGO_INFO
));
1423 SaData
.SpdSelector
= ChildSaSession
->SpdSelector
;
1426 // Store the remote SA into SAD.
1428 Status
= EfiIpSecConfigSetData (
1429 &Private
->IpSecConfig
,
1430 IPsecConfigDataTypeSad
,
1431 (EFI_IPSEC_CONFIG_SELECTOR
*) &SaId
,
1435 ASSERT_EFI_ERROR (Status
);
1438 // Store the local SA into SAD.
1440 ChildSaSession
->SpdSelector
->RemoteAddressCount
= ChildSaSession
->SpdSelector
->LocalAddressCount
;
1441 ChildSaSession
->SpdSelector
->RemoteAddress
= ChildSaSession
->SpdSelector
->LocalAddress
;
1443 ChildSaSession
->SpdSelector
->LocalAddress
= TempAddressInfo
;
1444 ChildSaSession
->SpdSelector
->LocalAddressCount
= TempAddressCount
;
1446 SaId
.Spi
= ChildSaSession
->RemotePeerSpi
;
1448 CopyMem (&SaId
.DestAddress
, &ChildSaSession
->SessionCommon
.RemotePeerIp
, sizeof (EFI_IP_ADDRESS
));
1449 CopyMem (&SaData
.AlgoInfo
, &ChildSaSession
->ChildKeymats
.RemotePeerInfo
, sizeof (EFI_IPSEC_ALGO_INFO
));
1450 SaData
.SpdSelector
= ChildSaSession
->SpdSelector
;
1453 // If it is tunnel mode, should add the TunnelDest and TunnelSource for SaData.
1455 if (SaData
.Mode
== EfiIPsecTunnel
) {
1457 &SaData
.TunnelSourceAddress
,
1458 &ChildSaSession
->Spd
->Data
->ProcessingPolicy
->TunnelOption
->LocalTunnelAddress
,
1459 sizeof (EFI_IP_ADDRESS
)
1462 &SaData
.TunnelDestinationAddress
,
1463 &ChildSaSession
->Spd
->Data
->ProcessingPolicy
->TunnelOption
->RemoteTunnelAddress
,
1464 sizeof (EFI_IP_ADDRESS
)
1468 Status
= EfiIpSecConfigSetData (
1469 &Private
->IpSecConfig
,
1470 IPsecConfigDataTypeSad
,
1471 (EFI_IPSEC_CONFIG_SELECTOR
*) &SaId
,
1476 ASSERT_EFI_ERROR (Status
);
1480 Call back function of the IKE life time is over.
1482 This function will mark the related IKE SA Session as deleting and trigger a
1483 Information negotiation.
1485 @param[in] Event The signaled Event.
1486 @param[in] Context Pointer to data passed by caller.
1491 Ikev2LifetimeNotify (
1496 IKEV2_SA_SESSION
*IkeSaSession
;
1497 IKEV2_CHILD_SA_SESSION
*ChildSaSession
;
1498 IKEV2_SESSION_COMMON
*SessionCommon
;
1500 ASSERT (Context
!= NULL
);
1501 SessionCommon
= (IKEV2_SESSION_COMMON
*) Context
;
1503 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1504 IkeSaSession
= IKEV2_SA_SESSION_FROM_COMMON (SessionCommon
);
1507 "\n---IkeSa Lifetime is out(cookie_i, cookie_r):(0x%lx, 0x%lx)---\n",
1508 IkeSaSession
->InitiatorCookie
,
1509 IkeSaSession
->ResponderCookie
1513 // Change the IKE SA Session's State to IKE_STATE_SA_DELETING.
1515 IKEV2_DUMP_STATE (IkeSaSession
->SessionCommon
.State
, IkeStateSaDeleting
);
1516 IkeSaSession
->SessionCommon
.State
= IkeStateSaDeleting
;
1519 ChildSaSession
= IKEV2_CHILD_SA_SESSION_FROM_COMMON (SessionCommon
);
1520 IkeSaSession
= ChildSaSession
->IkeSaSession
;
1523 // Link the timeout child SA to the DeleteSaList.
1525 InsertTailList (&IkeSaSession
->DeleteSaList
, &ChildSaSession
->ByDelete
);
1528 // Change the Child SA Session's State to IKE_STATE_SA_DELETING.
1532 "\n------ChildSa Lifetime is out(SPI):(0x%x)------\n",
1533 ChildSaSession
->LocalPeerSpi
1538 // TODO: Send the delete info packet or delete silently
1540 mIkev2Exchange
.NegotiateInfo ((UINT8
*) IkeSaSession
, NULL
);
1544 This function will be called if the TimeOut Event is signaled.
1546 @param[in] Event The signaled Event.
1547 @param[in] Context The data passed by caller.
1557 IPSEC_PRIVATE_DATA
*Private
;
1558 IKEV2_SA_SESSION
*IkeSaSession
;
1559 IKEV2_CHILD_SA_SESSION
*ChildSaSession
;
1560 IKEV2_SESSION_COMMON
*SessionCommon
;
1561 LIST_ENTRY
*ChildSaEntry
;
1565 ASSERT (Context
!= NULL
);
1566 IkeSaSession
= NULL
;
1567 ChildSaSession
= NULL
;
1568 SessionCommon
= (IKEV2_SESSION_COMMON
*) Context
;
1569 Private
= SessionCommon
->Private
;
1572 // Remove the SA session from the processing list if exceed the max retry.
1574 if (SessionCommon
->RetryCount
> IKE_MAX_RETRY
) {
1575 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1576 IkeSaSession
= IKEV2_SA_SESSION_FROM_COMMON (SessionCommon
);
1577 if (IkeSaSession
->SessionCommon
.State
== IkeStateSaDeleting
) {
1580 // If the IkeSaSession is initiator, delete all its Child SAs before removing IKE SA.
1581 // If the IkesaSession is responder, all ChildSa has been remove in Ikev2HandleInfo();
1583 for (ChildSaEntry
= IkeSaSession
->ChildSaEstablishSessionList
.ForwardLink
;
1584 ChildSaEntry
!= &IkeSaSession
->ChildSaEstablishSessionList
;
1586 ChildSaSession
= IKEV2_CHILD_SA_SESSION_BY_IKE_SA (ChildSaEntry
);
1588 // Move to next ChildSa Entry.
1590 ChildSaEntry
= ChildSaEntry
->ForwardLink
;
1592 // Delete LocalSpi & RemoteSpi and remove the ChildSaSession from the
1593 // EstablishedChildSaList.
1595 Ikev2ChildSaSilentDelete (IkeSaSession
, ChildSaSession
->LocalPeerSpi
);
1599 // If the IKE SA Delete Payload wasn't sent out successfully, Delete it from the EstablishedList.
1601 Ikev2SaSessionRemove (&Private
->Ikev2EstablishedList
, &SessionCommon
->RemotePeerIp
);
1603 if (Private
!= NULL
&& Private
->IsIPsecDisabling
) {
1605 // After all IKE SAs were deleted, set the IPSEC_STATUS_DISABLED value in
1606 // IPsec status variable.
1608 if (IsListEmpty (&Private
->Ikev1EstablishedList
) && IsListEmpty (&Private
->Ikev2EstablishedList
)) {
1609 Value
= IPSEC_STATUS_DISABLED
;
1610 Status
= gRT
->SetVariable (
1611 IPSECCONFIG_STATUS_NAME
,
1612 &gEfiIpSecConfigProtocolGuid
,
1613 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1617 if (!EFI_ERROR (Status
)) {
1619 // Set the Disabled Flag in Private data.
1621 Private
->IpSec
.DisabledFlag
= TRUE
;
1622 Private
->IsIPsecDisabling
= FALSE
;
1627 Ikev2SaSessionRemove (&Private
->Ikev2SessionList
, &SessionCommon
->RemotePeerIp
);
1629 Ikev2SaSessionFree (IkeSaSession
);
1634 // If the packet sent by Child SA.
1636 ChildSaSession
= IKEV2_CHILD_SA_SESSION_FROM_COMMON (SessionCommon
);
1637 IkeSaSession
= ChildSaSession
->IkeSaSession
;
1638 if (ChildSaSession
->SessionCommon
.State
== IkeStateSaDeleting
) {
1641 // Established Child SA should be remove from the SAD entry and
1642 // DeleteList. The function of Ikev2DeleteChildSaSilent() will remove
1643 // the childSA from the IkeSaSession->ChildSaEstablishedList. So there
1644 // is no need to remove it here.
1646 Ikev2ChildSaSilentDelete (IkeSaSession
, ChildSaSession
->LocalPeerSpi
);
1647 Ikev2ChildSaSessionRemove (
1648 &IkeSaSession
->DeleteSaList
,
1649 ChildSaSession
->LocalPeerSpi
,
1650 IKEV2_DELET_CHILDSA_LIST
1653 Ikev2ChildSaSessionRemove (
1654 &IkeSaSession
->ChildSaSessionList
,
1655 ChildSaSession
->LocalPeerSpi
,
1656 IKEV2_ESTABLISHING_CHILDSA_LIST
1660 Ikev2ChildSaSessionFree (ChildSaSession
);
1666 // Increase the retry count.
1668 SessionCommon
->RetryCount
++;
1669 DEBUG ((DEBUG_INFO
, ">>>Resending the last packet ...\n"));
1672 // Resend the last packet.
1674 Ikev2SendIkePacket (
1675 SessionCommon
->UdpService
,
1676 (UINT8
*)SessionCommon
,
1677 SessionCommon
->LastSentPacket
,
1683 Copy ChildSaSession->Spd->Selector to ChildSaSession->SpdSelector.
1685 ChildSaSession->SpdSelector stores the real Spdselector for its SA. Sometime,
1686 the SpdSelector in ChildSaSession is more accurated or the scope is smaller
1687 than the one in ChildSaSession->Spd, especially for the tunnel mode.
1689 @param[in, out] ChildSaSession Pointer to IKEV2_CHILD_SA_SESSION related to.
1691 @retval EFI_SUCCESS The operation complete successfully.
1692 @retval EFI_OUT_OF_RESOURCES If the required resource can't be allocated.
1696 Ikev2ChildSaSessionSpdSelectorCreate (
1697 IN OUT IKEV2_CHILD_SA_SESSION
*ChildSaSession
1702 Status
= EFI_SUCCESS
;
1704 if (ChildSaSession
->Spd
!= NULL
&& ChildSaSession
->Spd
->Selector
!= NULL
) {
1705 if (ChildSaSession
->SpdSelector
== NULL
) {
1706 ChildSaSession
->SpdSelector
= AllocateZeroPool (sizeof (EFI_IPSEC_SPD_SELECTOR
));
1707 if (ChildSaSession
->SpdSelector
== NULL
) {
1708 Status
= EFI_OUT_OF_RESOURCES
;
1713 ChildSaSession
->SpdSelector
,
1714 ChildSaSession
->Spd
->Selector
,
1715 sizeof (EFI_IPSEC_SPD_SELECTOR
)
1717 ChildSaSession
->SpdSelector
->RemoteAddress
= AllocateCopyPool (
1718 ChildSaSession
->Spd
->Selector
->RemoteAddressCount
*
1719 sizeof (EFI_IP_ADDRESS_INFO
),
1720 ChildSaSession
->Spd
->Selector
->RemoteAddress
1722 if (ChildSaSession
->SpdSelector
->RemoteAddress
== NULL
) {
1723 Status
= EFI_OUT_OF_RESOURCES
;
1725 FreePool (ChildSaSession
->SpdSelector
);
1730 ChildSaSession
->SpdSelector
->LocalAddress
= AllocateCopyPool (
1731 ChildSaSession
->Spd
->Selector
->LocalAddressCount
*
1732 sizeof (EFI_IP_ADDRESS_INFO
),
1733 ChildSaSession
->Spd
->Selector
->LocalAddress
1735 if (ChildSaSession
->SpdSelector
->LocalAddress
== NULL
) {
1736 Status
= EFI_OUT_OF_RESOURCES
;
1738 FreePool (ChildSaSession
->SpdSelector
->RemoteAddress
);
1740 FreePool (ChildSaSession
->SpdSelector
);
1745 ChildSaSession
->SpdSelector
->RemoteAddressCount
= ChildSaSession
->Spd
->Selector
->RemoteAddressCount
;
1746 ChildSaSession
->SpdSelector
->LocalAddressCount
= ChildSaSession
->Spd
->Selector
->LocalAddressCount
;
1753 Generate a ChildSa Session and insert it into related IkeSaSession.
1755 @param[in] IkeSaSession Pointer to related IKEV2_SA_SESSION.
1756 @param[in] UdpService Pointer to related IKE_UDP_SERVICE.
1758 @return pointer of IKEV2_CHILD_SA_SESSION.
1761 IKEV2_CHILD_SA_SESSION
*
1762 Ikev2ChildSaSessionCreate (
1763 IN IKEV2_SA_SESSION
*IkeSaSession
,
1764 IN IKE_UDP_SERVICE
*UdpService
1767 IKEV2_CHILD_SA_SESSION
*ChildSaSession
;
1768 IKEV2_SESSION_COMMON
*ChildSaCommon
;
1771 // Create a new ChildSaSession.Insert it into processing list and initiate the common parameters.
1773 ChildSaSession
= Ikev2ChildSaSessionAlloc (UdpService
, IkeSaSession
);
1774 if (ChildSaSession
== NULL
) {
1779 // Set the specific parameters.
1781 ChildSaSession
->Spd
= IkeSaSession
->Spd
;
1782 ChildSaCommon
= &ChildSaSession
->SessionCommon
;
1783 ChildSaCommon
->IsInitiator
= IkeSaSession
->SessionCommon
.IsInitiator
;
1784 if (IkeSaSession
->SessionCommon
.State
== IkeStateAuth
) {
1785 ChildSaCommon
->State
= IkeStateAuth
;
1786 IKEV2_DUMP_STATE (ChildSaCommon
->State
, IkeStateAuth
);
1788 ChildSaCommon
->State
= IkeStateCreateChild
;
1789 IKEV2_DUMP_STATE (ChildSaCommon
->State
, IkeStateCreateChild
);
1793 // If SPD->Selector is not NULL, copy it to the ChildSaSession->SpdSelector.
1794 // The ChildSaSession->SpdSelector might be changed after the traffic selector
1795 // negoniation and it will be copied into the SAData after ChildSA established.
1797 if (EFI_ERROR (Ikev2ChildSaSessionSpdSelectorCreate (ChildSaSession
))) {
1798 Ikev2ChildSaSessionFree (ChildSaSession
);
1803 // Copy first NiBlock and NrBlock to ChildSa Session
1805 ChildSaSession
->NiBlock
= AllocateZeroPool (IkeSaSession
->NiBlkSize
);
1806 if (ChildSaSession
->NiBlock
== NULL
) {
1807 Ikev2ChildSaSessionFree (ChildSaSession
);
1811 ChildSaSession
->NiBlkSize
= IkeSaSession
->NiBlkSize
;
1812 CopyMem (ChildSaSession
->NiBlock
, IkeSaSession
->NiBlock
, IkeSaSession
->NiBlkSize
);
1814 ChildSaSession
->NrBlock
= AllocateZeroPool (IkeSaSession
->NrBlkSize
);
1815 if (ChildSaSession
->NrBlock
== NULL
) {
1816 Ikev2ChildSaSessionFree (ChildSaSession
);
1820 ChildSaSession
->NrBlkSize
= IkeSaSession
->NrBlkSize
;
1821 CopyMem (ChildSaSession
->NrBlock
, IkeSaSession
->NrBlock
, IkeSaSession
->NrBlkSize
);
1824 // Only if the Create Child SA is called for the IKE_INIT Exchange and
1825 // IkeSaSession is initiator (Only Initiator's SPD is not NULL), Set the
1826 // Traffic Selectors related information here.
1828 if (IkeSaSession
->SessionCommon
.State
== IkeStateAuth
&& IkeSaSession
->Spd
!= NULL
) {
1829 ChildSaSession
->ProtoId
= IkeSaSession
->Spd
->Selector
->NextLayerProtocol
;
1830 ChildSaSession
->LocalPort
= IkeSaSession
->Spd
->Selector
->LocalPort
;
1831 ChildSaSession
->RemotePort
= IkeSaSession
->Spd
->Selector
->RemotePort
;
1835 // Insert the new ChildSaSession into processing child SA list.
1837 Ikev2ChildSaSessionInsert (&IkeSaSession
->ChildSaSessionList
, ChildSaSession
);
1838 return ChildSaSession
;
1842 Check if the SPD is related to the input Child SA Session.
1844 This function is the subfunction of Ikev1AssociateSpdEntry(). It is the call
1845 back function of IpSecVisitConfigData().
1848 @param[in] Type Type of the input Config Selector.
1849 @param[in] Selector Pointer to the Configure Selector to be checked.
1850 @param[in] Data Pointer to the Configure Selector's Data passed
1852 @param[in] SelectorSize The buffer size of Selector.
1853 @param[in] DataSize The buffer size of the Data.
1854 @param[in] Context The data passed from the caller. It is a Child
1855 SA Session in this context.
1857 @retval EFI_SUCCESS The SPD Selector is not related to the Child SA Session.
1858 @retval EFI_ABORTED The SPD Selector is related to the Child SA session and
1859 set the ChildSaSession->Spd to point to this SPD Selector.
1863 Ikev2MatchSpdEntry (
1864 IN EFI_IPSEC_CONFIG_DATA_TYPE Type
,
1865 IN EFI_IPSEC_CONFIG_SELECTOR
*Selector
,
1867 IN UINTN SelectorSize
,
1872 IKEV2_CHILD_SA_SESSION
*ChildSaSession
;
1873 EFI_IPSEC_SPD_SELECTOR
*SpdSelector
;
1874 EFI_IPSEC_SPD_DATA
*SpdData
;
1878 ASSERT (Type
== IPsecConfigDataTypeSpd
);
1879 SpdData
= (EFI_IPSEC_SPD_DATA
*) Data
;
1881 // Bypass all non-protect SPD entry first
1883 if (SpdData
->Action
!= EfiIPsecActionProtect
) {
1887 ChildSaSession
= (IKEV2_CHILD_SA_SESSION
*) Context
;
1888 IpVersion
= ChildSaSession
->SessionCommon
.UdpService
->IpVersion
;
1889 SpdSelector
= (EFI_IPSEC_SPD_SELECTOR
*) Selector
;
1892 if (SpdSelector
->NextLayerProtocol
== EFI_IP_PROTO_UDP
&&
1893 SpdSelector
->LocalPort
== IKE_DEFAULT_PORT
&&
1894 SpdSelector
->LocalPortRange
== 0 &&
1895 SpdSelector
->RemotePort
== IKE_DEFAULT_PORT
&&
1896 SpdSelector
->RemotePortRange
== 0
1899 // TODO: Skip IKE Policy here or set a SPD entry?
1904 if (SpdSelector
->NextLayerProtocol
!= EFI_IPSEC_ANY_PROTOCOL
&&
1905 SpdSelector
->NextLayerProtocol
!= ChildSaSession
->ProtoId
1910 if (SpdSelector
->LocalPort
!= EFI_IPSEC_ANY_PORT
&& SpdSelector
->LocalPort
!= ChildSaSession
->LocalPort
) {
1914 if (SpdSelector
->RemotePort
!= EFI_IPSEC_ANY_PORT
&& SpdSelector
->RemotePort
!= ChildSaSession
->RemotePort
) {
1918 IsMatch
= (BOOLEAN
) (IsMatch
&&
1919 IpSecMatchIpAddress (
1921 &ChildSaSession
->SessionCommon
.LocalPeerIp
,
1922 SpdSelector
->LocalAddress
,
1923 SpdSelector
->LocalAddressCount
1926 IsMatch
= (BOOLEAN
) (IsMatch
&&
1927 IpSecMatchIpAddress (
1929 &ChildSaSession
->SessionCommon
.RemotePeerIp
,
1930 SpdSelector
->RemoteAddress
,
1931 SpdSelector
->RemoteAddressCount
1935 ChildSaSession
->Spd
= IkeSearchSpdEntry (SpdSelector
);
1943 Check if the Algorithm ID is supported.
1945 @param[in] AlgorithmId The specified Algorithm ID.
1946 @param[in] Type The type used to indicate the Algorithm is for Encrypt or
1949 @retval TRUE If the Algorithm ID is supported.
1950 @retval FALSE If the Algorithm ID is not supported.
1955 IN UINT16 AlgorithmId
,
1961 case IKE_ENCRYPT_TYPE
:
1962 for (Index
= 0; Index
< IKEV2_SUPPORT_ENCRYPT_ALGORITHM_NUM
; Index
++) {
1963 if (mIkev2EncryptAlgorithmList
[Index
] == AlgorithmId
) {
1969 case IKE_AUTH_TYPE
:
1970 for (Index
= 0; Index
< IKEV2_SUPPORT_AUTH_ALGORITHM_NUM
; Index
++) {
1971 if (mIkev2AuthAlgorithmList
[Index
] == AlgorithmId
) {
1978 for (Index
= 0; Index
< IKEV2_SUPPORT_DH_ALGORITHM_NUM
; Index
++) {
1979 if (mIkev2DhGroupAlgorithmList
[Index
] == AlgorithmId
) {
1986 for (Index
= 0; Index
< IKEV2_SUPPORT_PRF_ALGORITHM_NUM
; Index
++) {
1987 if (mIkev2PrfAlgorithmList
[Index
] == AlgorithmId
) {
1996 Get the preferred algorithm types from ProposalData.
1998 @param[in] ProposalData Pointer to related IKEV2_PROPOSAL_DATA.
1999 @param[in, out] PreferEncryptAlgorithm Pointer to buffer which is used to store the
2000 preferred encrypt algorithm.
2001 Input value shall be initialized to zero that
2002 indicates to be parsed from ProposalData.
2003 Output of preferred encrypt algorithm.
2004 @param[in, out] PreferIntegrityAlgorithm Pointer to buffer which is used to store the
2005 preferred integrity algorithm.
2006 Input value shall be initialized to zero that
2007 indicates to be parsed from ProposalData.
2008 Output of preferred integrity algorithm.
2009 @param[in, out] PreferPrfAlgorithm Pointer to buffer which is used to store the
2010 preferred PRF algorithm.
2011 Input value shall be initialized to zero that
2012 indicates to be parsed from ProposalData.
2013 Output of preferred PRF algorithm. Only
2015 @param[in, out] PreferDhGroup Pointer to buffer which is used to store the
2017 Input value shall be initialized to zero that
2018 indicates to be parsed from ProposalData.
2019 Output of preferred DH group. Only for
2021 @param[out] PreferEncryptKeylength Pointer to buffer which is used to store the
2022 preferred encrypt key length in bytes.
2023 @param[out] IsSupportEsn Pointer to buffer which is used to store the
2024 value about the Extented Sequence Number is
2025 support or not. Only for Child SA.
2026 @param[in] IsChildSa If it is ture, the ProposalData is for IKE
2027 SA. Otherwise the proposalData is for Child SA.
2031 Ikev2ParseProposalData (
2032 IN IKEV2_PROPOSAL_DATA
*ProposalData
,
2033 IN OUT UINT16
*PreferEncryptAlgorithm
,
2034 IN OUT UINT16
*PreferIntegrityAlgorithm
,
2035 IN OUT UINT16
*PreferPrfAlgorithm
,
2036 IN OUT UINT16
*PreferDhGroup
,
2037 OUT UINTN
*PreferEncryptKeylength
,
2038 OUT BOOLEAN
*IsSupportEsn
,
2039 IN BOOLEAN IsChildSa
2042 IKEV2_TRANSFORM_DATA
*TransformData
;
2043 UINT8 TransformIndex
;
2046 // Check input parameters.
2048 if (ProposalData
== NULL
||
2049 PreferEncryptAlgorithm
== NULL
||
2050 PreferIntegrityAlgorithm
== NULL
||
2051 PreferEncryptKeylength
== NULL
2057 if (IsSupportEsn
== NULL
) {
2061 if (PreferPrfAlgorithm
== NULL
|| PreferDhGroup
== NULL
) {
2066 TransformData
= (IKEV2_TRANSFORM_DATA
*)(ProposalData
+ 1);
2067 for (TransformIndex
= 0; TransformIndex
< ProposalData
->NumTransforms
; TransformIndex
++) {
2068 switch (TransformData
->TransformType
) {
2070 // For IKE SA there are four algorithm types. Encryption Algorithm, Pseudo-random Function,
2071 // Integrity Algorithm, Diffie-Hellman Group. For Child SA, there are three algorithm types.
2072 // Encryption Algorithm, Integrity Algorithm, Extended Sequence Number.
2074 case IKEV2_TRANSFORM_TYPE_ENCR
:
2075 if (*PreferEncryptAlgorithm
== 0 && Ikev2IsSupportAlg (TransformData
->TransformId
, IKE_ENCRYPT_TYPE
)) {
2077 // Check the attribute value. According to RFC, only Keylength is support.
2079 if (TransformData
->Attribute
.AttrType
== IKEV2_ATTRIBUTE_TYPE_KEYLEN
) {
2081 // If the Keylength is not support, continue to check the next one.
2083 if (IpSecGetEncryptKeyLength ((UINT8
)TransformData
->TransformId
) != (UINTN
)(TransformData
->Attribute
.Attr
.AttrValue
>> 3)){
2086 *PreferEncryptKeylength
= TransformData
->Attribute
.Attr
.AttrValue
;
2089 *PreferEncryptAlgorithm
= TransformData
->TransformId
;
2093 case IKEV2_TRANSFORM_TYPE_PRF
:
2095 if (*PreferPrfAlgorithm
== 0 && Ikev2IsSupportAlg (TransformData
->TransformId
, IKE_PRF_TYPE
)) {
2096 *PreferPrfAlgorithm
= TransformData
->TransformId
;
2101 case IKEV2_TRANSFORM_TYPE_INTEG
:
2102 if (*PreferIntegrityAlgorithm
== 0 && Ikev2IsSupportAlg (TransformData
->TransformId
, IKE_AUTH_TYPE
)) {
2103 *PreferIntegrityAlgorithm
= TransformData
->TransformId
;
2107 case IKEV2_TRANSFORM_TYPE_DH
:
2109 if (*PreferDhGroup
== 0 && Ikev2IsSupportAlg (TransformData
->TransformId
, IKE_DH_TYPE
)) {
2110 *PreferDhGroup
= TransformData
->TransformId
;
2115 case IKEV2_TRANSFORM_TYPE_ESN
:
2117 if (TransformData
->TransformId
!= 0) {
2118 *IsSupportEsn
= TRUE
;
2126 TransformData
= (IKEV2_TRANSFORM_DATA
*)(TransformData
+ 1);
2131 Parse the received Initial Exchange Packet.
2133 This function parse the SA Payload and Key Payload to find out the cryptographic
2134 suite for the further IKE negotiation and fill it into the IKE SA Session's
2135 CommonSession->SaParams.
2137 @param[in, out] IkeSaSession Pointer to related IKEV2_SA_SESSION.
2138 @param[in] SaPayload The received packet.
2139 @param[in] Type The received packet IKE header flag.
2141 @retval TRUE If the SA proposal in Packet is acceptable.
2142 @retval FALSE If the SA proposal in Packet is not acceptable.
2146 Ikev2SaParseSaPayload (
2147 IN OUT IKEV2_SA_SESSION
*IkeSaSession
,
2148 IN IKE_PAYLOAD
*SaPayload
,
2152 IKEV2_PROPOSAL_DATA
*ProposalData
;
2153 UINT8 ProposalIndex
;
2154 UINT16 PreferEncryptAlgorithm
;
2155 UINT16 PreferIntegrityAlgorithm
;
2156 UINT16 PreferPrfAlgorithm
;
2157 UINT16 PreferDhGroup
;
2158 UINTN PreferEncryptKeylength
;
2159 UINT16 EncryptAlgorithm
;
2160 UINT16 IntegrityAlgorithm
;
2161 UINT16 PrfAlgorithm
;
2163 UINTN EncryptKeylength
;
2167 PreferPrfAlgorithm
= 0;
2168 PreferIntegrityAlgorithm
= 0;
2170 PreferEncryptAlgorithm
= 0;
2171 PreferEncryptKeylength
= 0;
2173 IntegrityAlgorithm
= 0;
2175 EncryptAlgorithm
= 0;
2176 EncryptKeylength
= 0;
2179 if (Type
== IKE_HEADER_FLAGS_INIT
) {
2180 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((IKEV2_SA_DATA
*)SaPayload
->PayloadBuf
+ 1);
2181 for (ProposalIndex
= 0; ProposalIndex
< ((IKEV2_SA_DATA
*)SaPayload
->PayloadBuf
)->NumProposals
; ProposalIndex
++) {
2183 // Iterate each proposal to find the perfered one.
2185 if (ProposalData
->ProtocolId
== IPSEC_PROTO_ISAKMP
&& ProposalData
->NumTransforms
>= 4) {
2187 // Get the preferred algorithms.
2189 Ikev2ParseProposalData (
2191 &PreferEncryptAlgorithm
,
2192 &PreferIntegrityAlgorithm
,
2193 &PreferPrfAlgorithm
,
2195 &PreferEncryptKeylength
,
2200 if (PreferEncryptAlgorithm
!= 0 &&
2201 PreferIntegrityAlgorithm
!= 0 &&
2202 PreferPrfAlgorithm
!= 0 &&
2206 // Find the matched one.
2208 IkeSaSession
->SessionCommon
.SaParams
= AllocateZeroPool (sizeof (IKEV2_SA_PARAMS
));
2209 if (IkeSaSession
->SessionCommon
.SaParams
== NULL
) {
2213 IkeSaSession
->SessionCommon
.SaParams
->EncAlgId
= PreferEncryptAlgorithm
;
2214 IkeSaSession
->SessionCommon
.SaParams
->EnckeyLen
= PreferEncryptKeylength
;
2215 IkeSaSession
->SessionCommon
.SaParams
->DhGroup
= PreferDhGroup
;
2216 IkeSaSession
->SessionCommon
.SaParams
->Prf
= PreferPrfAlgorithm
;
2217 IkeSaSession
->SessionCommon
.SaParams
->IntegAlgId
= PreferIntegrityAlgorithm
;
2218 IkeSaSession
->SessionCommon
.PreferDhGroup
= PreferDhGroup
;
2221 // Save the matched one in IKEV2_SA_DATA for furthure calculation.
2223 SaDataSize
= sizeof (IKEV2_SA_DATA
) +
2224 sizeof (IKEV2_PROPOSAL_DATA
) +
2225 sizeof (IKEV2_TRANSFORM_DATA
) * 4;
2226 IkeSaSession
->SaData
= AllocateZeroPool (SaDataSize
);
2227 if (IkeSaSession
->SaData
== NULL
) {
2228 FreePool (IkeSaSession
->SessionCommon
.SaParams
);
2232 IkeSaSession
->SaData
->NumProposals
= 1;
2235 // BUGBUG: Suppose the matched proposal only has 4 transforms. If
2236 // The matched Proposal has more than 4 transforms means it contains
2237 // one than one transform with same type.
2240 (IKEV2_PROPOSAL_DATA
*) (IkeSaSession
->SaData
+ 1),
2242 SaDataSize
- sizeof (IKEV2_SA_DATA
)
2245 ((IKEV2_PROPOSAL_DATA
*) (IkeSaSession
->SaData
+ 1))->ProposalIndex
= 1;
2249 PreferEncryptAlgorithm
= 0;
2250 PreferIntegrityAlgorithm
= 0;
2251 PreferPrfAlgorithm
= 0;
2253 PreferEncryptKeylength
= 0;
2257 // Point to next Proposal.
2259 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((UINT8
*)(ProposalData
+ 1) +
2260 ProposalData
->NumTransforms
* sizeof (IKEV2_TRANSFORM_DATA
));
2262 } else if (Type
== IKE_HEADER_FLAGS_RESPOND
) {
2264 // First check the SA proposal's ProtoctolID and Transform Numbers. Since it is
2265 // the responded SA proposal, suppose it only has one proposal and the transform Numbers
2268 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((IKEV2_SA_DATA
*) SaPayload
->PayloadBuf
+ 1);
2269 if (ProposalData
->ProtocolId
!= IPSEC_PROTO_ISAKMP
|| ProposalData
->NumTransforms
!= 4) {
2273 // Get the preferred algorithms.
2275 Ikev2ParseProposalData (
2277 &PreferEncryptAlgorithm
,
2278 &PreferIntegrityAlgorithm
,
2279 &PreferPrfAlgorithm
,
2281 &PreferEncryptKeylength
,
2286 // Check if the Sa proposal data from received packet is in the IkeSaSession->SaData.
2288 ProposalData
= (IKEV2_PROPOSAL_DATA
*) (IkeSaSession
->SaData
+ 1);
2290 for (ProposalIndex
= 0; ProposalIndex
< IkeSaSession
->SaData
->NumProposals
&& (!IsMatch
); ProposalIndex
++) {
2291 Ikev2ParseProposalData (
2294 &IntegrityAlgorithm
,
2301 if (EncryptAlgorithm
== PreferEncryptAlgorithm
&&
2302 EncryptKeylength
== PreferEncryptKeylength
&&
2303 IntegrityAlgorithm
== PreferIntegrityAlgorithm
&&
2304 PrfAlgorithm
== PreferPrfAlgorithm
&&
2305 DhGroup
== PreferDhGroup
2309 EncryptAlgorithm
= 0;
2310 IntegrityAlgorithm
= 0;
2313 EncryptKeylength
= 0;
2316 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((UINT8
*)(ProposalData
+ 1) +
2317 ProposalData
->NumTransforms
* sizeof (IKEV2_TRANSFORM_DATA
));
2321 IkeSaSession
->SessionCommon
.SaParams
= AllocateZeroPool (sizeof (IKEV2_SA_PARAMS
));
2322 if (IkeSaSession
->SessionCommon
.SaParams
== NULL
) {
2326 IkeSaSession
->SessionCommon
.SaParams
->EncAlgId
= PreferEncryptAlgorithm
;
2327 IkeSaSession
->SessionCommon
.SaParams
->EnckeyLen
= PreferEncryptKeylength
;
2328 IkeSaSession
->SessionCommon
.SaParams
->DhGroup
= PreferDhGroup
;
2329 IkeSaSession
->SessionCommon
.SaParams
->Prf
= PreferPrfAlgorithm
;
2330 IkeSaSession
->SessionCommon
.SaParams
->IntegAlgId
= PreferIntegrityAlgorithm
;
2331 IkeSaSession
->SessionCommon
.PreferDhGroup
= PreferDhGroup
;
2341 Parse the received Authentication Exchange Packet.
2343 This function parse the SA Payload and Key Payload to find out the cryptographic
2344 suite for the ESP and fill it into the Child SA Session's CommonSession->SaParams.
2346 @param[in, out] ChildSaSession Pointer to IKEV2_CHILD_SA_SESSION related to
2347 this Authentication Exchange.
2348 @param[in] SaPayload The received packet.
2349 @param[in] Type The IKE header's flag of received packet .
2351 @retval TRUE If the SA proposal in Packet is acceptable.
2352 @retval FALSE If the SA proposal in Packet is not acceptable.
2356 Ikev2ChildSaParseSaPayload (
2357 IN OUT IKEV2_CHILD_SA_SESSION
*ChildSaSession
,
2358 IN IKE_PAYLOAD
*SaPayload
,
2362 IKEV2_PROPOSAL_DATA
*ProposalData
;
2363 UINT8 ProposalIndex
;
2364 UINT16 PreferEncryptAlgorithm
;
2365 UINT16 PreferIntegrityAlgorithm
;
2366 UINTN PreferEncryptKeylength
;
2367 BOOLEAN PreferIsSupportEsn
;
2368 UINT16 EncryptAlgorithm
;
2369 UINT16 IntegrityAlgorithm
;
2370 UINTN EncryptKeylength
;
2371 BOOLEAN IsSupportEsn
;
2376 PreferIntegrityAlgorithm
= 0;
2377 PreferEncryptAlgorithm
= 0;
2378 PreferEncryptKeylength
= 0;
2379 IntegrityAlgorithm
= 0;
2380 EncryptAlgorithm
= 0;
2381 EncryptKeylength
= 0;
2383 IsSupportEsn
= FALSE
;
2384 PreferIsSupportEsn
= FALSE
;
2386 if (Type
== IKE_HEADER_FLAGS_INIT
) {
2387 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((IKEV2_SA_DATA
*) SaPayload
->PayloadBuf
+ 1);
2388 for (ProposalIndex
= 0; ProposalIndex
< ((IKEV2_SA_DATA
*) SaPayload
->PayloadBuf
)->NumProposals
; ProposalIndex
++) {
2390 // Iterate each proposal to find the preferred one.
2392 if (ProposalData
->ProtocolId
== IPSEC_PROTO_IPSEC_ESP
&& ProposalData
->NumTransforms
>= 3) {
2394 // Get the preferred algorithm.
2396 Ikev2ParseProposalData (
2398 &PreferEncryptAlgorithm
,
2399 &PreferIntegrityAlgorithm
,
2402 &PreferEncryptKeylength
,
2407 // Don't support the ESN now.
2409 if (PreferEncryptAlgorithm
!= 0 &&
2410 PreferIntegrityAlgorithm
!= 0 &&
2414 // Find the matched one.
2416 ChildSaSession
->SessionCommon
.SaParams
= AllocateZeroPool (sizeof (IKEV2_SA_PARAMS
));
2417 if (ChildSaSession
->SessionCommon
.SaParams
== NULL
) {
2421 ChildSaSession
->SessionCommon
.SaParams
->EncAlgId
= PreferEncryptAlgorithm
;
2422 ChildSaSession
->SessionCommon
.SaParams
->EnckeyLen
= PreferEncryptKeylength
;
2423 ChildSaSession
->SessionCommon
.SaParams
->IntegAlgId
= PreferIntegrityAlgorithm
;
2424 CopyMem (&ChildSaSession
->RemotePeerSpi
, ProposalData
->Spi
, sizeof (ChildSaSession
->RemotePeerSpi
));
2427 // Save the matched one in IKEV2_SA_DATA for furthure calculation.
2429 SaDataSize
= sizeof (IKEV2_SA_DATA
) +
2430 sizeof (IKEV2_PROPOSAL_DATA
) +
2431 sizeof (IKEV2_TRANSFORM_DATA
) * 4;
2433 ChildSaSession
->SaData
= AllocateZeroPool (SaDataSize
);
2434 if (ChildSaSession
->SaData
== NULL
) {
2435 FreePool (ChildSaSession
->SessionCommon
.SaParams
);
2439 ChildSaSession
->SaData
->NumProposals
= 1;
2442 // BUGBUG: Suppose there are 4 transforms in the matched proposal. If
2443 // the matched Proposal has more than 4 transforms that means there
2444 // are more than one transform with same type.
2447 (IKEV2_PROPOSAL_DATA
*) (ChildSaSession
->SaData
+ 1),
2449 SaDataSize
- sizeof (IKEV2_SA_DATA
)
2452 ((IKEV2_PROPOSAL_DATA
*) (ChildSaSession
->SaData
+ 1))->ProposalIndex
= 1;
2454 ((IKEV2_PROPOSAL_DATA
*) (ChildSaSession
->SaData
+ 1))->Spi
= AllocateCopyPool (
2455 sizeof (ChildSaSession
->LocalPeerSpi
),
2456 &ChildSaSession
->LocalPeerSpi
2458 if (((IKEV2_PROPOSAL_DATA
*) (ChildSaSession
->SaData
+ 1))->Spi
== NULL
) {
2459 FreePool (ChildSaSession
->SessionCommon
.SaParams
);
2461 FreePool (ChildSaSession
->SaData
);
2469 PreferEncryptAlgorithm
= 0;
2470 PreferIntegrityAlgorithm
= 0;
2471 IsSupportEsn
= TRUE
;
2475 // Point to next Proposal
2477 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((UINT8
*)(ProposalData
+ 1) +
2478 ProposalData
->NumTransforms
* sizeof (IKEV2_TRANSFORM_DATA
));
2480 } else if (Type
== IKE_HEADER_FLAGS_RESPOND
) {
2482 // First check the SA proposal's ProtoctolID and Transform Numbers. Since it is
2483 // the responded SA proposal, suppose it only has one proposal and the transform Numbers
2486 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((IKEV2_SA_DATA
*)SaPayload
->PayloadBuf
+ 1);
2487 if (ProposalData
->ProtocolId
!= IPSEC_PROTO_IPSEC_ESP
|| ProposalData
->NumTransforms
!= 3) {
2491 // Get the preferred algorithms.
2493 Ikev2ParseProposalData (
2495 &PreferEncryptAlgorithm
,
2496 &PreferIntegrityAlgorithm
,
2499 &PreferEncryptKeylength
,
2500 &PreferIsSupportEsn
,
2504 ProposalData
= (IKEV2_PROPOSAL_DATA
*) (ChildSaSession
->SaData
+ 1);
2506 for (ProposalIndex
= 0; ProposalIndex
< ChildSaSession
->SaData
->NumProposals
&& (!IsMatch
); ProposalIndex
++) {
2507 Ikev2ParseProposalData (
2510 &IntegrityAlgorithm
,
2517 if (EncryptAlgorithm
== PreferEncryptAlgorithm
&&
2518 EncryptKeylength
== PreferEncryptKeylength
&&
2519 IntegrityAlgorithm
== PreferIntegrityAlgorithm
&&
2520 IsSupportEsn
== PreferIsSupportEsn
2524 IntegrityAlgorithm
= 0;
2525 EncryptAlgorithm
= 0;
2526 EncryptKeylength
= 0;
2527 IsSupportEsn
= FALSE
;
2529 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((UINT8
*)(ProposalData
+ 1) +
2530 ProposalData
->NumTransforms
* sizeof (IKEV2_TRANSFORM_DATA
));
2533 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((IKEV2_SA_DATA
*)SaPayload
->PayloadBuf
+ 1);
2535 ChildSaSession
->SessionCommon
.SaParams
= AllocateZeroPool (sizeof (IKEV2_SA_PARAMS
));
2536 if (ChildSaSession
->SessionCommon
.SaParams
== NULL
) {
2540 ChildSaSession
->SessionCommon
.SaParams
->EncAlgId
= PreferEncryptAlgorithm
;
2541 ChildSaSession
->SessionCommon
.SaParams
->EnckeyLen
= PreferEncryptKeylength
;
2542 ChildSaSession
->SessionCommon
.SaParams
->IntegAlgId
= PreferIntegrityAlgorithm
;
2543 CopyMem (&ChildSaSession
->RemotePeerSpi
, ProposalData
->Spi
, sizeof (ChildSaSession
->RemotePeerSpi
));
2552 Generate Key buffer from fragments.
2554 If the digest length of specified HashAlgId is larger than or equal with the
2555 required output key length, derive the key directly. Otherwise, Key Material
2556 needs to be PRF-based concatenation according to 2.13 of RFC 4306:
2557 prf+ (K,S) = T1 | T2 | T3 | T4 | ..., T1 = prf (K, S | 0x01),
2558 T2 = prf (K, T1 | S | 0x02), T3 = prf (K, T2 | S | 0x03),T4 = prf (K, T3 | S | 0x04)
2559 then derive the key from this key material.
2561 @param[in] HashAlgId The Hash Algorithm ID used to generate key.
2562 @param[in] HashKey Pointer to a key buffer which contains hash key.
2563 @param[in] HashKeyLength The length of HashKey in bytes.
2564 @param[in, out] OutputKey Pointer to buffer which is used to receive the
2566 @param[in] OutputKeyLength The length of OutPutKey buffer.
2567 @param[in] Fragments Pointer to the data to be used to generate key.
2568 @param[in] NumFragments The numbers of the Fragement.
2570 @retval EFI_SUCCESS The operation complete successfully.
2571 @retval EFI_INVALID_PARAMETER If NumFragments is zero.
2572 If the authentication algorithm given by HashAlgId
2574 @retval EFI_OUT_OF_RESOURCES If the required resource can't be allocated.
2575 @retval Others The operation is failed.
2579 Ikev2SaGenerateKey (
2582 IN UINTN HashKeyLength
,
2583 IN OUT UINT8
*OutputKey
,
2584 IN UINTN OutputKeyLength
,
2585 IN PRF_DATA_FRAGMENT
*Fragments
,
2586 IN UINTN NumFragments
2590 PRF_DATA_FRAGMENT LocalFragments
[3];
2595 UINTN AuthKeyLength
;
2596 UINTN FragmentsSize
;
2599 Status
= EFI_SUCCESS
;
2601 if (NumFragments
== 0) {
2602 return EFI_INVALID_PARAMETER
;
2605 LocalFragments
[0].Data
= NULL
;
2606 LocalFragments
[1].Data
= NULL
;
2607 LocalFragments
[2].Data
= NULL
;
2609 AuthKeyLength
= IpSecGetHmacDigestLength (HashAlgId
);
2610 if (AuthKeyLength
== 0) {
2611 return EFI_INVALID_PARAMETER
;
2614 DigestSize
= AuthKeyLength
;
2615 Digest
= AllocateZeroPool (AuthKeyLength
);
2617 if (Digest
== NULL
) {
2618 return EFI_OUT_OF_RESOURCES
;
2621 // If the required output key length is less than the digest size,
2622 // copy the digest into OutputKey.
2624 if (OutputKeyLength
<= DigestSize
) {
2625 Status
= IpSecCryptoIoHmac (
2629 (HASH_DATA_FRAGMENT
*) Fragments
,
2634 if (EFI_ERROR (Status
)) {
2638 CopyMem (OutputKey
, Digest
, OutputKeyLength
);
2643 //Otherwise, Key Material need to be PRF-based concatenation according to 2.13
2644 //of RFC 4306: prf+ (K,S) = T1 | T2 | T3 | T4 | ..., T1 = prf (K, S | 0x01),
2645 //T2 = prf (K, T1 | S | 0x02), T3 = prf (K, T2 | S | 0x03),T4 = prf (K, T3 | S | 0x04)
2646 //then derive the key from this key material.
2649 for (Index
= 0; Index
< NumFragments
; Index
++) {
2650 FragmentsSize
= FragmentsSize
+ Fragments
[Index
].DataSize
;
2653 LocalFragments
[1].Data
= AllocateZeroPool (FragmentsSize
);
2654 if (LocalFragments
[1].Data
== NULL
) {
2655 Status
= EFI_OUT_OF_RESOURCES
;
2659 LocalFragments
[1].DataSize
= FragmentsSize
;
2662 // Copy all input fragments into LocalFragments[1];
2665 for (Index
= 0; Index
< NumFragments
; Index
++) {
2667 LocalFragments
[1].Data
+ FragmentsSize
,
2668 Fragments
[Index
].Data
,
2669 Fragments
[Index
].DataSize
2671 FragmentsSize
= FragmentsSize
+ Fragments
[Index
].DataSize
;
2675 // Prepare 0x01 as the first tail data.
2678 LocalFragments
[2].Data
= &TailData
;
2679 LocalFragments
[2].DataSize
= sizeof (TailData
);
2681 // Allocate buffer for the first fragment
2683 LocalFragments
[0].Data
= AllocateZeroPool (AuthKeyLength
);
2684 if (LocalFragments
[0].Data
== NULL
) {
2685 Status
= EFI_OUT_OF_RESOURCES
;
2689 LocalFragments
[0].DataSize
= AuthKeyLength
;
2691 Round
= (OutputKeyLength
- 1) / AuthKeyLength
+ 1;
2692 for (Index
= 0; Index
< Round
; Index
++) {
2693 Status
= IpSecCryptoIoHmac (
2697 (HASH_DATA_FRAGMENT
*)(Index
== 0 ? &LocalFragments
[1] : LocalFragments
),
2702 if (EFI_ERROR(Status
)) {
2706 LocalFragments
[0].Data
,
2710 if (OutputKeyLength
> DigestSize
* (Index
+ 1)) {
2712 OutputKey
+ Index
* DigestSize
,
2716 LocalFragments
[0].DataSize
= DigestSize
;
2723 OutputKey
+ Index
* DigestSize
,
2725 OutputKeyLength
- Index
* DigestSize
2732 // Only First and second Framgement Data need to be freed.
2734 for (Index
= 0 ; Index
< 2; Index
++) {
2735 if (LocalFragments
[Index
].Data
!= NULL
) {
2736 FreePool (LocalFragments
[Index
].Data
);
2739 if (Digest
!= NULL
) {