Beispiel #1
0
def deflate_long(n, add_sign_padding=True):
    """turns a long-int into a normalized byte string
    (adapted from Crypto.Util.number)"""
    # after much testing, this algorithm was deemed to be the fastest
    s = bytes()
    n = long(n)
    while (n != 0) and (n != -1):
        s = struct.pack('>I', n & xffffffff) + s
        n >>= 32
    # strip off leading zeros, FFs
    for i in enumerate(s):
        if (n == 0) and (i[1] != deflate_zero):
            break
        if (n == -1) and (i[1] != deflate_ff):
            break
    else:
        # degenerate case, n was either 0 or -1
        i = (0, )
        if n == 0:
            s = zero_byte
        else:
            s = max_byte
    s = s[i[0]:]
    if add_sign_padding:
        if (n == 0) and (byte_ord(s[0]) >= 0x80):
            s = zero_byte + s
        if (n == -1) and (byte_ord(s[0]) < 0x80):
            s = max_byte + s
    return s
Beispiel #2
0
def constant_time_bytes_eq(a, b):
    if len(a) != len(b):
        return False
    res = 0
    # noinspection PyUnresolvedReferences
    for i in (xrange if PY2 else range)(len(a)):  # noqa: F821
        res |= byte_ord(a[i]) ^ byte_ord(b[i])
    return res == 0
Beispiel #3
0
 def _read_packet(self):
     x = self._read_all(4)
     # most sftp servers won't accept packets larger than about 32k, so
     # anything with the high byte set (> 16MB) is just garbage.
     if byte_ord(x[0]):
         raise SFTPError('Garbage packet received')
     size = struct.unpack('>I', x)[0]
     data = self._read_all(size)
     if self.ultra_debug:
         self._log(DEBUG, util.format_binary(data, 'IN: '))
     if size > 0:
         t = byte_ord(data[0])
         return t, data[1:]
     return 0, bytes()
Beispiel #4
0
def safe_string(s):
    out = b''
    for c in s:
        i = byte_ord(c)
        if 32 <= i <= 127:
            out += byte_chr(i)
        else:
            out += b('%{:02X}'.format(i))
    return out
Beispiel #5
0
 def decode_next(self):
     if self.idx >= len(self.content):
         return None
     ident = byte_ord(self.content[self.idx])
     self.idx += 1
     if (ident & 31) == 31:
         # identifier > 30
         ident = 0
         while self.idx < len(self.content):
             t = byte_ord(self.content[self.idx])
             self.idx += 1
             ident = (ident << 7) | (t & 0x7f)
             if not (t & 0x80):
                 break
     if self.idx >= len(self.content):
         return None
     # now fetch length
     size = byte_ord(self.content[self.idx])
     self.idx += 1
     if size & 0x80:
         # more complimicated...
         # FIXME: theoretically should handle indefinite-length (0x80)
         t = size & 0x7f
         if self.idx + t > len(self.content):
             return None
         size = util.inflate_long(self.content[self.idx:self.idx + t], True)
         self.idx += t
     if self.idx + size > len(self.content):
         # can't fit
         return None
     data = self.content[self.idx:self.idx + size]
     self.idx += size
     # now switch on id
     if ident == 0x30:
         # sequence
         return self.decode_sequence(data)
     elif ident == 2:
         # int
         return util.inflate_long(data)
     else:
         # 1: boolean (00 false, otherwise true)
         msg = 'Unknown ber encoding type {:d} (robey is lazy)'
         raise BERException(msg.format(ident))
Beispiel #6
0
    def send_message(self, data):
        """
        Write a block of data using the current cipher, as an SSH block.
        """
        # encrypt this sucka
        data = asbytes(data)
        cmd = byte_ord(data[0])
        if cmd in MSG_NAMES:
            cmd_name = MSG_NAMES[cmd]
        else:
            cmd_name = '${:x}'.format(cmd)
        orig_len = len(data)
        self.__write_lock.acquire()
        try:
            if self.__compress_engine_out is not None:
                data = self.__compress_engine_out(data)
            packet = self._build_packet(data)
            if self.__dump_packets:
                self._log(
                    DEBUG,
                    'Write packet <{}>, length {}'.format(cmd_name, orig_len))
                self._log(DEBUG, util.format_binary(packet, 'OUT: '))
            if self.__block_engine_out is not None:
                out = self.__block_engine_out.update(packet)
            else:
                out = packet
            # + mac
            if self.__block_engine_out is not None:
                payload = struct.pack('>I',
                                      self.__sequence_number_out) + packet
                out += compute_hmac(
                    self.__mac_key_out, payload,
                    self.__mac_engine_out)[:self.__mac_size_out]
            self.__sequence_number_out = \
                (self.__sequence_number_out + 1) & xffffffff
            self.write_all(out)

            self.__sent_bytes += len(out)
            self.__sent_packets += 1
            sent_too_much = (self.__sent_packets >= self.REKEY_PACKETS
                             or self.__sent_bytes >= self.REKEY_BYTES)
            if sent_too_much and not self.__need_rekey:
                # only ask once for rekeying
                msg = "Rekeying (hit {} packets, {} bytes sent)"
                self._log(DEBUG,
                          msg.format(
                              self.__sent_packets,
                              self.__sent_bytes,
                          ))
                self.__received_bytes_overflow = 0
                self.__received_packets_overflow = 0
                self._trigger_rekey()
        finally:
            self.__write_lock.release()
