安全之证书与CA篇

申请证书

  • 申请证书的流程
    a主向互联网的一些ca审请证书;提交一些相关信息和自己的公钥.这时候ca会用在自己的私钥签名a主机的公钥
    a向b送自己的公钥;由于CA是权威机构所以b有ca的公钥;所以b可以打开由ca私钥加密的a的公钥;通过间接的CA来交换公钥

opssl 的介绍

加密文件与解密文件
 openssl enc -e -des3 -a -salt -in fstab -out fstab.ssl    加密文件fstab文件输出至fstab.ssl 
openssl enc -d -des3 -a -salt -in fstab.ssl -out fstab.l   解密fstab.ssl  文件输出至fstab.l

哈希值运算

[root@centos7 ~]# openssl dgst fstab
MD5(fstab)= 6565565f82e586bce20e7ea08058d26d
[root@centos7 ~]# md5sum fstab
6565565f82e586bce20e7ea08058d26d  fstab
[root@centos7 ~]# 
[root@centos7 ~]# openssl passwd -1 
Password: 
Verifying - Password: 
$1$3x5F7nVj$n7R2Lss4M.eqVkwRDQtnn0

生成随机数
openssl rand -base64|-hex NUM
生成公钥与私钥
openssl
[root@centos7 ~]# openssl genrsa -out /app/chenxi.ke -des 2048 并用对称密钥对私钥加密
[root@centos7 ~]# (umask 066; openssl genrsa -out yuer.key -des 2048) 创建密钥并用对称密钥加密;设置好文件的权限

创建ca服务器

####################################################################
[ ca ]
default_ca      = CA_default            # The default ca section   可以创建多个CA,ca_default是默认的CA

####################################################################
[ CA_default ]  默认CA的相关配置

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  CA证书
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  CA私钥文件路径
RANDFILE        = $dir/private/.rand    # private random number file

x509_extensions = usr_cert              # The extentions to add to the cert

# Comment out the following two lines for the "traditional"
# (and highly broken) format.
name_opt        = ca_default            # Subject Name options
cert_opt        = ca_default            # Certificate field options

# Extension copying option: use with caution.
# copy_extensions = copy

# Extensions to add to a CRL. Note: Netscape communicator chokes on V2 CRLs
# so this is commented out by default to leave a V1 CRL.
# crlnumber must also be commented out to leave a V1 CRL.
# crl_extensions        = crl_ext

default_days    = 365                   # how long to certify for证书颁发有效期
default_crl_days= 30                    # how long before next CRL30天发布一下证书吊销列表
default_md      = sha256                # use SHA-256 by default 哈希算法
preserve        = no                    # keep passed DN ordering

# A few difference way of specifying how similar the request should look
# For type CA, the listed attributes must be the same, and the optional
# and supplied fields are just that :-)
policy          = policy_match 策略匹配;默认使用的策略

# For the CA policy
[ policy_match ]
countryName             = match   国家名match必须与CA匹配optional可以不匹配
stateOrProvinceName     = match 州
organizationName        = match 组织
organizationalUnitName  = optional 部门
commonName              = supplied 主机或域名
emailAddress            = optional 邮件地址

# For the 'anything' policy
# At this point in time, you must list all acceptable 'object'
# types.
[ policy_anything ]
countryName             = optional
stateOrProvinceName     = optional
localityName            = optional
organizationName        = optional
organizationalUnitName  = optional
commonName              = supplied
emailAddress            = optional

创建CA的自签名证书

