x_all.c revision 1.25
1/* $OpenBSD: x_all.c,v 1.25 2021/12/03 16:46:50 jsing 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_lcl.h" 77 78X509 * 79d2i_X509_bio(BIO *bp, X509 **x509) 80{ 81 return ASN1_item_d2i_bio(&X509_it, bp, x509); 82} 83 84int 85i2d_X509_bio(BIO *bp, X509 *x509) 86{ 87 return ASN1_item_i2d_bio(&X509_it, bp, x509); 88} 89 90X509 * 91d2i_X509_fp(FILE *fp, X509 **x509) 92{ 93 return ASN1_item_d2i_fp(&X509_it, fp, x509); 94} 95 96int 97i2d_X509_fp(FILE *fp, X509 *x509) 98{ 99 return ASN1_item_i2d_fp(&X509_it, fp, x509); 100} 101 102X509_CRL * 103d2i_X509_CRL_bio(BIO *bp, X509_CRL **crl) 104{ 105 return ASN1_item_d2i_bio(&X509_CRL_it, bp, crl); 106} 107 108int 109i2d_X509_CRL_bio(BIO *bp, X509_CRL *crl) 110{ 111 return ASN1_item_i2d_bio(&X509_CRL_it, bp, crl); 112} 113 114X509_CRL * 115d2i_X509_CRL_fp(FILE *fp, X509_CRL **crl) 116{ 117 return ASN1_item_d2i_fp(&X509_CRL_it, fp, crl); 118} 119 120int 121i2d_X509_CRL_fp(FILE *fp, X509_CRL *crl) 122{ 123 return ASN1_item_i2d_fp(&X509_CRL_it, fp, crl); 124} 125 126PKCS7 * 127d2i_PKCS7_bio(BIO *bp, PKCS7 **p7) 128{ 129 return ASN1_item_d2i_bio(&PKCS7_it, bp, p7); 130} 131 132int 133i2d_PKCS7_bio(BIO *bp, PKCS7 *p7) 134{ 135 return ASN1_item_i2d_bio(&PKCS7_it, bp, p7); 136} 137 138PKCS7 * 139d2i_PKCS7_fp(FILE *fp, PKCS7 **p7) 140{ 141 return ASN1_item_d2i_fp(&PKCS7_it, fp, p7); 142} 143 144int 145i2d_PKCS7_fp(FILE *fp, PKCS7 *p7) 146{ 147 return ASN1_item_i2d_fp(&PKCS7_it, fp, p7); 148} 149 150X509_REQ * 151d2i_X509_REQ_bio(BIO *bp, X509_REQ **req) 152{ 153 return ASN1_item_d2i_bio(&X509_REQ_it, bp, req); 154} 155 156int 157i2d_X509_REQ_bio(BIO *bp, X509_REQ *req) 158{ 159 return ASN1_item_i2d_bio(&X509_REQ_it, bp, req); 160} 161 162X509_REQ * 163d2i_X509_REQ_fp(FILE *fp, X509_REQ **req) 164{ 165 return ASN1_item_d2i_fp(&X509_REQ_it, fp, req); 166} 167 168int 169i2d_X509_REQ_fp(FILE *fp, X509_REQ *req) 170{ 171 return ASN1_item_i2d_fp(&X509_REQ_it, fp, req); 172} 173 174#ifndef OPENSSL_NO_RSA 175RSA * 176d2i_RSAPrivateKey_bio(BIO *bp, RSA **rsa) 177{ 178 return ASN1_item_d2i_bio(&RSAPrivateKey_it, bp, rsa); 179} 180 181int 182i2d_RSAPrivateKey_bio(BIO *bp, RSA *rsa) 183{ 184 return ASN1_item_i2d_bio(&RSAPrivateKey_it, bp, rsa); 185} 186 187RSA * 188d2i_RSAPrivateKey_fp(FILE *fp, RSA **rsa) 189{ 190 return ASN1_item_d2i_fp(&RSAPrivateKey_it, fp, rsa); 191} 192 193int 194i2d_RSAPrivateKey_fp(FILE *fp, RSA *rsa) 195{ 196 return ASN1_item_i2d_fp(&RSAPrivateKey_it, fp, rsa); 197} 198 199RSA * 200d2i_RSAPublicKey_bio(BIO *bp, RSA **rsa) 201{ 202 return ASN1_item_d2i_bio(&RSAPublicKey_it, bp, rsa); 203} 204 205int 206i2d_RSAPublicKey_bio(BIO *bp, RSA *rsa) 207{ 208 return ASN1_item_i2d_bio(&RSAPublicKey_it, bp, rsa); 209} 210 211RSA * 212d2i_RSAPublicKey_fp(FILE *fp, RSA **rsa) 213{ 214 return ASN1_item_d2i_fp(&RSAPublicKey_it, fp, rsa); 215} 216 217int 218i2d_RSAPublicKey_fp(FILE *fp, RSA *rsa) 219{ 220 return ASN1_item_i2d_fp(&RSAPublicKey_it, fp, rsa); 221} 222#endif 223 224#ifndef OPENSSL_NO_DSA 225DSA * 226d2i_DSAPrivateKey_bio(BIO *bp, DSA **dsa) 227{ 228 return ASN1_item_d2i_bio(&DSAPrivateKey_it, bp, dsa); 229} 230 231int 232i2d_DSAPrivateKey_bio(BIO *bp, DSA *dsa) 233{ 234 return ASN1_item_i2d_bio(&DSAPrivateKey_it, bp, dsa); 235} 236 237DSA * 238d2i_DSAPrivateKey_fp(FILE *fp, DSA **dsa) 239{ 240 return ASN1_item_d2i_fp(&DSAPrivateKey_it, fp, dsa); 241} 242 243int 244i2d_DSAPrivateKey_fp(FILE *fp, DSA *dsa) 245{ 246 return ASN1_item_i2d_fp(&DSAPrivateKey_it, fp, dsa); 247} 248#endif 249 250#ifndef OPENSSL_NO_EC 251EC_KEY * 252d2i_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 257int 258i2d_ECPrivateKey_bio(BIO *bp, EC_KEY *eckey) 259{ 260 return ASN1_i2d_bio_of(EC_KEY, i2d_ECPrivateKey, bp, eckey); 261} 262 263EC_KEY * 264d2i_ECPrivateKey_fp(FILE *fp, EC_KEY **eckey) 265{ 266 return ASN1_d2i_fp_of(EC_KEY, EC_KEY_new, d2i_ECPrivateKey, fp, eckey); 267} 268 269int 270i2d_ECPrivateKey_fp(FILE *fp, EC_KEY *eckey) 271{ 272 return ASN1_i2d_fp_of(EC_KEY, i2d_ECPrivateKey, fp, eckey); 273} 274#endif 275 276X509_SIG * 277d2i_PKCS8_bio(BIO *bp, X509_SIG **p8) 278{ 279 return ASN1_item_d2i_bio(&X509_SIG_it, bp, p8); 280} 281 282int 283i2d_PKCS8_bio(BIO *bp, X509_SIG *p8) 284{ 285 return ASN1_item_i2d_bio(&X509_SIG_it, bp, p8); 286} 287 288X509_SIG * 289d2i_PKCS8_fp(FILE *fp, X509_SIG **p8) 290{ 291 return ASN1_item_d2i_fp(&X509_SIG_it, fp, p8); 292} 293 294int 295i2d_PKCS8_fp(FILE *fp, X509_SIG *p8) 296{ 297 return ASN1_item_i2d_fp(&X509_SIG_it, fp, p8); 298} 299 300PKCS8_PRIV_KEY_INFO * 301d2i_PKCS8_PRIV_KEY_INFO_bio(BIO *bp, PKCS8_PRIV_KEY_INFO **p8inf) 302{ 303 return ASN1_item_d2i_bio(&PKCS8_PRIV_KEY_INFO_it, bp, 304 p8inf); 305} 306 307int 308i2d_PKCS8_PRIV_KEY_INFO_bio(BIO *bp, PKCS8_PRIV_KEY_INFO *p8inf) 309{ 310 return ASN1_item_i2d_bio(&PKCS8_PRIV_KEY_INFO_it, bp, 311 p8inf); 312} 313 314PKCS8_PRIV_KEY_INFO * 315d2i_PKCS8_PRIV_KEY_INFO_fp(FILE *fp, PKCS8_PRIV_KEY_INFO **p8inf) 316{ 317 return ASN1_item_d2i_fp(&PKCS8_PRIV_KEY_INFO_it, fp, 318 p8inf); 319} 320 321int 322i2d_PKCS8_PRIV_KEY_INFO_fp(FILE *fp, PKCS8_PRIV_KEY_INFO *p8inf) 323{ 324 return ASN1_item_i2d_fp(&PKCS8_PRIV_KEY_INFO_it, fp, 325 p8inf); 326} 327 328EVP_PKEY * 329d2i_PrivateKey_bio(BIO *bp, EVP_PKEY **a) 330{ 331 return ASN1_d2i_bio_of(EVP_PKEY, EVP_PKEY_new, d2i_AutoPrivateKey, 332 bp, a); 333} 334 335int 336i2d_PrivateKey_bio(BIO *bp, EVP_PKEY *pkey) 337{ 338 return ASN1_i2d_bio_of(EVP_PKEY, i2d_PrivateKey, bp, pkey); 339} 340 341EVP_PKEY * 342d2i_PrivateKey_fp(FILE *fp, EVP_PKEY **a) 343{ 344 return ASN1_d2i_fp_of(EVP_PKEY, EVP_PKEY_new, d2i_AutoPrivateKey, 345 fp, a); 346} 347 348int 349i2d_PrivateKey_fp(FILE *fp, EVP_PKEY *pkey) 350{ 351 return ASN1_i2d_fp_of(EVP_PKEY, i2d_PrivateKey, fp, pkey); 352} 353 354int 355i2d_PKCS8PrivateKeyInfo_bio(BIO *bp, EVP_PKEY *key) 356{ 357 PKCS8_PRIV_KEY_INFO *p8inf; 358 int ret; 359 360 p8inf = EVP_PKEY2PKCS8(key); 361 if (!p8inf) 362 return 0; 363 ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf); 364 PKCS8_PRIV_KEY_INFO_free(p8inf); 365 return ret; 366} 367 368int 369i2d_PKCS8PrivateKeyInfo_fp(FILE *fp, EVP_PKEY *key) 370{ 371 PKCS8_PRIV_KEY_INFO *p8inf; 372 int ret; 373 p8inf = EVP_PKEY2PKCS8(key); 374 if (!p8inf) 375 return 0; 376 ret = i2d_PKCS8_PRIV_KEY_INFO_fp(fp, p8inf); 377 PKCS8_PRIV_KEY_INFO_free(p8inf); 378 return ret; 379} 380 381int 382X509_verify(X509 *a, EVP_PKEY *r) 383{ 384 if (X509_ALGOR_cmp(a->sig_alg, a->cert_info->signature)) 385 return 0; 386 return(ASN1_item_verify(&X509_CINF_it, a->sig_alg, 387 a->signature, a->cert_info, r)); 388} 389 390int 391X509_REQ_verify(X509_REQ *a, EVP_PKEY *r) 392{ 393 return (ASN1_item_verify(&X509_REQ_INFO_it, 394 a->sig_alg, a->signature, a->req_info, r)); 395} 396 397int 398NETSCAPE_SPKI_verify(NETSCAPE_SPKI *a, EVP_PKEY *r) 399{ 400 return (ASN1_item_verify(&NETSCAPE_SPKAC_it, 401 a->sig_algor, a->signature, a->spkac, r)); 402} 403 404int 405X509_sign(X509 *x, EVP_PKEY *pkey, const EVP_MD *md) 406{ 407 x->cert_info->enc.modified = 1; 408 return (ASN1_item_sign(&X509_CINF_it, 409 x->cert_info->signature, x->sig_alg, x->signature, 410 x->cert_info, pkey, md)); 411} 412 413int 414X509_sign_ctx(X509 *x, EVP_MD_CTX *ctx) 415{ 416 x->cert_info->enc.modified = 1; 417 return ASN1_item_sign_ctx(&X509_CINF_it, 418 x->cert_info->signature, x->sig_alg, x->signature, 419 x->cert_info, ctx); 420} 421 422int 423X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md) 424{ 425 return (ASN1_item_sign(&X509_REQ_INFO_it, 426 x->sig_alg, NULL, x->signature, x->req_info, pkey, md)); 427} 428 429int 430X509_REQ_sign_ctx(X509_REQ *x, EVP_MD_CTX *ctx) 431{ 432 return ASN1_item_sign_ctx(&X509_REQ_INFO_it, 433 x->sig_alg, NULL, x->signature, x->req_info, ctx); 434} 435 436int 437X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md) 438{ 439 x->crl->enc.modified = 1; 440 return(ASN1_item_sign(&X509_CRL_INFO_it, x->crl->sig_alg, 441 x->sig_alg, x->signature, x->crl, pkey, md)); 442} 443 444int 445X509_CRL_sign_ctx(X509_CRL *x, EVP_MD_CTX *ctx) 446{ 447 x->crl->enc.modified = 1; 448 return ASN1_item_sign_ctx(&X509_CRL_INFO_it, 449 x->crl->sig_alg, x->sig_alg, x->signature, x->crl, ctx); 450} 451 452int 453NETSCAPE_SPKI_sign(NETSCAPE_SPKI *x, EVP_PKEY *pkey, const EVP_MD *md) 454{ 455 return (ASN1_item_sign(&NETSCAPE_SPKAC_it, 456 x->sig_algor, NULL, x->signature, x->spkac, pkey, md)); 457} 458 459int 460X509_pubkey_digest(const X509 *data, const EVP_MD *type, unsigned char *md, 461 unsigned int *len) 462{ 463 ASN1_BIT_STRING *key; 464 key = X509_get0_pubkey_bitstr(data); 465 if (!key) 466 return 0; 467 return EVP_Digest(key->data, key->length, md, len, type, NULL); 468} 469 470int 471X509_digest(const X509 *data, const EVP_MD *type, unsigned char *md, 472 unsigned int *len) 473{ 474 return (ASN1_item_digest(&X509_it, type, (char *)data, 475 md, len)); 476} 477 478int 479X509_CRL_digest(const X509_CRL *data, const EVP_MD *type, unsigned char *md, 480 unsigned int *len) 481{ 482 return (ASN1_item_digest(&X509_CRL_it, type, (char *)data, 483 md, len)); 484} 485 486int 487X509_REQ_digest(const X509_REQ *data, const EVP_MD *type, unsigned char *md, 488 unsigned int *len) 489{ 490 return (ASN1_item_digest(&X509_REQ_it, type, (char *)data, 491 md, len)); 492} 493 494int 495X509_NAME_digest(const X509_NAME *data, const EVP_MD *type, unsigned char *md, 496 unsigned int *len) 497{ 498 return (ASN1_item_digest(&X509_NAME_it, type, (char *)data, 499 md, len)); 500} 501 502int 503PKCS7_ISSUER_AND_SERIAL_digest(PKCS7_ISSUER_AND_SERIAL *data, 504 const EVP_MD *type, unsigned char *md, unsigned int *len) 505{ 506 return(ASN1_item_digest(&PKCS7_ISSUER_AND_SERIAL_it, type, 507 (char *)data, md, len)); 508} 509 510int 511X509_up_ref(X509 *x) 512{ 513 int i = CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); 514 return i > 1 ? 1 : 0; 515} 516