【前言】
说来惭愧,干了快一年的运维,能力还是很欠缺,前些天因为ToB项目需求,需要用nginx搭建一个正向代理,研究了一番,在本地环境搭建一套七层代理,请移步这里查看。自认为理解了,其实不然,真正到ToB客户环境搭建的时候还是无从下手。听我扯了这么多,这些和本文有什么关系呢?当然有,这个七层代理的是https,中间人代理的话,就需要用到自建CA证书,那么问题来了,CA证书是什么呢?请移步这里查看。CA证书如何获取呢?请听我细细道来......
【OpenSSL自签普通证书】
大致流程如下
一、创建index.txt、serial文件
二、生成CA根证书
1.创建根证书私钥
2.使用根证书私钥创建一个自签根证书的申请
3.使用申请和私钥签发根证书
三、生成自签证书
1.创建自签证书私钥
2.创建一个自签证书申请
3.使用自签的根证书对自签证书申请进行签署
在造证书之前我们先来看看配置文件openssl.conf的一些说明吧
#CA配置相关说明:
[ ca ]
default_ca = CA_default # The default ca section 默认CA
####################################################################
[ CA_default ] 默认CA包含的信息
dir = /etc/pki/CA # Where everything is kept CA的公共目录
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
下一个证书颁发的编号 16进制数,默认不存在需要手动先创建,并且指定第一个证书的开始编号,serial为16进制数00开始
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
要添加到证书的扩展
default_days = 365 # how long to certify for 默认ca有效期
default_crl_days= 30 # how long before next CRL
定义多少天公布新的吊销证书名单
default_md = sha256 # use SHA-256 by default 默认加密算法
preserve = no # keep passed DN ordering
#创建CA和申请证书选项说明:
policy = policy_match
# For the CA policy
[ policy_match ]
countryName = match match的参数意味着签发的证书和根证书必须要保持一致
#stateOrProvinceName = match
#organizationName = match
stateOrProvinceName = optional
organizationName = optional 申请非根自签证书时组织必填项,即用户是什么组织
organizationalUnitName = optional optional 不关心是否一样
commonName = supplied supplied是必须提供的,即网站域名
emailAddress = optional
一、创建index.txt、serial文件
从配置文件可以看到,存放颁发证书的数据库文件index.txt和证书颁发的编号serial两个文件是必须要手动创建的。从配置文件里看到,默认公共目录是/etc/pki/CA,当然你也可以复制openssl.conf自定义配置到其他的路径也行。这点在后面的创建多IP/域名的自签证书里我会讲到,这里先用自带的默认配置文件生成根文件。
[root@VM118 CA]# touch index.txt
[root@VM118 CA]# echo 00 > serial
二、生成CA根证书
1、创建根证书私钥
这里openssl用到的是rsa算法,业界公认2048位是最安全的
openssl genrsa -out scwiperoot.key 2048
你可以选择一步到位,不创建申请文件,直接创建根证书
openssl req -x509 -new -sha512 -days 36500 -subj "/C=cn/ST=shenzhen/L=shenzhen/O=example/OU=Personal/CN=scwipe.com" -key scwiperoot.key -out scwipetestroot.crt
2、使用生成的根私钥创建一个根申请证书(.csr格式)
Common Name必填,表明你创建的这个CA根的机构名字
[root@VM118 CA]# openssl req -new -key scwiperoot.key -out scwiperoot.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
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:scwipe.com
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
3、使用根证书私钥签发申请证书
[root@VM118 CA]# openssl x509 -req -days 36500 -in scwiperoot.csr -signkey scwiperoot.key -out scwiperoot.crt
Signature ok
subject=/C=CN/L=Default City/O=Default Company Ltd/CN=scwipe.com
Getting Private key
三、生成自签证书
1、创建自签证书的私钥
[root@VM118 CA]# openssl genrsa -out scwipeserver.key 2048
2、创建自签证书申请(.csr格式)(此为单域名签发,多域名签发将在另外一篇文章讲解)
自签SSL证书时,申请组织(Organization Name)必填项,Country必须和根证书一致,这里我填的CN
[root@VM118 CA]# openssl req -new -key scwipeserver.key -out scwipeserver.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
State or Province Name (full name) []:GuangDong
Locality Name (eg, city) [Default City]:ShenZhen
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:www.scwipe.com
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
3、使用根证书和私钥签发server申请证书
[root@VM118 CA]# openssl ca -in scwipeserver.csr -cert scwiperoot.crt -keyfile scwiperoot.key -out scwipeserver.crt -days 36500
4、验证自签SSL证书是否ok
[root@VM118 CA]# openssl verify -verbose -CAfile scwiperoot.crt scwipeserver.crt
scwipeserver.crt: OK
至此,我们就完成了根证书的自签,以及使用自签的根证书签发单域名SSL证书,后面还会继续更新用自签的根证书签发多域名/IP的SSL证书。