创建简单的CA并签名

证书

概念

CA

CA全称Certificate Authority,也叫“证书授权中心”。它是负责管理和签发证书的第三方机构。

X.509

是密码学里公钥证书的格式标准。

指令

平时我们使用openssl最多的莫过于使用指令了,而最为常见的几个指令如下:

genrsa 生成RSA参数

req
x509
rsa
ca

genrsa简介

平时主要用来生成私钥,选择使用的算法、对称加密密码和私钥长度来生成私钥。

基本用法:

openssl genrsa [args] [numbits]

其中常见的参数:【更多参数查看:openssl genrsa -help】

args1 对生成的私钥文件是否要使用加密算法进行对称加密: 
    -des : CBC模式的DES加密 
    -des3 : CBC模式的3DES加密 
    -aes128 : CBC模式的AES128加密 
    -aes192 : CBC模式的AES192加密 
    -aes256 : CBC模式的AES256加密 
args2 对称加密密码
    -passout passwords
    其中passwords为对称加密(des、3des、aes)的密码(使用这个参数就省去了console交互提示输入密码的环节) 
args3 输出文件
    -out file : 输出证书私钥文件 
[numbits]: 密钥长度,理解为私钥长度

生成一个2048位的RSA私钥,并用des3加密(密码为123456),保存为server.key文件

生成私钥(ca-server和node1节点使用)

openssl genrsa -des3 -passout pass:123456 -out server.key   1024 
// -des3 是第一个参数args1;  
// -passout pass:123456 是第二个参数写法 args2
// -out server.key 第三个参数args3;   
// 2048 最后一个[numbits]参数

req

req的基本功能主要有两个:生成证书请求和生成自签名证书,当然这并不是其全部功能,但是这两个最为常见;

常见使用方法:

openssl req [args] outfile

主要参数:【更多参数查看:openssl req -help】

args1 是输入输入文件格式:-inform arg
    -inform DER 使用输入文件格式为DER
    -inform PEM 使用输入文件格式为PEM
args2 输出文件格式:-outform arg   
    -outform DER 使用输出文件格式为DER
    -outform PEM 使用输出文件格式为PEM
args3 是待处理文件 
    -in inputfilepath
args4 待输出文件
    -out outputfilepath
args5 用于签名待生成的请求证书的私钥文件的解密密码
    -passin passwords       
args6 用于签名待生成的请求证书的私钥文件
    -key file
args7指定输入密钥的编码格式 -keyform arg  
    -keyform  DER
    -keyform  NET
     -keyform  PEM
args8 生成新的证书请求 
    -new
​
args9输出一个X509格式的证书,签名证书时使用 
     -x509          
args10使用X509签名证书的有效时间  
    -days  // -days 3650 有效期10年
​
args11生成一个bits长度的RSA私钥文件,用于签发【生成私钥、并生成自签名证书】 
    -newkey rsa:bits 
​
args12设置HASH算法-[digest]【生成私钥指定的hash摘要算法】
    -md5
    -sha1  // 高版本浏览器开始不信任这种算法
    -md2
    -mdc2
    -md4
args13指定openssl配置文件,很多内容不容易通过参数配置,可以指定配置文件
    -config filepath   
args14 显示格式txt【用于查看证书、私钥信息】
    -text

使用的案例:利用私钥生成证书请求csr

在node1节点使用,在向ca服务器申请的那台机器上面执行

openssl req -new -key server.key -out server.csr

使用案例:利用私钥生成自签名证书

这是在ca服务器上面进行执行的

openssl req -new -x509 -days 3650 -key ca.key -out ca.crt

x509

x509是一个功能很丰富的证书处理工具。可以用来显示证书的内容,转换其格式,给CSR签名等X.509证书的管理工作;

用法如下:

openssl x509 [args]

参数如下:【更多参数查看:openssl x509 -help】

args1 是输入输入文件格式:-inform arg
    -inform DER 使用输入文件格式为DER
    -inform PEM 使用输入文件格式为PEM
args2 输出文件格式:-outform arg   
    -outform DER 使用输出文件格式为DER
    -outform PEM 使用输出文件格式为PEM
args3 是待处理X509证书文件 
    -in inputfilepath
args4 待输出X509证书文件
    -out outputfilepath
args5表明输入文件是一个"请求签发证书文件(CSR)",等待进行签发
    -req            
args6签名证书的有效时间  
    -days  // -days 3650 有效期10年      
args7 指定用于签发请求证书的根CA证书 
    -CA arg 
args8 根CA证书格式(默认是PEM)     
    -CAform arg     
args9 指定用于签发请求证书的CA私钥证书文件    
    -CAkey arg      
args10 指定根CA私钥证书文件格式(默认为PEM格式)
    -CAkeyform arg  
args11 指定序列号文件(serial number file)    
    -CAserial arg   
args12 如果序列号文件(serial number file)没有指定,则自动创建它 
    -CAcreateserial 
args13设置HASH算法-[digest]【生成私钥指定的hash摘要算法】
    -md5
    -sha1  // 高版本浏览器开始不信任这种算法
    -md2
    -mdc2
    -md4

使用实例: 使用根CA证书[ca.crt]和私钥[ca.key]对"请求签发证书"[server.csr]进行签发,生成x509格式证书

openssl x509 -req -days 3650 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out serverx509.crt
​
server.csr:想ca申请ca证书的那台服务器传给ca服务器的
ca.crt:ca服务器上面的(openssl req -new -x509 -key /etc/pki/CA/private/ca.pem -out /etc/pki/CA/ca.crt -days 365 自签名生成的)
ca.key:ca服务器申请的私钥(umask 077;openssl genrsa -out /etc/pki/CA/private/ca.key2048)

ca-server节点

一、搭建一台CA服务器

CA证书根目录/etc/pki/CA

1、创建索引数据库文件和指定证书编号

touch /etc/pki/CA/index.txt
echo 01 >/etc/pki/CA/serial

touch /etc/pki/CA/index.txt 生成证书索引数据库文件

echo 01 > /etc/pki/CA/serial 指定第一个颁发证书的序列号

2、CA自签名证书

2.1、生成私钥:

(umask 077;openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)

()代表在子bash中运行,目的是为了不改变当前Shell中的umask值

genrsa 生成私钥

-out 私钥的存放路径 cakey.pem 为密钥名需与配置文件中一致,配置文件是openssl.conf

2048 密钥长度

2.2、生成自签名证书:

openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 365

req 生成证书签署请求

-x509 生成自签署证书

-days n 证书的有效天数

-new 新请求

-key /path/to/keyfile 指定私钥文件

-out /path/to/somefile 输出文件位置

node1节点

二、客户机申请证书

1、生成私钥

(umask 066;openssl genrsa -out app.key 1024)

2、生成申请文件:

 openssl req -new -key app.key -out app.csr

证书申请一般通用csr

3、把签署请求文件发送给CA服务器

scp app.csr ca-server:/etc/pki/CA/

三、CA服务器签署证书

1、在CA服务器上签署证书,CA服务器为客户端颁发证书

openssl ca -in app.csr -out certs/app.crt -days 100

2、发送给客户机申请者,将生成的证书发送给申请的客户端

 scp certs/app.crt 192.168.128.129:

四、吊销证书

(一)在ca-server节点上面请求吊销node1证书

1、获取证书serial

 openssl x509 -in certs/app.crt -noout -serial -subject

x509 证书格式

-in 要吊销的证书

-noout 不输出额外信息

-serial 显示序列号

-subject 显示subject信息

(二)CA验证信息

1、获取证书serial

 openssl x509 -in certs/app.crt -noout -serial -subject

2、确认提交的serial和subject信息与index.txt文件中的信息是否一致

cat index.txt

3、吊销证书

openssl ca -revoke newcerts/01.pem

-revoke 删除证书

查看被吊销的证书列表

4、指定吊销证书的编号(如果是第一次吊销)。根据1步骤中得到的编号

echo 01 > /etc/pki/CA/crlnumber

5、更新证书吊销列表、生成吊销列表

openssl ca - -out /etc/pki/CA/crl.pem

-gencrl 生成证书吊销列表

6、查看crl文件内容

openssl crl -in crl.pem -nosalt -text

7、查看证书状态

openssl ca -status 1

1 证书serial(序列号)

实例

1、创建CA文件目录

root@autosar-virtual-machine:~#cd /usr/local/openssl/ssl
​
root@autosar-virtual-machine:/usr/local/openssl/ssl# mkdir demoCA
​
root@autosar-virtual-machine:/usr/local/openssl/ssl# cd demoCA
​
root@autosar-virtual-machine:/usr/local/openssl/ssl/demoCA# mkdir newcerts private crlnumber certs
​
root@autosar-virtual-machine:/usr/local/openssl/ssl/demoCA# touch index.txt
​
root@autosar-virtual-machine:/usr/local/openssl/ssl/demoCA# echo 01 >serial
​
root@autosar-virtual-machine:/usr/local/openssl/ssl/demoCA# ls
certs  crlnumber  index.txt  newcerts  private  serial

2、创建客户端私钥及申请

root@autosar-virtual-machine:/usr/local/openssl/ssl/demoCA# mkdir client
root@autosar-virtual-machine:/usr/local/openssl/ssl/demoCA# cd client/
root@autosar-virtual-machine:/usr/local/openssl/ssl/demoCA/client# (umask 066;openssl genrsa -out app.key 1024)
Generating RSA private key, 1024 bit long modulus (2 primes)
..............................................................+++++
.................................+++++
e is 65537 (0x010001)
root@autosar-virtual-machine:/usr/local/openssl/ssl/demoCA/client#  openssl req -new -key app.key -out app.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) [AU]:CN
State or Province Name (full name) [Some-State]:LiaoNing  
Locality Name (eg, city) []:ShenYang
Organization Name (eg, company) [Internet Widgits Pty Ltd]:RuiChi
Organizational Unit Name (eg, section) []:YanFaZhiLiang
Common Name (e.g. server FQDN or YOUR name) []:gdw
Email Address []:9820@qq.com
​
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456
An optional company name []:ruichi

3、生成服务端私钥及自签证书

root@autosar-virtual-machine:/usr/local/openssl/ssl/demoCA/client# cd ..
root@autosar-virtual-machine:/usr/local/openssl/ssl/demoCA# (umask 077;openssl genrsa -out private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus (2 primes)
.......................+++++
................................................+++++
e is 65537 (0x010001)
root@autosar-virtual-machine:/usr/local/openssl/ssl/demoCA# openssl req -new -x509 -key private/cakey.pem -out 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) [AU]:CN
State or Province Name (full name) [Some-State]:LiaoNing
Locality Name (eg, city) []:ShenYang
Organization Name (eg, company) [Internet Widgits Pty Ltd]:RuiChi
Organizational Unit Name (eg, section) []:YanFaZhiLiang
Common Name (e.g. server FQDN or YOUR name) []:gdw1
Email Address []:980@qq.com

4、验签并返回证书

root@autosar-virtual-machine:/usr/local/openssl/ssl/demoCA# cd ..
root@autosar-virtual-machine:/usr/local/openssl/ssl# openssl ca -in demoCA/client/app.csr -out demoCA/certs/app.crt -days 100
Using configuration from /usr/local/openssl/ssl/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Oct 12 06:41:12 2021 GMT
            Not After : Jan 20 06:41:12 2022 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = LiaoNing
            organizationName          = RuiChi
            organizationalUnitName    = YanFaZhiLiang
            commonName                = gdw
            emailAddress              = 9820@qq.com
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                35:EB:4D:A2:E1:16:5F:02:7E:2D:58:93:5B:BB:9C:0D:C9:AD:06:36
            X509v3 Authority Key Identifier: 
                keyid:88:7B:BF:C4:6A:F5:F7:1A:4F:47:48:70:B0:77:D8:D4:C9:64:D1:E4
​
Certificate is to be certified until Jan 20 06:41:12 2022 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

5、吊销证书

