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.

135 lines
5.1 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. """Large 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. This module contains functions to:
  29. - break a file into smaller blocks, and encrypt them, and store the
  30. encrypted blocks in another file.
  31. - take such an encrypted files, decrypt its blocks, and reconstruct the
  32. original file.
  33. The encrypted file format is as follows, where || denotes byte concatenation:
  34. FILE := VERSION || BLOCK || BLOCK ...
  35. BLOCK := LENGTH || DATA
  36. LENGTH := varint-encoded length of the subsequent data. Varint comes from
  37. Google Protobuf, and encodes an integer into a variable number of bytes.
  38. Each byte uses the 7 lowest bits to encode the value. The highest bit set
  39. to 1 indicates the next byte is also part of the varint. The last byte will
  40. have this bit set to 0.
  41. This file format is called the VARBLOCK format, in line with the varint format
  42. used to denote the block sizes.
  43. """
  44. import warnings
  45. from rsa import key, common, pkcs1, varblock
  46. from rsa._compat import byte
  47. def encrypt_bigfile(infile, outfile, pub_key):
  48. """Encrypts a file, writing it to 'outfile' in VARBLOCK format.
  49. .. deprecated:: 3.4
  50. This function was deprecated in Python-RSA version 3.4 due to security issues
  51. in the VARBLOCK format. See the documentation_ for more information.
  52. .. _documentation: https://stuvel.eu/python-rsa-doc/usage.html#working-with-big-files
  53. :param infile: file-like object to read the cleartext from
  54. :param outfile: file-like object to write the crypto in VARBLOCK format to
  55. :param pub_key: :py:class:`rsa.PublicKey` to encrypt with
  56. """
  57. warnings.warn("The 'rsa.bigfile.encrypt_bigfile' function was deprecated in Python-RSA version "
  58. "3.4 due to security issues in the VARBLOCK format. See "
  59. "https://stuvel.eu/python-rsa-doc/usage.html#working-with-big-files "
  60. "for more information.",
  61. DeprecationWarning, stacklevel=2)
  62. if not isinstance(pub_key, key.PublicKey):
  63. raise TypeError('Public key required, but got %r' % pub_key)
  64. key_bytes = common.bit_size(pub_key.n) // 8
  65. blocksize = key_bytes - 11 # keep space for PKCS#1 padding
  66. # Write the version number to the VARBLOCK file
  67. outfile.write(byte(varblock.VARBLOCK_VERSION))
  68. # Encrypt and write each block
  69. for block in varblock.yield_fixedblocks(infile, blocksize):
  70. crypto = pkcs1.encrypt(block, pub_key)
  71. varblock.write_varint(outfile, len(crypto))
  72. outfile.write(crypto)
  73. def decrypt_bigfile(infile, outfile, priv_key):
  74. """Decrypts an encrypted VARBLOCK file, writing it to 'outfile'
  75. .. deprecated:: 3.4
  76. This function was deprecated in Python-RSA version 3.4 due to security issues
  77. in the VARBLOCK format. See the documentation_ for more information.
  78. .. _documentation: https://stuvel.eu/python-rsa-doc/usage.html#working-with-big-files
  79. :param infile: file-like object to read the crypto in VARBLOCK format from
  80. :param outfile: file-like object to write the cleartext to
  81. :param priv_key: :py:class:`rsa.PrivateKey` to decrypt with
  82. """
  83. warnings.warn("The 'rsa.bigfile.decrypt_bigfile' function was deprecated in Python-RSA version "
  84. "3.4 due to security issues in the VARBLOCK format. See "
  85. "https://stuvel.eu/python-rsa-doc/usage.html#working-with-big-files "
  86. "for more information.",
  87. DeprecationWarning, stacklevel=2)
  88. if not isinstance(priv_key, key.PrivateKey):
  89. raise TypeError('Private key required, but got %r' % priv_key)
  90. for block in varblock.yield_varblocks(infile):
  91. cleartext = pkcs1.decrypt(block, priv_key)
  92. outfile.write(cleartext)
  93. __all__ = ['encrypt_bigfile', 'decrypt_bigfile']