随笔-10  评论-36  文章-6  trackbacks-0

Apache服务器SSL配置

笔者不久前参与了某系统代理服务器配置的研究,查阅了大量关于Apache的SSL配置的资料。本文即是笔者学习这些资料后的经验总结,以Win32版Apache与OpenSSL为例,介绍从创建数字证书到Apache配置的整个过程,希望对读者有所帮助。

Apache是目前最流行的WEB服务器之一,借助OpenSSL库,我们可以在Apache上建立SSL通道,提供SSL连接服务。OpenSSL库除提供Apache的SSL模块外,还提供了一套数字证书工具,可以创建、转换数字证书。

1       环境准备

软件下载

l       Apache:apache_2.2.4-win32-x86-openssl-0.9.8d.msi

l       openSSL:Win32 OpenSSL v0.9.8e

apache_2.2.4-win32-x86-openssl-0.9.8d.msi是一个捆绑的软件包,包含了apache与openssl必选组件,apache的版本是2.2.4,OpenSSL版本是0.9.8d。如不必使用最新的openssl,则仅下载此软件包即可。

软件安装

(1)   运行Win32OpenSSL-0_9_8e.exe安装OpenSSL;

(2)   运行apache_2.2.4-win32-x86-openssl-0.9.8d.msi安装apache。

如果想使用最新版的OpenSSL,则应删除apache安装目录下的libeay32.dll与ssleay32.dll两个文件,迫使Apache使用OpenSSL安装在c:"windows"system32下的两个最新文件。

软件配置

1.3.1            Apache配置

编辑apache的conf目录下的httpd.conf文件,将#LoadModule ssl_module modules/mod_ssl.so前的#删除,使得Apache启动时加载mod_ssl.so模块。重新启动apache,如果看到下列画面,说明mod_ssl.so已经加载成功。

1.3.2            OpenSSL配置

(1)创建证书管理目录与文件;

l       创建C:"CA作为证书管理主目录;

l       C:"CA下创建certs与keys两个目录,存储证书与私钥;

l       C:"CA下创建crl目录,存储证书注销列表文件;

l       C:"CA下创建一个空文件index.txt,存储证书清单;

l       C:"CA下创建一个index.txt.attr文件,内容为unique_subject = no

l       C:"CA下创建证书序列号文件serial,内容为01;

l       C:"CA下创建证书注销列表序号文件crlnumber,内容为01。

(2)编辑OpenSSL的bin/openssl.cnf文件,修改下列内容:

#

# OpenSSL example configuration file.

# This is mostly being used for generation of certificate requests.

#

# This definition stops the following lines choking if HOME isn't

# defined.

HOME          = .

RANDFILE      = $ENV::HOME/.rnd

# Extra OBJECT IDENTIFIER info:

#oid_file     = $ENV::HOME/.oid

oid_section        = new_oids

# To use this configuration file with the "-extfile" option of the

# "openssl x509" utility, name here the section containing the

# X.509v3 extensions to use:

# extensions       =

# (Alternatively, use a configuration file that has only

# X.509v3 extensions in its main [= default] section.)

[ new_oids ]

# We can add new OIDs in here for use by 'ca' and 'req'.

# Add a simple OID like this:

# testoid1=1.2.3.4

# Or use config file substitution like this:

# testoid2=${testoid1}.5.6

####################################################################

[ ca ]

default_ca    = CA_default       # The default ca section

####################################################################

[ CA_default ]

dir      = C:/CA       # Where everything is kept

certs         = $dir/certs       # Where the issued certs are kept

crl_dir       = $dir/crl         # Where the issued crl are kept

database= $dir/index.txt   # database index file.

#unique_subject    = no          # Set to 'no' to allow creation of

                       # several ctificates with same subject.

new_certs_dir= $dir/certs       # default place for new certs.

certificate   = $dir/certs/CA.YOUR.COM.crt     # The CA certificate

serial        = $dir/serial          # The current serial number

crlnumber= $dir/crlnumber   # the current crl number

                       # must be commented out to leave a V1 CRL

crl      = $dir/crl.pem         # The current CRL

private_key   = $dir/keys/CA.YOUR.COM.key    # The private key

RANDFILE= $dir/keys/.rand       # private random number file

x509_extensions    = usr_cert         # The extentions to add to the cert

# Comment out the following two lines for the "traditional"

# (and highly broken) format.

name_opt = ca_default       # Subject Name options

cert_opt = ca_default       # Certificate field options

# Extension copying option: use with caution.

# copy_extensions = copy

# Extensions to add to a CRL. Note: Netscape communicator chokes on V2 CRLs

# so this is commented out by default to leave a V1 CRL.

# crlnumber must also be commented out to leave a V1 CRL.

