-
Notifications
You must be signed in to change notification settings - Fork 35
/
ServerDataChecker.cpp
177 lines (148 loc) · 4.13 KB
/
ServerDataChecker.cpp
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
Copyright (C) 2019-Present SKALE Labs
This file is part of sgxwallet.
sgxwallet is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
sgxwallet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with sgxwallet. If not, see <https://www.gnu.org/licenses/>.
@file ServerDataChecker.cpp
@author Stan Kladko
@date 2019
*/
#include "ServerDataChecker.h"
#include <gmp.h>
#include <vector>
#include <iostream>
#include "common.h"
#include "third_party/spdlog/spdlog.h"
vector<string> SplitString(const string &str, const string &delim = ":") {
vector<string> tokens;
size_t prev = 0, pos = 0;
do {
pos = str.find(delim, prev);
if (pos == string::npos)
pos = str.length();
string token = str.substr(prev, pos - prev);
if (!token.empty())
tokens.push_back(token);
prev = pos + delim.length();
} while (pos < str.length() && prev < str.length());
return tokens;
}
bool checkECDSAKeyName(const string &keyName) {
vector<string> parts = SplitString(keyName);
if (parts.size() != 2) {
spdlog::info("ECDSAKeyName num parts != 2");
return false;
}
if (parts.at(0) != "NEK") {
spdlog::info("key doesn't start from NEK");
return false;
}
if (parts.at(1).length() > 64 || parts.at(1).length() < 1) {
spdlog::info("wrong key length");
return false;
}
mpz_t num;
mpz_init(num);
if (mpz_set_str(num, parts.at(1).c_str(), 16) == -1) {
mpz_clear(num);
return false;
}
mpz_clear(num);
return true;
}
bool checkHex(const string &hex, const uint32_t sizeInBytes) {
if (hex.length() > sizeInBytes * 2 || hex.length() == 0) {
spdlog::error("key is too long or zero {} ", hex.length());
return false;
}
mpz_t num;
mpz_init(num);
if (mpz_set_str(num, hex.c_str(), 16) == -1) {
spdlog::error("key is not hex {}", hex);
mpz_clear(num);
return false;
}
mpz_clear(num);
return true;
}
bool checkName(const string &Name, const string &prefix) {
vector<string> parts = SplitString(Name);
if (parts.size() != 7) {
spdlog::info("parts.size() != 7");
return false;
}
if (parts.at(0) != prefix) {
spdlog::info("parts.at(0) != prefix");
return false;
}
if (parts.at(1) != "SCHAIN_ID") {
spdlog::info("parts.at(1) != SCHAIN_ID");
return false;
}
if (parts.at(3) != "NODE_ID") {
spdlog::info("parts.at(3) != Node_ID");
return false;
}
if (parts.at(5) != "DKG_ID") {
spdlog::info("parts.at(1) != DKG_ID");
return false;
}
if (parts.at(2).length() > 78 || parts.at(2).length() < 1) {
spdlog::info("parts.at(2).length() > 78");
return false;
}
if (parts.at(4).length() > 5 || parts.at(4).length() < 1) {
spdlog::info("parts.at(4).length() > 5");
return false;
}
if (parts.at(6).length() > 78 || parts.at(6).length() < 1) {
spdlog::info("parts.at(6).length() > 78");
return false;
}
mpz_t num;
mpz_init(num);
if (mpz_set_str(num, parts.at(2).c_str(), 10) == -1) {
mpz_clear(num);
spdlog::info("parts.at(2) is not decimal number");
return false;
}
mpz_clear(num);
mpz_init(num);
if (mpz_set_str(num, parts.at(4).c_str(), 10) == -1) {
mpz_clear(num);
spdlog::info("parts.at(4) is not decimal number");
return false;
}
mpz_clear(num);
mpz_init(num);
if (mpz_set_str(num, parts.at(6).c_str(), 10) == -1) {
mpz_clear(num);
spdlog::info("parts.at(6) is not decimal number");
return false;
}
mpz_clear(num);
return true;
}
bool check_n_t(const int t, const int n) {
if (t > n) {
return false;
}
if (t == 0 || n == 0) {
return false;
}
if (n > 32) {
return false;
}
if (t < 0 || n < 0) {
return false;
}
return true;
}