[root@centos7 app]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 9000
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) []:chenxi  省份必须一致
Locality Name (eg, city) [Default City]:chenxi  市 
Organization Name (eg, company) [Default Company Ltd]:chenxideshijie  公司名字
Organizational Unit Name (eg, section) []:ai部门
Common Name (eg, your name or your server's hostname) []:chenxi.ca   ca服务器名
Email Address []:chenxi@123.com 邮箱 可以不写

查看证书问价


客户端申请证书
创建私钥文件

[root@chenxi ~]# (umask 066; openssl genrsa -out /app/service.key 1024 )
生成证书请求
[root@chenxi ~]# openssl req -new -key /app/service.key -out /app/service.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) []:chenxi  省名字必须与CA的一致
Locality Name (eg, city) [Default City]:chenxideyue  市的名字可以不一致
Organization Name (eg, company) [Default Company Ltd]:chenxideshijie 公司名字名字必须一致          
Organizational Unit Name (eg, section) []:shenghuo 部门名字不一致
Common Name (eg, your name or your server's hostname) []:chenxi  域名 
Email Address []:邮件地址

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: 
An optional company name []:

将客户端的申请证书文件传给CA服务器

[root@chenxi ~]# scp /app/service.csr 172.16.251.154:/etc/pki/CA/

为管理方便创建一个目录存放用户申请证书文件
[root@centos7 app]# mkdir /etc/pki/CA/csr
CA服务器为用户签署正整数
表示没有创建数据库/etc/pki/CA/index.txt



这个错误是没有此/etc/pki/CA/serial

[root@centos7 CA]# openssl ca -in /etc/pki/CA/crl/service.csr -out /etc/pki/CA/certs/service.crt -days 100
Using configuration from /etc/pki/tls/openssl.cnf
/etc/pki/CA/serial: No such file or directory
error while loading serial number
140233124767648:error:02001002:system library:fopen:No such file or directory:bss_file.c:398:fopen('/etc/pki/CA/serial','r')
140233124767648:error:20074002:BIO routines:FILE_CTRL:system lib:bss_file.c:400:

表示 /etc/pki/CA/serial的格式不对

Using configuration from /etc/pki/tls/openssl.cnf
unable to load number from /etc/pki/CA/serial
error while loading serial number
140223783708576:error:0D066096:asn1 encoding routines:a2i_ASN1_INTEGER:short line:f_int.c:215:

应该这样创建此/etc/pki/CA/serial文件
[root@centos7 CA]# echo 01 > /etc/pki/CA/serial
创建证书

[root@centos7 CA]# openssl ca -in /etc/pki/CA/crl/service.csr -out /etc/pki/CA/certs/service.crt -days 100   100 只有效的天数
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: Jul 17 02:51:30 2017 GMT
            Not After : Oct 25 02:51:30 2017 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = chenxi
            organizationName          = chenxideshijie
            organizationalUnitName    = shenghuo
            commonName                = chenxi
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                B8:AF:B5:28:90:17:97:75:21:35:A4:8A:EF:3D:15:A2:23:1D:D0:6B
            X509v3 Authority Key Identifier: 
                keyid:DC:42:0A:44:AF:2B:33:77:09:4C:6F:76:AE:7B:4C:EE:03:1D:84:4F

Certificate is to be certified until Oct 25 02:51:30 2017 GMT (100 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@centos7 CA]# openssl ca -in /etc/pki/CA/crl/service.csr -out /etc/pki/CA/certs/service.crt -days 100
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: Jul 17 02:51:30 2017 GMT
            Not After : Oct 25 02:51:30 2017 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = chenxi
            organizationName          = chenxideshijie
            organizationalUnitName    = shenghuo
            commonName                = chenxi
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                B8:AF:B5:28:90:17:97:75:21:35:A4:8A:EF:3D:15:A2:23:1D:D0:6B
            X509v3 Authority Key Identifier: 
                keyid:DC:42:0A:44:AF:2B:33:77:09:4C:6F:76:AE:7B:4C:EE:03:1D:84:4F

Certificate is to be certified until Oct 25 02:51:30 2017 GMT (100 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@centos7 CA]# openssl ca -in /etc/pki/CA/crl/service.csr -out /etc/pki/CA/certs/service.crt -days 100
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: Jul 17 02:51:30 2017 GMT
            Not After : Oct 25 02:51:30 2017 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = chenxi
            organizationName          = chenxideshijie
            organizationalUnitName    = shenghuo
            commonName                = chenxi
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                B8:AF:B5:28:90:17:97:75:21:35:A4:8A:EF:3D:15:A2:23:1D:D0:6B
            X509v3 Authority Key Identifier: 
                keyid:DC:42:0A:44:AF:2B:33:77:09:4C:6F:76:AE:7B:4C:EE:03:1D:84:4F

Certificate is to be certified until Oct 25 02:51:30 2017 GMT (100 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@centos7 CA]# tree
.
├── cacert.pem
├── certs  客户生成证书的存放目录
│   └── service.crt
├── crl
│   └── service.csr
├── csr
├── index.txt
├── index.txt.attr
├── index.txt.old
├── newcerts 新生成的
│   └── 01.pem
├── private
│   └── cakey.pem
├── serial
└── serial.old

5 directories, 10 files

查看证书的信息


如果同一台机器生成多个证书,不需要多个私钥
同一个私钥生成另一个请求


吊销证书



创建吊销序列号文件


更新吊销列表

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,898评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,401评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,058评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,539评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,382评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,319评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,706评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,370评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,664评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,715评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,476评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,326评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,730评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,003评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,275评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,683评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,877评论 2 335

推荐阅读更多精彩内容