def print_npdm(self):
     for _, sec in enumerate(self.sections):
         #print(_)
         #print(sec.fs_type)
         if sec.is_exefs:
             npdm = NPDM(sec.fs.open('main.npdm'))
             n = npdm.__str__()
             print(n)
             return n
 def ret_main(self):
     for _, sec in enumerate(self.sections):
         if sec.is_exefs:
             npdm = NPDM(sec.fs.open('main'))
             n = npdm.ret
             return n
    def _parse(self, titlekey=None, verify=False):
        raw_header = self._decrypt_header()
        self.header = NCAHeader(io.BytesIO(raw_header))

        if self.header.magic != b'NCA3':
            raise ValueError('Invalid NCA3 magic')

        if self.header.crypto_type_2 > self.header.crypto_type:
            self.crypto_type = self.header.crypto_type_2
        else:
            self.crypto_type = self.header.crypto_type
        if self.crypto_type > 0:
            self.crypto_type -= 1
        self.mkey = keys['master_key_%02x' % self.crypto_type]

        if titlekey is not None:
            #print(hx(titlekey))
            self.body_key = titlekey
            if self.header.rights_id != 0x10 * b'\0':
                self.has_rights_id = True
            else:
                self.has_rights_id = False
                self.kaek = self.kaeks[self.header.kaek_ind]
                keyblob = self._decrypt_keyarea(self.header.enc_key_area)
                self.key_area = b''.join(keyblob[0x10 * n:0x10 * (n + 1)]
                                         for n in range(4))
        elif self.header.rights_id == 0x10 * b'\0':
            self.has_rights_id = False
            self.kaek = self.kaeks[self.header.kaek_ind]
            keyblob = self._decrypt_keyarea(self.header.enc_key_area)
            self.key_area = b''.join(keyblob[0x10 * n:0x10 * (n + 1)]
                                     for n in range(4))
            self.body_key = self.key_area[0x20:0x30]
            #print(hx(keyblob))
            #print(hx(self.key_area))
            #print(hx(self.body_key))
        else:
            if self.header.rights_id != 0x10 * b'\0':
                self.has_rights_id = True
                self.rights_id = int.from_bytes(self.header.rights_id,
                                                byteorder='big')
                self.tkek = keys['titlekek_%02x' % self.crypto_type]
                if titlekey is not None:
                    self.enc_tkey = uhx(titlekey)
                else:
                    try:
                        self.enc_tkey = tkeys['%032x' % self.rights_id]
                    except KeyError:
                        self.has_tkey = False
                        return
                self.has_tkey = True
                self.body_key = self._decrypt_tkey(self.enc_tkey)
        self.sections = []
        if verify:
            self.is_valid = True
        for n, section_header in enumerate(self.header.section_headers):
            self.sections.append(
                self.SectionInNCA3(self,
                                   section_header,
                                   verify=verify,
                                   ifo=self.ifo))
            if verify and not self.sections[n].fs.is_valid:
                self.is_valid = False

        if verify:
            if self.header.sig_key_gen == 0:
                mod = int(hx(keys['nca_header_fixed_key_modulus_00']), 16)
            else:
                mod = int(hx(keys['nca_header_fixed_key_modulus_01']), 16)
            if not validate_rsa2048_pss_sig(mod, RSA_PUBLIC_EXPONENT,
                                            raw_header[0x200:0x400],
                                            self.header.rsa_sig_1):
                self.is_valid = False
                print('[WARN] Invalid fixed key signature')
            for sec in self.sections:
                if sec.is_exefs:
                    npdm = NPDM(sec.fs.open('main.npdm'))
                    mod = int.from_bytes(npdm.acid.rsa_pubk, byteorder='big')
                    if not validate_rsa2048_pss_sig(mod, RSA_PUBLIC_EXPONENT,
                                                    raw_header[0x200:0x400],
                                                    self.header.rsa_sig_2):
                        self.is_valid = False
                        print('[WARN] Invalid ACID signature')