个人工具

UbuntuHelp:OpenSSL

来自Ubuntu中文

Oneleaf讨论 | 贡献2007年5月13日 (日) 11:35的版本 (New page: {{From|https://help.ubuntu.com/community/OpenSSL}} {{Languages|php5}} === Introduction === This guide is designed to introduce the reader to the Secure Sockets Layer (SSL) application-le...)

(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航, 搜索



Introduction

This guide is designed to introduce the reader to the Secure Sockets Layer (SSL) application-level protocol, and particularly the OpenSSL implementation of SSL. After a brief description of exactly what OpenSSL is, and what it is useful for, the guide will further illustrate the practical usage of OpenSSL in a client-server environment, and provide specific examples of applications which may benefit from OpenSSL. Finally, the guide will lead the user through example procedures required to use OpenSSL with the popular Apache Hyper Text Transport Protocol (HTTP) server for the purpose of serving secured web pages from your Ubuntu computer.

Target Audience

To properly implement the practical steps found in this guide, the reader should be a user of Ubuntu who is comfortable with the use of command-line applications, using the Bourne Again SHell (bash) environment, and editing system configuration files with their preferred text editor application. While previous familiarity with Server Sockets Layer (SSL), or the OpenSSL implementation in particular, is not required for this guide, if desired, the reader is advised to pursue further learning from the resources listed in the Resources section of this guide in order to broaden his/her understanding of this powerful security layer.

About OpenSSL

Secure Sockets Layer is an application-level protocol which was developed by the Netscape Corporation for the purpose of transmitting sensitive information, such as Credit Card details via the Internet. SSL works by using a private key to encrypt data transferred over the SSL-enabled connection, thus thwarting eavesdropping of the information. The most popular use of SSL is in conjunction with web browsing with HTTP, but many network applications can benefit from using SSL. By convention, URLs that require an SSL connection start with https: instead of http:.

OpenSSL is a robust, commercial-grade implementation of SSL tools, and related general purpose library based upon SSLeay, developed by Eric A. Young and Tim J. Hudson. OpenSSL is available as an Open Source equivalent to commercial implementations of SSL via an Apache-style license.

About X.509

X.509 is a specification for digital certificates published by the International Telecommunications Union - Telecommunication (ITU-T). It specifies information and attributes required for the identification of a person or a computer system, and is used for secure management and distribution of digitally signed certificates across secure Internet networks. OpenSSL most commonly uses X.509 certificates.

Practical OpenSSL Usage

Installing OpenSSL

Some Ubuntu packages install OpenSSL as a dependency, but if you would like to install OpenSSL initially, or the libraries use the following apt-get command sequences at a terminal prompt:

Installing OpenSSL Toolkit

To install the OpenSSL binary toolkit, install the following packages openssl (see InstallingSoftware).

Installing OpenSSL Library

To install the OpenSSL general-purpose library, first determine the applicable version of the library available for your Ubuntu computer with the following command issued at a terminal prompt:

apt-cache search libssl | grep SSL

You should observe output from the command similar to the following:

libssl0.9.6 - SSL shared libraries (old version)
libssl-dev - SSL development libraries, header files and documentation
libssl0.9.7 - SSL shared libraries
</code>

In the above example, you would most likely want to install the current OpenSSL library, which appears in the output as <code>libssl0.9.7</code>.  Install the following packages <code>libssl0.9.7</code> (see InstallingSoftware).

===== Installing OpenSSL Development Library =====
In order to build software which requires the OpenSSL general-purpose library, you must first install the development instance of the OpenSSL library. Install the following packages <code>libssl-dev</code> (see InstallingSoftware).

==== Basic OpenSSL Commands ====
The following section of the guide presents some of the more common basic commands, and parameters to commands which are part of the OpenSSL toolkit.  For additional information, read the various OpenSSL system manual pages with the <code>man</code> command, and refer to the information presented in the '''Resources''' section of this guide.

Determine installed OpenSSL version:

<code>openssl version</code>

List of available OpenSSL sub-commands:

<code>openssl help</code>

Get additional help information on OpenSSL sub-commands by using the <code>openssl</code> command followed by the sub-command, and the <code>-h</code> switch. For example, to get additional information on the <code>openssl enc</code> sub-command:

<code>openssl enc -h</code>

List all available cipher algorithms:

<code>openssl ciphers -v</code>

You may benchmark your computer's speed with OpenSSL, measuring how many bytes per second can be processed for each algorithm, and the times needed for sign/verify cycles by using the following command:

<code>openssl speed</code>

=== SSL Certificates ===
The following sections of this guide will introduce the concepts involved in the generation and use of SSL certificates, both the self-signed variety, and those signed by a recognized certificate authority for use with a server application supporting SSL, and the use of X.509 certificates in client applications.

==== SSL Certificates for Server Use ====
Once you have properly generated an X.509-compliant SSL certificate, you may either elect to sign the certificate yourself, by generating a Certificate Authority (CA), or you may opt to have a globally recognized Certificate Authority sign the certificate.  When the certificate is signed, it is then ready to be used with the OpenSSL toolkit, or the library to enable encrypted SSL connections to a Lightweight Directory Access Protocol, (LDAP) or Hyper Text Transport Protocol (HTTP) server, for example. This section of the guide describes the certificate generation, and signing process for both self-signed, and recognized CA-signed certificates.

===== Generating and Signing Self-Signed Certificates =====
Self-signed certificates have a major advantage in that they are completely free to use, and may be generated, signed, and used on an as-needed basis.  Self-signed certificates are great for use in closed lab environments, or for testings purposes.  One of the drawbacks of using self-signed certificates however, is that warnings through web browser, and other applications will be generated when accessing servers using SSL with self-signed certificates because the certificates are not signed using a globally recognized, and trusted Certificate Authority, but importing the server's root certificate into the client application, if possible, will squelch these warnings as demonstrated later in this guide.  Using self-signed certificates in a publicly-accessible, production environment is not recommended due to the implicit trust issues arising from these warnings, and potential confusion to users.  

attachment:IconsPage/IconNote.png '''NOTE:''' You must obtain a certificate signed by a recognized Certificate Authority in order to establish a commercial site, e.g. for conducting "e-commerce".
 
Provided you've installed the OpenSSL toolkit previously, or per instructions above, the generation of X.509 SSL certificates is quite simple.  For self-signed certificates, you must first establish a Certificate Authority (CA) by following the steps below:

====== Creating the Certificate Authority ======
First, create an initial working environment, for example within your home directory by issuing the following command from a terminal prompt:

<pre>
cd && mkdir -p myCA/signedcerts && mkdir myCA/private && cd myCA
</code>

The above command will place you in a newly-created subdirectory of your home directory named ''CA'' and within this subdirectory, you should have two additional subdirectories, named <code>signedcerts</code> and <code>private</code>.

Within this initial working environment, the significance of the subdirectories, and their contents is as follows:

* <code>~/myCA</code> : contains CA certificate, certificates database, generated certificates, keys, and requests
* <code>~/myCA/signedcerts</code> : contains copies of each signed certificate 
* <code>~/myCA/private</code> : contains the private key.  
 
Next, create an initial certificate database in the <code>~/CA</code> subdirectory with the following command at a terminal prompt:

<code>echo '01' > serial  && touch index.txt</code>

Now create an initial <code>caconfig.cnf</code> file suitable for the creation of CA certificates.  Using your favorite editor, edit the file <code>~/myCA/caconfig.cnf</code>, and insert the following content into the file:

<pre>
# My sample caconfig.cnf file.
#
# Default configuration to use  when one
# is not provided on the command line.
#
[ ca ]
default_ca	= local_ca
#
#
# Default location  of  directories  and
# files needed to generate certificates.
#
[ local_ca ]
dir             = /home/<username>/myCA
certificate     = $dir/cacert.pem
database        = $dir/index.txt
new_certs_dir   = $dir/signedcerts
private_key     = $dir/private/cakey.pem
serial          = $dir/serial
#	
#
# Default   expiration   and  encryption
# policies for certificates.
#
default_crl_days        = 365
default_days            = 1825
default_md              = md5
#	
policy          = local_ca_policy
x509_extensions = local_ca_extensions
#	
#
# Default policy to use  when generating
# server   certificates.  The  following
# fields  must  be defined in the server
# certificate.
#
[ local_ca_policy ]
commonName              = supplied
stateOrProvinceName     = supplied
countryName             = supplied
emailAddress            = supplied
organizationName        = supplied
organizationalUnitName  = supplied
#	
#
# x509 extensions to use when generating
# server certificates.
#
[ local_ca_extensions ]
subjectAltName          = DNS:alt.tradeshowhell.com
basicConstraints        = CA:false
nsCertType              = server
#	
#
# The default root certificate
# generation policy.
#
[ req ]
default_bits    = 2048
default_keyfile = /home/<username>/myCA/private/cakey.pem
default_md      = md5
#	
prompt                  = no
distinguished_name      = root_ca_distinguished_name
x509_extensions         = root_ca_extensions
#
#
# Root  Certificate  Authority   distinguished 
# name.  Change these fields to match
# your local environment!
#
[ root_ca_distinguished_name ]
commonName              = MyOwn Root Certificate Authority
stateOrProvinceName     = NC
countryName             = US
emailAddress            = root@tradeshowhell.com
organizationName        = Trade Show Hell
#	
[ root_ca_extensions ]
basicConstraints        = CA:true
</code>

attachment:IconsPage/IconWarning3.png '''IMPORTANT''': Make sure to adjust the obvious site-specific details in the file, such as the two instances of <code>/home/<username>/</code>, <code>commonName</code>, <code>organizationName</code>, etc. to personalize for your environment.  For more information on the directives contained within this configuration file, use the <code>man config</code> command.

When you've edited the file to match your environment, save the file as <code>~/myCA/caconfig.cnf</code>.

Next, you need to generate the Certificate Authority Root Certificate and Key using the following commands:

<code>export OPENSSL_CONF=~/myCA/caconfig.cnf</code>

The previous command sets an environment variable <code>OPENSSL_CONF</code> which forces the <code>openssl</code> tool to look for a configuration file in an alternative location (in this case, <code>~/myCA/caconfig.cnf</code>).

Generate the CA certificate and key with the following command:

<pre>
openssl req -x509 -newkey rsa:2048 -out cacert.pem -outform PEM -days 1825
</code>

You should be prompted for a passphrase, and see output similar to this:

<pre>
Generating a 2048 bit RSA private key
.................................+++
.................................................................................................+++
writing new private key to '/home/bshumate/myCA/private/cakey.pem'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----
</code>

'''Do not forget the passphrase used with the command above!''' You'll need it every time you want to generate and sign a new server or client certificate!

The above process will create a self-signed certificate using PEM format and RSA public/private key encryption. The certificate will be valid for 1825 days.  The location, and purpose of the resultant files is as follows:

* <code>~/myCA/cacert.pem</code> : CA public certificate
* <code>~/myCA/private/cakey.pem</code> : CA private key
 
====== Creating a Self-Signed Server Certificate ======
Now that you have a Certificate Authority configured, you may use it to sign self-signed certificates.  Prior to beginning the steps below, you may wish to decide whether or not you want to encrypt the Certificate private key with a passphrase.  The advantages of encrypting the key with a passphrase include protection of the certificate in the event it is stolen.  

The certificate cannot be used with SSL-enabled applications without entering the passphrase every time the SSL-enabled application is started.  This condition, while being most secure, can present a problem:  If the server must be started in an unattended manner as in the case of a computer restart, then no one will be available to enter the passphrase, and subsequently the server will not start.  One way to eliminate this condition involves a trade-off in security:  

The key may be decrypted however, to remove the passphrase necessity, and thus SSL-enabled applications will then start automatically, without need to enter a passphrase.

To actually generate a self-signed certificate for use with an SSL application, follow this process:

Create the server configuration file, by editing <code>~/myCA/exampleserver.cnf</code> with your favorite text editor.  Add this example content:

<pre>
#
# exampleserver.cnf
#

[ req ]
prompt			= no
distinguished_name	= server_distinguished_name

[ server_distinguished_name ]
commonName		= tradeshowhell.com
stateOrProvinceName	= NC
countryName		= US
emailAddress		= root@tradeshowhell.com
organizationName	= My Organization Name
organizationalUnitName	= Subunit of My Large Organization
</code>

Be sure to change the values under <code>server_distinguished_name</code> especially the <code>commonName</code> value. The <code>commonName</code> value must match the host name, or CNAME for the host you wish to use the key for.  If the <code>commonName</code> does not match the intended hostname, then host / certificate mismatch errors will appear in the client applications of clients attempting to access the server.

Once you've edited the file appropriately, save it as <code>~/myCA/exampleserver.cnf</code>.  Generate the server certificate, and key with the following commands:

<code>export OPENSSL_CONF=~/myCA/exampleserver.cnf</code>

The previous command sets an environment variable <code>OPENSSL_CONF</code> which forces the <code>openssl</code> tool to look for a configuration file in an alternative location (in this case, <code>~/myCA/exampleserver.cnf</code>).

Now generate the certificate, and key:

<code>openssl req -newkey rsa:1024 -keyout tempkey.pem -keyform PEM -out tempreq.pem -outform PEM</code>

You should be prompted for a passphrase, and see output similar to this:

<pre>
Generating a 1024 bit RSA private key
...++++++
...............++++++
writing new private key to 'tempkey.pem'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----
</code>

'''Don't forget the passphrase!'''

Next, you may translate the temporary private key into an unencrypted key by using the following command:

<code>openssl rsa < tempkey.pem > server_key.pem</code>

You should be prompted for the passphrase used above, and see the following output:

<pre>
Enter pass phrase:
writing RSA key
</code>

If you wish to leave the key encrypted with a passphrase, simply rename the temporary key using the following command, instead of following the step above:

<code>mv tempkey.pem server_key.pem</code>

attachment:IconsPage/IconTip.png '''Remember''': If you use a server key encrypted with a passphrase, the passphrase will have to be entered each time the server application using the encrypted key is started. This means the server application will not start unless someone, or something enters the key.

Now you need to sign the server certificate with the Certificate Authority (CA) key using these commands:

<code>export OPENSSL_CONF=~/myCA/caconfig.cnf</code>

The previous command modifies the environment variable <code>OPENSSL_CONF</code> which forces the <code>openssl</code> tool to look for a configuration file in an alternative location (in this case, <code>~/myCA/caconfig.cnf</code> to switch back to the CA configuration).

Then sign the certificate as follows:

<code>openssl ca -in tempreq.pem -out server_crt.pem</code>

You will be prompted for the passphrase of the CA key as created in the Certificate Authority setup from above.  Enter this passphrase at the prompt, and you will then be prompted to confirm the information in the <code>exampleserver.cnf</code>, and finally asked to confirm signing the certificate.  Output should be similar to this:

<pre>
Using configuration from /home/bshumate/myCA/caconfig.cnf
Enter pass phrase for /home/bshumate/myCA/private/cakey.pem:
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :PRINTABLE:'tradeshowhell.com'
stateOrProvinceName   :PRINTABLE:'NC'
countryName           :PRINTABLE:'US'
emailAddress          :IA5STRING:'root@tradeshowhell.com'
organizationName      :PRINTABLE:'Trade Show Hell'
organizationalUnitName:PRINTABLE:'Black Ops'
Certificate is to be certified until Jan  4 21:50:08 2011 GMT (1825 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
</code>

Remove the temporary certificate, and key files with the following command:

<code>rm -f tempkey.pem &&	rm -f tempreq.pem</code>

Congratulations!  You now have a self-signed server application certificate, and key pair:

* <code>server_crt.pem</code> : Server application certificate file
* <code>server_key.pem</code> : Server application key file
 
Use the documentation provided with the server application you wish to use the certificate, and key for in order to properly use it.  See the '''Configuring Apache for SSL Support''' section below for an example usage.

====== Optional Step for Certain Server Applications ======

Some server applications, such as the Courier IMAP mail server application require that the unencrypted private key be prepended to the server certificate. To accomplish this, simply enter the following commands:

<pre>
cat server_key.pem server_crt.pem > hold.pem
mv	hold.pem server_crt.pem
chmod 	400	 server_crt.pem
</code>

====== Converting X.509 Certificates to PKCS#12 for Client Applications ======
If you wish to generate PKCS#12 certificates from your server's Root CA X.509 certificate for client use, you will need to use the following process on the particular server certificate, and key pair you desire to export a client certificate for:

Create a single file containing both the certificate, and key with the following command:

<code>openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem</code>

Then, convert this certificate / key combination file into the PKCS#12 certificate with the following command:

<code>openssl pkcs12 -export -out mycert.pfx -in mycert.pem -name "Certificate for Whatever"</code>

You will be prompted for an Export password, which you may use, or just leave blank.

The resultant <code>mycert.pfx</code> file may now be imported into applications such as Web Browsers, and E-Mail clients.  Since this certificate represents the ''Root CA'' certificate of your server, all subsequent server-level certificates signed by the CA certificate will also be automatically accepted by the client application which installs this PKCS#12 version of the certificate.

===== Generating Certificate for Signing by Recognized CA =====
In order to run a production server which will engage in commercial activity, such as "e-commerce", it is required to generate a certificate, and have it signed by a recognized Certificate Authority (CA) such as [https://www.verisign.com/ VeriSign].  The process for generating a certificate request, and obtaining a certificate signed by a recognized CA can be difficult.  You must complete all requisite "paperwork" prior to creating a certificate request, and ensure all required information is accurate.

Assuming you do not wish a passphrase-encrypted key, enter the following command to generate the private key, and certificate request:

<code>openssl req -new -newkey rsa:1024 -nodes -keyout mykey.pem -out myreq.pem</code>

If you already have a key you wish to use, then use the following command instead:

<code>openssl req -new -key mykey.pem -out myreq.pem</code>

You may wish to verify the signature, and information contained in the certificate request.  Verify the signature with this command:

<code>openssl req -in myreq.pem -noout -verify -key mykey.pem</code>

and verify the information with this command:

<code>openssl req -in myreq.pem -noout -text</code>

The next steps typically involve pasting the content of the certificate request file into the CA's signup form, and awaiting the certificate.  Also, you should safeguard the key file, as it will be required to use the certificate you receive from the CA.

==== Using PKCS#12 Certificates in Client Applications ====
This section of the guide explains using PKCS#12 certificates for SSL connections from the client perspective, and demonstrates the process of importing certificates into the Firefox Web Browser applications, and a couple of the more popular E-Mail clients. 

Importation of a root certificate into such applications enables a trusted SSL-encrypted connection to the server from whence the certificate came, free of nagging messages about the certificate being self-signed, and so on.

===== Importing a Certificate into Mozilla Firefox =====
Importation of a PKCS#12 certificate into the Mozilla Firefox Web Browser application is very simple:

 1. From within Firefox, click '''Edit''' > '''Preferences'''
 1. Click the '''Advanced''' icon
 1. Click the '''View Certificates''' button
 1. Click the '''Import''' button
 1. Browse to the location of the certificate file, which is typically a '''.pfx''' file type
 1. Highlight the certificate file, and click the '''Open''' button
 1. You may be prompted to enter Firefox's Master Password at this point, or to set an initial Master Password.  Enter the current, or net password as required, confirm it, and click '''OK'''
 1. You will then be prompted to enter the certificate's ''Export password''.  If there is no such password attached to the certificate, simply click '''OK''', otherwise enter the password, and click '''OK'''
 1. A message will appear advising that Firefox has "Successfully restored your security certificate(s) and private key(s)"
 1. Click '''OK'''
 1. You have successfully imported the server's client PKCS#12 certificate into Firefox
 
===== Importing a Certificate into Evolution =====
Importation of a PKCS#12 certificate into the Evolution E-Mail client couldn't be simpler:

 1. From within Evolution, click '''Edit''' > '''Preferences'''
 1. Click the '''Certificates''' icon
 1. Click the '''Import''' button
 1. Browse to the location of the certificate file, which is typically a '''.pfx''' file type
 1. Highlight the certificate file, and click the '''Open''' button
 1. You may be prompted to enter Evolution's certificate database password at this point, or to set an initial certificate database password.  Enter the current, or new password as required, confirm it, and click '''OK'''
 1. You will then be prompted to enter the ''PKCS12 File Password''.  If there is no such password attached to the certificate, simply click '''OK''', otherwise enter the password, and click '''OK'''
 1. You should see the certificate, and its details appear in the ''Your Certificates'' section of the ''Evolution Settings'' window
 1. Click the '''Close''' button
 1. You have successfully imported the server's client PKCS#12 certificate into Evolution
  
===== Importing a Certificate into Mozilla Thunderbird =====
Importation of a PKCS#12 certificate into the Mozilla Thunderbird E-Mail client application is very simple:

 1. From within Thunderbird, click '''Edit''' > '''Preferences'''
 1. Click the '''Advanced''' icon
 1. Click the '''Certificates''' entry in the right pane
 1. Click the '''Manage Certificates''' button
 1. Click the '''Import''' button
 1. Browse to the location of the certificate file, which is typically a '''.pfx''' file type
 1. Highlight the certificate file, and click the '''Open''' button
 1. You may be prompted to enter Thunderbird's Master Password at this point, or to set an initial Master Password.  Enter the current, or new password as required, confirm it, and click '''OK'''
 1. You will then be prompted to enter the certificate's ''Export password''.  If there is no such password attached to the certificate, simply click '''OK''', otherwise enter the password, and click '''OK'''
 1. A message will appear advising that Thunderbird has "Successfully restored your security certificate(s) and private key(s)"
 1. Click '''OK'''
 1. You have successfully imported the server's client PKCS#12 certificate into Thunderbird

===== Importing a Certificate into the System-Wide Certificate Authority Database =====
You can import a CA Certificate into the system-wide database of trusted certificate authorities.  Applications that use this database will automatically trust any certificates stored here.

1. Copy your certificate to the system certificate directory.  At a terminal prompt, type:
<pre>
$ sudo cp mycert.pem /usr/share/ca-certificates/mycert.crt
</code>
2. Edit the ca-certificates configuration file /etc/ca-certificates.conf.  Add the name of the file you copied to /use/share/ca-certificates to the top of the list just after the final "#".
For example:
<pre>
# This file lists certificates that you wish to use or to ignore to be
# installed in /etc/ssl/certs.
# update-ca-certificates(8) will update /etc/ssl/certs by reading this file.
#
# This is autogenerated by dpkg-reconfigure ca-certificates.
# certificates shoule be installed under /usr/share/ca-certificates
# and files with extension '.crt' is recognized as available certs.
#
# line begins with # is comment.
# line begins with ! is certificate filename to be deselected.
#
mycert.crt
brasil.gov.br/brasil.gov.br.crt
cacert.org/cacert.org.crt
mozilla/ABAecom_=sub.__Am._Bankers_Assn.=_Root_CA.crt
[... many additional certificates omitted ...]
</code>
3. Update the CA certificates database by typing:
<pre>
$ sudo update-ca-certificates
</code>
4. You have successfully imported your certificate into the system CA certificates database.
 
=== Configuring Apache for SSL Support ===
This section of the guide will describe the procedure for configuring the Apache2 HTTP server to provide HTTPS connections using OpenSSL.  This is fairly straightforward, and simple process.

==== Install Apache 2 ====
Install the Apache 2 HTTP server by installing the following package <code>apache2</code>

==== Generate a Server Certificate ====
The simplest method of generating a self-signed SSL certificate for Apache 2 use is to make use of the '''apache2-ssl-certificate''' shell script which is bundled with the Ubuntu Apache 2 package.  This shell script uses a specific configuration file (<code>/usr/share/apache2/ssleay.cnf</code>) and parameters to generate a self-signed certificate with no passphrase encryption of the key element.  To generate an Apache 2 certificate with the script, simply use the following command at a terminal prompt:

<code>sudo apache2-ssl-certificate</code>

Answer all of the questions which are asked, and you should then observe the creation of an <code>apache.pem</code> in the <code>/etc/apache2/ssl</code> directory.

Next you must enable SSL support in the Ubuntu Apache 2 package by executing the following command:

<code>sudo a2enmod ssl</code>

You should receive the following output from the above command:

<code>Module ssl installed; run /etc/init.d/apache2 force-reload to enable.</code>

Now configure SSL settings for Apache 2 by creating a new ssl enabled site configuration file, and adjusting the directory locations as needed for HTTP and HTTPS instances of Apache 2:

<pre>
sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/ssl
sudo ln -s /etc/apache2/sites-available/ssl /etc/apache2/sites-enabled/ssl 
</code>

Edit ''/etc/apache2/sites-enabled/ssl'', and modify the <code>NameVirtualHost *</code> directive to resemble:

<code>NameVirtualHost *:443</code>

and the <code>VirtualHost *</code> directive to resemble:

<code><VirtualHost *:443></code>

Add the following two lines under the <code><VirtualHost *:443></code> line:

<pre>
SSLEngine On
SSLCertificateFile /etc/apache2/ssl/apache.pem
</code>

You can also configure the <code>DocumentRoot</code> and <code>Directory</code> directives as needed.

Edit <code>/etc/apache2/sites-enabled/default</code>

Edit <code>/etc/apache2/sites-enabled/ssl</code>, and modify the <code>NameVirtualHost *</code> directive to resemble:

<code>NameVirtualHost *:80</code>

and the <code>VirtualHost *</code> directive to resemble:

<code><VirtualHost *:80></code>

You can also configure the <code>DocumentRoot</code> and <code>Directory</code> directives as needed.

Edit <code>/etc/apache2/ports.conf</code> and add the line:

<code>Listen 443</code>

under the existing <code>Listen 80</code> line.

Reload Apache 2 so that the new configuration directives take effect, with the command:

<code>sudo /etc/init.d/apache2 force-reload</code>

You should now have a functional, SSL-enabled Apache 2 HTTP/HTTPS server.
 
==== Redirecting HTTP traffic to HTTPS ====

If you want to force users to use HTTPS, you can redirect all HTTP traffic to the HTTPS site.  This is a great method because it is transparent to the users, they don't have do anything different.

First, you must enable URL rewrite support in the Ubuntu Apache 2 package by executing the following command:

<code>sudo a2enmod rewrite</code>

You should receive the following output from the above command:

<code>Module rewrite installed; run /etc/init.d/apache2 force-reload to enable.</code>

Next, edit <code>/etc/apache2/sites-enabled/default</code>, and add the following three lines under the <code><VirtualHost *:80></code> line:

<pre>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</code>

Now reload Apache 2 so that the new configuration directives take effect, with the command:

<code>sudo /etc/init.d/apache2 force-reload</code>

You are all done!  Users who go to the HTTP (port 80) site will now be redirected to HTTPS and all traffic will be encrypted.

=== Resources ===
Additional resources pertaining to OpenSSL and Secure Sockets Layer appear below.

==== Local System Resources ====

||<style="background:#F1F1ED;"><code>man config</code>|| System manual page for the OpenSSL library configuration files||
||<style="background:#F1F1ED;"><code>man gendsa</code>|| System manual page for the <code>gendsa</code> DSA private key generator||
||<style="background:#F1F1ED;"><code>man genrsa</code>|| System manual page for the <code>genrsa</code> RSA private key generator||
||<style="background:#F1F1ED;"><code>man openssl</code>|| System manual page for the <code>openssl</code> command-line tool||
||<style="background:#F1F1ED;"><code>man rand</code>|| System manual page for the <code>rand</code> pseudo-random byte generator utility||
||<style="background:#F1F1ED;"><code>man x509</code>|| System manual page for the <code>x509</code> certificate display and signing utility||

==== WWW Resources ====

[http://www.cacert.org/ CACert, a FREE X.509 Certificate Authority]

[http://www.pseudonym.org/ssl/ssl_ca.html#creating Creating a Certificate Authority (CA)]

[http://sial.org/howto/openssl/ca/ OpenSSL Certificate Authority Setup]

[http://www.openssl.org/ OpenSSL Website]

[http://www.ietf.org/html.charters/pkix-charter.html Public Key Infrastructure (X.509) (pkix)]

-----
CategorySecurity

[[category:UbuntuHelp]]