def load_public_key(self, from_private=False): """ Loads public key from path. Returns: True on success, False otherwise """ if self.path == '' or self.suffix == '': return False file_path = self.path + '/public_%s.pem' % self.suffix p_file = _libc.fopen(ctypes.c_char_p(file_path.encode('utf-8')), ctypes.c_char_p(b'r')) _ssl.PEM_read_RSAPublicKey(p_file, ctypes.byref(self.key), None, None) # Cleanup _libc.fclose(p_file) # Turn on blinding if not from_private and _ssl.RSA_blinding_on(self.key, None) != 1: logging.debug('Failed to turn on blinding for RSA key') return False self.size = _ssl.RSA_size(self.key) self.bn_n = self._get_mod() return True
def generate(self, bits): """ Generate RSA key of size bits. Args: bits (int): The size of the key. Has to be divisible by 8. Returns: True on success, False otherwise """ if bits % 8 != 0: return False if _ssl.RSA_generate_key_ex(self.key, bits, self.bn_e, None) != 1: logging.debug("Failed to generate rsa Key") return False self.size = _ssl.RSA_size(self.key) self._get_mod() self.is_private = True return True