1=pod
2
3=head1 NAME
4
5EVP_PKEY_set1_encoded_public_key, EVP_PKEY_get1_encoded_public_key,
6EVP_PKEY_set1_tls_encodedpoint, EVP_PKEY_get1_tls_encodedpoint
7- functions to set and get public key data within an EVP_PKEY
8
9=head1 SYNOPSIS
10
11 #include <openssl/evp.h>
12
13 int EVP_PKEY_set1_encoded_public_key(EVP_PKEY *pkey,
14                                      const unsigned char *pub, size_t publen);
15
16 size_t EVP_PKEY_get1_encoded_public_key(EVP_PKEY *pkey, unsigned char **ppub);
17
18The following functions have been deprecated since OpenSSL 3.0, and can be
19hidden entirely by defining B<OPENSSL_API_COMPAT> with a suitable version value,
20see L<openssl_user_macros(7)>:
21
22 int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
23                                    const unsigned char *pt, size_t ptlen);
24
25 size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt);
26
27=head1 DESCRIPTION
28
29EVP_PKEY_set1_encoded_public_key() can be used to set the public key value
30within an existing EVP_PKEY object. For the built-in OpenSSL algorithms this
31currently only works for those that support key exchange. Parameters are not
32set as part of this operation, so typically an application will create an
33EVP_PKEY first, set the parameters on it, and then call this function.
34For example setting the parameters might be done using
35L<EVP_PKEY_copy_parameters(3)>.
36
37The format for the encoded public key will depend on the algorithm in use. For
38DH it should be encoded as a positive integer in big-endian form. For EC is
39should be a point conforming to Sec. 2.3.4 of the SECG SEC 1 ("Elliptic
40Curve Cryptography") standard. For X25519 and X448 it should be encoded in a
41format as defined by RFC7748.
42
43The key to be updated is supplied in B<pkey>. The buffer containing the encoded
44key is pointed to be B<pub>. The length of the buffer is supplied in B<publen>.
45
46EVP_PKEY_get1_encoded_public_key() does the equivalent operation except that
47the encoded public key is returned to the application. The key containing the
48public key data is supplied in B<pkey>. A buffer containing the encoded key will
49be allocated and stored in B<*ppub>. The length of the encoded public key is
50returned by the function. The application is responsible for freeing the
51allocated buffer.
52
53The macro EVP_PKEY_set1_tls_encodedpoint() is deprecated and simply calls
54EVP_PKEY_set1_encoded_public_key() with all the same arguments. New applications
55should use EVP_PKEY_set1_encoded_public_key() instead.
56
57The macro EVP_PKEY_get1_tls_encodedpoint() is deprecated and simply calls
58EVP_PKEY_get1_encoded_public_key() with all the same arguments. New applications
59should use EVP_PKEY_get1_encoded_public_key() instead.
60
61
62=head1 RETURN VALUES
63
64EVP_PKEY_set1_encoded_public_key() returns 1 for success and 0 or a negative
65value for failure.
66
67EVP_PKEY_get1_encoded_public_key() returns the length of the encoded key or 0 for failure.
68
69=head1 EXAMPLES
70
71See L<EVP_PKEY_derive_init(3)> and L<EVP_PKEY_derive(3)> for information about
72performing a key exchange operation.
73
74=head2 Set up a peer's EVP_PKEY ready for a key exchange operation
75
76 #include <openssl/evp.h>
77
78 int exchange(EVP_PKEY *ourkey, unsigned char *peer_pub, size_t peer_pub_len)
79 {
80     EVP_PKEY *peerkey = EVP_PKEY_new();
81
82     if (peerkey == NULL || EVP_PKEY_copy_parameters(peerkey, ourkey) <= 0)
83         return 0;
84
85     if (EVP_PKEY_set1_encoded_public_key(peerkey, peer_pub,
86                                          peer_pub_len) <= 0)
87         return 0;
88
89     /* Do the key exchange here */
90
91     EVP_PKEY_free(peerkey);
92
93     return 1;
94 }
95
96=head2 Get an encoded public key to send to a peer
97
98 #include <openssl/evp.h>
99
100 int get_encoded_pub_key(EVP_PKEY *ourkey)
101 {
102     unsigned char *pubkey;
103     size_t pubkey_len;
104
105    pubkey_len = EVP_PKEY_get1_encoded_public_key(ourkey, &pubkey);
106    if (pubkey_len == 0)
107        return 0;
108
109    /*
110     * Send the encoded public key stored in the buffer at "pubkey" and of
111     * length pubkey_len, to the peer.
112     */
113
114    OPENSSL_free(pubkey);
115    return 1;
116 }
117
118=head1 SEE ALSO
119
120L<EVP_PKEY_new(3)>, L<EVP_PKEY_copy_parameters(3)>,
121L<EVP_PKEY_derive_init(3)>, L<EVP_PKEY_derive(3)>,
122L<EVP_PKEY-DH(7)>, L<EVP_PKEY-EC(7)>, L<EVP_PKEY-X25519(7)>, L<EVP_PKEY-X448(7)>
123
124=head1 HISTORY
125
126EVP_PKEY_set1_encoded_public_key() and EVP_PKEY_get1_encoded_public_key() were
127added in OpenSSL 3.0.
128
129EVP_PKEY_set1_tls_encodedpoint() and EVP_PKEY_get1_tls_encodedpoint() were
130deprecated in OpenSSL 3.0.
131
132=head1 COPYRIGHT
133
134Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
135
136Licensed under the Apache License 2.0 (the "License").  You may not use
137this file except in compliance with the License.  You can obtain a copy
138in the file LICENSE in the source distribution or at
139L<https://www.openssl.org/source/license.html>.
140
141=cut
142
143