5 OSSL_ENCODER_CTX_new_for_pkey,
6 OSSL_ENCODER_CTX_set_cipher,
7 OSSL_ENCODER_CTX_set_passphrase,
8 OSSL_ENCODER_CTX_set_pem_password_cb,
9 OSSL_ENCODER_CTX_set_passphrase_cb,
10 OSSL_ENCODER_CTX_set_passphrase_ui
11 - Encoder routines to encode EVP_PKEYs
15 #include <openssl/encoder.h>
18 OSSL_ENCODER_CTX_new_for_pkey(const EVP_PKEY *pkey, int selection,
19 const char *output_type,
20 const char *output_structure,
21 const char *propquery);
23 int OSSL_ENCODER_CTX_set_cipher(OSSL_ENCODER_CTX *ctx,
24 const char *cipher_name,
25 const char *propquery);
26 int OSSL_ENCODER_CTX_set_passphrase(OSSL_ENCODER_CTX *ctx,
27 const unsigned char *kstr,
29 int OSSL_ENCODER_CTX_set_pem_password_cb(OSSL_ENCODER_CTX *ctx,
30 pem_password_cb *cb, void *cbarg);
31 int OSSL_ENCODER_CTX_set_passphrase_ui(OSSL_ENCODER_CTX *ctx,
32 const UI_METHOD *ui_method,
34 int OSSL_ENCODER_CTX_set_passphrase_cb(OSSL_ENCODER_CTX *ctx,
35 OSSL_PASSPHRASE_CALLBACK *cb,
40 OSSL_ENCODER_CTX_new_for_pkey() is a utility function that creates a
41 B<OSSL_ENCODER_CTX>, finds all applicable encoder implementations and sets
42 them up, so almost all the caller has to do next is call functions like
43 L<OSSL_ENCODER_to_bio(3)>. I<output_type> determines the final output
44 encoding, and I<selection> can be used to select what parts of the I<pkey>
45 should be included in the output. I<output_type> is further discussed in
46 L</Output types> below, and I<selection> is further described in
49 Internally, OSSL_ENCODER_CTX_new_for_pkey() uses the names from the
50 L<EVP_KEYMGMT(3)> implementation associated with I<pkey> to build a list of
51 applicable encoder implementations that are used to process the I<pkey> into
52 the encoding named by I<output_type>, with the outermost structure named by
53 I<output_structure> if that's relevant. All these implementations are
54 implicitly fetched, with I<propquery> for finer selection.
56 If no suitable encoder implementation is found,
57 OSSL_ENCODER_CTX_new_for_pkey() still creates a B<OSSL_ENCODER_CTX>, but
58 with no associated encoder (L<OSSL_ENCODER_CTX_get_num_encoders(3)> returns
59 zero). This helps the caller to distinguish between an error when creating
60 the B<OSSL_ENCODER_CTX> and missing encoder implementation, and allows it to
63 OSSL_ENCODER_CTX_set_cipher() tells the implementation what cipher
64 should be used to encrypt encoded keys. The cipher is given by
65 name I<cipher_name>. The interpretation of that I<cipher_name> is
66 implementation dependent. The implementation may implement the cipher
67 directly itself or by other implementations, or it may choose to fetch
68 it. If the implementation supports fetching the cipher, then it may
69 use I<propquery> as properties to be queried for when fetching.
70 I<cipher_name> may also be NULL, which will result in unencrypted
73 OSSL_ENCODER_CTX_set_passphrase() gives the implementation a
74 pass phrase to use when encrypting the encoded private key.
75 Alternatively, a pass phrase callback may be specified with the
78 OSSL_ENCODER_CTX_set_pem_password_cb(), OSSL_ENCODER_CTX_set_passphrase_ui()
79 and OSSL_ENCODER_CTX_set_passphrase_cb() sets up a callback method that the
80 implementation can use to prompt for a pass phrase, giving the caller the
81 choice of prefered pass phrase callback form. These are called indirectly,
82 through an internal B<OSSL_PASSPHRASE_CALLBACK> function.
86 The possible B<EVP_PKEY> output types depends on the available
89 OpenSSL has built in implementations for the following output types:
95 The output is a human readable description of the key.
96 L<EVP_PKEY_print_private(3)>, L<EVP_PKEY_print_public(3)> and
97 L<EVP_PKEY_print_params(3)> use this for their output.
101 The output is the DER encoding of the I<selection> of the I<pkey>.
105 The output is the I<selection> of the I<pkey> in PEM format.
113 These constants should really be documented among the EVP manuals, but this
114 will have to do for now.
118 The following constants can be used for standard I<selection>:
122 =item B<EVP_PKEY_KEY_PARAMETERS>
124 Indicates that only the key parameters should be included in the output.
125 Where it matters, the data type in the output will indicate that the data is
126 parameters, not a key.
128 =item B<EVP_PKEY_PUBLIC_KEY>
130 Indicates that the public key and eventual key parameters will be included
131 in the output. Where it matters, the data type in the output will indicate
132 that the data is a public key.
134 =item B<EVP_PKEY_KEYPAIR>
136 Indicates that the private key, the public key and eventual key parameters
137 should be included in the output. Where it matters, the data type in the
138 output will indicate that the data is a private key.
142 These are only indications, the encoder implementations are free to
143 determine what makes sense to include in the output, and this may depend on
144 the desired output. For example, an EC key in a PKCS#8 structure doesn't
145 usually include the public key.
149 OSSL_ENCODER_CTX_new_for_pkey() returns a pointer to an B<OSSL_ENCODER_CTX>,
150 or NULL if it couldn't be created.
152 OSSL_ENCODER_CTX_set_cipher(), OSSL_ENCODER_CTX_set_passphrase(),
153 OSSL_ENCODER_CTX_set_pem_password_cb(), OSSL_ENCODER_CTX_set_passphrase_ui()
154 and OSSL_ENCODER_CTX_set_passphrase_cb() all return 1 on success, or 0 on
159 L<provider(7)>, L<OSSL_ENCODER(3)>, L<OSSL_ENCODER_CTX(3)>
163 The functions described here were added in OpenSSL 3.0.
167 Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
169 Licensed under the Apache License 2.0 (the "License"). You may not use
170 this file except in compliance with the License. You can obtain a copy
171 in the file LICENSE in the source distribution or at
172 L<https://www.openssl.org/source/license.html>.