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 IKE_UDP_SERVICE
*UdpService
;
881 IPSEC_PRIVATE_DATA
*Private
;
883 if (IkeSaSession
== NULL
) {
884 return EFI_NOT_FOUND
;
887 IsLocalFound
= FALSE
;
888 IsRemoteFound
= FALSE
;
890 LocalSelector
= NULL
;
891 RemoteSelector
= NULL
;
892 UdpService
= IkeSaSession
->SessionCommon
.UdpService
;
894 Private
= IkeSaSession
->SessionCommon
.Private
;
897 // Remove the Established SA from ChildSaEstablishlist.
899 ChildSession
= Ikev2ChildSaSessionRemove(
900 &(IkeSaSession
->ChildSaEstablishSessionList
),
902 IKEV2_ESTABLISHED_CHILDSA_LIST
904 if (ChildSession
== NULL
) {
905 return EFI_NOT_FOUND
;
908 LocalSpi
= ChildSession
->LocalPeerSpi
;
909 RemoteSpi
= ChildSession
->RemotePeerSpi
;
911 SelectorSize
= sizeof (EFI_IPSEC_CONFIG_SELECTOR
);
912 Selector
= AllocateZeroPool (SelectorSize
);
913 ASSERT (Selector
!= NULL
);
918 Status
= EfiIpSecConfigGetNextSelector (
919 &Private
->IpSecConfig
,
920 IPsecConfigDataTypeSad
,
924 if (Status
== EFI_BUFFER_TOO_SMALL
) {
927 Selector
= AllocateZeroPool (SelectorSize
);
928 ASSERT (Selector
!= NULL
);
929 Status
= EfiIpSecConfigGetNextSelector (
930 &Private
->IpSecConfig
,
931 IPsecConfigDataTypeSad
,
937 if (EFI_ERROR (Status
)) {
941 if (Selector
->SaId
.Spi
== RemoteSpi
) {
943 // SPI is unique. There is only one SAD whose SPI is
944 // same with RemoteSpi.
946 IsRemoteFound
= TRUE
;
947 RemoteSelector
= AllocateZeroPool (SelectorSize
);
948 ASSERT (RemoteSelector
!= NULL
);
949 CopyMem (RemoteSelector
, Selector
, SelectorSize
);
952 if (Selector
->SaId
.Spi
== LocalSpi
) {
954 // SPI is unique. There is only one SAD whose SPI is
955 // same with LocalSpi.
958 LocalSelector
= AllocateZeroPool (SelectorSize
);
959 ASSERT (LocalSelector
!= NULL
);
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
) {