OpenSSL CA 和非 CA 憑證

OpenSSL CA 和非 CA 憑證

我能夠產生自簽名 CA 憑證並產生中間 CA 憑證。

現在我嘗試產生一個最終用戶證書,該證書不應能夠簽署其他證書。因此,我打開openssl.cnf並將該行更改v3_reqbasicConstraints=CA:FALSE並產生一個.csrusing-extensions v3_req並使用中間 CA 對其進行簽名。

接下來我嘗試用它簽署另一個.csr,奇怪的是它也簽署了。當我嘗試時,驗證是OK的。

我完全困惑,不知道該怎麼辦。那你知道出了什麼問題嗎?這些是我使用的命令。

$ openssl req -x509 -extensions v3_ca -newkey rsa:2048 \
   -keyout root_ca.key -out root_ca.crt -days 365 \
   -subj /C=US/ST=abc/L=abc/O=test/OU=mine/CN=CA/[email protected] \
   -passout pass:123456

$ openssl req -new -extensions v3_ca -newkey rsa:2048 \
   -keyout s1.key -out s1.csr -days 365 \
   -subj /C=US/ST=abc/L=abc/O=test/OU=mine/CN=s1/[email protected] \
   -passout pass:123456

$ openssl ca -policy policy_anything -outdir ./ -out s1.crt \
   -cert root_ca.crt -infiles s1.csr -CAkey root_ca.key

$ openssl req -new -extensions v3_req -newkey rsa:2048 \
   -keyout client.key -out client.csr -days 365 \
   -subj /C=US/ST=abc/L=abc/O=test/OU=mine/CN=s1/[email protected] \
   -passout pass:123456

$ openssl ca -policy policy_match -outdir ./ -out client.crt -cert s1.crt \
   -infiles client.csr -CAkey s1.key

 $ openssl req -new -extensions v3_req -newkey rsa:2048 \
   -keyout client2.key -out client2.csr -days 365 \
   -subj /C=US/ST=abc/L=abc/O=test/OU=mine/CN=s1/[email protected] \
   -passout pass:123456

 $ openssl ca -policy policy_match -outdir ./ -out client2.crt \
   -cert client.crt -infiles client2.csr -CAkey client.key

 $ cat root_ca.crt s1.crt client.crt > ca.pem

 $ openssl verify -CAfile ca.pem client2.crt

這是設定檔:

HOME = .
RANDFILE = $ENV::HOME/.rnd
oid_section = new_oids
[ new_oids ]
[ ca ]
default_ca = CA_default
[ CA_default ]
dir = /root/new
certs = $dir/certs
crl_dir = $dir/crl
database = $dir/index.txt
new_certs_dir = $dir/newcerts
certificate = $dir/root_ca.crt 
serial = $dir/serial 
crlnumber = /root/index.txt
crl = $dir/crl.pem 
private_key = $dir/root_ca.key 
RANDFILE = $dir/private/.rand
x509_extensions = usr_cert
name_opt = ca_default
cert_opt = ca_default
default_days = 365
default_crl_days = 30
default_md = sha1
preserve = no
policy = policy_match
[ policy_match ]
countryName = match
stateOrProvinceName = match
organizationName = match
organizationalUnitName = optional
commonName = supplied
emailAddress = optional
[ policy_anything ]
countryName = optional
stateOrProvinceName = optional
localityName = optional
organizationName = optional
organizationalUnitName = optional
commonName = supplied
emailAddress = optional
[ req ]
default_bits = 1024
default_keyfile = privkey.pem
distinguished_name = req_distinguished_name
attributes = req_attributes
x509_extensions = v3_ca
string_mask = nombstr
[ req_distinguished_name ]
countryName = Country Name (2 letter code)
countryName_default = AU
countryName_min = 2
countryName_max = 2
stateOrProvinceName = State or Province Name (full name)
stateOrProvinceName_default = Some-State
localityName = Locality Name (eg, city)
0.organizationName = Organization Name (eg, company)
0.organizationName_default = Internet Widgits Pty Ltd
organizationalUnitName = Organizational Unit Name (eg, section)
commonName = Common Name (eg, YOUR name)
commonName_max = 64
emailAddress = Email Address
emailAddress_max = 64
[ req_attributes ]
challengePassword = A challenge password
challengePassword_min = 4
challengePassword_max = 20
unstructuredName = An optional company name
[ usr_cert ]
basicConstraints = CA:FALSE
keyUsage = digitalSignature, nonRepudiation,keyEncipherment, dataEncipherment, keyAgreement
nsComment = "OpenSSL Generated Certificate"
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer
[ v3_req ]
basicConstraints = critical,CA:false
keyUsage = nonRepudiation
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer:always
[ v3_ca ]
subjectKeyIdentifier = hash
extendedKeyUsage = critical,serverAuth, clientAuth
basicConstraints = CA:true
keyUsage = cRLSign, keyCertSign, digitalSignature, nonRepudiation,keyEncipherment, dataEncipherment, keyAgreement, keyCertSign, cRLSign
[ crl_ext ]
authorityKeyIdentifier = keyid:always,issuer:always
[ proxy_cert_ext ]
basicConstraints = CA:FALSE
nsComment = "OpenSSL Generated Certificate"
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer:always
proxyCertInfo = critical,language:id-ppl-anyLanguage,pathlen:3,policy:foo

