<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Anyone good with C/C++ and the ClientHello message who can help me out here?  I wrote the callback to SSL_CTX_set_client_hello_cb like this:<br>
<span style="background-color: rgb(255, 255, 255); font-family: monospace;">static int set_alpn_cb(SSL* ssl, int alert, void* arg)</span>
<div><span style="background-color: rgb(255, 255, 255); font-family: monospace;">{</span></div>
<div><span style="background-color: rgb(255, 255, 255); font-family: monospace;">      constexpr int ext_type{ TLSEXT_TYPE_application_layer_protocol_negotiation };</span></div>
<div><span style="background-color: rgb(255, 255, 255); font-family: monospace;">      int extensions[1]{ ext_type };</span></div>
<div><span style="background-color: rgb(255, 255, 255); font-family: monospace;">      std::size_t extensions_len{ 1u };</span></div>
<div><span style="background-color: rgb(255, 255, 255); font-family: monospace;">      int extensions_present{ SSL_client_hello_get1_extensions_present(ssl,
</span></div>
<div><span style="background-color: rgb(255, 255, 255); font-family: monospace;">            reinterpret_cast<int**>(extensions), &extensions_len) };</span></div>
<div><span style="background-color: rgb(255, 255, 255); font-family: monospace;">      const unsigned char** alpn_str;</span></div>
<div><span style="background-color: rgb(255, 255, 255); font-family: monospace;">      std::size_t alpn_str_len{};</span></div>
<div><br>
</div>
<div><span style="background-color: rgb(255, 255, 255); font-family: monospace;">      // 1 means success</span></div>
<div><span style="background-color: rgb(255, 255, 255); font-family: monospace;">      if (extensions_present == 1)</span></div>
<div><span style="background-color: rgb(255, 255, 255); font-family: monospace;">      {</span></div>
<div><span style="background-color: rgb(255, 255, 255); font-family: monospace;">            SSL_client_hello_get0_ext(ssl, ext_type, alpn_str, &alpn_str_len);</span></div>
<div><span style="background-color: rgb(255, 255, 255); font-family: monospace;">            std::cout << "alpn_str: " << alpn_str << std::endl;</span></div>
<div><span style="background-color: rgb(255, 255, 255); font-family: monospace;">      }</span></div>
<span style="background-color: rgb(255, 255, 255); font-family: monospace;">}</span><br>
</div>
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div class="x_x_x_BodyFragment"><font size="2"><span style="font-size:11pt">
<div class="x_x_x_PlainText x_x_x_elementToProof x_elementToProof elementToProof">
<br>
</div>
<div class="x_x_x_PlainText x_x_x_elementToProof x_elementToProof elementToProof">
(not sure if I did this right, and also don't know how to actually get the ALPN and choose a protocol with this since the <code>out</code>​ parameter is of type
<code>const unsigned char**</code>​ meaning it can't be modified inside the function either, or else I could've expected the ALPN string to be in there after the function returns.)</div>
<div class="x_x_x_PlainText x_x_x_elementToProof x_elementToProof elementToProof">
<br>
</div>
<div class="x_x_x_PlainText x_x_x_elementToProof x_elementToProof elementToProof">
And I called the <span style="font-family:Calibri, Helvetica, sans-serif;font-size:16px;background-color:rgb(255, 255, 255);display:inline !important">SSL_CTX_set_client_hello_cb function like this (inside the function where I'm doing the TLS handshake for
 the server):</span><br>
</div>
<div class="x_x_x_PlainText x_x_x_elementToProof x_elementToProof elementToProof">
<span style="font-family: monospace;">auto* ssl_ctx{ ctx.native_handle() };</span>
<div><span style="font-family: monospace;">SSL* ssl{};</span></div>
<div><span style="font-family: monospace;">void* arg{};</span></div>
</div>
<div class="x_x_x_PlainText x_x_x_elementToProof x_elementToProof elementToProof">
<span style="font-family: monospace;">SSL_CTX_set_client_hello_cb(ssl_ctx, &set_alpn_cb, nullptr);</span><br>
</div>
<div class="x_x_x_PlainText x_x_x_elementToProof x_elementToProof elementToProof">
<span style="font-family: monospace;"><br>
</span></div>
<div class="x_x_x_PlainText x_x_x_elementToProof x_elementToProof elementToProof">
<span style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;">​And now I have an error saying:</span></div>
<div class="x_x_x_PlainText x_x_x_elementToProof x_elementToProof elementToProof">
error C2664: 'void SSL_CTX_set_client_hello_cb(SSL_CTX *,SSL_client_hello_cb_fn,void *)': cannot convert argument 2 from 'int (__cdecl *)(SSL *,int,void *)' to 'SSL_client_hello_cb_fn'<span style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;"><br>
</span></div>
<div class="x_x_x_PlainText x_x_x_elementToProof x_elementToProof elementToProof">
<br>
</div>
<div class="x_x_x_PlainText x_x_x_elementToProof x_elementToProof elementToProof">
Without the <code>&</code>​ in front, I get this error:</div>
<div class="x_x_x_PlainText x_x_x_elementToProof x_elementToProof elementToProof">
 error C2664: 'void SSL_CTX_set_client_hello_cb(SSL_CTX *,SSL_client_hello_cb_fn,void *)': cannot convert argument 2 from 'int (__cdecl *)(SSL *,int,void *)' to 'SSL_client_hello_cb_fn'<br>