Beispiel #7
0
def bit_length(n):
    try:
        return n.bit_length()
    except AttributeError:
        norm = deflate_long(n, False)
        hbyte = byte_ord(norm[0])
        if hbyte == 0:
            return 1
        bitlen = len(norm) * 8
        while not (hbyte & 0x80):
            hbyte <<= 1
            bitlen -= 1
        return bitlen
Beispiel #8
0
 def _generate_x(self):
     # generate an "x" (1 < x < (p-1)/2).
     q = (self.p - 1) // 2
     qnorm = util.deflate_long(q, 0)
     qhbyte = byte_ord(qnorm[0])
     byte_count = len(qnorm)
     qmask = 0xff
     while not (qhbyte & 0x80):
         qhbyte <<= 1
         qmask >>= 1
     while True:
         x_bytes = os.urandom(byte_count)
         x_bytes = byte_mask(x_bytes[0], qmask) + x_bytes[1:]
         x = util.inflate_long(x_bytes, 1)
         if (x > 1) and (x < q):
             break
     self.x = x
Beispiel #9
0
def inflate_long(s, always_positive=False):
    """turns a normalized byte string into a long-int
    (adapted from Crypto.Util.number)"""
    out = long(0)
    negative = 0
    if not always_positive and (len(s) > 0) and (byte_ord(s[0]) >= 0x80):
        negative = 1
    if len(s) % 4:
        filler = zero_byte
        if negative:
            filler = max_byte
        # never convert this to ``s +=`` because this is a string, not a number
        # noinspection PyAugmentAssignment
        s = filler * (4 - len(s) % 4) + s
    for i in range(0, len(s), 4):
        out = (out << 32) + struct.unpack('>I', s[i:i + 4])[0]
    if negative:
        out -= (long(1) << (8 * len(s)))
    return out
Beispiel #10
0
    def read_message(self):
        """
        Only one thread should ever be in this function (no other locking is
        done).

        :raises: `.SSHException` -- if the packet is mangled
        :raises: `.NeedRekeyException` -- if the transport should rekey
        """
        header = self.read_all(self.__block_size_in, check_rekey=True)
        if self.__block_engine_in is not None:
            header = self.__block_engine_in.update(header)
        if self.__dump_packets:
            self._log(DEBUG, util.format_binary(header, 'IN: '))
        packet_size = struct.unpack('>I', header[:4])[0]
        # leftover contains decrypted bytes from the first block (after the
        # length field)
        leftover = header[4:]
        if (packet_size - len(leftover)) % self.__block_size_in != 0:
            raise SSHException('Invalid packet blocking')
        buf = self.read_all(packet_size + self.__mac_size_in - len(leftover))
        packet = buf[:packet_size - len(leftover)]
        post_packet = buf[packet_size - len(leftover):]
        if self.__block_engine_in is not None:
            packet = self.__block_engine_in.update(packet)
        if self.__dump_packets:
            self._log(DEBUG, util.format_binary(packet, 'IN: '))
        packet = leftover + packet

        if self.__mac_size_in > 0:
            mac = post_packet[:self.__mac_size_in]
            mac_payload = struct.pack('>II', self.__sequence_number_in,
                                      packet_size) + packet
            my_mac = compute_hmac(self.__mac_key_in, mac_payload,
                                  self.__mac_engine_in)[:self.__mac_size_in]
            if not util.constant_time_bytes_eq(my_mac, mac):
                raise SSHException('Mismatched MAC')
        padding = byte_ord(packet[0])
        payload = packet[1:packet_size - padding]

        if self.__dump_packets:
            self._log(
                DEBUG, 'Got payload ({} bytes, {} padding)'.format(
                    packet_size, padding))

        if self.__compress_engine_in is not None:
            payload = self.__compress_engine_in(payload)

        msg = Message(payload[1:])
        msg.seqno = self.__sequence_number_in
        self.__sequence_number_in = (self.__sequence_number_in + 1) & xffffffff

        # check for rekey
        raw_packet_size = packet_size + self.__mac_size_in + 4
        self.__received_bytes += raw_packet_size
        self.__received_packets += 1
        if self.__need_rekey:
            # we've asked to rekey -- give them some packets to comply before
            # dropping the connection
            self.__received_bytes_overflow += raw_packet_size
            self.__received_packets_overflow += 1
            if (self.__received_packets_overflow >=
                    self.REKEY_PACKETS_OVERFLOW_MAX) or \
               (self.__received_bytes_overflow >=
                    self.REKEY_BYTES_OVERFLOW_MAX):
                raise SSHException(
                    'Remote transport is ignoring rekey requests')
        elif (self.__received_packets >= self.REKEY_PACKETS) or \
             (self.__received_bytes >= self.REKEY_BYTES):
            # only ask once for rekeying
            err = "Rekeying (hit {} packets, {} bytes received)"
            self._log(
                DEBUG,
                err.format(
                    self.__received_packets,
                    self.__received_bytes,
                ))
            self.__received_bytes_overflow = 0
            self.__received_packets_overflow = 0
            self._trigger_rekey()

        cmd = byte_ord(payload[0])
        if cmd in MSG_NAMES:
            cmd_name = MSG_NAMES[cmd]
        else:
            cmd_name = '${:x}'.format(cmd)
        if self.__dump_packets:
            self._log(
                DEBUG,
                'Read packet <{}>, length {}'.format(cmd_name, len(payload)))
        return cmd, msg
Beispiel #11
0
def format_binary_line(data):
    left = ' '.join(['{:02X}'.format(byte_ord(c)) for c in data])
    right = ''.join([
        '.{:c}..'.format(byte_ord(c))[(byte_ord(c) + 63) // 95] for c in data
    ])
    return '{:50s} {}'.format(left, right)