ssl简介与openssl的使用

SSL证书:  是数字证书的一种,相似于驾驶证、护照和营业执照的电子副本。由于配置在服务器上,也称为SSL服务器证书。linux

ssl也是传输协议。算法

基于ssl协议开发的一款软件叫opensslapache

linux系统默认已经安装安全

基本功能

OpenSSL整个软件包大概能够分红三个主要的功能部分:SSL协议库、应用程序以及 密码算法库。OpenSSL的目录结构天然也是围绕这三个功能部分进行规划的。
做为一个基于密码学的安全开发包,OpenSSL提供的功能至关强大和全面,囊括了主要的密码算法、经常使用的 密钥和证书封装管理功能以及SSL协议,并提供了丰富的应用程序供测试或其它目的使用。
 
对称加密
OpenSSL一共提供了8种对称加密算法,其中7种是分组加密算法,仅有的一种流加密算法是RC4。这7种分组加密算法分别是AES、DES、Blowfish、CAST、IDEA、RC二、RC5,都支持电子密码本模式(ECB)、加密分组连接模式(CBC)、加密反馈模式(CFB)和输出反馈模式(OFB)四种经常使用的 分组密码加密模式。其中,AES使用的加密反馈模式(CFB)和输出反馈模式(OFB)分组长度是128位,其它算法使用的则是64位。事实上,DES算法里面不单单是经常使用的DES算法,还支持三个 密钥和两个密钥3DES算法。

非对称加密

OpenSSL一共实现了4种非对称 加密算法,包括 DH算法、 RSA算法、 DSA算法椭圆曲线算法(EC)。DH算法通常用于 密钥交换。RSA算法既能够用于密钥交换,也能够用于 数字签名,固然,若是你可以忍受其缓慢的速度,那么也能够用于 数据加密。DSA算法则通常只用于数字签名。
信息摘要
OpenSSL实现了5种信息摘要算法,分别是MD二、MD五、MDC二、SHA(SHA1)和 RIPEMD。SHA算法事实上包括了SHA和SHA1两种信息摘要算法。此外,OpenSSL还实现了DSS标准中规定的两种信息摘要算法DSS和DSS1。

openssl服务器

配置文件:测试

/etc/pki/tls/openssl.conf网站

dir=/etc/pki/CA      <<<指定CA工做目录ui

certs=$dir/certs    <<<指定撤销存储库的位置加密

crl_dir=$dir/crl      <<<证书撤销列表spa

database=$dir/index.txt  <<<已生成的证书信息的索引文件(此文件默认不存在,须要本身建立)

new_certs_dir=$dir/newcerts <<<新签发的证书的保存位置

certificate=$dir/cacert.pem <<<CA本身的证书名称

serial  = $dir/serial  <<<记录证书的序号,默承认以从1开始(此文件默认不存在,内容也不能为空,须要手动往里面添加一个数字)

crlnumber = $dir/crlnumber <<<吊销的证书序列号

crl = $dir/crl.pem<<< 吊销的证书的列表

private_key  = $dir/private/cakey.pem <<< CA本身的私钥的位置

RANDFILE   = $dir/private/.rand <<<随机数文件位置

x509_extensions = usr_cert  <<<扩展项

建立CA

    1.建立不存在的文件

        index,txt

        serial

   2.给本身(CA)建立证书

实现步骤

 1.建立那些不存在的文件

[root@ ~]# touch /etc/pki/CA/index.txt
[root@ ~]# echo "01">/etc/pki/CA/serial
[root@~]# cat/etc/pki/CA/serial
[root@~]# cat /etc/pki/CA/serial
01

2.给CA建立证书

 (1)生成CA的私钥文件

[root@ ~]# openssl genrsa -out /etc/pki/CA/private/cakey.pem 1024  <<<数字越大加密强度越大,然而小消耗系统资源也越多
Generating RSA private key, 1024 bit long modulus
.......++++++
....++++++
e is 65537 (0x10001)

 (2)从私钥文件中抽取公钥,并制做证书

-new:申请新的证书

