@@ -113,19 +113,23 @@ public func INT_mpi_exp_mod_4096_vs_python(env : &mut TestEnv) {
113113
114114@ test
115115public func INT_rsa4096_cert_signature_vs_python (env : &mut TestEnv) {
116+ var cert_path = test_tmp_file (string_view ("chem_rsa4096_cert.der" ))
116117 var script : [2048 ]u8; var sp : size_t = 0 ; var si : size_t = 0
117118 var hdr = "import datetime\n from cryptography import x509\n from cryptography.x509.oid import NameOID\n from cryptography.hazmat.primitives import hashes\n from cryptography.hazmat.primitives.asymmetric import rsa\n from cryptography.hazmat.primitives.serialization import Encoding\n " as *char; si=0
118119 while (hdr[si]!= 0 ){script[sp]=hdr[si] as u8; sp+= 1 ; si+= 1 }
119120 var l = "key=rsa.generate_private_key(65537,4096)\n pub=key.public_key()\n " as *char; si=0
120121 while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
121122 l = "now=datetime.datetime.utcnow()\n cert=(x509.CertificateBuilder().subject_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME,'test.example.com')])).issuer_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME,'Test4096CA')])).public_key(pub).serial_number(1).not_valid_before(now-datetime.timedelta(days=1)).not_valid_after(now+datetime.timedelta(days=365)).sign(key,hashes.SHA256()))\n " as *char; si=0
122123 while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
123- l = "der=cert.public_bytes(Encoding.DER)\n f=open('/tmp/chem_rsa4096_cert.der','wb');f.write(der);f.close()\n print('LEN='+str(len(der)))\n " as *char; si=0
124+ l = "der=cert.public_bytes(Encoding.DER)\n f=open('" as *char; si=0
125+ while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
126+ test_script_append_view (&raw mut script[0 ], &raw mut sp, cert_path.to_view ())
127+ l = "','wb');f.write(der);f.close()\n print('LEN='+str(len(der)))\n " as *char; si=0
124128 while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
125129
126130 test_python_run_script (&raw script[0 ], sp, string_view ("cert_rsa4096.py" ))
127131
128- var cert_file = fopen ("/tmp/chem_rsa4096_cert.der \0 " as *char, "rb\0 " as *char)
132+ var cert_file = fopen (cert_path. data () as *char, "rb\0 " as *char)
129133 if (cert_file == null) { env.error ("cannot open cert" ); return }
130134 var cert_buf : [2048 ]u8
131135 var cert_len = fread (&raw mut cert_buf[0 ] as *mut void, 1 as size_t, 2048 , cert_file)
@@ -152,19 +156,23 @@ public func INT_rsa4096_cert_signature_vs_python(env : &mut TestEnv) {
152156
153157@ test
154158public func INT_rsa2048_cert_signature_still_verifies (env : &mut TestEnv) {
159+ var cert_path = test_tmp_file (string_view ("chem_rsa2048_cert.der" ))
155160 var script : [2048 ]u8; var sp : size_t = 0 ; var si : size_t = 0
156161 var hdr = "import datetime\n from cryptography import x509\n from cryptography.x509.oid import NameOID\n from cryptography.hazmat.primitives import hashes\n from cryptography.hazmat.primitives.asymmetric import rsa\n from cryptography.hazmat.primitives.serialization import Encoding\n " as *char; si=0
157162 while (hdr[si]!= 0 ){script[sp]=hdr[si] as u8; sp+= 1 ; si+= 1 }
158163 var l = "key=rsa.generate_private_key(65537,2048)\n pub=key.public_key()\n " as *char; si=0
159164 while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
160165 l = "now=datetime.datetime.utcnow()\n cert=(x509.CertificateBuilder().subject_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME,'test.example.com')])).issuer_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME,'Test2048CA')])).public_key(pub).serial_number(1).not_valid_before(now-datetime.timedelta(days=1)).not_valid_after(now+datetime.timedelta(days=365)).sign(key,hashes.SHA256()))\n " as *char; si=0
161166 while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
162- l = "der=cert.public_bytes(Encoding.DER)\n f=open('/tmp/chem_rsa2048_cert.der','wb');f.write(der);f.close()\n " as *char; si=0
167+ l = "der=cert.public_bytes(Encoding.DER)\n f=open('" as *char; si=0
168+ while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
169+ test_script_append_view (&raw mut script[0 ], &raw mut sp, cert_path.to_view ())
170+ l = "','wb');f.write(der);f.close()\n " as *char; si=0
163171 while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
164172
165173 test_python_run_script (&raw script[0 ], sp, string_view ("cert_rsa2048.py" ))
166174
167- var cert_file = fopen ("/tmp/chem_rsa2048_cert.der \0 " as *char, "rb\0 " as *char)
175+ var cert_file = fopen (cert_path. data () as *char, "rb\0 " as *char)
168176 if (cert_file == null) { env.error ("cannot open cert" ); return }
169177 var cert_buf : [2048 ]u8
170178 var cert_len = fread (&raw mut cert_buf[0 ] as *mut void, 1 as size_t, 2048 , cert_file)
@@ -188,7 +196,10 @@ public func INT_rsa2048_cert_signature_still_verifies(env : &mut TestEnv) {
188196// at 4b) AND a 4096-bit RSA signature on the root→intermediate link.
189197
190198func test_write_chain_python () {
191- var script : [4096 ]u8; var sp : size_t = 0 ; var si : size_t = 0
199+ var root_path = test_tmp_file (string_view ("chem_chain_root.der" ))
200+ var inter_path = test_tmp_file (string_view ("chem_chain_inter.der" ))
201+ var leaf_path = test_tmp_file (string_view ("chem_chain_leaf.der" ))
202+ var script : [8192 ]u8; var sp : size_t = 0 ; var si : size_t = 0
192203 var hdr = "import datetime\n from cryptography import x509\n from cryptography.x509.oid import NameOID\n from cryptography.hazmat.primitives import hashes\n from cryptography.hazmat.primitives.asymmetric import rsa,ec\n from cryptography.hazmat.primitives.serialization import Encoding\n " as *char; si=0
193204 while (hdr[si]!= 0 ){script[sp]=hdr[si] as u8; sp+= 1 ; si+= 1 }
194205 var l = "def nm(cn):return x509.Name([x509.NameAttribute(NameOID.COMMON_NAME,cn)])\n " as *char; si=0
@@ -205,11 +216,20 @@ func test_write_chain_python() {
205216 while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
206217 l = "lk=ec.generate_private_key(ec.SECP256R1())\n leaf=san(x509.CertificateBuilder().subject_name(nm('test.example.com')).issuer_name(nm('InterCA')).public_key(lk.public_key()).serial_number(3).not_valid_before(na).not_valid_after(nb)).sign(ik,hashes.SHA256())\n " as *char; si=0
207218 while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
208- l = "open('/tmp/chem_chain_root.der','wb').write(root.public_bytes(Encoding.DER))\n " as *char; si=0
219+ l = "open('" as *char; si=0
220+ while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
221+ test_script_append_view (&raw mut script[0 ], &raw mut sp, root_path.to_view ())
222+ l = "','wb').write(root.public_bytes(Encoding.DER))\n " as *char; si=0
209223 while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
210- l = "open('/tmp/chem_chain_inter.der','wb').write(inter.public_bytes(Encoding.DER)) \n " as *char; si=0
224+ l = "open('" as *char; si=0
211225 while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
212- l = "open('/tmp/chem_chain_leaf.der','wb').write(leaf.public_bytes(Encoding.DER))\n " as *char; si=0
226+ test_script_append_view (&raw mut script[0 ], &raw mut sp, inter_path.to_view ())
227+ l = "','wb').write(inter.public_bytes(Encoding.DER))\n " as *char; si=0
228+ while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
229+ l = "open('" as *char; si=0
230+ while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
231+ test_script_append_view (&raw mut script[0 ], &raw mut sp, leaf_path.to_view ())
232+ l = "','wb').write(leaf.public_bytes(Encoding.DER))\n " as *char; si=0
213233 while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
214234 l = "print('OK')\n " as *char; si=0
215235 while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
@@ -229,9 +249,12 @@ func test_read_der_file(path : *char, buf : *mut u8, max_len : size_t) : size_t
229249public func INT_x509_chain_leaf_intermediate_root_vs_python (env : &mut TestEnv) {
230250 test_write_chain_python ()
231251
232- var root_buf : [2048 ]u8; var root_len = test_read_der_file ("/tmp/chem_chain_root.der\0 " as *char, &raw mut root_buf[0 ], 2048 )
233- var inter_buf : [2048 ]u8; var inter_len = test_read_der_file ("/tmp/chem_chain_inter.der\0 " as *char, &raw mut inter_buf[0 ], 2048 )
234- var leaf_buf : [2048 ]u8; var leaf_len = test_read_der_file ("/tmp/chem_chain_leaf.der\0 " as *char, &raw mut leaf_buf[0 ], 2048 )
252+ var root_path = test_tmp_file (string_view ("chem_chain_root.der" ))
253+ var inter_path = test_tmp_file (string_view ("chem_chain_inter.der" ))
254+ var leaf_path = test_tmp_file (string_view ("chem_chain_leaf.der" ))
255+ var root_buf : [2048 ]u8; var root_len = test_read_der_file (root_path.data () as *char, &raw mut root_buf[0 ], 2048 )
256+ var inter_buf : [2048 ]u8; var inter_len = test_read_der_file (inter_path.data () as *char, &raw mut inter_buf[0 ], 2048 )
257+ var leaf_buf : [2048 ]u8; var leaf_len = test_read_der_file (leaf_path.data () as *char, &raw mut leaf_buf[0 ], 2048 )
235258 if (root_len == 0 || inter_len == 0 || leaf_len == 0 ) { env.error ("failed to read chain DER files" ); return }
236259
237260 var root : X509Cert; x509_cert_init (&raw mut root)
@@ -263,24 +286,31 @@ public func INT_x509_chain_leaf_intermediate_root_vs_python(env : &mut TestEnv)
263286public func INT_x509_chain_wrong_root_fails_vs_python (env : &mut TestEnv) {
264287 test_write_chain_python ()
265288
266- var root_buf : [2048 ]u8; var root_len = test_read_der_file ("/tmp/chem_chain_root.der\0 " as *char, &raw mut root_buf[0 ], 2048 )
267- var inter_buf : [2048 ]u8; var inter_len = test_read_der_file ("/tmp/chem_chain_inter.der\0 " as *char, &raw mut inter_buf[0 ], 2048 )
268- var leaf_buf : [2048 ]u8; var leaf_len = test_read_der_file ("/tmp/chem_chain_leaf.der\0 " as *char, &raw mut leaf_buf[0 ], 2048 )
289+ var root_path = test_tmp_file (string_view ("chem_chain_root.der" ))
290+ var inter_path = test_tmp_file (string_view ("chem_chain_inter.der" ))
291+ var leaf_path = test_tmp_file (string_view ("chem_chain_leaf.der" ))
292+ var wrong_path = test_tmp_file (string_view ("chem_chain_wrong.der" ))
293+ var root_buf : [2048 ]u8; var root_len = test_read_der_file (root_path.data () as *char, &raw mut root_buf[0 ], 2048 )
294+ var inter_buf : [2048 ]u8; var inter_len = test_read_der_file (inter_path.data () as *char, &raw mut inter_buf[0 ], 2048 )
295+ var leaf_buf : [2048 ]u8; var leaf_len = test_read_der_file (leaf_path.data () as *char, &raw mut leaf_buf[0 ], 2048 )
269296 if (root_len == 0 || inter_len == 0 || leaf_len == 0 ) { env.error ("failed to read chain DER files" ); return }
270297
271298 // Generate an UNRELATED self-signed root.
272- var script : [2048 ]u8; var sp : size_t = 0 ; var si : size_t = 0
299+ var script : [8192 ]u8; var sp : size_t = 0 ; var si : size_t = 0
273300 var hdr = "import datetime\n from cryptography import x509\n from cryptography.x509.oid import NameOID\n from cryptography.hazmat.primitives import hashes\n from cryptography.hazmat.primitives.asymmetric import rsa\n from cryptography.hazmat.primitives.serialization import Encoding\n " as *char; si=0
274301 while (hdr[si]!= 0 ){script[sp]=hdr[si] as u8; sp+= 1 ; si+= 1 }
275302 var l = "import datetime\n key=rsa.generate_private_key(65537,2048)\n na=datetime.datetime.utcnow()-datetime.timedelta(days=1)\n nb=datetime.datetime.utcnow()+datetime.timedelta(days=365)\n " as *char; si=0
276303 while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
277304 l = "cert=(x509.CertificateBuilder().subject_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME,'WrongCA')])).issuer_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME,'WrongCA')])).public_key(key.public_key()).serial_number(9).not_valid_before(na).not_valid_after(nb).add_extension(x509.BasicConstraints(ca=True,path_length=None),critical=True).sign(key,hashes.SHA256()))\n " as *char; si=0
278305 while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
279- l = "open('/tmp/chem_chain_wrong.der','wb').write(cert.public_bytes(Encoding.DER))\n print('OK')\n " as *char; si=0
306+ l = "open('" as *char; si=0
307+ while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
308+ test_script_append_view (&raw mut script[0 ], &raw mut sp, wrong_path.to_view ())
309+ l = "','wb').write(cert.public_bytes(Encoding.DER))\n print('OK')\n " as *char; si=0
280310 while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
281311 test_python_run_script (&raw script[0 ], sp, string_view ("wrong_root.py" ))
282312
283- var wrong_buf : [2048 ]u8; var wrong_len = test_read_der_file ("/tmp/chem_chain_wrong.der \0 " as *char, &raw mut wrong_buf[0 ], 2048 )
313+ var wrong_buf : [2048 ]u8; var wrong_len = test_read_der_file (wrong_path. data () as *char, &raw mut wrong_buf[0 ], 2048 )
284314 if (wrong_len == 0 ) { env.error ("failed to read wrong root" ); return }
285315
286316 var root : X509Cert; x509_cert_init (&raw mut root)
@@ -310,8 +340,10 @@ public func INT_x509_chain_wrong_root_fails_vs_python(env : &mut TestEnv) {
310340public func INT_x509_chain_missing_intermediate_fails_vs_python (env : &mut TestEnv) {
311341 test_write_chain_python ()
312342
313- var root_buf : [2048 ]u8; var root_len = test_read_der_file ("/tmp/chem_chain_root.der\0 " as *char, &raw mut root_buf[0 ], 2048 )
314- var leaf_buf : [2048 ]u8; var leaf_len = test_read_der_file ("/tmp/chem_chain_leaf.der\0 " as *char, &raw mut leaf_buf[0 ], 2048 )
343+ var root_path = test_tmp_file (string_view ("chem_chain_root.der" ))
344+ var leaf_path = test_tmp_file (string_view ("chem_chain_leaf.der" ))
345+ var root_buf : [2048 ]u8; var root_len = test_read_der_file (root_path.data () as *char, &raw mut root_buf[0 ], 2048 )
346+ var leaf_buf : [2048 ]u8; var leaf_len = test_read_der_file (leaf_path.data () as *char, &raw mut leaf_buf[0 ], 2048 )
315347 if (root_len == 0 || leaf_len == 0 ) { env.error ("failed to read chain DER files" ); return }
316348
317349 var root : X509Cert; x509_cert_init (&raw mut root)
@@ -336,24 +368,40 @@ public func INT_x509_chain_missing_intermediate_fails_vs_python(env : &mut TestE
336368@ test
337369public func INT_x509_pem_bundle_multicert_vs_python (env : &mut TestEnv) {
338370 test_write_chain_python ()
371+ var root_path = test_tmp_file (string_view ("chem_chain_root.der" ))
372+ var inter_path = test_tmp_file (string_view ("chem_chain_inter.der" ))
373+ var leaf_path = test_tmp_file (string_view ("chem_chain_leaf.der" ))
374+ var bundle_path = test_tmp_file (string_view ("chem_chain_bundle.pem" ))
339375
340376 // Bundle all three PEMs into a single file.
341- var script : [4096 ]u8; var sp : size_t = 0 ; var si : size_t = 0
377+ var script : [8192 ]u8; var sp : size_t = 0 ; var si : size_t = 0
342378 var hdr = "from cryptography import x509\n from cryptography.x509 import load_pem_x509_certificate\n from cryptography.hazmat.primitives.serialization import Encoding\n " as *char; si=0
343379 while (hdr[si]!= 0 ){script[sp]=hdr[si] as u8; sp+= 1 ; si+= 1 }
344- var l = "root=x509.load_der_x509_certificate(open('/tmp/chem_chain_root.der','rb').read())\n " as *char; si=0
380+ var l = "root=x509.load_der_x509_certificate(open('" as *char; si=0
381+ while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
382+ test_script_append_view (&raw mut script[0 ], &raw mut sp, root_path.to_view ())
383+ l = "','rb').read())\n " as *char; si=0
384+ while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
385+ l = "inter=x509.load_der_x509_certificate(open('" as *char; si=0
345386 while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
346- l = "inter=x509.load_der_x509_certificate(open('/tmp/chem_chain_inter.der','rb').read())\n " as *char; si=0
387+ test_script_append_view (&raw mut script[0 ], &raw mut sp, inter_path.to_view ())
388+ l = "','rb').read())\n " as *char; si=0
347389 while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
348- l = "leaf=x509.load_der_x509_certificate(open('/tmp/chem_chain_leaf.der','rb').read())\n " as *char; si=0
390+ l = "leaf=x509.load_der_x509_certificate(open('" as *char; si=0
391+ while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
392+ test_script_append_view (&raw mut script[0 ], &raw mut sp, leaf_path.to_view ())
393+ l = "','rb').read())\n " as *char; si=0
349394 while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
350395 l = "bundle=root.public_bytes(Encoding.PEM)+inter.public_bytes(Encoding.PEM)+leaf.public_bytes(Encoding.PEM)\n " as *char; si=0
351396 while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
352- l = "open('/tmp/chem_chain_bundle.pem','wb').write(bundle)\n print('OK')\n " as *char; si=0
397+ l = "open('" as *char; si=0
398+ while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
399+ test_script_append_view (&raw mut script[0 ], &raw mut sp, bundle_path.to_view ())
400+ l = "','wb').write(bundle)\n print('OK')\n " as *char; si=0
353401 while (l[si]!= 0 ){script[sp]=l[si] as u8; sp+= 1 ; si+= 1 }
354402 test_python_run_script (&raw script[0 ], sp, string_view ("bundle_gen.py" ))
355403
356- var chain = x509_crt_load_pem_file ("/tmp/chem_chain_bundle.pem \0 " as *char)
404+ var chain = x509_crt_load_pem_file (bundle_path. data () as *char)
357405 if (chain == null) { env.error ("failed to load multi-cert PEM bundle" ); return }
358406
359407 // Walk the parsed chain: expect exactly 3 certs, root first.
0 commit comments