lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | =pod |
| 2 | |
| 3 | =head1 NAME |
| 4 | |
| 5 | openssl-pkeyutl, |
| 6 | pkeyutl - public key algorithm utility |
| 7 | |
| 8 | =head1 SYNOPSIS |
| 9 | |
| 10 | B<openssl> B<pkeyutl> |
| 11 | [B<-help>] |
| 12 | [B<-in file>] |
| 13 | [B<-out file>] |
| 14 | [B<-sigfile file>] |
| 15 | [B<-inkey file>] |
| 16 | [B<-keyform PEM|DER|ENGINE>] |
| 17 | [B<-passin arg>] |
| 18 | [B<-peerkey file>] |
| 19 | [B<-peerform PEM|DER|ENGINE>] |
| 20 | [B<-pubin>] |
| 21 | [B<-certin>] |
| 22 | [B<-rev>] |
| 23 | [B<-sign>] |
| 24 | [B<-verify>] |
| 25 | [B<-verifyrecover>] |
| 26 | [B<-encrypt>] |
| 27 | [B<-decrypt>] |
| 28 | [B<-derive>] |
| 29 | [B<-kdf algorithm>] |
| 30 | [B<-kdflen length>] |
| 31 | [B<-pkeyopt opt:value>] |
| 32 | [B<-hexdump>] |
| 33 | [B<-asn1parse>] |
| 34 | [B<-rand file...>] |
| 35 | [B<-writerand file>] |
| 36 | [B<-engine id>] |
| 37 | [B<-engine_impl>] |
| 38 | |
| 39 | =head1 DESCRIPTION |
| 40 | |
| 41 | The B<pkeyutl> command can be used to perform low-level public key operations |
| 42 | using any supported algorithm. |
| 43 | |
| 44 | =head1 OPTIONS |
| 45 | |
| 46 | =over 4 |
| 47 | |
| 48 | =item B<-help> |
| 49 | |
| 50 | Print out a usage message. |
| 51 | |
| 52 | =item B<-in filename> |
| 53 | |
| 54 | This specifies the input filename to read data from or standard input |
| 55 | if this option is not specified. |
| 56 | |
| 57 | =item B<-out filename> |
| 58 | |
| 59 | Specifies the output filename to write to or standard output by |
| 60 | default. |
| 61 | |
| 62 | =item B<-sigfile file> |
| 63 | |
| 64 | Signature file, required for B<verify> operations only |
| 65 | |
| 66 | =item B<-inkey file> |
| 67 | |
| 68 | The input key file, by default it should be a private key. |
| 69 | |
| 70 | =item B<-keyform PEM|DER|ENGINE> |
| 71 | |
| 72 | The key format PEM, DER or ENGINE. Default is PEM. |
| 73 | |
| 74 | =item B<-passin arg> |
| 75 | |
| 76 | The input key password source. For more information about the format of B<arg> |
| 77 | see L<openssl(1)/Pass Phrase Options>. |
| 78 | |
| 79 | =item B<-peerkey file> |
| 80 | |
| 81 | The peer key file, used by key derivation (agreement) operations. |
| 82 | |
| 83 | =item B<-peerform PEM|DER|ENGINE> |
| 84 | |
| 85 | The peer key format PEM, DER or ENGINE. Default is PEM. |
| 86 | |
| 87 | =item B<-pubin> |
| 88 | |
| 89 | The input file is a public key. |
| 90 | |
| 91 | =item B<-certin> |
| 92 | |
| 93 | The input is a certificate containing a public key. |
| 94 | |
| 95 | =item B<-rev> |
| 96 | |
| 97 | Reverse the order of the input buffer. This is useful for some libraries |
| 98 | (such as CryptoAPI) which represent the buffer in little endian format. |
| 99 | |
| 100 | =item B<-sign> |
| 101 | |
| 102 | Sign the input data (which must be a hash) and output the signed result. This |
| 103 | requires a private key. |
| 104 | |
| 105 | =item B<-verify> |
| 106 | |
| 107 | Verify the input data (which must be a hash) against the signature file and |
| 108 | indicate if the verification succeeded or failed. |
| 109 | |
| 110 | =item B<-verifyrecover> |
| 111 | |
| 112 | Verify the input data (which must be a hash) and output the recovered data. |
| 113 | |
| 114 | =item B<-encrypt> |
| 115 | |
| 116 | Encrypt the input data using a public key. |
| 117 | |
| 118 | =item B<-decrypt> |
| 119 | |
| 120 | Decrypt the input data using a private key. |
| 121 | |
| 122 | =item B<-derive> |
| 123 | |
| 124 | Derive a shared secret using the peer key. |
| 125 | |
| 126 | =item B<-kdf algorithm> |
| 127 | |
| 128 | Use key derivation function B<algorithm>. The supported algorithms are |
| 129 | at present B<TLS1-PRF> and B<HKDF>. |
| 130 | Note: additional parameters and the KDF output length will normally have to be |
| 131 | set for this to work. |
| 132 | See L<EVP_PKEY_CTX_set_hkdf_md(3)> and L<EVP_PKEY_CTX_set_tls1_prf_md(3)> |
| 133 | for the supported string parameters of each algorithm. |
| 134 | |
| 135 | =item B<-kdflen length> |
| 136 | |
| 137 | Set the output length for KDF. |
| 138 | |
| 139 | =item B<-pkeyopt opt:value> |
| 140 | |
| 141 | Public key options specified as opt:value. See NOTES below for more details. |
| 142 | |
| 143 | =item B<-hexdump> |
| 144 | |
| 145 | hex dump the output data. |
| 146 | |
| 147 | =item B<-asn1parse> |
| 148 | |
| 149 | Parse the ASN.1 output data, this is useful when combined with the |
| 150 | B<-verifyrecover> option when an ASN1 structure is signed. |
| 151 | |
| 152 | =item B<-rand file...> |
| 153 | |
| 154 | A file or files containing random data used to seed the random number |
| 155 | generator. |
| 156 | Multiple files can be specified separated by an OS-dependent character. |
| 157 | The separator is B<;> for MS-Windows, B<,> for OpenVMS, and B<:> for |
| 158 | all others. |
| 159 | |
| 160 | =item [B<-writerand file>] |
| 161 | |
| 162 | Writes random data to the specified I<file> upon exit. |
| 163 | This can be used with a subsequent B<-rand> flag. |
| 164 | |
| 165 | =item B<-engine id> |
| 166 | |
| 167 | Specifying an engine (by its unique B<id> string) will cause B<pkeyutl> |
| 168 | to attempt to obtain a functional reference to the specified engine, |
| 169 | thus initialising it if needed. The engine will then be set as the default |
| 170 | for all available algorithms. |
| 171 | |
| 172 | =item B<-engine_impl> |
| 173 | |
| 174 | When used with the B<-engine> option, it specifies to also use |
| 175 | engine B<id> for crypto operations. |
| 176 | |
| 177 | =back |
| 178 | |
| 179 | =head1 NOTES |
| 180 | |
| 181 | The operations and options supported vary according to the key algorithm |
| 182 | and its implementation. The OpenSSL operations and options are indicated below. |
| 183 | |
| 184 | Unless otherwise mentioned all algorithms support the B<digest:alg> option |
| 185 | which specifies the digest in use for sign, verify and verifyrecover operations. |
| 186 | The value B<alg> should represent a digest name as used in the |
| 187 | EVP_get_digestbyname() function for example B<sha1>. This value is not used to |
| 188 | hash the input data. It is used (by some algorithms) for sanity-checking the |
| 189 | lengths of data passed in to the B<pkeyutl> and for creating the structures that |
| 190 | make up the signature (e.g. B<DigestInfo> in RSASSA PKCS#1 v1.5 signatures). |
| 191 | |
| 192 | This utility does not hash the input data but rather it will use the data |
| 193 | directly as input to the signature algorithm. Depending on the key type, |
| 194 | signature type, and mode of padding, the maximum acceptable lengths of input |
| 195 | data differ. The signed data can't be longer than the key modulus with RSA. In |
| 196 | case of ECDSA and DSA the data shouldn't be longer than the field |
| 197 | size, otherwise it will be silently truncated to the field size. In any event |
| 198 | the input size must not be larger than the largest supported digest size. |
| 199 | |
| 200 | In other words, if the value of digest is B<sha1> the input should be the 20 |
| 201 | bytes long binary encoding of the SHA-1 hash function output. |
| 202 | |
| 203 | The Ed25519 and Ed448 signature algorithms are not supported by this utility. |
| 204 | They accept non-hashed input, but this utility can only be used to sign hashed |
| 205 | input. |
| 206 | |
| 207 | =head1 RSA ALGORITHM |
| 208 | |
| 209 | The RSA algorithm generally supports the encrypt, decrypt, sign, |
| 210 | verify and verifyrecover operations. However, some padding modes |
| 211 | support only a subset of these operations. The following additional |
| 212 | B<pkeyopt> values are supported: |
| 213 | |
| 214 | =over 4 |
| 215 | |
| 216 | =item B<rsa_padding_mode:mode> |
| 217 | |
| 218 | This sets the RSA padding mode. Acceptable values for B<mode> are B<pkcs1> for |
| 219 | PKCS#1 padding, B<sslv23> for SSLv23 padding, B<none> for no padding, B<oaep> |
| 220 | for B<OAEP> mode, B<x931> for X9.31 mode and B<pss> for PSS. |
| 221 | |
| 222 | In PKCS#1 padding if the message digest is not set then the supplied data is |
| 223 | signed or verified directly instead of using a B<DigestInfo> structure. If a |
| 224 | digest is set then the a B<DigestInfo> structure is used and its the length |
| 225 | must correspond to the digest type. |
| 226 | |
| 227 | For B<oaep> mode only encryption and decryption is supported. |
| 228 | |
| 229 | For B<x931> if the digest type is set it is used to format the block data |
| 230 | otherwise the first byte is used to specify the X9.31 digest ID. Sign, |
| 231 | verify and verifyrecover are can be performed in this mode. |
| 232 | |
| 233 | For B<pss> mode only sign and verify are supported and the digest type must be |
| 234 | specified. |
| 235 | |
| 236 | =item B<rsa_pss_saltlen:len> |
| 237 | |
| 238 | For B<pss> mode only this option specifies the salt length. Three special |
| 239 | values are supported: "digest" sets the salt length to the digest length, |
| 240 | "max" sets the salt length to the maximum permissible value. When verifying |
| 241 | "auto" causes the salt length to be automatically determined based on the |
| 242 | B<PSS> block structure. |
| 243 | |
| 244 | =item B<rsa_mgf1_md:digest> |
| 245 | |
| 246 | For PSS and OAEP padding sets the MGF1 digest. If the MGF1 digest is not |
| 247 | explicitly set in PSS mode then the signing digest is used. |
| 248 | |
| 249 | =item B<rsa_oaep_md:>I<digest> |
| 250 | |
| 251 | Sets the digest used for the OAEP hash function. If not explicitly set then |
| 252 | SHA1 is used. |
| 253 | |
| 254 | =back |
| 255 | |
| 256 | =head1 RSA-PSS ALGORITHM |
| 257 | |
| 258 | The RSA-PSS algorithm is a restricted version of the RSA algorithm which only |
| 259 | supports the sign and verify operations with PSS padding. The following |
| 260 | additional B<pkeyopt> values are supported: |
| 261 | |
| 262 | =over 4 |
| 263 | |
| 264 | =item B<rsa_padding_mode:mode>, B<rsa_pss_saltlen:len>, B<rsa_mgf1_md:digest> |
| 265 | |
| 266 | These have the same meaning as the B<RSA> algorithm with some additional |
| 267 | restrictions. The padding mode can only be set to B<pss> which is the |
| 268 | default value. |
| 269 | |
| 270 | If the key has parameter restrictions than the digest, MGF1 |
| 271 | digest and salt length are set to the values specified in the parameters. |
| 272 | The digest and MG cannot be changed and the salt length cannot be set to a |
| 273 | value less than the minimum restriction. |
| 274 | |
| 275 | =back |
| 276 | |
| 277 | =head1 DSA ALGORITHM |
| 278 | |
| 279 | The DSA algorithm supports signing and verification operations only. Currently |
| 280 | there are no additional B<-pkeyopt> options other than B<digest>. The SHA1 |
| 281 | digest is assumed by default. |
| 282 | |
| 283 | =head1 DH ALGORITHM |
| 284 | |
| 285 | The DH algorithm only supports the derivation operation and no additional |
| 286 | B<-pkeyopt> options. |
| 287 | |
| 288 | =head1 EC ALGORITHM |
| 289 | |
| 290 | The EC algorithm supports sign, verify and derive operations. The sign and |
| 291 | verify operations use ECDSA and derive uses ECDH. SHA1 is assumed by default for |
| 292 | the B<-pkeyopt> B<digest> option. |
| 293 | |
| 294 | =head1 X25519 and X448 ALGORITHMS |
| 295 | |
| 296 | The X25519 and X448 algorithms support key derivation only. Currently there are |
| 297 | no additional options. |
| 298 | |
| 299 | =head1 EXAMPLES |
| 300 | |
| 301 | Sign some data using a private key: |
| 302 | |
| 303 | openssl pkeyutl -sign -in file -inkey key.pem -out sig |
| 304 | |
| 305 | Recover the signed data (e.g. if an RSA key is used): |
| 306 | |
| 307 | openssl pkeyutl -verifyrecover -in sig -inkey key.pem |
| 308 | |
| 309 | Verify the signature (e.g. a DSA key): |
| 310 | |
| 311 | openssl pkeyutl -verify -in file -sigfile sig -inkey key.pem |
| 312 | |
| 313 | Sign data using a message digest value (this is currently only valid for RSA): |
| 314 | |
| 315 | openssl pkeyutl -sign -in file -inkey key.pem -out sig -pkeyopt digest:sha256 |
| 316 | |
| 317 | Derive a shared secret value: |
| 318 | |
| 319 | openssl pkeyutl -derive -inkey key.pem -peerkey pubkey.pem -out secret |
| 320 | |
| 321 | Hexdump 48 bytes of TLS1 PRF using digest B<SHA256> and shared secret and |
| 322 | seed consisting of the single byte 0xFF: |
| 323 | |
| 324 | openssl pkeyutl -kdf TLS1-PRF -kdflen 48 -pkeyopt md:SHA256 \ |
| 325 | -pkeyopt hexsecret:ff -pkeyopt hexseed:ff -hexdump |
| 326 | |
| 327 | Decrypt some data using a private key with OAEP padding using SHA256: |
| 328 | |
| 329 | openssl pkeyutl -decrypt -in file -inkey key.pem -out secret \ |
| 330 | -pkeyopt rsa_padding_mode:oaep -pkeyopt rsa_oaep_md:sha256 |
| 331 | |
| 332 | =head1 SEE ALSO |
| 333 | |
| 334 | L<genpkey(1)>, L<pkey(1)>, L<rsautl(1)> |
| 335 | L<dgst(1)>, L<rsa(1)>, L<genrsa(1)>, |
| 336 | L<EVP_PKEY_CTX_set_hkdf_md(3)>, L<EVP_PKEY_CTX_set_tls1_prf_md(3)> |
| 337 | |
| 338 | =head1 COPYRIGHT |
| 339 | |
| 340 | Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved. |
| 341 | |
| 342 | Licensed under the OpenSSL license (the "License"). You may not use |
| 343 | this file except in compliance with the License. You can obtain a copy |
| 344 | in the file LICENSE in the source distribution or at |
| 345 | L<https://www.openssl.org/source/license.html>. |
| 346 | |
| 347 | =cut |