答案1

你嘗試過嗎basicConstraints=critical,CA:false

PS:「ca」公用程式目前不檢查其簽章所使用的 CA 憑證的有效性。因此它會很樂意使用無效的 CA 進行簽名,但驗證例程將拒絕它。

以下的內容雖然有點冗長,但應該可行;您可以在您的設定中嘗試並報告任何問題嗎?

#!/bin/sh
#SSLEAY_CONFIG="-config yourfile.cnf"
ROOTCA_SUBJ="-subj /C=US/ST=abc/L=abc/O=test/OU=mine/CN=RootCA/[email protected]"
CA_SUBJ="-subj /C=US/ST=abc/L=abc/O=test/OU=mine/CN=CA/[email protected]"
CERT_SUBJ="-subj /C=US/ST=abc/L=abc/O=test/OU=mine/CN=cert/[email protected]"
ROOTCA_PASS="pass:test"
CA_PASS="pass:test"
CERT_PASS="pass:test"
DIR="demoCA"
mkdir "$DIR" "$DIR"/certs "$DIR"/crl "$DIR"/newcerts "$DIR"/private
touch "$DIR"/index.txt
echo 01 > "$DIR"/crlnumber

# create Root CA
mkdir rootCA rootCA/certs rootCA/crl rootCA/newcerts rootCA/private
openssl req $SSLEAY_CONFIG -new -keyout rootCA/private/rootCAkey.pem -out rootCA/rootCAreq.pem $ROOTCA_SUBJ -passout "$ROOTCA_PASS"
openssl ca $SSLEAY_CONFIG -create_serial -out rootCA/rootCAcert.pem -days 1095 -batch -keyfile rootCA/private/rootCAkey.pem -passin "$ROOTCA_PASS" -selfsign -extensions v3_ca -infiles rootCA/rootCAreq.pem

# create Intermediate CA
mkdir CA CA/certs CA/crl CA/newcerts CA/private
openssl req $SSLEAY_CONFIG -new -keyout CA/private/CAkey.pem -out CA/CAreq.pem -days 365 $CA_SUBJ -passout "$CA_PASS"
openssl ca $SSLEAY_CONFIG -cert rootCA/rootCAcert.pem -keyfile rootCA/private/rootCAkey.pem -passin "$ROOTCA_PASS" -policy policy_anything -out CA/CAcert.pem -extensions v3_ca -infiles CA/CAreq.pem

# create Final Cert
mkdir cert cert/private
openssl req $SSLEAY_CONFIG -new -keyout cert/private/certkey.pem -out cert/certreq.pem -days 365 $CERT_SUBJ -passout "$CERT_PASS"
openssl ca $SSLEAY_CONFIG -cert CA/CAcert.pem -keyfile CA/private/CAkey.pem -passin "$CA_PASS" -policy policy_anything -out cert/cert.pem -infiles cert/certreq.pem
cat rootCA/rootCAcert.pem CA/CAcert.pem > myCA.pem
openssl verify -CAfile myCA.pem cert/cert.pem

當你跑步時openssl verify-CAfile代表你信任證書。因此-CAfile應該只包含根 CA,以及可選的中間 CA;否則它總會回來OK。其他證書應透過該選項新增-untrusted

因此,如果您給它一個cert2.pem最終的 cert.pem證書,您將獲得:

$ cat CA/CAcert.pem cert/cert.pem > notrust.pem
$ openssl verify -CAfile myCA.pem -untrusted notrust.pem cert2/cert2.pem
    cert2/cert2.pem: C = US, ST = abc, L = abc, O = test, OU = mine, CN = cert, emailAddress = [email protected]
    error 24 at 1 depth lookup:invalid CA certificate

答案2

如前所述,CA 實用程式會樂意使用任何憑證簽署要求,即使它不是 CA 憑證。顯然,一些較舊的瀏覽器(至少 33 之前的 Firefox 版本)甚至會接受此類鏈。

若要查明您的憑證是否isCA設定了該位,請執行:

openssl x509 -text -noout -in your_cert_file.crt

在輸出中,尋找以下內容:

X509v3 Basic Constraints: 
    CA:TRUE

這是 CA 憑證。非 CA 憑證將具有CA:FALSE(或根本不具有副檔名)。

注意:您需要在請求中包含這些擴展,並確保 CA 在簽署請求時不會覆蓋它們。

相關內容