-x509:证书版本号,509是给CA本身建立证书的准用选项

-key:指定私钥文件

-days:指定证书有效期

[root@~]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem  -days 360 -out /etc/pki/CA/cacert.pem
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) [XX]:CN <<<国家名字,只能用两个字母表示
State or Province Name (full name) []:henan <<<所在省
Locality Name (eg, city) [Default City]:zhengzhou <<< 所在市                
Organization Name (eg, company) [Default Company Ltd]:baidu 组织名(公司名)
Organizational Unit Name (eg, section) []:nuomi <<<组织单位名
Common Name (eg, your name or your server's hostname) []:www.baidu.com <<<公司网站域名(必定不能错,不然证书无效)
Email Address []:123456789@126.com <<<  邮箱地址                

 给其余主机制做颁发证书的流程

1.客户端生成一个证书请求文件

2.客户端将证书申请文件发送到CA请求证书

3.检查证书请求文件中的信息的真伪,若是为真,则制做证书并颁发个客户端

实现步骤(以给apache颁发证书为例,服务器端10.220.5.67,客户端10.220.5.63

1.给apache建立私钥文件(用于存放私钥,公钥以及颁发的证书)

 (1)生成客户端私钥

[root@apache ~]# mkdir /etc/httpd/ssl
[root@apache ~]# openssl genrsa -out /etc/httpd/ssl/httpd.key 1024
Generating RSA private key, 1024 bit long modulus
........................................................++++++
........................................++++++
e is 65537 (0x10001)

 (2)生成请求文件

[root@apache ~]# openssl req -new -key /etc/httpd/ssl/httpd.key -days 365 -out /etc/httpd/ssl/httpd.req

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) [XX]:CN State or Province Name (full name) []:henan 
Locality Name (eg, city) [Default City]:zhengzhou Organization Name (eg, company) [Default Company Ltd]:baidu Organizational Unit Name (eg, section) []:nuomi Common Name (eg, your name or your server's hostname) []:www.baidu.com
Email Address []:123456789@126.com 
Please enter the following 'extra' attributesto be sent with your certificate request
A challenge password []: <<<对生成的证书进行加密,这里省略
An optional company name []:

 (3)查看生成的文件

[root@apache ~]# ls /etc/httpd/ssl/
httpd.key  httpd.req<<此文件就是生成的请求文件

(4)将请求文件发送到服务器端

[root@apache ~]# scp /etc/httpd/ssl/httpd.req root@10.220.5.67:/tmp
The authenticity of host '10.220.5.67 (10.220.5.67)' can't be established.
ECDSA key fingerprint is SHA256:Fi2Rlnl2uce8/7OiRG1JReD158iHVydpZ+bW+IgoutY.
ECDSA key fingerprint is MD5:d0:96:1f:a9:83:fc:0a:bf:1f:20:1b:ec:4d:79:6e:7e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.220.5.67' (ECDSA) to the list of known hosts.
root@10.220.5.67's password: 
httpd.req                                                      100%  700   321.8KB/s   00:00   

2.服务端检查请求文件中的信息的真实性(在服务端操做)

[root@e ~]# openssl ca -in /tmp/httpd.req -out /tmp/httpd.crt -days 7000
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  3 16:53:28 2018 GMT
            Not After : Jan  2 16:53:28 2038 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = henan
            organizationName          = baidu
            organizationalUnitName    = nuomi
            commonName                = www.baidu.com
            emailAddress              = 123456789@126.com 
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                90:DA:6B:61:2B:D1:3D:AD:10:45:47:15:6D:9D:A6:B0:AB:7B:80:39
            X509v3 Authority Key Identifier: 
                keyid:50:EF:77:9C:65:CD:D1:42:C2:FC:9E:C4:09:82:16:9C:BB:41:0C:18

Certificate is to be certified until Jan  2 16:53:28 2038 GMT (7000 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

2)将生成的证书发送给客户端

[root@ ~]# scp /tmp/httpd.crt root@10.220.5.63:/etc/httpd/ssl
httpd.crt                                                      100% 3233     1.1MB/s   00:00