为了签署客户端证书,您将需要您控制的 CA 证书。在大多数情况下,购买一个证书是不可能的,因为全球信任的 CA 证书对 Internet 的其余部分来说是一种安全隐患。因此,在这些情况下,您必须创建自己的 CA 并创建自己的服务器和客户端证书。
因此,让我们从一个基本的openssl.conf文件开始,我们将使用它来生成所有这些证书:
[ ca ]
default_ca = CA_default # The default ca section
[ CA_default ]
certs = certs # Where the issued certs are kept
crl_dir = crl # Where the issued crl are kept
database = database.txt # database index file.
new_certs_dir = certs # default place for new certs.
certificate = cacert.pem # The CA certificate
serial = serial.txt # The current serial number
crl = crl.pem # The current CRL
private_key = private\cakey.pem # The private key
RANDFILE = private\private.rnd # private random number file
x509_extensions = v3_usr # The extentions to add to the cert
default_days = 365
default_crl_days = 30 # how long before next CRL
default_md = sha256 # which md to use.
preserve = no # keep passed DN ordering
policy = policy_match
email_in_dn =
[ policy_match ]
commonName = supplied
[ req ]
default_bits = 2048
default_keyfile = privkey.pem
distinguished_name = req_distinguished_name
x509_extensions = v3_ca
[ v3_ca ]
basicConstraints = CA:TRUE
subjectKeyIdentifier = hash
[ v3_usr ]
basicConstraints = CA:FALSE
subjectKeyIdentifier = hash
[ server ]
basicConstraints = CA:FALSE
nsCertType = server
nsComment = "Server Certificate"
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer:always
extendedKeyUsage = serverAuth
keyUsage = digitalSignature, keyEncipherment
[ client ]
basicConstraints = CA:FALSE
nsCertType = client
nsComment = "Client Certificate"
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer:always
extendedKeyUsage = clientAuth
keyUsage = digitalSignature
[ req_distinguished_name ]
此配置文件用于从批处理脚本自动生成证书。如果您需要更多控制或命名选项,则需要根据您的情况进行调整。
因此,要生成 CA,请转到要创建 CA 的目录,将 openssl.conf 放在那里,然后:
PASSWORD="PUT_YOUR_CA_PASSWORD_HERE"
# Make the config CA specific
cat openssl.conf > use.conf
echo "CN=PUT_CA_NAME_HERE" >> use.conf
# Create the necessary files
mkdir keys requests certs
touch database.txt
echo 01 > serial.txt
# Generate your CA key (Use appropriate bit size here for your situation)
openssl genrsa -aes256 -out keys/ca.key -passout pass:$PASSWORD 2048
# Generate your CA req
openssl req -config use.conf -new -key keys/ca.key -out requests/ca.req -passin pass:$PASSWORD
# Make your self-signed CA certificate
openssl ca -config use.conf -selfsign -keyfile keys/ca.key -out certs/ca.crt -in requests/ca.req -extensions v3_ca -passin pass:$PASSWORD -batch
# Cleanup
rm requests/ca.req use.conf
现在生成服务器证书(例如为您的 Web 服务器):
PASSWORD="PUT_YOUR_CA_PASSWORD_HERE"
NAME="PUT_THE_NAME_OF_SERVER_TO_GENERATE_HERE"
# Make the config Server specific
cat openssl.conf > use.conf
echo "CN=$NAME" >> use.conf
openssl req -new -nodes -extensions server -out "requests/$NAME.req" -keyout "$NAME.key" -config use.conf -passin pass:$PASSWORD )
openssl ca -batch -extensions server -keyfile keys/ca.key -cert certs/ca.crt -config use.conf -out "certs/$NAME.crt" -passin pass:$PASSWORD -infiles "requests/$NAME.req"
# Cleanup
rm "requests/$NAME.req" use.conf
现在生成客户端证书:
PASSWORD="PUT_YOUR_CA_PASSWORD_HERE"
NAME="PUT_THE_NAME_OF_CLIENT_TO_GENERATE_HERE"
# Make the config Client specific
cat openssl.conf > use.conf
echo "CN=$NAME" >> use.conf
openssl req -new -nodes -extensions client -out "requests/$NAME.req" -keyout "$NAME.key" -config use.conf -passin pass:$PASSWORD )
openssl ca -batch -extensions client -keyfile keys/ca.key -cert certs/ca.crt -config use.conf -out "certs/$NAME.crt" -passin pass:$PASSWORD -infiles "requests/$NAME.req"
# Cleanup
rm "requests/$NAME.req" use.conf
为客户端和服务器生成密钥和证书之间的唯一区别是,它可以防止被盗的客户端证书也可用于播放服务器并“欺骗”其他客户端连接到它(这只有在您的应用程序支持客户端时才有效)和证书中的服务器扩展)。