Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] */ |
| 56 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 57 | #include <openssl/x509.h> |
| 58 | |
| 59 | #include <limits.h> |
| 60 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 61 | #include <openssl/asn1.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 62 | #include <openssl/digest.h> |
| 63 | #include <openssl/dsa.h> |
| 64 | #include <openssl/evp.h> |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 65 | #include <openssl/mem.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 66 | #include <openssl/rsa.h> |
| 67 | #include <openssl/stack.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 68 | |
David Benjamin | aaecb82 | 2021-05-14 12:15:18 -0400 | [diff] [blame] | 69 | #include "internal.h" |
| 70 | |
| 71 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 72 | int X509_verify(X509 *x509, EVP_PKEY *pkey) { |
| 73 | if (X509_ALGOR_cmp(x509->sig_alg, x509->cert_info->signature)) { |
| 74 | OPENSSL_PUT_ERROR(X509, X509_R_SIGNATURE_ALGORITHM_MISMATCH); |
| 75 | return 0; |
| 76 | } |
| 77 | return ASN1_item_verify(ASN1_ITEM_rptr(X509_CINF), x509->sig_alg, |
| 78 | x509->signature, x509->cert_info, pkey); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 79 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 80 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 81 | int X509_REQ_verify(X509_REQ *req, EVP_PKEY *pkey) { |
| 82 | return ASN1_item_verify(ASN1_ITEM_rptr(X509_REQ_INFO), req->sig_alg, |
| 83 | req->signature, req->req_info, pkey); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 84 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 85 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 86 | int X509_sign(X509 *x, EVP_PKEY *pkey, const EVP_MD *md) { |
| 87 | x->cert_info->enc.modified = 1; |
| 88 | return (ASN1_item_sign(ASN1_ITEM_rptr(X509_CINF), x->cert_info->signature, |
| 89 | x->sig_alg, x->signature, x->cert_info, pkey, md)); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 90 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 91 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 92 | int X509_sign_ctx(X509 *x, EVP_MD_CTX *ctx) { |
| 93 | x->cert_info->enc.modified = 1; |
| 94 | return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_CINF), x->cert_info->signature, |
| 95 | x->sig_alg, x->signature, x->cert_info, ctx); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 96 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 97 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 98 | int X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md) { |
| 99 | return (ASN1_item_sign(ASN1_ITEM_rptr(X509_REQ_INFO), x->sig_alg, NULL, |
| 100 | x->signature, x->req_info, pkey, md)); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 101 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 102 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 103 | int X509_REQ_sign_ctx(X509_REQ *x, EVP_MD_CTX *ctx) { |
| 104 | return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_REQ_INFO), x->sig_alg, NULL, |
| 105 | x->signature, x->req_info, ctx); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 106 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 107 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 108 | int X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md) { |
| 109 | x->crl->enc.modified = 1; |
| 110 | return (ASN1_item_sign(ASN1_ITEM_rptr(X509_CRL_INFO), x->crl->sig_alg, |
| 111 | x->sig_alg, x->signature, x->crl, pkey, md)); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 112 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 113 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 114 | int X509_CRL_sign_ctx(X509_CRL *x, EVP_MD_CTX *ctx) { |
| 115 | x->crl->enc.modified = 1; |
| 116 | return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_CRL_INFO), x->crl->sig_alg, |
| 117 | x->sig_alg, x->signature, x->crl, ctx); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 118 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 119 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 120 | int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *x, EVP_PKEY *pkey, const EVP_MD *md) { |
| 121 | return (ASN1_item_sign(ASN1_ITEM_rptr(NETSCAPE_SPKAC), x->sig_algor, NULL, |
| 122 | x->signature, x->spkac, pkey, md)); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 123 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 124 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 125 | int NETSCAPE_SPKI_verify(NETSCAPE_SPKI *spki, EVP_PKEY *pkey) { |
| 126 | return (ASN1_item_verify(ASN1_ITEM_rptr(NETSCAPE_SPKAC), spki->sig_algor, |
| 127 | spki->signature, spki->spkac, pkey)); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 128 | } |
Matt Braithwaite | 7e8f2ba | 2015-06-17 15:55:25 -0700 | [diff] [blame] | 129 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 130 | X509 *d2i_X509_fp(FILE *fp, X509 **x509) { |
| 131 | return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509), fp, x509); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 132 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 133 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 134 | int i2d_X509_fp(FILE *fp, X509 *x509) { |
| 135 | return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509), fp, x509); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 136 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 137 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 138 | X509 *d2i_X509_bio(BIO *bp, X509 **x509) { |
| 139 | return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509), bp, x509); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 140 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 141 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 142 | int i2d_X509_bio(BIO *bp, X509 *x509) { |
| 143 | return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509), bp, x509); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 144 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 145 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 146 | X509_CRL *d2i_X509_CRL_fp(FILE *fp, X509_CRL **crl) { |
| 147 | return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_CRL), fp, crl); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 148 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 149 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 150 | int i2d_X509_CRL_fp(FILE *fp, X509_CRL *crl) { |
| 151 | return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_CRL), fp, crl); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 152 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 153 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 154 | X509_CRL *d2i_X509_CRL_bio(BIO *bp, X509_CRL **crl) { |
| 155 | return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509_CRL), bp, crl); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 156 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 157 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 158 | int i2d_X509_CRL_bio(BIO *bp, X509_CRL *crl) { |
| 159 | return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_CRL), bp, crl); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 160 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 161 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 162 | X509_REQ *d2i_X509_REQ_fp(FILE *fp, X509_REQ **req) { |
| 163 | return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_REQ), fp, req); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 164 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 165 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 166 | int i2d_X509_REQ_fp(FILE *fp, X509_REQ *req) { |
| 167 | return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_REQ), fp, req); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 168 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 169 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 170 | X509_REQ *d2i_X509_REQ_bio(BIO *bp, X509_REQ **req) { |
| 171 | return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509_REQ), bp, req); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 172 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 173 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 174 | int i2d_X509_REQ_bio(BIO *bp, X509_REQ *req) { |
| 175 | return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_REQ), bp, req); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 176 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 177 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 178 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 179 | #define IMPLEMENT_D2I_FP(type, name, bio_func) \ |
| 180 | type *name(FILE *fp, type **obj) { \ |
| 181 | BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE); \ |
| 182 | if (bio == NULL) { \ |
| 183 | return NULL; \ |
| 184 | } \ |
| 185 | type *ret = bio_func(bio, obj); \ |
| 186 | BIO_free(bio); \ |
| 187 | return ret; \ |
| 188 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 189 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 190 | #define IMPLEMENT_I2D_FP(type, name, bio_func) \ |
| 191 | int name(FILE *fp, type *obj) { \ |
| 192 | BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE); \ |
| 193 | if (bio == NULL) { \ |
| 194 | return 0; \ |
| 195 | } \ |
| 196 | int ret = bio_func(bio, obj); \ |
| 197 | BIO_free(bio); \ |
| 198 | return ret; \ |
| 199 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 200 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 201 | IMPLEMENT_D2I_FP(RSA, d2i_RSAPrivateKey_fp, d2i_RSAPrivateKey_bio) |
| 202 | IMPLEMENT_I2D_FP(RSA, i2d_RSAPrivateKey_fp, i2d_RSAPrivateKey_bio) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 203 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 204 | IMPLEMENT_D2I_FP(RSA, d2i_RSAPublicKey_fp, d2i_RSAPublicKey_bio) |
| 205 | IMPLEMENT_I2D_FP(RSA, i2d_RSAPublicKey_fp, i2d_RSAPublicKey_bio) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 206 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 207 | IMPLEMENT_D2I_FP(RSA, d2i_RSA_PUBKEY_fp, d2i_RSA_PUBKEY_bio) |
| 208 | IMPLEMENT_I2D_FP(RSA, i2d_RSA_PUBKEY_fp, i2d_RSA_PUBKEY_bio) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 209 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 210 | #define IMPLEMENT_D2I_BIO(type, name, d2i_func) \ |
| 211 | type *name(BIO *bio, type **obj) { \ |
| 212 | uint8_t *data; \ |
| 213 | size_t len; \ |
| 214 | if (!BIO_read_asn1(bio, &data, &len, 100 * 1024)) { \ |
| 215 | return NULL; \ |
| 216 | } \ |
| 217 | const uint8_t *ptr = data; \ |
| 218 | type *ret = d2i_func(obj, &ptr, (long)len); \ |
| 219 | OPENSSL_free(data); \ |
| 220 | return ret; \ |
| 221 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 222 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 223 | #define IMPLEMENT_I2D_BIO(type, name, i2d_func) \ |
| 224 | int name(BIO *bio, type *obj) { \ |
| 225 | uint8_t *data = NULL; \ |
| 226 | int len = i2d_func(obj, &data); \ |
| 227 | if (len < 0) { \ |
| 228 | return 0; \ |
| 229 | } \ |
| 230 | int ret = BIO_write_all(bio, data, len); \ |
| 231 | OPENSSL_free(data); \ |
| 232 | return ret; \ |
| 233 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 234 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 235 | IMPLEMENT_D2I_BIO(RSA, d2i_RSAPrivateKey_bio, d2i_RSAPrivateKey) |
| 236 | IMPLEMENT_I2D_BIO(RSA, i2d_RSAPrivateKey_bio, i2d_RSAPrivateKey) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 237 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 238 | IMPLEMENT_D2I_BIO(RSA, d2i_RSAPublicKey_bio, d2i_RSAPublicKey) |
| 239 | IMPLEMENT_I2D_BIO(RSA, i2d_RSAPublicKey_bio, i2d_RSAPublicKey) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 240 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 241 | IMPLEMENT_D2I_BIO(RSA, d2i_RSA_PUBKEY_bio, d2i_RSA_PUBKEY) |
| 242 | IMPLEMENT_I2D_BIO(RSA, i2d_RSA_PUBKEY_bio, i2d_RSA_PUBKEY) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 243 | |
| 244 | #ifndef OPENSSL_NO_DSA |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 245 | IMPLEMENT_D2I_FP(DSA, d2i_DSAPrivateKey_fp, d2i_DSAPrivateKey_bio) |
| 246 | IMPLEMENT_I2D_FP(DSA, i2d_DSAPrivateKey_fp, i2d_DSAPrivateKey_bio) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 247 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 248 | IMPLEMENT_D2I_FP(DSA, d2i_DSA_PUBKEY_fp, d2i_DSA_PUBKEY_bio) |
| 249 | IMPLEMENT_I2D_FP(DSA, i2d_DSA_PUBKEY_fp, i2d_DSA_PUBKEY_bio) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 250 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 251 | IMPLEMENT_D2I_BIO(DSA, d2i_DSAPrivateKey_bio, d2i_DSAPrivateKey) |
| 252 | IMPLEMENT_I2D_BIO(DSA, i2d_DSAPrivateKey_bio, i2d_DSAPrivateKey) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 253 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 254 | IMPLEMENT_D2I_BIO(DSA, d2i_DSA_PUBKEY_bio, d2i_DSA_PUBKEY) |
| 255 | IMPLEMENT_I2D_BIO(DSA, i2d_DSA_PUBKEY_bio, i2d_DSA_PUBKEY) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 256 | #endif |
| 257 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 258 | IMPLEMENT_D2I_FP(EC_KEY, d2i_ECPrivateKey_fp, d2i_ECPrivateKey_bio) |
| 259 | IMPLEMENT_I2D_FP(EC_KEY, i2d_ECPrivateKey_fp, i2d_ECPrivateKey_bio) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 260 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 261 | IMPLEMENT_D2I_FP(EC_KEY, d2i_EC_PUBKEY_fp, d2i_EC_PUBKEY_bio) |
| 262 | IMPLEMENT_I2D_FP(EC_KEY, i2d_EC_PUBKEY_fp, i2d_EC_PUBKEY_bio) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 263 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 264 | IMPLEMENT_D2I_BIO(EC_KEY, d2i_ECPrivateKey_bio, d2i_ECPrivateKey) |
| 265 | IMPLEMENT_I2D_BIO(EC_KEY, i2d_ECPrivateKey_bio, i2d_ECPrivateKey) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 266 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 267 | IMPLEMENT_D2I_BIO(EC_KEY, d2i_EC_PUBKEY_bio, d2i_EC_PUBKEY) |
| 268 | IMPLEMENT_I2D_BIO(EC_KEY, i2d_EC_PUBKEY_bio, i2d_EC_PUBKEY) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 269 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 270 | int X509_pubkey_digest(const X509 *data, const EVP_MD *type, unsigned char *md, |
| 271 | unsigned int *len) { |
| 272 | ASN1_BIT_STRING *key; |
| 273 | key = X509_get0_pubkey_bitstr(data); |
| 274 | if (!key) |
| 275 | return 0; |
| 276 | return EVP_Digest(key->data, key->length, md, len, type, NULL); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 277 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 278 | |
| 279 | int X509_digest(const X509 *data, const EVP_MD *type, unsigned char *md, |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 280 | unsigned int *len) { |
| 281 | return (ASN1_item_digest(ASN1_ITEM_rptr(X509), type, (char *)data, md, len)); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 282 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 283 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 284 | int X509_CRL_digest(const X509_CRL *data, const EVP_MD *type, unsigned char *md, |
| 285 | unsigned int *len) { |
| 286 | return ( |
| 287 | ASN1_item_digest(ASN1_ITEM_rptr(X509_CRL), type, (char *)data, md, len)); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 288 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 289 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 290 | int X509_REQ_digest(const X509_REQ *data, const EVP_MD *type, unsigned char *md, |
| 291 | unsigned int *len) { |
| 292 | return ( |
| 293 | ASN1_item_digest(ASN1_ITEM_rptr(X509_REQ), type, (char *)data, md, len)); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 294 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 295 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 296 | int X509_NAME_digest(const X509_NAME *data, const EVP_MD *type, |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 297 | unsigned char *md, unsigned int *len) { |
| 298 | return ( |
| 299 | ASN1_item_digest(ASN1_ITEM_rptr(X509_NAME), type, (char *)data, md, len)); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 300 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 301 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 302 | IMPLEMENT_D2I_FP(X509_SIG, d2i_PKCS8_fp, d2i_PKCS8_bio) |
| 303 | IMPLEMENT_I2D_FP(X509_SIG, i2d_PKCS8_fp, i2d_PKCS8_bio) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 304 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 305 | IMPLEMENT_D2I_BIO(X509_SIG, d2i_PKCS8_bio, d2i_X509_SIG) |
| 306 | IMPLEMENT_I2D_BIO(X509_SIG, i2d_PKCS8_bio, i2d_X509_SIG) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 307 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 308 | IMPLEMENT_D2I_FP(PKCS8_PRIV_KEY_INFO, d2i_PKCS8_PRIV_KEY_INFO_fp, |
| 309 | d2i_PKCS8_PRIV_KEY_INFO_bio) |
| 310 | IMPLEMENT_I2D_FP(PKCS8_PRIV_KEY_INFO, i2d_PKCS8_PRIV_KEY_INFO_fp, |
| 311 | i2d_PKCS8_PRIV_KEY_INFO_bio) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 312 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 313 | int i2d_PKCS8PrivateKeyInfo_fp(FILE *fp, EVP_PKEY *key) { |
| 314 | PKCS8_PRIV_KEY_INFO *p8inf; |
| 315 | int ret; |
| 316 | p8inf = EVP_PKEY2PKCS8(key); |
| 317 | if (!p8inf) |
| 318 | return 0; |
| 319 | ret = i2d_PKCS8_PRIV_KEY_INFO_fp(fp, p8inf); |
| 320 | PKCS8_PRIV_KEY_INFO_free(p8inf); |
| 321 | return ret; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 322 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 323 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 324 | IMPLEMENT_D2I_FP(EVP_PKEY, d2i_PrivateKey_fp, d2i_PrivateKey_bio) |
| 325 | IMPLEMENT_I2D_FP(EVP_PKEY, i2d_PrivateKey_fp, i2d_PrivateKey_bio) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 326 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 327 | IMPLEMENT_D2I_FP(EVP_PKEY, d2i_PUBKEY_fp, d2i_PUBKEY_bio) |
| 328 | IMPLEMENT_I2D_FP(EVP_PKEY, i2d_PUBKEY_fp, i2d_PUBKEY_bio) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 329 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 330 | IMPLEMENT_D2I_BIO(PKCS8_PRIV_KEY_INFO, d2i_PKCS8_PRIV_KEY_INFO_bio, |
| 331 | d2i_PKCS8_PRIV_KEY_INFO) |
| 332 | IMPLEMENT_I2D_BIO(PKCS8_PRIV_KEY_INFO, i2d_PKCS8_PRIV_KEY_INFO_bio, |
| 333 | i2d_PKCS8_PRIV_KEY_INFO) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 334 | |
David Benjamin | 260a10c | 2022-06-16 13:58:28 -0400 | [diff] [blame^] | 335 | int i2d_PKCS8PrivateKeyInfo_bio(BIO *bp, EVP_PKEY *key) { |
| 336 | PKCS8_PRIV_KEY_INFO *p8inf; |
| 337 | int ret; |
| 338 | p8inf = EVP_PKEY2PKCS8(key); |
| 339 | if (!p8inf) |
| 340 | return 0; |
| 341 | ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf); |
| 342 | PKCS8_PRIV_KEY_INFO_free(p8inf); |
| 343 | return ret; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 344 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 345 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 346 | IMPLEMENT_D2I_BIO(EVP_PKEY, d2i_PrivateKey_bio, d2i_AutoPrivateKey) |
| 347 | IMPLEMENT_I2D_BIO(EVP_PKEY, i2d_PrivateKey_bio, i2d_PrivateKey) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 348 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 349 | IMPLEMENT_D2I_BIO(EVP_PKEY, d2i_PUBKEY_bio, d2i_PUBKEY) |
| 350 | IMPLEMENT_I2D_BIO(EVP_PKEY, i2d_PUBKEY_bio, i2d_PUBKEY) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 351 | |
David Benjamin | 419144a | 2018-09-22 19:35:12 -0500 | [diff] [blame] | 352 | IMPLEMENT_D2I_BIO(DH, d2i_DHparams_bio, d2i_DHparams) |
| 353 | IMPLEMENT_I2D_BIO(const DH, i2d_DHparams_bio, i2d_DHparams) |