package ikev212import (3"fmt"4"io"56"github.com/projectdiscovery/n3iwf/pkg/ike/message"7"github.com/projectdiscovery/n3iwf/pkg/logger"8)910func init() {11logger.Log.SetOutput(io.Discard)12}1314type (15// IKEMessage is the IKEv2 message16//17// IKEv2 implements a limited subset of IKEv2 Protocol, specifically18// the IKE_NOTIFY and IKE_NONCE payloads and the IKE_SA_INIT exchange.19IKEMessage struct {20InitiatorSPI uint6421Version uint822ExchangeType uint823Flags uint824payloads []IKEPayload25}26)2728// AppendPayload appends a payload to the IKE message29// payload can be any of the payloads like IKENotification, IKENonce, etc.30// @example31// ```javascript32// const ikev2 = require('nuclei/ikev2');33// const message = new ikev2.IKEMessage();34// const nonce = new ikev2.IKENonce();35// nonce.NonceData = [1, 2, 3];36// message.AppendPayload(nonce);37// ```38func (m *IKEMessage) AppendPayload(payload any) error {39if _, ok := payload.(IKEPayload); !ok {40return fmt.Errorf("invalid payload type only types defined in ikev module like IKENotification, IKENonce, etc. are allowed")41}42m.payloads = append(m.payloads, payload.(IKEPayload))43return nil44}4546// Encode encodes the final IKE message47// @example48// ```javascript49// const ikev2 = require('nuclei/ikev2');50// const message = new ikev2.IKEMessage();51// const nonce = new ikev2.IKENonce();52// nonce.NonceData = [1, 2, 3];53// message.AppendPayload(nonce);54// log(message.Encode());55// ```56func (m *IKEMessage) Encode() ([]byte, error) {57var payloads message.IKEPayloadContainer58for _, payload := range m.payloads {59p, err := payload.encode()60if err != nil {61return nil, err62}63payloads = append(payloads, p)64}6566msg := &message.IKEMessage{67InitiatorSPI: m.InitiatorSPI,68Version: m.Version,69ExchangeType: m.ExchangeType,70Flags: m.Flags,71Payloads: payloads,72}73encoded, err := msg.Encode()74return encoded, err75}7677// IKEPayload is the IKEv2 payload interface78// All the payloads like IKENotification, IKENonce, etc. implement79// this interface.80type IKEPayload interface {81encode() (message.IKEPayload, error)82}8384type (85// IKEv2Notify is the IKEv2 Notification payload86// this implements the IKEPayload interface87// @example88// ```javascript89// const ikev2 = require('nuclei/ikev2');90// const notify = new ikev2.IKENotification();91// notify.NotifyMessageType = ikev2.IKE_NOTIFY_NO_PROPOSAL_CHOSEN;92// notify.NotificationData = [1, 2, 3];93// ```94IKENotification struct {95NotifyMessageType uint1696NotificationData []byte97}98)99100// encode encodes the IKEv2 Notification payload101func (i *IKENotification) encode() (message.IKEPayload, error) {102notify := message.Notification{103NotifyMessageType: i.NotifyMessageType,104NotificationData: i.NotificationData,105}106return ¬ify, nil107}108109const (110// Notify message types111IKE_NOTIFY_NO_PROPOSAL_CHOSEN = 14112IKE_NOTIFY_USE_TRANSPORT_MODE = 16391113114IKE_VERSION_2 = 0x20115116// Exchange Type117IKE_EXCHANGE_SA_INIT = 34118IKE_EXCHANGE_AUTH = 35119IKE_EXCHANGE_CREATE_CHILD_SA = 36120IKE_EXCHANGE_INFORMATIONAL = 37121122// Flags123IKE_FLAGS_InitiatorBitCheck = 0x08124)125126type (127// IKENonce is the IKEv2 Nonce payload128// this implements the IKEPayload interface129// @example130// ```javascript131// const ikev2 = require('nuclei/ikev2');132// const nonce = new ikev2.IKENonce();133// nonce.NonceData = [1, 2, 3];134// ```135IKENonce struct {136NonceData []byte137}138)139140// encode encodes the IKEv2 Nonce payload141func (i *IKENonce) encode() (message.IKEPayload, error) {142nonce := message.Nonce{143NonceData: i.NonceData,144}145return &nonce, nil146}147148149