使用Openssl构建私有CA

使用Openssl构建私有CA

Openssl是SSL的开源实现,是一种安全机密程序,主要用于提高远程登录访问的安全性。也是目前加密算法所使用的工具之一,功能很强大。

    Openssl为网络通信提供安全及数据完整性的一种安全协议,包括了主要的密码算法、常用的密钥和证书封装管理功能(CA)以及SSL协议,并提供了丰富的应用程序供测试或其它目的使用,例如我们将会使用Openssl实现私有CA,并实现证书颁发。

OpenSSL:SSL的开源实现  

     libcrypto:通用加密库,提供了各种加密函数  

     libssl:TLS/SSL协议的实现,基于会话的、实现了身份认证、数据机密性和会话完整性的TLS/SSL库  

     openssl:多用途的命令行工具;能够实现私有证书颁发机构;即在公司内部实现身份的验证; 

     SSL:(Secure Socket Layer)安全套接字层,通过一种机制在互联网上提供密钥传输。其主要目标是保证两个应用间通信数据的保密性和可靠性,可在服务器端和用户端同时支持的一种加密算法。目前主流版本SSLV2、SSLV3

一、SSL提供以下功能:

数据的机密性:通过对称加密算法实现数据的机密性。  

数据的完整性:通过单向加密算法保证数据的完整性。  

身份的安全验证:提供数据发送者的身份。


二、加密类型及功能:

单向加密:提取数据特征码,实现数据完整性验证

对称加密:数据加密,实现数据私密性

公钥加密:使用对方公钥加密,实现秘钥交换;使用自己私钥加密,实现身份验证

三、SSL 会话流程

https会话.jpg   ssl.jpg


1.客户端向服务端发起SSL会话信息,将自己支持的一套加密规则发送给网站。 

2.服务端从中选出一组加密算法与HASH算法与浏览器进行协商确认,并将自己的身份信息CA证书的发送给浏览器。

证书信息如图所示

x509.jpg

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 kept
crl_dir         = $dir/crl                  # Where the issued crl are kept
database        = $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 certificate
serial          = $dir/serial               # The current serial number
crlnumber       = $dir/crlnumber            # the current crl number
                                            # must be commented out to leave a V1 CRL
crl             = $dir/crl.pem              # The current CRL
private_key     = $dir/private/cakey.pem    # The private key
RANDFILE        = $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 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) [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 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                        #需要跟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) []:www
Common Name (eg, your name or your server's hostname) []:    
Email Address []:zhuzw_1203@126.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:                                   #证书秘钥(可以不加密码)
An optional company name []:                               #可选的公司名称
#####将证书签名请求发送给CA服务器
[root@localhost ~]# scp /root/cakey.csr mylinux@192.168.13.134:/home/mylinux
mylinux@192.168.13.134's password: 
cakey.csr                                          100% 1050     1.0KB/s   00:00

   4.4 CA服务器对此证书请求进行签名并返回给此服务器

[root@localhost ~]# openssl ca -in /home/mylinux/cakey.csr -out /home/mylinux/cakey.crt -days 365
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate 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:91

Certificate is to be certified until Oct  4 21:21:14 2016 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@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/s   00:00

吊销证书

如果你的私钥泄露了,那么证书就不能使用了,需要去CA服务器申请吊销此证书

#####获取你证书的序列号和相关信息
[root@localhost ~]# openssl x509 -in /root/cakey.crt -noout  -serial  -subject
serial=100001
subject= /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.cnf
Revoking 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构建私有CA国庆假期快乐~!

原创文章,作者:东郭先生,如若转载,请注明出处:http://www.178linux.com/8576

(0)
上一篇 2015-10-07 12:00
下一篇 2015-10-08 21:39

相关推荐

  • 干货 | 20 行 Python 代码创建可视化地图

    当我开始建造Vincent时, 我的一个目的就是使得地图的建造尽可能合理化. 有一些很棒的python地图库-参见Basemap 和 Kartograph能让地图更有意思. 我强烈推荐这两个工具, 因为他们都很好用而且很强大. 我想有更简单一些的工具,能依靠Vega的力量并且允许简单的语法点到geoJSON文件,详细描述一个投影和大小/比列,最后输出地图. …

    2017-09-08
  • 马哥教育网络班25期-第3周作业

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 [root@localhost ~]# who root     tty1         2016-12-…

    Linux干货 2016-12-19
  • 8.1-用户和组(作业篇)

     1、将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中。 [root@localhost ~]# tr a-z A-Z </etc/issue > /tmp/issue.out [root@localhost ~]#&n…

    Linux干货 2016-08-04
  • Linux进程管理之“四大名捕”

    一、四大名捕 四大名捕,最初出现于温瑞安创作的武侠小说,是朝廷中正义力量诸葛小花的四大徒弟,四人各怀绝技,分别是轻功暗器高手“无情”、内功卓越的高手“铁手”、腿功惊人的“追命”和剑法一流的“冷血”本文四大名捕由linux命令所出演      无情:ps     出演      铁…

    Linux干货 2016-04-25
  • raid 0磁盘阵列

    raid0 1  先给sdb磁盘和sdc磁盘分区 [root@localhost ~]# fdisk /dev/sdb    Command (m for help): n Command action   e   extended   p   primary part…

    Linux干货 2017-04-25
  • 正则表达式基础

    一、正则表达式: 元字符是用来阐释字符表达式意义的字符,简言之,就是用来描述字符的字符。 正则表达式RE(Regular Expression)是由一串字符和元字符构成的字符串。 正则表达式的主要功能是文本查询和字符串操作,它可以匹配文本的一个字符或字符集合。实际上正则表达式完成了数据的过滤,将不满足正则表达式定义的数据拒绝掉,剩下与正则表达式匹配的数据。 …

    Linux干货 2017-06-04