OpenSSL

密碼學和 SSL/TLS 工具組

openssl-rsautl

名稱

openssl-rsautl - RSA 指令

語法

openssl rsautl [-help] [-in 檔案] [-passin 參數] [-rev] [-out 檔案] [-inkey 檔名|uri] [-keyform DER|PEM|P12|ENGINE] [-pubin] [-certin] [-sign] [-verify] [-encrypt] [-decrypt] [-pkcs] [-x931] [-oaep] [-raw] [-hexdump] [-asn1parse] [-engine id] [-rand 檔案] [-writerand 檔案] [-provider 名稱] [-provider-path 路徑] [-propquery propq]

說明

此指令已棄用。應改用 openssl-pkeyutl(1) 指令。

此指令可用於使用 RSA 演算法簽署、驗證、加密和解密資料。

選項

-help

列印使用說明訊息。

-in 檔名

這會指定輸入檔名以讀取資料,如果未指定此選項,則為標準輸入。

-passin 參數

輸出檔案中使用的密碼。請參閱 openssl-passphrase-options(1)

-rev

反轉輸入順序。

-out 檔名

指定輸出檔名以寫入,或預設為標準輸出。

-inkey 檔名|uri

輸入金鑰,預設應為 RSA 私密金鑰。

-keyform DER|PEM|P12|ENGINE

金鑰格式;預設未指定。有關詳細資訊,請參閱 openssl-format-options(1)

-pubin

預設會從金鑰輸入讀取私密金鑰。使用此選項會改為讀取公開金鑰。如果輸入不包含公開金鑰,但包含私密金鑰,則會使用其公開部分。

-certin

輸入為包含 RSA 公開金鑰的憑證。

-sign

簽署輸入資料並輸出簽署結果。這需要 RSA 私密金鑰。

-verify

驗證輸入資料並輸出復原的資料。

-encrypt

使用 RSA 公鑰加密輸入資料。

-decrypt

使用 RSA 私鑰解密輸入資料。

-pkcs-oaep-x931-raw

要使用的填充:PKCS#1 v1.5(預設值)、PKCS#1 OAEP、ANSI X9.31 或不填充。對於簽章,只能使用 -pkcs-raw

注意:由於防護 Bleichenbacher 攻擊,使用 PKCS#1 v1.5 模式解密時,即使填充檢查失敗也不會傳回錯誤。請使用 -raw 並手動檢查傳回值,以確認填充是否正確。

-hexdump

將輸出資料轉換為十六進位傾印。

-asn1parse

剖析 ASN.1 輸出資料,這在與 -verify 選項結合使用時很有用。

-engine id

請參閱 openssl(1) 中的「引擎選項」。此選項已不建議使用。

-rand files-writerand file

請參閱 openssl(1) 中的「隨機狀態選項」 以取得詳細資料。

-provider name
-provider-path path
-propquery propq

請參閱 openssl(1) 中的「提供者選項」provider(7)property(7)

附註

由於此指令直接使用 RSA 演算法,因此只能用於簽署或驗證小型資料。

範例

可以在非不建議使用的 openssl-pkeyutl(1) 指令的說明文件中找到等同於這些範例的範例。

使用私鑰簽署一些資料

openssl rsautl -sign -in file -inkey key.pem -out sig

復原已簽署的資料

openssl rsautl -verify -in sig -inkey key.pem

檢查原始已簽署的資料

openssl rsautl -verify -in sig -inkey key.pem -raw -hexdump

0000 - 00 01 ff ff ff ff ff ff-ff ff ff ff ff ff ff ff   ................
0010 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff   ................
0020 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff   ................
0030 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff   ................
0040 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff   ................
0050 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff   ................
0060 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff   ................
0070 - ff ff ff ff 00 68 65 6c-6c 6f 20 77 6f 72 6c 64   .....hello world

PKCS#1 區塊格式化由此可見。如果這是使用加密和解密完成的,區塊類型將為 2(第二個位元組),並且會看到隨機填充資料,而不是 0xff 位元組。

可以使用此指令搭配 openssl-asn1parse(1) 來分析憑證的簽章。考慮 certs/pca-cert.pem 中的自簽署範例。執行 openssl-asn1parse(1) 如下所示,會產生

openssl asn1parse -in pca-cert.pem

   0:d=0  hl=4 l= 742 cons: SEQUENCE
   4:d=1  hl=4 l= 591 cons:  SEQUENCE
   8:d=2  hl=2 l=   3 cons:   cont [ 0 ]
  10:d=3  hl=2 l=   1 prim:    INTEGER           :02
  13:d=2  hl=2 l=   1 prim:   INTEGER           :00
  16:d=2  hl=2 l=  13 cons:   SEQUENCE
  18:d=3  hl=2 l=   9 prim:    OBJECT            :md5WithRSAEncryption
  29:d=3  hl=2 l=   0 prim:    NULL
  31:d=2  hl=2 l=  92 cons:   SEQUENCE
  33:d=3  hl=2 l=  11 cons:    SET
  35:d=4  hl=2 l=   9 cons:     SEQUENCE
  37:d=5  hl=2 l=   3 prim:      OBJECT            :countryName
  42:d=5  hl=2 l=   2 prim:      PRINTABLESTRING   :AU
 ....
 599:d=1  hl=2 l=  13 cons:  SEQUENCE
 601:d=2  hl=2 l=   9 prim:   OBJECT            :md5WithRSAEncryption
 612:d=2  hl=2 l=   0 prim:   NULL
 614:d=1  hl=3 l= 129 prim:  BIT STRING

最後的 BIT STRING 包含實際簽章。可以用以下方式提取:

openssl asn1parse -in pca-cert.pem -out sig -noout -strparse 614

可以用以下方式提取憑證公鑰:

openssl x509 -in test/testx509.pem -pubkey -noout >pubkey.pem

可以用以下方式分析簽章:

openssl rsautl -in sig -verify -asn1parse -inkey pubkey.pem -pubin

   0:d=0  hl=2 l=  32 cons: SEQUENCE
   2:d=1  hl=2 l=  12 cons:  SEQUENCE
   4:d=2  hl=2 l=   8 prim:   OBJECT            :md5
  14:d=2  hl=2 l=   0 prim:   NULL
  16:d=1  hl=2 l=  16 prim:  OCTET STRING
     0000 - f3 46 9e aa 1a 4a 73 c9-37 ea 93 00 48 25 08 b5   .F...Js.7...H%..

這是 ASN1 DigestInfo 結構的剖析版本。可以看到使用的摘要是 md5。可以用以下方式提取憑證中實際簽署的部分:

openssl asn1parse -in pca-cert.pem -out tbs -noout -strparse 4

以及使用

openssl md5 -c tbs
MD5(tbs)= f3:46:9e:aa:1a:4a:73:c9:37:ea:93:00:48:25:08:b5

計算出的摘要

請參閱

openssl(1)openssl-pkeyutl(1)openssl-dgst(1)openssl-rsa(1)openssl-genrsa(1)

歷史

此命令已在 OpenSSL 3.0 中棄用。

-engine 選項已在 OpenSSL 3.0 中棄用。

版權所有 2000-2023 The OpenSSL Project Authors。保留所有權利。

根據 Apache 授權條款 2.0(「授權條款」)授權。您只能在遵守授權條款的情況下使用此檔案。您可以在原始程式碼散佈中的 LICENSE 檔案或 https://www.openssl.org/source/license.html 取得副本。