def encrypt(self, msg, addr): logger.debug('before encryption') if not self.knows(addr): logger.debug('dont know who') return False, None logger.debug('checking methods') methods = self.get_methods(shex(self.contact_capa[addr])) if None in methods: logger.debug('dont have methods') return False, None rsa_tag, cipher_tag = methods logger.debug('methods checked: rsa_tag=%s, cipher_tag=%s' % (rsa_tag, cipher_tag)) try: rsa_key = self.contact_keys[addr][RSALEN[rsa_tag]] except KeyError: logger.debug('does not have public key for %s, %s' % (repr(addr), RSALEN[rsa_tag])) return False, None logger.debug('got rsa key %s' % (RSALEN[rsa_tag])) cipher = Cipher(*CIPHERS[cipher_tag]) logger.debug('ready to encypt cipher') enc_msg = cipher.encrypt(msg) enc_ses_key = rsa_key.encrypt(cipher.session_key) capa = shex(rsa_tag | cipher_tag) enc_raw = capa + ':' + binascii.b2a_hex( enc_ses_key) + ':' + binascii.b2a_hex(enc_msg) logger.debug('encryption ok') return True, enc_raw
def encrypt(self, msg, addr): logger.debug('before encryption') if not self.knows(addr): logger.debug('dont know who') return False, None logger.debug('checking methods') methods = self.get_methods(shex(self.contact_capa[addr])) if None in methods: logger.debug('dont have methods') return False, None rsa_tag, cipher_tag = methods logger.debug('methods checked: rsa_tag=%s, cipher_tag=%s' % (rsa_tag, cipher_tag)) try: rsa_key = self.contact_keys[addr][RSALEN[rsa_tag]] except KeyError: logger.debug('does not have public key for %s, %s' % (repr(addr), RSALEN[rsa_tag])) return False, None logger.debug('got rsa key %s' % (RSALEN[rsa_tag])) cipher = Cipher(*CIPHERS[cipher_tag]) logger.debug('ready to encypt cipher') enc_msg = cipher.encrypt(msg) enc_ses_key = rsa_key.encrypt(cipher.session_key) capa = shex(rsa_tag | cipher_tag) enc_raw = capa + ':' + binascii.b2a_hex(enc_ses_key) + ':' + binascii.b2a_hex(enc_msg) logger.debug('encryption ok') return True, enc_raw
def get_pubkey_raw(self, addr): capa = self.contact_capa[addr] & self.encrypt_capa logger.debug('try get_pubkey_raw: capa=%s' % (capa)) rsa_tag, cipher_tag = self.get_methods(shex(capa)) logger.debug('try get_pubkey_raw: rsa_tag=%s' % (rsa_tag)) if not rsa_tag: capa = self.encrypt_capa rsa_tag = c.IPMSG_RSA_1024 logger.debug('try get_pubkey_raw: %s' % (repr(addr))) raw = shex(capa) + ':' + self.get_pubkey_hex(rsa_tag) logger.debug('get_pubkey_raw: %s' % raw) return raw
def get_pubkey_hex(self, rsa_tag): logger.debug('try get_pubkey_hex: %s' % rsa_tag) key = self.key[RSALEN[rsa_tag]] logger.debug('key: %s' % key) logger.debug('get pubkey tuple') e, n = key.get_pubkey_tuple() logger.debug('key: e=%s, n=%s' % (e, n)) e = shex(e) n = shex(n) e = binascii.a2b_hex('0' * (len(e) % 2) + e) n = binascii.a2b_hex('0' * (len(n) % 2) + n) logger.debug('key: e=%s, n=%s' % (e, n)) key_hex = binascii.b2a_hex(e)[1:] + '-' + binascii.b2a_hex(n) logger.debug('got key_hex: %s' % (key_hex)) return key_hex
def get_pubkey_hex(self, rsa_tag): logger.debug('try get_pubkey_hex: %s' % rsa_tag) key = self.key[RSALEN[rsa_tag]] logger.debug('key: %s' % key) logger.debug('get pubkey tuple') e, n = key.get_pubkey_tuple() logger.debug('key: e=%s, n=%s' % (e, n)) e = shex(e) n = shex(n) e = binascii.a2b_hex('0'*(len(e)%2) + e) n = binascii.a2b_hex('0'*(len(n)%2) + n) logger.debug('key: e=%s, n=%s' % (e, n)) key_hex = binascii.b2a_hex(e)[1:] + '-' + binascii.b2a_hex(n) logger.debug('got key_hex: %s' % (key_hex)) return key_hex
def understands(self, addr): if not self.knows(addr): return False capa = self.contact_capa[addr] & self.encrypt_capa methods = self.get_methods(shex(capa)) if None in methods: return False return True
def req_pubkey(self, addr): tag = c.IPMSG_GETPUBKEY msg = shex(cry.encrypt_capa) engine.send(addr, tag, msg)