ikjjty656r 发表于 2015-8-20 10:51:26

架设CA服务器实现https通信,web服务器使用CA自签证书与https通信

教程目标:web服务器使用CA自签证书与https通信



====================================================
准备:

系统环境:CentOS 6.6 X64 Desktop安装,附加开发工具包的安装


本机作为签证机构,ip地址为 192.168.1.101
本机自身开启http服务,
本教程示例为本机既作为发证机构,又作为web服务器,自己给自己签证。

===================================================
开始:在CA 端操作

//签证机构iP 192.168.1.101

//安装mod_ssl
# yum install openssl mod_ssl -y

# cd /etc/pki/CA/

//生成密钥对 cakey.pem
# (umask 077;openssl genrsa -out private/cakey.pem 2048)

//查看生成的公钥
# openssl rsa -in private/cakey.pem -text



//开始申请CA机构的根证书,10年x509类型
# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3655

Country Name (2 letter code) :CN
State or Province Name (full name) []:Zhejiang
Locality Name (eg, city) :Hangzhou
Organization Name (eg, company) ://这里填写你的公司英文名称
Organizational Unit Name (eg, section) []://这里填写你的部门
Common Name (eg, your name or your server's hostname) []:ca.iyunv.com   //这里填写CA机构根的域名
Email Address []:linux_lcl@163.com//填写你的联系邮箱

创建需要的文件
# touch index.txt serial crlnumber
# echo 01 > serial

CA根机构,准备结束,产生CA根证书cacert.pem


==================================================
下面:在web服务端操作
比如我是一个web服务器的网站,下面我要生成自己的私钥key与csr证书请求,然后把这些信息发给CA签证机构帮我签证,签好了再发给我。

下面就是web服务器端生成自己的私钥与证书请求,因为本教程是自己开启web服务,自己又作为CA签证机构,那么,下面的操作还是在本机上面操作。

//生成私钥 1024的长度
cd /etc/httpd/
# mkdir ssl
# cd ssl/
# (umask 077;openssl genrsa-out httpd.key 1024 )

#openssl req -new -key httpd.key -out httpd.csr

Country Name (2 letter code) :CN
State or Province Name (full name) []:Zhejiang
Locality Name (eg, city) :Hangzhou
Organization Name (eg, company) ://这里填写你的公司英文名称
Organizational Unit Name (eg, section) []: //这里填写你的部门
Common Name (eg, your name or your server's hostname) []:www.iyunv.com   //注意:填写你的web服务器的域名
Email Address []:linux_lcl@163.com         
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:    //回车
An optional company name []://回车


// OK,上面操作生成了 私钥httpd.key和签证请求httpd.csr两个文件了
// 如果中途敲错了,把生成的csr删除,再来一次,就可以了
========================================================

// 到现在为止,你的CA发证机构已经准备就绪了,web服务端的私钥key和证书请求文件csr也创建好了
// 那么,开始签证吧
// 把web端的csr文件复制给CA端

cd /etc/pki/CA/
openssl ca -in /etc/httpd/ssl/httpd.csr -out /etc/httpd/ssl/httpd.crt   

// OK 生成crt证书文件!



***********************************************************************
【如果报错】
openssl TXT_DB error number 2 failed to update database
  产生的原因是:
  This thing happens when certificates share common data. You cannot have two certificates that look otherwise the same.

  方法一:
  删除demoCA下的index.txt,并再touch一个
  rm    index.txt
   touch index.txt
  

    方法二:  
    修改demoCA下 index.txt.attr
  将  unique_subject = yes
  改为 unique_subject = no

  方法三:
  将 common name设置成不同的
====================================================
下面在web服务端操作:

看看有没有安装ssl模块
# grep mod_ssl /etc/httpd/conf.d/*.conf

安装mod_ssl
# yum install mod_ssl
vim /etc/httpd/conf.d/ssl.conf


DocumentRoot "/var/www/html" // 去除注释
ServerName www.iyunv.com:443// 去除注释

SSLCertificateFile /etc/httpd/ssl/httpd.crt//指定CA签给我的证书的crt证书的路径
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key//指定本机私钥的路径

保存退出



检查语法
# httpd -t
# service httpd restart
看看有没有443 端口
# netstat -tnlp或者 # ss -tnl
// 那么OK ,443端口已被监听,可以使用https服务了

打开iptables的443端口

iptables -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT

service iptables save

=================================================
访问测试:

安装CA根证书cacert.pem到浏览器端

打开浏览器设置,找到证书导入,安装证书到可信任


如果你没有DNS服务器, 把www.iyunv.com 指向这个web服务器的IP

修改浏览器端主机的host文件,添加
192.168.1.101 www.iyunv.com

微软 C:\Windows\System32\drivers\etc
Linux    vim /etc/hosts
MAC OS    vi /etc/hosts

访问测试https://www.iyunv.com/   OK!



IE浏览器    接受此证书,未出现不信任警告
遨游浏览器    接受此证书,未出现不信任警告
UC浏览器    接受此证书,未出现不信任警告
===================================================
到此,https的基础服务已经可用。







页: [1]
查看完整版本: 架设CA服务器实现https通信,web服务器使用CA自签证书与https通信