sl@0: /* apps/x509.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #ifdef OPENSSL_NO_STDIO sl@0: #define APPS_WIN16 sl@0: #endif sl@0: #include "apps.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #ifndef OPENSSL_NO_RSA sl@0: #include sl@0: #endif sl@0: #ifndef OPENSSL_NO_DSA sl@0: #include sl@0: #endif sl@0: sl@0: #undef PROG sl@0: #define PROG x509_main sl@0: sl@0: #undef POSTFIX sl@0: #define POSTFIX ".srl" sl@0: #define DEF_DAYS 30 sl@0: sl@0: static const char *x509_usage[]={ sl@0: "usage: x509 args\n", sl@0: " -inform arg - input format - default PEM (one of DER, NET or PEM)\n", sl@0: " -outform arg - output format - default PEM (one of DER, NET or PEM)\n", sl@0: " -keyform arg - private key format - default PEM\n", sl@0: " -CAform arg - CA format - default PEM\n", sl@0: " -CAkeyform arg - CA key format - default PEM\n", sl@0: " -in arg - input file - default stdin\n", sl@0: " -out arg - output file - default stdout\n", sl@0: " -passin arg - private key password source\n", sl@0: " -serial - print serial number value\n", sl@0: " -subject_hash - print subject hash value\n", sl@0: " -issuer_hash - print issuer hash value\n", sl@0: " -hash - synonym for -subject_hash\n", sl@0: " -subject - print subject DN\n", sl@0: " -issuer - print issuer DN\n", sl@0: " -email - print email address(es)\n", sl@0: " -startdate - notBefore field\n", sl@0: " -enddate - notAfter field\n", sl@0: " -purpose - print out certificate purposes\n", sl@0: " -dates - both Before and After dates\n", sl@0: " -modulus - print the RSA key modulus\n", sl@0: " -pubkey - output the public key\n", sl@0: " -fingerprint - print the certificate fingerprint\n", sl@0: " -alias - output certificate alias\n", sl@0: " -noout - no certificate output\n", sl@0: " -ocspid - print OCSP hash values for the subject name and public key\n", sl@0: " -trustout - output a \"trusted\" certificate\n", sl@0: " -clrtrust - clear all trusted purposes\n", sl@0: " -clrreject - clear all rejected purposes\n", sl@0: " -addtrust arg - trust certificate for a given purpose\n", sl@0: " -addreject arg - reject certificate for a given purpose\n", sl@0: " -setalias arg - set certificate alias\n", sl@0: " -days arg - How long till expiry of a signed certificate - def 30 days\n", sl@0: " -checkend arg - check whether the cert expires in the next arg seconds\n", sl@0: " exit 1 if so, 0 if not\n", sl@0: " -signkey arg - self sign cert with arg\n", sl@0: " -x509toreq - output a certification request object\n", sl@0: " -req - input is a certificate request, sign and output.\n", sl@0: " -CA arg - set the CA certificate, must be PEM format.\n", sl@0: " -CAkey arg - set the CA key, must be PEM format\n", sl@0: " missing, it is assumed to be in the CA file.\n", sl@0: " -CAcreateserial - create serial number file if it does not exist\n", sl@0: " -CAserial arg - serial file\n", sl@0: " -set_serial - serial number to use\n", sl@0: " -text - print the certificate in text form\n", sl@0: " -C - print out C code forms\n", sl@0: " -md2/-md5/-sha1/-mdc2 - digest to use\n", sl@0: " -extfile - configuration file with X509V3 extensions to add\n", sl@0: " -extensions - section from config file with X509V3 extensions to add\n", sl@0: " -clrext - delete extensions before signing and input certificate\n", sl@0: " -nameopt arg - various certificate name options\n", sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: " -engine e - use engine e, possibly a hardware device.\n", sl@0: #endif sl@0: " -certopt arg - various certificate text options\n", sl@0: NULL sl@0: }; sl@0: sl@0: static int MS_CALLBACK callb(int ok, X509_STORE_CTX *ctx); sl@0: static int sign (X509 *x, EVP_PKEY *pkey,int days,int clrext, const EVP_MD *digest, sl@0: CONF *conf, char *section); sl@0: static int x509_certify (X509_STORE *ctx,char *CAfile,const EVP_MD *digest, sl@0: X509 *x,X509 *xca,EVP_PKEY *pkey,char *serial, sl@0: int create,int days, int clrext, CONF *conf, char *section, sl@0: ASN1_INTEGER *sno); sl@0: static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt); sl@0: static int reqfile=0; sl@0: sl@0: sl@0: int MAIN(int, char **); sl@0: sl@0: int MAIN(int argc, char **argv) sl@0: { sl@0: ENGINE *e = NULL; sl@0: int ret=1; sl@0: X509_REQ *req=NULL; sl@0: X509 *x=NULL,*xca=NULL; sl@0: ASN1_OBJECT *objtmp; sl@0: EVP_PKEY *Upkey=NULL,*CApkey=NULL; sl@0: ASN1_INTEGER *sno = NULL; sl@0: int i,num,badops=0; sl@0: BIO *out=NULL; sl@0: BIO *STDout=NULL; sl@0: STACK_OF(ASN1_OBJECT) *trust = NULL, *reject = NULL; sl@0: int informat,outformat,keyformat,CAformat,CAkeyformat; sl@0: char *infile=NULL,*outfile=NULL,*keyfile=NULL,*CAfile=NULL; sl@0: char *CAkeyfile=NULL,*CAserial=NULL; sl@0: char *alias=NULL; sl@0: int text=0,serial=0,subject=0,issuer=0,startdate=0,enddate=0; sl@0: int next_serial=0; sl@0: int subject_hash=0,issuer_hash=0,ocspid=0; sl@0: int noout=0,sign_flag=0,CA_flag=0,CA_createserial=0,email=0; sl@0: int trustout=0,clrtrust=0,clrreject=0,aliasout=0,clrext=0; sl@0: int C=0; sl@0: int x509req=0,days=DEF_DAYS,modulus=0,pubkey=0; sl@0: int pprint = 0; sl@0: const char **pp; sl@0: X509_STORE *ctx=NULL; sl@0: X509_REQ *rq=NULL; sl@0: int fingerprint=0; sl@0: char buf[256]; sl@0: const EVP_MD *md_alg,*digest=EVP_sha1(); sl@0: CONF *extconf = NULL; sl@0: char *extsect = NULL, *extfile = NULL, *passin = NULL, *passargin = NULL; sl@0: int need_rand = 0; sl@0: int checkend=0,checkoffset=0; sl@0: unsigned long nmflag = 0, certflag = 0; sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: char *engine=NULL; sl@0: #endif sl@0: sl@0: reqfile=0; sl@0: sl@0: apps_startup(); sl@0: sl@0: if (bio_err == NULL) sl@0: sl@0: bio_err=BIO_new_fp(stderr,BIO_NOCLOSE); sl@0: if (!load_config(bio_err, NULL)) sl@0: goto end; sl@0: STDout=BIO_new_fp(stdout,BIO_NOCLOSE); sl@0: #ifdef OPENSSL_SYS_VMS sl@0: { sl@0: BIO *tmpbio = BIO_new(BIO_f_linebuffer()); sl@0: STDout = BIO_push(tmpbio, STDout); sl@0: } sl@0: #endif sl@0: sl@0: informat=FORMAT_PEM; sl@0: outformat=FORMAT_PEM; sl@0: keyformat=FORMAT_PEM; sl@0: CAformat=FORMAT_PEM; sl@0: CAkeyformat=FORMAT_PEM; sl@0: sl@0: ctx=X509_STORE_new(); sl@0: if (ctx == NULL) goto end; sl@0: X509_STORE_set_verify_cb_func(ctx,callb); sl@0: sl@0: argc--; sl@0: argv++; sl@0: num=0; sl@0: while (argc >= 1) sl@0: { sl@0: if (strcmp(*argv,"-inform") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: informat=str2fmt(*(++argv)); sl@0: } sl@0: else if (strcmp(*argv,"-outform") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: outformat=str2fmt(*(++argv)); sl@0: } sl@0: else if (strcmp(*argv,"-keyform") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: keyformat=str2fmt(*(++argv)); sl@0: } sl@0: else if (strcmp(*argv,"-req") == 0) sl@0: { sl@0: reqfile=1; sl@0: need_rand = 1; sl@0: } sl@0: else if (strcmp(*argv,"-CAform") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: CAformat=str2fmt(*(++argv)); sl@0: } sl@0: else if (strcmp(*argv,"-CAkeyform") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: CAkeyformat=str2fmt(*(++argv)); sl@0: } sl@0: else if (strcmp(*argv,"-days") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: days=atoi(*(++argv)); sl@0: if (days == 0) sl@0: { sl@0: BIO_printf(STDout,"bad number of days\n"); sl@0: goto bad; sl@0: } sl@0: } sl@0: else if (strcmp(*argv,"-passin") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: passargin= *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-extfile") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: extfile= *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-extensions") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: extsect= *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-in") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: infile= *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-out") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: outfile= *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-signkey") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: keyfile= *(++argv); sl@0: sign_flag= ++num; sl@0: need_rand = 1; sl@0: } sl@0: else if (strcmp(*argv,"-CA") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: CAfile= *(++argv); sl@0: CA_flag= ++num; sl@0: need_rand = 1; sl@0: } sl@0: else if (strcmp(*argv,"-CAkey") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: CAkeyfile= *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-CAserial") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: CAserial= *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-set_serial") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: if (!(sno = s2i_ASN1_INTEGER(NULL, *(++argv)))) sl@0: goto bad; sl@0: } sl@0: else if (strcmp(*argv,"-addtrust") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: if (!(objtmp = OBJ_txt2obj(*(++argv), 0))) sl@0: { sl@0: BIO_printf(bio_err, sl@0: "Invalid trust object value %s\n", *argv); sl@0: goto bad; sl@0: } sl@0: if (!trust) trust = sk_ASN1_OBJECT_new_null(); sl@0: sk_ASN1_OBJECT_push(trust, objtmp); sl@0: trustout = 1; sl@0: } sl@0: else if (strcmp(*argv,"-addreject") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: if (!(objtmp = OBJ_txt2obj(*(++argv), 0))) sl@0: { sl@0: BIO_printf(bio_err, sl@0: "Invalid reject object value %s\n", *argv); sl@0: goto bad; sl@0: } sl@0: if (!reject) reject = sk_ASN1_OBJECT_new_null(); sl@0: sk_ASN1_OBJECT_push(reject, objtmp); sl@0: trustout = 1; sl@0: } sl@0: else if (strcmp(*argv,"-setalias") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: alias= *(++argv); sl@0: trustout = 1; sl@0: } sl@0: else if (strcmp(*argv,"-certopt") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: if (!set_cert_ex(&certflag, *(++argv))) goto bad; sl@0: } sl@0: else if (strcmp(*argv,"-nameopt") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: if (!set_name_ex(&nmflag, *(++argv))) goto bad; sl@0: } sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: else if (strcmp(*argv,"-engine") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: engine= *(++argv); sl@0: } sl@0: #endif sl@0: else if (strcmp(*argv,"-C") == 0) sl@0: C= ++num; sl@0: else if (strcmp(*argv,"-email") == 0) sl@0: email= ++num; sl@0: else if (strcmp(*argv,"-serial") == 0) sl@0: serial= ++num; sl@0: else if (strcmp(*argv,"-next_serial") == 0) sl@0: next_serial= ++num; sl@0: else if (strcmp(*argv,"-modulus") == 0) sl@0: modulus= ++num; sl@0: else if (strcmp(*argv,"-pubkey") == 0) sl@0: pubkey= ++num; sl@0: else if (strcmp(*argv,"-x509toreq") == 0) sl@0: x509req= ++num; sl@0: else if (strcmp(*argv,"-text") == 0) sl@0: text= ++num; sl@0: else if (strcmp(*argv,"-hash") == 0 sl@0: || strcmp(*argv,"-subject_hash") == 0) sl@0: subject_hash= ++num; sl@0: else if (strcmp(*argv,"-issuer_hash") == 0) sl@0: issuer_hash= ++num; sl@0: else if (strcmp(*argv,"-subject") == 0) sl@0: subject= ++num; sl@0: else if (strcmp(*argv,"-issuer") == 0) sl@0: issuer= ++num; sl@0: else if (strcmp(*argv,"-fingerprint") == 0) sl@0: fingerprint= ++num; sl@0: else if (strcmp(*argv,"-dates") == 0) sl@0: { sl@0: startdate= ++num; sl@0: enddate= ++num; sl@0: } sl@0: else if (strcmp(*argv,"-purpose") == 0) sl@0: pprint= ++num; sl@0: else if (strcmp(*argv,"-startdate") == 0) sl@0: startdate= ++num; sl@0: else if (strcmp(*argv,"-enddate") == 0) sl@0: enddate= ++num; sl@0: else if (strcmp(*argv,"-checkend") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: checkoffset=atoi(*(++argv)); sl@0: checkend=1; sl@0: } sl@0: else if (strcmp(*argv,"-noout") == 0) sl@0: noout= ++num; sl@0: else if (strcmp(*argv,"-trustout") == 0) sl@0: trustout= 1; sl@0: else if (strcmp(*argv,"-clrtrust") == 0) sl@0: clrtrust= ++num; sl@0: else if (strcmp(*argv,"-clrreject") == 0) sl@0: clrreject= ++num; sl@0: else if (strcmp(*argv,"-alias") == 0) sl@0: aliasout= ++num; sl@0: else if (strcmp(*argv,"-CAcreateserial") == 0) sl@0: CA_createserial= ++num; sl@0: else if (strcmp(*argv,"-clrext") == 0) sl@0: clrext = 1; sl@0: #if 1 /* stay backwards-compatible with 0.9.5; this should go away soon */ sl@0: else if (strcmp(*argv,"-crlext") == 0) sl@0: { sl@0: BIO_printf(bio_err,"use -clrext instead of -crlext\n"); sl@0: clrext = 1; sl@0: } sl@0: #endif sl@0: else if (strcmp(*argv,"-ocspid") == 0) sl@0: ocspid= ++num; sl@0: else if ((md_alg=EVP_get_digestbyname(*argv + 1))) sl@0: { sl@0: /* ok */ sl@0: digest=md_alg; sl@0: } sl@0: else sl@0: { sl@0: BIO_printf(bio_err,"unknown option %s\n",*argv); sl@0: badops=1; sl@0: break; sl@0: } sl@0: argc--; sl@0: argv++; sl@0: } sl@0: sl@0: if (badops) sl@0: { sl@0: bad: sl@0: for (pp=x509_usage; (*pp != NULL); pp++) sl@0: BIO_printf(bio_err,"%s",*pp); sl@0: goto end; sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: e = setup_engine(bio_err, engine, 0); sl@0: #endif sl@0: sl@0: if (need_rand) sl@0: app_RAND_load_file(NULL, bio_err, 0); sl@0: sl@0: ERR_load_crypto_strings(); sl@0: sl@0: if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) sl@0: { sl@0: BIO_printf(bio_err, "Error getting password\n"); sl@0: goto end; sl@0: } sl@0: sl@0: if (!X509_STORE_set_default_paths(ctx)) sl@0: { sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: sl@0: if ((CAkeyfile == NULL) && (CA_flag) && (CAformat == FORMAT_PEM)) sl@0: { CAkeyfile=CAfile; } sl@0: else if ((CA_flag) && (CAkeyfile == NULL)) sl@0: { sl@0: BIO_printf(bio_err,"need to specify a CAkey if using the CA command\n"); sl@0: goto end; sl@0: } sl@0: sl@0: if (extfile) sl@0: { sl@0: long errorline = -1; sl@0: X509V3_CTX ctx2; sl@0: extconf = NCONF_new(NULL); sl@0: if (!NCONF_load(extconf, extfile,&errorline)) sl@0: { sl@0: if (errorline <= 0) sl@0: BIO_printf(bio_err, sl@0: "error loading the config file '%s'\n", sl@0: extfile); sl@0: else sl@0: BIO_printf(bio_err, sl@0: "error on line %ld of config file '%s'\n" sl@0: ,errorline,extfile); sl@0: goto end; sl@0: } sl@0: if (!extsect) sl@0: { sl@0: extsect = NCONF_get_string(extconf, "default", "extensions"); sl@0: if (!extsect) sl@0: { sl@0: ERR_clear_error(); sl@0: extsect = "default"; sl@0: } sl@0: } sl@0: X509V3_set_ctx_test(&ctx2); sl@0: X509V3_set_nconf(&ctx2, extconf); sl@0: if (!X509V3_EXT_add_nconf(extconf, &ctx2, extsect, NULL)) sl@0: { sl@0: BIO_printf(bio_err, sl@0: "Error Loading extension section %s\n", sl@0: extsect); sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: } sl@0: sl@0: sl@0: if (reqfile) sl@0: { sl@0: EVP_PKEY *pkey; sl@0: X509_CINF *ci; sl@0: BIO *in; sl@0: sl@0: if (!sign_flag && !CA_flag) sl@0: { sl@0: BIO_printf(bio_err,"We need a private key to sign with\n"); sl@0: goto end; sl@0: } sl@0: in=BIO_new(BIO_s_file()); sl@0: if (in == NULL) sl@0: { sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: sl@0: if (infile == NULL) sl@0: BIO_set_fp(in,stdin,BIO_NOCLOSE|BIO_FP_TEXT); sl@0: else sl@0: { sl@0: if (BIO_read_filename(in,infile) <= 0) sl@0: { sl@0: perror(infile); sl@0: BIO_free(in); sl@0: goto end; sl@0: } sl@0: } sl@0: req=PEM_read_bio_X509_REQ(in,NULL,NULL,NULL); sl@0: BIO_free(in); sl@0: sl@0: if (req == NULL) sl@0: { sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: sl@0: if ( (req->req_info == NULL) || sl@0: (req->req_info->pubkey == NULL) || sl@0: (req->req_info->pubkey->public_key == NULL) || sl@0: (req->req_info->pubkey->public_key->data == NULL)) sl@0: { sl@0: BIO_printf(bio_err,"The certificate request appears to corrupted\n"); sl@0: BIO_printf(bio_err,"It does not contain a public key\n"); sl@0: goto end; sl@0: } sl@0: if ((pkey=X509_REQ_get_pubkey(req)) == NULL) sl@0: { sl@0: BIO_printf(bio_err,"error unpacking public key\n"); sl@0: goto end; sl@0: } sl@0: i=X509_REQ_verify(req,pkey); sl@0: EVP_PKEY_free(pkey); sl@0: if (i < 0) sl@0: { sl@0: BIO_printf(bio_err,"Signature verification error\n"); sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: if (i == 0) sl@0: { sl@0: BIO_printf(bio_err,"Signature did not match the certificate request\n"); sl@0: goto end; sl@0: } sl@0: else sl@0: BIO_printf(bio_err,"Signature ok\n"); sl@0: sl@0: print_name(bio_err, "subject=", X509_REQ_get_subject_name(req), nmflag); sl@0: sl@0: if ((x=X509_new()) == NULL) goto end; sl@0: ci=x->cert_info; sl@0: sl@0: if (sno == NULL) sl@0: { sl@0: sno = ASN1_INTEGER_new(); sl@0: if (!sno || !rand_serial(NULL, sno)) sl@0: goto end; sl@0: if (!X509_set_serialNumber(x, sno)) sl@0: goto end; sl@0: ASN1_INTEGER_free(sno); sl@0: sno = NULL; sl@0: } sl@0: else if (!X509_set_serialNumber(x, sno)) sl@0: goto end; sl@0: sl@0: if (!X509_set_issuer_name(x,req->req_info->subject)) goto end; sl@0: if (!X509_set_subject_name(x,req->req_info->subject)) goto end; sl@0: sl@0: X509_gmtime_adj(X509_get_notBefore(x),0); sl@0: X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days); sl@0: sl@0: pkey = X509_REQ_get_pubkey(req); sl@0: X509_set_pubkey(x,pkey); sl@0: EVP_PKEY_free(pkey); sl@0: } sl@0: else sl@0: x=load_cert(bio_err,infile,informat,NULL,e,"Certificate"); sl@0: sl@0: if (x == NULL) goto end; sl@0: if (CA_flag) sl@0: { sl@0: xca=load_cert(bio_err,CAfile,CAformat,NULL,e,"CA Certificate"); sl@0: if (xca == NULL) goto end; sl@0: } sl@0: sl@0: if (!noout || text || next_serial) sl@0: { sl@0: OBJ_create("2.99999.3", sl@0: "SET.ex3","SET x509v3 extension 3"); sl@0: sl@0: out=BIO_new(BIO_s_file()); sl@0: if (out == NULL) sl@0: { sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: if (outfile == NULL) sl@0: { sl@0: BIO_set_fp(out,stdout,BIO_NOCLOSE); sl@0: #ifdef OPENSSL_SYS_VMS sl@0: { sl@0: BIO *tmpbio = BIO_new(BIO_f_linebuffer()); sl@0: out = BIO_push(tmpbio, out); sl@0: } sl@0: #endif sl@0: } sl@0: else sl@0: { sl@0: if (BIO_write_filename(out,outfile) <= 0) sl@0: { sl@0: perror(outfile); sl@0: goto end; sl@0: } sl@0: } sl@0: } sl@0: sl@0: if (alias) X509_alias_set1(x, (unsigned char *)alias, -1); sl@0: sl@0: if (clrtrust) X509_trust_clear(x); sl@0: if (clrreject) X509_reject_clear(x); sl@0: sl@0: if (trust) sl@0: { sl@0: for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++) sl@0: { sl@0: objtmp = sk_ASN1_OBJECT_value(trust, i); sl@0: X509_add1_trust_object(x, objtmp); sl@0: } sl@0: } sl@0: sl@0: if (reject) sl@0: { sl@0: for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++) sl@0: { sl@0: objtmp = sk_ASN1_OBJECT_value(reject, i); sl@0: X509_add1_reject_object(x, objtmp); sl@0: } sl@0: } sl@0: sl@0: if (num) sl@0: { sl@0: for (i=1; i<=num; i++) sl@0: { sl@0: if (issuer == i) sl@0: { sl@0: print_name(STDout, "issuer= ", sl@0: X509_get_issuer_name(x), nmflag); sl@0: } sl@0: else if (subject == i) sl@0: { sl@0: print_name(STDout, "subject= ", sl@0: X509_get_subject_name(x), nmflag); sl@0: } sl@0: else if (serial == i) sl@0: { sl@0: BIO_printf(STDout,"serial="); sl@0: i2a_ASN1_INTEGER(STDout, sl@0: X509_get_serialNumber(x)); sl@0: BIO_printf(STDout,"\n"); sl@0: } sl@0: else if (next_serial == i) sl@0: { sl@0: BIGNUM *bnser; sl@0: ASN1_INTEGER *ser; sl@0: ser = X509_get_serialNumber(x); sl@0: bnser = ASN1_INTEGER_to_BN(ser, NULL); sl@0: if (!bnser) sl@0: goto end; sl@0: if (!BN_add_word(bnser, 1)) sl@0: goto end; sl@0: ser = BN_to_ASN1_INTEGER(bnser, NULL); sl@0: if (!ser) sl@0: goto end; sl@0: BN_free(bnser); sl@0: i2a_ASN1_INTEGER(out, ser); sl@0: ASN1_INTEGER_free(ser); sl@0: BIO_puts(out, "\n"); sl@0: } sl@0: else if (email == i) sl@0: { sl@0: int j; sl@0: STACK *emlst; sl@0: emlst = X509_get1_email(x); sl@0: for (j = 0; j < sk_num(emlst); j++) sl@0: BIO_printf(STDout, "%s\n", sk_value(emlst, j)); sl@0: X509_email_free(emlst); sl@0: } sl@0: else if (aliasout == i) sl@0: { sl@0: unsigned char *alstr; sl@0: alstr = X509_alias_get0(x, NULL); sl@0: if (alstr) BIO_printf(STDout,"%s\n", alstr); sl@0: else BIO_puts(STDout,"\n"); sl@0: } sl@0: else if (subject_hash == i) sl@0: { sl@0: BIO_printf(STDout,"%08lx\n",X509_subject_name_hash(x)); sl@0: } sl@0: else if (issuer_hash == i) sl@0: { sl@0: BIO_printf(STDout,"%08lx\n",X509_issuer_name_hash(x)); sl@0: } sl@0: else if (pprint == i) sl@0: { sl@0: X509_PURPOSE *ptmp; sl@0: int j; sl@0: BIO_printf(STDout, "Certificate purposes:\n"); sl@0: for (j = 0; j < X509_PURPOSE_get_count(); j++) sl@0: { sl@0: ptmp = X509_PURPOSE_get0(j); sl@0: purpose_print(STDout, x, ptmp); sl@0: } sl@0: } sl@0: else sl@0: if (modulus == i) sl@0: { sl@0: EVP_PKEY *pkey; sl@0: sl@0: pkey=X509_get_pubkey(x); sl@0: if (pkey == NULL) sl@0: { sl@0: BIO_printf(bio_err,"Modulus=unavailable\n"); sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: BIO_printf(STDout,"Modulus="); sl@0: #ifndef OPENSSL_NO_RSA sl@0: if (pkey->type == EVP_PKEY_RSA) sl@0: BN_print(STDout,pkey->pkey.rsa->n); sl@0: else sl@0: #endif sl@0: #ifndef OPENSSL_NO_DSA sl@0: if (pkey->type == EVP_PKEY_DSA) sl@0: BN_print(STDout,pkey->pkey.dsa->pub_key); sl@0: else sl@0: #endif sl@0: BIO_printf(STDout,"Wrong Algorithm type"); sl@0: BIO_printf(STDout,"\n"); sl@0: EVP_PKEY_free(pkey); sl@0: } sl@0: else sl@0: if (pubkey == i) sl@0: { sl@0: EVP_PKEY *pkey; sl@0: sl@0: pkey=X509_get_pubkey(x); sl@0: if (pkey == NULL) sl@0: { sl@0: BIO_printf(bio_err,"Error getting public key\n"); sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: PEM_write_bio_PUBKEY(STDout, pkey); sl@0: EVP_PKEY_free(pkey); sl@0: } sl@0: else sl@0: if (C == i) sl@0: { sl@0: unsigned char *d; sl@0: char *m; sl@0: int y,z; sl@0: sl@0: X509_NAME_oneline(X509_get_subject_name(x), sl@0: buf,sizeof buf); sl@0: BIO_printf(STDout,"/* subject:%s */\n",buf); sl@0: m=X509_NAME_oneline( sl@0: X509_get_issuer_name(x),buf, sl@0: sizeof buf); sl@0: BIO_printf(STDout,"/* issuer :%s */\n",buf); sl@0: sl@0: z=i2d_X509(x,NULL); sl@0: m=OPENSSL_malloc(z); sl@0: sl@0: d=(unsigned char *)m; sl@0: z=i2d_X509_NAME(X509_get_subject_name(x),&d); sl@0: BIO_printf(STDout,"unsigned char XXX_subject_name[%d]={\n",z); sl@0: d=(unsigned char *)m; sl@0: for (y=0; ytype == EVP_PKEY_DSA) sl@0: digest=EVP_dss1(); sl@0: #endif sl@0: #ifndef OPENSSL_NO_ECDSA sl@0: if (Upkey->type == EVP_PKEY_EC) sl@0: digest=EVP_ecdsa(); sl@0: #endif sl@0: sl@0: assert(need_rand); sl@0: if (!sign(x,Upkey,days,clrext,digest, sl@0: extconf, extsect)) goto end; sl@0: } sl@0: else if (CA_flag == i) sl@0: { sl@0: BIO_printf(bio_err,"Getting CA Private Key\n"); sl@0: if (CAkeyfile != NULL) sl@0: { sl@0: CApkey=load_key(bio_err, sl@0: CAkeyfile, CAkeyformat, sl@0: 0, passin, e, sl@0: "CA Private Key"); sl@0: if (CApkey == NULL) goto end; sl@0: } sl@0: #ifndef OPENSSL_NO_DSA sl@0: if (CApkey->type == EVP_PKEY_DSA) sl@0: digest=EVP_dss1(); sl@0: #endif sl@0: #ifndef OPENSSL_NO_ECDSA sl@0: if (CApkey->type == EVP_PKEY_EC) sl@0: digest = EVP_ecdsa(); sl@0: #endif sl@0: sl@0: assert(need_rand); sl@0: if (!x509_certify(ctx,CAfile,digest,x,xca, sl@0: CApkey, CAserial,CA_createserial,days, clrext, sl@0: extconf, extsect, sno)) sl@0: goto end; sl@0: } sl@0: else if (x509req == i) sl@0: { sl@0: EVP_PKEY *pk; sl@0: sl@0: BIO_printf(bio_err,"Getting request Private Key\n"); sl@0: if (keyfile == NULL) sl@0: { sl@0: BIO_printf(bio_err,"no request key file specified\n"); sl@0: goto end; sl@0: } sl@0: else sl@0: { sl@0: pk=load_key(bio_err, sl@0: keyfile, FORMAT_PEM, 0, sl@0: passin, e, "request key"); sl@0: if (pk == NULL) goto end; sl@0: } sl@0: sl@0: BIO_printf(bio_err,"Generating certificate request\n"); sl@0: sl@0: #ifndef OPENSSL_NO_DSA sl@0: if (pk->type == EVP_PKEY_DSA) sl@0: digest=EVP_dss1(); sl@0: #endif sl@0: #ifndef OPENSSL_NO_ECDSA sl@0: if (pk->type == EVP_PKEY_EC) sl@0: digest=EVP_ecdsa(); sl@0: #endif sl@0: sl@0: rq=X509_to_X509_REQ(x,pk,digest); sl@0: EVP_PKEY_free(pk); sl@0: if (rq == NULL) sl@0: { sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: if (!noout) sl@0: { sl@0: X509_REQ_print(out,rq); sl@0: PEM_write_bio_X509_REQ(out,rq); sl@0: } sl@0: noout=1; sl@0: } sl@0: else if (ocspid == i) sl@0: { sl@0: X509_ocspid_print(out, x); sl@0: } sl@0: } sl@0: } sl@0: sl@0: if (checkend) sl@0: { sl@0: time_t tcheck=time(NULL) + checkoffset; sl@0: sl@0: if (X509_cmp_time(X509_get_notAfter(x), &tcheck) < 0) sl@0: { sl@0: BIO_printf(out,"Certificate will expire\n"); sl@0: ret=1; sl@0: } sl@0: else sl@0: { sl@0: BIO_printf(out,"Certificate will not expire\n"); sl@0: ret=0; sl@0: } sl@0: goto end; sl@0: } sl@0: sl@0: if (noout) sl@0: { sl@0: ret=0; sl@0: goto end; sl@0: } sl@0: sl@0: if (outformat == FORMAT_ASN1) sl@0: i=i2d_X509_bio(out,x); sl@0: else if (outformat == FORMAT_PEM) sl@0: { sl@0: if (trustout) i=PEM_write_bio_X509_AUX(out,x); sl@0: else i=PEM_write_bio_X509(out,x); sl@0: } sl@0: else if (outformat == FORMAT_NETSCAPE) sl@0: { sl@0: ASN1_HEADER ah; sl@0: ASN1_OCTET_STRING os; sl@0: sl@0: os.data=(unsigned char *)NETSCAPE_CERT_HDR; sl@0: os.length=strlen(NETSCAPE_CERT_HDR); sl@0: ah.header= &os; sl@0: ah.data=(char *)x; sl@0: ah.meth=X509_asn1_meth(); sl@0: sl@0: i=ASN1_i2d_bio_of(ASN1_HEADER,i2d_ASN1_HEADER,out,&ah); sl@0: } sl@0: else { sl@0: BIO_printf(bio_err,"bad output format specified for outfile\n"); sl@0: goto end; sl@0: } sl@0: if (!i) sl@0: { sl@0: BIO_printf(bio_err,"unable to write certificate\n"); sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: ret=0; sl@0: end: sl@0: if (need_rand) sl@0: app_RAND_write_file(NULL, bio_err); sl@0: OBJ_cleanup(); sl@0: NCONF_free(extconf); sl@0: BIO_free_all(out); sl@0: BIO_free_all(STDout); sl@0: X509_STORE_free(ctx); sl@0: X509_REQ_free(req); sl@0: X509_free(x); sl@0: X509_free(xca); sl@0: EVP_PKEY_free(Upkey); sl@0: EVP_PKEY_free(CApkey); sl@0: X509_REQ_free(rq); sl@0: ASN1_INTEGER_free(sno); sl@0: sk_ASN1_OBJECT_pop_free(trust, ASN1_OBJECT_free); sl@0: sk_ASN1_OBJECT_pop_free(reject, ASN1_OBJECT_free); sl@0: if (passin) OPENSSL_free(passin); sl@0: apps_shutdown(); sl@0: OPENSSL_EXIT(ret); sl@0: } sl@0: sl@0: static ASN1_INTEGER *x509_load_serial(char *CAfile, char *serialfile, int create) sl@0: { sl@0: char *buf = NULL, *p; sl@0: ASN1_INTEGER *bs = NULL; sl@0: BIGNUM *serial = NULL; sl@0: size_t len; sl@0: sl@0: len = ((serialfile == NULL) sl@0: ?(strlen(CAfile)+strlen(POSTFIX)+1) sl@0: :(strlen(serialfile)))+1; sl@0: buf=OPENSSL_malloc(len); sl@0: if (buf == NULL) { BIO_printf(bio_err,"out of mem\n"); goto end; } sl@0: if (serialfile == NULL) sl@0: { sl@0: BUF_strlcpy(buf,CAfile,len); sl@0: for (p=buf; *p; p++) sl@0: if (*p == '.') sl@0: { sl@0: *p='\0'; sl@0: break; sl@0: } sl@0: BUF_strlcat(buf,POSTFIX,len); sl@0: } sl@0: else sl@0: BUF_strlcpy(buf,serialfile,len); sl@0: sl@0: serial = load_serial(buf, create, NULL); sl@0: if (serial == NULL) goto end; sl@0: sl@0: if (!BN_add_word(serial,1)) sl@0: { BIO_printf(bio_err,"add_word failure\n"); goto end; } sl@0: sl@0: if (!save_serial(buf, NULL, serial, &bs)) goto end; sl@0: sl@0: end: sl@0: if (buf) OPENSSL_free(buf); sl@0: BN_free(serial); sl@0: return bs; sl@0: } sl@0: sl@0: static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest, sl@0: X509 *x, X509 *xca, EVP_PKEY *pkey, char *serialfile, int create, sl@0: int days, int clrext, CONF *conf, char *section, ASN1_INTEGER *sno) sl@0: { sl@0: int ret=0; sl@0: ASN1_INTEGER *bs=NULL; sl@0: X509_STORE_CTX xsc; sl@0: EVP_PKEY *upkey; sl@0: sl@0: upkey = X509_get_pubkey(xca); sl@0: EVP_PKEY_copy_parameters(upkey,pkey); sl@0: EVP_PKEY_free(upkey); sl@0: sl@0: if(!X509_STORE_CTX_init(&xsc,ctx,x,NULL)) sl@0: { sl@0: BIO_printf(bio_err,"Error initialising X509 store\n"); sl@0: goto end; sl@0: } sl@0: if (sno) bs = sno; sl@0: else if (!(bs = x509_load_serial(CAfile, serialfile, create))) sl@0: goto end; sl@0: sl@0: /* if (!X509_STORE_add_cert(ctx,x)) goto end;*/ sl@0: sl@0: /* NOTE: this certificate can/should be self signed, unless it was sl@0: * a certificate request in which case it is not. */ sl@0: X509_STORE_CTX_set_cert(&xsc,x); sl@0: if (!reqfile && !X509_verify_cert(&xsc)) sl@0: goto end; sl@0: sl@0: if (!X509_check_private_key(xca,pkey)) sl@0: { sl@0: BIO_printf(bio_err,"CA certificate and CA private key do not match\n"); sl@0: goto end; sl@0: } sl@0: sl@0: if (!X509_set_issuer_name(x,X509_get_subject_name(xca))) goto end; sl@0: if (!X509_set_serialNumber(x,bs)) goto end; sl@0: sl@0: if (X509_gmtime_adj(X509_get_notBefore(x),0L) == NULL) sl@0: goto end; sl@0: sl@0: /* hardwired expired */ sl@0: if (X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days) == NULL) sl@0: goto end; sl@0: sl@0: if (clrext) sl@0: { sl@0: while (X509_get_ext_count(x) > 0) X509_delete_ext(x, 0); sl@0: } sl@0: sl@0: if (conf) sl@0: { sl@0: X509V3_CTX ctx2; sl@0: X509_set_version(x,2); /* version 3 certificate */ sl@0: X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0); sl@0: X509V3_set_nconf(&ctx2, conf); sl@0: if (!X509V3_EXT_add_nconf(conf, &ctx2, section, x)) goto end; sl@0: } sl@0: sl@0: if (!X509_sign(x,pkey,digest)) goto end; sl@0: ret=1; sl@0: end: sl@0: X509_STORE_CTX_cleanup(&xsc); sl@0: if (!ret) sl@0: ERR_print_errors(bio_err); sl@0: if (!sno) ASN1_INTEGER_free(bs); sl@0: return ret; sl@0: } sl@0: sl@0: static int MS_CALLBACK callb(int ok, X509_STORE_CTX *ctx) sl@0: { sl@0: int err; sl@0: X509 *err_cert; sl@0: sl@0: /* it is ok to use a self signed certificate sl@0: * This case will catch both the initial ok == 0 and the sl@0: * final ok == 1 calls to this function */ sl@0: err=X509_STORE_CTX_get_error(ctx); sl@0: if (err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) sl@0: return 1; sl@0: sl@0: /* BAD we should have gotten an error. Normally if everything sl@0: * worked X509_STORE_CTX_get_error(ctx) will still be set to sl@0: * DEPTH_ZERO_SELF_.... */ sl@0: if (ok) sl@0: { sl@0: BIO_printf(bio_err,"error with certificate to be certified - should be self signed\n"); sl@0: return 0; sl@0: } sl@0: else sl@0: { sl@0: err_cert=X509_STORE_CTX_get_current_cert(ctx); sl@0: print_name(bio_err, NULL, X509_get_subject_name(err_cert),0); sl@0: BIO_printf(bio_err,"error with certificate - error %d at depth %d\n%s\n", sl@0: err,X509_STORE_CTX_get_error_depth(ctx), sl@0: X509_verify_cert_error_string(err)); sl@0: return 1; sl@0: } sl@0: } sl@0: sl@0: /* self sign */ sl@0: static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext, const EVP_MD *digest, sl@0: CONF *conf, char *section) sl@0: { sl@0: sl@0: EVP_PKEY *pktmp; sl@0: sl@0: pktmp = X509_get_pubkey(x); sl@0: EVP_PKEY_copy_parameters(pktmp,pkey); sl@0: EVP_PKEY_save_parameters(pktmp,1); sl@0: EVP_PKEY_free(pktmp); sl@0: sl@0: if (!X509_set_issuer_name(x,X509_get_subject_name(x))) goto err; sl@0: if (X509_gmtime_adj(X509_get_notBefore(x),0) == NULL) goto err; sl@0: sl@0: /* Lets just make it 12:00am GMT, Jan 1 1970 */ sl@0: /* memcpy(x->cert_info->validity->notBefore,"700101120000Z",13); */ sl@0: /* 28 days to be certified */ sl@0: sl@0: if (X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days) == NULL) sl@0: goto err; sl@0: sl@0: if (!X509_set_pubkey(x,pkey)) goto err; sl@0: if (clrext) sl@0: { sl@0: while (X509_get_ext_count(x) > 0) X509_delete_ext(x, 0); sl@0: } sl@0: if (conf) sl@0: { sl@0: X509V3_CTX ctx; sl@0: X509_set_version(x,2); /* version 3 certificate */ sl@0: X509V3_set_ctx(&ctx, x, x, NULL, NULL, 0); sl@0: X509V3_set_nconf(&ctx, conf); sl@0: if (!X509V3_EXT_add_nconf(conf, &ctx, section, x)) goto err; sl@0: } sl@0: if (!X509_sign(x,pkey,digest)) goto err; sl@0: return 1; sl@0: err: sl@0: ERR_print_errors(bio_err); sl@0: return 0; sl@0: } sl@0: sl@0: static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt) sl@0: { sl@0: int id, i, idret; sl@0: char *pname; sl@0: id = X509_PURPOSE_get_id(pt); sl@0: pname = X509_PURPOSE_get0_name(pt); sl@0: for (i = 0; i < 2; i++) sl@0: { sl@0: idret = X509_check_purpose(cert, id, i); sl@0: BIO_printf(bio, "%s%s : ", pname, i ? " CA" : ""); sl@0: if (idret == 1) BIO_printf(bio, "Yes\n"); sl@0: else if (idret == 0) BIO_printf(bio, "No\n"); sl@0: else BIO_printf(bio, "Yes (WARNING code=%d)\n", idret); sl@0: } sl@0: return 1; sl@0: }