Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/js/libs/ikev2/ikev2.go
2070 views
1
package ikev2
2
3
import (
4
"fmt"
5
"io"
6
7
"github.com/projectdiscovery/n3iwf/pkg/ike/message"
8
"github.com/projectdiscovery/n3iwf/pkg/logger"
9
)
10
11
func init() {
12
logger.Log.SetOutput(io.Discard)
13
}
14
15
type (
16
// IKEMessage is the IKEv2 message
17
//
18
// IKEv2 implements a limited subset of IKEv2 Protocol, specifically
19
// the IKE_NOTIFY and IKE_NONCE payloads and the IKE_SA_INIT exchange.
20
IKEMessage struct {
21
InitiatorSPI uint64
22
Version uint8
23
ExchangeType uint8
24
Flags uint8
25
payloads []IKEPayload
26
}
27
)
28
29
// AppendPayload appends a payload to the IKE message
30
// payload can be any of the payloads like IKENotification, IKENonce, etc.
31
// @example
32
// ```javascript
33
// const ikev2 = require('nuclei/ikev2');
34
// const message = new ikev2.IKEMessage();
35
// const nonce = new ikev2.IKENonce();
36
// nonce.NonceData = [1, 2, 3];
37
// message.AppendPayload(nonce);
38
// ```
39
func (m *IKEMessage) AppendPayload(payload any) error {
40
if _, ok := payload.(IKEPayload); !ok {
41
return fmt.Errorf("invalid payload type only types defined in ikev module like IKENotification, IKENonce, etc. are allowed")
42
}
43
m.payloads = append(m.payloads, payload.(IKEPayload))
44
return nil
45
}
46
47
// Encode encodes the final IKE message
48
// @example
49
// ```javascript
50
// const ikev2 = require('nuclei/ikev2');
51
// const message = new ikev2.IKEMessage();
52
// const nonce = new ikev2.IKENonce();
53
// nonce.NonceData = [1, 2, 3];
54
// message.AppendPayload(nonce);
55
// log(message.Encode());
56
// ```
57
func (m *IKEMessage) Encode() ([]byte, error) {
58
var payloads message.IKEPayloadContainer
59
for _, payload := range m.payloads {
60
p, err := payload.encode()
61
if err != nil {
62
return nil, err
63
}
64
payloads = append(payloads, p)
65
}
66
67
msg := &message.IKEMessage{
68
InitiatorSPI: m.InitiatorSPI,
69
Version: m.Version,
70
ExchangeType: m.ExchangeType,
71
Flags: m.Flags,
72
Payloads: payloads,
73
}
74
encoded, err := msg.Encode()
75
return encoded, err
76
}
77
78
// IKEPayload is the IKEv2 payload interface
79
// All the payloads like IKENotification, IKENonce, etc. implement
80
// this interface.
81
type IKEPayload interface {
82
encode() (message.IKEPayload, error)
83
}
84
85
type (
86
// IKEv2Notify is the IKEv2 Notification payload
87
// this implements the IKEPayload interface
88
// @example
89
// ```javascript
90
// const ikev2 = require('nuclei/ikev2');
91
// const notify = new ikev2.IKENotification();
92
// notify.NotifyMessageType = ikev2.IKE_NOTIFY_NO_PROPOSAL_CHOSEN;
93
// notify.NotificationData = [1, 2, 3];
94
// ```
95
IKENotification struct {
96
NotifyMessageType uint16
97
NotificationData []byte
98
}
99
)
100
101
// encode encodes the IKEv2 Notification payload
102
func (i *IKENotification) encode() (message.IKEPayload, error) {
103
notify := message.Notification{
104
NotifyMessageType: i.NotifyMessageType,
105
NotificationData: i.NotificationData,
106
}
107
return &notify, nil
108
}
109
110
const (
111
// Notify message types
112
IKE_NOTIFY_NO_PROPOSAL_CHOSEN = 14
113
IKE_NOTIFY_USE_TRANSPORT_MODE = 16391
114
115
IKE_VERSION_2 = 0x20
116
117
// Exchange Type
118
IKE_EXCHANGE_SA_INIT = 34
119
IKE_EXCHANGE_AUTH = 35
120
IKE_EXCHANGE_CREATE_CHILD_SA = 36
121
IKE_EXCHANGE_INFORMATIONAL = 37
122
123
// Flags
124
IKE_FLAGS_InitiatorBitCheck = 0x08
125
)
126
127
type (
128
// IKENonce is the IKEv2 Nonce payload
129
// this implements the IKEPayload interface
130
// @example
131
// ```javascript
132
// const ikev2 = require('nuclei/ikev2');
133
// const nonce = new ikev2.IKENonce();
134
// nonce.NonceData = [1, 2, 3];
135
// ```
136
IKENonce struct {
137
NonceData []byte
138
}
139
)
140
141
// encode encodes the IKEv2 Nonce payload
142
func (i *IKENonce) encode() (message.IKEPayload, error) {
143
nonce := message.Nonce{
144
NonceData: i.NonceData,
145
}
146
return &nonce, nil
147
}
148
149