1.\" $OpenBSD: X509_sign.3,v 1.9 2021/10/30 16:20:35 schwarze Exp $ 2.\" full merge up to: OpenSSL df75c2bf Dec 9 01:02:36 2018 +0100 3.\" 4.\" This file was written by Dr. Stephen Henson <steve@openssl.org>. 5.\" Copyright (c) 2015, 2016 The OpenSSL Project. All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in 16.\" the documentation and/or other materials provided with the 17.\" distribution. 18.\" 19.\" 3. All advertising materials mentioning features or use of this 20.\" software must display the following acknowledgment: 21.\" "This product includes software developed by the OpenSSL Project 22.\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 23.\" 24.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 25.\" endorse or promote products derived from this software without 26.\" prior written permission. For written permission, please contact 27.\" openssl-core@openssl.org. 28.\" 29.\" 5. Products derived from this software may not be called "OpenSSL" 30.\" nor may "OpenSSL" appear in their names without prior written 31.\" permission of the OpenSSL Project. 32.\" 33.\" 6. Redistributions of any form whatsoever must retain the following 34.\" acknowledgment: 35.\" "This product includes software developed by the OpenSSL Project 36.\" for use in the OpenSSL Toolkit (http://www.openssl.org/)" 37.\" 38.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 39.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 40.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 41.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 42.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 43.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 44.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 45.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 46.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 47.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 48.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 49.\" OF THE POSSIBILITY OF SUCH DAMAGE. 50.\" 51.Dd $Mdocdate: October 30 2021 $ 52.Dt X509_SIGN 3 53.Os 54.Sh NAME 55.Nm X509_sign , 56.Nm X509_sign_ctx , 57.Nm X509_verify , 58.Nm X509_REQ_sign , 59.Nm X509_REQ_sign_ctx , 60.Nm X509_REQ_verify , 61.Nm X509_CRL_sign , 62.Nm X509_CRL_sign_ctx , 63.Nm X509_CRL_verify 64.Nd sign or verify certificate, certificate request, or CRL signature 65.Sh SYNOPSIS 66.In openssl/x509.h 67.Ft int 68.Fo X509_sign 69.Fa "X509 *x" 70.Fa "EVP_PKEY *pkey" 71.Fa "const EVP_MD *md" 72.Fc 73.Ft int 74.Fo X509_sign_ctx 75.Fa "X509 *x" 76.Fa "EVP_MD_CTX *ctx" 77.Fc 78.Ft int 79.Fo X509_verify 80.Fa "X509 *a" 81.Fa "EVP_PKEY *r" 82.Fc 83.Ft int 84.Fo X509_REQ_sign 85.Fa "X509_REQ *x" 86.Fa "EVP_PKEY *pkey" 87.Fa "const EVP_MD *md" 88.Fc 89.Ft int 90.Fo X509_REQ_sign_ctx 91.Fa "X509_REQ *x" 92.Fa "EVP_MD_CTX *ctx" 93.Fc 94.Ft int 95.Fo X509_REQ_verify 96.Fa "X509_REQ *a" 97.Fa "EVP_PKEY *r" 98.Fc 99.Ft int 100.Fo X509_CRL_sign 101.Fa "X509_CRL *x" 102.Fa "EVP_PKEY *pkey" 103.Fa "const EVP_MD *md" 104.Fc 105.Ft int 106.Fo X509_CRL_sign_ctx 107.Fa "X509_CRL *x" 108.Fa "EVP_MD_CTX *ctx" 109.Fc 110.Ft int 111.Fo X509_CRL_verify 112.Fa "X509_CRL *a" 113.Fa "EVP_PKEY *r" 114.Fc 115.Sh DESCRIPTION 116.Fn X509_sign 117signs the certificate 118.Fa x 119using the private key 120.Fa pkey 121and the message digest 122.Fa md 123and sets the signature in 124.Fa x . 125.Fn X509_sign_ctx 126also signs the certificate 127.Fa x 128but uses the parameters contained in digest context 129.Fa ctx . 130.Pp 131.Fn X509_verify 132verifies the signature of certificate 133.Fa x 134using the public key 135.Fa pkey . 136Only the signature is checked: no other checks (such as certificate 137chain validity) are performed. 138.Pp 139.Fn X509_REQ_sign , 140.Fn X509_REQ_sign_ctx , 141.Fn X509_REQ_verify , 142.Fn X509_CRL_sign , 143.Fn X509_CRL_sign_ctx , 144and 145.Fn X509_CRL_verify 146sign and verify certificate requests and CRLs, respectively. 147.Pp 148If 149.Xr X509_CRL_set_default_method 3 150was in effect at the time the 151.Vt X509_CRL 152object was created, 153.Fn X509_CRL_verify 154calls the 155.Fn crl_verify 156callback function instead of performing the default action. 157.Pp 158.Fn X509_sign_ctx 159is used where the default parameters for the corresponding public key 160and digest are not suitable. 161It can be used to sign keys using RSA-PSS for example. 162.Pp 163For efficiency reasons and to work around ASN.1 encoding issues, the 164encoding of the signed portion of a certificate, certificate request, 165and CRL is cached internally. 166If the signed portion of the structure is modified, the encoding is not 167always updated, meaning a stale version is sometimes used. 168This is not normally a problem because modifying the signed portion will 169invalidate the signature and signing will always update the encoding. 170.Sh RETURN VALUES 171.Fn X509_sign , 172.Fn X509_sign_ctx , 173.Fn X509_REQ_sign , 174.Fn X509_REQ_sign_ctx , 175.Fn X509_CRL_sign , 176and 177.Fn X509_CRL_sign_ctx 178return the size of the signature in bytes for success or 0 for failure. 179.Pp 180.Fn X509_verify , 181.Fn X509_REQ_verify , 182and 183.Fn X509_CRL_verify 184return 1 if the signature is valid or 0 if the signature check fails. 185If the signature could not be checked at all because it was invalid or 186some other error occurred, then -1 is returned. 187.Pp 188In some cases of failure, the reason can be determined with 189.Xr ERR_get_error 3 . 190.Sh SEE ALSO 191.Xr d2i_X509 3 , 192.Xr EVP_DigestInit 3 , 193.Xr X509_CRL_get0_by_serial 3 , 194.Xr X509_CRL_METHOD_new 3 , 195.Xr X509_CRL_new 3 , 196.Xr X509_get_pubkey 3 , 197.Xr X509_get_subject_name 3 , 198.Xr X509_get_version 3 , 199.Xr X509_NAME_add_entry_by_txt 3 , 200.Xr X509_NAME_ENTRY_get_object 3 , 201.Xr X509_NAME_get_index_by_NID 3 , 202.Xr X509_NAME_print_ex 3 , 203.Xr X509_new 3 , 204.Xr X509_REQ_new 3 , 205.Xr X509_verify_cert 3 , 206.Xr X509V3_get_d2i 3 207.Sh HISTORY 208.Fn X509_verify 209appeared in SSLeay 0.4 or earlier. 210.Fn X509_sign 211and 212.Fn X509_REQ_sign 213first appeared in SSLeay 0.4.4. 214.Fn X509_REQ_verify 215and 216.Fn X509_CRL_verify 217first appeared in SSLeay 0.4.5b. 218.Fn X509_CRL_sign 219first appeared in SSLeay 0.5.1. 220These functions have been available since 221.Ox 2.4 . 222.Pp 223.Fn X509_sign_ctx , 224.Fn X509_REQ_sign_ctx , 225and 226.Fn X509_CRL_sign_ctx 227first appeared in OpenSSL 1.0.1 and have been available since 228.Ox 5.3 . 229