1*ebfedea0SLionel Sambuc /* apps/req.c */ 2*ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3*ebfedea0SLionel Sambuc * All rights reserved. 4*ebfedea0SLionel Sambuc * 5*ebfedea0SLionel Sambuc * This package is an SSL implementation written 6*ebfedea0SLionel Sambuc * by Eric Young (eay@cryptsoft.com). 7*ebfedea0SLionel Sambuc * The implementation was written so as to conform with Netscapes SSL. 8*ebfedea0SLionel Sambuc * 9*ebfedea0SLionel Sambuc * This library is free for commercial and non-commercial use as long as 10*ebfedea0SLionel Sambuc * the following conditions are aheared to. The following conditions 11*ebfedea0SLionel Sambuc * apply to all code found in this distribution, be it the RC4, RSA, 12*ebfedea0SLionel Sambuc * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13*ebfedea0SLionel Sambuc * included with this distribution is covered by the same copyright terms 14*ebfedea0SLionel Sambuc * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15*ebfedea0SLionel Sambuc * 16*ebfedea0SLionel Sambuc * Copyright remains Eric Young's, and as such any Copyright notices in 17*ebfedea0SLionel Sambuc * the code are not to be removed. 18*ebfedea0SLionel Sambuc * If this package is used in a product, Eric Young should be given attribution 19*ebfedea0SLionel Sambuc * as the author of the parts of the library used. 20*ebfedea0SLionel Sambuc * This can be in the form of a textual message at program startup or 21*ebfedea0SLionel Sambuc * in documentation (online or textual) provided with the package. 22*ebfedea0SLionel Sambuc * 23*ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without 24*ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions 25*ebfedea0SLionel Sambuc * are met: 26*ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the copyright 27*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer. 28*ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright 29*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the 30*ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution. 31*ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this software 32*ebfedea0SLionel Sambuc * must display the following acknowledgement: 33*ebfedea0SLionel Sambuc * "This product includes cryptographic software written by 34*ebfedea0SLionel Sambuc * Eric Young (eay@cryptsoft.com)" 35*ebfedea0SLionel Sambuc * The word 'cryptographic' can be left out if the rouines from the library 36*ebfedea0SLionel Sambuc * being used are not cryptographic related :-). 37*ebfedea0SLionel Sambuc * 4. If you include any Windows specific code (or a derivative thereof) from 38*ebfedea0SLionel Sambuc * the apps directory (application code) you must include an acknowledgement: 39*ebfedea0SLionel Sambuc * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40*ebfedea0SLionel Sambuc * 41*ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42*ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43*ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44*ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45*ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46*ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47*ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48*ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49*ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50*ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51*ebfedea0SLionel Sambuc * SUCH DAMAGE. 52*ebfedea0SLionel Sambuc * 53*ebfedea0SLionel Sambuc * The licence and distribution terms for any publically available version or 54*ebfedea0SLionel Sambuc * derivative of this code cannot be changed. i.e. this code cannot simply be 55*ebfedea0SLionel Sambuc * copied and put under another distribution licence 56*ebfedea0SLionel Sambuc * [including the GNU Public Licence.] 57*ebfedea0SLionel Sambuc */ 58*ebfedea0SLionel Sambuc 59*ebfedea0SLionel Sambuc /* Until the key-gen callbacks are modified to use newer prototypes, we allow 60*ebfedea0SLionel Sambuc * deprecated functions for openssl-internal code */ 61*ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_DEPRECATED 62*ebfedea0SLionel Sambuc #undef OPENSSL_NO_DEPRECATED 63*ebfedea0SLionel Sambuc #endif 64*ebfedea0SLionel Sambuc 65*ebfedea0SLionel Sambuc #include <stdio.h> 66*ebfedea0SLionel Sambuc #include <stdlib.h> 67*ebfedea0SLionel Sambuc #include <time.h> 68*ebfedea0SLionel Sambuc #include <string.h> 69*ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_STDIO 70*ebfedea0SLionel Sambuc #define APPS_WIN16 71*ebfedea0SLionel Sambuc #endif 72*ebfedea0SLionel Sambuc #include "apps.h" 73*ebfedea0SLionel Sambuc #include <openssl/bio.h> 74*ebfedea0SLionel Sambuc #include <openssl/evp.h> 75*ebfedea0SLionel Sambuc #include <openssl/conf.h> 76*ebfedea0SLionel Sambuc #include <openssl/err.h> 77*ebfedea0SLionel Sambuc #include <openssl/asn1.h> 78*ebfedea0SLionel Sambuc #include <openssl/x509.h> 79*ebfedea0SLionel Sambuc #include <openssl/x509v3.h> 80*ebfedea0SLionel Sambuc #include <openssl/objects.h> 81*ebfedea0SLionel Sambuc #include <openssl/pem.h> 82*ebfedea0SLionel Sambuc #include <openssl/bn.h> 83*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA 84*ebfedea0SLionel Sambuc #include <openssl/rsa.h> 85*ebfedea0SLionel Sambuc #endif 86*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DSA 87*ebfedea0SLionel Sambuc #include <openssl/dsa.h> 88*ebfedea0SLionel Sambuc #endif 89*ebfedea0SLionel Sambuc 90*ebfedea0SLionel Sambuc #define SECTION "req" 91*ebfedea0SLionel Sambuc 92*ebfedea0SLionel Sambuc #define BITS "default_bits" 93*ebfedea0SLionel Sambuc #define KEYFILE "default_keyfile" 94*ebfedea0SLionel Sambuc #define PROMPT "prompt" 95*ebfedea0SLionel Sambuc #define DISTINGUISHED_NAME "distinguished_name" 96*ebfedea0SLionel Sambuc #define ATTRIBUTES "attributes" 97*ebfedea0SLionel Sambuc #define V3_EXTENSIONS "x509_extensions" 98*ebfedea0SLionel Sambuc #define REQ_EXTENSIONS "req_extensions" 99*ebfedea0SLionel Sambuc #define STRING_MASK "string_mask" 100*ebfedea0SLionel Sambuc #define UTF8_IN "utf8" 101*ebfedea0SLionel Sambuc 102*ebfedea0SLionel Sambuc #define DEFAULT_KEY_LENGTH 512 103*ebfedea0SLionel Sambuc #define MIN_KEY_LENGTH 384 104*ebfedea0SLionel Sambuc 105*ebfedea0SLionel Sambuc #undef PROG 106*ebfedea0SLionel Sambuc #define PROG req_main 107*ebfedea0SLionel Sambuc 108*ebfedea0SLionel Sambuc /* -inform arg - input format - default PEM (DER or PEM) 109*ebfedea0SLionel Sambuc * -outform arg - output format - default PEM 110*ebfedea0SLionel Sambuc * -in arg - input file - default stdin 111*ebfedea0SLionel Sambuc * -out arg - output file - default stdout 112*ebfedea0SLionel Sambuc * -verify - check request signature 113*ebfedea0SLionel Sambuc * -noout - don't print stuff out. 114*ebfedea0SLionel Sambuc * -text - print out human readable text. 115*ebfedea0SLionel Sambuc * -nodes - no des encryption 116*ebfedea0SLionel Sambuc * -config file - Load configuration file. 117*ebfedea0SLionel Sambuc * -key file - make a request using key in file (or use it for verification). 118*ebfedea0SLionel Sambuc * -keyform arg - key file format. 119*ebfedea0SLionel Sambuc * -rand file(s) - load the file(s) into the PRNG. 120*ebfedea0SLionel Sambuc * -newkey - make a key and a request. 121*ebfedea0SLionel Sambuc * -modulus - print RSA modulus. 122*ebfedea0SLionel Sambuc * -pubkey - output Public Key. 123*ebfedea0SLionel Sambuc * -x509 - output a self signed X509 structure instead. 124*ebfedea0SLionel Sambuc * -asn1-kludge - output new certificate request in a format that some CA's 125*ebfedea0SLionel Sambuc * require. This format is wrong 126*ebfedea0SLionel Sambuc */ 127*ebfedea0SLionel Sambuc 128*ebfedea0SLionel Sambuc static int make_REQ(X509_REQ *req,EVP_PKEY *pkey,char *dn,int mutlirdn, 129*ebfedea0SLionel Sambuc int attribs,unsigned long chtype); 130*ebfedea0SLionel Sambuc static int build_subject(X509_REQ *req, char *subj, unsigned long chtype, 131*ebfedea0SLionel Sambuc int multirdn); 132*ebfedea0SLionel Sambuc static int prompt_info(X509_REQ *req, 133*ebfedea0SLionel Sambuc STACK_OF(CONF_VALUE) *dn_sk, char *dn_sect, 134*ebfedea0SLionel Sambuc STACK_OF(CONF_VALUE) *attr_sk, char *attr_sect, int attribs, 135*ebfedea0SLionel Sambuc unsigned long chtype); 136*ebfedea0SLionel Sambuc static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *sk, 137*ebfedea0SLionel Sambuc STACK_OF(CONF_VALUE) *attr, int attribs, 138*ebfedea0SLionel Sambuc unsigned long chtype); 139*ebfedea0SLionel Sambuc static int add_attribute_object(X509_REQ *req, char *text, const char *def, 140*ebfedea0SLionel Sambuc char *value, int nid, int n_min, 141*ebfedea0SLionel Sambuc int n_max, unsigned long chtype); 142*ebfedea0SLionel Sambuc static int add_DN_object(X509_NAME *n, char *text, const char *def, char *value, 143*ebfedea0SLionel Sambuc int nid,int n_min,int n_max, unsigned long chtype, int mval); 144*ebfedea0SLionel Sambuc static int genpkey_cb(EVP_PKEY_CTX *ctx); 145*ebfedea0SLionel Sambuc static int req_check_len(int len,int n_min,int n_max); 146*ebfedea0SLionel Sambuc static int check_end(const char *str, const char *end); 147*ebfedea0SLionel Sambuc static EVP_PKEY_CTX *set_keygen_ctx(BIO *err, const char *gstr, int *pkey_type, 148*ebfedea0SLionel Sambuc long *pkeylen, char **palgnam, 149*ebfedea0SLionel Sambuc ENGINE *keygen_engine); 150*ebfedea0SLionel Sambuc #ifndef MONOLITH 151*ebfedea0SLionel Sambuc static char *default_config_file=NULL; 152*ebfedea0SLionel Sambuc #endif 153*ebfedea0SLionel Sambuc static CONF *req_conf=NULL; 154*ebfedea0SLionel Sambuc static int batch=0; 155*ebfedea0SLionel Sambuc 156*ebfedea0SLionel Sambuc int MAIN(int, char **); 157*ebfedea0SLionel Sambuc 158*ebfedea0SLionel Sambuc int MAIN(int argc, char **argv) 159*ebfedea0SLionel Sambuc { 160*ebfedea0SLionel Sambuc ENGINE *e = NULL, *gen_eng = NULL; 161*ebfedea0SLionel Sambuc unsigned long nmflag = 0, reqflag = 0; 162*ebfedea0SLionel Sambuc int ex=1,x509=0,days=30; 163*ebfedea0SLionel Sambuc X509 *x509ss=NULL; 164*ebfedea0SLionel Sambuc X509_REQ *req=NULL; 165*ebfedea0SLionel Sambuc EVP_PKEY_CTX *genctx = NULL; 166*ebfedea0SLionel Sambuc const char *keyalg = NULL; 167*ebfedea0SLionel Sambuc char *keyalgstr = NULL; 168*ebfedea0SLionel Sambuc STACK_OF(OPENSSL_STRING) *pkeyopts = NULL, *sigopts = NULL; 169*ebfedea0SLionel Sambuc EVP_PKEY *pkey=NULL; 170*ebfedea0SLionel Sambuc int i=0,badops=0,newreq=0,verbose=0,pkey_type=-1; 171*ebfedea0SLionel Sambuc long newkey = -1; 172*ebfedea0SLionel Sambuc BIO *in=NULL,*out=NULL; 173*ebfedea0SLionel Sambuc int informat,outformat,verify=0,noout=0,text=0,keyform=FORMAT_PEM; 174*ebfedea0SLionel Sambuc int nodes=0,kludge=0,newhdr=0,subject=0,pubkey=0; 175*ebfedea0SLionel Sambuc char *infile,*outfile,*prog,*keyfile=NULL,*template=NULL,*keyout=NULL; 176*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE 177*ebfedea0SLionel Sambuc char *engine=NULL; 178*ebfedea0SLionel Sambuc #endif 179*ebfedea0SLionel Sambuc char *extensions = NULL; 180*ebfedea0SLionel Sambuc char *req_exts = NULL; 181*ebfedea0SLionel Sambuc const EVP_CIPHER *cipher=NULL; 182*ebfedea0SLionel Sambuc ASN1_INTEGER *serial = NULL; 183*ebfedea0SLionel Sambuc int modulus=0; 184*ebfedea0SLionel Sambuc char *inrand=NULL; 185*ebfedea0SLionel Sambuc char *passargin = NULL, *passargout = NULL; 186*ebfedea0SLionel Sambuc char *passin = NULL, *passout = NULL; 187*ebfedea0SLionel Sambuc char *p; 188*ebfedea0SLionel Sambuc char *subj = NULL; 189*ebfedea0SLionel Sambuc int multirdn = 0; 190*ebfedea0SLionel Sambuc const EVP_MD *md_alg=NULL,*digest=NULL; 191*ebfedea0SLionel Sambuc unsigned long chtype = MBSTRING_ASC; 192*ebfedea0SLionel Sambuc #ifndef MONOLITH 193*ebfedea0SLionel Sambuc char *to_free; 194*ebfedea0SLionel Sambuc long errline; 195*ebfedea0SLionel Sambuc #endif 196*ebfedea0SLionel Sambuc 197*ebfedea0SLionel Sambuc req_conf = NULL; 198*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DES 199*ebfedea0SLionel Sambuc cipher=EVP_des_ede3_cbc(); 200*ebfedea0SLionel Sambuc #endif 201*ebfedea0SLionel Sambuc apps_startup(); 202*ebfedea0SLionel Sambuc 203*ebfedea0SLionel Sambuc if (bio_err == NULL) 204*ebfedea0SLionel Sambuc if ((bio_err=BIO_new(BIO_s_file())) != NULL) 205*ebfedea0SLionel Sambuc BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); 206*ebfedea0SLionel Sambuc 207*ebfedea0SLionel Sambuc infile=NULL; 208*ebfedea0SLionel Sambuc outfile=NULL; 209*ebfedea0SLionel Sambuc informat=FORMAT_PEM; 210*ebfedea0SLionel Sambuc outformat=FORMAT_PEM; 211*ebfedea0SLionel Sambuc 212*ebfedea0SLionel Sambuc prog=argv[0]; 213*ebfedea0SLionel Sambuc argc--; 214*ebfedea0SLionel Sambuc argv++; 215*ebfedea0SLionel Sambuc while (argc >= 1) 216*ebfedea0SLionel Sambuc { 217*ebfedea0SLionel Sambuc if (strcmp(*argv,"-inform") == 0) 218*ebfedea0SLionel Sambuc { 219*ebfedea0SLionel Sambuc if (--argc < 1) goto bad; 220*ebfedea0SLionel Sambuc informat=str2fmt(*(++argv)); 221*ebfedea0SLionel Sambuc } 222*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-outform") == 0) 223*ebfedea0SLionel Sambuc { 224*ebfedea0SLionel Sambuc if (--argc < 1) goto bad; 225*ebfedea0SLionel Sambuc outformat=str2fmt(*(++argv)); 226*ebfedea0SLionel Sambuc } 227*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE 228*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-engine") == 0) 229*ebfedea0SLionel Sambuc { 230*ebfedea0SLionel Sambuc if (--argc < 1) goto bad; 231*ebfedea0SLionel Sambuc engine= *(++argv); 232*ebfedea0SLionel Sambuc } 233*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-keygen_engine") == 0) 234*ebfedea0SLionel Sambuc { 235*ebfedea0SLionel Sambuc if (--argc < 1) goto bad; 236*ebfedea0SLionel Sambuc gen_eng = ENGINE_by_id(*(++argv)); 237*ebfedea0SLionel Sambuc if (gen_eng == NULL) 238*ebfedea0SLionel Sambuc { 239*ebfedea0SLionel Sambuc BIO_printf(bio_err, "Can't find keygen engine %s\n", *argv); 240*ebfedea0SLionel Sambuc goto end; 241*ebfedea0SLionel Sambuc } 242*ebfedea0SLionel Sambuc } 243*ebfedea0SLionel Sambuc #endif 244*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-key") == 0) 245*ebfedea0SLionel Sambuc { 246*ebfedea0SLionel Sambuc if (--argc < 1) goto bad; 247*ebfedea0SLionel Sambuc keyfile= *(++argv); 248*ebfedea0SLionel Sambuc } 249*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-pubkey") == 0) 250*ebfedea0SLionel Sambuc { 251*ebfedea0SLionel Sambuc pubkey=1; 252*ebfedea0SLionel Sambuc } 253*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-new") == 0) 254*ebfedea0SLionel Sambuc { 255*ebfedea0SLionel Sambuc newreq=1; 256*ebfedea0SLionel Sambuc } 257*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-config") == 0) 258*ebfedea0SLionel Sambuc { 259*ebfedea0SLionel Sambuc if (--argc < 1) goto bad; 260*ebfedea0SLionel Sambuc template= *(++argv); 261*ebfedea0SLionel Sambuc } 262*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-keyform") == 0) 263*ebfedea0SLionel Sambuc { 264*ebfedea0SLionel Sambuc if (--argc < 1) goto bad; 265*ebfedea0SLionel Sambuc keyform=str2fmt(*(++argv)); 266*ebfedea0SLionel Sambuc } 267*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-in") == 0) 268*ebfedea0SLionel Sambuc { 269*ebfedea0SLionel Sambuc if (--argc < 1) goto bad; 270*ebfedea0SLionel Sambuc infile= *(++argv); 271*ebfedea0SLionel Sambuc } 272*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-out") == 0) 273*ebfedea0SLionel Sambuc { 274*ebfedea0SLionel Sambuc if (--argc < 1) goto bad; 275*ebfedea0SLionel Sambuc outfile= *(++argv); 276*ebfedea0SLionel Sambuc } 277*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-keyout") == 0) 278*ebfedea0SLionel Sambuc { 279*ebfedea0SLionel Sambuc if (--argc < 1) goto bad; 280*ebfedea0SLionel Sambuc keyout= *(++argv); 281*ebfedea0SLionel Sambuc } 282*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-passin") == 0) 283*ebfedea0SLionel Sambuc { 284*ebfedea0SLionel Sambuc if (--argc < 1) goto bad; 285*ebfedea0SLionel Sambuc passargin= *(++argv); 286*ebfedea0SLionel Sambuc } 287*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-passout") == 0) 288*ebfedea0SLionel Sambuc { 289*ebfedea0SLionel Sambuc if (--argc < 1) goto bad; 290*ebfedea0SLionel Sambuc passargout= *(++argv); 291*ebfedea0SLionel Sambuc } 292*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-rand") == 0) 293*ebfedea0SLionel Sambuc { 294*ebfedea0SLionel Sambuc if (--argc < 1) goto bad; 295*ebfedea0SLionel Sambuc inrand= *(++argv); 296*ebfedea0SLionel Sambuc } 297*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-newkey") == 0) 298*ebfedea0SLionel Sambuc { 299*ebfedea0SLionel Sambuc if (--argc < 1) 300*ebfedea0SLionel Sambuc goto bad; 301*ebfedea0SLionel Sambuc keyalg = *(++argv); 302*ebfedea0SLionel Sambuc newreq=1; 303*ebfedea0SLionel Sambuc } 304*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-pkeyopt") == 0) 305*ebfedea0SLionel Sambuc { 306*ebfedea0SLionel Sambuc if (--argc < 1) 307*ebfedea0SLionel Sambuc goto bad; 308*ebfedea0SLionel Sambuc if (!pkeyopts) 309*ebfedea0SLionel Sambuc pkeyopts = sk_OPENSSL_STRING_new_null(); 310*ebfedea0SLionel Sambuc if (!pkeyopts || !sk_OPENSSL_STRING_push(pkeyopts, *(++argv))) 311*ebfedea0SLionel Sambuc goto bad; 312*ebfedea0SLionel Sambuc } 313*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-sigopt") == 0) 314*ebfedea0SLionel Sambuc { 315*ebfedea0SLionel Sambuc if (--argc < 1) 316*ebfedea0SLionel Sambuc goto bad; 317*ebfedea0SLionel Sambuc if (!sigopts) 318*ebfedea0SLionel Sambuc sigopts = sk_OPENSSL_STRING_new_null(); 319*ebfedea0SLionel Sambuc if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, *(++argv))) 320*ebfedea0SLionel Sambuc goto bad; 321*ebfedea0SLionel Sambuc } 322*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-batch") == 0) 323*ebfedea0SLionel Sambuc batch=1; 324*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-newhdr") == 0) 325*ebfedea0SLionel Sambuc newhdr=1; 326*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-modulus") == 0) 327*ebfedea0SLionel Sambuc modulus=1; 328*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-verify") == 0) 329*ebfedea0SLionel Sambuc verify=1; 330*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-nodes") == 0) 331*ebfedea0SLionel Sambuc nodes=1; 332*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-noout") == 0) 333*ebfedea0SLionel Sambuc noout=1; 334*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-verbose") == 0) 335*ebfedea0SLionel Sambuc verbose=1; 336*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-utf8") == 0) 337*ebfedea0SLionel Sambuc chtype = MBSTRING_UTF8; 338*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-nameopt") == 0) 339*ebfedea0SLionel Sambuc { 340*ebfedea0SLionel Sambuc if (--argc < 1) goto bad; 341*ebfedea0SLionel Sambuc if (!set_name_ex(&nmflag, *(++argv))) goto bad; 342*ebfedea0SLionel Sambuc } 343*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-reqopt") == 0) 344*ebfedea0SLionel Sambuc { 345*ebfedea0SLionel Sambuc if (--argc < 1) goto bad; 346*ebfedea0SLionel Sambuc if (!set_cert_ex(&reqflag, *(++argv))) goto bad; 347*ebfedea0SLionel Sambuc } 348*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-subject") == 0) 349*ebfedea0SLionel Sambuc subject=1; 350*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-text") == 0) 351*ebfedea0SLionel Sambuc text=1; 352*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-x509") == 0) 353*ebfedea0SLionel Sambuc x509=1; 354*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-asn1-kludge") == 0) 355*ebfedea0SLionel Sambuc kludge=1; 356*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-no-asn1-kludge") == 0) 357*ebfedea0SLionel Sambuc kludge=0; 358*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-subj") == 0) 359*ebfedea0SLionel Sambuc { 360*ebfedea0SLionel Sambuc if (--argc < 1) goto bad; 361*ebfedea0SLionel Sambuc subj= *(++argv); 362*ebfedea0SLionel Sambuc } 363*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-multivalue-rdn") == 0) 364*ebfedea0SLionel Sambuc multirdn=1; 365*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-days") == 0) 366*ebfedea0SLionel Sambuc { 367*ebfedea0SLionel Sambuc if (--argc < 1) goto bad; 368*ebfedea0SLionel Sambuc days= atoi(*(++argv)); 369*ebfedea0SLionel Sambuc if (days == 0) days=30; 370*ebfedea0SLionel Sambuc } 371*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-set_serial") == 0) 372*ebfedea0SLionel Sambuc { 373*ebfedea0SLionel Sambuc if (--argc < 1) goto bad; 374*ebfedea0SLionel Sambuc serial = s2i_ASN1_INTEGER(NULL, *(++argv)); 375*ebfedea0SLionel Sambuc if (!serial) goto bad; 376*ebfedea0SLionel Sambuc } 377*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-extensions") == 0) 378*ebfedea0SLionel Sambuc { 379*ebfedea0SLionel Sambuc if (--argc < 1) goto bad; 380*ebfedea0SLionel Sambuc extensions = *(++argv); 381*ebfedea0SLionel Sambuc } 382*ebfedea0SLionel Sambuc else if (strcmp(*argv,"-reqexts") == 0) 383*ebfedea0SLionel Sambuc { 384*ebfedea0SLionel Sambuc if (--argc < 1) goto bad; 385*ebfedea0SLionel Sambuc req_exts = *(++argv); 386*ebfedea0SLionel Sambuc } 387*ebfedea0SLionel Sambuc else if ((md_alg=EVP_get_digestbyname(&((*argv)[1]))) != NULL) 388*ebfedea0SLionel Sambuc { 389*ebfedea0SLionel Sambuc /* ok */ 390*ebfedea0SLionel Sambuc digest=md_alg; 391*ebfedea0SLionel Sambuc } 392*ebfedea0SLionel Sambuc else 393*ebfedea0SLionel Sambuc { 394*ebfedea0SLionel Sambuc BIO_printf(bio_err,"unknown option %s\n",*argv); 395*ebfedea0SLionel Sambuc badops=1; 396*ebfedea0SLionel Sambuc break; 397*ebfedea0SLionel Sambuc } 398*ebfedea0SLionel Sambuc argc--; 399*ebfedea0SLionel Sambuc argv++; 400*ebfedea0SLionel Sambuc } 401*ebfedea0SLionel Sambuc 402*ebfedea0SLionel Sambuc if (badops) 403*ebfedea0SLionel Sambuc { 404*ebfedea0SLionel Sambuc bad: 405*ebfedea0SLionel Sambuc BIO_printf(bio_err,"%s [options] <infile >outfile\n",prog); 406*ebfedea0SLionel Sambuc BIO_printf(bio_err,"where options are\n"); 407*ebfedea0SLionel Sambuc BIO_printf(bio_err," -inform arg input format - DER or PEM\n"); 408*ebfedea0SLionel Sambuc BIO_printf(bio_err," -outform arg output format - DER or PEM\n"); 409*ebfedea0SLionel Sambuc BIO_printf(bio_err," -in arg input file\n"); 410*ebfedea0SLionel Sambuc BIO_printf(bio_err," -out arg output file\n"); 411*ebfedea0SLionel Sambuc BIO_printf(bio_err," -text text form of request\n"); 412*ebfedea0SLionel Sambuc BIO_printf(bio_err," -pubkey output public key\n"); 413*ebfedea0SLionel Sambuc BIO_printf(bio_err," -noout do not output REQ\n"); 414*ebfedea0SLionel Sambuc BIO_printf(bio_err," -verify verify signature on REQ\n"); 415*ebfedea0SLionel Sambuc BIO_printf(bio_err," -modulus RSA modulus\n"); 416*ebfedea0SLionel Sambuc BIO_printf(bio_err," -nodes don't encrypt the output key\n"); 417*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE 418*ebfedea0SLionel Sambuc BIO_printf(bio_err," -engine e use engine e, possibly a hardware device\n"); 419*ebfedea0SLionel Sambuc #endif 420*ebfedea0SLionel Sambuc BIO_printf(bio_err," -subject output the request's subject\n"); 421*ebfedea0SLionel Sambuc BIO_printf(bio_err," -passin private key password source\n"); 422*ebfedea0SLionel Sambuc BIO_printf(bio_err," -key file use the private key contained in file\n"); 423*ebfedea0SLionel Sambuc BIO_printf(bio_err," -keyform arg key file format\n"); 424*ebfedea0SLionel Sambuc BIO_printf(bio_err," -keyout arg file to send the key to\n"); 425*ebfedea0SLionel Sambuc BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR); 426*ebfedea0SLionel Sambuc BIO_printf(bio_err," load the file (or the files in the directory) into\n"); 427*ebfedea0SLionel Sambuc BIO_printf(bio_err," the random number generator\n"); 428*ebfedea0SLionel Sambuc BIO_printf(bio_err," -newkey rsa:bits generate a new RSA key of 'bits' in size\n"); 429*ebfedea0SLionel Sambuc BIO_printf(bio_err," -newkey dsa:file generate a new DSA key, parameters taken from CA in 'file'\n"); 430*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDSA 431*ebfedea0SLionel Sambuc BIO_printf(bio_err," -newkey ec:file generate a new EC key, parameters taken from CA in 'file'\n"); 432*ebfedea0SLionel Sambuc #endif 433*ebfedea0SLionel Sambuc BIO_printf(bio_err," -[digest] Digest to sign with (md5, sha1, md2, mdc2, md4)\n"); 434*ebfedea0SLionel Sambuc BIO_printf(bio_err," -config file request template file.\n"); 435*ebfedea0SLionel Sambuc BIO_printf(bio_err," -subj arg set or modify request subject\n"); 436*ebfedea0SLionel Sambuc BIO_printf(bio_err," -multivalue-rdn enable support for multivalued RDNs\n"); 437*ebfedea0SLionel Sambuc BIO_printf(bio_err," -new new request.\n"); 438*ebfedea0SLionel Sambuc BIO_printf(bio_err," -batch do not ask anything during request generation\n"); 439*ebfedea0SLionel Sambuc BIO_printf(bio_err," -x509 output a x509 structure instead of a cert. req.\n"); 440*ebfedea0SLionel Sambuc BIO_printf(bio_err," -days number of days a certificate generated by -x509 is valid for.\n"); 441*ebfedea0SLionel Sambuc BIO_printf(bio_err," -set_serial serial number to use for a certificate generated by -x509.\n"); 442*ebfedea0SLionel Sambuc BIO_printf(bio_err," -newhdr output \"NEW\" in the header lines\n"); 443*ebfedea0SLionel Sambuc BIO_printf(bio_err," -asn1-kludge Output the 'request' in a format that is wrong but some CA's\n"); 444*ebfedea0SLionel Sambuc BIO_printf(bio_err," have been reported as requiring\n"); 445*ebfedea0SLionel Sambuc BIO_printf(bio_err," -extensions .. specify certificate extension section (override value in config file)\n"); 446*ebfedea0SLionel Sambuc BIO_printf(bio_err," -reqexts .. specify request extension section (override value in config file)\n"); 447*ebfedea0SLionel Sambuc BIO_printf(bio_err," -utf8 input characters are UTF8 (default ASCII)\n"); 448*ebfedea0SLionel Sambuc BIO_printf(bio_err," -nameopt arg - various certificate name options\n"); 449*ebfedea0SLionel Sambuc BIO_printf(bio_err," -reqopt arg - various request text options\n\n"); 450*ebfedea0SLionel Sambuc goto end; 451*ebfedea0SLionel Sambuc } 452*ebfedea0SLionel Sambuc 453*ebfedea0SLionel Sambuc ERR_load_crypto_strings(); 454*ebfedea0SLionel Sambuc if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) { 455*ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error getting passwords\n"); 456*ebfedea0SLionel Sambuc goto end; 457*ebfedea0SLionel Sambuc } 458*ebfedea0SLionel Sambuc 459*ebfedea0SLionel Sambuc #ifndef MONOLITH /* else this has happened in openssl.c (global `config') */ 460*ebfedea0SLionel Sambuc /* Lets load up our environment a little */ 461*ebfedea0SLionel Sambuc p=getenv("OPENSSL_CONF"); 462*ebfedea0SLionel Sambuc if (p == NULL) 463*ebfedea0SLionel Sambuc p=getenv("SSLEAY_CONF"); 464*ebfedea0SLionel Sambuc if (p == NULL) 465*ebfedea0SLionel Sambuc p=to_free=make_config_name(); 466*ebfedea0SLionel Sambuc default_config_file=p; 467*ebfedea0SLionel Sambuc config=NCONF_new(NULL); 468*ebfedea0SLionel Sambuc i=NCONF_load(config, p, &errline); 469*ebfedea0SLionel Sambuc #endif 470*ebfedea0SLionel Sambuc 471*ebfedea0SLionel Sambuc if (template != NULL) 472*ebfedea0SLionel Sambuc { 473*ebfedea0SLionel Sambuc long errline = -1; 474*ebfedea0SLionel Sambuc 475*ebfedea0SLionel Sambuc if( verbose ) 476*ebfedea0SLionel Sambuc BIO_printf(bio_err,"Using configuration from %s\n",template); 477*ebfedea0SLionel Sambuc req_conf=NCONF_new(NULL); 478*ebfedea0SLionel Sambuc i=NCONF_load(req_conf,template,&errline); 479*ebfedea0SLionel Sambuc if (i == 0) 480*ebfedea0SLionel Sambuc { 481*ebfedea0SLionel Sambuc BIO_printf(bio_err,"error on line %ld of %s\n",errline,template); 482*ebfedea0SLionel Sambuc goto end; 483*ebfedea0SLionel Sambuc } 484*ebfedea0SLionel Sambuc } 485*ebfedea0SLionel Sambuc else 486*ebfedea0SLionel Sambuc { 487*ebfedea0SLionel Sambuc req_conf=config; 488*ebfedea0SLionel Sambuc 489*ebfedea0SLionel Sambuc if (req_conf == NULL) 490*ebfedea0SLionel Sambuc { 491*ebfedea0SLionel Sambuc BIO_printf(bio_err,"Unable to load config info from %s\n", default_config_file); 492*ebfedea0SLionel Sambuc if (newreq) 493*ebfedea0SLionel Sambuc goto end; 494*ebfedea0SLionel Sambuc } 495*ebfedea0SLionel Sambuc else if( verbose ) 496*ebfedea0SLionel Sambuc BIO_printf(bio_err,"Using configuration from %s\n", 497*ebfedea0SLionel Sambuc default_config_file); 498*ebfedea0SLionel Sambuc } 499*ebfedea0SLionel Sambuc 500*ebfedea0SLionel Sambuc if (req_conf != NULL) 501*ebfedea0SLionel Sambuc { 502*ebfedea0SLionel Sambuc if (!load_config(bio_err, req_conf)) 503*ebfedea0SLionel Sambuc goto end; 504*ebfedea0SLionel Sambuc p=NCONF_get_string(req_conf,NULL,"oid_file"); 505*ebfedea0SLionel Sambuc if (p == NULL) 506*ebfedea0SLionel Sambuc ERR_clear_error(); 507*ebfedea0SLionel Sambuc if (p != NULL) 508*ebfedea0SLionel Sambuc { 509*ebfedea0SLionel Sambuc BIO *oid_bio; 510*ebfedea0SLionel Sambuc 511*ebfedea0SLionel Sambuc oid_bio=BIO_new_file(p,"r"); 512*ebfedea0SLionel Sambuc if (oid_bio == NULL) 513*ebfedea0SLionel Sambuc { 514*ebfedea0SLionel Sambuc /* 515*ebfedea0SLionel Sambuc BIO_printf(bio_err,"problems opening %s for extra oid's\n",p); 516*ebfedea0SLionel Sambuc ERR_print_errors(bio_err); 517*ebfedea0SLionel Sambuc */ 518*ebfedea0SLionel Sambuc } 519*ebfedea0SLionel Sambuc else 520*ebfedea0SLionel Sambuc { 521*ebfedea0SLionel Sambuc OBJ_create_objects(oid_bio); 522*ebfedea0SLionel Sambuc BIO_free(oid_bio); 523*ebfedea0SLionel Sambuc } 524*ebfedea0SLionel Sambuc } 525*ebfedea0SLionel Sambuc } 526*ebfedea0SLionel Sambuc if(!add_oid_section(bio_err, req_conf)) goto end; 527*ebfedea0SLionel Sambuc 528*ebfedea0SLionel Sambuc if (md_alg == NULL) 529*ebfedea0SLionel Sambuc { 530*ebfedea0SLionel Sambuc p=NCONF_get_string(req_conf,SECTION,"default_md"); 531*ebfedea0SLionel Sambuc if (p == NULL) 532*ebfedea0SLionel Sambuc ERR_clear_error(); 533*ebfedea0SLionel Sambuc if (p != NULL) 534*ebfedea0SLionel Sambuc { 535*ebfedea0SLionel Sambuc if ((md_alg=EVP_get_digestbyname(p)) != NULL) 536*ebfedea0SLionel Sambuc digest=md_alg; 537*ebfedea0SLionel Sambuc } 538*ebfedea0SLionel Sambuc } 539*ebfedea0SLionel Sambuc 540*ebfedea0SLionel Sambuc if (!extensions) 541*ebfedea0SLionel Sambuc { 542*ebfedea0SLionel Sambuc extensions = NCONF_get_string(req_conf, SECTION, V3_EXTENSIONS); 543*ebfedea0SLionel Sambuc if (!extensions) 544*ebfedea0SLionel Sambuc ERR_clear_error(); 545*ebfedea0SLionel Sambuc } 546*ebfedea0SLionel Sambuc if (extensions) { 547*ebfedea0SLionel Sambuc /* Check syntax of file */ 548*ebfedea0SLionel Sambuc X509V3_CTX ctx; 549*ebfedea0SLionel Sambuc X509V3_set_ctx_test(&ctx); 550*ebfedea0SLionel Sambuc X509V3_set_nconf(&ctx, req_conf); 551*ebfedea0SLionel Sambuc if(!X509V3_EXT_add_nconf(req_conf, &ctx, extensions, NULL)) { 552*ebfedea0SLionel Sambuc BIO_printf(bio_err, 553*ebfedea0SLionel Sambuc "Error Loading extension section %s\n", extensions); 554*ebfedea0SLionel Sambuc goto end; 555*ebfedea0SLionel Sambuc } 556*ebfedea0SLionel Sambuc } 557*ebfedea0SLionel Sambuc 558*ebfedea0SLionel Sambuc if(!passin) 559*ebfedea0SLionel Sambuc { 560*ebfedea0SLionel Sambuc passin = NCONF_get_string(req_conf, SECTION, "input_password"); 561*ebfedea0SLionel Sambuc if (!passin) 562*ebfedea0SLionel Sambuc ERR_clear_error(); 563*ebfedea0SLionel Sambuc } 564*ebfedea0SLionel Sambuc 565*ebfedea0SLionel Sambuc if(!passout) 566*ebfedea0SLionel Sambuc { 567*ebfedea0SLionel Sambuc passout = NCONF_get_string(req_conf, SECTION, "output_password"); 568*ebfedea0SLionel Sambuc if (!passout) 569*ebfedea0SLionel Sambuc ERR_clear_error(); 570*ebfedea0SLionel Sambuc } 571*ebfedea0SLionel Sambuc 572*ebfedea0SLionel Sambuc p = NCONF_get_string(req_conf, SECTION, STRING_MASK); 573*ebfedea0SLionel Sambuc if (!p) 574*ebfedea0SLionel Sambuc ERR_clear_error(); 575*ebfedea0SLionel Sambuc 576*ebfedea0SLionel Sambuc if(p && !ASN1_STRING_set_default_mask_asc(p)) { 577*ebfedea0SLionel Sambuc BIO_printf(bio_err, "Invalid global string mask setting %s\n", p); 578*ebfedea0SLionel Sambuc goto end; 579*ebfedea0SLionel Sambuc } 580*ebfedea0SLionel Sambuc 581*ebfedea0SLionel Sambuc if (chtype != MBSTRING_UTF8) 582*ebfedea0SLionel Sambuc { 583*ebfedea0SLionel Sambuc p = NCONF_get_string(req_conf, SECTION, UTF8_IN); 584*ebfedea0SLionel Sambuc if (!p) 585*ebfedea0SLionel Sambuc ERR_clear_error(); 586*ebfedea0SLionel Sambuc else if (!strcmp(p, "yes")) 587*ebfedea0SLionel Sambuc chtype = MBSTRING_UTF8; 588*ebfedea0SLionel Sambuc } 589*ebfedea0SLionel Sambuc 590*ebfedea0SLionel Sambuc 591*ebfedea0SLionel Sambuc if(!req_exts) 592*ebfedea0SLionel Sambuc { 593*ebfedea0SLionel Sambuc req_exts = NCONF_get_string(req_conf, SECTION, REQ_EXTENSIONS); 594*ebfedea0SLionel Sambuc if (!req_exts) 595*ebfedea0SLionel Sambuc ERR_clear_error(); 596*ebfedea0SLionel Sambuc } 597*ebfedea0SLionel Sambuc if(req_exts) { 598*ebfedea0SLionel Sambuc /* Check syntax of file */ 599*ebfedea0SLionel Sambuc X509V3_CTX ctx; 600*ebfedea0SLionel Sambuc X509V3_set_ctx_test(&ctx); 601*ebfedea0SLionel Sambuc X509V3_set_nconf(&ctx, req_conf); 602*ebfedea0SLionel Sambuc if(!X509V3_EXT_add_nconf(req_conf, &ctx, req_exts, NULL)) { 603*ebfedea0SLionel Sambuc BIO_printf(bio_err, 604*ebfedea0SLionel Sambuc "Error Loading request extension section %s\n", 605*ebfedea0SLionel Sambuc req_exts); 606*ebfedea0SLionel Sambuc goto end; 607*ebfedea0SLionel Sambuc } 608*ebfedea0SLionel Sambuc } 609*ebfedea0SLionel Sambuc 610*ebfedea0SLionel Sambuc in=BIO_new(BIO_s_file()); 611*ebfedea0SLionel Sambuc out=BIO_new(BIO_s_file()); 612*ebfedea0SLionel Sambuc if ((in == NULL) || (out == NULL)) 613*ebfedea0SLionel Sambuc goto end; 614*ebfedea0SLionel Sambuc 615*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE 616*ebfedea0SLionel Sambuc e = setup_engine(bio_err, engine, 0); 617*ebfedea0SLionel Sambuc #endif 618*ebfedea0SLionel Sambuc 619*ebfedea0SLionel Sambuc if (keyfile != NULL) 620*ebfedea0SLionel Sambuc { 621*ebfedea0SLionel Sambuc pkey = load_key(bio_err, keyfile, keyform, 0, passin, e, 622*ebfedea0SLionel Sambuc "Private Key"); 623*ebfedea0SLionel Sambuc if (!pkey) 624*ebfedea0SLionel Sambuc { 625*ebfedea0SLionel Sambuc /* load_key() has already printed an appropriate 626*ebfedea0SLionel Sambuc message */ 627*ebfedea0SLionel Sambuc goto end; 628*ebfedea0SLionel Sambuc } 629*ebfedea0SLionel Sambuc else 630*ebfedea0SLionel Sambuc { 631*ebfedea0SLionel Sambuc char *randfile = NCONF_get_string(req_conf,SECTION,"RANDFILE"); 632*ebfedea0SLionel Sambuc if (randfile == NULL) 633*ebfedea0SLionel Sambuc ERR_clear_error(); 634*ebfedea0SLionel Sambuc app_RAND_load_file(randfile, bio_err, 0); 635*ebfedea0SLionel Sambuc } 636*ebfedea0SLionel Sambuc } 637*ebfedea0SLionel Sambuc 638*ebfedea0SLionel Sambuc if (newreq && (pkey == NULL)) 639*ebfedea0SLionel Sambuc { 640*ebfedea0SLionel Sambuc char *randfile = NCONF_get_string(req_conf,SECTION,"RANDFILE"); 641*ebfedea0SLionel Sambuc if (randfile == NULL) 642*ebfedea0SLionel Sambuc ERR_clear_error(); 643*ebfedea0SLionel Sambuc app_RAND_load_file(randfile, bio_err, 0); 644*ebfedea0SLionel Sambuc if (inrand) 645*ebfedea0SLionel Sambuc app_RAND_load_files(inrand); 646*ebfedea0SLionel Sambuc 647*ebfedea0SLionel Sambuc if (keyalg) 648*ebfedea0SLionel Sambuc { 649*ebfedea0SLionel Sambuc genctx = set_keygen_ctx(bio_err, keyalg, &pkey_type, &newkey, 650*ebfedea0SLionel Sambuc &keyalgstr, gen_eng); 651*ebfedea0SLionel Sambuc if (!genctx) 652*ebfedea0SLionel Sambuc goto end; 653*ebfedea0SLionel Sambuc } 654*ebfedea0SLionel Sambuc 655*ebfedea0SLionel Sambuc if (newkey <= 0) 656*ebfedea0SLionel Sambuc { 657*ebfedea0SLionel Sambuc if (!NCONF_get_number(req_conf,SECTION,BITS, &newkey)) 658*ebfedea0SLionel Sambuc newkey=DEFAULT_KEY_LENGTH; 659*ebfedea0SLionel Sambuc } 660*ebfedea0SLionel Sambuc 661*ebfedea0SLionel Sambuc if (newkey < MIN_KEY_LENGTH && (pkey_type == EVP_PKEY_RSA || pkey_type == EVP_PKEY_DSA)) 662*ebfedea0SLionel Sambuc { 663*ebfedea0SLionel Sambuc BIO_printf(bio_err,"private key length is too short,\n"); 664*ebfedea0SLionel Sambuc BIO_printf(bio_err,"it needs to be at least %d bits, not %ld\n",MIN_KEY_LENGTH,newkey); 665*ebfedea0SLionel Sambuc goto end; 666*ebfedea0SLionel Sambuc } 667*ebfedea0SLionel Sambuc 668*ebfedea0SLionel Sambuc if (!genctx) 669*ebfedea0SLionel Sambuc { 670*ebfedea0SLionel Sambuc genctx = set_keygen_ctx(bio_err, NULL, &pkey_type, &newkey, 671*ebfedea0SLionel Sambuc &keyalgstr, gen_eng); 672*ebfedea0SLionel Sambuc if (!genctx) 673*ebfedea0SLionel Sambuc goto end; 674*ebfedea0SLionel Sambuc } 675*ebfedea0SLionel Sambuc 676*ebfedea0SLionel Sambuc if (pkeyopts) 677*ebfedea0SLionel Sambuc { 678*ebfedea0SLionel Sambuc char *genopt; 679*ebfedea0SLionel Sambuc for (i = 0; i < sk_OPENSSL_STRING_num(pkeyopts); i++) 680*ebfedea0SLionel Sambuc { 681*ebfedea0SLionel Sambuc genopt = sk_OPENSSL_STRING_value(pkeyopts, i); 682*ebfedea0SLionel Sambuc if (pkey_ctrl_string(genctx, genopt) <= 0) 683*ebfedea0SLionel Sambuc { 684*ebfedea0SLionel Sambuc BIO_printf(bio_err, 685*ebfedea0SLionel Sambuc "parameter error \"%s\"\n", 686*ebfedea0SLionel Sambuc genopt); 687*ebfedea0SLionel Sambuc ERR_print_errors(bio_err); 688*ebfedea0SLionel Sambuc goto end; 689*ebfedea0SLionel Sambuc } 690*ebfedea0SLionel Sambuc } 691*ebfedea0SLionel Sambuc } 692*ebfedea0SLionel Sambuc 693*ebfedea0SLionel Sambuc BIO_printf(bio_err,"Generating a %ld bit %s private key\n", 694*ebfedea0SLionel Sambuc newkey, keyalgstr); 695*ebfedea0SLionel Sambuc 696*ebfedea0SLionel Sambuc EVP_PKEY_CTX_set_cb(genctx, genpkey_cb); 697*ebfedea0SLionel Sambuc EVP_PKEY_CTX_set_app_data(genctx, bio_err); 698*ebfedea0SLionel Sambuc 699*ebfedea0SLionel Sambuc if (EVP_PKEY_keygen(genctx, &pkey) <= 0) 700*ebfedea0SLionel Sambuc { 701*ebfedea0SLionel Sambuc BIO_puts(bio_err, "Error Generating Key\n"); 702*ebfedea0SLionel Sambuc goto end; 703*ebfedea0SLionel Sambuc } 704*ebfedea0SLionel Sambuc 705*ebfedea0SLionel Sambuc EVP_PKEY_CTX_free(genctx); 706*ebfedea0SLionel Sambuc genctx = NULL; 707*ebfedea0SLionel Sambuc 708*ebfedea0SLionel Sambuc app_RAND_write_file(randfile, bio_err); 709*ebfedea0SLionel Sambuc 710*ebfedea0SLionel Sambuc if (keyout == NULL) 711*ebfedea0SLionel Sambuc { 712*ebfedea0SLionel Sambuc keyout=NCONF_get_string(req_conf,SECTION,KEYFILE); 713*ebfedea0SLionel Sambuc if (keyout == NULL) 714*ebfedea0SLionel Sambuc ERR_clear_error(); 715*ebfedea0SLionel Sambuc } 716*ebfedea0SLionel Sambuc 717*ebfedea0SLionel Sambuc if (keyout == NULL) 718*ebfedea0SLionel Sambuc { 719*ebfedea0SLionel Sambuc BIO_printf(bio_err,"writing new private key to stdout\n"); 720*ebfedea0SLionel Sambuc BIO_set_fp(out,stdout,BIO_NOCLOSE); 721*ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_VMS 722*ebfedea0SLionel Sambuc { 723*ebfedea0SLionel Sambuc BIO *tmpbio = BIO_new(BIO_f_linebuffer()); 724*ebfedea0SLionel Sambuc out = BIO_push(tmpbio, out); 725*ebfedea0SLionel Sambuc } 726*ebfedea0SLionel Sambuc #endif 727*ebfedea0SLionel Sambuc } 728*ebfedea0SLionel Sambuc else 729*ebfedea0SLionel Sambuc { 730*ebfedea0SLionel Sambuc BIO_printf(bio_err,"writing new private key to '%s'\n",keyout); 731*ebfedea0SLionel Sambuc if (BIO_write_filename(out,keyout) <= 0) 732*ebfedea0SLionel Sambuc { 733*ebfedea0SLionel Sambuc perror(keyout); 734*ebfedea0SLionel Sambuc goto end; 735*ebfedea0SLionel Sambuc } 736*ebfedea0SLionel Sambuc } 737*ebfedea0SLionel Sambuc 738*ebfedea0SLionel Sambuc p=NCONF_get_string(req_conf,SECTION,"encrypt_rsa_key"); 739*ebfedea0SLionel Sambuc if (p == NULL) 740*ebfedea0SLionel Sambuc { 741*ebfedea0SLionel Sambuc ERR_clear_error(); 742*ebfedea0SLionel Sambuc p=NCONF_get_string(req_conf,SECTION,"encrypt_key"); 743*ebfedea0SLionel Sambuc if (p == NULL) 744*ebfedea0SLionel Sambuc ERR_clear_error(); 745*ebfedea0SLionel Sambuc } 746*ebfedea0SLionel Sambuc if ((p != NULL) && (strcmp(p,"no") == 0)) 747*ebfedea0SLionel Sambuc cipher=NULL; 748*ebfedea0SLionel Sambuc if (nodes) cipher=NULL; 749*ebfedea0SLionel Sambuc 750*ebfedea0SLionel Sambuc i=0; 751*ebfedea0SLionel Sambuc loop: 752*ebfedea0SLionel Sambuc if (!PEM_write_bio_PrivateKey(out,pkey,cipher, 753*ebfedea0SLionel Sambuc NULL,0,NULL,passout)) 754*ebfedea0SLionel Sambuc { 755*ebfedea0SLionel Sambuc if ((ERR_GET_REASON(ERR_peek_error()) == 756*ebfedea0SLionel Sambuc PEM_R_PROBLEMS_GETTING_PASSWORD) && (i < 3)) 757*ebfedea0SLionel Sambuc { 758*ebfedea0SLionel Sambuc ERR_clear_error(); 759*ebfedea0SLionel Sambuc i++; 760*ebfedea0SLionel Sambuc goto loop; 761*ebfedea0SLionel Sambuc } 762*ebfedea0SLionel Sambuc goto end; 763*ebfedea0SLionel Sambuc } 764*ebfedea0SLionel Sambuc BIO_printf(bio_err,"-----\n"); 765*ebfedea0SLionel Sambuc } 766*ebfedea0SLionel Sambuc 767*ebfedea0SLionel Sambuc if (!newreq) 768*ebfedea0SLionel Sambuc { 769*ebfedea0SLionel Sambuc /* Since we are using a pre-existing certificate 770*ebfedea0SLionel Sambuc * request, the kludge 'format' info should not be 771*ebfedea0SLionel Sambuc * changed. */ 772*ebfedea0SLionel Sambuc kludge= -1; 773*ebfedea0SLionel Sambuc if (infile == NULL) 774*ebfedea0SLionel Sambuc BIO_set_fp(in,stdin,BIO_NOCLOSE); 775*ebfedea0SLionel Sambuc else 776*ebfedea0SLionel Sambuc { 777*ebfedea0SLionel Sambuc if (BIO_read_filename(in,infile) <= 0) 778*ebfedea0SLionel Sambuc { 779*ebfedea0SLionel Sambuc perror(infile); 780*ebfedea0SLionel Sambuc goto end; 781*ebfedea0SLionel Sambuc } 782*ebfedea0SLionel Sambuc } 783*ebfedea0SLionel Sambuc 784*ebfedea0SLionel Sambuc if (informat == FORMAT_ASN1) 785*ebfedea0SLionel Sambuc req=d2i_X509_REQ_bio(in,NULL); 786*ebfedea0SLionel Sambuc else if (informat == FORMAT_PEM) 787*ebfedea0SLionel Sambuc req=PEM_read_bio_X509_REQ(in,NULL,NULL,NULL); 788*ebfedea0SLionel Sambuc else 789*ebfedea0SLionel Sambuc { 790*ebfedea0SLionel Sambuc BIO_printf(bio_err,"bad input format specified for X509 request\n"); 791*ebfedea0SLionel Sambuc goto end; 792*ebfedea0SLionel Sambuc } 793*ebfedea0SLionel Sambuc if (req == NULL) 794*ebfedea0SLionel Sambuc { 795*ebfedea0SLionel Sambuc BIO_printf(bio_err,"unable to load X509 request\n"); 796*ebfedea0SLionel Sambuc goto end; 797*ebfedea0SLionel Sambuc } 798*ebfedea0SLionel Sambuc } 799*ebfedea0SLionel Sambuc 800*ebfedea0SLionel Sambuc if (newreq || x509) 801*ebfedea0SLionel Sambuc { 802*ebfedea0SLionel Sambuc if (pkey == NULL) 803*ebfedea0SLionel Sambuc { 804*ebfedea0SLionel Sambuc BIO_printf(bio_err,"you need to specify a private key\n"); 805*ebfedea0SLionel Sambuc goto end; 806*ebfedea0SLionel Sambuc } 807*ebfedea0SLionel Sambuc 808*ebfedea0SLionel Sambuc if (req == NULL) 809*ebfedea0SLionel Sambuc { 810*ebfedea0SLionel Sambuc req=X509_REQ_new(); 811*ebfedea0SLionel Sambuc if (req == NULL) 812*ebfedea0SLionel Sambuc { 813*ebfedea0SLionel Sambuc goto end; 814*ebfedea0SLionel Sambuc } 815*ebfedea0SLionel Sambuc 816*ebfedea0SLionel Sambuc i=make_REQ(req,pkey,subj,multirdn,!x509, chtype); 817*ebfedea0SLionel Sambuc subj=NULL; /* done processing '-subj' option */ 818*ebfedea0SLionel Sambuc if ((kludge > 0) && !sk_X509_ATTRIBUTE_num(req->req_info->attributes)) 819*ebfedea0SLionel Sambuc { 820*ebfedea0SLionel Sambuc sk_X509_ATTRIBUTE_free(req->req_info->attributes); 821*ebfedea0SLionel Sambuc req->req_info->attributes = NULL; 822*ebfedea0SLionel Sambuc } 823*ebfedea0SLionel Sambuc if (!i) 824*ebfedea0SLionel Sambuc { 825*ebfedea0SLionel Sambuc BIO_printf(bio_err,"problems making Certificate Request\n"); 826*ebfedea0SLionel Sambuc goto end; 827*ebfedea0SLionel Sambuc } 828*ebfedea0SLionel Sambuc } 829*ebfedea0SLionel Sambuc if (x509) 830*ebfedea0SLionel Sambuc { 831*ebfedea0SLionel Sambuc EVP_PKEY *tmppkey; 832*ebfedea0SLionel Sambuc X509V3_CTX ext_ctx; 833*ebfedea0SLionel Sambuc if ((x509ss=X509_new()) == NULL) goto end; 834*ebfedea0SLionel Sambuc 835*ebfedea0SLionel Sambuc /* Set version to V3 */ 836*ebfedea0SLionel Sambuc if(extensions && !X509_set_version(x509ss, 2)) goto end; 837*ebfedea0SLionel Sambuc if (serial) 838*ebfedea0SLionel Sambuc { 839*ebfedea0SLionel Sambuc if (!X509_set_serialNumber(x509ss, serial)) goto end; 840*ebfedea0SLionel Sambuc } 841*ebfedea0SLionel Sambuc else 842*ebfedea0SLionel Sambuc { 843*ebfedea0SLionel Sambuc if (!rand_serial(NULL, 844*ebfedea0SLionel Sambuc X509_get_serialNumber(x509ss))) 845*ebfedea0SLionel Sambuc goto end; 846*ebfedea0SLionel Sambuc } 847*ebfedea0SLionel Sambuc 848*ebfedea0SLionel Sambuc if (!X509_set_issuer_name(x509ss, X509_REQ_get_subject_name(req))) goto end; 849*ebfedea0SLionel Sambuc if (!X509_gmtime_adj(X509_get_notBefore(x509ss),0)) goto end; 850*ebfedea0SLionel Sambuc if (!X509_time_adj_ex(X509_get_notAfter(x509ss), days, 0, NULL)) goto end; 851*ebfedea0SLionel Sambuc if (!X509_set_subject_name(x509ss, X509_REQ_get_subject_name(req))) goto end; 852*ebfedea0SLionel Sambuc tmppkey = X509_REQ_get_pubkey(req); 853*ebfedea0SLionel Sambuc if (!tmppkey || !X509_set_pubkey(x509ss,tmppkey)) goto end; 854*ebfedea0SLionel Sambuc EVP_PKEY_free(tmppkey); 855*ebfedea0SLionel Sambuc 856*ebfedea0SLionel Sambuc /* Set up V3 context struct */ 857*ebfedea0SLionel Sambuc 858*ebfedea0SLionel Sambuc X509V3_set_ctx(&ext_ctx, x509ss, x509ss, NULL, NULL, 0); 859*ebfedea0SLionel Sambuc X509V3_set_nconf(&ext_ctx, req_conf); 860*ebfedea0SLionel Sambuc 861*ebfedea0SLionel Sambuc /* Add extensions */ 862*ebfedea0SLionel Sambuc if(extensions && !X509V3_EXT_add_nconf(req_conf, 863*ebfedea0SLionel Sambuc &ext_ctx, extensions, x509ss)) 864*ebfedea0SLionel Sambuc { 865*ebfedea0SLionel Sambuc BIO_printf(bio_err, 866*ebfedea0SLionel Sambuc "Error Loading extension section %s\n", 867*ebfedea0SLionel Sambuc extensions); 868*ebfedea0SLionel Sambuc goto end; 869*ebfedea0SLionel Sambuc } 870*ebfedea0SLionel Sambuc 871*ebfedea0SLionel Sambuc i=do_X509_sign(bio_err, x509ss, pkey, digest, sigopts); 872*ebfedea0SLionel Sambuc if (!i) 873*ebfedea0SLionel Sambuc { 874*ebfedea0SLionel Sambuc ERR_print_errors(bio_err); 875*ebfedea0SLionel Sambuc goto end; 876*ebfedea0SLionel Sambuc } 877*ebfedea0SLionel Sambuc } 878*ebfedea0SLionel Sambuc else 879*ebfedea0SLionel Sambuc { 880*ebfedea0SLionel Sambuc X509V3_CTX ext_ctx; 881*ebfedea0SLionel Sambuc 882*ebfedea0SLionel Sambuc /* Set up V3 context struct */ 883*ebfedea0SLionel Sambuc 884*ebfedea0SLionel Sambuc X509V3_set_ctx(&ext_ctx, NULL, NULL, req, NULL, 0); 885*ebfedea0SLionel Sambuc X509V3_set_nconf(&ext_ctx, req_conf); 886*ebfedea0SLionel Sambuc 887*ebfedea0SLionel Sambuc /* Add extensions */ 888*ebfedea0SLionel Sambuc if(req_exts && !X509V3_EXT_REQ_add_nconf(req_conf, 889*ebfedea0SLionel Sambuc &ext_ctx, req_exts, req)) 890*ebfedea0SLionel Sambuc { 891*ebfedea0SLionel Sambuc BIO_printf(bio_err, 892*ebfedea0SLionel Sambuc "Error Loading extension section %s\n", 893*ebfedea0SLionel Sambuc req_exts); 894*ebfedea0SLionel Sambuc goto end; 895*ebfedea0SLionel Sambuc } 896*ebfedea0SLionel Sambuc i=do_X509_REQ_sign(bio_err, req, pkey, digest, sigopts); 897*ebfedea0SLionel Sambuc if (!i) 898*ebfedea0SLionel Sambuc { 899*ebfedea0SLionel Sambuc ERR_print_errors(bio_err); 900*ebfedea0SLionel Sambuc goto end; 901*ebfedea0SLionel Sambuc } 902*ebfedea0SLionel Sambuc } 903*ebfedea0SLionel Sambuc } 904*ebfedea0SLionel Sambuc 905*ebfedea0SLionel Sambuc if (subj && x509) 906*ebfedea0SLionel Sambuc { 907*ebfedea0SLionel Sambuc BIO_printf(bio_err, "Cannot modifiy certificate subject\n"); 908*ebfedea0SLionel Sambuc goto end; 909*ebfedea0SLionel Sambuc } 910*ebfedea0SLionel Sambuc 911*ebfedea0SLionel Sambuc if (subj && !x509) 912*ebfedea0SLionel Sambuc { 913*ebfedea0SLionel Sambuc if (verbose) 914*ebfedea0SLionel Sambuc { 915*ebfedea0SLionel Sambuc BIO_printf(bio_err, "Modifying Request's Subject\n"); 916*ebfedea0SLionel Sambuc print_name(bio_err, "old subject=", X509_REQ_get_subject_name(req), nmflag); 917*ebfedea0SLionel Sambuc } 918*ebfedea0SLionel Sambuc 919*ebfedea0SLionel Sambuc if (build_subject(req, subj, chtype, multirdn) == 0) 920*ebfedea0SLionel Sambuc { 921*ebfedea0SLionel Sambuc BIO_printf(bio_err, "ERROR: cannot modify subject\n"); 922*ebfedea0SLionel Sambuc ex=1; 923*ebfedea0SLionel Sambuc goto end; 924*ebfedea0SLionel Sambuc } 925*ebfedea0SLionel Sambuc 926*ebfedea0SLionel Sambuc req->req_info->enc.modified = 1; 927*ebfedea0SLionel Sambuc 928*ebfedea0SLionel Sambuc if (verbose) 929*ebfedea0SLionel Sambuc { 930*ebfedea0SLionel Sambuc print_name(bio_err, "new subject=", X509_REQ_get_subject_name(req), nmflag); 931*ebfedea0SLionel Sambuc } 932*ebfedea0SLionel Sambuc } 933*ebfedea0SLionel Sambuc 934*ebfedea0SLionel Sambuc if (verify && !x509) 935*ebfedea0SLionel Sambuc { 936*ebfedea0SLionel Sambuc int tmp=0; 937*ebfedea0SLionel Sambuc 938*ebfedea0SLionel Sambuc if (pkey == NULL) 939*ebfedea0SLionel Sambuc { 940*ebfedea0SLionel Sambuc pkey=X509_REQ_get_pubkey(req); 941*ebfedea0SLionel Sambuc tmp=1; 942*ebfedea0SLionel Sambuc if (pkey == NULL) goto end; 943*ebfedea0SLionel Sambuc } 944*ebfedea0SLionel Sambuc 945*ebfedea0SLionel Sambuc i=X509_REQ_verify(req,pkey); 946*ebfedea0SLionel Sambuc if (tmp) { 947*ebfedea0SLionel Sambuc EVP_PKEY_free(pkey); 948*ebfedea0SLionel Sambuc pkey=NULL; 949*ebfedea0SLionel Sambuc } 950*ebfedea0SLionel Sambuc 951*ebfedea0SLionel Sambuc if (i < 0) 952*ebfedea0SLionel Sambuc { 953*ebfedea0SLionel Sambuc goto end; 954*ebfedea0SLionel Sambuc } 955*ebfedea0SLionel Sambuc else if (i == 0) 956*ebfedea0SLionel Sambuc { 957*ebfedea0SLionel Sambuc BIO_printf(bio_err,"verify failure\n"); 958*ebfedea0SLionel Sambuc ERR_print_errors(bio_err); 959*ebfedea0SLionel Sambuc } 960*ebfedea0SLionel Sambuc else /* if (i > 0) */ 961*ebfedea0SLionel Sambuc BIO_printf(bio_err,"verify OK\n"); 962*ebfedea0SLionel Sambuc } 963*ebfedea0SLionel Sambuc 964*ebfedea0SLionel Sambuc if (noout && !text && !modulus && !subject && !pubkey) 965*ebfedea0SLionel Sambuc { 966*ebfedea0SLionel Sambuc ex=0; 967*ebfedea0SLionel Sambuc goto end; 968*ebfedea0SLionel Sambuc } 969*ebfedea0SLionel Sambuc 970*ebfedea0SLionel Sambuc if (outfile == NULL) 971*ebfedea0SLionel Sambuc { 972*ebfedea0SLionel Sambuc BIO_set_fp(out,stdout,BIO_NOCLOSE); 973*ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_VMS 974*ebfedea0SLionel Sambuc { 975*ebfedea0SLionel Sambuc BIO *tmpbio = BIO_new(BIO_f_linebuffer()); 976*ebfedea0SLionel Sambuc out = BIO_push(tmpbio, out); 977*ebfedea0SLionel Sambuc } 978*ebfedea0SLionel Sambuc #endif 979*ebfedea0SLionel Sambuc } 980*ebfedea0SLionel Sambuc else 981*ebfedea0SLionel Sambuc { 982*ebfedea0SLionel Sambuc if ((keyout != NULL) && (strcmp(outfile,keyout) == 0)) 983*ebfedea0SLionel Sambuc i=(int)BIO_append_filename(out,outfile); 984*ebfedea0SLionel Sambuc else 985*ebfedea0SLionel Sambuc i=(int)BIO_write_filename(out,outfile); 986*ebfedea0SLionel Sambuc if (!i) 987*ebfedea0SLionel Sambuc { 988*ebfedea0SLionel Sambuc perror(outfile); 989*ebfedea0SLionel Sambuc goto end; 990*ebfedea0SLionel Sambuc } 991*ebfedea0SLionel Sambuc } 992*ebfedea0SLionel Sambuc 993*ebfedea0SLionel Sambuc if (pubkey) 994*ebfedea0SLionel Sambuc { 995*ebfedea0SLionel Sambuc EVP_PKEY *tpubkey; 996*ebfedea0SLionel Sambuc tpubkey=X509_REQ_get_pubkey(req); 997*ebfedea0SLionel Sambuc if (tpubkey == NULL) 998*ebfedea0SLionel Sambuc { 999*ebfedea0SLionel Sambuc BIO_printf(bio_err,"Error getting public key\n"); 1000*ebfedea0SLionel Sambuc ERR_print_errors(bio_err); 1001*ebfedea0SLionel Sambuc goto end; 1002*ebfedea0SLionel Sambuc } 1003*ebfedea0SLionel Sambuc PEM_write_bio_PUBKEY(out, tpubkey); 1004*ebfedea0SLionel Sambuc EVP_PKEY_free(tpubkey); 1005*ebfedea0SLionel Sambuc } 1006*ebfedea0SLionel Sambuc 1007*ebfedea0SLionel Sambuc if (text) 1008*ebfedea0SLionel Sambuc { 1009*ebfedea0SLionel Sambuc if (x509) 1010*ebfedea0SLionel Sambuc X509_print_ex(out, x509ss, nmflag, reqflag); 1011*ebfedea0SLionel Sambuc else 1012*ebfedea0SLionel Sambuc X509_REQ_print_ex(out, req, nmflag, reqflag); 1013*ebfedea0SLionel Sambuc } 1014*ebfedea0SLionel Sambuc 1015*ebfedea0SLionel Sambuc if(subject) 1016*ebfedea0SLionel Sambuc { 1017*ebfedea0SLionel Sambuc if(x509) 1018*ebfedea0SLionel Sambuc print_name(out, "subject=", X509_get_subject_name(x509ss), nmflag); 1019*ebfedea0SLionel Sambuc else 1020*ebfedea0SLionel Sambuc print_name(out, "subject=", X509_REQ_get_subject_name(req), nmflag); 1021*ebfedea0SLionel Sambuc } 1022*ebfedea0SLionel Sambuc 1023*ebfedea0SLionel Sambuc if (modulus) 1024*ebfedea0SLionel Sambuc { 1025*ebfedea0SLionel Sambuc EVP_PKEY *tpubkey; 1026*ebfedea0SLionel Sambuc 1027*ebfedea0SLionel Sambuc if (x509) 1028*ebfedea0SLionel Sambuc tpubkey=X509_get_pubkey(x509ss); 1029*ebfedea0SLionel Sambuc else 1030*ebfedea0SLionel Sambuc tpubkey=X509_REQ_get_pubkey(req); 1031*ebfedea0SLionel Sambuc if (tpubkey == NULL) 1032*ebfedea0SLionel Sambuc { 1033*ebfedea0SLionel Sambuc fprintf(stdout,"Modulus=unavailable\n"); 1034*ebfedea0SLionel Sambuc goto end; 1035*ebfedea0SLionel Sambuc } 1036*ebfedea0SLionel Sambuc fprintf(stdout,"Modulus="); 1037*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA 1038*ebfedea0SLionel Sambuc if (EVP_PKEY_base_id(tpubkey) == EVP_PKEY_RSA) 1039*ebfedea0SLionel Sambuc BN_print(out,tpubkey->pkey.rsa->n); 1040*ebfedea0SLionel Sambuc else 1041*ebfedea0SLionel Sambuc #endif 1042*ebfedea0SLionel Sambuc fprintf(stdout,"Wrong Algorithm type"); 1043*ebfedea0SLionel Sambuc EVP_PKEY_free(tpubkey); 1044*ebfedea0SLionel Sambuc fprintf(stdout,"\n"); 1045*ebfedea0SLionel Sambuc } 1046*ebfedea0SLionel Sambuc 1047*ebfedea0SLionel Sambuc if (!noout && !x509) 1048*ebfedea0SLionel Sambuc { 1049*ebfedea0SLionel Sambuc if (outformat == FORMAT_ASN1) 1050*ebfedea0SLionel Sambuc i=i2d_X509_REQ_bio(out,req); 1051*ebfedea0SLionel Sambuc else if (outformat == FORMAT_PEM) { 1052*ebfedea0SLionel Sambuc if(newhdr) i=PEM_write_bio_X509_REQ_NEW(out,req); 1053*ebfedea0SLionel Sambuc else i=PEM_write_bio_X509_REQ(out,req); 1054*ebfedea0SLionel Sambuc } else { 1055*ebfedea0SLionel Sambuc BIO_printf(bio_err,"bad output format specified for outfile\n"); 1056*ebfedea0SLionel Sambuc goto end; 1057*ebfedea0SLionel Sambuc } 1058*ebfedea0SLionel Sambuc if (!i) 1059*ebfedea0SLionel Sambuc { 1060*ebfedea0SLionel Sambuc BIO_printf(bio_err,"unable to write X509 request\n"); 1061*ebfedea0SLionel Sambuc goto end; 1062*ebfedea0SLionel Sambuc } 1063*ebfedea0SLionel Sambuc } 1064*ebfedea0SLionel Sambuc if (!noout && x509 && (x509ss != NULL)) 1065*ebfedea0SLionel Sambuc { 1066*ebfedea0SLionel Sambuc if (outformat == FORMAT_ASN1) 1067*ebfedea0SLionel Sambuc i=i2d_X509_bio(out,x509ss); 1068*ebfedea0SLionel Sambuc else if (outformat == FORMAT_PEM) 1069*ebfedea0SLionel Sambuc i=PEM_write_bio_X509(out,x509ss); 1070*ebfedea0SLionel Sambuc else { 1071*ebfedea0SLionel Sambuc BIO_printf(bio_err,"bad output format specified for outfile\n"); 1072*ebfedea0SLionel Sambuc goto end; 1073*ebfedea0SLionel Sambuc } 1074*ebfedea0SLionel Sambuc if (!i) 1075*ebfedea0SLionel Sambuc { 1076*ebfedea0SLionel Sambuc BIO_printf(bio_err,"unable to write X509 certificate\n"); 1077*ebfedea0SLionel Sambuc goto end; 1078*ebfedea0SLionel Sambuc } 1079*ebfedea0SLionel Sambuc } 1080*ebfedea0SLionel Sambuc ex=0; 1081*ebfedea0SLionel Sambuc end: 1082*ebfedea0SLionel Sambuc #ifndef MONOLITH 1083*ebfedea0SLionel Sambuc if(to_free) 1084*ebfedea0SLionel Sambuc OPENSSL_free(to_free); 1085*ebfedea0SLionel Sambuc #endif 1086*ebfedea0SLionel Sambuc if (ex) 1087*ebfedea0SLionel Sambuc { 1088*ebfedea0SLionel Sambuc ERR_print_errors(bio_err); 1089*ebfedea0SLionel Sambuc } 1090*ebfedea0SLionel Sambuc if ((req_conf != NULL) && (req_conf != config)) NCONF_free(req_conf); 1091*ebfedea0SLionel Sambuc BIO_free(in); 1092*ebfedea0SLionel Sambuc BIO_free_all(out); 1093*ebfedea0SLionel Sambuc EVP_PKEY_free(pkey); 1094*ebfedea0SLionel Sambuc if (genctx) 1095*ebfedea0SLionel Sambuc EVP_PKEY_CTX_free(genctx); 1096*ebfedea0SLionel Sambuc if (pkeyopts) 1097*ebfedea0SLionel Sambuc sk_OPENSSL_STRING_free(pkeyopts); 1098*ebfedea0SLionel Sambuc if (sigopts) 1099*ebfedea0SLionel Sambuc sk_OPENSSL_STRING_free(sigopts); 1100*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE 1101*ebfedea0SLionel Sambuc if (gen_eng) 1102*ebfedea0SLionel Sambuc ENGINE_free(gen_eng); 1103*ebfedea0SLionel Sambuc #endif 1104*ebfedea0SLionel Sambuc if (keyalgstr) 1105*ebfedea0SLionel Sambuc OPENSSL_free(keyalgstr); 1106*ebfedea0SLionel Sambuc X509_REQ_free(req); 1107*ebfedea0SLionel Sambuc X509_free(x509ss); 1108*ebfedea0SLionel Sambuc ASN1_INTEGER_free(serial); 1109*ebfedea0SLionel Sambuc if(passargin && passin) OPENSSL_free(passin); 1110*ebfedea0SLionel Sambuc if(passargout && passout) OPENSSL_free(passout); 1111*ebfedea0SLionel Sambuc OBJ_cleanup(); 1112*ebfedea0SLionel Sambuc apps_shutdown(); 1113*ebfedea0SLionel Sambuc OPENSSL_EXIT(ex); 1114*ebfedea0SLionel Sambuc } 1115*ebfedea0SLionel Sambuc 1116*ebfedea0SLionel Sambuc static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, char *subj, int multirdn, 1117*ebfedea0SLionel Sambuc int attribs, unsigned long chtype) 1118*ebfedea0SLionel Sambuc { 1119*ebfedea0SLionel Sambuc int ret=0,i; 1120*ebfedea0SLionel Sambuc char no_prompt = 0; 1121*ebfedea0SLionel Sambuc STACK_OF(CONF_VALUE) *dn_sk, *attr_sk = NULL; 1122*ebfedea0SLionel Sambuc char *tmp, *dn_sect,*attr_sect; 1123*ebfedea0SLionel Sambuc 1124*ebfedea0SLionel Sambuc tmp=NCONF_get_string(req_conf,SECTION,PROMPT); 1125*ebfedea0SLionel Sambuc if (tmp == NULL) 1126*ebfedea0SLionel Sambuc ERR_clear_error(); 1127*ebfedea0SLionel Sambuc if((tmp != NULL) && !strcmp(tmp, "no")) no_prompt = 1; 1128*ebfedea0SLionel Sambuc 1129*ebfedea0SLionel Sambuc dn_sect=NCONF_get_string(req_conf,SECTION,DISTINGUISHED_NAME); 1130*ebfedea0SLionel Sambuc if (dn_sect == NULL) 1131*ebfedea0SLionel Sambuc { 1132*ebfedea0SLionel Sambuc BIO_printf(bio_err,"unable to find '%s' in config\n", 1133*ebfedea0SLionel Sambuc DISTINGUISHED_NAME); 1134*ebfedea0SLionel Sambuc goto err; 1135*ebfedea0SLionel Sambuc } 1136*ebfedea0SLionel Sambuc dn_sk=NCONF_get_section(req_conf,dn_sect); 1137*ebfedea0SLionel Sambuc if (dn_sk == NULL) 1138*ebfedea0SLionel Sambuc { 1139*ebfedea0SLionel Sambuc BIO_printf(bio_err,"unable to get '%s' section\n",dn_sect); 1140*ebfedea0SLionel Sambuc goto err; 1141*ebfedea0SLionel Sambuc } 1142*ebfedea0SLionel Sambuc 1143*ebfedea0SLionel Sambuc attr_sect=NCONF_get_string(req_conf,SECTION,ATTRIBUTES); 1144*ebfedea0SLionel Sambuc if (attr_sect == NULL) 1145*ebfedea0SLionel Sambuc { 1146*ebfedea0SLionel Sambuc ERR_clear_error(); 1147*ebfedea0SLionel Sambuc attr_sk=NULL; 1148*ebfedea0SLionel Sambuc } 1149*ebfedea0SLionel Sambuc else 1150*ebfedea0SLionel Sambuc { 1151*ebfedea0SLionel Sambuc attr_sk=NCONF_get_section(req_conf,attr_sect); 1152*ebfedea0SLionel Sambuc if (attr_sk == NULL) 1153*ebfedea0SLionel Sambuc { 1154*ebfedea0SLionel Sambuc BIO_printf(bio_err,"unable to get '%s' section\n",attr_sect); 1155*ebfedea0SLionel Sambuc goto err; 1156*ebfedea0SLionel Sambuc } 1157*ebfedea0SLionel Sambuc } 1158*ebfedea0SLionel Sambuc 1159*ebfedea0SLionel Sambuc /* setup version number */ 1160*ebfedea0SLionel Sambuc if (!X509_REQ_set_version(req,0L)) goto err; /* version 1 */ 1161*ebfedea0SLionel Sambuc 1162*ebfedea0SLionel Sambuc if (no_prompt) 1163*ebfedea0SLionel Sambuc i = auto_info(req, dn_sk, attr_sk, attribs, chtype); 1164*ebfedea0SLionel Sambuc else 1165*ebfedea0SLionel Sambuc { 1166*ebfedea0SLionel Sambuc if (subj) 1167*ebfedea0SLionel Sambuc i = build_subject(req, subj, chtype, multirdn); 1168*ebfedea0SLionel Sambuc else 1169*ebfedea0SLionel Sambuc i = prompt_info(req, dn_sk, dn_sect, attr_sk, attr_sect, attribs, chtype); 1170*ebfedea0SLionel Sambuc } 1171*ebfedea0SLionel Sambuc if(!i) goto err; 1172*ebfedea0SLionel Sambuc 1173*ebfedea0SLionel Sambuc if (!X509_REQ_set_pubkey(req,pkey)) goto err; 1174*ebfedea0SLionel Sambuc 1175*ebfedea0SLionel Sambuc ret=1; 1176*ebfedea0SLionel Sambuc err: 1177*ebfedea0SLionel Sambuc return(ret); 1178*ebfedea0SLionel Sambuc } 1179*ebfedea0SLionel Sambuc 1180*ebfedea0SLionel Sambuc /* 1181*ebfedea0SLionel Sambuc * subject is expected to be in the format /type0=value0/type1=value1/type2=... 1182*ebfedea0SLionel Sambuc * where characters may be escaped by \ 1183*ebfedea0SLionel Sambuc */ 1184*ebfedea0SLionel Sambuc static int build_subject(X509_REQ *req, char *subject, unsigned long chtype, int multirdn) 1185*ebfedea0SLionel Sambuc { 1186*ebfedea0SLionel Sambuc X509_NAME *n; 1187*ebfedea0SLionel Sambuc 1188*ebfedea0SLionel Sambuc if (!(n = parse_name(subject, chtype, multirdn))) 1189*ebfedea0SLionel Sambuc return 0; 1190*ebfedea0SLionel Sambuc 1191*ebfedea0SLionel Sambuc if (!X509_REQ_set_subject_name(req, n)) 1192*ebfedea0SLionel Sambuc { 1193*ebfedea0SLionel Sambuc X509_NAME_free(n); 1194*ebfedea0SLionel Sambuc return 0; 1195*ebfedea0SLionel Sambuc } 1196*ebfedea0SLionel Sambuc X509_NAME_free(n); 1197*ebfedea0SLionel Sambuc return 1; 1198*ebfedea0SLionel Sambuc } 1199*ebfedea0SLionel Sambuc 1200*ebfedea0SLionel Sambuc 1201*ebfedea0SLionel Sambuc static int prompt_info(X509_REQ *req, 1202*ebfedea0SLionel Sambuc STACK_OF(CONF_VALUE) *dn_sk, char *dn_sect, 1203*ebfedea0SLionel Sambuc STACK_OF(CONF_VALUE) *attr_sk, char *attr_sect, int attribs, 1204*ebfedea0SLionel Sambuc unsigned long chtype) 1205*ebfedea0SLionel Sambuc { 1206*ebfedea0SLionel Sambuc int i; 1207*ebfedea0SLionel Sambuc char *p,*q; 1208*ebfedea0SLionel Sambuc char buf[100]; 1209*ebfedea0SLionel Sambuc int nid, mval; 1210*ebfedea0SLionel Sambuc long n_min,n_max; 1211*ebfedea0SLionel Sambuc char *type, *value; 1212*ebfedea0SLionel Sambuc const char *def; 1213*ebfedea0SLionel Sambuc CONF_VALUE *v; 1214*ebfedea0SLionel Sambuc X509_NAME *subj; 1215*ebfedea0SLionel Sambuc subj = X509_REQ_get_subject_name(req); 1216*ebfedea0SLionel Sambuc 1217*ebfedea0SLionel Sambuc if(!batch) 1218*ebfedea0SLionel Sambuc { 1219*ebfedea0SLionel Sambuc BIO_printf(bio_err,"You are about to be asked to enter information that will be incorporated\n"); 1220*ebfedea0SLionel Sambuc BIO_printf(bio_err,"into your certificate request.\n"); 1221*ebfedea0SLionel Sambuc BIO_printf(bio_err,"What you are about to enter is what is called a Distinguished Name or a DN.\n"); 1222*ebfedea0SLionel Sambuc BIO_printf(bio_err,"There are quite a few fields but you can leave some blank\n"); 1223*ebfedea0SLionel Sambuc BIO_printf(bio_err,"For some fields there will be a default value,\n"); 1224*ebfedea0SLionel Sambuc BIO_printf(bio_err,"If you enter '.', the field will be left blank.\n"); 1225*ebfedea0SLionel Sambuc BIO_printf(bio_err,"-----\n"); 1226*ebfedea0SLionel Sambuc } 1227*ebfedea0SLionel Sambuc 1228*ebfedea0SLionel Sambuc 1229*ebfedea0SLionel Sambuc if (sk_CONF_VALUE_num(dn_sk)) 1230*ebfedea0SLionel Sambuc { 1231*ebfedea0SLionel Sambuc i= -1; 1232*ebfedea0SLionel Sambuc start: for (;;) 1233*ebfedea0SLionel Sambuc { 1234*ebfedea0SLionel Sambuc i++; 1235*ebfedea0SLionel Sambuc if (sk_CONF_VALUE_num(dn_sk) <= i) break; 1236*ebfedea0SLionel Sambuc 1237*ebfedea0SLionel Sambuc v=sk_CONF_VALUE_value(dn_sk,i); 1238*ebfedea0SLionel Sambuc p=q=NULL; 1239*ebfedea0SLionel Sambuc type=v->name; 1240*ebfedea0SLionel Sambuc if(!check_end(type,"_min") || !check_end(type,"_max") || 1241*ebfedea0SLionel Sambuc !check_end(type,"_default") || 1242*ebfedea0SLionel Sambuc !check_end(type,"_value")) continue; 1243*ebfedea0SLionel Sambuc /* Skip past any leading X. X: X, etc to allow for 1244*ebfedea0SLionel Sambuc * multiple instances 1245*ebfedea0SLionel Sambuc */ 1246*ebfedea0SLionel Sambuc for(p = v->name; *p ; p++) 1247*ebfedea0SLionel Sambuc if ((*p == ':') || (*p == ',') || 1248*ebfedea0SLionel Sambuc (*p == '.')) { 1249*ebfedea0SLionel Sambuc p++; 1250*ebfedea0SLionel Sambuc if(*p) type = p; 1251*ebfedea0SLionel Sambuc break; 1252*ebfedea0SLionel Sambuc } 1253*ebfedea0SLionel Sambuc if (*type == '+') 1254*ebfedea0SLionel Sambuc { 1255*ebfedea0SLionel Sambuc mval = -1; 1256*ebfedea0SLionel Sambuc type++; 1257*ebfedea0SLionel Sambuc } 1258*ebfedea0SLionel Sambuc else 1259*ebfedea0SLionel Sambuc mval = 0; 1260*ebfedea0SLionel Sambuc /* If OBJ not recognised ignore it */ 1261*ebfedea0SLionel Sambuc if ((nid=OBJ_txt2nid(type)) == NID_undef) goto start; 1262*ebfedea0SLionel Sambuc if (BIO_snprintf(buf,sizeof buf,"%s_default",v->name) 1263*ebfedea0SLionel Sambuc >= (int)sizeof(buf)) 1264*ebfedea0SLionel Sambuc { 1265*ebfedea0SLionel Sambuc BIO_printf(bio_err,"Name '%s' too long\n",v->name); 1266*ebfedea0SLionel Sambuc return 0; 1267*ebfedea0SLionel Sambuc } 1268*ebfedea0SLionel Sambuc 1269*ebfedea0SLionel Sambuc if ((def=NCONF_get_string(req_conf,dn_sect,buf)) == NULL) 1270*ebfedea0SLionel Sambuc { 1271*ebfedea0SLionel Sambuc ERR_clear_error(); 1272*ebfedea0SLionel Sambuc def=""; 1273*ebfedea0SLionel Sambuc } 1274*ebfedea0SLionel Sambuc 1275*ebfedea0SLionel Sambuc BIO_snprintf(buf,sizeof buf,"%s_value",v->name); 1276*ebfedea0SLionel Sambuc if ((value=NCONF_get_string(req_conf,dn_sect,buf)) == NULL) 1277*ebfedea0SLionel Sambuc { 1278*ebfedea0SLionel Sambuc ERR_clear_error(); 1279*ebfedea0SLionel Sambuc value=NULL; 1280*ebfedea0SLionel Sambuc } 1281*ebfedea0SLionel Sambuc 1282*ebfedea0SLionel Sambuc BIO_snprintf(buf,sizeof buf,"%s_min",v->name); 1283*ebfedea0SLionel Sambuc if (!NCONF_get_number(req_conf,dn_sect,buf, &n_min)) 1284*ebfedea0SLionel Sambuc { 1285*ebfedea0SLionel Sambuc ERR_clear_error(); 1286*ebfedea0SLionel Sambuc n_min = -1; 1287*ebfedea0SLionel Sambuc } 1288*ebfedea0SLionel Sambuc 1289*ebfedea0SLionel Sambuc BIO_snprintf(buf,sizeof buf,"%s_max",v->name); 1290*ebfedea0SLionel Sambuc if (!NCONF_get_number(req_conf,dn_sect,buf, &n_max)) 1291*ebfedea0SLionel Sambuc { 1292*ebfedea0SLionel Sambuc ERR_clear_error(); 1293*ebfedea0SLionel Sambuc n_max = -1; 1294*ebfedea0SLionel Sambuc } 1295*ebfedea0SLionel Sambuc 1296*ebfedea0SLionel Sambuc if (!add_DN_object(subj,v->value,def,value,nid, 1297*ebfedea0SLionel Sambuc n_min,n_max, chtype, mval)) 1298*ebfedea0SLionel Sambuc return 0; 1299*ebfedea0SLionel Sambuc } 1300*ebfedea0SLionel Sambuc if (X509_NAME_entry_count(subj) == 0) 1301*ebfedea0SLionel Sambuc { 1302*ebfedea0SLionel Sambuc BIO_printf(bio_err,"error, no objects specified in config file\n"); 1303*ebfedea0SLionel Sambuc return 0; 1304*ebfedea0SLionel Sambuc } 1305*ebfedea0SLionel Sambuc 1306*ebfedea0SLionel Sambuc if (attribs) 1307*ebfedea0SLionel Sambuc { 1308*ebfedea0SLionel Sambuc if ((attr_sk != NULL) && (sk_CONF_VALUE_num(attr_sk) > 0) && (!batch)) 1309*ebfedea0SLionel Sambuc { 1310*ebfedea0SLionel Sambuc BIO_printf(bio_err,"\nPlease enter the following 'extra' attributes\n"); 1311*ebfedea0SLionel Sambuc BIO_printf(bio_err,"to be sent with your certificate request\n"); 1312*ebfedea0SLionel Sambuc } 1313*ebfedea0SLionel Sambuc 1314*ebfedea0SLionel Sambuc i= -1; 1315*ebfedea0SLionel Sambuc start2: for (;;) 1316*ebfedea0SLionel Sambuc { 1317*ebfedea0SLionel Sambuc i++; 1318*ebfedea0SLionel Sambuc if ((attr_sk == NULL) || 1319*ebfedea0SLionel Sambuc (sk_CONF_VALUE_num(attr_sk) <= i)) 1320*ebfedea0SLionel Sambuc break; 1321*ebfedea0SLionel Sambuc 1322*ebfedea0SLionel Sambuc v=sk_CONF_VALUE_value(attr_sk,i); 1323*ebfedea0SLionel Sambuc type=v->name; 1324*ebfedea0SLionel Sambuc if ((nid=OBJ_txt2nid(type)) == NID_undef) 1325*ebfedea0SLionel Sambuc goto start2; 1326*ebfedea0SLionel Sambuc 1327*ebfedea0SLionel Sambuc if (BIO_snprintf(buf,sizeof buf,"%s_default",type) 1328*ebfedea0SLionel Sambuc >= (int)sizeof(buf)) 1329*ebfedea0SLionel Sambuc { 1330*ebfedea0SLionel Sambuc BIO_printf(bio_err,"Name '%s' too long\n",v->name); 1331*ebfedea0SLionel Sambuc return 0; 1332*ebfedea0SLionel Sambuc } 1333*ebfedea0SLionel Sambuc 1334*ebfedea0SLionel Sambuc if ((def=NCONF_get_string(req_conf,attr_sect,buf)) 1335*ebfedea0SLionel Sambuc == NULL) 1336*ebfedea0SLionel Sambuc { 1337*ebfedea0SLionel Sambuc ERR_clear_error(); 1338*ebfedea0SLionel Sambuc def=""; 1339*ebfedea0SLionel Sambuc } 1340*ebfedea0SLionel Sambuc 1341*ebfedea0SLionel Sambuc 1342*ebfedea0SLionel Sambuc BIO_snprintf(buf,sizeof buf,"%s_value",type); 1343*ebfedea0SLionel Sambuc if ((value=NCONF_get_string(req_conf,attr_sect,buf)) 1344*ebfedea0SLionel Sambuc == NULL) 1345*ebfedea0SLionel Sambuc { 1346*ebfedea0SLionel Sambuc ERR_clear_error(); 1347*ebfedea0SLionel Sambuc value=NULL; 1348*ebfedea0SLionel Sambuc } 1349*ebfedea0SLionel Sambuc 1350*ebfedea0SLionel Sambuc BIO_snprintf(buf,sizeof buf,"%s_min",type); 1351*ebfedea0SLionel Sambuc if (!NCONF_get_number(req_conf,attr_sect,buf, &n_min)) 1352*ebfedea0SLionel Sambuc { 1353*ebfedea0SLionel Sambuc ERR_clear_error(); 1354*ebfedea0SLionel Sambuc n_min = -1; 1355*ebfedea0SLionel Sambuc } 1356*ebfedea0SLionel Sambuc 1357*ebfedea0SLionel Sambuc BIO_snprintf(buf,sizeof buf,"%s_max",type); 1358*ebfedea0SLionel Sambuc if (!NCONF_get_number(req_conf,attr_sect,buf, &n_max)) 1359*ebfedea0SLionel Sambuc { 1360*ebfedea0SLionel Sambuc ERR_clear_error(); 1361*ebfedea0SLionel Sambuc n_max = -1; 1362*ebfedea0SLionel Sambuc } 1363*ebfedea0SLionel Sambuc 1364*ebfedea0SLionel Sambuc if (!add_attribute_object(req, 1365*ebfedea0SLionel Sambuc v->value,def,value,nid,n_min,n_max, chtype)) 1366*ebfedea0SLionel Sambuc return 0; 1367*ebfedea0SLionel Sambuc } 1368*ebfedea0SLionel Sambuc } 1369*ebfedea0SLionel Sambuc } 1370*ebfedea0SLionel Sambuc else 1371*ebfedea0SLionel Sambuc { 1372*ebfedea0SLionel Sambuc BIO_printf(bio_err,"No template, please set one up.\n"); 1373*ebfedea0SLionel Sambuc return 0; 1374*ebfedea0SLionel Sambuc } 1375*ebfedea0SLionel Sambuc 1376*ebfedea0SLionel Sambuc return 1; 1377*ebfedea0SLionel Sambuc 1378*ebfedea0SLionel Sambuc } 1379*ebfedea0SLionel Sambuc 1380*ebfedea0SLionel Sambuc static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *dn_sk, 1381*ebfedea0SLionel Sambuc STACK_OF(CONF_VALUE) *attr_sk, int attribs, unsigned long chtype) 1382*ebfedea0SLionel Sambuc { 1383*ebfedea0SLionel Sambuc int i; 1384*ebfedea0SLionel Sambuc char *p,*q; 1385*ebfedea0SLionel Sambuc char *type; 1386*ebfedea0SLionel Sambuc CONF_VALUE *v; 1387*ebfedea0SLionel Sambuc X509_NAME *subj; 1388*ebfedea0SLionel Sambuc 1389*ebfedea0SLionel Sambuc subj = X509_REQ_get_subject_name(req); 1390*ebfedea0SLionel Sambuc 1391*ebfedea0SLionel Sambuc for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) 1392*ebfedea0SLionel Sambuc { 1393*ebfedea0SLionel Sambuc int mval; 1394*ebfedea0SLionel Sambuc v=sk_CONF_VALUE_value(dn_sk,i); 1395*ebfedea0SLionel Sambuc p=q=NULL; 1396*ebfedea0SLionel Sambuc type=v->name; 1397*ebfedea0SLionel Sambuc /* Skip past any leading X. X: X, etc to allow for 1398*ebfedea0SLionel Sambuc * multiple instances 1399*ebfedea0SLionel Sambuc */ 1400*ebfedea0SLionel Sambuc for(p = v->name; *p ; p++) 1401*ebfedea0SLionel Sambuc #ifndef CHARSET_EBCDIC 1402*ebfedea0SLionel Sambuc if ((*p == ':') || (*p == ',') || (*p == '.')) { 1403*ebfedea0SLionel Sambuc #else 1404*ebfedea0SLionel Sambuc if ((*p == os_toascii[':']) || (*p == os_toascii[',']) || (*p == os_toascii['.'])) { 1405*ebfedea0SLionel Sambuc #endif 1406*ebfedea0SLionel Sambuc p++; 1407*ebfedea0SLionel Sambuc if(*p) type = p; 1408*ebfedea0SLionel Sambuc break; 1409*ebfedea0SLionel Sambuc } 1410*ebfedea0SLionel Sambuc #ifndef CHARSET_EBCDIC 1411*ebfedea0SLionel Sambuc if (*p == '+') 1412*ebfedea0SLionel Sambuc #else 1413*ebfedea0SLionel Sambuc if (*p == os_toascii['+']) 1414*ebfedea0SLionel Sambuc #endif 1415*ebfedea0SLionel Sambuc { 1416*ebfedea0SLionel Sambuc p++; 1417*ebfedea0SLionel Sambuc mval = -1; 1418*ebfedea0SLionel Sambuc } 1419*ebfedea0SLionel Sambuc else 1420*ebfedea0SLionel Sambuc mval = 0; 1421*ebfedea0SLionel Sambuc if (!X509_NAME_add_entry_by_txt(subj,type, chtype, 1422*ebfedea0SLionel Sambuc (unsigned char *) v->value,-1,-1,mval)) return 0; 1423*ebfedea0SLionel Sambuc 1424*ebfedea0SLionel Sambuc } 1425*ebfedea0SLionel Sambuc 1426*ebfedea0SLionel Sambuc if (!X509_NAME_entry_count(subj)) 1427*ebfedea0SLionel Sambuc { 1428*ebfedea0SLionel Sambuc BIO_printf(bio_err,"error, no objects specified in config file\n"); 1429*ebfedea0SLionel Sambuc return 0; 1430*ebfedea0SLionel Sambuc } 1431*ebfedea0SLionel Sambuc if (attribs) 1432*ebfedea0SLionel Sambuc { 1433*ebfedea0SLionel Sambuc for (i = 0; i < sk_CONF_VALUE_num(attr_sk); i++) 1434*ebfedea0SLionel Sambuc { 1435*ebfedea0SLionel Sambuc v=sk_CONF_VALUE_value(attr_sk,i); 1436*ebfedea0SLionel Sambuc if(!X509_REQ_add1_attr_by_txt(req, v->name, chtype, 1437*ebfedea0SLionel Sambuc (unsigned char *)v->value, -1)) return 0; 1438*ebfedea0SLionel Sambuc } 1439*ebfedea0SLionel Sambuc } 1440*ebfedea0SLionel Sambuc return 1; 1441*ebfedea0SLionel Sambuc } 1442*ebfedea0SLionel Sambuc 1443*ebfedea0SLionel Sambuc 1444*ebfedea0SLionel Sambuc static int add_DN_object(X509_NAME *n, char *text, const char *def, char *value, 1445*ebfedea0SLionel Sambuc int nid, int n_min, int n_max, unsigned long chtype, int mval) 1446*ebfedea0SLionel Sambuc { 1447*ebfedea0SLionel Sambuc int i,ret=0; 1448*ebfedea0SLionel Sambuc MS_STATIC char buf[1024]; 1449*ebfedea0SLionel Sambuc start: 1450*ebfedea0SLionel Sambuc if (!batch) BIO_printf(bio_err,"%s [%s]:",text,def); 1451*ebfedea0SLionel Sambuc (void)BIO_flush(bio_err); 1452*ebfedea0SLionel Sambuc if(value != NULL) 1453*ebfedea0SLionel Sambuc { 1454*ebfedea0SLionel Sambuc BUF_strlcpy(buf,value,sizeof buf); 1455*ebfedea0SLionel Sambuc BUF_strlcat(buf,"\n",sizeof buf); 1456*ebfedea0SLionel Sambuc BIO_printf(bio_err,"%s\n",value); 1457*ebfedea0SLionel Sambuc } 1458*ebfedea0SLionel Sambuc else 1459*ebfedea0SLionel Sambuc { 1460*ebfedea0SLionel Sambuc buf[0]='\0'; 1461*ebfedea0SLionel Sambuc if (!batch) 1462*ebfedea0SLionel Sambuc { 1463*ebfedea0SLionel Sambuc if (!fgets(buf,sizeof buf,stdin)) 1464*ebfedea0SLionel Sambuc return 0; 1465*ebfedea0SLionel Sambuc } 1466*ebfedea0SLionel Sambuc else 1467*ebfedea0SLionel Sambuc { 1468*ebfedea0SLionel Sambuc buf[0] = '\n'; 1469*ebfedea0SLionel Sambuc buf[1] = '\0'; 1470*ebfedea0SLionel Sambuc } 1471*ebfedea0SLionel Sambuc } 1472*ebfedea0SLionel Sambuc 1473*ebfedea0SLionel Sambuc if (buf[0] == '\0') return(0); 1474*ebfedea0SLionel Sambuc else if (buf[0] == '\n') 1475*ebfedea0SLionel Sambuc { 1476*ebfedea0SLionel Sambuc if ((def == NULL) || (def[0] == '\0')) 1477*ebfedea0SLionel Sambuc return(1); 1478*ebfedea0SLionel Sambuc BUF_strlcpy(buf,def,sizeof buf); 1479*ebfedea0SLionel Sambuc BUF_strlcat(buf,"\n",sizeof buf); 1480*ebfedea0SLionel Sambuc } 1481*ebfedea0SLionel Sambuc else if ((buf[0] == '.') && (buf[1] == '\n')) return(1); 1482*ebfedea0SLionel Sambuc 1483*ebfedea0SLionel Sambuc i=strlen(buf); 1484*ebfedea0SLionel Sambuc if (buf[i-1] != '\n') 1485*ebfedea0SLionel Sambuc { 1486*ebfedea0SLionel Sambuc BIO_printf(bio_err,"weird input :-(\n"); 1487*ebfedea0SLionel Sambuc return(0); 1488*ebfedea0SLionel Sambuc } 1489*ebfedea0SLionel Sambuc buf[--i]='\0'; 1490*ebfedea0SLionel Sambuc #ifdef CHARSET_EBCDIC 1491*ebfedea0SLionel Sambuc ebcdic2ascii(buf, buf, i); 1492*ebfedea0SLionel Sambuc #endif 1493*ebfedea0SLionel Sambuc if(!req_check_len(i, n_min, n_max)) goto start; 1494*ebfedea0SLionel Sambuc if (!X509_NAME_add_entry_by_NID(n,nid, chtype, 1495*ebfedea0SLionel Sambuc (unsigned char *) buf, -1,-1,mval)) goto err; 1496*ebfedea0SLionel Sambuc ret=1; 1497*ebfedea0SLionel Sambuc err: 1498*ebfedea0SLionel Sambuc return(ret); 1499*ebfedea0SLionel Sambuc } 1500*ebfedea0SLionel Sambuc 1501*ebfedea0SLionel Sambuc static int add_attribute_object(X509_REQ *req, char *text, const char *def, 1502*ebfedea0SLionel Sambuc char *value, int nid, int n_min, 1503*ebfedea0SLionel Sambuc int n_max, unsigned long chtype) 1504*ebfedea0SLionel Sambuc { 1505*ebfedea0SLionel Sambuc int i; 1506*ebfedea0SLionel Sambuc static char buf[1024]; 1507*ebfedea0SLionel Sambuc 1508*ebfedea0SLionel Sambuc start: 1509*ebfedea0SLionel Sambuc if (!batch) BIO_printf(bio_err,"%s [%s]:",text,def); 1510*ebfedea0SLionel Sambuc (void)BIO_flush(bio_err); 1511*ebfedea0SLionel Sambuc if (value != NULL) 1512*ebfedea0SLionel Sambuc { 1513*ebfedea0SLionel Sambuc BUF_strlcpy(buf,value,sizeof buf); 1514*ebfedea0SLionel Sambuc BUF_strlcat(buf,"\n",sizeof buf); 1515*ebfedea0SLionel Sambuc BIO_printf(bio_err,"%s\n",value); 1516*ebfedea0SLionel Sambuc } 1517*ebfedea0SLionel Sambuc else 1518*ebfedea0SLionel Sambuc { 1519*ebfedea0SLionel Sambuc buf[0]='\0'; 1520*ebfedea0SLionel Sambuc if (!batch) 1521*ebfedea0SLionel Sambuc { 1522*ebfedea0SLionel Sambuc if (!fgets(buf,sizeof buf,stdin)) 1523*ebfedea0SLionel Sambuc return 0; 1524*ebfedea0SLionel Sambuc } 1525*ebfedea0SLionel Sambuc else 1526*ebfedea0SLionel Sambuc { 1527*ebfedea0SLionel Sambuc buf[0] = '\n'; 1528*ebfedea0SLionel Sambuc buf[1] = '\0'; 1529*ebfedea0SLionel Sambuc } 1530*ebfedea0SLionel Sambuc } 1531*ebfedea0SLionel Sambuc 1532*ebfedea0SLionel Sambuc if (buf[0] == '\0') return(0); 1533*ebfedea0SLionel Sambuc else if (buf[0] == '\n') 1534*ebfedea0SLionel Sambuc { 1535*ebfedea0SLionel Sambuc if ((def == NULL) || (def[0] == '\0')) 1536*ebfedea0SLionel Sambuc return(1); 1537*ebfedea0SLionel Sambuc BUF_strlcpy(buf,def,sizeof buf); 1538*ebfedea0SLionel Sambuc BUF_strlcat(buf,"\n",sizeof buf); 1539*ebfedea0SLionel Sambuc } 1540*ebfedea0SLionel Sambuc else if ((buf[0] == '.') && (buf[1] == '\n')) return(1); 1541*ebfedea0SLionel Sambuc 1542*ebfedea0SLionel Sambuc i=strlen(buf); 1543*ebfedea0SLionel Sambuc if (buf[i-1] != '\n') 1544*ebfedea0SLionel Sambuc { 1545*ebfedea0SLionel Sambuc BIO_printf(bio_err,"weird input :-(\n"); 1546*ebfedea0SLionel Sambuc return(0); 1547*ebfedea0SLionel Sambuc } 1548*ebfedea0SLionel Sambuc buf[--i]='\0'; 1549*ebfedea0SLionel Sambuc #ifdef CHARSET_EBCDIC 1550*ebfedea0SLionel Sambuc ebcdic2ascii(buf, buf, i); 1551*ebfedea0SLionel Sambuc #endif 1552*ebfedea0SLionel Sambuc if(!req_check_len(i, n_min, n_max)) goto start; 1553*ebfedea0SLionel Sambuc 1554*ebfedea0SLionel Sambuc if(!X509_REQ_add1_attr_by_NID(req, nid, chtype, 1555*ebfedea0SLionel Sambuc (unsigned char *)buf, -1)) { 1556*ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error adding attribute\n"); 1557*ebfedea0SLionel Sambuc ERR_print_errors(bio_err); 1558*ebfedea0SLionel Sambuc goto err; 1559*ebfedea0SLionel Sambuc } 1560*ebfedea0SLionel Sambuc 1561*ebfedea0SLionel Sambuc return(1); 1562*ebfedea0SLionel Sambuc err: 1563*ebfedea0SLionel Sambuc return(0); 1564*ebfedea0SLionel Sambuc } 1565*ebfedea0SLionel Sambuc 1566*ebfedea0SLionel Sambuc static int req_check_len(int len, int n_min, int n_max) 1567*ebfedea0SLionel Sambuc { 1568*ebfedea0SLionel Sambuc if ((n_min > 0) && (len < n_min)) 1569*ebfedea0SLionel Sambuc { 1570*ebfedea0SLionel Sambuc BIO_printf(bio_err,"string is too short, it needs to be at least %d bytes long\n",n_min); 1571*ebfedea0SLionel Sambuc return(0); 1572*ebfedea0SLionel Sambuc } 1573*ebfedea0SLionel Sambuc if ((n_max >= 0) && (len > n_max)) 1574*ebfedea0SLionel Sambuc { 1575*ebfedea0SLionel Sambuc BIO_printf(bio_err,"string is too long, it needs to be less than %d bytes long\n",n_max); 1576*ebfedea0SLionel Sambuc return(0); 1577*ebfedea0SLionel Sambuc } 1578*ebfedea0SLionel Sambuc return(1); 1579*ebfedea0SLionel Sambuc } 1580*ebfedea0SLionel Sambuc 1581*ebfedea0SLionel Sambuc /* Check if the end of a string matches 'end' */ 1582*ebfedea0SLionel Sambuc static int check_end(const char *str, const char *end) 1583*ebfedea0SLionel Sambuc { 1584*ebfedea0SLionel Sambuc int elen, slen; 1585*ebfedea0SLionel Sambuc const char *tmp; 1586*ebfedea0SLionel Sambuc elen = strlen(end); 1587*ebfedea0SLionel Sambuc slen = strlen(str); 1588*ebfedea0SLionel Sambuc if(elen > slen) return 1; 1589*ebfedea0SLionel Sambuc tmp = str + slen - elen; 1590*ebfedea0SLionel Sambuc return strcmp(tmp, end); 1591*ebfedea0SLionel Sambuc } 1592*ebfedea0SLionel Sambuc 1593*ebfedea0SLionel Sambuc static EVP_PKEY_CTX *set_keygen_ctx(BIO *err, const char *gstr, int *pkey_type, 1594*ebfedea0SLionel Sambuc long *pkeylen, char **palgnam, 1595*ebfedea0SLionel Sambuc ENGINE *keygen_engine) 1596*ebfedea0SLionel Sambuc { 1597*ebfedea0SLionel Sambuc EVP_PKEY_CTX *gctx = NULL; 1598*ebfedea0SLionel Sambuc EVP_PKEY *param = NULL; 1599*ebfedea0SLionel Sambuc long keylen = -1; 1600*ebfedea0SLionel Sambuc BIO *pbio = NULL; 1601*ebfedea0SLionel Sambuc const char *paramfile = NULL; 1602*ebfedea0SLionel Sambuc 1603*ebfedea0SLionel Sambuc if (gstr == NULL) 1604*ebfedea0SLionel Sambuc { 1605*ebfedea0SLionel Sambuc *pkey_type = EVP_PKEY_RSA; 1606*ebfedea0SLionel Sambuc keylen = *pkeylen; 1607*ebfedea0SLionel Sambuc } 1608*ebfedea0SLionel Sambuc else if (gstr[0] >= '0' && gstr[0] <= '9') 1609*ebfedea0SLionel Sambuc { 1610*ebfedea0SLionel Sambuc *pkey_type = EVP_PKEY_RSA; 1611*ebfedea0SLionel Sambuc keylen = atol(gstr); 1612*ebfedea0SLionel Sambuc *pkeylen = keylen; 1613*ebfedea0SLionel Sambuc } 1614*ebfedea0SLionel Sambuc else if (!strncmp(gstr, "param:", 6)) 1615*ebfedea0SLionel Sambuc paramfile = gstr + 6; 1616*ebfedea0SLionel Sambuc else 1617*ebfedea0SLionel Sambuc { 1618*ebfedea0SLionel Sambuc const char *p = strchr(gstr, ':'); 1619*ebfedea0SLionel Sambuc int len; 1620*ebfedea0SLionel Sambuc ENGINE *tmpeng; 1621*ebfedea0SLionel Sambuc const EVP_PKEY_ASN1_METHOD *ameth; 1622*ebfedea0SLionel Sambuc 1623*ebfedea0SLionel Sambuc if (p) 1624*ebfedea0SLionel Sambuc len = p - gstr; 1625*ebfedea0SLionel Sambuc else 1626*ebfedea0SLionel Sambuc len = strlen(gstr); 1627*ebfedea0SLionel Sambuc /* The lookup of a the string will cover all engines so 1628*ebfedea0SLionel Sambuc * keep a note of the implementation. 1629*ebfedea0SLionel Sambuc */ 1630*ebfedea0SLionel Sambuc 1631*ebfedea0SLionel Sambuc ameth = EVP_PKEY_asn1_find_str(&tmpeng, gstr, len); 1632*ebfedea0SLionel Sambuc 1633*ebfedea0SLionel Sambuc if (!ameth) 1634*ebfedea0SLionel Sambuc { 1635*ebfedea0SLionel Sambuc BIO_printf(err, "Unknown algorithm %.*s\n", len, gstr); 1636*ebfedea0SLionel Sambuc return NULL; 1637*ebfedea0SLionel Sambuc } 1638*ebfedea0SLionel Sambuc 1639*ebfedea0SLionel Sambuc EVP_PKEY_asn1_get0_info(NULL, pkey_type, NULL, NULL, NULL, 1640*ebfedea0SLionel Sambuc ameth); 1641*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE 1642*ebfedea0SLionel Sambuc if (tmpeng) 1643*ebfedea0SLionel Sambuc ENGINE_finish(tmpeng); 1644*ebfedea0SLionel Sambuc #endif 1645*ebfedea0SLionel Sambuc if (*pkey_type == EVP_PKEY_RSA) 1646*ebfedea0SLionel Sambuc { 1647*ebfedea0SLionel Sambuc if (p) 1648*ebfedea0SLionel Sambuc { 1649*ebfedea0SLionel Sambuc keylen = atol(p + 1); 1650*ebfedea0SLionel Sambuc *pkeylen = keylen; 1651*ebfedea0SLionel Sambuc } 1652*ebfedea0SLionel Sambuc } 1653*ebfedea0SLionel Sambuc else if (p) 1654*ebfedea0SLionel Sambuc paramfile = p + 1; 1655*ebfedea0SLionel Sambuc } 1656*ebfedea0SLionel Sambuc 1657*ebfedea0SLionel Sambuc if (paramfile) 1658*ebfedea0SLionel Sambuc { 1659*ebfedea0SLionel Sambuc pbio = BIO_new_file(paramfile, "r"); 1660*ebfedea0SLionel Sambuc if (!pbio) 1661*ebfedea0SLionel Sambuc { 1662*ebfedea0SLionel Sambuc BIO_printf(err, "Can't open parameter file %s\n", 1663*ebfedea0SLionel Sambuc paramfile); 1664*ebfedea0SLionel Sambuc return NULL; 1665*ebfedea0SLionel Sambuc } 1666*ebfedea0SLionel Sambuc param = PEM_read_bio_Parameters(pbio, NULL); 1667*ebfedea0SLionel Sambuc 1668*ebfedea0SLionel Sambuc if (!param) 1669*ebfedea0SLionel Sambuc { 1670*ebfedea0SLionel Sambuc X509 *x; 1671*ebfedea0SLionel Sambuc (void)BIO_reset(pbio); 1672*ebfedea0SLionel Sambuc x = PEM_read_bio_X509(pbio, NULL, NULL, NULL); 1673*ebfedea0SLionel Sambuc if (x) 1674*ebfedea0SLionel Sambuc { 1675*ebfedea0SLionel Sambuc param = X509_get_pubkey(x); 1676*ebfedea0SLionel Sambuc X509_free(x); 1677*ebfedea0SLionel Sambuc } 1678*ebfedea0SLionel Sambuc } 1679*ebfedea0SLionel Sambuc 1680*ebfedea0SLionel Sambuc BIO_free(pbio); 1681*ebfedea0SLionel Sambuc 1682*ebfedea0SLionel Sambuc if (!param) 1683*ebfedea0SLionel Sambuc { 1684*ebfedea0SLionel Sambuc BIO_printf(err, "Error reading parameter file %s\n", 1685*ebfedea0SLionel Sambuc paramfile); 1686*ebfedea0SLionel Sambuc return NULL; 1687*ebfedea0SLionel Sambuc } 1688*ebfedea0SLionel Sambuc if (*pkey_type == -1) 1689*ebfedea0SLionel Sambuc *pkey_type = EVP_PKEY_id(param); 1690*ebfedea0SLionel Sambuc else if (*pkey_type != EVP_PKEY_base_id(param)) 1691*ebfedea0SLionel Sambuc { 1692*ebfedea0SLionel Sambuc BIO_printf(err, "Key Type does not match parameters\n"); 1693*ebfedea0SLionel Sambuc EVP_PKEY_free(param); 1694*ebfedea0SLionel Sambuc return NULL; 1695*ebfedea0SLionel Sambuc } 1696*ebfedea0SLionel Sambuc } 1697*ebfedea0SLionel Sambuc 1698*ebfedea0SLionel Sambuc if (palgnam) 1699*ebfedea0SLionel Sambuc { 1700*ebfedea0SLionel Sambuc const EVP_PKEY_ASN1_METHOD *ameth; 1701*ebfedea0SLionel Sambuc ENGINE *tmpeng; 1702*ebfedea0SLionel Sambuc const char *anam; 1703*ebfedea0SLionel Sambuc ameth = EVP_PKEY_asn1_find(&tmpeng, *pkey_type); 1704*ebfedea0SLionel Sambuc if (!ameth) 1705*ebfedea0SLionel Sambuc { 1706*ebfedea0SLionel Sambuc BIO_puts(err, "Internal error: can't find key algorithm\n"); 1707*ebfedea0SLionel Sambuc return NULL; 1708*ebfedea0SLionel Sambuc } 1709*ebfedea0SLionel Sambuc EVP_PKEY_asn1_get0_info(NULL, NULL, NULL, NULL, &anam, ameth); 1710*ebfedea0SLionel Sambuc *palgnam = BUF_strdup(anam); 1711*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE 1712*ebfedea0SLionel Sambuc if (tmpeng) 1713*ebfedea0SLionel Sambuc ENGINE_finish(tmpeng); 1714*ebfedea0SLionel Sambuc #endif 1715*ebfedea0SLionel Sambuc } 1716*ebfedea0SLionel Sambuc 1717*ebfedea0SLionel Sambuc if (param) 1718*ebfedea0SLionel Sambuc { 1719*ebfedea0SLionel Sambuc gctx = EVP_PKEY_CTX_new(param, keygen_engine); 1720*ebfedea0SLionel Sambuc *pkeylen = EVP_PKEY_bits(param); 1721*ebfedea0SLionel Sambuc EVP_PKEY_free(param); 1722*ebfedea0SLionel Sambuc } 1723*ebfedea0SLionel Sambuc else 1724*ebfedea0SLionel Sambuc gctx = EVP_PKEY_CTX_new_id(*pkey_type, keygen_engine); 1725*ebfedea0SLionel Sambuc 1726*ebfedea0SLionel Sambuc if (!gctx) 1727*ebfedea0SLionel Sambuc { 1728*ebfedea0SLionel Sambuc BIO_puts(err, "Error allocating keygen context\n"); 1729*ebfedea0SLionel Sambuc ERR_print_errors(err); 1730*ebfedea0SLionel Sambuc return NULL; 1731*ebfedea0SLionel Sambuc } 1732*ebfedea0SLionel Sambuc 1733*ebfedea0SLionel Sambuc if (EVP_PKEY_keygen_init(gctx) <= 0) 1734*ebfedea0SLionel Sambuc { 1735*ebfedea0SLionel Sambuc BIO_puts(err, "Error initializing keygen context\n"); 1736*ebfedea0SLionel Sambuc ERR_print_errors(err); 1737*ebfedea0SLionel Sambuc return NULL; 1738*ebfedea0SLionel Sambuc } 1739*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA 1740*ebfedea0SLionel Sambuc if ((*pkey_type == EVP_PKEY_RSA) && (keylen != -1)) 1741*ebfedea0SLionel Sambuc { 1742*ebfedea0SLionel Sambuc if (EVP_PKEY_CTX_set_rsa_keygen_bits(gctx, keylen) <= 0) 1743*ebfedea0SLionel Sambuc { 1744*ebfedea0SLionel Sambuc BIO_puts(err, "Error setting RSA keysize\n"); 1745*ebfedea0SLionel Sambuc ERR_print_errors(err); 1746*ebfedea0SLionel Sambuc EVP_PKEY_CTX_free(gctx); 1747*ebfedea0SLionel Sambuc return NULL; 1748*ebfedea0SLionel Sambuc } 1749*ebfedea0SLionel Sambuc } 1750*ebfedea0SLionel Sambuc #endif 1751*ebfedea0SLionel Sambuc 1752*ebfedea0SLionel Sambuc return gctx; 1753*ebfedea0SLionel Sambuc } 1754*ebfedea0SLionel Sambuc 1755*ebfedea0SLionel Sambuc static int genpkey_cb(EVP_PKEY_CTX *ctx) 1756*ebfedea0SLionel Sambuc { 1757*ebfedea0SLionel Sambuc char c='*'; 1758*ebfedea0SLionel Sambuc BIO *b = EVP_PKEY_CTX_get_app_data(ctx); 1759*ebfedea0SLionel Sambuc int p; 1760*ebfedea0SLionel Sambuc p = EVP_PKEY_CTX_get_keygen_info(ctx, 0); 1761*ebfedea0SLionel Sambuc if (p == 0) c='.'; 1762*ebfedea0SLionel Sambuc if (p == 1) c='+'; 1763*ebfedea0SLionel Sambuc if (p == 2) c='*'; 1764*ebfedea0SLionel Sambuc if (p == 3) c='\n'; 1765*ebfedea0SLionel Sambuc BIO_write(b,&c,1); 1766*ebfedea0SLionel Sambuc (void)BIO_flush(b); 1767*ebfedea0SLionel Sambuc #ifdef LINT 1768*ebfedea0SLionel Sambuc p=n; 1769*ebfedea0SLionel Sambuc #endif 1770*ebfedea0SLionel Sambuc return 1; 1771*ebfedea0SLionel Sambuc } 1772*ebfedea0SLionel Sambuc 1773*ebfedea0SLionel Sambuc static int do_sign_init(BIO *err, EVP_MD_CTX *ctx, EVP_PKEY *pkey, 1774*ebfedea0SLionel Sambuc const EVP_MD *md, STACK_OF(OPENSSL_STRING) *sigopts) 1775*ebfedea0SLionel Sambuc { 1776*ebfedea0SLionel Sambuc EVP_PKEY_CTX *pkctx = NULL; 1777*ebfedea0SLionel Sambuc int i; 1778*ebfedea0SLionel Sambuc EVP_MD_CTX_init(ctx); 1779*ebfedea0SLionel Sambuc if (!EVP_DigestSignInit(ctx, &pkctx, md, NULL, pkey)) 1780*ebfedea0SLionel Sambuc return 0; 1781*ebfedea0SLionel Sambuc for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) 1782*ebfedea0SLionel Sambuc { 1783*ebfedea0SLionel Sambuc char *sigopt = sk_OPENSSL_STRING_value(sigopts, i); 1784*ebfedea0SLionel Sambuc if (pkey_ctrl_string(pkctx, sigopt) <= 0) 1785*ebfedea0SLionel Sambuc { 1786*ebfedea0SLionel Sambuc BIO_printf(err, "parameter error \"%s\"\n", sigopt); 1787*ebfedea0SLionel Sambuc ERR_print_errors(bio_err); 1788*ebfedea0SLionel Sambuc return 0; 1789*ebfedea0SLionel Sambuc } 1790*ebfedea0SLionel Sambuc } 1791*ebfedea0SLionel Sambuc return 1; 1792*ebfedea0SLionel Sambuc } 1793*ebfedea0SLionel Sambuc 1794*ebfedea0SLionel Sambuc int do_X509_sign(BIO *err, X509 *x, EVP_PKEY *pkey, const EVP_MD *md, 1795*ebfedea0SLionel Sambuc STACK_OF(OPENSSL_STRING) *sigopts) 1796*ebfedea0SLionel Sambuc { 1797*ebfedea0SLionel Sambuc int rv; 1798*ebfedea0SLionel Sambuc EVP_MD_CTX mctx; 1799*ebfedea0SLionel Sambuc EVP_MD_CTX_init(&mctx); 1800*ebfedea0SLionel Sambuc rv = do_sign_init(err, &mctx, pkey, md, sigopts); 1801*ebfedea0SLionel Sambuc if (rv > 0) 1802*ebfedea0SLionel Sambuc rv = X509_sign_ctx(x, &mctx); 1803*ebfedea0SLionel Sambuc EVP_MD_CTX_cleanup(&mctx); 1804*ebfedea0SLionel Sambuc return rv > 0 ? 1 : 0; 1805*ebfedea0SLionel Sambuc } 1806*ebfedea0SLionel Sambuc 1807*ebfedea0SLionel Sambuc 1808*ebfedea0SLionel Sambuc int do_X509_REQ_sign(BIO *err, X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md, 1809*ebfedea0SLionel Sambuc STACK_OF(OPENSSL_STRING) *sigopts) 1810*ebfedea0SLionel Sambuc { 1811*ebfedea0SLionel Sambuc int rv; 1812*ebfedea0SLionel Sambuc EVP_MD_CTX mctx; 1813*ebfedea0SLionel Sambuc EVP_MD_CTX_init(&mctx); 1814*ebfedea0SLionel Sambuc rv = do_sign_init(err, &mctx, pkey, md, sigopts); 1815*ebfedea0SLionel Sambuc if (rv > 0) 1816*ebfedea0SLionel Sambuc rv = X509_REQ_sign_ctx(x, &mctx); 1817*ebfedea0SLionel Sambuc EVP_MD_CTX_cleanup(&mctx); 1818*ebfedea0SLionel Sambuc return rv > 0 ? 1 : 0; 1819*ebfedea0SLionel Sambuc } 1820*ebfedea0SLionel Sambuc 1821*ebfedea0SLionel Sambuc 1822*ebfedea0SLionel Sambuc 1823*ebfedea0SLionel Sambuc int do_X509_CRL_sign(BIO *err, X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md, 1824*ebfedea0SLionel Sambuc STACK_OF(OPENSSL_STRING) *sigopts) 1825*ebfedea0SLionel Sambuc { 1826*ebfedea0SLionel Sambuc int rv; 1827*ebfedea0SLionel Sambuc EVP_MD_CTX mctx; 1828*ebfedea0SLionel Sambuc EVP_MD_CTX_init(&mctx); 1829*ebfedea0SLionel Sambuc rv = do_sign_init(err, &mctx, pkey, md, sigopts); 1830*ebfedea0SLionel Sambuc if (rv > 0) 1831*ebfedea0SLionel Sambuc rv = X509_CRL_sign_ctx(x, &mctx); 1832*ebfedea0SLionel Sambuc EVP_MD_CTX_cleanup(&mctx); 1833*ebfedea0SLionel Sambuc return rv > 0 ? 1 : 0; 1834*ebfedea0SLionel Sambuc } 1835*ebfedea0SLionel Sambuc 1836*ebfedea0SLionel Sambuc 1837