openssl-pkeyutl.pod.in 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. =pod
  2. {- OpenSSL::safe::output_do_not_edit_headers(); -}
  3. =head1 NAME
  4. openssl-pkeyutl - public key algorithm command
  5. =head1 SYNOPSIS
  6. B<openssl> B<pkeyutl>
  7. [B<-help>]
  8. [B<-in> I<file>]
  9. [B<-rawin>]
  10. [B<-digest> I<algorithm>]
  11. [B<-out> I<file>]
  12. [B<-sigfile> I<file>]
  13. [B<-inkey> I<filename>|I<uri>]
  14. [B<-keyform> B<DER>|B<PEM>|B<P12>|B<ENGINE>]
  15. [B<-passin> I<arg>]
  16. [B<-peerkey> I<file>]
  17. [B<-peerform> B<DER>|B<PEM>|B<P12>|B<ENGINE>]
  18. [B<-pubin>]
  19. [B<-certin>]
  20. [B<-rev>]
  21. [B<-sign>]
  22. [B<-verify>]
  23. [B<-verifyrecover>]
  24. [B<-encrypt>]
  25. [B<-decrypt>]
  26. [B<-derive>]
  27. [B<-kdf> I<algorithm>]
  28. [B<-kdflen> I<length>]
  29. [B<-pkeyopt> I<opt>:I<value>]
  30. [B<-pkeyopt_passin> I<opt>[:I<passarg>]]
  31. [B<-hexdump>]
  32. [B<-asn1parse>]
  33. {- $OpenSSL::safe::opt_engine_synopsis -}[B<-engine_impl>]
  34. {- $OpenSSL::safe::opt_r_synopsis -}
  35. {- $OpenSSL::safe::opt_provider_synopsis -}
  36. {- $OpenSSL::safe::opt_config_synopsis -}
  37. =head1 DESCRIPTION
  38. This command can be used to perform low-level public key
  39. operations using any supported algorithm.
  40. By default the signing operation (see B<-sign> option) is assumed.
  41. =head1 OPTIONS
  42. =over 4
  43. =item B<-help>
  44. Print out a usage message.
  45. =item B<-in> I<filename>
  46. This specifies the input filename to read data from or standard input
  47. if this option is not specified.
  48. =item B<-rawin>
  49. This indicates that the signature or verification input data is raw data,
  50. which is not hashed by any message digest algorithm.
  51. Except with EdDSA,
  52. the user can specify a digest algorithm by using the B<-digest> option.
  53. For signature algorithms like RSA, DSA and ECDSA,
  54. the default digest algorithm is SHA-256. For SM2, it is SM3.
  55. This option can only be used with B<-sign> and B<-verify>.
  56. For EdDSA (the Ed25519 and Ed448 algorithms) this option is required.
  57. =item B<-digest> I<algorithm>
  58. This option can only be used with B<-sign> and B<-verify>.
  59. It specifies the digest algorithm that is used to hash the input data
  60. before signing or verifying it with the input key. This option could be omitted
  61. if the signature algorithm does not require preprocessing the input through
  62. a pluggable hash function before signing (for instance, EdDSA). If this option
  63. is omitted but the signature algorithm requires one and the B<-rawin> option
  64. is given, a default value will be used (see B<-rawin> for details).
  65. If this option is present, then the B<-rawin> option is required.
  66. At this time, HashEdDSA (the ph or "prehash" variant of EdDSA) is not supported,
  67. so the B<-digest> option cannot be used with EdDSA.
  68. =item B<-out> I<filename>
  69. Specifies the output filename to write to or standard output by
  70. default.
  71. =item B<-sigfile> I<file>
  72. Signature file, required and allowed for B<-verify> operations only
  73. =item B<-inkey> I<filename>|I<uri>
  74. The input key, by default it should be a private key.
  75. =item B<-keyform> B<DER>|B<PEM>|B<P12>|B<ENGINE>
  76. The key format; unspecified by default.
  77. See L<openssl-format-options(1)> for details.
  78. =item B<-passin> I<arg>
  79. The input key password source. For more information about the format of I<arg>
  80. see L<openssl-passphrase-options(1)>.
  81. =item B<-peerkey> I<file>
  82. The peer key file, used by key derivation (agreement) operations.
  83. =item B<-peerform> B<DER>|B<PEM>|B<P12>|B<ENGINE>
  84. The peer key format; unspecified by default.
  85. See L<openssl-format-options(1)> for details.
  86. =item B<-pubin>
  87. By default a private key is read from the key input.
  88. With this option a public key is read instead.
  89. If the input contains no public key but a private key, its public part is used.
  90. =item B<-certin>
  91. The input is a certificate containing a public key.
  92. =item B<-rev>
  93. Reverse the order of the input buffer. This is useful for some libraries
  94. (such as CryptoAPI) which represent the buffer in little-endian format.
  95. This cannot be used in conjunction with B<-rawin>.
  96. =item B<-sign>
  97. Sign the input data and output the signed result. This requires a private key.
  98. Using a message digest operation along with this is recommended,
  99. when applicable, see the B<-rawin> and B<-digest> options for details.
  100. Otherwise, the input data given with the B<-in> option is assumed to already
  101. be a digest, but this may then require an additional B<-pkeyopt> C<digest:>I<md>
  102. in some cases (e.g., RSA with the default PKCS#1 padding mode).
  103. Even for other algorithms like ECDSA, where the additional B<-pkeyopt> option
  104. does not affect signature output, it is recommended, as it enables
  105. checking that the input length is consistent with the intended digest.
  106. =item B<-verify>
  107. Verify the input data against the signature given with the B<-sigfile> option
  108. and indicate if the verification succeeded or failed.
  109. The input data given with the B<-in> option is assumed to be a hash value
  110. unless the B<-rawin> option is specified or implied.
  111. With raw data, when a digest algorithm is applicable, though it may be inferred
  112. from the signature or take a default value, it should also be specified.
  113. =item B<-verifyrecover>
  114. Verify the given signature and output the recovered data (signature payload).
  115. For example, in case of RSA PKCS#1 the recovered data is the B<EMSA-PKCS-v1_5>
  116. DER encoding of the digest algorithm OID and value as specified in
  117. L<RFC8017 Section 9.2|https://datatracker.ietf.org/doc/html/rfc8017#section-9.2>.
  118. Note that here the input given with the B<-in> option is not a signature input
  119. (as with the B<-sign> and B<-verify> options) but a signature output value,
  120. typically produced using the B<-sign> option.
  121. This option is available only for use with RSA keys.
  122. =item B<-encrypt>
  123. Encrypt the input data using a public key.
  124. =item B<-decrypt>
  125. Decrypt the input data using a private key.
  126. =item B<-derive>
  127. Derive a shared secret using the peer key.
  128. =item B<-kdf> I<algorithm>
  129. Use key derivation function I<algorithm>. The supported algorithms are
  130. at present B<TLS1-PRF> and B<HKDF>.
  131. Note: additional parameters and the KDF output length will normally have to be
  132. set for this to work.
  133. See L<EVP_PKEY_CTX_set_hkdf_md(3)> and L<EVP_PKEY_CTX_set_tls1_prf_md(3)>
  134. for the supported string parameters of each algorithm.
  135. =item B<-kdflen> I<length>
  136. Set the output length for KDF.
  137. =item B<-pkeyopt> I<opt>:I<value>
  138. Public key options specified as opt:value. See NOTES below for more details.
  139. =item B<-pkeyopt_passin> I<opt>[:I<passarg>]
  140. Allows reading a public key option I<opt> from stdin or a password source.
  141. If only I<opt> is specified, the user will be prompted to enter a password on
  142. stdin. Alternatively, I<passarg> can be specified which can be any value
  143. supported by L<openssl-passphrase-options(1)>.
  144. =item B<-hexdump>
  145. hex dump the output data.
  146. =item B<-asn1parse>
  147. Parse the ASN.1 output data to check its DER encoding and print any errors.
  148. When combined with the B<-verifyrecover> option, this may be useful only in case
  149. an ASN.1 DER-encoded structure had been signed directly (without hashing it).
  150. {- $OpenSSL::safe::opt_engine_item -}
  151. {- output_off() if $disabled{"deprecated-3.0"}; "" -}
  152. =item B<-engine_impl>
  153. When used with the B<-engine> option, it specifies to also use
  154. engine I<id> for crypto operations.
  155. {- output_on() if $disabled{"deprecated-3.0"}; "" -}
  156. {- $OpenSSL::safe::opt_r_item -}
  157. {- $OpenSSL::safe::opt_provider_item -}
  158. {- $OpenSSL::safe::opt_config_item -}
  159. =back
  160. =head1 NOTES
  161. The operations and options supported vary according to the key algorithm
  162. and its implementation. The OpenSSL operations and options are indicated below.
  163. Unless otherwise mentioned, all algorithms support the B<digest:>I<alg> option,
  164. which specifies the digest in use for the signing and verification operations.
  165. The value I<alg> should represent a digest name as used in the
  166. EVP_get_digestbyname() function for example B<sha1>. This value is not used to
  167. hash the input data. It is used (by some algorithms) for sanity-checking the
  168. lengths of data passed in and for creating the structures that make up the
  169. signature (e.g. B<DigestInfo> in RSASSA PKCS#1 v1.5 signatures).
  170. This command does not hash the input data (except where -rawin is used) but
  171. rather it will use the data directly as input to the signature algorithm.
  172. Depending on the key type, signature type, and mode of padding, the maximum
  173. acceptable lengths of input data differ. The signed data can't be longer than
  174. the key modulus with RSA. In case of ECDSA and DSA the data shouldn't be longer
  175. than the field size, otherwise it will be silently truncated to the field size.
  176. In any event the input size must not be larger than the largest supported digest
  177. size.
  178. In other words, if the value of digest is B<sha1> the input should be the 20
  179. bytes long binary encoding of the SHA-1 hash function output.
  180. =head1 RSA ALGORITHM
  181. The RSA algorithm generally supports the encrypt, decrypt, sign,
  182. verify and verifyrecover operations. However, some padding modes
  183. support only a subset of these operations. The following additional
  184. B<pkeyopt> values are supported:
  185. =over 4
  186. =item B<rsa_padding_mode:>I<mode>
  187. This sets the RSA padding mode. Acceptable values for I<mode> are B<pkcs1> for
  188. PKCS#1 padding, B<none> for no padding, B<oaep>
  189. for B<OAEP> mode, B<x931> for X9.31 mode and B<pss> for PSS.
  190. In PKCS#1 padding, if the message digest is not set, then the supplied data is
  191. signed or verified directly instead of using a B<DigestInfo> structure. If a
  192. digest is set, then the B<DigestInfo> structure is used and its length
  193. must correspond to the digest type.
  194. Note, for B<pkcs1> padding, as a protection against the Bleichenbacher attack,
  195. the decryption will not fail in case of padding check failures. Use B<none>
  196. and manual inspection of the decrypted message to verify if the decrypted
  197. value has correct PKCS#1 v1.5 padding.
  198. For B<oaep> mode only encryption and decryption is supported.
  199. For B<x931> if the digest type is set it is used to format the block data
  200. otherwise the first byte is used to specify the X9.31 digest ID. Sign,
  201. verify and verifyrecover are can be performed in this mode.
  202. For B<pss> mode only sign and verify are supported and the digest type must be
  203. specified.
  204. =item B<rsa_pss_saltlen:>I<len>
  205. For B<pss> mode only this option specifies the salt length. Three special
  206. values are supported: B<digest> sets the salt length to the digest length,
  207. B<max> sets the salt length to the maximum permissible value. When verifying
  208. B<auto> causes the salt length to be automatically determined based on the
  209. B<PSS> block structure.
  210. =item B<rsa_mgf1_md:>I<digest>
  211. For PSS and OAEP padding sets the MGF1 digest. If the MGF1 digest is not
  212. explicitly set in PSS mode then the signing digest is used.
  213. =item B<rsa_oaep_md:>I<digest>
  214. Sets the digest used for the OAEP hash function. If not explicitly set then
  215. SHA1 is used.
  216. =item B<rsa_pkcs1_implicit_rejection:>I<flag>
  217. Disables (when set to 0) or enables (when set to 1) the use of implicit
  218. rejection with PKCS#1 v1.5 decryption. When enabled (the default), as a
  219. protection against Bleichenbacher attack, the library will generate a
  220. deterministic random plaintext that it will return to the caller in case
  221. of padding check failure.
  222. When disabled, it's the callers' responsibility to handle the returned
  223. errors in a side-channel free manner.
  224. =back
  225. =head1 RSA-PSS ALGORITHM
  226. The RSA-PSS algorithm is a restricted version of the RSA algorithm which only
  227. supports the sign and verify operations with PSS padding. The following
  228. additional B<-pkeyopt> values are supported:
  229. =over 4
  230. =item B<rsa_padding_mode:>I<mode>, B<rsa_pss_saltlen:>I<len>,
  231. B<rsa_mgf1_md:>I<digest>
  232. These have the same meaning as the B<RSA> algorithm with some additional
  233. restrictions. The padding mode can only be set to B<pss> which is the
  234. default value.
  235. If the key has parameter restrictions then the digest, MGF1
  236. digest and salt length are set to the values specified in the parameters.
  237. The digest and MG cannot be changed and the salt length cannot be set to a
  238. value less than the minimum restriction.
  239. =back
  240. =head1 DSA ALGORITHM
  241. The DSA algorithm supports signing and verification operations only. Currently
  242. there are no additional B<-pkeyopt> options other than B<digest>. The SHA1
  243. digest is assumed by default.
  244. =head1 DH ALGORITHM
  245. The DH algorithm only supports the derivation operation and no additional
  246. B<-pkeyopt> options.
  247. =head1 EC ALGORITHM
  248. The EC algorithm supports sign, verify and derive operations. The sign and
  249. verify operations use ECDSA and derive uses ECDH. SHA1 is assumed by default for
  250. the B<-pkeyopt> B<digest> option.
  251. =head1 X25519 AND X448 ALGORITHMS
  252. The X25519 and X448 algorithms support key derivation only. Currently there are
  253. no additional options.
  254. =head1 ED25519 AND ED448 ALGORITHMS
  255. These algorithms only support signing and verifying. OpenSSL only implements the
  256. "pure" variants of these algorithms so raw data can be passed directly to them
  257. without hashing them first. The option B<-rawin> must be used with these
  258. algorithms with no B<-digest> specified. Additionally OpenSSL only supports
  259. "oneshot" operation with these algorithms. This means that the entire file to
  260. be signed/verified must be read into memory before processing it. Signing or
  261. Verifying very large files should be avoided. Additionally the size of the file
  262. must be known for this to work. If the size of the file cannot be determined
  263. (for example if the input is stdin) then the sign or verify operation will fail.
  264. =head1 SM2
  265. The SM2 algorithm supports sign, verify, encrypt and decrypt operations. For
  266. the sign and verify operations, SM2 requires an Distinguishing ID string to
  267. be passed in. The following B<-pkeyopt> value is supported:
  268. =over 4
  269. =item B<distid:>I<string>
  270. This sets the ID string used in SM2 sign or verify operations. While verifying
  271. an SM2 signature, the ID string must be the same one used when signing the data.
  272. Otherwise the verification will fail.
  273. =item B<hexdistid:>I<hex_string>
  274. This sets the ID string used in SM2 sign or verify operations. While verifying
  275. an SM2 signature, the ID string must be the same one used when signing the data.
  276. Otherwise the verification will fail. The ID string provided with this option
  277. should be a valid hexadecimal value.
  278. =back
  279. =head1 EXAMPLES
  280. Sign some data using a private key:
  281. openssl pkeyutl -sign -in file -inkey key.pem -out sig
  282. Recover the signed data (e.g. if an RSA key is used):
  283. openssl pkeyutl -verifyrecover -in sig -inkey key.pem
  284. Verify the signature (e.g. a DSA key):
  285. openssl pkeyutl -verify -in file -sigfile sig -inkey key.pem
  286. Sign data using a message digest value (this is currently only valid for RSA):
  287. openssl pkeyutl -sign -in file -inkey key.pem -out sig -pkeyopt digest:sha256
  288. Derive a shared secret value:
  289. openssl pkeyutl -derive -inkey key.pem -peerkey pubkey.pem -out secret
  290. Hexdump 48 bytes of TLS1 PRF using digest B<SHA256> and shared secret and
  291. seed consisting of the single byte 0xFF:
  292. openssl pkeyutl -kdf TLS1-PRF -kdflen 48 -pkeyopt md:SHA256 \
  293. -pkeyopt hexsecret:ff -pkeyopt hexseed:ff -hexdump
  294. Derive a key using B<scrypt> where the password is read from command line:
  295. openssl pkeyutl -kdf scrypt -kdflen 16 -pkeyopt_passin pass \
  296. -pkeyopt hexsalt:aabbcc -pkeyopt N:16384 -pkeyopt r:8 -pkeyopt p:1
  297. Derive using the same algorithm, but read key from environment variable MYPASS:
  298. openssl pkeyutl -kdf scrypt -kdflen 16 -pkeyopt_passin pass:env:MYPASS \
  299. -pkeyopt hexsalt:aabbcc -pkeyopt N:16384 -pkeyopt r:8 -pkeyopt p:1
  300. Sign some data using an L<SM2(7)> private key and a specific ID:
  301. openssl pkeyutl -sign -in file -inkey sm2.key -out sig -rawin -digest sm3 \
  302. -pkeyopt distid:someid
  303. Verify some data using an L<SM2(7)> certificate and a specific ID:
  304. openssl pkeyutl -verify -certin -in file -inkey sm2.cert -sigfile sig \
  305. -rawin -digest sm3 -pkeyopt distid:someid
  306. Decrypt some data using a private key with OAEP padding using SHA256:
  307. openssl pkeyutl -decrypt -in file -inkey key.pem -out secret \
  308. -pkeyopt rsa_padding_mode:oaep -pkeyopt rsa_oaep_md:sha256
  309. =head1 SEE ALSO
  310. L<openssl(1)>,
  311. L<openssl-genpkey(1)>,
  312. L<openssl-pkey(1)>,
  313. L<openssl-rsautl(1)>
  314. L<openssl-dgst(1)>,
  315. L<openssl-rsa(1)>,
  316. L<openssl-genrsa(1)>,
  317. L<openssl-kdf(1)>
  318. L<EVP_PKEY_CTX_set_hkdf_md(3)>,
  319. L<EVP_PKEY_CTX_set_tls1_prf_md(3)>,
  320. =head1 HISTORY
  321. The B<-engine> option was deprecated in OpenSSL 3.0.
  322. =head1 COPYRIGHT
  323. Copyright 2006-2023 The OpenSSL Project Authors. All Rights Reserved.
  324. Licensed under the Apache License 2.0 (the "License"). You may not use
  325. this file except in compliance with the License. You can obtain a copy
  326. in the file LICENSE in the source distribution or at
  327. L<https://www.openssl.org/source/license.html>.
  328. =cut