題:
直接在命令行上將subjectAltName提供給openssl
Michael Seiwald
2014-12-05 18:38:22 UTC
view on stackexchange narkive permalink

是否可以直接在命令行上為openssl req 模塊提供subjectAltName-Extension?

我知道可以通過openssl.cnf文件進行操作,但這就是對於批量創建CSR而言並不是很優雅。

我剛剛開發了一個基於Web的工具,該工具將根據表單輸入自動生成此命令並顯示輸出。 http://kernelmanic.com/certificate-request-generator-with-multiple-common-names-and-subject-alternative-names/
從包含的內容還不清楚,工具的輸出如何應用於問題。另外,您能否解釋一下您的工具如何工作(以防鏈接失效)?
彼得(編輯):'OpenSSL'是_project_及其整體輸出的名稱,但'openssl'最低是與該Q相關的命令行'實用程序'的名稱。
截至2019年,此答案應為公認的答案:https://security.stackexchange.com/a/183973/143034
十五 答案:
user749618
2015-06-13 09:57:47 UTC
view on stackexchange narkive permalink

基於DarkLighting的 link,這是我使用嵌套子shell提出的命令。

  openssl req -new -sha256 \ -key domain.key \ -subj“ / C = US / ST = CA / O = Acme,Inc./CN=example.com” \ -reqexts SAN \ -config <(cat /etc/ssl/openssl.cnf \ <(printf“ \ n [SAN] \ nsubjectAltName = DNS:example.com,DNS:www.example.com“))\ -out domain.csr  

所有一行:

  openssl req -new -sha256 -key domain.key -subj“ / C = US / ST = CA / O = Acme,Inc./CN=example.com” -reqexts SAN -config <(cat / etc / ssl / openssl.cnf <(printf“ [SAN] \ nsubjectAltName = DNS:example.com,DNS:www.example.com”))-out domain.csr  

示例使用:

  user @ hostname:〜$ openssl req -new -sha256 -key domain.key -subj“ / C = US / ST = CA / O = Acme,Inc./CN=example。 com“ -reqexts SAN -config <(cat /etc/ssl/openssl.cnf <(printf” \ n [SAN] \ nsubjectAltName = DNS:example.com,DNS:www.example.com \ n“))-出domain.csruser@h ostname:〜$ openssl req -in domain.csr -text -noout證書請求:數據:版本:0(0x0)主題:C = US,ST = CA,O = Acme,Inc.,CN = example.com主題公鑰信息:公鑰算法:rsaEncryption公鑰:(2048位)模數:00:a8:05:50:86:49:98:c8:05:01:e9:50:18:7f:2f:b4:89 :09:29:d1:c1:58:d8:14:bb:58:1d:25:50:11:bb:43:d8:28:03:a5:de:59:49:bb:d2:f7 :d3:79:5c:c6:99:2c:98:ff:99:23:8c:df:96:7c:ea:4b:62:2a:a4:c2:84:f5:5d:62:7f :7d:c4:7c:e2:c3:db:e6:58:03:c2:26:9d:02:da:bb:84:d9:11:82:fe:38:12:9b:c7:b6 :ff:b2:40:30:38:b1:44:d8:47:1d:43:4a:29:58:6b:49:ec:33:d7:dc:a7:1b:90:05:3a :f5:e6:16:98:08:5d:2d:7e:b4:ea:a2:a4:b1:84:89:f7:f1:c4:67:a6:a1:06:70:dd:4e :6b:0c:f8:b5:9b:bc:3f:06:
