1 /* $OpenBSD: x_all.c,v 1.31 2024/03/27 01:22:30 tb Exp $ */ 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 59 #include <stdio.h> 60 61 #include <openssl/opensslconf.h> 62 63 #include <openssl/asn1.h> 64 #include <openssl/buffer.h> 65 #include <openssl/evp.h> 66 #include <openssl/stack.h> 67 #include <openssl/x509.h> 68 69 #ifndef OPENSSL_NO_DSA 70 #include <openssl/dsa.h> 71 #endif 72 #ifndef OPENSSL_NO_RSA 73 #include <openssl/rsa.h> 74 #endif 75 76 #include "x509_local.h" 77 78 X509 * 79 d2i_X509_bio(BIO *bp, X509 **x509) 80 { 81 return ASN1_item_d2i_bio(&X509_it, bp, x509); 82 } 83 LCRYPTO_ALIAS(d2i_X509_bio); 84 85 int 86 i2d_X509_bio(BIO *bp, X509 *x509) 87 { 88 return ASN1_item_i2d_bio(&X509_it, bp, x509); 89 } 90 LCRYPTO_ALIAS(i2d_X509_bio); 91 92 X509 * 93 d2i_X509_fp(FILE *fp, X509 **x509) 94 { 95 return ASN1_item_d2i_fp(&X509_it, fp, x509); 96 } 97 LCRYPTO_ALIAS(d2i_X509_fp); 98 99 int 100 i2d_X509_fp(FILE *fp, X509 *x509) 101 { 102 return ASN1_item_i2d_fp(&X509_it, fp, x509); 103 } 104 LCRYPTO_ALIAS(i2d_X509_fp); 105 106 X509_CRL * 107 d2i_X509_CRL_bio(BIO *bp, X509_CRL **crl) 108 { 109 return ASN1_item_d2i_bio(&X509_CRL_it, bp, crl); 110 } 111 LCRYPTO_ALIAS(d2i_X509_CRL_bio); 112 113 int 114 i2d_X509_CRL_bio(BIO *bp, X509_CRL *crl) 115 { 116 return ASN1_item_i2d_bio(&X509_CRL_it, bp, crl); 117 } 118 LCRYPTO_ALIAS(i2d_X509_CRL_bio); 119 120 X509_CRL * 121 d2i_X509_CRL_fp(FILE *fp, X509_CRL **crl) 122 { 123 return ASN1_item_d2i_fp(&X509_CRL_it, fp, crl); 124 } 125 LCRYPTO_ALIAS(d2i_X509_CRL_fp); 126 127 int 128 i2d_X509_CRL_fp(FILE *fp, X509_CRL *crl) 129 { 130 return ASN1_item_i2d_fp(&X509_CRL_it, fp, crl); 131 } 132 LCRYPTO_ALIAS(i2d_X509_CRL_fp); 133 134 X509_REQ * 135 d2i_X509_REQ_bio(BIO *bp, X509_REQ **req) 136 { 137 return ASN1_item_d2i_bio(&X509_REQ_it, bp, req); 138 } 139 LCRYPTO_ALIAS(d2i_X509_REQ_bio); 140 141 int 142 i2d_X509_REQ_bio(BIO *bp, X509_REQ *req) 143 { 144 return ASN1_item_i2d_bio(&X509_REQ_it, bp, req); 145 } 146 LCRYPTO_ALIAS(i2d_X509_REQ_bio); 147 148 X509_REQ * 149 d2i_X509_REQ_fp(FILE *fp, X509_REQ **req) 150 { 151 return ASN1_item_d2i_fp(&X509_REQ_it, fp, req); 152 } 153 LCRYPTO_ALIAS(d2i_X509_REQ_fp); 154 155 int 156 i2d_X509_REQ_fp(FILE *fp, X509_REQ *req) 157 { 158 return ASN1_item_i2d_fp(&X509_REQ_it, fp, req); 159 } 160 LCRYPTO_ALIAS(i2d_X509_REQ_fp); 161 162 #ifndef OPENSSL_NO_RSA 163 RSA * 164 d2i_RSAPrivateKey_bio(BIO *bp, RSA **rsa) 165 { 166 return ASN1_item_d2i_bio(&RSAPrivateKey_it, bp, rsa); 167 } 168 LCRYPTO_ALIAS(d2i_RSAPrivateKey_bio); 169 170 int 171 i2d_RSAPrivateKey_bio(BIO *bp, RSA *rsa) 172 { 173 return ASN1_item_i2d_bio(&RSAPrivateKey_it, bp, rsa); 174 } 175 LCRYPTO_ALIAS(i2d_RSAPrivateKey_bio); 176 177 RSA * 178 d2i_RSAPrivateKey_fp(FILE *fp, RSA **rsa) 179 { 180 return ASN1_item_d2i_fp(&RSAPrivateKey_it, fp, rsa); 181 } 182 LCRYPTO_ALIAS(d2i_RSAPrivateKey_fp); 183 184 int 185 i2d_RSAPrivateKey_fp(FILE *fp, RSA *rsa) 186 { 187 return ASN1_item_i2d_fp(&RSAPrivateKey_it, fp, rsa); 188 } 189 LCRYPTO_ALIAS(i2d_RSAPrivateKey_fp); 190 191 RSA * 192 d2i_RSAPublicKey_bio(BIO *bp, RSA **rsa) 193 { 194 return ASN1_item_d2i_bio(&RSAPublicKey_it, bp, rsa); 195 } 196 LCRYPTO_ALIAS(d2i_RSAPublicKey_bio); 197 198 int 199 i2d_RSAPublicKey_bio(BIO *bp, RSA *rsa) 200 { 201 return ASN1_item_i2d_bio(&RSAPublicKey_it, bp, rsa); 202 } 203 LCRYPTO_ALIAS(i2d_RSAPublicKey_bio); 204 205 RSA * 206 d2i_RSAPublicKey_fp(FILE *fp, RSA **rsa) 207 { 208 return ASN1_item_d2i_fp(&RSAPublicKey_it, fp, rsa); 209 } 210 LCRYPTO_ALIAS(d2i_RSAPublicKey_fp); 211 212 int 213 i2d_RSAPublicKey_fp(FILE *fp, RSA *rsa) 214 { 215 return ASN1_item_i2d_fp(&RSAPublicKey_it, fp, rsa); 216 } 217 LCRYPTO_ALIAS(i2d_RSAPublicKey_fp); 218 #endif 219 220 #ifndef OPENSSL_NO_DSA 221 DSA * 222 d2i_DSAPrivateKey_bio(BIO *bp, DSA **dsa) 223 { 224 return ASN1_item_d2i_bio(&DSAPrivateKey_it, bp, dsa); 225 } 226 LCRYPTO_ALIAS(d2i_DSAPrivateKey_bio); 227 228 int 229 i2d_DSAPrivateKey_bio(BIO *bp, DSA *dsa) 230 { 231 return ASN1_item_i2d_bio(&DSAPrivateKey_it, bp, dsa); 232 } 233 LCRYPTO_ALIAS(i2d_DSAPrivateKey_bio); 234 235 DSA * 236 d2i_DSAPrivateKey_fp(FILE *fp, DSA **dsa) 237 { 238 return ASN1_item_d2i_fp(&DSAPrivateKey_it, fp, dsa); 239 } 240 LCRYPTO_ALIAS(d2i_DSAPrivateKey_fp); 241 242 int 243 i2d_DSAPrivateKey_fp(FILE *fp, DSA *dsa) 244 { 245 return ASN1_item_i2d_fp(&DSAPrivateKey_it, fp, dsa); 246 } 247 LCRYPTO_ALIAS(i2d_DSAPrivateKey_fp); 248 #endif 249 250 #ifndef OPENSSL_NO_EC 251 EC_KEY * 252 d2i_ECPrivateKey_bio(BIO *bp, EC_KEY **eckey) 253 { 254 return ASN1_d2i_bio_of(EC_KEY, EC_KEY_new, d2i_ECPrivateKey, bp, eckey); 255 } 256 LCRYPTO_ALIAS(d2i_ECPrivateKey_bio); 257 258 int 259 i2d_ECPrivateKey_bio(BIO *bp, EC_KEY *eckey) 260 { 261 return ASN1_i2d_bio_of(EC_KEY, i2d_ECPrivateKey, bp, eckey); 262 } 263 LCRYPTO_ALIAS(i2d_ECPrivateKey_bio); 264 265 EC_KEY * 266 d2i_ECPrivateKey_fp(FILE *fp, EC_KEY **eckey) 267 { 268 return ASN1_d2i_fp_of(EC_KEY, EC_KEY_new, d2i_ECPrivateKey, fp, eckey); 269 } 270 LCRYPTO_ALIAS(d2i_ECPrivateKey_fp); 271 272 int 273 i2d_ECPrivateKey_fp(FILE *fp, EC_KEY *eckey) 274 { 275 return ASN1_i2d_fp_of(EC_KEY, i2d_ECPrivateKey, fp, eckey); 276 } 277 LCRYPTO_ALIAS(i2d_ECPrivateKey_fp); 278 #endif 279 280 X509_SIG * 281 d2i_PKCS8_bio(BIO *bp, X509_SIG **p8) 282 { 283 return ASN1_item_d2i_bio(&X509_SIG_it, bp, p8); 284 } 285 LCRYPTO_ALIAS(d2i_PKCS8_bio); 286 287 int 288 i2d_PKCS8_bio(BIO *bp, X509_SIG *p8) 289 { 290 return ASN1_item_i2d_bio(&X509_SIG_it, bp, p8); 291 } 292 LCRYPTO_ALIAS(i2d_PKCS8_bio); 293 294 X509_SIG * 295 d2i_PKCS8_fp(FILE *fp, X509_SIG **p8) 296 { 297 return ASN1_item_d2i_fp(&X509_SIG_it, fp, p8); 298 } 299 LCRYPTO_ALIAS(d2i_PKCS8_fp); 300 301 int 302 i2d_PKCS8_fp(FILE *fp, X509_SIG *p8) 303 { 304 return ASN1_item_i2d_fp(&X509_SIG_it, fp, p8); 305 } 306 LCRYPTO_ALIAS(i2d_PKCS8_fp); 307 308 PKCS8_PRIV_KEY_INFO * 309 d2i_PKCS8_PRIV_KEY_INFO_bio(BIO *bp, PKCS8_PRIV_KEY_INFO **p8inf) 310 { 311 return ASN1_item_d2i_bio(&PKCS8_PRIV_KEY_INFO_it, bp, 312 p8inf); 313 } 314 LCRYPTO_ALIAS(d2i_PKCS8_PRIV_KEY_INFO_bio); 315 316 int 317 i2d_PKCS8_PRIV_KEY_INFO_bio(BIO *bp, PKCS8_PRIV_KEY_INFO *p8inf) 318 { 319 return ASN1_item_i2d_bio(&PKCS8_PRIV_KEY_INFO_it, bp, 320 p8inf); 321 } 322 LCRYPTO_ALIAS(i2d_PKCS8_PRIV_KEY_INFO_bio); 323 324 PKCS8_PRIV_KEY_INFO * 325 d2i_PKCS8_PRIV_KEY_INFO_fp(FILE *fp, PKCS8_PRIV_KEY_INFO **p8inf) 326 { 327 return ASN1_item_d2i_fp(&PKCS8_PRIV_KEY_INFO_it, fp, 328 p8inf); 329 } 330 LCRYPTO_ALIAS(d2i_PKCS8_PRIV_KEY_INFO_fp); 331 332 int 333 i2d_PKCS8_PRIV_KEY_INFO_fp(FILE *fp, PKCS8_PRIV_KEY_INFO *p8inf) 334 { 335 return ASN1_item_i2d_fp(&PKCS8_PRIV_KEY_INFO_it, fp, 336 p8inf); 337 } 338 LCRYPTO_ALIAS(i2d_PKCS8_PRIV_KEY_INFO_fp); 339 340 EVP_PKEY * 341 d2i_PrivateKey_bio(BIO *bp, EVP_PKEY **a) 342 { 343 return ASN1_d2i_bio_of(EVP_PKEY, EVP_PKEY_new, d2i_AutoPrivateKey, 344 bp, a); 345 } 346 LCRYPTO_ALIAS(d2i_PrivateKey_bio); 347 348 int 349 i2d_PrivateKey_bio(BIO *bp, EVP_PKEY *pkey) 350 { 351 return ASN1_i2d_bio_of(EVP_PKEY, i2d_PrivateKey, bp, pkey); 352 } 353 LCRYPTO_ALIAS(i2d_PrivateKey_bio); 354 355 EVP_PKEY * 356 d2i_PrivateKey_fp(FILE *fp, EVP_PKEY **a) 357 { 358 return ASN1_d2i_fp_of(EVP_PKEY, EVP_PKEY_new, d2i_AutoPrivateKey, 359 fp, a); 360 } 361 LCRYPTO_ALIAS(d2i_PrivateKey_fp); 362 363 int 364 i2d_PrivateKey_fp(FILE *fp, EVP_PKEY *pkey) 365 { 366 return ASN1_i2d_fp_of(EVP_PKEY, i2d_PrivateKey, fp, pkey); 367 } 368 LCRYPTO_ALIAS(i2d_PrivateKey_fp); 369 370 int 371 i2d_PKCS8PrivateKeyInfo_bio(BIO *bp, EVP_PKEY *key) 372 { 373 PKCS8_PRIV_KEY_INFO *p8inf; 374 int ret; 375 376 p8inf = EVP_PKEY2PKCS8(key); 377 if (!p8inf) 378 return 0; 379 ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf); 380 PKCS8_PRIV_KEY_INFO_free(p8inf); 381 return ret; 382 } 383 LCRYPTO_ALIAS(i2d_PKCS8PrivateKeyInfo_bio); 384 385 int 386 i2d_PKCS8PrivateKeyInfo_fp(FILE *fp, EVP_PKEY *key) 387 { 388 PKCS8_PRIV_KEY_INFO *p8inf; 389 int ret; 390 p8inf = EVP_PKEY2PKCS8(key); 391 if (!p8inf) 392 return 0; 393 ret = i2d_PKCS8_PRIV_KEY_INFO_fp(fp, p8inf); 394 PKCS8_PRIV_KEY_INFO_free(p8inf); 395 return ret; 396 } 397 LCRYPTO_ALIAS(i2d_PKCS8PrivateKeyInfo_fp); 398 399 int 400 X509_verify(X509 *a, EVP_PKEY *r) 401 { 402 if (X509_ALGOR_cmp(a->sig_alg, a->cert_info->signature)) 403 return 0; 404 return (ASN1_item_verify(&X509_CINF_it, a->sig_alg, 405 a->signature, a->cert_info, r)); 406 } 407 LCRYPTO_ALIAS(X509_verify); 408 409 int 410 X509_REQ_verify(X509_REQ *a, EVP_PKEY *r) 411 { 412 return (ASN1_item_verify(&X509_REQ_INFO_it, 413 a->sig_alg, a->signature, a->req_info, r)); 414 } 415 LCRYPTO_ALIAS(X509_REQ_verify); 416 417 int 418 NETSCAPE_SPKI_verify(NETSCAPE_SPKI *a, EVP_PKEY *r) 419 { 420 return (ASN1_item_verify(&NETSCAPE_SPKAC_it, 421 a->sig_algor, a->signature, a->spkac, r)); 422 } 423 LCRYPTO_ALIAS(NETSCAPE_SPKI_verify); 424 425 int 426 X509_sign(X509 *x, EVP_PKEY *pkey, const EVP_MD *md) 427 { 428 x->cert_info->enc.modified = 1; 429 return (ASN1_item_sign(&X509_CINF_it, 430 x->cert_info->signature, x->sig_alg, x->signature, 431 x->cert_info, pkey, md)); 432 } 433 LCRYPTO_ALIAS(X509_sign); 434 435 int 436 X509_sign_ctx(X509 *x, EVP_MD_CTX *ctx) 437 { 438 x->cert_info->enc.modified = 1; 439 return ASN1_item_sign_ctx(&X509_CINF_it, 440 x->cert_info->signature, x->sig_alg, x->signature, 441 x->cert_info, ctx); 442 } 443 LCRYPTO_ALIAS(X509_sign_ctx); 444 445 int 446 X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md) 447 { 448 return (ASN1_item_sign(&X509_REQ_INFO_it, 449 x->sig_alg, NULL, x->signature, x->req_info, pkey, md)); 450 } 451 LCRYPTO_ALIAS(X509_REQ_sign); 452 453 int 454 X509_REQ_sign_ctx(X509_REQ *x, EVP_MD_CTX *ctx) 455 { 456 return ASN1_item_sign_ctx(&X509_REQ_INFO_it, 457 x->sig_alg, NULL, x->signature, x->req_info, ctx); 458 } 459 LCRYPTO_ALIAS(X509_REQ_sign_ctx); 460 461 int 462 X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md) 463 { 464 x->crl->enc.modified = 1; 465 return(ASN1_item_sign(&X509_CRL_INFO_it, x->crl->sig_alg, 466 x->sig_alg, x->signature, x->crl, pkey, md)); 467 } 468 LCRYPTO_ALIAS(X509_CRL_sign); 469 470 int 471 X509_CRL_sign_ctx(X509_CRL *x, EVP_MD_CTX *ctx) 472 { 473 x->crl->enc.modified = 1; 474 return ASN1_item_sign_ctx(&X509_CRL_INFO_it, 475 x->crl->sig_alg, x->sig_alg, x->signature, x->crl, ctx); 476 } 477 LCRYPTO_ALIAS(X509_CRL_sign_ctx); 478 479 int 480 NETSCAPE_SPKI_sign(NETSCAPE_SPKI *x, EVP_PKEY *pkey, const EVP_MD *md) 481 { 482 return (ASN1_item_sign(&NETSCAPE_SPKAC_it, 483 x->sig_algor, NULL, x->signature, x->spkac, pkey, md)); 484 } 485 LCRYPTO_ALIAS(NETSCAPE_SPKI_sign); 486 487 int 488 X509_pubkey_digest(const X509 *data, const EVP_MD *type, unsigned char *md, 489 unsigned int *len) 490 { 491 ASN1_BIT_STRING *key; 492 key = X509_get0_pubkey_bitstr(data); 493 if (!key) 494 return 0; 495 return EVP_Digest(key->data, key->length, md, len, type, NULL); 496 } 497 LCRYPTO_ALIAS(X509_pubkey_digest); 498 499 int 500 X509_digest(const X509 *data, const EVP_MD *type, unsigned char *md, 501 unsigned int *len) 502 { 503 return (ASN1_item_digest(&X509_it, type, (char *)data, 504 md, len)); 505 } 506 LCRYPTO_ALIAS(X509_digest); 507 508 int 509 X509_CRL_digest(const X509_CRL *data, const EVP_MD *type, unsigned char *md, 510 unsigned int *len) 511 { 512 return (ASN1_item_digest(&X509_CRL_it, type, (char *)data, 513 md, len)); 514 } 515 LCRYPTO_ALIAS(X509_CRL_digest); 516 517 int 518 X509_REQ_digest(const X509_REQ *data, const EVP_MD *type, unsigned char *md, 519 unsigned int *len) 520 { 521 return (ASN1_item_digest(&X509_REQ_it, type, (char *)data, 522 md, len)); 523 } 524 LCRYPTO_ALIAS(X509_REQ_digest); 525 526 int 527 X509_NAME_digest(const X509_NAME *data, const EVP_MD *type, unsigned char *md, 528 unsigned int *len) 529 { 530 return (ASN1_item_digest(&X509_NAME_it, type, (char *)data, 531 md, len)); 532 } 533 LCRYPTO_ALIAS(X509_NAME_digest); 534 535 int 536 X509_up_ref(X509 *x) 537 { 538 return CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509) > 1; 539 } 540 LCRYPTO_ALIAS(X509_up_ref); 541