Skip to content

Commit 4e2f166

Browse files
committed
Address code review comments
- Close outFile on early return in clu_decrypt.c to fix handle leak - Fix misleading "Input file does not exist" error message in decrypt - Add #undef LARGE_TEMP_SZ to prevent macro namespace pollution - Require path separator after drive letter in Windows absolute path check - Use dynamic port allocation in OCSP and OCSP-SCGI tests to avoid port conflicts
1 parent cb921a2 commit 4e2f166

4 files changed

Lines changed: 27 additions & 14 deletions

File tree

src/crypto/clu_decrypt.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ int wolfCLU_decrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
8181
if (length < saltAndIvSize) {
8282
wolfCLU_LogError("Input file too small (missing salt/IV).");
8383
XFCLOSE(inFile);
84+
XFCLOSE(outFile);
8485
return DECRYPT_ERROR;
8586
}
8687

src/x509/clu_x509_sign.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ int wolfCLU_GenChimeraCertSign(WOLFSSL_BIO *bioCaKey, WOLFSSL_BIO *bioAltCaKey,
288288
int caCertSz = LARGE_TEMP_SZ;
289289
byte serverKeyBuf[LARGE_TEMP_SZ];
290290
int serverKeySz = LARGE_TEMP_SZ;
291+
#undef LARGE_TEMP_SZ
291292

292293
if (bioCaKey == NULL || bioAltCaKey == NULL || bioAltSubjPubKey == NULL
293294
|| subject == NULL || outFileName == NULL) {
@@ -1049,7 +1050,9 @@ int wolfCLU_CertSignAppendOut(WOLFCLU_CERT_SIGN* csign, char* out)
10491050
* Matches OpenSSL's ossl_is_absolute_path() behaviour. */
10501051
if (out[0] == '/'
10511052
#ifdef _WIN32
1052-
|| out[0] == '\\' || (out[0] != '\0' && out[1] == ':')
1053+
|| out[0] == '\\'
1054+
|| (isalpha((unsigned char)out[0]) && out[1] == ':'
1055+
&& (out[2] == '\\' || out[2] == '/'))
10531056
#endif
10541057
) {
10551058
s = (char*)XMALLOC(outSz + 1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);

tests/ocsp-scgi/ocsp-scgi-test.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525

2626
HAS_OPENSSL = shutil.which("openssl") is not None
2727

28-
SCGI_PORT = 6961
29-
HTTP_PORT = 8089
28+
def _find_free_port():
29+
"""Bind to port 0 to let the OS assign a free ephemeral port."""
30+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
31+
s.bind(("127.0.0.1", 0))
32+
return s.getsockname()[1]
3033

3134
INDEX_VALID = (
3235
"V\t991231235959Z\t\t01\tunknown\t"
@@ -74,7 +77,7 @@ class _SCGIProxyHandler(http.server.BaseHTTPRequestHandler):
7477
"""HTTP handler that proxies POST requests to an SCGI backend."""
7578

7679
scgi_host = "127.0.0.1"
77-
scgi_port = SCGI_PORT
80+
scgi_port = None
7881

7982
def do_POST(self):
8083
length = int(self.headers.get("Content-Length", 0))
@@ -138,7 +141,9 @@ def setUpClass(cls):
138141
cls._tmpdir = tempfile.mkdtemp()
139142
cls._wolfclu_proc = None
140143
cls._wolfclu_log = None
141-
cls._proxy = _HTTPProxy(HTTP_PORT, SCGI_PORT)
144+
cls._scgi_port = _find_free_port()
145+
cls._http_port = _find_free_port()
146+
cls._proxy = _HTTPProxy(cls._http_port, cls._scgi_port)
142147
cls._proxy.start()
143148

144149
@classmethod
@@ -181,7 +186,7 @@ def _start_responder(self, index_content,
181186
log_file = open(log_path, "w")
182187
proc = subprocess.Popen(
183188
[WOLFSSL_BIN, "ocsp", "-scgi",
184-
"-port", str(SCGI_PORT),
189+
"-port", str(self._scgi_port),
185190
"-index", index,
186191
"-rsigner", rsigner,
187192
"-rkey", rkey,
@@ -205,7 +210,7 @@ def _ocsp_query(self):
205210
"-issuer", os.path.join(CERTS_DIR, "ca-cert.pem"),
206211
"-cert", os.path.join(CERTS_DIR, "server-cert.pem"),
207212
"-CAfile", os.path.join(CERTS_DIR, "ca-cert.pem"),
208-
"-url", f"http://127.0.0.1:{HTTP_PORT}/ocsp"],
213+
"-url", f"http://127.0.0.1:{self._http_port}/ocsp"],
209214
capture_output=True, text=True,
210215
stdin=subprocess.DEVNULL, timeout=30,
211216
)

tests/ocsp/ocsp-test.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import os
99
import re
1010
import shutil
11+
import socket
1112
import subprocess
1213
import sys
1314
import tempfile
@@ -18,7 +19,13 @@
1819
from wolfclu_test import WOLFSSL_BIN, CERTS_DIR, test_main
1920

2021
HAS_OPENSSL = shutil.which("openssl") is not None
21-
OCSP_PORT_BASE = 6960
22+
23+
24+
def _find_free_port():
25+
"""Bind to port 0 to let the OS assign a free ephemeral port."""
26+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
27+
s.bind(("127.0.0.1", 0))
28+
return s.getsockname()[1]
2229

2330
INDEX_VALID = (
2431
"V\t991231235959Z\t\t01\tunknown\t"
@@ -116,7 +123,7 @@ class _OCSPInteropBase(unittest.TestCase):
116123

117124
CLIENT_BIN = None
118125
RESPONDER_BIN = None
119-
PORT = OCSP_PORT_BASE
126+
PORT = None
120127

121128
@classmethod
122129
def setUpClass(cls):
@@ -128,6 +135,7 @@ def setUpClass(cls):
128135
raise unittest.SkipTest(
129136
f"OCSP not supported by {cls.RESPONDER_BIN}")
130137

138+
cls.PORT = _find_free_port()
131139
cls._tmpdir = tempfile.mkdtemp()
132140
cls._responder = None
133141

@@ -307,33 +315,29 @@ def test_12_graceful_shutdown(self):
307315

308316

309317
# Concrete test classes for each client/responder combination.
310-
# Each gets a unique port to avoid conflicts if run in parallel.
318+
# Each gets a dynamically assigned port in setUpClass to avoid conflicts.
311319

312320
class TestWolfsslClientWolfsslResponder(_OCSPInteropBase):
313321
CLIENT_BIN = WOLFSSL_BIN
314322
RESPONDER_BIN = WOLFSSL_BIN
315-
PORT = OCSP_PORT_BASE
316323

317324

318325
@unittest.skipUnless(HAS_OPENSSL, "openssl not available")
319326
class TestWolfsslClientOpensslResponder(_OCSPInteropBase):
320327
CLIENT_BIN = WOLFSSL_BIN
321328
RESPONDER_BIN = "openssl"
322-
PORT = OCSP_PORT_BASE + 1
323329

324330

325331
@unittest.skipUnless(HAS_OPENSSL, "openssl not available")
326332
class TestOpensslClientWolfsslResponder(_OCSPInteropBase):
327333
CLIENT_BIN = "openssl"
328334
RESPONDER_BIN = WOLFSSL_BIN
329-
PORT = OCSP_PORT_BASE + 2
330335

331336

332337
@unittest.skipUnless(HAS_OPENSSL, "openssl not available")
333338
class TestOpensslClientOpensslResponder(_OCSPInteropBase):
334339
CLIENT_BIN = "openssl"
335340
RESPONDER_BIN = "openssl"
336-
PORT = OCSP_PORT_BASE + 3
337341

338342

339343
def load_tests(loader, tests, pattern):

0 commit comments

Comments
 (0)