You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
5.3 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu>
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # https://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """VARBLOCK file support
  17. .. deprecated:: 3.4
  18. The VARBLOCK format is NOT recommended for general use, has been deprecated since
  19. Python-RSA 3.4, and will be removed in a future release. It's vulnerable to a
  20. number of attacks:
  21. 1. decrypt/encrypt_bigfile() does not implement `Authenticated encryption`_ nor
  22. uses MACs to verify messages before decrypting public key encrypted messages.
  23. 2. decrypt/encrypt_bigfile() does not use hybrid encryption (it uses plain RSA)
  24. and has no method for chaining, so block reordering is possible.
  25. See `issue #19 on Github`_ for more information.
  26. .. _Authenticated encryption: https://en.wikipedia.org/wiki/Authenticated_encryption
  27. .. _issue #19 on Github: https://github.com/sybrenstuvel/python-rsa/issues/13
  28. The VARBLOCK file format is as follows, where || denotes byte concatenation:
  29. FILE := VERSION || BLOCK || BLOCK ...
  30. BLOCK := LENGTH || DATA
  31. LENGTH := varint-encoded length of the subsequent data. Varint comes from
  32. Google Protobuf, and encodes an integer into a variable number of bytes.
  33. Each byte uses the 7 lowest bits to encode the value. The highest bit set
  34. to 1 indicates the next byte is also part of the varint. The last byte will
  35. have this bit set to 0.
  36. This file format is called the VARBLOCK format, in line with the varint format
  37. used to denote the block sizes.
  38. """
  39. import warnings
  40. from rsa._compat import byte, b
  41. ZERO_BYTE = b('\x00')
  42. VARBLOCK_VERSION = 1
  43. warnings.warn("The 'rsa.varblock' module was deprecated in Python-RSA version "
  44. "3.4 due to security issues in the VARBLOCK format. See "
  45. "https://github.com/sybrenstuvel/python-rsa/issues/13 for more information.",
  46. DeprecationWarning)
  47. def read_varint(infile):
  48. """Reads a varint from the file.
  49. When the first byte to be read indicates EOF, (0, 0) is returned. When an
  50. EOF occurs when at least one byte has been read, an EOFError exception is
  51. raised.
  52. :param infile: the file-like object to read from. It should have a read()
  53. method.
  54. :returns: (varint, length), the read varint and the number of read bytes.
  55. """
  56. varint = 0
  57. read_bytes = 0
  58. while True:
  59. char = infile.read(1)
  60. if len(char) == 0:
  61. if read_bytes == 0:
  62. return 0, 0
  63. raise EOFError('EOF while reading varint, value is %i so far' %
  64. varint)
  65. byte = ord(char)
  66. varint += (byte & 0x7F) << (7 * read_bytes)
  67. read_bytes += 1
  68. if not byte & 0x80:
  69. return varint, read_bytes
  70. def write_varint(outfile, value):
  71. """Writes a varint to a file.
  72. :param outfile: the file-like object to write to. It should have a write()
  73. method.
  74. :returns: the number of written bytes.
  75. """
  76. # there is a big difference between 'write the value 0' (this case) and
  77. # 'there is nothing left to write' (the false-case of the while loop)
  78. if value == 0:
  79. outfile.write(ZERO_BYTE)
  80. return 1
  81. written_bytes = 0
  82. while value > 0:
  83. to_write = value & 0x7f
  84. value >>= 7
  85. if value > 0:
  86. to_write |= 0x80
  87. outfile.write(byte(to_write))
  88. written_bytes += 1
  89. return written_bytes
  90. def yield_varblocks(infile):
  91. """Generator, yields each block in the input file.
  92. :param infile: file to read, is expected to have the VARBLOCK format as
  93. described in the module's docstring.
  94. @yields the contents of each block.
  95. """
  96. # Check the version number
  97. first_char = infile.read(1)
  98. if len(first_char) == 0:
  99. raise EOFError('Unable to read VARBLOCK version number')
  100. version = ord(first_char)
  101. if version != VARBLOCK_VERSION:
  102. raise ValueError('VARBLOCK version %i not supported' % version)
  103. while True:
  104. (block_size, read_bytes) = read_varint(infile)
  105. # EOF at block boundary, that's fine.
  106. if read_bytes == 0 and block_size == 0:
  107. break
  108. block = infile.read(block_size)
  109. read_size = len(block)
  110. if read_size != block_size:
  111. raise EOFError('Block size is %i, but could read only %i bytes' %
  112. (block_size, read_size))
  113. yield block
  114. def yield_fixedblocks(infile, blocksize):
  115. """Generator, yields each block of ``blocksize`` bytes in the input file.
  116. :param infile: file to read and separate in blocks.
  117. :returns: a generator that yields the contents of each block
  118. """
  119. while True:
  120. block = infile.read(blocksize)
  121. read_bytes = len(block)
  122. if read_bytes == 0:
  123. break
  124. yield block
  125. if read_bytes < blocksize:
  126. break