def abort(self, errormessage=None, bad_csm_option=None): self.log.warning("Aborting connection: %s", errormessage) abort_msg = Message(code=ABORT) if errormessage is not None: abort_msg.payload = errormessage.encode('utf8') if bad_csm_option is not None: bad_csm_option_option = optiontypes.UintOption(2, bad_csm_option) abort_msg.opt.add_option(bad_csm_option_option) self._abort_with(abort_msg)
def wraps(self: BaseResource, request: Message): try: request.payload = gzdecompress(request.payload) except OSError: # Since the received message was not properly compressed, return a non compressed response just in case return Message(code=Code.BAD_REQUEST, payload=b'{"error":"Bad GZIP compression"}') response = func(self, request) response.payload = gzcompress(response.payload) return response
def _decode_message(data: bytes) -> Message: tokenoffset, tkl, _ = _extract_message_size(data) if tkl > 8: raise error.UnparsableMessage("Overly long token") code = data[tokenoffset - 1] token = data[tokenoffset:tokenoffset + tkl] msg = Message(code=code, token=token) msg.payload = msg.opt.decode(data[tokenoffset + tkl:]) return msg
def _decode_message(data: bytes) -> Message: codeoffset = 1 tokenoffset = 2 tkl = data[0] if tkl > 8: raise error.UnparsableMessage("Overly long token") code = data[codeoffset] token = data[tokenoffset:tokenoffset + tkl] msg = Message(code=code, token=token) msg.payload = msg.opt.decode(data[tokenoffset + tkl:]) return msg
def abort(self, errormessage=None, bad_csm_option=None): self.log.warning("Aborting connection: %s", errormessage) abort_msg = Message(code=ABORT) if errormessage is not None: abort_msg.payload = errormessage.encode('utf8') if bad_csm_option is not None: bad_csm_option_option = optiontypes.UintOption(2, bad_csm_option) abort_msg.opt.add_option(bad_csm_option_option) if self._transport is not None: self._send_message(abort_msg) self._transport.close() else: # FIXME: find out how this happens; i've only seen it after nmap # runs against an aiocoap server and then shutting it down. # "poisoning" the object to make sure this can not be exploited to # bypass the server shutdown. self._ctx = None
def inner(self, request: Message): if len(request.payload) < 4: return Message(code=Code.BAD_REQUEST, payload=b'{"error":"Bad request format"}') sig_len = int.from_bytes(request.payload[:2], 'big') if len(request.payload) < 4 + sig_len: return Message(code=Code.BAD_REQUEST, payload=b'{"error":"Bad request format"}') signature = request.payload[2:2 + sig_len] message = request.payload[2 + sig_len:] try: self._ecc_pub_key.verify( signature, message, ec.ECDSA(SHA256()) ) except InvalidSignature: return Message(code=Code.UNAUTHORIZED) else: request.payload = message return func(self, request)