Beispiel #1
0
 def __init__(self, data, imageinfo=None, mbn_properties=None, 
     general_properties=None, 
     encdec=None, 
     debug_dir=None, 
     debug_prefix=None, 
     debug_suffix=None):
     SecParseGenBase.__init__(self, data, imageinfo, general_properties,
                              encdec, debug_dir, debug_prefix, debug_suffix)
     
     # Check if file is encrypted
     bin_encryption_params = ''
     if self.encdec is not None:
         data, bin_encryption_params = self.encdec.extract_encryption_parameters(data)
         if bin_encryption_params:
             decryptor = self.encdec.get_decryptor(bin_encryption_params, self.encdec.get_dummy_key())
             data = decryptor.decrypt_segment(data, 0)
     
     SecParseGenMbn.__init__(self, data, imageinfo=imageinfo,
                             mbn_properties=mbn_properties,
                             general_properties=general_properties, 
                             encdec=self.encdec, 
                             debug_dir=debug_dir, 
                             debug_prefix=debug_prefix, 
                             debug_suffix=debug_suffix) 
     self.encryption_params = bin_encryption_params
Beispiel #2
0
 def __init__(self, data, imageinfo=None, ewm_properties=None,
              general_properties=None,
              encdec=None,
              debug_dir=None,
              debug_prefix=None,
              debug_suffix=None,):
     SecParseGenBase.__init__(self, data, imageinfo, general_properties,
                              encdec, debug_dir, debug_prefix, debug_suffix)
     
     # Check the arguments
     if imageinfo is not None:
         ewm_properties = imageinfo.image_type.ewm_properties
         general_properties = imageinfo.general_properties
     if ewm_properties is None:
         raise RuntimeError('EWM properties must not be None.')
     
     # Initialize internal properties
     self._image_entry = 0
     self._relocatable = False
     
     # Set properties from the config file
     self.image_entry = int(ewm_properties.image_entry, 16)
     self.relocatable = ewm_properties.relocatable
     
     # Remove elf header if it already exists
     data = self.extract_elf_wrapper(data)
     
     # Initialize the base mbn parser
     SecParseGenMbn.__init__(self, data, None, ewm_properties, general_properties,
                             self.encdec, debug_dir, debug_prefix, debug_suffix)
Beispiel #3
0
 def __init__(self, data, imageinfo=None, elf_properties=None,
              general_properties=None,
              encdec=None,
              debug_dir=None,
              debug_prefix=None,
              debug_suffix=None,):
     SecParseGenBase.__init__(self, data, imageinfo, general_properties,
                              encdec, debug_dir, debug_prefix, debug_suffix)
     
     # Check the arguments
     if imageinfo is not None:
         elf_properties = imageinfo.image_type.elf_properties
         general_properties = imageinfo.general_properties
     if elf_properties is None:
         raise RuntimeError('ELF properties must not be None.')
     
     # Initialize internal properties
     self._image_type = 0
     self._serial_num = None
     
     # Set properties from the config file
     self.image_type = elf_properties.image_type
     self.serial_num = elf_properties.serial_num
     
     # Initialize the elf parsegen
     self._elf_parsegen = elf.ParseGenElf(data, 
                                          self.debug_dir, 
                                          self.debug_prefix,
                                          self.debug_suffix)
     
     # Remove the prog header and hash segment 
     phdr_segment, hash_segment = self.extract_phdr_hash(self._elf_parsegen)
     self.store_debug_data(FILE_PROG_SEG_IN, phdr_segment)
     self.store_debug_data(FILE_HASH_SEG_IN, hash_segment)
     self.store_debug_data(FILE_PROG_HASH_REMOVED_IN, self._elf_parsegen.get_data())
     
     # If hash_segment is empty, create dummy hash_segment
     if not hash_segment:
         hash_segment = self._generate_default_hash_segment()
     
     # Initialize the base now
     SecParseGenMbn.__init__(self,
                             data=hash_segment,
                             imageinfo=None,
                             mbn_properties=self._get_sec_parsegen_mbn_properties(),
                             general_properties=general_properties,
                             encdec=self.encdec,
                             debug_dir=debug_dir,
                             debug_prefix=debug_prefix, 
                             debug_suffix=debug_suffix)
     
     # Check if the file is encrypted
     if self.is_encrypted():
         self._decrypt_data()
         self.store_debug_data(FILE_DECRYPTED_IN, self._elf_parsegen.get_data())
