<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<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;">Hello,
<div style="color: rgb(0, 0, 0);"><br>
</div>
<div style="color: rgb(0, 0, 0);">I can't figure out how OpenSSL connections work.</div>
<div style="color: rgb(0, 0, 0);"><br>
</div>
<div style="color: rgb(0, 0, 0);">I would like to use 2 bios (rbio, wbio) which will be shared for all my connections.</div>
<div style="color: rgb(0, 0, 0);">The problem is that I really don't understand the error messages.</div>
<div style="color: rgb(0, 0, 0);">I never get an SSL_ERROR_WANT_WRITE error code, I only get SSL_ERROR_WANT_READ.</div>
<div style="color: rgb(0, 0, 0);"><br>
</div>
<div style="color: rgb(0, 0, 0);">For example, for the handshake, whether I have to send or receive data, I only receive the error SSL_ERROR_WANT_READ.</div>
<div style="color: rgb(0, 0, 0);">(calls are blocking for simplicity)</div>
<div style="">
<pre style="background-color: rgb(40, 44, 52); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><span style="color: rgb(171, 178, 191);"><span style="color:#c678dd">int </span><span style="color:#61afef">main</span>()<br>{<br>       <span style="color:#c678dd">struct </span><span style="color:#e5c07b">addrinfo </span>hints, *result;<br>       <span style="color:#61afef">memset</span>(&hints, <span style="color:#d19a66">0</span>, <span style="color:#c678dd">sizeof</span>(<span style="color:#c678dd">struct </span><span style="color:#e5c07b">addrinfo</span>));<br>       hints.<span style="color:#e06c75">ai_family </span>= <span style="color:#e06c75">AF_UNSPEC</span>;<br>       hints.<span style="color:#e06c75">ai_socktype </span>= <span style="color:#e06c75">SOCK_STREAM</span>;<br><br>       <span style="color:#c678dd">if </span>(<span style="color:#61afef">getaddrinfo</span>(<span style="color:#98c379">"google.com"</span>, <span style="color:#98c379">"443"</span>, &hints, &result) != <span style="color:#d19a66">0</span>) {<br>              <span style="color:#61afef">printf</span>(<span style="color:#98c379">"getaddrinfo error"</span>);<br>              <span style="color:#61afef">exit</span>(<span style="color:#d19a66">1</span>);<br>       }<br><br>       <span style="color:#c678dd">int </span>fd = -<span style="color:#d19a66">1</span>;<br>       <span style="color:#c678dd">for </span>(; result != <span style="color:#c678dd">nullptr</span>; result = result-><span style="color:#e06c75">ai_next</span>) {<br>              fd = <span style="color:#61afef">socket</span>(result-><span style="color:#e06c75">ai_family</span>, result-><span style="color:#e06c75">ai_socktype</span>, result-><span style="color:#e06c75">ai_protocol</span>);<br>              <span style="color:#c678dd">if </span>(fd < <span style="color:#d19a66">0</span>)<br>                     <span style="color:#c678dd">continue</span>;<br>              <span style="color:#c678dd">int </span>res = <span style="color:#61afef">connect</span>(fd, result-><span style="color:#e06c75">ai_addr</span>, result-><span style="color:#e06c75">ai_addrlen</span>);<br>              <span style="color:#c678dd">if </span>(res == -<span style="color:#d19a66">1</span>) {<br>                     <span style="color:#c678dd">continue</span>;<br>              } break;<br>       }<br>       <span style="color:#c678dd">if </span>(fd < <span style="color:#d19a66">0</span>) {<br>              <span style="color:#61afef">printf</span>(<span style="color:#98c379">"Connection error"</span>);<br>              <span style="color:#61afef">exit</span>(<span style="color:#d19a66">1</span>);<br>       }<br>       <span style="color:#61afef">freeaddrinfo</span>(result);<br><br>       <span style="color:#e06c75">SSL_library_init</span>();<br>       <span style="color:#e06c75">OpenSSL_add_all_algorithms</span>();<br>       <span style="color:#e06c75">SSL_load_error_strings</span>();<br>       <span style="color:#61afef">ERR_load_BIO_strings</span>();<br>       <span style="color:#e06c75">ERR_load_crypto_strings</span>();<br>       <span style="color:#e5c07b">SSL_CTX </span>*ctx = <span style="color:#61afef">SSL_CTX_new</span>(<span style="color:#e06c75">SSLv23_method</span>());<br><br>       <span style="color:#e5c07b">BIO </span>*rbio = <span style="color:#61afef">BIO_new</span>(<span style="color:#61afef">BIO_s_mem</span>());<br>       <span style="color:#e5c07b">BIO </span>*wbio = <span style="color:#61afef">BIO_new</span>(<span style="color:#61afef">BIO_s_mem</span>());<br>       <span style="color:#e5c07b">SSL </span>*ssl = <span style="color:#61afef">SSL_new</span>(ctx);<br>       <span style="color:#61afef">SSL_set_bio</span>(ssl, rbio, wbio);<br>       <span style="color:#61afef">SSL_set_connect_state</span>(ssl);<br><br>       <span style="color:#c678dd">int </span>n;<br>       <span style="color:#c678dd">do </span>{<br>              n = <span style="color:#61afef">SSL_do_handshake</span>(ssl);<br>              n = <span style="color:#61afef">SSL_get_error</span>(ssl, n);<br>              <span style="color:#c678dd">if </span>(n == <span style="color:#e06c75">SSL_ERROR_WANT_READ</span>) {<br>                     <span style="color:#c678dd">char </span>buffer[</span><font color="#d19a66">4096</font><font color="#abb2bf">];<br>                     </font><span style="color: rgb(198, 120, 221);">int </span><font color="#abb2bf">r = </font><span style="color: rgb(97, 175, 239);">BIO_read</span><font color="#abb2bf">(wbio, buffer, </font><span style="color: rgb(198, 120, 221);">sizeof</span><font color="#abb2bf">(buffer));<br>                     </font><span style="color: rgb(198, 120, 221);">int </span><font color="#abb2bf">error = </font><span style="color: rgb(97, 175, 239);">SSL_get_error</span><font color="#abb2bf">(ssl, r);<br>                     </font><span style="color: rgb(198, 120, 221);">if </span><font color="#abb2bf">(error == </font><span style="color: rgb(224, 108, 117);">SSL_ERROR_WANT_READ</span><font color="#abb2bf">) {<br>                            r = </font><span style="color: rgb(97, 175, 239);">read</span><font color="#abb2bf">(fd, buffer, </font><span style="color: rgb(198, 120, 221);">sizeof</span><font color="#abb2bf">(buffer));<br>                            </font><span style="color: rgb(97, 175, 239);">BIO_write</span><font color="#abb2bf">(rbio, buffer, r);<br>                     } </font><span style="color: rgb(198, 120, 221);">else </span><font color="#abb2bf">{<br>                            </font><span style="color: rgb(97, 175, 239);">send</span><font color="#abb2bf">(fd, buffer, r, </font><span style="color: rgb(209, 154, 102);">0</span><font color="#abb2bf">);<br>                     }<br>              }<br>       } </font><span style="color: rgb(198, 120, 221);">while </span><font color="#abb2bf">(n != </font><span style="color: rgb(224, 108, 117);">SSL_ERROR_NONE</span><font color="#abb2bf">);<br>       </font><span style="color: rgb(97, 175, 239);">printf</span><font color="#abb2bf">(</font><span style="color: rgb(152, 195, 121);">"Connected"</span><font color="#abb2bf">);<br>}</font></pre>
<br>
</div>
<div style="color: rgb(0, 0, 0);"><br>
</div>
<div style="color: rgb(0, 0, 0);"><br>
</div>
<div style="color: rgb(0, 0, 0);">How to manage the connection (with several sockets on the same bios) if I don't know if I have to send data or receive it according to the error message?</div>
<div style="color: rgb(0, 0, 0);"><br>
</div>
Thank you<br>
</div>
</body>
</html>