Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
153 commits
Select commit Hold shift + click to select a range
d6d1414
add test file
alii May 7, 2025
35152da
errors
alii May 7, 2025
87c520d
Specify all tls versions according to Node
alii May 7, 2025
addc357
basic wireup of tls min/max
alii May 7, 2025
37a55d2
more zig
alii May 7, 2025
b6ea92f
more
alii May 7, 2025
9059426
add the other minmax test
alii May 8, 2025
0332642
more coverage, and slightly related ProcessEnv should not extend Impo…
alii May 8, 2025
4f1233e
fix test impl
alii May 8, 2025
7b0e470
changes
alii May 8, 2025
563b8f5
consume values
alii May 8, 2025
14ea951
assign tls versions
alii May 8, 2025
aae850e
`catch unreachable;` increased
alii May 8, 2025
bd51d5e
do set min tls
alii May 8, 2025
2baa668
default to 1.2 min
alii May 8, 2025
f32b6d0
stuff
alii May 8, 2025
4098051
debug
alii May 8, 2025
7c57c3c
rm debug
alii May 8, 2025
23fd3b8
change how versions get passed
alii May 8, 2025
5b767a9
pass tls versions as openssl values
alii May 8, 2025
82a019a
pass openssl values in nativeland
alii May 8, 2025
ef6b9da
nativeland changes
alii May 8, 2025
4037fe9
debug
alii May 8, 2025
5dbd814
Merge branch 'main' of github.com:oven-sh/bun into ali/tls-min-max-cl…
alii May 8, 2025
6cb5e1c
revert openssl changes for dead path
alii May 8, 2025
4c14c7e
unused constants
alii May 8, 2025
38ff15a
rm
alii May 8, 2025
8a22127
2 more test files
alii May 8, 2025
b6530ba
2 more test files
alii May 8, 2025
d91ca54
add min v1.1 test file
alii May 8, 2025
90be0a5
fix node cli args, other diff cleanup
alii May 8, 2025
d60da18
fix cli failing arg test
alii May 8, 2025
0626a0f
Fix bad words count
alii May 8, 2025
8380a8f
rm debug logs
alii May 8, 2025
5986566
add validations test
alii May 8, 2025
de05784
partially complete validation
alii May 8, 2025
da62a89
housekeeping
alii May 8, 2025
821848f
more validation
alii May 8, 2025
040b00d
rm
alii May 8, 2025
629b677
fix alpn
alii May 8, 2025
416121f
rm
alii May 8, 2025
a5a8966
proper check for checkServerIdentity
alii May 8, 2025
2091132
clearer naming for tls cli binding logic
alii May 8, 2025
3d25000
rm secureprotocol
alii May 8, 2025
2283341
Support "instance of" in ERR_INVALID_ARG_TYPE
alii May 8, 2025
164675c
use ERR_INVALID_ARG_TYPE for Buffer instance
alii May 8, 2025
0ecf930
change ERR_TLS_INVALID_PROTOCOL_VERSION builtin
alii May 8, 2025
8ef431c
Revert 2283341efdca75b3dce065d18d972c6c9e9a68c8
alii May 8, 2025
97324e2
add min-version 1.0 test file
alii May 8, 2025
b3b276b
ticket keys test?
alii May 8, 2025
187dc46
passing
alii May 8, 2025
94293cd
use ERR_TLS_INVALID_PROTOCOL_VERSION new signature
alii May 8, 2025
f728d17
Merge branch 'main' of github.com:oven-sh/bun into ali/tls-min-max-cl…
alii May 8, 2025
9e0bdfa
update sources
alii May 9, 2025
df92e99
dont organize imports for zig
alii May 9, 2025
be32a2f
propogate err
alii May 9, 2025
f88344a
Merge branch 'main' of github.com:oven-sh/bun into ali/tls-min-max-cl…
alii May 9, 2025
74e369e
debug
alii May 9, 2025
dfa4dee
fix bun-types
alii May 9, 2025
d72e605
Merge branch 'main' into ali/tls-min-max-cli-and-test
alii May 9, 2025
2798379
net changes, ERR_TLS_PROTOCOL_VERSION_CONFLICT fix message
alii May 9, 2025
7d5ae23
Merge branch 'main' of github.com:oven-sh/bun into ali/tls-min-max-cl…
alii May 9, 2025
0b9cdc0
refactor tls version resolution
alii May 9, 2025
6f398a1
`bun run prettier`
alii May 9, 2025
5bdc738
passes
alii May 10, 2025
2d04dcf
`bun run prettier`
alii May 10, 2025
f3b769f
fix tls exports
alii May 10, 2025
f7fc34d
Merge branch 'ali/tls-min-max-cli-and-test' of github.com:oven-sh/bun…
alii May 10, 2025
d3fcad8
Merge branch 'main' of github.com:oven-sh/bun into ali/tls-min-max-cl…
alii May 10, 2025
ef0c48a
`bun run prettier`
alii May 10, 2025
f8fd2c2
glob sources
alii May 10, 2025
7d8b741
Merge branch 'ali/tls-min-max-cli-and-test' of github.com:oven-sh/bun…
alii May 10, 2025
9b05d51
temporarily ignore socket hang up (lol)
alii May 10, 2025
1ecd74a
call socket hang up
alii May 10, 2025
36cc379
revert bun.zig
alii May 10, 2025
14fe8df
export tls
alii May 10, 2025
ec5bb50
revert cli.zig, apply tls changes
alii May 10, 2025
3512087
Merge branch 'main' of github.com:oven-sh/bun into ali/tls-min-max-cl…
alii May 10, 2025
2cfec42
accidental removal
alii May 10, 2025
0c72bb5
revert imports in server.zig
alii May 10, 2025
7b64f89
fix imports uws.zig
alii May 10, 2025
28a6ba9
refactor out tls validation logic
alii May 10, 2025
2a36a4a
apply tls options
alii May 12, 2025
2e6eaae
Merge branch 'main' of github.com:oven-sh/bun into ali/tls-min-max-cl…
alii May 12, 2025
72eb996
isolate test cases for now
alii May 12, 2025
a55e27a
fix: check min/max version for ssl context re-use
alii May 12, 2025
043f24a
commiting to pair with ciro
alii May 12, 2025
210d4fc
Merge branch 'main' into ali/tls-min-max-cli-and-test
alii May 12, 2025
763c017
from node
alii May 12, 2025
aea9697
revert net.ts
alii May 12, 2025
f044b44
bun happy eyeballs message
alii May 12, 2025
cb05ed0
net
alii May 12, 2025
ace91ce
match node logic
alii May 12, 2025
5eec63b
dont allow numerical assignment of min/max versions
alii May 12, 2025
5f65bf5
debugging
alii May 13, 2025
e91d022
rm
alii May 13, 2025
27dd1e8
rm
alii May 13, 2025
a257dc2
debug
alii May 13, 2025
67a26f0
Merge branch 'main' of github.com:oven-sh/bun into ali/tls-min-max-cl…
alii May 13, 2025
4b04480
changes
alii May 13, 2025
0c9208c
changes
alii May 13, 2025
2c91911
hm
alii May 13, 2025
7a52e75
rm
alii May 14, 2025
382e848
isolate test, other debug
alii May 14, 2025
c3bdf0e
fix it
alii May 14, 2025
a7e9b1e
add more tests
alii May 14, 2025
9268977
rejection test
alii May 14, 2025
f2be87e
ssl test file, new err code
alii May 14, 2025
5154101
fix errno
alii May 14, 2025
1fed307
broken
alii May 14, 2025
881a1f2
Merge branch 'main' of github.com:oven-sh/bun into ali/tls-min-max-cl…
alii May 14, 2025
ff7c049
Works up to here!
alii May 14, 2025
7d59378
thse are all passing
alii May 14, 2025
2bbac60
also passing
alii May 14, 2025
7f2b54e
for now two failings
alii May 14, 2025
5fb30c8
debugs
alii May 14, 2025
18e3443
passing!
alii May 14, 2025
99a76fb
checkpoint
alii May 15, 2025
0257a24
debug
alii May 15, 2025
4befd51
Works
alii May 15, 2025
527e59d
nice
alii May 15, 2025
10fc8ad
Merge branch 'main' into ali/tls-min-max-cli-and-test
alii May 15, 2025
b86a462
merge
alii May 15, 2025
1b19c8c
Merge branch 'ali/tls-min-max-cli-and-test' of github.com:oven-sh/bun…
alii May 15, 2025
3d94247
restart
alii May 15, 2025
f893b08
passing
alii May 15, 2025
ceca54e
failing
alii May 15, 2025
f5047e9
debug
alii May 15, 2025
447216a
peek openssl error queue for real reason(🤦‍♂️)
alii May 15, 2025
234fa60
Almost all cases passing
alii May 15, 2025
a1b374b
old/invalid protocol
alii May 15, 2025
6154c5d
define secure_protocol_method
alii May 15, 2025
670d6b5
debug
alii May 15, 2025
6ab9b1c
secure protocol property plumbing
alii May 15, 2025
8786ff8
almost there
alii May 15, 2025
4a663f3
passing bar these
alii May 15, 2025
2d57bb1
isolate failure
alii May 15, 2025
9317dc2
try
alii May 16, 2025
5e267af
2 failing
alii May 16, 2025
bdeaea6
remove redundant bloat
alii May 16, 2025
00e8701
rm
alii May 16, 2025
0acdedd
some debloat
alii May 16, 2025
447a9a6
debloat
alii May 16, 2025
29a8a45
Merge branch 'main' into ali/tls-min-max-cli-and-test
alii May 16, 2025
bf87b05
check verify error code
alii May 16, 2025
a1124d6
Merge branch 'ali/tls-min-max-cli-and-test' of github.com:oven-sh/bun…
alii May 16, 2025
4042785
debug
alii May 16, 2025
3afa13b
fix: properly map non min/max vers related error codes
alii May 16, 2025
372b0c3
remove logs
alii May 16, 2025
38686f4
define consts earlier
alii May 17, 2025
53dd1f9
Merge branch 'main' of github.com:oven-sh/bun into ali/tls-min-max-cl…
alii May 19, 2025
2f635c4
Merge branch 'main' of github.com:oven-sh/bun into ali/tls-min-max-cl…
alii May 22, 2025
7749bf7
glob srcs
alii May 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,7 @@ codegen-for-zig-team.tar.gz
*.sock
scratch*.{js,ts,tsx,cjs,mjs}

