Openssl是SSL的开源实现,是一种安全机密程序,主要用于提高远程登录访问的安全性。也是目前加密算法所使用的工具之一,功能很强大。
Openssl为网络通信提供安全及数据完整性的一种安全协议,包括了主要的密码算法、常用的密钥和证书封装管理功能(CA)以及SSL协议,并提供了丰富的应用程序供测试或其它目的使用,例如我们将会使用Openssl实现私有CA,并实现证书颁发。
OpenSSL:SSL的开源实现
libcrypto:通用加密库,提供了各种加密函数
libssl:TLS/SSL协议的实现,基于会话的、实现了身份认证、数据机密性和会话完整性的TLS/SSL库
openssl:多用途的命令行工具;能够实现私有证书颁发机构;即在公司内部实现身份的验证;
SSL:(Secure Socket Layer)安全套接字层,通过一种机制在互联网上提供密钥传输。其主要目标是保证两个应用间通信数据的保密性和可靠性,可在服务器端和用户端同时支持的一种加密算法。目前主流版本SSLV2、SSLV3
一、SSL提供以下功能:
数据的机密性:通过对称加密算法实现数据的机密性。
数据的完整性:通过单向加密算法保证数据的完整性。
身份的安全验证:提供数据发送者的身份。
二、加密类型及功能:
单向加密:提取数据特征码,实现数据完整性验证
对称加密:数据加密,实现数据私密性
公钥加密:使用对方公钥加密,实现秘钥交换;使用自己私钥加密,实现身份验证
三、SSL 会话流程
 
   
1.客户端向服务端发起SSL会话信息,将自己支持的一套加密规则发送给网站。
2.服务端从中选出一组加密算法与HASH算法与浏览器进行协商确认,并将自己的身份信息CA证书的发送给浏览器。
证书信息如图所示

