使用openssl实现私有CA的搭建和证书的颁发

2022-10-22,,,,

ca的相关该概念

  • pki:public key infrastructure 公共密钥加密体系
  • ca:certificate authority,证书签发机构.实现身份的验证的一个机构。

ca工作逻辑

  • a和b通信需要将彼此的公钥传递给对方,但是直接传递公钥不安全。
  • 通过将公钥传递给认证机构,认证机构对公钥进行签名(私钥加密,公钥解密)并加上一些属性信息。
  • ca机构将证书制作完成后再返还给用户。
  • 用户将证书发送给需要通信的一方,对方通过使用ca机构的公钥来进行解密得到他的公钥。

根ca:根ca用于管理下级ca,子ca向根ca获取授权,使得它能给用户颁发证书。

cert:证书

证书:ca对用户公钥进行签名后形成的一个文件。

证书的来源:

  • 自签名证书
  • ca机构颁发的证书

ca机构颁发的证书流程:

  • 用户生成证书请求文件:.csr
  • 将证书请求文件发送给ca
  • ca进行签名并颁发证书

csr是英文certificate signing request的缩写,即证书签名请求

私有ca的搭建

可以通过以下工具来搭建私有ca

使用openssl搭建私有ca:

openssll和私有ca搭建相关的配置文件

里面包含了很多和证书相关的设置,后续创建对应文件的时候需要根据配置文件中的信息进行创建。

[root@centos8 tls]#  vim /etc/pki/tls/openssl.cnf
[ ca ]
default_ca      = ca_default            # 默认使用的ca

####################################################################
[ ca_default ]
dir             = /etc/pki/ca           # 存放和ca相关的文件的目录(centos7这个文件默认存在)
certs           = $dir/certs            # 存放颁发的证书  cert:证书
crl_dir         = $dir/crl              # 存放被吊销的证书
database        = $dir/index.txt        # 存放ca的索引(需要人为创建)
new_certs_dir   = $dir/newcerts         # 存放新证书的位置
certificate     = $dir/cacert.pem       # ca的自签名证书
serial          = $dir/serial           # 证书的编号(第一次需要人为创建并编号,后面会自动递增) serial:连续的
crlnumber       = $dir/crlnumber        # 证书吊销列表的编号
crl             = $dir/crl.pem          # 证书吊销列表的文件
private_key     = $dir/private/cakey.pem# ca的私钥
x509_extensions = usr_cert              # the extensions to add to the cert
name_opt        = ca_default            # subject name options
cert_opt        = ca_default            # certificate field options

policy          = policy_match  #指定使用的匹配策略

# for the ca policy
[ policy_match ]
countryname             = match
stateorprovincename     = match
organizationname        = match
organizationalunitname  = optional
commonname              = supplied
emailaddress            = optional

三种策略:match匹配、optional可选、supplied提供

  • match:要求申请填写的信息跟ca设置信息必须一致(前三项)
  • optional:可有可无,跟ca设置信息可不一致
  • supplied:必须填写这项申请信息

范例:centos8搭建私有ca

流程:

1.创建对应的文件和目录(创建的文件和目录需要和配置文件里面的信息对应)

2.生成ca自己的私钥

3.利用ca的私钥生成自签名证书

1. 创建对应的文件和目录

[root@centos8 ca]# mkdir -pv /etc/pki/ca/{certs,crl,newcerts,private}
mkdir: created directory '/etc/pki/ca/certs' #存放颁发的证书文件
mkdir: created directory '/etc/pki/ca/crl'  #存放吊销的证书文件
mkdir: created directory '/etc/pki/ca/newcerts' #存放新生成的证书文件
mkdir: created directory '/etc/pki/ca/private'  #存放ca自己的私钥

#证书的数据库文件:存放证书的颁发等信息,不需要人工维护里面的内容,只需要创建对应的文件就行了,会自动往里面写入数据的
[root@centos8 ~]# touch /etc/pki/ca/index.txt

#颁发证书的序号(十六进制):第一个证书颁发的时候使用的就是这个编号,后续会自动递增
[root@centos8 ~]# echo 01 > /etc/pki/ca/serial

2. 生成ca自己的私钥

[root@centos8 ca]# openssl genrsa -out private/cakey.pem 2048
generating rsa private key, 2048 bit long modulus (2 primes)
..........................+++++
........................................................................+++++
e is 65537 (0x010001)

3.颁发自签名证书