# crl_extensions   = crl_ext

default_days = 365              # how long to certify for

default_crl_days= 30             # how long before next CRL

default_md    = sha1             # which md to use.

preserve= no          # keep passed DN ordering

# A few difference way of specifying how similar the request should look

# For type CA, the listed attributes must be the same, and the optional

# and supplied fields are just that :-)

policy        = policy_match

# For the CA policy

[ policy_match ]

countryName        = match

stateOrProvinceName    = match

organizationName   = match

organizationalUnitName= optional

commonName         = supplied

emailAddress       = optional

# For the 'anything' policy

# At this point in time, you must list all acceptable 'object'

# types.

[ policy_anything ]

countryName        = optional

stateOrProvinceName    = optional

localityName       = optional

organizationName   = optional

organizationalUnitName= optional

commonName         = supplied

emailAddress       = optional

####################################################################

[ req ]

default_bits       = 1024

default_keyfile    = privkey.pem

distinguished_name= req_distinguished_name

attributes         = req_attributes

x509_extensions    = v3_ca # The extentions to add to the self signed cert

# Passwords for private keys if not present they will be prompted for

# input_password = secret

# output_password = secret

# This sets a mask for permitted string types. There are several options.

# default: PrintableString, T61String, BMPString.

# pkix   : PrintableString, BMPString.

# utf8only: only UTF8Strings.

# nombstr : PrintableString, T61String (no BMPStrings or UTF8Strings).

# MASK:XXXX a literal mask value.

# WARNING: current versions of Netscape crash on BMPStrings or UTF8Strings

# so use this option with caution!

string_mask = nombstr

# req_extensions = v3_req # The extensions to add to a certificate request

[ req_distinguished_name ]

countryName            = Country Name (2 letter code)

countryName_default         = CN

countryName_min             = 2

countryName_max             = 2

stateOrProvinceName         = State or Province Name (full name)

stateOrProvinceName_default= BeiJing

localityName           = Locality Name (eg, city)

0.organizationName     = Organization Name (eg, company)

0.organizationName_default = COM

# we can do this but it is not needed normally :-)

#1.organizationName         = Second Organization Name (eg, company)

#1.organizationName_default= World Wide Web Pty Ltd

organizationalUnitName      = Organizational Unit Name (eg, section)

organizationalUnitName_default   = YOUR

commonName             = Common Name (eg, YOUR name)

commonName_max              = 64

emailAddress           = Email Address

emailAddress_max       = 64

# SET-ex3          = SET extension number 3

[ req_attributes ]

challengePassword      = A challenge password

challengePassword_min       = 4

challengePassword_max       = 20

unstructuredName       = An optional company name

[ usr_cert ]

# These extensions are added when 'ca' signs a request.

# This goes against PKIX guidelines but some CAs do it and some software

# requires this to avoid interpreting an end user certificate as a CA.

basicConstraints=CA:FALSE

# Here are some examples of the usage of nsCertType. If it is omitted

# the certificate can be used for anything *except* object signing.

# This is OK for an SSL server.

# nsCertType           = server

# For an object signing certificate this would be used.

# nsCertType = objsign

# For normal client use this is typical

# nsCertType = client, email

# and for everything including object signing:

# nsCertType = client, email, objsign

# This is typical in keyUsage for a client certificate.

# keyUsage = nonRepudiation, digitalSignature, keyEncipherment

# This will be displayed in Netscape's comment listbox.

nsComment          = "OpenSSL Generated Certificate"

# PKIX recommendations harmless if included in all certificates.

subjectKeyIdentifier=hash

authorityKeyIdentifier=keyid,issuer

# This stuff is for subjectAltName and issuerAltname.

# Import the email address.

# subjectAltName=email:copy

# An alternative to produce certificates that aren't

# deprecated according to PKIX.

# subjectAltName=email:move

# Copy subject details

# issuerAltName=issuer:copy

#nsCaRevocationUrl     = http://www.domain.dom/ca-crl.pem

#nsBaseUrl

#nsRevocationUrl

#nsRenewalUrl

#nsCaPolicyUrl

#nsSslServerName

[ v3_req ]

# Extensions to add to a certificate request

basicConstraints = CA:FALSE

keyUsage = nonRepudiation, digitalSignature, keyEncipherment

[ v3_ca ]

# Extensions for a typical CA

# PKIX recommendation.

subjectKeyIdentifier=hash

authorityKeyIdentifier=keyid:always,issuer:always

# This is what PKIX recommends but some broken software chokes on critical

# extensions.

#basicConstraints = critical,CA:true

# So we do this instead.

basicConstraints = CA:true

# Key usage: this is typical for a CA certificate. However since it will