3.验证证书的合法性(颁发证书的机构是否合法,证书是否完整有无篡改、证书是否在CA吊销库中、证书中包含的网站地址是否与正在访问的地址一致等);
4.如果证书不受信任,浏览器会弹出警告信息;等待操作人员确认是否继续,继续极为信任证书执行4步骤,如不信任终止会话;
5.如果证书受信任,或者是用户接受了不受信的证书,浏览器会生成一串随机数的密码(这串密码将作为后续对称加密通讯的初始秘钥使用);
6.使用协商好的HASH算法对随机秘钥进行单向加密计算出指纹信息,并使用客户端私钥对指纹信息进行加密,使用服务端公钥信息对秘钥和加密的指纹信息加密后发送给服务端;
7.服务端使用自己的私钥将密文解开后获取秘钥,使用客户端公钥解密获得秘钥指纹信息并作对比并验证客户端身份;
8.之后所有的通信数据将由之前浏览器生成的随机密码并利用对称加密算法进行加密,并按照协商定期更换秘钥。
CA证书是一个加密会话建立信任的初始阶段,互联网上的知名网站的证书肯定是要想CA认证机构申请的,费用也是相当可观的。而公司内部一些建立于CA之上的通讯完成可以通过Openssl来建立私有的CA服务。
四、使用Openssl建立私有CA
openssl 配置文件 /etc/pki/tls/openssl.cnf
| dir= /etc/pki/CA# Where everything is kept certs           = $dir/certs# Where the issued certs are keptcrl_dir         = $dir/crl# Where the issued crl are keptdatabase        = $dir/index.txt            # database index file.#unique_subject = no                        # Set to ‘no‘ to allow creation of                                            # several ctificates with same subject.new_certs_dir   = $dir/newcerts# default place for new certs.certificate     = $dir/cacert.pem           # The CA certificateserial          = $dir/serial# The current serial numbercrlnumber       = $dir/crlnumber# the current crl number                                            # must be commented out to leave a V1 CRLcrl             = $dir/crl.pem              # The current CRLprivate_key     = $dir/private/cakey.pem    # The private keyRANDFILE        = $dir/private/.rand        # private random number file | 
4.1初始化服务器工作环境
| [root@localhost ~]# touch /etc/pki/CA/{index.txit,serial,crlnumber} #创建证书发送数据库、证书序列号、吊销证书数字文件[root@localhost ~]# echo 100001 > /etc/pki/CA/serial        #建立发放证书初始序列号[root@localhost ~]# echo 000001 > /etc/pki/CA/crlnumber                 #键入吊销证书初始序列号 | 
4.2 创建秘钥对及自认证证书
| [root@localhost ~]# (umask 077; openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)   #创建私钥;(umask 077;)改变umask并只对此子shell有效括号内的命令之行结束后即失效Generating RSA private key, 2048 bit long modulus...............+++.........................................................................................+++e is 65537 (0x10001)[root@localhost ~]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 365You are about to be asked to enter information that will be incorporatedinto 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 blankFor 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) []:shandong                        #定义证书所属省份(州)Locality Name (eg, city) [Default City]:dezhou                        #定义证书所属城市Organization Name (eg, company) [Default Company Ltd]:eway            #定义证书所属组织、公司Organizational Unit Name (eg, section) []:support                     #定义证书所属部门Common Name (eg, your name or your server‘s hostname) []:ca.eway.com  #定义使用此证书的主机名称Email Address []:zhuzw_1203@126.com                                   #定义管理员邮箱###################################################命令参数req: 生成证书签署请求     -news: 新请求     -key /path/to/keyfile: 指定私钥文件     -out /path/to/somefile: 指定生成证书位置     -x509: 生成自签署证书     -days n: 有效天数 | 
4.3 证书申请节点生成私钥及签名请求
| #####生成私钥[root@localhost ~]# (umask 077; openssl genrsa -out /root/cekay.pam 2048)Generating RSA private key, 2048 bit long modulus............................................................+++..............+++e is 65537 (0x10001)#####生成证书签名请求[root@localhost ~]# openssl req -new -key /root/cekay.pam -out /root/cakey.csr You are about to be asked to enter information that will be incorporatedinto 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 blankFor some fields there will be a default value,If you enter ‘.‘, the field will be left blank.-----Country Name (2 letter code) [XX]:cn                        #需要跟CA服务器保持一致             State or Province Name (full name) []:shandong              #需要跟CA服务器保持一致Locality Name (eg, city) [Default City]:dezhou              #需要跟CA服务器保持一致Organization Name (eg, company) [Default Company Ltd]:eway  #需要跟CA服务器保持一致Organizational Unit Name (eg, section) []:wwwCommon Name (eg, your name or your server‘s hostname) []:    Email Address []:zhuzw_1203@126.comPlease enter the following ‘extra‘attributesto be sent with your certificate requestA challenge password []:                                   #证书秘钥(可以不加密码)An optional company name []:                               #可选的公司名称#####将证书签名请求发送给CA服务器[root@localhost ~]# scp /root/cakey.csr mylinux@192.168.13.134:/home/mylinuxmylinux@192.168.13.134‘s password: cakey.csr                                          100% 1050     1.0KB/s00:00 | 
4.4 CA服务器对此证书请求进行签名并返回给此服务器
| [root@localhost ~]# openssl ca -in /home/mylinux/cakey.csr -out /home/mylinux/cakey.crt -days 365Using configuration from /etc/pki/tls/openssl.cnfCheck that the request matches the signatureSignature okCertificate Details:        Serial Number: 1048577 (0x100001)        Validity            Not Before: Oct  5 21:21:14 2015 GMT            Not After : Oct  4 21:21:14 2016 GMT        Subject:            countryName               = cn            stateOrProvinceName       = shandong            organizationName          = eway            organizationalUnitName    = www            commonName                = www.eway.com            emailAddress              = zhuzw_1203@126.com        X509v3 extensions:            X509v3 Basic Constraints:                 CA:FALSE            Netscape Comment:                 OpenSSL Generated Certificate            X509v3 Subject Key Identifier:                 73:0E:C3:34:70:BE:AF:E4:4F:B6:86:5E:94:33:29:E8:84:8F:A6:2D            X509v3 Authority Key Identifier:                 keyid:B1:46:C8:1F:7F:05:63:B5:B5:6E:36:A9:CF:D4:5C:A2:B7:DF:D7:91Certificate is to be certified untilOct  4 21:21:14 2016 GMT (365 days)Sign the certificate? [y/n]:y1 out of 1 certificate requests certified, commit? [y/n]yWrite out database with 1 new entriesData Base Updated#####将证书远程复制给客户机[root@localhost ~]# scp /home/mylinux/cakey.crt root@192.168.13.3:/root/root@192.168.13.3‘s password: cakey.crt                                      100% 4620     4.5KB/s00:00 | 
吊销证书
如果你的私钥泄露了,那么证书就不能使用了,需要去CA服务器申请吊销此证书
| #####获取你证书的序列号和相关信息[root@localhost ~]# openssl x509 -in /root/cakey.crt -noout  -serial  -subjectserial=100001subject= /C=cn/ST=shandong/O=eway/OU=www/CN=#####将此信息及相关的***明信息发送给CA服务管理员,管理员在核对信息无误后对证书进项吊销操作。随后你需要重新申请证书。#####CA服务吊销证书操作[root@localhost ~]# openssl ca -revoke /etc/pki/CA/newcerts/100001.pem     #证书吊销是以序列名称来实现的Using configuration from /etc/pki/tls/openssl.cnfRevoking Certificate 100001.Data Base Updated[root@localhost ~]# openssl ca -gencrl -out /etc/pki/CA/crl/ca.crl         #更新证书吊销列表Using configuration from /etc/pki/tls/openssl.cnf | 
 国庆假期快乐~!
