2020-04-03 09:20:55 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2019-06-26 20:13:36 +00:00
|
|
|
|
2020-04-03 09:20:55 +00:00
|
|
|
validate new block's metadata
|
2019-11-20 10:52:50 +00:00
|
|
|
"""
|
2020-04-03 09:33:30 +00:00
|
|
|
from json import JSONDecodeError
|
2020-04-03 09:20:55 +00:00
|
|
|
import ujson as json
|
|
|
|
|
|
|
|
import logger, onionrexceptions
|
|
|
|
from etc import onionrvalues
|
|
|
|
from . import stringvalidators, epoch, bytesconverter
|
|
|
|
import config, filepaths, onionrcrypto
|
2019-11-20 10:52:50 +00:00
|
|
|
"""
|
2019-06-26 20:13:36 +00:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-11-20 10:52:50 +00:00
|
|
|
"""
|
2020-04-03 09:20:55 +00:00
|
|
|
|
2019-11-20 10:52:50 +00:00
|
|
|
|
2019-08-10 01:04:56 +00:00
|
|
|
def validate_metadata(metadata, block_data) -> bool:
|
2019-11-20 10:52:50 +00:00
|
|
|
"""Validate metadata meets onionr spec (does not validate proof value computation), take in either dictionary or json string"""
|
2019-08-09 20:41:27 +00:00
|
|
|
|
|
|
|
ret_data = False
|
2019-08-10 01:04:56 +00:00
|
|
|
max_clock_difference = onionrvalues.MAX_BLOCK_CLOCK_SKEW
|
2019-06-23 07:00:27 +00:00
|
|
|
|
|
|
|
# convert to dict if it is json string
|
|
|
|
if type(metadata) is str:
|
|
|
|
try:
|
|
|
|
metadata = json.loads(metadata)
|
2020-04-03 09:33:30 +00:00
|
|
|
except JSONDecodeError:
|
2019-06-23 07:00:27 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
# Validate metadata dict for invalid keys to sizes that are too large
|
2020-02-23 08:12:13 +00:00
|
|
|
maxAge = config.get("general.max_block_age", onionrvalues.DEFAULT_EXPIRE)
|
2019-06-23 07:00:27 +00:00
|
|
|
if type(metadata) is dict:
|
|
|
|
for i in metadata:
|
|
|
|
try:
|
2019-08-09 20:41:27 +00:00
|
|
|
onionrvalues.BLOCK_METADATA_LENGTHS[i]
|
2019-06-23 07:00:27 +00:00
|
|
|
except KeyError:
|
|
|
|
logger.warn('Block has invalid metadata key ' + i)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
testData = metadata[i]
|
|
|
|
try:
|
|
|
|
testData = len(testData)
|
|
|
|
except (TypeError, AttributeError) as e:
|
|
|
|
testData = len(str(testData))
|
2019-08-09 20:41:27 +00:00
|
|
|
if onionrvalues.BLOCK_METADATA_LENGTHS[i] < testData:
|
2019-06-23 07:00:27 +00:00
|
|
|
logger.warn('Block metadata key ' + i + ' exceeded maximum size')
|
|
|
|
break
|
|
|
|
if i == 'time':
|
2019-06-23 17:41:07 +00:00
|
|
|
if not stringvalidators.is_integer_string(metadata[i]):
|
2019-06-23 07:00:27 +00:00
|
|
|
logger.warn('Block metadata time stamp is not integer string or int')
|
|
|
|
break
|
2019-06-25 23:07:35 +00:00
|
|
|
isFuture = (metadata[i] - epoch.get_epoch())
|
2019-08-10 01:04:56 +00:00
|
|
|
if isFuture > max_clock_difference:
|
2019-11-30 07:11:46 +00:00
|
|
|
logger.warn('Block timestamp is skewed to the future over the max %s: %s', (max_clock_difference, isFuture))
|
2019-06-23 07:00:27 +00:00
|
|
|
break
|
2019-06-25 23:07:35 +00:00
|
|
|
if (epoch.get_epoch() - metadata[i]) > maxAge:
|
2019-06-23 07:00:27 +00:00
|
|
|
logger.warn('Block is outdated: %s' % (metadata[i],))
|
|
|
|
break
|
|
|
|
elif i == 'expire':
|
|
|
|
try:
|
2019-09-09 00:21:36 +00:00
|
|
|
if not int(metadata[i]) > epoch.get_epoch(): raise ValueError
|
|
|
|
except ValueError:
|
2019-06-25 23:07:35 +00:00
|
|
|
logger.warn('Block is expired: %s less than %s' % (metadata[i], epoch.get_epoch()))
|
2019-06-23 07:00:27 +00:00
|
|
|
break
|
|
|
|
elif i == 'encryptType':
|
|
|
|
try:
|
2019-09-09 00:21:36 +00:00
|
|
|
if not metadata[i] in ('asym', 'sym', ''): raise ValueError
|
2019-09-28 01:38:47 +00:00
|
|
|
if not config.get('general.store_plaintext_blocks', True):
|
|
|
|
break
|
2019-09-09 00:21:36 +00:00
|
|
|
except ValueError:
|
2019-06-23 07:00:27 +00:00
|
|
|
logger.warn('Invalid encryption mode')
|
|
|
|
break
|
2019-09-06 22:22:33 +00:00
|
|
|
elif i == 'sig':
|
|
|
|
try:
|
|
|
|
metadata['encryptType']
|
|
|
|
except KeyError:
|
|
|
|
signer = metadata['signer']
|
|
|
|
sig = metadata['sig']
|
|
|
|
encodedMeta = bytesconverter.str_to_bytes(metadata['meta'])
|
|
|
|
encodedBlock = bytesconverter.str_to_bytes(block_data)
|
|
|
|
if not onionrcrypto.signing.ed_verify(encodedMeta + encodedBlock[1:], signer, sig):
|
2020-08-23 11:29:12 +00:00
|
|
|
logger.warn(f'Block was signed by {signer}, but signature failed')
|
2019-09-06 22:22:33 +00:00
|
|
|
break
|
2019-06-23 07:00:27 +00:00
|
|
|
else:
|
|
|
|
# if metadata loop gets no errors, it does not break, therefore metadata is valid
|
|
|
|
# make sure we do not have another block with the same data content (prevent data duplication and replay attacks)
|
2019-09-22 08:28:38 +00:00
|
|
|
|
|
|
|
# Make sure time is set (validity was checked above if it is)
|
|
|
|
try:
|
|
|
|
metadata['time']
|
|
|
|
except KeyError:
|
|
|
|
return False
|
|
|
|
|
2019-08-10 01:04:56 +00:00
|
|
|
nonce = bytesconverter.bytes_to_str(onionrcrypto.hashers.sha3_hash(block_data))
|
2019-06-23 07:00:27 +00:00
|
|
|
try:
|
2019-07-19 19:49:56 +00:00
|
|
|
with open(filepaths.data_nonce_file, 'r') as nonceFile:
|
2019-06-23 07:00:27 +00:00
|
|
|
if nonce in nonceFile.read():
|
2019-09-17 01:16:06 +00:00
|
|
|
# we've seen that nonce before, so we can't pass metadata
|
2019-06-23 07:00:27 +00:00
|
|
|
raise onionrexceptions.DataExists
|
|
|
|
except FileNotFoundError:
|
2019-08-09 20:41:27 +00:00
|
|
|
ret_data = True
|
2019-06-23 07:00:27 +00:00
|
|
|
except onionrexceptions.DataExists:
|
2019-09-17 01:16:06 +00:00
|
|
|
# do not set ret_data to True, because data has been seen before
|
|
|
|
logger.warn(f'{nonce} seen before')
|
|
|
|
raise onionrexceptions.DataExists
|
2019-06-23 07:00:27 +00:00
|
|
|
else:
|
2019-08-09 20:41:27 +00:00
|
|
|
ret_data = True
|
2019-06-23 07:00:27 +00:00
|
|
|
else:
|
|
|
|
logger.warn('In call to utils.validateMetadata, metadata must be JSON string or a dictionary object')
|
|
|
|
|
2019-08-09 20:41:27 +00:00
|
|
|
return ret_data
|