mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-04-16 08:42:50 +00:00
pkcs7/test: Format generate test script
Adhere to syntax and format recommendations from check-python-files.py Signed-off-by: Nick Child <nick.child@ibm.com>
This commit is contained in:
parent
3dafc6c3b3
commit
c7c94df715
@ -29,18 +29,26 @@ Given a valid DER pkcs7 file add tests to the test_suite_pkcs7.data file
|
|||||||
import sys
|
import sys
|
||||||
from os.path import exists
|
from os.path import exists
|
||||||
|
|
||||||
pkcs7_test_file = "../suites/test_suite_pkcs7.data"
|
PKCS7_TEST_FILE = "../suites/test_suite_pkcs7.data"
|
||||||
|
|
||||||
class Test:
|
class Test: # pylint: disable=too-few-public-methods
|
||||||
|
"""
|
||||||
|
A instance of a test in test_suite_pkcs7.data
|
||||||
|
"""
|
||||||
def __init__(self, name, depends, func_call):
|
def __init__(self, name, depends, func_call):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.depends = depends
|
self.depends = depends
|
||||||
self.func_call = func_call
|
self.func_call = func_call
|
||||||
|
|
||||||
|
# pylint: disable=no-self-use
|
||||||
def to_string(self):
|
def to_string(self):
|
||||||
return "\n" + self.name + "\n" + self.depends + "\n" + self.func_call + "\n"
|
return "\n" + self.name + "\n" + self.depends + "\n" + self.func_call + "\n"
|
||||||
|
|
||||||
class TestData:
|
class TestData:
|
||||||
|
"""
|
||||||
|
Take in test_suite_pkcs7.data file.
|
||||||
|
Allow for new tests to be added.
|
||||||
|
"""
|
||||||
mandatory_dep = "MBEDTLS_SHA256_C"
|
mandatory_dep = "MBEDTLS_SHA256_C"
|
||||||
test_name = "PKCS7 Parse Failure Invalid ASN1"
|
test_name = "PKCS7 Parse Failure Invalid ASN1"
|
||||||
test_function = "pkcs7_asn1_fail:"
|
test_function = "pkcs7_asn1_fail:"
|
||||||
@ -50,18 +58,20 @@ class TestData:
|
|||||||
self.new_tests = []
|
self.new_tests = []
|
||||||
|
|
||||||
def read_test_file(self, file):
|
def read_test_file(self, file):
|
||||||
|
"""
|
||||||
|
Parse the test_suite_pkcs7.data file.
|
||||||
|
"""
|
||||||
tests = []
|
tests = []
|
||||||
if not exists(file):
|
if not exists(file):
|
||||||
print(file + " Does not exist")
|
print(file + " Does not exist")
|
||||||
quit(1)
|
sys.exit()
|
||||||
f = open(file, "r")
|
with open(file, "r", encoding='UTF-8') as fp:
|
||||||
data = f.read()
|
data = fp.read()
|
||||||
f.close()
|
|
||||||
lines = [line.strip() for line in data.split('\n') if len(line.strip()) > 1]
|
lines = [line.strip() for line in data.split('\n') if len(line.strip()) > 1]
|
||||||
i = 0
|
i = 0
|
||||||
while i < len(lines):
|
while i < len(lines):
|
||||||
if "depends" in lines[i+1]:
|
if "depends" in lines[i+1]:
|
||||||
tests.append(Test(lines[i],lines[i+1], lines[i+2]))
|
tests.append(Test(lines[i], lines[i+1], lines[i+2]))
|
||||||
i += 3
|
i += 3
|
||||||
else:
|
else:
|
||||||
tests.append(Test(lines[i], None, lines[i+1]))
|
tests.append(Test(lines[i], None, lines[i+1]))
|
||||||
@ -71,28 +81,32 @@ class TestData:
|
|||||||
|
|
||||||
def add(self, name, func_call):
|
def add(self, name, func_call):
|
||||||
self.last_test_num += 1
|
self.last_test_num += 1
|
||||||
self.new_tests.append(Test(self.test_name + ": " + name + " #" + str(self.last_test_num), "depends_on:" + self.mandatory_dep, self.test_function + '"' + func_call + '"'))
|
self.new_tests.append(Test(self.test_name + ": " + name + " #" + \
|
||||||
|
str(self.last_test_num), "depends_on:" + self.mandatory_dep, \
|
||||||
|
self.test_function + '"' + func_call + '"'))
|
||||||
|
|
||||||
def write_changes(self):
|
def write_changes(self):
|
||||||
f = open(self.file_name, 'a')
|
with open(self.file_name, 'a', encoding='UTF-8') as fw:
|
||||||
f.write("\n")
|
fw.write("\n")
|
||||||
for t in self.new_tests:
|
for t in self.new_tests:
|
||||||
f.write(t.to_string())
|
fw.write(t.to_string())
|
||||||
f.close()
|
|
||||||
|
|
||||||
|
|
||||||
def asn1_mutate(data):
|
def asn1_mutate(data):
|
||||||
mutations = []
|
"""
|
||||||
reasons = []
|
We have been given an asn1 structure representing a pkcs7.
|
||||||
# we have been given an asn1 structure representing a pkcs7
|
We want to return an array of slightly modified versions of this data
|
||||||
# we want to return an array of slightly modified versions of this data
|
they should be modified in a way which makes the structure invalid
|
||||||
# they should be modified in a way which makes the structure invalid
|
|
||||||
|
|
||||||
# we know that asn1 structures are:
|
We know that asn1 structures are:
|
||||||
# |---1 byte showing data type---|----byte(s) for length of data---|---data content--|
|
|---1 byte showing data type---|----byte(s) for length of data---|---data content--|
|
||||||
# we know that some data types can contain other data types
|
We know that some data types can contain other data types.
|
||||||
|
Return a dictionary of reasons and mutated data types.
|
||||||
|
"""
|
||||||
|
|
||||||
# off the bat just add bytes to start and end of the buffer
|
# off the bat just add bytes to start and end of the buffer
|
||||||
|
mutations = []
|
||||||
|
reasons = []
|
||||||
mutations.append(["00"] + data)
|
mutations.append(["00"] + data)
|
||||||
reasons.append("Add null byte to start")
|
reasons.append("Add null byte to start")
|
||||||
mutations.append(data + ["00"])
|
mutations.append(data + ["00"])
|
||||||
@ -112,7 +126,8 @@ def asn1_mutate(data):
|
|||||||
length = int(data[leng_i], 16)
|
length = int(data[leng_i], 16)
|
||||||
|
|
||||||
tag = data[tag_i]
|
tag = data[tag_i]
|
||||||
print("Looking at ans1: offset " + str(i) + " tag = " + tag + ", length = " + str(length)+ ":")
|
print("Looking at ans1: offset " + str(i) + " tag = " + tag + \
|
||||||
|
", length = " + str(length)+ ":")
|
||||||
print(''.join(data[data_i:data_i+length]))
|
print(''.join(data[data_i:data_i+length]))
|
||||||
# change tag to something else
|
# change tag to something else
|
||||||
if tag == "02":
|
if tag == "02":
|
||||||
@ -131,11 +146,16 @@ def asn1_mutate(data):
|
|||||||
if len(new_length) == 1:
|
if len(new_length) == 1:
|
||||||
new_length = "0"+new_length
|
new_length = "0"+new_length
|
||||||
mutations.append(data[:data_i -1] + [new_length] + data[data_i:])
|
mutations.append(data[:data_i -1] + [new_length] + data[data_i:])
|
||||||
reasons.append("Change length from " + str(length) + " to " + str(length + 1))
|
reasons.append("Change length from " + str(length) + " to " \
|
||||||
# we can add another test here for tags that contain other tags where they have more data than there containing tags account for
|
+ str(length + 1))
|
||||||
|
# we can add another test here for tags that contain other tags \
|
||||||
|
# where they have more data than there containing tags account for
|
||||||
if tag in ["30", "a0", "31"]:
|
if tag in ["30", "a0", "31"]:
|
||||||
mutations.append(data[:data_i -1] + [new_length] + data[data_i:data_i + length] + ["00"] + data[data_i + length:])
|
mutations.append(data[:data_i -1] + [new_length] + \
|
||||||
reasons.append("Change contents of tag " + tag + " to contain one unaccounted extra byte")
|
data[data_i:data_i + length] + ["00"] + \
|
||||||
|
data[data_i + length:])
|
||||||
|
reasons.append("Change contents of tag " + tag + " to contain \
|
||||||
|
one unaccounted extra byte")
|
||||||
# change lengths to too small
|
# change lengths to too small
|
||||||
if int(data[data_i - 1], 16) > 0:
|
if int(data[data_i - 1], 16) > 0:
|
||||||
new_length = str(hex(int(data[data_i - 1], 16) - 1))[2:]
|
new_length = str(hex(int(data[data_i - 1], 16) - 1))[2:]
|
||||||
@ -152,24 +172,23 @@ def asn1_mutate(data):
|
|||||||
|
|
||||||
return list(zip(reasons, mutations))
|
return list(zip(reasons, mutations))
|
||||||
|
|
||||||
if len(sys.argv) < 2:
|
if __name__ == "__main__":
|
||||||
print("USAGE: " + sys.argv[0] + " <pkcs7_der_file>")
|
if len(sys.argv) < 2:
|
||||||
quit(1)
|
print("USAGE: " + sys.argv[0] + " <pkcs7_der_file>")
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
data_file = sys.argv[1]
|
DATA_FILE = sys.argv[1]
|
||||||
test_data = TestData(pkcs7_test_file)
|
TEST_DATA = TestData(PKCS7_TEST_FILE)
|
||||||
f = open(data_file, 'rb')
|
with open(DATA_FILE, 'rb') as f:
|
||||||
data_str = f.read().hex()
|
DATA_STR = f.read().hex()
|
||||||
f.close()
|
# make data an array of byte strings eg ['de','ad','be','ef']
|
||||||
# make data an array of byte strings eg ['de','ad','be','ef']
|
HEX_DATA = list(map(''.join, [[DATA_STR[i], DATA_STR[i+1]] for i in range(0, len(DATA_STR), \
|
||||||
data = list(map(''.join, [[data_str[i], data_str[i+1]] for i in range(0,len(data_str),2)]))
|
2)]))
|
||||||
# returns tuples of test_names and modified data buffers
|
# returns tuples of test_names and modified data buffers
|
||||||
mutations = asn1_mutate(data)
|
MUT_ARR = asn1_mutate(HEX_DATA)
|
||||||
|
|
||||||
print("made " + str(len(mutations)) + " new tests")
|
print("made " + str(len(MUT_ARR)) + " new tests")
|
||||||
for new_test in mutations:
|
for new_test in MUT_ARR:
|
||||||
test_data.add(new_test[0], ''.join(new_test[1]))
|
TEST_DATA.add(new_test[0], ''.join(new_test[1]))
|
||||||
|
|
||||||
|
|
||||||
test_data.write_changes()
|
|
||||||
|
|
||||||
|
TEST_DATA.write_changes()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user