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