# prevent it being used as an test self-signed certificate it is best

# left out by default.

# keyUsage = cRLSign, keyCertSign

# Some might want this also

# nsCertType = sslCA, emailCA

# Include email address in subject alt name: another PKIX recommendation

# subjectAltName=email:copy

# Copy issuer details

# issuerAltName=issuer:copy

# DER hex encoding of an extension: beware experts only!

# obj=DER:02:03

# Where 'obj' is a standard or added object

# You can even override a supported extension:

# basicConstraints= critical, DER:30:03:01:01:FF

[ crl_ext ]

# CRL extensions.

# Only issuerAltName and authorityKeyIdentifier make any sense in a CRL.

# issuerAltName=issuer:copy

authorityKeyIdentifier=keyid:always,issuer:always

[ proxy_cert_ext ]

# These extensions should be added when creating a proxy certificate

# This goes against PKIX guidelines but some CAs do it and some software

# requires this to avoid interpreting an end user certificate as a CA.

basicConstraints=CA:FALSE

# Here are some examples of the usage of nsCertType. If it is omitted

# the certificate can be used for anything *except* object signing.

# This is OK for an SSL server.

# nsCertType           = server

# For an object signing certificate this would be used.

# nsCertType = objsign

# For normal client use this is typical

# nsCertType = client, email

# and for everything including object signing:

# nsCertType = client, email, objsign

# This is typical in keyUsage for a client certificate.

# keyUsage = nonRepudiation, digitalSignature, keyEncipherment

# This will be displayed in Netscape's comment listbox.

nsComment          = "OpenSSL Generated Certificate"

# PKIX recommendations harmless if included in all certificates.

subjectKeyIdentifier=hash

authorityKeyIdentifier=keyid,issuer:always

# This stuff is for subjectAltName and issuerAltname.

# Import the email address.

# subjectAltName=email:copy

# An alternative to produce certificates that aren't

# deprecated according to PKIX.

# subjectAltName=email:move

# Copy subject details

# issuerAltName=issuer:copy

#nsCaRevocationUrl     = http://www.domain.dom/ca-crl.pem

#nsBaseUrl

#nsRevocationUrl

#nsRenewalUrl

#nsCaPolicyUrl

#nsSslServerName

# This really needs to be in place for it to be a proxy certificate.

proxyCertInfo=critical,language:id-ppl-anyLanguage,pathlen:3,policy:foo

(3)创建随机数种子rand文件:

cd c:"openssl"bin

openssl rand -out c:/ca/keys/.rand 1024

2       创建数字证书

创建根证书(CA.COM

(1)创建根证书的私钥与证书申请;

openssl genrsa -des3 -out c:/ca/keys/CA.COM.key 2048

** 创建时需要指定根私钥保护密码,请牢记此密码。

openssl req -new -out CA.COM.csr -key c:/ca/keys/CA.COM.key -config openssl.cnf

** 创建时需要指定下列内容,其他内容使用默认值。

Country Name : CN

State or Province Name: BeiJing

Organization Name:COM

Organizational Unit Name:COM

Common Name:CA.COM

或:

openssl req -newkey rsa:2048 -keyout c:/ca/Keys/CA.COM.key -out CA.COM.csr -config openssl.cnf

** 创建时需要指定下列内容,其他内容使用默认值。

Country Name : CN

State or Province Name: BeiJing

Organization Name:COM

Organizational Unit Name:COM

Common Name:CA.COM

(2)使用根私钥对根证书自签名;

openssl ca -in CA.COM.csr -out c:/ca/certs/CA.COM.crt -selfsign -keyfile c:/ca/keys/CA.COM.key -days 7305 -extensions v3_ca -config openssl.cnf

** 确认证书主题是:

            countryName               = CN

            stateOrProvinceName       = BeiJing

            organizationName          = COM

            organizationalUnitName    = COM

            commonName                = CA.COM

或:

openssl x509 -in CA.COM.csr -out c:/ca/certs/CA.COM.crt -req -signkey c:/ca/keys/CA.COM.key -days 7305 -extensions v3_ca -config openssl.cnf

** 确认证书主题是:

            countryName               = CN

            stateOrProvinceName       = BeiJing

            organizationName          = COM

            organizationalUnitName    = COM

            commonName                = CA.COM

(3)删除根证书申请文件CA.COM.csr。

创建二级根证书(CA.YOUR.COM

创建完根证书后,我们可以使用根证书创建二级根证书。

(1)创建二级根证书的证书申请;

openssl req -newkey rsa:2048 -keyout c:/ca/keys/CA.YOUR.COM.key -out CA.YOUR.COM.csr -config openssl.cnf

** 创建时需要指定二级证书私钥的保护密码,请牢记此密码;

**