blob: 48b18e66d231565a6e50ca9c28c78cd18e0ab819 [file] [log] [blame]
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Verifying the integrity of a Chrome OS update payload.
6
7This module is used internally by the main Payload class for verifying the
8integrity of an update payload. The interface for invoking the checks is as
9follows:
10
11 checker = PayloadChecker(payload)
12 checker.Run(...)
Gilad Arnold553b0ec2013-01-26 01:00:39 -080013"""
14
Gilad Arnoldf583a7d2015-02-05 13:23:55 -080015from __future__ import print_function
16
Gilad Arnold553b0ec2013-01-26 01:00:39 -080017import array
18import base64
19import hashlib
Gilad Arnoldcb638912013-06-24 04:57:11 -070020import itertools
Gilad Arnold9b90c932013-05-22 17:12:56 -070021import os
Gilad Arnold553b0ec2013-01-26 01:00:39 -080022import subprocess
23
24import common
Gilad Arnoldcb638912013-06-24 04:57:11 -070025import error
Gilad Arnold553b0ec2013-01-26 01:00:39 -080026import format_utils
27import histogram
28import update_metadata_pb2
29
30
31#
Gilad Arnold9b90c932013-05-22 17:12:56 -070032# Constants.
Gilad Arnold553b0ec2013-01-26 01:00:39 -080033#
Gilad Arnoldcb638912013-06-24 04:57:11 -070034
Gilad Arnoldeaed0d12013-04-30 15:38:22 -070035_CHECK_DST_PSEUDO_EXTENTS = 'dst-pseudo-extents'
36_CHECK_MOVE_SAME_SRC_DST_BLOCK = 'move-same-src-dst-block'
37_CHECK_PAYLOAD_SIG = 'payload-sig'
38CHECKS_TO_DISABLE = (
Gilad Arnold382df5c2013-05-03 12:49:28 -070039 _CHECK_DST_PSEUDO_EXTENTS,
40 _CHECK_MOVE_SAME_SRC_DST_BLOCK,
41 _CHECK_PAYLOAD_SIG,
Gilad Arnoldeaed0d12013-04-30 15:38:22 -070042)
43
Gilad Arnold553b0ec2013-01-26 01:00:39 -080044_TYPE_FULL = 'full'
45_TYPE_DELTA = 'delta'
46
47_DEFAULT_BLOCK_SIZE = 4096
48
Gilad Arnold9b90c932013-05-22 17:12:56 -070049_DEFAULT_PUBKEY_BASE_NAME = 'update-payload-key.pub.pem'
50_DEFAULT_PUBKEY_FILE_NAME = os.path.join(os.path.dirname(__file__),
51 _DEFAULT_PUBKEY_BASE_NAME)
52
Gilad Arnold553b0ec2013-01-26 01:00:39 -080053
54#
55# Helper functions.
56#
Gilad Arnoldcb638912013-06-24 04:57:11 -070057
Gilad Arnold553b0ec2013-01-26 01:00:39 -080058def _IsPowerOfTwo(val):
59 """Returns True iff val is a power of two."""
60 return val > 0 and (val & (val - 1)) == 0
61
62
63def _AddFormat(format_func, value):
64 """Adds a custom formatted representation to ordinary string representation.
65
66 Args:
Gilad Arnoldcb638912013-06-24 04:57:11 -070067 format_func: A value formatter.
68 value: Value to be formatted and returned.
Gilad Arnoldf583a7d2015-02-05 13:23:55 -080069
Gilad Arnold553b0ec2013-01-26 01:00:39 -080070 Returns:
71 A string 'x (y)' where x = str(value) and y = format_func(value).
Gilad Arnold553b0ec2013-01-26 01:00:39 -080072 """
Gilad Arnold6a3a3872013-10-04 18:18:45 -070073 ret = str(value)
74 formatted_str = format_func(value)
75 if formatted_str:
76 ret += ' (%s)' % formatted_str
77 return ret
Gilad Arnold553b0ec2013-01-26 01:00:39 -080078
79
80def _AddHumanReadableSize(size):
81 """Adds a human readable representation to a byte size value."""
82 return _AddFormat(format_utils.BytesToHumanReadable, size)
83
84
85#
86# Payload report generator.
87#
Gilad Arnoldcb638912013-06-24 04:57:11 -070088
Gilad Arnold553b0ec2013-01-26 01:00:39 -080089class _PayloadReport(object):
90 """A payload report generator.
91
92 A report is essentially a sequence of nodes, which represent data points. It
93 is initialized to have a "global", untitled section. A node may be a
94 sub-report itself.
Gilad Arnold553b0ec2013-01-26 01:00:39 -080095 """
96
Gilad Arnoldcb638912013-06-24 04:57:11 -070097 # Report nodes: Field, sub-report, section.
Gilad Arnold553b0ec2013-01-26 01:00:39 -080098 class Node(object):
99 """A report node interface."""
100
101 @staticmethod
102 def _Indent(indent, line):
103 """Indents a line by a given indentation amount.
104
105 Args:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700106 indent: The indentation amount.
107 line: The line content (string).
Gilad Arnoldf583a7d2015-02-05 13:23:55 -0800108
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800109 Returns:
110 The properly indented line (string).
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800111 """
112 return '%*s%s' % (indent, '', line)
113
114 def GenerateLines(self, base_indent, sub_indent, curr_section):
115 """Generates the report lines for this node.
116
117 Args:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700118 base_indent: Base indentation for each line.
119 sub_indent: Additional indentation for sub-nodes.
120 curr_section: The current report section object.
Gilad Arnoldf583a7d2015-02-05 13:23:55 -0800121
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800122 Returns:
123 A pair consisting of a list of properly indented report lines and a new
124 current section object.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800125 """
Gilad Arnoldcb638912013-06-24 04:57:11 -0700126 raise NotImplementedError
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800127
128 class FieldNode(Node):
129 """A field report node, representing a (name, value) pair."""
130
131 def __init__(self, name, value, linebreak, indent):
132 super(_PayloadReport.FieldNode, self).__init__()
133 self.name = name
134 self.value = value
135 self.linebreak = linebreak
136 self.indent = indent
137
138 def GenerateLines(self, base_indent, sub_indent, curr_section):
139 """Generates a properly formatted 'name : value' entry."""
140 report_output = ''
141 if self.name:
142 report_output += self.name.ljust(curr_section.max_field_name_len) + ' :'
143 value_lines = str(self.value).splitlines()
144 if self.linebreak and self.name:
145 report_output += '\n' + '\n'.join(
146 ['%*s%s' % (self.indent, '', line) for line in value_lines])
147 else:
148 if self.name:
149 report_output += ' '
150 report_output += '%*s' % (self.indent, '')
151 cont_line_indent = len(report_output)
152 indented_value_lines = [value_lines[0]]
153 indented_value_lines.extend(['%*s%s' % (cont_line_indent, '', line)
154 for line in value_lines[1:]])
155 report_output += '\n'.join(indented_value_lines)
156
157 report_lines = [self._Indent(base_indent, line + '\n')
158 for line in report_output.split('\n')]
159 return report_lines, curr_section
160
161 class SubReportNode(Node):
162 """A sub-report node, representing a nested report."""
163
164 def __init__(self, title, report):
165 super(_PayloadReport.SubReportNode, self).__init__()
166 self.title = title
167 self.report = report
168
169 def GenerateLines(self, base_indent, sub_indent, curr_section):
170 """Recurse with indentation."""
171 report_lines = [self._Indent(base_indent, self.title + ' =>\n')]
172 report_lines.extend(self.report.GenerateLines(base_indent + sub_indent,
173 sub_indent))
174 return report_lines, curr_section
175
176 class SectionNode(Node):
177 """A section header node."""
178
179 def __init__(self, title=None):
180 super(_PayloadReport.SectionNode, self).__init__()
181 self.title = title
182 self.max_field_name_len = 0
183
184 def GenerateLines(self, base_indent, sub_indent, curr_section):
185 """Dump a title line, return self as the (new) current section."""
186 report_lines = []
187 if self.title:
188 report_lines.append(self._Indent(base_indent,
189 '=== %s ===\n' % self.title))
190 return report_lines, self
191
192 def __init__(self):
193 self.report = []
194 self.last_section = self.global_section = self.SectionNode()
195 self.is_finalized = False
196
197 def GenerateLines(self, base_indent, sub_indent):
198 """Generates the lines in the report, properly indented.
199
200 Args:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700201 base_indent: The indentation used for root-level report lines.
202 sub_indent: The indentation offset used for sub-reports.
Gilad Arnoldf583a7d2015-02-05 13:23:55 -0800203
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800204 Returns:
205 A list of indented report lines.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800206 """
207 report_lines = []
208 curr_section = self.global_section
209 for node in self.report:
210 node_report_lines, curr_section = node.GenerateLines(
211 base_indent, sub_indent, curr_section)
212 report_lines.extend(node_report_lines)
213
214 return report_lines
215
216 def Dump(self, out_file, base_indent=0, sub_indent=2):
217 """Dumps the report to a file.
218
219 Args:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700220 out_file: File object to output the content to.
221 base_indent: Base indentation for report lines.
222 sub_indent: Added indentation for sub-reports.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800223 """
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800224 report_lines = self.GenerateLines(base_indent, sub_indent)
225 if report_lines and not self.is_finalized:
226 report_lines.append('(incomplete report)\n')
227
228 for line in report_lines:
229 out_file.write(line)
230
231 def AddField(self, name, value, linebreak=False, indent=0):
232 """Adds a field/value pair to the payload report.
233
234 Args:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700235 name: The field's name.
236 value: The field's value.
237 linebreak: Whether the value should be printed on a new line.
238 indent: Amount of extra indent for each line of the value.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800239 """
240 assert not self.is_finalized
241 if name and self.last_section.max_field_name_len < len(name):
242 self.last_section.max_field_name_len = len(name)
243 self.report.append(self.FieldNode(name, value, linebreak, indent))
244
245 def AddSubReport(self, title):
246 """Adds and returns a sub-report with a title."""
247 assert not self.is_finalized
248 sub_report = self.SubReportNode(title, type(self)())
249 self.report.append(sub_report)
250 return sub_report.report
251
252 def AddSection(self, title):
253 """Adds a new section title."""
254 assert not self.is_finalized
255 self.last_section = self.SectionNode(title)
256 self.report.append(self.last_section)
257
258 def Finalize(self):
259 """Seals the report, marking it as complete."""
260 self.is_finalized = True
261
262
263#
264# Payload verification.
265#
Gilad Arnoldcb638912013-06-24 04:57:11 -0700266
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800267class PayloadChecker(object):
268 """Checking the integrity of an update payload.
269
270 This is a short-lived object whose purpose is to isolate the logic used for
271 verifying the integrity of an update payload.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800272 """
273
Gilad Arnoldeaed0d12013-04-30 15:38:22 -0700274 def __init__(self, payload, assert_type=None, block_size=0,
275 allow_unhashed=False, disabled_tests=()):
Gilad Arnold272a4992013-05-08 13:12:53 -0700276 """Initialize the checker.
Gilad Arnoldeaed0d12013-04-30 15:38:22 -0700277
278 Args:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700279 payload: The payload object to check.
280 assert_type: Assert that payload is either 'full' or 'delta' (optional).
281 block_size: Expected filesystem / payload block size (optional).
282 allow_unhashed: Allow operations with unhashed data blobs.
283 disabled_tests: Sequence of tests to disable.
Gilad Arnoldeaed0d12013-04-30 15:38:22 -0700284 """
Gilad Arnoldcb638912013-06-24 04:57:11 -0700285 if not payload.is_init:
286 raise ValueError('Uninitialized update payload.')
Gilad Arnoldeaed0d12013-04-30 15:38:22 -0700287
288 # Set checker configuration.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800289 self.payload = payload
Gilad Arnoldeaed0d12013-04-30 15:38:22 -0700290 self.block_size = block_size if block_size else _DEFAULT_BLOCK_SIZE
291 if not _IsPowerOfTwo(self.block_size):
Gilad Arnoldcb638912013-06-24 04:57:11 -0700292 raise error.PayloadError(
293 'Expected block (%d) size is not a power of two.' % self.block_size)
Gilad Arnoldeaed0d12013-04-30 15:38:22 -0700294 if assert_type not in (None, _TYPE_FULL, _TYPE_DELTA):
Gilad Arnoldcb638912013-06-24 04:57:11 -0700295 raise error.PayloadError('Invalid assert_type value (%r).' %
296 assert_type)
Gilad Arnoldeaed0d12013-04-30 15:38:22 -0700297 self.payload_type = assert_type
298 self.allow_unhashed = allow_unhashed
299
300 # Disable specific tests.
301 self.check_dst_pseudo_extents = (
302 _CHECK_DST_PSEUDO_EXTENTS not in disabled_tests)
303 self.check_move_same_src_dst_block = (
304 _CHECK_MOVE_SAME_SRC_DST_BLOCK not in disabled_tests)
305 self.check_payload_sig = _CHECK_PAYLOAD_SIG not in disabled_tests
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800306
307 # Reset state; these will be assigned when the manifest is checked.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800308 self.sigs_offset = 0
309 self.sigs_size = 0
Gilad Arnold382df5c2013-05-03 12:49:28 -0700310 self.old_rootfs_fs_size = 0
311 self.old_kernel_fs_size = 0
312 self.new_rootfs_fs_size = 0
313 self.new_kernel_fs_size = 0
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800314
315 @staticmethod
316 def _CheckElem(msg, name, report, is_mandatory, is_submsg, convert=str,
317 msg_name=None, linebreak=False, indent=0):
318 """Adds an element from a protobuf message to the payload report.
319
320 Checks to see whether a message contains a given element, and if so adds
321 the element value to the provided report. A missing mandatory element
322 causes an exception to be raised.
323
324 Args:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700325 msg: The message containing the element.
326 name: The name of the element.
327 report: A report object to add the element name/value to.
328 is_mandatory: Whether or not this element must be present.
329 is_submsg: Whether this element is itself a message.
330 convert: A function for converting the element value for reporting.
331 msg_name: The name of the message object (for error reporting).
332 linebreak: Whether the value report should induce a line break.
333 indent: Amount of indent used for reporting the value.
Gilad Arnoldf583a7d2015-02-05 13:23:55 -0800334
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800335 Returns:
336 A pair consisting of the element value and the generated sub-report for
337 it (if the element is a sub-message, None otherwise). If the element is
338 missing, returns (None, None).
Gilad Arnoldf583a7d2015-02-05 13:23:55 -0800339
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800340 Raises:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700341 error.PayloadError if a mandatory element is missing.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800342 """
343 if not msg.HasField(name):
344 if is_mandatory:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700345 raise error.PayloadError('%smissing mandatory %s %r.' %
346 (msg_name + ' ' if msg_name else '',
347 'sub-message' if is_submsg else 'field',
348 name))
349 return None, None
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800350
351 value = getattr(msg, name)
352 if is_submsg:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700353 return value, report and report.AddSubReport(name)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800354 else:
355 if report:
356 report.AddField(name, convert(value), linebreak=linebreak,
357 indent=indent)
Gilad Arnoldcb638912013-06-24 04:57:11 -0700358 return value, None
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800359
360 @staticmethod
361 def _CheckMandatoryField(msg, field_name, report, msg_name, convert=str,
362 linebreak=False, indent=0):
363 """Adds a mandatory field; returning first component from _CheckElem."""
364 return PayloadChecker._CheckElem(msg, field_name, report, True, False,
365 convert=convert, msg_name=msg_name,
366 linebreak=linebreak, indent=indent)[0]
367
368 @staticmethod
369 def _CheckOptionalField(msg, field_name, report, convert=str,
370 linebreak=False, indent=0):
371 """Adds an optional field; returning first component from _CheckElem."""
372 return PayloadChecker._CheckElem(msg, field_name, report, False, False,
373 convert=convert, linebreak=linebreak,
374 indent=indent)[0]
375
376 @staticmethod
377 def _CheckMandatorySubMsg(msg, submsg_name, report, msg_name):
378 """Adds a mandatory sub-message; wrapper for _CheckElem."""
379 return PayloadChecker._CheckElem(msg, submsg_name, report, True, True,
380 msg_name)
381
382 @staticmethod
383 def _CheckOptionalSubMsg(msg, submsg_name, report):
384 """Adds an optional sub-message; wrapper for _CheckElem."""
385 return PayloadChecker._CheckElem(msg, submsg_name, report, False, True)
386
387 @staticmethod
388 def _CheckPresentIff(val1, val2, name1, name2, obj_name):
389 """Checks that val1 is None iff val2 is None.
390
391 Args:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700392 val1: first value to be compared.
393 val2: second value to be compared.
394 name1: name of object holding the first value.
395 name2: name of object holding the second value.
396 obj_name: Name of the object containing these values.
Gilad Arnoldf583a7d2015-02-05 13:23:55 -0800397
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800398 Raises:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700399 error.PayloadError if assertion does not hold.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800400 """
401 if None in (val1, val2) and val1 is not val2:
402 present, missing = (name1, name2) if val2 is None else (name2, name1)
Gilad Arnoldcb638912013-06-24 04:57:11 -0700403 raise error.PayloadError('%r present without %r%s.' %
404 (present, missing,
405 ' in ' + obj_name if obj_name else ''))
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800406
407 @staticmethod
408 def _Run(cmd, send_data=None):
409 """Runs a subprocess, returns its output.
410
411 Args:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700412 cmd: Sequence of command-line argument for invoking the subprocess.
413 send_data: Data to feed to the process via its stdin.
Gilad Arnoldf583a7d2015-02-05 13:23:55 -0800414
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800415 Returns:
416 A tuple containing the stdout and stderr output of the process.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800417 """
418 run_process = subprocess.Popen(cmd, stdin=subprocess.PIPE,
419 stdout=subprocess.PIPE)
Gilad Arnoldcb638912013-06-24 04:57:11 -0700420 try:
421 result = run_process.communicate(input=send_data)
422 finally:
423 exit_code = run_process.wait()
424
425 if exit_code:
426 raise RuntimeError('Subprocess %r failed with code %r.' %
427 (cmd, exit_code))
428
429 return result
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800430
431 @staticmethod
432 def _CheckSha256Signature(sig_data, pubkey_file_name, actual_hash, sig_name):
433 """Verifies an actual hash against a signed one.
434
435 Args:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700436 sig_data: The raw signature data.
437 pubkey_file_name: Public key used for verifying signature.
438 actual_hash: The actual hash digest.
439 sig_name: Signature name for error reporting.
Gilad Arnoldf583a7d2015-02-05 13:23:55 -0800440
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800441 Raises:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700442 error.PayloadError if signature could not be verified.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800443 """
444 if len(sig_data) != 256:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700445 raise error.PayloadError(
446 '%s: signature size (%d) not as expected (256).' %
447 (sig_name, len(sig_data)))
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800448 signed_data, _ = PayloadChecker._Run(
449 ['openssl', 'rsautl', '-verify', '-pubin', '-inkey', pubkey_file_name],
450 send_data=sig_data)
451
Gilad Arnold5502b562013-03-08 13:22:31 -0800452 if len(signed_data) != len(common.SIG_ASN1_HEADER) + 32:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700453 raise error.PayloadError('%s: unexpected signed data length (%d).' %
454 (sig_name, len(signed_data)))
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800455
Gilad Arnold5502b562013-03-08 13:22:31 -0800456 if not signed_data.startswith(common.SIG_ASN1_HEADER):
Gilad Arnoldcb638912013-06-24 04:57:11 -0700457 raise error.PayloadError('%s: not containing standard ASN.1 prefix.' %
458 sig_name)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800459
Gilad Arnold5502b562013-03-08 13:22:31 -0800460 signed_hash = signed_data[len(common.SIG_ASN1_HEADER):]
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800461 if signed_hash != actual_hash:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700462 raise error.PayloadError(
463 '%s: signed hash (%s) different from actual (%s).' %
464 (sig_name, common.FormatSha256(signed_hash),
465 common.FormatSha256(actual_hash)))
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800466
467 @staticmethod
468 def _CheckBlocksFitLength(length, num_blocks, block_size, length_name,
469 block_name=None):
470 """Checks that a given length fits given block space.
471
472 This ensures that the number of blocks allocated is appropriate for the
473 length of the data residing in these blocks.
474
475 Args:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700476 length: The actual length of the data.
477 num_blocks: The number of blocks allocated for it.
478 block_size: The size of each block in bytes.
479 length_name: Name of length (used for error reporting).
480 block_name: Name of block (used for error reporting).
Gilad Arnoldf583a7d2015-02-05 13:23:55 -0800481
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800482 Raises:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700483 error.PayloadError if the aforementioned invariant is not satisfied.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800484 """
485 # Check: length <= num_blocks * block_size.
Gilad Arnold382df5c2013-05-03 12:49:28 -0700486 if length > num_blocks * block_size:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700487 raise error.PayloadError(
488 '%s (%d) > num %sblocks (%d) * block_size (%d).' %
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800489 (length_name, length, block_name or '', num_blocks, block_size))
490
491 # Check: length > (num_blocks - 1) * block_size.
Gilad Arnold382df5c2013-05-03 12:49:28 -0700492 if length <= (num_blocks - 1) * block_size:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700493 raise error.PayloadError(
494 '%s (%d) <= (num %sblocks - 1 (%d)) * block_size (%d).' %
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800495 (length_name, length, block_name or '', num_blocks - 1, block_size))
496
Allie Wood7cf9f132015-02-26 14:28:19 -0800497 def _CheckMinorVersion(self, report, minor_version, payload_type):
498 """Checks that the minor version matches the payload type.
499
500 Args:
501 report: The report object to add to.
502 minor_version: The minor version of the payload.
503 payload_type: The type of payload (full or delta).
504
505 Raises:
506 error.PayloadError if any of the checks fails.
507 """
508 report.AddField('minor version', minor_version)
509
510 if minor_version == 0:
511 # Minor version 0 implies a full payload.
512 if payload_type != _TYPE_FULL:
513 raise error.PayloadError(
514 'Minor version 0 not compatible with payload type: %s.'
515 % payload_type)
516 elif minor_version in (1, 2):
517 # Minor version 1 or 2 implies a delta payload.
518 if payload_type != _TYPE_DELTA:
519 raise error.PayloadError(
520 'Minor version %d not compatible with payload type: %s.'
521 % (minor_version, payload_type))
522 else:
523 raise error.PayloadError('Unsupported minor version: %d' % minor_version)
524
Gilad Arnold382df5c2013-05-03 12:49:28 -0700525 def _CheckManifest(self, report, rootfs_part_size=0, kernel_part_size=0):
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800526 """Checks the payload manifest.
527
528 Args:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700529 report: A report object to add to.
530 rootfs_part_size: Size of the rootfs partition in bytes.
531 kernel_part_size: Size of the kernel partition in bytes.
Gilad Arnoldf583a7d2015-02-05 13:23:55 -0800532
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800533 Returns:
534 A tuple consisting of the partition block size used during the update
535 (integer), the signatures block offset and size.
Gilad Arnoldf583a7d2015-02-05 13:23:55 -0800536
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800537 Raises:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700538 error.PayloadError if any of the checks fail.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800539 """
540 manifest = self.payload.manifest
541 report.AddSection('manifest')
542
543 # Check: block_size must exist and match the expected value.
544 actual_block_size = self._CheckMandatoryField(manifest, 'block_size',
545 report, 'manifest')
546 if actual_block_size != self.block_size:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700547 raise error.PayloadError('Block_size (%d) not as expected (%d).' %
548 (actual_block_size, self.block_size))
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800549
550 # Check: signatures_offset <==> signatures_size.
551 self.sigs_offset = self._CheckOptionalField(manifest, 'signatures_offset',
552 report)
553 self.sigs_size = self._CheckOptionalField(manifest, 'signatures_size',
554 report)
555 self._CheckPresentIff(self.sigs_offset, self.sigs_size,
556 'signatures_offset', 'signatures_size', 'manifest')
557
558 # Check: old_kernel_info <==> old_rootfs_info.
559 oki_msg, oki_report = self._CheckOptionalSubMsg(manifest,
560 'old_kernel_info', report)
561 ori_msg, ori_report = self._CheckOptionalSubMsg(manifest,
562 'old_rootfs_info', report)
563 self._CheckPresentIff(oki_msg, ori_msg, 'old_kernel_info',
564 'old_rootfs_info', 'manifest')
565 if oki_msg: # equivalently, ori_msg
566 # Assert/mark delta payload.
567 if self.payload_type == _TYPE_FULL:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700568 raise error.PayloadError(
569 'Apparent full payload contains old_{kernel,rootfs}_info.')
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800570 self.payload_type = _TYPE_DELTA
571
572 # Check: {size, hash} present in old_{kernel,rootfs}_info.
Gilad Arnold382df5c2013-05-03 12:49:28 -0700573 self.old_kernel_fs_size = self._CheckMandatoryField(
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800574 oki_msg, 'size', oki_report, 'old_kernel_info')
575 self._CheckMandatoryField(oki_msg, 'hash', oki_report, 'old_kernel_info',
576 convert=common.FormatSha256)
Gilad Arnold382df5c2013-05-03 12:49:28 -0700577 self.old_rootfs_fs_size = self._CheckMandatoryField(
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800578 ori_msg, 'size', ori_report, 'old_rootfs_info')
579 self._CheckMandatoryField(ori_msg, 'hash', ori_report, 'old_rootfs_info',
580 convert=common.FormatSha256)
Gilad Arnold382df5c2013-05-03 12:49:28 -0700581
582 # Check: old_{kernel,rootfs} size must fit in respective partition.
583 if kernel_part_size and self.old_kernel_fs_size > kernel_part_size:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700584 raise error.PayloadError(
585 'Old kernel content (%d) exceed partition size (%d).' %
Gilad Arnold382df5c2013-05-03 12:49:28 -0700586 (self.old_kernel_fs_size, kernel_part_size))
587 if rootfs_part_size and self.old_rootfs_fs_size > rootfs_part_size:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700588 raise error.PayloadError(
589 'Old rootfs content (%d) exceed partition size (%d).' %
Gilad Arnold382df5c2013-05-03 12:49:28 -0700590 (self.old_rootfs_fs_size, rootfs_part_size))
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800591 else:
592 # Assert/mark full payload.
593 if self.payload_type == _TYPE_DELTA:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700594 raise error.PayloadError(
595 'Apparent delta payload missing old_{kernel,rootfs}_info.')
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800596 self.payload_type = _TYPE_FULL
597
598 # Check: new_kernel_info present; contains {size, hash}.
599 nki_msg, nki_report = self._CheckMandatorySubMsg(
600 manifest, 'new_kernel_info', report, 'manifest')
Gilad Arnold382df5c2013-05-03 12:49:28 -0700601 self.new_kernel_fs_size = self._CheckMandatoryField(
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800602 nki_msg, 'size', nki_report, 'new_kernel_info')
603 self._CheckMandatoryField(nki_msg, 'hash', nki_report, 'new_kernel_info',
604 convert=common.FormatSha256)
605
606 # Check: new_rootfs_info present; contains {size, hash}.
607 nri_msg, nri_report = self._CheckMandatorySubMsg(
608 manifest, 'new_rootfs_info', report, 'manifest')
Gilad Arnold382df5c2013-05-03 12:49:28 -0700609 self.new_rootfs_fs_size = self._CheckMandatoryField(
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800610 nri_msg, 'size', nri_report, 'new_rootfs_info')
611 self._CheckMandatoryField(nri_msg, 'hash', nri_report, 'new_rootfs_info',
612 convert=common.FormatSha256)
613
Gilad Arnold382df5c2013-05-03 12:49:28 -0700614 # Check: new_{kernel,rootfs} size must fit in respective partition.
615 if kernel_part_size and self.new_kernel_fs_size > kernel_part_size:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700616 raise error.PayloadError(
617 'New kernel content (%d) exceed partition size (%d).' %
Gilad Arnold382df5c2013-05-03 12:49:28 -0700618 (self.new_kernel_fs_size, kernel_part_size))
619 if rootfs_part_size and self.new_rootfs_fs_size > rootfs_part_size:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700620 raise error.PayloadError(
621 'New rootfs content (%d) exceed partition size (%d).' %
Gilad Arnold382df5c2013-05-03 12:49:28 -0700622 (self.new_rootfs_fs_size, rootfs_part_size))
623
Allie Woodf5c4f3e2015-02-20 16:57:46 -0800624 # Check: minor_version makes sense for the payload type. This check should
625 # run after the payload type has been set.
626 self._CheckMinorVersion(report, manifest.minor_version, self.payload_type)
627
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800628 def _CheckLength(self, length, total_blocks, op_name, length_name):
629 """Checks whether a length matches the space designated in extents.
630
631 Args:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700632 length: The total length of the data.
633 total_blocks: The total number of blocks in extents.
634 op_name: Operation name (for error reporting).
635 length_name: Length name (for error reporting).
Gilad Arnoldf583a7d2015-02-05 13:23:55 -0800636
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800637 Raises:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700638 error.PayloadError is there a problem with the length.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800639 """
640 # Check: length is non-zero.
641 if length == 0:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700642 raise error.PayloadError('%s: %s is zero.' % (op_name, length_name))
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800643
644 # Check that length matches number of blocks.
645 self._CheckBlocksFitLength(length, total_blocks, self.block_size,
646 '%s: %s' % (op_name, length_name))
647
Gilad Arnold382df5c2013-05-03 12:49:28 -0700648 def _CheckExtents(self, extents, usable_size, block_counters, name,
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800649 allow_pseudo=False, allow_signature=False):
650 """Checks a sequence of extents.
651
652 Args:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700653 extents: The sequence of extents to check.
654 usable_size: The usable size of the partition to which the extents apply.
655 block_counters: Array of counters corresponding to the number of blocks.
656 name: The name of the extent block.
657 allow_pseudo: Whether or not pseudo block numbers are allowed.
658 allow_signature: Whether or not the extents are used for a signature.
Gilad Arnoldf583a7d2015-02-05 13:23:55 -0800659
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800660 Returns:
661 The total number of blocks in the extents.
Gilad Arnoldf583a7d2015-02-05 13:23:55 -0800662
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800663 Raises:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700664 error.PayloadError if any of the entailed checks fails.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800665 """
666 total_num_blocks = 0
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800667 for ex, ex_name in common.ExtentIter(extents, name):
Gilad Arnoldcb638912013-06-24 04:57:11 -0700668 # Check: Mandatory fields.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800669 start_block = PayloadChecker._CheckMandatoryField(ex, 'start_block',
670 None, ex_name)
671 num_blocks = PayloadChecker._CheckMandatoryField(ex, 'num_blocks', None,
672 ex_name)
673 end_block = start_block + num_blocks
674
675 # Check: num_blocks > 0.
676 if num_blocks == 0:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700677 raise error.PayloadError('%s: extent length is zero.' % ex_name)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800678
679 if start_block != common.PSEUDO_EXTENT_MARKER:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700680 # Check: Make sure we're within the partition limit.
Gilad Arnold382df5c2013-05-03 12:49:28 -0700681 if usable_size and end_block * self.block_size > usable_size:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700682 raise error.PayloadError(
683 '%s: extent (%s) exceeds usable partition size (%d).' %
Gilad Arnold382df5c2013-05-03 12:49:28 -0700684 (ex_name, common.FormatExtent(ex, self.block_size), usable_size))
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800685
686 # Record block usage.
Gilad Arnoldcb638912013-06-24 04:57:11 -0700687 for i in xrange(start_block, end_block):
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800688 block_counters[i] += 1
Gilad Arnold5502b562013-03-08 13:22:31 -0800689 elif not (allow_pseudo or (allow_signature and len(extents) == 1)):
690 # Pseudo-extents must be allowed explicitly, or otherwise be part of a
691 # signature operation (in which case there has to be exactly one).
Gilad Arnoldcb638912013-06-24 04:57:11 -0700692 raise error.PayloadError('%s: unexpected pseudo-extent.' % ex_name)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800693
694 total_num_blocks += num_blocks
695
696 return total_num_blocks
697
698 def _CheckReplaceOperation(self, op, data_length, total_dst_blocks, op_name):
699 """Specific checks for REPLACE/REPLACE_BZ operations.
700
701 Args:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700702 op: The operation object from the manifest.
703 data_length: The length of the data blob associated with the operation.
704 total_dst_blocks: Total number of blocks in dst_extents.
705 op_name: Operation name for error reporting.
Gilad Arnoldf583a7d2015-02-05 13:23:55 -0800706
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800707 Raises:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700708 error.PayloadError if any check fails.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800709 """
Gilad Arnoldcb638912013-06-24 04:57:11 -0700710 # Check: Does not contain src extents.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800711 if op.src_extents:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700712 raise error.PayloadError('%s: contains src_extents.' % op_name)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800713
Gilad Arnoldcb638912013-06-24 04:57:11 -0700714 # Check: Contains data.
Gilad Arnold5502b562013-03-08 13:22:31 -0800715 if data_length is None:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700716 raise error.PayloadError('%s: missing data_{offset,length}.' % op_name)
Gilad Arnold5502b562013-03-08 13:22:31 -0800717
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800718 if op.type == common.OpType.REPLACE:
719 PayloadChecker._CheckBlocksFitLength(data_length, total_dst_blocks,
720 self.block_size,
721 op_name + '.data_length', 'dst')
722 else:
723 # Check: data_length must be smaller than the alotted dst blocks.
724 if data_length >= total_dst_blocks * self.block_size:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700725 raise error.PayloadError(
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800726 '%s: data_length (%d) must be less than allotted dst block '
Gilad Arnoldcb638912013-06-24 04:57:11 -0700727 'space (%d * %d).' %
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800728 (op_name, data_length, total_dst_blocks, self.block_size))
729
730 def _CheckMoveOperation(self, op, data_offset, total_src_blocks,
731 total_dst_blocks, op_name):
732 """Specific checks for MOVE operations.
733
734 Args:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700735 op: The operation object from the manifest.
736 data_offset: The offset of a data blob for the operation.
737 total_src_blocks: Total number of blocks in src_extents.
738 total_dst_blocks: Total number of blocks in dst_extents.
739 op_name: Operation name for error reporting.
Gilad Arnoldf583a7d2015-02-05 13:23:55 -0800740
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800741 Raises:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700742 error.PayloadError if any check fails.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800743 """
Gilad Arnoldcb638912013-06-24 04:57:11 -0700744 # Check: No data_{offset,length}.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800745 if data_offset is not None:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700746 raise error.PayloadError('%s: contains data_{offset,length}.' % op_name)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800747
Gilad Arnoldcb638912013-06-24 04:57:11 -0700748 # Check: total_src_blocks == total_dst_blocks.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800749 if total_src_blocks != total_dst_blocks:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700750 raise error.PayloadError(
751 '%s: total src blocks (%d) != total dst blocks (%d).' %
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800752 (op_name, total_src_blocks, total_dst_blocks))
753
Gilad Arnoldcb638912013-06-24 04:57:11 -0700754 # Check: For all i, i-th src block index != i-th dst block index.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800755 i = 0
756 src_extent_iter = iter(op.src_extents)
757 dst_extent_iter = iter(op.dst_extents)
758 src_extent = dst_extent = None
759 src_idx = src_num = dst_idx = dst_num = 0
760 while i < total_src_blocks:
761 # Get the next source extent, if needed.
762 if not src_extent:
763 try:
764 src_extent = src_extent_iter.next()
765 except StopIteration:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700766 raise error.PayloadError('%s: ran out of src extents (%d/%d).' %
767 (op_name, i, total_src_blocks))
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800768 src_idx = src_extent.start_block
769 src_num = src_extent.num_blocks
770
771 # Get the next dest extent, if needed.
772 if not dst_extent:
773 try:
774 dst_extent = dst_extent_iter.next()
775 except StopIteration:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700776 raise error.PayloadError('%s: ran out of dst extents (%d/%d).' %
777 (op_name, i, total_dst_blocks))
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800778 dst_idx = dst_extent.start_block
779 dst_num = dst_extent.num_blocks
780
Gilad Arnoldeaed0d12013-04-30 15:38:22 -0700781 if self.check_move_same_src_dst_block and src_idx == dst_idx:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700782 raise error.PayloadError(
783 '%s: src/dst block number %d is the same (%d).' %
784 (op_name, i, src_idx))
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800785
786 advance = min(src_num, dst_num)
787 i += advance
788
789 src_idx += advance
790 src_num -= advance
791 if src_num == 0:
792 src_extent = None
793
794 dst_idx += advance
795 dst_num -= advance
796 if dst_num == 0:
797 dst_extent = None
798
Gilad Arnold5502b562013-03-08 13:22:31 -0800799 # Make sure we've exhausted all src/dst extents.
800 if src_extent:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700801 raise error.PayloadError('%s: excess src blocks.' % op_name)
Gilad Arnold5502b562013-03-08 13:22:31 -0800802 if dst_extent:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700803 raise error.PayloadError('%s: excess dst blocks.' % op_name)
Gilad Arnold5502b562013-03-08 13:22:31 -0800804
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800805 def _CheckBsdiffOperation(self, data_length, total_dst_blocks, op_name):
Allie Woodf5c4f3e2015-02-20 16:57:46 -0800806 """Specific checks for BSDIFF and SOURCE_BSDIFF operations.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800807
808 Args:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700809 data_length: The length of the data blob associated with the operation.
810 total_dst_blocks: Total number of blocks in dst_extents.
811 op_name: Operation name for error reporting.
Gilad Arnoldf583a7d2015-02-05 13:23:55 -0800812
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800813 Raises:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700814 error.PayloadError if any check fails.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800815 """
Gilad Arnold5502b562013-03-08 13:22:31 -0800816 # Check: data_{offset,length} present.
817 if data_length is None:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700818 raise error.PayloadError('%s: missing data_{offset,length}.' % op_name)
Gilad Arnold5502b562013-03-08 13:22:31 -0800819
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800820 # Check: data_length is strictly smaller than the alotted dst blocks.
821 if data_length >= total_dst_blocks * self.block_size:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700822 raise error.PayloadError(
Gilad Arnold5502b562013-03-08 13:22:31 -0800823 '%s: data_length (%d) must be smaller than allotted dst space '
Gilad Arnoldcb638912013-06-24 04:57:11 -0700824 '(%d * %d = %d).' %
Gilad Arnold5502b562013-03-08 13:22:31 -0800825 (op_name, data_length, total_dst_blocks, self.block_size,
826 total_dst_blocks * self.block_size))
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800827
Allie Woodf5c4f3e2015-02-20 16:57:46 -0800828 def _CheckSourceCopyOperation(self, data_offset, total_src_blocks,
829 total_dst_blocks, op_name):
830 """Specific checks for SOURCE_COPY.
831
832 Args:
Allie Woodf5c4f3e2015-02-20 16:57:46 -0800833 data_offset: The offset of a data blob for the operation.
834 total_src_blocks: Total number of blocks in src_extents.
835 total_dst_blocks: Total number of blocks in dst_extents.
836 op_name: Operation name for error reporting.
837
838 Raises:
839 error.PayloadError if any check fails.
840 """
841 # Check: No data_{offset,length}.
842 if data_offset is not None:
843 raise error.PayloadError('%s: contains data_{offset,length}.' % op_name)
844
845 # Check: total_src_blocks == total_dst_blocks.
846 if total_src_blocks != total_dst_blocks:
847 raise error.PayloadError(
848 '%s: total src blocks (%d) != total dst blocks (%d).' %
849 (op_name, total_src_blocks, total_dst_blocks))
850
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800851 def _CheckOperation(self, op, op_name, is_last, old_block_counters,
Gilad Arnold4f50b412013-05-14 09:19:17 -0700852 new_block_counters, old_usable_size, new_usable_size,
Gilad Arnoldeaed0d12013-04-30 15:38:22 -0700853 prev_data_offset, allow_signature, blob_hash_counts):
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800854 """Checks a single update operation.
855
856 Args:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700857 op: The operation object.
858 op_name: Operation name string for error reporting.
859 is_last: Whether this is the last operation in the sequence.
860 old_block_counters: Arrays of block read counters.
861 new_block_counters: Arrays of block write counters.
862 old_usable_size: The overall usable size for src data in bytes.
863 new_usable_size: The overall usable size for dst data in bytes.
864 prev_data_offset: Offset of last used data bytes.
865 allow_signature: Whether this may be a signature operation.
866 blob_hash_counts: Counters for hashed/unhashed blobs.
Gilad Arnoldf583a7d2015-02-05 13:23:55 -0800867
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800868 Returns:
869 The amount of data blob associated with the operation.
Gilad Arnoldf583a7d2015-02-05 13:23:55 -0800870
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800871 Raises:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700872 error.PayloadError if any check has failed.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800873 """
874 # Check extents.
875 total_src_blocks = self._CheckExtents(
Gilad Arnold4f50b412013-05-14 09:19:17 -0700876 op.src_extents, old_usable_size, old_block_counters,
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800877 op_name + '.src_extents', allow_pseudo=True)
878 allow_signature_in_extents = (allow_signature and is_last and
879 op.type == common.OpType.REPLACE)
880 total_dst_blocks = self._CheckExtents(
Gilad Arnold382df5c2013-05-03 12:49:28 -0700881 op.dst_extents, new_usable_size, new_block_counters,
Gilad Arnoldeaed0d12013-04-30 15:38:22 -0700882 op_name + '.dst_extents',
883 allow_pseudo=(not self.check_dst_pseudo_extents),
884 allow_signature=allow_signature_in_extents)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800885
886 # Check: data_offset present <==> data_length present.
887 data_offset = self._CheckOptionalField(op, 'data_offset', None)
888 data_length = self._CheckOptionalField(op, 'data_length', None)
889 self._CheckPresentIff(data_offset, data_length, 'data_offset',
890 'data_length', op_name)
891
Gilad Arnoldcb638912013-06-24 04:57:11 -0700892 # Check: At least one dst_extent.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800893 if not op.dst_extents:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700894 raise error.PayloadError('%s: dst_extents is empty.' % op_name)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800895
896 # Check {src,dst}_length, if present.
897 if op.HasField('src_length'):
898 self._CheckLength(op.src_length, total_src_blocks, op_name, 'src_length')
899 if op.HasField('dst_length'):
900 self._CheckLength(op.dst_length, total_dst_blocks, op_name, 'dst_length')
901
902 if op.HasField('data_sha256_hash'):
903 blob_hash_counts['hashed'] += 1
904
Gilad Arnoldcb638912013-06-24 04:57:11 -0700905 # Check: Operation carries data.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800906 if data_offset is None:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700907 raise error.PayloadError(
908 '%s: data_sha256_hash present but no data_{offset,length}.' %
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800909 op_name)
910
Gilad Arnoldcb638912013-06-24 04:57:11 -0700911 # Check: Hash verifies correctly.
912 # pylint cannot find the method in hashlib, for some reason.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800913 # pylint: disable=E1101
914 actual_hash = hashlib.sha256(self.payload.ReadDataBlob(data_offset,
915 data_length))
916 if op.data_sha256_hash != actual_hash.digest():
Gilad Arnoldcb638912013-06-24 04:57:11 -0700917 raise error.PayloadError(
918 '%s: data_sha256_hash (%s) does not match actual hash (%s).' %
Gilad Arnold96405372013-05-04 00:24:58 -0700919 (op_name, common.FormatSha256(op.data_sha256_hash),
920 common.FormatSha256(actual_hash.digest())))
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800921 elif data_offset is not None:
922 if allow_signature_in_extents:
923 blob_hash_counts['signature'] += 1
Gilad Arnoldeaed0d12013-04-30 15:38:22 -0700924 elif self.allow_unhashed:
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800925 blob_hash_counts['unhashed'] += 1
926 else:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700927 raise error.PayloadError('%s: unhashed operation not allowed.' %
928 op_name)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800929
930 if data_offset is not None:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700931 # Check: Contiguous use of data section.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800932 if data_offset != prev_data_offset:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700933 raise error.PayloadError(
934 '%s: data offset (%d) not matching amount used so far (%d).' %
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800935 (op_name, data_offset, prev_data_offset))
936
937 # Type-specific checks.
Allie Wood7cf9f132015-02-26 14:28:19 -0800938 minor_version = self.payload.manifest.minor_version
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800939 if op.type in (common.OpType.REPLACE, common.OpType.REPLACE_BZ):
940 self._CheckReplaceOperation(op, data_length, total_dst_blocks, op_name)
Allie Wood7cf9f132015-02-26 14:28:19 -0800941 elif op.type == common.OpType.MOVE and minor_version == 1:
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800942 self._CheckMoveOperation(op, data_offset, total_src_blocks,
943 total_dst_blocks, op_name)
Allie Wood7cf9f132015-02-26 14:28:19 -0800944 elif op.type == common.OpType.BSDIFF and minor_version == 1:
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800945 self._CheckBsdiffOperation(data_length, total_dst_blocks, op_name)
Allie Wood7cf9f132015-02-26 14:28:19 -0800946 elif op.type == common.OpType.SOURCE_COPY and minor_version == 2:
Allie Woodf5c4f3e2015-02-20 16:57:46 -0800947 self._CheckSourceCopyOperation(data_offset, total_src_blocks,
948 total_dst_blocks, op_name)
Allie Wood7cf9f132015-02-26 14:28:19 -0800949 elif op.type == common.OpType.SOURCE_BSDIFF and minor_version == 2:
950 self._CheckBsdiffOperation(data_length, total_dst_blocks, op_name)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800951 else:
Allie Wood7cf9f132015-02-26 14:28:19 -0800952 raise error.PayloadError(
953 'Operation %s (type %d) not allowed in minor version %d' %
954 (op_name, op.type, minor_version))
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800955 return data_length if data_length is not None else 0
956
Gilad Arnold382df5c2013-05-03 12:49:28 -0700957 def _SizeToNumBlocks(self, size):
958 """Returns the number of blocks needed to contain a given byte size."""
959 return (size + self.block_size - 1) / self.block_size
960
961 def _AllocBlockCounters(self, total_size):
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800962 """Returns a freshly initialized array of block counters.
963
Gilad Arnoldcb638912013-06-24 04:57:11 -0700964 Note that the generated array is not portable as is due to byte-ordering
965 issues, hence it should not be serialized.
966
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800967 Args:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700968 total_size: The total block size in bytes.
Gilad Arnoldf583a7d2015-02-05 13:23:55 -0800969
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800970 Returns:
Gilad Arnold9753f3d2013-07-23 08:34:45 -0700971 An array of unsigned short elements initialized to zero, one for each of
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800972 the blocks necessary for containing the partition.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800973 """
Gilad Arnoldcb638912013-06-24 04:57:11 -0700974 return array.array('H',
975 itertools.repeat(0, self._SizeToNumBlocks(total_size)))
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800976
Gilad Arnold382df5c2013-05-03 12:49:28 -0700977 def _CheckOperations(self, operations, report, base_name, old_fs_size,
978 new_fs_size, new_usable_size, prev_data_offset,
979 allow_signature):
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800980 """Checks a sequence of update operations.
981
982 Args:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700983 operations: The sequence of operations to check.
984 report: The report object to add to.
985 base_name: The name of the operation block.
986 old_fs_size: The old filesystem size in bytes.
987 new_fs_size: The new filesystem size in bytes.
988 new_usable_size: The overall usable size of the new partition in bytes.
989 prev_data_offset: Offset of last used data bytes.
990 allow_signature: Whether this sequence may contain signature operations.
Gilad Arnoldf583a7d2015-02-05 13:23:55 -0800991
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800992 Returns:
Gilad Arnold5502b562013-03-08 13:22:31 -0800993 The total data blob size used.
Gilad Arnoldf583a7d2015-02-05 13:23:55 -0800994
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800995 Raises:
Gilad Arnoldcb638912013-06-24 04:57:11 -0700996 error.PayloadError if any of the checks fails.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800997 """
998 # The total size of data blobs used by operations scanned thus far.
999 total_data_used = 0
1000 # Counts of specific operation types.
1001 op_counts = {
1002 common.OpType.REPLACE: 0,
1003 common.OpType.REPLACE_BZ: 0,
1004 common.OpType.MOVE: 0,
1005 common.OpType.BSDIFF: 0,
Allie Woodf5c4f3e2015-02-20 16:57:46 -08001006 common.OpType.SOURCE_COPY: 0,
1007 common.OpType.SOURCE_BSDIFF: 0,
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001008 }
1009 # Total blob sizes for each operation type.
1010 op_blob_totals = {
1011 common.OpType.REPLACE: 0,
1012 common.OpType.REPLACE_BZ: 0,
Gilad Arnoldcb638912013-06-24 04:57:11 -07001013 # MOVE operations don't have blobs.
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001014 common.OpType.BSDIFF: 0,
Allie Woodf5c4f3e2015-02-20 16:57:46 -08001015 # SOURCE_COPY operations don't have blobs.
1016 common.OpType.SOURCE_BSDIFF: 0,
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001017 }
1018 # Counts of hashed vs unhashed operations.
1019 blob_hash_counts = {
1020 'hashed': 0,
1021 'unhashed': 0,
1022 }
1023 if allow_signature:
1024 blob_hash_counts['signature'] = 0
1025
1026 # Allocate old and new block counters.
Gilad Arnold4f50b412013-05-14 09:19:17 -07001027 old_block_counters = (self._AllocBlockCounters(new_usable_size)
Gilad Arnold382df5c2013-05-03 12:49:28 -07001028 if old_fs_size else None)
1029 new_block_counters = self._AllocBlockCounters(new_usable_size)
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001030
1031 # Process and verify each operation.
1032 op_num = 0
1033 for op, op_name in common.OperationIter(operations, base_name):
1034 op_num += 1
1035
Gilad Arnoldcb638912013-06-24 04:57:11 -07001036 # Check: Type is valid.
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001037 if op.type not in op_counts.keys():
Gilad Arnoldcb638912013-06-24 04:57:11 -07001038 raise error.PayloadError('%s: invalid type (%d).' % (op_name, op.type))
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001039 op_counts[op.type] += 1
1040
1041 is_last = op_num == len(operations)
1042 curr_data_used = self._CheckOperation(
1043 op, op_name, is_last, old_block_counters, new_block_counters,
Gilad Arnold4f50b412013-05-14 09:19:17 -07001044 new_usable_size if old_fs_size else 0, new_usable_size,
1045 prev_data_offset + total_data_used, allow_signature,
1046 blob_hash_counts)
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001047 if curr_data_used:
1048 op_blob_totals[op.type] += curr_data_used
1049 total_data_used += curr_data_used
1050
1051 # Report totals and breakdown statistics.
1052 report.AddField('total operations', op_num)
1053 report.AddField(
1054 None,
1055 histogram.Histogram.FromCountDict(op_counts,
1056 key_names=common.OpType.NAMES),
1057 indent=1)
1058 report.AddField('total blobs', sum(blob_hash_counts.values()))
1059 report.AddField(None,
1060 histogram.Histogram.FromCountDict(blob_hash_counts),
1061 indent=1)
1062 report.AddField('total blob size', _AddHumanReadableSize(total_data_used))
1063 report.AddField(
1064 None,
1065 histogram.Histogram.FromCountDict(op_blob_totals,
1066 formatter=_AddHumanReadableSize,
1067 key_names=common.OpType.NAMES),
1068 indent=1)
1069
1070 # Report read/write histograms.
1071 if old_block_counters:
1072 report.AddField('block read hist',
1073 histogram.Histogram.FromKeyList(old_block_counters),
1074 linebreak=True, indent=1)
1075
Gilad Arnold382df5c2013-05-03 12:49:28 -07001076 new_write_hist = histogram.Histogram.FromKeyList(
1077 new_block_counters[:self._SizeToNumBlocks(new_fs_size)])
1078 report.AddField('block write hist', new_write_hist, linebreak=True,
1079 indent=1)
1080
Gilad Arnoldcb638912013-06-24 04:57:11 -07001081 # Check: Full update must write each dst block once.
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001082 if self.payload_type == _TYPE_FULL and new_write_hist.GetKeys() != [1]:
Gilad Arnoldcb638912013-06-24 04:57:11 -07001083 raise error.PayloadError(
1084 '%s: not all blocks written exactly once during full update.' %
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001085 base_name)
1086
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001087 return total_data_used
1088
1089 def _CheckSignatures(self, report, pubkey_file_name):
1090 """Checks a payload's signature block."""
1091 sigs_raw = self.payload.ReadDataBlob(self.sigs_offset, self.sigs_size)
1092 sigs = update_metadata_pb2.Signatures()
1093 sigs.ParseFromString(sigs_raw)
1094 report.AddSection('signatures')
1095
Gilad Arnoldcb638912013-06-24 04:57:11 -07001096 # Check: At least one signature present.
1097 # pylint cannot see through the protobuf object, it seems.
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001098 # pylint: disable=E1101
1099 if not sigs.signatures:
Gilad Arnoldcb638912013-06-24 04:57:11 -07001100 raise error.PayloadError('Signature block is empty.')
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001101
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001102 last_ops_section = (self.payload.manifest.kernel_install_operations or
1103 self.payload.manifest.install_operations)
1104 fake_sig_op = last_ops_section[-1]
Gilad Arnold5502b562013-03-08 13:22:31 -08001105 # Check: signatures_{offset,size} must match the last (fake) operation.
1106 if not (fake_sig_op.type == common.OpType.REPLACE and
1107 self.sigs_offset == fake_sig_op.data_offset and
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001108 self.sigs_size == fake_sig_op.data_length):
Gilad Arnoldcb638912013-06-24 04:57:11 -07001109 raise error.PayloadError(
1110 'Signatures_{offset,size} (%d+%d) does not match last operation '
1111 '(%d+%d).' %
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001112 (self.sigs_offset, self.sigs_size, fake_sig_op.data_offset,
1113 fake_sig_op.data_length))
1114
1115 # Compute the checksum of all data up to signature blob.
1116 # TODO(garnold) we're re-reading the whole data section into a string
1117 # just to compute the checksum; instead, we could do it incrementally as
1118 # we read the blobs one-by-one, under the assumption that we're reading
1119 # them in order (which currently holds). This should be reconsidered.
1120 payload_hasher = self.payload.manifest_hasher.copy()
1121 common.Read(self.payload.payload_file, self.sigs_offset,
1122 offset=self.payload.data_offset, hasher=payload_hasher)
1123
1124 for sig, sig_name in common.SignatureIter(sigs.signatures, 'signatures'):
1125 sig_report = report.AddSubReport(sig_name)
1126
Gilad Arnoldcb638912013-06-24 04:57:11 -07001127 # Check: Signature contains mandatory fields.
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001128 self._CheckMandatoryField(sig, 'version', sig_report, sig_name)
1129 self._CheckMandatoryField(sig, 'data', None, sig_name)
1130 sig_report.AddField('data len', len(sig.data))
1131
Gilad Arnoldcb638912013-06-24 04:57:11 -07001132 # Check: Signatures pertains to actual payload hash.
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001133 if sig.version == 1:
1134 self._CheckSha256Signature(sig.data, pubkey_file_name,
1135 payload_hasher.digest(), sig_name)
1136 else:
Gilad Arnoldcb638912013-06-24 04:57:11 -07001137 raise error.PayloadError('Unknown signature version (%d).' %
1138 sig.version)
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001139
1140 def Run(self, pubkey_file_name=None, metadata_sig_file=None,
Gilad Arnold382df5c2013-05-03 12:49:28 -07001141 rootfs_part_size=0, kernel_part_size=0, report_out_file=None):
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001142 """Checker entry point, invoking all checks.
1143
1144 Args:
Gilad Arnoldcb638912013-06-24 04:57:11 -07001145 pubkey_file_name: Public key used for signature verification.
1146 metadata_sig_file: Metadata signature, if verification is desired.
1147 rootfs_part_size: The size of rootfs partitions in bytes (default: use
1148 reported filesystem size).
1149 kernel_part_size: The size of kernel partitions in bytes (default: use
1150 reported filesystem size).
1151 report_out_file: File object to dump the report to.
Gilad Arnoldf583a7d2015-02-05 13:23:55 -08001152
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001153 Raises:
Gilad Arnoldcb638912013-06-24 04:57:11 -07001154 error.PayloadError if payload verification failed.
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001155 """
Gilad Arnold9b90c932013-05-22 17:12:56 -07001156 if not pubkey_file_name:
1157 pubkey_file_name = _DEFAULT_PUBKEY_FILE_NAME
1158
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001159 report = _PayloadReport()
1160
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001161 # Get payload file size.
1162 self.payload.payload_file.seek(0, 2)
1163 payload_file_size = self.payload.payload_file.tell()
1164 self.payload.ResetFile()
1165
1166 try:
1167 # Check metadata signature (if provided).
1168 if metadata_sig_file:
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001169 metadata_sig = base64.b64decode(metadata_sig_file.read())
1170 self._CheckSha256Signature(metadata_sig, pubkey_file_name,
1171 self.payload.manifest_hasher.digest(),
1172 'metadata signature')
1173
Gilad Arnoldcb638912013-06-24 04:57:11 -07001174 # Part 1: Check the file header.
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001175 report.AddSection('header')
Gilad Arnoldcb638912013-06-24 04:57:11 -07001176 # Check: Payload version is valid.
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001177 if self.payload.header.version != 1:
Gilad Arnoldcb638912013-06-24 04:57:11 -07001178 raise error.PayloadError('Unknown payload version (%d).' %
1179 self.payload.header.version)
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001180 report.AddField('version', self.payload.header.version)
1181 report.AddField('manifest len', self.payload.header.manifest_len)
1182
Gilad Arnoldcb638912013-06-24 04:57:11 -07001183 # Part 2: Check the manifest.
Gilad Arnold382df5c2013-05-03 12:49:28 -07001184 self._CheckManifest(report, rootfs_part_size, kernel_part_size)
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001185 assert self.payload_type, 'payload type should be known by now'
1186
Gilad Arnoldcb638912013-06-24 04:57:11 -07001187 # Part 3: Examine rootfs operations.
Gilad Arnold0990f512013-05-30 17:09:31 -07001188 # TODO(garnold)(chromium:243559) only default to the filesystem size if
1189 # no explicit size provided *and* the partition size is not embedded in
1190 # the payload; see issue for more details.
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001191 report.AddSection('rootfs operations')
1192 total_blob_size = self._CheckOperations(
1193 self.payload.manifest.install_operations, report,
Gilad Arnold382df5c2013-05-03 12:49:28 -07001194 'install_operations', self.old_rootfs_fs_size,
1195 self.new_rootfs_fs_size,
1196 rootfs_part_size if rootfs_part_size else self.new_rootfs_fs_size,
1197 0, False)
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001198
Gilad Arnoldcb638912013-06-24 04:57:11 -07001199 # Part 4: Examine kernel operations.
Gilad Arnold0990f512013-05-30 17:09:31 -07001200 # TODO(garnold)(chromium:243559) as above.
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001201 report.AddSection('kernel operations')
1202 total_blob_size += self._CheckOperations(
1203 self.payload.manifest.kernel_install_operations, report,
Gilad Arnold382df5c2013-05-03 12:49:28 -07001204 'kernel_install_operations', self.old_kernel_fs_size,
1205 self.new_kernel_fs_size,
1206 kernel_part_size if kernel_part_size else self.new_kernel_fs_size,
1207 total_blob_size, True)
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001208
Gilad Arnoldcb638912013-06-24 04:57:11 -07001209 # Check: Operations data reach the end of the payload file.
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001210 used_payload_size = self.payload.data_offset + total_blob_size
1211 if used_payload_size != payload_file_size:
Gilad Arnoldcb638912013-06-24 04:57:11 -07001212 raise error.PayloadError(
1213 'Used payload size (%d) different from actual file size (%d).' %
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001214 (used_payload_size, payload_file_size))
1215
Gilad Arnoldcb638912013-06-24 04:57:11 -07001216 # Part 5: Handle payload signatures message.
Gilad Arnoldeaed0d12013-04-30 15:38:22 -07001217 if self.check_payload_sig and self.sigs_size:
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001218 self._CheckSignatures(report, pubkey_file_name)
1219
Gilad Arnoldcb638912013-06-24 04:57:11 -07001220 # Part 6: Summary.
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001221 report.AddSection('summary')
1222 report.AddField('update type', self.payload_type)
1223
1224 report.Finalize()
1225 finally:
1226 if report_out_file:
1227 report.Dump(report_out_file)