apache添加ssl协议实现用户认证


目标

1对服务器的访问由http改为https,

2仅有证书的客户端可以访问服务器,

3.通过服务器端的配置,可以停用某个客户端的证书。

Apache服务器相关配置:

../apache/conf/httpd.conf中,

1.关闭80端口,禁止通过http访问服务器,在Listen 80之前加上’#’,修改后如下:红色字体为修改之后的。

#Listen 0.0.0.0:80

#Listen [::]:80

#Listen 80

2.加载ssl模块,去掉之前的’#’,修改后如下:

LoadModule ssl_module modules/mod_ssl.so

LoadModule status_module modules/mod_status.so

3.包括ssl模块的配置文件

# Secure (SSL/TLS) connections

Include "conf/extra/httpd-ssl.conf"

4服务器目录访问权限设置;

htdocs目录访问限制。

<Directory "C:/xampplite/htdocs">

    #

    # Possible values for the Options directive are "None", "All",

    # or any combination of:

    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews

    #

    # Note that "MultiViews" must be named *explicitly* --- "Options All"

    # doesn't give it to you.

    #

    # The Options directive is both complicated and important.  Please see

    # http://httpd.apache.org/docs/2.2/mod/core.html#options

    # for more information.

    #

    Options Indexes FollowSymLinks Includes ExecCGI

 

    #

    # AllowOverride controls what directives may be placed in .htaccess files.

    # It can be "All", "None", or any combination of the keywords:

    # Options FileInfo AuthConfig Limit

    #

    AllowOverride All

 

    #

    # Controls who can get stuff from this server.

    #

 Order allow,deny

 #Deny from all

  Allow from all

</Directory>

 

服务器根目录访问限制

#

# Each directory to which Apache has access can be configured with respect

# to which services and features are allowed and/or disabled in that

# directory (and its subdirectories).

#

# First, we configure the "default" to be a very restrictive set of

# features.  

#

<Directory />

    Options FollowSymLinks

    AllowOverride None

    Order deny,allow

   #Deny from all

   allow from all

</Directory>

 

../apache/conf/extra/httpd-ssl.conf

1.启用https访问,去掉#Listen 443之前的‘#,修改后如下:

#Listen 0.0.0.0:443

#Listen [::]:443

Listen 443

 

2.配置服务器名:ServerName 如果基于IP访问填写如192.168.48.136:443IP必须与证书上的commonName一致,否则访问时回弹出警告。

<VirtualHost _default_:443>

    #   General setup for the virtual host

    DocumentRoot "C:/xampplite/htdocs"

    ServerName l92.168.48.136:443

    ServerAdmin webmaster@localhost

    ErrorLog "logs/error.log"

    <IfModule log_config_module>

        CustomLog "logs/access.log" combined

</IfModule>

3.服务器证书相关配置:(具体证书文件名根据自己后面制作生成的而定)

SSLCertificateFile "conf/ssl.crt/server.crt"               服务器证书文件

SSLCertificateKeyFile "conf/ssl.key/server1.key"          服务器私钥文件

SSLCACertificateFile "conf/ssl.crt/ca.crt"                  ca证书文件

SSLCARevocationPath "conf/ssl.crl"       证书吊销列表路径

SSLCARevocationFile "conf/ssl.crl/client.crl"   证书吊销列表文件

4.用户认证配置修改如下:

#   Client Authentication (Type):

    #   Client certificate verification type and depth.  Types are

    #   none, optional, require and optional_no_ca.  Depth is a

    #   number which specifies how deeply to verify the certificate

    #   issuer chain before deciding the certificate is not valid.

    SSLVerifyClient require

    SSLVerifyDepth  1

Openssl安装配置

1.安装xampp后里面自带了openssl模块。由于openssl是使用时需要调用C库函数,因此确认电脑上安装了,vc++6.0或者visual studio 2008.如果是在win xp下安装,xampp里面包含的php版本必须是v5.4或之前的.

路径:../apache/bin/openssl.conf

2.Openssl的配置,配置证书生成的文件的存储路径。

dir = c:/ca # Where everything is kept

3.申请证书时,需要填写证书信息,如果多次申请证书,这些证书的大部分信息相同(如果国家、城市等),可配置默认值,使用默认值是回车跳过就可以了,以下是我配置的例子:

[ 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 = BeiJing (eg, city)

 

0.organizationName = companyName(eg, company)

0.organizationName_default = companyName

 

# 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 = t3 (eg, section)

#organizationalUnitName_default =

 

commonName = Common Name (eg, YOUR name)

commonName_max = 64

 

emailAddress = XXX@xx.com

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

 

注意事项:安装后配置文件格式为cnfWin xp下因为无法正确识别文件而打不开,

解决办法:将此openssl.cnf文件(误识别为快速拨号文件)拖入到UltraEdit软件图标处打开,将文件另存为temp.conf格式,然后新建文件命名为openssl.conf,temp.conf右键用写字板打开,全部复制到openssl.conf文件夹下,保存openssl.conf,然后将此文件放到../apache/bin目录下。temp.conf可以删除了.

 

三 生成CA证书的步骤:

1.生成CA证书的私钥(.key)和证书申请文件(.csr);(ps:因为我之前生成过ca.key,ca.csr,所以这次就用cb.keycb.csr,不过文件名无所谓)

 

2输入证书私钥保护密码:

私钥保护密码一定要牢记,利用用到证书私钥的地方必须输入私钥保护密码。

 

输入证书申请文件相关的信息:主要填写国家、省份、城市、公司名、部门、Common name(用户名)(此项必须手动输入,这是证书身份区别靠这项)、邮箱随便填、challenge password是什么不知道,之后没用到过,不过还是记住为好,最后一项备选公司名随便填了。如果以后证书申请文件的内容大部分相同,可以直接在openssl.conf配置文件中修改,进行默认配置

 

3.使用根证书私钥对根证书私钥进行自签名

 

输入根证书私钥保护密码:

 

5接着连续输入y,确认

 

 

 

 

 

四 服务器端证书生成步骤:

1.生成服务器证书的私钥(.key)和证书申请文件(.csr)

 

输入证书申请文件相关信息:注意:如果服务器是基于域名访问,common name输入域名,如果基于IP地址访问,common name输入服务器对应的IP地址

 

2.用生成的CA证书给服务器证书签名。(ps:后面没有截取到的命令是 “.\openssl.conf”)

输入CA证书的私钥保护密码

 

接下来连续输入y确认

 

3.如果apache是安装在linux下,跳过这一步,如果安装在windows环境下,则不能给CA私钥和服务器私钥设置保护密码,否则服务器无法启动,需要去掉密码,去掉CA私钥保护密码用如下命令:

去掉服务器密码,只需修改cb.keyserver1.key.即可

然后将\ca\certs目录下的服务器证书和CA证书,拷贝到apache..\apache\ssl.crt目录

\ca\csr目录下的服务器证书申请文件server1.csr,拷贝到apache..\apache\ssl.csr目录

\ca\keys目录下的服务器私钥文件server..key,拷贝到apache..\apache\ssl.key目录

 

五 客户端证书生成步骤:

1.生成客户端的私钥.key和证书申请文件.csr

 

2.输入私钥保护密码:

私钥保护密码必须牢记!!

 

3输入证书申请文件相关的信息:

 

4.使用根证书对个人证书进行签名:(ps:后面没有截取到的命令是 “.\openssl.conf”)

需要输入CA证书的私钥保护密码,后面的连续输入y

 

 

5.将客户端证书文件(公钥和用户信息)和私钥合并成pfx格式。

 

输入client2的私钥保护密码:

输入导入证书的密码并确认输入:

 

将生成的client.pfx的个人证书和ca.crt证书安装到PC浏览器或手机中。(ps:手机中安装,如果出现格式不支持,请参考链接:http://www.cnblogs.com/dosboy/archive/2013/03/05/2943963.html

 

 

 

 

六 吊销证书:(即通过吊销证书来限制某个客户端对服务器的访问权限)

1.查看\ca目录下,打开index.txt文件,查看需要吊销证书的信息,尤其是序列号。

例如:

V 150506074352Z 05 unknown /C=CN/ST=BeiJing/O=linekong/CN=client3

可以看到client3的证书序列号为:05     而且当前证书状态是未吊销(前面第一个字母是V表示未吊销,吊销后为R

打开\ca\cert目录后可以看到有个05.pem的文件。

2.通过这个命令查看将要吊销的证书内部的详细信息,进行最后确认。

 

 

3.输入一下命令吊销该证书

 

输入ca的私钥保护密码:

 

可以看到证书已经吊销:

 

4.更新证书吊销文件列表

 

输入ca证书私钥保护密码

 

可以看到证书吊销列表多出05号的证书

 

5.将client.crl文件复制到服务器目录下(我的是在..\apache\conf\ssl.crl目录下),并修改..apache\conf\extra目录下httpd-ssl.conf中的配置,修改后如下:

    #   Certificate Revocation Lists (CRL):

    #   Set the CA revocation path where to find CA CRLs for client

    #   authentication or alternatively one huge file containing all

    #   of them (file must be PEM encoded)

    #   Note: Inside SSLCARevocationPath you need hash symlinks

    #         to point to the certificate files. Use the provided

    #         Makefile to update the hash symlinks after changes.

    SSLCARevocationPath "conf/ssl.crl"

SSLCARevocationFile "conf/ssl.crl/client.crl"

6.重启服务器。

注意事项:如果以后还要吊销其他由CA签发的证书时,前3步根据被吊销证书信息操作,第4步不变,因为由CA签发的证书被吊销后,统一都放在同一个证书吊销列表文件中,(如本例中是:client.crl)证书吊销列表文件更新后,需同时用最新的文件替换..\apache\conf\ssl.crl目录下原来的旧文件。否则被吊销的证书依然是可用的。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM