1首先要生成一个自己的私钥mypvk.pem
这个密钥一定要保持好,不能泄露
openssl genrsa -out mypvk.pem
2, 通过这个私钥,自签名生成服务器的证书
2.1 openssl req -new -keymypvk.pem -out rootCA.csr
2.2 openssl ca -selfsign -in rootCA.csr
会生成一个 服务器自己的证书,拷贝到配置中指定的目录下,命名保持一致
3,通过生成的证书和私钥,给其他用户颁发证书
openssl ca -in user1.csr
既生成了目标证书
关于openssl.cnf配置示例:
[default]
name = root-ca #* 变量*/
default_ca = CA_default
name_opt = ca_default
cert_opt = ca_default
[CA_default]
home = . #* 变量*
database = $home/db/index
serial = $home/db/serial
crlnumber = $home/db/crlnumber
certificate = $home/$name.crt
private_key = $home/private/$name.key
RANDFILE = $home/private/random
new_certs_dir = $home/certs
unique_subject = no
copy_extensions = none
default_days = 3650
default_crl_days = 365
default_md = sha256
policy = policy_to_match
[policy_to_match]
countryName = match
stateOrProvinceName = optional
organizationName = match
organizationalUnitName = optional
commonName = supplied
emailAddress = optional
[CA_DN]
countryName = "C"
contryName_default = "CN"
organizationName = "O"
organizationName_default = "jmu"
commonName = "CN"
commonName_default = "longshuai.com"
[req]
default_bits = 4096
encrypt_key = no
default_md = sha256
utf8 = yes
string_mask = utf8only
# prompt = no /* 测试时该选项导致出错,所以将其注释掉*/
distinguished_name = CA_DN
req_extensions = ca_ext
[ca_ext]
basicConstraints = critical,CA:true
keyUsage = critical,keyCertSign,cRLSign
subjectKeyIdentifier = hash
其中Country Name、State or Province Name、Organization Name和Common Name必须提供,且前三者必须和CA的subject中的对应项完全相同。这些是由配置文件中的匹配策略决定的。
"match"表示openssl ca要签署的证书请求文件中的项要和CA证书中的项匹配,即要相同,"supplied"表示必须要提供的项,"optional"表示可选项,所以可以留空。