root@autosar-virtual-machine:/usr/local/openssl/ssl#  openssl x509 -in demoCA/certs/app.crt -noout -serial -subject 
serial=01
subject=C = CN, ST = LiaoNing, O = RuiChi, OU = YanFaZhiLiang, CN = gdw, emailAddress = 982090165@qq.com
root@autosar-virtual-machine:/usr/local/openssl/ssl# openssl ca -revoke demoCA/newcerts/01.pem
Using configuration from /usr/local/openssl/ssl/openssl.cnf
Revoking Certificate 01.
Data Base Updated
root@autosar-virtual-machine:/usr/local/openssl/ssl# echo 01 > demoCA/crlnumber
root@autosar-virtual-machine:/usr/local/openssl/ssl# openssl ca -gencrl -out demoCA/crl.pem
Using configuration from /usr/local/openssl/ssl/openssl.cnf
root@autosar-virtual-machine:/usr/local/openssl/ssl# openssl crl -in demoCA/crl.pem 
-----BEGIN X509 CRL-----
MIICHzCCAQcCAQEwDQYJKoZIhvcNAQELBQAwgYYxCzAJBgNVBAYTAkNOMREwDwYD
VQQIDAhMaWFvTmluZzERMA8GA1UEBwwIU2hlbllhbmcxDzANBgNVBAoMBlJ1aUNo
aTEWMBQGA1UECwwNWWFuRmFaaGlMaWFuZzENMAsGA1UEAwwEZ2R3MTEZMBcGCSqG
SIb3DQEJARYKOTgwQHFxLmNvbRcNMjExMDI1MDgyMjAxWhcNMjExMTI0MDgyMjAx
WjA8MBICAQEXDTIxMTAyNTA3MzkzOFowEgIBAhcNMjExMDI1MDc1MDI1WjASAgED
Fw0yMTEwMjUwODIxMTRaoA4wDDAKBgNVHRQEAwIBAzANBgkqhkiG9w0BAQsFAAOC
AQEAMejkkCCKMb40D3H/zj8g13xerf096Meel7FT4R6Kv9r8R7HdIZjFyP3YQkxu
TDAQsxk1ie0BOLHQ/jZjpNxNB6LImXdGQePkzVrgWKql3146NqrSKJ6uzUbei6Tv
DaooDTyoILwizjS5gj8E9MLnnbjlGR7XEH49SSfWu4FzoHtUie6CAZUG2yUgc7It
zt3EmWfsYw88KzdzTkp3aJ/AGLVyB9Ql6bSBNOf7Av2G5Pu1Y8tf/hKZ/jDl8sp2
N2KbGqXDSIK1GVAP+8S8pWagsShSH/Zt61XdC2gZIz/A8FOGmc8XKD6M0P+Tn4nx
WjHQHQ5541RzbKXlAEKoIbvbRQ==
-----END X509 CRL-----
root@autosar-virtual-machine:/usr/local/openssl/ssl# openssl ca -status 1
Using configuration from /usr/local/openssl/ssl/openssl.cnf
01=Revoked (R)
root@autosar-virtual-machine:/usr/local/openssl/ssl# 

常见问题

openssl.cnf

执行:openssl version -a

OpenSSL 1.0.1e-fips 11 Feb 2013 built on: Mon Jun 29 12:45:07 UTC 2015 platform: linux-x86_64 options: bn(64,64) md2(int) rc4(16x,int) des(idx,cisc,16,int) idea(int) blowfish(idx) compiler: gcc -fPIC -DOPENSSL_PIC -DZLIB -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -DKRB5_MIT -m64 -DL_ENDIAN -DTERMIO -Wall -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -Wa,--noexecstack -DPURIFY -DOPENSSL_IA32_SSE2 -DOPENSSL_BN_ASM_MONT -DOPENSSL_BN_ASM_MONT5 -DOPENSSL_BN_ASM_GF2m -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM -DMD5_ASM -DAES_ASM -DVPAES_ASM -DBSAES_ASM -DWHIRLPOOL_ASM -DGHASH_ASM OPENSSLDIR: "/etc/pki/tls" engines: rdrand dynamic

其中OPENSSLDIR对应的路径就是openssl.conf文件的路径

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值