</div>
<div class="x_x_x_PlainText x_x_x_elementToProof x_elementToProof elementToProof">
<br>
</div>
<div class="x_x_x_PlainText x_x_x_elementToProof x_elementToProof elementToProof">
Someone please help, if possible.  Thanks.</div>
<div class="x_x_x_PlainText x_x_x_elementToProof x_elementToProof elementToProof">
-------- Original Message --------<br>
<br>
*Subject:* Re: SSL_CTX_set_alpn_select_cb and Other OpenSSL API ALPN<br>
Functions + Their Callbacks<br>
*From:* Osman Zakir <osmanzakir90@hotmail.com><br>
*To:* "angus@magsys.co.uk" <angus@magsys.co.uk><br>
*Date:* Sat, 30 Jul 2022 16:51:36 +0000<br>
<br>
> Sorry, I can not educate you in how write C programmes.<br>
<br>
I'm not asking you to.  I'm only asking if there are good examples of<br>
C/C++ code using the function properly.  And at least answer my<br>
question about the wire-format for HTTP/2, please.<br>
<br>
And what does the value of the SSL* pointer have to be?<br>
<br>
-------- Original Message --------<br>
<br>
*Subject:* Re: SSL_CTX_set_alpn_select_cb and Other OpenSSL API ALPN<br>
Functions + Their Callbacks<br>
*From:* Osman Zakir <osmanzakir90@hotmail.com><br>
*To:* "angus@magsys.co.uk" <angus@magsys.co.uk><br>
*Date:* Sat, 30 Jul 2022 14:41:04 +0000<br>
<br>
> There is a detailed OpenSSL manaul page for<br>
>SSL_CTX_set_client_hello_cb.<br>
<br>
I took a look at that and have some questions: In the callback function<br>
arguments, would it be okay to pass 0 as the second argument if I have<br>
no error to alert?  What should the third argument be?  The protocol in<br>
wire-format?  And what do pass as the first argument?  The SSL_CTX?<br>
<br>
I also don't really get the wire-format itself.  Is HTTP/2 guaranteed<br>
to always be represented by the char array {6, 's', 'p', 'd', 'y', '/',<br>
'1'}_?<br>
<br>
> Google brings up lots of examples of it being used.<br>
<br>
Having some trouble finding them.<br>
<br>
> My own implementation is open source at:<br>
<br>
 > <a href="https://svn.overbyte.be/svn/ics/trunk/Source/OverbyteIcsWSocket.pas" data-auth="NotApplicable">
https://svn.overbyte.be/svn/ics/trunk/Source/OverbyteIcsWSocket.pas</a><br>
<br>
> but is Delphi/pascal language.<br>
<br>
Thanks for that, but I don't really understand Pascal.  I need a good C<br>
or C++ example.<br>
<br>
Osman<br>
<br>
-------- Original Message --------<br>
<br>
*Subject:* Re: SSL_CTX_set_alpn_select_cb and Other OpenSSL API ALPN<br>
Functions + Their Callbacks<br>
*From:* Osman Zakir <osmanzakir90@hotmail.com><br>
*To:* "angus@magsys.co.uk" <angus@magsys.co.uk><br>
*Date:* Sat, 30 Jul 2022 00:50:56 +0000<br>
<br>
Thanks for the reply.<br>
<br>
So, how do I write the SSL_client_hello_cb_fn callback?  I need some<br>
examples for this and also for parsing whatever I need to parse to get<br>
the TLSEXT_TYPE_server_name and<br>
TLSEXT_TYPE_application_layer_protocol_negotiation that I need.  I'm<br>
sorry about this but I'm completely new to using OpenSSL directly and<br>
also to ALPN.<br>
<br>
I'm not a networking person or library author originally, so I also had<br>
to read up on ASIO before I could use Boost.Beast to write my app<br>
server.  Difference is that ASIO and Beast are at least documented well<br>
and the latter even has server and client examples that are their own<br>
self-contained apps that you can just take and work with.<br>
<br>
[Sorry for the top-posting, but hotmail isn't letting me fix it for<br>
some reason; it used to before.]<br>
________________________________<br>
From: openssl-users <openssl-users-bounces@openssl.org> on behalf of<br>
Angus Robertson - Magenta Systems Ltd <angus@magsys.co.uk><br>
Sent: Friday, July 29, 2022 9:22 PM<br>
To: openssl-users@openssl.org <openssl-users@openssl.org><br>
Subject: Re: SSL_CTX_set_alpn_select_cb and Other OpenSSL API ALPN<br>
Functions + Their Callbacks<br>
<br>
> I don't understand how to write the callback functions some of<br>
> the OpenSSL ALPN functions expect, and the manual really isn't<br>
> helping there either, so I'd like some help.<br>
<br>
Use SSL_CTX_set_client_hello_cb to set a SSL_client_hello_cb_fn<br>
function, which you can parse to get TLSEXT_TYPE_server_name and<br>
TLSEXT_TYPE_application_layer_protocol_negotiation, and everything else<br>
sent in the Client Hello (if you need it) like SSL versions and ciphers<br>
supported.<br>
<br>
Within this callback you can change SSL_CTX depending on SNI and ALPN.<br>
<br>
<br>
Ignore the SNI and ALPN callbacks.  client_hello_cb was only added in<br>
1.1.1 so is often missing from old examples, FAQs and manuals.<br>
<br>
Angus<br>
<br>
<br>
<br>
--<br>
*Included Files:*<br>
am2file:001-HTML_Message.html<br>
<br>
<br>
<br>
--<br>
*Included Files:*<br>
am2file:001-HTML_Message.html<br>
<br>
<br>
<br>
--<br>
*Included Files:*<br>
am2file:001-HTML_Message.html<br>
<br>
</div>
</span></font></div>
</div>
</div>
</div>
</body>
</html>