Sophie

Sophie

distrib > Mageia > cauldron > i586 > by-pkgid > 92a9f9a2ff17105809ab69f8e09db031 > files > 87

botan-doc-1.10.17-7.mga7.noarch.rpm


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>SSL and TLS &#8212; Botan</title>
    <link rel="stylesheet" href="_static/agogo.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <script type="text/javascript" src="_static/language_data.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="BigInt" href="bigint.html" />
    <link rel="prev" title="Certificate Handling" href="x509.html" /> 
  </head><body>
    <div class="header-wrapper">
      <div class="header">
        <h1>Botan</h1>
      </div>
    </div>

    <div class="content-wrapper">
      <div class="content">
        <div class="document">
            
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="ssl-and-tls">
<span id="ssl-api"></span><h1>SSL and TLS<a class="headerlink" href="#ssl-and-tls" title="Permalink to this headline">¶</a></h1>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.9.4.</span></p>
</div>
<p>Botan supports both client and server implementations of the SSL/TLS
protocols, including SSL v3, TLS v1.0, and TLS v1.1. The insecure and
obsolete SSL v2 is not supported.</p>
<p>The implementation uses <code class="docutils literal notranslate"><span class="pre">std::tr1::function</span></code>, so it may not have
been compiled into the version you are using; you can test for the
feature macro <code class="docutils literal notranslate"><span class="pre">BOTAN_HAS_SSL_TLS</span></code> to check.</p>
<div class="section" id="tls-clients">
<h2>TLS Clients<a class="headerlink" href="#tls-clients" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="_CPPv310TLS_Client">
<span id="_CPPv210TLS_Client"></span><span id="TLS_Client"></span><em class="property">class </em><code class="descname">TLS_Client</code><a class="headerlink" href="#_CPPv310TLS_Client" title="Permalink to this definition">¶</a><br /></dt>
<dd><dl class="function">
<dt id="_CPPv3N10TLS_Client10TLS_ClientENSt3tr18functionI6size_tP4byte6size_tEENSt3tr18functionIvPK4byte6size_tEERK10TLS_PolicyR21RandomNumberGenerator">
<span id="_CPPv2N10TLS_Client10TLS_ClientENSt3tr18functionI6size_tP4byte6size_tEENSt3tr18functionIvPK4byte6size_tEERK10TLS_PolicyR21RandomNumberGenerator"></span><span id="TLS_Client::TLS_Client__std::tr1::function:s.byteP.s:.std::tr1::function:void.byteCP.s:.TLS_PolicyCR.RandomNumberGeneratorR"></span><code class="descname">TLS_Client</code><span class="sig-paren">(</span>std::tr1::function&lt;size_t, byte *, size_t&gt; <em>input_fn</em>, std::tr1::function&lt;void, <em class="property">const</em> byte *, size_t&gt; <em>output_fn</em>, <em class="property">const</em> TLS_Policy &amp;<em>policy</em>, RandomNumberGenerator &amp;<em>rng</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv3N10TLS_Client10TLS_ClientENSt3tr18functionI6size_tP4byte6size_tEENSt3tr18functionIvPK4byte6size_tEERK10TLS_PolicyR21RandomNumberGenerator" title="Permalink to this definition">¶</a><br /></dt>
<dd></dd></dl>

<p>Creates a TLS client. It will call <em>input_fn</em> to read bytes from
the network and call <em>output_fn</em> when bytes need to be written to
the network.</p>
<dl class="function">
<dt id="_CPPv3N10TLS_Client4readEP4byte6size_t">
<span id="_CPPv2N10TLS_Client4readEP4byte6size_t"></span><span id="TLS_Client::read__byteP.s"></span>size_t <code class="descname">read</code><span class="sig-paren">(</span>byte *<em>buf</em>, size_t <em>buf_len</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv3N10TLS_Client4readEP4byte6size_t" title="Permalink to this definition">¶</a><br /></dt>
<dd></dd></dl>

<p>Reads up to <em>buf_len</em> bytes from the open connection into <em>buf</em>,
returning the number of bytes actually written.</p>
<dl class="function">
<dt id="_CPPv3N10TLS_Client5writeEPK4byte6size_t">
<span id="_CPPv2N10TLS_Client5writeEPK4byte6size_t"></span><span id="TLS_Client::write__byteCP.s"></span>void <code class="descname">write</code><span class="sig-paren">(</span><em class="property">const</em> byte *<em>buf</em>, size_t <em>buf_len</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv3N10TLS_Client5writeEPK4byte6size_t" title="Permalink to this definition">¶</a><br /></dt>
<dd></dd></dl>

<p>Writes <em>buf_len</em> bytes in <em>buf</em> to the remote side</p>
<dl class="function">
<dt id="_CPPv3N10TLS_Client5closeEv">
<span id="_CPPv2N10TLS_Client5closeEv"></span><span id="TLS_Client::close"></span>void <code class="descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv3N10TLS_Client5closeEv" title="Permalink to this definition">¶</a><br /></dt>
<dd></dd></dl>

<p>Closes the connection</p>
<dl class="function">
<dt id="_CPPv3N10TLS_Client15peer_cert_chainEv">
<span id="_CPPv2N10TLS_Client15peer_cert_chainEv"></span><span id="TLS_Client::peer_cert_chain"></span>std::vector&lt;X509_Certificate&gt; <code class="descname">peer_cert_chain</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv3N10TLS_Client15peer_cert_chainEv" title="Permalink to this definition">¶</a><br /></dt>
<dd></dd></dl>

<p>Returns the certificate chain of the server</p>
</dd></dl>

<p>A simple TLS client example:</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">&lt;botan/botan.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;botan/tls_client.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&quot;socket.h&quot;</span><span class="cp"></span>

<span class="k">using</span> <span class="k">namespace</span> <span class="n">Botan</span><span class="p">;</span>

<span class="cp">#include</span> <span class="cpf">&lt;stdio.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;string&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;memory&gt;</span><span class="cp"></span>

<span class="k">class</span> <span class="nc">Client_TLS_Policy</span> <span class="o">:</span> <span class="k">public</span> <span class="n">TLS_Policy</span>
   <span class="p">{</span>
   <span class="k">public</span><span class="o">:</span>
      <span class="kt">bool</span> <span class="n">check_cert</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">X509_Certificate</span><span class="o">&gt;&amp;</span> <span class="n">certs</span><span class="p">)</span> <span class="k">const</span>
         <span class="p">{</span>
         <span class="k">for</span><span class="p">(</span><span class="kt">size_t</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">!=</span> <span class="n">certs</span><span class="p">.</span><span class="n">size</span><span class="p">();</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span>
            <span class="p">{</span>
            <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">certs</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">to_string</span><span class="p">();</span>
            <span class="p">}</span>

         <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Warning: not checking cert signatures</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span>

         <span class="k">return</span> <span class="nb">true</span><span class="p">;</span>
         <span class="p">}</span>
   <span class="p">};</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span><span class="o">*</span> <span class="n">argv</span><span class="p">[])</span>
   <span class="p">{</span>
   <span class="k">if</span><span class="p">(</span><span class="n">argc</span> <span class="o">!=</span> <span class="mi">2</span> <span class="o">&amp;&amp;</span> <span class="n">argc</span> <span class="o">!=</span> <span class="mi">3</span><span class="p">)</span>
      <span class="p">{</span>
      <span class="n">printf</span><span class="p">(</span><span class="s">&quot;Usage: %s host [port]</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">]);</span>
      <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
      <span class="p">}</span>

   <span class="k">try</span>
      <span class="p">{</span>
      <span class="n">LibraryInitializer</span> <span class="n">botan_init</span><span class="p">;</span>

      <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">host</span> <span class="o">=</span> <span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">];</span>
      <span class="n">u32bit</span> <span class="n">port</span> <span class="o">=</span> <span class="n">argc</span> <span class="o">==</span> <span class="mi">3</span> <span class="o">?</span> <span class="n">Botan</span><span class="o">::</span><span class="n">to_u32bit</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span> <span class="o">:</span> <span class="mi">443</span><span class="p">;</span>

      <span class="n">printf</span><span class="p">(</span><span class="s">&quot;Connecting to %s:%d...</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">host</span><span class="p">.</span><span class="n">c_str</span><span class="p">(),</span> <span class="n">port</span><span class="p">);</span>

      <span class="n">SocketInitializer</span> <span class="n">socket_init</span><span class="p">;</span>

      <span class="n">Socket</span> <span class="n">sock</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">port</span><span class="p">);</span>

      <span class="n">AutoSeeded_RNG</span> <span class="n">rng</span><span class="p">;</span>

      <span class="n">Client_TLS_Policy</span> <span class="n">policy</span><span class="p">;</span>

      <span class="n">TLS_Client</span> <span class="n">tls</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">tr1</span><span class="o">::</span><span class="n">bind</span><span class="p">(</span><span class="o">&amp;</span><span class="n">Socket</span><span class="o">::</span><span class="n">read</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">tr1</span><span class="o">::</span><span class="n">ref</span><span class="p">(</span><span class="n">sock</span><span class="p">),</span> <span class="n">_1</span><span class="p">,</span> <span class="n">_2</span><span class="p">),</span>
                     <span class="n">std</span><span class="o">::</span><span class="n">tr1</span><span class="o">::</span><span class="n">bind</span><span class="p">(</span><span class="o">&amp;</span><span class="n">Socket</span><span class="o">::</span><span class="n">write</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">tr1</span><span class="o">::</span><span class="n">ref</span><span class="p">(</span><span class="n">sock</span><span class="p">),</span> <span class="n">_1</span><span class="p">,</span> <span class="n">_2</span><span class="p">),</span>
                     <span class="n">policy</span><span class="p">,</span> <span class="n">rng</span><span class="p">);</span>

      <span class="n">printf</span><span class="p">(</span><span class="s">&quot;Handshake extablished...</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>

<span class="cp">#if 0</span><span class="c"></span>
<span class="c">      std::string http_command = &quot;GET / HTTP/1.1\r\n&quot;</span>
<span class="c">                                 &quot;Server: &quot; + host + &#39;:&#39; + to_string(port) + &quot;\r\n\r\n&quot;;</span>
<span class="cp">#else</span>
      <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">http_command</span> <span class="o">=</span> <span class="s">&quot;GET / HTTP/1.0</span><span class="se">\r\n\r\n</span><span class="s">&quot;</span><span class="p">;</span>
<span class="cp">#endif</span>

      <span class="n">tls</span><span class="p">.</span><span class="n">write</span><span class="p">((</span><span class="k">const</span> <span class="n">Botan</span><span class="o">::</span><span class="n">byte</span><span class="o">*</span><span class="p">)</span><span class="n">http_command</span><span class="p">.</span><span class="n">c_str</span><span class="p">(),</span>
                <span class="n">http_command</span><span class="p">.</span><span class="n">length</span><span class="p">());</span>

      <span class="kt">size_t</span> <span class="n">total_got</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>

      <span class="k">while</span><span class="p">(</span><span class="nb">true</span><span class="p">)</span>
         <span class="p">{</span>
         <span class="k">if</span><span class="p">(</span><span class="n">tls</span><span class="p">.</span><span class="n">is_closed</span><span class="p">())</span>
            <span class="k">break</span><span class="p">;</span>

         <span class="n">Botan</span><span class="o">::</span><span class="n">byte</span> <span class="n">buf</span><span class="p">[</span><span class="mi">128</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span> <span class="mi">0</span> <span class="p">};</span>
         <span class="kt">size_t</span> <span class="n">got</span> <span class="o">=</span> <span class="n">tls</span><span class="p">.</span><span class="n">read</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">buf</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">);</span>
         <span class="n">printf</span><span class="p">(</span><span class="s">&quot;%s&quot;</span><span class="p">,</span> <span class="n">buf</span><span class="p">);</span>
         <span class="n">fflush</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>

         <span class="n">total_got</span> <span class="o">+=</span> <span class="n">got</span><span class="p">;</span>
         <span class="p">}</span>

      <span class="n">printf</span><span class="p">(</span><span class="s">&quot;</span><span class="se">\n</span><span class="s">Retrieved %d bytes total</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">total_got</span><span class="p">);</span>
   <span class="p">}</span>
   <span class="k">catch</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">exception</span><span class="o">&amp;</span> <span class="n">e</span><span class="p">)</span>
      <span class="p">{</span>
      <span class="n">printf</span><span class="p">(</span><span class="s">&quot;%s</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">e</span><span class="p">.</span><span class="n">what</span><span class="p">());</span>
      <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
      <span class="p">}</span>
   <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
   <span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="tls-servers">
<h2>TLS Servers<a class="headerlink" href="#tls-servers" title="Permalink to this headline">¶</a></h2>
<p>A simple TLS server</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">&lt;botan/botan.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;botan/tls_server.h&gt;</span><span class="cp"></span>

<span class="cp">#include</span> <span class="cpf">&lt;botan/rsa.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;botan/dsa.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;botan/x509self.h&gt;</span><span class="cp"></span>

<span class="cp">#include</span> <span class="cpf">&quot;socket.h&quot;</span><span class="cp"></span>

<span class="k">using</span> <span class="k">namespace</span> <span class="n">Botan</span><span class="p">;</span>

<span class="cp">#include</span> <span class="cpf">&lt;stdio.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;string&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;memory&gt;</span><span class="cp"></span>

<span class="k">class</span> <span class="nc">Server_TLS_Policy</span> <span class="o">:</span> <span class="k">public</span> <span class="n">TLS_Policy</span>
   <span class="p">{</span>
   <span class="k">public</span><span class="o">:</span>
      <span class="kt">bool</span> <span class="n">check_cert</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">X509_Certificate</span><span class="o">&gt;&amp;</span> <span class="n">certs</span><span class="p">)</span> <span class="k">const</span>
         <span class="p">{</span>
         <span class="k">for</span><span class="p">(</span><span class="kt">size_t</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">!=</span> <span class="n">certs</span><span class="p">.</span><span class="n">size</span><span class="p">();</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span>
            <span class="p">{</span>
            <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">certs</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">to_string</span><span class="p">();</span>
            <span class="p">}</span>

         <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Warning: not checking cert signatures</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span>

         <span class="k">return</span> <span class="nb">true</span><span class="p">;</span>
         <span class="p">}</span>
   <span class="p">};</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span><span class="o">*</span> <span class="n">argv</span><span class="p">[])</span>
   <span class="p">{</span>
   <span class="kt">int</span> <span class="n">port</span> <span class="o">=</span> <span class="mi">4433</span><span class="p">;</span>

   <span class="k">if</span><span class="p">(</span><span class="n">argc</span> <span class="o">==</span> <span class="mi">2</span><span class="p">)</span>
      <span class="n">port</span> <span class="o">=</span> <span class="n">to_u32bit</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]);</span>

   <span class="k">try</span>
      <span class="p">{</span>
      <span class="n">LibraryInitializer</span> <span class="n">botan_init</span><span class="p">;</span>
      <span class="n">SocketInitializer</span> <span class="n">socket_init</span><span class="p">;</span>

      <span class="n">AutoSeeded_RNG</span> <span class="n">rng</span><span class="p">;</span>

      <span class="c1">//RSA_PrivateKey key(rng, 1024);</span>
      <span class="n">DSA_PrivateKey</span> <span class="n">key</span><span class="p">(</span><span class="n">rng</span><span class="p">,</span> <span class="n">DL_Group</span><span class="p">(</span><span class="s">&quot;dsa/jce/1024&quot;</span><span class="p">));</span>

      <span class="n">X509_Cert_Options</span> <span class="n">options</span><span class="p">(</span>
         <span class="s">&quot;localhost/US/Syn Ack Labs/Mathematical Munitions Dept&quot;</span><span class="p">);</span>

      <span class="n">X509_Certificate</span> <span class="n">cert</span> <span class="o">=</span>
         <span class="n">X509</span><span class="o">::</span><span class="n">create_self_signed_cert</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="s">&quot;SHA-1&quot;</span><span class="p">,</span> <span class="n">rng</span><span class="p">);</span>

      <span class="n">Server_Socket</span> <span class="n">listener</span><span class="p">(</span><span class="n">port</span><span class="p">);</span>

      <span class="n">Server_TLS_Policy</span> <span class="n">policy</span><span class="p">;</span>

      <span class="k">while</span><span class="p">(</span><span class="nb">true</span><span class="p">)</span>
         <span class="p">{</span>
         <span class="k">try</span> <span class="p">{</span>
            <span class="n">printf</span><span class="p">(</span><span class="s">&quot;Listening for new connection on port %d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">port</span><span class="p">);</span>

            <span class="n">Socket</span><span class="o">*</span> <span class="n">sock</span> <span class="o">=</span> <span class="n">listener</span><span class="p">.</span><span class="n">accept</span><span class="p">();</span>

            <span class="n">printf</span><span class="p">(</span><span class="s">&quot;Got new connection</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>

            <span class="n">TLS_Server</span> <span class="n">tls</span><span class="p">(</span>
              <span class="n">std</span><span class="o">::</span><span class="n">tr1</span><span class="o">::</span><span class="n">bind</span><span class="p">(</span><span class="o">&amp;</span><span class="n">Socket</span><span class="o">::</span><span class="n">read</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">tr1</span><span class="o">::</span><span class="n">ref</span><span class="p">(</span><span class="n">sock</span><span class="p">),</span> <span class="n">_1</span><span class="p">,</span> <span class="n">_2</span><span class="p">),</span>
              <span class="n">std</span><span class="o">::</span><span class="n">tr1</span><span class="o">::</span><span class="n">bind</span><span class="p">(</span><span class="o">&amp;</span><span class="n">Socket</span><span class="o">::</span><span class="n">write</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">tr1</span><span class="o">::</span><span class="n">ref</span><span class="p">(</span><span class="n">sock</span><span class="p">),</span> <span class="n">_1</span><span class="p">,</span> <span class="n">_2</span><span class="p">),</span>
              <span class="n">policy</span><span class="p">,</span>
              <span class="n">rng</span><span class="p">,</span>
              <span class="n">cert</span><span class="p">,</span>
              <span class="n">key</span><span class="p">);</span>

            <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">hostname</span> <span class="o">=</span> <span class="n">tls</span><span class="p">.</span><span class="n">requested_hostname</span><span class="p">();</span>

            <span class="k">if</span><span class="p">(</span><span class="n">hostname</span> <span class="o">!=</span> <span class="s">&quot;&quot;</span><span class="p">)</span>
               <span class="n">printf</span><span class="p">(</span><span class="s">&quot;Client requested host &#39;%s&#39;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">hostname</span><span class="p">.</span><span class="n">c_str</span><span class="p">());</span>

            <span class="n">printf</span><span class="p">(</span><span class="s">&quot;Writing some text</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>

            <span class="kt">char</span> <span class="n">msg</span><span class="p">[]</span> <span class="o">=</span> <span class="s">&quot;Foo</span><span class="se">\n</span><span class="s">Bar</span><span class="se">\n</span><span class="s">Baz</span><span class="se">\n</span><span class="s">Quux</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span>
            <span class="n">tls</span><span class="p">.</span><span class="n">write</span><span class="p">((</span><span class="k">const</span> <span class="n">Botan</span><span class="o">::</span><span class="n">byte</span><span class="o">*</span><span class="p">)</span><span class="n">msg</span><span class="p">,</span> <span class="n">strlen</span><span class="p">(</span><span class="n">msg</span><span class="p">));</span>

            <span class="n">printf</span><span class="p">(</span><span class="s">&quot;Now trying a read...</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>

            <span class="kt">char</span> <span class="n">buf</span><span class="p">[</span><span class="mi">1024</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span> <span class="mi">0</span> <span class="p">};</span>
            <span class="n">u32bit</span> <span class="n">got</span> <span class="o">=</span> <span class="n">tls</span><span class="p">.</span><span class="n">read</span><span class="p">((</span><span class="n">Botan</span><span class="o">::</span><span class="n">byte</span><span class="o">*</span><span class="p">)</span><span class="n">buf</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">buf</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">);</span>
            <span class="n">printf</span><span class="p">(</span><span class="s">&quot;%d: &#39;%s&#39;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">got</span><span class="p">,</span> <span class="n">buf</span><span class="p">);</span>

            <span class="n">tls</span><span class="p">.</span><span class="n">close</span><span class="p">();</span>
            <span class="p">}</span>
         <span class="k">catch</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">exception</span><span class="o">&amp;</span> <span class="n">e</span><span class="p">)</span> <span class="p">{</span> <span class="n">printf</span><span class="p">(</span><span class="s">&quot;%s</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">e</span><span class="p">.</span><span class="n">what</span><span class="p">());</span> <span class="p">}</span>
         <span class="p">}</span>
   <span class="p">}</span>
   <span class="k">catch</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">exception</span><span class="o">&amp;</span> <span class="n">e</span><span class="p">)</span>
      <span class="p">{</span>
      <span class="n">printf</span><span class="p">(</span><span class="s">&quot;%s</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">e</span><span class="p">.</span><span class="n">what</span><span class="p">());</span>
      <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
      <span class="p">}</span>
   <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
   <span class="p">}</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
        </div>
        <div class="sidebar">
          <h3>Table of Contents</h3>
          <ul class="current">
<li class="toctree-l1"><a class="reference internal" href="index.html">Welcome</a></li>
<li class="toctree-l1"><a class="reference internal" href="reading.html">Recommended Reading</a></li>
<li class="toctree-l1"><a class="reference internal" href="building.html">Building The Library</a></li>
<li class="toctree-l1"><a class="reference internal" href="firststep.html">Getting Started</a></li>
<li class="toctree-l1"><a class="reference internal" href="filters.html">Information Flow: Pipes and Filters</a></li>
<li class="toctree-l1"><a class="reference internal" href="pubkey.html">Public Key Cryptography</a></li>
<li class="toctree-l1"><a class="reference internal" href="x509.html">Certificate Handling</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">SSL and TLS</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#tls-clients">TLS Clients</a></li>
<li class="toctree-l2"><a class="reference internal" href="#tls-servers">TLS Servers</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="bigint.html">BigInt</a></li>
<li class="toctree-l1"><a class="reference internal" href="lowlevel.html">The Low-Level Interface</a></li>
<li class="toctree-l1"><a class="reference internal" href="secmem.html">Secure Memory Containers</a></li>
<li class="toctree-l1"><a class="reference internal" href="kdf.html">Key Derivation Functions</a></li>
<li class="toctree-l1"><a class="reference internal" href="pbkdf.html">PBKDF Algorithms</a></li>
<li class="toctree-l1"><a class="reference internal" href="passhash.html">Password Hashing</a></li>
<li class="toctree-l1"><a class="reference internal" href="rng.html">Random Number Generators</a></li>
<li class="toctree-l1"><a class="reference internal" href="fpe.html">Format Preserving Encryption</a></li>
<li class="toctree-l1"><a class="reference internal" href="python.html">Python Binding</a></li>
</ul>

          <div role="search">
            <h3 style="margin-top: 1.5em;">Search</h3>
            <form class="search" action="search.html" method="get">
                <input type="text" name="q" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
            </form>
          </div>
        </div>
        <div class="clearer"></div>
      </div>
    </div>

    <div class="footer-wrapper">
      <div class="footer">
        <div class="left">
          <div role="navigation" aria-label="related navigaton">
            <a href="x509.html" title="Certificate Handling"
              accesskey="P">previous</a> |
            <a href="bigint.html" title="BigInt"
              accesskey="N">next</a> |
            <a href="genindex.html" title="General Index"
              accesskey="I">index</a>
          </div>
          <div role="note" aria-label="source link">
              <br/>
              <a href="_sources/ssl.txt"
                rel="nofollow">Show Source</a>
          </div>
        </div>

        <div class="right">
          
    <div class="footer" role="contentinfo">
        &#169; Copyright 2000-2011, Jack Lloyd.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.8.3.
    </div>
        </div>
        <div class="clearer"></div>
      </div>
    </div>

  </body>
</html>