Skip to content

Commit 58af7b4

Browse files
dhobsonglws-team
authored andcommitted
From df9761a Mon Sep 17 00:00:00 2001
Subject: [PATCH] remove LWS_CALLBACK_OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY callback When a certificate for a TLS connection is provided, but a private key is not, the SSL_CTX initialization exits early, before the CONTEXT_REQUIRES_PRIVATE_KEY callback can be issued. Remove the now obsolete callback and update the vhost field description to state that the LOAD_EXTRA_SERVER_VERIFY_CERTS callback should be used instead.
1 parent 8796dc0 commit 58af7b4

3 files changed

Lines changed: 17 additions & 31 deletions

File tree

include/libwebsockets/lws-callbacks.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,6 @@ enum lws_callback_reasons {
160160
* the default callback action of returning 0 allows the client
161161
* certificates. */
162162

163-
LWS_CALLBACK_OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY = 37,
164-
/**< if configured for including OpenSSL support but no private key
165-
* file has been specified (ssl_private_key_filepath is NULL), this is
166-
* called to allow the user to set the private key directly via
167-
* libopenssl and perform further operations if required; this might be
168-
* useful in situations where the private key is not directly accessible
169-
* by the OS, for example if it is stored on a smartcard.
170-
* user is the server's OpenSSL SSL_CTX* */
171-
172163
LWS_CALLBACK_SSL_INFO = 67,
173164
/**< SSL connections only. An event you registered an
174165
* interest in at the vhost has occurred on a connection

include/libwebsockets/lws-context-vhost.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,15 @@ struct lws_context_creation_info {
390390
*/
391391
const char *ssl_private_key_filepath;
392392
/**< VHOST: filepath to private key if wanting SSL mode;
393-
* if this is set to NULL but ssl_cert_filepath is set, the
394-
* OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY callback is called
395-
* to allow setting of the private key directly via openSSL
396-
* library calls. (For backwards compatibility, this can also be used
393+
* this should not be set to NULL when ssl_cert_filepath is set.
394+
*
395+
* Alteratively, the certificate and private key can both be set in
396+
* the OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS callback directly via
397+
* openSSL library calls. This requires that
398+
* LWS_SERVER_OPTION_CREATE_VHOST_SSL_CTX is set in the vhost info options
399+
* to force initializtion of the SSL_CTX context.
400+
*
401+
* (For backwards compatibility, this can also be used
397402
* to pass the client cert private key filepath when setting up a
398403
* vhost client SSL context, but it is preferred to use
399404
* .client_ssl_private_key_filepath for that.)

lib/tls/openssl/openssl-server.c

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,10 @@ lws_tls_server_certs_load(struct lws_vhost *vhost, struct lws *wsi,
228228
return 1;
229229
}
230230

231-
if (private_key) {
231+
if (!private_key) {
232+
lwsl_err("ssl private key not set\n");
233+
return 1;
234+
} else {
232235
/* set the private key from KeyFile */
233236
if (SSL_CTX_use_PrivateKey_file(vhost->tls.ssl_ctx, private_key,
234237
SSL_FILETYPE_PEM) != 1) {
@@ -244,14 +247,6 @@ lws_tls_server_certs_load(struct lws_vhost *vhost, struct lws *wsi,
244247
private_key, error, s);
245248
return 1;
246249
}
247-
} else {
248-
if (vhost->protocols[0].callback(wsi,
249-
LWS_CALLBACK_OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY,
250-
vhost->tls.ssl_ctx, NULL, 0)) {
251-
lwsl_err("ssl private key not set\n");
252-
253-
return 1;
254-
}
255250
}
256251

257252
return 0;
@@ -389,7 +384,10 @@ lws_tls_server_certs_load(struct lws_vhost *vhost, struct lws *wsi,
389384
return 1;
390385
}
391386

392-
if (n != LWS_TLS_EXTANT_ALTERNATIVE && private_key) {
387+
if (n == LWS_TLS_EXTANT_ALTERNATIVE || !private_key) {
388+
lwsl_err("ssl private key not set\n");
389+
return 1;
390+
} else {
393391
/* set the private key from KeyFile */
394392
if (SSL_CTX_use_PrivateKey_file(vhost->tls.ssl_ctx, private_key,
395393
SSL_FILETYPE_PEM) != 1) {
@@ -400,14 +398,6 @@ lws_tls_server_certs_load(struct lws_vhost *vhost, struct lws *wsi,
400398
(char *)vhost->context->pt[0].serv_buf));
401399
return 1;
402400
}
403-
} else {
404-
if (vhost->protocols[0].callback(wsi,
405-
LWS_CALLBACK_OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY,
406-
vhost->tls.ssl_ctx, NULL, 0)) {
407-
lwsl_err("ssl private key not set\n");
408-
409-
return 1;
410-
}
411401
}
412402

413403
check_key:

0 commit comments

Comments
 (0)