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