Add X509_NAME_hash_ex() to be able to check if it failed due to unsupported SHA1
[openssl.git] / doc / man3 / EVP_PKEY_new.pod
1 =pod
2
3 =head1 NAME
4
5 EVP_PKEY,
6 EVP_PKEY_new,
7 EVP_PKEY_up_ref,
8 EVP_PKEY_free,
9 EVP_PKEY_new_raw_private_key_ex,
10 EVP_PKEY_new_raw_private_key,
11 EVP_PKEY_new_raw_public_key_ex,
12 EVP_PKEY_new_raw_public_key,
13 EVP_PKEY_new_CMAC_key_ex,
14 EVP_PKEY_new_CMAC_key,
15 EVP_PKEY_new_mac_key,
16 EVP_PKEY_get_raw_private_key,
17 EVP_PKEY_get_raw_public_key
18 - public/private key allocation and raw key handling functions
19
20 =head1 SYNOPSIS
21
22  #include <openssl/evp.h>
23
24  typedef evp_pkey_st EVP_PKEY;
25
26  EVP_PKEY *EVP_PKEY_new(void);
27  int EVP_PKEY_up_ref(EVP_PKEY *key);
28  void EVP_PKEY_free(EVP_PKEY *key);
29
30  EVP_PKEY *EVP_PKEY_new_raw_private_key_ex(OSSL_LIB_CTX *libctx,
31                                            const char *keytype,
32                                            const char *propq,
33                                            const unsigned char *key,
34                                            size_t keylen);
35  EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
36                                         const unsigned char *key, size_t keylen);
37  EVP_PKEY *EVP_PKEY_new_raw_public_key_ex(OSSL_LIB_CTX *libctx,
38                                           const char *keytype,
39                                           const char *propq,
40                                           const unsigned char *key,
41                                           size_t keylen);
42  EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
43                                        const unsigned char *key, size_t keylen);
44  EVP_PKEY *EVP_PKEY_new_CMAC_key_ex(const unsigned char *priv, size_t len,
45                                     const char *cipher_name,
46                                     OSSL_LIB_CTX *libctx, const char *propq);
47  EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
48                                  size_t len, const EVP_CIPHER *cipher);
49  EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e, const unsigned char *key,
50                                 int keylen);
51
52  int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
53                                   size_t *len);
54  int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
55                                  size_t *len);
56
57 =head1 DESCRIPTION
58
59 B<EVP_PKEY> is a generic structure to hold diverse types of asymmetric keys
60 (also known as "key pairs"), and can be used for diverse operations, like
61 signing, verifying signatures, key derivation, etc.  The asymmetric keys
62 themselves are often refered to as the "internal key", and are handled by
63 backends, such as providers (through L<EVP_KEYMGMT(3)>) or B<ENGINE>s.
64
65 Conceptually, an B<EVP_PKEY> internal key may hold a private key, a public
66 key, or both (a keypair), and along with those, key parameters if the key type
67 requires them.  The presence of these components determine what operations can
68 be made; for example, signing normally requires the presence of a private key,
69 and verifying normally requires the presence of a public key.
70
71 =for comment ED signature require both the private and public key...
72
73 B<EVP_PKEY> has also been used for MAC algorithm that were conceived as
74 producing signatures, although not being public key algorithms; "POLY1305",
75 "SIPHASH", "HMAC", "CMAC".  This usage is considered legacy and is discouraged
76 in favor of the L<EVP_MAC(3)> API.
77
78 The EVP_PKEY_new() function allocates an empty B<EVP_PKEY> structure which is
79 used by OpenSSL to store public and private keys. The reference count is set to
80 B<1>.
81
82 EVP_PKEY_up_ref() increments the reference count of I<key>.
83
84 EVP_PKEY_free() decrements the reference count of I<key> and, if the reference
85 count is zero, frees it up. If I<key> is NULL, nothing is done.
86
87 EVP_PKEY_new_raw_private_key_ex() allocates a new B<EVP_PKEY>. Unless an
88 engine should be used for the key type, a provider for the key is found using
89 the library context I<libctx> and the property query string I<propq>. The
90 I<keytype> argument indicates what kind of key this is. The value should be a
91 string for a public key algorithm that supports raw private keys, i.e one of
92 "X25519", "ED25519", "X448" or "ED448". I<key> points to the raw private key
93 data for this B<EVP_PKEY> which should be of length I<keylen>. The length
94 should be appropriate for the type of the key. The public key data will be
95 automatically derived from the given private key data (if appropriate for the
96 algorithm type).
97
98 EVP_PKEY_new_raw_private_key() does the same as
99 EVP_PKEY_new_raw_private_key_ex() except that the default library context and
100 default property query are used instead. If I<e> is non-NULL then the new
101 B<EVP_PKEY> structure is associated with the engine I<e>. The I<type> argument
102 indicates what kind of key this is. The value should be a NID for a public key
103 algorithm that supports raw private keys, i.e. one of B<EVP_PKEY_X25519>,
104 B<EVP_PKEY_ED25519>, B<EVP_PKEY_X448> or B<EVP_PKEY_ED448>.
105
106 EVP_PKEY_new_raw_private_key_ex() and EVP_PKEY_new_raw_private_key() may also
107 be used with most MACs implemented as public key algorithms, so key types such
108 as "HMAC", "POLY1305", "SIPHASH", or their NID form B<EVP_PKEY_POLY1305>,
109 B<EVP_PKEY_SIPHASH>, B<EVP_PKEY_HMAC> are also accepted.  This usage is,
110 as mentioned above, discouraged in favor of the L<EVP_MAC(3)> API.
111
112 EVP_PKEY_new_raw_public_key_ex() works in the same way as
113 EVP_PKEY_new_raw_private_key_ex() except that I<key> points to the raw
114 public key data. The B<EVP_PKEY> structure will be initialised without any
115 private key information. Algorithm types that support raw public keys are
116 "X25519", "ED25519", "X448" or "ED448".
117
118 EVP_PKEY_new_raw_public_key() works in the same way as
119 EVP_PKEY_new_raw_private_key() except that I<key> points to the raw public key
120 data. The B<EVP_PKEY> structure will be initialised without any private key
121 information. Algorithm types that support raw public keys are
122 B<EVP_PKEY_X25519>, B<EVP_PKEY_ED25519>, B<EVP_PKEY_X448> or B<EVP_PKEY_ED448>.
123
124 EVP_PKEY_new_CMAC_key_ex() works in the same way as
125 EVP_PKEY_new_raw_private_key() except it is only for the B<EVP_PKEY_CMAC>
126 algorithm type. In addition to the raw private key data, it also takes a cipher
127 algorithm to be used during creation of a CMAC in the I<cipher> argument. The
128 cipher should be a standard encryption only cipher. For example AEAD and XTS
129 ciphers should not be used. Finally it also takes a library context I<libctx>
130 and property query I<propq> which are used when fetching any cryptographic
131 algorithms which may be NULL to use the default values.
132
133 EVP_PKEY_new_CMAC_key() is the same as EVP_PKEY_new_CMAC_key_ex()
134 except that the default values are used for I<libctx> and I<propq>.
135
136 Using EVP_PKEY_new_CMAC_key_ex() or EVP_PKEY_new_CMAC_key() is discouraged in
137 favor of the L<EVP_MAC(3)> API.
138
139 EVP_PKEY_new_mac_key() works in the same way as EVP_PKEY_new_raw_private_key().
140 New applications should use EVP_PKEY_new_raw_private_key() instead.
141
142 EVP_PKEY_get_raw_private_key() fills the buffer provided by I<priv> with raw
143 private key data. The size of the I<priv> buffer should be in I<*len> on entry
144 to the function, and on exit I<*len> is updated with the number of bytes
145 actually written. If the buffer I<priv> is NULL then I<*len> is populated with
146 the number of bytes required to hold the key. The calling application is
147 responsible for ensuring that the buffer is large enough to receive the private
148 key data. This function only works for algorithms that support raw private keys.
149 Currently this is: B<EVP_PKEY_HMAC>, B<EVP_PKEY_POLY1305>, B<EVP_PKEY_SIPHASH>,
150 B<EVP_PKEY_X25519>, B<EVP_PKEY_ED25519>, B<EVP_PKEY_X448> or B<EVP_PKEY_ED448>.
151
152 EVP_PKEY_get_raw_public_key() fills the buffer provided by I<pub> with raw
153 public key data. The size of the I<pub> buffer should be in I<*len> on entry
154 to the function, and on exit I<*len> is updated with the number of bytes
155 actually written. If the buffer I<pub> is NULL then I<*len> is populated with
156 the number of bytes required to hold the key. The calling application is
157 responsible for ensuring that the buffer is large enough to receive the public
158 key data. This function only works for algorithms that support raw public  keys.
159 Currently this is: B<EVP_PKEY_X25519>, B<EVP_PKEY_ED25519>, B<EVP_PKEY_X448> or
160 B<EVP_PKEY_ED448>.
161
162 =head1 NOTES
163
164 The B<EVP_PKEY> structure is used by various OpenSSL functions which require a
165 general private key without reference to any particular algorithm.
166
167 The structure returned by EVP_PKEY_new() is empty. To add a private or public
168 key to this empty structure use the appropriate functions described in
169 L<EVP_PKEY_set1_RSA(3)>, L<EVP_PKEY_set1_DSA(3)>, L<EVP_PKEY_set1_DH(3)> or
170 L<EVP_PKEY_set1_EC_KEY(3)>.
171
172 =head1 RETURN VALUES
173
174 EVP_PKEY_new(), EVP_PKEY_new_raw_private_key(), EVP_PKEY_new_raw_public_key(),
175 EVP_PKEY_new_CMAC_key() and EVP_PKEY_new_mac_key() return either the newly
176 allocated B<EVP_PKEY> structure or B<NULL> if an error occurred.
177
178 EVP_PKEY_up_ref(), EVP_PKEY_get_raw_private_key() and
179 EVP_PKEY_get_raw_public_key() return 1 for success and 0 for failure.
180
181 =head1 SEE ALSO
182
183 L<EVP_PKEY_set1_RSA(3)>, L<EVP_PKEY_set1_DSA(3)>, L<EVP_PKEY_set1_DH(3)> or
184 L<EVP_PKEY_set1_EC_KEY(3)>
185
186 =head1 HISTORY
187
188 The
189 EVP_PKEY_new() and EVP_PKEY_free() functions exist in all versions of OpenSSL.
190
191 The EVP_PKEY_up_ref() function was added in OpenSSL 1.1.0.
192
193 The
194 EVP_PKEY_new_raw_private_key(), EVP_PKEY_new_raw_public_key(),
195 EVP_PKEY_new_CMAC_key(), EVP_PKEY_new_raw_private_key() and
196 EVP_PKEY_get_raw_public_key() functions were added in OpenSSL 1.1.1.
197
198 The EVP_PKEY_new_raw_private_key_ex(),
199 EVP_PKEY_new_raw_public_key_ex() and
200 EVP_PKEY_new_CMAC_key_ex() functions were added in OpenSSL 3.0.
201
202 The documentation of B<EVP_PKEY> was amended in OpenSSL 3.0 to allow there to
203 be the private part of the keypair without the public part, where this was
204 previously implied to be disallowed.
205
206 =head1 COPYRIGHT
207
208 Copyright 2002-2020 The OpenSSL Project Authors. All Rights Reserved.
209
210 Licensed under the Apache License 2.0 (the "License").  You may not use
211 this file except in compliance with the License.  You can obtain a copy
212 in the file LICENSE in the source distribution or at
213 L<https://www.openssl.org/source/license.html>.
214
215 =cut