預設目錄在 OpenSSL 設定檔中不起作用

預設目錄在 OpenSSL 設定檔中不起作用

我已在 /etc/ssl/openssl.cnf 檔案中設定目錄,但每次發出指令時

openssl req -x509 -newkey rsa:4096 -keyout cakey.pem -out cacert.pem -days 3650

它將檔案放置在我正在工作的目錄的根目錄中。

[ CA_default ]

dir     = /home/will/myCA   # Where everything is kept
certs       = $dir/certs        # Where the issued certs are kept
crl_dir     = $dir/crl      # Where the issued crl are kept
database    = $dir/index.txt    # database index file.
#unique_subject = no            # Set to 'no' to allow creation of
                    # several certs with same subject.
new_certs_dir   = $dir/newcerts     # default place for new certs.

certificate = $dir/cacert.pem   # The CA certificate
serial      = $dir/serial       # The current serial number
crlnumber   = $dir/crlnumber    # the current crl number
                    # must be commented out to leave a V1 CRL
crl     = $dir/crl.pem      # The current CRL
private_key = $dir/private/cakey.pem# The private key
RANDFILE    = $dir/private/.rand    # private random number file

x509_extensions = usr_cert      # The extensions to add to the cert

# Comment out the following two lines for the "traditional"
# (and highly broken) format.
name_opt    = ca_default        # Subject Name options
cert_opt    = ca_default        # Certificate field options

# Extension copying option: use with caution.
# copy_extensions = copy

# Extensions to add to a CRL. Note: Netscape communicator chokes on V2 CRLs
# so this is commented out by default to leave a V1 CRL.
# crlnumber must also be commented out to leave a V1 CRL.
# crl_extensions    = crl_ext

default_days    = 365           # how long to certify for
default_crl_days= 30            # how long before next CRL
default_md  = default       # use public key default MD
preserve    = no            # keep passed DN ordering

# A few difference way of specifying how similar the request should look
# For type CA, the listed attributes must be the same, and the optional
# and supplied fields are just that :-)
policy      = policy_match

如果目錄正常工作我應該期待這個

Generating a 2048 bit RSA private key
.................................+++
.................................................................................................+++
writing new private key to '/home/will/myCA/private/cakey.pem'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----

將新私鑰寫入“/home/will/myCA/private/cakey.pem”

我已經使用二進位直接從網站升級了 OpenSSL 版本,現在安裝在 /etc/local/ssl 下。不幸的是,我仍然不明白為什麼我使用 OpenSSL 生成的檔案沒有被分類到資料夾/目錄中。

有誰知道為什麼預設目錄不起作用?

非常感謝

將要

更新 11:00 30/05/2019

我已經運行了命令

openssl req -x509 -newkey rsa:4096 -days 3650

但它只是在終端機視窗內列印密鑰,而不輸出到檔案。

我在命令中添加了 -noout,但檔案已保存,它保存在 ~privkey.pem 中,而不是我在 openssl.cnf 檔案 /home/will/demoCA 中設定的目錄中。

我注意到該檔案保存在終端機開啟的工作目錄中。

使用該命令openssl version -d顯示我在其中設定目錄的設定檔的預設位置OPENSSLDIR: "/usr/local/ssl"

答案1

指向的文件[ CA_defaults ]由命令內部使用openssl ca

如果您查看內部,new_certs_dir您將看到使用該命令時由 CA 簽署的所有證書openssl ca,檔案名稱由附加的證書序號組成.pem

當您使用時,openssl req這些文件不會被使用。

命令的手冊頁req是這樣說​​的:

-輸出檔名

預設情況下,這指定要寫入的輸出檔名或標準輸出。

因此,它將寫入給定的檔案名,並位於運行命令的目錄中;或者它將寫入標準輸出。

-keyout 檔案名稱

這給出了要寫入新建立的私鑰的檔案名稱。如果未指定此選項,則使用設定檔中存在的檔案名稱。

這將寫入給定的檔案名,該檔案名稱再次位於您執行命令的目錄中;或者它將寫入default_keyfile選項中給出的文件名([ req ]當然在下面)。

在這兩種情況下,如果您不希望將檔案放置在目前目錄中,則可以在命令中給出檔案的絕對路徑。


當您使用命令簽署來自下屬(CA 或最終實體)的請求時,您在文件中配置的結構.conf將會起作用。openssl ca但是,要使其進入可以簽署憑證的階段,它需要 CA 憑證和金鑰。您的openssl req命令會產生這些。要在 CA 憑證中取得合理的值,您需要.conf在文件中添加更多內容。

類似以下內容應該可以幫助您開始:

[ req ]

# Don't prompt for the DN, use configured values instead
# This saves having to type in your DN each time.

prompt             = no
string_mask        = default
distinguished_name = req_dn

# The size of the keys in bits:
default_bits       = 4096

[ req_dn ]

countryName            = GB
stateOrProvinceName    = Somewhere
organizationName       = Example
organizationalUnitName = PKI
commonName             = Example Test Root CA

[ ca_ext ]

# Extensions added to the request

basicConstraints =  critical, CA:TRUE
keyUsage =          critical, keyCertSign, cRLSign

使用先前命令的稍微修改版本來建立 CA 憑證:

openssl req -x509 -newkey rsa:4096 -keyout /home/will/myCA/private/cakey.pem -out /home/will/myCA/cacert.pem -days 3650 -nodes -config <path-to>/openssl.cnf -extensions ca_ext

-config注意:僅當您不使用/編輯預設設定檔時才需要該選項。

如果一切正常,您將擁有適用於上述 CA 設定的正確憑證和金鑰。在使用該命令簽署任何證書之前openssl ca,您需要確保index.txt存在並serial使用初始序號(例如01)建立。

OpenSSL 是加密領域的瑞士軍刀,因此有很多選擇。不幸的是,閱讀手冊頁是理解它的唯一方法。

相關內容