OLD | NEW |
(Empty) | |
| 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 |
| 17 """VARBLOCK file support |
| 18 |
| 19 .. deprecated:: 3.4 |
| 20 |
| 21 The VARBLOCK format is NOT recommended for general use, has been deprecated
since |
| 22 Python-RSA 3.4, and will be removed in a future release. It's vulnerable to
a |
| 23 number of attacks: |
| 24 |
| 25 1. decrypt/encrypt_bigfile() does not implement `Authenticated encryption`_
nor |
| 26 uses MACs to verify messages before decrypting public key encrypted messa
ges. |
| 27 |
| 28 2. decrypt/encrypt_bigfile() does not use hybrid encryption (it uses plain R
SA) |
| 29 and has no method for chaining, so block reordering is possible. |
| 30 |
| 31 See `issue #19 on Github`_ for more information. |
| 32 |
| 33 .. _Authenticated encryption: https://en.wikipedia.org/wiki/Authenticated_encryp
tion |
| 34 .. _issue #19 on Github: https://github.com/sybrenstuvel/python-rsa/issues/13 |
| 35 |
| 36 |
| 37 The VARBLOCK file format is as follows, where || denotes byte concatenation: |
| 38 |
| 39 FILE := VERSION || BLOCK || BLOCK ... |
| 40 |
| 41 BLOCK := LENGTH || DATA |
| 42 |
| 43 LENGTH := varint-encoded length of the subsequent data. Varint comes from |
| 44 Google Protobuf, and encodes an integer into a variable number of bytes. |
| 45 Each byte uses the 7 lowest bits to encode the value. The highest bit set |
| 46 to 1 indicates the next byte is also part of the varint. The last byte will |
| 47 have this bit set to 0. |
| 48 |
| 49 This file format is called the VARBLOCK format, in line with the varint format |
| 50 used to denote the block sizes. |
| 51 |
| 52 """ |
| 53 |
| 54 import warnings |
| 55 |
| 56 from rsa._compat import byte, b |
| 57 |
| 58 ZERO_BYTE = b('\x00') |
| 59 VARBLOCK_VERSION = 1 |
| 60 |
| 61 warnings.warn("The 'rsa.varblock' module was deprecated in Python-RSA version " |
| 62 "3.4 due to security issues in the VARBLOCK format. See " |
| 63 "https://github.com/sybrenstuvel/python-rsa/issues/13 for more inf
ormation.", |
| 64 DeprecationWarning) |
| 65 |
| 66 |
| 67 def read_varint(infile): |
| 68 """Reads a varint from the file. |
| 69 |
| 70 When the first byte to be read indicates EOF, (0, 0) is returned. When an |
| 71 EOF occurs when at least one byte has been read, an EOFError exception is |
| 72 raised. |
| 73 |
| 74 :param infile: the file-like object to read from. It should have a read() |
| 75 method. |
| 76 :returns: (varint, length), the read varint and the number of read bytes. |
| 77 """ |
| 78 |
| 79 varint = 0 |
| 80 read_bytes = 0 |
| 81 |
| 82 while True: |
| 83 char = infile.read(1) |
| 84 if len(char) == 0: |
| 85 if read_bytes == 0: |
| 86 return 0, 0 |
| 87 raise EOFError('EOF while reading varint, value is %i so far' % |
| 88 varint) |
| 89 |
| 90 byte = ord(char) |
| 91 varint += (byte & 0x7F) << (7 * read_bytes) |
| 92 |
| 93 read_bytes += 1 |
| 94 |
| 95 if not byte & 0x80: |
| 96 return varint, read_bytes |
| 97 |
| 98 |
| 99 def write_varint(outfile, value): |
| 100 """Writes a varint to a file. |
| 101 |
| 102 :param outfile: the file-like object to write to. It should have a write() |
| 103 method. |
| 104 :returns: the number of written bytes. |
| 105 """ |
| 106 |
| 107 # there is a big difference between 'write the value 0' (this case) and |
| 108 # 'there is nothing left to write' (the false-case of the while loop) |
| 109 |
| 110 if value == 0: |
| 111 outfile.write(ZERO_BYTE) |
| 112 return 1 |
| 113 |
| 114 written_bytes = 0 |
| 115 while value > 0: |
| 116 to_write = value & 0x7f |
| 117 value >>= 7 |
| 118 |
| 119 if value > 0: |
| 120 to_write |= 0x80 |
| 121 |
| 122 outfile.write(byte(to_write)) |
| 123 written_bytes += 1 |
| 124 |
| 125 return written_bytes |
| 126 |
| 127 |
| 128 def yield_varblocks(infile): |
| 129 """Generator, yields each block in the input file. |
| 130 |
| 131 :param infile: file to read, is expected to have the VARBLOCK format as |
| 132 described in the module's docstring. |
| 133 @yields the contents of each block. |
| 134 """ |
| 135 |
| 136 # Check the version number |
| 137 first_char = infile.read(1) |
| 138 if len(first_char) == 0: |
| 139 raise EOFError('Unable to read VARBLOCK version number') |
| 140 |
| 141 version = ord(first_char) |
| 142 if version != VARBLOCK_VERSION: |
| 143 raise ValueError('VARBLOCK version %i not supported' % version) |
| 144 |
| 145 while True: |
| 146 (block_size, read_bytes) = read_varint(infile) |
| 147 |
| 148 # EOF at block boundary, that's fine. |
| 149 if read_bytes == 0 and block_size == 0: |
| 150 break |
| 151 |
| 152 block = infile.read(block_size) |
| 153 |
| 154 read_size = len(block) |
| 155 if read_size != block_size: |
| 156 raise EOFError('Block size is %i, but could read only %i bytes' % |
| 157 (block_size, read_size)) |
| 158 |
| 159 yield block |
| 160 |
| 161 |
| 162 def yield_fixedblocks(infile, blocksize): |
| 163 """Generator, yields each block of ``blocksize`` bytes in the input file. |
| 164 |
| 165 :param infile: file to read and separate in blocks. |
| 166 :returns: a generator that yields the contents of each block |
| 167 """ |
| 168 |
| 169 while True: |
| 170 block = infile.read(blocksize) |
| 171 |
| 172 read_bytes = len(block) |
| 173 if read_bytes == 0: |
| 174 break |
| 175 |
| 176 yield block |
| 177 |
| 178 if read_bytes < blocksize: |
| 179 break |
OLD | NEW |