*.bun-build
*.bun-build
**/.claude/settings.local.json

/.tmp
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"zig.zls.path": "${workspaceFolder}/vendor/zig/zls.exe",
"zig.formattingProvider": "zls",
"zig.zls.enableInlayHints": false,

"[zig]": {
"editor.tabSize": 4,
"editor.useTabStops": false,
Expand Down
4 changes: 2 additions & 2 deletions cmake/sources/CxxSources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,8 @@ src/bun.js/bindings/webcrypto/CryptoAlgorithmSHA1.cpp
src/bun.js/bindings/webcrypto/CryptoAlgorithmSHA224.cpp
src/bun.js/bindings/webcrypto/CryptoAlgorithmSHA256.cpp
src/bun.js/bindings/webcrypto/CryptoAlgorithmSHA384.cpp
src/bun.js/bindings/webcrypto/CryptoAlgorithmX25519.cpp
src/bun.js/bindings/webcrypto/CryptoAlgorithmSHA512.cpp
src/bun.js/bindings/webcrypto/CryptoAlgorithmX25519.cpp
src/bun.js/bindings/webcrypto/CryptoDigest.cpp
src/bun.js/bindings/webcrypto/CryptoKey.cpp
src/bun.js/bindings/webcrypto/CryptoKeyAES.cpp
Expand Down Expand Up @@ -443,14 +443,14 @@ src/bun.js/bindings/webcrypto/JSHkdfParams.cpp
src/bun.js/bindings/webcrypto/JSHmacKeyParams.cpp
src/bun.js/bindings/webcrypto/JSJsonWebKey.cpp
src/bun.js/bindings/webcrypto/JSPbkdf2Params.cpp
src/bun.js/bindings/webcrypto/JSX25519Params.cpp
src/bun.js/bindings/webcrypto/JSRsaHashedImportParams.cpp
src/bun.js/bindings/webcrypto/JSRsaHashedKeyGenParams.cpp
src/bun.js/bindings/webcrypto/JSRsaKeyGenParams.cpp
src/bun.js/bindings/webcrypto/JSRsaOaepParams.cpp
src/bun.js/bindings/webcrypto/JSRsaOtherPrimesInfo.cpp
src/bun.js/bindings/webcrypto/JSRsaPssParams.cpp
src/bun.js/bindings/webcrypto/JSSubtleCrypto.cpp
src/bun.js/bindings/webcrypto/JSX25519Params.cpp
src/bun.js/bindings/webcrypto/OpenSSLUtilities.cpp
src/bun.js/bindings/webcrypto/PhonyWorkQueue.cpp
src/bun.js/bindings/webcrypto/SerializedCryptoKeyWrapOpenSSL.cpp
Expand Down
2 changes: 2 additions & 0 deletions cmake/sources/ZigSources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ src/bun.js/node/node_http_binding.zig
src/bun.js/node/node_net_binding.zig
src/bun.js/node/node_os.zig
src/bun.js/node/node_process.zig
src/bun.js/node/node_tls_binding.zig
src/bun.js/node/node_util_binding.zig
src/bun.js/node/node_zlib_binding.zig
src/bun.js/node/nodejs_error_code.zig
Expand Down Expand Up @@ -581,6 +582,7 @@ src/system_timer.zig
src/test/fixtures.zig
src/test/recover.zig
src/thread_pool.zig
src/tls.zig
src/tmp.zig
src/toml/toml_lexer.zig
src/toml/toml_parser.zig
Expand Down
2 changes: 1 addition & 1 deletion packages/bun-types/overrides.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export {};

declare global {
namespace NodeJS {
interface ProcessEnv extends Bun.Env, ImportMetaEnv {}
interface ProcessEnv extends Bun.Env {}

interface Process {
readonly version: string;
Expand Down
134 changes: 126 additions & 8 deletions packages/bun-usockets/src/crypto/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ struct loop_ssl_data {

struct us_internal_ssl_socket_context_t {
struct us_socket_context_t sc;
struct us_bun_socket_context_options_t options;

// this thing can be shared with other socket contexts via socket transfer!
// maybe instead of holding once you hold many, a vector or set
Expand Down Expand Up @@ -285,7 +286,6 @@ int us_internal_handle_shutdown(struct us_internal_ssl_socket_t *s, int force_fa
// we got some error here, but we dont care about it, we are closing the socket
int err = SSL_get_error(s->ssl, ret);
if (err == SSL_ERROR_SSL || err == SSL_ERROR_SYSCALL) {
// clear
ERR_clear_error();
s->fatal_error = 1;
// Fatal error occurred, we should close the socket imeadiatly
Expand Down Expand Up @@ -326,6 +326,41 @@ int us_internal_ssl_socket_is_closed(struct us_internal_ssl_socket_t *s) {
return us_socket_is_closed(0, &s->s);
}

/**
* Override the protocol error if the secure_protocol_method is set. This is to match Node's
* behaviour
* Will modify the verify_error struct to override the error code and reason if necessary.

* Returns 1 if the protocol error was overridden, 0 otherwise
*/
static int should_override_protocol_error(const char *proto, int is_server, int openssl_reason, struct us_bun_verify_error_t *verify_error) {
if (!proto) return 0;
if (is_server) {
if (strcmp(proto, "TLSv1_method") == 0 || strcmp(proto, "TLSv1_1_method") == 0) {
if (openssl_reason == SSL_R_WRONG_VERSION_NUMBER) {
verify_error->code = "ERR_SSL_WRONG_VERSION_NUMBER";
verify_error->reason = "Wrong version number on server";
} else if (openssl_reason == SSL_R_UNSUPPORTED_PROTOCOL) {
verify_error->code = "ERR_SSL_UNSUPPORTED_PROTOCOL";
verify_error->reason = "Unsupported protocol on server";
}

verify_error->error = -1;
ERR_clear_error();
return 1;
}
} else {
if (strcmp(proto, "SSLv23_method") == 0) {
verify_error->code = "ERR_SSL_UNSUPPORTED_PROTOCOL";
verify_error->reason = "Unsupported protocol";
verify_error->error = -1;
ERR_clear_error();
return 1;
}
}

return 0;
}

void us_internal_trigger_handshake_callback_econnreset(struct us_internal_ssl_socket_t *s) {
struct us_internal_ssl_socket_context_t *context =
Expand All @@ -348,6 +383,72 @@ void us_internal_trigger_handshake_callback(struct us_internal_ssl_socket_t *s,

if (context->on_handshake != NULL) {
struct us_bun_verify_error_t verify_error = us_internal_verify_error(s);

if (!success && (verify_error.code == NULL || verify_error.code[0] == 0)) {
const char *proto = context->options.secure_protocol_method;
unsigned long err = ERR_peek_error();
int reason = ERR_GET_REASON(err);
if (should_override_protocol_error(proto, SSL_is_server(s->ssl), reason, &verify_error)) {
context->on_handshake(s, success, verify_error, context->handshake_data);
return;
}

if (context->options.secure_protocol_method) {
if (SSL_is_server(s->ssl)) {
unsigned long err = ERR_peek_error();
int reason = ERR_GET_REASON(err);
if ((strcmp(proto, "TLSv1_1_method") == 0 || strcmp(proto, "TLSv1_method") == 0)) {
if (reason == SSL_R_WRONG_VERSION_NUMBER) {
verify_error.code = "ERR_SSL_WRONG_VERSION_NUMBER";
verify_error.reason = "Wrong version number on server";
verify_error.error = -1;
ERR_clear_error();
context->on_handshake(s, success, verify_error, context->handshake_data);
return;
} else if (reason == SSL_R_UNSUPPORTED_PROTOCOL) {
verify_error.code = "ERR_SSL_UNSUPPORTED_PROTOCOL";
verify_error.reason = "Unsupported protocol on server";
verify_error.error = -1;
ERR_clear_error();
context->on_handshake(s, success, verify_error, context->handshake_data);
return;
}
}
} else {
if (strcmp(proto, "SSLv23_method") == 0) {
verify_error.code = "ERR_SSL_UNSUPPORTED_PROTOCOL";
verify_error.reason = "Unsupported protocol";
verify_error.error = -1;
ERR_clear_error();
context->on_handshake(s, success, verify_error, context->handshake_data);
return;
}
}
}

if (verify_error.error == 0) {
verify_error.error = -1;

unsigned long err = ERR_peek_error();
int reason = ERR_GET_REASON(err);

if (reason == SSL_R_TLSV1_ALERT_PROTOCOL_VERSION) {
verify_error.code = "ERR_SSL_TLSV1_ALERT_PROTOCOL_VERSION";
verify_error.reason = "TLSv1 alert protocol version";
} else if (reason == SSL_R_UNSUPPORTED_PROTOCOL) {
verify_error.code = "ERR_SSL_UNSUPPORTED_PROTOCOL";
verify_error.reason = SSL_is_server(s->ssl) ? "Unsupported protocol on server" : "Unsupported protocol on client";
} else if (reason == SSL_R_WRONG_VERSION_NUMBER) {
verify_error.code = "ERR_SSL_WRONG_VERSION_NUMBER";
verify_error.reason = "Wrong version number on server";
} else {
verify_error.code = "ERR_SSL_UNSUPPORTED_PROTOCOL";
verify_error.reason = "Unsupported protocol";
}
ERR_clear_error();
}
}

context->on_handshake(s, success, verify_error, context->handshake_data);
}
}
Expand Down Expand Up @@ -400,8 +501,7 @@ void us_internal_update_handshake(struct us_internal_ssl_socket_t *s) {

if (us_internal_ssl_socket_is_closed(s) || us_internal_ssl_socket_is_shut_down(s) ||
(s->ssl && SSL_get_shutdown(s->ssl) & SSL_RECEIVED_SHUTDOWN)) {

us_internal_trigger_handshake_callback(s, 0);
us_internal_trigger_handshake_callback_econnreset(s);
return;
}

Expand Down Expand Up @@ -507,6 +607,7 @@ struct us_internal_ssl_socket_t *ssl_on_data(struct us_internal_ssl_socket_t *s,

if (just_read <= 0) {
int err = SSL_get_error(s->ssl, just_read);

// as far as I know these are the only errors we want to handle
if (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE) {
if (err == SSL_ERROR_WANT_RENEGOTIATE) {
Expand Down Expand Up @@ -540,12 +641,11 @@ struct us_internal_ssl_socket_t *ssl_on_data(struct us_internal_ssl_socket_t *s,
}

if (err == SSL_ERROR_SSL || err == SSL_ERROR_SYSCALL) {
// clear per thread error queue if it may contain something
ERR_clear_error();
s->fatal_error = 1;
us_internal_trigger_handshake_callback(s, 0);
}

// terminate connection here
// Terminate connection after reporting the handshake error
us_internal_ssl_socket_close(s, 0, NULL);
return NULL; // stop processing data
} else {
Expand Down Expand Up @@ -1140,14 +1240,31 @@ SSL_CTX *create_ssl_context_from_bun_options(
/* Create the context */
SSL_CTX *ssl_context = SSL_CTX_new(TLS_method());


/* Default options we rely on - changing these will break our logic */
SSL_CTX_set_read_ahead(ssl_context, 1);
/* we should always accept moving write buffer so we can retry writes with a
* buffer allocated in a different address */
SSL_CTX_set_mode(ssl_context, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
if (options.min_tls_version > 0) {
if (!SSL_CTX_set_min_proto_version(ssl_context, options.min_tls_version)) {
free_ssl_context(ssl_context);
return NULL;
}
} else {

if (!SSL_CTX_set_min_proto_version(ssl_context, TLS1_2_VERSION)) {
free_ssl_context(ssl_context);
return NULL;
}
}

/* Anything below TLS 1.2 is disabled */
SSL_CTX_set_min_proto_version(ssl_context, TLS1_2_VERSION);
if (options.max_tls_version > 0) {
if (!SSL_CTX_set_max_proto_version(ssl_context, options.max_tls_version)) {
free_ssl_context(ssl_context);
return NULL;
}
}

/* The following are helpers. You may easily implement whatever you want by
* using the native handle directly */
Expand Down Expand Up @@ -1545,6 +1662,7 @@ us_internal_bun_create_ssl_socket_context(
context->ssl_context =
ssl_context; // create_ssl_context_from_options(options);
context->is_parent = 1;
context->options = options;

context->on_handshake = NULL;
context->handshake_data = NULL;
Expand Down
3 changes: 3 additions & 0 deletions packages/bun-usockets/src/libusockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ struct us_bun_socket_context_options_t {
int request_cert;
unsigned int client_renegotiation_limit;
unsigned int client_renegotiation_window;
unsigned int min_tls_version;
unsigned int max_tls_version;
const char *secure_protocol_method;
};

/* Return 15-bit timestamp for this context */
Expand Down
3 changes: 3 additions & 0 deletions packages/bun-uws/src/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ namespace uWS {
int request_cert = 0;
unsigned int client_renegotiation_limit = 3;
unsigned int client_renegotiation_window = 600;
unsigned int min_tls_version = 0;
unsigned int max_tls_version = 0;
const char **secure_protocol_method = nullptr;

/* Conversion operator used internally */
operator struct us_bun_socket_context_options_t() const {
Expand Down
12 changes: 12 additions & 0 deletions src/bun.js/api/bun/socket.zig
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,12 @@ pub const Listener = struct {
return globalObject.throwValue(err);
};

if (ssl_enabled and create_err != .none) {
const js_err = create_err.toJS(globalObject);
uws.us_socket_context_free(1, socket_context);
return globalObject.throwValue(js_err);
}

if (ssl_enabled) {
if (ssl.?.protos) |p| {
protos = p[0..ssl.?.protos_len];
Expand Down Expand Up @@ -1217,6 +1223,12 @@ pub const Listener = struct {
return globalObject.throwValue(err.toErrorInstance(globalObject));
};

if (ssl_enabled and create_err != .none) {
const js_err = create_err.toJS(globalObject);
uws.us_socket_context_free(1, socket_context);
return globalObject.throwValue(js_err);
}

if (ssl_enabled) {
if (ssl.?.protos) |p| {
protos = p[0..ssl.?.protos_len];
Expand Down
20 changes: 19 additions & 1 deletion src/bun.js/api/bun/ssl_wrapper.zig
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ pub fn SSLWrapper(comptime T: type) type {
if (this.flags.closed_notified) return;

this.flags.authorized = success;

// trigger the handshake callback
this.handlers.onHandshake(this.handlers.ctx, success, result);
}
Expand Down Expand Up @@ -311,8 +312,25 @@ pub fn SSLWrapper(comptime T: type) type {
if (this.isShutdown()) {
return .{};
}

const ssl = this.ssl orelse return .{};
return uws.us_ssl_socket_verify_error_from_ssl(ssl);

const peek_err = BoringSSL.ERR_peek_error();
var verr = uws.us_ssl_socket_verify_error_from_ssl(ssl);

// no certificate verification = handshake error
if (verr.code == null and peek_err != 0) {
const reason_ptr = BoringSSL.ERR_reason_error_string(peek_err);

verr = uws.us_bun_verify_error_t{
.error_no = @intCast(peek_err),
.code = reason_ptr,
.reason = reason_ptr,
};
BoringSSL.ERR_clear_error();
}

return verr;
}

/// Update the handshake state
Expand Down
Loading