Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [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 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 57 | #include <openssl/x509.h> |
| 58 | |
| 59 | #include <limits.h> |
| 60 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 61 | #include <openssl/asn1.h> |
| 62 | #include <openssl/buf.h> |
| 63 | #include <openssl/digest.h> |
| 64 | #include <openssl/dsa.h> |
| 65 | #include <openssl/evp.h> |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 66 | #include <openssl/mem.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 67 | #include <openssl/rsa.h> |
| 68 | #include <openssl/stack.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 69 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 70 | int X509_verify(X509 *a, EVP_PKEY *r) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 71 | { |
Robert Sloan | 036981b | 2018-09-24 10:20:02 -0700 | [diff] [blame] | 72 | if (X509_ALGOR_cmp(a->sig_alg, a->cert_info->signature)) { |
| 73 | OPENSSL_PUT_ERROR(X509, X509_R_SIGNATURE_ALGORITHM_MISMATCH); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 74 | return 0; |
Robert Sloan | 036981b | 2018-09-24 10:20:02 -0700 | [diff] [blame] | 75 | } |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 76 | return (ASN1_item_verify(ASN1_ITEM_rptr(X509_CINF), a->sig_alg, |
| 77 | a->signature, a->cert_info, r)); |
| 78 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 79 | |
| 80 | int X509_REQ_verify(X509_REQ *a, EVP_PKEY *r) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 81 | { |
| 82 | return (ASN1_item_verify(ASN1_ITEM_rptr(X509_REQ_INFO), |
| 83 | a->sig_alg, a->signature, a->req_info, r)); |
| 84 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 85 | |
| 86 | int X509_sign(X509 *x, EVP_PKEY *pkey, const EVP_MD *md) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 87 | { |
| 88 | x->cert_info->enc.modified = 1; |
| 89 | return (ASN1_item_sign(ASN1_ITEM_rptr(X509_CINF), x->cert_info->signature, |
| 90 | x->sig_alg, x->signature, x->cert_info, pkey, md)); |
| 91 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 92 | |
| 93 | int X509_sign_ctx(X509 *x, EVP_MD_CTX *ctx) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 94 | { |
| 95 | x->cert_info->enc.modified = 1; |
| 96 | return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_CINF), |
| 97 | x->cert_info->signature, |
| 98 | x->sig_alg, x->signature, x->cert_info, ctx); |
| 99 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 100 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 101 | int X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 102 | { |
| 103 | return (ASN1_item_sign(ASN1_ITEM_rptr(X509_REQ_INFO), x->sig_alg, NULL, |
| 104 | x->signature, x->req_info, pkey, md)); |
| 105 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 106 | |
| 107 | int X509_REQ_sign_ctx(X509_REQ *x, EVP_MD_CTX *ctx) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 108 | { |
| 109 | return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_REQ_INFO), |
| 110 | x->sig_alg, NULL, x->signature, x->req_info, |
| 111 | ctx); |
| 112 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 113 | |
| 114 | int X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 115 | { |
| 116 | x->crl->enc.modified = 1; |
| 117 | return (ASN1_item_sign(ASN1_ITEM_rptr(X509_CRL_INFO), x->crl->sig_alg, |
| 118 | x->sig_alg, x->signature, x->crl, pkey, md)); |
| 119 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 120 | |
| 121 | int X509_CRL_sign_ctx(X509_CRL *x, EVP_MD_CTX *ctx) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 122 | { |
| 123 | x->crl->enc.modified = 1; |
| 124 | return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_CRL_INFO), |
| 125 | x->crl->sig_alg, x->sig_alg, x->signature, |
| 126 | x->crl, ctx); |
| 127 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 128 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 129 | int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *x, EVP_PKEY *pkey, const EVP_MD *md) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 130 | { |
| 131 | return (ASN1_item_sign(ASN1_ITEM_rptr(NETSCAPE_SPKAC), x->sig_algor, NULL, |
| 132 | x->signature, x->spkac, pkey, md)); |
| 133 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 134 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 135 | int NETSCAPE_SPKI_verify(NETSCAPE_SPKI *x, EVP_PKEY *pkey) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 136 | { |
| 137 | return (ASN1_item_verify(ASN1_ITEM_rptr(NETSCAPE_SPKAC), x->sig_algor, |
| 138 | x->signature, x->spkac, pkey)); |
| 139 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 140 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 141 | #ifndef OPENSSL_NO_FP_API |
| 142 | X509 *d2i_X509_fp(FILE *fp, X509 **x509) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 143 | { |
| 144 | return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509), fp, x509); |
| 145 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 146 | |
| 147 | int i2d_X509_fp(FILE *fp, X509 *x509) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 148 | { |
| 149 | return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509), fp, x509); |
| 150 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 151 | #endif |
| 152 | |
| 153 | X509 *d2i_X509_bio(BIO *bp, X509 **x509) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 154 | { |
| 155 | return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509), bp, x509); |
| 156 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 157 | |
| 158 | int i2d_X509_bio(BIO *bp, X509 *x509) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 159 | { |
| 160 | return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509), bp, x509); |
| 161 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 162 | |
| 163 | #ifndef OPENSSL_NO_FP_API |
| 164 | X509_CRL *d2i_X509_CRL_fp(FILE *fp, X509_CRL **crl) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 165 | { |
| 166 | return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_CRL), fp, crl); |
| 167 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 168 | |
| 169 | int i2d_X509_CRL_fp(FILE *fp, X509_CRL *crl) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 170 | { |
| 171 | return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_CRL), fp, crl); |
| 172 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 173 | #endif |
| 174 | |
| 175 | X509_CRL *d2i_X509_CRL_bio(BIO *bp, X509_CRL **crl) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 176 | { |
| 177 | return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509_CRL), bp, crl); |
| 178 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 179 | |
| 180 | int i2d_X509_CRL_bio(BIO *bp, X509_CRL *crl) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 181 | { |
| 182 | return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_CRL), bp, crl); |
| 183 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 184 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 185 | #ifndef OPENSSL_NO_FP_API |
| 186 | X509_REQ *d2i_X509_REQ_fp(FILE *fp, X509_REQ **req) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 187 | { |
| 188 | return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_REQ), fp, req); |
| 189 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 190 | |
| 191 | int i2d_X509_REQ_fp(FILE *fp, X509_REQ *req) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 192 | { |
| 193 | return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_REQ), fp, req); |
| 194 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 195 | #endif |
| 196 | |
| 197 | X509_REQ *d2i_X509_REQ_bio(BIO *bp, X509_REQ **req) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 198 | { |
| 199 | return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509_REQ), bp, req); |
| 200 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 201 | |
| 202 | int i2d_X509_REQ_bio(BIO *bp, X509_REQ *req) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 203 | { |
| 204 | return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_REQ), bp, req); |
| 205 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 206 | |
| 207 | #ifndef OPENSSL_NO_FP_API |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 208 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 209 | #define IMPLEMENT_D2I_FP(type, name, bio_func) \ |
| 210 | type *name(FILE *fp, type **obj) { \ |
| 211 | BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE); \ |
| 212 | if (bio == NULL) { \ |
| 213 | return NULL; \ |
| 214 | } \ |
| 215 | type *ret = bio_func(bio, obj); \ |
| 216 | BIO_free(bio); \ |
| 217 | return ret; \ |
| 218 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 219 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 220 | #define IMPLEMENT_I2D_FP(type, name, bio_func) \ |
| 221 | int name(FILE *fp, type *obj) { \ |
| 222 | BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE); \ |
| 223 | if (bio == NULL) { \ |
| 224 | return 0; \ |
| 225 | } \ |
| 226 | int ret = bio_func(bio, obj); \ |
| 227 | BIO_free(bio); \ |
| 228 | return ret; \ |
| 229 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 230 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 231 | IMPLEMENT_D2I_FP(RSA, d2i_RSAPrivateKey_fp, d2i_RSAPrivateKey_bio) |
| 232 | IMPLEMENT_I2D_FP(RSA, i2d_RSAPrivateKey_fp, i2d_RSAPrivateKey_bio) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 233 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 234 | IMPLEMENT_D2I_FP(RSA, d2i_RSAPublicKey_fp, d2i_RSAPublicKey_bio) |
| 235 | IMPLEMENT_I2D_FP(RSA, i2d_RSAPublicKey_fp, i2d_RSAPublicKey_bio) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 236 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 237 | IMPLEMENT_D2I_FP(RSA, d2i_RSA_PUBKEY_fp, d2i_RSA_PUBKEY_bio) |
| 238 | IMPLEMENT_I2D_FP(RSA, i2d_RSA_PUBKEY_fp, i2d_RSA_PUBKEY_bio) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 239 | #endif |
| 240 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 241 | #define IMPLEMENT_D2I_BIO(type, name, d2i_func) \ |
| 242 | type *name(BIO *bio, type **obj) { \ |
| 243 | uint8_t *data; \ |
| 244 | size_t len; \ |
| 245 | if (!BIO_read_asn1(bio, &data, &len, 100 * 1024)) { \ |
| 246 | return NULL; \ |
| 247 | } \ |
| 248 | const uint8_t *ptr = data; \ |
| 249 | type *ret = d2i_func(obj, &ptr, (long)len); \ |
| 250 | OPENSSL_free(data); \ |
| 251 | return ret; \ |
| 252 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 253 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 254 | #define IMPLEMENT_I2D_BIO(type, name, i2d_func) \ |
| 255 | int name(BIO *bio, type *obj) { \ |
| 256 | uint8_t *data = NULL; \ |
| 257 | int len = i2d_func(obj, &data); \ |
| 258 | if (len < 0) { \ |
| 259 | return 0; \ |
| 260 | } \ |
| 261 | int ret = BIO_write_all(bio, data, len); \ |
| 262 | OPENSSL_free(data); \ |
| 263 | return ret; \ |
| 264 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 265 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 266 | IMPLEMENT_D2I_BIO(RSA, d2i_RSAPrivateKey_bio, d2i_RSAPrivateKey) |
| 267 | IMPLEMENT_I2D_BIO(RSA, i2d_RSAPrivateKey_bio, i2d_RSAPrivateKey) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 268 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 269 | IMPLEMENT_D2I_BIO(RSA, d2i_RSAPublicKey_bio, d2i_RSAPublicKey) |
| 270 | IMPLEMENT_I2D_BIO(RSA, i2d_RSAPublicKey_bio, i2d_RSAPublicKey) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 271 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 272 | IMPLEMENT_D2I_BIO(RSA, d2i_RSA_PUBKEY_bio, d2i_RSA_PUBKEY) |
| 273 | IMPLEMENT_I2D_BIO(RSA, i2d_RSA_PUBKEY_bio, i2d_RSA_PUBKEY) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 274 | |
| 275 | #ifndef OPENSSL_NO_DSA |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 276 | # ifndef OPENSSL_NO_FP_API |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 277 | IMPLEMENT_D2I_FP(DSA, d2i_DSAPrivateKey_fp, d2i_DSAPrivateKey_bio) |
| 278 | IMPLEMENT_I2D_FP(DSA, i2d_DSAPrivateKey_fp, i2d_DSAPrivateKey_bio) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 279 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 280 | IMPLEMENT_D2I_FP(DSA, d2i_DSA_PUBKEY_fp, d2i_DSA_PUBKEY_bio) |
| 281 | IMPLEMENT_I2D_FP(DSA, i2d_DSA_PUBKEY_fp, i2d_DSA_PUBKEY_bio) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 282 | # endif |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 283 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 284 | IMPLEMENT_D2I_BIO(DSA, d2i_DSAPrivateKey_bio, d2i_DSAPrivateKey) |
| 285 | IMPLEMENT_I2D_BIO(DSA, i2d_DSAPrivateKey_bio, i2d_DSAPrivateKey) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 286 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 287 | IMPLEMENT_D2I_BIO(DSA, d2i_DSA_PUBKEY_bio, d2i_DSA_PUBKEY) |
| 288 | IMPLEMENT_I2D_BIO(DSA, i2d_DSA_PUBKEY_bio, i2d_DSA_PUBKEY) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 289 | #endif |
| 290 | |
| 291 | #ifndef OPENSSL_NO_FP_API |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 292 | IMPLEMENT_D2I_FP(EC_KEY, d2i_ECPrivateKey_fp, d2i_ECPrivateKey_bio) |
| 293 | IMPLEMENT_I2D_FP(EC_KEY, i2d_ECPrivateKey_fp, i2d_ECPrivateKey_bio) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 294 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 295 | IMPLEMENT_D2I_FP(EC_KEY, d2i_EC_PUBKEY_fp, d2i_EC_PUBKEY_bio) |
| 296 | IMPLEMENT_I2D_FP(EC_KEY, i2d_EC_PUBKEY_fp, i2d_EC_PUBKEY_bio) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 297 | #endif |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 298 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 299 | IMPLEMENT_D2I_BIO(EC_KEY, d2i_ECPrivateKey_bio, d2i_ECPrivateKey) |
| 300 | IMPLEMENT_I2D_BIO(EC_KEY, i2d_ECPrivateKey_bio, i2d_ECPrivateKey) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 301 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 302 | IMPLEMENT_D2I_BIO(EC_KEY, d2i_EC_PUBKEY_bio, d2i_EC_PUBKEY) |
| 303 | IMPLEMENT_I2D_BIO(EC_KEY, i2d_EC_PUBKEY_bio, i2d_EC_PUBKEY) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 304 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 305 | int X509_pubkey_digest(const X509 *data, const EVP_MD *type, |
| 306 | unsigned char *md, unsigned int *len) |
| 307 | { |
| 308 | ASN1_BIT_STRING *key; |
| 309 | key = X509_get0_pubkey_bitstr(data); |
| 310 | if (!key) |
| 311 | return 0; |
| 312 | return EVP_Digest(key->data, key->length, md, len, type, NULL); |
| 313 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 314 | |
| 315 | int X509_digest(const X509 *data, const EVP_MD *type, unsigned char *md, |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 316 | unsigned int *len) |
| 317 | { |
| 318 | return (ASN1_item_digest |
| 319 | (ASN1_ITEM_rptr(X509), type, (char *)data, md, len)); |
| 320 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 321 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 322 | int X509_CRL_digest(const X509_CRL *data, const EVP_MD *type, |
| 323 | unsigned char *md, unsigned int *len) |
| 324 | { |
| 325 | return (ASN1_item_digest |
| 326 | (ASN1_ITEM_rptr(X509_CRL), type, (char *)data, md, len)); |
| 327 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 328 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 329 | int X509_REQ_digest(const X509_REQ *data, const EVP_MD *type, |
| 330 | unsigned char *md, unsigned int *len) |
| 331 | { |
| 332 | return (ASN1_item_digest |
| 333 | (ASN1_ITEM_rptr(X509_REQ), type, (char *)data, md, len)); |
| 334 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 335 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 336 | int X509_NAME_digest(const X509_NAME *data, const EVP_MD *type, |
| 337 | unsigned char *md, unsigned int *len) |
| 338 | { |
| 339 | return (ASN1_item_digest |
| 340 | (ASN1_ITEM_rptr(X509_NAME), type, (char *)data, md, len)); |
| 341 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 342 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 343 | #ifndef OPENSSL_NO_FP_API |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 344 | IMPLEMENT_D2I_FP(X509_SIG, d2i_PKCS8_fp, d2i_PKCS8_bio) |
| 345 | IMPLEMENT_I2D_FP(X509_SIG, i2d_PKCS8_fp, i2d_PKCS8_bio) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 346 | #endif |
| 347 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 348 | IMPLEMENT_D2I_BIO(X509_SIG, d2i_PKCS8_bio, d2i_X509_SIG) |
| 349 | IMPLEMENT_I2D_BIO(X509_SIG, i2d_PKCS8_bio, i2d_X509_SIG) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 350 | |
| 351 | #ifndef OPENSSL_NO_FP_API |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 352 | IMPLEMENT_D2I_FP(PKCS8_PRIV_KEY_INFO, d2i_PKCS8_PRIV_KEY_INFO_fp, |
| 353 | d2i_PKCS8_PRIV_KEY_INFO_bio) |
| 354 | IMPLEMENT_I2D_FP(PKCS8_PRIV_KEY_INFO, i2d_PKCS8_PRIV_KEY_INFO_fp, |
| 355 | i2d_PKCS8_PRIV_KEY_INFO_bio) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 356 | |
| 357 | int i2d_PKCS8PrivateKeyInfo_fp(FILE *fp, EVP_PKEY *key) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 358 | { |
| 359 | PKCS8_PRIV_KEY_INFO *p8inf; |
| 360 | int ret; |
| 361 | p8inf = EVP_PKEY2PKCS8(key); |
| 362 | if (!p8inf) |
| 363 | return 0; |
| 364 | ret = i2d_PKCS8_PRIV_KEY_INFO_fp(fp, p8inf); |
| 365 | PKCS8_PRIV_KEY_INFO_free(p8inf); |
| 366 | return ret; |
| 367 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 368 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 369 | IMPLEMENT_D2I_FP(EVP_PKEY, d2i_PrivateKey_fp, d2i_PrivateKey_bio) |
| 370 | IMPLEMENT_I2D_FP(EVP_PKEY, i2d_PrivateKey_fp, i2d_PrivateKey_bio) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 371 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 372 | IMPLEMENT_D2I_FP(EVP_PKEY, d2i_PUBKEY_fp, d2i_PUBKEY_bio) |
| 373 | IMPLEMENT_I2D_FP(EVP_PKEY, i2d_PUBKEY_fp, i2d_PUBKEY_bio) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 374 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 375 | IMPLEMENT_D2I_BIO(PKCS8_PRIV_KEY_INFO, d2i_PKCS8_PRIV_KEY_INFO_bio, |
| 376 | d2i_PKCS8_PRIV_KEY_INFO) |
| 377 | IMPLEMENT_I2D_BIO(PKCS8_PRIV_KEY_INFO, i2d_PKCS8_PRIV_KEY_INFO_bio, |
| 378 | i2d_PKCS8_PRIV_KEY_INFO) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 379 | |
| 380 | int i2d_PKCS8PrivateKeyInfo_bio(BIO *bp, EVP_PKEY *key) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 381 | { |
| 382 | PKCS8_PRIV_KEY_INFO *p8inf; |
| 383 | int ret; |
| 384 | p8inf = EVP_PKEY2PKCS8(key); |
| 385 | if (!p8inf) |
| 386 | return 0; |
| 387 | ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf); |
| 388 | PKCS8_PRIV_KEY_INFO_free(p8inf); |
| 389 | return ret; |
| 390 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 391 | #endif |
| 392 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 393 | IMPLEMENT_D2I_BIO(EVP_PKEY, d2i_PrivateKey_bio, d2i_AutoPrivateKey) |
| 394 | IMPLEMENT_I2D_BIO(EVP_PKEY, i2d_PrivateKey_bio, i2d_PrivateKey) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 395 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 396 | IMPLEMENT_D2I_BIO(EVP_PKEY, d2i_PUBKEY_bio, d2i_PUBKEY) |
| 397 | IMPLEMENT_I2D_BIO(EVP_PKEY, i2d_PUBKEY_bio, i2d_PUBKEY) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 398 | |
Robert Sloan | f068def | 2018-10-10 18:45:40 -0700 | [diff] [blame] | 399 | IMPLEMENT_D2I_BIO(DH, d2i_DHparams_bio, d2i_DHparams) |
| 400 | IMPLEMENT_I2D_BIO(const DH, i2d_DHparams_bio, i2d_DHparams) |