1.\" $OpenBSD: X509_get_pubkey.3,v 1.8 2020/06/19 14:31:29 schwarze Exp $ 2.\" selective merge up to: OpenSSL 99d63d46 Oct 26 13:56:48 2016 -0400 3.\" X509_REQ_get0_pubkey and X509_REQ_get_X509_PUBKEY not yet in LibreSSL 4.\" 5.\" This file is a derived work. 6.\" The changes are covered by the following Copyright and license: 7.\" 8.\" Copyright (c) 2020 Ingo Schwarze <schwarze@openbsd.org> 9.\" 10.\" Permission to use, copy, modify, and distribute this software for any 11.\" purpose with or without fee is hereby granted, provided that the above 12.\" copyright notice and this permission notice appear in all copies. 13.\" 14.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 15.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 16.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 17.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 18.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 19.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 20.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 21.\" 22.\" The original file was written by Dr. Stephen Henson <steve@openssl.org>. 23.\" Copyright (c) 2015 The OpenSSL Project. All rights reserved. 24.\" 25.\" Redistribution and use in source and binary forms, with or without 26.\" modification, are permitted provided that the following conditions 27.\" are met: 28.\" 29.\" 1. Redistributions of source code must retain the above copyright 30.\" notice, this list of conditions and the following disclaimer. 31.\" 32.\" 2. Redistributions in binary form must reproduce the above copyright 33.\" notice, this list of conditions and the following disclaimer in 34.\" the documentation and/or other materials provided with the 35.\" distribution. 36.\" 37.\" 3. All advertising materials mentioning features or use of this 38.\" software must display the following acknowledgment: 39.\" "This product includes software developed by the OpenSSL Project 40.\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 41.\" 42.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 43.\" endorse or promote products derived from this software without 44.\" prior written permission. For written permission, please contact 45.\" openssl-core@openssl.org. 46.\" 47.\" 5. Products derived from this software may not be called "OpenSSL" 48.\" nor may "OpenSSL" appear in their names without prior written 49.\" permission of the OpenSSL Project. 50.\" 51.\" 6. Redistributions of any form whatsoever must retain the following 52.\" acknowledgment: 53.\" "This product includes software developed by the OpenSSL Project 54.\" for use in the OpenSSL Toolkit (http://www.openssl.org/)" 55.\" 56.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 57.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 58.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 59.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 60.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 61.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 62.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 63.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 64.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 65.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 66.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 67.\" OF THE POSSIBILITY OF SUCH DAMAGE. 68.\" 69.Dd $Mdocdate: June 19 2020 $ 70.Dt X509_GET_PUBKEY 3 71.Os 72.Sh NAME 73.Nm X509_get_pubkey , 74.Nm X509_get0_pubkey , 75.Nm X509_set_pubkey , 76.Nm X509_get_X509_PUBKEY , 77.Nm X509_get0_pubkey_bitstr , 78.Nm X509_REQ_get_pubkey , 79.Nm X509_REQ_set_pubkey 80.Nd get or set certificate or certificate request public key 81.Sh SYNOPSIS 82.In openssl/x509.h 83.Ft EVP_PKEY * 84.Fo X509_get_pubkey 85.Fa "X509 *x" 86.Fc 87.Ft EVP_PKEY * 88.Fo X509_get0_pubkey 89.Fa "const X509 *x" 90.Fc 91.Ft int 92.Fo X509_set_pubkey 93.Fa "X509 *x" 94.Fa "EVP_PKEY *pkey" 95.Fc 96.Ft X509_PUBKEY * 97.Fo X509_get_X509_PUBKEY 98.Fa "X509 *x" 99.Fc 100.Ft ASN1_BIT_STRING * 101.Fo X509_get0_pubkey_bitstr 102.Fa "const X509 *x" 103.Fc 104.Ft EVP_PKEY * 105.Fo X509_REQ_get_pubkey 106.Fa "X509_REQ *req" 107.Fc 108.Ft int 109.Fo X509_REQ_set_pubkey 110.Fa "X509_REQ *x" 111.Fa "EVP_PKEY *pkey" 112.Fc 113.Sh DESCRIPTION 114.Fn X509_get_pubkey 115attempts to decode the public key for certificate 116.Fa x . 117If successful it returns the public key as an 118.Vt EVP_PKEY 119pointer with its reference count incremented: this means the returned 120key must be freed up after use. 121.Fn X509_get0_pubkey 122is similar except that it does not increment the reference count 123of the returned 124.Vt EVP_PKEY , 125so it must not be freed up after use. 126.Pp 127.Fn X509_get_X509_PUBKEY 128returns an internal pointer to the 129.Vt SubjectPublicKeyInfo 130structure contained in 131.Fa x . 132The returned value must not be freed up after use. 133.Fn X509_get_X509_PUBKEY 134is implemented as a macro. 135.Pp 136.Fn X509_get0_pubkey_bitstr 137returns an internal pointer to just the public key contained in this 138.Vt SubjectPublicKeyInfo 139structure, without the information about the algorithm used. 140.Pp 141.Fn X509_set_pubkey 142attempts to set the public key for certificate 143.Fa x 144to 145.Fa pkey . 146The key 147.Fa pkey 148should be freed up after use. 149.Pp 150.Fn X509_REQ_get_pubkey 151and 152.Fn X509_REQ_set_pubkey 153are similar but operate on certificate request 154.Fa req . 155.Pp 156The first time a public key is decoded, the 157.Vt EVP_PKEY 158structure is cached in the certificate or certificate request itself. 159Subsequent calls return the cached structure with its reference count 160incremented to improve performance. 161.Sh RETURN VALUES 162.Fn X509_get_pubkey , 163.Fn X509_get0_pubkey , 164.Fn X509_get_X509_PUBKEY , 165.Fn X509_get0_pubkey_bitstr , 166and 167.Fn X509_REQ_get_pubkey 168return a public key or 169.Dv NULL 170if an error occurred. 171.Pp 172.Fn X509_set_pubkey 173and 174.Fn X509_REQ_set_pubkey 175return 1 for success or 0 for failure. 176.Pp 177In some cases of failure of 178.Fn X509_get0_pubkey , 179.Fn X509_set_pubkey , 180.Fn X509_REQ_get_pubkey , 181and 182.Fn X509_REQ_set_pubkey , 183the reason can be determined with 184.Xr ERR_get_error 3 . 185.Sh ERRORS 186.Fn X509_get_pubkey , 187.Fn X509_get0_pubkey , 188and 189.Fn X509_REQ_get_pubkey 190provide diagnostics as documented for 191.Xr X509_PUBKEY_get 3 . 192If 193.Fa x 194or 195.Fa req 196is 197.Dv NULL 198or contains no certificate information, 199they fail without pushing an error onto the stack. 200.Pp 201.Fn X509_get_X509_PUBKEY 202provides no diagnostics and crashes by accessing a 203.Dv NULL 204pointer if 205.Fa x 206is 207.Dv NULL 208or contains no certificate information, 209.Pp 210.Fn X509_get0_pubkey_bitstr 211provides no diagnostics 212and fails without pushing an error onto the stack if 213.Fa x 214is 215.Dv NULL , 216but it crashes by accessing a 217.Dv NULL 218pointer if 219.Fa x 220contains no certificate information. 221.Sh SEE ALSO 222.Xr d2i_X509 3 , 223.Xr X509_CRL_get0_by_serial 3 , 224.Xr X509_NAME_add_entry_by_txt 3 , 225.Xr X509_NAME_ENTRY_get_object 3 , 226.Xr X509_NAME_get_index_by_NID 3 , 227.Xr X509_NAME_print_ex 3 , 228.Xr X509_new 3 , 229.Xr X509_PUBKEY_new 3 , 230.Xr X509_REQ_new 3 , 231.Xr X509_sign 3 , 232.Xr X509_verify_cert 3 , 233.Xr X509V3_get_d2i 3 234.Sh STANDARDS 235RFC 5280, Internet X.509 Public Key Infrastructure Certificate 236and Certificate Revocation List (CRL) Profile, 237section 4.1 Basic Certificate Fields 238.Pp 239RFC 2986: PKCS #10: Certification Request Syntax Specification, 240section 4.1 CertificationRequestInfo 241.Sh HISTORY 242.Fn X509_get_pubkey , 243.Fn X509_set_pubkey , 244.Fn X509_REQ_get_pubkey , 245and 246.Fn X509_REQ_set_pubkey 247first appeared in SSLeay 0.6.5. 248.Fn X509_get_X509_PUBKEY 249first appeared in SSLeay 0.8.0. 250These functions have been available since 251.Ox 2.4 . 252.Pp 253.Fn X509_get0_pubkey_bitstr 254first appeared in OpenSSL 0.9.7 and has been available since 255.Ox 3.4 . 256.Pp 257.Fn X509_get0_pubkey 258first appeared in OpenSSL 1.1.0 and has been available since 259.Ox 6.3 . 260