def get_data(self, integrity_check=None, sign=None): # Resolve the operation integrity_check = self.integrity_check if integrity_check is None else integrity_check sign = self.sign if sign is None else sign integrity_check = True if (integrity_check or sign) else False # Allow base to do any checks SecParseGenBase.get_data(self, integrity_check, sign) if not (integrity_check or sign): data = self._elf_parsegen.get_data() else: # Add the header and hash segment prog_phdr_entry, hash_phdr_entry, hash_segment_size =\ self._add_phdr_and_hash_segs(sign) try: # Generate the hash segment now hash_segment = self.get_hash_segment(sign) hash_segment += PAD_BYTE_1 * self._hash_padding_size # Check here for sizes mismatching just in case if len(hash_segment) != hash_segment_size: raise RuntimeError( 'Estimated hash table size was wrong. Estimate - ' + str(hash_segment_size) + ', Actual - ' + str(len(hash_segment))) # Re-add the hash segment, this time with the real data self._elf_parsegen.remove_segment(hash_phdr_entry) self._elf_parsegen.remove_segment(prog_phdr_entry) self._elf_parsegen.add_segment(prog_phdr_entry, '', toalign=self._align) self._elf_parsegen.add_segment(hash_phdr_entry, hash_segment, index=1, toalign=self._align) try: # Get the elf data data = self._elf_parsegen.get_data() finally: pass finally: # Remove the prog try: self._elf_parsegen.remove_segment(prog_phdr_entry) except Exception as e: logger.warning(str(e)) # Remove the hash try: self._elf_parsegen.remove_segment(hash_phdr_entry) except Exception as e: logger.warning(str(e)) return data
def get_data(self, integrity_check=None, sign=None, encrypt=None, get_hash_segment=False): # Resolve the operation integrity_check = self.integrity_check if integrity_check is None else integrity_check sign = self.sign if sign is None else sign encrypt = self.encrypt if encrypt is None else encrypt integrity_check = True if (integrity_check or sign or encrypt) else False # Allow base to do any checks SecParseGenBase.get_data(self, integrity_check, sign, encrypt) hash_segment = None if not (integrity_check or sign or encrypt): data = self._elf_parsegen.get_data() else: # Add the header and hash segment prog_phdr_entry, hash_phdr_entry, hash_segment_size = self._add_phdr_and_hash_segs(integrity_check, sign, encrypt) try: # Generate the hash segment now hash_segment = self.get_hash_segment(integrity_check, sign, encrypt) # Check here for sizes mismatching just in case if len(hash_segment) != hash_segment_size: raise RuntimeError('Estimated hash table size was wrong. Estimate - ' + str(hash_segment_size) + ', Actual - ' + str(len(hash_segment))) # Re-add the hash segment, this time with the real data self._elf_parsegen.remove_segment(hash_phdr_entry) self._elf_parsegen.remove_segment(prog_phdr_entry) self._elf_parsegen.add_segment(prog_phdr_entry, '') self._elf_parsegen.add_segment(hash_phdr_entry, hash_segment, index=1) # If encrypting, change the process segment data if encrypt: parsegen_updater = ParseGenEncDec(self.store_debug_data, self.encrypt_segment) parsegen_updater.update_parsegen(self.encdec.get_segment_num_scheme(), self._elf_parsegen) try: # Get the elf data data = self._elf_parsegen.get_data() finally: # Restore the process segment data if encrypt: try: parsegen_updater.revert_parsegen(self._elf_parsegen) except Exception as e: logger.warning(str(e)) finally: # Remove the prog try: self._elf_parsegen.remove_segment(prog_phdr_entry) except Exception as e: logger.warning(str(e)) # Remove the hash try: self._elf_parsegen.remove_segment(hash_phdr_entry) except Exception as e: logger.warning(str(e)) if get_hash_segment: return hash_segment return data
def get_data(self, sign=None, encrypt=None): # Resolve the operation sign = self.sign if sign is None else sign encrypt = self.encrypt if encrypt is None else encrypt # Allow base to do any checks SecParseGenBase.get_data(self, sign, encrypt) if encrypt: logger.error('Mbn Images do not support encryption. Returning the ' + ('signed' if sign else 'raw') + ' image.') return self._get_data_int(sign, False)
def get_data(self, integrity_check=None, sign=None): # Resolve the operation integrity_check = self.integrity_check if integrity_check is None else integrity_check sign = self.sign if sign is None else sign # Allow base to do any checks SecParseGenBase.get_data(self, integrity_check, sign) if integrity_check: raise RuntimeError('Mbn Images do not support integrity check.') return self._get_data_int(sign)
def get_data(self, sign=None, encrypt=None): # Resolve the operation sign = self.sign if sign is None else sign encrypt = self.encrypt if encrypt is None else encrypt # Allow base to do any checks SecParseGenBase.get_data(self, sign, encrypt) if sign: logger.error('Bin Images do not support signing. Returning the raw image.') return self._get_data_int(False, encrypt)
def get_data(self, integrity_check=None, sign=None, encrypt=None): # Resolve the operation integrity_check = self.integrity_check if integrity_check is None else integrity_check sign = self.sign if sign is None else sign encrypt = self.encrypt if encrypt is None else encrypt # Allow base to do any checks SecParseGenBase.get_data(self, integrity_check, sign, encrypt) if integrity_check: logger.error('Mbn Images do not support integrity check.') if encrypt: logger.error('Mbn Images do not support encryption.') return self._get_data_int(sign, False)
def get_data(self, integrity_check=None, sign=None, encrypt=None): # Resolve the operation integrity_check = self.integrity_check if integrity_check is None else integrity_check sign = self.sign if sign is None else sign encrypt = self.encrypt if encrypt is None else encrypt # Allow base to do any checks SecParseGenBase.get_data(self, integrity_check, sign, encrypt) if integrity_check: raise RuntimeError('AOST Licenses do not support integrity check.') if encrypt: raise RuntimeError('AOST Licenses do not support encryption.') return self._get_data_int(sign)
def get_data(self, integrity_check=None, sign=None, encrypt=None): # Resolve the operation integrity_check = self.integrity_check if integrity_check is None else integrity_check sign = self.sign if sign is None else sign encrypt = self.encrypt if encrypt is None else encrypt # Allow base to do any checks SecParseGenBase.get_data(self, integrity_check, sign, encrypt) if integrity_check: raise RuntimeError('Mbn Images do not support integrity check.') if encrypt: raise RuntimeError('Mbn Images do not support encryption.') return self._get_data_int(sign, False)
def get_data(self, sign=None, encrypt=None): # Resolve the operation sign = self.sign if sign is None else sign encrypt = self.encrypt if encrypt is None else encrypt # Allow base to do any checks SecParseGenBase.get_data(self, sign=sign, encrypt=False) # Get the signed data from the elf parser data = SecParseGenMbn.get_data(self, sign=sign, encrypt=False) # Add encryption params if encrypt is true if encrypt: data = self.encryption_params + self.encrypt_segment(data, 0) return data
def get_data(self, integrity_check=None, sign=None, encrypt=None): # Resolve the operation integrity_check = self.integrity_check if integrity_check is None else integrity_check sign = self.sign if sign is None else sign encrypt = self.encrypt if encrypt is None else encrypt integrity_check = True if (integrity_check or sign or encrypt) else False # Allow base to do any checks SecParseGenBase.get_data(self, integrity_check, sign, encrypt) if not (integrity_check or sign or encrypt): data = self._elf_parsegen.get_data() else: # Get the size of the hash segment hash_segment_size = self._compute_hash_segment_size( integrity_check, sign, encrypt) hash_segment_addr = self._compute_hash_address(self._elf_parsegen) # Add the prog & hash entries in phdrs # The data in the prog and hash at this time maybe dummy data phdr_class = self._elf_parsegen.get_new_phdr_entry() prog_phdr_entry = self._get_prog_phdr_entry( phdr_class(), self._elf_parsegen.ehdr) hash_phdr_entry = self._get_hash_phdr_entry( phdr_class(), hash_segment_size, hash_segment_addr, prog_phdr_entry.p_offset + prog_phdr_entry.p_filesz) self._elf_parsegen.shift( prog_phdr_entry.p_offset, hash_phdr_entry.p_offset + hash_phdr_entry.p_filesz - prog_phdr_entry.p_offset, hash_phdr_entry.p_align) self._elf_parsegen.add_segment(prog_phdr_entry, '') self._elf_parsegen.add_segment(hash_phdr_entry, PAD_BYTE_1 * hash_segment_size, index=1) # Update the dest pointer for the mbn self._mbn_parsegen.header.image_dest_ptr = hash_segment_addr + self._mbn_parsegen.header.get_size( ) try: # Generate the hash segment now hash_segment = self.get_hash_segment(integrity_check, sign, encrypt) # Check here for sizes mismatching just in case if len(hash_segment) != hash_segment_size: raise RuntimeError( 'Estimating the size of the hash table was wrong.') # Re-add the hash segment, this time with the real data self._elf_parsegen.remove_segment(hash_phdr_entry) self._elf_parsegen.remove_segment(prog_phdr_entry) self._elf_parsegen.add_segment(prog_phdr_entry, '') self._elf_parsegen.add_segment(hash_phdr_entry, hash_segment, index=1) # If encrypting, change the process segment data if encrypt: parsegen_updater_cls = ParseGenEncDecMap[ self.encdec.get_segment_num_scheme()] parsegen_updater = parsegen_updater_cls( self.store_debug_data, self.encrypt_segment) parsegen_updater.update_parsegen(self._elf_parsegen) try: # Get the elf data data = self._elf_parsegen.get_data() finally: # Restore the process segment data if encrypt: try: parsegen_updater.revert_parsegen( self._elf_parsegen) except Exception as e: logger.warning(str(e)) finally: # Remove the prog try: self._elf_parsegen.remove_segment(prog_phdr_entry) except Exception as e: logger.warning(str(e)) # Remove the hash try: self._elf_parsegen.remove_segment(hash_phdr_entry) except Exception as e: logger.warning(str(e)) return data
def get_data(self, integrity_check=None, sign=None, encrypt=None): # Resolve the operation integrity_check = self.integrity_check if integrity_check is None else integrity_check sign = self.sign if sign is None else sign encrypt = self.encrypt if encrypt is None else encrypt integrity_check = True if (integrity_check or sign or encrypt) else False # Allow base to do any checks SecParseGenBase.get_data(self, integrity_check, sign, encrypt) if not (integrity_check or sign or encrypt): data = self._elf_parsegen.get_data() else: # Get the size of the hash segment hash_segment_size = self._compute_hash_segment_size( integrity_check, sign, encrypt) hash_segment_addr = self._compute_hash_address(self._elf_parsegen) # Add the prog & hash entries in phdrs # The data in the prog and hash at this time maybe dummy data phdr_class = self._elf_parsegen.get_new_phdr_entry() prog_phdr_entry = self._get_prog_phdr_entry( phdr_class(), self._elf_parsegen.ehdr) hash_phdr_entry = self._get_hash_phdr_entry( phdr_class(), hash_segment_size, hash_segment_addr) self._elf_parsegen.add_segment(hash_phdr_entry, PAD_BYTE_1 * hash_segment_size) self._elf_parsegen.add_segment(prog_phdr_entry, '') # Update the dest pointer for the mbn self._mbn_parsegen.header.image_dest_ptr = hash_segment_addr + self._mbn_parsegen.header.get_size( ) error = None try: # Generate the hash segment now hash_segment = self.get_hash_segment(integrity_check, sign, encrypt) # Check here for sizes mismatching just in case if len(hash_segment) != hash_segment_size: raise RuntimeError( 'Estimating the size of the hash table was wrong.') # Re-add the hash segment, this time with the real data index = self._elf_parsegen.remove_segment(hash_phdr_entry) self._elf_parsegen.add_segment(hash_phdr_entry, hash_segment, index) # If encrypting, change the process segment data if encrypt: orig_process_data = self._elf_parsegen._process_segment_data self._elf_parsegen._process_segment_data = self._int_process_data self._int_process_data_segment_number = 0 try: # Get the elf data data = self._elf_parsegen.get_data() except Exception as e: error = e finally: # Restore the process segment data if encrypt: try: self._elf_parsegen._process_segment_data = orig_process_data except Exception as tmp_e: logger.warning(str(tmp_e)) # Raise error if needed if error is not None: raise error except Exception as e: error = e finally: # Remove the prog try: self._elf_parsegen.remove_segment(prog_phdr_entry) except Exception as tmp_e: logger.warning(str(tmp_e)) # Remove the hash try: self._elf_parsegen.remove_segment(hash_phdr_entry) except Exception as e: logger.warning(str(tmp_e)) # Raise error if needed if error is not None: raise error return data
def get_data(self, integrity_check=None, sign=None, encrypt=None): # Resolve the operation integrity_check = self.integrity_check if integrity_check is None else integrity_check sign = self.sign if sign is None else sign encrypt = self.encrypt if encrypt is None else encrypt integrity_check = True if (integrity_check or sign or encrypt) else False # Allow base to do any checks SecParseGenBase.get_data(self, integrity_check, sign, encrypt) if not (integrity_check or sign or encrypt): data = self._elf_parsegen.get_data() else: # Get the size of the hash segment hash_segment_size = self._compute_hash_segment_size(integrity_check, sign, encrypt) hash_segment_addr = self._compute_hash_address(self._elf_parsegen) # Add the prog & hash entries in phdrs # The data in the prog and hash at this time maybe dummy data phdr_class = self._elf_parsegen.get_new_phdr_entry() prog_phdr_entry = self._get_prog_phdr_entry(phdr_class(), self._elf_parsegen.ehdr) hash_phdr_entry = self._get_hash_phdr_entry(phdr_class(), hash_segment_size, hash_segment_addr) self._elf_parsegen.add_segment(hash_phdr_entry, PAD_BYTE_1 * hash_segment_size) self._elf_parsegen.add_segment(prog_phdr_entry, '') # Update the dest pointer for the mbn self._mbn_parsegen.header.image_dest_ptr = hash_segment_addr + self._mbn_parsegen.header.get_size() error = None try: # Generate the hash segment now hash_segment = self.get_hash_segment(integrity_check, sign, encrypt) # Check here for sizes mismatching just in case if len(hash_segment) != hash_segment_size: raise RuntimeError('Estimating the size of the hash table was wrong.') # Re-add the hash segment, this time with the real data self._elf_parsegen.remove_segment(hash_phdr_entry) self._elf_parsegen.remove_segment(prog_phdr_entry) self._elf_parsegen.add_segment(hash_phdr_entry, hash_segment) self._elf_parsegen.add_segment(prog_phdr_entry, '') # If encrypting, change the process segment data if encrypt: orig_process_data = self._elf_parsegen._process_segment_data self._elf_parsegen._process_segment_data = self._int_process_data self._int_process_data_segment_number = 0 try: # Get the elf data data = self._elf_parsegen.get_data() except Exception as e: error = e finally: # Restore the process segment data if encrypt: try: self._elf_parsegen._process_segment_data = orig_process_data except Exception as tmp_e: logger.warning(str(tmp_e)) # Raise error if needed if error is not None: raise error except Exception as e: error = e finally: # Remove the prog try: self._elf_parsegen.remove_segment(prog_phdr_entry) except Exception as tmp_e: logger.warning(str(tmp_e)) # Remove the hash try: self._elf_parsegen.remove_segment(hash_phdr_entry) except Exception as e: logger.warning(str(tmp_e)) # Raise error if needed if error is not None: raise error return data