嵌入式系统证书链

时间:2018-08-08 12:20:55

标签: ssl openssl certificate ssl-certificate

我正在制造许多启用了https的嵌入式设备,因此需要为其生成SSL证书。这些设备在客户的基于本地ip的网络上运行(通常不连接到较大的Internet),因此需要为每个设备的ip地址分配证书。

我正在研究的解决方案是拥有一个ROOT证书,两个中间证书级别(一个用于生产计算机,一个用于每个设备)以及一个针对每个设备的最终服务器证书。由于可以为这些设备配置新的ip地址,因此每次为它们分配新的ip时,它们都需要生成新的证书。

ROOT CA -> Production CA +--> Device 1 CA -> Device Certificate
                         |
                         +--> Device 2 CA -> Device Certificate
                         |
                         :
                         |
                         +--> Device N CA -> Device Certificate

使用OpenSSL生成此证书链,我无法验证设备CA,因此无法验证设备证书。根据以下代码,无法从根CA验证到设备CA的问题是什么?

这是为了生成根证书:

cd /root
mkdir -p ca

cd ca
mkdir certs crl newcerts private
chmod 700 private
touch index.txt
openssl rand -hex 16 > serial

openssl genrsa -aes256 -out private/ca.key.pem 4096
chmod 400 private/ca.key.pem

openssl req -config openssl.cnf \
        -key private/ca.key.pem \
        -new -x509 -days 7300 \
        -batch -sha256 \
        -extensions v3_ca -out certs/ca.cert.pem
chmod 444 certs/ca.cert.pem

这是生成生产机器的中间证书

mkdir /root/ca/jenkins
cd /root/ca/jenkins/
mkdir certs crl csr newcerts private
chmod 700 private
touch index.txt
echo 1001 > serial
echo 1001 > /root/ca/jenkins/crlnumber

cd /root/ca
openssl genrsa -aes256 \
        -out jenkins/private/jenkins.key.pem 4096
chmod 400 jenkins/private/jenkins.key.pem
openssl req -config jenkins/openssl.cnf \
        -extensions v3_intermediate_ca \
        -batch -new -sha256 \
        -key jenkins/private/jenkins.key.pem \
        -out jenkins/csr/jenkins.csr.pem

openssl ca -config openssl.cnf -extensions v3_ca -batch \
        -days 3650 -notext -md sha256 \
        -in jenkins/csr/jenkins.csr.pem \
        -out jenkins/certs/jenkins.cert.pem
chmod 444 jenkins/certs/jenkins.cert.pem

cat jenkins/certs/jenkins.cert.pem \
    certs/ca.cert.pem > jenkins/certs/ca-chain.cert.pem

openssl verify -verbose -CAfile certs/ca.cert.pem \
               jenkins/certs/ca-chain.cert.pem
chmod 444 jenkins/certs/ca-chain.cert.pem

验证成功到此阶段。现在是第二个中间证书:

serial="00051"

mkdir -p /root/ca/$serial
cd /root/ca/$serial/
mkdir certs crl csr newcerts private
chmod 700 private
touch index.txt
echo 1002 > serial
echo 1002 > /root/ca/$serial/crlnumber

cd /root/ca
openssl genrsa -aes256 -out $serial/private/$serial.key.pem 4096
chmod 400 $serial/private/$serial.key.pem

openssl req -config $serial/openssl.cnf \
        -extensions v3_intermediate_ca \
        -batch -passin pass:hamedunit -new -sha256 \
        -key $serial/private/$serial.key.pem \
        -out $serial/csr/$serial.csr.pem

openssl ca -config jenkins/openssl.cnf \
        -extensions v3_intermediate_ca \
        -batch -days 3650 -notext -md sha256 \
        -in $serial/csr/$serial.csr.pem \
        -out $serial/certs/$serial.cert.pem
chmod 444 $serial/certs/$serial.cert.pem

cat $serial/certs/$serial.cert.pem \
    jenkins/certs/ca-chain.cert.pem > $serial/certs/$serial-chain.cert.pem
chmod 444 $serial/certs/$serial-chain.cert.pem

最后一个无法通过以下消息进行验证:

