From 170d28b1ebaae48e010324c56ad265f9640f89ff Mon Sep 17 00:00:00 2001 From: Artem Bityutskiy Date: Mon, 6 May 2013 17:51:58 +0300 Subject: [PATCH] BmapCopy: verify bmap file checksum If bmap file format is greater than 1.3, verify its integrity by checking the SHA1 checksum. Change-Id: I2b1ed51d9eda71fe3f3681cc92314b1bcc83a23f Signed-off-by: Artem Bityutskiy --- bmaptools/BmapCopy.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/bmaptools/BmapCopy.py b/bmaptools/BmapCopy.py index af55006..889f357 100644 --- a/bmaptools/BmapCopy.py +++ b/bmaptools/BmapCopy.py @@ -135,6 +135,33 @@ class BmapCopy: self.mapped_size = self.image_size self.mapped_size_human = self.image_size_human + def _verify_bmap_checksum(self): + """ This is a helper function which verifies SHA1 checksum of the bmap + file. """ + + import mmap + + correct_sha1 = self._xml.find("BmapFileSHA1").text.strip() + + # Before verifying the shecksum, we have to substitute the SHA1 value + # stored in the file with all zeroes. For these purposes we create + # private memory mapping of the bmap file. + mapped_bmap = mmap.mmap(self._f_bmap.fileno(), 0, + access = mmap.ACCESS_COPY) + + sha1_pos = mapped_bmap.find(correct_sha1) + assert sha1_pos != -1 + + mapped_bmap[sha1_pos:sha1_pos + 40] = '0' * 40 + calculated_sha1 = hashlib.sha1(mapped_bmap).hexdigest() + + mapped_bmap.close() + + if calculated_sha1 != correct_sha1: + raise Error("checksum mismatch for bmap file '%s': calculated " \ + "'%s', should be '%s'" % \ + (self._bmap_path, calculated_sha1, correct_sha1)) + def _parse_bmap(self): """ Parse the bmap file and initialize corresponding class instance attributs. """ @@ -172,6 +199,10 @@ class BmapCopy: "blocks count (%d bytes != %d blocks * %d bytes)" \ % (self.image_size, self.blocks_cnt, self.block_size)) + if self.bmap_version_major >= 1 and self.bmap_version_minor >= 3: + # Bmap file checksum appeard in format 1.3 + self._verify_bmap_checksum() + def __init__(self, image, dest, bmap = None, image_size = None): """ The class constructor. The parameters are: image - file-like object of the image which should be copied, -- 2.7.4