10Sstevel@tonic-gate /* apps/req.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This package is an SSL implementation written
60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate * the code are not to be removed.
180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate * as the author of the parts of the library used.
200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate * are met:
260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate * must display the following acknowledgement:
330Sstevel@tonic-gate * "This product includes cryptographic software written by
340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate * being used are not cryptographic related :-).
370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate * SUCH DAMAGE.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
550Sstevel@tonic-gate * copied and put under another distribution licence
560Sstevel@tonic-gate * [including the GNU Public Licence.]
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
59*2139Sjp161948 /* Until the key-gen callbacks are modified to use newer prototypes, we allow
60*2139Sjp161948 * deprecated functions for openssl-internal code */
61*2139Sjp161948 #ifdef OPENSSL_NO_DEPRECATED
62*2139Sjp161948 #undef OPENSSL_NO_DEPRECATED
63*2139Sjp161948 #endif
64*2139Sjp161948
650Sstevel@tonic-gate #include <stdio.h>
660Sstevel@tonic-gate #include <stdlib.h>
670Sstevel@tonic-gate #include <time.h>
680Sstevel@tonic-gate #include <string.h>
690Sstevel@tonic-gate #ifdef OPENSSL_NO_STDIO
700Sstevel@tonic-gate #define APPS_WIN16
710Sstevel@tonic-gate #endif
720Sstevel@tonic-gate #include "apps.h"
730Sstevel@tonic-gate #include <openssl/bio.h>
740Sstevel@tonic-gate #include <openssl/evp.h>
750Sstevel@tonic-gate #include <openssl/conf.h>
760Sstevel@tonic-gate #include <openssl/err.h>
770Sstevel@tonic-gate #include <openssl/asn1.h>
780Sstevel@tonic-gate #include <openssl/x509.h>
790Sstevel@tonic-gate #include <openssl/x509v3.h>
800Sstevel@tonic-gate #include <openssl/objects.h>
810Sstevel@tonic-gate #include <openssl/pem.h>
82*2139Sjp161948 #include <openssl/bn.h>
83*2139Sjp161948 #ifndef OPENSSL_NO_RSA
84*2139Sjp161948 #include <openssl/rsa.h>
85*2139Sjp161948 #endif
86*2139Sjp161948 #ifndef OPENSSL_NO_DSA
87*2139Sjp161948 #include <openssl/dsa.h>
88*2139Sjp161948 #endif
890Sstevel@tonic-gate
900Sstevel@tonic-gate #define SECTION "req"
910Sstevel@tonic-gate
920Sstevel@tonic-gate #define BITS "default_bits"
930Sstevel@tonic-gate #define KEYFILE "default_keyfile"
940Sstevel@tonic-gate #define PROMPT "prompt"
950Sstevel@tonic-gate #define DISTINGUISHED_NAME "distinguished_name"
960Sstevel@tonic-gate #define ATTRIBUTES "attributes"
970Sstevel@tonic-gate #define V3_EXTENSIONS "x509_extensions"
980Sstevel@tonic-gate #define REQ_EXTENSIONS "req_extensions"
990Sstevel@tonic-gate #define STRING_MASK "string_mask"
1000Sstevel@tonic-gate #define UTF8_IN "utf8"
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate #define DEFAULT_KEY_LENGTH 512
1030Sstevel@tonic-gate #define MIN_KEY_LENGTH 384
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate #undef PROG
1060Sstevel@tonic-gate #define PROG req_main
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate /* -inform arg - input format - default PEM (DER or PEM)
1090Sstevel@tonic-gate * -outform arg - output format - default PEM
1100Sstevel@tonic-gate * -in arg - input file - default stdin
1110Sstevel@tonic-gate * -out arg - output file - default stdout
1120Sstevel@tonic-gate * -verify - check request signature
1130Sstevel@tonic-gate * -noout - don't print stuff out.
1140Sstevel@tonic-gate * -text - print out human readable text.
1150Sstevel@tonic-gate * -nodes - no des encryption
1160Sstevel@tonic-gate * -config file - Load configuration file.
1170Sstevel@tonic-gate * -key file - make a request using key in file (or use it for verification).
1180Sstevel@tonic-gate * -keyform arg - key file format.
1190Sstevel@tonic-gate * -rand file(s) - load the file(s) into the PRNG.
1200Sstevel@tonic-gate * -newkey - make a key and a request.
1210Sstevel@tonic-gate * -modulus - print RSA modulus.
1220Sstevel@tonic-gate * -pubkey - output Public Key.
1230Sstevel@tonic-gate * -x509 - output a self signed X509 structure instead.
1240Sstevel@tonic-gate * -asn1-kludge - output new certificate request in a format that some CA's
1250Sstevel@tonic-gate * require. This format is wrong
1260Sstevel@tonic-gate */
1270Sstevel@tonic-gate
128*2139Sjp161948 static int make_REQ(X509_REQ *req,EVP_PKEY *pkey,char *dn,int mutlirdn,
129*2139Sjp161948 int attribs,unsigned long chtype);
130*2139Sjp161948 static int build_subject(X509_REQ *req, char *subj, unsigned long chtype,
131*2139Sjp161948 int multirdn);
1320Sstevel@tonic-gate static int prompt_info(X509_REQ *req,
1330Sstevel@tonic-gate STACK_OF(CONF_VALUE) *dn_sk, char *dn_sect,
1340Sstevel@tonic-gate STACK_OF(CONF_VALUE) *attr_sk, char *attr_sect, int attribs,
1350Sstevel@tonic-gate unsigned long chtype);
1360Sstevel@tonic-gate static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *sk,
1370Sstevel@tonic-gate STACK_OF(CONF_VALUE) *attr, int attribs,
1380Sstevel@tonic-gate unsigned long chtype);
139*2139Sjp161948 static int add_attribute_object(X509_REQ *req, char *text, const char *def,
140*2139Sjp161948 char *value, int nid, int n_min,
1410Sstevel@tonic-gate int n_max, unsigned long chtype);
142*2139Sjp161948 static int add_DN_object(X509_NAME *n, char *text, const char *def, char *value,
143*2139Sjp161948 int nid,int n_min,int n_max, unsigned long chtype, int mval);
1440Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
145*2139Sjp161948 static int MS_CALLBACK req_cb(int p, int n, BN_GENCB *cb);
1460Sstevel@tonic-gate #endif
1470Sstevel@tonic-gate static int req_check_len(int len,int n_min,int n_max);
148*2139Sjp161948 static int check_end(const char *str, const char *end);
1490Sstevel@tonic-gate #ifndef MONOLITH
1500Sstevel@tonic-gate static char *default_config_file=NULL;
1510Sstevel@tonic-gate #endif
1520Sstevel@tonic-gate static CONF *req_conf=NULL;
1530Sstevel@tonic-gate static int batch=0;
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate #define TYPE_RSA 1
1560Sstevel@tonic-gate #define TYPE_DSA 2
1570Sstevel@tonic-gate #define TYPE_DH 3
158*2139Sjp161948 #define TYPE_EC 4
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate int MAIN(int, char **);
1610Sstevel@tonic-gate
MAIN(int argc,char ** argv)1620Sstevel@tonic-gate int MAIN(int argc, char **argv)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate ENGINE *e = NULL;
1650Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
1660Sstevel@tonic-gate DSA *dsa_params=NULL;
1670Sstevel@tonic-gate #endif
168*2139Sjp161948 #ifndef OPENSSL_NO_ECDSA
169*2139Sjp161948 EC_KEY *ec_params = NULL;
170*2139Sjp161948 #endif
1710Sstevel@tonic-gate unsigned long nmflag = 0, reqflag = 0;
1720Sstevel@tonic-gate int ex=1,x509=0,days=30;
1730Sstevel@tonic-gate X509 *x509ss=NULL;
1740Sstevel@tonic-gate X509_REQ *req=NULL;
1750Sstevel@tonic-gate EVP_PKEY *pkey=NULL;
1760Sstevel@tonic-gate int i=0,badops=0,newreq=0,verbose=0,pkey_type=TYPE_RSA;
1770Sstevel@tonic-gate long newkey = -1;
1780Sstevel@tonic-gate BIO *in=NULL,*out=NULL;
1790Sstevel@tonic-gate int informat,outformat,verify=0,noout=0,text=0,keyform=FORMAT_PEM;
1800Sstevel@tonic-gate int nodes=0,kludge=0,newhdr=0,subject=0,pubkey=0;
1810Sstevel@tonic-gate char *infile,*outfile,*prog,*keyfile=NULL,*template=NULL,*keyout=NULL;
1820Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1830Sstevel@tonic-gate char *engine=NULL;
1840Sstevel@tonic-gate #endif
1850Sstevel@tonic-gate char *extensions = NULL;
1860Sstevel@tonic-gate char *req_exts = NULL;
1870Sstevel@tonic-gate const EVP_CIPHER *cipher=NULL;
1880Sstevel@tonic-gate ASN1_INTEGER *serial = NULL;
1890Sstevel@tonic-gate int modulus=0;
1900Sstevel@tonic-gate char *inrand=NULL;
1910Sstevel@tonic-gate char *passargin = NULL, *passargout = NULL;
1920Sstevel@tonic-gate char *passin = NULL, *passout = NULL;
1930Sstevel@tonic-gate char *p;
1940Sstevel@tonic-gate char *subj = NULL;
195*2139Sjp161948 int multirdn = 0;
196*2139Sjp161948 const EVP_MD *md_alg=NULL,*digest=EVP_sha1();
1970Sstevel@tonic-gate unsigned long chtype = MBSTRING_ASC;
1980Sstevel@tonic-gate #ifndef MONOLITH
1990Sstevel@tonic-gate char *to_free;
2000Sstevel@tonic-gate long errline;
2010Sstevel@tonic-gate #endif
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate req_conf = NULL;
2040Sstevel@tonic-gate #ifndef OPENSSL_NO_DES
2050Sstevel@tonic-gate cipher=EVP_des_ede3_cbc();
2060Sstevel@tonic-gate #endif
2070Sstevel@tonic-gate apps_startup();
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate if (bio_err == NULL)
2100Sstevel@tonic-gate if ((bio_err=BIO_new(BIO_s_file())) != NULL)
2110Sstevel@tonic-gate BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate infile=NULL;
2140Sstevel@tonic-gate outfile=NULL;
2150Sstevel@tonic-gate informat=FORMAT_PEM;
2160Sstevel@tonic-gate outformat=FORMAT_PEM;
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate prog=argv[0];
2190Sstevel@tonic-gate argc--;
2200Sstevel@tonic-gate argv++;
2210Sstevel@tonic-gate while (argc >= 1)
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate if (strcmp(*argv,"-inform") == 0)
2240Sstevel@tonic-gate {
2250Sstevel@tonic-gate if (--argc < 1) goto bad;
2260Sstevel@tonic-gate informat=str2fmt(*(++argv));
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate else if (strcmp(*argv,"-outform") == 0)
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate if (--argc < 1) goto bad;
2310Sstevel@tonic-gate outformat=str2fmt(*(++argv));
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
2340Sstevel@tonic-gate else if (strcmp(*argv,"-engine") == 0)
2350Sstevel@tonic-gate {
2360Sstevel@tonic-gate if (--argc < 1) goto bad;
2370Sstevel@tonic-gate engine= *(++argv);
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate #endif
2400Sstevel@tonic-gate else if (strcmp(*argv,"-key") == 0)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate if (--argc < 1) goto bad;
2430Sstevel@tonic-gate keyfile= *(++argv);
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate else if (strcmp(*argv,"-pubkey") == 0)
2460Sstevel@tonic-gate {
2470Sstevel@tonic-gate pubkey=1;
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate else if (strcmp(*argv,"-new") == 0)
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate newreq=1;
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate else if (strcmp(*argv,"-config") == 0)
2540Sstevel@tonic-gate {
2550Sstevel@tonic-gate if (--argc < 1) goto bad;
2560Sstevel@tonic-gate template= *(++argv);
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate else if (strcmp(*argv,"-keyform") == 0)
2590Sstevel@tonic-gate {
2600Sstevel@tonic-gate if (--argc < 1) goto bad;
2610Sstevel@tonic-gate keyform=str2fmt(*(++argv));
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate else if (strcmp(*argv,"-in") == 0)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate if (--argc < 1) goto bad;
2660Sstevel@tonic-gate infile= *(++argv);
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate else if (strcmp(*argv,"-out") == 0)
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate if (--argc < 1) goto bad;
2710Sstevel@tonic-gate outfile= *(++argv);
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate else if (strcmp(*argv,"-keyout") == 0)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate if (--argc < 1) goto bad;
2760Sstevel@tonic-gate keyout= *(++argv);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate else if (strcmp(*argv,"-passin") == 0)
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate if (--argc < 1) goto bad;
2810Sstevel@tonic-gate passargin= *(++argv);
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate else if (strcmp(*argv,"-passout") == 0)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate if (--argc < 1) goto bad;
2860Sstevel@tonic-gate passargout= *(++argv);
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate else if (strcmp(*argv,"-rand") == 0)
2890Sstevel@tonic-gate {
2900Sstevel@tonic-gate if (--argc < 1) goto bad;
2910Sstevel@tonic-gate inrand= *(++argv);
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate else if (strcmp(*argv,"-newkey") == 0)
2940Sstevel@tonic-gate {
2950Sstevel@tonic-gate int is_numeric;
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate if (--argc < 1) goto bad;
2980Sstevel@tonic-gate p= *(++argv);
2990Sstevel@tonic-gate is_numeric = p[0] >= '0' && p[0] <= '9';
3000Sstevel@tonic-gate if (strncmp("rsa:",p,4) == 0 || is_numeric)
3010Sstevel@tonic-gate {
3020Sstevel@tonic-gate pkey_type=TYPE_RSA;
3030Sstevel@tonic-gate if(!is_numeric)
3040Sstevel@tonic-gate p+=4;
3050Sstevel@tonic-gate newkey= atoi(p);
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate else
3080Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
3090Sstevel@tonic-gate if (strncmp("dsa:",p,4) == 0)
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate X509 *xtmp=NULL;
3120Sstevel@tonic-gate EVP_PKEY *dtmp;
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate pkey_type=TYPE_DSA;
3150Sstevel@tonic-gate p+=4;
3160Sstevel@tonic-gate if ((in=BIO_new_file(p,"r")) == NULL)
3170Sstevel@tonic-gate {
3180Sstevel@tonic-gate perror(p);
3190Sstevel@tonic-gate goto end;
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate if ((dsa_params=PEM_read_bio_DSAparams(in,NULL,NULL,NULL)) == NULL)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate ERR_clear_error();
3240Sstevel@tonic-gate (void)BIO_reset(in);
3250Sstevel@tonic-gate if ((xtmp=PEM_read_bio_X509(in,NULL,NULL,NULL)) == NULL)
3260Sstevel@tonic-gate {
3270Sstevel@tonic-gate BIO_printf(bio_err,"unable to load DSA parameters from file\n");
3280Sstevel@tonic-gate goto end;
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate if ((dtmp=X509_get_pubkey(xtmp)) == NULL) goto end;
3320Sstevel@tonic-gate if (dtmp->type == EVP_PKEY_DSA)
3330Sstevel@tonic-gate dsa_params=DSAparams_dup(dtmp->pkey.dsa);
3340Sstevel@tonic-gate EVP_PKEY_free(dtmp);
3350Sstevel@tonic-gate X509_free(xtmp);
3360Sstevel@tonic-gate if (dsa_params == NULL)
3370Sstevel@tonic-gate {
3380Sstevel@tonic-gate BIO_printf(bio_err,"Certificate does not contain DSA parameters\n");
3390Sstevel@tonic-gate goto end;
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate }
3420Sstevel@tonic-gate BIO_free(in);
343*2139Sjp161948 in=NULL;
3440Sstevel@tonic-gate newkey=BN_num_bits(dsa_params->p);
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate else
3470Sstevel@tonic-gate #endif
348*2139Sjp161948 #ifndef OPENSSL_NO_ECDSA
349*2139Sjp161948 if (strncmp("ec:",p,3) == 0)
350*2139Sjp161948 {
351*2139Sjp161948 X509 *xtmp=NULL;
352*2139Sjp161948 EVP_PKEY *dtmp;
353*2139Sjp161948 EC_GROUP *group;
354*2139Sjp161948
355*2139Sjp161948 pkey_type=TYPE_EC;
356*2139Sjp161948 p+=3;
357*2139Sjp161948 if ((in=BIO_new_file(p,"r")) == NULL)
358*2139Sjp161948 {
359*2139Sjp161948 perror(p);
360*2139Sjp161948 goto end;
361*2139Sjp161948 }
362*2139Sjp161948 if ((ec_params = EC_KEY_new()) == NULL)
363*2139Sjp161948 goto end;
364*2139Sjp161948 group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL);
365*2139Sjp161948 if (group == NULL)
366*2139Sjp161948 {
367*2139Sjp161948 EC_KEY_free(ec_params);
368*2139Sjp161948 ERR_clear_error();
369*2139Sjp161948 (void)BIO_reset(in);
370*2139Sjp161948 if ((xtmp=PEM_read_bio_X509(in,NULL,NULL,NULL)) == NULL)
371*2139Sjp161948 {
372*2139Sjp161948 BIO_printf(bio_err,"unable to load EC parameters from file\n");
373*2139Sjp161948 goto end;
374*2139Sjp161948 }
375*2139Sjp161948
376*2139Sjp161948 if ((dtmp=X509_get_pubkey(xtmp))==NULL)
377*2139Sjp161948 goto end;
378*2139Sjp161948 if (dtmp->type == EVP_PKEY_EC)
379*2139Sjp161948 ec_params = EC_KEY_dup(dtmp->pkey.ec);
380*2139Sjp161948 EVP_PKEY_free(dtmp);
381*2139Sjp161948 X509_free(xtmp);
382*2139Sjp161948 if (ec_params == NULL)
383*2139Sjp161948 {
384*2139Sjp161948 BIO_printf(bio_err,"Certificate does not contain EC parameters\n");
385*2139Sjp161948 goto end;
386*2139Sjp161948 }
387*2139Sjp161948 }
388*2139Sjp161948 else
389*2139Sjp161948 {
390*2139Sjp161948 if (EC_KEY_set_group(ec_params, group) == 0)
391*2139Sjp161948 goto end;
392*2139Sjp161948 EC_GROUP_free(group);
393*2139Sjp161948 }
394*2139Sjp161948
395*2139Sjp161948 BIO_free(in);
396*2139Sjp161948 in=NULL;
397*2139Sjp161948 newkey = EC_GROUP_get_degree(EC_KEY_get0_group(ec_params));
398*2139Sjp161948 }
399*2139Sjp161948 else
400*2139Sjp161948 #endif
4010Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
4020Sstevel@tonic-gate if (strncmp("dh:",p,4) == 0)
4030Sstevel@tonic-gate {
4040Sstevel@tonic-gate pkey_type=TYPE_DH;
4050Sstevel@tonic-gate p+=3;
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate else
4080Sstevel@tonic-gate #endif
409*2139Sjp161948 {
410*2139Sjp161948 goto bad;
411*2139Sjp161948 }
4120Sstevel@tonic-gate
4130Sstevel@tonic-gate newreq=1;
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate else if (strcmp(*argv,"-batch") == 0)
4160Sstevel@tonic-gate batch=1;
4170Sstevel@tonic-gate else if (strcmp(*argv,"-newhdr") == 0)
4180Sstevel@tonic-gate newhdr=1;
4190Sstevel@tonic-gate else if (strcmp(*argv,"-modulus") == 0)
4200Sstevel@tonic-gate modulus=1;
4210Sstevel@tonic-gate else if (strcmp(*argv,"-verify") == 0)
4220Sstevel@tonic-gate verify=1;
4230Sstevel@tonic-gate else if (strcmp(*argv,"-nodes") == 0)
4240Sstevel@tonic-gate nodes=1;
4250Sstevel@tonic-gate else if (strcmp(*argv,"-noout") == 0)
4260Sstevel@tonic-gate noout=1;
4270Sstevel@tonic-gate else if (strcmp(*argv,"-verbose") == 0)
4280Sstevel@tonic-gate verbose=1;
4290Sstevel@tonic-gate else if (strcmp(*argv,"-utf8") == 0)
4300Sstevel@tonic-gate chtype = MBSTRING_UTF8;
4310Sstevel@tonic-gate else if (strcmp(*argv,"-nameopt") == 0)
4320Sstevel@tonic-gate {
4330Sstevel@tonic-gate if (--argc < 1) goto bad;
4340Sstevel@tonic-gate if (!set_name_ex(&nmflag, *(++argv))) goto bad;
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate else if (strcmp(*argv,"-reqopt") == 0)
4370Sstevel@tonic-gate {
4380Sstevel@tonic-gate if (--argc < 1) goto bad;
4390Sstevel@tonic-gate if (!set_cert_ex(&reqflag, *(++argv))) goto bad;
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate else if (strcmp(*argv,"-subject") == 0)
4420Sstevel@tonic-gate subject=1;
4430Sstevel@tonic-gate else if (strcmp(*argv,"-text") == 0)
4440Sstevel@tonic-gate text=1;
4450Sstevel@tonic-gate else if (strcmp(*argv,"-x509") == 0)
4460Sstevel@tonic-gate x509=1;
4470Sstevel@tonic-gate else if (strcmp(*argv,"-asn1-kludge") == 0)
4480Sstevel@tonic-gate kludge=1;
4490Sstevel@tonic-gate else if (strcmp(*argv,"-no-asn1-kludge") == 0)
4500Sstevel@tonic-gate kludge=0;
4510Sstevel@tonic-gate else if (strcmp(*argv,"-subj") == 0)
4520Sstevel@tonic-gate {
4530Sstevel@tonic-gate if (--argc < 1) goto bad;
4540Sstevel@tonic-gate subj= *(++argv);
4550Sstevel@tonic-gate }
456*2139Sjp161948 else if (strcmp(*argv,"-multivalue-rdn") == 0)
457*2139Sjp161948 multirdn=1;
4580Sstevel@tonic-gate else if (strcmp(*argv,"-days") == 0)
4590Sstevel@tonic-gate {
4600Sstevel@tonic-gate if (--argc < 1) goto bad;
4610Sstevel@tonic-gate days= atoi(*(++argv));
4620Sstevel@tonic-gate if (days == 0) days=30;
4630Sstevel@tonic-gate }
4640Sstevel@tonic-gate else if (strcmp(*argv,"-set_serial") == 0)
4650Sstevel@tonic-gate {
4660Sstevel@tonic-gate if (--argc < 1) goto bad;
4670Sstevel@tonic-gate serial = s2i_ASN1_INTEGER(NULL, *(++argv));
4680Sstevel@tonic-gate if (!serial) goto bad;
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate else if ((md_alg=EVP_get_digestbyname(&((*argv)[1]))) != NULL)
4710Sstevel@tonic-gate {
4720Sstevel@tonic-gate /* ok */
4730Sstevel@tonic-gate digest=md_alg;
4740Sstevel@tonic-gate }
4750Sstevel@tonic-gate else if (strcmp(*argv,"-extensions") == 0)
4760Sstevel@tonic-gate {
4770Sstevel@tonic-gate if (--argc < 1) goto bad;
4780Sstevel@tonic-gate extensions = *(++argv);
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate else if (strcmp(*argv,"-reqexts") == 0)
4810Sstevel@tonic-gate {
4820Sstevel@tonic-gate if (--argc < 1) goto bad;
4830Sstevel@tonic-gate req_exts = *(++argv);
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate else
4860Sstevel@tonic-gate {
4870Sstevel@tonic-gate BIO_printf(bio_err,"unknown option %s\n",*argv);
4880Sstevel@tonic-gate badops=1;
4890Sstevel@tonic-gate break;
4900Sstevel@tonic-gate }
4910Sstevel@tonic-gate argc--;
4920Sstevel@tonic-gate argv++;
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate
4950Sstevel@tonic-gate if (badops)
4960Sstevel@tonic-gate {
4970Sstevel@tonic-gate bad:
4980Sstevel@tonic-gate BIO_printf(bio_err,"%s [options] <infile >outfile\n",prog);
4990Sstevel@tonic-gate BIO_printf(bio_err,"where options are\n");
5000Sstevel@tonic-gate BIO_printf(bio_err," -inform arg input format - DER or PEM\n");
5010Sstevel@tonic-gate BIO_printf(bio_err," -outform arg output format - DER or PEM\n");
5020Sstevel@tonic-gate BIO_printf(bio_err," -in arg input file\n");
5030Sstevel@tonic-gate BIO_printf(bio_err," -out arg output file\n");
5040Sstevel@tonic-gate BIO_printf(bio_err," -text text form of request\n");
5050Sstevel@tonic-gate BIO_printf(bio_err," -pubkey output public key\n");
5060Sstevel@tonic-gate BIO_printf(bio_err," -noout do not output REQ\n");
5070Sstevel@tonic-gate BIO_printf(bio_err," -verify verify signature on REQ\n");
5080Sstevel@tonic-gate BIO_printf(bio_err," -modulus RSA modulus\n");
5090Sstevel@tonic-gate BIO_printf(bio_err," -nodes don't encrypt the output key\n");
5100Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
5110Sstevel@tonic-gate BIO_printf(bio_err," -engine e use engine e, possibly a hardware device\n");
5120Sstevel@tonic-gate #endif
5130Sstevel@tonic-gate BIO_printf(bio_err," -subject output the request's subject\n");
5140Sstevel@tonic-gate BIO_printf(bio_err," -passin private key password source\n");
5150Sstevel@tonic-gate BIO_printf(bio_err," -key file use the private key contained in file\n");
5160Sstevel@tonic-gate BIO_printf(bio_err," -keyform arg key file format\n");
5170Sstevel@tonic-gate BIO_printf(bio_err," -keyout arg file to send the key to\n");
5180Sstevel@tonic-gate BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
5190Sstevel@tonic-gate BIO_printf(bio_err," load the file (or the files in the directory) into\n");
5200Sstevel@tonic-gate BIO_printf(bio_err," the random number generator\n");
5210Sstevel@tonic-gate BIO_printf(bio_err," -newkey rsa:bits generate a new RSA key of 'bits' in size\n");
5220Sstevel@tonic-gate BIO_printf(bio_err," -newkey dsa:file generate a new DSA key, parameters taken from CA in 'file'\n");
523*2139Sjp161948 #ifndef OPENSSL_NO_ECDSA
524*2139Sjp161948 BIO_printf(bio_err," -newkey ec:file generate a new EC key, parameters taken from CA in 'file'\n");
525*2139Sjp161948 #endif
5260Sstevel@tonic-gate BIO_printf(bio_err," -[digest] Digest to sign with (md5, sha1, md2, mdc2, md4)\n");
5270Sstevel@tonic-gate BIO_printf(bio_err," -config file request template file.\n");
5280Sstevel@tonic-gate BIO_printf(bio_err," -subj arg set or modify request subject\n");
529*2139Sjp161948 BIO_printf(bio_err," -multivalue-rdn enable support for multivalued RDNs\n");
5300Sstevel@tonic-gate BIO_printf(bio_err," -new new request.\n");
5310Sstevel@tonic-gate BIO_printf(bio_err," -batch do not ask anything during request generation\n");
5320Sstevel@tonic-gate BIO_printf(bio_err," -x509 output a x509 structure instead of a cert. req.\n");
5330Sstevel@tonic-gate BIO_printf(bio_err," -days number of days a certificate generated by -x509 is valid for.\n");
5340Sstevel@tonic-gate BIO_printf(bio_err," -set_serial serial number to use for a certificate generated by -x509.\n");
5350Sstevel@tonic-gate BIO_printf(bio_err," -newhdr output \"NEW\" in the header lines\n");
5360Sstevel@tonic-gate BIO_printf(bio_err," -asn1-kludge Output the 'request' in a format that is wrong but some CA's\n");
5370Sstevel@tonic-gate BIO_printf(bio_err," have been reported as requiring\n");
5380Sstevel@tonic-gate BIO_printf(bio_err," -extensions .. specify certificate extension section (override value in config file)\n");
5390Sstevel@tonic-gate BIO_printf(bio_err," -reqexts .. specify request extension section (override value in config file)\n");
5400Sstevel@tonic-gate BIO_printf(bio_err," -utf8 input characters are UTF8 (default ASCII)\n");
5410Sstevel@tonic-gate BIO_printf(bio_err," -nameopt arg - various certificate name options\n");
5420Sstevel@tonic-gate BIO_printf(bio_err," -reqopt arg - various request text options\n\n");
5430Sstevel@tonic-gate goto end;
5440Sstevel@tonic-gate }
5450Sstevel@tonic-gate
5460Sstevel@tonic-gate ERR_load_crypto_strings();
5470Sstevel@tonic-gate if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
5480Sstevel@tonic-gate BIO_printf(bio_err, "Error getting passwords\n");
5490Sstevel@tonic-gate goto end;
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate
5520Sstevel@tonic-gate #ifndef MONOLITH /* else this has happened in openssl.c (global `config') */
5530Sstevel@tonic-gate /* Lets load up our environment a little */
5540Sstevel@tonic-gate p=getenv("OPENSSL_CONF");
5550Sstevel@tonic-gate if (p == NULL)
5560Sstevel@tonic-gate p=getenv("SSLEAY_CONF");
5570Sstevel@tonic-gate if (p == NULL)
5580Sstevel@tonic-gate p=to_free=make_config_name();
5590Sstevel@tonic-gate default_config_file=p;
5600Sstevel@tonic-gate config=NCONF_new(NULL);
5610Sstevel@tonic-gate i=NCONF_load(config, p, &errline);
5620Sstevel@tonic-gate #endif
5630Sstevel@tonic-gate
5640Sstevel@tonic-gate if (template != NULL)
5650Sstevel@tonic-gate {
5660Sstevel@tonic-gate long errline = -1;
5670Sstevel@tonic-gate
5680Sstevel@tonic-gate if( verbose )
5690Sstevel@tonic-gate BIO_printf(bio_err,"Using configuration from %s\n",template);
5700Sstevel@tonic-gate req_conf=NCONF_new(NULL);
5710Sstevel@tonic-gate i=NCONF_load(req_conf,template,&errline);
5720Sstevel@tonic-gate if (i == 0)
5730Sstevel@tonic-gate {
5740Sstevel@tonic-gate BIO_printf(bio_err,"error on line %ld of %s\n",errline,template);
5750Sstevel@tonic-gate goto end;
5760Sstevel@tonic-gate }
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate else
5790Sstevel@tonic-gate {
5800Sstevel@tonic-gate req_conf=config;
581*2139Sjp161948
582*2139Sjp161948 if (req_conf == NULL)
583*2139Sjp161948 {
584*2139Sjp161948 BIO_printf(bio_err,"Unable to load config info from %s\n", default_config_file);
585*2139Sjp161948 if (newreq)
586*2139Sjp161948 goto end;
587*2139Sjp161948 }
588*2139Sjp161948 else if( verbose )
5890Sstevel@tonic-gate BIO_printf(bio_err,"Using configuration from %s\n",
5900Sstevel@tonic-gate default_config_file);
5910Sstevel@tonic-gate }
5920Sstevel@tonic-gate
5930Sstevel@tonic-gate if (req_conf != NULL)
5940Sstevel@tonic-gate {
5950Sstevel@tonic-gate if (!load_config(bio_err, req_conf))
5960Sstevel@tonic-gate goto end;
5970Sstevel@tonic-gate p=NCONF_get_string(req_conf,NULL,"oid_file");
5980Sstevel@tonic-gate if (p == NULL)
5990Sstevel@tonic-gate ERR_clear_error();
6000Sstevel@tonic-gate if (p != NULL)
6010Sstevel@tonic-gate {
6020Sstevel@tonic-gate BIO *oid_bio;
6030Sstevel@tonic-gate
6040Sstevel@tonic-gate oid_bio=BIO_new_file(p,"r");
6050Sstevel@tonic-gate if (oid_bio == NULL)
6060Sstevel@tonic-gate {
6070Sstevel@tonic-gate /*
6080Sstevel@tonic-gate BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
6090Sstevel@tonic-gate ERR_print_errors(bio_err);
6100Sstevel@tonic-gate */
6110Sstevel@tonic-gate }
6120Sstevel@tonic-gate else
6130Sstevel@tonic-gate {
6140Sstevel@tonic-gate OBJ_create_objects(oid_bio);
6150Sstevel@tonic-gate BIO_free(oid_bio);
6160Sstevel@tonic-gate }
6170Sstevel@tonic-gate }
6180Sstevel@tonic-gate }
6190Sstevel@tonic-gate if(!add_oid_section(bio_err, req_conf)) goto end;
6200Sstevel@tonic-gate
6210Sstevel@tonic-gate if (md_alg == NULL)
6220Sstevel@tonic-gate {
6230Sstevel@tonic-gate p=NCONF_get_string(req_conf,SECTION,"default_md");
6240Sstevel@tonic-gate if (p == NULL)
6250Sstevel@tonic-gate ERR_clear_error();
6260Sstevel@tonic-gate if (p != NULL)
6270Sstevel@tonic-gate {
6280Sstevel@tonic-gate if ((md_alg=EVP_get_digestbyname(p)) != NULL)
6290Sstevel@tonic-gate digest=md_alg;
6300Sstevel@tonic-gate }
6310Sstevel@tonic-gate }
6320Sstevel@tonic-gate
6330Sstevel@tonic-gate if (!extensions)
6340Sstevel@tonic-gate {
6350Sstevel@tonic-gate extensions = NCONF_get_string(req_conf, SECTION, V3_EXTENSIONS);
6360Sstevel@tonic-gate if (!extensions)
6370Sstevel@tonic-gate ERR_clear_error();
6380Sstevel@tonic-gate }
6390Sstevel@tonic-gate if (extensions) {
6400Sstevel@tonic-gate /* Check syntax of file */
6410Sstevel@tonic-gate X509V3_CTX ctx;
6420Sstevel@tonic-gate X509V3_set_ctx_test(&ctx);
6430Sstevel@tonic-gate X509V3_set_nconf(&ctx, req_conf);
6440Sstevel@tonic-gate if(!X509V3_EXT_add_nconf(req_conf, &ctx, extensions, NULL)) {
6450Sstevel@tonic-gate BIO_printf(bio_err,
6460Sstevel@tonic-gate "Error Loading extension section %s\n", extensions);
6470Sstevel@tonic-gate goto end;
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate }
6500Sstevel@tonic-gate
6510Sstevel@tonic-gate if(!passin)
6520Sstevel@tonic-gate {
6530Sstevel@tonic-gate passin = NCONF_get_string(req_conf, SECTION, "input_password");
6540Sstevel@tonic-gate if (!passin)
6550Sstevel@tonic-gate ERR_clear_error();
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate
6580Sstevel@tonic-gate if(!passout)
6590Sstevel@tonic-gate {
6600Sstevel@tonic-gate passout = NCONF_get_string(req_conf, SECTION, "output_password");
6610Sstevel@tonic-gate if (!passout)
6620Sstevel@tonic-gate ERR_clear_error();
6630Sstevel@tonic-gate }
6640Sstevel@tonic-gate
6650Sstevel@tonic-gate p = NCONF_get_string(req_conf, SECTION, STRING_MASK);
6660Sstevel@tonic-gate if (!p)
6670Sstevel@tonic-gate ERR_clear_error();
6680Sstevel@tonic-gate
6690Sstevel@tonic-gate if(p && !ASN1_STRING_set_default_mask_asc(p)) {
6700Sstevel@tonic-gate BIO_printf(bio_err, "Invalid global string mask setting %s\n", p);
6710Sstevel@tonic-gate goto end;
6720Sstevel@tonic-gate }
6730Sstevel@tonic-gate
6740Sstevel@tonic-gate if (chtype != MBSTRING_UTF8)
6750Sstevel@tonic-gate {
6760Sstevel@tonic-gate p = NCONF_get_string(req_conf, SECTION, UTF8_IN);
6770Sstevel@tonic-gate if (!p)
6780Sstevel@tonic-gate ERR_clear_error();
6790Sstevel@tonic-gate else if (!strcmp(p, "yes"))
6800Sstevel@tonic-gate chtype = MBSTRING_UTF8;
6810Sstevel@tonic-gate }
6820Sstevel@tonic-gate
6830Sstevel@tonic-gate
6840Sstevel@tonic-gate if(!req_exts)
6850Sstevel@tonic-gate {
6860Sstevel@tonic-gate req_exts = NCONF_get_string(req_conf, SECTION, REQ_EXTENSIONS);
6870Sstevel@tonic-gate if (!req_exts)
6880Sstevel@tonic-gate ERR_clear_error();
6890Sstevel@tonic-gate }
6900Sstevel@tonic-gate if(req_exts) {
6910Sstevel@tonic-gate /* Check syntax of file */
6920Sstevel@tonic-gate X509V3_CTX ctx;
6930Sstevel@tonic-gate X509V3_set_ctx_test(&ctx);
6940Sstevel@tonic-gate X509V3_set_nconf(&ctx, req_conf);
6950Sstevel@tonic-gate if(!X509V3_EXT_add_nconf(req_conf, &ctx, req_exts, NULL)) {
6960Sstevel@tonic-gate BIO_printf(bio_err,
6970Sstevel@tonic-gate "Error Loading request extension section %s\n",
6980Sstevel@tonic-gate req_exts);
6990Sstevel@tonic-gate goto end;
7000Sstevel@tonic-gate }
7010Sstevel@tonic-gate }
7020Sstevel@tonic-gate
7030Sstevel@tonic-gate in=BIO_new(BIO_s_file());
7040Sstevel@tonic-gate out=BIO_new(BIO_s_file());
7050Sstevel@tonic-gate if ((in == NULL) || (out == NULL))
7060Sstevel@tonic-gate goto end;
7070Sstevel@tonic-gate
7080Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
7090Sstevel@tonic-gate e = setup_engine(bio_err, engine, 0);
7100Sstevel@tonic-gate #endif
7110Sstevel@tonic-gate
7120Sstevel@tonic-gate if (keyfile != NULL)
7130Sstevel@tonic-gate {
7140Sstevel@tonic-gate pkey = load_key(bio_err, keyfile, keyform, 0, passin, e,
7150Sstevel@tonic-gate "Private Key");
7160Sstevel@tonic-gate if (!pkey)
7170Sstevel@tonic-gate {
7180Sstevel@tonic-gate /* load_key() has already printed an appropriate
7190Sstevel@tonic-gate message */
7200Sstevel@tonic-gate goto end;
7210Sstevel@tonic-gate }
722*2139Sjp161948 if (EVP_PKEY_type(pkey->type) == EVP_PKEY_DSA ||
723*2139Sjp161948 EVP_PKEY_type(pkey->type) == EVP_PKEY_EC)
7240Sstevel@tonic-gate {
7250Sstevel@tonic-gate char *randfile = NCONF_get_string(req_conf,SECTION,"RANDFILE");
7260Sstevel@tonic-gate if (randfile == NULL)
7270Sstevel@tonic-gate ERR_clear_error();
7280Sstevel@tonic-gate app_RAND_load_file(randfile, bio_err, 0);
7290Sstevel@tonic-gate }
7300Sstevel@tonic-gate }
7310Sstevel@tonic-gate
7320Sstevel@tonic-gate if (newreq && (pkey == NULL))
7330Sstevel@tonic-gate {
734*2139Sjp161948 #ifndef OPENSSL_NO_RSA
735*2139Sjp161948 BN_GENCB cb;
736*2139Sjp161948 #endif
7370Sstevel@tonic-gate char *randfile = NCONF_get_string(req_conf,SECTION,"RANDFILE");
7380Sstevel@tonic-gate if (randfile == NULL)
7390Sstevel@tonic-gate ERR_clear_error();
7400Sstevel@tonic-gate app_RAND_load_file(randfile, bio_err, 0);
7410Sstevel@tonic-gate if (inrand)
7420Sstevel@tonic-gate app_RAND_load_files(inrand);
7430Sstevel@tonic-gate
7440Sstevel@tonic-gate if (newkey <= 0)
7450Sstevel@tonic-gate {
7460Sstevel@tonic-gate if (!NCONF_get_number(req_conf,SECTION,BITS, &newkey))
7470Sstevel@tonic-gate newkey=DEFAULT_KEY_LENGTH;
7480Sstevel@tonic-gate }
7490Sstevel@tonic-gate
750*2139Sjp161948 if (newkey < MIN_KEY_LENGTH && (pkey_type == TYPE_RSA || pkey_type == TYPE_DSA))
7510Sstevel@tonic-gate {
7520Sstevel@tonic-gate BIO_printf(bio_err,"private key length is too short,\n");
753*2139Sjp161948 BIO_printf(bio_err,"it needs to be at least %d bits, not %ld\n",MIN_KEY_LENGTH,newkey);
7540Sstevel@tonic-gate goto end;
7550Sstevel@tonic-gate }
756*2139Sjp161948 BIO_printf(bio_err,"Generating a %ld bit %s private key\n",
757*2139Sjp161948 newkey,(pkey_type == TYPE_RSA)?"RSA":
758*2139Sjp161948 (pkey_type == TYPE_DSA)?"DSA":"EC");
7590Sstevel@tonic-gate
7600Sstevel@tonic-gate if ((pkey=EVP_PKEY_new()) == NULL) goto end;
7610Sstevel@tonic-gate
7620Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
763*2139Sjp161948 BN_GENCB_set(&cb, req_cb, bio_err);
7640Sstevel@tonic-gate if (pkey_type == TYPE_RSA)
7650Sstevel@tonic-gate {
766*2139Sjp161948 RSA *rsa = RSA_new();
767*2139Sjp161948 BIGNUM *bn = BN_new();
768*2139Sjp161948 if(!bn || !rsa || !BN_set_word(bn, 0x10001) ||
769*2139Sjp161948 !RSA_generate_key_ex(rsa, newkey, bn, &cb) ||
770*2139Sjp161948 !EVP_PKEY_assign_RSA(pkey, rsa))
771*2139Sjp161948 {
772*2139Sjp161948 if(bn) BN_free(bn);
773*2139Sjp161948 if(rsa) RSA_free(rsa);
7740Sstevel@tonic-gate goto end;
775*2139Sjp161948 }
776*2139Sjp161948 BN_free(bn);
7770Sstevel@tonic-gate }
7780Sstevel@tonic-gate else
7790Sstevel@tonic-gate #endif
7800Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
7810Sstevel@tonic-gate if (pkey_type == TYPE_DSA)
7820Sstevel@tonic-gate {
7830Sstevel@tonic-gate if (!DSA_generate_key(dsa_params)) goto end;
7840Sstevel@tonic-gate if (!EVP_PKEY_assign_DSA(pkey,dsa_params)) goto end;
7850Sstevel@tonic-gate dsa_params=NULL;
7860Sstevel@tonic-gate }
7870Sstevel@tonic-gate #endif
788*2139Sjp161948 #ifndef OPENSSL_NO_ECDSA
789*2139Sjp161948 if (pkey_type == TYPE_EC)
790*2139Sjp161948 {
791*2139Sjp161948 if (!EC_KEY_generate_key(ec_params)) goto end;
792*2139Sjp161948 if (!EVP_PKEY_assign_EC_KEY(pkey, ec_params))
793*2139Sjp161948 goto end;
794*2139Sjp161948 ec_params = NULL;
795*2139Sjp161948 }
796*2139Sjp161948 #endif
7970Sstevel@tonic-gate
7980Sstevel@tonic-gate app_RAND_write_file(randfile, bio_err);
7990Sstevel@tonic-gate
8000Sstevel@tonic-gate if (pkey == NULL) goto end;
8010Sstevel@tonic-gate
8020Sstevel@tonic-gate if (keyout == NULL)
8030Sstevel@tonic-gate {
8040Sstevel@tonic-gate keyout=NCONF_get_string(req_conf,SECTION,KEYFILE);
8050Sstevel@tonic-gate if (keyout == NULL)
8060Sstevel@tonic-gate ERR_clear_error();
8070Sstevel@tonic-gate }
8080Sstevel@tonic-gate
8090Sstevel@tonic-gate if (keyout == NULL)
8100Sstevel@tonic-gate {
8110Sstevel@tonic-gate BIO_printf(bio_err,"writing new private key to stdout\n");
8120Sstevel@tonic-gate BIO_set_fp(out,stdout,BIO_NOCLOSE);
8130Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
8140Sstevel@tonic-gate {
8150Sstevel@tonic-gate BIO *tmpbio = BIO_new(BIO_f_linebuffer());
8160Sstevel@tonic-gate out = BIO_push(tmpbio, out);
8170Sstevel@tonic-gate }
8180Sstevel@tonic-gate #endif
8190Sstevel@tonic-gate }
8200Sstevel@tonic-gate else
8210Sstevel@tonic-gate {
8220Sstevel@tonic-gate BIO_printf(bio_err,"writing new private key to '%s'\n",keyout);
8230Sstevel@tonic-gate if (BIO_write_filename(out,keyout) <= 0)
8240Sstevel@tonic-gate {
8250Sstevel@tonic-gate perror(keyout);
8260Sstevel@tonic-gate goto end;
8270Sstevel@tonic-gate }
8280Sstevel@tonic-gate }
8290Sstevel@tonic-gate
8300Sstevel@tonic-gate p=NCONF_get_string(req_conf,SECTION,"encrypt_rsa_key");
8310Sstevel@tonic-gate if (p == NULL)
8320Sstevel@tonic-gate {
8330Sstevel@tonic-gate ERR_clear_error();
8340Sstevel@tonic-gate p=NCONF_get_string(req_conf,SECTION,"encrypt_key");
8350Sstevel@tonic-gate if (p == NULL)
8360Sstevel@tonic-gate ERR_clear_error();
8370Sstevel@tonic-gate }
8380Sstevel@tonic-gate if ((p != NULL) && (strcmp(p,"no") == 0))
8390Sstevel@tonic-gate cipher=NULL;
8400Sstevel@tonic-gate if (nodes) cipher=NULL;
8410Sstevel@tonic-gate
8420Sstevel@tonic-gate i=0;
8430Sstevel@tonic-gate loop:
8440Sstevel@tonic-gate if (!PEM_write_bio_PrivateKey(out,pkey,cipher,
8450Sstevel@tonic-gate NULL,0,NULL,passout))
8460Sstevel@tonic-gate {
8470Sstevel@tonic-gate if ((ERR_GET_REASON(ERR_peek_error()) ==
8480Sstevel@tonic-gate PEM_R_PROBLEMS_GETTING_PASSWORD) && (i < 3))
8490Sstevel@tonic-gate {
8500Sstevel@tonic-gate ERR_clear_error();
8510Sstevel@tonic-gate i++;
8520Sstevel@tonic-gate goto loop;
8530Sstevel@tonic-gate }
8540Sstevel@tonic-gate goto end;
8550Sstevel@tonic-gate }
8560Sstevel@tonic-gate BIO_printf(bio_err,"-----\n");
8570Sstevel@tonic-gate }
8580Sstevel@tonic-gate
8590Sstevel@tonic-gate if (!newreq)
8600Sstevel@tonic-gate {
8610Sstevel@tonic-gate /* Since we are using a pre-existing certificate
8620Sstevel@tonic-gate * request, the kludge 'format' info should not be
8630Sstevel@tonic-gate * changed. */
8640Sstevel@tonic-gate kludge= -1;
8650Sstevel@tonic-gate if (infile == NULL)
8660Sstevel@tonic-gate BIO_set_fp(in,stdin,BIO_NOCLOSE);
8670Sstevel@tonic-gate else
8680Sstevel@tonic-gate {
8690Sstevel@tonic-gate if (BIO_read_filename(in,infile) <= 0)
8700Sstevel@tonic-gate {
8710Sstevel@tonic-gate perror(infile);
8720Sstevel@tonic-gate goto end;
8730Sstevel@tonic-gate }
8740Sstevel@tonic-gate }
8750Sstevel@tonic-gate
8760Sstevel@tonic-gate if (informat == FORMAT_ASN1)
8770Sstevel@tonic-gate req=d2i_X509_REQ_bio(in,NULL);
8780Sstevel@tonic-gate else if (informat == FORMAT_PEM)
8790Sstevel@tonic-gate req=PEM_read_bio_X509_REQ(in,NULL,NULL,NULL);
8800Sstevel@tonic-gate else
8810Sstevel@tonic-gate {
8820Sstevel@tonic-gate BIO_printf(bio_err,"bad input format specified for X509 request\n");
8830Sstevel@tonic-gate goto end;
8840Sstevel@tonic-gate }
8850Sstevel@tonic-gate if (req == NULL)
8860Sstevel@tonic-gate {
8870Sstevel@tonic-gate BIO_printf(bio_err,"unable to load X509 request\n");
8880Sstevel@tonic-gate goto end;
8890Sstevel@tonic-gate }
8900Sstevel@tonic-gate }
8910Sstevel@tonic-gate
8920Sstevel@tonic-gate if (newreq || x509)
8930Sstevel@tonic-gate {
8940Sstevel@tonic-gate if (pkey == NULL)
8950Sstevel@tonic-gate {
8960Sstevel@tonic-gate BIO_printf(bio_err,"you need to specify a private key\n");
8970Sstevel@tonic-gate goto end;
8980Sstevel@tonic-gate }
8990Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
9000Sstevel@tonic-gate if (pkey->type == EVP_PKEY_DSA)
9010Sstevel@tonic-gate digest=EVP_dss1();
9020Sstevel@tonic-gate #endif
903*2139Sjp161948 #ifndef OPENSSL_NO_ECDSA
904*2139Sjp161948 if (pkey->type == EVP_PKEY_EC)
905*2139Sjp161948 digest=EVP_ecdsa();
906*2139Sjp161948 #endif
9070Sstevel@tonic-gate if (req == NULL)
9080Sstevel@tonic-gate {
9090Sstevel@tonic-gate req=X509_REQ_new();
9100Sstevel@tonic-gate if (req == NULL)
9110Sstevel@tonic-gate {
9120Sstevel@tonic-gate goto end;
9130Sstevel@tonic-gate }
9140Sstevel@tonic-gate
915*2139Sjp161948 i=make_REQ(req,pkey,subj,multirdn,!x509, chtype);
9160Sstevel@tonic-gate subj=NULL; /* done processing '-subj' option */
9170Sstevel@tonic-gate if ((kludge > 0) && !sk_X509_ATTRIBUTE_num(req->req_info->attributes))
9180Sstevel@tonic-gate {
9190Sstevel@tonic-gate sk_X509_ATTRIBUTE_free(req->req_info->attributes);
9200Sstevel@tonic-gate req->req_info->attributes = NULL;
9210Sstevel@tonic-gate }
9220Sstevel@tonic-gate if (!i)
9230Sstevel@tonic-gate {
9240Sstevel@tonic-gate BIO_printf(bio_err,"problems making Certificate Request\n");
9250Sstevel@tonic-gate goto end;
9260Sstevel@tonic-gate }
9270Sstevel@tonic-gate }
9280Sstevel@tonic-gate if (x509)
9290Sstevel@tonic-gate {
9300Sstevel@tonic-gate EVP_PKEY *tmppkey;
9310Sstevel@tonic-gate X509V3_CTX ext_ctx;
9320Sstevel@tonic-gate if ((x509ss=X509_new()) == NULL) goto end;
9330Sstevel@tonic-gate
9340Sstevel@tonic-gate /* Set version to V3 */
9350Sstevel@tonic-gate if(extensions && !X509_set_version(x509ss, 2)) goto end;
9360Sstevel@tonic-gate if (serial)
9370Sstevel@tonic-gate {
9380Sstevel@tonic-gate if (!X509_set_serialNumber(x509ss, serial)) goto end;
9390Sstevel@tonic-gate }
9400Sstevel@tonic-gate else
9410Sstevel@tonic-gate {
942*2139Sjp161948 if (!rand_serial(NULL,
943*2139Sjp161948 X509_get_serialNumber(x509ss)))
944*2139Sjp161948 goto end;
9450Sstevel@tonic-gate }
9460Sstevel@tonic-gate
9470Sstevel@tonic-gate if (!X509_set_issuer_name(x509ss, X509_REQ_get_subject_name(req))) goto end;
9480Sstevel@tonic-gate if (!X509_gmtime_adj(X509_get_notBefore(x509ss),0)) goto end;
9490Sstevel@tonic-gate if (!X509_gmtime_adj(X509_get_notAfter(x509ss), (long)60*60*24*days)) goto end;
9500Sstevel@tonic-gate if (!X509_set_subject_name(x509ss, X509_REQ_get_subject_name(req))) goto end;
9510Sstevel@tonic-gate tmppkey = X509_REQ_get_pubkey(req);
9520Sstevel@tonic-gate if (!tmppkey || !X509_set_pubkey(x509ss,tmppkey)) goto end;
9530Sstevel@tonic-gate EVP_PKEY_free(tmppkey);
9540Sstevel@tonic-gate
9550Sstevel@tonic-gate /* Set up V3 context struct */
9560Sstevel@tonic-gate
9570Sstevel@tonic-gate X509V3_set_ctx(&ext_ctx, x509ss, x509ss, NULL, NULL, 0);
9580Sstevel@tonic-gate X509V3_set_nconf(&ext_ctx, req_conf);
9590Sstevel@tonic-gate
9600Sstevel@tonic-gate /* Add extensions */
9610Sstevel@tonic-gate if(extensions && !X509V3_EXT_add_nconf(req_conf,
9620Sstevel@tonic-gate &ext_ctx, extensions, x509ss))
9630Sstevel@tonic-gate {
9640Sstevel@tonic-gate BIO_printf(bio_err,
9650Sstevel@tonic-gate "Error Loading extension section %s\n",
9660Sstevel@tonic-gate extensions);
9670Sstevel@tonic-gate goto end;
9680Sstevel@tonic-gate }
9690Sstevel@tonic-gate
9700Sstevel@tonic-gate if (!(i=X509_sign(x509ss,pkey,digest)))
9710Sstevel@tonic-gate goto end;
9720Sstevel@tonic-gate }
9730Sstevel@tonic-gate else
9740Sstevel@tonic-gate {
9750Sstevel@tonic-gate X509V3_CTX ext_ctx;
9760Sstevel@tonic-gate
9770Sstevel@tonic-gate /* Set up V3 context struct */
9780Sstevel@tonic-gate
9790Sstevel@tonic-gate X509V3_set_ctx(&ext_ctx, NULL, NULL, req, NULL, 0);
9800Sstevel@tonic-gate X509V3_set_nconf(&ext_ctx, req_conf);
9810Sstevel@tonic-gate
9820Sstevel@tonic-gate /* Add extensions */
9830Sstevel@tonic-gate if(req_exts && !X509V3_EXT_REQ_add_nconf(req_conf,
9840Sstevel@tonic-gate &ext_ctx, req_exts, req))
9850Sstevel@tonic-gate {
9860Sstevel@tonic-gate BIO_printf(bio_err,
9870Sstevel@tonic-gate "Error Loading extension section %s\n",
9880Sstevel@tonic-gate req_exts);
9890Sstevel@tonic-gate goto end;
9900Sstevel@tonic-gate }
9910Sstevel@tonic-gate if (!(i=X509_REQ_sign(req,pkey,digest)))
9920Sstevel@tonic-gate goto end;
9930Sstevel@tonic-gate }
9940Sstevel@tonic-gate }
9950Sstevel@tonic-gate
9960Sstevel@tonic-gate if (subj && x509)
9970Sstevel@tonic-gate {
9980Sstevel@tonic-gate BIO_printf(bio_err, "Cannot modifiy certificate subject\n");
9990Sstevel@tonic-gate goto end;
10000Sstevel@tonic-gate }
10010Sstevel@tonic-gate
10020Sstevel@tonic-gate if (subj && !x509)
10030Sstevel@tonic-gate {
10040Sstevel@tonic-gate if (verbose)
10050Sstevel@tonic-gate {
10060Sstevel@tonic-gate BIO_printf(bio_err, "Modifying Request's Subject\n");
10070Sstevel@tonic-gate print_name(bio_err, "old subject=", X509_REQ_get_subject_name(req), nmflag);
10080Sstevel@tonic-gate }
10090Sstevel@tonic-gate
1010*2139Sjp161948 if (build_subject(req, subj, chtype, multirdn) == 0)
10110Sstevel@tonic-gate {
10120Sstevel@tonic-gate BIO_printf(bio_err, "ERROR: cannot modify subject\n");
10130Sstevel@tonic-gate ex=1;
10140Sstevel@tonic-gate goto end;
10150Sstevel@tonic-gate }
10160Sstevel@tonic-gate
10170Sstevel@tonic-gate req->req_info->enc.modified = 1;
10180Sstevel@tonic-gate
10190Sstevel@tonic-gate if (verbose)
10200Sstevel@tonic-gate {
10210Sstevel@tonic-gate print_name(bio_err, "new subject=", X509_REQ_get_subject_name(req), nmflag);
10220Sstevel@tonic-gate }
10230Sstevel@tonic-gate }
10240Sstevel@tonic-gate
10250Sstevel@tonic-gate if (verify && !x509)
10260Sstevel@tonic-gate {
10270Sstevel@tonic-gate int tmp=0;
10280Sstevel@tonic-gate
10290Sstevel@tonic-gate if (pkey == NULL)
10300Sstevel@tonic-gate {
10310Sstevel@tonic-gate pkey=X509_REQ_get_pubkey(req);
10320Sstevel@tonic-gate tmp=1;
10330Sstevel@tonic-gate if (pkey == NULL) goto end;
10340Sstevel@tonic-gate }
10350Sstevel@tonic-gate
10360Sstevel@tonic-gate i=X509_REQ_verify(req,pkey);
10370Sstevel@tonic-gate if (tmp) {
10380Sstevel@tonic-gate EVP_PKEY_free(pkey);
10390Sstevel@tonic-gate pkey=NULL;
10400Sstevel@tonic-gate }
10410Sstevel@tonic-gate
10420Sstevel@tonic-gate if (i < 0)
10430Sstevel@tonic-gate {
10440Sstevel@tonic-gate goto end;
10450Sstevel@tonic-gate }
10460Sstevel@tonic-gate else if (i == 0)
10470Sstevel@tonic-gate {
10480Sstevel@tonic-gate BIO_printf(bio_err,"verify failure\n");
10490Sstevel@tonic-gate ERR_print_errors(bio_err);
10500Sstevel@tonic-gate }
10510Sstevel@tonic-gate else /* if (i > 0) */
10520Sstevel@tonic-gate BIO_printf(bio_err,"verify OK\n");
10530Sstevel@tonic-gate }
10540Sstevel@tonic-gate
10550Sstevel@tonic-gate if (noout && !text && !modulus && !subject && !pubkey)
10560Sstevel@tonic-gate {
10570Sstevel@tonic-gate ex=0;
10580Sstevel@tonic-gate goto end;
10590Sstevel@tonic-gate }
10600Sstevel@tonic-gate
10610Sstevel@tonic-gate if (outfile == NULL)
10620Sstevel@tonic-gate {
10630Sstevel@tonic-gate BIO_set_fp(out,stdout,BIO_NOCLOSE);
10640Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
10650Sstevel@tonic-gate {
10660Sstevel@tonic-gate BIO *tmpbio = BIO_new(BIO_f_linebuffer());
10670Sstevel@tonic-gate out = BIO_push(tmpbio, out);
10680Sstevel@tonic-gate }
10690Sstevel@tonic-gate #endif
10700Sstevel@tonic-gate }
10710Sstevel@tonic-gate else
10720Sstevel@tonic-gate {
10730Sstevel@tonic-gate if ((keyout != NULL) && (strcmp(outfile,keyout) == 0))
10740Sstevel@tonic-gate i=(int)BIO_append_filename(out,outfile);
10750Sstevel@tonic-gate else
10760Sstevel@tonic-gate i=(int)BIO_write_filename(out,outfile);
10770Sstevel@tonic-gate if (!i)
10780Sstevel@tonic-gate {
10790Sstevel@tonic-gate perror(outfile);
10800Sstevel@tonic-gate goto end;
10810Sstevel@tonic-gate }
10820Sstevel@tonic-gate }
10830Sstevel@tonic-gate
10840Sstevel@tonic-gate if (pubkey)
10850Sstevel@tonic-gate {
10860Sstevel@tonic-gate EVP_PKEY *tpubkey;
10870Sstevel@tonic-gate tpubkey=X509_REQ_get_pubkey(req);
10880Sstevel@tonic-gate if (tpubkey == NULL)
10890Sstevel@tonic-gate {
10900Sstevel@tonic-gate BIO_printf(bio_err,"Error getting public key\n");
10910Sstevel@tonic-gate ERR_print_errors(bio_err);
10920Sstevel@tonic-gate goto end;
10930Sstevel@tonic-gate }
10940Sstevel@tonic-gate PEM_write_bio_PUBKEY(out, tpubkey);
10950Sstevel@tonic-gate EVP_PKEY_free(tpubkey);
10960Sstevel@tonic-gate }
10970Sstevel@tonic-gate
10980Sstevel@tonic-gate if (text)
10990Sstevel@tonic-gate {
11000Sstevel@tonic-gate if (x509)
11010Sstevel@tonic-gate X509_print_ex(out, x509ss, nmflag, reqflag);
11020Sstevel@tonic-gate else
11030Sstevel@tonic-gate X509_REQ_print_ex(out, req, nmflag, reqflag);
11040Sstevel@tonic-gate }
11050Sstevel@tonic-gate
11060Sstevel@tonic-gate if(subject)
11070Sstevel@tonic-gate {
11080Sstevel@tonic-gate if(x509)
11090Sstevel@tonic-gate print_name(out, "subject=", X509_get_subject_name(x509ss), nmflag);
11100Sstevel@tonic-gate else
11110Sstevel@tonic-gate print_name(out, "subject=", X509_REQ_get_subject_name(req), nmflag);
11120Sstevel@tonic-gate }
11130Sstevel@tonic-gate
11140Sstevel@tonic-gate if (modulus)
11150Sstevel@tonic-gate {
11160Sstevel@tonic-gate EVP_PKEY *tpubkey;
11170Sstevel@tonic-gate
11180Sstevel@tonic-gate if (x509)
11190Sstevel@tonic-gate tpubkey=X509_get_pubkey(x509ss);
11200Sstevel@tonic-gate else
11210Sstevel@tonic-gate tpubkey=X509_REQ_get_pubkey(req);
11220Sstevel@tonic-gate if (tpubkey == NULL)
11230Sstevel@tonic-gate {
11240Sstevel@tonic-gate fprintf(stdout,"Modulus=unavailable\n");
11250Sstevel@tonic-gate goto end;
11260Sstevel@tonic-gate }
11270Sstevel@tonic-gate fprintf(stdout,"Modulus=");
11280Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
11290Sstevel@tonic-gate if (tpubkey->type == EVP_PKEY_RSA)
11300Sstevel@tonic-gate BN_print(out,tpubkey->pkey.rsa->n);
11310Sstevel@tonic-gate else
11320Sstevel@tonic-gate #endif
11330Sstevel@tonic-gate fprintf(stdout,"Wrong Algorithm type");
11340Sstevel@tonic-gate EVP_PKEY_free(tpubkey);
11350Sstevel@tonic-gate fprintf(stdout,"\n");
11360Sstevel@tonic-gate }
11370Sstevel@tonic-gate
11380Sstevel@tonic-gate if (!noout && !x509)
11390Sstevel@tonic-gate {
11400Sstevel@tonic-gate if (outformat == FORMAT_ASN1)
11410Sstevel@tonic-gate i=i2d_X509_REQ_bio(out,req);
11420Sstevel@tonic-gate else if (outformat == FORMAT_PEM) {
11430Sstevel@tonic-gate if(newhdr) i=PEM_write_bio_X509_REQ_NEW(out,req);
11440Sstevel@tonic-gate else i=PEM_write_bio_X509_REQ(out,req);
11450Sstevel@tonic-gate } else {
11460Sstevel@tonic-gate BIO_printf(bio_err,"bad output format specified for outfile\n");
11470Sstevel@tonic-gate goto end;
11480Sstevel@tonic-gate }
11490Sstevel@tonic-gate if (!i)
11500Sstevel@tonic-gate {
11510Sstevel@tonic-gate BIO_printf(bio_err,"unable to write X509 request\n");
11520Sstevel@tonic-gate goto end;
11530Sstevel@tonic-gate }
11540Sstevel@tonic-gate }
11550Sstevel@tonic-gate if (!noout && x509 && (x509ss != NULL))
11560Sstevel@tonic-gate {
11570Sstevel@tonic-gate if (outformat == FORMAT_ASN1)
11580Sstevel@tonic-gate i=i2d_X509_bio(out,x509ss);
11590Sstevel@tonic-gate else if (outformat == FORMAT_PEM)
11600Sstevel@tonic-gate i=PEM_write_bio_X509(out,x509ss);
11610Sstevel@tonic-gate else {
11620Sstevel@tonic-gate BIO_printf(bio_err,"bad output format specified for outfile\n");
11630Sstevel@tonic-gate goto end;
11640Sstevel@tonic-gate }
11650Sstevel@tonic-gate if (!i)
11660Sstevel@tonic-gate {
11670Sstevel@tonic-gate BIO_printf(bio_err,"unable to write X509 certificate\n");
11680Sstevel@tonic-gate goto end;
11690Sstevel@tonic-gate }
11700Sstevel@tonic-gate }
11710Sstevel@tonic-gate ex=0;
11720Sstevel@tonic-gate end:
11730Sstevel@tonic-gate #ifndef MONOLITH
11740Sstevel@tonic-gate if(to_free)
11750Sstevel@tonic-gate OPENSSL_free(to_free);
11760Sstevel@tonic-gate #endif
11770Sstevel@tonic-gate if (ex)
11780Sstevel@tonic-gate {
11790Sstevel@tonic-gate ERR_print_errors(bio_err);
11800Sstevel@tonic-gate }
11810Sstevel@tonic-gate if ((req_conf != NULL) && (req_conf != config)) NCONF_free(req_conf);
11820Sstevel@tonic-gate BIO_free(in);
11830Sstevel@tonic-gate BIO_free_all(out);
11840Sstevel@tonic-gate EVP_PKEY_free(pkey);
11850Sstevel@tonic-gate X509_REQ_free(req);
11860Sstevel@tonic-gate X509_free(x509ss);
11870Sstevel@tonic-gate ASN1_INTEGER_free(serial);
11880Sstevel@tonic-gate if(passargin && passin) OPENSSL_free(passin);
11890Sstevel@tonic-gate if(passargout && passout) OPENSSL_free(passout);
11900Sstevel@tonic-gate OBJ_cleanup();
11910Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
11920Sstevel@tonic-gate if (dsa_params != NULL) DSA_free(dsa_params);
11930Sstevel@tonic-gate #endif
1194*2139Sjp161948 #ifndef OPENSSL_NO_ECDSA
1195*2139Sjp161948 if (ec_params != NULL) EC_KEY_free(ec_params);
1196*2139Sjp161948 #endif
11970Sstevel@tonic-gate apps_shutdown();
11980Sstevel@tonic-gate OPENSSL_EXIT(ex);
11990Sstevel@tonic-gate }
12000Sstevel@tonic-gate
make_REQ(X509_REQ * req,EVP_PKEY * pkey,char * subj,int multirdn,int attribs,unsigned long chtype)1201*2139Sjp161948 static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, char *subj, int multirdn,
1202*2139Sjp161948 int attribs, unsigned long chtype)
12030Sstevel@tonic-gate {
12040Sstevel@tonic-gate int ret=0,i;
12050Sstevel@tonic-gate char no_prompt = 0;
12060Sstevel@tonic-gate STACK_OF(CONF_VALUE) *dn_sk, *attr_sk = NULL;
12070Sstevel@tonic-gate char *tmp, *dn_sect,*attr_sect;
12080Sstevel@tonic-gate
12090Sstevel@tonic-gate tmp=NCONF_get_string(req_conf,SECTION,PROMPT);
12100Sstevel@tonic-gate if (tmp == NULL)
12110Sstevel@tonic-gate ERR_clear_error();
12120Sstevel@tonic-gate if((tmp != NULL) && !strcmp(tmp, "no")) no_prompt = 1;
12130Sstevel@tonic-gate
12140Sstevel@tonic-gate dn_sect=NCONF_get_string(req_conf,SECTION,DISTINGUISHED_NAME);
12150Sstevel@tonic-gate if (dn_sect == NULL)
12160Sstevel@tonic-gate {
12170Sstevel@tonic-gate BIO_printf(bio_err,"unable to find '%s' in config\n",
12180Sstevel@tonic-gate DISTINGUISHED_NAME);
12190Sstevel@tonic-gate goto err;
12200Sstevel@tonic-gate }
12210Sstevel@tonic-gate dn_sk=NCONF_get_section(req_conf,dn_sect);
12220Sstevel@tonic-gate if (dn_sk == NULL)
12230Sstevel@tonic-gate {
12240Sstevel@tonic-gate BIO_printf(bio_err,"unable to get '%s' section\n",dn_sect);
12250Sstevel@tonic-gate goto err;
12260Sstevel@tonic-gate }
12270Sstevel@tonic-gate
12280Sstevel@tonic-gate attr_sect=NCONF_get_string(req_conf,SECTION,ATTRIBUTES);
12290Sstevel@tonic-gate if (attr_sect == NULL)
12300Sstevel@tonic-gate {
12310Sstevel@tonic-gate ERR_clear_error();
12320Sstevel@tonic-gate attr_sk=NULL;
12330Sstevel@tonic-gate }
12340Sstevel@tonic-gate else
12350Sstevel@tonic-gate {
12360Sstevel@tonic-gate attr_sk=NCONF_get_section(req_conf,attr_sect);
12370Sstevel@tonic-gate if (attr_sk == NULL)
12380Sstevel@tonic-gate {
12390Sstevel@tonic-gate BIO_printf(bio_err,"unable to get '%s' section\n",attr_sect);
12400Sstevel@tonic-gate goto err;
12410Sstevel@tonic-gate }
12420Sstevel@tonic-gate }
12430Sstevel@tonic-gate
12440Sstevel@tonic-gate /* setup version number */
12450Sstevel@tonic-gate if (!X509_REQ_set_version(req,0L)) goto err; /* version 1 */
12460Sstevel@tonic-gate
12470Sstevel@tonic-gate if (no_prompt)
12480Sstevel@tonic-gate i = auto_info(req, dn_sk, attr_sk, attribs, chtype);
12490Sstevel@tonic-gate else
12500Sstevel@tonic-gate {
12510Sstevel@tonic-gate if (subj)
1252*2139Sjp161948 i = build_subject(req, subj, chtype, multirdn);
12530Sstevel@tonic-gate else
12540Sstevel@tonic-gate i = prompt_info(req, dn_sk, dn_sect, attr_sk, attr_sect, attribs, chtype);
12550Sstevel@tonic-gate }
12560Sstevel@tonic-gate if(!i) goto err;
12570Sstevel@tonic-gate
12580Sstevel@tonic-gate if (!X509_REQ_set_pubkey(req,pkey)) goto err;
12590Sstevel@tonic-gate
12600Sstevel@tonic-gate ret=1;
12610Sstevel@tonic-gate err:
12620Sstevel@tonic-gate return(ret);
12630Sstevel@tonic-gate }
12640Sstevel@tonic-gate
12650Sstevel@tonic-gate /*
12660Sstevel@tonic-gate * subject is expected to be in the format /type0=value0/type1=value1/type2=...
12670Sstevel@tonic-gate * where characters may be escaped by \
12680Sstevel@tonic-gate */
build_subject(X509_REQ * req,char * subject,unsigned long chtype,int multirdn)1269*2139Sjp161948 static int build_subject(X509_REQ *req, char *subject, unsigned long chtype, int multirdn)
12700Sstevel@tonic-gate {
12710Sstevel@tonic-gate X509_NAME *n;
12720Sstevel@tonic-gate
1273*2139Sjp161948 if (!(n = parse_name(subject, chtype, multirdn)))
12740Sstevel@tonic-gate return 0;
12750Sstevel@tonic-gate
12760Sstevel@tonic-gate if (!X509_REQ_set_subject_name(req, n))
12770Sstevel@tonic-gate {
12780Sstevel@tonic-gate X509_NAME_free(n);
12790Sstevel@tonic-gate return 0;
12800Sstevel@tonic-gate }
12810Sstevel@tonic-gate X509_NAME_free(n);
12820Sstevel@tonic-gate return 1;
12830Sstevel@tonic-gate }
12840Sstevel@tonic-gate
12850Sstevel@tonic-gate
prompt_info(X509_REQ * req,STACK_OF (CONF_VALUE)* dn_sk,char * dn_sect,STACK_OF (CONF_VALUE)* attr_sk,char * attr_sect,int attribs,unsigned long chtype)12860Sstevel@tonic-gate static int prompt_info(X509_REQ *req,
12870Sstevel@tonic-gate STACK_OF(CONF_VALUE) *dn_sk, char *dn_sect,
12880Sstevel@tonic-gate STACK_OF(CONF_VALUE) *attr_sk, char *attr_sect, int attribs,
12890Sstevel@tonic-gate unsigned long chtype)
12900Sstevel@tonic-gate {
12910Sstevel@tonic-gate int i;
12920Sstevel@tonic-gate char *p,*q;
12930Sstevel@tonic-gate char buf[100];
1294*2139Sjp161948 int nid, mval;
12950Sstevel@tonic-gate long n_min,n_max;
1296*2139Sjp161948 char *type, *value;
1297*2139Sjp161948 const char *def;
12980Sstevel@tonic-gate CONF_VALUE *v;
12990Sstevel@tonic-gate X509_NAME *subj;
13000Sstevel@tonic-gate subj = X509_REQ_get_subject_name(req);
13010Sstevel@tonic-gate
13020Sstevel@tonic-gate if(!batch)
13030Sstevel@tonic-gate {
13040Sstevel@tonic-gate BIO_printf(bio_err,"You are about to be asked to enter information that will be incorporated\n");
13050Sstevel@tonic-gate BIO_printf(bio_err,"into your certificate request.\n");
13060Sstevel@tonic-gate BIO_printf(bio_err,"What you are about to enter is what is called a Distinguished Name or a DN.\n");
13070Sstevel@tonic-gate BIO_printf(bio_err,"There are quite a few fields but you can leave some blank\n");
13080Sstevel@tonic-gate BIO_printf(bio_err,"For some fields there will be a default value,\n");
13090Sstevel@tonic-gate BIO_printf(bio_err,"If you enter '.', the field will be left blank.\n");
13100Sstevel@tonic-gate BIO_printf(bio_err,"-----\n");
13110Sstevel@tonic-gate }
13120Sstevel@tonic-gate
13130Sstevel@tonic-gate
13140Sstevel@tonic-gate if (sk_CONF_VALUE_num(dn_sk))
13150Sstevel@tonic-gate {
13160Sstevel@tonic-gate i= -1;
13170Sstevel@tonic-gate start: for (;;)
13180Sstevel@tonic-gate {
13190Sstevel@tonic-gate i++;
13200Sstevel@tonic-gate if (sk_CONF_VALUE_num(dn_sk) <= i) break;
13210Sstevel@tonic-gate
13220Sstevel@tonic-gate v=sk_CONF_VALUE_value(dn_sk,i);
13230Sstevel@tonic-gate p=q=NULL;
13240Sstevel@tonic-gate type=v->name;
13250Sstevel@tonic-gate if(!check_end(type,"_min") || !check_end(type,"_max") ||
13260Sstevel@tonic-gate !check_end(type,"_default") ||
13270Sstevel@tonic-gate !check_end(type,"_value")) continue;
13280Sstevel@tonic-gate /* Skip past any leading X. X: X, etc to allow for
13290Sstevel@tonic-gate * multiple instances
13300Sstevel@tonic-gate */
13310Sstevel@tonic-gate for(p = v->name; *p ; p++)
13320Sstevel@tonic-gate if ((*p == ':') || (*p == ',') ||
13330Sstevel@tonic-gate (*p == '.')) {
13340Sstevel@tonic-gate p++;
13350Sstevel@tonic-gate if(*p) type = p;
13360Sstevel@tonic-gate break;
13370Sstevel@tonic-gate }
1338*2139Sjp161948 if (*type == '+')
1339*2139Sjp161948 {
1340*2139Sjp161948 mval = -1;
1341*2139Sjp161948 type++;
1342*2139Sjp161948 }
1343*2139Sjp161948 else
1344*2139Sjp161948 mval = 0;
13450Sstevel@tonic-gate /* If OBJ not recognised ignore it */
13460Sstevel@tonic-gate if ((nid=OBJ_txt2nid(type)) == NID_undef) goto start;
13470Sstevel@tonic-gate if (BIO_snprintf(buf,sizeof buf,"%s_default",v->name)
1348*2139Sjp161948 >= (int)sizeof(buf))
13490Sstevel@tonic-gate {
13500Sstevel@tonic-gate BIO_printf(bio_err,"Name '%s' too long\n",v->name);
13510Sstevel@tonic-gate return 0;
13520Sstevel@tonic-gate }
13530Sstevel@tonic-gate
13540Sstevel@tonic-gate if ((def=NCONF_get_string(req_conf,dn_sect,buf)) == NULL)
13550Sstevel@tonic-gate {
13560Sstevel@tonic-gate ERR_clear_error();
13570Sstevel@tonic-gate def="";
13580Sstevel@tonic-gate }
13590Sstevel@tonic-gate
13600Sstevel@tonic-gate BIO_snprintf(buf,sizeof buf,"%s_value",v->name);
13610Sstevel@tonic-gate if ((value=NCONF_get_string(req_conf,dn_sect,buf)) == NULL)
13620Sstevel@tonic-gate {
13630Sstevel@tonic-gate ERR_clear_error();
13640Sstevel@tonic-gate value=NULL;
13650Sstevel@tonic-gate }
13660Sstevel@tonic-gate
13670Sstevel@tonic-gate BIO_snprintf(buf,sizeof buf,"%s_min",v->name);
13680Sstevel@tonic-gate if (!NCONF_get_number(req_conf,dn_sect,buf, &n_min))
13690Sstevel@tonic-gate {
13700Sstevel@tonic-gate ERR_clear_error();
13710Sstevel@tonic-gate n_min = -1;
13720Sstevel@tonic-gate }
13730Sstevel@tonic-gate
13740Sstevel@tonic-gate BIO_snprintf(buf,sizeof buf,"%s_max",v->name);
13750Sstevel@tonic-gate if (!NCONF_get_number(req_conf,dn_sect,buf, &n_max))
13760Sstevel@tonic-gate {
13770Sstevel@tonic-gate ERR_clear_error();
13780Sstevel@tonic-gate n_max = -1;
13790Sstevel@tonic-gate }
13800Sstevel@tonic-gate
13810Sstevel@tonic-gate if (!add_DN_object(subj,v->value,def,value,nid,
1382*2139Sjp161948 n_min,n_max, chtype, mval))
13830Sstevel@tonic-gate return 0;
13840Sstevel@tonic-gate }
13850Sstevel@tonic-gate if (X509_NAME_entry_count(subj) == 0)
13860Sstevel@tonic-gate {
13870Sstevel@tonic-gate BIO_printf(bio_err,"error, no objects specified in config file\n");
13880Sstevel@tonic-gate return 0;
13890Sstevel@tonic-gate }
13900Sstevel@tonic-gate
13910Sstevel@tonic-gate if (attribs)
13920Sstevel@tonic-gate {
13930Sstevel@tonic-gate if ((attr_sk != NULL) && (sk_CONF_VALUE_num(attr_sk) > 0) && (!batch))
13940Sstevel@tonic-gate {
13950Sstevel@tonic-gate BIO_printf(bio_err,"\nPlease enter the following 'extra' attributes\n");
13960Sstevel@tonic-gate BIO_printf(bio_err,"to be sent with your certificate request\n");
13970Sstevel@tonic-gate }
13980Sstevel@tonic-gate
13990Sstevel@tonic-gate i= -1;
14000Sstevel@tonic-gate start2: for (;;)
14010Sstevel@tonic-gate {
14020Sstevel@tonic-gate i++;
14030Sstevel@tonic-gate if ((attr_sk == NULL) ||
14040Sstevel@tonic-gate (sk_CONF_VALUE_num(attr_sk) <= i))
14050Sstevel@tonic-gate break;
14060Sstevel@tonic-gate
14070Sstevel@tonic-gate v=sk_CONF_VALUE_value(attr_sk,i);
14080Sstevel@tonic-gate type=v->name;
14090Sstevel@tonic-gate if ((nid=OBJ_txt2nid(type)) == NID_undef)
14100Sstevel@tonic-gate goto start2;
14110Sstevel@tonic-gate
14120Sstevel@tonic-gate if (BIO_snprintf(buf,sizeof buf,"%s_default",type)
1413*2139Sjp161948 >= (int)sizeof(buf))
14140Sstevel@tonic-gate {
14150Sstevel@tonic-gate BIO_printf(bio_err,"Name '%s' too long\n",v->name);
14160Sstevel@tonic-gate return 0;
14170Sstevel@tonic-gate }
14180Sstevel@tonic-gate
14190Sstevel@tonic-gate if ((def=NCONF_get_string(req_conf,attr_sect,buf))
14200Sstevel@tonic-gate == NULL)
14210Sstevel@tonic-gate {
14220Sstevel@tonic-gate ERR_clear_error();
14230Sstevel@tonic-gate def="";
14240Sstevel@tonic-gate }
14250Sstevel@tonic-gate
14260Sstevel@tonic-gate
14270Sstevel@tonic-gate BIO_snprintf(buf,sizeof buf,"%s_value",type);
14280Sstevel@tonic-gate if ((value=NCONF_get_string(req_conf,attr_sect,buf))
14290Sstevel@tonic-gate == NULL)
14300Sstevel@tonic-gate {
14310Sstevel@tonic-gate ERR_clear_error();
14320Sstevel@tonic-gate value=NULL;
14330Sstevel@tonic-gate }
14340Sstevel@tonic-gate
14350Sstevel@tonic-gate BIO_snprintf(buf,sizeof buf,"%s_min",type);
14360Sstevel@tonic-gate if (!NCONF_get_number(req_conf,attr_sect,buf, &n_min))
14370Sstevel@tonic-gate n_min = -1;
14380Sstevel@tonic-gate
14390Sstevel@tonic-gate BIO_snprintf(buf,sizeof buf,"%s_max",type);
14400Sstevel@tonic-gate if (!NCONF_get_number(req_conf,attr_sect,buf, &n_max))
14410Sstevel@tonic-gate n_max = -1;
14420Sstevel@tonic-gate
14430Sstevel@tonic-gate if (!add_attribute_object(req,
14440Sstevel@tonic-gate v->value,def,value,nid,n_min,n_max, chtype))
14450Sstevel@tonic-gate return 0;
14460Sstevel@tonic-gate }
14470Sstevel@tonic-gate }
14480Sstevel@tonic-gate }
14490Sstevel@tonic-gate else
14500Sstevel@tonic-gate {
14510Sstevel@tonic-gate BIO_printf(bio_err,"No template, please set one up.\n");
14520Sstevel@tonic-gate return 0;
14530Sstevel@tonic-gate }
14540Sstevel@tonic-gate
14550Sstevel@tonic-gate return 1;
14560Sstevel@tonic-gate
14570Sstevel@tonic-gate }
14580Sstevel@tonic-gate
auto_info(X509_REQ * req,STACK_OF (CONF_VALUE)* dn_sk,STACK_OF (CONF_VALUE)* attr_sk,int attribs,unsigned long chtype)14590Sstevel@tonic-gate static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *dn_sk,
14600Sstevel@tonic-gate STACK_OF(CONF_VALUE) *attr_sk, int attribs, unsigned long chtype)
14610Sstevel@tonic-gate {
14620Sstevel@tonic-gate int i;
14630Sstevel@tonic-gate char *p,*q;
14640Sstevel@tonic-gate char *type;
14650Sstevel@tonic-gate CONF_VALUE *v;
14660Sstevel@tonic-gate X509_NAME *subj;
14670Sstevel@tonic-gate
14680Sstevel@tonic-gate subj = X509_REQ_get_subject_name(req);
14690Sstevel@tonic-gate
14700Sstevel@tonic-gate for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++)
14710Sstevel@tonic-gate {
1472*2139Sjp161948 int mval;
14730Sstevel@tonic-gate v=sk_CONF_VALUE_value(dn_sk,i);
14740Sstevel@tonic-gate p=q=NULL;
14750Sstevel@tonic-gate type=v->name;
14760Sstevel@tonic-gate /* Skip past any leading X. X: X, etc to allow for
14770Sstevel@tonic-gate * multiple instances
14780Sstevel@tonic-gate */
14790Sstevel@tonic-gate for(p = v->name; *p ; p++)
14800Sstevel@tonic-gate #ifndef CHARSET_EBCDIC
14810Sstevel@tonic-gate if ((*p == ':') || (*p == ',') || (*p == '.')) {
14820Sstevel@tonic-gate #else
14830Sstevel@tonic-gate if ((*p == os_toascii[':']) || (*p == os_toascii[',']) || (*p == os_toascii['.'])) {
14840Sstevel@tonic-gate #endif
14850Sstevel@tonic-gate p++;
14860Sstevel@tonic-gate if(*p) type = p;
14870Sstevel@tonic-gate break;
14880Sstevel@tonic-gate }
1489*2139Sjp161948 #ifndef CHARSET_EBCDIC
1490*2139Sjp161948 if (*p == '+')
1491*2139Sjp161948 #else
1492*2139Sjp161948 if (*p == os_toascii['+'])
1493*2139Sjp161948 #endif
1494*2139Sjp161948 {
1495*2139Sjp161948 p++;
1496*2139Sjp161948 mval = -1;
1497*2139Sjp161948 }
1498*2139Sjp161948 else
1499*2139Sjp161948 mval = 0;
15000Sstevel@tonic-gate if (!X509_NAME_add_entry_by_txt(subj,type, chtype,
1501*2139Sjp161948 (unsigned char *) v->value,-1,-1,mval)) return 0;
15020Sstevel@tonic-gate
15030Sstevel@tonic-gate }
15040Sstevel@tonic-gate
15050Sstevel@tonic-gate if (!X509_NAME_entry_count(subj))
15060Sstevel@tonic-gate {
15070Sstevel@tonic-gate BIO_printf(bio_err,"error, no objects specified in config file\n");
15080Sstevel@tonic-gate return 0;
15090Sstevel@tonic-gate }
15100Sstevel@tonic-gate if (attribs)
15110Sstevel@tonic-gate {
15120Sstevel@tonic-gate for (i = 0; i < sk_CONF_VALUE_num(attr_sk); i++)
15130Sstevel@tonic-gate {
15140Sstevel@tonic-gate v=sk_CONF_VALUE_value(attr_sk,i);
15150Sstevel@tonic-gate if(!X509_REQ_add1_attr_by_txt(req, v->name, chtype,
15160Sstevel@tonic-gate (unsigned char *)v->value, -1)) return 0;
15170Sstevel@tonic-gate }
15180Sstevel@tonic-gate }
15190Sstevel@tonic-gate return 1;
15200Sstevel@tonic-gate }
15210Sstevel@tonic-gate
15220Sstevel@tonic-gate
1523*2139Sjp161948 static int add_DN_object(X509_NAME *n, char *text, const char *def, char *value,
1524*2139Sjp161948 int nid, int n_min, int n_max, unsigned long chtype, int mval)
15250Sstevel@tonic-gate {
15260Sstevel@tonic-gate int i,ret=0;
15270Sstevel@tonic-gate MS_STATIC char buf[1024];
15280Sstevel@tonic-gate start:
15290Sstevel@tonic-gate if (!batch) BIO_printf(bio_err,"%s [%s]:",text,def);
15300Sstevel@tonic-gate (void)BIO_flush(bio_err);
15310Sstevel@tonic-gate if(value != NULL)
15320Sstevel@tonic-gate {
15330Sstevel@tonic-gate BUF_strlcpy(buf,value,sizeof buf);
15340Sstevel@tonic-gate BUF_strlcat(buf,"\n",sizeof buf);
15350Sstevel@tonic-gate BIO_printf(bio_err,"%s\n",value);
15360Sstevel@tonic-gate }
15370Sstevel@tonic-gate else
15380Sstevel@tonic-gate {
15390Sstevel@tonic-gate buf[0]='\0';
15400Sstevel@tonic-gate if (!batch)
15410Sstevel@tonic-gate {
15420Sstevel@tonic-gate fgets(buf,sizeof buf,stdin);
15430Sstevel@tonic-gate }
15440Sstevel@tonic-gate else
15450Sstevel@tonic-gate {
15460Sstevel@tonic-gate buf[0] = '\n';
15470Sstevel@tonic-gate buf[1] = '\0';
15480Sstevel@tonic-gate }
15490Sstevel@tonic-gate }
15500Sstevel@tonic-gate
15510Sstevel@tonic-gate if (buf[0] == '\0') return(0);
15520Sstevel@tonic-gate else if (buf[0] == '\n')
15530Sstevel@tonic-gate {
15540Sstevel@tonic-gate if ((def == NULL) || (def[0] == '\0'))
15550Sstevel@tonic-gate return(1);
15560Sstevel@tonic-gate BUF_strlcpy(buf,def,sizeof buf);
15570Sstevel@tonic-gate BUF_strlcat(buf,"\n",sizeof buf);
15580Sstevel@tonic-gate }
15590Sstevel@tonic-gate else if ((buf[0] == '.') && (buf[1] == '\n')) return(1);
15600Sstevel@tonic-gate
15610Sstevel@tonic-gate i=strlen(buf);
15620Sstevel@tonic-gate if (buf[i-1] != '\n')
15630Sstevel@tonic-gate {
15640Sstevel@tonic-gate BIO_printf(bio_err,"weird input :-(\n");
15650Sstevel@tonic-gate return(0);
15660Sstevel@tonic-gate }
15670Sstevel@tonic-gate buf[--i]='\0';
15680Sstevel@tonic-gate #ifdef CHARSET_EBCDIC
15690Sstevel@tonic-gate ebcdic2ascii(buf, buf, i);
15700Sstevel@tonic-gate #endif
15710Sstevel@tonic-gate if(!req_check_len(i, n_min, n_max)) goto start;
15720Sstevel@tonic-gate if (!X509_NAME_add_entry_by_NID(n,nid, chtype,
1573*2139Sjp161948 (unsigned char *) buf, -1,-1,mval)) goto err;
15740Sstevel@tonic-gate ret=1;
15750Sstevel@tonic-gate err:
15760Sstevel@tonic-gate return(ret);
15770Sstevel@tonic-gate }
15780Sstevel@tonic-gate
1579*2139Sjp161948 static int add_attribute_object(X509_REQ *req, char *text, const char *def,
1580*2139Sjp161948 char *value, int nid, int n_min,
15810Sstevel@tonic-gate int n_max, unsigned long chtype)
15820Sstevel@tonic-gate {
15830Sstevel@tonic-gate int i;
15840Sstevel@tonic-gate static char buf[1024];
15850Sstevel@tonic-gate
15860Sstevel@tonic-gate start:
15870Sstevel@tonic-gate if (!batch) BIO_printf(bio_err,"%s [%s]:",text,def);
15880Sstevel@tonic-gate (void)BIO_flush(bio_err);
15890Sstevel@tonic-gate if (value != NULL)
15900Sstevel@tonic-gate {
15910Sstevel@tonic-gate BUF_strlcpy(buf,value,sizeof buf);
15920Sstevel@tonic-gate BUF_strlcat(buf,"\n",sizeof buf);
15930Sstevel@tonic-gate BIO_printf(bio_err,"%s\n",value);
15940Sstevel@tonic-gate }
15950Sstevel@tonic-gate else
15960Sstevel@tonic-gate {
15970Sstevel@tonic-gate buf[0]='\0';
15980Sstevel@tonic-gate if (!batch)
15990Sstevel@tonic-gate {
16000Sstevel@tonic-gate fgets(buf,sizeof buf,stdin);
16010Sstevel@tonic-gate }
16020Sstevel@tonic-gate else
16030Sstevel@tonic-gate {
16040Sstevel@tonic-gate buf[0] = '\n';
16050Sstevel@tonic-gate buf[1] = '\0';
16060Sstevel@tonic-gate }
16070Sstevel@tonic-gate }
16080Sstevel@tonic-gate
16090Sstevel@tonic-gate if (buf[0] == '\0') return(0);
16100Sstevel@tonic-gate else if (buf[0] == '\n')
16110Sstevel@tonic-gate {
16120Sstevel@tonic-gate if ((def == NULL) || (def[0] == '\0'))
16130Sstevel@tonic-gate return(1);
16140Sstevel@tonic-gate BUF_strlcpy(buf,def,sizeof buf);
16150Sstevel@tonic-gate BUF_strlcat(buf,"\n",sizeof buf);
16160Sstevel@tonic-gate }
16170Sstevel@tonic-gate else if ((buf[0] == '.') && (buf[1] == '\n')) return(1);
16180Sstevel@tonic-gate
16190Sstevel@tonic-gate i=strlen(buf);
16200Sstevel@tonic-gate if (buf[i-1] != '\n')
16210Sstevel@tonic-gate {
16220Sstevel@tonic-gate BIO_printf(bio_err,"weird input :-(\n");
16230Sstevel@tonic-gate return(0);
16240Sstevel@tonic-gate }
16250Sstevel@tonic-gate buf[--i]='\0';
16260Sstevel@tonic-gate #ifdef CHARSET_EBCDIC
16270Sstevel@tonic-gate ebcdic2ascii(buf, buf, i);
16280Sstevel@tonic-gate #endif
16290Sstevel@tonic-gate if(!req_check_len(i, n_min, n_max)) goto start;
16300Sstevel@tonic-gate
16310Sstevel@tonic-gate if(!X509_REQ_add1_attr_by_NID(req, nid, chtype,
16320Sstevel@tonic-gate (unsigned char *)buf, -1)) {
16330Sstevel@tonic-gate BIO_printf(bio_err, "Error adding attribute\n");
16340Sstevel@tonic-gate ERR_print_errors(bio_err);
16350Sstevel@tonic-gate goto err;
16360Sstevel@tonic-gate }
16370Sstevel@tonic-gate
16380Sstevel@tonic-gate return(1);
16390Sstevel@tonic-gate err:
16400Sstevel@tonic-gate return(0);
16410Sstevel@tonic-gate }
16420Sstevel@tonic-gate
16430Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
1644*2139Sjp161948 static int MS_CALLBACK req_cb(int p, int n, BN_GENCB *cb)
16450Sstevel@tonic-gate {
16460Sstevel@tonic-gate char c='*';
16470Sstevel@tonic-gate
16480Sstevel@tonic-gate if (p == 0) c='.';
16490Sstevel@tonic-gate if (p == 1) c='+';
16500Sstevel@tonic-gate if (p == 2) c='*';
16510Sstevel@tonic-gate if (p == 3) c='\n';
1652*2139Sjp161948 BIO_write(cb->arg,&c,1);
1653*2139Sjp161948 (void)BIO_flush(cb->arg);
16540Sstevel@tonic-gate #ifdef LINT
16550Sstevel@tonic-gate p=n;
16560Sstevel@tonic-gate #endif
1657*2139Sjp161948 return 1;
16580Sstevel@tonic-gate }
16590Sstevel@tonic-gate #endif
16600Sstevel@tonic-gate
16610Sstevel@tonic-gate static int req_check_len(int len, int n_min, int n_max)
16620Sstevel@tonic-gate {
16630Sstevel@tonic-gate if ((n_min > 0) && (len < n_min))
16640Sstevel@tonic-gate {
16650Sstevel@tonic-gate BIO_printf(bio_err,"string is too short, it needs to be at least %d bytes long\n",n_min);
16660Sstevel@tonic-gate return(0);
16670Sstevel@tonic-gate }
16680Sstevel@tonic-gate if ((n_max >= 0) && (len > n_max))
16690Sstevel@tonic-gate {
16700Sstevel@tonic-gate BIO_printf(bio_err,"string is too long, it needs to be less than %d bytes long\n",n_max);
16710Sstevel@tonic-gate return(0);
16720Sstevel@tonic-gate }
16730Sstevel@tonic-gate return(1);
16740Sstevel@tonic-gate }
16750Sstevel@tonic-gate
16760Sstevel@tonic-gate /* Check if the end of a string matches 'end' */
1677*2139Sjp161948 static int check_end(const char *str, const char *end)
16780Sstevel@tonic-gate {
16790Sstevel@tonic-gate int elen, slen;
1680*2139Sjp161948 const char *tmp;
16810Sstevel@tonic-gate elen = strlen(end);
16820Sstevel@tonic-gate slen = strlen(str);
16830Sstevel@tonic-gate if(elen > slen) return 1;
16840Sstevel@tonic-gate tmp = str + slen - elen;
16850Sstevel@tonic-gate return strcmp(tmp, end);
16860Sstevel@tonic-gate }
1687