当前位置: 首页>>代码示例>>Python>>正文


Python Backend.get_public_key_raw方法代码示例

本文整理汇总了Python中backend.Backend.get_public_key_raw方法的典型用法代码示例。如果您正苦于以下问题:Python Backend.get_public_key_raw方法的具体用法?Python Backend.get_public_key_raw怎么用?Python Backend.get_public_key_raw使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在backend.Backend的用法示例。


在下文中一共展示了Backend.get_public_key_raw方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: run

# 需要导入模块: from backend import Backend [as 别名]
# 或者: from backend.Backend import get_public_key_raw [as 别名]
    def run(self):
        """
        Defines the logic for the communication with the target.

        IOErrors have to be handled carefully. Otherwise, the terminal application might crash
        and stops responding.
        :return:
        """
        backend = Backend()

        hw_activated = True
        delay_time = 0
        while hw_activated:
            try:
                self.device.start_nfc_reader()
                # Protocol states
                nonce_r_S = None
                nonce_r_T = None
                backend_token_hash = None

                """
                Protocol step 1: receive {r_S, pseudo_student_id} encrypted with our T_pub key
                """
                self.log.debug('Protocol step 1: receive {r_S, pseudo_student_id}')
                raw_msg_in = self.receive_and_decrypt()
                # Todo: check for errors

                # Extract nonce and pseudo student ID
                msg_in = json.loads(raw_msg_in.decode('utf-8'))
                nonce_r_S = msg_in['rs']
                pseudo_student_id = msg_in['pid']
                # Fetch public key of student
                self.log.info('Check Pseudo StudentID: %s', pseudo_student_id)
                key, backend_token_hash = backend.get_public_key_raw(pseudo_student_id)
                if key is not None:
                    self.log.debug('Got pub key from backend: %s', key)
                    # Todo: convert key to be usable in Python
                    # Set public key for future secure communication with target
                    self.nfccrypto.import_public_key_from_b64str(key)
                else:
                    # No key yet in the database
                    # Todo: inform user
                    self.log.info('Currently no key associated with this pseudo ID.')
                    raise ValueError('No key for empty or given pseudo ID.')

                """
                Protocol step 2: send {r_s, r_t} encrypted with S_pub key

                r_s: the nonce the target sent us
                r_t: a new nonce of our own
                T: hash of our public key (does this help anything? better sign the message in addition to that)
                """
                self.log.debug('Protocol step 2: send {r_s, r_t, T}')
                rawRandom = os.urandom(32)
                nonce_r_T = base64.b64encode(rawRandom).decode('utf-8')
                self.log.debug('My random r_t: %s', nonce_r_T)

                msg_out = json.dumps({
                    "type": 2,
                    "rs": nonce_r_S,
                    "rt": nonce_r_T
                }).encode('utf-8')

                self.encrypt_and_send(msg_out)

                """
                Protocol step 3: receive {r_t, token_hash} encrypted with our T_pub key

                At this step, the properties freshness and confidentiality should be fulfilled.
                It should be ok to trust the target.
                """
                self.log.debug('Protocol step 3: receive {r_t, commands}')
                raw_msg_in = self.receive_and_decrypt()
                # Todo: check for errors
                msg_in = json.loads(raw_msg_in.decode('utf-8'))
                msg_r_T = msg_in['rt']
                msg_token_hash = msg_in['htoken']
                self.log.debug('Androids token hash: %s', msg_token_hash)
                # Validate response
                if nonce_r_T == msg_r_T and backend_token_hash == msg_token_hash:
                    # Identical nonces and hashed tokens: message should be fresh and user should be authentic
                    self.log.info('Both nonce values are identical and hashes are the same. r_T="%s", hash="%s"',
                                  nonce_r_T, backend_token_hash)
                    # Grant access here
                    self.log.info('ACCESS GRANTED.')
                else:
                    self.log.error('Nonce r_T and received value or token hashes do not match. Replay attack?')
                    self.log.error('Nonce values match: %s, token hash values match: %s',
                                   nonce_r_T == msg_r_T,
                                   backend_token_hash == msg_token_hash)

                self.device.shutdown_nfc_reader()

                # Wait some time before permitting a client to connect again
                # Otherwise Android tries to reconnect, but actually, it should not be necessary
                delay_time = 3

            except (KeyboardInterrupt, SystemExit):
                hw_activated = False
            except AttributeError as e:
#.........这里部分代码省略.........
开发者ID:grandcat,项目名称:tum_getin,代码行数:103,代码来源:ProtocolLogic.py


注:本文中的backend.Backend.get_public_key_raw方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。