Exemplo n.º 1
0
def verify(key, sig, message):
    try:
        rc = key.verify(sig, message)
        return rc
    except Exception as E:
        _logger.error(E)
        return False
Exemplo n.º 2
0
def get_key_path(i, ktype):
    try:
        KEY_NAME = ktype + str(i) + ASYMM_FILE_FORMATS[ktype]
        # _logger.info("KPATH - FETCH - %s -- %s" % (ktype, KEY_NAME))
        return os.path.join(KD, KEY_NAME)
    # generic catch
    except Exception as E:
        _logger.error(E)
        return
Exemplo n.º 3
0
def validate_keypair(i, s, v):
    msg = "message" + str(i)
    sig = s.sign(msg)
    ver = v.verify(sig, msg)
    if not ver:
        _logger.error("Error while reading keypair: " % i)
        return False
    _logger.info("Round succeeded for keypair: " % i)
    return True
Exemplo n.º 4
0
def signal_handler(event, frame):
    sys.stdout.write("handling signal: %s\n" % event)
    sys.stdout.flush()
    _logger.error("Kill signal (%s) detected. Stopping pbft.." % event)
    countdown = 3  # seconds
    if event == signal.SIGINT:
        print("Committing deliberate suicide in %s seconds" % countdown)
        t = Timer(countdown, suicide)
        t.start()
        sys.exit(130)  # Ctrl-C for bash
Exemplo n.º 5
0
 def parse_request(self, request_bytes, fd):
     # attempt to reconstruct the request object
     # close connection and return on failure
     _logger.info("Phase - PARSE REQUEST - fd [%s]" % fd)
     try:
         # import pdb; pdb.set_trace()
         req = request_pb2.Request()
         req.ParseFromString(request_bytes)
         record_pbft(self.debuglog, req)
         key = get_asymm_key(req.inner.id, ktype="sign")
         if not bool(key):
             return
         # if not isinstance(key, ecdsa.SigningKey):
         if not bool(key):
             return
         req = message.check(key, req)
         if req is None:
             _logger.error("Failed message sig check. 'req' is empty..")
             return
     except Exception as E:
         req = None
         _logger.error("ERROR IN PROTOBUF TYPES: %s" % E)
         # raise  # for debug
         self.clean(fd)
         return
     # print(req.inner.type, len(request_bytes))
     # TODO: Check for view number and view change, h/H
     if req.inner.view != self.view or not self.view_active:
         if req.inner.type != "VCHA" and req.inner.type != "NEVW" and \
                 req.inner.type != "CHKP" and req.inner.type != "REQU":
             debug_msg = "TYPE: %s - ID %s - SEQ %s - VIEW - %s" % (
                 req.inner.type,
                 req.inner.id,
                 req.inner.seq,
                 req.inner.view
             )
             _logger.warn("Bad view number - %s" % debug_msg)
             return
     if self.in_node_history(req):
         _logger.warn("Duplicate node message")
         # return
         pass
     if req.inner.type in self.request_types and not self.in_client_history(req):
         # call to actual success
         self.request_types[req.inner.type](req, fd)
     else:
         self.clean(fd)
         _logger.warn("BAD MESSAGE TYPE - %s - %s" % (
             req.inner.type,
             req.inner.id))
Exemplo n.º 6
0
def read_keys_test(n, validate=False):
    if not os.path.isdir(KD):
        _logger.error("Can't find key directory")
        sys.exit(0)
    s = []
    v = []
    for i in range(0, n):
        secret_key = open(get_key_path(i, "sign"), "rb").read()
        public_key = open(get_key_path(i, "verify"), "rb").read()
        s.append(ecdsa.SigningKey.from_pem(secret_key))
        v.append(ecdsa.VerifyingKey.from_pem(public_key))
        if validate:
            assert validate_keypair(i, s[-1], v[-1]) is True
    return s, v
Exemplo n.º 7
0
    def process_preprepare(self, req, fd):
        _logger.info("Phase - PRE-PREPARE - fd [%s]" % fd)
        if req.inner.seq in self.node_message_log["PRPR"]:
            return None

        # the msg field for a preprepare should be the digest of the original client request
        if req.outer != "":
            try:
                client_req = request_pb2.Request()
                client_req.ParseFromString(req.outer)
                record_pbft(self.debuglog, client_req)
                client_key = get_asymm_key(client_req.inner.id, ktype="sign")
                if not bool(client_key):
                    return
                client_req = message.check(client_key, client_req)
                if client_req == None or req.inner.msg != client_req.dig:
                    _logger.warn("FAILED PRPR OUTER SIGCHECK")
                    return
            except:
                _logger.error("ERROR IN PRPR OUTER PROTOBUFF")
                raise
        else:
            client_req = None
        if req.inner.msg not in self.active:
            # self.active[req.inner.msg] = (client_req, Timer(self.timeout, self.handle_timeout), fd)
            request_timer = Timer(self.timeout, self.handle_timeout, [req.inner.msg, req.inner.view])
            request_timer.daemon = True
            request_timer.start()
            self.active[req.inner.msg] = (client_req, request_timer, fd)

        self.add_node_history(req)
        m = self.create_request("PREP", req.inner.seq, req.inner.msg)
        self.add_node_history(m)
        record_pbft(self.debuglog, m)
        self.inc_prep_dict(req.inner.msg)
        self.broadcast_to_nodes(m)

        if self.check_prepared_margin(req.inner.msg, req.inner.seq):
            record(self.debuglog, "PREPARED sequence number " + str(req.inner.seq))
            m = self.create_request("COMM", req.inner.seq, req.inner.msg)
            self.broadcast_to_nodes(m)
            self.add_node_history(m)
            self.inc_comm_dict(m.inner.msg)
            record_pbft(self.debuglog, m)
            self.prepared[req.inner.seq] = req.inner.msg
            if self.check_committed_margin(m.inner.msg, m):
                record(self.debuglog, "COMMITTED sequence number " + str(m.inner.seq))
                record_pbft(self.commitlog, m)
                self.execute_in_order(m)
Exemplo n.º 8
0
 def init_server_socket(self, _id=None):
     """
     triggers setup using testbed_config. Increments given server id
     if that (ip, socket) from Replica List RL is already in use.
     """
     global N
     c = _id
     while c < N:
         s = None
         try:
             s = init_server(c)
         except OSError as E:
             _logger.error("%s -- Server ID: [%s]" % (E, c))
             c -= 1
         if s:
             return s, c
Exemplo n.º 9
0
def get_asymm_key(i, ktype=None):
    kpath = get_key_path(i, ktype)
    found_error = False
    try:
        if not os.path.isfile(kpath):
            result = "File Not Found: %s" % kpath
            _logger.error(result)
            found_error = True
        else:
            key_pem = open(kpath, 'rb').read()
            result = ASYMM_FUNC_MAP[ktype](key_pem)
    except Exception as result:
        found_error = True

    if found_error:
        _logger.error("%s" % result)
        return

    return result