<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: rdemirkoparan</title>
    <description>The latest articles on DEV Community by rdemirkoparan (@rdemirkoparan).</description>
    <link>https://dev.to/rdemirkoparan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F690219%2F2ffb54ff-7e2a-4cf6-9f96-df5d3b057abf.png</url>
      <title>DEV Community: rdemirkoparan</title>
      <link>https://dev.to/rdemirkoparan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rdemirkoparan"/>
    <language>en</language>
    <item>
      <title>Authentication Using Apache HTTPD</title>
      <dc:creator>rdemirkoparan</dc:creator>
      <pubDate>Wed, 25 Aug 2021 07:51:39 +0000</pubDate>
      <link>https://dev.to/rdemirkoparan/authentication-using-apache-httpd-2imo</link>
      <guid>https://dev.to/rdemirkoparan/authentication-using-apache-httpd-2imo</guid>
      <description>&lt;h2&gt;
  
  
  Table Of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Standard Authentication vs Mutual Authentication&lt;/li&gt;
&lt;li&gt;Standard (One-Way) SSL Authentication&lt;/li&gt;
&lt;li&gt;Two-way (mutual) SSL Authentication&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this short article authentication of a client using Apache HTTPD will be described.&lt;/p&gt;

&lt;h1&gt;
  
  
  Standard Authentication vs Mutual Authentication &lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;Standard authentication (also known as one-way SSL authentication) is an authentication protocol in which only the client verifies the server certificate. Mutual authentication (two-way SSL authentication), on the other hand, is the authentication protocol in which two parties authenticate each other. It is the default mode of authentication in some protocols (such as IKE, SSH) and optional in some others (such as TLS).&lt;/p&gt;

&lt;h1&gt;
  
  
  Standard (One-Way) SSL Authentication &lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Server Side
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Requirements
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;An Apache HTTPD installation with mod_ssl&lt;/li&gt;
&lt;li&gt;Self-signed or CA signed server certificate&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Preparing the Certificate
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create root certificate

&lt;ul&gt;
&lt;li&gt;openssl req -new -x509 -extensions v3_ca -keyout cakey.key -out cacert.pem&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Create server key and sign request

&lt;ul&gt;
&lt;li&gt;openssl req -new -nodes -out server-req.pem -keyout server-key.key&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Self sign the certificate

&lt;ul&gt;
&lt;li&gt;openssl ca -out server-cert.pem -infiles server-req.pem
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Here, only a summary of certificate preparation steps provided. You can find many detailed documents about this process on the internet. Note that self-signed certificates are only for test purposes. Using them at production is discouraged.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Apache HTTPD Implementation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Configure "ssl.conf" file and add the following lines at the end of the "VirtualHost" section.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# SSL CONFIGURATION – SERVER SIDE
# Enable the single way SSL authentication
SSLEngine on

# Apache client CA certificate
SSLCertificateFile "/etc/httpd/conf/server-cert.pem"

# Apache client CA certificate private key file
SSLCertificateKeyFile  "/etc/httpd/conf/server-key.key"

# END OF SSL CONFIGURATION – SERVER SIDE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Restart apache HTTPD for changes to be applied

&lt;ul&gt;
&lt;li&gt;systemctl restart httpd&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Client-Side (Proxy)
&lt;/h2&gt;

&lt;p&gt;There is no special action to be taken at the client-side.&lt;/p&gt;

&lt;h1&gt;
  
  
  Two-way (mutual) SSL Authentication &lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Server Side
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Requirements
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;An Apache HTTPD installation with mod_ssl&lt;/li&gt;
&lt;li&gt;Self-Signed or CA Signed server certificate&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Prepare Certificate
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create root certificate

&lt;ul&gt;
&lt;li&gt;openssl req -new -x509 -extensions v3_ca -keyout cakey.key -out cacert.pem&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Create server key and sign request

&lt;ul&gt;
&lt;li&gt;openssl req -new -nodes -out server-req.pem -keyout server-key.key&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Self sign the certificate

&lt;ul&gt;
&lt;li&gt;openssl ca -out server-cert.pem -infiles server-req.pem
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Here, only a summary of certificate preparation steps provided. You can find many detailed documents about this process on the internet. Note that self-signed certificates are only for test purposes. Using them at production is discouraged.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Apache HTTPD Implementation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Configure "ssl.conf" file and add the following lines at the end of the "VirtualHost" section.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# SSL CONFIGURATION – SERVER SIDE
# Enable the single way SSL authentication
SSLEngine on

