1=pod
2
3=head1 NAME
4
5d2i_PrivateKey_ex, d2i_PrivateKey, d2i_PublicKey, d2i_KeyParams,
6d2i_AutoPrivateKey_ex,  d2i_AutoPrivateKey, i2d_PrivateKey,
7i2d_PKCS8PrivateKey, i2d_PublicKey, i2d_KeyParams, i2d_KeyParams_bio,
8d2i_PrivateKey_ex_bio, d2i_PrivateKey_bio, d2i_PrivateKey_ex_fp,
9d2i_PrivateKey_fp, d2i_KeyParams_bio, i2d_PrivateKey_bio, i2d_PrivateKey_fp
10- decode and encode functions for reading and saving EVP_PKEY structures
11
12=head1 SYNOPSIS
13
14 #include <openssl/evp.h>
15
16 EVP_PKEY *d2i_PrivateKey_ex(int type, EVP_PKEY **a, const unsigned char **pp,
17                             long length, OSSL_LIB_CTX *libctx,
18                             const char *propq);
19 EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp,
20                          long length);
21 EVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **a, const unsigned char **pp,
22                         long length);
23 EVP_PKEY *d2i_KeyParams(int type, EVP_PKEY **a, const unsigned char **pp,
24                         long length);
25 EVP_PKEY *d2i_AutoPrivateKey_ex(EVP_PKEY **a, const unsigned char **pp,
26                                 long length, OSSL_LIB_CTX *libctx,
27                                 const char *propq);
28 EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **a, const unsigned char **pp,
29                              long length);
30
31 int i2d_PrivateKey(const EVP_PKEY *a, unsigned char **pp);
32 int i2d_PKCS8PrivateKey(const EVP_PKEY *a, unsigned char **pp);
33 int i2d_PublicKey(const EVP_PKEY *a, unsigned char **pp);
34 int i2d_KeyParams(const EVP_PKEY *a, unsigned char **pp);
35 int i2d_KeyParams_bio(BIO *bp, const EVP_PKEY *pkey);
36 EVP_PKEY *d2i_KeyParams_bio(int type, EVP_PKEY **a, BIO *in);
37
38
39 #include <openssl/x509.h>
40
41 EVP_PKEY *d2i_PrivateKey_ex_bio(BIO *bp, EVP_PKEY **a, OSSL_LIB_CTX *libctx,
42                                 const char *propq);
43 EVP_PKEY *d2i_PrivateKey_bio(BIO *bp, EVP_PKEY **a);
44 EVP_PKEY *d2i_PrivateKey_ex_fp(FILE *fp, EVP_PKEY **a, OSSL_LIB_CTX *libctx,
45                                const char *propq);
46 EVP_PKEY *d2i_PrivateKey_fp(FILE *fp, EVP_PKEY **a);
47
48 int i2d_PrivateKey_bio(BIO *bp, const EVP_PKEY *pkey);
49 int i2d_PrivateKey_fp(FILE *fp, const EVP_PKEY *pkey);
50
51=head1 DESCRIPTION
52
53d2i_PrivateKey_ex() decodes a private key using algorithm I<type>. It attempts
54to use any key-specific format or PKCS#8 unencrypted PrivateKeyInfo format.
55The I<type> parameter should be a public key algorithm constant such as
56B<EVP_PKEY_RSA>. An error occurs if the decoded key does not match I<type>. Some
57private key decoding implementations may use cryptographic algorithms (for
58example to automatically derive the public key if it is not explicitly
59included in the encoding). In this case the supplied library context I<libctx>
60and property query string I<propq> are used.
61If successful and the I<a> parameter is not NULL the function assigns the
62returned B<EVP_PKEY> structure pointer to I<*a>, overwriting any previous value.
63
64d2i_PrivateKey() does the same as d2i_PrivateKey_ex() except that the default
65library context and property query string are used.
66d2i_PublicKey() does the same for public keys.
67d2i_KeyParams() does the same for key parameters.
68
69The d2i_PrivateKey_ex_bio() and d2i_PrivateKey_bio() functions are similar to
70d2i_PrivateKey_ex() and d2i_PrivateKey() respectively except that they decode
71the data read from the given BIO. The d2i_PrivateKey_ex_fp() and
72d2i_PrivateKey_fp() functions are the same except that they read the data from
73the given FILE.
74
75d2i_AutoPrivateKey_ex() and d2i_AutoPrivateKey() are similar to
76d2i_PrivateKey_ex() and d2i_PrivateKey() respectively except that they attempt
77to automatically detect the private key format.
78
79i2d_PrivateKey() encodes I<a>. It uses a key specific format or, if none is
80defined for that key type, PKCS#8 unencrypted PrivateKeyInfo format.
81i2d_PKCS8PrivateKey() does the same using only the PKCS#8 unencrypted
82PrivateKeyInfo format.
83i2d_PublicKey() does the same for public keys.
84i2d_KeyParams() does the same for key parameters.
85These functions are similar to the d2i_X509() functions; see L<d2i_X509(3)>.
86i2d_PrivateKey_bio() and i2d_PrivateKey_fp() do the same thing except that they
87encode to a B<BIO> or B<FILE> respectively. Again, these work similarly to the
88functions described in L<d2i_X509(3)>.
89
90=head1 NOTES
91
92All the functions that operate on data in memory update the data pointer I<*pp>
93after a successful operation, just like the other d2i and i2d functions;
94see L<d2i_X509(3)>.
95
96All these functions use DER format and unencrypted keys. Applications wishing
97to encrypt or decrypt private keys should use other functions such as
98d2i_PKCS8PrivateKey() instead.
99
100To decode a key with type B<EVP_PKEY_EC>, d2i_PublicKey() requires I<*a> to be
101a non-NULL EVP_PKEY structure assigned an EC_KEY structure referencing the proper
102EC_GROUP.
103
104=head1 RETURN VALUES
105
106The d2i_PrivateKey_ex(), d2i_PrivateKey(), d2i_AutoPrivateKey_ex(),
107d2i_AutoPrivateKey(), d2i_PrivateKey_ex_bio(), d2i_PrivateKey_bio(),
108d2i_PrivateKey_ex_fp(), d2i_PrivateKey_fp(), d2i_PublicKey(), d2i_KeyParams()
109and d2i_KeyParams_bio() functions return a valid B<EVP_PKEY> structure or NULL if
110an error occurs. The error code can be obtained by calling L<ERR_get_error(3)>.
111
112i2d_PrivateKey(), i2d_PKCS8PrivateKey(), i2d_PublicKey() and i2d_KeyParams()
113return the number of bytes successfully encoded or a negative value if an error
114occurs. The error code can be obtained by calling L<ERR_get_error(3)>.
115
116i2d_PrivateKey_bio(), i2d_PrivateKey_fp() and i2d_KeyParams_bio() return 1 if
117successfully encoded or zero if an error occurs.
118
119=head1 SEE ALSO
120
121L<crypto(7)>,
122L<d2i_PKCS8PrivateKey_bio(3)>
123
124=head1 HISTORY
125
126d2i_PrivateKey_ex(), d2i_PrivateKey_ex_bio(), d2i_PrivateKey_ex_fp(), and
127d2i_AutoPrivateKey_ex() were added in OpenSSL 3.0.
128
129i2d_PKCS8PrivateKey() was added in OpenSSL 3.6.
130
131=head1 COPYRIGHT
132
133Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
134
135Licensed under the Apache License 2.0 (the "License").  You may not use
136this file except in compliance with the License.  You can obtain a copy
137in the file LICENSE in the source distribution or at
138L<https://www.openssl.org/source/license.html>.
139
140=cut
141