how to convert a pem file to server_ssl_private_key_mem?
If I get similar below Pem file like, how to convert it to server_ssl_private_key_mem? It seems like that doesn't work that reading all file content to a buffer, and then pass the buffer to server_ssl_private_key_mem. Need to filter key content between "BEGIN PRIVATE KEY" and "END PRIVATE KEY"? or something else?
Pem file:
'''
Bag Attributes
localKeyID: A7 73 B0 ...
Key Attributes:
Both mbedtls and openssl use an lws function lws_tls_alloc_pem_to_der_file() now to handle these, because of differences in the tls library helper apis functionality. The lws helper should accept either PEM or DER (DER is what you get if you base64-decode the active part of a PEM).
https://libwebsockets.org/git/libwebsockets/tree/lib/tls/tls.c?h=master#n125
If you have time please take a look at what's happening there with your key.
Before using lws_tls_alloc_pem_to_der_file(), I guess I can assign key_mem and cert_mem with a fixed pair of key and cert. Just do it like an example from "https://libwebsockets.org/git/libwebsockets/tree/minimal-examples/http-server/minimal-http-server-tls-mem/minimal-http-server-tls-mem.c"
And my code like this(don't need to care about the real value about cert and key):
static const char *cert_pem =
"-----BEGIN CERTIFICATE-----\n"
"BAgIUG57sIVThEXaOvnT+cplTDfeKSoIwCwYJKoZIhvcNAQEL\n"
"MHAxdwGAYDVQQKExFBUlJJUyBHcm91cCwgSW5jLjEfMB0G\n"
"A1UECxMWSFdST0IgRGV2adMCIGA1UEAxMbSFdST0IgRGV2\n"
"aWNlIE1vZGVsIFN1Yi1DQSAdMzQzOVoXDTQzMTAyNzEyMzQz\n"
"-----END CERTIFICATE-----\n";
static const char *key_pem =
"-----BEGIN PRIVATE KEY-----\n"
"BAgIUG57sIVThEXaOvnT+cplTDfeKSoIwCwYJKoZIhvcNAQEL\n"
"MHAxdwGAYDVQQKExFBUlJJUyBHcm91cCwgSW5jLjEfMB0G\n"
"A1UECxMWSFdST0IgRGV2adMCIGA1UEAxMbSFdST0IgRGV2\n"
"aWNlIE1vZGVsIFN1Yi1DQSAdMzQzOVoXDTQzMTAyNzEyMzQz\n"
"-----END PRIVATE KEY-----\n";
creationInfo.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
creationInfo.server_ssl_cert_mem = cert_pem;
creationInfo.server_ssl_cert_mem_len = std::strlen(cert_pem);
creationInfo.server_ssl_private_key_mem = key_pem;
creationInfo.server_ssl_private_key_mem_len = std::strlen(key_pem);
creationInfo.user = this;
LwsContext = lws_create_context(&creationInfo);
assert(LwsContext);
auto vhost = lws_create_vhost(LwsContext, &creationInfo)
From below log, there is a failure to create a vhost. There is only this log, even I enable "-D CMAKE_BUILD_TYPE=DEBUG"
950 08:00:25.613 None: [1970/01/01 00:00:25:6129] NOTICE: Using SSL mode 951 08:00:25.613 None: [1970/01/01 00:00:25:6135] ERR: Problem loading update cert 952 08:00:25.613 None: [1970/01/01 00:00:25:6136] ERR: lws_create_vhost: lws_context_init_server_ssl failed
Do you have any idea about this err? By the way, could it possible to support PCKS12 file in lws_context_creation_info(without providing cert and key again)?
minimal-server-tls-mem
This works fine here
$ ./lws-minimal-http-server-tls-mem
[2019/06/28 08:32:19:4489] USER: LWS minimal http server TLS | visit https://localhost:7681
[2019/06/28 08:32:19:4557] NOTICE: Using SSL mode
[2019/06/28 08:32:19:4577] NOTICE: SSL ECDH curve 'prime256v1'
[2019/06/28 08:32:19:4653] NOTICE: created client ssl context for first
[2019/06/28 08:32:19:4655] NOTICE: Using SSL mode
[2019/06/28 08:32:19:4659] NOTICE: SSL ECDH curve 'prime256v1'
[2019/06/28 08:32:19:4659] NOTICE: created client ssl context for second
[2019/06/28 08:32:19:4660] NOTICE: vhost first: cert expiry: 729994d
[2019/06/28 08:32:19:4660] NOTICE: vhost second: cert expiry: 729994d
...
so back up and just try the same minimal example unchanged. That works for you too?
There is only this log, even I enable "-D CMAKE_BUILD_TYPE=DEBUG"
User code also dynamically selects the log verbosity level... look at -d 1151 for example. Without CMAKE_BUILD_TYPE=DEBUG the more verbose logs are stripped out at build-time.
Do you have any idea about this err?
Sure... minimal example works unchanged? Problem with your code. Minimal example doesn't work unchanged? Some other problem.
What is your tls library, openssl or mbedtls? What version?
By the way, could it possible to support PCKS12 file in lws_context_creation_info(without providing cert and key again)?
Of course, please send the patches.
Hmm, There is the following error when I build "minimal-http-server-tls" with a command "cmake . && make" in README.md.
CMake Error at CMakeLists.txt:56 (message): This project requires lws must have been configured with LWS_ROLE_H1 See also "/home/liyang/libwebsockets/libwebsockets/minimal-examples/http-server/minimal-http-server-tls/CMakeFiles/CMakeError.log".
And this is error in above CMakeError.log
/home/liyang/libwebsockets/libwebsockets/minimal-examples/http-server/minimal-http-server-tls/CMakeFiles/CMakeTmp/src.c:1:10: fatal error: libwebsockets.h: No such file or directory #include <libwebsockets.h> /home/liyang/libwebsockets/libwebsockets/minimal-examples/http-server/minimal-http-server-tls/CMakeFiles/CMakeTmp/src.c:1:10: fatal error: libwebsockets.h: No such file or directory #include <libwebsockets.h>
sorry for don't have a patch for PCKS12 now, this is just an idea now...
openssl in my case is 1.0.2q.
Do the make install step for lws before trying to build things against it.
minimal-http-server-tls-mem is passed in local. Maybe something wrong with my side. From the name "minimal-http-server-tls-mem", does that mean related "server_ssl_cert_mem" only works for https, and is not worked for ws/wss protocol? If there is a version requirement for openssl that server_ssl_cert_mem could work?
only works for https, and is not worked for ws/wss protocol
That's not how any of this works. https = http inside a tls tunnel. wss = ( http -> ws ) inside a tls tunnel.
Since all ws connections start off life as http, there is no gap like you are imagining.
If there is a version requirement for openssl that server_ssl_cert_mem could work?
Everything in OpenSSL touches on some api version dependency. However I wrote an lws function to handle loading PEM / DER loading and translation centrally, independent of whether the tls backend is mbedtls or openssl. So it's mainly a matter of the openssl version availability for SSL_CTX_use_certificate_ASN1().
https://libwebsockets.org/git/libwebsockets/tree/lib/tls/openssl/openssl-server.c#n253
There are two code paths depending on openssl 1.1+ or not
https://libwebsockets.org/git/libwebsockets/tree/lib/tls/openssl/openssl-server.c#n188
Hmm, I will use file way(eg: ssl_cert_filepath) instead temporarily.
If you're using openssl with version < 1.1.1, you need to convert the certificate PEM buffer to DER buffer, then openssl will handle the certificate successfully. Following patch can help. --- libwebsockets-master_aeb025.orig/lib/tls/openssl/openssl-server.c +++ libwebsockets-master_aeb025/lib/tls/openssl/openssl-server.c @@ -302,9 +302,14 @@ lws_tls_server_certs_load(struct lws_vho * memory-buffer private key image is PEM. */ #ifndef USE_WOLFSSL + if (lws_tls_alloc_pem_to_der_file(vhost->context, cert, mem_cert, + mem_cert_len, &p, &flen)) { + lwsl_err("%s: couldn't convert pem to der\n", func); + return 1; + } if (SSL_CTX_use_certificate_ASN1(vhost->tls.ssl_ctx, - (int)mem_cert_len, - (uint8_t *)mem_cert) != 1) { + (int)flen, + (url) p) != 1) { #else if (wolfSSL_CTX_use_certificate_buffer(vhost->tls.ssl_ctx, (uint8_t *)mem_cert,
Thanks... I screenscraped it and pushed it on master.
I think these things will leak a little bit as it is, if you use the mem cert. But it's once per vhost using them until exit, I am not sure if the openssl api needs the DER to be kept around after calling.