def validate_signature(signature, data): data_json = json.loads(data.decode('hex')) sig_cryptor = Cryptor(pubkey_hex=data_json['pubkey']) if sig_cryptor.verify(signature, data): return True else: return False
def encrypt(self, data): """ Encrypt the data with self.pub and return the ciphertext. @raises Exception: The encryption failed. """ assert self.pub, "Attempt to encrypt without key." cryptor = Cryptor(pubkey_hex=self.pub) return cryptor.encrypt(data)
def __init__(self, ip, port, pubkey, secret, ctx, guid, data_cb): super(CryptoPeerListener, self).__init__(ip, port, ctx, guid, data_cb) self.pubkey = pubkey self.secret = secret # FIXME: refactor this mess # this was copied as is from CryptoTransportLayer # soon all crypto code will be refactored and this will be removed self.cryptor = Cryptor(pubkey_hex=self.pubkey, privkey_hex=self.secret)
def update_listings_index(self): """This method is responsible for updating the DHT index of your store's listings. There is a dictionary in the DHT that has an array of your listing IDs. This updates that listing index in the DHT, simply put. """ # Store to marketplace listing index contract_index_key = hashlib.sha1('contracts-%s' % self.transport.guid).hexdigest() hashvalue = hashlib.new('ripemd160') hashvalue.update(contract_index_key) contract_index_key = hashvalue.hexdigest() # Calculate index of contracts contract_ids = self.db.selectEntries("contracts", { "market_id": self.transport.market_id, "deleted": 0 }) my_contracts = [] for contract_id in contract_ids: my_contracts.append(contract_id['key']) self.log.debug("My Contracts: %s", my_contracts) # Sign listing index for validation and tamper resistance data_string = str({ 'guid': self.transport.guid, 'contracts': my_contracts }) cryptor = Cryptor(privkey_hex=self.transport.settings['secret']) signature = cryptor.sign(data_string) value = { 'signature': signature.encode('hex'), 'data': { 'guid': self.transport.guid, 'contracts': my_contracts } } # Pass off to thread to keep GUI snappy t = Thread(target=self.transport.store, args=( contract_index_key, value, self.transport.guid, )) t.start()
def update_listings_index(self): """This method is responsible for updating the DHT index of your store's listings. There is a dictionary in the DHT that has an array of your listing IDs. This updates that listing index in the DHT, simply put. """ # Store to marketplace listing index contract_index_key = hashlib.sha1('contracts-%s' % self.transport.guid).hexdigest() hashvalue = hashlib.new('ripemd160') hashvalue.update(contract_index_key) contract_index_key = hashvalue.hexdigest() # Calculate index of contracts contract_ids = self.db.selectEntries( "contracts", {"market_id": self.transport.market_id, "deleted": 0} ) my_contracts = [] for contract_id in contract_ids: my_contracts.append(contract_id['key']) self.log.debug("My Contracts: %s", my_contracts) # Sign listing index for validation and tamper resistance data_string = str({'guid': self.transport.guid, 'contracts': my_contracts}) cryptor = Cryptor(privkey_hex=self.transport.settings['secret']) signature = cryptor.sign(data_string) value = { 'signature': signature.encode('hex'), 'data': { 'guid': self.transport.guid, 'contracts': my_contracts } } # Pass off to thread to keep GUI snappy t = Thread( target=self.transport.store, args=( contract_index_key, value, self.transport.guid, ) ) t.start()
def _setup_settings(self): try: self.settings = self.db.selectEntries( "settings", {"market_id": self.market_id}) except (OperationalError, DatabaseError) as e: print e raise SystemExit( "database file %s corrupt or empty - cannot continue" % self.db.db_path) if len(self.settings) == 0: self.settings = {"market_id": self.market_id, "welcome": "enable"} self.db.insertEntry("settings", self.settings) else: self.settings = self.settings[0] # Generate PGP key during initial setup or if previous PGP gen failed if not self.settings.get('PGPPubKey'): try: self.log.info( 'Generating PGP keypair. This may take several minutes...') print 'Generating PGP keypair. This may take several minutes...' gpg = gnupg.GPG() input_data = gpg.gen_key_input( key_type="RSA", key_length=2048, name_email='*****@*****.**', name_comment="Autogenerated by Open Bazaar", passphrase="P@ssw0rd") assert input_data is not None key = gpg.gen_key(input_data) assert key is not None pubkey_text = gpg.export_keys(key.fingerprint) newsettings = { "PGPPubKey": pubkey_text, "PGPPubkeyFingerprint": key.fingerprint } self.db.updateEntries("settings", newsettings, {"market_id": self.market_id}) self.settings.update(newsettings) self.log.info('PGP keypair generated.') except Exception as e: sys.exit("Encountered a problem with GPG: %s" % e) if not self.settings.get('pubkey'): # Generate Bitcoin keypair self._generate_new_keypair() if not self.settings.get('nickname'): newsettings = {'nickname': 'Default'} self.db.updateEntries('settings', newsettings, {"market_id": self.market_id}) self.settings.update(newsettings) self.nickname = self.settings.get('nickname', '') self.secret = self.settings.get('secret', '') self.pubkey = self.settings.get('pubkey', '') self.privkey = self.settings.get('privkey') self.btc_pubkey = privkey_to_pubkey(self.privkey) self.guid = self.settings.get('guid', '') self.sin = self.settings.get('sin', '') self.bitmessage = self.settings.get('bitmessage', '') if not self.settings.get('bitmessage'): # Generate Bitmessage address if self.bitmessage_api is not None: self._generate_new_bitmessage_address() self.cryptor = Cryptor(pubkey_hex=self.pubkey, privkey_hex=self.secret) # In case user wants to override with command line passed bitmessage values if self.ob_ctx.bm_user is not None and \ self.ob_ctx.bm_pass is not None and \ self.ob_ctx.bm_port is not None: self._connect_to_bitmessage()
class CryptoPeerListener(PeerListener): def __init__(self, ip, port, pubkey, secret, ctx, guid, data_cb): super(CryptoPeerListener, self).__init__(ip, port, ctx, guid, data_cb) self.pubkey = pubkey self.secret = secret # FIXME: refactor this mess # this was copied as is from CryptoTransportLayer # soon all crypto code will be refactored and this will be removed self.cryptor = Cryptor(pubkey_hex=self.pubkey, privkey_hex=self.secret) @staticmethod def is_handshake(message): """ Return whether message is a plaintext handshake :param message: serialized JSON :return: True if proper handshake message """ try: message = json.loads(message) except (ValueError, TypeError): return False return 'type' in message def _on_raw_message(self, serialized): """ Handles receipt of encrypted/plaintext message and passes to appropriate callback. :param serialized: :return: """ if not self.is_handshake(serialized): try: message = self.cryptor.decrypt(serialized) message = json.loads(message) signature = message['sig'].decode('hex') signed_data = message['data'] if CryptoPeerListener.validate_signature( signature, signed_data): message = signed_data.decode('hex') message = json.loads(message) if message.get('guid') != self.guid: return else: return except RuntimeError as e: self.log.error('Could not decrypt message properly %s', e) return except Exception as e: self.log.error('Cannot unpack data: %s', e) return else: message = json.loads(serialized) self.log.debugv('Received message of type "%s"', message.get('type', 'unknown')) self._data_cb(message) @staticmethod def validate_signature(signature, data): data_json = json.loads(data.decode('hex')) sig_cryptor = Cryptor(pubkey_hex=data_json['pubkey']) if sig_cryptor.verify(signature, data): return True else: return False
def sign(self, data): cryptor = Cryptor(privkey_hex=self.transport.settings['secret']) return cryptor.sign(data)
class CryptoPeerListener(PeerListener): def __init__(self, ip, port, pubkey, secret, ctx, guid, data_cb): super(CryptoPeerListener, self).__init__(ip, port, ctx, guid, data_cb) self.pubkey = pubkey self.secret = secret # FIXME: refactor this mess # this was copied as is from CryptoTransportLayer # soon all crypto code will be refactored and this will be removed self.cryptor = Cryptor(pubkey_hex=self.pubkey, privkey_hex=self.secret) @staticmethod def is_handshake(message): """ Return whether message is a plaintext handshake :param message: serialized JSON :return: True if proper handshake message """ try: message = json.loads(message) except (ValueError, TypeError): return False return 'type' in message def _on_raw_message(self, serialized): """ Handles receipt of encrypted/plaintext message and passes to appropriate callback. :param serialized: :return: """ if not self.is_handshake(serialized): try: message = self.cryptor.decrypt(serialized) message = json.loads(message) signature = message['sig'].decode('hex') signed_data = message['data'] if CryptoPeerListener.validate_signature(signature, signed_data): message = signed_data.decode('hex') message = json.loads(message) if message.get('guid') != self.guid: return else: return except RuntimeError as e: self.log.error('Could not decrypt message properly %s', e) return except Exception as e: self.log.error('Cannot unpack data: %s', e) return else: message = json.loads(serialized) self.log.info('Message [%s]', message.get('type')) self._data_cb(message) @staticmethod def validate_signature(signature, data): data_json = json.loads(data.decode('hex')) sig_cryptor = Cryptor(pubkey_hex=data_json['pubkey']) if sig_cryptor.verify(signature, data): return True else: return False