61 #ifdef OPENSSL_NO_DEPRECATED
62 #undef OPENSSL_NO_DEPRECATED
69 #ifdef OPENSSL_NO_STDIO
83 #ifndef OPENSSL_NO_RSA
86 #ifndef OPENSSL_NO_DSA
92 #define BITS "default_bits"
93 #define KEYFILE "default_keyfile"
94 #define PROMPT "prompt"
95 #define DISTINGUISHED_NAME "distinguished_name"
96 #define ATTRIBUTES "attributes"
97 #define V3_EXTENSIONS "x509_extensions"
98 #define REQ_EXTENSIONS "req_extensions"
99 #define STRING_MASK "string_mask"
100 #define UTF8_IN "utf8"
102 #define DEFAULT_KEY_LENGTH 512
103 #define MIN_KEY_LENGTH 384
106 #define PROG req_main
129 int attribs,
unsigned long chtype);
130 static int build_subject(
X509_REQ *req,
char *subj,
unsigned long chtype,
132 static int prompt_info(
X509_REQ *req,
135 unsigned long chtype);
138 unsigned long chtype);
139 static int add_attribute_object(
X509_REQ *req,
char *
text,
const char *def,
140 char *value,
int nid,
int n_min,
141 int n_max,
unsigned long chtype);
142 static int add_DN_object(
X509_NAME *n,
char *
text,
const char *def,
char *value,
143 int nid,
int n_min,
int n_max,
unsigned long chtype,
int mval);
145 static int req_check_len(
int len,
int n_min,
int n_max);
146 static int check_end(
const char *str,
const char *end);
147 static EVP_PKEY_CTX *set_keygen_ctx(
BIO *err,
const char *gstr,
int *pkey_type,
148 long *pkeylen,
char **palgnam,
151 static char *default_config_file=NULL;
153 static CONF *req_conf=NULL;
156 int MAIN(
int,
char **);
158 int MAIN(
int argc,
char **argv)
160 ENGINE *
e = NULL, *gen_eng = NULL;
161 unsigned long nmflag = 0, reqflag = 0;
162 int ex=1,x509=0,days=30;
166 const char *keyalg = NULL;
167 char *keyalgstr = NULL;
170 int i=0,badops=0,newreq=0,
verbose=0,pkey_type=-1;
173 int informat,outformat,verify=0,noout=0,
text=0,keyform=
FORMAT_PEM;
174 int nodes=0,kludge=0,newhdr=0,subject=0,pubkey=0;
175 char *infile,*outfile,*prog,*keyfile=NULL,*
template=NULL,*keyout=NULL;
176 #ifndef OPENSSL_NO_ENGINE
179 char *extensions = NULL;
180 char *req_exts = NULL;
185 char *passargin = NULL, *passargout = NULL;
186 char *passin = NULL, *passout = NULL;
190 const EVP_MD *md_alg=NULL,*digest=NULL;
198 #ifndef OPENSSL_NO_DES
217 if (strcmp(*argv,
"-inform") == 0)
219 if (--argc < 1)
goto bad;
222 else if (strcmp(*argv,
"-outform") == 0)
224 if (--argc < 1)
goto bad;
227 #ifndef OPENSSL_NO_ENGINE
228 else if (strcmp(*argv,
"-engine") == 0)
230 if (--argc < 1)
goto bad;
233 else if (strcmp(*argv,
"-keygen_engine") == 0)
235 if (--argc < 1)
goto bad;
244 else if (strcmp(*argv,
"-key") == 0)
246 if (--argc < 1)
goto bad;
249 else if (strcmp(*argv,
"-pubkey") == 0)
253 else if (strcmp(*argv,
"-new") == 0)
257 else if (strcmp(*argv,
"-config") == 0)
259 if (--argc < 1)
goto bad;
262 else if (strcmp(*argv,
"-keyform") == 0)
264 if (--argc < 1)
goto bad;
267 else if (strcmp(*argv,
"-in") == 0)
269 if (--argc < 1)
goto bad;
272 else if (strcmp(*argv,
"-out") == 0)
274 if (--argc < 1)
goto bad;
277 else if (strcmp(*argv,
"-keyout") == 0)
279 if (--argc < 1)
goto bad;
282 else if (strcmp(*argv,
"-passin") == 0)
284 if (--argc < 1)
goto bad;
285 passargin= *(++argv);
287 else if (strcmp(*argv,
"-passout") == 0)
289 if (--argc < 1)
goto bad;
290 passargout= *(++argv);
292 else if (strcmp(*argv,
"-rand") == 0)
294 if (--argc < 1)
goto bad;
297 else if (strcmp(*argv,
"-newkey") == 0)
304 else if (strcmp(*argv,
"-pkeyopt") == 0)
313 else if (strcmp(*argv,
"-sigopt") == 0)
322 else if (strcmp(*argv,
"-batch") == 0)
324 else if (strcmp(*argv,
"-newhdr") == 0)
326 else if (strcmp(*argv,
"-modulus") == 0)
328 else if (strcmp(*argv,
"-verify") == 0)
330 else if (strcmp(*argv,
"-nodes") == 0)
332 else if (strcmp(*argv,
"-noout") == 0)
334 else if (strcmp(*argv,
"-verbose") == 0)
336 else if (strcmp(*argv,
"-utf8") == 0)
338 else if (strcmp(*argv,
"-nameopt") == 0)
340 if (--argc < 1)
goto bad;
343 else if (strcmp(*argv,
"-reqopt") == 0)
345 if (--argc < 1)
goto bad;
348 else if (strcmp(*argv,
"-subject") == 0)
350 else if (strcmp(*argv,
"-text") == 0)
352 else if (strcmp(*argv,
"-x509") == 0)
354 else if (strcmp(*argv,
"-asn1-kludge") == 0)
356 else if (strcmp(*argv,
"-no-asn1-kludge") == 0)
358 else if (strcmp(*argv,
"-subj") == 0)
360 if (--argc < 1)
goto bad;
363 else if (strcmp(*argv,
"-multivalue-rdn") == 0)
365 else if (strcmp(*argv,
"-days") == 0)
367 if (--argc < 1)
goto bad;
368 days= atoi(*(++argv));
369 if (days == 0) days=30;
371 else if (strcmp(*argv,
"-set_serial") == 0)
373 if (--argc < 1)
goto bad;
375 if (!serial)
goto bad;
377 else if (strcmp(*argv,
"-extensions") == 0)
379 if (--argc < 1)
goto bad;
380 extensions = *(++argv);
382 else if (strcmp(*argv,
"-reqexts") == 0)
384 if (--argc < 1)
goto bad;
385 req_exts = *(++argv);
417 #ifndef OPENSSL_NO_ENGINE
428 BIO_printf(
bio_err,
" -newkey rsa:bits generate a new RSA key of 'bits' in size\n");
429 BIO_printf(
bio_err,
" -newkey dsa:file generate a new DSA key, parameters taken from CA in 'file'\n");
430 #ifndef OPENSSL_NO_ECDSA
431 BIO_printf(
bio_err,
" -newkey ec:file generate a new EC key, parameters taken from CA in 'file'\n");
433 BIO_printf(
bio_err,
" -[digest] Digest to sign with (md5, sha1, md2, mdc2, md4)\n");
440 BIO_printf(
bio_err,
" -days number of days a certificate generated by -x509 is valid for.\n");
441 BIO_printf(
bio_err,
" -set_serial serial number to use for a certificate generated by -x509.\n");
443 BIO_printf(
bio_err,
" -asn1-kludge Output the 'request' in a format that is wrong but some CA's\n");
445 BIO_printf(
bio_err,
" -extensions .. specify certificate extension section (override value in config file)\n");
446 BIO_printf(
bio_err,
" -reqexts .. specify request extension section (override value in config file)\n");
461 p=getenv(
"OPENSSL_CONF");
463 p=getenv(
"SSLEAY_CONF");
466 default_config_file=
p;
471 if (
template != NULL)
489 if (req_conf == NULL)
497 default_config_file);
500 if (req_conf != NULL)
553 "Error Loading extension section %s\n", extensions);
586 else if (!strcmp(p,
"yes"))
604 "Error Loading request extension section %s\n",
612 if ((in == NULL) || (out == NULL))
615 #ifndef OPENSSL_NO_ENGINE
632 if (randfile == NULL)
638 if (newreq && (pkey == NULL))
641 if (randfile == NULL)
649 genctx = set_keygen_ctx(
bio_err, keyalg, &pkey_type, &newkey,
650 &keyalgstr, gen_eng);
670 genctx = set_keygen_ctx(
bio_err, NULL, &pkey_type, &newkey,
671 &keyalgstr, gen_eng);
685 "parameter error \"%s\"\n",
721 #ifdef OPENSSL_SYS_VMS
746 if ((p != NULL) && (strcmp(p,
"no") == 0))
748 if (nodes) cipher=NULL;
753 NULL,0,NULL,passout))
787 req=PEM_read_bio_X509_REQ(in,NULL,NULL,NULL);
816 i=make_REQ(req,pkey,subj,multirdn,!x509, chtype);
833 if ((x509ss=X509_new()) == NULL)
goto end;
863 &ext_ctx, extensions, x509ss))
866 "Error Loading extension section %s\n",
889 &ext_ctx, req_exts, req))
892 "Error Loading extension section %s\n",
919 if (build_subject(req, subj, chtype, multirdn) == 0)
942 if (pkey == NULL)
goto end;
964 if (noout && !
text && !modulus && !subject && !pubkey)
973 #ifdef OPENSSL_SYS_VMS
982 if ((keyout != NULL) && (strcmp(outfile,keyout) == 0))
1003 PEM_write_bio_PUBKEY(out, tpubkey);
1031 if (tpubkey == NULL)
1033 fprintf(stdout,
"Modulus=unavailable\n");
1036 fprintf(stdout,
"Modulus=");
1037 #ifndef OPENSSL_NO_RSA
1042 fprintf(stdout,
"Wrong Algorithm type");
1044 fprintf(stdout,
"\n");
1047 if (!noout && !x509)
1052 if(newhdr) i=PEM_write_bio_X509_REQ_NEW(out,req);
1053 else i=PEM_write_bio_X509_REQ(out,req);
1064 if (!noout && x509 && (x509ss != NULL))
1069 i=PEM_write_bio_X509(out,x509ss);
1100 #ifndef OPENSSL_NO_ENGINE
1108 ASN1_INTEGER_free(serial);
1116 static int make_REQ(
X509_REQ *req,
EVP_PKEY *pkey,
char *subj,
int multirdn,
1117 int attribs,
unsigned long chtype)
1122 char *tmp, *dn_sect,*attr_sect;
1127 if((tmp != NULL) && !strcmp(tmp,
"no")) no_prompt = 1;
1130 if (dn_sect == NULL)
1136 dn_sk=NCONF_get_section(req_conf,dn_sect);
1144 if (attr_sect == NULL)
1151 attr_sk=NCONF_get_section(req_conf,attr_sect);
1152 if (attr_sk == NULL)
1163 i = auto_info(req, dn_sk, attr_sk, attribs, chtype);
1167 i = build_subject(req, subj, chtype, multirdn);
1169 i = prompt_info(req, dn_sk, dn_sect, attr_sk, attr_sect, attribs, chtype);
1184 static int build_subject(
X509_REQ *req,
char *subject,
unsigned long chtype,
int multirdn)
1188 if (!(n =
parse_name(subject, chtype, multirdn)))
1201 static int prompt_info(
X509_REQ *req,
1204 unsigned long chtype)
1219 BIO_printf(
bio_err,
"You are about to be asked to enter information that will be incorporated\n");
1221 BIO_printf(
bio_err,
"What you are about to enter is what is called a Distinguished Name or a DN.\n");
1222 BIO_printf(
bio_err,
"There are quite a few fields but you can leave some blank\n");
1240 if(!check_end(type,
"_min") || !check_end(type,
"_max") ||
1241 !check_end(type,
"_default") ||
1242 !check_end(type,
"_value"))
continue;
1246 for(p = v->
name; *p ; p++)
1247 if ((*p ==
':') || (*p ==
',') ||
1263 >= (
int)
sizeof(buf))
1296 if (!add_DN_object(subj,v->
value,def,value,nid,
1297 n_min,n_max, chtype, mval))
1318 if ((attr_sk == NULL) ||
1328 >= (
int)
sizeof(buf))
1364 if (!add_attribute_object(req,
1365 v->
value,def,value,nid,n_min,n_max, chtype))
1400 for(p = v->
name; *p ; p++)
1401 #ifndef CHARSET_EBCDIC
1402 if ((*p ==
':') || (*p ==
',') || (*p ==
'.')) {
1410 #ifndef CHARSET_EBCDIC
1422 (
unsigned char *) v->
value,-1,-1,mval))
return 0;
1437 (
unsigned char *)v->
value, -1))
return 0;
1444 static int add_DN_object(
X509_NAME *n,
char *
text,
const char *def,
char *value,
1445 int nid,
int n_min,
int n_max,
unsigned long chtype,
int mval)
1463 if (!fgets(buf,
sizeof buf,stdin))
1473 if (buf[0] ==
'\0')
return(0);
1474 else if (buf[0] ==
'\n')
1476 if ((def == NULL) || (def[0] ==
'\0'))
1481 else if ((buf[0] ==
'.') && (buf[1] ==
'\n'))
return(1);
1484 if (buf[i-1] !=
'\n')
1490 #ifdef CHARSET_EBCDIC
1493 if(!req_check_len(i, n_min, n_max))
goto start;
1495 (
unsigned char *) buf, -1,-1,mval))
goto err;
1501 static int add_attribute_object(
X509_REQ *req,
char *text,
const char *def,
1502 char *value,
int nid,
int n_min,
1503 int n_max,
unsigned long chtype)
1506 static char buf[1024];
1522 if (!fgets(buf,
sizeof buf,stdin))
1532 if (buf[0] ==
'\0')
return(0);
1533 else if (buf[0] ==
'\n')
1535 if ((def == NULL) || (def[0] ==
'\0'))
1540 else if ((buf[0] ==
'.') && (buf[1] ==
'\n'))
return(1);
1543 if (buf[i-1] !=
'\n')
1549 #ifdef CHARSET_EBCDIC
1552 if(!req_check_len(i, n_min, n_max))
goto start;
1555 (
unsigned char *)buf, -1)) {
1566 static int req_check_len(
int len,
int n_min,
int n_max)
1568 if ((n_min > 0) && (len < n_min))
1570 BIO_printf(
bio_err,
"string is too short, it needs to be at least %d bytes long\n",n_min);
1573 if ((n_max >= 0) && (len > n_max))
1575 BIO_printf(
bio_err,
"string is too long, it needs to be less than %d bytes long\n",n_max);
1582 static int check_end(
const char *str,
const char *end)
1588 if(elen > slen)
return 1;
1589 tmp = str + slen - elen;
1590 return strcmp(tmp, end);
1593 static EVP_PKEY_CTX *set_keygen_ctx(
BIO *err,
const char *gstr,
int *pkey_type,
1594 long *pkeylen,
char **palgnam,
1601 const char *paramfile = NULL;
1608 else if (gstr[0] >=
'0' && gstr[0] <=
'9')
1611 keylen = atol(gstr);
1614 else if (!strncmp(gstr,
"param:", 6))
1615 paramfile = gstr + 6;
1618 const char *p = strchr(gstr,
':');
1635 BIO_printf(err,
"Unknown algorithm %.*s\n", len, gstr);
1641 #ifndef OPENSSL_NO_ENGINE
1649 keylen = atol(p + 1);
1662 BIO_printf(err,
"Can't open parameter file %s\n",
1672 x = PEM_read_bio_X509(pbio, NULL, NULL, NULL);
1684 BIO_printf(err,
"Error reading parameter file %s\n",
1688 if (*pkey_type == -1)
1692 BIO_printf(err,
"Key Type does not match parameters\n");
1706 BIO_puts(err,
"Internal error: can't find key algorithm\n");
1711 #ifndef OPENSSL_NO_ENGINE
1728 BIO_puts(err,
"Error allocating keygen context\n");
1735 BIO_puts(err,
"Error initializing keygen context\n");
1739 #ifndef OPENSSL_NO_RSA
1744 BIO_puts(err,
"Error setting RSA keysize\n");
1786 BIO_printf(err,
"parameter error \"%s\"\n", sigopt);
1800 rv = do_sign_init(err, &mctx, pkey, md, sigopts);
1804 return rv > 0 ? 1 : 0;
1814 rv = do_sign_init(err, &mctx, pkey, md, sigopts);
1818 return rv > 0 ? 1 : 0;
1829 rv = do_sign_init(err, &mctx, pkey, md, sigopts);
1833 return rv > 0 ? 1 : 0;