ee:90:d6:86:29:52:d3:af:f6:d4:2f:c6:cf:4b:5a:b8:cd:01:74:6d:5c:25:a8:02:1c: 7c:e8:66:3d:46:07:b1:9d:ef:cc:eb:90:b6:bf:7b:33:e0:5f:b2:9b:e8:b4:12:67:2f: 8d:0d:9b:54:9d:95:6e:09:83:cb:f3:5b:1f:31:8e:3b:ca:4e:08:e0:40:c0:60:40:72: dd:0d:3e:99:ec:7c:ac:c4:3c:ba:85:9d:d9:d9:6b:02:2e:bf:a8:a3:02:1d:eb:c8:58: e3:04:b3:a5:f1:67:37指數:65537(0x10001)屬性:請求的擴展名:X509v3使用者替代名稱:DNS:example.com,DNS:www.example.com簽名算法:sha256WithRSAEncryption a2:1d: 1a:e8:56:43:e7:e5:c7:c1:04:c1:6a:eb:d5:70:92:78:06:c1:96:fa:60:e2:5f:3c:95: ee:75:ed:70:52:c1:f0:a7:54:d2:9f:4a:2f:52:0f:d4:27:d8:13:73:1f:21:be:34:3f: 0a:9c:f1:2a:5c:98:d4:28:b8:9c:78:44:e8:ea:70:f3:11:6b:26:c3:d6:29:b3:25:a0: 81:ea:a2:55:31:f2:63:c8:60:6d: 68:e3:ab:24:c9:46:33:92:8f:f2:a7:72:43:c6:aa:bd:8d:e9:6f:64:64:9e:fe:30:48: 3f:06:2e:58:7c:b5:ef:b1:4d:c3:84:cc:02:a5:58:c3:3f:d8:ed:98:c7:54:b9:5e:50: 44:5e:be:99:c2:e4:03:81:4b:1f:47:9a:b0:4d:74:7b:10:29:2f:84:fd:d1:70:88:2e: ea:f3:42:b7:06:94:4a:06:f6:92:10:4c:ce:de:65:89:2d:0a:f1:0f:79:90:02:a4:b9: 6d:b8:39:db:de:6e:34:61:4f:21:36:a0:b5:73:2b:2b:c6:7e:2f:f2:e5:1e:51:9f:85: c8:17:9c:1a:b6:59:b0:41:a7:06:c8:5b:f4:88:92:c9:34:71:9d:73:f0:2e:31:ae:ed: ab:35:0e:b4:8a:9a:72:7c:6f:7a:3e:5d:66:49:26:26:99:e1:69  
如果您的配置缺少`[SAN]`部分,則`-reqexts SAN`部分將導致錯誤消息'Error Loading request extension section SAN'。萬一其他人遇到那個。
我還必須設置-extensions SAN才能使其正常工作。完整的1-liner:`openssl req -new -sha256 -key domain.key -subj“ / C = US / ST = CA / O = Acme,Inc./CN=example.com” -reqexts SAN -extensions SAN -config<(cat /etc/ssl/openssl.cnf <(printf“ [SAN] \ nsubjectAltName = DNS:example.com,DNS:www.example.com”))-out domain.csr`
對於自簽名證書,我需要在配置文件中使用x509_extensions = SAN,對於CSR -reqexts似乎可以工作...
-1
可能由於sub-shell語法,此oneliner僅在BASH(bash)中不能在Bourne shell(sh)中起作用。
Peter W
2018-04-18 15:14:26 UTC
view on stackexchange narkive permalink

從OpenSSL 1.1.1開始,通過在 openssl req中引入 -addext 標誌,在命令行上直接提供subjectAltName變得容易得多。代碼>(通過此提交)。

提交示例添加到 openssl req 手冊頁

 在命令行中提供最常見屬性(主題和擴展名)的示例:openssl req -new -subj“ / C = GB / CN = foo” \- addext“ subjectAltName = DNS:foo.co.uk” \ -addext“ certificatePolicies = 1.2.3.4” \ -newkey rsa:2048 -keyout key.pem -out req.pem  

此已合併到Github上 openssl命令的master分支的主分支中,並且自2018年4月18日起,可以通過git pull + compile(或在OS X上通過Homebrew安裝: --devel openssl@1.1 )。

請注意,如果您在openssl.cfg的“ [req]”部分設置了配置屬性“ req_extensions”,它將忽略命令行參數

-addext用於創建簽名請求很方便,但是在對csr進行簽名時仍然必須添加SAN,對嗎?Openssl在x509命令上沒有等效的標誌,因此必須使用文件。
如果您使用`openssl x509 -req -CA / CAkey`頒發證書(對CSR進行_not_簽名),則為@end-user:。如果您使用“ openssl ca”,則可以使用“ copy_extensions”對其進行配置,以將來自CSR的擴展名放入證書中。
rustyx
2015-04-28 01:57:51 UTC
view on stackexchange narkive permalink

我的解決方案是通過環境變量傳遞 subjectAltName

首先將其添加到 openssl.conf

  [san_env] subjectAltName = $ {ENV :: SAN}  

然後在調用openssl之前設置環境變量:

  export SAN = DNS :value1,DNS:value2openssl req -extensions san_env -subj'/ CN = value1'...  

注意: -extensions san_env 參數需要存在在簽署CSR以及生成CSR時。因此,對於CA簽名的CSR,也將-擴展名san_env 添加到 openssl ca 命令。

