sl@0: /* apps/req.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: /* Until the key-gen callbacks are modified to use newer prototypes, we allow sl@0: * deprecated functions for openssl-internal code */ sl@0: #ifdef OPENSSL_NO_DEPRECATED sl@0: #undef OPENSSL_NO_DEPRECATED sl@0: #endif 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: #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: #define SECTION "req" sl@0: sl@0: #define BITS "default_bits" sl@0: #define KEYFILE "default_keyfile" sl@0: #define PROMPT "prompt" sl@0: #define DISTINGUISHED_NAME "distinguished_name" sl@0: #define ATTRIBUTES "attributes" sl@0: #define V3_EXTENSIONS "x509_extensions" sl@0: #define REQ_EXTENSIONS "req_extensions" sl@0: #define STRING_MASK "string_mask" sl@0: #define UTF8_IN "utf8" sl@0: sl@0: #define DEFAULT_KEY_LENGTH 512 sl@0: #define MIN_KEY_LENGTH 384 sl@0: sl@0: #undef PROG sl@0: #define PROG req_main sl@0: sl@0: /* -inform arg - input format - default PEM (DER or PEM) sl@0: * -outform arg - output format - default PEM sl@0: * -in arg - input file - default stdin sl@0: * -out arg - output file - default stdout sl@0: * -verify - check request signature sl@0: * -noout - don't print stuff out. sl@0: * -text - print out human readable text. sl@0: * -nodes - no des encryption sl@0: * -config file - Load configuration file. sl@0: * -key file - make a request using key in file (or use it for verification). sl@0: * -keyform arg - key file format. sl@0: * -rand file(s) - load the file(s) into the PRNG. sl@0: * -newkey - make a key and a request. sl@0: * -modulus - print RSA modulus. sl@0: * -pubkey - output Public Key. sl@0: * -x509 - output a self signed X509 structure instead. sl@0: * -asn1-kludge - output new certificate request in a format that some CA's sl@0: * require. This format is wrong sl@0: */ sl@0: sl@0: static int make_REQ(X509_REQ *req,EVP_PKEY *pkey,char *dn,int mutlirdn, sl@0: int attribs,unsigned long chtype); sl@0: static int build_subject(X509_REQ *req, char *subj, unsigned long chtype, sl@0: int multirdn); sl@0: static int prompt_info(X509_REQ *req, sl@0: STACK_OF(CONF_VALUE) *dn_sk, char *dn_sect, sl@0: STACK_OF(CONF_VALUE) *attr_sk, char *attr_sect, int attribs, sl@0: unsigned long chtype); sl@0: static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *sk, sl@0: STACK_OF(CONF_VALUE) *attr, int attribs, sl@0: unsigned long chtype); sl@0: static int add_attribute_object(X509_REQ *req, char *text, const char *def, sl@0: char *value, int nid, int n_min, sl@0: int n_max, unsigned long chtype); sl@0: static int add_DN_object(X509_NAME *n, char *text, const char *def, char *value, sl@0: int nid,int n_min,int n_max, unsigned long chtype, int mval); sl@0: #ifndef OPENSSL_NO_RSA sl@0: static int MS_CALLBACK req_cb(int p, int n, BN_GENCB *cb); sl@0: #endif sl@0: static int req_check_len(int len,int n_min,int n_max); sl@0: static int check_end(const char *str, const char *end); sl@0: #ifndef MONOLITH sl@0: static char *default_config_file=NULL; sl@0: #endif sl@0: static CONF *req_conf=NULL; sl@0: static int batch=0; sl@0: sl@0: #define TYPE_RSA 1 sl@0: #define TYPE_DSA 2 sl@0: #define TYPE_DH 3 sl@0: #define TYPE_EC 4 sl@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: #ifndef OPENSSL_NO_DSA sl@0: DSA *dsa_params=NULL; sl@0: #endif sl@0: #ifndef OPENSSL_NO_ECDSA sl@0: EC_KEY *ec_params = NULL; sl@0: #endif sl@0: unsigned long nmflag = 0, reqflag = 0; sl@0: int ex=1,x509=0,days=30; sl@0: X509 *x509ss=NULL; sl@0: X509_REQ *req=NULL; sl@0: EVP_PKEY *pkey=NULL; sl@0: int i=0,badops=0,newreq=0,verbose=0,pkey_type=TYPE_RSA; sl@0: long newkey = -1; sl@0: BIO *in=NULL,*out=NULL; sl@0: int informat,outformat,verify=0,noout=0,text=0,keyform=FORMAT_PEM; sl@0: int nodes=0,kludge=0,newhdr=0,subject=0,pubkey=0; sl@0: char *infile,*outfile,*prog,*keyfile=NULL,*template=NULL,*keyout=NULL; sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: char *engine=NULL; sl@0: #endif sl@0: char *extensions = NULL; sl@0: char *req_exts = NULL; sl@0: const EVP_CIPHER *cipher=NULL; sl@0: ASN1_INTEGER *serial = NULL; sl@0: int modulus=0; sl@0: char *inrand=NULL; sl@0: char *passargin = NULL, *passargout = NULL; sl@0: char *passin = NULL, *passout = NULL; sl@0: char *p; sl@0: char *subj = NULL; sl@0: int multirdn = 0; sl@0: const EVP_MD *md_alg=NULL,*digest=EVP_sha1(); sl@0: unsigned long chtype = MBSTRING_ASC; sl@0: #ifndef MONOLITH sl@0: char *to_free; sl@0: long errline; sl@0: #endif sl@0: sl@0: req_conf = NULL; sl@0: #ifndef OPENSSL_NO_DES sl@0: cipher=EVP_des_ede3_cbc(); sl@0: #endif sl@0: apps_startup(); sl@0: sl@0: if (bio_err == NULL) sl@0: if ((bio_err=BIO_new(BIO_s_file())) != NULL) sl@0: BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); sl@0: sl@0: sl@0: infile=NULL; sl@0: outfile=NULL; sl@0: informat=FORMAT_PEM; sl@0: outformat=FORMAT_PEM; sl@0: sl@0: prog=argv[0]; sl@0: argc--; sl@0: argv++; 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: #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,"-key") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: keyfile= *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-pubkey") == 0) sl@0: { sl@0: pubkey=1; sl@0: } sl@0: else if (strcmp(*argv,"-new") == 0) sl@0: { sl@0: newreq=1; sl@0: } sl@0: else if (strcmp(*argv,"-config") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: template= *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-keyform") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: keyform=str2fmt(*(++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,"-keyout") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: keyout= *(++argv); 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,"-passout") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: passargout= *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-rand") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: inrand= *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-newkey") == 0) sl@0: { sl@0: int is_numeric; sl@0: sl@0: if (--argc < 1) goto bad; sl@0: p= *(++argv); sl@0: is_numeric = p[0] >= '0' && p[0] <= '9'; sl@0: if (strncmp("rsa:",p,4) == 0 || is_numeric) sl@0: { sl@0: pkey_type=TYPE_RSA; sl@0: if(!is_numeric) sl@0: p+=4; sl@0: newkey= atoi(p); sl@0: } sl@0: else sl@0: #ifndef OPENSSL_NO_DSA sl@0: if (strncmp("dsa:",p,4) == 0) sl@0: { sl@0: X509 *xtmp=NULL; sl@0: EVP_PKEY *dtmp; sl@0: sl@0: pkey_type=TYPE_DSA; sl@0: p+=4; sl@0: if ((in=BIO_new_file(p,"r")) == NULL) sl@0: { sl@0: perror(p); sl@0: goto end; sl@0: } sl@0: if ((dsa_params=PEM_read_bio_DSAparams(in,NULL,NULL,NULL)) == NULL) sl@0: { sl@0: ERR_clear_error(); sl@0: (void)BIO_reset(in); sl@0: if ((xtmp=PEM_read_bio_X509(in,NULL,NULL,NULL)) == NULL) sl@0: { sl@0: BIO_printf(bio_err,"unable to load DSA parameters from file\n"); sl@0: goto end; sl@0: } sl@0: sl@0: if ((dtmp=X509_get_pubkey(xtmp)) == NULL) goto end; sl@0: if (dtmp->type == EVP_PKEY_DSA) sl@0: dsa_params=DSAparams_dup(dtmp->pkey.dsa); sl@0: EVP_PKEY_free(dtmp); sl@0: X509_free(xtmp); sl@0: if (dsa_params == NULL) sl@0: { sl@0: BIO_printf(bio_err,"Certificate does not contain DSA parameters\n"); sl@0: goto end; sl@0: } sl@0: } sl@0: BIO_free(in); sl@0: in=NULL; sl@0: newkey=BN_num_bits(dsa_params->p); sl@0: } sl@0: else sl@0: #endif sl@0: #ifndef OPENSSL_NO_ECDSA sl@0: if (strncmp("ec:",p,3) == 0) sl@0: { sl@0: X509 *xtmp=NULL; sl@0: EVP_PKEY *dtmp; sl@0: EC_GROUP *group; sl@0: sl@0: pkey_type=TYPE_EC; sl@0: p+=3; sl@0: if ((in=BIO_new_file(p,"r")) == NULL) sl@0: { sl@0: perror(p); sl@0: goto end; sl@0: } sl@0: if ((ec_params = EC_KEY_new()) == NULL) sl@0: goto end; sl@0: group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL); sl@0: if (group == NULL) sl@0: { sl@0: EC_KEY_free(ec_params); sl@0: ERR_clear_error(); sl@0: (void)BIO_reset(in); sl@0: if ((xtmp=PEM_read_bio_X509(in,NULL,NULL,NULL)) == NULL) sl@0: { sl@0: BIO_printf(bio_err,"unable to load EC parameters from file\n"); sl@0: goto end; sl@0: } sl@0: sl@0: if ((dtmp=X509_get_pubkey(xtmp))==NULL) sl@0: goto end; sl@0: if (dtmp->type == EVP_PKEY_EC) sl@0: ec_params = EC_KEY_dup(dtmp->pkey.ec); sl@0: EVP_PKEY_free(dtmp); sl@0: X509_free(xtmp); sl@0: if (ec_params == NULL) sl@0: { sl@0: BIO_printf(bio_err,"Certificate does not contain EC parameters\n"); sl@0: goto end; sl@0: } sl@0: } sl@0: else sl@0: { sl@0: if (EC_KEY_set_group(ec_params, group) == 0) sl@0: goto end; sl@0: EC_GROUP_free(group); sl@0: } sl@0: sl@0: BIO_free(in); sl@0: in=NULL; sl@0: newkey = EC_GROUP_get_degree(EC_KEY_get0_group(ec_params)); sl@0: } sl@0: else sl@0: #endif sl@0: #ifndef OPENSSL_NO_DH sl@0: if (strncmp("dh:",p,4) == 0) sl@0: { sl@0: pkey_type=TYPE_DH; sl@0: p+=3; sl@0: } sl@0: else sl@0: #endif sl@0: { sl@0: goto bad; sl@0: } sl@0: sl@0: newreq=1; sl@0: } sl@0: else if (strcmp(*argv,"-batch") == 0) sl@0: batch=1; sl@0: else if (strcmp(*argv,"-newhdr") == 0) sl@0: newhdr=1; sl@0: else if (strcmp(*argv,"-modulus") == 0) sl@0: modulus=1; sl@0: else if (strcmp(*argv,"-verify") == 0) sl@0: verify=1; sl@0: else if (strcmp(*argv,"-nodes") == 0) sl@0: nodes=1; sl@0: else if (strcmp(*argv,"-noout") == 0) sl@0: noout=1; sl@0: else if (strcmp(*argv,"-verbose") == 0) sl@0: verbose=1; sl@0: else if (strcmp(*argv,"-utf8") == 0) sl@0: chtype = MBSTRING_UTF8; 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: else if (strcmp(*argv,"-reqopt") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: if (!set_cert_ex(&reqflag, *(++argv))) goto bad; sl@0: } sl@0: else if (strcmp(*argv,"-subject") == 0) sl@0: subject=1; sl@0: else if (strcmp(*argv,"-text") == 0) sl@0: text=1; sl@0: else if (strcmp(*argv,"-x509") == 0) sl@0: x509=1; sl@0: else if (strcmp(*argv,"-asn1-kludge") == 0) sl@0: kludge=1; sl@0: else if (strcmp(*argv,"-no-asn1-kludge") == 0) sl@0: kludge=0; sl@0: else if (strcmp(*argv,"-subj") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: subj= *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-multivalue-rdn") == 0) sl@0: multirdn=1; 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) days=30; sl@0: } sl@0: else if (strcmp(*argv,"-set_serial") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: serial = s2i_ASN1_INTEGER(NULL, *(++argv)); sl@0: if (!serial) goto bad; sl@0: } sl@0: else if ((md_alg=EVP_get_digestbyname(&((*argv)[1]))) != NULL) sl@0: { sl@0: /* ok */ sl@0: digest=md_alg; sl@0: } sl@0: else if (strcmp(*argv,"-extensions") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: extensions = *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-reqexts") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: req_exts = *(++argv); 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: BIO_printf(bio_err,"%s [options] outfile\n",prog); sl@0: BIO_printf(bio_err,"where options are\n"); sl@0: BIO_printf(bio_err," -inform arg input format - DER or PEM\n"); sl@0: BIO_printf(bio_err," -outform arg output format - DER or PEM\n"); sl@0: BIO_printf(bio_err," -in arg input file\n"); sl@0: BIO_printf(bio_err," -out arg output file\n"); sl@0: BIO_printf(bio_err," -text text form of request\n"); sl@0: BIO_printf(bio_err," -pubkey output public key\n"); sl@0: BIO_printf(bio_err," -noout do not output REQ\n"); sl@0: BIO_printf(bio_err," -verify verify signature on REQ\n"); sl@0: BIO_printf(bio_err," -modulus RSA modulus\n"); sl@0: BIO_printf(bio_err," -nodes don't encrypt the output key\n"); sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: BIO_printf(bio_err," -engine e use engine e, possibly a hardware device\n"); sl@0: #endif sl@0: BIO_printf(bio_err," -subject output the request's subject\n"); sl@0: BIO_printf(bio_err," -passin private key password source\n"); sl@0: BIO_printf(bio_err," -key file use the private key contained in file\n"); sl@0: BIO_printf(bio_err," -keyform arg key file format\n"); sl@0: BIO_printf(bio_err," -keyout arg file to send the key to\n"); sl@0: BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR); sl@0: BIO_printf(bio_err," load the file (or the files in the directory) into\n"); sl@0: BIO_printf(bio_err," the random number generator\n"); sl@0: BIO_printf(bio_err," -newkey rsa:bits generate a new RSA key of 'bits' in size\n"); sl@0: BIO_printf(bio_err," -newkey dsa:file generate a new DSA key, parameters taken from CA in 'file'\n"); sl@0: #ifndef OPENSSL_NO_ECDSA sl@0: BIO_printf(bio_err," -newkey ec:file generate a new EC key, parameters taken from CA in 'file'\n"); sl@0: #endif sl@0: BIO_printf(bio_err," -[digest] Digest to sign with (md5, sha1, md2, mdc2, md4)\n"); sl@0: BIO_printf(bio_err," -config file request template file.\n"); sl@0: BIO_printf(bio_err," -subj arg set or modify request subject\n"); sl@0: BIO_printf(bio_err," -multivalue-rdn enable support for multivalued RDNs\n"); sl@0: BIO_printf(bio_err," -new new request.\n"); sl@0: BIO_printf(bio_err," -batch do not ask anything during request generation\n"); sl@0: BIO_printf(bio_err," -x509 output a x509 structure instead of a cert. req.\n"); sl@0: BIO_printf(bio_err," -days number of days a certificate generated by -x509 is valid for.\n"); sl@0: BIO_printf(bio_err," -set_serial serial number to use for a certificate generated by -x509.\n"); sl@0: BIO_printf(bio_err," -newhdr output \"NEW\" in the header lines\n"); sl@0: BIO_printf(bio_err," -asn1-kludge Output the 'request' in a format that is wrong but some CA's\n"); sl@0: BIO_printf(bio_err," have been reported as requiring\n"); sl@0: BIO_printf(bio_err," -extensions .. specify certificate extension section (override value in config file)\n"); sl@0: BIO_printf(bio_err," -reqexts .. specify request extension section (override value in config file)\n"); sl@0: BIO_printf(bio_err," -utf8 input characters are UTF8 (default ASCII)\n"); sl@0: BIO_printf(bio_err," -nameopt arg - various certificate name options\n"); sl@0: BIO_printf(bio_err," -reqopt arg - various request text options\n\n"); sl@0: goto end; sl@0: } sl@0: sl@0: ERR_load_crypto_strings(); sl@0: if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) { sl@0: BIO_printf(bio_err, "Error getting passwords\n"); sl@0: goto end; sl@0: } sl@0: sl@0: #ifndef MONOLITH /* else this has happened in openssl.c (global `config') */ sl@0: /* Lets load up our environment a little */ sl@0: p=getenv("OPENSSL_CONF"); sl@0: if (p == NULL) sl@0: p=getenv("SSLEAY_CONF"); sl@0: if (p == NULL) sl@0: p=to_free=make_config_name(); sl@0: default_config_file=p; sl@0: config=NCONF_new(NULL); sl@0: i=NCONF_load(config, p, &errline); sl@0: #endif sl@0: sl@0: if (template != NULL) sl@0: { sl@0: long errline = -1; sl@0: sl@0: if( verbose ) sl@0: BIO_printf(bio_err,"Using configuration from %s\n",template); sl@0: req_conf=NCONF_new(NULL); sl@0: i=NCONF_load(req_conf,template,&errline); sl@0: if (i == 0) sl@0: { sl@0: BIO_printf(bio_err,"error on line %ld of %s\n",errline,template); sl@0: goto end; sl@0: } sl@0: } sl@0: else sl@0: { sl@0: req_conf=config; sl@0: sl@0: if (req_conf == NULL) sl@0: { sl@0: BIO_printf(bio_err,"Unable to load config info from %s\n", default_config_file); sl@0: if (newreq) sl@0: goto end; sl@0: } sl@0: else if( verbose ) sl@0: BIO_printf(bio_err,"Using configuration from %s\n", sl@0: default_config_file); sl@0: } sl@0: sl@0: if (req_conf != NULL) sl@0: { sl@0: if (!load_config(bio_err, req_conf)) sl@0: goto end; sl@0: p=NCONF_get_string(req_conf,NULL,"oid_file"); sl@0: if (p == NULL) sl@0: ERR_clear_error(); sl@0: if (p != NULL) sl@0: { sl@0: BIO *oid_bio; sl@0: sl@0: oid_bio=BIO_new_file(p,"r"); sl@0: if (oid_bio == NULL) sl@0: { sl@0: /* sl@0: BIO_printf(bio_err,"problems opening %s for extra oid's\n",p); sl@0: ERR_print_errors(bio_err); sl@0: */ sl@0: } sl@0: else sl@0: { sl@0: OBJ_create_objects(oid_bio); sl@0: BIO_free(oid_bio); sl@0: } sl@0: } sl@0: } sl@0: if(!add_oid_section(bio_err, req_conf)) goto end; sl@0: sl@0: if (md_alg == NULL) sl@0: { sl@0: p=NCONF_get_string(req_conf,SECTION,"default_md"); sl@0: if (p == NULL) sl@0: ERR_clear_error(); sl@0: if (p != NULL) sl@0: { sl@0: if ((md_alg=EVP_get_digestbyname(p)) != NULL) sl@0: digest=md_alg; sl@0: } sl@0: } sl@0: sl@0: if (!extensions) sl@0: { sl@0: extensions = NCONF_get_string(req_conf, SECTION, V3_EXTENSIONS); sl@0: if (!extensions) sl@0: ERR_clear_error(); sl@0: } sl@0: if (extensions) { sl@0: /* Check syntax of file */ sl@0: X509V3_CTX ctx; sl@0: X509V3_set_ctx_test(&ctx); sl@0: X509V3_set_nconf(&ctx, req_conf); sl@0: if(!X509V3_EXT_add_nconf(req_conf, &ctx, extensions, NULL)) { sl@0: BIO_printf(bio_err, sl@0: "Error Loading extension section %s\n", extensions); sl@0: goto end; sl@0: } sl@0: } sl@0: sl@0: if(!passin) sl@0: { sl@0: passin = NCONF_get_string(req_conf, SECTION, "input_password"); sl@0: if (!passin) sl@0: ERR_clear_error(); sl@0: } sl@0: sl@0: if(!passout) sl@0: { sl@0: passout = NCONF_get_string(req_conf, SECTION, "output_password"); sl@0: if (!passout) sl@0: ERR_clear_error(); sl@0: } sl@0: sl@0: p = NCONF_get_string(req_conf, SECTION, STRING_MASK); sl@0: if (!p) sl@0: ERR_clear_error(); sl@0: sl@0: if(p && !ASN1_STRING_set_default_mask_asc(p)) { sl@0: BIO_printf(bio_err, "Invalid global string mask setting %s\n", p); sl@0: goto end; sl@0: } sl@0: sl@0: if (chtype != MBSTRING_UTF8) sl@0: { sl@0: p = NCONF_get_string(req_conf, SECTION, UTF8_IN); sl@0: if (!p) sl@0: ERR_clear_error(); sl@0: else if (!strcmp(p, "yes")) sl@0: chtype = MBSTRING_UTF8; sl@0: } sl@0: sl@0: sl@0: if(!req_exts) sl@0: { sl@0: req_exts = NCONF_get_string(req_conf, SECTION, REQ_EXTENSIONS); sl@0: if (!req_exts) sl@0: ERR_clear_error(); sl@0: } sl@0: if(req_exts) { sl@0: /* Check syntax of file */ sl@0: X509V3_CTX ctx; sl@0: X509V3_set_ctx_test(&ctx); sl@0: X509V3_set_nconf(&ctx, req_conf); sl@0: if(!X509V3_EXT_add_nconf(req_conf, &ctx, req_exts, NULL)) { sl@0: BIO_printf(bio_err, sl@0: "Error Loading request extension section %s\n", sl@0: req_exts); sl@0: goto end; sl@0: } sl@0: } sl@0: sl@0: in=BIO_new(BIO_s_file()); sl@0: out=BIO_new(BIO_s_file()); sl@0: if ((in == NULL) || (out == NULL)) sl@0: goto end; 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 (keyfile != NULL) sl@0: { sl@0: pkey = load_key(bio_err, keyfile, keyform, 0, passin, e, sl@0: "Private Key"); sl@0: if (!pkey) sl@0: { sl@0: /* load_key() has already printed an appropriate sl@0: message */ sl@0: goto end; sl@0: } sl@0: if (EVP_PKEY_type(pkey->type) == EVP_PKEY_DSA || sl@0: EVP_PKEY_type(pkey->type) == EVP_PKEY_EC) sl@0: { sl@0: char *randfile = NCONF_get_string(req_conf,SECTION,"RANDFILE"); sl@0: if (randfile == NULL) sl@0: ERR_clear_error(); sl@0: app_RAND_load_file(randfile, bio_err, 0); sl@0: } sl@0: } sl@0: sl@0: if (newreq && (pkey == NULL)) sl@0: { sl@0: #ifndef OPENSSL_NO_RSA sl@0: BN_GENCB cb; sl@0: #endif sl@0: char *randfile = NCONF_get_string(req_conf,SECTION,"RANDFILE"); sl@0: if (randfile == NULL) sl@0: ERR_clear_error(); sl@0: app_RAND_load_file(randfile, bio_err, 0); sl@0: if (inrand) sl@0: app_RAND_load_files(inrand); sl@0: sl@0: if (newkey <= 0) sl@0: { sl@0: if (!NCONF_get_number(req_conf,SECTION,BITS, &newkey)) sl@0: newkey=DEFAULT_KEY_LENGTH; sl@0: } sl@0: sl@0: if (newkey < MIN_KEY_LENGTH && (pkey_type == TYPE_RSA || pkey_type == TYPE_DSA)) sl@0: { sl@0: BIO_printf(bio_err,"private key length is too short,\n"); sl@0: BIO_printf(bio_err,"it needs to be at least %d bits, not %ld\n",MIN_KEY_LENGTH,newkey); sl@0: goto end; sl@0: } sl@0: BIO_printf(bio_err,"Generating a %ld bit %s private key\n", sl@0: newkey,(pkey_type == TYPE_RSA)?"RSA": sl@0: (pkey_type == TYPE_DSA)?"DSA":"EC"); sl@0: sl@0: if ((pkey=EVP_PKEY_new()) == NULL) goto end; sl@0: sl@0: #ifndef OPENSSL_NO_RSA sl@0: BN_GENCB_set(&cb, req_cb, bio_err); sl@0: if (pkey_type == TYPE_RSA) sl@0: { sl@0: RSA *rsa = RSA_new(); sl@0: BIGNUM *bn = BN_new(); sl@0: if(!bn || !rsa || !BN_set_word(bn, 0x10001) || sl@0: !RSA_generate_key_ex(rsa, newkey, bn, &cb) || sl@0: !EVP_PKEY_assign_RSA(pkey, rsa)) sl@0: { sl@0: if(bn) BN_free(bn); sl@0: if(rsa) RSA_free(rsa); sl@0: goto end; sl@0: } sl@0: BN_free(bn); sl@0: } sl@0: else sl@0: #endif sl@0: #ifndef OPENSSL_NO_DSA sl@0: if (pkey_type == TYPE_DSA) sl@0: { sl@0: if (!DSA_generate_key(dsa_params)) goto end; sl@0: if (!EVP_PKEY_assign_DSA(pkey,dsa_params)) goto end; sl@0: dsa_params=NULL; sl@0: } sl@0: #endif sl@0: #ifndef OPENSSL_NO_ECDSA sl@0: if (pkey_type == TYPE_EC) sl@0: { sl@0: if (!EC_KEY_generate_key(ec_params)) goto end; sl@0: if (!EVP_PKEY_assign_EC_KEY(pkey, ec_params)) sl@0: goto end; sl@0: ec_params = NULL; sl@0: } sl@0: #endif sl@0: sl@0: app_RAND_write_file(randfile, bio_err); sl@0: sl@0: if (pkey == NULL) goto end; sl@0: sl@0: if (keyout == NULL) sl@0: { sl@0: keyout=NCONF_get_string(req_conf,SECTION,KEYFILE); sl@0: if (keyout == NULL) sl@0: ERR_clear_error(); sl@0: } sl@0: sl@0: if (keyout == NULL) sl@0: { sl@0: BIO_printf(bio_err,"writing new private key to stdout\n"); sl@0: BIO_set_fp(out,stdout,BIO_NOCLOSE); sl@0: 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: BIO_printf(bio_err,"writing new private key to '%s'\n",keyout); sl@0: if (BIO_write_filename(out,keyout) <= 0) sl@0: { sl@0: perror(keyout); sl@0: goto end; sl@0: } sl@0: } sl@0: sl@0: p=NCONF_get_string(req_conf,SECTION,"encrypt_rsa_key"); sl@0: if (p == NULL) sl@0: { sl@0: ERR_clear_error(); sl@0: p=NCONF_get_string(req_conf,SECTION,"encrypt_key"); sl@0: if (p == NULL) sl@0: ERR_clear_error(); sl@0: } sl@0: if ((p != NULL) && (strcmp(p,"no") == 0)) sl@0: cipher=NULL; sl@0: if (nodes) cipher=NULL; sl@0: sl@0: i=0; sl@0: loop: sl@0: if (!PEM_write_bio_PrivateKey(out,pkey,cipher, sl@0: NULL,0,NULL,passout)) sl@0: { sl@0: if ((ERR_GET_REASON(ERR_peek_error()) == sl@0: PEM_R_PROBLEMS_GETTING_PASSWORD) && (i < 3)) sl@0: { sl@0: ERR_clear_error(); sl@0: i++; sl@0: goto loop; sl@0: } sl@0: goto end; sl@0: } sl@0: BIO_printf(bio_err,"-----\n"); sl@0: } sl@0: sl@0: if (!newreq) sl@0: { sl@0: /* Since we are using a pre-existing certificate sl@0: * request, the kludge 'format' info should not be sl@0: * changed. */ sl@0: kludge= -1; sl@0: if (infile == NULL) sl@0: BIO_set_fp(in,stdin,BIO_NOCLOSE); sl@0: sl@0: else sl@0: { sl@0: if (BIO_read_filename(in,infile) <= 0) sl@0: { sl@0: perror(infile); sl@0: goto end; sl@0: } sl@0: } sl@0: sl@0: if (informat == FORMAT_ASN1) sl@0: req=d2i_X509_REQ_bio(in,NULL); sl@0: else if (informat == FORMAT_PEM) sl@0: req=PEM_read_bio_X509_REQ(in,NULL,NULL,NULL); sl@0: else sl@0: { sl@0: BIO_printf(bio_err,"bad input format specified for X509 request\n"); sl@0: goto end; sl@0: } sl@0: if (req == NULL) sl@0: { sl@0: BIO_printf(bio_err,"unable to load X509 request\n"); sl@0: goto end; sl@0: } sl@0: } sl@0: sl@0: if (newreq || x509) sl@0: { sl@0: if (pkey == NULL) sl@0: { sl@0: BIO_printf(bio_err,"you need to specify a private key\n"); sl@0: goto end; sl@0: } sl@0: #ifndef OPENSSL_NO_DSA sl@0: if (pkey->type == EVP_PKEY_DSA) sl@0: digest=EVP_dss1(); sl@0: #endif sl@0: #ifndef OPENSSL_NO_ECDSA sl@0: if (pkey->type == EVP_PKEY_EC) sl@0: digest=EVP_ecdsa(); sl@0: #endif sl@0: if (req == NULL) sl@0: { sl@0: req=X509_REQ_new(); sl@0: if (req == NULL) sl@0: { sl@0: goto end; sl@0: } sl@0: sl@0: i=make_REQ(req,pkey,subj,multirdn,!x509, chtype); sl@0: subj=NULL; /* done processing '-subj' option */ sl@0: if ((kludge > 0) && !sk_X509_ATTRIBUTE_num(req->req_info->attributes)) sl@0: { sl@0: sk_X509_ATTRIBUTE_free(req->req_info->attributes); sl@0: req->req_info->attributes = NULL; sl@0: } sl@0: if (!i) sl@0: { sl@0: BIO_printf(bio_err,"problems making Certificate Request\n"); sl@0: goto end; sl@0: } sl@0: } sl@0: if (x509) sl@0: { sl@0: EVP_PKEY *tmppkey; sl@0: X509V3_CTX ext_ctx; sl@0: if ((x509ss=X509_new()) == NULL) goto end; sl@0: sl@0: /* Set version to V3 */ sl@0: if(extensions && !X509_set_version(x509ss, 2)) goto end; sl@0: if (serial) sl@0: { sl@0: if (!X509_set_serialNumber(x509ss, serial)) goto end; sl@0: } sl@0: else sl@0: { sl@0: if (!rand_serial(NULL, sl@0: X509_get_serialNumber(x509ss))) sl@0: goto end; sl@0: } sl@0: sl@0: if (!X509_set_issuer_name(x509ss, X509_REQ_get_subject_name(req))) goto end; sl@0: if (!X509_gmtime_adj(X509_get_notBefore(x509ss),0)) goto end; sl@0: if (!X509_gmtime_adj(X509_get_notAfter(x509ss), (long)60*60*24*days)) goto end; sl@0: if (!X509_set_subject_name(x509ss, X509_REQ_get_subject_name(req))) goto end; sl@0: tmppkey = X509_REQ_get_pubkey(req); sl@0: if (!tmppkey || !X509_set_pubkey(x509ss,tmppkey)) goto end; sl@0: EVP_PKEY_free(tmppkey); sl@0: sl@0: /* Set up V3 context struct */ sl@0: sl@0: X509V3_set_ctx(&ext_ctx, x509ss, x509ss, NULL, NULL, 0); sl@0: X509V3_set_nconf(&ext_ctx, req_conf); sl@0: sl@0: /* Add extensions */ sl@0: if(extensions && !X509V3_EXT_add_nconf(req_conf, sl@0: &ext_ctx, extensions, x509ss)) sl@0: { sl@0: BIO_printf(bio_err, sl@0: "Error Loading extension section %s\n", sl@0: extensions); sl@0: goto end; sl@0: } sl@0: sl@0: if (!(i=X509_sign(x509ss,pkey,digest))) sl@0: goto end; sl@0: } sl@0: else sl@0: { sl@0: X509V3_CTX ext_ctx; sl@0: sl@0: /* Set up V3 context struct */ sl@0: sl@0: X509V3_set_ctx(&ext_ctx, NULL, NULL, req, NULL, 0); sl@0: X509V3_set_nconf(&ext_ctx, req_conf); sl@0: sl@0: /* Add extensions */ sl@0: if(req_exts && !X509V3_EXT_REQ_add_nconf(req_conf, sl@0: &ext_ctx, req_exts, req)) sl@0: { sl@0: BIO_printf(bio_err, sl@0: "Error Loading extension section %s\n", sl@0: req_exts); sl@0: goto end; sl@0: } sl@0: if (!(i=X509_REQ_sign(req,pkey,digest))) sl@0: goto end; sl@0: } sl@0: } sl@0: sl@0: if (subj && x509) sl@0: { sl@0: BIO_printf(bio_err, "Cannot modifiy certificate subject\n"); sl@0: goto end; sl@0: } sl@0: sl@0: if (subj && !x509) sl@0: { sl@0: if (verbose) sl@0: { sl@0: BIO_printf(bio_err, "Modifying Request's Subject\n"); sl@0: print_name(bio_err, "old subject=", X509_REQ_get_subject_name(req), nmflag); sl@0: } sl@0: sl@0: if (build_subject(req, subj, chtype, multirdn) == 0) sl@0: { sl@0: BIO_printf(bio_err, "ERROR: cannot modify subject\n"); sl@0: ex=1; sl@0: goto end; sl@0: } sl@0: sl@0: req->req_info->enc.modified = 1; sl@0: sl@0: if (verbose) sl@0: { sl@0: print_name(bio_err, "new subject=", X509_REQ_get_subject_name(req), nmflag); sl@0: } sl@0: } sl@0: sl@0: if (verify && !x509) sl@0: { sl@0: int tmp=0; sl@0: sl@0: if (pkey == NULL) sl@0: { sl@0: pkey=X509_REQ_get_pubkey(req); sl@0: tmp=1; sl@0: if (pkey == NULL) goto end; sl@0: } sl@0: sl@0: i=X509_REQ_verify(req,pkey); sl@0: if (tmp) { sl@0: EVP_PKEY_free(pkey); sl@0: pkey=NULL; sl@0: } sl@0: sl@0: if (i < 0) sl@0: { sl@0: goto end; sl@0: } sl@0: else if (i == 0) sl@0: { sl@0: BIO_printf(bio_err,"verify failure\n"); sl@0: ERR_print_errors(bio_err); sl@0: } sl@0: else /* if (i > 0) */ sl@0: BIO_printf(bio_err,"verify OK\n"); sl@0: } sl@0: sl@0: if (noout && !text && !modulus && !subject && !pubkey) sl@0: { sl@0: ex=0; sl@0: goto end; sl@0: } 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 ((keyout != NULL) && (strcmp(outfile,keyout) == 0)) sl@0: i=(int)BIO_append_filename(out,outfile); sl@0: else sl@0: i=(int)BIO_write_filename(out,outfile); sl@0: if (!i) sl@0: { sl@0: perror(outfile); sl@0: goto end; sl@0: } sl@0: } sl@0: sl@0: if (pubkey) sl@0: { sl@0: EVP_PKEY *tpubkey; sl@0: tpubkey=X509_REQ_get_pubkey(req); sl@0: if (tpubkey == 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(out, tpubkey); sl@0: EVP_PKEY_free(tpubkey); sl@0: } sl@0: sl@0: if (text) sl@0: { sl@0: if (x509) sl@0: X509_print_ex(out, x509ss, nmflag, reqflag); sl@0: else sl@0: X509_REQ_print_ex(out, req, nmflag, reqflag); sl@0: } sl@0: sl@0: if(subject) sl@0: { sl@0: if(x509) sl@0: print_name(out, "subject=", X509_get_subject_name(x509ss), nmflag); sl@0: else sl@0: print_name(out, "subject=", X509_REQ_get_subject_name(req), nmflag); sl@0: } sl@0: sl@0: if (modulus) sl@0: { sl@0: EVP_PKEY *tpubkey; sl@0: sl@0: if (x509) sl@0: tpubkey=X509_get_pubkey(x509ss); sl@0: else sl@0: tpubkey=X509_REQ_get_pubkey(req); sl@0: if (tpubkey == NULL) sl@0: { sl@0: fprintf(stdout,"Modulus=unavailable\n"); sl@0: goto end; sl@0: } sl@0: sl@0: fprintf(stdout,"Modulus="); sl@0: sl@0: #ifndef OPENSSL_NO_RSA sl@0: if (tpubkey->type == EVP_PKEY_RSA) sl@0: BN_print(out,tpubkey->pkey.rsa->n); sl@0: else sl@0: #endif sl@0: fprintf(stdout,"Wrong Algorithm type"); sl@0: EVP_PKEY_free(tpubkey); sl@0: fprintf(stdout,"\n"); sl@0: sl@0: } sl@0: sl@0: if (!noout && !x509) sl@0: { sl@0: if (outformat == FORMAT_ASN1) sl@0: i=i2d_X509_REQ_bio(out,req); sl@0: else if (outformat == FORMAT_PEM) { sl@0: if(newhdr) i=PEM_write_bio_X509_REQ_NEW(out,req); sl@0: else i=PEM_write_bio_X509_REQ(out,req); 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 X509 request\n"); sl@0: goto end; sl@0: } sl@0: } sl@0: if (!noout && x509 && (x509ss != NULL)) sl@0: { sl@0: if (outformat == FORMAT_ASN1) sl@0: i=i2d_X509_bio(out,x509ss); sl@0: else if (outformat == FORMAT_PEM) sl@0: i=PEM_write_bio_X509(out,x509ss); 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 X509 certificate\n"); sl@0: goto end; sl@0: } sl@0: } sl@0: ex=0; sl@0: end: sl@0: #ifndef MONOLITH sl@0: if(to_free) sl@0: OPENSSL_free(to_free); sl@0: #endif sl@0: if (ex) sl@0: { sl@0: ERR_print_errors(bio_err); sl@0: } sl@0: if ((req_conf != NULL) && (req_conf != config)) NCONF_free(req_conf); sl@0: BIO_free(in); sl@0: BIO_free_all(out); sl@0: EVP_PKEY_free(pkey); sl@0: X509_REQ_free(req); sl@0: X509_free(x509ss); sl@0: ASN1_INTEGER_free(serial); sl@0: if(passargin && passin) OPENSSL_free(passin); sl@0: if(passargout && passout) OPENSSL_free(passout); sl@0: OBJ_cleanup(); sl@0: #ifndef OPENSSL_NO_DSA sl@0: if (dsa_params != NULL) DSA_free(dsa_params); sl@0: #endif sl@0: #ifndef OPENSSL_NO_ECDSA sl@0: if (ec_params != NULL) EC_KEY_free(ec_params); sl@0: #endif sl@0: apps_shutdown(); sl@0: OPENSSL_EXIT(ex); sl@0: } sl@0: sl@0: static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, char *subj, int multirdn, sl@0: int attribs, unsigned long chtype) sl@0: { sl@0: int ret=0,i; sl@0: char no_prompt = 0; sl@0: STACK_OF(CONF_VALUE) *dn_sk, *attr_sk = NULL; sl@0: char *tmp, *dn_sect,*attr_sect; sl@0: sl@0: tmp=NCONF_get_string(req_conf,SECTION,PROMPT); sl@0: if (tmp == NULL) sl@0: ERR_clear_error(); sl@0: if((tmp != NULL) && !strcmp(tmp, "no")) no_prompt = 1; sl@0: sl@0: dn_sect=NCONF_get_string(req_conf,SECTION,DISTINGUISHED_NAME); sl@0: if (dn_sect == NULL) sl@0: { sl@0: BIO_printf(bio_err,"unable to find '%s' in config\n", sl@0: DISTINGUISHED_NAME); sl@0: goto err; sl@0: } sl@0: dn_sk=NCONF_get_section(req_conf,dn_sect); sl@0: if (dn_sk == NULL) sl@0: { sl@0: BIO_printf(bio_err,"unable to get '%s' section\n",dn_sect); sl@0: goto err; sl@0: } sl@0: sl@0: attr_sect=NCONF_get_string(req_conf,SECTION,ATTRIBUTES); sl@0: if (attr_sect == NULL) sl@0: { sl@0: ERR_clear_error(); sl@0: attr_sk=NULL; sl@0: } sl@0: else sl@0: { sl@0: attr_sk=NCONF_get_section(req_conf,attr_sect); sl@0: if (attr_sk == NULL) sl@0: { sl@0: BIO_printf(bio_err,"unable to get '%s' section\n",attr_sect); sl@0: goto err; sl@0: } sl@0: } sl@0: sl@0: /* setup version number */ sl@0: if (!X509_REQ_set_version(req,0L)) goto err; /* version 1 */ sl@0: sl@0: if (no_prompt) sl@0: i = auto_info(req, dn_sk, attr_sk, attribs, chtype); sl@0: else sl@0: { sl@0: if (subj) sl@0: i = build_subject(req, subj, chtype, multirdn); sl@0: else sl@0: i = prompt_info(req, dn_sk, dn_sect, attr_sk, attr_sect, attribs, chtype); sl@0: } sl@0: if(!i) goto err; sl@0: sl@0: if (!X509_REQ_set_pubkey(req,pkey)) goto err; sl@0: sl@0: ret=1; sl@0: err: sl@0: return(ret); sl@0: } sl@0: sl@0: /* sl@0: * subject is expected to be in the format /type0=value0/type1=value1/type2=... sl@0: * where characters may be escaped by \ sl@0: */ sl@0: static int build_subject(X509_REQ *req, char *subject, unsigned long chtype, int multirdn) sl@0: { sl@0: X509_NAME *n; sl@0: sl@0: if (!(n = parse_name(subject, chtype, multirdn))) sl@0: return 0; sl@0: sl@0: if (!X509_REQ_set_subject_name(req, n)) sl@0: { sl@0: X509_NAME_free(n); sl@0: return 0; sl@0: } sl@0: X509_NAME_free(n); sl@0: return 1; sl@0: } sl@0: sl@0: sl@0: static int prompt_info(X509_REQ *req, sl@0: STACK_OF(CONF_VALUE) *dn_sk, char *dn_sect, sl@0: STACK_OF(CONF_VALUE) *attr_sk, char *attr_sect, int attribs, sl@0: unsigned long chtype) sl@0: { sl@0: int i; sl@0: char *p,*q; sl@0: char buf[100]; sl@0: int nid, mval; sl@0: long n_min,n_max; sl@0: char *type, *value; sl@0: const char *def; sl@0: CONF_VALUE *v; sl@0: X509_NAME *subj; sl@0: subj = X509_REQ_get_subject_name(req); sl@0: sl@0: if(!batch) sl@0: { sl@0: BIO_printf(bio_err,"You are about to be asked to enter information that will be incorporated\n"); sl@0: BIO_printf(bio_err,"into your certificate request.\n"); sl@0: BIO_printf(bio_err,"What you are about to enter is what is called a Distinguished Name or a DN.\n"); sl@0: BIO_printf(bio_err,"There are quite a few fields but you can leave some blank\n"); sl@0: BIO_printf(bio_err,"For some fields there will be a default value,\n"); sl@0: BIO_printf(bio_err,"If you enter '.', the field will be left blank.\n"); sl@0: BIO_printf(bio_err,"-----\n"); sl@0: } sl@0: sl@0: sl@0: if (sk_CONF_VALUE_num(dn_sk)) sl@0: { sl@0: i= -1; sl@0: start: for (;;) sl@0: { sl@0: i++; sl@0: if (sk_CONF_VALUE_num(dn_sk) <= i) break; sl@0: sl@0: v=sk_CONF_VALUE_value(dn_sk,i); sl@0: p=q=NULL; sl@0: type=v->name; sl@0: if(!check_end(type,"_min") || !check_end(type,"_max") || sl@0: !check_end(type,"_default") || sl@0: !check_end(type,"_value")) continue; sl@0: /* Skip past any leading X. X: X, etc to allow for sl@0: * multiple instances sl@0: */ sl@0: for(p = v->name; *p ; p++) sl@0: if ((*p == ':') || (*p == ',') || sl@0: (*p == '.')) { sl@0: p++; sl@0: if(*p) type = p; sl@0: break; sl@0: } sl@0: if (*type == '+') sl@0: { sl@0: mval = -1; sl@0: type++; sl@0: } sl@0: else sl@0: mval = 0; sl@0: /* If OBJ not recognised ignore it */ sl@0: if ((nid=OBJ_txt2nid(type)) == NID_undef) goto start; sl@0: if (BIO_snprintf(buf,sizeof buf,"%s_default",v->name) sl@0: >= (int)sizeof(buf)) sl@0: { sl@0: BIO_printf(bio_err,"Name '%s' too long\n",v->name); sl@0: return 0; sl@0: } sl@0: sl@0: if ((def=NCONF_get_string(req_conf,dn_sect,buf)) == NULL) sl@0: { sl@0: ERR_clear_error(); sl@0: def=""; sl@0: } sl@0: sl@0: BIO_snprintf(buf,sizeof buf,"%s_value",v->name); sl@0: if ((value=NCONF_get_string(req_conf,dn_sect,buf)) == NULL) sl@0: { sl@0: ERR_clear_error(); sl@0: value=NULL; sl@0: } sl@0: sl@0: BIO_snprintf(buf,sizeof buf,"%s_min",v->name); sl@0: if (!NCONF_get_number(req_conf,dn_sect,buf, &n_min)) sl@0: { sl@0: ERR_clear_error(); sl@0: n_min = -1; sl@0: } sl@0: sl@0: BIO_snprintf(buf,sizeof buf,"%s_max",v->name); sl@0: if (!NCONF_get_number(req_conf,dn_sect,buf, &n_max)) sl@0: { sl@0: ERR_clear_error(); sl@0: n_max = -1; sl@0: } sl@0: sl@0: if (!add_DN_object(subj,v->value,def,value,nid, sl@0: n_min,n_max, chtype, mval)) sl@0: return 0; sl@0: } sl@0: if (X509_NAME_entry_count(subj) == 0) sl@0: { sl@0: BIO_printf(bio_err,"error, no objects specified in config file\n"); sl@0: return 0; sl@0: } sl@0: sl@0: if (attribs) sl@0: { sl@0: if ((attr_sk != NULL) && (sk_CONF_VALUE_num(attr_sk) > 0) && (!batch)) sl@0: { sl@0: BIO_printf(bio_err,"\nPlease enter the following 'extra' attributes\n"); sl@0: BIO_printf(bio_err,"to be sent with your certificate request\n"); sl@0: } sl@0: sl@0: i= -1; sl@0: start2: for (;;) sl@0: { sl@0: i++; sl@0: if ((attr_sk == NULL) || sl@0: (sk_CONF_VALUE_num(attr_sk) <= i)) sl@0: break; sl@0: sl@0: v=sk_CONF_VALUE_value(attr_sk,i); sl@0: type=v->name; sl@0: if ((nid=OBJ_txt2nid(type)) == NID_undef) sl@0: goto start2; sl@0: sl@0: if (BIO_snprintf(buf,sizeof buf,"%s_default",type) sl@0: >= (int)sizeof(buf)) sl@0: { sl@0: BIO_printf(bio_err,"Name '%s' too long\n",v->name); sl@0: return 0; sl@0: } sl@0: sl@0: if ((def=NCONF_get_string(req_conf,attr_sect,buf)) sl@0: == NULL) sl@0: { sl@0: ERR_clear_error(); sl@0: def=""; sl@0: } sl@0: sl@0: sl@0: BIO_snprintf(buf,sizeof buf,"%s_value",type); sl@0: if ((value=NCONF_get_string(req_conf,attr_sect,buf)) sl@0: == NULL) sl@0: { sl@0: ERR_clear_error(); sl@0: value=NULL; sl@0: } sl@0: sl@0: BIO_snprintf(buf,sizeof buf,"%s_min",type); sl@0: if (!NCONF_get_number(req_conf,attr_sect,buf, &n_min)) sl@0: n_min = -1; sl@0: sl@0: BIO_snprintf(buf,sizeof buf,"%s_max",type); sl@0: if (!NCONF_get_number(req_conf,attr_sect,buf, &n_max)) sl@0: n_max = -1; sl@0: sl@0: if (!add_attribute_object(req, sl@0: v->value,def,value,nid,n_min,n_max, chtype)) sl@0: return 0; sl@0: } sl@0: } sl@0: } sl@0: else sl@0: { sl@0: BIO_printf(bio_err,"No template, please set one up.\n"); sl@0: return 0; sl@0: } sl@0: sl@0: return 1; sl@0: sl@0: } sl@0: sl@0: static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *dn_sk, sl@0: STACK_OF(CONF_VALUE) *attr_sk, int attribs, unsigned long chtype) sl@0: { sl@0: int i; sl@0: char *p,*q; sl@0: char *type; sl@0: CONF_VALUE *v; sl@0: X509_NAME *subj; sl@0: sl@0: subj = X509_REQ_get_subject_name(req); sl@0: sl@0: for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) sl@0: { sl@0: int mval; sl@0: v=sk_CONF_VALUE_value(dn_sk,i); sl@0: p=q=NULL; sl@0: type=v->name; sl@0: /* Skip past any leading X. X: X, etc to allow for sl@0: * multiple instances sl@0: */ sl@0: for(p = v->name; *p ; p++) sl@0: #ifndef CHARSET_EBCDIC sl@0: if ((*p == ':') || (*p == ',') || (*p == '.')) { sl@0: #else sl@0: if ((*p == os_toascii[':']) || (*p == os_toascii[',']) || (*p == os_toascii['.'])) { sl@0: #endif sl@0: p++; sl@0: if(*p) type = p; sl@0: break; sl@0: } sl@0: #ifndef CHARSET_EBCDIC sl@0: if (*p == '+') sl@0: #else sl@0: if (*p == os_toascii['+']) sl@0: #endif sl@0: { sl@0: p++; sl@0: mval = -1; sl@0: } sl@0: else sl@0: mval = 0; sl@0: if (!X509_NAME_add_entry_by_txt(subj,type, chtype, sl@0: (unsigned char *) v->value,-1,-1,mval)) return 0; sl@0: sl@0: } sl@0: sl@0: if (!X509_NAME_entry_count(subj)) sl@0: { sl@0: BIO_printf(bio_err,"error, no objects specified in config file\n"); sl@0: return 0; sl@0: } sl@0: if (attribs) sl@0: { sl@0: for (i = 0; i < sk_CONF_VALUE_num(attr_sk); i++) sl@0: { sl@0: v=sk_CONF_VALUE_value(attr_sk,i); sl@0: if(!X509_REQ_add1_attr_by_txt(req, v->name, chtype, sl@0: (unsigned char *)v->value, -1)) return 0; sl@0: } sl@0: } sl@0: return 1; sl@0: } sl@0: sl@0: sl@0: static int add_DN_object(X509_NAME *n, char *text, const char *def, char *value, sl@0: int nid, int n_min, int n_max, unsigned long chtype, int mval) sl@0: { sl@0: int i,ret=0; sl@0: MS_STATIC char buf[1024]; sl@0: start: sl@0: if (!batch) BIO_printf(bio_err,"%s [%s]:",text,def); sl@0: (void)BIO_flush(bio_err); sl@0: if(value != NULL) sl@0: { sl@0: BUF_strlcpy(buf,value,sizeof buf); sl@0: BUF_strlcat(buf,"\n",sizeof buf); sl@0: BIO_printf(bio_err,"%s\n",value); sl@0: } sl@0: else sl@0: { sl@0: buf[0]='\0'; sl@0: if (!batch) sl@0: { sl@0: fgets(buf,sizeof buf,stdin); sl@0: } sl@0: else sl@0: { sl@0: buf[0] = '\n'; sl@0: buf[1] = '\0'; sl@0: } sl@0: } sl@0: sl@0: if (buf[0] == '\0') return(0); sl@0: else if (buf[0] == '\n') sl@0: { sl@0: if ((def == NULL) || (def[0] == '\0')) sl@0: return(1); sl@0: BUF_strlcpy(buf,def,sizeof buf); sl@0: BUF_strlcat(buf,"\n",sizeof buf); sl@0: } sl@0: else if ((buf[0] == '.') && (buf[1] == '\n')) return(1); sl@0: sl@0: i=strlen(buf); sl@0: if (buf[i-1] != '\n') sl@0: { sl@0: BIO_printf(bio_err,"weird input :-(\n"); sl@0: return(0); sl@0: } sl@0: buf[--i]='\0'; sl@0: #ifdef CHARSET_EBCDIC sl@0: ebcdic2ascii(buf, buf, i); sl@0: #endif sl@0: if(!req_check_len(i, n_min, n_max)) goto start; sl@0: if (!X509_NAME_add_entry_by_NID(n,nid, chtype, sl@0: (unsigned char *) buf, -1,-1,mval)) goto err; sl@0: ret=1; sl@0: err: sl@0: return(ret); sl@0: } sl@0: sl@0: static int add_attribute_object(X509_REQ *req, char *text, const char *def, sl@0: char *value, int nid, int n_min, sl@0: int n_max, unsigned long chtype) sl@0: { sl@0: int i; sl@0: static char buf[1024]; sl@0: sl@0: start: sl@0: if (!batch) BIO_printf(bio_err,"%s [%s]:",text,def); sl@0: (void)BIO_flush(bio_err); sl@0: if (value != NULL) sl@0: { sl@0: BUF_strlcpy(buf,value,sizeof buf); sl@0: BUF_strlcat(buf,"\n",sizeof buf); sl@0: BIO_printf(bio_err,"%s\n",value); sl@0: } sl@0: else sl@0: { sl@0: buf[0]='\0'; sl@0: if (!batch) sl@0: { sl@0: fgets(buf,sizeof buf,stdin); sl@0: } sl@0: else sl@0: { sl@0: buf[0] = '\n'; sl@0: buf[1] = '\0'; sl@0: } sl@0: } sl@0: sl@0: if (buf[0] == '\0') return(0); sl@0: else if (buf[0] == '\n') sl@0: { sl@0: if ((def == NULL) || (def[0] == '\0')) sl@0: return(1); sl@0: BUF_strlcpy(buf,def,sizeof buf); sl@0: BUF_strlcat(buf,"\n",sizeof buf); sl@0: } sl@0: else if ((buf[0] == '.') && (buf[1] == '\n')) return(1); sl@0: sl@0: i=strlen(buf); sl@0: if (buf[i-1] != '\n') sl@0: { sl@0: BIO_printf(bio_err,"weird input :-(\n"); sl@0: return(0); sl@0: } sl@0: buf[--i]='\0'; sl@0: #ifdef CHARSET_EBCDIC sl@0: ebcdic2ascii(buf, buf, i); sl@0: #endif sl@0: if(!req_check_len(i, n_min, n_max)) goto start; sl@0: sl@0: if(!X509_REQ_add1_attr_by_NID(req, nid, chtype, sl@0: (unsigned char *)buf, -1)) { sl@0: BIO_printf(bio_err, "Error adding attribute\n"); sl@0: ERR_print_errors(bio_err); sl@0: goto err; sl@0: } sl@0: sl@0: return(1); sl@0: err: sl@0: return(0); sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_RSA sl@0: static int MS_CALLBACK req_cb(int p, int n, BN_GENCB *cb) sl@0: { sl@0: char c='*'; sl@0: sl@0: if (p == 0) c='.'; sl@0: if (p == 1) c='+'; sl@0: if (p == 2) c='*'; sl@0: if (p == 3) c='\n'; sl@0: BIO_write(cb->arg,&c,1); sl@0: (void)BIO_flush(cb->arg); sl@0: #ifdef LINT sl@0: p=n; sl@0: #endif sl@0: return 1; sl@0: } sl@0: #endif sl@0: sl@0: static int req_check_len(int len, int n_min, int n_max) sl@0: { sl@0: if ((n_min > 0) && (len < n_min)) sl@0: { sl@0: BIO_printf(bio_err,"string is too short, it needs to be at least %d bytes long\n",n_min); sl@0: return(0); sl@0: } sl@0: if ((n_max >= 0) && (len > n_max)) sl@0: { sl@0: BIO_printf(bio_err,"string is too long, it needs to be less than %d bytes long\n",n_max); sl@0: return(0); sl@0: } sl@0: return(1); sl@0: } sl@0: sl@0: /* Check if the end of a string matches 'end' */ sl@0: static int check_end(const char *str, const char *end) sl@0: { sl@0: int elen, slen; sl@0: const char *tmp; sl@0: elen = strlen(end); sl@0: slen = strlen(str); sl@0: if(elen > slen) return 1; sl@0: tmp = str + slen - elen; sl@0: return strcmp(tmp, end); sl@0: }