# Apache client CA certificate
SSLCertificateFile "/etc/httpd/conf/server-cert.pem"

# Apache client CA certificate private key file
SSLCertificateKeyFile  "/etc/httpd/conf/server-key.key"

# END OF SSL CONFIGURATION – SERVER SIDE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Restart apache HTTPD for changes to be applied

&lt;ul&gt;
&lt;li&gt;systemctl restart httpd&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Client-Side (Proxy)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Requirements
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;An Apache HTTPD installation with mod_ssl and mod_proxy&lt;/li&gt;
&lt;li&gt;Self-Signed or CA Signed certificate from the server&lt;/li&gt;
&lt;li&gt;Self-Signed or CA Signed certificate for client&lt;/li&gt;
&lt;li&gt;CN of the server certificate&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Prepare Certificates
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Generate key longer than or equal to 2048 bits

&lt;ul&gt;
&lt;li&gt;openssl genrsa -aes256 -out partner-domain.key 2048
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CN is very important here. Check the sample below. CN is provided by the server as 'partner-domain'.

Country Name (2 letter code) [XX]:TR
State or Province Name (full name) []:Marmara
Locality Name (eg, city) [Default City]:Istanbul
Organization Name (eg, company) [Default Company Ltd]:Telenity
Organizational Unit Name (eg, section) []:Telenity
Common Name (eg, your name or your server's hostname) []:partner-domain
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Generate certificate sign request

&lt;ul&gt;
&lt;li&gt;openssl req -key partner-domain.key -new -sha256 -out partner-domain.csr&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Convert key to RSA key

&lt;ul&gt;
&lt;li&gt;openssl rsa -in partner-domain.key -outform pem &amp;gt; partner-domain-rsa.key
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Apache HTTPD only supports keys encoded in PKCS1 RSA, DSA or EC formats. Keys encoded in PKCS8 format (ie. starting with "-----BEGIN PRIVATE KEY-----") must be converted to a supported format.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Sign the certificate

&lt;ul&gt;
&lt;li&gt;Self-Sign: openssl x509 -signkey partner-domain.key -in partner-domain.csr -req -out partner-domain.crt&lt;/li&gt;
&lt;li&gt;CA-Sign: Send CSR file to the Authentication server provider.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Apache HTTPD Implementation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Merge the key and the certificate received from server

&lt;ul&gt;
&lt;li&gt;cat partner-domain-rsa.key partner-domain.ca &amp;gt; partner-domain-includekey.pem
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The order of the key and the certificate does not matter. Two restrictions are

1. RSA key must be used
2. The merged file name must denote that it contains both the key and the certificate.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Configure "ssl.conf" file and add the following lines at the end of the "VirtualHost" section.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# SSL CONFIGURATION – CLIENT SIDE
# Enable SSL Client on this virtualhost (the traffic to the backends can be encrypted)
SSLProxyEngine on

# It’s mandatory for apache to authenticate the backends’ certificate.
SSLProxyVerify require

# Specify the depth of the check if the certificate has an CA approval
SSLVerifyDepth  10

# If CN and hostname will not match below configs must be off. Default values are on
SSLProxyCheckPeerCN off
SSLProxyCheckPeerName off

# Apache client CA certificate (certificate of who released your client certificate)
SSLProxyMachineCertificateFile "/etc/httpd/conf/partner-domain-includekey.pem"
# Backends’ CA certificates (list of certificates of who released your backends’ certificates)
SSLProxyCACertificateFile "/etc/httpd/conf/thirdparty-cert-provided-by-server.cert.pem"

# END OF SSL CONFIGURATION – CLIENT SIDE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Refer to the official Apache documentation at https://httpd.apache.org/docs/current/mod/mod_ssl.html.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add the related proxy definition inside "proxy.conf" file
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ProxyPass        /secureendpoint https://api.partner-domain/secureapi
ProxyPassReverse /secureendpoint https://api.partner-domain/secureapi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Restart apache HTTPD for changes to be applied

&lt;ul&gt;
&lt;li&gt;systemctl restart httpd&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
  </channel>
</rss>
