openssl-pkeyutl.pod.in 14 KB

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