- }
- else
- {
- BIO_printf(bio_err,"writing new private key to '%s'\n",keyout);
- if (BIO_write_filename(out,keyout) <= 0)
- {
- perror(keyout);
- goto end;
- }
- }
-
- p=NCONF_get_string(req_conf,SECTION,"encrypt_rsa_key");
- if (p == NULL)
- {
- ERR_clear_error();
- p=NCONF_get_string(req_conf,SECTION,"encrypt_key");
- if (p == NULL)
- ERR_clear_error();
- }
- if ((p != NULL) && (strcmp(p,"no") == 0))
- cipher=NULL;
- if (nodes) cipher=NULL;
-
- i=0;
-loop:
- if (!PEM_write_bio_PrivateKey(out,pkey,cipher,
- NULL,0,NULL,passout))
- {
- if ((ERR_GET_REASON(ERR_peek_error()) ==
- PEM_R_PROBLEMS_GETTING_PASSWORD) && (i < 3))
- {
- ERR_clear_error();
- i++;
- goto loop;
- }
- goto end;
- }
- BIO_printf(bio_err,"-----\n");
- }
-
- if (!newreq)
- {
- /* Since we are using a pre-existing certificate
- * request, the kludge 'format' info should not be
- * changed. */
- kludge= -1;
- if (infile == NULL)
- BIO_set_fp(in,stdin,BIO_NOCLOSE);
- else
- {
- if (BIO_read_filename(in,infile) <= 0)
- {
- perror(infile);
- goto end;
- }
- }
-
- if (informat == FORMAT_ASN1)
- req=d2i_X509_REQ_bio(in,NULL);
- else if (informat == FORMAT_PEM)
- req=PEM_read_bio_X509_REQ(in,NULL,NULL,NULL);
- else
- {
- BIO_printf(bio_err,"bad input format specified for X509 request\n");
- goto end;
- }
- if (req == NULL)
- {
- BIO_printf(bio_err,"unable to load X509 request\n");
- goto end;
- }
- }
-
- if (newreq || x509)
- {
- if (pkey == NULL)
- {
- BIO_printf(bio_err,"you need to specify a private key\n");
- goto end;
- }
-#ifndef OPENSSL_NO_DSA
- if (pkey->type == EVP_PKEY_DSA)
- digest=EVP_dss1();
-#endif
-#ifndef OPENSSL_NO_ECDSA
- if (pkey->type == EVP_PKEY_ECDSA)
- digest=EVP_ecdsa();
-#endif
- if (req == NULL)
- {
- req=X509_REQ_new();
- if (req == NULL)
- {
- goto end;
- }
-
- i=make_REQ(req,pkey,subj,!x509, chtype);
- subj=NULL; /* done processing '-subj' option */
- if ((kludge > 0) && !sk_X509_ATTRIBUTE_num(req->req_info->attributes))
- {
- sk_X509_ATTRIBUTE_free(req->req_info->attributes);
- req->req_info->attributes = NULL;
- }
- if (!i)
- {
- BIO_printf(bio_err,"problems making Certificate Request\n");
- goto end;
- }
- }
- if (x509)
- {
- EVP_PKEY *tmppkey;
- X509V3_CTX ext_ctx;
- if ((x509ss=X509_new()) == NULL) goto end;
-
- /* Set version to V3 */
- if(!X509_set_version(x509ss, 2)) goto end;
- if (serial)
- {
- if (!X509_set_serialNumber(x509ss, serial)) goto end;
- }
- else
- {
- if (!ASN1_INTEGER_set(X509_get_serialNumber(x509ss),0L)) goto end;
- }
-
- if (!X509_set_issuer_name(x509ss, X509_REQ_get_subject_name(req))) goto end;
- if (!X509_gmtime_adj(X509_get_notBefore(x509ss),0)) goto end;
- if (!X509_gmtime_adj(X509_get_notAfter(x509ss), (long)60*60*24*days)) goto end;
- if (!X509_set_subject_name(x509ss, X509_REQ_get_subject_name(req))) goto end;
- tmppkey = X509_REQ_get_pubkey(req);
- if (!tmppkey || !X509_set_pubkey(x509ss,tmppkey)) goto end;
- EVP_PKEY_free(tmppkey);
-
- /* Set up V3 context struct */
-
- X509V3_set_ctx(&ext_ctx, x509ss, x509ss, NULL, NULL, 0);
- X509V3_set_nconf(&ext_ctx, req_conf);
-
- /* Add extensions */
- if(extensions && !X509V3_EXT_add_nconf(req_conf,
- &ext_ctx, extensions, x509ss))
- {
- BIO_printf(bio_err,
- "Error Loading extension section %s\n",
- extensions);
- goto end;
- }
-
- if (!(i=X509_sign(x509ss,pkey,digest)))
- goto end;
- }
- else
- {
- X509V3_CTX ext_ctx;
-
- /* Set up V3 context struct */
-
- X509V3_set_ctx(&ext_ctx, NULL, NULL, req, NULL, 0);
- X509V3_set_nconf(&ext_ctx, req_conf);
-
- /* Add extensions */
- if(req_exts && !X509V3_EXT_REQ_add_nconf(req_conf,
- &ext_ctx, req_exts, req))
- {
- BIO_printf(bio_err,
- "Error Loading extension section %s\n",
- req_exts);
- goto end;
- }
- if (!(i=X509_REQ_sign(req,pkey,digest)))
- goto end;
- }
- }
-
- if (subj && x509)
- {
- BIO_printf(bio_err, "Cannot modifiy certificate subject\n");
- goto end;
- }
-
- if (subj && !x509)
- {
- if (verbose)
- {
- BIO_printf(bio_err, "Modifying Request's Subject\n");
- print_name(bio_err, "old subject=", X509_REQ_get_subject_name(req), nmflag);
- }
-
- if (build_subject(req, subj, chtype) == 0)
- {
- BIO_printf(bio_err, "ERROR: cannot modify subject\n");
- ex=1;
- goto end;
- }
-
- req->req_info->enc.modified = 1;
-
- if (verbose)
- {
- print_name(bio_err, "new subject=", X509_REQ_get_subject_name(req), nmflag);
- }
- }
-
- if (verify && !x509)
- {
- int tmp=0;
-
- if (pkey == NULL)
- {
- pkey=X509_REQ_get_pubkey(req);
- tmp=1;
- if (pkey == NULL) goto end;
- }
-
- i=X509_REQ_verify(req,pkey);
- if (tmp) {
- EVP_PKEY_free(pkey);
- pkey=NULL;
- }
-
- if (i < 0)
- {
- goto end;
- }
- else if (i == 0)
- {
- BIO_printf(bio_err,"verify failure\n");
- ERR_print_errors(bio_err);
- }
- else /* if (i > 0) */
- BIO_printf(bio_err,"verify OK\n");
- }
-
- if (noout && !text && !modulus && !subject && !pubkey)
- {
- ex=0;
- goto end;
- }
-
- if (outfile == NULL)
- {
- BIO_set_fp(out,stdout,BIO_NOCLOSE);
-#ifdef OPENSSL_SYS_VMS
- {
- BIO *tmpbio = BIO_new(BIO_f_linebuffer());
- out = BIO_push(tmpbio, out);
- }
-#endif
- }
- else
- {
- if ((keyout != NULL) && (strcmp(outfile,keyout) == 0))
- i=(int)BIO_append_filename(out,outfile);
- else
- i=(int)BIO_write_filename(out,outfile);
- if (!i)
- {
- perror(outfile);
- goto end;
- }
- }
-
- if (pubkey)
- {
- EVP_PKEY *tpubkey;
- tpubkey=X509_REQ_get_pubkey(req);
- if (tpubkey == NULL)
- {
- BIO_printf(bio_err,"Error getting public key\n");
- ERR_print_errors(bio_err);
- goto end;
- }
- PEM_write_bio_PUBKEY(out, tpubkey);
- EVP_PKEY_free(tpubkey);
- }
-
- if (text)
- {
- if (x509)
- X509_print(out,x509ss);
- else
- X509_REQ_print(out,req);
- }
-
- if(subject)
- {
- if(x509)
- print_name(out, "subject=", X509_get_subject_name(x509ss), nmflag);
- else
- print_name(out, "subject=", X509_REQ_get_subject_name(req), nmflag);
- }
-
- if (modulus)
- {
- EVP_PKEY *tpubkey;
-
- if (x509)
- tpubkey=X509_get_pubkey(x509ss);
- else
- tpubkey=X509_REQ_get_pubkey(req);
- if (tpubkey == NULL)
- {
- fprintf(stdout,"Modulus=unavailable\n");
- goto end;
- }
- fprintf(stdout,"Modulus=");
+ break;
+ case OPT_KEY:
+ keyfile = opt_arg();
+ break;
+ case OPT_PUBKEY:
+ pubkey = 1;
+ break;
+ case OPT_NEW:
+ newreq = 1;
+ break;
+ case OPT_CONFIG:
+ template = opt_arg();
+ break;
+ case OPT_KEYFORM:
+ if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &keyform))
+ goto opthelp;
+ break;
+ case OPT_IN:
+ infile = opt_arg();
+ break;
+ case OPT_OUT:
+ outfile = opt_arg();
+ break;
+ case OPT_KEYOUT:
+ keyout = opt_arg();
+ break;
+ case OPT_PASSIN:
+ passargin = opt_arg();
+ break;
+ case OPT_PASSOUT:
+ passargout = opt_arg();
+ break;
+ case OPT_RAND:
+ inrand = opt_arg();
+ break;
+ case OPT_NEWKEY:
+ keyalg = opt_arg();
+ newreq = 1;
+ break;
+ case OPT_PKEYOPT:
+ if (!pkeyopts)
+ pkeyopts = sk_OPENSSL_STRING_new_null();
+ if (!pkeyopts || !sk_OPENSSL_STRING_push(pkeyopts, opt_arg()))
+ goto opthelp;
+ break;
+ case OPT_SIGOPT:
+ if (!sigopts)
+ sigopts = sk_OPENSSL_STRING_new_null();
+ if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))
+ goto opthelp;
+ break;
+ case OPT_BATCH:
+ batch = 1;
+ break;
+ case OPT_NEWHDR:
+ newhdr = 1;
+ break;
+ case OPT_MODULUS:
+ modulus = 1;
+ break;
+ case OPT_VERIFY:
+ verify = 1;
+ break;
+ case OPT_NODES:
+ nodes = 1;
+ break;
+ case OPT_NOOUT:
+ noout = 1;
+ break;
+ case OPT_VERBOSE:
+ verbose = 1;
+ break;
+ case OPT_UTF8:
+ chtype = MBSTRING_UTF8;
+ break;
+ case OPT_NAMEOPT:
+ nmflag_set = 1;
+ if (!set_name_ex(&nmflag, opt_arg()))
+ goto opthelp;
+ break;
+ case OPT_REQOPT:
+ if (!set_cert_ex(&reqflag, opt_arg()))
+ goto opthelp;
+ break;
+ case OPT_TEXT:
+ text = 1;
+ break;
+ case OPT_X509:
+ x509 = 1;
+ break;
+ case OPT_DAYS:
+ days = atoi(opt_arg());
+ break;
+ case OPT_SET_SERIAL:
+ serial = s2i_ASN1_INTEGER(NULL, opt_arg());
+ if (serial == NULL)
+ goto opthelp;
+ break;
+ case OPT_SUBJECT:
+ subject = 1;
+ break;
+ case OPT_SUBJ:
+ subj = opt_arg();
+ break;
+ case OPT_MULTIVALUE_RDN:
+ multirdn = 1;
+ break;
+ case OPT_EXTENSIONS:
+ extensions = opt_arg();
+ break;
+ case OPT_REQEXTS:
+ req_exts = opt_arg();
+ break;
+ case OPT_MD:
+ if (!opt_md(opt_unknown(), &md_alg))
+ goto opthelp;
+ digest = md_alg;
+ break;
+ }
+ }
+ argc = opt_num_rest();
+ argv = opt_rest();
+
+ if (!nmflag_set)
+ nmflag = XN_FLAG_ONELINE;
+
+ private = newreq && (pkey == NULL) ? 1 : 0;
+
+ if (!app_passwd(passargin, passargout, &passin, &passout)) {
+ BIO_printf(bio_err, "Error getting passwords\n");
+ goto end;
+ }
+
+ if (verbose)
+ BIO_printf(bio_err, "Using configuration from %s\n", template);
+ req_conf = app_load_config(template);
+ if (!app_load_modules(req_conf))
+ goto end;
+
+ if (req_conf != NULL) {
+ p = NCONF_get_string(req_conf, NULL, "oid_file");
+ if (p == NULL)
+ ERR_clear_error();
+ if (p != NULL) {
+ BIO *oid_bio;
+
+ oid_bio = BIO_new_file(p, "r");
+ if (oid_bio == NULL) {
+ /*-
+ BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
+ ERR_print_errors(bio_err);
+ */
+ } else {
+ OBJ_create_objects(oid_bio);
+ BIO_free(oid_bio);
+ }
+ }
+ }
+ if (!add_oid_section(req_conf))
+ goto end;
+
+ if (md_alg == NULL) {
+ p = NCONF_get_string(req_conf, SECTION, "default_md");
+ if (p == NULL)
+ ERR_clear_error();
+ else {
+ if (!opt_md(p, &md_alg))
+ goto opthelp;
+ digest = md_alg;
+ }
+ }
+
+ if (!extensions) {
+ extensions = NCONF_get_string(req_conf, SECTION, V3_EXTENSIONS);
+ if (!extensions)
+ ERR_clear_error();
+ }
+ if (extensions) {
+ /* Check syntax of file */
+ X509V3_CTX ctx;
+ X509V3_set_ctx_test(&ctx);
+ X509V3_set_nconf(&ctx, req_conf);
+ if (!X509V3_EXT_add_nconf(req_conf, &ctx, extensions, NULL)) {
+ BIO_printf(bio_err,
+ "Error Loading extension section %s\n", extensions);
+ goto end;
+ }
+ }
+
+ if (!passin) {
+ passin = NCONF_get_string(req_conf, SECTION, "input_password");
+ if (!passin)
+ ERR_clear_error();
+ }
+
+ if (!passout) {
+ passout = NCONF_get_string(req_conf, SECTION, "output_password");
+ if (!passout)
+ ERR_clear_error();
+ }
+
+ p = NCONF_get_string(req_conf, SECTION, STRING_MASK);
+ if (!p)
+ ERR_clear_error();
+
+ if (p && !ASN1_STRING_set_default_mask_asc(p)) {
+ BIO_printf(bio_err, "Invalid global string mask setting %s\n", p);
+ goto end;
+ }
+
+ if (chtype != MBSTRING_UTF8) {
+ p = NCONF_get_string(req_conf, SECTION, UTF8_IN);
+ if (!p)
+ ERR_clear_error();
+ else if (strcmp(p, "yes") == 0)
+ chtype = MBSTRING_UTF8;
+ }
+
+ if (!req_exts) {
+ req_exts = NCONF_get_string(req_conf, SECTION, REQ_EXTENSIONS);
+ if (!req_exts)
+ ERR_clear_error();
+ }
+ if (req_exts) {
+ /* Check syntax of file */
+ X509V3_CTX ctx;
+ X509V3_set_ctx_test(&ctx);
+ X509V3_set_nconf(&ctx, req_conf);
+ if (!X509V3_EXT_add_nconf(req_conf, &ctx, req_exts, NULL)) {
+ BIO_printf(bio_err,
+ "Error Loading request extension section %s\n",
+ req_exts);
+ goto end;
+ }
+ }
+
+ if (keyfile != NULL) {
+ pkey = load_key(keyfile, keyform, 0, passin, e, "Private Key");
+ if (!pkey) {
+ /* load_key() has already printed an appropriate message */
+ goto end;
+ } else {
+ char *randfile = NCONF_get_string(req_conf, SECTION, "RANDFILE");
+ if (randfile == NULL)
+ ERR_clear_error();
+ app_RAND_load_file(randfile, 0);
+ }
+ }
+
+ if (newreq && (pkey == NULL)) {
+ char *randfile = NCONF_get_string(req_conf, SECTION, "RANDFILE");
+ if (randfile == NULL)
+ ERR_clear_error();
+ app_RAND_load_file(randfile, 0);
+ if (inrand)
+ app_RAND_load_files(inrand);
+
+ if (!NCONF_get_number(req_conf, SECTION, BITS, &newkey)) {
+ newkey = DEFAULT_KEY_LENGTH;
+ }
+
+ if (keyalg) {
+ genctx = set_keygen_ctx(keyalg, &pkey_type, &newkey,
+ &keyalgstr, gen_eng);
+ if (!genctx)
+ goto end;
+ }
+
+ if (newkey < MIN_KEY_LENGTH
+ && (pkey_type == EVP_PKEY_RSA || pkey_type == EVP_PKEY_DSA)) {
+ BIO_printf(bio_err, "private key length is too short,\n");
+ BIO_printf(bio_err, "it needs to be at least %d bits, not %ld\n",
+ MIN_KEY_LENGTH, newkey);
+ goto end;
+ }
+
+ if (!genctx) {
+ genctx = set_keygen_ctx(NULL, &pkey_type, &newkey,
+ &keyalgstr, gen_eng);
+ if (!genctx)
+ goto end;
+ }
+
+ if (pkeyopts) {
+ char *genopt;
+ for (i = 0; i < sk_OPENSSL_STRING_num(pkeyopts); i++) {
+ genopt = sk_OPENSSL_STRING_value(pkeyopts, i);
+ if (pkey_ctrl_string(genctx, genopt) <= 0) {
+ BIO_printf(bio_err, "parameter error \"%s\"\n", genopt);
+ ERR_print_errors(bio_err);
+ goto end;
+ }
+ }
+ }
+
+ BIO_printf(bio_err, "Generating a %ld bit %s private key\n",
+ newkey, keyalgstr);
+
+ EVP_PKEY_CTX_set_cb(genctx, genpkey_cb);
+ EVP_PKEY_CTX_set_app_data(genctx, bio_err);
+
+ if (EVP_PKEY_keygen(genctx, &pkey) <= 0) {
+ BIO_puts(bio_err, "Error Generating Key\n");
+ goto end;
+ }
+
+ EVP_PKEY_CTX_free(genctx);
+ genctx = NULL;
+
+ app_RAND_write_file(randfile);
+
+ if (keyout == NULL) {
+ keyout = NCONF_get_string(req_conf, SECTION, KEYFILE);
+ if (keyout == NULL)
+ ERR_clear_error();
+ }
+
+ if (keyout == NULL)
+ BIO_printf(bio_err, "writing new private key to stdout\n");
+ else
+ BIO_printf(bio_err, "writing new private key to '%s'\n", keyout);
+ out = bio_open_owner(keyout, outformat, private);
+ if (out == NULL)
+ goto end;
+
+ p = NCONF_get_string(req_conf, SECTION, "encrypt_rsa_key");
+ if (p == NULL) {
+ ERR_clear_error();
+ p = NCONF_get_string(req_conf, SECTION, "encrypt_key");
+ if (p == NULL)
+ ERR_clear_error();
+ }
+ if ((p != NULL) && (strcmp(p, "no") == 0))
+ cipher = NULL;
+ if (nodes)
+ cipher = NULL;
+
+ i = 0;
+ loop:
+ assert(private);
+ if (!PEM_write_bio_PrivateKey(out, pkey, cipher,
+ NULL, 0, NULL, passout)) {
+ if ((ERR_GET_REASON(ERR_peek_error()) ==
+ PEM_R_PROBLEMS_GETTING_PASSWORD) && (i < 3)) {
+ ERR_clear_error();
+ i++;
+ goto loop;
+ }
+ goto end;
+ }
+ BIO_free(out);
+ out = NULL;
+ BIO_printf(bio_err, "-----\n");
+ }
+
+ if (!newreq) {
+ in = bio_open_default(infile, 'r', informat);
+ if (in == NULL)
+ goto end;
+
+ if (informat == FORMAT_ASN1)
+ req = d2i_X509_REQ_bio(in, NULL);
+ else
+ req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL);
+ if (req == NULL) {
+ BIO_printf(bio_err, "unable to load X509 request\n");
+ goto end;
+ }
+ }
+
+ if (newreq || x509) {
+ if (pkey == NULL) {
+ BIO_printf(bio_err, "you need to specify a private key\n");
+ goto end;
+ }
+
+ if (req == NULL) {
+ req = X509_REQ_new();
+ if (req == NULL) {
+ goto end;
+ }
+
+ i = make_REQ(req, pkey, subj, multirdn, !x509, chtype);
+ subj = NULL; /* done processing '-subj' option */
+ if (!i) {
+ BIO_printf(bio_err, "problems making Certificate Request\n");
+ goto end;
+ }
+ }
+ if (x509) {
+ EVP_PKEY *tmppkey;
+ X509V3_CTX ext_ctx;
+ if ((x509ss = X509_new()) == NULL)
+ goto end;
+
+ /* Set version to V3 */
+ if (extensions && !X509_set_version(x509ss, 2))
+ goto end;
+ if (serial) {
+ if (!X509_set_serialNumber(x509ss, serial))
+ goto end;
+ } else {
+ if (!rand_serial(NULL, X509_get_serialNumber(x509ss)))
+ goto end;
+ }
+
+ if (!X509_set_issuer_name(x509ss, X509_REQ_get_subject_name(req)))
+ goto end;
+ if (!X509_gmtime_adj(X509_get_notBefore(x509ss), 0))
+ goto end;
+ if (!X509_time_adj_ex(X509_get_notAfter(x509ss), days, 0, NULL))
+ goto end;
+ if (!X509_set_subject_name
+ (x509ss, X509_REQ_get_subject_name(req)))
+ goto end;
+ tmppkey = X509_REQ_get_pubkey(req);
+ if (!tmppkey || !X509_set_pubkey(x509ss, tmppkey))
+ goto end;
+ EVP_PKEY_free(tmppkey);
+
+ /* Set up V3 context struct */
+
+ X509V3_set_ctx(&ext_ctx, x509ss, x509ss, NULL, NULL, 0);
+ X509V3_set_nconf(&ext_ctx, req_conf);
+
+ /* Add extensions */
+ if (extensions && !X509V3_EXT_add_nconf(req_conf,
+ &ext_ctx, extensions,
+ x509ss)) {
+ BIO_printf(bio_err, "Error Loading extension section %s\n",
+ extensions);
+ goto end;
+ }
+
+ i = do_X509_sign(x509ss, pkey, digest, sigopts);
+ if (!i) {
+ ERR_print_errors(bio_err);
+ goto end;
+ }
+ } else {
+ X509V3_CTX ext_ctx;
+
+ /* Set up V3 context struct */
+
+ X509V3_set_ctx(&ext_ctx, NULL, NULL, req, NULL, 0);
+ X509V3_set_nconf(&ext_ctx, req_conf);
+
+ /* Add extensions */
+ if (req_exts && !X509V3_EXT_REQ_add_nconf(req_conf,
+ &ext_ctx, req_exts,
+ req)) {
+ BIO_printf(bio_err, "Error Loading extension section %s\n",
+ req_exts);
+ goto end;
+ }
+ i = do_X509_REQ_sign(req, pkey, digest, sigopts);
+ if (!i) {
+ ERR_print_errors(bio_err);
+ goto end;
+ }
+ }
+ }
+
+ if (subj && x509) {
+ BIO_printf(bio_err, "Cannot modify certificate subject\n");
+ goto end;
+ }
+
+ if (subj && !x509) {
+ if (verbose) {
+ BIO_printf(bio_err, "Modifying Request's Subject\n");
+ print_name(bio_err, "old subject=",
+ X509_REQ_get_subject_name(req), nmflag);
+ }
+
+ if (build_subject(req, subj, chtype, multirdn) == 0) {
+ BIO_printf(bio_err, "ERROR: cannot modify subject\n");
+ ret = 1;
+ goto end;
+ }
+
+ if (verbose) {
+ print_name(bio_err, "new subject=",
+ X509_REQ_get_subject_name(req), nmflag);
+ }
+ }
+
+ if (verify && !x509) {
+ int tmp = 0;
+
+ if (pkey == NULL) {
+ pkey = X509_REQ_get_pubkey(req);
+ tmp = 1;
+ if (pkey == NULL)
+ goto end;
+ }
+
+ i = X509_REQ_verify(req, pkey);
+ if (tmp) {
+ EVP_PKEY_free(pkey);
+ pkey = NULL;
+ }
+
+ if (i < 0) {
+ goto end;
+ } else if (i == 0) {
+ BIO_printf(bio_err, "verify failure\n");
+ ERR_print_errors(bio_err);
+ } else /* if (i > 0) */
+ BIO_printf(bio_err, "verify OK\n");
+ }
+
+ if (noout && !text && !modulus && !subject && !pubkey) {
+ ret = 0;
+ goto end;
+ }
+
+ out = bio_open_default(outfile,
+ keyout != NULL && outfile != NULL &&
+ strcmp(keyout, outfile) == 0 ? 'a' : 'w',
+ outformat);
+ if (out == NULL)
+ goto end;
+
+ if (pubkey) {
+ EVP_PKEY *tpubkey;
+ tpubkey = X509_REQ_get_pubkey(req);
+ if (tpubkey == NULL) {
+ BIO_printf(bio_err, "Error getting public key\n");
+ ERR_print_errors(bio_err);
+ goto end;
+ }
+ PEM_write_bio_PUBKEY(out, tpubkey);
+ EVP_PKEY_free(tpubkey);
+ }
+
+ if (text) {
+ if (x509)
+ X509_print_ex(out, x509ss, nmflag, reqflag);
+ else
+ X509_REQ_print_ex(out, req, nmflag, reqflag);
+ }
+
+ if (subject) {
+ if (x509)
+ print_name(out, "subject=", X509_get_subject_name(x509ss),
+ nmflag);
+ else
+ print_name(out, "subject=", X509_REQ_get_subject_name(req),
+ nmflag);
+ }
+
+ if (modulus) {
+ EVP_PKEY *tpubkey;
+
+ if (x509)
+ tpubkey = X509_get_pubkey(x509ss);
+ else
+ tpubkey = X509_REQ_get_pubkey(req);
+ if (tpubkey == NULL) {
+ fprintf(stdout, "Modulus=unavailable\n");
+ goto end;
+ }
+ fprintf(stdout, "Modulus=");