<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[wolfSSL - Embedded SSL Library — Retrieving the X509 structure of a peer certificate with WolfSSL]]></title>
		<link>https://www.wolfssl.com/forums/topic747-retrieving-the-x509-structure-of-a-peer-certificate-with-wolfssl.html</link>
		<atom:link href="https://www.wolfssl.com/forums/feed-rss-topic747.xml" rel="self" type="application/rss+xml" />
		<description><![CDATA[The most recent posts in Retrieving the X509 structure of a peer certificate with WolfSSL.]]></description>
		<lastBuildDate>Wed, 02 Dec 2015 22:29:33 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: Retrieving the X509 structure of a peer certificate with WolfSSL]]></title>
			<link>https://www.wolfssl.com/forums/post2352.html#p2352</link>
			<description><![CDATA[<p>I have tried to define KEEP_PEER_CERT as well as running the configure script with --enable-sep but I am still unable to retrieve the peer certificate <img src="https://www.wolfssl.com/forums/img/smilies/sad.png" width="15" height="15" alt="sad" /></p><p>Every once in a while the application gets &#039;lucky&#039; and can retrieve the cert (usually after a clean build) but most of the time SSL_get_peer_certificate() still returns a NULL pointer.</p>]]></description>
			<author><![CDATA[null@example.com (alex_b)]]></author>
			<pubDate>Wed, 02 Dec 2015 22:29:33 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post2352.html#p2352</guid>
		</item>
		<item>
			<title><![CDATA[Re: Retrieving the X509 structure of a peer certificate with WolfSSL]]></title>
			<link>https://www.wolfssl.com/forums/post2351.html#p2351</link>
			<description><![CDATA[<p>Hello chrisc,</p><p>As always, your reply is very much appreciated and very helpful indeed. I just knew the peer cert was discarded to optimize memory usage, I just didn&#039;t know how to hang on to it.&nbsp; <img src="https://www.wolfssl.com/forums/img/smilies/smile.png" width="15" height="15" alt="smile" /></p><p>FYI, My application creates it&#039;s own peer certificates and currently stores the name of the user as the subject CN. After a successful SSL/TLS handshake this information is extracted from the peer certificate using </p><div class="codebox"><pre><code>char *peer = X509_NAME_oneline(X509_get_subject_name(x509), nullptr, 0);</code></pre></div><p>My next challenge is to extract the other information I need from the certificate.</p><p>I want to store the seed key for a one-time-password generator in the certificate (encrypted using the public key generated from the private key used with that peer certificate).<br />A have already found a way to store that data in the certificate (as part of the &#039;X509v3 Subject Alternative Name&#039;) like this:<br /></p><div class="codebox"><pre><code>$ openssl x509 -noout -text -in peer.pem

Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number: 109881812884 (0x1995776794)
    Signature Algorithm: sha256WithRSAEncryption
        Issuer: O=openGalaxy, OU=openGalaxy websocket interface, CN=openGalaxyCA/emailAddress=&lt;empty&gt;
        Validity
            Not Before: Dec  2 18:36:53 2015 GMT
            Not After : Dec  1 18:36:53 2016 GMT
        Subject: O=openGalaxy, OU=openGalaxy websocket interface, CN=NAME SURNAME/emailAddress=EMAIL-ADDRESS
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                Public-Key: (2048 bit)
                Modulus:
                    ...
                Exponent: 65537 (0x10001)
        X509v3 extensions:
            X509v3 Subject Key Identifier: 
                BA:33:4D:3C:60:14:B7:5F:9A:AA:A0:E6:8F:39:7F:10:23:9D:9C:C2
            X509v3 Authority Key Identifier: 
                keyid:CF:F9:12:EF:25:C4:D6:D7:68:E6:C1:4B:86:0F:C3:19:D8:9C:DC:66

            X509v3 Basic Constraints: 
                CA:FALSE
            X509v3 Key Usage: 
                Digital Signature, Non Repudiation, Key Encipherment, Data Encipherment
            X509v3 Extended Key Usage: 
                TLS Web Client Authentication
            X509v3 Subject Alternative Name: 
                DirName:/CN=ENCRYPTED OTP SEED
            X509v3 Issuer Alternative Name: 
                email:&lt;empty&gt;
    Signature Algorithm: sha256WithRSAEncryption
         ...</code></pre></div><p>My goal is to get and decrypt the otp seed from the peer certificate and then use it to generate a one-time-password the user of the certificate would have to match (possibly using http basic authentication once the SSL/TLS connection has been established). I have however not looked into retrieving or decrypting the otp seed from the peer certificate using wolfSSL library functions yet...</p>]]></description>
			<author><![CDATA[null@example.com (alex_b)]]></author>
			<pubDate>Wed, 02 Dec 2015 19:05:41 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post2351.html#p2351</guid>
		</item>
		<item>
			<title><![CDATA[Re: Retrieving the X509 structure of a peer certificate with WolfSSL]]></title>
			<link>https://www.wolfssl.com/forums/post2349.html#p2349</link>
			<description><![CDATA[<p>Hi alex_b,</p><p>If you would like wolfSSL to hang on to the peer certificate after the SSL/TLS handshake, you can define <strong>KEEP_PEER_CERT</strong> when compiling wolfSSL.</p><p>With wolfSSL, by default the verify callback is only called upon verification failure.&nbsp; wolfSSL handles certificate verification internally, which is unlike OpenSSL that forces the user to do the verification.&nbsp; This means that on all failure cases, the preverify parameter will be zero.&nbsp; </p><p>Optionally, you can force wolfSSL to call the verify callback on every verification - regardless if it is successful or a failure - by defining <strong>WOLFSSL_ALWAYS_VERIFY_CB</strong>.&nbsp; In this case, if preverify is equal to &quot;1&quot;, wolfSSL has already successfully verified the peer certificate.&nbsp; We provide this option for those users who wish to do custom inspection of certificate elements past normal certificate verification measures.</p><p>Does this help clear things up?</p><p>Best Regards,<br />Chris</p>]]></description>
			<author><![CDATA[null@example.com (chrisc)]]></author>
			<pubDate>Tue, 01 Dec 2015 22:28:54 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post2349.html#p2349</guid>
		</item>
		<item>
			<title><![CDATA[Retrieving the X509 structure of a peer certificate with WolfSSL]]></title>
			<link>https://www.wolfssl.com/forums/post2347.html#p2347</link>
			<description><![CDATA[<p>I have a couple of questions about WolfSSL and verifying peer certificates:</p><p>My applications calls </p><div class="codebox"><pre><code>SSL_CTX_set_verify()</code></pre></div><p> with </p><div class="codebox"><pre><code>SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT</code></pre></div><p>And it also wants to retrieve the X509 structure of each client&#039;s peer certificate in order to use information stored in it.</p><p>When I use OpenSSL my application does this by calling </p><div class="codebox"><pre><code>SSL_get_peer_certificate()</code></pre></div><p> right after the connection has been established.</p><p>But when I use this function with WolfSSL I run into difficulties.<br />With WolfSSL the SSL_get_peer_certificate() function allways returns a NULL pointer, unless it is called from within </p><div class="codebox"><pre><code>int verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx);</code></pre></div><p>The problem I have with this is that from within verify_callback() I have no meaningfull place to store the information retrieved from any X509 structure.<br />Thus, I need to call SSL_get_peer_certificate() after the connection has been established and not from within verify_callback().</p><p>Is it possible to retrieve the peer certificate X509 structure after the connection has been established?<br />(Or is this information discarded by WolfSSL after verify_callback() completes, to save memory?)</p><p>My second question also relates to the process of verifiying a clients peer certificate.<br />Do I need to do extra verification when verify_callback() is called with preverify_ok=1 ?</p><p>With openSSL my application checks that SSL_get_verify_result() returns a value of X509_V_OK, but I have noticed that WolfSSL always returns X509_V_OK when you call this function (hardcoded into the C header files).</p><p>Just to be sure, my application uses a certificate manager to verify the peer certificate (again) like this:</p><div class="codebox"><pre><code>     int n = 1; // Assume the peer certificate is invalid or could not be authenticated
     X509 * x509 = wolfSSL_get_peer_certificate(ssl);
     if(x509){
       WOLFSSL_CERT_MANAGER* cm = wolfSSL_CertManagerNew();
       if(cm != nullptr){
         if(wolfSSL_CertManagerLoadCA(cm, &quot;ca.pem&quot;, nullptr) == SSL_SUCCESS){ // load the CA cert into the CM
           if(wolfSSL_CertManagerSetCRL_Cb(cm, MissingCRL) == SSL_SUCCESS){ // cb to call when the CRL cert is missing
             if(wolfSSL_CertManagerLoadCRL(cm, &quot;/path/to/crl&quot;, SSL_FILETYPE_PEM, 0) == SSL_SUCCESS){ // load the CRL path into the CM
               if(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL) == SSL_SUCCESS){ // enable CRL checking for this CM
                 const unsigned char *der;
                 int der_length, verify_ok;
                 der = wolfSSL_X509_get_der(x509, &amp;der_length); // get peer X509 certificate in DER format
                 verify_ok = wolfSSL_CertManagerVerifyBuffer(cm, der, der_length, SSL_FILETYPE_ASN1); // verify it using the CM
                 if(verify_ok == SSL_SUCCESS){
                   n = 0; // Allow the connection to continue.
                 }
                 else {
                   n = 1; // Block the connection.
                 }
               }
             }
           }
         }
         wolfSSL_CertManagerFree(cm);
       }
     }</code></pre></div><p>Is this nessesary or can I trust the hardcoded X509_V_OK that SSL_get_verify_result() returns.</p>]]></description>
			<author><![CDATA[null@example.com (alex_b)]]></author>
			<pubDate>Sun, 29 Nov 2015 02:14:45 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post2347.html#p2347</guid>
		</item>
	</channel>
</rss>
