2 The Common operations used by IKE Exchange Process.
4 Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "IpSecDebug.h"
18 #include "IkeService.h"
19 #include "IpSecConfigImpl.h"
21 UINT16 mIkev2EncryptAlgorithmList
[IKEV2_SUPPORT_ENCRYPT_ALGORITHM_NUM
] = {
22 IKEV2_TRANSFORM_ID_ENCR_3DES
,
23 IKEV2_TRANSFORM_ID_ENCR_AES_CBC
,
26 UINT16 mIkev2PrfAlgorithmList
[IKEV2_SUPPORT_PRF_ALGORITHM_NUM
] = {
27 IKEV2_TRANSFORM_ID_PRF_HMAC_SHA1
,
30 UINT16 mIkev2DhGroupAlgorithmList
[IKEV2_SUPPORT_DH_ALGORITHM_NUM
] = {
31 IKEV2_TRANSFORM_ID_DH_1024MODP
,
32 IKEV2_TRANSFORM_ID_DH_2048MODP
,
35 UINT16 mIkev2AuthAlgorithmList
[IKEV2_SUPPORT_AUTH_ALGORITHM_NUM
] = {
36 IKEV2_TRANSFORM_ID_AUTH_HMAC_SHA1_96
,
40 Allocate buffer for IKEV2_SA_SESSION and initialize it.
42 @param[in] Private Pointer to IPSEC_PRIVATE_DATA.
43 @param[in] UdpService Pointer to IKE_UDP_SERVICE related to this IKE SA Session.
45 @return Pointer to IKEV2_SA_SESSION or NULL.
50 IN IPSEC_PRIVATE_DATA
*Private
,
51 IN IKE_UDP_SERVICE
*UdpService
55 IKEV2_SESSION_COMMON
*SessionCommon
;
56 IKEV2_SA_SESSION
*IkeSaSession
;
58 IkeSaSession
= AllocateZeroPool (sizeof (IKEV2_SA_SESSION
));
59 ASSERT (IkeSaSession
!= NULL
);
62 // Initialize the fields of IkeSaSession and its SessionCommon.
64 IkeSaSession
->NCookie
= NULL
;
65 IkeSaSession
->Signature
= IKEV2_SA_SESSION_SIGNATURE
;
66 IkeSaSession
->InitiatorCookie
= IkeGenerateCookie ();
67 IkeSaSession
->ResponderCookie
= 0;
69 // BUGBUG: Message ID starts from 2 is to match the OpenSwan requirement, but it
70 // might not match the IPv6 Logo. In its test specification, it mentions that
71 // the Message ID should start from zero after the IKE_SA_INIT exchange.
73 IkeSaSession
->MessageId
= 2;
74 SessionCommon
= &IkeSaSession
->SessionCommon
;
75 SessionCommon
->UdpService
= UdpService
;
76 SessionCommon
->Private
= Private
;
77 SessionCommon
->IkeSessionType
= IkeSessionTypeIkeSa
;
78 SessionCommon
->IkeVer
= 2;
79 SessionCommon
->AfterEncodePayload
= NULL
;
80 SessionCommon
->BeforeDecodePayload
= NULL
;
83 // Create a resend notfiy event for retry.
85 Status
= gBS
->CreateEvent (
86 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
90 &SessionCommon
->TimeoutEvent
93 if (EFI_ERROR (Status
)) {
94 FreePool (IkeSaSession
);
99 // Initialize the lists in IkeSaSession.
101 InitializeListHead (&IkeSaSession
->ChildSaSessionList
);
102 InitializeListHead (&IkeSaSession
->ChildSaEstablishSessionList
);
103 InitializeListHead (&IkeSaSession
->InfoMIDList
);
104 InitializeListHead (&IkeSaSession
->DeleteSaList
);
110 Register the established IKEv2 SA into Private->Ikev2EstablishedList. If there is
111 IKEV2_SA_SESSION with same remote peer IP, remove the old one then register the
114 @param[in] IkeSaSession Pointer to IKEV2_SA_SESSION to be registered.
115 @param[in] Private Pointer to IPSEC_PRAVATE_DATA.
120 IN IKEV2_SA_SESSION
*IkeSaSession
,
121 IN IPSEC_PRIVATE_DATA
*Private
124 IKEV2_SESSION_COMMON
*SessionCommon
;
125 IKEV2_SA_SESSION
*OldIkeSaSession
;
130 // Keep IKE SA exclusive to remote ip address.
132 SessionCommon
= &IkeSaSession
->SessionCommon
;
133 OldIkeSaSession
= Ikev2SaSessionRemove (&Private
->Ikev2EstablishedList
, &SessionCommon
->RemotePeerIp
);
134 if (OldIkeSaSession
!= NULL
) {
136 // TODO: It should delete all child SAs if rekey the IKE SA.
138 Ikev2SaSessionFree (OldIkeSaSession
);
142 // Cleanup the fields of SessionCommon for processing.
144 Ikev2SessionCommonRefresh (SessionCommon
);
147 // Insert the ready IKE SA session into established list.
149 Ikev2SaSessionInsert (&Private
->Ikev2EstablishedList
, IkeSaSession
, &SessionCommon
->RemotePeerIp
);
152 // Create a notfiy event for the IKE SA life time counting.
154 Status
= gBS
->CreateEvent (
155 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
159 &SessionCommon
->TimeoutEvent
161 if (EFI_ERROR(Status
)){
163 // If TimerEvent creation failed, the SA will be alive untill user disable it or
164 // receiving a Delete Payload from peer.
170 // Start to count the lifetime of the IKE SA.
172 if (IkeSaSession
->Spd
->Data
->ProcessingPolicy
->SaLifetime
.HardLifetime
== 0) {
173 Lifetime
= IKE_SA_DEFAULT_LIFETIME
;
175 Lifetime
= IkeSaSession
->Spd
->Data
->ProcessingPolicy
->SaLifetime
.HardLifetime
;
178 Status
= gBS
->SetTimer (
179 SessionCommon
->TimeoutEvent
,
181 MultU64x32(Lifetime
, 10000000) // ms->100ns
183 if (EFI_ERROR(Status
)){
185 // If SetTimer failed, the SA will be alive untill user disable it or
186 // receiving a Delete Payload from peer.
193 "\n------IkeSa established and start to count down %d seconds lifetime\n",
201 Find a IKEV2_SA_SESSION by the remote peer IP.
203 @param[in] SaSessionList SaSession List to be searched.
204 @param[in] RemotePeerIp Pointer to specified IP address.
206 @return Pointer to IKEV2_SA_SESSION if find one or NULL.
210 Ikev2SaSessionLookup (
211 IN LIST_ENTRY
*SaSessionList
,
212 IN EFI_IP_ADDRESS
*RemotePeerIp
216 IKEV2_SA_SESSION
*IkeSaSession
;
218 NET_LIST_FOR_EACH (Entry
, SaSessionList
) {
219 IkeSaSession
= IKEV2_SA_SESSION_BY_SESSION (Entry
);
222 &IkeSaSession
->SessionCommon
.RemotePeerIp
,
224 sizeof (EFI_IP_ADDRESS
)
235 Insert a IKE_SA_SESSION into IkeSaSession list. The IkeSaSession list is either
236 Private->Ikev2SaSession list or Private->Ikev2EstablishedList list.
238 @param[in] SaSessionList Pointer to list to be inserted into.
239 @param[in] IkeSaSession Pointer to IKEV2_SA_SESSION to be inserted.
240 @param[in] RemotePeerIp Pointer to EFI_IP_ADDRESSS to indicate the
241 unique IKEV2_SA_SESSION.
245 Ikev2SaSessionInsert (
246 IN LIST_ENTRY
*SaSessionList
,
247 IN IKEV2_SA_SESSION
*IkeSaSession
,
248 IN EFI_IP_ADDRESS
*RemotePeerIp
251 Ikev2SaSessionRemove (SaSessionList
, RemotePeerIp
);
252 InsertTailList (SaSessionList
, &IkeSaSession
->BySessionTable
);
256 Remove the SA Session by Remote Peer IP.
258 @param[in] SaSessionList Pointer to list to be searched.
259 @param[in] RemotePeerIp Pointer to EFI_IP_ADDRESS to use for SA Session search.
261 @retval Pointer to IKEV2_SA_SESSION with the specified remote IP address or NULL.
265 Ikev2SaSessionRemove (
266 IN LIST_ENTRY
*SaSessionList
,
267 IN EFI_IP_ADDRESS
*RemotePeerIp
271 IKEV2_SA_SESSION
*IkeSaSession
;
273 NET_LIST_FOR_EACH (Entry
, SaSessionList
) {
274 IkeSaSession
= IKEV2_SA_SESSION_BY_SESSION (Entry
);
277 &IkeSaSession
->SessionCommon
.RemotePeerIp
,
279 sizeof (EFI_IP_ADDRESS
)
282 RemoveEntryList (Entry
);
291 Marking a SA session as on deleting.
293 @param[in] IkeSaSession Pointer to IKEV2_SA_SESSION.
295 @retval EFI_SUCCESS Find the related SA session and marked it.
299 Ikev2SaSessionOnDeleting (
300 IN IKEV2_SA_SESSION
*IkeSaSession
307 Free specified Seession Common. The session common would belong to a IKE SA or
310 @param[in] SessionCommon Pointer to a Session Common.
314 Ikev2SaSessionCommonFree (
315 IN IKEV2_SESSION_COMMON
*SessionCommon
319 ASSERT (SessionCommon
!= NULL
);
321 if (SessionCommon
->LastSentPacket
!= NULL
) {
322 IkePacketFree (SessionCommon
->LastSentPacket
);
325 if (SessionCommon
->SaParams
!= NULL
) {
326 FreePool (SessionCommon
->SaParams
);
328 if (SessionCommon
->TimeoutEvent
!= NULL
) {
329 gBS
->CloseEvent (SessionCommon
->TimeoutEvent
);
334 After IKE/Child SA is estiblished, close the time event and free sent packet.
336 @param[in] SessionCommon Pointer to a Session Common.
340 Ikev2SessionCommonRefresh (
341 IN IKEV2_SESSION_COMMON
*SessionCommon
344 ASSERT (SessionCommon
!= NULL
);
346 gBS
->CloseEvent (SessionCommon
->TimeoutEvent
);
347 SessionCommon
->TimeoutEvent
= NULL
;
348 SessionCommon
->TimeoutInterval
= 0;
349 SessionCommon
->RetryCount
= 0;
350 if (SessionCommon
->LastSentPacket
!= NULL
) {
351 IkePacketFree (SessionCommon
->LastSentPacket
);
352 SessionCommon
->LastSentPacket
= NULL
;
358 Free specified IKEV2 SA Session.
360 @param[in] IkeSaSession Pointer to IKEV2_SA_SESSION to be freed.
365 IN IKEV2_SA_SESSION
*IkeSaSession
368 IKEV2_SESSION_KEYS
*IkeKeys
;
370 IKEV2_CHILD_SA_SESSION
*ChildSa
;
371 IKEV2_DH_BUFFER
*DhBuffer
;
373 ASSERT (IkeSaSession
!= NULL
);
376 // Delete Common Session
378 Ikev2SaSessionCommonFree (&IkeSaSession
->SessionCommon
);
381 // Delete ChildSaEstablish List and SAD
383 for (Entry
= IkeSaSession
->ChildSaEstablishSessionList
.ForwardLink
;
384 Entry
!= &IkeSaSession
->ChildSaEstablishSessionList
;
387 ChildSa
= IKEV2_CHILD_SA_SESSION_BY_IKE_SA (Entry
);
388 Entry
= Entry
->ForwardLink
;
389 Ikev2ChildSaSilentDelete (ChildSa
->IkeSaSession
, ChildSa
->LocalPeerSpi
);
394 // Delete ChildSaSessionList
396 for ( Entry
= IkeSaSession
->ChildSaSessionList
.ForwardLink
;
397 Entry
!= &IkeSaSession
->ChildSaSessionList
;
399 ChildSa
= IKEV2_CHILD_SA_SESSION_BY_IKE_SA (Entry
);
400 Entry
= Entry
->ForwardLink
;
401 RemoveEntryList (Entry
->BackLink
);
402 Ikev2ChildSaSessionFree (ChildSa
);
406 // Delete DhBuffer and Keys
408 if (IkeSaSession
->IkeKeys
!= NULL
) {
409 IkeKeys
= IkeSaSession
->IkeKeys
;
410 DhBuffer
= IkeKeys
->DhBuffer
;
415 Ikev2DhBufferFree (DhBuffer
);
420 if (IkeKeys
->SkAiKey
!= NULL
) {
421 FreePool (IkeKeys
->SkAiKey
);
423 if (IkeKeys
->SkArKey
!= NULL
) {
424 FreePool (IkeKeys
->SkArKey
);
426 if (IkeKeys
->SkdKey
!= NULL
) {
427 FreePool (IkeKeys
->SkdKey
);
429 if (IkeKeys
->SkEiKey
!= NULL
) {
430 FreePool (IkeKeys
->SkEiKey
);
432 if (IkeKeys
->SkErKey
!= NULL
) {
433 FreePool (IkeKeys
->SkErKey
);
435 if (IkeKeys
->SkPiKey
!= NULL
) {
436 FreePool (IkeKeys
->SkPiKey
);
438 if (IkeKeys
->SkPrKey
!= NULL
) {
439 FreePool (IkeKeys
->SkPrKey
);
444 if (IkeSaSession
->SaData
!= NULL
) {
445 FreePool (IkeSaSession
->SaData
);
448 if (IkeSaSession
->NiBlock
!= NULL
) {
449 FreePool (IkeSaSession
->NiBlock
);
452 if (IkeSaSession
->NrBlock
!= NULL
) {
453 FreePool (IkeSaSession
->NrBlock
);
456 if (IkeSaSession
->NCookie
!= NULL
) {
457 FreePool (IkeSaSession
->NCookie
);
460 if (IkeSaSession
->InitPacket
!= NULL
) {
461 FreePool (IkeSaSession
->InitPacket
);
464 if (IkeSaSession
->RespPacket
!= NULL
) {
465 FreePool (IkeSaSession
->RespPacket
);
468 FreePool (IkeSaSession
);
474 Increase the MessageID in IkeSaSession.
476 @param[in] IkeSaSession Pointer to a specified IKEV2_SA_SESSION.
480 Ikev2SaSessionIncreaseMessageId (
481 IN IKEV2_SA_SESSION
*IkeSaSession
484 if (IkeSaSession
->MessageId
< 0xffffffff) {
485 IkeSaSession
->MessageId
++;
488 // TODO: Trigger Rekey process.
494 Allocate memory for IKEV2 Child SA Session.
496 @param[in] UdpService Pointer to IKE_UDP_SERVICE.
497 @param[in] IkeSaSession Pointer to IKEV2_SA_SESSION related to this Child SA
500 @retval Pointer of a new created IKEV2 Child SA Session or NULL.
503 IKEV2_CHILD_SA_SESSION
*
504 Ikev2ChildSaSessionAlloc (
505 IN IKE_UDP_SERVICE
*UdpService
,
506 IN IKEV2_SA_SESSION
*IkeSaSession
510 IKEV2_CHILD_SA_SESSION
*ChildSaSession
;
511 IKEV2_SESSION_COMMON
*ChildSaCommon
;
512 IKEV2_SESSION_COMMON
*SaCommon
;
514 ChildSaSession
= AllocateZeroPool (sizeof (IKEV2_CHILD_SA_SESSION
));
515 if (ChildSaSession
== NULL
) {
520 // Initialize the fields of ChildSaSession and its SessionCommon.
522 ChildSaSession
->Signature
= IKEV2_CHILD_SA_SESSION_SIGNATURE
;
523 ChildSaSession
->IkeSaSession
= IkeSaSession
;
524 ChildSaSession
->MessageId
= IkeSaSession
->MessageId
;
525 ChildSaSession
->LocalPeerSpi
= IkeGenerateSpi ();
526 ChildSaCommon
= &ChildSaSession
->SessionCommon
;
527 ChildSaCommon
->UdpService
= UdpService
;
528 ChildSaCommon
->Private
= IkeSaSession
->SessionCommon
.Private
;
529 ChildSaCommon
->IkeSessionType
= IkeSessionTypeChildSa
;
530 ChildSaCommon
->IkeVer
= 2;
531 ChildSaCommon
->AfterEncodePayload
= Ikev2ChildSaAfterEncodePayload
;
532 ChildSaCommon
->BeforeDecodePayload
= Ikev2ChildSaBeforeDecodePayload
;
533 SaCommon
= &ChildSaSession
->IkeSaSession
->SessionCommon
;
536 // Create a resend notfiy event for retry.
538 Status
= gBS
->CreateEvent (
539 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
543 &ChildSaCommon
->TimeoutEvent
545 if (EFI_ERROR (Status
)) {
546 FreePool (ChildSaSession
);
550 CopyMem (&ChildSaCommon
->LocalPeerIp
, &SaCommon
->LocalPeerIp
, sizeof (EFI_IP_ADDRESS
));
551 CopyMem (&ChildSaCommon
->RemotePeerIp
, &SaCommon
->RemotePeerIp
, sizeof (EFI_IP_ADDRESS
));
553 return ChildSaSession
;
557 Register a established IKEv2 Child SA into IkeSaSession->ChildSaEstablishSessionList.
558 If the there is IKEV2_CHILD_SA_SESSION with same remote peer IP, remove the old one
559 then register the new one.
561 @param[in] ChildSaSession Pointer to IKEV2_CHILD_SA_SESSION to be registered.
562 @param[in] Private Pointer to IPSEC_PRAVATE_DATA.
566 Ikev2ChildSaSessionReg (
567 IN IKEV2_CHILD_SA_SESSION
*ChildSaSession
,
568 IN IPSEC_PRIVATE_DATA
*Private
571 IKEV2_SESSION_COMMON
*SessionCommon
;
572 IKEV2_CHILD_SA_SESSION
*OldChildSaSession
;
573 IKEV2_SA_SESSION
*IkeSaSession
;
574 IKEV2_SA_PARAMS
*SaParams
;
579 // Keep the IKE SA exclusive.
581 SessionCommon
= &ChildSaSession
->SessionCommon
;
582 IkeSaSession
= ChildSaSession
->IkeSaSession
;
583 OldChildSaSession
= Ikev2ChildSaSessionRemove (
584 &IkeSaSession
->ChildSaEstablishSessionList
,
585 ChildSaSession
->LocalPeerSpi
,
586 IKEV2_ESTABLISHED_CHILDSA_LIST
588 if (OldChildSaSession
!= NULL
) {
592 Ikev2ChildSaSessionFree (OldChildSaSession
);
596 // Store the ready child SA into SAD.
598 Ikev2StoreSaData (ChildSaSession
);
601 // Cleanup the fields of SessionCommon for processing.
603 Ikev2SessionCommonRefresh (SessionCommon
);
606 // Insert the ready child SA session into established list.
608 Ikev2ChildSaSessionInsert (&IkeSaSession
->ChildSaEstablishSessionList
, ChildSaSession
);
611 // Create a Notify event for the IKE SA life time counting.
613 Status
= gBS
->CreateEvent (
614 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
618 &SessionCommon
->TimeoutEvent
620 if (EFI_ERROR(Status
)){
625 // Start to count the lifetime of the IKE SA.
627 SaParams
= SessionCommon
->SaParams
;
628 if (ChildSaSession
->Spd
->Data
->ProcessingPolicy
->SaLifetime
.HardLifetime
!= 0){
629 Lifetime
= ChildSaSession
->Spd
->Data
->ProcessingPolicy
->SaLifetime
.HardLifetime
;
631 Lifetime
= CHILD_SA_DEFAULT_LIFETIME
;
634 Status
= gBS
->SetTimer (
635 SessionCommon
->TimeoutEvent
,
637 MultU64x32(Lifetime
, 10000000) // ms->100ns
639 if (EFI_ERROR(Status
)){
645 "\n------ChildSa established and start to count down %d seconds lifetime\n",
653 Find the ChildSaSession by it's MessagId.
655 @param[in] SaSessionList Pointer to a ChildSaSession List.
656 @param[in] Mid The messageId used to search ChildSaSession.
658 @return Pointer to IKEV2_CHILD_SA_SESSION or NULL.
661 IKEV2_CHILD_SA_SESSION
*
662 Ikev2ChildSaSessionLookupByMid (
663 IN LIST_ENTRY
*SaSessionList
,
668 IKEV2_CHILD_SA_SESSION
*ChildSaSession
;
670 NET_LIST_FOR_EACH (Entry
, SaSessionList
) {
671 ChildSaSession
= IKEV2_CHILD_SA_SESSION_BY_IKE_SA (Entry
);
673 if (ChildSaSession
->MessageId
== Mid
) {
674 return ChildSaSession
;
681 This function find the Child SA by the specified SPI.
683 This functin find a ChildSA session by searching the ChildSaSessionlist of
684 the input IKEV2_SA_SESSION by specified MessageID.
686 @param[in] SaSessionList Pointer to List to be searched.
687 @param[in] Spi Specified SPI.
689 @return Pointer to IKEV2_CHILD_SA_SESSION or NULL.
692 IKEV2_CHILD_SA_SESSION
*
693 Ikev2ChildSaSessionLookupBySpi (
694 IN LIST_ENTRY
*SaSessionList
,
699 IKEV2_CHILD_SA_SESSION
*ChildSaSession
;
701 NET_LIST_FOR_EACH (Entry
, SaSessionList
) {
702 ChildSaSession
= IKEV2_CHILD_SA_SESSION_BY_IKE_SA (Entry
);
704 if (ChildSaSession
->RemotePeerSpi
== Spi
|| ChildSaSession
->LocalPeerSpi
== Spi
) {
705 return ChildSaSession
;
713 Insert a Child SA Session into the specified ChildSa list.
715 @param[in] SaSessionList Pointer to list to be inserted in.
716 @param[in] ChildSaSession Pointer to IKEV2_CHILD_SA_SESSION to be inserted.
720 Ikev2ChildSaSessionInsert (
721 IN LIST_ENTRY
*SaSessionList
,
722 IN IKEV2_CHILD_SA_SESSION
*ChildSaSession
725 InsertTailList (SaSessionList
, &ChildSaSession
->ByIkeSa
);
729 Remove the IKEV2_CHILD_SA_SESSION from IkeSaSessionList.
731 @param[in] SaSessionList The SA Session List to be iterated.
732 @param[in] Spi Spi used to identified the IKEV2_CHILD_SA_SESSION.
733 @param[in] ListType The type of the List to indicate whether it is a
736 @return The point to IKEV2_CHILD_SA_SESSION or NULL.
739 IKEV2_CHILD_SA_SESSION
*
740 Ikev2ChildSaSessionRemove (
741 IN LIST_ENTRY
*SaSessionList
,
747 LIST_ENTRY
*NextEntry
;
748 IKEV2_CHILD_SA_SESSION
*ChildSaSession
;
750 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, SaSessionList
) {
752 if (ListType
== IKEV2_ESTABLISHED_CHILDSA_LIST
|| ListType
== IKEV2_ESTABLISHING_CHILDSA_LIST
) {
753 ChildSaSession
= IKEV2_CHILD_SA_SESSION_BY_IKE_SA (Entry
);
754 } else if (ListType
== IKEV2_DELET_CHILDSA_LIST
) {
755 ChildSaSession
= IKEV2_CHILD_SA_SESSION_BY_DEL_SA (Entry
);
760 if (ChildSaSession
->RemotePeerSpi
== Spi
|| ChildSaSession
->LocalPeerSpi
== Spi
) {
761 RemoveEntryList (Entry
);
762 return ChildSaSession
;
770 Mark a specified Child SA Session as on deleting.
772 @param[in] ChildSaSession Pointer to IKEV2_CHILD_SA_SESSION.
774 @retval EFI_SUCCESS Operation is successful.
778 Ikev2ChildSaSessionOnDeleting (
779 IN IKEV2_CHILD_SA_SESSION
*ChildSaSession
786 Free the memory located for the specified IKEV2_CHILD_SA_SESSION.
788 @param[in] ChildSaSession Pointer to IKEV2_CHILD_SA_SESSION.
792 Ikev2ChildSaSessionFree (
793 IN IKEV2_CHILD_SA_SESSION
*ChildSaSession
796 IKEV2_SESSION_COMMON
*SessionCommon
;
798 SessionCommon
= &ChildSaSession
->SessionCommon
;
799 if (ChildSaSession
->SaData
!= NULL
) {
800 FreePool (ChildSaSession
->SaData
);
803 if (ChildSaSession
->NiBlock
!= NULL
) {
804 FreePool (ChildSaSession
->NiBlock
);
807 if (ChildSaSession
->NrBlock
!= NULL
) {
808 FreePool (ChildSaSession
->NrBlock
);
811 if (ChildSaSession
->ChildKeymats
.LocalPeerInfo
.EspAlgoInfo
.AuthKey
!= NULL
) {
812 FreePool (ChildSaSession
->ChildKeymats
.LocalPeerInfo
.EspAlgoInfo
.AuthKey
);
815 if (ChildSaSession
->ChildKeymats
.LocalPeerInfo
.EspAlgoInfo
.EncKey
!= NULL
) {
816 FreePool (ChildSaSession
->ChildKeymats
.LocalPeerInfo
.EspAlgoInfo
.EncKey
);
819 if (ChildSaSession
->ChildKeymats
.RemotePeerInfo
.EspAlgoInfo
.AuthKey
!= NULL
) {
820 FreePool (ChildSaSession
->ChildKeymats
.RemotePeerInfo
.EspAlgoInfo
.AuthKey
);
823 if (ChildSaSession
->ChildKeymats
.RemotePeerInfo
.EspAlgoInfo
.EncKey
!= NULL
) {
824 FreePool (ChildSaSession
->ChildKeymats
.RemotePeerInfo
.EspAlgoInfo
.EncKey
);
830 Ikev2DhBufferFree (ChildSaSession
->DhBuffer
);
833 // Delete SpdSelector
835 if (ChildSaSession
->SpdSelector
!= NULL
) {
836 if (ChildSaSession
->SpdSelector
->LocalAddress
!= NULL
) {
837 FreePool (ChildSaSession
->SpdSelector
->LocalAddress
);
839 if (ChildSaSession
->SpdSelector
->RemoteAddress
!= NULL
) {
840 FreePool (ChildSaSession
->SpdSelector
->RemoteAddress
);
842 FreePool (ChildSaSession
->SpdSelector
);
844 Ikev2SaSessionCommonFree (SessionCommon
);
845 FreePool (ChildSaSession
);
851 Delete the specified established Child SA.
853 This function delete the Child SA directly and don't send the Information Packet to
856 @param[in] IkeSaSession Pointer to a IKE SA Session used to be searched for.
857 @param[in] Spi SPI used to find the Child SA.
859 @retval EFI_NOT_FOUND Pointer of IKE SA Session is NULL.
860 @retval EFI_NOT_FOUND There is no specified Child SA related with the input
861 SPI under this IKE SA Session.
862 @retval EFI_SUCCESS Delete the Child SA successfully.
866 Ikev2ChildSaSilentDelete (
867 IN IKEV2_SA_SESSION
*IkeSaSession
,
872 EFI_IPSEC_CONFIG_SELECTOR
*Selector
;
874 BOOLEAN IsLocalFound
;
875 BOOLEAN IsRemoteFound
;
878 IKEV2_CHILD_SA_SESSION
*ChildSession
;
879 EFI_IPSEC_CONFIG_SELECTOR
*LocalSelector
;
880 EFI_IPSEC_CONFIG_SELECTOR
*RemoteSelector
;
881 IKE_UDP_SERVICE
*UdpService
;
882 IPSEC_PRIVATE_DATA
*Private
;
884 if (IkeSaSession
== NULL
) {
885 return EFI_NOT_FOUND
;
888 IsLocalFound
= FALSE
;
889 IsRemoteFound
= FALSE
;
891 LocalSelector
= NULL
;
892 RemoteSelector
= NULL
;
893 UdpService
= IkeSaSession
->SessionCommon
.UdpService
;
895 Private
= (UdpService
->IpVersion
== IP_VERSION_4
) ?
896 IPSEC_PRIVATE_DATA_FROM_UDP4LIST(UdpService
->ListHead
) :
897 IPSEC_PRIVATE_DATA_FROM_UDP6LIST(UdpService
->ListHead
);
900 // Remove the Established SA from ChildSaEstablishlist.
902 ChildSession
= Ikev2ChildSaSessionRemove(
903 &(IkeSaSession
->ChildSaEstablishSessionList
),
905 IKEV2_ESTABLISHED_CHILDSA_LIST
907 if (ChildSession
== NULL
) {
908 return EFI_NOT_FOUND
;
911 LocalSpi
= ChildSession
->LocalPeerSpi
;
912 RemoteSpi
= ChildSession
->RemotePeerSpi
;
914 SelectorSize
= sizeof (EFI_IPSEC_CONFIG_SELECTOR
);
915 Selector
= AllocateZeroPool (SelectorSize
);
916 ASSERT (Selector
!= NULL
);
921 Status
= EfiIpSecConfigGetNextSelector (
922 &Private
->IpSecConfig
,
923 IPsecConfigDataTypeSad
,
927 if (Status
== EFI_BUFFER_TOO_SMALL
) {
930 Selector
= AllocateZeroPool (SelectorSize
);
931 Status
= EfiIpSecConfigGetNextSelector (
932 &Private
->IpSecConfig
,
933 IPsecConfigDataTypeSad
,
939 if (EFI_ERROR (Status
)) {
943 if (Selector
->SaId
.Spi
== RemoteSpi
) {
945 // SPI is unique. There is only one SAD whose SPI is
946 // same with RemoteSpi.
948 IsRemoteFound
= TRUE
;
949 RemoteSelector
= AllocateZeroPool (SelectorSize
);
950 CopyMem (RemoteSelector
, Selector
, SelectorSize
);
953 if (Selector
->SaId
.Spi
== LocalSpi
) {
955 // SPI is unique. There is only one SAD whose SPI is
956 // same with LocalSpi.
959 LocalSelector
= AllocateZeroPool (SelectorSize
);
960 CopyMem (LocalSelector
, Selector
, SelectorSize
);
964 // Delete SA from the Variable.
967 Status
= EfiIpSecConfigSetData (
968 &Private
->IpSecConfig
,
969 IPsecConfigDataTypeSad
,
977 Status
= EfiIpSecConfigSetData (
978 &Private
->IpSecConfig
,
979 IPsecConfigDataTypeSad
,
989 "\n------IKEV2 deleted ChildSa(local spi, remote spi):(0x%x, 0x%x)------\n",
993 Ikev2ChildSaSessionFree (ChildSession
);
995 if (RemoteSelector
!= NULL
) {
996 FreePool (RemoteSelector
);
999 if (LocalSelector
!= NULL
) {
1000 FreePool (LocalSelector
);
1003 if (Selector
!= NULL
) {
1004 FreePool (Selector
);
1011 Free the specified DhBuffer.
1013 @param[in] DhBuffer Pointer to IKEV2_DH_BUFFER to be freed.
1018 IKEV2_DH_BUFFER
*DhBuffer
1021 if (DhBuffer
!= NULL
) {
1022 if (DhBuffer
->GxBuffer
!= NULL
) {
1023 FreePool (DhBuffer
->GxBuffer
);
1025 if (DhBuffer
->GyBuffer
!= NULL
) {
1026 FreePool (DhBuffer
->GyBuffer
);
1028 if (DhBuffer
->GxyBuffer
!= NULL
) {
1029 FreePool (DhBuffer
->GxyBuffer
);
1031 if (DhBuffer
->DhContext
!= NULL
) {
1032 IpSecCryptoIoFreeDh (&DhBuffer
->DhContext
);
1034 FreePool (DhBuffer
);
1039 This function is to parse a request IKE packet and return its request type.
1040 The request type is one of IKE CHILD SA creation, IKE SA rekeying and
1041 IKE CHILD SA rekeying.
1043 @param[in] IkePacket IKE packet to be prased.
1045 return the type of the IKE packet.
1048 IKEV2_CREATE_CHILD_REQUEST_TYPE
1049 Ikev2ChildExchangeRequestType(
1050 IN IKE_PACKET
*IkePacket
1055 IKE_PAYLOAD
*IkePayload
;
1059 NET_LIST_FOR_EACH (Entry
, &(IkePacket
)->PayloadList
) {
1060 IkePayload
= IKE_PAYLOAD_BY_PACKET (Entry
);
1061 if (IkePayload
->PayloadType
== IKEV2_PAYLOAD_TYPE_TS_INIT
) {
1063 // Packet with Ts Payload means it is for either CHILD_SA_CREATE or CHILD_SA_REKEY.
1067 if (IkePayload
->PayloadType
== IKEV2_PAYLOAD_TYPE_NOTIFY
) {
1068 if (((IKEV2_NOTIFY
*)IkePayload
)->MessageType
== IKEV2_NOTIFICATION_REKEY_SA
) {
1070 // If notify payload with REKEY_SA message type, the IkePacket is for
1071 // rekeying Child SA.
1073 return IkeRequestTypeRekeyChildSa
;
1080 // The Create Child Exchange is for IKE SA rekeying.
1082 return IkeRequestTypeRekeyIkeSa
;
1085 // If the Notify payloaad with transport mode message type, the IkePacket is
1086 // for create Child SA.
1088 return IkeRequestTypeCreateChildSa
;
1093 Associate a SPD selector to the Child SA Session.
1095 This function is called when the Child SA is not the first child SA of its
1096 IKE SA. It associate a SPD to this Child SA.
1098 @param[in, out] ChildSaSession Pointer to the Child SA Session to be associated to
1101 @retval EFI_SUCCESS Associate one SPD selector to this Child SA Session successfully.
1102 @retval EFI_NOT_FOUND Can't find the related SPD selector.
1106 Ikev2ChildSaAssociateSpdEntry (
1107 IN OUT IKEV2_CHILD_SA_SESSION
*ChildSaSession
1110 IpSecVisitConfigData (IPsecConfigDataTypeSpd
, Ikev2MatchSpdEntry
, ChildSaSession
);
1111 if (ChildSaSession
->Spd
!= NULL
) {
1114 return EFI_NOT_FOUND
;
1120 This function finds the SPI from Create Child SA Exchange Packet.
1122 @param[in] IkePacket Pointer to IKE_PACKET to be searched.
1124 @retval SPI number or 0 if it is not supported.
1128 Ikev2ChildExchangeRekeySpi (
1129 IN IKE_PACKET
*IkePacket
1139 Validate the IKE header of received IKE packet.
1141 @param[in] IkeSaSession Pointer to IKEV2_SA_SESSION related to this IKE packet.
1142 @param[in] IkeHdr Pointer to IKE header of received IKE packet.
1144 @retval TRUE If the IKE header is valid.
1145 @retval FALSE If the IKE header is invalid.
1149 Ikev2ValidateHeader (
1150 IN IKEV2_SA_SESSION
*IkeSaSession
,
1151 IN IKE_HEADER
*IkeHdr
1155 IKEV2_SESSION_STATE State
;
1157 State
= IkeSaSession
->SessionCommon
.State
;
1158 if (State
== IkeStateInit
) {
1160 // For the IKE Initial Exchange, the MessagId should be zero.
1162 if (IkeHdr
->MessageId
!= 0) {
1166 if (State
== IkeStateAuth
) {
1167 if (IkeHdr
->MessageId
!= 1) {
1171 if (IkeHdr
->InitiatorCookie
!= IkeSaSession
->InitiatorCookie
||
1172 IkeHdr
->ResponderCookie
!= IkeSaSession
->ResponderCookie
1175 // TODO: send notification INVALID-COOKIE
1182 // Information Exchagne and Create Child Exchange can be started from each part.
1184 if (IkeHdr
->ExchangeType
!= IKEV2_EXCHANGE_TYPE_INFO
&&
1185 IkeHdr
->ExchangeType
!= IKEV2_EXCHANGE_TYPE_CREATE_CHILD
1187 if (IkeSaSession
->SessionCommon
.IsInitiator
) {
1188 if (IkeHdr
->InitiatorCookie
!= IkeSaSession
->InitiatorCookie
) {
1190 // TODO: send notification INVALID-COOKIE
1194 if (IkeHdr
->Flags
!= IKE_HEADER_FLAGS_RESPOND
) {
1198 if (IkeHdr
->Flags
!= IKE_HEADER_FLAGS_INIT
) {
1208 Create and intialize IKEV2_SA_DATA for speicifed IKEV2_SESSION_COMMON.
1210 This function will be only called by the initiator. The responder's IKEV2_SA_DATA
1211 will be generated during parsed the initiator packet.
1213 @param[in] SessionCommon Pointer to IKEV2_SESSION_COMMON related to.
1215 @retval a Pointer to a new IKEV2_SA_DATA or NULL.
1219 Ikev2InitializeSaData (
1220 IN IKEV2_SESSION_COMMON
*SessionCommon
1223 IKEV2_CHILD_SA_SESSION
*ChildSaSession
;
1224 IKEV2_SA_DATA
*SaData
;
1225 IKEV2_PROPOSAL_DATA
*ProposalData
;
1226 IKEV2_TRANSFORM_DATA
*TransformData
;
1227 IKE_SA_ATTRIBUTE
*Attribute
;
1229 ASSERT (SessionCommon
!= NULL
);
1231 // TODO: Remove the hard code of the support Alogrithm. Those data should be
1232 // get from the SPD/PAD data.
1234 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1235 SaData
= AllocateZeroPool (
1236 sizeof (IKEV2_SA_DATA
) +
1237 sizeof (IKEV2_PROPOSAL_DATA
) * 2 +
1238 sizeof (IKEV2_TRANSFORM_DATA
) * 4 * 2
1241 SaData
= AllocateZeroPool (
1242 sizeof (IKEV2_SA_DATA
) +
1243 sizeof (IKEV2_PROPOSAL_DATA
) * 2 +
1244 sizeof (IKEV2_TRANSFORM_DATA
) * 3 * 2
1247 if (SaData
== NULL
) {
1252 // First proposal payload: 3DES + SHA1 + DH
1254 SaData
->NumProposals
= 2;
1255 ProposalData
= (IKEV2_PROPOSAL_DATA
*) (SaData
+ 1);
1256 ProposalData
->ProposalIndex
= 1;
1259 // If SA data for IKE_SA_INIT exchage, contains 4 transforms. If SA data for
1260 // IKE_AUTH exchange contains 3 transforms.
1262 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1263 ProposalData
->NumTransforms
= 4;
1265 ProposalData
->NumTransforms
= 3;
1269 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1270 ProposalData
->ProtocolId
= IPSEC_PROTO_ISAKMP
;
1272 ChildSaSession
= IKEV2_CHILD_SA_SESSION_FROM_COMMON (SessionCommon
);
1273 ProposalData
->ProtocolId
= IPSEC_PROTO_IPSEC_ESP
;
1274 ProposalData
->Spi
= AllocateZeroPool (sizeof (ChildSaSession
->LocalPeerSpi
));
1275 ASSERT (ProposalData
->Spi
!= NULL
);
1278 &ChildSaSession
->LocalPeerSpi
,
1279 sizeof(ChildSaSession
->LocalPeerSpi
)
1284 // Set transform attribute for Encryption Algorithm - 3DES
1286 TransformData
= (IKEV2_TRANSFORM_DATA
*) (ProposalData
+ 1);
1287 TransformData
->TransformIndex
= 0;
1288 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_ENCR
;
1289 TransformData
->TransformId
= IKEV2_TRANSFORM_ID_ENCR_3DES
;
1292 // Set transform attribute for Integrity Algorithm - SHA1_96
1294 TransformData
= (IKEV2_TRANSFORM_DATA
*) (TransformData
+ 1);
1295 TransformData
->TransformIndex
= 1;
1296 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_INTEG
;
1297 TransformData
->TransformId
= IKEV2_TRANSFORM_ID_AUTH_HMAC_SHA1_96
;
1299 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1301 // Set transform attribute for Pseduo-Random Function - HAMC_SHA1
1303 TransformData
= (IKEV2_TRANSFORM_DATA
*) (TransformData
+ 1);
1304 TransformData
->TransformIndex
= 2;
1305 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_PRF
;
1306 TransformData
->TransformId
= IKEV2_TRANSFORM_ID_PRF_HMAC_SHA1
;
1309 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1311 // Set transform attribute for DH Group - DH 1024
1313 TransformData
= (IKEV2_TRANSFORM_DATA
*) (TransformData
+ 1);
1314 TransformData
->TransformIndex
= 3;
1315 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_DH
;
1316 TransformData
->TransformId
= IKEV2_TRANSFORM_ID_DH_1024MODP
;
1319 // Transform type for Extended Sequence Numbers. Currently not support Extended
1322 TransformData
= (IKEV2_TRANSFORM_DATA
*) (TransformData
+ 1);
1323 TransformData
->TransformIndex
= 2;
1324 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_ESN
;
1325 TransformData
->TransformId
= 0;
1329 // Second proposal payload: 3DES + SHA1 + DH
1331 ProposalData
= (IKEV2_PROPOSAL_DATA
*) (TransformData
+ 1);
1332 ProposalData
->ProposalIndex
= 2;
1334 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1335 ProposalData
->ProtocolId
= IPSEC_PROTO_ISAKMP
;
1336 ProposalData
->NumTransforms
= 4;
1339 ChildSaSession
= IKEV2_CHILD_SA_SESSION_FROM_COMMON (SessionCommon
);
1340 ProposalData
->ProtocolId
= IPSEC_PROTO_IPSEC_ESP
;
1341 ProposalData
->NumTransforms
= 3;
1342 ProposalData
->Spi
= AllocateZeroPool (sizeof (ChildSaSession
->LocalPeerSpi
));
1343 ASSERT (ProposalData
->Spi
!= NULL
);
1346 &ChildSaSession
->LocalPeerSpi
,
1347 sizeof(ChildSaSession
->LocalPeerSpi
)
1352 // Set transform attribute for Encryption Algorithm - AES-CBC
1354 TransformData
= (IKEV2_TRANSFORM_DATA
*) (ProposalData
+ 1);
1355 TransformData
->TransformIndex
= 0;
1356 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_ENCR
;
1357 TransformData
->TransformId
= IKEV2_TRANSFORM_ID_ENCR_AES_CBC
;
1358 Attribute
= &TransformData
->Attribute
;
1359 Attribute
->AttrType
= IKEV2_ATTRIBUTE_TYPE_KEYLEN
;
1360 Attribute
->Attr
.AttrLength
= (UINT16
) (8 * IpSecGetEncryptKeyLength (IKEV2_TRANSFORM_ID_ENCR_AES_CBC
));
1363 // Set transform attribute for Integrity Algorithm - SHA1_96
1365 TransformData
= (IKEV2_TRANSFORM_DATA
*) (TransformData
+ 1);
1366 TransformData
->TransformIndex
= 1;
1367 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_INTEG
;
1368 TransformData
->TransformId
= IKEV2_TRANSFORM_ID_AUTH_HMAC_SHA1_96
;
1370 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1372 // Set transform attribute for Pseduo-Random Function - HAMC_SHA1
1374 TransformData
= (IKEV2_TRANSFORM_DATA
*) (TransformData
+ 1);
1375 TransformData
->TransformIndex
= 2;
1376 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_PRF
;
1377 TransformData
->TransformId
= IKEV2_TRANSFORM_ID_PRF_HMAC_SHA1
;
1380 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1382 // Set transform attrbiute for DH Group - DH-1024
1384 TransformData
= (IKEV2_TRANSFORM_DATA
*) (TransformData
+ 1);
1385 TransformData
->TransformIndex
= 3;
1386 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_DH
;
1387 TransformData
->TransformId
= IKEV2_TRANSFORM_ID_DH_1024MODP
;
1390 // Transform type for Extended Sequence Numbers. Currently not support Extended
1393 TransformData
= (IKEV2_TRANSFORM_DATA
*) (TransformData
+ 1);
1394 TransformData
->TransformIndex
= 2;
1395 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_ESN
;
1396 TransformData
->TransformId
= 0;
1403 Store the SA into SAD.
1405 @param[in] ChildSaSession Pointer to IKEV2_CHILD_SA_SESSION.
1410 IN IKEV2_CHILD_SA_SESSION
*ChildSaSession
1414 EFI_IPSEC_SA_ID SaId
;
1415 EFI_IPSEC_SA_DATA2 SaData
;
1416 IKEV2_SESSION_COMMON
*SessionCommon
;
1417 IPSEC_PRIVATE_DATA
*Private
;
1418 UINT32 TempAddressCount
;
1419 EFI_IP_ADDRESS_INFO
*TempAddressInfo
;
1421 SessionCommon
= &ChildSaSession
->SessionCommon
;
1422 Private
= SessionCommon
->Private
;
1424 ZeroMem (&SaId
, sizeof (EFI_IPSEC_SA_ID
));
1425 ZeroMem (&SaData
, sizeof (EFI_IPSEC_SA_DATA2
));
1428 // Create a SpdSelector. In this implementation, one SPD represents
1429 // 2 direction traffic, so in here, there needs to reverse the local address
1430 // and remote address for Remote Peer's SA, then reverse again for the locate
1433 TempAddressCount
= ChildSaSession
->SpdSelector
->LocalAddressCount
;
1434 TempAddressInfo
= ChildSaSession
->SpdSelector
->LocalAddress
;
1436 ChildSaSession
->SpdSelector
->LocalAddressCount
= ChildSaSession
->SpdSelector
->RemoteAddressCount
;
1437 ChildSaSession
->SpdSelector
->LocalAddress
= ChildSaSession
->SpdSelector
->RemoteAddress
;
1439 ChildSaSession
->SpdSelector
->RemoteAddress
= TempAddressInfo
;
1440 ChildSaSession
->SpdSelector
->RemoteAddressCount
= TempAddressCount
;
1443 // Set the SaId and SaData.
1445 SaId
.Spi
= ChildSaSession
->LocalPeerSpi
;
1446 SaId
.Proto
= EfiIPsecESP
;
1447 SaData
.AntiReplayWindows
= 16;
1449 SaData
.Mode
= ChildSaSession
->Spd
->Data
->ProcessingPolicy
->Mode
;
1452 // If it is tunnel mode, should add the TunnelDest and TunnelSource for SaData.
1454 if (SaData
.Mode
== EfiIPsecTunnel
) {
1456 &SaData
.TunnelSourceAddress
,
1457 &ChildSaSession
->Spd
->Data
->ProcessingPolicy
->TunnelOption
->RemoteTunnelAddress
,
1458 sizeof (EFI_IP_ADDRESS
)
1461 &SaData
.TunnelDestinationAddress
,
1462 &ChildSaSession
->Spd
->Data
->ProcessingPolicy
->TunnelOption
->LocalTunnelAddress
,
1463 sizeof (EFI_IP_ADDRESS
)
1467 CopyMem (&SaId
.DestAddress
, &ChildSaSession
->SessionCommon
.LocalPeerIp
, sizeof (EFI_IP_ADDRESS
));
1468 CopyMem (&SaData
.AlgoInfo
, &ChildSaSession
->ChildKeymats
.LocalPeerInfo
, sizeof (EFI_IPSEC_ALGO_INFO
));
1469 SaData
.SpdSelector
= ChildSaSession
->SpdSelector
;
1472 // Store the remote SA into SAD.
1474 Status
= EfiIpSecConfigSetData (
1475 &Private
->IpSecConfig
,
1476 IPsecConfigDataTypeSad
,
1477 (EFI_IPSEC_CONFIG_SELECTOR
*) &SaId
,
1481 ASSERT_EFI_ERROR (Status
);
1484 // Store the local SA into SAD.
1486 ChildSaSession
->SpdSelector
->RemoteAddressCount
= ChildSaSession
->SpdSelector
->LocalAddressCount
;
1487 ChildSaSession
->SpdSelector
->RemoteAddress
= ChildSaSession
->SpdSelector
->LocalAddress
;
1489 ChildSaSession
->SpdSelector
->LocalAddress
= TempAddressInfo
;
1490 ChildSaSession
->SpdSelector
->LocalAddressCount
= TempAddressCount
;
1492 SaId
.Spi
= ChildSaSession
->RemotePeerSpi
;
1494 CopyMem (&SaId
.DestAddress
, &ChildSaSession
->SessionCommon
.RemotePeerIp
, sizeof (EFI_IP_ADDRESS
));
1495 CopyMem (&SaData
.AlgoInfo
, &ChildSaSession
->ChildKeymats
.RemotePeerInfo
, sizeof (EFI_IPSEC_ALGO_INFO
));
1496 SaData
.SpdSelector
= ChildSaSession
->SpdSelector
;
1499 // If it is tunnel mode, should add the TunnelDest and TunnelSource for SaData.
1501 if (SaData
.Mode
== EfiIPsecTunnel
) {
1503 &SaData
.TunnelSourceAddress
,
1504 &ChildSaSession
->Spd
->Data
->ProcessingPolicy
->TunnelOption
->LocalTunnelAddress
,
1505 sizeof (EFI_IP_ADDRESS
)
1508 &SaData
.TunnelDestinationAddress
,
1509 &ChildSaSession
->Spd
->Data
->ProcessingPolicy
->TunnelOption
->RemoteTunnelAddress
,
1510 sizeof (EFI_IP_ADDRESS
)
1514 Status
= EfiIpSecConfigSetData (
1515 &Private
->IpSecConfig
,
1516 IPsecConfigDataTypeSad
,
1517 (EFI_IPSEC_CONFIG_SELECTOR
*) &SaId
,
1522 ASSERT_EFI_ERROR (Status
);
1526 Call back function of the IKE life time is over.
1528 This function will mark the related IKE SA Session as deleting and trigger a
1529 Information negotiation.
1531 @param[in] Event The signaled Event.
1532 @param[in] Context Pointer to data passed by caller.
1537 Ikev2LifetimeNotify (
1542 IKEV2_SA_SESSION
*IkeSaSession
;
1543 IKEV2_CHILD_SA_SESSION
*ChildSaSession
;
1544 IKEV2_SESSION_COMMON
*SessionCommon
;
1546 ASSERT (Context
!= NULL
);
1547 SessionCommon
= (IKEV2_SESSION_COMMON
*) Context
;
1549 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1550 IkeSaSession
= IKEV2_SA_SESSION_FROM_COMMON (SessionCommon
);
1553 "\n---IkeSa Lifetime is out(cookie_i, cookie_r):(0x%lx, 0x%lx)---\n",
1554 IkeSaSession
->InitiatorCookie
,
1555 IkeSaSession
->ResponderCookie
1559 // Change the IKE SA Session's State to IKE_STATE_SA_DELETING.
1561 IKEV2_DUMP_STATE (IkeSaSession
->SessionCommon
.State
, IkeStateSaDeleting
);
1562 IkeSaSession
->SessionCommon
.State
= IkeStateSaDeleting
;
1565 ChildSaSession
= IKEV2_CHILD_SA_SESSION_FROM_COMMON (SessionCommon
);
1566 IkeSaSession
= ChildSaSession
->IkeSaSession
;
1569 // Link the timeout child SA to the DeleteSaList.
1571 InsertTailList (&IkeSaSession
->DeleteSaList
, &ChildSaSession
->ByDelete
);
1574 // Change the Child SA Session's State to IKE_STATE_SA_DELETING.
1578 "\n------ChildSa Lifetime is out(SPI):(0x%x)------\n",
1579 ChildSaSession
->LocalPeerSpi
1584 // TODO: Send the delete info packet or delete silently
1586 mIkev2Exchange
.NegotiateInfo ((UINT8
*) IkeSaSession
, NULL
);
1590 This function will be called if the TimeOut Event is signaled.
1592 @param[in] Event The signaled Event.
1593 @param[in] Context The data passed by caller.
1603 IPSEC_PRIVATE_DATA
*Private
;
1604 IKEV2_SA_SESSION
*IkeSaSession
;
1605 IKEV2_CHILD_SA_SESSION
*ChildSaSession
;
1606 IKEV2_SESSION_COMMON
*SessionCommon
;
1607 LIST_ENTRY
*ChildSaEntry
;
1611 ASSERT (Context
!= NULL
);
1612 IkeSaSession
= NULL
;
1613 ChildSaSession
= NULL
;
1614 SessionCommon
= (IKEV2_SESSION_COMMON
*) Context
;
1615 Private
= SessionCommon
->Private
;
1618 // Remove the SA session from the processing list if exceed the max retry.
1620 if (SessionCommon
->RetryCount
> IKE_MAX_RETRY
) {
1621 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1622 IkeSaSession
= IKEV2_SA_SESSION_FROM_COMMON (SessionCommon
);
1623 if (IkeSaSession
->SessionCommon
.State
== IkeStateSaDeleting
) {
1626 // If the IkeSaSession is initiator, delete all its Child SAs before removing IKE SA.
1627 // If the IkesaSession is responder, all ChildSa has been remove in Ikev2HandleInfo();
1629 for (ChildSaEntry
= IkeSaSession
->ChildSaEstablishSessionList
.ForwardLink
;
1630 ChildSaEntry
!= &IkeSaSession
->ChildSaEstablishSessionList
;
1632 ChildSaSession
= IKEV2_CHILD_SA_SESSION_BY_IKE_SA (ChildSaEntry
);
1634 // Move to next ChildSa Entry.
1636 ChildSaEntry
= ChildSaEntry
->ForwardLink
;
1638 // Delete LocalSpi & RemoteSpi and remove the ChildSaSession from the
1639 // EstablishedChildSaList.
1641 Ikev2ChildSaSilentDelete (IkeSaSession
, ChildSaSession
->LocalPeerSpi
);
1645 // If the IKE SA Delete Payload wasn't sent out successfully, Delete it from the EstablishedList.
1647 Ikev2SaSessionRemove (&Private
->Ikev2EstablishedList
, &SessionCommon
->RemotePeerIp
);
1649 if (Private
!= NULL
&& Private
->IsIPsecDisabling
) {
1651 // After all IKE SAs were deleted, set the IPSEC_STATUS_DISABLED value in
1652 // IPsec status variable.
1654 if (IsListEmpty (&Private
->Ikev1EstablishedList
) && IsListEmpty (&Private
->Ikev2EstablishedList
)) {
1655 Value
= IPSEC_STATUS_DISABLED
;
1656 Status
= gRT
->SetVariable (
1657 IPSECCONFIG_STATUS_NAME
,
1658 &gEfiIpSecConfigProtocolGuid
,
1659 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1663 if (!EFI_ERROR (Status
)) {
1665 // Set the Disabled Flag in Private data.
1667 Private
->IpSec
.DisabledFlag
= TRUE
;
1668 Private
->IsIPsecDisabling
= FALSE
;
1673 Ikev2SaSessionRemove (&Private
->Ikev2SessionList
, &SessionCommon
->RemotePeerIp
);
1675 Ikev2SaSessionFree (IkeSaSession
);
1680 // If the packet sent by Child SA.
1682 ChildSaSession
= IKEV2_CHILD_SA_SESSION_FROM_COMMON (SessionCommon
);
1683 IkeSaSession
= ChildSaSession
->IkeSaSession
;
1684 if (ChildSaSession
->SessionCommon
.State
== IkeStateSaDeleting
) {
1687 // Established Child SA should be remove from the SAD entry and
1688 // DeleteList. The function of Ikev2DeleteChildSaSilent() will remove
1689 // the childSA from the IkeSaSession->ChildSaEstablishedList. So there
1690 // is no need to remove it here.
1692 Ikev2ChildSaSilentDelete (IkeSaSession
, ChildSaSession
->LocalPeerSpi
);
1693 Ikev2ChildSaSessionRemove (
1694 &IkeSaSession
->DeleteSaList
,
1695 ChildSaSession
->LocalPeerSpi
,
1696 IKEV2_DELET_CHILDSA_LIST
1699 Ikev2ChildSaSessionRemove (
1700 &IkeSaSession
->ChildSaSessionList
,
1701 ChildSaSession
->LocalPeerSpi
,
1702 IKEV2_ESTABLISHING_CHILDSA_LIST
1706 Ikev2ChildSaSessionFree (ChildSaSession
);
1712 // Increase the retry count.
1714 SessionCommon
->RetryCount
++;
1715 DEBUG ((DEBUG_INFO
, ">>>Resending the last packet ...\n"));
1718 // Resend the last packet.
1720 Ikev2SendIkePacket (
1721 SessionCommon
->UdpService
,
1722 (UINT8
*)SessionCommon
,
1723 SessionCommon
->LastSentPacket
,
1729 Copy ChildSaSession->Spd->Selector to ChildSaSession->SpdSelector.
1731 ChildSaSession->SpdSelector stores the real Spdselector for its SA. Sometime,
1732 the SpdSelector in ChildSaSession is more accurated or the scope is smaller
1733 than the one in ChildSaSession->Spd, especially for the tunnel mode.
1735 @param[in, out] ChildSaSession Pointer to IKEV2_CHILD_SA_SESSION related to.
1739 Ikev2ChildSaSessionSpdSelectorCreate (
1740 IN OUT IKEV2_CHILD_SA_SESSION
*ChildSaSession
1743 if (ChildSaSession
->Spd
!= NULL
&& ChildSaSession
->Spd
->Selector
!= NULL
) {
1744 if (ChildSaSession
->SpdSelector
== NULL
) {
1745 ChildSaSession
->SpdSelector
= AllocateZeroPool (sizeof (EFI_IPSEC_SPD_SELECTOR
));
1746 ASSERT (ChildSaSession
->SpdSelector
!= NULL
);
1749 ChildSaSession
->SpdSelector
,
1750 ChildSaSession
->Spd
->Selector
,
1751 sizeof (EFI_IPSEC_SPD_SELECTOR
)
1753 ChildSaSession
->SpdSelector
->RemoteAddress
= AllocateCopyPool (
1754 ChildSaSession
->Spd
->Selector
->RemoteAddressCount
*
1755 sizeof (EFI_IP_ADDRESS_INFO
),
1756 ChildSaSession
->Spd
->Selector
->RemoteAddress
1758 ChildSaSession
->SpdSelector
->LocalAddress
= AllocateCopyPool (
1759 ChildSaSession
->Spd
->Selector
->LocalAddressCount
*
1760 sizeof (EFI_IP_ADDRESS_INFO
),
1761 ChildSaSession
->Spd
->Selector
->LocalAddress
1764 ASSERT (ChildSaSession
->SpdSelector
->LocalAddress
!= NULL
);
1765 ASSERT (ChildSaSession
->SpdSelector
->RemoteAddress
!= NULL
);
1767 ChildSaSession
->SpdSelector
->RemoteAddressCount
= ChildSaSession
->Spd
->Selector
->RemoteAddressCount
;
1768 ChildSaSession
->SpdSelector
->LocalAddressCount
= ChildSaSession
->Spd
->Selector
->LocalAddressCount
;
1773 Generate a ChildSa Session and insert it into related IkeSaSession.
1775 @param[in] IkeSaSession Pointer to related IKEV2_SA_SESSION.
1776 @param[in] UdpService Pointer to related IKE_UDP_SERVICE.
1778 @return pointer of IKEV2_CHILD_SA_SESSION.
1781 IKEV2_CHILD_SA_SESSION
*
1782 Ikev2ChildSaSessionCreate (
1783 IN IKEV2_SA_SESSION
*IkeSaSession
,
1784 IN IKE_UDP_SERVICE
*UdpService
1787 IKEV2_CHILD_SA_SESSION
*ChildSaSession
;
1788 IKEV2_SESSION_COMMON
*ChildSaCommon
;
1791 // Create a new ChildSaSession.Insert it into processing list and initiate the common parameters.
1793 ChildSaSession
= Ikev2ChildSaSessionAlloc (UdpService
, IkeSaSession
);
1794 ASSERT (ChildSaSession
!= NULL
);
1797 // Set the specific parameters.
1799 ChildSaSession
->Spd
= IkeSaSession
->Spd
;
1800 ChildSaCommon
= &ChildSaSession
->SessionCommon
;
1801 ChildSaCommon
->IsInitiator
= IkeSaSession
->SessionCommon
.IsInitiator
;
1802 if (IkeSaSession
->SessionCommon
.State
== IkeStateAuth
) {
1803 ChildSaCommon
->State
= IkeStateAuth
;
1804 IKEV2_DUMP_STATE (ChildSaCommon
->State
, IkeStateAuth
);
1806 ChildSaCommon
->State
= IkeStateCreateChild
;
1807 IKEV2_DUMP_STATE (ChildSaCommon
->State
, IkeStateCreateChild
);
1811 // If SPD->Selector is not NULL, copy it to the ChildSaSession->SpdSelector.
1812 // The ChildSaSession->SpdSelector might be changed after the traffic selector
1813 // negoniation and it will be copied into the SAData after ChildSA established.
1815 Ikev2ChildSaSessionSpdSelectorCreate (ChildSaSession
);
1818 // Copy first NiBlock and NrBlock to ChildSa Session
1820 ChildSaSession
->NiBlock
= AllocateZeroPool (IkeSaSession
->NiBlkSize
);
1821 ASSERT (ChildSaSession
->NiBlock
!= NULL
);
1822 ChildSaSession
->NiBlkSize
= IkeSaSession
->NiBlkSize
;
1823 CopyMem (ChildSaSession
->NiBlock
, IkeSaSession
->NiBlock
, IkeSaSession
->NiBlkSize
);
1825 ChildSaSession
->NrBlock
= AllocateZeroPool (IkeSaSession
->NrBlkSize
);
1826 ASSERT (ChildSaSession
->NrBlock
!= NULL
);
1827 ChildSaSession
->NrBlkSize
= IkeSaSession
->NrBlkSize
;
1828 CopyMem (ChildSaSession
->NrBlock
, IkeSaSession
->NrBlock
, IkeSaSession
->NrBlkSize
);
1831 // Only if the Create Child SA is called for the IKE_INIT Exchange and
1832 // IkeSaSession is initiator (Only Initiator's SPD is not NULL), Set the
1833 // Traffic Selectors related information here.
1835 if (IkeSaSession
->SessionCommon
.State
== IkeStateAuth
&& IkeSaSession
->Spd
!= NULL
) {
1836 ChildSaSession
->ProtoId
= IkeSaSession
->Spd
->Selector
->NextLayerProtocol
;
1837 ChildSaSession
->LocalPort
= IkeSaSession
->Spd
->Selector
->LocalPort
;
1838 ChildSaSession
->RemotePort
= IkeSaSession
->Spd
->Selector
->RemotePort
;
1842 // Insert the new ChildSaSession into processing child SA list.
1844 Ikev2ChildSaSessionInsert (&IkeSaSession
->ChildSaSessionList
, ChildSaSession
);
1845 return ChildSaSession
;
1849 Check if the SPD is related to the input Child SA Session.
1851 This function is the subfunction of Ikev1AssociateSpdEntry(). It is the call
1852 back function of IpSecVisitConfigData().
1855 @param[in] Type Type of the input Config Selector.
1856 @param[in] Selector Pointer to the Configure Selector to be checked.
1857 @param[in] Data Pointer to the Configure Selector's Data passed
1859 @param[in] SelectorSize The buffer size of Selector.
1860 @param[in] DataSize The buffer size of the Data.
1861 @param[in] Context The data passed from the caller. It is a Child
1862 SA Session in this context.
1864 @retval EFI_SUCCESS The SPD Selector is not related to the Child SA Session.
1865 @retval EFI_ABORTED The SPD Selector is related to the Child SA session and
1866 set the ChildSaSession->Spd to point to this SPD Selector.
1870 Ikev2MatchSpdEntry (
1871 IN EFI_IPSEC_CONFIG_DATA_TYPE Type
,
1872 IN EFI_IPSEC_CONFIG_SELECTOR
*Selector
,
1874 IN UINTN SelectorSize
,
1879 IKEV2_CHILD_SA_SESSION
*ChildSaSession
;
1880 EFI_IPSEC_SPD_SELECTOR
*SpdSelector
;
1881 EFI_IPSEC_SPD_DATA
*SpdData
;
1885 ASSERT (Type
== IPsecConfigDataTypeSpd
);
1886 SpdData
= (EFI_IPSEC_SPD_DATA
*) Data
;
1888 // Bypass all non-protect SPD entry first
1890 if (SpdData
->Action
!= EfiIPsecActionProtect
) {
1894 ChildSaSession
= (IKEV2_CHILD_SA_SESSION
*) Context
;
1895 IpVersion
= ChildSaSession
->SessionCommon
.UdpService
->IpVersion
;
1896 SpdSelector
= (EFI_IPSEC_SPD_SELECTOR
*) Selector
;
1899 if (SpdSelector
->NextLayerProtocol
== EFI_IP_PROTO_UDP
&&
1900 SpdSelector
->LocalPort
== IKE_DEFAULT_PORT
&&
1901 SpdSelector
->LocalPortRange
== 0 &&
1902 SpdSelector
->RemotePort
== IKE_DEFAULT_PORT
&&
1903 SpdSelector
->RemotePortRange
== 0
1906 // TODO: Skip IKE Policy here or set a SPD entry?
1911 if (SpdSelector
->NextLayerProtocol
!= EFI_IPSEC_ANY_PROTOCOL
&&
1912 SpdSelector
->NextLayerProtocol
!= ChildSaSession
->ProtoId
1917 if (SpdSelector
->LocalPort
!= EFI_IPSEC_ANY_PORT
&& SpdSelector
->LocalPort
!= ChildSaSession
->LocalPort
) {
1921 if (SpdSelector
->RemotePort
!= EFI_IPSEC_ANY_PORT
&& SpdSelector
->RemotePort
!= ChildSaSession
->RemotePort
) {
1925 IsMatch
= (BOOLEAN
) (IsMatch
&&
1926 IpSecMatchIpAddress (
1928 &ChildSaSession
->SessionCommon
.LocalPeerIp
,
1929 SpdSelector
->LocalAddress
,
1930 SpdSelector
->LocalAddressCount
1933 IsMatch
= (BOOLEAN
) (IsMatch
&&
1934 IpSecMatchIpAddress (
1936 &ChildSaSession
->SessionCommon
.RemotePeerIp
,
1937 SpdSelector
->RemoteAddress
,
1938 SpdSelector
->RemoteAddressCount
1942 ChildSaSession
->Spd
= IkeSearchSpdEntry (SpdSelector
);
1950 Check if the Algorithm ID is supported.
1952 @param[in] AlgorithmId The specified Algorithm ID.
1953 @param[in] Type The type used to indicate the Algorithm is for Encrypt or
1956 @retval TRUE If the Algorithm ID is supported.
1957 @retval FALSE If the Algorithm ID is not supported.
1962 IN UINT16 AlgorithmId
,
1968 case IKE_ENCRYPT_TYPE
:
1969 for (Index
= 0; Index
< IKEV2_SUPPORT_ENCRYPT_ALGORITHM_NUM
; Index
++) {
1970 if (mIkev2EncryptAlgorithmList
[Index
] == AlgorithmId
) {
1976 case IKE_AUTH_TYPE
:
1977 for (Index
= 0; Index
< IKEV2_SUPPORT_AUTH_ALGORITHM_NUM
; Index
++) {
1978 if (mIkev2AuthAlgorithmList
[Index
] == AlgorithmId
) {
1985 for (Index
= 0; Index
< IKEV2_SUPPORT_DH_ALGORITHM_NUM
; Index
++) {
1986 if (mIkev2DhGroupAlgorithmList
[Index
] == AlgorithmId
) {
1993 for (Index
= 0; Index
< IKEV2_SUPPORT_PRF_ALGORITHM_NUM
; Index
++) {
1994 if (mIkev2PrfAlgorithmList
[Index
] == AlgorithmId
) {
2003 Get the preferred algorithm types from ProposalData.
2005 @param[in] ProposalData Pointer to related IKEV2_PROPOSAL_DATA.
2006 @param[out] PreferEncryptAlgorithm Output of preferred encrypt algorithm.
2007 @param[out] PreferIntegrityAlgorithm Output of preferred integrity algorithm.
2008 @param[out] PreferPrfAlgorithm Output of preferred PRF algorithm. Only
2010 @param[out] PreferDhGroup Output of preferred DH group. Only for
2012 @param[out] PreferEncryptKeylength Output of preferred encrypt key length
2014 @param[out] IsSupportEsn Output of value about the Extented Sequence
2015 Number is support or not. Only for Child SA.
2016 @param[in] IsChildSa If it is ture, the ProposalData is for IKE
2017 SA. Otherwise the proposalData is for Child SA.
2021 Ikev2ParseProposalData (
2022 IN IKEV2_PROPOSAL_DATA
*ProposalData
,
2023 OUT UINT16
*PreferEncryptAlgorithm
,
2024 OUT UINT16
*PreferIntegrityAlgorithm
,
2025 OUT UINT16
*PreferPrfAlgorithm
,
2026 OUT UINT16
*PreferDhGroup
,
2027 OUT UINTN
*PreferEncryptKeylength
,
2028 OUT BOOLEAN
*IsSupportEsn
,
2029 IN BOOLEAN IsChildSa
2032 IKEV2_TRANSFORM_DATA
*TransformData
;
2033 UINT8 TransformIndex
;
2036 // Check input parameters.
2038 if (ProposalData
== NULL
||
2039 PreferEncryptAlgorithm
== NULL
||
2040 PreferIntegrityAlgorithm
== NULL
||
2041 PreferEncryptKeylength
== NULL
2047 if (IsSupportEsn
== NULL
) {
2051 if (PreferPrfAlgorithm
== NULL
|| PreferDhGroup
== NULL
) {
2056 TransformData
= (IKEV2_TRANSFORM_DATA
*)(ProposalData
+ 1);
2057 for (TransformIndex
= 0; TransformIndex
< ProposalData
->NumTransforms
; TransformIndex
++) {
2058 switch (TransformData
->TransformType
) {
2060 // For IKE SA there are four algorithm types. Encryption Algorithm, Pseudo-random Function,
2061 // Integrity Algorithm, Diffie-Hellman Group. For Child SA, there are three algorithm types.
2062 // Encryption Algorithm, Integrity Algorithm, Extended Sequence Number.
2064 case IKEV2_TRANSFORM_TYPE_ENCR
:
2065 if (*PreferEncryptAlgorithm
== 0 && Ikev2IsSupportAlg (TransformData
->TransformId
, IKE_ENCRYPT_TYPE
)) {
2067 // Check the attribute value. According to RFC, only Keylength is support.
2069 if (TransformData
->Attribute
.AttrType
== IKEV2_ATTRIBUTE_TYPE_KEYLEN
) {
2071 // If the Keylength is not support, continue to check the next one.
2073 if (IpSecGetEncryptKeyLength ((UINT8
)TransformData
->TransformId
) != (UINTN
)(TransformData
->Attribute
.Attr
.AttrValue
>> 3)){
2076 *PreferEncryptKeylength
= TransformData
->Attribute
.Attr
.AttrValue
;
2079 *PreferEncryptAlgorithm
= TransformData
->TransformId
;
2083 case IKEV2_TRANSFORM_TYPE_PRF
:
2085 if (*PreferPrfAlgorithm
== 0 && Ikev2IsSupportAlg (TransformData
->TransformId
, IKE_PRF_TYPE
)) {
2086 *PreferPrfAlgorithm
= TransformData
->TransformId
;
2091 case IKEV2_TRANSFORM_TYPE_INTEG
:
2092 if (*PreferIntegrityAlgorithm
== 0 && Ikev2IsSupportAlg (TransformData
->TransformId
, IKE_AUTH_TYPE
)) {
2093 *PreferIntegrityAlgorithm
= TransformData
->TransformId
;
2097 case IKEV2_TRANSFORM_TYPE_DH
:
2099 if (*PreferDhGroup
== 0 && Ikev2IsSupportAlg (TransformData
->TransformId
, IKE_DH_TYPE
)) {
2100 *PreferDhGroup
= TransformData
->TransformId
;
2105 case IKEV2_TRANSFORM_TYPE_ESN
:
2107 if (TransformData
->TransformId
!= 0) {
2108 *IsSupportEsn
= TRUE
;
2116 TransformData
= (IKEV2_TRANSFORM_DATA
*)(TransformData
+ 1);
2121 Parse the received Initial Exchange Packet.
2123 This function parse the SA Payload and Key Payload to find out the cryptographic
2124 suite for the further IKE negotiation and fill it into the IKE SA Session's
2125 CommonSession->SaParams.
2127 @param[in, out] IkeSaSession Pointer to related IKEV2_SA_SESSION.
2128 @param[in] SaPayload The received packet.
2129 @param[in] Type The received packet IKE header flag.
2131 @retval TRUE If the SA proposal in Packet is acceptable.
2132 @retval FALSE If the SA proposal in Packet is not acceptable.
2136 Ikev2SaParseSaPayload (
2137 IN OUT IKEV2_SA_SESSION
*IkeSaSession
,
2138 IN IKE_PAYLOAD
*SaPayload
,
2142 IKEV2_PROPOSAL_DATA
*ProposalData
;
2143 UINT8 ProposalIndex
;
2144 UINT16 PreferEncryptAlgorithm
;
2145 UINT16 PreferIntegrityAlgorithm
;
2146 UINT16 PreferPrfAlgorithm
;
2147 UINT16 PreferDhGroup
;
2148 UINTN PreferEncryptKeylength
;
2149 UINT16 EncryptAlgorithm
;
2150 UINT16 IntegrityAlgorithm
;
2151 UINT16 PrfAlgorithm
;
2153 UINTN EncryptKeylength
;
2157 PreferPrfAlgorithm
= 0;
2158 PreferIntegrityAlgorithm
= 0;
2160 PreferEncryptAlgorithm
= 0;
2161 PreferEncryptKeylength
= 0;
2163 IntegrityAlgorithm
= 0;
2165 EncryptAlgorithm
= 0;
2166 EncryptKeylength
= 0;
2169 if (Type
== IKE_HEADER_FLAGS_INIT
) {
2170 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((IKEV2_SA_DATA
*)SaPayload
->PayloadBuf
+ 1);
2171 for (ProposalIndex
= 0; ProposalIndex
< ((IKEV2_SA_DATA
*)SaPayload
->PayloadBuf
)->NumProposals
; ProposalIndex
++) {
2173 // Iterate each proposal to find the perfered one.
2175 if (ProposalData
->ProtocolId
== IPSEC_PROTO_ISAKMP
&& ProposalData
->NumTransforms
>= 4) {
2177 // Get the preferred algorithms.
2179 Ikev2ParseProposalData (
2181 &PreferEncryptAlgorithm
,
2182 &PreferIntegrityAlgorithm
,
2183 &PreferPrfAlgorithm
,
2185 &PreferEncryptKeylength
,
2190 if (PreferEncryptAlgorithm
!= 0 &&
2191 PreferIntegrityAlgorithm
!= 0 &&
2192 PreferPrfAlgorithm
!= 0 &&
2196 // Find the matched one.
2198 IkeSaSession
->SessionCommon
.SaParams
= AllocateZeroPool (sizeof (IKEV2_SA_PARAMS
));
2199 ASSERT (IkeSaSession
->SessionCommon
.SaParams
!= NULL
);
2200 IkeSaSession
->SessionCommon
.SaParams
->EncAlgId
= PreferEncryptAlgorithm
;
2201 IkeSaSession
->SessionCommon
.SaParams
->EnckeyLen
= PreferEncryptKeylength
;
2202 IkeSaSession
->SessionCommon
.SaParams
->DhGroup
= PreferDhGroup
;
2203 IkeSaSession
->SessionCommon
.SaParams
->Prf
= PreferPrfAlgorithm
;
2204 IkeSaSession
->SessionCommon
.SaParams
->IntegAlgId
= PreferIntegrityAlgorithm
;
2205 IkeSaSession
->SessionCommon
.PreferDhGroup
= PreferDhGroup
;
2208 // Save the matched one in IKEV2_SA_DATA for furthure calculation.
2210 SaDataSize
= sizeof (IKEV2_SA_DATA
) +
2211 sizeof (IKEV2_PROPOSAL_DATA
) +
2212 sizeof (IKEV2_TRANSFORM_DATA
) * 4;
2213 IkeSaSession
->SaData
= AllocateZeroPool (SaDataSize
);
2214 ASSERT (IkeSaSession
->SaData
!= NULL
);
2216 IkeSaSession
->SaData
->NumProposals
= 1;
2219 // BUGBUG: Suppose the matched proposal only has 4 transforms. If
2220 // The matched Proposal has more than 4 transforms means it contains
2221 // one than one transform with same type.
2224 (IKEV2_PROPOSAL_DATA
*) (IkeSaSession
->SaData
+ 1),
2226 SaDataSize
- sizeof (IKEV2_SA_DATA
)
2229 ((IKEV2_PROPOSAL_DATA
*) (IkeSaSession
->SaData
+ 1))->ProposalIndex
= 1;
2232 PreferEncryptAlgorithm
= 0;
2233 PreferIntegrityAlgorithm
= 0;
2234 PreferPrfAlgorithm
= 0;
2236 PreferEncryptKeylength
= 0;
2240 // Point to next Proposal.
2242 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((UINT8
*)(ProposalData
+ 1) +
2243 ProposalData
->NumTransforms
* sizeof (IKEV2_TRANSFORM_DATA
));
2245 } else if (Type
== IKE_HEADER_FLAGS_RESPOND
) {
2247 // First check the SA proposal's ProtoctolID and Transform Numbers. Since it is
2248 // the responded SA proposal, suppose it only has one proposal and the transform Numbers
2251 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((IKEV2_SA_DATA
*) SaPayload
->PayloadBuf
+ 1);
2252 if (ProposalData
->ProtocolId
!= IPSEC_PROTO_ISAKMP
|| ProposalData
->NumTransforms
!= 4) {
2256 // Get the preferred algorithms.
2258 Ikev2ParseProposalData (
2260 &PreferEncryptAlgorithm
,
2261 &PreferIntegrityAlgorithm
,
2262 &PreferPrfAlgorithm
,
2264 &PreferEncryptKeylength
,
2269 // Check if the Sa proposal data from received packet is in the IkeSaSession->SaData.
2271 ProposalData
= (IKEV2_PROPOSAL_DATA
*) (IkeSaSession
->SaData
+ 1);
2273 for (ProposalIndex
= 0; ProposalIndex
< IkeSaSession
->SaData
->NumProposals
&& (!IsMatch
); ProposalIndex
++) {
2274 Ikev2ParseProposalData (
2277 &IntegrityAlgorithm
,
2284 if (EncryptAlgorithm
== PreferEncryptAlgorithm
&&
2285 EncryptKeylength
== PreferEncryptKeylength
&&
2286 IntegrityAlgorithm
== PreferIntegrityAlgorithm
&&
2287 PrfAlgorithm
== PreferPrfAlgorithm
&&
2288 DhGroup
== PreferDhGroup
2292 EncryptAlgorithm
= 0;
2293 IntegrityAlgorithm
= 0;
2296 EncryptKeylength
= 0;
2299 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((UINT8
*)(ProposalData
+ 1) +
2300 ProposalData
->NumTransforms
* sizeof (IKEV2_TRANSFORM_DATA
));
2304 IkeSaSession
->SessionCommon
.SaParams
= AllocateZeroPool (sizeof (IKEV2_SA_PARAMS
));
2305 ASSERT (IkeSaSession
->SessionCommon
.SaParams
!= NULL
);
2306 IkeSaSession
->SessionCommon
.SaParams
->EncAlgId
= PreferEncryptAlgorithm
;
2307 IkeSaSession
->SessionCommon
.SaParams
->EnckeyLen
= PreferEncryptKeylength
;
2308 IkeSaSession
->SessionCommon
.SaParams
->DhGroup
= PreferDhGroup
;
2309 IkeSaSession
->SessionCommon
.SaParams
->Prf
= PreferPrfAlgorithm
;
2310 IkeSaSession
->SessionCommon
.SaParams
->IntegAlgId
= PreferIntegrityAlgorithm
;
2311 IkeSaSession
->SessionCommon
.PreferDhGroup
= PreferDhGroup
;
2320 Parse the received Authentication Exchange Packet.
2322 This function parse the SA Payload and Key Payload to find out the cryptographic
2323 suite for the ESP and fill it into the Child SA Session's CommonSession->SaParams.
2325 @param[in, out] ChildSaSession Pointer to IKEV2_CHILD_SA_SESSION related to
2326 this Authentication Exchange.
2327 @param[in] SaPayload The received packet.
2328 @param[in] Type The IKE header's flag of received packet .
2330 @retval TRUE If the SA proposal in Packet is acceptable.
2331 @retval FALSE If the SA proposal in Packet is not acceptable.
2335 Ikev2ChildSaParseSaPayload (
2336 IN OUT IKEV2_CHILD_SA_SESSION
*ChildSaSession
,
2337 IN IKE_PAYLOAD
*SaPayload
,
2341 IKEV2_PROPOSAL_DATA
*ProposalData
;
2342 UINT8 ProposalIndex
;
2343 UINT16 PreferEncryptAlgorithm
;
2344 UINT16 PreferIntegrityAlgorithm
;
2345 UINTN PreferEncryptKeylength
;
2346 BOOLEAN PreferIsSupportEsn
;
2347 UINT16 EncryptAlgorithm
;
2348 UINT16 IntegrityAlgorithm
;
2349 UINTN EncryptKeylength
;
2350 BOOLEAN IsSupportEsn
;
2355 PreferIntegrityAlgorithm
= 0;
2356 PreferEncryptAlgorithm
= 0;
2357 PreferEncryptKeylength
= 0;
2358 IntegrityAlgorithm
= 0;
2359 EncryptAlgorithm
= 0;
2360 EncryptKeylength
= 0;
2362 IsSupportEsn
= FALSE
;
2363 PreferIsSupportEsn
= FALSE
;
2365 if (Type
== IKE_HEADER_FLAGS_INIT
) {
2366 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((IKEV2_SA_DATA
*) SaPayload
->PayloadBuf
+ 1);
2367 for (ProposalIndex
= 0; ProposalIndex
< ((IKEV2_SA_DATA
*) SaPayload
->PayloadBuf
)->NumProposals
; ProposalIndex
++) {
2369 // Iterate each proposal to find the preferred one.
2371 if (ProposalData
->ProtocolId
== IPSEC_PROTO_IPSEC_ESP
&& ProposalData
->NumTransforms
>= 3) {
2373 // Get the preferred algorithm.
2375 Ikev2ParseProposalData (
2377 &PreferEncryptAlgorithm
,
2378 &PreferIntegrityAlgorithm
,
2381 &PreferEncryptKeylength
,
2386 // Don't support the ESN now.
2388 if (PreferEncryptAlgorithm
!= 0 &&
2389 PreferIntegrityAlgorithm
!= 0 &&
2393 // Find the matched one.
2395 ChildSaSession
->SessionCommon
.SaParams
= AllocateZeroPool (sizeof (IKEV2_SA_PARAMS
));
2396 ASSERT (ChildSaSession
->SessionCommon
.SaParams
!= NULL
);
2397 ChildSaSession
->SessionCommon
.SaParams
->EncAlgId
= PreferEncryptAlgorithm
;
2398 ChildSaSession
->SessionCommon
.SaParams
->EnckeyLen
= PreferEncryptKeylength
;
2399 ChildSaSession
->SessionCommon
.SaParams
->IntegAlgId
= PreferIntegrityAlgorithm
;
2400 CopyMem (&ChildSaSession
->RemotePeerSpi
, ProposalData
->Spi
, sizeof (ChildSaSession
->RemotePeerSpi
));
2403 // Save the matched one in IKEV2_SA_DATA for furthure calculation.
2405 SaDataSize
= sizeof (IKEV2_SA_DATA
) +
2406 sizeof (IKEV2_PROPOSAL_DATA
) +
2407 sizeof (IKEV2_TRANSFORM_DATA
) * 4;
2409 ChildSaSession
->SaData
= AllocateZeroPool (SaDataSize
);
2410 ASSERT (ChildSaSession
->SaData
!= NULL
);
2412 ChildSaSession
->SaData
->NumProposals
= 1;
2415 // BUGBUG: Suppose there are 4 transforms in the matched proposal. If
2416 // the matched Proposal has more than 4 transforms that means there
2417 // are more than one transform with same type.
2420 (IKEV2_PROPOSAL_DATA
*) (ChildSaSession
->SaData
+ 1),
2422 SaDataSize
- sizeof (IKEV2_SA_DATA
)
2425 ((IKEV2_PROPOSAL_DATA
*) (ChildSaSession
->SaData
+ 1))->ProposalIndex
= 1;
2427 ((IKEV2_PROPOSAL_DATA
*) (ChildSaSession
->SaData
+ 1))->Spi
= AllocateCopyPool (
2428 sizeof (ChildSaSession
->LocalPeerSpi
),
2429 &ChildSaSession
->LocalPeerSpi
2431 ASSERT (((IKEV2_PROPOSAL_DATA
*) (ChildSaSession
->SaData
+ 1))->Spi
!= NULL
);
2435 PreferEncryptAlgorithm
= 0;
2436 PreferIntegrityAlgorithm
= 0;
2437 IsSupportEsn
= TRUE
;
2441 // Point to next Proposal
2443 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((UINT8
*)(ProposalData
+ 1) +
2444 ProposalData
->NumTransforms
* sizeof (IKEV2_TRANSFORM_DATA
));
2446 } else if (Type
== IKE_HEADER_FLAGS_RESPOND
) {
2448 // First check the SA proposal's ProtoctolID and Transform Numbers. Since it is
2449 // the responded SA proposal, suppose it only has one proposal and the transform Numbers
2452 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((IKEV2_SA_DATA
*)SaPayload
->PayloadBuf
+ 1);
2453 if (ProposalData
->ProtocolId
!= IPSEC_PROTO_IPSEC_ESP
|| ProposalData
->NumTransforms
!= 3) {
2457 // Get the preferred algorithms.
2459 Ikev2ParseProposalData (
2461 &PreferEncryptAlgorithm
,
2462 &PreferIntegrityAlgorithm
,
2465 &PreferEncryptKeylength
,
2466 &PreferIsSupportEsn
,
2470 ProposalData
= (IKEV2_PROPOSAL_DATA
*) (ChildSaSession
->SaData
+ 1);
2472 for (ProposalIndex
= 0; ProposalIndex
< ChildSaSession
->SaData
->NumProposals
&& (!IsMatch
); ProposalIndex
++) {
2473 Ikev2ParseProposalData (
2476 &IntegrityAlgorithm
,
2483 if (EncryptAlgorithm
== PreferEncryptAlgorithm
&&
2484 EncryptKeylength
== PreferEncryptKeylength
&&
2485 IntegrityAlgorithm
== PreferIntegrityAlgorithm
&&
2486 IsSupportEsn
== PreferIsSupportEsn
2490 PreferEncryptAlgorithm
= 0;
2491 PreferIntegrityAlgorithm
= 0;
2492 IsSupportEsn
= TRUE
;
2494 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((UINT8
*)(ProposalData
+ 1) +
2495 ProposalData
->NumTransforms
* sizeof (IKEV2_TRANSFORM_DATA
));
2498 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((IKEV2_SA_DATA
*)SaPayload
->PayloadBuf
+ 1);
2500 ChildSaSession
->SessionCommon
.SaParams
= AllocateZeroPool (sizeof (IKEV2_SA_PARAMS
));
2501 ASSERT (ChildSaSession
->SessionCommon
.SaParams
!= NULL
);
2502 ChildSaSession
->SessionCommon
.SaParams
->EncAlgId
= PreferEncryptAlgorithm
;
2503 ChildSaSession
->SessionCommon
.SaParams
->EnckeyLen
= PreferEncryptKeylength
;
2504 ChildSaSession
->SessionCommon
.SaParams
->IntegAlgId
= PreferIntegrityAlgorithm
;
2505 CopyMem (&ChildSaSession
->RemotePeerSpi
, ProposalData
->Spi
, sizeof (ChildSaSession
->RemotePeerSpi
));
2514 Generate Key buffer from fragments.
2516 If the digest length of specified HashAlgId is larger than or equal with the
2517 required output key length, derive the key directly. Otherwise, Key Material
2518 needs to be PRF-based concatenation according to 2.13 of RFC 4306:
2519 prf+ (K,S) = T1 | T2 | T3 | T4 | ..., T1 = prf (K, S | 0x01),
2520 T2 = prf (K, T1 | S | 0x02), T3 = prf (K, T2 | S | 0x03),T4 = prf (K, T3 | S | 0x04)
2521 then derive the key from this key material.
2523 @param[in] HashAlgId The Hash Algorithm ID used to generate key.
2524 @param[in] HashKey Pointer to a key buffer which contains hash key.
2525 @param[in] HashKeyLength The length of HashKey in bytes.
2526 @param[in, out] OutputKey Pointer to buffer which is used to receive the
2528 @param[in] OutputKeyLength The length of OutPutKey buffer.
2529 @param[in] Fragments Pointer to the data to be used to generate key.
2530 @param[in] NumFragments The numbers of the Fragement.
2532 @retval EFI_SUCCESS The operation complete successfully.
2533 @retval EFI_INVALID_PARAMETER If NumFragments is zero.
2534 @retval EFI_OUT_OF_RESOURCES If the required resource can't be allocated.
2535 @retval Others The operation is failed.
2539 Ikev2SaGenerateKey (
2542 IN UINTN HashKeyLength
,
2543 IN OUT UINT8
*OutputKey
,
2544 IN UINTN OutputKeyLength
,
2545 IN PRF_DATA_FRAGMENT
*Fragments
,
2546 IN UINTN NumFragments
2550 PRF_DATA_FRAGMENT LocalFragments
[3];
2555 UINTN AuthKeyLength
;
2556 UINTN FragmentsSize
;
2559 Status
= EFI_SUCCESS
;
2561 if (NumFragments
== 0) {
2562 return EFI_INVALID_PARAMETER
;
2565 LocalFragments
[0].Data
= NULL
;
2566 LocalFragments
[1].Data
= NULL
;
2567 LocalFragments
[2].Data
= NULL
;
2569 AuthKeyLength
= IpSecGetHmacDigestLength (HashAlgId
);
2570 DigestSize
= AuthKeyLength
;
2571 Digest
= AllocateZeroPool (AuthKeyLength
);
2573 if (Digest
== NULL
) {
2574 return EFI_OUT_OF_RESOURCES
;
2577 // If the required output key length is less than the digest size,
2578 // copy the digest into OutputKey.
2580 if (OutputKeyLength
<= DigestSize
) {
2581 Status
= IpSecCryptoIoHmac (
2585 (HASH_DATA_FRAGMENT
*) Fragments
,
2590 if (EFI_ERROR (Status
)) {
2594 CopyMem (OutputKey
, Digest
, OutputKeyLength
);
2599 //Otherwise, Key Material need to be PRF-based concatenation according to 2.13
2600 //of RFC 4306: prf+ (K,S) = T1 | T2 | T3 | T4 | ..., T1 = prf (K, S | 0x01),
2601 //T2 = prf (K, T1 | S | 0x02), T3 = prf (K, T2 | S | 0x03),T4 = prf (K, T3 | S | 0x04)
2602 //then derive the key from this key material.
2605 for (Index
= 0; Index
< NumFragments
; Index
++) {
2606 FragmentsSize
= FragmentsSize
+ Fragments
[Index
].DataSize
;
2609 LocalFragments
[1].Data
= AllocateZeroPool (FragmentsSize
);
2610 ASSERT (LocalFragments
[1].Data
!= NULL
);
2611 LocalFragments
[1].DataSize
= FragmentsSize
;
2614 // Copy all input fragments into LocalFragments[1];
2617 for (Index
= 0; Index
< NumFragments
; Index
++) {
2619 LocalFragments
[1].Data
+ FragmentsSize
,
2620 Fragments
[Index
].Data
,
2621 Fragments
[Index
].DataSize
2623 FragmentsSize
= FragmentsSize
+ Fragments
[Index
].DataSize
;
2627 // Prepare 0x01 as the first tail data.
2630 LocalFragments
[2].Data
= &TailData
;
2631 LocalFragments
[2].DataSize
= sizeof (TailData
);
2633 // Allocate buffer for the first fragment
2635 LocalFragments
[0].Data
= AllocateZeroPool (AuthKeyLength
);
2636 ASSERT (LocalFragments
[0].Data
!= NULL
);
2637 LocalFragments
[0].DataSize
= AuthKeyLength
;
2639 Round
= (OutputKeyLength
- 1) / AuthKeyLength
+ 1;
2640 for (Index
= 0; Index
< Round
; Index
++) {
2641 Status
= IpSecCryptoIoHmac (
2645 (HASH_DATA_FRAGMENT
*)(Index
== 0 ? &LocalFragments
[1] : LocalFragments
),
2650 if (EFI_ERROR(Status
)) {
2654 LocalFragments
[0].Data
,
2658 if (OutputKeyLength
> DigestSize
* (Index
+ 1)) {
2660 OutputKey
+ Index
* DigestSize
,
2664 LocalFragments
[0].DataSize
= DigestSize
;
2671 OutputKey
+ Index
* DigestSize
,
2673 OutputKeyLength
- Index
* DigestSize
2680 // Only First and second Framgement Data need to be freed.
2682 for (Index
= 0 ; Index
< 2; Index
++) {
2683 if (LocalFragments
[Index
].Data
!= NULL
) {
2684 FreePool (LocalFragments
[Index
].Data
);
2687 if (Digest
!= NULL
) {