def source_domain_report(): destination_domain = request.form['destination_domain'] click_data_src = request.form['click_data_src'] report_data_dest = request.form['report_data_dest'] nonce = unhexlify(request.form['nonce']) signature_src = unhexlify(request.form['signature_src']) signature_dest = unhexlify(request.form['signature_dest']) source_domain_public_key = get_public_key( ('source-domain.com', destination_domain, click_data_src)) # this would normally make an http request out to a service like key transparency destination_domain_public_key = get_public_key( ('destination-domain.com', report_data_dest)) signature_src_valid = validate_signature(source_domain_public_key, nonce, signature_src) signature_dest_valid = validate_signature(destination_domain_public_key, nonce, signature_dest) valid = str(signature_src_valid and signature_dest_valid) app.logger.info(f'report recieved: valid: {valid} \n' f'click_data_src: {click_data_src}, \n' f'report_data_dest: {report_data_dest}\n' f'signature_src_valid: {signature_src_valid} \n' f'signature_dest_valid: {signature_dest_valid}') return valid
def blinding_service_unblind_message(): public_key = unhexlify(request.form['public_key']) message = unhexlify(request.form['message']) blinded_message = unhexlify(request.form['blind_message']) blinding_factor = unhexlify(request.form['blinding_factor']) unblinded = unblind_message(public_key, blinded_message, blinding_factor) valid = validate_signature(public_key, message, unblinded) if not valid: raise Exception('unblinded signature is invalid') return hexlify(unblinded)
def test_sign_blind_message(self): public_key = get_public_key(self.cache_key) blinded_message = blind_message( public_key, self.message, self.blinding_factor ) rsa_key = get_rsa_key(self.cache_key) blinded_sig = sign_message(rsa_key, blinded_message) unblinded_sig = unblind_message( public_key, blinded_sig, self.blinding_factor ) valid = validate_signature(public_key, self.message, unblinded_sig) self.assertTrue(valid)
def test_click_bind(self): # click data nonce = self.app.get( 'blinding-service.com/.well-known/random-bytes/16').data click_blinding_factor = self.app.get( 'blinding-service.com/.well-known/random-bytes/16').data csrf_token = build_new_csrf_token() # blind nonce for source-domain.com source_domain_public_key_response = self.app.get( f'/source-domain.com/.well-known/public-key/' f'{self.destination_domain}/{self.click_data_src}') self.assertEqual(source_domain_public_key_response.status_code, 200) source_domain_public_key = source_domain_public_key_response.data self.assertTrue(source_domain_public_key) blind_nonce_response = self.app.post( 'blinding-service.com/.well-known/blind', data={ 'public_key': source_domain_public_key, 'message': nonce, 'blinding_factor': click_blinding_factor }) self.assertEqual(blind_nonce_response.status_code, 200) blind_nonce = blind_nonce_response.data self.assertTrue(blind_nonce) self.assertEqual( unhexlify(blind_nonce), blind_message( unhexlify(source_domain_public_key), hash_message(unhexlify(nonce), 256), unhexlify(click_blinding_factor), )) # get signature of blind nonce from source-domain.com source_domain_blind_nonce_signature_response = self.app.post( 'source-domain.com/.well-known/blind-signing', data={ 'destination_domain': self.destination_domain, 'click_data_src': self.click_data_src, 'blinded_nonce_src': blind_nonce, 'csrf_token': csrf_token, }) self.assertEqual( source_domain_blind_nonce_signature_response.status_code, 200) source_domain_blind_nonce_signature = ( source_domain_blind_nonce_signature_response.data) self.assertTrue(source_domain_blind_nonce_signature) source_domain_public_key_bytes = unhexlify(source_domain_public_key) blind_nonce_bytes = unhexlify(blind_nonce) source_domain_blind_nonce_signature_bytes = ( unhexlify(source_domain_blind_nonce_signature)) # unblind signature from source-domain.com source_domain_nonce_signature_response = self.app.post( 'blinding-service.com/.well-known/unblind', data={ 'public_key': source_domain_public_key, 'message': nonce, 'blind_message': source_domain_blind_nonce_signature, 'blinding_factor': click_blinding_factor, }) self.assertEqual(source_domain_nonce_signature_response.status_code, 200) source_domain_nonce_signature = source_domain_nonce_signature_response.data self.assertTrue(source_domain_nonce_signature) # validate signature from source-domain.com self.assertTrue( validate_signature( unhexlify(source_domain_public_key), hash_message(unhexlify(nonce), 256), unhexlify(source_domain_nonce_signature), )) # report signing report_blinding_factor = self.app.get( 'blinding-service.com/.well-known/random-bytes/16').data csrf_token = build_new_csrf_token() # blind nonce for destination-domain.com destination_domain_public_key_response = self.app.get( f'/destination-domain.com/.well-known/public-key/{self.report_data_dest}' ) self.assertEqual(destination_domain_public_key_response.status_code, 200) destination_domain_public_key = destination_domain_public_key_response.data self.assertTrue(destination_domain_public_key) blind_nonce_response = self.app.post( 'blinding-service.com/.well-known/blind', data={ 'public_key': destination_domain_public_key, 'message': nonce, 'blinding_factor': report_blinding_factor }) self.assertEqual(blind_nonce_response.status_code, 200) blind_nonce = blind_nonce_response.data self.assertTrue(blind_nonce) self.assertEqual( unhexlify(blind_nonce), blind_message( unhexlify(destination_domain_public_key), hash_message(unhexlify(nonce), 256), unhexlify(report_blinding_factor), )) # get signature of blind nonce from destination-domain.com destination_domain_blind_nonce_signature_response = self.app.post( 'destination-domain.com/.well-known/blind-signing', data={ 'report_data_dest': self.report_data_dest, 'blinded_nonce_dest': blind_nonce, 'csrf_token': csrf_token, }) self.assertEqual( destination_domain_blind_nonce_signature_response.status_code, 200) destination_domain_blind_nonce_signature = ( destination_domain_blind_nonce_signature_response.data) self.assertTrue(destination_domain_blind_nonce_signature) # unblind signature from destination-domain.com destination_domain_nonce_signature_response = self.app.post( 'blinding-service.com/.well-known/unblind', data={ 'public_key': destination_domain_public_key, 'message': nonce, 'blind_message': destination_domain_blind_nonce_signature, 'blinding_factor': report_blinding_factor, }) self.assertEqual( destination_domain_nonce_signature_response.status_code, 200) destination_domain_nonce_signature = destination_domain_nonce_signature_response.data self.assertTrue(destination_domain_nonce_signature) # validate signature from destination-domain.com self.assertTrue( validate_signature( unhexlify(destination_domain_public_key), hash_message(unhexlify(nonce), 256), unhexlify(destination_domain_nonce_signature), )) source_domain_report_response = self.app.post( 'source-domain.com/.well-known/report', data={ 'destination_domain': self.destination_domain, 'click_data_src': self.click_data_src, 'report_data_dest': self.report_data_dest, 'nonce': nonce, 'signature_src': source_domain_nonce_signature, 'signature_dest': destination_domain_nonce_signature, }, ) self.assertEqual(source_domain_report_response.status_code, 200) source_domain_report = source_domain_report_response.data self.assertEqual(source_domain_report.decode('utf8'), 'True')
def test_sign_message(self): rsa_key = get_rsa_key(self.cache_key) sig = sign_message(rsa_key, self.message) public_key = get_public_key(self.cache_key) valid = validate_signature(public_key, self.message, sig) self.assertTrue(valid)