openssl verify -verbose \
        -CAfile certs/ca.cert.pem \
        $serial/certs/$serial.cert.pem
error 20 at 0 depth lookup:unable to get local issuer certificate

但是可以使用生产证书进行验证:

openssl verify -verbose \
        -CAfile jenkins/certs/ca-chain.cert.pem \
        $serial/certs/$serial.cert.pem
00051/certs/00051.cert.pem: OK

我正在使用的配置文件是:

# OpenSSL root CA configuration file.
# Copy to `/root/ca/openssl.cnf`.

[ ca ]
# `man ca`
default_ca = CA_default

[ CA_default ]
# Directory and file locations.
dir               = /root/ca
certs             = $dir/certs
crl_dir           = $dir/crl
new_certs_dir     = $dir/newcerts
database          = $dir/index.txt
serial            = $dir/serial
RANDFILE          = $dir/private/.rand

# The root key and root certificate.
private_key       = $dir/private/ca.key.pem
certificate       = $dir/certs/ca.cert.pem

# For certificate revocation lists.
crlnumber         = $dir/crlnumber
crl               = $dir/crl/ca.crl.pem
crl_extensions    = crl_ext
default_crl_days  = 30

# SHA-1 is deprecated, so use SHA-2 instead.
default_md        = sha256

name_opt          = ca_default
cert_opt          = ca_default
default_days      = 375
preserve          = no
policy            = policy_loose

[ policy_strict ]
# The root CA should only sign intermediate certificates that match.
# See the POLICY FORMAT section of `man ca`.
countryName             = match
stateOrProvinceName     = match
organizationName        = match
organizationalUnitName  = optional
commonName              = supplied
emailAddress            = optional

[ policy_loose ]
# Allow the intermediate CA to sign a more diverse range of certificates.
# See the POLICY FORMAT section of the `ca` man page.
countryName             = optional
stateOrProvinceName     = optional
localityName            = optional
organizationName        = optional
organizationalUnitName  = optional
commonName              = supplied
emailAddress            = optional

[ req ]
# Options for the `req` tool (`man req`).
default_bits        = 2048
distinguished_name  = req_distinguished_name
string_mask         = utf8only

# SHA-1 is deprecated, so use SHA-2 instead.
default_md          = sha256

# Extension to add when the -x509 option is used.
x509_extensions     = v3_ca

[ req_distinguished_name ]
# See <https://en.wikipedia.org/wiki/Certificate_signing_request>.
countryName                     = Country Name (2 letter code)
stateOrProvinceName             = State or Province Name
localityName                    = Locality Name
0.organizationName              = Organization Name
organizationalUnitName          = Organizational Unit Name
commonName                      = Common Name
emailAddress                    = Email Address

[ v3_ca ]
# Extensions for a typical CA (`man x509v3_config`).
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
basicConstraints = critical, CA:true
keyUsage = critical, digitalSignature, cRLSign, keyCertSign

[ v3_intermediate_ca ]
# Extensions for a typical intermediate CA (`man x509v3_config`).
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
basicConstraints = critical, CA:true
keyUsage = critical, digitalSignature, cRLSign, keyCertSign

[ usr_cert ]
# Extensions for client certificates (`man x509v3_config`).
basicConstraints = CA:FALSE
nsCertType = client, email
nsComment = "OpenSSL Generated Client Certificate"
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer
keyUsage = critical, nonRepudiation, digitalSignature, keyEncipherment
extendedKeyUsage = clientAuth, emailProtection

[ server_cert ]
# Extensions for server certificates (`man x509v3_config`).
basicConstraints = CA:FALSE
nsCertType = server
nsComment = "OpenSSL Generated Server Certificate"
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer:always
keyUsage = critical, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth

[ crl_ext ]
# Extension for CRLs (`man x509v3_config`).
authorityKeyIdentifier=keyid:always

[ ocsp ]
# Extension for OCSP signing certificates (`man ocsp`).
basicConstraints = CA:FALSE
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer
keyUsage = critical, digitalSignature
extendedKeyUsage = critical, OCSPSigning

上述步骤中哪些问题无法从根CA验证到设备CA?

0 个答案:

没有答案