设为首页 收藏本站
查看: 901|回复: 0

[经验分享] OpenSSL配置Apache的https功能

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-11-28 09:38:53 | 显示全部楼层 |阅读模式
https:会话建立过程
1.客户端和服务器端先三次握手
2.客户端发起请求,请求服务器端的443端口。
3.双方协商建立ssl会话
4.协商完成后,服务器发送自己的证书给客户端。
5.客户端拿到证书后,查看证书是不是受信任证书颁发机构颁发的,和验证证书是不是完整的。
6.客户端传递加密后的对称密码给服务器端。
连接建立完成


CA给客户端颁发证书过程
1.创建CA
   自己生成一对密钥
   生成自签名证书
2.客户端
    生成一对密钥
    生成证书颁发请求,.csr
    将请求发给CA
3.CA端
     签署此证书
     传送给客户端

注意:SSL仅能基于Ip地址进行,如果有多个虚拟主机的话,那么只能给其中一个加密

实验部分

查看httpd模块,确保有ssl,如果没有就手动安装
[iyunv@localhost ~]# httpd -M
ssl_module
查看ssl生成的文件
[iyunv@localhost ~]# rpm -ql mod_ssl
/etc/httpd/conf.d/ssl.conf          #ssl主配置文件
/usr/lib/httpd/modules/mod_ssl.so   #安装了一个ssl模块
/var/cache/mod_ssl                  #ssl缓存目录

建立私有证书颁发机构
生成Ca的私钥
[iyunv@localhost ~]# cd /etc/pki/CA/
[iyunv@localhost CA]# (umask 077; openssl genrsa -out private/cakey.pem 2048)   
Generating RSA private key, 2048 bit long modulus
.....................................+++
.........................+++
e is 65537 (0x10001)
[iyunv@localhost CA]#


使用私钥生产自签名证书
[iyunv@localhost CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [GB]:CN               #设置国家名称
State or Province Name (full name) [Berkshire]:HB  #设置省份名称
Locality Name (eg, city) [Newbury]:SJG             #设置市名称
Organization Name (eg, company) [My Company Ltd]:BENET   #设置组织名称
Organizational Unit Name (eg, section) []:LB       #设置部门名称
Common Name (eg, your name or your server's hostname) []:www.benet.com     #设置主机名需要与颁发的主机名一直,不然会报证书不信任
Email Address []:ca@benet.com  #设置电子邮件地址



编辑配置文件***的存放位置
[iyunv@localhost ~]# vim /etc/pki/tls/openssl.cnf
dir             = /etc/pki/CA


准备三个目录,证书颁发目录(certs),证书吊销(crl),刚生成的证书(newcerts)和颁发证书的序列号文件index.txt并且写入第一个证书序列号为01
[iyunv@localhost CA]# mkdir certs crl newcerts
[iyunv@localhost CA]# touch index.txt
[iyunv@localhost CA]# echo 01 > serial


这个时候CA就可以用了

web服务器配置
准备一个目录存储证书
[iyunv@localhost CA]# cd /etc/httpd/
[iyunv@localhost httpd]# mkdir ssl
[iyunv@localhost httpd]# cd ssl/


生成web的私钥证书
[iyunv@localhost ssl]# (umask 077;openssl genrsa 1024 > httpd.key)
Generating RSA private key, 1024 bit long modulus
............++++++
..........................++++++
e is 65537 (0x10001)

生成证书签署请求
[iyunv@localhost ssl]# openssl req -new -key httpd.key -out httpd.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [GB]:CN
State or Province Name (full name) [Berkshire]:HB
Locality Name (eg, city) [Newbury]:SJZ
Organization Name (eg, company) [My Company Ltd]:BENET
Organizational Unit Name (eg, section) []:LB
Common Name (eg, your name or your server's hostname) []:www.benet.com  #和网站的域名必须保持完全一致
Email Address []:aa@benet.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:


CA服务器签署web服务器的证书
[iyunv@localhost ~]# openssl ca -in /etc/httpd/ssl/httpd.csr -out /etc/httpd/ssl/httpd.crt -days 365   
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Nov 26 10:42:56 2014 GMT
            Not After : Nov 26 10:42:56 2015 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = HB
            organizationName          = BENET
            organizationalUnitName    = LB
            commonName                = www.benet.com
            emailAddress              = aa@benet.com
        X509v3 extensions:
            X509v3 Basic Constraints:
                CA:FALSE
            Netscape Comment:
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier:
                35:F3:EE:62:1F:64:D5:06:DB:5C:60:77:0B:19:33:C2:6A:8B:2D:B4
            X509v3 Authority Key Identifier:
                keyid:57:72:4C:91:1C:9B:F2:B0:E8:4A:E1:34:AB:03:E6:E6:31:2A:1D:C3

Certificate is to be certified until Nov 26 10:42:56 2015 GMT (365 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


查看颁发情况
[iyunv@localhost CA]# cat index.txt
V       151126104256Z           01      unknown /C=CN/ST=HB/O=BENET/OU=LB/CN=www.benet.com/emailAddress=aa@benet.com
查看下一个请求给的序列号
[iyunv@localhost CA]# cat serial
02


证书申请完毕

客户端配置ssl
配置之前先备份配置文件
[iyunv@localhost ssl]# cd /etc/httpd/conf.d/
[iyunv@localhost conf.d]# cp ssl.conf ssl.conf.bak

编辑ssl配置文件,除了列出的需要设置,其他统统不管

ServerName www.benet.com  #设置网站域名
DocumentRoot "/www/benet.com"  #设置网站存放位置
ErrorLog logs/ssl_error_log   #设置网站错误日志
TransferLog logs/ssl_access_log #设置网站访问日志
LogLevel warn  #设置警告级别
SSLEngine on  #是否开启ssl功能
SSLProtocol all -SSLv2  #支持的协议为,不支持sslv2,然后支持所有
SSLCertificateFile /etc/httpd/ssl/httpd.crt #设置证书文件位置
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key #设置私钥文件位置



编辑完成之后重新启动httpd服务,然后查看443端口已经启动了
[iyunv@localhost conf.d]# service httpd restart
[iyunv@localhost conf.d]# netstat -tnlp         
tcp        0      0 :::443                      :::*                        LISTEN      6436/httpd  



这时客户端访问www.benet.com的443端口还是会报错,这时因为客户端不信任CA颁发机构
需要将/etc/pki/CA/cacert.pem文件复制到客户端

并且客户端改名为cacert.crt,然后安装证书,并且放到根颁发机构
wKiom1R200Hz0h17AAFlzMRoEqs724.jpg

这时客户端在访问就不会报错了,而且访问全部是加密的访问了
wKioL1R20-SQFi2XAACGRxskrQo991.jpg


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-34021-1-1.html 上篇帖子: 利用Apache的beanutils判断字符串是否为一个合法日期 下篇帖子: 如何查看apache服务器启动时加载了哪些模块
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表