node-ikev2 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,90 @@
1
+ import { Transform } from "./transform";
2
+ import { securityProtocolId } from "./payload";
3
+ /**
4
+ * Proposal Substructure
5
+ 1 2 3
6
+ 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
7
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
8
+ ! 0 (last) or 2 ! RESERVED (1) ! Proposal Length !
9
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
10
+ ! Proposal # ! Protocol ID ! SPI Size !# of Transforms!
11
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
12
+ ~ SPI (variable) ~
13
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
14
+ ! !
15
+ ~ <Transforms> ~
16
+ ! !
17
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
18
+ Proposal Substructure
19
+
20
+ For an initial IKE_SA negotiation, this field MUST be zero; the SPI is \
21
+ obtained from the outer header. During subsequent negotiations, it is \
22
+ equal to the size, in octets, of the SPI of the corresponding protocol \
23
+ (8 for IKE, 4 for ESP and AH).
24
+ */
25
+ /**
26
+ * IKEv2 Proposal
27
+ * @class
28
+ * @property {number} lastSubstructure - Last Substructure (1 bit)
29
+ * @property {number} length - 2 bytes
30
+ * @property {number} proposalNumber - 1 byte
31
+ * @property {securityProtocolId} protocolId - 1 byte
32
+ * @property {Buffer} spiSize - 1 bytes (SPI Size)
33
+ * @property {number} numTransforms - 1 byte (number of transforms)
34
+ * @property {Buffer} spi - variable length
35
+ * @property {Transform} transforms - variable length
36
+ */
37
+ export declare class Proposal {
38
+ lastSubstructure: number;
39
+ length: number;
40
+ proposalNumber: number;
41
+ protocolId: securityProtocolId;
42
+ spiSize: number;
43
+ numTransforms: number;
44
+ spi: Buffer;
45
+ transforms: Transform[];
46
+ constructor(lastSubstructure: number, length: number, proposalNumber: number, protocolId: securityProtocolId, spiSize: number, numTransforms: number, spi: Buffer, transforms: Transform[]);
47
+ /**
48
+ * Parses a Proposal from a Buffer
49
+ * @param {Buffer} buffer
50
+ * @static
51
+ * @public
52
+ * @returns {Proposal}
53
+ */
54
+ static parse(buffer: Buffer): Proposal;
55
+ /**
56
+ * Serializes a Proposal from a JSON object
57
+ * @param {any} json
58
+ * @static
59
+ * @public
60
+ * @returns {Buffer}
61
+ */
62
+ static serializeJSON(json: Record<string, any>): Buffer;
63
+ /**
64
+ * Parses Transforms from a Buffer
65
+ * @param {Buffer} buffer
66
+ * @static
67
+ * @public
68
+ * @returns {Transform[]}
69
+ */
70
+ private static parseTransforms;
71
+ /**
72
+ * Serializes a Proposal to a Buffer
73
+ * @param proposal
74
+ * @public
75
+ * @returns {Buffer}
76
+ */
77
+ serialize(): Buffer;
78
+ /**
79
+ * Converts Proposal to JSON
80
+ * @public
81
+ * @returns {Record<string, any>} JSON object
82
+ */
83
+ toJSON(): Record<string, any>;
84
+ /**
85
+ * Returns a string representation of the proposal
86
+ * @public
87
+ * @returns {string}
88
+ */
89
+ toString(): string;
90
+ }
@@ -0,0 +1,200 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Proposal = void 0;
4
+ const transform_1 = require("./transform");
5
+ const payload_1 = require("./payload");
6
+ /**
7
+ * Proposal Substructure
8
+ 1 2 3
9
+ 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
10
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
11
+ ! 0 (last) or 2 ! RESERVED (1) ! Proposal Length !
12
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
13
+ ! Proposal # ! Protocol ID ! SPI Size !# of Transforms!
14
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
15
+ ~ SPI (variable) ~
16
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
17
+ ! !
18
+ ~ <Transforms> ~
19
+ ! !
20
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
21
+ Proposal Substructure
22
+
23
+ For an initial IKE_SA negotiation, this field MUST be zero; the SPI is \
24
+ obtained from the outer header. During subsequent negotiations, it is \
25
+ equal to the size, in octets, of the SPI of the corresponding protocol \
26
+ (8 for IKE, 4 for ESP and AH).
27
+ */
28
+ /**
29
+ * IKEv2 Proposal
30
+ * @class
31
+ * @property {number} lastSubstructure - Last Substructure (1 bit)
32
+ * @property {number} length - 2 bytes
33
+ * @property {number} proposalNumber - 1 byte
34
+ * @property {securityProtocolId} protocolId - 1 byte
35
+ * @property {Buffer} spiSize - 1 bytes (SPI Size)
36
+ * @property {number} numTransforms - 1 byte (number of transforms)
37
+ * @property {Buffer} spi - variable length
38
+ * @property {Transform} transforms - variable length
39
+ */
40
+ class Proposal {
41
+ constructor(lastSubstructure, length, proposalNumber, protocolId, spiSize, numTransforms, spi, transforms) {
42
+ this.lastSubstructure = lastSubstructure;
43
+ this.length = length;
44
+ this.proposalNumber = proposalNumber;
45
+ this.protocolId = protocolId;
46
+ this.spiSize = spiSize;
47
+ this.numTransforms = numTransforms;
48
+ this.spi = spi;
49
+ this.transforms = transforms;
50
+ if (transforms.length === 0) {
51
+ throw new Error("Transforms length cannot be 0");
52
+ }
53
+ }
54
+ /**
55
+ * Parses a Proposal from a Buffer
56
+ * @param {Buffer} buffer
57
+ * @static
58
+ * @public
59
+ * @returns {Proposal}
60
+ */
61
+ static parse(buffer) {
62
+ try {
63
+ const lastSubstructure = buffer.readUInt8(0); // First octet
64
+ const length = buffer.readUInt16BE(2);
65
+ const proposalNumber = buffer.readUInt8(4);
66
+ const protocolId = buffer.readUInt8(5);
67
+ const spiSize = buffer.readUInt8(6);
68
+ const numTransforms = buffer.readUInt8(7);
69
+ const spi = Buffer.alloc(spiSize);
70
+ if (spiSize > 0) {
71
+ buffer.copy(spi, 0, 8, 8 + spiSize);
72
+ }
73
+ const transforms = this.parseTransforms(buffer.subarray(8 + spiSize, length));
74
+ return new Proposal(lastSubstructure, length, proposalNumber, protocolId, spiSize, numTransforms, spi, transforms);
75
+ }
76
+ catch (error) {
77
+ throw new Error(`Failed to parse proposal from buffer ${buffer.toString("hex")}: ${error}`);
78
+ }
79
+ }
80
+ /**
81
+ * Serializes a Proposal from a JSON object
82
+ * @param {any} json
83
+ * @static
84
+ * @public
85
+ * @returns {Buffer}
86
+ */
87
+ static serializeJSON(json) {
88
+ const lastSubstructure = json.lastSubstructure;
89
+ const length = json.length;
90
+ const proposalNumber = json.proposalNumber;
91
+ const protocolId = json.protocolId;
92
+ const spiSize = json.spiSize;
93
+ const numTransforms = json.numTransforms;
94
+ let spi = Buffer.alloc(spiSize);
95
+ if (spiSize > 0 && json.spi) {
96
+ spi = Buffer.from(json.spi, "hex");
97
+ }
98
+ const transformsBuffer = json.transforms.map((transform) => transform_1.Transform.serializeJSON(transform));
99
+ const totalLength = 8 +
100
+ spiSize +
101
+ transformsBuffer.reduce((acc, buf) => acc + buf.length, 0);
102
+ const buffer = Buffer.alloc(totalLength);
103
+ buffer.writeUInt8(lastSubstructure, 0);
104
+ buffer.writeUInt8(0, 1);
105
+ buffer.writeUInt16BE(length, 2);
106
+ buffer.writeUInt8(proposalNumber, 4);
107
+ buffer.writeUInt8(protocolId, 5);
108
+ buffer.writeUInt8(spiSize, 6);
109
+ buffer.writeUInt8(numTransforms, 7);
110
+ if (spiSize > 0) {
111
+ spi.copy(buffer, 8);
112
+ }
113
+ // Copy transforms directly into buffer instead of using Buffer.concat
114
+ let offset = 8 + spiSize;
115
+ for (const transformBuffer of transformsBuffer) {
116
+ transformBuffer.copy(buffer, offset);
117
+ offset += transformBuffer.length;
118
+ }
119
+ return buffer;
120
+ }
121
+ /**
122
+ * Parses Transforms from a Buffer
123
+ * @param {Buffer} buffer
124
+ * @static
125
+ * @public
126
+ * @returns {Transform[]}
127
+ */
128
+ static parseTransforms(buffer) {
129
+ const transforms = [];
130
+ let offset = 0;
131
+ while (offset < buffer.length) {
132
+ const transformLength = buffer.readUInt16BE(offset + 2);
133
+ const transformBuffer = buffer.subarray(offset, offset + transformLength);
134
+ const transform = transform_1.Transform.parse(transformBuffer);
135
+ transforms.push(transform);
136
+ offset += transformLength;
137
+ }
138
+ return transforms;
139
+ }
140
+ /**
141
+ * Serializes a Proposal to a Buffer
142
+ * @param proposal
143
+ * @public
144
+ * @returns {Buffer}
145
+ */
146
+ serialize() {
147
+ // Encode deep transforms first
148
+ const transforms = this.transforms.map((transform) => transform.serialize());
149
+ const transformsBuffer = Buffer.concat(transforms);
150
+ // Fix the length
151
+ this.length = 8 + this.spiSize + transformsBuffer.length;
152
+ // Fix the numTransforms
153
+ this.numTransforms = this.transforms.length;
154
+ const buffer = Buffer.alloc(this.length);
155
+ buffer.writeUInt8(this.lastSubstructure, 0);
156
+ buffer.writeUInt8(0, 1);
157
+ buffer.writeUInt16BE(this.length, 2);
158
+ buffer.writeUInt8(this.proposalNumber, 4);
159
+ buffer.writeUInt8(this.protocolId, 5);
160
+ buffer.writeUInt8(this.spiSize, 6);
161
+ buffer.writeUInt8(this.numTransforms, 7);
162
+ if (this.spiSize > 0) {
163
+ this.spi.copy(buffer, 8, 0, this.spiSize);
164
+ }
165
+ transformsBuffer.copy(buffer, 8 + this.spiSize);
166
+ return buffer;
167
+ }
168
+ /**
169
+ * Converts Proposal to JSON
170
+ * @public
171
+ * @returns {Record<string, any>} JSON object
172
+ */
173
+ toJSON() {
174
+ var _a, _b;
175
+ return {
176
+ lastSubstructure: this.lastSubstructure,
177
+ length: this.length,
178
+ proposalNumber: this.proposalNumber,
179
+ protocolId: this.protocolId,
180
+ spiSize: this.spiSize,
181
+ numTransforms: this.numTransforms,
182
+ spi: (_b = (_a = this.spi) === null || _a === void 0 ? void 0 : _a.toString("hex")) !== null && _b !== void 0 ? _b : "",
183
+ transforms: this.transforms.map((transform) => transform.toJSON()),
184
+ };
185
+ }
186
+ /**
187
+ * Returns a string representation of the proposal
188
+ * @public
189
+ * @returns {string}
190
+ */
191
+ toString() {
192
+ const prettyJson = this.toJSON();
193
+ prettyJson.lastSubstructure =
194
+ this.lastSubstructure === 0 ? "None (0)" : "Proposal (2)";
195
+ prettyJson.protocolId = `${payload_1.securityProtocolId[prettyJson.protocolId]} (${prettyJson.protocolId})`;
196
+ prettyJson.transforms = this.transforms.map((transform) => JSON.parse(transform.toString()));
197
+ return JSON.stringify(prettyJson, null, 2);
198
+ }
199
+ }
200
+ exports.Proposal = Proposal;
@@ -0,0 +1,80 @@
1
+ /**
2
+ * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
3
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
4
+ | TS Type |IP Protocol ID*| Selector Length |
5
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
6
+ | Start Port* | End Port* |
7
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
8
+ | |
9
+ ~ Starting Address* ~
10
+ | |
11
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
12
+ | |
13
+ ~ Ending Address* ~
14
+ | |
15
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16
+
17
+ Traffic Selector
18
+ */
19
+ /**
20
+ * Traffic Selector Type
21
+ */
22
+ export declare enum TrafficSelectorType {
23
+ TS_IPV4_ADDR_RANGE = 7,
24
+ TS_IPV6_ADDR_RANGE = 8
25
+ }
26
+ /**
27
+ * Traffic Selector
28
+ * @class
29
+ * @property {number} type - 1 byte
30
+ * @property {number} protocolId - 1 byte
31
+ * @property {number} length - 2 bytes
32
+ * @property {number} startPort - 2 bytes
33
+ * @property {number} endPort - 2 bytes
34
+ * @property {Buffer} startAddress - 4 bytes
35
+ * @property {Buffer} endAddress - 4 bytes
36
+ */
37
+ export declare class TrafficSelector {
38
+ type: number;
39
+ protocolId: number;
40
+ length: number;
41
+ startPort: number;
42
+ endPort: number;
43
+ startAddress: Buffer;
44
+ endAddress: Buffer;
45
+ constructor(type: number, protocolId: number, length: number, startPort: number, endPort: number, startAddress: Buffer, endAddress: Buffer);
46
+ /**
47
+ * Parses a Traffic Selector from a buffer
48
+ * @param buffer The buffer to parse from.
49
+ * @static
50
+ * @public
51
+ * @returns {TrafficSelector}
52
+ */
53
+ static parse(buffer: Buffer): TrafficSelector;
54
+ /**
55
+ * Serializes a JSON representation of the Traffic Selector to a buffer
56
+ * @param json The JSON object to serialize.
57
+ * @public
58
+ * @static
59
+ * @returns {Buffer}
60
+ */
61
+ static serializeJSON(json: Record<string, any>): Buffer;
62
+ /**
63
+ * Serializes a Traffic Selector to a buffer
64
+ * @public
65
+ * @returns {Buffer}
66
+ */
67
+ serialize(): Buffer;
68
+ /**
69
+ * Returns the Traffic Selector as a JSON object
70
+ * @public
71
+ * @returns {Record<string, any>}
72
+ */
73
+ toJSON(): Record<string, any>;
74
+ /**
75
+ * Returns a string representation of the Traffic Selector
76
+ * @public
77
+ * @returns {string}
78
+ */
79
+ toString(): string;
80
+ }
@@ -0,0 +1,132 @@
1
+ "use strict";
2
+ /**
3
+ * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
4
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
5
+ | TS Type |IP Protocol ID*| Selector Length |
6
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
7
+ | Start Port* | End Port* |
8
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9
+ | |
10
+ ~ Starting Address* ~
11
+ | |
12
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
13
+ | |
14
+ ~ Ending Address* ~
15
+ | |
16
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
17
+
18
+ Traffic Selector
19
+ */
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.TrafficSelector = exports.TrafficSelectorType = void 0;
22
+ /**
23
+ * Traffic Selector Type
24
+ */
25
+ var TrafficSelectorType;
26
+ (function (TrafficSelectorType) {
27
+ TrafficSelectorType[TrafficSelectorType["TS_IPV4_ADDR_RANGE"] = 7] = "TS_IPV4_ADDR_RANGE";
28
+ TrafficSelectorType[TrafficSelectorType["TS_IPV6_ADDR_RANGE"] = 8] = "TS_IPV6_ADDR_RANGE";
29
+ })(TrafficSelectorType || (exports.TrafficSelectorType = TrafficSelectorType = {}));
30
+ /**
31
+ * Traffic Selector
32
+ * @class
33
+ * @property {number} type - 1 byte
34
+ * @property {number} protocolId - 1 byte
35
+ * @property {number} length - 2 bytes
36
+ * @property {number} startPort - 2 bytes
37
+ * @property {number} endPort - 2 bytes
38
+ * @property {Buffer} startAddress - 4 bytes
39
+ * @property {Buffer} endAddress - 4 bytes
40
+ */
41
+ class TrafficSelector {
42
+ constructor(type, protocolId, length, startPort, endPort, startAddress, endAddress) {
43
+ this.type = type;
44
+ this.protocolId = protocolId;
45
+ this.length = length;
46
+ this.startPort = startPort;
47
+ this.endPort = endPort;
48
+ this.startAddress = startAddress;
49
+ this.endAddress = endAddress;
50
+ }
51
+ /**
52
+ * Parses a Traffic Selector from a buffer
53
+ * @param buffer The buffer to parse from.
54
+ * @static
55
+ * @public
56
+ * @returns {TrafficSelector}
57
+ */
58
+ static parse(buffer) {
59
+ try {
60
+ const type = buffer.readUInt8(0);
61
+ const protocolId = buffer.readUInt8(1);
62
+ const length = buffer.readUInt16BE(2);
63
+ const startPort = buffer.readUInt16BE(4);
64
+ const endPort = buffer.readUInt16BE(6);
65
+ const startAddress = buffer.subarray(8, 12);
66
+ const endAddress = buffer.subarray(12, 16);
67
+ return new TrafficSelector(type, protocolId, length, startPort, endPort, startAddress, endAddress);
68
+ }
69
+ catch (error) {
70
+ throw new Error("Failed to parse traffic selector");
71
+ }
72
+ }
73
+ /**
74
+ * Serializes a JSON representation of the Traffic Selector to a buffer
75
+ * @param json The JSON object to serialize.
76
+ * @public
77
+ * @static
78
+ * @returns {Buffer}
79
+ */
80
+ static serializeJSON(json) {
81
+ const buffer = Buffer.alloc(16);
82
+ buffer.writeUInt8(json.type, 0);
83
+ buffer.writeUInt8(json.protocolId, 1);
84
+ buffer.writeUInt16BE(json.length, 2);
85
+ buffer.writeUInt16BE(json.startPort, 4);
86
+ buffer.writeUInt16BE(json.endPort, 6);
87
+ Buffer.from(json.startAddress, "hex").copy(buffer, 8);
88
+ Buffer.from(json.endAddress, "hex").copy(buffer, 12);
89
+ return buffer;
90
+ }
91
+ /**
92
+ * Serializes a Traffic Selector to a buffer
93
+ * @public
94
+ * @returns {Buffer}
95
+ */
96
+ serialize() {
97
+ const buffer = Buffer.alloc(16);
98
+ buffer.writeUInt8(this.type, 0);
99
+ buffer.writeUInt8(this.protocolId, 1);
100
+ buffer.writeUInt16BE(this.length, 2);
101
+ buffer.writeUInt16BE(this.startPort, 4);
102
+ buffer.writeUInt16BE(this.endPort, 6);
103
+ this.startAddress.copy(buffer, 8);
104
+ this.endAddress.copy(buffer, 12);
105
+ return buffer;
106
+ }
107
+ /**
108
+ * Returns the Traffic Selector as a JSON object
109
+ * @public
110
+ * @returns {Record<string, any>}
111
+ */
112
+ toJSON() {
113
+ return {
114
+ type: this.type,
115
+ protocolId: this.protocolId,
116
+ length: this.length,
117
+ startPort: this.startPort,
118
+ endPort: this.endPort,
119
+ startAddress: this.startAddress.toString("hex"),
120
+ endAddress: this.endAddress.toString("hex"),
121
+ };
122
+ }
123
+ /**
124
+ * Returns a string representation of the Traffic Selector
125
+ * @public
126
+ * @returns {string}
127
+ */
128
+ toString() {
129
+ return JSON.stringify(this.toJSON(), null, 2);
130
+ }
131
+ }
132
+ exports.TrafficSelector = TrafficSelector;