/*
- * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the OpenSSL license (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
"Cert extension section (override value in config file)"},
{"reqexts", OPT_REQEXTS, 's',
"Request extension section (override value in config file)"},
- {"precert", OPT_PRECERT, '-', "Add a poison extension"},
+ {"precert", OPT_PRECERT, '-', "Add a poison extension (implies -new)"},
{"", OPT_MD, '-', "Any supported digest"},
#ifndef OPENSSL_NO_ENGINE
{"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
int pkey_type = -1, private = 0;
int informat = FORMAT_PEM, outformat = FORMAT_PEM, keyform = FORMAT_PEM;
int modulus = 0, multirdn = 0, verify = 0, noout = 0, text = 0;
- int nodes = 0, newhdr = 0, subject = 0, pubkey = 0;
- int precert = 0;
+ int nodes = 0, newhdr = 0, subject = 0, pubkey = 0, precert = 0;
long newkey = -1;
- unsigned long chtype = MBSTRING_ASC, nmflag = 0, reqflag = 0;
- char nmflag_set = 0;
+ unsigned long chtype = MBSTRING_ASC, reqflag = 0;
#ifndef OPENSSL_NO_DES
cipher = EVP_des_ede3_cbc();
chtype = MBSTRING_UTF8;
break;
case OPT_NAMEOPT:
- nmflag_set = 1;
- if (!set_name_ex(&nmflag, opt_arg()))
+ if (!set_nameopt(opt_arg()))
goto opthelp;
break;
case OPT_REQOPT:
break;
case OPT_X509:
x509 = 1;
- newreq = 1;
break;
case OPT_DAYS:
days = atoi(opt_arg());
req_exts = opt_arg();
break;
case OPT_PRECERT:
- precert = 1;
+ newreq = precert = 1;
break;
case OPT_MD:
if (!opt_md(opt_unknown(), &md_alg))
if (argc != 0)
goto opthelp;
- if (!nmflag_set)
- nmflag = XN_FLAG_ONELINE;
+ if (x509 && infile == NULL)
+ newreq = 1;
/* TODO: simplify this as pkey is still always NULL here */
private = newreq && (pkey == NULL) ? 1 : 0;
if (md_alg == NULL) {
p = NCONF_get_string(req_conf, SECTION, "default_md");
- if (p == NULL)
+ if (p == NULL) {
ERR_clear_error();
- else {
+ } else {
if (!opt_md(p, &md_alg))
goto opthelp;
digest = md_alg;
}
}
- if (!extensions) {
+ if (extensions == NULL) {
extensions = NCONF_get_string(req_conf, SECTION, V3_EXTENSIONS);
- if (!extensions)
+ if (extensions == NULL)
ERR_clear_error();
}
- if (extensions) {
+ if (extensions != NULL) {
/* Check syntax of file */
X509V3_CTX ctx;
X509V3_set_ctx_test(&ctx);
}
p = NCONF_get_string(req_conf, SECTION, STRING_MASK);
- if (!p)
+ if (p == NULL)
ERR_clear_error();
- if (p && !ASN1_STRING_set_default_mask_asc(p)) {
+ if (p != NULL && !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)
+ if (p == NULL)
ERR_clear_error();
else if (strcmp(p, "yes") == 0)
chtype = MBSTRING_UTF8;
}
- if (!req_exts) {
+ if (req_exts == NULL) {
req_exts = NCONF_get_string(req_conf, SECTION, REQ_EXTENSIONS);
- if (!req_exts)
+ if (req_exts == NULL)
ERR_clear_error();
}
- if (req_exts) {
+ if (req_exts != NULL) {
/* Check syntax of file */
X509V3_CTX ctx;
X509V3_set_ctx_test(&ctx);
if (keyfile != NULL) {
pkey = load_key(keyfile, keyform, 0, passin, e, "Private Key");
- if (!pkey) {
+ if (pkey == NULL) {
/* load_key() has already printed an appropriate message */
goto end;
} else {
if (randfile == NULL)
ERR_clear_error();
app_RAND_load_file(randfile, 0);
- if (inrand)
+ if (inrand != NULL)
app_RAND_load_files(inrand);
if (!NCONF_get_number(req_conf, SECTION, BITS, &newkey)) {
newkey = DEFAULT_KEY_LENGTH;
}
- if (keyalg) {
+ if (keyalg != NULL) {
genctx = set_keygen_ctx(keyalg, &pkey_type, &newkey,
&keyalgstr, gen_eng);
- if (!genctx)
+ if (genctx == NULL)
goto end;
}
goto end;
}
- if (!genctx) {
+ if (genctx == NULL) {
genctx = set_keygen_ctx(NULL, &pkey_type, &newkey,
&keyalgstr, gen_eng);
if (!genctx)
goto end;
}
- if (pkeyopts) {
+ if (pkeyopts != NULL) {
char *genopt;
for (i = 0; i < sk_OPENSSL_STRING_num(pkeyopts); i++) {
genopt = sk_OPENSSL_STRING_value(pkeyopts, i);
}
}
- if (newreq) {
+ if (newreq || x509) {
if (pkey == NULL) {
BIO_printf(bio_err, "you need to specify a private key\n");
goto end;
goto end;
/* Set version to V3 */
- if (extensions && !X509_set_version(x509ss, 2))
+ if (extensions != NULL && !X509_set_version(x509ss, 2))
goto end;
- if (serial) {
+ if (serial != NULL) {
if (!X509_set_serialNumber(x509ss, serial))
goto end;
} else {
X509V3_set_nconf(&ext_ctx, req_conf);
/* Add extensions */
- if (extensions && !X509V3_EXT_add_nconf(req_conf,
- &ext_ctx, extensions,
- x509ss)) {
+ if (extensions != NULL && !X509V3_EXT_add_nconf(req_conf,
+ &ext_ctx, extensions,
+ x509ss)) {
BIO_printf(bio_err, "Error Loading extension section %s\n",
extensions);
goto end;
X509V3_set_nconf(&ext_ctx, req_conf);
/* Add extensions */
- if (req_exts && !X509V3_EXT_REQ_add_nconf(req_conf,
- &ext_ctx, req_exts,
- req)) {
+ if (req_exts != NULL
+ && !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 (verbose) {
BIO_printf(bio_err, "Modifying Request's Subject\n");
print_name(bio_err, "old subject=",
- X509_REQ_get_subject_name(req), nmflag);
+ X509_REQ_get_subject_name(req), get_nameopt());
}
if (build_subject(req, subj, chtype, multirdn) == 0) {
if (verbose) {
print_name(bio_err, "new subject=",
- X509_REQ_get_subject_name(req), nmflag);
+ X509_REQ_get_subject_name(req), get_nameopt());
}
}
} else if (i == 0) {
BIO_printf(bio_err, "verify failure\n");
ERR_print_errors(bio_err);
- } else /* if (i > 0) */
+ } else { /* if (i > 0) */
BIO_printf(bio_err, "verify OK\n");
+ }
}
if (noout && !text && !modulus && !subject && !pubkey) {
if (text) {
if (x509)
- X509_print_ex(out, x509ss, nmflag, reqflag);
+ X509_print_ex(out, x509ss, get_nameopt(), reqflag);
else
- X509_REQ_print_ex(out, req, nmflag, reqflag);
+ X509_REQ_print_ex(out, req, get_nameopt(), reqflag);
}
if (subject) {
if (x509)
print_name(out, "subject=", X509_get_subject_name(x509ss),
- nmflag);
+ get_nameopt());
else
print_name(out, "subject=", X509_REQ_get_subject_name(req),
- nmflag);
+ get_nameopt());
}
if (modulus) {
if (sk_CONF_VALUE_num(dn_sk)) {
i = -1;
- start:for (;;) {
+ start:
+ for ( ; ; ) {
i++;
if (sk_CONF_VALUE_num(dn_sk) <= i)
break;
if (*type == '+') {
mval = -1;
type++;
- } else
+ } else {
mval = 0;
+ }
/* If OBJ not recognised ignore it */
if ((nid = OBJ_txt2nid(type)) == NID_undef)
goto start;
}
i = -1;
- start2: for (;;) {
+ start2:
+ for ( ; ; ) {
i++;
if ((attr_sk == NULL) || (sk_CONF_VALUE_num(attr_sk) <= i))
break;
if (plus_char) {
type++;
mval = -1;
- } else
+ } else {
mval = 0;
+ }
if (!X509_NAME_add_entry_by_txt(subj, type, chtype,
(unsigned char *)v->value, -1, -1,
mval))
}
if (buf[0] == '\0')
- return (0);
- else if (buf[0] == '\n') {
+ return 0;
+ if (buf[0] == '\n') {
if ((def == NULL) || (def[0] == '\0'))
- return (1);
+ return 1;
OPENSSL_strlcpy(buf, def, sizeof buf);
OPENSSL_strlcat(buf, "\n", sizeof buf);
- } else if ((buf[0] == '.') && (buf[1] == '\n'))
- return (1);
+ } else if ((buf[0] == '.') && (buf[1] == '\n')) {
+ return 1;
+ }
i = strlen(buf);
if (buf[i - 1] != '\n') {
BIO_printf(bio_err, "weird input :-(\n");
- return (0);
+ return 0;
}
buf[--i] = '\0';
#ifdef CHARSET_EBCDIC
}
if (buf[0] == '\0')
- return (0);
- else if (buf[0] == '\n') {
+ return 0;
+ if (buf[0] == '\n') {
if ((def == NULL) || (def[0] == '\0'))
- return (1);
+ return 1;
OPENSSL_strlcpy(buf, def, sizeof buf);
OPENSSL_strlcat(buf, "\n", sizeof buf);
- } else if ((buf[0] == '.') && (buf[1] == '\n'))
- return (1);
+ } else if ((buf[0] == '.') && (buf[1] == '\n')) {
+ return 1;
+ }
i = strlen(buf);
if (buf[i - 1] != '\n') {
BIO_printf(bio_err, "weird input :-(\n");
- return (0);
+ return 0;
}
buf[--i] = '\0';
#ifdef CHARSET_EBCDIC
*pkey_type = EVP_PKEY_RSA;
keylen = atol(gstr);
*pkeylen = keylen;
- } else if (strncmp(gstr, "param:", 6) == 0)
+ } else if (strncmp(gstr, "param:", 6) == 0) {
paramfile = gstr + 6;
- else {
+ } else {
const char *p = strchr(gstr, ':');
int len;
ENGINE *tmpeng;
const EVP_PKEY_ASN1_METHOD *ameth;
- if (p)
+ if (p != NULL)
len = p - gstr;
else
len = strlen(gstr);
ameth = EVP_PKEY_asn1_find_str(&tmpeng, gstr, len);
- if (!ameth) {
+ if (ameth == NULL) {
BIO_printf(bio_err, "Unknown algorithm %.*s\n", len, gstr);
return NULL;
}
ENGINE_finish(tmpeng);
#endif
if (*pkey_type == EVP_PKEY_RSA) {
- if (p) {
+ if (p != NULL) {
keylen = atol(p + 1);
*pkeylen = keylen;
- } else
+ } else {
keylen = *pkeylen;
- } else if (p)
+ }
+ } else if (p != NULL) {
paramfile = p + 1;
+ }
}
- if (paramfile) {
+ if (paramfile != NULL) {
pbio = BIO_new_file(paramfile, "r");
- if (!pbio) {
+ if (pbio == NULL) {
BIO_printf(bio_err, "Can't open parameter file %s\n", paramfile);
return NULL;
}
param = PEM_read_bio_Parameters(pbio, NULL);
- if (!param) {
+ if (param == NULL) {
X509 *x;
+
(void)BIO_reset(pbio);
x = PEM_read_bio_X509(pbio, NULL, NULL, NULL);
- if (x) {
+ if (x != NULL) {
param = X509_get_pubkey(x);
X509_free(x);
}
BIO_free(pbio);
- if (!param) {
+ if (param == NULL) {
BIO_printf(bio_err, "Error reading parameter file %s\n", paramfile);
return NULL;
}
- if (*pkey_type == -1)
+ if (*pkey_type == -1) {
*pkey_type = EVP_PKEY_id(param);
- else if (*pkey_type != EVP_PKEY_base_id(param)) {
+ } else if (*pkey_type != EVP_PKEY_base_id(param)) {
BIO_printf(bio_err, "Key Type does not match parameters\n");
EVP_PKEY_free(param);
return NULL;
}
}
- if (palgnam) {
+ if (palgnam != NULL) {
const EVP_PKEY_ASN1_METHOD *ameth;
ENGINE *tmpeng;
const char *anam;
+
ameth = EVP_PKEY_asn1_find(&tmpeng, *pkey_type);
- if (!ameth) {
+ if (ameth == NULL) {
BIO_puts(bio_err, "Internal error: can't find key algorithm\n");
return NULL;
}
#endif
}
- if (param) {
+ if (param != NULL) {
gctx = EVP_PKEY_CTX_new(param, keygen_engine);
*pkeylen = EVP_PKEY_bits(param);
EVP_PKEY_free(param);
- } else
+ } else {
gctx = EVP_PKEY_CTX_new_id(*pkey_type, keygen_engine);
+ }
if (gctx == NULL) {
BIO_puts(bio_err, "Error allocating keygen context\n");