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
10 #include <openssl/evp.h>
12 int EVP_PKEY_set1_encoded_public_key(EVP_PKEY *pkey,
13 const unsigned char *pub, size_t publen);
15 size_t EVP_PKEY_get1_encoded_public_key(EVP_PKEY *pkey, unsigned char **ppub);
17 #define EVP_PKEY_set1_tls_encodedpoint(pkey, pt, ptlen) \
18 EVP_PKEY_set1_encoded_public_key((pkey), (pt), (ptlen))
20 #define EVP_PKEY_get1_tls_encodedpoint(pkey, ppt) \
21 EVP_PKEY_get1_encoded_public_key((pkey), (ppt))
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)>.
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.
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>.
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
51 EVP_PKEY_set1_encoded_public_key() returns 1 for success and 0 or a negative
54 EVP_PKEY_get1_encoded_public_key() return 1
58 See L<EVP_PKEY_derive_init(3)> and L<EVP_PKEY_derive(3)> for information about
59 performing a key exchange operation.
61 =head2 Set up a peer's EVP_PKEY ready for a key exchange operation
63 #include <openssl/evp.h>
65 int exchange(EVP_PKEY *ourkey, unsigned char *peer_pub, size_t peer_pub_len)
67 EVP_PKEY *peerkey = EVP_PKEY_new();
69 if (peerkey == NULL || EVP_PKEY_copy_parameters(peerkey, ourkey) <= 0)
72 if (EVP_PKEY_set1_encoded_public_key(peerkey, peer_pub,
76 /* Do the key exchange here */
78 EVP_PKEY_free(peerkey);
83 =head2 Get an encoded public key to send to a peer
85 #include <openssl/evp.h>
87 int get_encoded_pub_key(EVP_PKEY *ourkey)
89 unsigned char *pubkey;
92 pubkey_len = EVP_PKEY_get1_encoded_public_key(ourkey, &pubkey);
97 * Send the encoded public key stored in the buffer at "pubkey" and of
98 * length pubkey_len, to the peer.
101 OPENSSL_free(pubkey);
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)>
113 These functions were added in OpenSSL 3.0.
117 Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
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>.