ServerDataChecker.cpp 4.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
    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
*/
23

24 25 26 27
#include <vector>
#include "ServerDataChecker.h"
#include <gmp.h>

28 29
#include <iostream>

30
#include "spdlog/spdlog.h"
kladko's avatar
kladko committed
31
#include "common.h"
32

kladko's avatar
kladko committed
33 34
vector<string> SplitString(const string& str, const string& delim = ":"){
    vector<string> tokens;
35 36 37
    size_t prev = 0, pos = 0;
    do {
        pos = str.find(delim, prev);
kladko's avatar
kladko committed
38 39
        if (pos == string::npos) pos = str.length();
        string token = str.substr(prev, pos-prev);
40 41 42 43 44 45 46
        if (!token.empty()) tokens.push_back(token);
        prev = pos + delim.length();
    } while (pos < str.length() && prev < str.length());

    return tokens;
}

kladko's avatar
kladko committed
47 48
bool checkECDSAKeyName(const string& keyName) {
  vector<string> parts = SplitString(keyName);
49
  if (parts.size() != 2) {
50
    spdlog::info("ECDSAKeyName num parts != 2");
51 52 53
    return false;
  }
  if (parts.at(0) != "NEK") {
54
      spdlog::info("key doesn't start from NEK");
55
      return false;
56 57
  }
  if ( parts.at(1).length() > 64 || parts.at(1).length() < 1){
58
      spdlog::info("wrong key length");
59
      return false;
60 61 62 63 64 65 66 67 68 69 70 71 72
  }

  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;
}

kladko's avatar
kladko committed
73
bool checkHex(const string& hex, const uint32_t sizeInBytes){
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  if ( hex.length() > sizeInBytes * 2 || hex.length() == 0){
    return false;
  }

  mpz_t num;
  mpz_init(num);

  if ( mpz_set_str(num, hex.c_str(), 16) == -1){
    mpz_clear(num);
    return false;
  }
  mpz_clear(num);

  return true;
}

kladko's avatar
kladko committed
90 91
bool checkName (const string& Name, const string& prefix){
    vector<string> parts = SplitString(Name);
92
    if ( parts.size() != 7) {
93
        spdlog::info("parts.size() != 7");
94 95 96
        return false;
    }
    if ( parts.at(0) != prefix ) {
97
        spdlog::info("parts.at(0) != prefix");
98 99 100
        return false;
    }
    if ( parts.at(1) != "SCHAIN_ID"){
101
        spdlog::info("parts.at(1) != SCHAIN_ID");
102 103 104
        return false;
    }
    if ( parts.at(3) != "NODE_ID"){
105
        spdlog::info("parts.at(3) != Node_ID");
106 107 108
        return false;
    }
    if ( parts.at(5) != "DKG_ID"){
109
        spdlog::info("parts.at(1) != DKG_ID");
110 111 112
        return false;
    }

113
    if ( parts.at(2).length() > 78 || parts.at(2).length() < 1){
114
        spdlog::info("parts.at(2).length() > 78");
115 116 117
        return false;
    }
    if (parts.at(4).length() > 5 || parts.at(4).length() < 1){
118
        spdlog::info("parts.at(4).length() > 5");
119 120
        return false;
    }
121
    if ( parts.at(6).length() > 78 || parts.at(6).length() < 1){
122
        spdlog::info("parts.at(6).length() > 78");
123 124 125 126 127 128
        return false;
    }

    mpz_t num;
    mpz_init(num);

129
    if ( mpz_set_str(num, parts.at(2).c_str(), 10) == -1){
130
        mpz_clear(num);
131
        spdlog::info("parts.at(2) not num");
132 133 134 135 136 137 138
        return false;
    }
    mpz_clear(num);
    mpz_init(num);

    if ( mpz_set_str(num, parts.at(4).c_str(), 10) == -1){
        mpz_clear(num);
139
        spdlog::info("parts.at(4) not num");
140 141 142 143 144
        return false;
    }
    mpz_clear(num);
    mpz_init(num);

145
    if ( mpz_set_str(num, parts.at(6).c_str(),10) == -1){
146
        mpz_clear(num);
147
        spdlog::info("parts.at(6) not num");
148 149 150 151 152 153 154
        return false;
    }
    mpz_clear(num);

    return true;
}

155
bool check_n_t ( const int t, const int n){
156 157 158 159 160 161 162 163
  if (t > n){
    return false;
  }

  if ( t == 0 || n == 0){
    return false;
  }

164 165 166 167
  if (n > 32){
    return false;
  }

168 169 170
  if ( t < 0 || n < 0){
    return false;
  }
171

172
  return true;
173
}