blob: 214703f30d957dc05f2fb8bf280f656c6f03b94f [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001import sys
2import os
3import struct
4script_folder, script_name = os.path.split(os.path.realpath(__file__))
5sys.path.append(os.path.join(script_folder, "lib"))
6import gfh
7import cert
8
9def get_file_sizeb(file_path):
10 if not os.path.isfile(file_path):
11 return 0
12 file_handle = open(file_path, "rb")
13 file_handle.seek(0, 2)
14 file_size = file_handle.tell()
15 file_handle.close()
16 return file_size
17
18def concatb(file1_path, file2_path):
19 file1_size = get_file_sizeb(file1_path)
20 file2_size = get_file_sizeb(file2_path)
21 file1 = open(file1_path, "ab+")
22 file2 = open(file2_path, "rb")
23 file1.write(file2.read(file2_size))
24 file2.close()
25 file1.close()
26
27class sctrl_cert:
28 def __init__(self, out_path, sctrl_cert_path):
29 self.m_out_path = out_path
30 if not os.path.exists(self.m_out_path):
31 os.makedirs(self.m_out_path)
32 self.m_sctrl_cert_path = sctrl_cert_path
33 self.m_gfh = gfh.image_gfh()
34 self.m_key_path = ""
35 self.m_out_path = out_path
36 self.m_sig_handler = None
37 def create_gfh(self, gfh_config):
38 self.m_gfh.load_ini(gfh_config)
39 self.m_gfh.dump()
40 return
41 def sign(self, key_ini_path, key_cert_path, primary_dbg_config_ini_path, primary_dbg_path, secondary_config_file_path):
42 #tool auth contains only gfh and signature, no extra content
43 self.m_gfh.finalize(0, key_ini_path)
44 #create tbs_sctrl_cert.bin
45 tbs_sctrl_cert_file_path = os.path.join(self.m_out_path, "tbs_sctrl_cert.bin")
46 tbs_sctrl_cert_file = open(tbs_sctrl_cert_file_path, "wb")
47 tbs_sctrl_cert_file.write(self.m_gfh.pack())
48 tbs_sctrl_cert_file.close()
49 print "===sctrl_cert sign==="
50 if self.m_gfh.get_sig_type() == "CERT_CHAIN":
51 self.m_sig_handler = cert.cert_chain_v5()
52 #create key cert
53 if key_cert_path == "":
54 key_cert_path = os.path.join(self.m_out_path, "key_cert.bin")
55 if os.path.isfile(key_ini_path):
56 key_cert_folder_name, key_cert_file_name = os.path.split(os.path.abspath(key_cert_path))
57 self.m_sig_handler.create_key_cert(key_ini_path, self.m_out_path, key_cert_file_name)
58 key_cert_path = os.path.join(self.m_out_path, key_cert_file_name)
59 else:
60 self.m_sig_handler.set_key_cert(key_cert_path)
61 #create primary debug cert
62 if primary_dbg_path == "":
63 primary_dbg_path = "primary_dbg_cert.bin"
64 if os.path.isfile(primary_dbg_config_ini_path):
65 primary_dbg_cert_folder_name, primary_dbg_cert_file_name = os.path.split(os.path.abspath(primary_dbg_path))
66 self.m_sig_handler.create_primary_dbg_cert(primary_dbg_config_ini_path, tbs_sctrl_cert_file_path, self.m_out_path, primary_dbg_cert_file_name)
67 primary_dbg_cert_path = os.path.join(self.m_out_path, primary_dbg_cert_file_name)
68 else:
69 self.m_sig_handler.set_primary_dbg_cert(primary_dbg_path)
70 #create secondary debug cert
71 secondary_dbg_cert_file_name = "secondary_dbg_cert.bin"
72 secondary_dbg_cert_file_path = os.path.join(self.m_out_path, secondary_dbg_cert_file_name)
73 self.m_sig_handler.create_secondary_dbg_cert(secondary_config_file_path, self.m_out_path, secondary_dbg_cert_file_name)
74 #create final cert chain
75 sig_name = "sctrl_cert.sig"
76 sig_file_path = os.path.join(self.m_out_path, sig_name)
77 self.m_sig_handler.output(self.m_out_path, sig_name)
78 #create final sctrl cert
79 if os.path.isfile(self.m_sctrl_cert_path):
80 os.remove(self.m_sctrl_cert_path)
81 concatb(self.m_sctrl_cert_path, tbs_sctrl_cert_file_path)
82 concatb(self.m_sctrl_cert_path, sig_file_path)
83 os.remove(secondary_dbg_cert_file_path)
84 elif self.m_gfh.get_sig_type() == "SINGLE":
85 self.m_sig_handler = cert.sig_single(self.m_gfh.get_pad_type())
86 self.m_sig_handler.set_out_path(self.m_out_path)
87 self.m_sig_handler.create(key_ini_path, tbs_sctrl_cert_file_path)
88 self.m_sig_handler.sign()
89 sig_name = "sctrl_cert.sig"
90 sig_file_path = os.path.join(self.m_out_path, sig_name)
91 self.m_sig_handler.output(self.m_out_path, sig_name)
92 #create final toolauth file
93 if os.path.isfile(self.m_sctrl_cert_path):
94 os.remove(self.m_sctrl_cert_path)
95 concatb(self.m_sctrl_cert_path, tbs_sctrl_cert_file_path)
96 concatb(self.m_sctrl_cert_path, sig_file_path)
97 else:
98 print "unknown signature type"
99
100 #clean up
101 os.remove(tbs_sctrl_cert_file_path)
102 os.remove(sig_file_path)
103 return
104
105def main():
106 #parameter parsing
107 idx = 1
108 key_ini_path = ""
109 key_cert_path = ""
110 gfh_config_ini_path = ""
111 primary_dbg_path = ""
112 primary_dbg_config_ini_path = ""
113 secondary_dbg_config_ini_path = ""
114 sctrl_cert_path = ""
115
116 while idx < len(sys.argv):
117 if sys.argv[idx][0] == '-':
118 if sys.argv[idx][1] == 'i':
119 print "key: " + sys.argv[idx + 1]
120 key_ini_path = sys.argv[idx + 1]
121 idx += 2
122 elif sys.argv[idx][1] == 'g':
123 print "gfh config: " + sys.argv[idx + 1]
124 gfh_config_ini_path = sys.argv[idx + 1]
125 idx += 2
126 elif sys.argv[idx][1] == 'p':
127 print "primary dbg cert: " + sys.argv[idx + 1]
128 primary_dbg_path = sys.argv[idx + 1]
129 idx += 2
130 elif sys.argv[idx][1] == 'q':
131 print "primary dbg cert config: " + sys.argv[idx + 1]
132 primary_dbg_config_ini_path = sys.argv[idx + 1]
133 idx += 2
134 elif sys.argv[idx][1] == 's':
135 print "secondary dbg cert config: " + sys.argv[idx + 1]
136 secondary_dbg_config_ini_path = sys.argv[idx + 1]
137 idx += 2
138 elif sys.argv[idx][1] == 'k':
139 print "key cert: " + sys.argv[idx + 1]
140 key_cert_path = sys.argv[idx + 1]
141 idx += 2
142 else:
143 print "unknown input"
144 idx += 2
145 else:
146 sctrl_cert_path = sys.argv[idx]
147 print "sctrl_cert_path: " + sctrl_cert_path
148 idx += 1
149
150 if not key_cert_path and not key_ini_path:
151 print "key path is not given!"
152 return -1
153 if not gfh_config_ini_path:
154 print "sctrl_cert_config_path is not given!"
155 return -1
156 if not sctrl_cert_path:
157 print "sctrl_cert is not given!"
158 return -1
159
160 out_path = os.path.dirname(os.path.abspath(sctrl_cert_path))
161
162 sctrl_cert_obj = sctrl_cert(out_path, sctrl_cert_path)
163 sctrl_cert_obj.create_gfh(gfh_config_ini_path)
164 sctrl_cert_obj.sign(key_ini_path, key_cert_path, primary_dbg_config_ini_path, primary_dbg_path, secondary_dbg_config_ini_path)
165
166 return 0
167
168if __name__ == '__main__':
169 main()
170