[root@centos8 ca]# openssl req -new -x509 -key /etc/pki/ca/private/cakey.pem -days 3650 -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) []:sc
locality name (eg, city) [default city]:cd
organization name (eg, company) [default company ltd]:sx
organizational unit name (eg, section) []:ll
common name (eg, your name or your server's hostname) []:tom
email address []:111

选项:
-new:创建一个新的证书,生成新证书签署请求

-x509:表示证书的格式,专用于ca生成自签证书

-key:生成请求时用到的私钥文件

-days n:证书的有效期限

-out /path/to/somecertfile: 证书的保存路径

查看自签名证书的方法

查看自签名证书的信息
[root@centos8 ~]#openssl x509 -in /etc/pki/ca/cacert.pem -noout -text

-in:指定输入的文件

-noout:不输出为文件

-text:以文本方式来进行显示

说明:
颁发自签名证书的时候会要求输入需要输入国家、身份、组织等信息。

用户向私有ca申请证书的流程

1.生成私钥文件

2.通过私钥文件生成证书申请文件,若是match这种策略。填写的 国家 省 组织必须一致

3.ca颁发证书

4.查看证书

1.生成私钥文件

私钥一般使用key作为后缀要标识

[root@centos8 ca]# mkdir /data/app1

[root@centos8 app1]# openssl genrsa -out /data/app1/app1.key 2048
generating rsa private key, 2048 bit long modulus (2 primes)
.................................+++++
...............................................................................................+++++
e is 65537 (0x010001)

2.通过私钥文件生成证书申请文件

证书申请文件的后缀一般都是以csr为后缀作为标识

[root@centos8 app1]# openssl req -new -key /data/app1/app1.key -out /data/app1/app1.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) []:cn #省份
locality name (eg, city) [default city]:cn #组织
organization name (eg, company) [default company ltd]:cn
organizational unit name (eg, section) []:cn
common name (eg, your name or your server's hostname) []:cn
email address []:cn

please enter the following 'extra' attributes
to be sent with your certificate request
a challenge password []:
an optional company name []:

说明:

采用match这种策略,默认有三项内容必须和ca一致:国家,省份,组织,如果不同,会出现下面的提示

如果采用的是option这种策略的话就不用保持一致都可以

3. ca 颁发证书

ca需要使用用户的证书申请文件才能颁发证书,利用证书申请文件里面的用户私钥来实现数字签名。

[root@centos8 app1]# openssl ca -in /data/app1/app1.csr  -out /etc/pki/ca/certs/app1.crt -days 1000
using configuration from /etc/pki/tls/openssl.cnf
check that the request matches the signature
signature ok
certificate details:
        serial number: 15 (0xf)
        validity
            not before: oct 14 06:53:07 2022 gmt
            not after : jul 10 06:53:07 2025 gmt
        subject:
            countryname               = cn
            stateorprovincename       = cn
            organizationname          = cn
            organizationalunitname    = cn
            commonname                = cn
            emailaddress              = cn
        x509v3 extensions:
            x509v3 basic constraints: 
                ca:false
            netscape comment: 
                openssl generated certificate
            x509v3 subject key identifier: 
                12:c5:3f:8e:86:e4:e8:3c:06:b1:01:79:90:ea:b6:66:32:53:3e:6a
            x509v3 authority key identifier: 
                keyid:10:59:cd:c9:34:58:5e:30:67:43:0a:3e:dd:7c:63:2b:9c:60:50:3a

certificate is to be certified until jul 10 06:53:07 2025 gmt (1000 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@centos8 ca]# tree
.
├── cacert.pem
├── certs
│   └── app1.crt #给用户生成的生成的证书文件
├── crl
├── index.txt
├── index.txt.attr
├── index.txt.old  #前一个文件的备份
├── newcerts
│   └── 0f.pem #和app1.crt是同一个东西,自动生成的一个备份文件
├── private
│   └── cakey.pem
├── serial
└── serial.old

4 directories, 9 files

#serial:存放的是下一个证书的证书编号

查看证书的有效性

[root@centos8 ca]# openssl ca -status 0f  #0f就是这个证书的标号
using configuration from /etc/pki/tls/openssl.cnf
0f=valid (v)

v:标识生效的   r:标识无效的证书

查看证书的信息

openssl x509 -in /etc/pki/ca/certs/app1.crt -noout -text

例如

[root@centos8 ca]# openssl x509 -in /etc/pki/ca/certs/app1.crt -noout -issuer
issuer=c = cn, st = cn, l = cn, o = cn, ou = cn, cn = cn, emailaddress = cn

证书文件后缀

后缀规定:
.crt #证书文件的标识

.csr #证书申请文件的标识  证书申请完成后,这个证书申请文件就没啥用了

.key #私钥的标识  .pem也是私钥的标识,但是windows不是别pem结尾的文件

一个证书申请文件只能申请一次证书。

实现一个申请文件申请多个证书的方法;

root@centos8 ca]# pwd
/etc/pki/ca
[root@centos8 ca]# cat index.txt.attr 
unique_subject = yes


unique_subject = yes  #把yes变为no就可以了

证书的吊销

openssl ca -revoke /path/file

例如:

[root@centos8 ca]# openssl ca -revoke certs/app1.crt
using configuration from /etc/pki/tls/openssl.cnf
revoking certificate 0f.
data base updated

[root@centos8 ca]# openssl ca -status 0f 
using configuration from /etc/pki/tls/openssl.cnf
0f=revoked (r)

[root@centos8 ca]# cat /etc/pki/ca/index.txt
r	250710065307z	221014072459z	0f	unknown	/c=cn/st=cn/o=cn/ou=cn/cn=cn/emailaddress=cn

生成证书吊销列表文件

公开被吊销的文件。其他用户可以获取已经吊销了的证书文件列表

#需要创建一个clinumer文件才可以  吊销证书也需要一个吊销证书的number 类似于index.txt

#这个文件默认不存在,需要手动创建出来

echo 01 > /etc/pki/ca/crlnumber

openssl ca -gencrl -out /etc/pki/ca/crl.pem   #证书吊销文件的路径是约定好的

到此这篇关于使用openssl实现私有ca的搭建和证书的颁发的文章就介绍到这了,更多相关openssl私有ca的搭建颁发内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《使用openssl实现私有CA的搭建和证书的颁发.doc》

下载本文的Word格式文档,以方便收藏与打印。