2 The Common operations used by IKE Exchange Process.
4 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2010 - 2015, 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 ASSERT (IkeSaSession
!= NULL
);
63 // Initialize the fields of IkeSaSession and its SessionCommon.
65 IkeSaSession
->NCookie
= NULL
;
66 IkeSaSession
->Signature
= IKEV2_SA_SESSION_SIGNATURE
;
67 IkeSaSession
->InitiatorCookie
= IkeGenerateCookie ();
68 IkeSaSession
->ResponderCookie
= 0;
70 // BUGBUG: Message ID starts from 2 is to match the OpenSwan requirement, but it
71 // might not match the IPv6 Logo. In its test specification, it mentions that
72 // the Message ID should start from zero after the IKE_SA_INIT exchange.
74 IkeSaSession
->MessageId
= 2;
75 SessionCommon
= &IkeSaSession
->SessionCommon
;
76 SessionCommon
->UdpService
= UdpService
;
77 SessionCommon
->Private
= Private
;
78 SessionCommon
->IkeSessionType
= IkeSessionTypeIkeSa
;
79 SessionCommon
->IkeVer
= 2;
80 SessionCommon
->AfterEncodePayload
= NULL
;
81 SessionCommon
->BeforeDecodePayload
= NULL
;
84 // Create a resend notfiy event for retry.
86 Status
= gBS
->CreateEvent (
87 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
91 &SessionCommon
->TimeoutEvent
94 if (EFI_ERROR (Status
)) {
95 FreePool (IkeSaSession
);
100 // Initialize the lists in IkeSaSession.
102 InitializeListHead (&IkeSaSession
->ChildSaSessionList
);
103 InitializeListHead (&IkeSaSession
->ChildSaEstablishSessionList
);
104 InitializeListHead (&IkeSaSession
->InfoMIDList
);
105 InitializeListHead (&IkeSaSession
->DeleteSaList
);
111 Register the established IKEv2 SA into Private->Ikev2EstablishedList. If there is
112 IKEV2_SA_SESSION with same remote peer IP, remove the old one then register the
115 @param[in] IkeSaSession Pointer to IKEV2_SA_SESSION to be registered.
116 @param[in] Private Pointer to IPSEC_PRAVATE_DATA.
121 IN IKEV2_SA_SESSION
*IkeSaSession
,
122 IN IPSEC_PRIVATE_DATA
*Private
125 IKEV2_SESSION_COMMON
*SessionCommon
;
126 IKEV2_SA_SESSION
*OldIkeSaSession
;
131 // Keep IKE SA exclusive to remote ip address.
133 SessionCommon
= &IkeSaSession
->SessionCommon
;
134 OldIkeSaSession
= Ikev2SaSessionRemove (&Private
->Ikev2EstablishedList
, &SessionCommon
->RemotePeerIp
);
135 if (OldIkeSaSession
!= NULL
) {
137 // TODO: It should delete all child SAs if rekey the IKE SA.
139 Ikev2SaSessionFree (OldIkeSaSession
);
143 // Cleanup the fields of SessionCommon for processing.
145 Ikev2SessionCommonRefresh (SessionCommon
);
148 // Insert the ready IKE SA session into established list.
150 Ikev2SaSessionInsert (&Private
->Ikev2EstablishedList
, IkeSaSession
, &SessionCommon
->RemotePeerIp
);
153 // Create a notfiy event for the IKE SA life time counting.
155 Status
= gBS
->CreateEvent (
156 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
160 &SessionCommon
->TimeoutEvent
162 if (EFI_ERROR(Status
)){
164 // If TimerEvent creation failed, the SA will be alive untill user disable it or
165 // receiving a Delete Payload from peer.
171 // Start to count the lifetime of the IKE SA.
173 if (IkeSaSession
->Spd
->Data
->ProcessingPolicy
->SaLifetime
.HardLifetime
== 0) {
174 Lifetime
= IKE_SA_DEFAULT_LIFETIME
;
176 Lifetime
= IkeSaSession
->Spd
->Data
->ProcessingPolicy
->SaLifetime
.HardLifetime
;
179 Status
= gBS
->SetTimer (
180 SessionCommon
->TimeoutEvent
,
182 MultU64x32(Lifetime
, 10000000) // ms->100ns
184 if (EFI_ERROR(Status
)){
186 // If SetTimer failed, the SA will be alive untill user disable it or
187 // receiving a Delete Payload from peer.
194 "\n------IkeSa established and start to count down %d seconds lifetime\n",
202 Find a IKEV2_SA_SESSION by the remote peer IP.
204 @param[in] SaSessionList SaSession List to be searched.
205 @param[in] RemotePeerIp Pointer to specified IP address.
207 @return Pointer to IKEV2_SA_SESSION if find one or NULL.
211 Ikev2SaSessionLookup (
212 IN LIST_ENTRY
*SaSessionList
,
213 IN EFI_IP_ADDRESS
*RemotePeerIp
217 IKEV2_SA_SESSION
*IkeSaSession
;
219 NET_LIST_FOR_EACH (Entry
, SaSessionList
) {
220 IkeSaSession
= IKEV2_SA_SESSION_BY_SESSION (Entry
);
223 &IkeSaSession
->SessionCommon
.RemotePeerIp
,
225 sizeof (EFI_IP_ADDRESS
)
236 Insert a IKE_SA_SESSION into IkeSaSession list. The IkeSaSession list is either
237 Private->Ikev2SaSession list or Private->Ikev2EstablishedList list.
239 @param[in] SaSessionList Pointer to list to be inserted into.
240 @param[in] IkeSaSession Pointer to IKEV2_SA_SESSION to be inserted.
241 @param[in] RemotePeerIp Pointer to EFI_IP_ADDRESSS to indicate the
242 unique IKEV2_SA_SESSION.
246 Ikev2SaSessionInsert (
247 IN LIST_ENTRY
*SaSessionList
,
248 IN IKEV2_SA_SESSION
*IkeSaSession
,
249 IN EFI_IP_ADDRESS
*RemotePeerIp
252 Ikev2SaSessionRemove (SaSessionList
, RemotePeerIp
);
253 InsertTailList (SaSessionList
, &IkeSaSession
->BySessionTable
);
257 Remove the SA Session by Remote Peer IP.
259 @param[in] SaSessionList Pointer to list to be searched.
260 @param[in] RemotePeerIp Pointer to EFI_IP_ADDRESS to use for SA Session search.
262 @retval Pointer to IKEV2_SA_SESSION with the specified remote IP address or NULL.
266 Ikev2SaSessionRemove (
267 IN LIST_ENTRY
*SaSessionList
,
268 IN EFI_IP_ADDRESS
*RemotePeerIp
272 IKEV2_SA_SESSION
*IkeSaSession
;
274 NET_LIST_FOR_EACH (Entry
, SaSessionList
) {
275 IkeSaSession
= IKEV2_SA_SESSION_BY_SESSION (Entry
);
278 &IkeSaSession
->SessionCommon
.RemotePeerIp
,
280 sizeof (EFI_IP_ADDRESS
)
283 RemoveEntryList (Entry
);
292 Marking a SA session as on deleting.
294 @param[in] IkeSaSession Pointer to IKEV2_SA_SESSION.
296 @retval EFI_SUCCESS Find the related SA session and marked it.
300 Ikev2SaSessionOnDeleting (
301 IN IKEV2_SA_SESSION
*IkeSaSession
308 Free specified Seession Common. The session common would belong to a IKE SA or
311 @param[in] SessionCommon Pointer to a Session Common.
315 Ikev2SaSessionCommonFree (
316 IN IKEV2_SESSION_COMMON
*SessionCommon
320 ASSERT (SessionCommon
!= NULL
);
322 if (SessionCommon
->LastSentPacket
!= NULL
) {
323 IkePacketFree (SessionCommon
->LastSentPacket
);
326 if (SessionCommon
->SaParams
!= NULL
) {
327 FreePool (SessionCommon
->SaParams
);
329 if (SessionCommon
->TimeoutEvent
!= NULL
) {
330 gBS
->CloseEvent (SessionCommon
->TimeoutEvent
);
335 After IKE/Child SA is estiblished, close the time event and free sent packet.
337 @param[in] SessionCommon Pointer to a Session Common.
341 Ikev2SessionCommonRefresh (
342 IN IKEV2_SESSION_COMMON
*SessionCommon
345 ASSERT (SessionCommon
!= NULL
);
347 gBS
->CloseEvent (SessionCommon
->TimeoutEvent
);
348 SessionCommon
->TimeoutEvent
= NULL
;
349 SessionCommon
->TimeoutInterval
= 0;
350 SessionCommon
->RetryCount
= 0;
351 if (SessionCommon
->LastSentPacket
!= NULL
) {
352 IkePacketFree (SessionCommon
->LastSentPacket
);
353 SessionCommon
->LastSentPacket
= NULL
;
359 Free specified IKEV2 SA Session.
361 @param[in] IkeSaSession Pointer to IKEV2_SA_SESSION to be freed.
366 IN IKEV2_SA_SESSION
*IkeSaSession
369 IKEV2_SESSION_KEYS
*IkeKeys
;
371 IKEV2_CHILD_SA_SESSION
*ChildSa
;
372 IKEV2_DH_BUFFER
*DhBuffer
;
374 ASSERT (IkeSaSession
!= NULL
);
377 // Delete Common Session
379 Ikev2SaSessionCommonFree (&IkeSaSession
->SessionCommon
);
382 // Delete ChildSaEstablish List and SAD
384 for (Entry
= IkeSaSession
->ChildSaEstablishSessionList
.ForwardLink
;
385 Entry
!= &IkeSaSession
->ChildSaEstablishSessionList
;
388 ChildSa
= IKEV2_CHILD_SA_SESSION_BY_IKE_SA (Entry
);
389 Entry
= Entry
->ForwardLink
;
390 Ikev2ChildSaSilentDelete (ChildSa
->IkeSaSession
, ChildSa
->LocalPeerSpi
);
395 // Delete ChildSaSessionList
397 for ( Entry
= IkeSaSession
->ChildSaSessionList
.ForwardLink
;
398 Entry
!= &IkeSaSession
->ChildSaSessionList
;
400 ChildSa
= IKEV2_CHILD_SA_SESSION_BY_IKE_SA (Entry
);
401 Entry
= Entry
->ForwardLink
;
402 RemoveEntryList (Entry
->BackLink
);
403 Ikev2ChildSaSessionFree (ChildSa
);
407 // Delete DhBuffer and Keys
409 if (IkeSaSession
->IkeKeys
!= NULL
) {
410 IkeKeys
= IkeSaSession
->IkeKeys
;
411 DhBuffer
= IkeKeys
->DhBuffer
;
416 Ikev2DhBufferFree (DhBuffer
);
421 if (IkeKeys
->SkAiKey
!= NULL
) {
422 FreePool (IkeKeys
->SkAiKey
);
424 if (IkeKeys
->SkArKey
!= NULL
) {
425 FreePool (IkeKeys
->SkArKey
);
427 if (IkeKeys
->SkdKey
!= NULL
) {
428 FreePool (IkeKeys
->SkdKey
);
430 if (IkeKeys
->SkEiKey
!= NULL
) {
431 FreePool (IkeKeys
->SkEiKey
);
433 if (IkeKeys
->SkErKey
!= NULL
) {
434 FreePool (IkeKeys
->SkErKey
);
436 if (IkeKeys
->SkPiKey
!= NULL
) {
437 FreePool (IkeKeys
->SkPiKey
);
439 if (IkeKeys
->SkPrKey
!= NULL
) {
440 FreePool (IkeKeys
->SkPrKey
);
445 if (IkeSaSession
->SaData
!= NULL
) {
446 FreePool (IkeSaSession
->SaData
);
449 if (IkeSaSession
->NiBlock
!= NULL
) {
450 FreePool (IkeSaSession
->NiBlock
);
453 if (IkeSaSession
->NrBlock
!= NULL
) {
454 FreePool (IkeSaSession
->NrBlock
);
457 if (IkeSaSession
->NCookie
!= NULL
) {
458 FreePool (IkeSaSession
->NCookie
);
461 if (IkeSaSession
->InitPacket
!= NULL
) {
462 FreePool (IkeSaSession
->InitPacket
);
465 if (IkeSaSession
->RespPacket
!= NULL
) {
466 FreePool (IkeSaSession
->RespPacket
);
469 FreePool (IkeSaSession
);
475 Increase the MessageID in IkeSaSession.
477 @param[in] IkeSaSession Pointer to a specified IKEV2_SA_SESSION.
481 Ikev2SaSessionIncreaseMessageId (
482 IN IKEV2_SA_SESSION
*IkeSaSession
485 if (IkeSaSession
->MessageId
< 0xffffffff) {
486 IkeSaSession
->MessageId
++;
489 // TODO: Trigger Rekey process.
495 Allocate memory for IKEV2 Child SA Session.
497 @param[in] UdpService Pointer to IKE_UDP_SERVICE.
498 @param[in] IkeSaSession Pointer to IKEV2_SA_SESSION related to this Child SA
501 @retval Pointer of a new created IKEV2 Child SA Session or NULL.
504 IKEV2_CHILD_SA_SESSION
*
505 Ikev2ChildSaSessionAlloc (
506 IN IKE_UDP_SERVICE
*UdpService
,
507 IN IKEV2_SA_SESSION
*IkeSaSession
511 IKEV2_CHILD_SA_SESSION
*ChildSaSession
;
512 IKEV2_SESSION_COMMON
*ChildSaCommon
;
513 IKEV2_SESSION_COMMON
*SaCommon
;
515 ChildSaSession
= AllocateZeroPool (sizeof (IKEV2_CHILD_SA_SESSION
));
516 if (ChildSaSession
== NULL
) {
521 // Initialize the fields of ChildSaSession and its SessionCommon.
523 ChildSaSession
->Signature
= IKEV2_CHILD_SA_SESSION_SIGNATURE
;
524 ChildSaSession
->IkeSaSession
= IkeSaSession
;
525 ChildSaSession
->MessageId
= IkeSaSession
->MessageId
;
526 ChildSaSession
->LocalPeerSpi
= IkeGenerateSpi ();
527 ChildSaCommon
= &ChildSaSession
->SessionCommon
;
528 ChildSaCommon
->UdpService
= UdpService
;
529 ChildSaCommon
->Private
= IkeSaSession
->SessionCommon
.Private
;
530 ChildSaCommon
->IkeSessionType
= IkeSessionTypeChildSa
;
531 ChildSaCommon
->IkeVer
= 2;
532 ChildSaCommon
->AfterEncodePayload
= Ikev2ChildSaAfterEncodePayload
;
533 ChildSaCommon
->BeforeDecodePayload
= Ikev2ChildSaBeforeDecodePayload
;
534 SaCommon
= &ChildSaSession
->IkeSaSession
->SessionCommon
;
537 // Create a resend notfiy event for retry.
539 Status
= gBS
->CreateEvent (
540 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
544 &ChildSaCommon
->TimeoutEvent
546 if (EFI_ERROR (Status
)) {
547 FreePool (ChildSaSession
);
551 CopyMem (&ChildSaCommon
->LocalPeerIp
, &SaCommon
->LocalPeerIp
, sizeof (EFI_IP_ADDRESS
));
552 CopyMem (&ChildSaCommon
->RemotePeerIp
, &SaCommon
->RemotePeerIp
, sizeof (EFI_IP_ADDRESS
));
554 return ChildSaSession
;
558 Register a established IKEv2 Child SA into IkeSaSession->ChildSaEstablishSessionList.
559 If the there is IKEV2_CHILD_SA_SESSION with same remote peer IP, remove the old one
560 then register the new one.
562 @param[in] ChildSaSession Pointer to IKEV2_CHILD_SA_SESSION to be registered.
563 @param[in] Private Pointer to IPSEC_PRAVATE_DATA.
567 Ikev2ChildSaSessionReg (
568 IN IKEV2_CHILD_SA_SESSION
*ChildSaSession
,
569 IN IPSEC_PRIVATE_DATA
*Private
572 IKEV2_SESSION_COMMON
*SessionCommon
;
573 IKEV2_CHILD_SA_SESSION
*OldChildSaSession
;
574 IKEV2_SA_SESSION
*IkeSaSession
;
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 if (ChildSaSession
->Spd
->Data
->ProcessingPolicy
->SaLifetime
.HardLifetime
!= 0){
628 Lifetime
= ChildSaSession
->Spd
->Data
->ProcessingPolicy
->SaLifetime
.HardLifetime
;
630 Lifetime
= CHILD_SA_DEFAULT_LIFETIME
;
633 Status
= gBS
->SetTimer (
634 SessionCommon
->TimeoutEvent
,
636 MultU64x32(Lifetime
, 10000000) // ms->100ns
638 if (EFI_ERROR(Status
)){
644 "\n------ChildSa established and start to count down %d seconds lifetime\n",
652 Find the ChildSaSession by it's MessagId.
654 @param[in] SaSessionList Pointer to a ChildSaSession List.
655 @param[in] Mid The messageId used to search ChildSaSession.
657 @return Pointer to IKEV2_CHILD_SA_SESSION or NULL.
660 IKEV2_CHILD_SA_SESSION
*
661 Ikev2ChildSaSessionLookupByMid (
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
->MessageId
== Mid
) {
673 return ChildSaSession
;
680 This function find the Child SA by the specified SPI.
682 This functin find a ChildSA session by searching the ChildSaSessionlist of
683 the input IKEV2_SA_SESSION by specified MessageID.
685 @param[in] SaSessionList Pointer to List to be searched.
686 @param[in] Spi Specified SPI.
688 @return Pointer to IKEV2_CHILD_SA_SESSION or NULL.
691 IKEV2_CHILD_SA_SESSION
*
692 Ikev2ChildSaSessionLookupBySpi (
693 IN LIST_ENTRY
*SaSessionList
,
698 IKEV2_CHILD_SA_SESSION
*ChildSaSession
;
700 NET_LIST_FOR_EACH (Entry
, SaSessionList
) {
701 ChildSaSession
= IKEV2_CHILD_SA_SESSION_BY_IKE_SA (Entry
);
703 if (ChildSaSession
->RemotePeerSpi
== Spi
|| ChildSaSession
->LocalPeerSpi
== Spi
) {
704 return ChildSaSession
;
712 Insert a Child SA Session into the specified ChildSa list.
714 @param[in] SaSessionList Pointer to list to be inserted in.
715 @param[in] ChildSaSession Pointer to IKEV2_CHILD_SA_SESSION to be inserted.
719 Ikev2ChildSaSessionInsert (
720 IN LIST_ENTRY
*SaSessionList
,
721 IN IKEV2_CHILD_SA_SESSION
*ChildSaSession
724 InsertTailList (SaSessionList
, &ChildSaSession
->ByIkeSa
);
728 Remove the IKEV2_CHILD_SA_SESSION from IkeSaSessionList.
730 @param[in] SaSessionList The SA Session List to be iterated.
731 @param[in] Spi Spi used to identified the IKEV2_CHILD_SA_SESSION.
732 @param[in] ListType The type of the List to indicate whether it is a
735 @return The point to IKEV2_CHILD_SA_SESSION or NULL.
738 IKEV2_CHILD_SA_SESSION
*
739 Ikev2ChildSaSessionRemove (
740 IN LIST_ENTRY
*SaSessionList
,
746 LIST_ENTRY
*NextEntry
;
747 IKEV2_CHILD_SA_SESSION
*ChildSaSession
;
749 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, SaSessionList
) {
751 if (ListType
== IKEV2_ESTABLISHED_CHILDSA_LIST
|| ListType
== IKEV2_ESTABLISHING_CHILDSA_LIST
) {
752 ChildSaSession
= IKEV2_CHILD_SA_SESSION_BY_IKE_SA (Entry
);
753 } else if (ListType
== IKEV2_DELET_CHILDSA_LIST
) {
754 ChildSaSession
= IKEV2_CHILD_SA_SESSION_BY_DEL_SA (Entry
);
759 if (ChildSaSession
->RemotePeerSpi
== Spi
|| ChildSaSession
->LocalPeerSpi
== Spi
) {
760 RemoveEntryList (Entry
);
761 return ChildSaSession
;
769 Mark a specified Child SA Session as on deleting.
771 @param[in] ChildSaSession Pointer to IKEV2_CHILD_SA_SESSION.
773 @retval EFI_SUCCESS Operation is successful.
777 Ikev2ChildSaSessionOnDeleting (
778 IN IKEV2_CHILD_SA_SESSION
*ChildSaSession
785 Free the memory located for the specified IKEV2_CHILD_SA_SESSION.
787 @param[in] ChildSaSession Pointer to IKEV2_CHILD_SA_SESSION.
791 Ikev2ChildSaSessionFree (
792 IN IKEV2_CHILD_SA_SESSION
*ChildSaSession
795 IKEV2_SESSION_COMMON
*SessionCommon
;
797 SessionCommon
= &ChildSaSession
->SessionCommon
;
798 if (ChildSaSession
->SaData
!= NULL
) {
799 FreePool (ChildSaSession
->SaData
);
802 if (ChildSaSession
->NiBlock
!= NULL
) {
803 FreePool (ChildSaSession
->NiBlock
);
806 if (ChildSaSession
->NrBlock
!= NULL
) {
807 FreePool (ChildSaSession
->NrBlock
);
810 if (ChildSaSession
->ChildKeymats
.LocalPeerInfo
.EspAlgoInfo
.AuthKey
!= NULL
) {
811 FreePool (ChildSaSession
->ChildKeymats
.LocalPeerInfo
.EspAlgoInfo
.AuthKey
);
814 if (ChildSaSession
->ChildKeymats
.LocalPeerInfo
.EspAlgoInfo
.EncKey
!= NULL
) {
815 FreePool (ChildSaSession
->ChildKeymats
.LocalPeerInfo
.EspAlgoInfo
.EncKey
);
818 if (ChildSaSession
->ChildKeymats
.RemotePeerInfo
.EspAlgoInfo
.AuthKey
!= NULL
) {
819 FreePool (ChildSaSession
->ChildKeymats
.RemotePeerInfo
.EspAlgoInfo
.AuthKey
);
822 if (ChildSaSession
->ChildKeymats
.RemotePeerInfo
.EspAlgoInfo
.EncKey
!= NULL
) {
823 FreePool (ChildSaSession
->ChildKeymats
.RemotePeerInfo
.EspAlgoInfo
.EncKey
);
829 Ikev2DhBufferFree (ChildSaSession
->DhBuffer
);
832 // Delete SpdSelector
834 if (ChildSaSession
->SpdSelector
!= NULL
) {
835 if (ChildSaSession
->SpdSelector
->LocalAddress
!= NULL
) {
836 FreePool (ChildSaSession
->SpdSelector
->LocalAddress
);
838 if (ChildSaSession
->SpdSelector
->RemoteAddress
!= NULL
) {
839 FreePool (ChildSaSession
->SpdSelector
->RemoteAddress
);
841 FreePool (ChildSaSession
->SpdSelector
);
843 Ikev2SaSessionCommonFree (SessionCommon
);
844 FreePool (ChildSaSession
);
850 Delete the specified established Child SA.
852 This function delete the Child SA directly and don't send the Information Packet to
855 @param[in] IkeSaSession Pointer to a IKE SA Session used to be searched for.
856 @param[in] Spi SPI used to find the Child SA.
858 @retval EFI_NOT_FOUND Pointer of IKE SA Session is NULL.
859 @retval EFI_NOT_FOUND There is no specified Child SA related with the input
860 SPI under this IKE SA Session.
861 @retval EFI_SUCCESS Delete the Child SA successfully.
865 Ikev2ChildSaSilentDelete (
866 IN IKEV2_SA_SESSION
*IkeSaSession
,
871 EFI_IPSEC_CONFIG_SELECTOR
*Selector
;
873 BOOLEAN IsLocalFound
;
874 BOOLEAN IsRemoteFound
;
877 IKEV2_CHILD_SA_SESSION
*ChildSession
;
878 EFI_IPSEC_CONFIG_SELECTOR
*LocalSelector
;
879 EFI_IPSEC_CONFIG_SELECTOR
*RemoteSelector
;
880 IPSEC_PRIVATE_DATA
*Private
;
882 if (IkeSaSession
== NULL
) {
883 return EFI_NOT_FOUND
;
886 IsLocalFound
= FALSE
;
887 IsRemoteFound
= FALSE
;
889 LocalSelector
= NULL
;
890 RemoteSelector
= NULL
;
892 Private
= IkeSaSession
->SessionCommon
.Private
;
895 // Remove the Established SA from ChildSaEstablishlist.
897 ChildSession
= Ikev2ChildSaSessionRemove(
898 &(IkeSaSession
->ChildSaEstablishSessionList
),
900 IKEV2_ESTABLISHED_CHILDSA_LIST
902 if (ChildSession
== NULL
) {
903 return EFI_NOT_FOUND
;
906 LocalSpi
= ChildSession
->LocalPeerSpi
;
907 RemoteSpi
= ChildSession
->RemotePeerSpi
;
909 SelectorSize
= sizeof (EFI_IPSEC_CONFIG_SELECTOR
);
910 Selector
= AllocateZeroPool (SelectorSize
);
911 ASSERT (Selector
!= NULL
);
916 Status
= EfiIpSecConfigGetNextSelector (
917 &Private
->IpSecConfig
,
918 IPsecConfigDataTypeSad
,
922 if (Status
== EFI_BUFFER_TOO_SMALL
) {
925 Selector
= AllocateZeroPool (SelectorSize
);
926 ASSERT (Selector
!= NULL
);
927 Status
= EfiIpSecConfigGetNextSelector (
928 &Private
->IpSecConfig
,
929 IPsecConfigDataTypeSad
,
935 if (EFI_ERROR (Status
)) {
939 if (Selector
->SaId
.Spi
== RemoteSpi
) {
941 // SPI is unique. There is only one SAD whose SPI is
942 // same with RemoteSpi.
944 IsRemoteFound
= TRUE
;
945 RemoteSelector
= AllocateZeroPool (SelectorSize
);
946 ASSERT (RemoteSelector
!= NULL
);
947 CopyMem (RemoteSelector
, Selector
, SelectorSize
);
950 if (Selector
->SaId
.Spi
== LocalSpi
) {
952 // SPI is unique. There is only one SAD whose SPI is
953 // same with LocalSpi.
956 LocalSelector
= AllocateZeroPool (SelectorSize
);
957 ASSERT (LocalSelector
!= NULL
);
958 CopyMem (LocalSelector
, Selector
, SelectorSize
);
962 // Delete SA from the Variable.
965 Status
= EfiIpSecConfigSetData (
966 &Private
->IpSecConfig
,
967 IPsecConfigDataTypeSad
,
975 Status
= EfiIpSecConfigSetData (
976 &Private
->IpSecConfig
,
977 IPsecConfigDataTypeSad
,
987 "\n------IKEV2 deleted ChildSa(local spi, remote spi):(0x%x, 0x%x)------\n",
991 Ikev2ChildSaSessionFree (ChildSession
);
993 if (RemoteSelector
!= NULL
) {
994 FreePool (RemoteSelector
);
997 if (LocalSelector
!= NULL
) {
998 FreePool (LocalSelector
);
1001 if (Selector
!= NULL
) {
1002 FreePool (Selector
);
1009 Free the specified DhBuffer.
1011 @param[in] DhBuffer Pointer to IKEV2_DH_BUFFER to be freed.
1016 IKEV2_DH_BUFFER
*DhBuffer
1019 if (DhBuffer
!= NULL
) {
1020 if (DhBuffer
->GxBuffer
!= NULL
) {
1021 FreePool (DhBuffer
->GxBuffer
);
1023 if (DhBuffer
->GyBuffer
!= NULL
) {
1024 FreePool (DhBuffer
->GyBuffer
);
1026 if (DhBuffer
->GxyBuffer
!= NULL
) {
1027 FreePool (DhBuffer
->GxyBuffer
);
1029 if (DhBuffer
->DhContext
!= NULL
) {
1030 IpSecCryptoIoFreeDh (&DhBuffer
->DhContext
);
1032 FreePool (DhBuffer
);
1037 This function is to parse a request IKE packet and return its request type.
1038 The request type is one of IKE CHILD SA creation, IKE SA rekeying and
1039 IKE CHILD SA rekeying.
1041 @param[in] IkePacket IKE packet to be prased.
1043 return the type of the IKE packet.
1046 IKEV2_CREATE_CHILD_REQUEST_TYPE
1047 Ikev2ChildExchangeRequestType(
1048 IN IKE_PACKET
*IkePacket
1053 IKE_PAYLOAD
*IkePayload
;
1057 NET_LIST_FOR_EACH (Entry
, &(IkePacket
)->PayloadList
) {
1058 IkePayload
= IKE_PAYLOAD_BY_PACKET (Entry
);
1059 if (IkePayload
->PayloadType
== IKEV2_PAYLOAD_TYPE_TS_INIT
) {
1061 // Packet with Ts Payload means it is for either CHILD_SA_CREATE or CHILD_SA_REKEY.
1065 if (IkePayload
->PayloadType
== IKEV2_PAYLOAD_TYPE_NOTIFY
) {
1066 if (((IKEV2_NOTIFY
*)IkePayload
)->MessageType
== IKEV2_NOTIFICATION_REKEY_SA
) {
1068 // If notify payload with REKEY_SA message type, the IkePacket is for
1069 // rekeying Child SA.
1071 return IkeRequestTypeRekeyChildSa
;
1078 // The Create Child Exchange is for IKE SA rekeying.
1080 return IkeRequestTypeRekeyIkeSa
;
1083 // If the Notify payloaad with transport mode message type, the IkePacket is
1084 // for create Child SA.
1086 return IkeRequestTypeCreateChildSa
;
1091 Associate a SPD selector to the Child SA Session.
1093 This function is called when the Child SA is not the first child SA of its
1094 IKE SA. It associate a SPD to this Child SA.
1096 @param[in, out] ChildSaSession Pointer to the Child SA Session to be associated to
1099 @retval EFI_SUCCESS Associate one SPD selector to this Child SA Session successfully.
1100 @retval EFI_NOT_FOUND Can't find the related SPD selector.
1104 Ikev2ChildSaAssociateSpdEntry (
1105 IN OUT IKEV2_CHILD_SA_SESSION
*ChildSaSession
1108 IpSecVisitConfigData (IPsecConfigDataTypeSpd
, Ikev2MatchSpdEntry
, ChildSaSession
);
1109 if (ChildSaSession
->Spd
!= NULL
) {
1112 return EFI_NOT_FOUND
;
1118 This function finds the SPI from Create Child SA Exchange Packet.
1120 @param[in] IkePacket Pointer to IKE_PACKET to be searched.
1122 @retval SPI number or 0 if it is not supported.
1126 Ikev2ChildExchangeRekeySpi (
1127 IN IKE_PACKET
*IkePacket
1137 Validate the IKE header of received IKE packet.
1139 @param[in] IkeSaSession Pointer to IKEV2_SA_SESSION related to this IKE packet.
1140 @param[in] IkeHdr Pointer to IKE header of received IKE packet.
1142 @retval TRUE If the IKE header is valid.
1143 @retval FALSE If the IKE header is invalid.
1147 Ikev2ValidateHeader (
1148 IN IKEV2_SA_SESSION
*IkeSaSession
,
1149 IN IKE_HEADER
*IkeHdr
1153 IKEV2_SESSION_STATE State
;
1155 State
= IkeSaSession
->SessionCommon
.State
;
1156 if (State
== IkeStateInit
) {
1158 // For the IKE Initial Exchange, the MessagId should be zero.
1160 if (IkeHdr
->MessageId
!= 0) {
1164 if (State
== IkeStateAuth
) {
1165 if (IkeHdr
->MessageId
!= 1) {
1169 if (IkeHdr
->InitiatorCookie
!= IkeSaSession
->InitiatorCookie
||
1170 IkeHdr
->ResponderCookie
!= IkeSaSession
->ResponderCookie
1173 // TODO: send notification INVALID-COOKIE
1180 // Information Exchagne and Create Child Exchange can be started from each part.
1182 if (IkeHdr
->ExchangeType
!= IKEV2_EXCHANGE_TYPE_INFO
&&
1183 IkeHdr
->ExchangeType
!= IKEV2_EXCHANGE_TYPE_CREATE_CHILD
1185 if (IkeSaSession
->SessionCommon
.IsInitiator
) {
1186 if (IkeHdr
->InitiatorCookie
!= IkeSaSession
->InitiatorCookie
) {
1188 // TODO: send notification INVALID-COOKIE
1192 if (IkeHdr
->Flags
!= IKE_HEADER_FLAGS_RESPOND
) {
1196 if (IkeHdr
->Flags
!= IKE_HEADER_FLAGS_INIT
) {
1206 Create and intialize IKEV2_SA_DATA for speicifed IKEV2_SESSION_COMMON.
1208 This function will be only called by the initiator. The responder's IKEV2_SA_DATA
1209 will be generated during parsed the initiator packet.
1211 @param[in] SessionCommon Pointer to IKEV2_SESSION_COMMON related to.
1213 @retval a Pointer to a new IKEV2_SA_DATA or NULL.
1217 Ikev2InitializeSaData (
1218 IN IKEV2_SESSION_COMMON
*SessionCommon
1221 IKEV2_CHILD_SA_SESSION
*ChildSaSession
;
1222 IKEV2_SA_DATA
*SaData
;
1223 IKEV2_PROPOSAL_DATA
*ProposalData
;
1224 IKEV2_TRANSFORM_DATA
*TransformData
;
1225 IKE_SA_ATTRIBUTE
*Attribute
;
1227 ASSERT (SessionCommon
!= NULL
);
1229 // TODO: Remove the hard code of the support Alogrithm. Those data should be
1230 // get from the SPD/PAD data.
1232 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1233 SaData
= AllocateZeroPool (
1234 sizeof (IKEV2_SA_DATA
) +
1235 sizeof (IKEV2_PROPOSAL_DATA
) * 2 +
1236 sizeof (IKEV2_TRANSFORM_DATA
) * 4 * 2
1239 SaData
= AllocateZeroPool (
1240 sizeof (IKEV2_SA_DATA
) +
1241 sizeof (IKEV2_PROPOSAL_DATA
) * 2 +
1242 sizeof (IKEV2_TRANSFORM_DATA
) * 3 * 2
1245 if (SaData
== NULL
) {
1250 // First proposal payload: 3DES + SHA1 + DH
1252 SaData
->NumProposals
= 2;
1253 ProposalData
= (IKEV2_PROPOSAL_DATA
*) (SaData
+ 1);
1254 ProposalData
->ProposalIndex
= 1;
1257 // If SA data for IKE_SA_INIT exchage, contains 4 transforms. If SA data for
1258 // IKE_AUTH exchange contains 3 transforms.
1260 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1261 ProposalData
->NumTransforms
= 4;
1263 ProposalData
->NumTransforms
= 3;
1267 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1268 ProposalData
->ProtocolId
= IPSEC_PROTO_ISAKMP
;
1270 ChildSaSession
= IKEV2_CHILD_SA_SESSION_FROM_COMMON (SessionCommon
);
1271 ProposalData
->ProtocolId
= IPSEC_PROTO_IPSEC_ESP
;
1272 ProposalData
->Spi
= AllocateZeroPool (sizeof (ChildSaSession
->LocalPeerSpi
));
1273 ASSERT (ProposalData
->Spi
!= NULL
);
1276 &ChildSaSession
->LocalPeerSpi
,
1277 sizeof(ChildSaSession
->LocalPeerSpi
)
1282 // Set transform attribute for Encryption Algorithm - 3DES
1284 TransformData
= (IKEV2_TRANSFORM_DATA
*) (ProposalData
+ 1);
1285 TransformData
->TransformIndex
= 0;
1286 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_ENCR
;
1287 TransformData
->TransformId
= IKEV2_TRANSFORM_ID_ENCR_3DES
;
1290 // Set transform attribute for Integrity Algorithm - SHA1_96
1292 TransformData
= (IKEV2_TRANSFORM_DATA
*) (TransformData
+ 1);
1293 TransformData
->TransformIndex
= 1;
1294 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_INTEG
;
1295 TransformData
->TransformId
= IKEV2_TRANSFORM_ID_AUTH_HMAC_SHA1_96
;
1297 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1299 // Set transform attribute for Pseduo-Random Function - HAMC_SHA1
1301 TransformData
= (IKEV2_TRANSFORM_DATA
*) (TransformData
+ 1);
1302 TransformData
->TransformIndex
= 2;
1303 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_PRF
;
1304 TransformData
->TransformId
= IKEV2_TRANSFORM_ID_PRF_HMAC_SHA1
;
1307 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1309 // Set transform attribute for DH Group - DH 1024
1311 TransformData
= (IKEV2_TRANSFORM_DATA
*) (TransformData
+ 1);
1312 TransformData
->TransformIndex
= 3;
1313 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_DH
;
1314 TransformData
->TransformId
= IKEV2_TRANSFORM_ID_DH_1024MODP
;
1317 // Transform type for Extended Sequence Numbers. Currently not support Extended
1320 TransformData
= (IKEV2_TRANSFORM_DATA
*) (TransformData
+ 1);
1321 TransformData
->TransformIndex
= 2;
1322 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_ESN
;
1323 TransformData
->TransformId
= 0;
1327 // Second proposal payload: 3DES + SHA1 + DH
1329 ProposalData
= (IKEV2_PROPOSAL_DATA
*) (TransformData
+ 1);
1330 ProposalData
->ProposalIndex
= 2;
1332 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1333 ProposalData
->ProtocolId
= IPSEC_PROTO_ISAKMP
;
1334 ProposalData
->NumTransforms
= 4;
1337 ChildSaSession
= IKEV2_CHILD_SA_SESSION_FROM_COMMON (SessionCommon
);
1338 ProposalData
->ProtocolId
= IPSEC_PROTO_IPSEC_ESP
;
1339 ProposalData
->NumTransforms
= 3;
1340 ProposalData
->Spi
= AllocateZeroPool (sizeof (ChildSaSession
->LocalPeerSpi
));
1341 ASSERT (ProposalData
->Spi
!= NULL
);
1344 &ChildSaSession
->LocalPeerSpi
,
1345 sizeof(ChildSaSession
->LocalPeerSpi
)
1350 // Set transform attribute for Encryption Algorithm - AES-CBC
1352 TransformData
= (IKEV2_TRANSFORM_DATA
*) (ProposalData
+ 1);
1353 TransformData
->TransformIndex
= 0;
1354 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_ENCR
;
1355 TransformData
->TransformId
= IKEV2_TRANSFORM_ID_ENCR_AES_CBC
;
1356 Attribute
= &TransformData
->Attribute
;
1357 Attribute
->AttrType
= IKEV2_ATTRIBUTE_TYPE_KEYLEN
;
1358 Attribute
->Attr
.AttrLength
= (UINT16
) (8 * IpSecGetEncryptKeyLength (IKEV2_TRANSFORM_ID_ENCR_AES_CBC
));
1361 // Set transform attribute for Integrity Algorithm - SHA1_96
1363 TransformData
= (IKEV2_TRANSFORM_DATA
*) (TransformData
+ 1);
1364 TransformData
->TransformIndex
= 1;
1365 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_INTEG
;
1366 TransformData
->TransformId
= IKEV2_TRANSFORM_ID_AUTH_HMAC_SHA1_96
;
1368 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1370 // Set transform attribute for Pseduo-Random Function - HAMC_SHA1
1372 TransformData
= (IKEV2_TRANSFORM_DATA
*) (TransformData
+ 1);
1373 TransformData
->TransformIndex
= 2;
1374 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_PRF
;
1375 TransformData
->TransformId
= IKEV2_TRANSFORM_ID_PRF_HMAC_SHA1
;
1378 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1380 // Set transform attrbiute for DH Group - DH-1024
1382 TransformData
= (IKEV2_TRANSFORM_DATA
*) (TransformData
+ 1);
1383 TransformData
->TransformIndex
= 3;
1384 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_DH
;
1385 TransformData
->TransformId
= IKEV2_TRANSFORM_ID_DH_1024MODP
;
1388 // Transform type for Extended Sequence Numbers. Currently not support Extended
1391 TransformData
= (IKEV2_TRANSFORM_DATA
*) (TransformData
+ 1);
1392 TransformData
->TransformIndex
= 2;
1393 TransformData
->TransformType
= IKEV2_TRANSFORM_TYPE_ESN
;
1394 TransformData
->TransformId
= 0;
1401 Store the SA into SAD.
1403 @param[in] ChildSaSession Pointer to IKEV2_CHILD_SA_SESSION.
1408 IN IKEV2_CHILD_SA_SESSION
*ChildSaSession
1412 EFI_IPSEC_SA_ID SaId
;
1413 EFI_IPSEC_SA_DATA2 SaData
;
1414 IKEV2_SESSION_COMMON
*SessionCommon
;
1415 IPSEC_PRIVATE_DATA
*Private
;
1416 UINT32 TempAddressCount
;
1417 EFI_IP_ADDRESS_INFO
*TempAddressInfo
;
1419 SessionCommon
= &ChildSaSession
->SessionCommon
;
1420 Private
= SessionCommon
->Private
;
1422 ZeroMem (&SaId
, sizeof (EFI_IPSEC_SA_ID
));
1423 ZeroMem (&SaData
, sizeof (EFI_IPSEC_SA_DATA2
));
1426 // Create a SpdSelector. In this implementation, one SPD represents
1427 // 2 direction traffic, so in here, there needs to reverse the local address
1428 // and remote address for Remote Peer's SA, then reverse again for the locate
1431 TempAddressCount
= ChildSaSession
->SpdSelector
->LocalAddressCount
;
1432 TempAddressInfo
= ChildSaSession
->SpdSelector
->LocalAddress
;
1434 ChildSaSession
->SpdSelector
->LocalAddressCount
= ChildSaSession
->SpdSelector
->RemoteAddressCount
;
1435 ChildSaSession
->SpdSelector
->LocalAddress
= ChildSaSession
->SpdSelector
->RemoteAddress
;
1437 ChildSaSession
->SpdSelector
->RemoteAddress
= TempAddressInfo
;
1438 ChildSaSession
->SpdSelector
->RemoteAddressCount
= TempAddressCount
;
1441 // Set the SaId and SaData.
1443 SaId
.Spi
= ChildSaSession
->LocalPeerSpi
;
1444 SaId
.Proto
= EfiIPsecESP
;
1445 SaData
.AntiReplayWindows
= 16;
1447 SaData
.Mode
= ChildSaSession
->Spd
->Data
->ProcessingPolicy
->Mode
;
1450 // If it is tunnel mode, should add the TunnelDest and TunnelSource for SaData.
1452 if (SaData
.Mode
== EfiIPsecTunnel
) {
1454 &SaData
.TunnelSourceAddress
,
1455 &ChildSaSession
->Spd
->Data
->ProcessingPolicy
->TunnelOption
->RemoteTunnelAddress
,
1456 sizeof (EFI_IP_ADDRESS
)
1459 &SaData
.TunnelDestinationAddress
,
1460 &ChildSaSession
->Spd
->Data
->ProcessingPolicy
->TunnelOption
->LocalTunnelAddress
,
1461 sizeof (EFI_IP_ADDRESS
)
1465 CopyMem (&SaId
.DestAddress
, &ChildSaSession
->SessionCommon
.LocalPeerIp
, sizeof (EFI_IP_ADDRESS
));
1466 CopyMem (&SaData
.AlgoInfo
, &ChildSaSession
->ChildKeymats
.LocalPeerInfo
, sizeof (EFI_IPSEC_ALGO_INFO
));
1467 SaData
.SpdSelector
= ChildSaSession
->SpdSelector
;
1470 // Store the remote SA into SAD.
1472 Status
= EfiIpSecConfigSetData (
1473 &Private
->IpSecConfig
,
1474 IPsecConfigDataTypeSad
,
1475 (EFI_IPSEC_CONFIG_SELECTOR
*) &SaId
,
1479 ASSERT_EFI_ERROR (Status
);
1482 // Store the local SA into SAD.
1484 ChildSaSession
->SpdSelector
->RemoteAddressCount
= ChildSaSession
->SpdSelector
->LocalAddressCount
;
1485 ChildSaSession
->SpdSelector
->RemoteAddress
= ChildSaSession
->SpdSelector
->LocalAddress
;
1487 ChildSaSession
->SpdSelector
->LocalAddress
= TempAddressInfo
;
1488 ChildSaSession
->SpdSelector
->LocalAddressCount
= TempAddressCount
;
1490 SaId
.Spi
= ChildSaSession
->RemotePeerSpi
;
1492 CopyMem (&SaId
.DestAddress
, &ChildSaSession
->SessionCommon
.RemotePeerIp
, sizeof (EFI_IP_ADDRESS
));
1493 CopyMem (&SaData
.AlgoInfo
, &ChildSaSession
->ChildKeymats
.RemotePeerInfo
, sizeof (EFI_IPSEC_ALGO_INFO
));
1494 SaData
.SpdSelector
= ChildSaSession
->SpdSelector
;
1497 // If it is tunnel mode, should add the TunnelDest and TunnelSource for SaData.
1499 if (SaData
.Mode
== EfiIPsecTunnel
) {
1501 &SaData
.TunnelSourceAddress
,
1502 &ChildSaSession
->Spd
->Data
->ProcessingPolicy
->TunnelOption
->LocalTunnelAddress
,
1503 sizeof (EFI_IP_ADDRESS
)
1506 &SaData
.TunnelDestinationAddress
,
1507 &ChildSaSession
->Spd
->Data
->ProcessingPolicy
->TunnelOption
->RemoteTunnelAddress
,
1508 sizeof (EFI_IP_ADDRESS
)
1512 Status
= EfiIpSecConfigSetData (
1513 &Private
->IpSecConfig
,
1514 IPsecConfigDataTypeSad
,
1515 (EFI_IPSEC_CONFIG_SELECTOR
*) &SaId
,
1520 ASSERT_EFI_ERROR (Status
);
1524 Call back function of the IKE life time is over.
1526 This function will mark the related IKE SA Session as deleting and trigger a
1527 Information negotiation.
1529 @param[in] Event The signaled Event.
1530 @param[in] Context Pointer to data passed by caller.
1535 Ikev2LifetimeNotify (
1540 IKEV2_SA_SESSION
*IkeSaSession
;
1541 IKEV2_CHILD_SA_SESSION
*ChildSaSession
;
1542 IKEV2_SESSION_COMMON
*SessionCommon
;
1544 ASSERT (Context
!= NULL
);
1545 SessionCommon
= (IKEV2_SESSION_COMMON
*) Context
;
1547 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1548 IkeSaSession
= IKEV2_SA_SESSION_FROM_COMMON (SessionCommon
);
1551 "\n---IkeSa Lifetime is out(cookie_i, cookie_r):(0x%lx, 0x%lx)---\n",
1552 IkeSaSession
->InitiatorCookie
,
1553 IkeSaSession
->ResponderCookie
1557 // Change the IKE SA Session's State to IKE_STATE_SA_DELETING.
1559 IKEV2_DUMP_STATE (IkeSaSession
->SessionCommon
.State
, IkeStateSaDeleting
);
1560 IkeSaSession
->SessionCommon
.State
= IkeStateSaDeleting
;
1563 ChildSaSession
= IKEV2_CHILD_SA_SESSION_FROM_COMMON (SessionCommon
);
1564 IkeSaSession
= ChildSaSession
->IkeSaSession
;
1567 // Link the timeout child SA to the DeleteSaList.
1569 InsertTailList (&IkeSaSession
->DeleteSaList
, &ChildSaSession
->ByDelete
);
1572 // Change the Child SA Session's State to IKE_STATE_SA_DELETING.
1576 "\n------ChildSa Lifetime is out(SPI):(0x%x)------\n",
1577 ChildSaSession
->LocalPeerSpi
1582 // TODO: Send the delete info packet or delete silently
1584 mIkev2Exchange
.NegotiateInfo ((UINT8
*) IkeSaSession
, NULL
);
1588 This function will be called if the TimeOut Event is signaled.
1590 @param[in] Event The signaled Event.
1591 @param[in] Context The data passed by caller.
1601 IPSEC_PRIVATE_DATA
*Private
;
1602 IKEV2_SA_SESSION
*IkeSaSession
;
1603 IKEV2_CHILD_SA_SESSION
*ChildSaSession
;
1604 IKEV2_SESSION_COMMON
*SessionCommon
;
1605 LIST_ENTRY
*ChildSaEntry
;
1609 ASSERT (Context
!= NULL
);
1610 IkeSaSession
= NULL
;
1611 ChildSaSession
= NULL
;
1612 SessionCommon
= (IKEV2_SESSION_COMMON
*) Context
;
1613 Private
= SessionCommon
->Private
;
1616 // Remove the SA session from the processing list if exceed the max retry.
1618 if (SessionCommon
->RetryCount
> IKE_MAX_RETRY
) {
1619 if (SessionCommon
->IkeSessionType
== IkeSessionTypeIkeSa
) {
1620 IkeSaSession
= IKEV2_SA_SESSION_FROM_COMMON (SessionCommon
);
1621 if (IkeSaSession
->SessionCommon
.State
== IkeStateSaDeleting
) {
1624 // If the IkeSaSession is initiator, delete all its Child SAs before removing IKE SA.
1625 // If the IkesaSession is responder, all ChildSa has been remove in Ikev2HandleInfo();
1627 for (ChildSaEntry
= IkeSaSession
->ChildSaEstablishSessionList
.ForwardLink
;
1628 ChildSaEntry
!= &IkeSaSession
->ChildSaEstablishSessionList
;
1630 ChildSaSession
= IKEV2_CHILD_SA_SESSION_BY_IKE_SA (ChildSaEntry
);
1632 // Move to next ChildSa Entry.
1634 ChildSaEntry
= ChildSaEntry
->ForwardLink
;
1636 // Delete LocalSpi & RemoteSpi and remove the ChildSaSession from the
1637 // EstablishedChildSaList.
1639 Ikev2ChildSaSilentDelete (IkeSaSession
, ChildSaSession
->LocalPeerSpi
);
1643 // If the IKE SA Delete Payload wasn't sent out successfully, Delete it from the EstablishedList.
1645 Ikev2SaSessionRemove (&Private
->Ikev2EstablishedList
, &SessionCommon
->RemotePeerIp
);
1647 if (Private
!= NULL
&& Private
->IsIPsecDisabling
) {
1649 // After all IKE SAs were deleted, set the IPSEC_STATUS_DISABLED value in
1650 // IPsec status variable.
1652 if (IsListEmpty (&Private
->Ikev1EstablishedList
) && IsListEmpty (&Private
->Ikev2EstablishedList
)) {
1653 Value
= IPSEC_STATUS_DISABLED
;
1654 Status
= gRT
->SetVariable (
1655 IPSECCONFIG_STATUS_NAME
,
1656 &gEfiIpSecConfigProtocolGuid
,
1657 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1661 if (!EFI_ERROR (Status
)) {
1663 // Set the Disabled Flag in Private data.
1665 Private
->IpSec
.DisabledFlag
= TRUE
;
1666 Private
->IsIPsecDisabling
= FALSE
;
1671 Ikev2SaSessionRemove (&Private
->Ikev2SessionList
, &SessionCommon
->RemotePeerIp
);
1673 Ikev2SaSessionFree (IkeSaSession
);
1678 // If the packet sent by Child SA.
1680 ChildSaSession
= IKEV2_CHILD_SA_SESSION_FROM_COMMON (SessionCommon
);
1681 IkeSaSession
= ChildSaSession
->IkeSaSession
;
1682 if (ChildSaSession
->SessionCommon
.State
== IkeStateSaDeleting
) {
1685 // Established Child SA should be remove from the SAD entry and
1686 // DeleteList. The function of Ikev2DeleteChildSaSilent() will remove
1687 // the childSA from the IkeSaSession->ChildSaEstablishedList. So there
1688 // is no need to remove it here.
1690 Ikev2ChildSaSilentDelete (IkeSaSession
, ChildSaSession
->LocalPeerSpi
);
1691 Ikev2ChildSaSessionRemove (
1692 &IkeSaSession
->DeleteSaList
,
1693 ChildSaSession
->LocalPeerSpi
,
1694 IKEV2_DELET_CHILDSA_LIST
1697 Ikev2ChildSaSessionRemove (
1698 &IkeSaSession
->ChildSaSessionList
,
1699 ChildSaSession
->LocalPeerSpi
,
1700 IKEV2_ESTABLISHING_CHILDSA_LIST
1704 Ikev2ChildSaSessionFree (ChildSaSession
);
1710 // Increase the retry count.
1712 SessionCommon
->RetryCount
++;
1713 DEBUG ((DEBUG_INFO
, ">>>Resending the last packet ...\n"));
1716 // Resend the last packet.
1718 Ikev2SendIkePacket (
1719 SessionCommon
->UdpService
,
1720 (UINT8
*)SessionCommon
,
1721 SessionCommon
->LastSentPacket
,
1727 Copy ChildSaSession->Spd->Selector to ChildSaSession->SpdSelector.
1729 ChildSaSession->SpdSelector stores the real Spdselector for its SA. Sometime,
1730 the SpdSelector in ChildSaSession is more accurated or the scope is smaller
1731 than the one in ChildSaSession->Spd, especially for the tunnel mode.
1733 @param[in, out] ChildSaSession Pointer to IKEV2_CHILD_SA_SESSION related to.
1737 Ikev2ChildSaSessionSpdSelectorCreate (
1738 IN OUT IKEV2_CHILD_SA_SESSION
*ChildSaSession
1741 if (ChildSaSession
->Spd
!= NULL
&& ChildSaSession
->Spd
->Selector
!= NULL
) {
1742 if (ChildSaSession
->SpdSelector
== NULL
) {
1743 ChildSaSession
->SpdSelector
= AllocateZeroPool (sizeof (EFI_IPSEC_SPD_SELECTOR
));
1744 ASSERT (ChildSaSession
->SpdSelector
!= NULL
);
1747 ChildSaSession
->SpdSelector
,
1748 ChildSaSession
->Spd
->Selector
,
1749 sizeof (EFI_IPSEC_SPD_SELECTOR
)
1751 ChildSaSession
->SpdSelector
->RemoteAddress
= AllocateCopyPool (
1752 ChildSaSession
->Spd
->Selector
->RemoteAddressCount
*
1753 sizeof (EFI_IP_ADDRESS_INFO
),
1754 ChildSaSession
->Spd
->Selector
->RemoteAddress
1756 ChildSaSession
->SpdSelector
->LocalAddress
= AllocateCopyPool (
1757 ChildSaSession
->Spd
->Selector
->LocalAddressCount
*
1758 sizeof (EFI_IP_ADDRESS_INFO
),
1759 ChildSaSession
->Spd
->Selector
->LocalAddress
1762 ASSERT (ChildSaSession
->SpdSelector
->LocalAddress
!= NULL
);
1763 ASSERT (ChildSaSession
->SpdSelector
->RemoteAddress
!= NULL
);
1765 ChildSaSession
->SpdSelector
->RemoteAddressCount
= ChildSaSession
->Spd
->Selector
->RemoteAddressCount
;
1766 ChildSaSession
->SpdSelector
->LocalAddressCount
= ChildSaSession
->Spd
->Selector
->LocalAddressCount
;
1771 Generate a ChildSa Session and insert it into related IkeSaSession.
1773 @param[in] IkeSaSession Pointer to related IKEV2_SA_SESSION.
1774 @param[in] UdpService Pointer to related IKE_UDP_SERVICE.
1776 @return pointer of IKEV2_CHILD_SA_SESSION.
1779 IKEV2_CHILD_SA_SESSION
*
1780 Ikev2ChildSaSessionCreate (
1781 IN IKEV2_SA_SESSION
*IkeSaSession
,
1782 IN IKE_UDP_SERVICE
*UdpService
1785 IKEV2_CHILD_SA_SESSION
*ChildSaSession
;
1786 IKEV2_SESSION_COMMON
*ChildSaCommon
;
1789 // Create a new ChildSaSession.Insert it into processing list and initiate the common parameters.
1791 ChildSaSession
= Ikev2ChildSaSessionAlloc (UdpService
, IkeSaSession
);
1792 ASSERT (ChildSaSession
!= NULL
);
1795 // Set the specific parameters.
1797 ChildSaSession
->Spd
= IkeSaSession
->Spd
;
1798 ChildSaCommon
= &ChildSaSession
->SessionCommon
;
1799 ChildSaCommon
->IsInitiator
= IkeSaSession
->SessionCommon
.IsInitiator
;
1800 if (IkeSaSession
->SessionCommon
.State
== IkeStateAuth
) {
1801 ChildSaCommon
->State
= IkeStateAuth
;
1802 IKEV2_DUMP_STATE (ChildSaCommon
->State
, IkeStateAuth
);
1804 ChildSaCommon
->State
= IkeStateCreateChild
;
1805 IKEV2_DUMP_STATE (ChildSaCommon
->State
, IkeStateCreateChild
);
1809 // If SPD->Selector is not NULL, copy it to the ChildSaSession->SpdSelector.
1810 // The ChildSaSession->SpdSelector might be changed after the traffic selector
1811 // negoniation and it will be copied into the SAData after ChildSA established.
1813 Ikev2ChildSaSessionSpdSelectorCreate (ChildSaSession
);
1816 // Copy first NiBlock and NrBlock to ChildSa Session
1818 ChildSaSession
->NiBlock
= AllocateZeroPool (IkeSaSession
->NiBlkSize
);
1819 ASSERT (ChildSaSession
->NiBlock
!= NULL
);
1820 ChildSaSession
->NiBlkSize
= IkeSaSession
->NiBlkSize
;
1821 CopyMem (ChildSaSession
->NiBlock
, IkeSaSession
->NiBlock
, IkeSaSession
->NiBlkSize
);
1823 ChildSaSession
->NrBlock
= AllocateZeroPool (IkeSaSession
->NrBlkSize
);
1824 ASSERT (ChildSaSession
->NrBlock
!= NULL
);
1825 ChildSaSession
->NrBlkSize
= IkeSaSession
->NrBlkSize
;
1826 CopyMem (ChildSaSession
->NrBlock
, IkeSaSession
->NrBlock
, IkeSaSession
->NrBlkSize
);
1829 // Only if the Create Child SA is called for the IKE_INIT Exchange and
1830 // IkeSaSession is initiator (Only Initiator's SPD is not NULL), Set the
1831 // Traffic Selectors related information here.
1833 if (IkeSaSession
->SessionCommon
.State
== IkeStateAuth
&& IkeSaSession
->Spd
!= NULL
) {
1834 ChildSaSession
->ProtoId
= IkeSaSession
->Spd
->Selector
->NextLayerProtocol
;
1835 ChildSaSession
->LocalPort
= IkeSaSession
->Spd
->Selector
->LocalPort
;
1836 ChildSaSession
->RemotePort
= IkeSaSession
->Spd
->Selector
->RemotePort
;
1840 // Insert the new ChildSaSession into processing child SA list.
1842 Ikev2ChildSaSessionInsert (&IkeSaSession
->ChildSaSessionList
, ChildSaSession
);
1843 return ChildSaSession
;
1847 Check if the SPD is related to the input Child SA Session.
1849 This function is the subfunction of Ikev1AssociateSpdEntry(). It is the call
1850 back function of IpSecVisitConfigData().
1853 @param[in] Type Type of the input Config Selector.
1854 @param[in] Selector Pointer to the Configure Selector to be checked.
1855 @param[in] Data Pointer to the Configure Selector's Data passed
1857 @param[in] SelectorSize The buffer size of Selector.
1858 @param[in] DataSize The buffer size of the Data.
1859 @param[in] Context The data passed from the caller. It is a Child
1860 SA Session in this context.
1862 @retval EFI_SUCCESS The SPD Selector is not related to the Child SA Session.
1863 @retval EFI_ABORTED The SPD Selector is related to the Child SA session and
1864 set the ChildSaSession->Spd to point to this SPD Selector.
1868 Ikev2MatchSpdEntry (
1869 IN EFI_IPSEC_CONFIG_DATA_TYPE Type
,
1870 IN EFI_IPSEC_CONFIG_SELECTOR
*Selector
,
1872 IN UINTN SelectorSize
,
1877 IKEV2_CHILD_SA_SESSION
*ChildSaSession
;
1878 EFI_IPSEC_SPD_SELECTOR
*SpdSelector
;
1879 EFI_IPSEC_SPD_DATA
*SpdData
;
1883 ASSERT (Type
== IPsecConfigDataTypeSpd
);
1884 SpdData
= (EFI_IPSEC_SPD_DATA
*) Data
;
1886 // Bypass all non-protect SPD entry first
1888 if (SpdData
->Action
!= EfiIPsecActionProtect
) {
1892 ChildSaSession
= (IKEV2_CHILD_SA_SESSION
*) Context
;
1893 IpVersion
= ChildSaSession
->SessionCommon
.UdpService
->IpVersion
;
1894 SpdSelector
= (EFI_IPSEC_SPD_SELECTOR
*) Selector
;
1897 if (SpdSelector
->NextLayerProtocol
== EFI_IP_PROTO_UDP
&&
1898 SpdSelector
->LocalPort
== IKE_DEFAULT_PORT
&&
1899 SpdSelector
->LocalPortRange
== 0 &&
1900 SpdSelector
->RemotePort
== IKE_DEFAULT_PORT
&&
1901 SpdSelector
->RemotePortRange
== 0
1904 // TODO: Skip IKE Policy here or set a SPD entry?
1909 if (SpdSelector
->NextLayerProtocol
!= EFI_IPSEC_ANY_PROTOCOL
&&
1910 SpdSelector
->NextLayerProtocol
!= ChildSaSession
->ProtoId
1915 if (SpdSelector
->LocalPort
!= EFI_IPSEC_ANY_PORT
&& SpdSelector
->LocalPort
!= ChildSaSession
->LocalPort
) {
1919 if (SpdSelector
->RemotePort
!= EFI_IPSEC_ANY_PORT
&& SpdSelector
->RemotePort
!= ChildSaSession
->RemotePort
) {
1923 IsMatch
= (BOOLEAN
) (IsMatch
&&
1924 IpSecMatchIpAddress (
1926 &ChildSaSession
->SessionCommon
.LocalPeerIp
,
1927 SpdSelector
->LocalAddress
,
1928 SpdSelector
->LocalAddressCount
1931 IsMatch
= (BOOLEAN
) (IsMatch
&&
1932 IpSecMatchIpAddress (
1934 &ChildSaSession
->SessionCommon
.RemotePeerIp
,
1935 SpdSelector
->RemoteAddress
,
1936 SpdSelector
->RemoteAddressCount
1940 ChildSaSession
->Spd
= IkeSearchSpdEntry (SpdSelector
);
1948 Check if the Algorithm ID is supported.
1950 @param[in] AlgorithmId The specified Algorithm ID.
1951 @param[in] Type The type used to indicate the Algorithm is for Encrypt or
1954 @retval TRUE If the Algorithm ID is supported.
1955 @retval FALSE If the Algorithm ID is not supported.
1960 IN UINT16 AlgorithmId
,
1966 case IKE_ENCRYPT_TYPE
:
1967 for (Index
= 0; Index
< IKEV2_SUPPORT_ENCRYPT_ALGORITHM_NUM
; Index
++) {
1968 if (mIkev2EncryptAlgorithmList
[Index
] == AlgorithmId
) {
1974 case IKE_AUTH_TYPE
:
1975 for (Index
= 0; Index
< IKEV2_SUPPORT_AUTH_ALGORITHM_NUM
; Index
++) {
1976 if (mIkev2AuthAlgorithmList
[Index
] == AlgorithmId
) {
1983 for (Index
= 0; Index
< IKEV2_SUPPORT_DH_ALGORITHM_NUM
; Index
++) {
1984 if (mIkev2DhGroupAlgorithmList
[Index
] == AlgorithmId
) {
1991 for (Index
= 0; Index
< IKEV2_SUPPORT_PRF_ALGORITHM_NUM
; Index
++) {
1992 if (mIkev2PrfAlgorithmList
[Index
] == AlgorithmId
) {
2001 Get the preferred algorithm types from ProposalData.
2003 @param[in] ProposalData Pointer to related IKEV2_PROPOSAL_DATA.
2004 @param[out] PreferEncryptAlgorithm Output of preferred encrypt algorithm.
2005 @param[out] PreferIntegrityAlgorithm Output of preferred integrity algorithm.
2006 @param[out] PreferPrfAlgorithm Output of preferred PRF algorithm. Only
2008 @param[out] PreferDhGroup Output of preferred DH group. Only for
2010 @param[out] PreferEncryptKeylength Output of preferred encrypt key length
2012 @param[out] IsSupportEsn Output of value about the Extented Sequence
2013 Number is support or not. Only for Child SA.
2014 @param[in] IsChildSa If it is ture, the ProposalData is for IKE
2015 SA. Otherwise the proposalData is for Child SA.
2019 Ikev2ParseProposalData (
2020 IN IKEV2_PROPOSAL_DATA
*ProposalData
,
2021 OUT UINT16
*PreferEncryptAlgorithm
,
2022 OUT UINT16
*PreferIntegrityAlgorithm
,
2023 OUT UINT16
*PreferPrfAlgorithm
,
2024 OUT UINT16
*PreferDhGroup
,
2025 OUT UINTN
*PreferEncryptKeylength
,
2026 OUT BOOLEAN
*IsSupportEsn
,
2027 IN BOOLEAN IsChildSa
2030 IKEV2_TRANSFORM_DATA
*TransformData
;
2031 UINT8 TransformIndex
;
2034 // Check input parameters.
2036 if (ProposalData
== NULL
||
2037 PreferEncryptAlgorithm
== NULL
||
2038 PreferIntegrityAlgorithm
== NULL
||
2039 PreferEncryptKeylength
== NULL
2045 if (IsSupportEsn
== NULL
) {
2049 if (PreferPrfAlgorithm
== NULL
|| PreferDhGroup
== NULL
) {
2054 TransformData
= (IKEV2_TRANSFORM_DATA
*)(ProposalData
+ 1);
2055 for (TransformIndex
= 0; TransformIndex
< ProposalData
->NumTransforms
; TransformIndex
++) {
2056 switch (TransformData
->TransformType
) {
2058 // For IKE SA there are four algorithm types. Encryption Algorithm, Pseudo-random Function,
2059 // Integrity Algorithm, Diffie-Hellman Group. For Child SA, there are three algorithm types.
2060 // Encryption Algorithm, Integrity Algorithm, Extended Sequence Number.
2062 case IKEV2_TRANSFORM_TYPE_ENCR
:
2063 if (*PreferEncryptAlgorithm
== 0 && Ikev2IsSupportAlg (TransformData
->TransformId
, IKE_ENCRYPT_TYPE
)) {
2065 // Check the attribute value. According to RFC, only Keylength is support.
2067 if (TransformData
->Attribute
.AttrType
== IKEV2_ATTRIBUTE_TYPE_KEYLEN
) {
2069 // If the Keylength is not support, continue to check the next one.
2071 if (IpSecGetEncryptKeyLength ((UINT8
)TransformData
->TransformId
) != (UINTN
)(TransformData
->Attribute
.Attr
.AttrValue
>> 3)){
2074 *PreferEncryptKeylength
= TransformData
->Attribute
.Attr
.AttrValue
;
2077 *PreferEncryptAlgorithm
= TransformData
->TransformId
;
2081 case IKEV2_TRANSFORM_TYPE_PRF
:
2083 if (*PreferPrfAlgorithm
== 0 && Ikev2IsSupportAlg (TransformData
->TransformId
, IKE_PRF_TYPE
)) {
2084 *PreferPrfAlgorithm
= TransformData
->TransformId
;
2089 case IKEV2_TRANSFORM_TYPE_INTEG
:
2090 if (*PreferIntegrityAlgorithm
== 0 && Ikev2IsSupportAlg (TransformData
->TransformId
, IKE_AUTH_TYPE
)) {
2091 *PreferIntegrityAlgorithm
= TransformData
->TransformId
;
2095 case IKEV2_TRANSFORM_TYPE_DH
:
2097 if (*PreferDhGroup
== 0 && Ikev2IsSupportAlg (TransformData
->TransformId
, IKE_DH_TYPE
)) {
2098 *PreferDhGroup
= TransformData
->TransformId
;
2103 case IKEV2_TRANSFORM_TYPE_ESN
:
2105 if (TransformData
->TransformId
!= 0) {
2106 *IsSupportEsn
= TRUE
;
2114 TransformData
= (IKEV2_TRANSFORM_DATA
*)(TransformData
+ 1);
2119 Parse the received Initial Exchange Packet.
2121 This function parse the SA Payload and Key Payload to find out the cryptographic
2122 suite for the further IKE negotiation and fill it into the IKE SA Session's
2123 CommonSession->SaParams.
2125 @param[in, out] IkeSaSession Pointer to related IKEV2_SA_SESSION.
2126 @param[in] SaPayload The received packet.
2127 @param[in] Type The received packet IKE header flag.
2129 @retval TRUE If the SA proposal in Packet is acceptable.
2130 @retval FALSE If the SA proposal in Packet is not acceptable.
2134 Ikev2SaParseSaPayload (
2135 IN OUT IKEV2_SA_SESSION
*IkeSaSession
,
2136 IN IKE_PAYLOAD
*SaPayload
,
2140 IKEV2_PROPOSAL_DATA
*ProposalData
;
2141 UINT8 ProposalIndex
;
2142 UINT16 PreferEncryptAlgorithm
;
2143 UINT16 PreferIntegrityAlgorithm
;
2144 UINT16 PreferPrfAlgorithm
;
2145 UINT16 PreferDhGroup
;
2146 UINTN PreferEncryptKeylength
;
2147 UINT16 EncryptAlgorithm
;
2148 UINT16 IntegrityAlgorithm
;
2149 UINT16 PrfAlgorithm
;
2151 UINTN EncryptKeylength
;
2155 PreferPrfAlgorithm
= 0;
2156 PreferIntegrityAlgorithm
= 0;
2158 PreferEncryptAlgorithm
= 0;
2159 PreferEncryptKeylength
= 0;
2161 IntegrityAlgorithm
= 0;
2163 EncryptAlgorithm
= 0;
2164 EncryptKeylength
= 0;
2167 if (Type
== IKE_HEADER_FLAGS_INIT
) {
2168 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((IKEV2_SA_DATA
*)SaPayload
->PayloadBuf
+ 1);
2169 for (ProposalIndex
= 0; ProposalIndex
< ((IKEV2_SA_DATA
*)SaPayload
->PayloadBuf
)->NumProposals
; ProposalIndex
++) {
2171 // Iterate each proposal to find the perfered one.
2173 if (ProposalData
->ProtocolId
== IPSEC_PROTO_ISAKMP
&& ProposalData
->NumTransforms
>= 4) {
2175 // Get the preferred algorithms.
2177 Ikev2ParseProposalData (
2179 &PreferEncryptAlgorithm
,
2180 &PreferIntegrityAlgorithm
,
2181 &PreferPrfAlgorithm
,
2183 &PreferEncryptKeylength
,
2188 if (PreferEncryptAlgorithm
!= 0 &&
2189 PreferIntegrityAlgorithm
!= 0 &&
2190 PreferPrfAlgorithm
!= 0 &&
2194 // Find the matched one.
2196 IkeSaSession
->SessionCommon
.SaParams
= AllocateZeroPool (sizeof (IKEV2_SA_PARAMS
));
2197 ASSERT (IkeSaSession
->SessionCommon
.SaParams
!= NULL
);
2198 IkeSaSession
->SessionCommon
.SaParams
->EncAlgId
= PreferEncryptAlgorithm
;
2199 IkeSaSession
->SessionCommon
.SaParams
->EnckeyLen
= PreferEncryptKeylength
;
2200 IkeSaSession
->SessionCommon
.SaParams
->DhGroup
= PreferDhGroup
;
2201 IkeSaSession
->SessionCommon
.SaParams
->Prf
= PreferPrfAlgorithm
;
2202 IkeSaSession
->SessionCommon
.SaParams
->IntegAlgId
= PreferIntegrityAlgorithm
;
2203 IkeSaSession
->SessionCommon
.PreferDhGroup
= PreferDhGroup
;
2206 // Save the matched one in IKEV2_SA_DATA for furthure calculation.
2208 SaDataSize
= sizeof (IKEV2_SA_DATA
) +
2209 sizeof (IKEV2_PROPOSAL_DATA
) +
2210 sizeof (IKEV2_TRANSFORM_DATA
) * 4;
2211 IkeSaSession
->SaData
= AllocateZeroPool (SaDataSize
);
2212 ASSERT (IkeSaSession
->SaData
!= NULL
);
2214 IkeSaSession
->SaData
->NumProposals
= 1;
2217 // BUGBUG: Suppose the matched proposal only has 4 transforms. If
2218 // The matched Proposal has more than 4 transforms means it contains
2219 // one than one transform with same type.
2222 (IKEV2_PROPOSAL_DATA
*) (IkeSaSession
->SaData
+ 1),
2224 SaDataSize
- sizeof (IKEV2_SA_DATA
)
2227 ((IKEV2_PROPOSAL_DATA
*) (IkeSaSession
->SaData
+ 1))->ProposalIndex
= 1;
2230 PreferEncryptAlgorithm
= 0;
2231 PreferIntegrityAlgorithm
= 0;
2232 PreferPrfAlgorithm
= 0;
2234 PreferEncryptKeylength
= 0;
2238 // Point to next Proposal.
2240 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((UINT8
*)(ProposalData
+ 1) +
2241 ProposalData
->NumTransforms
* sizeof (IKEV2_TRANSFORM_DATA
));
2243 } else if (Type
== IKE_HEADER_FLAGS_RESPOND
) {
2245 // First check the SA proposal's ProtoctolID and Transform Numbers. Since it is
2246 // the responded SA proposal, suppose it only has one proposal and the transform Numbers
2249 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((IKEV2_SA_DATA
*) SaPayload
->PayloadBuf
+ 1);
2250 if (ProposalData
->ProtocolId
!= IPSEC_PROTO_ISAKMP
|| ProposalData
->NumTransforms
!= 4) {
2254 // Get the preferred algorithms.
2256 Ikev2ParseProposalData (
2258 &PreferEncryptAlgorithm
,
2259 &PreferIntegrityAlgorithm
,
2260 &PreferPrfAlgorithm
,
2262 &PreferEncryptKeylength
,
2267 // Check if the Sa proposal data from received packet is in the IkeSaSession->SaData.
2269 ProposalData
= (IKEV2_PROPOSAL_DATA
*) (IkeSaSession
->SaData
+ 1);
2271 for (ProposalIndex
= 0; ProposalIndex
< IkeSaSession
->SaData
->NumProposals
&& (!IsMatch
); ProposalIndex
++) {
2272 Ikev2ParseProposalData (
2275 &IntegrityAlgorithm
,
2282 if (EncryptAlgorithm
== PreferEncryptAlgorithm
&&
2283 EncryptKeylength
== PreferEncryptKeylength
&&
2284 IntegrityAlgorithm
== PreferIntegrityAlgorithm
&&
2285 PrfAlgorithm
== PreferPrfAlgorithm
&&
2286 DhGroup
== PreferDhGroup
2290 EncryptAlgorithm
= 0;
2291 IntegrityAlgorithm
= 0;
2294 EncryptKeylength
= 0;
2297 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((UINT8
*)(ProposalData
+ 1) +
2298 ProposalData
->NumTransforms
* sizeof (IKEV2_TRANSFORM_DATA
));
2302 IkeSaSession
->SessionCommon
.SaParams
= AllocateZeroPool (sizeof (IKEV2_SA_PARAMS
));
2303 ASSERT (IkeSaSession
->SessionCommon
.SaParams
!= NULL
);
2304 IkeSaSession
->SessionCommon
.SaParams
->EncAlgId
= PreferEncryptAlgorithm
;
2305 IkeSaSession
->SessionCommon
.SaParams
->EnckeyLen
= PreferEncryptKeylength
;
2306 IkeSaSession
->SessionCommon
.SaParams
->DhGroup
= PreferDhGroup
;
2307 IkeSaSession
->SessionCommon
.SaParams
->Prf
= PreferPrfAlgorithm
;
2308 IkeSaSession
->SessionCommon
.SaParams
->IntegAlgId
= PreferIntegrityAlgorithm
;
2309 IkeSaSession
->SessionCommon
.PreferDhGroup
= PreferDhGroup
;
2318 Parse the received Authentication Exchange Packet.
2320 This function parse the SA Payload and Key Payload to find out the cryptographic
2321 suite for the ESP and fill it into the Child SA Session's CommonSession->SaParams.
2323 @param[in, out] ChildSaSession Pointer to IKEV2_CHILD_SA_SESSION related to
2324 this Authentication Exchange.
2325 @param[in] SaPayload The received packet.
2326 @param[in] Type The IKE header's flag of received packet .
2328 @retval TRUE If the SA proposal in Packet is acceptable.
2329 @retval FALSE If the SA proposal in Packet is not acceptable.
2333 Ikev2ChildSaParseSaPayload (
2334 IN OUT IKEV2_CHILD_SA_SESSION
*ChildSaSession
,
2335 IN IKE_PAYLOAD
*SaPayload
,
2339 IKEV2_PROPOSAL_DATA
*ProposalData
;
2340 UINT8 ProposalIndex
;
2341 UINT16 PreferEncryptAlgorithm
;
2342 UINT16 PreferIntegrityAlgorithm
;
2343 UINTN PreferEncryptKeylength
;
2344 BOOLEAN PreferIsSupportEsn
;
2345 UINT16 EncryptAlgorithm
;
2346 UINT16 IntegrityAlgorithm
;
2347 UINTN EncryptKeylength
;
2348 BOOLEAN IsSupportEsn
;
2353 PreferIntegrityAlgorithm
= 0;
2354 PreferEncryptAlgorithm
= 0;
2355 PreferEncryptKeylength
= 0;
2356 IntegrityAlgorithm
= 0;
2357 EncryptAlgorithm
= 0;
2358 EncryptKeylength
= 0;
2360 IsSupportEsn
= FALSE
;
2361 PreferIsSupportEsn
= FALSE
;
2363 if (Type
== IKE_HEADER_FLAGS_INIT
) {
2364 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((IKEV2_SA_DATA
*) SaPayload
->PayloadBuf
+ 1);
2365 for (ProposalIndex
= 0; ProposalIndex
< ((IKEV2_SA_DATA
*) SaPayload
->PayloadBuf
)->NumProposals
; ProposalIndex
++) {
2367 // Iterate each proposal to find the preferred one.
2369 if (ProposalData
->ProtocolId
== IPSEC_PROTO_IPSEC_ESP
&& ProposalData
->NumTransforms
>= 3) {
2371 // Get the preferred algorithm.
2373 Ikev2ParseProposalData (
2375 &PreferEncryptAlgorithm
,
2376 &PreferIntegrityAlgorithm
,
2379 &PreferEncryptKeylength
,
2384 // Don't support the ESN now.
2386 if (PreferEncryptAlgorithm
!= 0 &&
2387 PreferIntegrityAlgorithm
!= 0 &&
2391 // Find the matched one.
2393 ChildSaSession
->SessionCommon
.SaParams
= AllocateZeroPool (sizeof (IKEV2_SA_PARAMS
));
2394 ASSERT (ChildSaSession
->SessionCommon
.SaParams
!= NULL
);
2395 ChildSaSession
->SessionCommon
.SaParams
->EncAlgId
= PreferEncryptAlgorithm
;
2396 ChildSaSession
->SessionCommon
.SaParams
->EnckeyLen
= PreferEncryptKeylength
;
2397 ChildSaSession
->SessionCommon
.SaParams
->IntegAlgId
= PreferIntegrityAlgorithm
;
2398 CopyMem (&ChildSaSession
->RemotePeerSpi
, ProposalData
->Spi
, sizeof (ChildSaSession
->RemotePeerSpi
));
2401 // Save the matched one in IKEV2_SA_DATA for furthure calculation.
2403 SaDataSize
= sizeof (IKEV2_SA_DATA
) +
2404 sizeof (IKEV2_PROPOSAL_DATA
) +
2405 sizeof (IKEV2_TRANSFORM_DATA
) * 4;
2407 ChildSaSession
->SaData
= AllocateZeroPool (SaDataSize
);
2408 ASSERT (ChildSaSession
->SaData
!= NULL
);
2410 ChildSaSession
->SaData
->NumProposals
= 1;
2413 // BUGBUG: Suppose there are 4 transforms in the matched proposal. If
2414 // the matched Proposal has more than 4 transforms that means there
2415 // are more than one transform with same type.
2418 (IKEV2_PROPOSAL_DATA
*) (ChildSaSession
->SaData
+ 1),
2420 SaDataSize
- sizeof (IKEV2_SA_DATA
)
2423 ((IKEV2_PROPOSAL_DATA
*) (ChildSaSession
->SaData
+ 1))->ProposalIndex
= 1;
2425 ((IKEV2_PROPOSAL_DATA
*) (ChildSaSession
->SaData
+ 1))->Spi
= AllocateCopyPool (
2426 sizeof (ChildSaSession
->LocalPeerSpi
),
2427 &ChildSaSession
->LocalPeerSpi
2429 ASSERT (((IKEV2_PROPOSAL_DATA
*) (ChildSaSession
->SaData
+ 1))->Spi
!= NULL
);
2433 PreferEncryptAlgorithm
= 0;
2434 PreferIntegrityAlgorithm
= 0;
2435 IsSupportEsn
= TRUE
;
2439 // Point to next Proposal
2441 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((UINT8
*)(ProposalData
+ 1) +
2442 ProposalData
->NumTransforms
* sizeof (IKEV2_TRANSFORM_DATA
));
2444 } else if (Type
== IKE_HEADER_FLAGS_RESPOND
) {
2446 // First check the SA proposal's ProtoctolID and Transform Numbers. Since it is
2447 // the responded SA proposal, suppose it only has one proposal and the transform Numbers
2450 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((IKEV2_SA_DATA
*)SaPayload
->PayloadBuf
+ 1);
2451 if (ProposalData
->ProtocolId
!= IPSEC_PROTO_IPSEC_ESP
|| ProposalData
->NumTransforms
!= 3) {
2455 // Get the preferred algorithms.
2457 Ikev2ParseProposalData (
2459 &PreferEncryptAlgorithm
,
2460 &PreferIntegrityAlgorithm
,
2463 &PreferEncryptKeylength
,
2464 &PreferIsSupportEsn
,
2468 ProposalData
= (IKEV2_PROPOSAL_DATA
*) (ChildSaSession
->SaData
+ 1);
2470 for (ProposalIndex
= 0; ProposalIndex
< ChildSaSession
->SaData
->NumProposals
&& (!IsMatch
); ProposalIndex
++) {
2471 Ikev2ParseProposalData (
2474 &IntegrityAlgorithm
,
2481 if (EncryptAlgorithm
== PreferEncryptAlgorithm
&&
2482 EncryptKeylength
== PreferEncryptKeylength
&&
2483 IntegrityAlgorithm
== PreferIntegrityAlgorithm
&&
2484 IsSupportEsn
== PreferIsSupportEsn
2488 PreferEncryptAlgorithm
= 0;
2489 PreferIntegrityAlgorithm
= 0;
2490 IsSupportEsn
= TRUE
;
2492 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((UINT8
*)(ProposalData
+ 1) +
2493 ProposalData
->NumTransforms
* sizeof (IKEV2_TRANSFORM_DATA
));
2496 ProposalData
= (IKEV2_PROPOSAL_DATA
*)((IKEV2_SA_DATA
*)SaPayload
->PayloadBuf
+ 1);
2498 ChildSaSession
->SessionCommon
.SaParams
= AllocateZeroPool (sizeof (IKEV2_SA_PARAMS
));
2499 ASSERT (ChildSaSession
->SessionCommon
.SaParams
!= NULL
);
2500 ChildSaSession
->SessionCommon
.SaParams
->EncAlgId
= PreferEncryptAlgorithm
;
2501 ChildSaSession
->SessionCommon
.SaParams
->EnckeyLen
= PreferEncryptKeylength
;
2502 ChildSaSession
->SessionCommon
.SaParams
->IntegAlgId
= PreferIntegrityAlgorithm
;
2503 CopyMem (&ChildSaSession
->RemotePeerSpi
, ProposalData
->Spi
, sizeof (ChildSaSession
->RemotePeerSpi
));
2512 Generate Key buffer from fragments.
2514 If the digest length of specified HashAlgId is larger than or equal with the
2515 required output key length, derive the key directly. Otherwise, Key Material
2516 needs to be PRF-based concatenation according to 2.13 of RFC 4306:
2517 prf+ (K,S) = T1 | T2 | T3 | T4 | ..., T1 = prf (K, S | 0x01),
2518 T2 = prf (K, T1 | S | 0x02), T3 = prf (K, T2 | S | 0x03),T4 = prf (K, T3 | S | 0x04)
2519 then derive the key from this key material.
2521 @param[in] HashAlgId The Hash Algorithm ID used to generate key.
2522 @param[in] HashKey Pointer to a key buffer which contains hash key.
2523 @param[in] HashKeyLength The length of HashKey in bytes.
2524 @param[in, out] OutputKey Pointer to buffer which is used to receive the
2526 @param[in] OutputKeyLength The length of OutPutKey buffer.
2527 @param[in] Fragments Pointer to the data to be used to generate key.
2528 @param[in] NumFragments The numbers of the Fragement.
2530 @retval EFI_SUCCESS The operation complete successfully.
2531 @retval EFI_INVALID_PARAMETER If NumFragments is zero.
2532 @retval EFI_OUT_OF_RESOURCES If the required resource can't be allocated.
2533 @retval Others The operation is failed.
2537 Ikev2SaGenerateKey (
2540 IN UINTN HashKeyLength
,
2541 IN OUT UINT8
*OutputKey
,
2542 IN UINTN OutputKeyLength
,
2543 IN PRF_DATA_FRAGMENT
*Fragments
,
2544 IN UINTN NumFragments
2548 PRF_DATA_FRAGMENT LocalFragments
[3];
2553 UINTN AuthKeyLength
;
2554 UINTN FragmentsSize
;
2557 Status
= EFI_SUCCESS
;
2559 if (NumFragments
== 0) {
2560 return EFI_INVALID_PARAMETER
;
2563 LocalFragments
[0].Data
= NULL
;
2564 LocalFragments
[1].Data
= NULL
;
2565 LocalFragments
[2].Data
= NULL
;
2567 AuthKeyLength
= IpSecGetHmacDigestLength (HashAlgId
);
2568 DigestSize
= AuthKeyLength
;
2569 Digest
= AllocateZeroPool (AuthKeyLength
);
2571 if (Digest
== NULL
) {
2572 return EFI_OUT_OF_RESOURCES
;
2575 // If the required output key length is less than the digest size,
2576 // copy the digest into OutputKey.
2578 if (OutputKeyLength
<= DigestSize
) {
2579 Status
= IpSecCryptoIoHmac (
2583 (HASH_DATA_FRAGMENT
*) Fragments
,
2588 if (EFI_ERROR (Status
)) {
2592 CopyMem (OutputKey
, Digest
, OutputKeyLength
);
2597 //Otherwise, Key Material need to be PRF-based concatenation according to 2.13
2598 //of RFC 4306: prf+ (K,S) = T1 | T2 | T3 | T4 | ..., T1 = prf (K, S | 0x01),
2599 //T2 = prf (K, T1 | S | 0x02), T3 = prf (K, T2 | S | 0x03),T4 = prf (K, T3 | S | 0x04)
2600 //then derive the key from this key material.
2603 for (Index
= 0; Index
< NumFragments
; Index
++) {
2604 FragmentsSize
= FragmentsSize
+ Fragments
[Index
].DataSize
;
2607 LocalFragments
[1].Data
= AllocateZeroPool (FragmentsSize
);
2608 ASSERT (LocalFragments
[1].Data
!= NULL
);
2609 LocalFragments
[1].DataSize
= FragmentsSize
;
2612 // Copy all input fragments into LocalFragments[1];
2615 for (Index
= 0; Index
< NumFragments
; Index
++) {
2617 LocalFragments
[1].Data
+ FragmentsSize
,
2618 Fragments
[Index
].Data
,
2619 Fragments
[Index
].DataSize
2621 FragmentsSize
= FragmentsSize
+ Fragments
[Index
].DataSize
;
2625 // Prepare 0x01 as the first tail data.
2628 LocalFragments
[2].Data
= &TailData
;
2629 LocalFragments
[2].DataSize
= sizeof (TailData
);
2631 // Allocate buffer for the first fragment
2633 LocalFragments
[0].Data
= AllocateZeroPool (AuthKeyLength
);
2634 ASSERT (LocalFragments
[0].Data
!= NULL
);
2635 LocalFragments
[0].DataSize
= AuthKeyLength
;
2637 Round
= (OutputKeyLength
- 1) / AuthKeyLength
+ 1;
2638 for (Index
= 0; Index
< Round
; Index
++) {
2639 Status
= IpSecCryptoIoHmac (
2643 (HASH_DATA_FRAGMENT
*)(Index
== 0 ? &LocalFragments
[1] : LocalFragments
),
2648 if (EFI_ERROR(Status
)) {
2652 LocalFragments
[0].Data
,
2656 if (OutputKeyLength
> DigestSize
* (Index
+ 1)) {
2658 OutputKey
+ Index
* DigestSize
,
2662 LocalFragments
[0].DataSize
= DigestSize
;
2669 OutputKey
+ Index
* DigestSize
,
2671 OutputKeyLength
- Index
* DigestSize
2678 // Only First and second Framgement Data need to be freed.
2680 for (Index
= 0 ; Index
< 2; Index
++) {
2681 if (LocalFragments
[Index
].Data
!= NULL
) {
2682 FreePool (LocalFragments
[Index
].Data
);
2685 if (Digest
!= NULL
) {