-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNASHelperS1AP_SecurityModeComplete.cpp
More file actions
123 lines (110 loc) · 4.59 KB
/
NASHelperS1AP_SecurityModeComplete.cpp
File metadata and controls
123 lines (110 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "NASHelperS1AP.hpp"
#include <utility>
namespace {
bool appendTlvFixedTotalOptional(const std::uint8_t* ie,
std::size_t n,
std::size_t& i,
std::uint8_t expectedIei,
std::size_t totalOctets,
NASHelperS1AP::SecurityModeCompleteInformationElementsParse& out) {
if (i + totalOctets > n || ie[i] != expectedIei) {
return false;
}
const std::uint8_t len = ie[i + 1];
if (2U + static_cast<std::size_t>(len) != totalOctets) {
return false;
}
NASHelperS1AP::SecurityModeCompleteOptionalInformationElement opt{};
opt.iei = expectedIei;
opt.encoding = NASHelperS1AP::SecurityModeCompleteOptionalInformationElement::Encoding::Tlv;
opt.value.assign(ie + i + 2U, ie + i + totalOctets);
out.optionals.push_back(std::move(opt));
i += totalOctets;
return true;
}
bool appendTlvVariableOptional(const std::uint8_t* ie,
std::size_t n,
std::size_t& i,
std::uint8_t expectedIei,
NASHelperS1AP::SecurityModeCompleteInformationElementsParse& out) {
if (i + 2U > n || ie[i] != expectedIei) {
return false;
}
const std::uint8_t len = ie[i + 1];
if (len == 0U || i + 2U + static_cast<std::size_t>(len) > n) {
return false;
}
NASHelperS1AP::SecurityModeCompleteOptionalInformationElement opt{};
opt.iei = expectedIei;
opt.encoding = NASHelperS1AP::SecurityModeCompleteOptionalInformationElement::Encoding::Tlv;
opt.value.assign(ie + i + 2U, ie + i + 2U + static_cast<std::size_t>(len));
out.optionals.push_back(std::move(opt));
i += 2U + static_cast<std::size_t>(len);
return true;
}
bool appendTlvEOptional(const std::uint8_t* ie,
std::size_t n,
std::size_t& i,
std::uint8_t expectedIei,
NASHelperS1AP::SecurityModeCompleteInformationElementsParse& out) {
if (i + 3U > n || ie[i] != expectedIei) {
return false;
}
const std::uint16_t len =
static_cast<std::uint16_t>((static_cast<unsigned>(ie[i + 1]) << 8U) | static_cast<unsigned>(ie[i + 2]));
const std::size_t chunk = 3U + static_cast<std::size_t>(len);
if (chunk < 3U || i + chunk > n || i + chunk < i) {
return false;
}
NASHelperS1AP::SecurityModeCompleteOptionalInformationElement opt{};
opt.iei = expectedIei;
opt.encoding = NASHelperS1AP::SecurityModeCompleteOptionalInformationElement::Encoding::TlvE;
opt.value.assign(ie + i + 3U, ie + i + 3U + len);
out.optionals.push_back(std::move(opt));
i += chunk;
return true;
}
} // namespace
bool NASHelperS1AP::tryParseSecurityModeCompleteInformationElements(const std::uint8_t* const informationElements,
const std::size_t informationElementOctetCount,
SecurityModeCompleteInformationElementsParse& out) {
out = SecurityModeCompleteInformationElementsParse{};
if (informationElementOctetCount == 0U) {
out.ok = true;
return true;
}
if (informationElements == nullptr) {
return false;
}
std::size_t i = 0U;
while (i < informationElementOctetCount) {
const std::uint8_t b = informationElements[i];
bool consumed = false;
switch (b) {
case 0x23U:
consumed = appendTlvFixedTotalOptional(informationElements, informationElementOctetCount, i, 0x23U, 11U,
out);
break;
case 0x79U:
consumed = appendTlvEOptional(informationElements, informationElementOctetCount, i, 0x79U, out);
break;
case 0x66U:
consumed = appendTlvVariableOptional(informationElements, informationElementOctetCount, i, 0x66U, out);
break;
case 0x67U:
consumed = appendTlvFixedTotalOptional(informationElements, informationElementOctetCount, i, 0x67U, 8U,
out);
break;
default:
break;
}
if (consumed) {
continue;
}
break;
}
out.unparsed_begin = informationElements + i;
out.unparsed_octet_count = informationElementOctetCount - i;
out.ok = true;
return true;
}