| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/x509.h> |
| 5 |
|
| 6 |
#include <limits.h> |
| 7 |
|
| 8 |
#include <openssl/asn1.h> |
| 9 |
#include <openssl/digest.h> |
| 10 |
#include <openssl/dsa.h> |
| 11 |
#include <openssl/evp.h> |
| 12 |
#include <openssl/mem.h> |
| 13 |
#include <openssl/rsa.h> |
| 14 |
#include <openssl/stack.h> |
| 15 |
|
| 16 |
#include "../asn1/internal.h" |
| 17 |
#include "../ocsp/internal.h" |
| 18 |
#include "internal.h" |
| 19 |
|
| 20 |
|
| 21 |
int X509_verify(X509 *x509, EVP_PKEY *pkey) { |
| 22 |
if (X509_ALGOR_cmp(x509->sig_alg, x509->cert_info->signature)) { |
| 23 |
OPENSSL_PUT_ERROR(X509, X509_R_SIGNATURE_ALGORITHM_MISMATCH); |
| 24 |
return 0; |
| 25 |
} |
| 26 |
return ASN1_item_verify(ASN1_ITEM_rptr(X509_CINF), x509->sig_alg, |
| 27 |
x509->signature, x509->cert_info, pkey); |
| 28 |
} |
| 29 |
|
| 30 |
int X509_REQ_verify(X509_REQ *req, EVP_PKEY *pkey) { |
| 31 |
return ASN1_item_verify(ASN1_ITEM_rptr(X509_REQ_INFO), req->sig_alg, |
| 32 |
req->signature, req->req_info, pkey); |
| 33 |
} |
| 34 |
|
| 35 |
int X509_sign(X509 *x, EVP_PKEY *pkey, const EVP_MD *md) { |
| 36 |
asn1_encoding_clear(&x->cert_info->enc); |
| 37 |
return (ASN1_item_sign(ASN1_ITEM_rptr(X509_CINF), x->cert_info->signature, |
| 38 |
x->sig_alg, x->signature, x->cert_info, pkey, md)); |
| 39 |
} |
| 40 |
|
| 41 |
int X509_sign_ctx(X509 *x, EVP_MD_CTX *ctx) { |
| 42 |
asn1_encoding_clear(&x->cert_info->enc); |
| 43 |
return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_CINF), x->cert_info->signature, |
| 44 |
x->sig_alg, x->signature, x->cert_info, ctx); |
| 45 |
} |
| 46 |
|
| 47 |
int X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md) { |
| 48 |
asn1_encoding_clear(&x->req_info->enc); |
| 49 |
return (ASN1_item_sign(ASN1_ITEM_rptr(X509_REQ_INFO), x->sig_alg, NULL, |
| 50 |
x->signature, x->req_info, pkey, md)); |
| 51 |
} |
| 52 |
|
| 53 |
int X509_REQ_sign_ctx(X509_REQ *x, EVP_MD_CTX *ctx) { |
| 54 |
asn1_encoding_clear(&x->req_info->enc); |
| 55 |
return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_REQ_INFO), x->sig_alg, NULL, |
| 56 |
x->signature, x->req_info, ctx); |
| 57 |
} |
| 58 |
|
| 59 |
int X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md) { |
| 60 |
asn1_encoding_clear(&x->crl->enc); |
| 61 |
return (ASN1_item_sign(ASN1_ITEM_rptr(X509_CRL_INFO), x->crl->sig_alg, |
| 62 |
x->sig_alg, x->signature, x->crl, pkey, md)); |
| 63 |
} |
| 64 |
|
| 65 |
int X509_CRL_sign_ctx(X509_CRL *x, EVP_MD_CTX *ctx) { |
| 66 |
asn1_encoding_clear(&x->crl->enc); |
| 67 |
return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_CRL_INFO), x->crl->sig_alg, |
| 68 |
x->sig_alg, x->signature, x->crl, ctx); |
| 69 |
} |
| 70 |
|
| 71 |
int X509_CRL_http_nbio(OCSP_REQ_CTX *rctx, X509_CRL **pcrl) { |
| 72 |
return OCSP_REQ_CTX_nbio_d2i(rctx, (ASN1_VALUE **)pcrl, |
| 73 |
ASN1_ITEM_rptr(X509_CRL)); |
| 74 |
} |
| 75 |
|
| 76 |
int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *x, EVP_PKEY *pkey, const EVP_MD *md) { |
| 77 |
return (ASN1_item_sign(ASN1_ITEM_rptr(NETSCAPE_SPKAC), x->sig_algor, NULL, |
| 78 |
x->signature, x->spkac, pkey, md)); |
| 79 |
} |
| 80 |
|
| 81 |
int NETSCAPE_SPKI_verify(NETSCAPE_SPKI *spki, EVP_PKEY *pkey) { |
| 82 |
return (ASN1_item_verify(ASN1_ITEM_rptr(NETSCAPE_SPKAC), spki->sig_algor, |
| 83 |
spki->signature, spki->spkac, pkey)); |
| 84 |
} |
| 85 |
|
| 86 |
X509 *d2i_X509_fp(FILE *fp, X509 **x509) { |
| 87 |
return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509), fp, x509); |
| 88 |
} |
| 89 |
|
| 90 |
int i2d_X509_fp(FILE *fp, X509 *x509) { |
| 91 |
return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509), fp, x509); |
| 92 |
} |
| 93 |
|
| 94 |
X509 *d2i_X509_bio(BIO *bp, X509 **x509) { |
| 95 |
return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509), bp, x509); |
| 96 |
} |
| 97 |
|
| 98 |
int i2d_X509_bio(BIO *bp, X509 *x509) { |
| 99 |
return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509), bp, x509); |
| 100 |
} |
| 101 |
|
| 102 |
X509_CRL *d2i_X509_CRL_fp(FILE *fp, X509_CRL **crl) { |
| 103 |
return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_CRL), fp, crl); |
| 104 |
} |
| 105 |
|
| 106 |
int i2d_X509_CRL_fp(FILE *fp, X509_CRL *crl) { |
| 107 |
return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_CRL), fp, crl); |
| 108 |
} |
| 109 |
|
| 110 |
X509_CRL *d2i_X509_CRL_bio(BIO *bp, X509_CRL **crl) { |
| 111 |
return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509_CRL), bp, crl); |
| 112 |
} |
| 113 |
|
| 114 |
int i2d_X509_CRL_bio(BIO *bp, X509_CRL *crl) { |
| 115 |
return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_CRL), bp, crl); |
| 116 |
} |
| 117 |
|
| 118 |
X509_REQ *d2i_X509_REQ_fp(FILE *fp, X509_REQ **req) { |
| 119 |
return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_REQ), fp, req); |
| 120 |
} |
| 121 |
|
| 122 |
int i2d_X509_REQ_fp(FILE *fp, X509_REQ *req) { |
| 123 |
return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_REQ), fp, req); |
| 124 |
} |
| 125 |
|
| 126 |
X509_REQ *d2i_X509_REQ_bio(BIO *bp, X509_REQ **req) { |
| 127 |
return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509_REQ), bp, req); |
| 128 |
} |
| 129 |
|
| 130 |
int i2d_X509_REQ_bio(BIO *bp, X509_REQ *req) { |
| 131 |
return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_REQ), bp, req); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
#define IMPLEMENT_D2I_FP(type, name, bio_func) \ |
| 136 |
type *name(FILE *fp, type **obj) { \ |
| 137 |
BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE); \ |
| 138 |
if (bio == NULL) { \ |
| 139 |
return NULL; \ |
| 140 |
} \ |
| 141 |
type *ret = bio_func(bio, obj); \ |
| 142 |
BIO_free(bio); \ |
| 143 |
return ret; \ |
| 144 |
} |
| 145 |
|
| 146 |
#define IMPLEMENT_I2D_FP(type, name, bio_func) \ |
| 147 |
int name(FILE *fp, type *obj) { \ |
| 148 |
BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE); \ |
| 149 |
if (bio == NULL) { \ |
| 150 |
return 0; \ |
| 151 |
} \ |
| 152 |
int ret = bio_func(bio, obj); \ |
| 153 |
BIO_free(bio); \ |
| 154 |
return ret; \ |
| 155 |
} |
| 156 |
|
| 157 |
IMPLEMENT_D2I_FP(RSA, d2i_RSAPrivateKey_fp, d2i_RSAPrivateKey_bio) |
| 158 |
IMPLEMENT_I2D_FP(RSA, i2d_RSAPrivateKey_fp, i2d_RSAPrivateKey_bio) |
| 159 |
|
| 160 |
IMPLEMENT_D2I_FP(RSA, d2i_RSAPublicKey_fp, d2i_RSAPublicKey_bio) |
| 161 |
IMPLEMENT_I2D_FP(RSA, i2d_RSAPublicKey_fp, i2d_RSAPublicKey_bio) |
| 162 |
|
| 163 |
IMPLEMENT_D2I_FP(RSA, d2i_RSA_PUBKEY_fp, d2i_RSA_PUBKEY_bio) |
| 164 |
IMPLEMENT_I2D_FP(RSA, i2d_RSA_PUBKEY_fp, i2d_RSA_PUBKEY_bio) |
| 165 |
|
| 166 |
#define IMPLEMENT_D2I_BIO(type, name, d2i_func) \ |
| 167 |
type *name(BIO *bio, type **obj) { \ |
| 168 |
uint8_t *data; \ |
| 169 |
size_t len; \ |
| 170 |
if (!BIO_read_asn1(bio, &data, &len, 100 * 1024)) { \ |
| 171 |
return NULL; \ |
| 172 |
} \ |
| 173 |
const uint8_t *ptr = data; \ |
| 174 |
type *ret = d2i_func(obj, &ptr, (long)len); \ |
| 175 |
OPENSSL_free(data); \ |
| 176 |
return ret; \ |
| 177 |
} |
| 178 |
|
| 179 |
#define IMPLEMENT_I2D_BIO(type, name, i2d_func) \ |
| 180 |
int name(BIO *bio, type *obj) { \ |
| 181 |
uint8_t *data = NULL; \ |
| 182 |
int len = i2d_func(obj, &data); \ |
| 183 |
if (len < 0) { \ |
| 184 |
return 0; \ |
| 185 |
} \ |
| 186 |
int ret = BIO_write_all(bio, data, len); \ |
| 187 |
OPENSSL_free(data); \ |
| 188 |
return ret; \ |
| 189 |
} |
| 190 |
|
| 191 |
IMPLEMENT_D2I_BIO(RSA, d2i_RSAPrivateKey_bio, d2i_RSAPrivateKey) |
| 192 |
IMPLEMENT_I2D_BIO(RSA, i2d_RSAPrivateKey_bio, i2d_RSAPrivateKey) |
| 193 |
|
| 194 |
IMPLEMENT_D2I_BIO(RSA, d2i_RSAPublicKey_bio, d2i_RSAPublicKey) |
| 195 |
IMPLEMENT_I2D_BIO(RSA, i2d_RSAPublicKey_bio, i2d_RSAPublicKey) |
| 196 |
|
| 197 |
IMPLEMENT_D2I_BIO(RSA, d2i_RSA_PUBKEY_bio, d2i_RSA_PUBKEY) |
| 198 |
IMPLEMENT_I2D_BIO(RSA, i2d_RSA_PUBKEY_bio, i2d_RSA_PUBKEY) |
| 199 |
|
| 200 |
IMPLEMENT_D2I_FP(DSA, d2i_DSAPrivateKey_fp, d2i_DSAPrivateKey_bio) |
| 201 |
IMPLEMENT_I2D_FP(DSA, i2d_DSAPrivateKey_fp, i2d_DSAPrivateKey_bio) |
| 202 |
|
| 203 |
IMPLEMENT_D2I_FP(DSA, d2i_DSA_PUBKEY_fp, d2i_DSA_PUBKEY_bio) |
| 204 |
IMPLEMENT_I2D_FP(DSA, i2d_DSA_PUBKEY_fp, i2d_DSA_PUBKEY_bio) |
| 205 |
|
| 206 |
IMPLEMENT_D2I_BIO(DSA, d2i_DSAPrivateKey_bio, d2i_DSAPrivateKey) |
| 207 |
IMPLEMENT_I2D_BIO(DSA, i2d_DSAPrivateKey_bio, i2d_DSAPrivateKey) |
| 208 |
|
| 209 |
IMPLEMENT_D2I_BIO(DSA, d2i_DSA_PUBKEY_bio, d2i_DSA_PUBKEY) |
| 210 |
IMPLEMENT_I2D_BIO(DSA, i2d_DSA_PUBKEY_bio, i2d_DSA_PUBKEY) |
| 211 |
|
| 212 |
IMPLEMENT_D2I_FP(EC_KEY, d2i_ECPrivateKey_fp, d2i_ECPrivateKey_bio) |
| 213 |
IMPLEMENT_I2D_FP(EC_KEY, i2d_ECPrivateKey_fp, i2d_ECPrivateKey_bio) |
| 214 |
|
| 215 |
IMPLEMENT_D2I_FP(EC_KEY, d2i_EC_PUBKEY_fp, d2i_EC_PUBKEY_bio) |
| 216 |
IMPLEMENT_I2D_FP(EC_KEY, i2d_EC_PUBKEY_fp, i2d_EC_PUBKEY_bio) |
| 217 |
|
| 218 |
IMPLEMENT_D2I_BIO(EC_KEY, d2i_ECPrivateKey_bio, d2i_ECPrivateKey) |
| 219 |
IMPLEMENT_I2D_BIO(EC_KEY, i2d_ECPrivateKey_bio, i2d_ECPrivateKey) |
| 220 |
|
| 221 |
IMPLEMENT_D2I_BIO(EC_KEY, d2i_EC_PUBKEY_bio, d2i_EC_PUBKEY) |
| 222 |
IMPLEMENT_I2D_BIO(EC_KEY, i2d_EC_PUBKEY_bio, i2d_EC_PUBKEY) |
| 223 |
|
| 224 |
int X509_pubkey_digest(const X509 *data, const EVP_MD *type, unsigned char *md, |
| 225 |
unsigned int *len) { |
| 226 |
ASN1_BIT_STRING *key; |
| 227 |
key = X509_get0_pubkey_bitstr(data); |
| 228 |
if (!key) { |
| 229 |
return 0; |
| 230 |
} |
| 231 |
return EVP_Digest(key->data, key->length, md, len, type, NULL); |
| 232 |
} |
| 233 |
|
| 234 |
int X509_digest(const X509 *data, const EVP_MD *type, unsigned char *md, |
| 235 |
unsigned int *len) { |
| 236 |
return (ASN1_item_digest(ASN1_ITEM_rptr(X509), type, (char *)data, md, len)); |
| 237 |
} |
| 238 |
|
| 239 |
int X509_CRL_digest(const X509_CRL *data, const EVP_MD *type, unsigned char *md, |
| 240 |
unsigned int *len) { |
| 241 |
return ( |
| 242 |
ASN1_item_digest(ASN1_ITEM_rptr(X509_CRL), type, (char *)data, md, len)); |
| 243 |
} |
| 244 |
|
| 245 |
int X509_REQ_digest(const X509_REQ *data, const EVP_MD *type, unsigned char *md, |
| 246 |
unsigned int *len) { |
| 247 |
return ( |
| 248 |
ASN1_item_digest(ASN1_ITEM_rptr(X509_REQ), type, (char *)data, md, len)); |
| 249 |
} |
| 250 |
|
| 251 |
int X509_NAME_digest(const X509_NAME *data, const EVP_MD *type, |
| 252 |
unsigned char *md, unsigned int *len) { |
| 253 |
return ( |
| 254 |
ASN1_item_digest(ASN1_ITEM_rptr(X509_NAME), type, (char *)data, md, len)); |
| 255 |
} |
| 256 |
|
| 257 |
IMPLEMENT_D2I_FP(X509_SIG, d2i_PKCS8_fp, d2i_PKCS8_bio) |
| 258 |
IMPLEMENT_I2D_FP(X509_SIG, i2d_PKCS8_fp, i2d_PKCS8_bio) |
| 259 |
|
| 260 |
IMPLEMENT_D2I_BIO(X509_SIG, d2i_PKCS8_bio, d2i_X509_SIG) |
| 261 |
IMPLEMENT_I2D_BIO(X509_SIG, i2d_PKCS8_bio, i2d_X509_SIG) |
| 262 |
|
| 263 |
IMPLEMENT_D2I_FP(PKCS8_PRIV_KEY_INFO, d2i_PKCS8_PRIV_KEY_INFO_fp, |
| 264 |
d2i_PKCS8_PRIV_KEY_INFO_bio) |
| 265 |
IMPLEMENT_I2D_FP(PKCS8_PRIV_KEY_INFO, i2d_PKCS8_PRIV_KEY_INFO_fp, |
| 266 |
i2d_PKCS8_PRIV_KEY_INFO_bio) |
| 267 |
|
| 268 |
int i2d_PKCS8PrivateKeyInfo_fp(FILE *fp, EVP_PKEY *key) { |
| 269 |
PKCS8_PRIV_KEY_INFO *p8inf; |
| 270 |
int ret; |
| 271 |
p8inf = EVP_PKEY2PKCS8(key); |
| 272 |
if (!p8inf) { |
| 273 |
return 0; |
| 274 |
} |
| 275 |
ret = i2d_PKCS8_PRIV_KEY_INFO_fp(fp, p8inf); |
| 276 |
PKCS8_PRIV_KEY_INFO_free(p8inf); |
| 277 |
return ret; |
| 278 |
} |
| 279 |
|
| 280 |
IMPLEMENT_D2I_FP(EVP_PKEY, d2i_PrivateKey_fp, d2i_PrivateKey_bio) |
| 281 |
IMPLEMENT_I2D_FP(EVP_PKEY, i2d_PrivateKey_fp, i2d_PrivateKey_bio) |
| 282 |
|
| 283 |
IMPLEMENT_D2I_FP(EVP_PKEY, d2i_PUBKEY_fp, d2i_PUBKEY_bio) |
| 284 |
IMPLEMENT_I2D_FP(EVP_PKEY, i2d_PUBKEY_fp, i2d_PUBKEY_bio) |
| 285 |
|
| 286 |
IMPLEMENT_D2I_BIO(PKCS8_PRIV_KEY_INFO, d2i_PKCS8_PRIV_KEY_INFO_bio, |
| 287 |
d2i_PKCS8_PRIV_KEY_INFO) |
| 288 |
IMPLEMENT_I2D_BIO(PKCS8_PRIV_KEY_INFO, i2d_PKCS8_PRIV_KEY_INFO_bio, |
| 289 |
i2d_PKCS8_PRIV_KEY_INFO) |
| 290 |
|
| 291 |
int i2d_PKCS8PrivateKeyInfo_bio(BIO *bp, EVP_PKEY *key) { |
| 292 |
PKCS8_PRIV_KEY_INFO *p8inf; |
| 293 |
int ret; |
| 294 |
p8inf = EVP_PKEY2PKCS8(key); |
| 295 |
if (!p8inf) { |
| 296 |
return 0; |
| 297 |
} |
| 298 |
ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf); |
| 299 |
PKCS8_PRIV_KEY_INFO_free(p8inf); |
| 300 |
return ret; |
| 301 |
} |
| 302 |
|
| 303 |
IMPLEMENT_D2I_BIO(EVP_PKEY, d2i_PrivateKey_bio, d2i_AutoPrivateKey) |
| 304 |
IMPLEMENT_I2D_BIO(EVP_PKEY, i2d_PrivateKey_bio, i2d_PrivateKey) |
| 305 |
|
| 306 |
IMPLEMENT_D2I_BIO(EVP_PKEY, d2i_PUBKEY_bio, d2i_PUBKEY) |
| 307 |
IMPLEMENT_I2D_BIO(EVP_PKEY, i2d_PUBKEY_bio, i2d_PUBKEY) |
| 308 |
|
| 309 |
IMPLEMENT_D2I_BIO(DH, d2i_DHparams_bio, d2i_DHparams) |
| 310 |
IMPLEMENT_I2D_BIO(const DH, i2d_DHparams_bio, i2d_DHparams) |