openssl实现私有CA及https的服务配置
openssl简介
openSSL是一款功能强大的加密工具,我们当中许多人已经在使用openSSL,用于创建RSA私钥或证书签名请求。不过,你可知道可以使用openSSL来测试计算机速度?或者还可以用它来对文件或消息进行加密。
openssl是一个开源程序的套件、这个套件有三个部分组成:
一是libcryto:这是一个具有通用功能的加密库,里面实现了众多的加密库
二是 libssl:这个是实现ssl机制的,他是用于实现TLS/SSL的功能
三是openssl是个多功能命令行工具,他可以实现加密解密,甚至还可以 当CA来用,可以让你创建证书、吊销证书
这里我们用openssl enc对一个文件进行加密和解密
#创建一个测试文件,使用openssl来验证加密和解密 [root@linux-node1 ~]# vim test.txt this is my test file this is my test file #对test.txt文件做加密处理,生成一个加密文件text.txt.des3 [root@linux-node1 ~]# openssl enc -des3 -a -salt -in test.txt -out test.txt.des3 enter des-ede3-cbc encryption password: #对加密文件设置密码 Verifying - enter des-ede3-cbc encryption password: [root@linux-node1 ~]# cat test.txt.des3 U2FsdGVkX19E3XvnXPhr7AgrRq1tl8lPia87RDPe3ypNbU73t+FCVosiSG9offTr EpxcgUc1kGu6HI8dfX8BAsBvK6EdgTZCinTSIYStOa5sO9zAM5uPifEIjyssp27v c0xLub9ETtJF5LTkGlOhJ4on5klNGBYQMYNsc5udigvWP9KgKWNLWQ== #删除原始文件test.txt,使用openssl是否可以解密加密文件来还原原始文件test.txt [root@linux-node1 ~]# rm -f test.txt [root@linux-node1 ~]# openssl enc -des3 -d -a -salt -in test.txt.des3 -out test.txt [root@linux-node1 ~]# cat test.txt this is my test file this is my test file
数字证书:
证书格式通常是x509的数字证书的格式、还有pkcs等。
对于x509这种证书内容当中都包含哪些呢:
1、公钥和公钥的有效期限。
2、持有者的个人合法身份信息、这个信息有可能是一个公司、也可能是个人、也可以是主机名。
3、证书的使用方式、比如用来进行主机之间的认证等。
4、CA(证书颁发机构)的信息
5、CA的数字签名、CA的证是自签证书
公钥加密、也叫非对称加密:
公钥加密最大的特性就是密钥成对的、公钥称为public key(pkey)、私钥称为secret key(skey)、一般而言、公钥用来加密、私钥用来解密、如果要实现电子签名那就是私钥来用加密、公钥用来解密、而公钥是可以给任何人的、私钥就得自 己保存;公钥加密一般不会加来对数据加密、因为他的加密速度很慢、比对称加密慢3个数量级(一个数量级是10倍、3个就是1000倍)、所以公钥加密通常 用于密钥交换(IKE)和身份认证的。
他的常用算法有:RSA和EIGamal、目前RSA是比较广泛的加密算法、而DSA(Digital Signature Algorithm)只能加来做签名、而无法加于加密的算法
他的工具通常用:gpg、openssl rsautl
单向加密、也叫hash算法:(One-Way加密)
用不生成数据指纹的、也叫数据摘要算法、输出是定长的、MD5是128位定长输出、SHA1定长输出160位、他的特性是不会出现碰撞的、每位数据只要 有一位不一样就会产生巨大的变化、我们称这种为雪崩效应、常用的算法MD5、SHA1、SHA512、常用工具有sha2sum、md5sum、 cksum、openssl dgst。
虽然加密可以使用不同工具来加密,但只要是使用相同的加密算法,无论用什么加密工具得出来的结果应该都是一样的。如下
[root@linux-node1 ~]# sha1sum test.txt 122fe708b51c15b36fc19576ad2cf0a870059299 test.txt [root@linux-node1 ~]# openssl dgst -sha1 test.txt SHA1(test.txt)= 122fe708b51c15b36fc19576ad2cf0a870059299 [root@linux-node1 ~]# md5sum test.txt a8e55a89cadd9a90abfe0ba2c27399f3 test.txt [root@linux-node1 ~]# openssl dgst -md5 test.txt MD5(test.txt)= a8e55a89cadd9a90abfe0ba2c27399f3
创建私有CA(即创建私有的证书颁发机构)
环境配置:linux-node1:作为证书颁发机构(CA) linux-node2:证书申请单位 win7:访问https的客户端
创建私有CA
创建证书颁发机构的证书的时候会调用openssl的配置文件的一些默认设置,所以要先修改openssl的配置文件
[root@linux-node1 ~]# vim /etc/pki/tls/openssl.cnf dir = /etc/pki/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. new_certs_dir = $dir/newcerts # default place for new certs. certificate = $dir/cacert.pem # The CA certificate serial = $dir/serial # The current serial number crlnumber = $dir/crlnumber # the current crl number crl = $dir/crl.pem # The current CRL private_key = $dir/private/cakey.pem# The private key RANDFILE = $dir/private/.rand # private random number file x509_extensions = usr_cert # The extentions to add to the cert [ 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 = SH localityName = Locality Name (eg, city) localityName_default = Shanghai 0.organizationName = Organization Name (eg, company) 0.organizationName_default = goser organizationalUnitName = Organizational Unit Name (eg, section) organizationalUnitName_default = Tech
进入到/etc/pki/CA目录下,创建证书颁发机构的私钥和自签证书
[root@linux-node1 ~]# cd /etc/pki/CA #创建证书颁发机构的私钥 [root@linux-node1 CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048) [root@linux-node1 CA]# ll private/cakey.pem -rw------- 1 root root 1675 Mar 13 20:37 private/cakey.pem #根据私钥创建证书颁发机构的自签证书 [root@linux-node1 CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3650 Country Name (2 letter code) [CN]: State or Province Name (full name) [SH]: Locality Name (eg, city) [Shanghai]: Organization Name (eg, company) [goser]: Organizational Unit Name (eg, section) [Tech]: Common Name (eg, your name or your server's hostname) []:ca.goser.com Email Address []:caadmin@goser.com
根据openssl的配置文件中的描述,在/etc/pki/CA目录下还需要创建一些文件和文件夹
[root@linux-node1 CA]# mkdir crl certs newcerts #index.txt文件是签发证书的列表,serial是签发证书的索引,随着签发增加,索引也随着增加,先给其一个默认值01 [root@linux-node1 CA]# touch index.txt serial [root@linux-node1 CA]# echo 01 >serial
私有CA创建完成后,下面就要对证书申请单位签发证书了,比如对需要提供https加密的ssl web服务签发证书,这里需要注意的是,web服务端申请的证书填写的主机名一定要和web服务提供的虚拟主机名保持一致,下面以httpd服务为例:
#创建私钥和证书存放目录 [root@linux-node2 ~]# mkdir -p /etc/httpd/ssl [root@linux-node2 ~]# cd /etc/httpd/ssl/ #创建私钥 [root@linux-node2 ssl]# (umask 077;openssl genrsa -out httpd.key 1024) #生成证书签署请求,这里的填写的信息一定要和证书颁发机构信息保持一致,也可以将证书颁发机构的openssl的配置文件拷贝过来 [root@linux-node1 tls]# scp openssl.cnf 192.168.1.190:/etc/pki/tls/ [root@linux-node2 ssl]# openssl req -new -key httpd.key -out httpd.csr Country Name (2 letter code) [CN]: State or Province Name (full name) [SH]: Locality Name (eg, city) [Shanghai]: Organization Name (eg, company) [goser]: Organizational Unit Name (eg, section) [Tech]: Common Name (eg, your name or your server's hostname) []:www.goser.com Email Address []:wwwadmin@goser.com Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []: [root@linux-node2 ssl]# ll total 8 -rw-r--r-- 1 root root 696 Mar 13 20:47 httpd.csr -rw------- 1 root root 887 Mar 13 20:44 httpd.key
证书申请单位再完成了证书签署请求后,就可以将这个证书签署请求发送到证书颁发机构对其进行签署认证,签署认证后再将此签署完成后的证书发送给证书申请单位即可,这样就完成了通过第三方机构证书颁发申请
[root@linux-node2 ssl]# scp httpd.csr 192.168.1.180:/tmp/
#颁发机构签署这个证书签署请求,并设置证书的过期时间为1年
[root@linux-node1 tmp]# openssl ca -in httpd.csr -out 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: 2 (0x2)
Validity
Not Before: Mar 13 12:51:36 2018 GMT
Not After : Mar 13 12:51:36 2019 GMT
Subject:
countryName = CN
stateOrProvinceName = SH
organizationName = goser
organizationalUnitName = Tech
commonName = www.goser.com
emailAddress = wwwadmin@goser.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
3A:67:E4:3B:F1:4E:48:C0:8D:88:AE:72:82:B5:7E:66:CD:CC:A5:FF
X509v3 Authority Key Identifier:
keyid:B3:2A:D4:46:99:C1:35:CC:25:E1:95:64:C6:1D:4F:0C:E0:32:01:43
Certificate is to be certified until Mar 13 12:51:36 2019 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
[root@linux-node1 tmp]# scp httpd.crt 192.168.1.190:/etc/httpd/ssl/
这时候再证书颁发机构上查看index.txt和serial文件的变化如下:
[root@linux-node1 CA]# cat index.txt V 190313125136Z 01 unknown /C=CN/ST=SH/O=goser/OU=Tech/CN=www.goser.com/emailAddress=wwwadmin@goser.com [root@linux-node1 CA]# cat serial 02
基于ssl加密的https的服务配置
在linux-node2安装完httpd服务并配置好虚拟主机后,要想让http服务成为https服务的话,就需要安装一个ssl的认证模块
[root@linux-node2 ~]# yum install -y mod_ssl
配置ssl认证模块的配置文件
[root@linux-node2 ~]# cd /etc/httpd/conf.d/ [root@linux-node2 conf.d]# cp ssl.conf ssl.conf.bak [root@linux-node2 conf.d]# vim ssl.conf <VirtualHost 192.168.1.190:443> ServerName www.goser.com #和httpd配置文件中的虚拟主机名对应一致 DocumentRoot "/var/www/html" #和httpd的配置文件中的对应的虚拟主机目录一致 SSLEngine on SSLCertificateFile /etc/httpd/ssl/httpd.crt SSLCertificateKeyFile /etc/httpd/ssl/httpd.key #检查httpd的配置语法: [root@linux-node2 conf.d]# httpd -t #重启httpd服务 [root@linux-node2 conf.d]# systemctl restart httpd #最后检查端口,查看443端口是否启用 netstat -lntup
最后将证书颁发机构的证书拷贝到window的客户端,并且让客户端安装这个证书到受信任的根目录中,同时修改windows客户端的hosts文件,添加192.168.1.190 www.goser.com这样在windows客户端使用浏览器访问地址https://www.goser.com的时候就不会出现警告信息了



待证书导入成功后,https的服务配置就完成了,客户端就可以正常地访问web的ssl加密的https服务了。
浙公网安备 33010602011771号