Beispiel #4
0
 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
 
     # Get the mbn data
     data = SecParseGenMbn.get_data(self, sign, encrypt)
     
     # Wrap the data in elf
     data = self.inject_elf_wrapper(data)
     
     return data
Beispiel #5
0
 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 __init__(
        self,
        data,
        imageinfo=None,
        elf_properties=None,
        general_properties=None,
        encdec=None,
        debug_dir=None,
        debug_prefix=None,
        debug_suffix=None,
    ):
        SecParseGenBase.__init__(self, data, imageinfo, general_properties,
                                 encdec, debug_dir, debug_prefix, debug_suffix)

        # Check the arguments
        if imageinfo is not None:
            elf_properties = imageinfo.image_type.elf_properties
            general_properties = imageinfo.general_properties
        if elf_properties is None:
            raise RuntimeError('ELF properties must not be None.')

        # Initialize internal properties
        self._image_type = 0
        self._serial_num = None
        self._max_elf_segments = MAX_PHDR_COUNT

        # Set properties from the config file
        self.image_type = elf_properties.image_type
        self.serial_num = general_properties.testsig_serialnum
        if elf_properties.max_elf_segments is not None:
            self.max_elf_segments = elf_properties.max_elf_segments

        # Initialize the elf parsegen
        self._elf_parsegen = elf.ParseGenElf(data, self.debug_dir,
                                             self.debug_prefix,
                                             self.debug_suffix)
        self._elf_parsegen.stabilize_phdrs()

        # Remove the prog header and hash segment
        phdr_segment, hash_segment = self.extract_phdr_hash(self._elf_parsegen)
        self.store_debug_data(FILE_PROG_SEG_IN, phdr_segment)
        self.store_debug_data(FILE_HASH_SEG_IN, hash_segment)
        self.store_debug_data(FILE_PROG_HASH_REMOVED_IN,
                              self._elf_parsegen.get_data())

        self.hash_segment = hash_segment

        # If hash_segment is empty, create dummy hash_segment
        if not hash_segment:
            hash_segment = self._generate_default_hash_segment()

        # Initialize the base now
        SecParseGenMbn.__init__(
            self,
            data=hash_segment,
            imageinfo=None,
            mbn_properties=self._get_sec_parsegen_mbn_properties(),
            general_properties=general_properties,
            encdec=self.encdec,
            debug_dir=debug_dir,
            debug_prefix=debug_prefix,
            debug_suffix=debug_suffix)

        if self._elf_parsegen.ehdr.e_ident_class == ELFCLASS64:
            self._mbn_parsegen.invalidate_pointers = True

        # Check if the file is encrypted
        if self.is_encrypted():
            self._decrypt_data()
            self.store_debug_data(FILE_DECRYPTED_IN,
                                  self._elf_parsegen.get_data())

            # Get the original data
            self._elf_parsegen = elf.ParseGenElf(self._elf_parsegen.get_data(),
                                                 self.debug_dir,
                                                 self.debug_prefix,
                                                 self.debug_suffix)

        # Do any segment checks
        self._validate_segments()
 def get_hash_segment(self, integrity_check, sign, encrypt):
     hash_table = self.get_hash_table()
     self._mbn_parsegen.code = hash_table
     return SecParseGenMbn._get_data_int(self, sign, encrypt)
    def __init__(
        self,
        data,
        imageinfo=None,
        elf_properties=None,
        general_properties=None,
        encdec=None,
        debug_dir=None,
        debug_prefix=None,
        debug_suffix=None,
    ):

        SecParseGenBase.__init__(self, data, imageinfo, general_properties,
                                 encdec, debug_dir, debug_prefix, debug_suffix)

        # Check the arguments
        if imageinfo is not None:
            elf_properties = imageinfo.image_type.elf_properties
            general_properties = imageinfo.general_properties
        if elf_properties is None:
            raise RuntimeError('ELF properties must not be None.')

        # Initialize internal properties
        self._image_type = 0
        self._serial_num = None
        self._max_elf_segments = MAX_PHDR_COUNT
        self._validate_ph_addrs = True
        self._validate_vir_addrs = False

        # Set properties from the config file
        self.image_type = elf_properties.image_type
        self.hash_seg_placement = elf_properties.hash_seg_placement
        self.serial_num = general_properties.testsig_serialnum
        if elf_properties.max_elf_segments is not None:
            self.max_elf_segments = elf_properties.max_elf_segments
        if elf_properties.validate_ph_addrs is not None:
            self.validate_ph_addrs = elf_properties.validate_ph_addrs
        if elf_properties.validate_vir_addrs is not None:
            self.validate_vir_addrs = elf_properties.validate_vir_addrs

        # Initialize the elf parsegen
        self._elf_parsegen = elf.ParseGenElf(data, self.debug_dir,
                                             self.debug_prefix,
                                             self.debug_suffix)
        self._elf_parsegen.stabilize_phdrs()

        # Remove the prog header and hash segment
        phdr_segment, hash_segment = self.extract_phdr_hash(self._elf_parsegen)
        self.store_debug_data(FILE_PROG_SEG_IN, phdr_segment)
        self.store_debug_data(FILE_HASH_SEG_IN, hash_segment)
        self.store_debug_data(FILE_PROG_HASH_REMOVED_IN,
                              self._elf_parsegen.get_data())

        self.hash_segment = hash_segment

        # If hash_segment is empty, create dummy hash_segment
        if not hash_segment:
            hash_segment = self._generate_default_hash_segment(
                self.secboot_version)

        # Initialize the base now
        SecParseGenMbn.__init__(
            self,
            data=hash_segment,
            imageinfo=None,
            mbn_properties=self._get_sec_parsegen_mbn_properties(),
            general_properties=general_properties,
            encdec=self.encdec,
            debug_dir=debug_dir,
            debug_prefix=debug_prefix,
            debug_suffix=debug_suffix)

        if self._elf_parsegen.ehdr.e_ident_class == ELFCLASS64 or self.secboot_version == SECBOOT_VERSION_2_0:
            self._mbn_parsegen.invalidate_pointers = True

        # Set the elf parsegen delegate
        delegate = SecParseGenElfDelegate(self._elf_parsegen,
                                          self.validate_ph_addrs,
                                          self.validate_vir_addrs)
        self._elf_parsegen.delegate = delegate

        # Check if the file is encrypted
        if self.is_encrypted():
            self._decrypt_data()
            self.store_debug_data(FILE_DECRYPTED_IN,
                                  self._elf_parsegen.get_data())

            # Get the original data
            self._elf_parsegen = elf.ParseGenElf(self._elf_parsegen.get_data(),
                                                 self.debug_dir,
                                                 self.debug_prefix,
                                                 self.debug_suffix)

            # Ensure that hashes match. If they don't we can assume that decryption failed
            if self._mbn_parsegen.code[32 * 2:] != self.get_hash_table():
                raise RuntimeError(
                    "Decryption of image failed. This can be caused by the use of an invalid L3 key"
                )

            # Set the elf parsegen delegate
            delegate = SecParseGenElfDelegate(self._elf_parsegen,
                                              self.validate_ph_addrs,
                                              self.validate_vir_addrs)
            self._elf_parsegen.delegate = delegate
    def __init__(self,
                 data,
                 imageinfo=None,
                 elf_properties=None,
                 general_properties=None,
                 encdec=None,
                 debug_dir=None,
                 debug_prefix=None,
                 debug_suffix=None,
                 validating=False,
                 signing=False,
                 sign_attr=False):

        SecParseGenBase.__init__(self,
                                 data,
                                 imageinfo,
                                 general_properties,
                                 encdec,
                                 debug_dir,
                                 debug_prefix,
                                 debug_suffix,
                                 validating,
                                 signing,
                                 sign_attr)

        # Check the arguments
        if imageinfo is not None:
            elf_properties = imageinfo.image_type.elf_properties
            general_properties = imageinfo.general_properties
        if elf_properties is None:
            raise RuntimeError('ELF properties must not be None.')

        # Initialize internal properties
        self._image_type = 0
        self._serial_num = None
        self._max_elf_segments = MAX_PHDR_COUNT
        self._validate_ph_addrs = True
        self._validate_vir_addrs = False

        # Set properties from the config file
        self.has_license_manager_segment = False
        self.lib_id = general_properties.lib_id
        self.client_id = general_properties.client_id
        self.image_type = elf_properties.image_type
        self.hash_seg_placement = elf_properties.hash_seg_placement
        self.serial_num = general_properties.testsig_serialnum
        self.signer = general_properties.selected_signer
        if elf_properties.max_elf_segments is not None:
            self.max_elf_segments = elf_properties.max_elf_segments
        if elf_properties.validate_ph_addrs is not None:
            self.validate_ph_addrs = elf_properties.validate_ph_addrs
        if elf_properties.validate_vir_addrs is not None:
            self.validate_vir_addrs = elf_properties.validate_vir_addrs
        if elf_properties.has_license_manager_segment is not None:
            self.has_license_manager_segment = elf_properties.has_license_manager_segment
        self._license_manager_segment = None

        # Initialize the elf parsegen
        self._elf_parsegen = elf.ParseGenElf(data,
                                             self.debug_dir,
                                             self.debug_prefix,
                                             self.debug_suffix)
        self._elf_parsegen.stabilize_phdrs()

        # Remove the prog header and hash segment
        phdr_segment, hash_segment = self.extract_phdr_hash(self._elf_parsegen)
        self.store_debug_data(FILE_PROG_SEG_IN, phdr_segment)
        self.store_debug_data(FILE_HASH_SEG_IN, hash_segment)
        self.store_debug_data(FILE_PROG_HASH_REMOVED_IN, self._elf_parsegen.get_data())

        self.hash_segment = hash_segment

        # Extract the hash table from the extracted hash segment
        if hash_segment:
            extracted_hash_table_size = SecParseGenMbn(data=hash_segment,
                                                       imageinfo=None,
                                                       mbn_properties=self._get_sec_parsegen_mbn_properties(),
                                                       general_properties=general_properties,
                                                       encdec=self.encdec,
                                                       debug_dir=debug_dir,
                                                       debug_prefix=debug_prefix,
                                                       debug_suffix=debug_suffix,
                                                       validating=validating,
                                                       signing=signing).code_size
        else:
            extracted_hash_table_size = 0
            # If hash_segment is empty, create dummy hash_segment
            hash_segment = self._generate_default_hash_segment(self.secboot_version)

        # Initialize the base now
        SecParseGenMbn.__init__(self,
                                data=hash_segment,
                                imageinfo=None,
                                mbn_properties=self._get_sec_parsegen_mbn_properties(),
                                general_properties=general_properties,
                                encdec=self.encdec,
                                debug_dir=debug_dir,
                                debug_prefix=debug_prefix,
                                debug_suffix=debug_suffix,
                                validating=validating,
                                signing=signing,
                                sign_attr=sign_attr)

        if self._elf_parsegen.ehdr.e_ident_class == ELFCLASS64 or self.secboot_version in [SECBOOT_VERSION_2_0, SECBOOT_VERSION_3_0]:
            self._mbn_parsegen.invalidate_pointers = True

        # Extract the hash algorithm that was used to generate the extracted hash table
        self._extract_hash_segment_algorithm(extracted_hash_table_size)

        # Set the elf parsegen delegate
        delegate = SecParseGenElfDelegate(self._elf_parsegen, self.validate_ph_addrs, self.validate_vir_addrs)
        self._elf_parsegen.delegate = delegate

        # Check if the file is encrypted
        if self.is_encrypted():
            if encrypted_key_provider_id_supported(general_properties.UIE_key):
                raise RuntimeError("Cannot decrypt an encrypted image when encrypted key provider is configured as UIE server")

            self._decrypt_data()
            self.store_debug_data(FILE_DECRYPTED_IN, self._elf_parsegen.get_data())

            # Get the original data
            self._elf_parsegen = elf.ParseGenElf(self._elf_parsegen.get_data(),
                                                 self.debug_dir,
                                                 self.debug_prefix,
                                                 self.debug_suffix)

            # Ensure that hashes match. If they don't we can assume that decryption failed
            if self._mbn_parsegen.code[SecParseGenElf.HASH_ALGO_TO_SIZE_MAP[self._mbn_parsegen.extracted_segment_hash_algorithm]*2:] != self.get_hash_table():
                raise RuntimeError("Decryption of image failed. This can be caused by:"
                                   "\n\t1) use of an invalid L3 key"
                                   "\n\t2) use of an incorrect segment hash algorithm")

            # Set the elf parsegen delegate
            delegate = SecParseGenElfDelegate(self._elf_parsegen, self.validate_ph_addrs, self.validate_vir_addrs)
            self._elf_parsegen.delegate = delegate

        # Perform license manager segment operations
        self.add_or_update_license_manager_segment()
    def __init__(self, data, imageinfo=None, elf_properties=None,
                 general_properties=None,
                 encdec=None,
                 debug_dir=None,
                 debug_prefix=None,
                 debug_suffix=None,):
        SecParseGenBase.__init__(self, data, imageinfo, general_properties,
                                 encdec, debug_dir, debug_prefix, debug_suffix)

        # Check the arguments
        if imageinfo is not None:
            elf_properties = imageinfo.image_type.elf_properties
            general_properties = imageinfo.general_properties
        if elf_properties is None:
            raise RuntimeError('ELF properties must not be None.')

        # Initialize internal properties
        self._image_type = 0
        self._serial_num = None

        # Set properties from the config file
        self.image_type = elf_properties.image_type
        self.serial_num = general_properties.testsig_serialnum

        # Initialize the elf parsegen
        self._elf_parsegen = elf.ParseGenElf(data,
                                             self.debug_dir,
                                             self.debug_prefix,
                                             self.debug_suffix)

        # Remove the prog header and hash segment
        phdr_segment, hash_segment = self.extract_phdr_hash(self._elf_parsegen)
        self.store_debug_data(FILE_PROG_SEG_IN, phdr_segment)
        self.store_debug_data(FILE_HASH_SEG_IN, hash_segment)
        self.store_debug_data(FILE_PROG_HASH_REMOVED_IN, self._elf_parsegen.get_data())

        self.hash_segment = hash_segment

        # If hash_segment is empty, create dummy hash_segment
        if not hash_segment:
            hash_segment = self._generate_default_hash_segment()

        # Initialize the base now
        SecParseGenMbn.__init__(self,
                                data=hash_segment,
                                imageinfo=None,
                                mbn_properties=self._get_sec_parsegen_mbn_properties(),
                                general_properties=general_properties,
                                encdec=self.encdec,
                                debug_dir=debug_dir,
                                debug_prefix=debug_prefix,
                                debug_suffix=debug_suffix)

        # Check if the file is encrypted
        if self.is_encrypted():
            self._decrypt_data()
            self.store_debug_data(FILE_DECRYPTED_IN, self._elf_parsegen.get_data())

            # Get the original data
            self._elf_parsegen = elf.ParseGenElf(self._elf_parsegen.get_data(),
                                                 self.debug_dir,
                                                 self.debug_prefix,
                                                 self.debug_suffix)
 def get_hash_segment(self, integrity_check, sign, encrypt):
     hash_table = self.get_hash_table()
     self._mbn_parsegen.code = hash_table
     return SecParseGenMbn._get_data_int(self, sign, encrypt)