国庆假期快乐~!
Openssl是SSL的开源实现,是一种安全机密程序,主要用于提高远程登录访问的安全性。也是目前加密算法所使用的工具之一,功能很强大。
Openssl为网络通信提供安全及数据完整性的一种安全协议,包括了主要的密码算法、常用的密钥和证书封装管理功能(CA)以及SSL协议,并提供了丰富的应用程序供测试或其它目的使用,例如我们将会使用Openssl实现私有CA,并实现证书颁发。
OpenSSL:SSL的开源实现
libcrypto:通用加密库,提供了各种加密函数
libssl:TLS/SSL协议的实现,基于会话的、实现了身份认证、数据机密性和会话完整性的TLS/SSL库
openssl:多用途的命令行工具;能够实现私有证书颁发机构;即在公司内部实现身份的验证;
SSL:(Secure Socket Layer)安全套接字层,通过一种机制在互联网上提供密钥传输。其主要目标是保证两个应用间通信数据的保密性和可靠性,可在服务器端和用户端同时支持的一种加密算法。目前主流版本SSLV2、SSLV3
一、SSL提供以下功能:
数据的机密性:通过对称加密算法实现数据的机密性。
数据的完整性:通过单向加密算法保证数据的完整性。
身份的安全验证:提供数据发送者的身份。
二、加密类型及功能:
单向加密:提取数据特征码,实现数据完整性验证
对称加密:数据加密,实现数据私密性
公钥加密:使用对方公钥加密,实现秘钥交换;使用自己私钥加密,实现身份验证
三、SSL 会话流程
 
   
1.客户端向服务端发起SSL会话信息,将自己支持的一套加密规则发送给网站。
2.服务端从中选出一组加密算法与HASH算法与浏览器进行协商确认,并将自己的身份信息CA证书的发送给浏览器。
证书信息如图所示