我收到此錯誤消息“錯誤加載擴展部分san_env”。
這可能是因為未設置SAN環境變量或該變量為空。
我將接受的答案和此答案結合使用,以從命令行編寫所有腳本。
它對我有很大幫助。...我以前在Windows OS上進行BATCH
好的.conf語法要知道!輕微增強:避免通過SAN = DNS:value1,DNS:value2 openssl req ...使用export污染當前的bash環境。
我認為這不適用於macOS。我嘗試使用export和像Anton建議的那樣內聯的變量進行嘗試。我最終以4696151660:錯誤:0EFFF068:配置文件例程:CRYPTO_internal:variable沒有值:/BuildRoot/Library/Caches/com.apple.xbs/Sources/libressl/libressl-22.260.1/libressl-2.6/crypto/conf/conf_def.c:563:第122行`
tsl0922
2017-05-15 19:44:55 UTC
view on stackexchange narkive permalink

這是我根據上面的答案最終生成一個有效的自簽名證書的解決方案(接受的答案對我不起作用):

  openssl genrsa -out ca.key 2048openssl req -new -x509 -days 365 -key ca.key -subj“ / C = CN / ST = GD / L = SZ / O = Acme,Inc./CN=Acme Root CA” -out ca.crtopenssl req- newkey rsa:2048 -nodes -keyout server.key -subj“ / C = CN / ST = GD / L = SZ / O = Acme,Inc./CN=*.example.com” -out server.csropenssl x509 -req -extfile <(printf“ subjectAltName = DNS:example.com,DNS:www.example.com”)-days 365 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt 代碼> 

openssl x509 -in server.crt -text -noout :

 證書:數據:版本:3(0x2)序列號:ef:ca:cb:c7:3e:5c:25:85簽名算法:sha1WithRSAEncryption頒發者:C = CN,ST = GD,L = SZ,O = Acme,Inc.,CN = Acme根CA有效性不早於: 2017年5月15日14:42:17 GMT不遲於:5月15日14:4 2:17 2018 GMT主題:C = CN,ST = GD,L = SZ,O = Acme,Inc.,CN = *。example.com主題公鑰信息:公鑰算法:rsaEncryption RSA公鑰:(2048位)模數(2048位):00:f0:19:32:51:9c:13:ec:dc:d4:52:30:d9:39:4a:f5:9b:53:60:48:10:2d :c1:c0:48:ac:75:a3:2a:d2:6c:62:f1:ed:39:46:7e:e7:e7:03:34:7a:c2:53:b7:42:5a :f2:47:ff:34:68:b1:c9:28:3c:1c:eb:57:af:90:87:53:85:3c:0f:6c:85:62:a1:02:94 :b6:5f:3e:e2:d1:bc:48:20:81:46:fe:25:b4:06:cd:b8:04:c4:f5:81:f6:29:55:66:98 :95:2f:db:75:39:82:7f:32:5b:18:d9:9d:69:d0:f4:6b:0b:a2:92:83:b2:02:1b:6c:d9 :1e:f9:c4:f4:72:a6:76:e7:03:14:d6:29:2b:be:e7:96:3e:42:3a:12:16:8b:51:11:22 :7d:c1:d9:47:ab:cd:93:36:27:d3:ad:af:85:0b:c4:d1:75:6e:
c1:a8:ed:f8:0f:4a:c8:79:21:4c:02:7f:27:70:00:60:ed:68:8f:97:e0:0e:63:86:9f: 12:07:78:aa:bf:b1:bb:d1:30:ff:e6:7e:5c:cd:48:3b:31:fd:ab:54:b4:af:dd:95:49: a6:17:0b:23:98:5f:3d:98:f2:eb:8c:e4:aa:6e:44:2e:2d:5e:d5:91:a3:3a:61:18:3b: 56:29:47:86:1f:1d:d7:7c:6b:29:e7:ae:28:ec:3c:e3:b1指數:65537(0x10001)X509v3擴展名:X509v3使用者替代名稱:DNS:example .com,DNS:www.example.com簽名算法:sha1WithRSAEncryption 56:d2:5b:d0:6a:d9:1d:0b:d4:2d:b3:99:cf:5f:92:e6:9f:4d: ea:b7:22:57:0b:85:e1:f7:4b:b1:13:c1:45:f7:7c:06:34:bd:0c:4b:e8:45:01:84:58: 8a:7a:0d:7b:08:90:a0:91:7c:f1:f7:ef:de:3b:94:be:44:4b:71:c5:40:6f:3c:35:3e: 61:79:b1:46:d9:81:31:bf:11:15:6a:b2:53:b9:a3:d7:81:cd:2d:f5:3e:20:dc:06:1c: a0:74:16:9f:d4:53:5d:f2:3a:23:1c:43:2d:ce:8b:68:d3:35:f3:36:8 a:05:13:34:a7:42:75:6e:df:a2:b5:95:77:71:99:ae:be:4a:6c:ae:14:b4:d1:e4:f7: b4:39:b0:30:04:57:8a:d8:21:c5:1c:50:f3:86:38:ec:eb:0c:a6:f6:94:f3:f4:af:ec: 1b:d1:79:ad:16:45:bc:c9:10:2a:a8:2d:b8:cf:7d:8a:aa:b4:b5:74:e0:d4:53:82:b5: 71:b8:bb:2f:d2:12:51:87:ab:f1:b6:dd:1c:24:b1:8b:36:05:83:29:ca:58:ba:6b:f0: 83:cc:27:86:43:00:da:73:a0:d5:36:31:bb:e7:e5:1b:2f:c0:42:55:7b:b4:2e:57:4f: 88:b4:cd:0d:d0:bf:a8:87:76:a1:1b:bc:e4:fc:31:ba:ee:04  

Repro “接受的答案對我不起作用”的步驟(在OSX 10.12.4上,使用系統openssl):

  bash-3.2 $ openssl genrsa -out domain.key 2048生成RSA私鑰,2048位長模數................................. ............................................ ++++ ... ................................. +++ e是65537(0x10001)
bash-3.2 $ openssl req -new -sha256 -key domain.key -subj“ / C = US / ST = CA / O = Acme,Inc./CN=example.com” -reqexts SAN -config <(cat / etc /ssl/openssl.cnf <(printf“ [SAN] \ nsubjectAltName = DNS:example.com,DNS:www.example.com”))-out domain.csrbash-3.2 $ openssl req -in domain.csr -text- nooutCertificate請求:數據:版本:0(0x0)主題:C = US,ST = CA,O = Acme,Inc.,CN = example.com主題公鑰信息:公鑰算法:rsaEncryption RSA公鑰:(2048位)模數(2048位):00:cd:a5:97:b2:1a:83:c6:1d:0e:78:1a:6f:ca:4c:e6:e3:64:94:41:b8:fb :f3:4a:4c:56:8c:33:36:c1:5d:10:25:f5:86:f5:14:c6:17:22:53:34:7b:16:52:ea:f2 :ac:bf:0d:09:7d:55:c8:16:ce:0e:f9:98:20:aa:11:4e:bb:4d:75:b1:ed:1b:ca:37:82 :f1:15:71:56:ad:c0:be:40:b4:ef:f2:e6:a5:a2:3b:e3:a8:0c:8b:38:3d:d5:41:1a:e8 :92 :f6:78:52:9f:35:c2:98:a6:58:87:64:e6:d3:7e:a0:00:8c:d0:16:13:80:e9:ee:81:aa :40:c7:1d:9d:fc:52:9a:50:7d:50:e6:ca:20:38:89:12:7d:99:a0:68:ae:45:64:03:e0 :00:3c:30:b7:94:87:ab:de:51:90:73:6b:bc:48:c4:e8:47:2d:0e:5a:d0:fb:b4:1b:cb :76:7b:05:70:1a:a8:03:bc:35:38:70:b5:ca:07:43:d3:9d:66:8c:32:32:74:7e:6f:61 :e8:de:80:de:d9:fd:fc:27:d8:bb:fa:8c:f9:94:42:c4:b8:e0:bb:24:8b:1f:71:5b:18 :99:ca:ac:42:3b:ed:d7:4d:5f:dc:79:8c:6c:fe:d1:df:44:05:5f:1a:a7:bd:e8:1c:85 :0c:70:fb:4e:29:62:a0:e9:71指數:65537(0x10001)屬性:請求的擴展名:X509v3使用者替代名稱:DNS:example.com,DNS:www.example.com簽名算法: sha256WithRSAEncryption 47:f3:82:ae:78:f2:19:76:05:e3:97:30:00:16:c5:9c:89:94:
ef:b0:51:b0:cf:4a:93:81:7d:ee:94:25:9a:0a:9e:1f:7f:e0:d8:72:55:75:2d:ac:c3: f9:3a:74:b6:1f:1b:c3:f1:68:d4:66:72:89:ed:53:7b:09:da:35:eb:40:63:e6:6a:0f: 9a:4f:6e:25:9f:63:df:bb:d6:00:77:c2:e7:d6:96:0c:50:58:01:c9:d1:ff:df:de:fb: 19:fb:72:38:48:25:5d:b7:56:fb:eb:d7:41:f5:f6:d7:f7:4b:c7:07:4f:59:b4:b8:c3: d8:bf:c9:2c:07:5a:c3:0a:51:f8:02:4f:dc:de:2d:88:49:b7:6d:de:67:04:d0:78:6e: 0f:96:d8:06:e4:73:4f:fb:ce:29:0f:1e:3a:1a:6e:3c:a5:f3:f1:68:3d:22:85:34:fa: f0:ad:f6:75:61:02:81:f1:c4:e3:69:2b:80:3d:05:39:c6:9d:72:66:2a:50:93:6c:79: 5d:d0:33:42:cf:a6:68:6a:16:d7:dc:61:b4:c3:4e:01:ac:68:7c:77:29:d4:fe:0d:9d: 34:0a:3e:73:02:27:12:a4:08:9c:b9:2e:3e:c8:3f:1d:91:33:3b:71:8f:24:6b:66:f5: c3:8a:d7:7b:fe:2d:7f:b4:6d:96:cf:52:74bash-3.2 $ openssl x509 -req -in domain.csr -signkey domain.key -out domain.crtSignature oksubject = / C = US / ST = CA / O = Acme,Inc./CN=example.com獲取私鑰bash-3.2 $ openssl x509 -in domain.crt -text -noout證書:數據:版本:1(0x0)序列號:de:c5:cf:28:1f:33:6c:53簽名算法:sha1WithRSAEncryption頒發者:C = US,ST = CA,O = Acme,Inc.,CN = example.com有效期不早於:2017年5月15日15:30:07 GMT不晚於:2017年6月14日15:30:07 GMT主題:C = US,ST = CA,O = Acme,Inc 。,CN = example.com主題公共密鑰信息:公共密鑰算法:rsaEncryption RSA公共密鑰:(2048位)模數(2048位):00:cd:a5:97:b2:1a:83:c6:1d:0e :78:1a:6f:ca:4c:e6:e3:64:94:41:b8:fb:f3:4a:4c:56:8c:33:36:c1:5d:10:25:f5:86 :f5:14:c6:17:22:53:34:7b:16:52:ea:f2:ac:bf:0d:09:7d:55:c8:16:ce:0e:f9:98:20 :aa:11:4e:bb:4d:75:b1:ed:1b:ca:37:82:f1:15:71:56:ad:c0:be:40:b4:ef:f2:e6:a5 :a2:3b:e3:a8:0c:
8b:38:3d:d5:41:1a:e8:92:f6:78:52:9f:35:c2:98:a6:58:87:64:e6:d3:7e:a0:00:8c: d0:16:13:80:e9:ee:81:aa:40:c7:1d:9d:fc:52:9a:50:7d:50:e6:ca:20:38:89:12:7d: 99:a0:68:ae:45:64:03:e0:00:3c:30:b7:94:87:ab:de:51:90:73:6b:bc:48:c4:e8:47: 2d:0e:5a:d0:fb:b4:1b:cb:76:7b:05:70:1a:a8:03:bc:35:38:70:b5:ca:07:43:d3:9d: 66:8c:32:32:74:7e:6f:61:e8:de:80:de:d9:fd:fc:27:d8:bb:fa:8c:f9:94:42:c4:b8: e0:bb:24:8b:1f:71:5b:18:99:ca:ac:42:3b:ed:d7:4d:5f:dc:79:8c:6c:fe:d1:df:44: 05:5f:1a:a7:bd:e8:1c:85:0c:70:fb:4e:29:62:a0:e9:71指數:65537(0x10001)簽名算法:sha1WithRSAEncryption 02:71:7f:a5 :8e:aa:7d:4b:0a:9d:54:8c:25:cb:b3:66:a3:22:c5:61:73:0c:c4:da:3b:ce:e8:4b:ec :ee:45:83:ca:db:e0:25:9b:a6:a3:c0:c9:7c:d9:76:a2:8c:38:38:b1:77:c7:84:33:03 : b7:9a:cb:ff:bf:83:bc:7b:d8:4c:7e:c4:b3:8f:c5:23:22:75:67:d3:d6:5e:0e:bd:ef: 0b:0f:6a:8d:f0:d3:20:8f:5a:cf:37:94:b7:8a:d9:b3:0e:99:31:4f:77:6f:89:33:c5: 93:99:2e:8b:61:ad:84:17:af:b5:8e:1e:f0:4a:af:b1:90:c3:09:3a:d6:16:4b:1b:c4: 6b:2e:22:7e:b1:7d:9b:3c:a9:3b:06:20:e2:37:14:8b:0d:da:c6:4b:e3:6e:83:9c:df: 20:67:2e:d0:33:68:05:17:01:d5:5a:6f:51:b3:50:d7:73:10:73:c8:be:3b:de:e6:bd: 28:60:6f:19:75:0c:05:16:37:4d:50:df:f4:bb:41:f0:65:ba:6f:7f:5c:56:27:ae:0e: 18:0a:df:7e:d2:7b:93:db:40:d2:bb:e0:dc:b8:57:c7:08:07:37:e4:db:d4:09:b6:13: d7:22:e2:ef:6d:60:fa:3e:7c:f4:1f:0b:bf:26:f4:08:d0:39:cf:51:dd:bf:b1:0 e:ee: 46:d1bash-3.2 $ openssl versionOpenSSL 0.9.8zh 2016年1月14日 
為什麼是您的解決方案?你能說說我們嗎?
這與接受的答案有何不同?
嗨,我已經編輯了答案,我發布了它,因為它對我有用。在`openssl x509 -req -in server.csr`中,接受的答案對我不起作用,我錯過了什麼嗎?
好的,但是*為什麼*對您有用?哪一部分代替了可接受的答案並使之起作用?現在,這只是一堵代碼牆。
不知道為什麼,我在答案中添加了重現步驟。更改是:我將“ subjectAltName”參數從“ openssl req”移到了“ openssl x509”命令,然後它起作用了。
問題是關於創建CSR(大概是提交給真正的CA),然後接受的答案就可以了。您的答案會使用SAN創建一個自簽名證書,但不會使用SAN創建CSR,因此儘管它確實回答了https://security.stackexchange.com/questions/150078/missing-x509-extensions-,但它在這裡沒有回答問題與一個openssl生成的證書
dave_thompson_085是正確的,但包括我在內的其他人顯然對這裡的答案很有用。問題是關於企業社會責任的,但標題沒有具體說明。我的建議是,您複製或鏈接到他提到的問題的答案,即“缺少帶有openssl生成的證書的X509擴展” https://security.stackexchange.com/questions/150078/missing-x509-extensions-with-openssl生成的證書
與接受的答案(僅處理如何使用subjectAltName生成CSR)相比,此答案的主要區別在於,在此答案中生成了兩個證書。根CA證書和域證書。根CA充當域證書的頒發者。
在此答案中也顯示瞭如何對csr進行簽名,以便結果證書包含SAN
並且此答案不會讀取整個openssl配置文件\ o /
Excalibur
2017-08-03 04:43:20 UTC
view on stackexchange narkive permalink

因此,我花了一點時間來使此工作正常進行,並完全投入Ansible。由於Ansible的 command 模塊不允許文件重定向( <(...)),因此我不得不使用一個小的 .cnf 文件作為模板,但現在都可以使用。這是我所做的工作:

san.cnf模板(為每個CSR / CRT對生成):

  [req ] distinguished_name = req_distinguished_namereq_extensions = v3_reqx509_extensions = v3_req [req_distinguished_name] commonName = {{common_name}} emailAddress = {{ssl_certs_email}} organizationName = {{ssl_certs_}本地名稱{{要添加到自簽名certsubjectKeyIdentifier = hashbasicConstraints = critical,CA:falsesubjectAltName = DNS:{{common_name}} keyUsage = critical,digitalSignature,keyEncipherment  
 

某些變量

以下命令中使用了這些Ansible變量,但是您可以根據需要在腳本中進行替換:

  ssl_certs_fields:“ / C = {{ssl_certs_country}} / ST = {{ssl_certs_state}} / L = {{ssl_certs_locality}} / O = {{ssl_certs_organization}} / CN = {{common_nam e}} / emailAddress = {{ssl_certs_email}}“ ssl_certs_local_privkey_path:專用密鑰的路徑ssl_certs_local_csr_path:CSRssl_certs_local_path的路徑:此PKI文件的本地目錄設置了SSL_certs_local_decrypt_cakey_pre  presert> > 

密鑰ssl_certs_local_caserial_path:CA的序列號文件ssl_certs_local_cert_path:最終生成的證書文件。

CSR生成命令

  openssl req -new -sha256 -subj“ {{ssl_certs_fields}}”-鍵“ {{ssl_certs_local_privkey_path}}”-out“ {{ssl_certs_local_csr_path}}”“ -config” {{ssl_certs_local_path}} / san.cnf“  

自簽名CSR以創建證書

  openssl x509 -req -days {{ssl_certs_days}} -sha256 -extfile“ {{ssl_certs_local_path}} /san.cnf“ -extensions v3_req -in” {{ssl_certs_local_csr_path}}“ -CA” {{ssl_certs_local_ca_path}}“ -CAkey” {{ssl_certs_local_decrypt_cakey_path}}“ -CAcreateserial -CA_er _ {{ {ssl_certs_local_cert_path}}“  

要驗證結果

  openssl x509 -noout -text -in {{ssl_certs_local_cert_path}}  

其中應包括如下所示的部分:

  X509v3擴展名:X509v3主題密鑰標識符:3B:6E:E9:9F:B2:30 :08:21:1C:C7:0D:4C:21:7A:B4:92:40:B6:71:98 X509v3基本約束:嚴重CA:FALSE X509v3使用者替代名稱:DNS:foo.bar.com 代碼> 
對於類似這樣的複雜事物,應使用shell Ansible模塊代替命令。無論哪種方式都不是很優雅-但是感謝您的模板解決方法,這當然也可以。
我已經使用下面的Ansible shell模塊提供了一個解決方案。如果僅是更輕鬆地以合理的方式生成CSR(或使用openssl至少具有更好的用戶界面)...
DarkLighting
2014-12-05 20:08:01 UTC
view on stackexchange narkive permalink

鏈接中的第二條帖子說不可能僅從命令行執行此操作,但是同一鏈接中的第四條帖子提供了使用bash功能的解決方法

進一步查看數據,有人提到了 reqexts 參數,該參數用於添加證書請求。該 blog使用bash的 env 作為對此的一種方法。

但是我只是想提供幫助。我自己還沒有測試過任何一個。

謝謝。我使用bash的命令替換功能解決了這個問題。這樣,所有內容都包含在一個腳本中。
vog
2018-11-26 15:44:31 UTC
view on stackexchange narkive permalink

以下命令演示瞭如何使用SAN為 example.com example.net 生成自簽名證書。

它是可移植的從某種意義上說,我們不必弄亂(甚至不必知道) openssl.cnf 文件的位置:

  openssl req -x509- newkey rsa:4096 -sha256 -days 3650 -nodes \ -keyout example.key -out example.crt -subj'/CN=example.com'\ -extensions san \ -config <(echo'[req]'; echo' distinguished_name = req'; echo'[san]'; echo'subjectAltName = DNS:example.com,DNS:example.net') 

此處的技巧是包含一個最小的 [req] 部分足以使OpenSSL在沒有其主 openssl.cnf 文件的情況下相處。

在OpenSSL≥1.1.1中,可以縮寫為:

  openssl req -x509 -newkey rsa:4096 -sha256 -days 3650 -nodes \ -keyout example.key -out example.crt -subj'/CN=example.com' \ -addext'subjectAltName = DNS:考試ple.com,DNS:example.net' 

在這裡,我們使用了新的 -addext 選項,因此我們不需要 -extensions -config

別忘了驗證生成的證書的內容:

  openssl x509 -noout-文本-​​例如example.crt  

另請參見: https://stackoverflow.com/a/41366949/19163 https:// unix。 stackexchange.com/a/333325/20407

Cameron Kerr
2017-06-20 02:38:13 UTC
view on stackexchange narkive permalink

經過RHEL7測試(使用SAN創建自簽名證書)

  openssl req -x509 -nodes -newkey rsa:2048 -days 3650 -sha256 -keyout test.key -out test.cert -reqexts SAN-擴展SAN -subj'/CN=test.example.com'-config <(cat /etc/pki/tls/openssl.cnf; printf“ [SAN] \ nsubjectAltName = DNS:test.example .com,DNS:test2.example.com“) 
Nick2253
2014-12-05 22:03:48 UTC
view on stackexchange narkive permalink

我對這個問題的解決方案是通過附加我的命令行收集的subjectAltName信息來創建和引用一個臨時cnf文件。

Roger W
2019-02-16 00:16:11 UTC
view on stackexchange narkive permalink

我想要一個命令行來創建CSR-完美運行,沒有conf文件,但是沒有生成SubjAltName條目。此版本是我使用的使用read -p請求FQDN的原因,我也希望它也可以與SAN條目一起使用-所以這是一個可行的解決方案。

有一個依賴項在openssl版本上,至少應為1.1.1。因為需要-addext。

 讀取-p“ FQDN嗎?” CN; openssl req-新-key yourkeyfile.key -subj / C = GB / ST = county / L = city / O = company / OU = yourorg / CN = $ CN -addext“ subjectAltName = DNS:$ CN” -out./ $ CN.csr  

不要用這種方式弄亂conf文件。

Josiah DeWitt
2019-05-23 03:46:26 UTC
view on stackexchange narkive permalink

這個問題已經回答了,但是我仍然在努力使它成為一種優雅而有用的形式來自動生成CSR。一個內襯很好,因此我將其合併到一個例程中,該例程允許使用主題替代名稱作為命令參數,而不是文件中的值,這對於SAN或不SAN也具有靈活性。

 #!/ bin / bash#san_cert.sh#默認為================== ================================================== DOM = domain.comO = My \ Company,\ LLCL = SeattleST = WashingtonC = USOU = OperationsEMAIL = certalert#基本檢查和字符串====================== ==============================如果[-z“ $ 1”];然後回顯用法:$ 0 name1 optionalname optionalname ..回顯示例:san_cert.sh www web w3 exch mail exitelse CN = $ 1 SUBJ =“ / C = $ C / ST = $ ST / L = $ L / O = $ O / OU = $ OU / CN = $ CN。 $ DOM / emailAddress = $ EMAIL。$ DOM“ fi#清除舊文件rm $ CN。$ DOM.ssl_csr $ CN。$ DOM.ssl_key#create私鑰================ =========================================== openssl genrsa -out $ CN 。$ DOM.ssl_key 2048if [$#-gt 1];然後#test for arg count #build SAN string ================== A =($ @)I = 1 [[$ I -lt $ {#A [@]}]進行SAN =“ DNS:$ {A [I]}。$ DOM $ CMA $ {SAN}” CMA =“,” I = $ [$ I +1]完成SAN =“ \ n [SAN] \ nsubjectAltName = $ {SAN}”#=================== ===============創建SAN證書籤名請求=========================== ======== openssl req -new -sha256 \ -subj“ $ SUBJ” \ -key $ CN。$ DOM.ssl_key \ -out $ CN。$ DOM.ssl_csr \ -reqexts SAN -config <(cat /etc/ssl/openssl.cnf <(printf“ $ SAN”))else #create單個證書籤名請求======================== ======= openssl req -new -sha256 \ -subj“ $ SUBJ” \ -key $ CN。$ DOM.ssl_key \ -out $ CN。$ DOM.ssl_csrfi#verification ======== ================================================== ======== openssl req -text -noout -verify -in $ CN。$ DOM.ssl_csr  
GlobalVariable
2019-10-11 04:47:05 UTC
view on stackexchange narkive permalink

已經回答了這個問題,但是,如果有人仍在尋找一種無提示,僅限cli的方法來創建自簽名根證書(沒有CA或CSR),並且不介意使用Java keytool ,這是一種替代方案:

使用 keytool

  keytool -genkeypair \ -keyalg RSA \ -keysize 3072 \-生成PKCS12密鑰庫別名titan \ -dname“ CN = titan,OU = Engineering,O = Titan Corp.,C = US” \ -ext BC:c = ca:false \ -ext EKU:c = serverAuth \ -ext“ SAN:c =DNS:titan,IP:192.168.1.7“ \-有效性3650 \ -keystore服務器。p12\ -storepass s3cr3t \ -keypass s3cr3t \ -storetype pkcs12  

導出證書和帶有> openssl

  openssl pkcs12 -in server.p12 -nodes -out cert.pem -passin pass:s3cr3topenssl pkcs12 -in server.p12 -nodes -nocerts -out key.pem-passin pass:s3cr3t  
Mordred
2020-02-13 23:56:47 UTC
view on stackexchange narkive permalink

我需要執行此操作以創建用於本地測試的自簽名證書,但還希望能夠傳遞多個擴展參數,而不僅僅是SAN。我發現執行多個 -extfile 命令似乎相互覆蓋,只有最後一個 -extfile 值以cert結尾。

解決方案只是向printf添加更多變量:

  openssl x509 -req -sha256 \ -extfile <(printf“ extendedKeyUsage = serverAuth \ nsubjectAltName = DNS:example.com”)\-天820 -in server.csr -signkey key.pem -out cert.pem  

工作正常,但是我們的工作流已經通過將命令存儲在package.json文件中而生成了證書,並且然後運行 npm運行newcert 。嘗試將 \ n 添加到printf只是破壞了該命令。解決方案是切換到使用大量回顯以及顯式定義擴展名。

  • 注意:要將它們作為npm腳本運行,您必須轉義雙引號,並且不能使用連續行。
  openssl req -newkey rsa:2048 -sha256 -nodes -keyout key.pem \- subj“ /C=CN/ST=GD/L=SZ/O=Example/CN=example.com” -out server.csropenssl x509 -req -sha256 -extensions v3_ca \ -extfile <(echo“ [v3_ca]”; echo“ extendedKeyUsage = serverAuth”; echo“ subjectAltName = DNS:example.com”)\-天820 -in server.csr -signkey key.pem -out cert.pem  

正在運行 openssl x509 -noout -text -in cert.pem 顯示成功:

  X509v3擴展名:X509v3擴展密鑰用法:TLS Web服務器身份驗證X509v3使用者備用名稱:DNS: example.com  
AdamKalisz
2020-07-07 01:35:59 UTC
view on stackexchange narkive permalink

作為@Excalibur回答的補充(順便說一句,謝謝您的工作!)

我發現這種形式更適合Ansible。它避開了由於庫依賴性和版本問題而使正式模塊 openssl_csr 難以使用的問題。

以下內容是對腳本生成一部分的改編通過@Excalibur。您無需創建文件。這個特定的劇本將證書輸出到stdin,您可以使用( ansible-playbook -vvvv <playbook.yml> )出示證書,或將其轉儲到變量並使用調試模塊輸出。

  ----名稱:測試CSR生成主機:localhost vars:-國家:'US'#C-狀態:' NJ'#ST-位置:'Trenton'#L-組織:'ACME'#O-Organization_unit:'IT'#OU-common_name:'host.example.com'-email_address:'info@example.com'#emailAddress -add_subj_alt_name:'IP:192.0.2.0'#不具有common_name,例如IP:2001:db8 :: 1任務:-名稱:生成CSR Shell:| STR =“ / C = {{國家}} / ST = {{省}} / L = {{地區}} / O = {{組織}} / OU = {{組織單位}} / CN = {{common_name} } / emailAddress = {{email_address}}“ openssl req -new -sha256 -key domain.key -subj” $ STR“ \ -reqexts v3_req -extensions v3_req -config \ <(cat <<<'[reqq = dist_name v3_req x509_extensions = v3_req [req_distinguished_name] countryName = {{country}} stateOrProvinceNamecountryName = {{state}} localityName = {{locality}} OrganizationName = {{Organization}}
organizationUnitName = {{Organization_unit}} commonName = {{common_name}} emailAddress = {{email_address}} [v3_req]#要添加到自簽名證書subject的擴展名subjectKeyIdentifier = hash basicConstraints =關鍵,CA:false subjectAltName = DNS:{{common_name}},{{add_subj_alt_name}} keyUsage = critical,digitalSignature,keyEncipherment')-noout -text args:可執行文件:'/ bin / bash' 
sirkubax
2020-07-08 13:10:27 UTC
view on stackexchange narkive permalink
將CSR簽名到CRT時用於IP SAN的

extfile https://www.golinuxcloud.com/openssl-create-client-server-certificate/

  openssl x509 -req -in server.csr -CA selfca.crt -CAkey selfca.key -CAcreateserial --extensions v3_req -extfile server.req -out server.crt  
為什麼要使用extfile?這個答案與提到extfile的所有其他答案有何不同?如果答案在鏈接中,請在此處在答案中包括鏈接的相關部分。


該問答將自動從英語翻譯而來。原始內容可在stackexchange上找到,我們感謝它分發的cc by-sa 3.0許可。
Loading...