38 __THIS_FILE_VERSION_NUMBER__ = 3
39 __THIS_FILE_VERSION_STRING__ =
"0.1.5"
40 __THIS_FILE_ALGORITHM_VERSION =
'hc1'
42 __cfp_json_path__ =
'/../cFp.json'
43 __shell_type_key__ =
'cFpSRAtype'
44 __mod_type_key__ =
'cFpMOD'
45 __dcps_folder_name__ =
'/dcps/'
46 __sig_file_ending__ =
'sig'
47 __rpt_file_ending__ =
'rpt'
48 __ignore_key__ =
'ignore'
49 __ignore_hash__ =
'719a965d6d8936f09550efb75bcf4bff9f956143d9f78e30b62b966b6a9ebc35'
54 assert __THIS_FILE_ALGORITHM_VERSION ==
'hc1'
55 sha256_hash = hashlib.sha256()
56 with open(file_path,
'rb')
as f:
58 for byte_block
in iter(
lambda: f.read(4096), b
""):
59 sha256_hash.update(byte_block)
60 return sha256_hash.hexdigest()
65 assert __THIS_FILE_ALGORITHM_VERSION ==
'hc1'
66 sha256_hash = hashlib.sha256(inp_string.encode(
'utf-8'))
67 return sha256_hash.hexdigest()
70 def get_sig_string(dcp_hash, my_hash, current_cl_cert, new_pr_hash, rpt_hash, debugging_flow=None):
72 assert __THIS_FILE_ALGORITHM_VERSION ==
'hc1'
73 new_cert_string =
str(dcp_hash) +
str(my_hash) +
str(current_cl_cert) +
str(new_pr_hash) +
str(rpt_hash)
74 cert = hashlib.sha384(new_cert_string.encode(
'utf-8')).hexdigest()
75 if debugging_flow
is not None:
76 print(
"\tnew_cert_string: {}".format(new_cert_string))
80 def main(new_bin_file_name, pr_verify_rpt_file_name):
82 me_abs_dir = os.path.dirname(os.path.realpath(__file__))
83 me_abs_file = os.path.abspath(os.path.realpath(__file__))
84 cfp_json_file = me_abs_dir + __cfp_json_path__
85 debugging_flow = os.environ.get(
'CFP_DEBUGGING')
86 if debugging_flow
is not None:
87 cfp_json_file = me_abs_dir + debugging_flow +
'/cFp.json'
88 with open(cfp_json_file,
'r')
as json_file:
89 cFp_data = json.load(json_file)
92 root_abs = os.path.realpath(me_abs_dir+
"/../")
93 if debugging_flow
is not None:
94 root_abs = os.path.realpath(me_abs_dir + debugging_flow +
"/env/" +
"/../")
95 cFp_data[
'abs_path'] = root_abs
96 dcps_folder = root_abs + __dcps_folder_name__
98 dcp_file_name =
"3_top{}_STATIC.dcp".format(cFp_data[__mod_type_key__])
99 target_file_name = os.path.abspath(dcps_folder +
"/" + dcp_file_name)
100 meta_file_name =
"3_top{}_STATIC.json".format(cFp_data[__mod_type_key__])
101 target_meta_name = os.path.abspath(dcps_folder +
"/" + meta_file_name)
103 if not os.path.isfile(target_file_name)
or not os.path.isfile(target_meta_name):
104 print(
"[cFBuild] WARNING: {} or {} does not exist, so no signature can be created. Stop.".format(dcp_file_name, meta_file_name))
108 new_bin_file_path = os.path.abspath(dcps_folder +
'/' + new_bin_file_name)
109 if not os.path.isfile(new_bin_file_path):
110 print(
"[cFBuild] ERROR: {} is not a file. STOP.".format(new_bin_file_path))
112 ignore_pr_verify =
False
113 if pr_verify_rpt_file_name == __ignore_key__:
114 ignore_pr_verify =
True
115 if not ignore_pr_verify:
116 pr_verify_rpt_file_path = os.path.abspath(dcps_folder +
'/' + pr_verify_rpt_file_name)
117 if not os.path.isfile(pr_verify_rpt_file_path):
118 print(
"[cFBuild] ERROR: {} is not a file. STOP.".format(pr_verify_rpt_file_path))
121 with open(pr_verify_rpt_file_path)
as rpt_in:
123 rpt_file_lines.append(line.rstrip())
124 pr_verify_str =
''.join(rpt_file_lines)
126 sig_file_path = os.path.abspath(new_bin_file_path +
'.' + __sig_file_ending__)
127 new_sig = {
'build_id': __THIS_FILE_VERSION_NUMBER__,
'algorithm': __THIS_FILE_ALGORITHM_VERSION,
128 'file': new_bin_file_name}
130 with open(target_meta_name,
'r')
as meta_file:
131 cur_meta = json.load(meta_file)
132 current_cl_cert = cur_meta[
'cert']
133 pl_id = cur_meta[
'id']
135 if 'pl_id' in cur_meta:
136 pl_id = cur_meta[
'pl_id']
137 new_sig[
'pl_id'] = pl_id
143 if not ignore_pr_verify:
147 rpt_hash = __ignore_hash__
149 if debugging_flow
is not None:
150 print(
"\tdcp hash: {}".format(dcp_hash))
151 print(
"\trpt hash: {}".format(rpt_hash))
152 print(
"\tmy hash: {}".format(my_hash))
153 print(
"\tsig_file_path: {}".format(sig_file_path))
155 new_sig[
'sig'] =
get_sig_string(dcp_hash, my_hash, current_cl_cert, new_pr_hash, rpt_hash,
156 debugging_flow=debugging_flow)
157 new_sig[
'hash'] = new_pr_hash
159 if not ignore_pr_verify:
160 rpt_sum_line = rpt_file_lines[-1]
161 new_sig[
'verify_rpt'] = rpt_sum_line
162 if dcp_file_name
in rpt_sum_line:
163 new_sig[
'verify'] =
'OK'
165 new_sig[
'verify'] =
'NOK'
167 new_sig[
'verify_rpt'] = __ignore_key__
168 new_sig[
'verify'] =
'OK'
170 with open(sig_file_path,
'w')
as outfile:
171 json.dump(new_sig, outfile)
174 if not ignore_pr_verify:
175 verify_report_name = os.path.abspath(dcps_folder +
'/5_' + new_bin_file_name[2:-4] +
'.' + __rpt_file_ending__)
176 os.system(
"cp -f {} {}".format(pr_verify_rpt_file_path, verify_report_name))
181 if __name__ ==
'__main__':
183 if len(sys.argv) != 3:
184 print(
'ERROR: Usage is {} <new-bin-file-name> <pr-verify-rpt-file-name>. STOP'.format(sys.argv[0]))
186 main(sys.argv[1], sys.argv[2])
def get_string_hash(inp_string)
def get_file_hash(file_path)
def main(new_bin_file_name, pr_verify_rpt_file_name)
def get_sig_string(dcp_hash, my_hash, current_cl_cert, new_pr_hash, rpt_hash, debugging_flow=None)