3.验证证书的合法性(颁发证书的机构是否合法,证书是否完整有无篡改、证书是否在CA吊销库中、证书中包含的网站地址是否与正在访问的地址一致等);
4.如果证书不受信任,浏览器会弹出警告信息;等待操作人员确认是否继续,继续极为信任证书执行4步骤,如不信任终止会话;
5.如果证书受信任,或者是用户接受了不受信的证书,浏览器会生成一串随机数的密码(这串密码将作为后续对称加密通讯的初始秘钥使用);
6.使用协商好的HASH算法对随机秘钥进行单向加密计算出指纹信息,并使用客户端私钥对指纹信息进行加密,使用服务端公钥信息对秘钥和加密的指纹信息加密后发送给服务端;
7.服务端使用自己的私钥将密文解开后获取秘钥,使用客户端公钥解密获得秘钥指纹信息并作对比并验证客户端身份;
8.之后所有的通信数据将由之前浏览器生成的随机密码并利用对称加密算法进行加密,并按照协商定期更换秘钥。
CA证书是一个加密会话建立信任的初始阶段,互联网上的知名网站的证书肯定是要想CA认证机构申请的,费用也是相当可观的。而公司内部一些建立于CA之上的通讯完成可以通过Openssl来建立私有的CA服务。
四、使用Openssl建立私有CA
openssl 配置文件 /etc/pki/tls/openssl.cnf
| dir= /etc/pki/CA# Where everything is kept certs           = $dir/certs# Where the issued certs are keptcrl_dir         = $dir/crl# Where the issued crl are keptdatabase        = $dir/index.txt            # database index file.#unique_subject = no                        # Set to ‘no‘ to allow creation of                                            # several ctificates with same subject.new_certs_dir   = $dir/newcerts# default place for new certs.certificate     = $dir/cacert.pem           # The CA certificateserial          = $dir/serial# The current serial numbercrlnumber       = $dir/crlnumber# the current crl number                                            # must be commented out to leave a V1 CRLcrl             = $dir/crl.pem              # The current CRLprivate_key     = $dir/private/cakey.pem    # The private keyRANDFILE        = $dir/private/.rand        # private random number file | 
4.1初始化服务器工作环境
| [root@localhost ~]# touch /etc/pki/CA/{index.txit,serial,crlnumber} #创建证书发送数据库、证书序列号、吊销证书数字文件[root@localhost ~]# echo 100001 > /etc/pki/CA/serial        #建立发放证书初始序列号[root@localhost ~]# echo 000001 > /etc/pki/CA/crlnumber                 #键入吊销证书初始序列号 | 
4.2 创建秘钥对及自认证证书
| [root@localhost ~]# (umask 077; openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)   #创建私钥;(umask 077;)改变umask并只对此子shell有效括号内的命令之行结束后即失效Generating RSA private key, 2048 bit long modulus...............+++.........................................................................................+++e is 65537 (0x10001)[root@localhost ~]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 365You are about to be asked to enter information that will be incorporatedinto 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 blankFor 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) []:shandong                        #定义证书所属省份(州)Locality Name (eg, city) [Default City]:dezhou                        #定义证书所属城市Organization Name (eg, company) [Default Company Ltd]:eway            #定义证书所属组织、公司Organizational Unit Name (eg, section) []:support                     #定义证书所属部门Common Name (eg, your name or your server‘s hostname) []:ca.eway.com  #定义使用此证书的主机名称Email Address []:zhuzw_1203@126.com                                   #定义管理员邮箱###################################################命令参数req: 生成证书签署请求     -news: 新请求     -key /path/to/keyfile: 指定私钥文件     -out /path/to/somefile: 指定生成证书位置     -x509: 生成自签署证书     -days n: 有效天数 | 
4.3 证书申请节点生成私钥及签名请求
| #####生成私钥[root@localhost ~]# (umask 077; openssl genrsa -out /root/cekay.pam 2048)Generating RSA private key, 2048 bit long modulus............................................................+++..............+++e is 65537 (0x10001)#####生成证书签名请求[root@localhost ~]# openssl req -new -key /root/cekay.pam -out /root/cakey.csr You are about to be asked to enter information that will be incorporatedinto 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 blankFor some fields there will be a default value,If you enter ‘.‘, the field will be left blank.-----Country Name (2 letter code) [XX]:cn                        #需要跟CA服务器保持一致             State or Province Name (full name) []:shandong              #需要跟CA服务器保持一致Locality Name (eg, city) [Default City]:dezhou              #需要跟CA服务器保持一致Organization Name (eg, company) [Default Company Ltd]:eway  #需要跟CA服务器保持一致Organizational Unit Name (eg, section) []:wwwCommon Name (eg, your name or your server‘s hostname) []:    Email Address []:zhuzw_1203@126.comPlease enter the following ‘extra‘attributesto be sent with your certificate requestA challenge password []:                                   #证书秘钥(可以不加密码)An optional company name []:                               #可选的公司名称#####将证书签名请求发送给CA服务器[root@localhost ~]# scp /root/cakey.csr mylinux@192.168.13.134:/home/mylinuxmylinux@192.168.13.134‘s password: cakey.csr                                          100% 1050     1.0KB/s00:00 | 
4.4 CA服务器对此证书请求进行签名并返回给此服务器
| [root@localhost ~]# openssl ca -in /home/mylinux/cakey.csr -out /home/mylinux/cakey.crt -days 365Using configuration from /etc/pki/tls/openssl.cnfCheck that the request matches the signatureSignature okCertificate Details:        Serial Number: 1048577 (0x100001)        Validity            Not Before: Oct  5 21:21:14 2015 GMT            Not After : Oct  4 21:21:14 2016 GMT        Subject:            countryName               = cn            stateOrProvinceName       = shandong            organizationName          = eway            organizationalUnitName    = www            commonName                = www.eway.com            emailAddress              = zhuzw_1203@126.com        X509v3 extensions:            X509v3 Basic Constraints:                 CA:FALSE            Netscape Comment:                 OpenSSL Generated Certificate            X509v3 Subject Key Identifier:                 73:0E:C3:34:70:BE:AF:E4:4F:B6:86:5E:94:33:29:E8:84:8F:A6:2D            X509v3 Authority Key Identifier:                 keyid:B1:46:C8:1F:7F:05:63:B5:B5:6E:36:A9:CF:D4:5C:A2:B7:DF:D7:91Certificate is to be certified untilOct  4 21:21:14 2016 GMT (365 days)Sign the certificate? [y/n]:y1 out of 1 certificate requests certified, commit? [y/n]yWrite out database with 1 new entriesData Base Updated#####将证书远程复制给客户机[root@localhost ~]# scp /home/mylinux/cakey.crt root@192.168.13.3:/root/root@192.168.13.3‘s password: cakey.crt                                      100% 4620     4.5KB/s00:00 | 
吊销证书
如果你的私钥泄露了,那么证书就不能使用了,需要去CA服务器申请吊销此证书
| #####获取你证书的序列号和相关信息[root@localhost ~]# openssl x509 -in /root/cakey.crt -noout  -serial  -subjectserial=100001subject= /C=cn/ST=shandong/O=eway/OU=www/CN=#####将此信息及相关的***明信息发送给CA服务管理员,管理员在核对信息无误后对证书进项吊销操作。随后你需要重新申请证书。#####CA服务吊销证书操作[root@localhost ~]# openssl ca -revoke /etc/pki/CA/newcerts/100001.pem     #证书吊销是以序列名称来实现的Using configuration from /etc/pki/tls/openssl.cnfRevoking Certificate 100001.Data Base Updated[root@localhost ~]# openssl ca -gencrl -out /etc/pki/CA/crl/ca.crl         #更新证书吊销列表Using configuration from /etc/pki/tls/openssl.cnf | 
 国庆假期快乐~!
国庆假期快乐~!
原文地址:http://zhuzw.blog.51cto.com/8691727/1705390