ServerDataChecker.cpp 4.29 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
std::vector<std::string> SplitString(const std::string& str, const std::string& delim = ":"){
31 32 33 34 35 36 37 38 39 40 41 42 43
    std::vector<std::string> tokens;
    size_t prev = 0, pos = 0;
    do {
        pos = str.find(delim, prev);
        if (pos == std::string::npos) pos = str.length();
        std::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;
}

44 45 46
bool checkECDSAKeyName(const std::string& keyName) {
  std::vector<std::string> parts = SplitString(keyName);
  if (parts.size() != 2) {
47
    std::cerr << "num parts != 2" << std::endl;
48 49 50
    return false;
  }
  if (parts.at(0) != "NEK") {
51 52
      std::cerr << "key doesn't start from NEK" << std::endl;
      return false;
53 54
  }
  if ( parts.at(1).length() > 64 || parts.at(1).length() < 1){
55 56
      std::cerr << "wrong key length" << std::endl;
      return false;
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
  }

  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 std::string& hex, const uint32_t sizeInBytes){
  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;
}

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

110
    if ( parts.at(2).length() > 78 || parts.at(2).length() < 1){
111
        std::cerr << "parts.at(2).length() > 78" << std::endl;
112 113 114
        return false;
    }
    if (parts.at(4).length() > 5 || parts.at(4).length() < 1){
115
        std::cerr << "parts.at(4).length() > 5" << std::endl;
116 117
        return false;
    }
118
    if ( parts.at(6).length() > 78 || parts.at(6).length() < 1){
119
        std::cerr << "parts.at(6).length() > 78" << std::endl;
120 121 122 123 124 125
        return false;
    }

    mpz_t num;
    mpz_init(num);

126
    if ( mpz_set_str(num, parts.at(2).c_str(), 10) == -1){
127
        mpz_clear(num);
128
        std::cerr << "parts.at(2) not num" << std::endl;
129 130 131 132 133 134 135
        return false;
    }
    mpz_clear(num);
    mpz_init(num);

    if ( mpz_set_str(num, parts.at(4).c_str(), 10) == -1){
        mpz_clear(num);
136
        std::cerr << "parts.at(4) not num" << std::endl;
137 138 139 140 141
        return false;
    }
    mpz_clear(num);
    mpz_init(num);

142
    if ( mpz_set_str(num, parts.at(6).c_str(),10) == -1){
143
        mpz_clear(num);
144
        std::cerr << "parts.at(6) not num" << std::endl;
145 146 147 148 149 150 151
        return false;
    }
    mpz_clear(num);

    return true;
}

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

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

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

165
  return true;
166
}