2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
17 #include <openssl/bio.h>
18 #include <openssl/evp.h>
19 #include <openssl/conf.h>
20 #include <openssl/err.h>
21 #include <openssl/asn1.h>
22 #include <openssl/x509.h>
23 #include <openssl/x509v3.h>
24 #include <openssl/objects.h>
25 #include <openssl/pem.h>
26 #include <openssl/bn.h>
27 #include <openssl/lhash.h>
28 #ifndef OPENSSL_NO_RSA
29 # include <openssl/rsa.h>
31 #ifndef OPENSSL_NO_DSA
32 # include <openssl/dsa.h>
37 #define BITS "default_bits"
38 #define KEYFILE "default_keyfile"
39 #define PROMPT "prompt"
40 #define DISTINGUISHED_NAME "distinguished_name"
41 #define ATTRIBUTES "attributes"
42 #define V3_EXTENSIONS "x509_extensions"
43 #define REQ_EXTENSIONS "req_extensions"
44 #define STRING_MASK "string_mask"
45 #define UTF8_IN "utf8"
47 #define DEFAULT_KEY_LENGTH 2048
48 #define MIN_KEY_LENGTH 512
50 static int make_REQ(X509_REQ
*req
, EVP_PKEY
*pkey
, char *dn
, int mutlirdn
,
51 int attribs
, unsigned long chtype
);
52 static int build_subject(X509_REQ
*req
, const char *subj
, unsigned long chtype
,
54 static int prompt_info(X509_REQ
*req
,
55 STACK_OF(CONF_VALUE
) *dn_sk
, const char *dn_sect
,
56 STACK_OF(CONF_VALUE
) *attr_sk
, const char *attr_sect
,
57 int attribs
, unsigned long chtype
);
58 static int auto_info(X509_REQ
*req
, STACK_OF(CONF_VALUE
) *sk
,
59 STACK_OF(CONF_VALUE
) *attr
, int attribs
,
60 unsigned long chtype
);
61 static int add_attribute_object(X509_REQ
*req
, char *text
, const char *def
,
62 char *value
, int nid
, int n_min
, int n_max
,
63 unsigned long chtype
);
64 static int add_DN_object(X509_NAME
*n
, char *text
, const char *def
,
65 char *value
, int nid
, int n_min
, int n_max
,
66 unsigned long chtype
, int mval
);
67 static int genpkey_cb(EVP_PKEY_CTX
*ctx
);
68 static int build_data(char *text
, const char *def
,
69 char *value
, int n_min
, int n_max
,
70 char *buf
, const int buf_size
,
71 const char *desc1
, const char *desc2
73 static int req_check_len(int len
, int n_min
, int n_max
);
74 static int check_end(const char *str
, const char *end
);
75 static int join(char buf
[], size_t buf_size
, const char *name
,
76 const char *tail
, const char *desc
);
77 static EVP_PKEY_CTX
*set_keygen_ctx(const char *gstr
,
78 int *pkey_type
, long *pkeylen
,
79 char **palgnam
, ENGINE
*keygen_engine
);
80 static CONF
*req_conf
= NULL
;
81 static CONF
*addext_conf
= NULL
;
84 typedef enum OPTION_choice
{
85 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
86 OPT_INFORM
, OPT_OUTFORM
, OPT_ENGINE
, OPT_KEYGEN_ENGINE
, OPT_KEY
,
87 OPT_PUBKEY
, OPT_NEW
, OPT_CONFIG
, OPT_KEYFORM
, OPT_IN
, OPT_OUT
,
88 OPT_KEYOUT
, OPT_PASSIN
, OPT_PASSOUT
, OPT_NEWKEY
,
89 OPT_PKEYOPT
, OPT_SIGOPT
, OPT_BATCH
, OPT_NEWHDR
, OPT_MODULUS
,
90 OPT_VERIFY
, OPT_NODES
, OPT_NOOUT
, OPT_VERBOSE
, OPT_UTF8
,
91 OPT_NAMEOPT
, OPT_REQOPT
, OPT_SUBJ
, OPT_SUBJECT
, OPT_TEXT
, OPT_X509
,
92 OPT_MULTIVALUE_RDN
, OPT_DAYS
, OPT_SET_SERIAL
, OPT_ADDEXT
, OPT_EXTENSIONS
,
93 OPT_REQEXTS
, OPT_PRECERT
, OPT_MD
, OPT_SM2ID
, OPT_SM2HEXID
,
97 const OPTIONS req_options
[] = {
98 {"help", OPT_HELP
, '-', "Display this summary"},
99 {"inform", OPT_INFORM
, 'F', "Input format - DER or PEM"},
100 {"outform", OPT_OUTFORM
, 'F', "Output format - DER or PEM"},
101 {"in", OPT_IN
, '<', "Input file"},
102 {"out", OPT_OUT
, '>', "Output file"},
103 {"key", OPT_KEY
, 's', "Private key to use"},
104 {"keyform", OPT_KEYFORM
, 'f', "Key file format"},
105 {"pubkey", OPT_PUBKEY
, '-', "Output public key"},
106 {"new", OPT_NEW
, '-', "New request"},
107 {"config", OPT_CONFIG
, '<', "Request template file"},
108 {"keyout", OPT_KEYOUT
, '>', "File to send the key to"},
109 {"passin", OPT_PASSIN
, 's', "Private key password source"},
110 {"passout", OPT_PASSOUT
, 's', "Output file pass phrase source"},
112 {"newkey", OPT_NEWKEY
, 's', "Specify as type:bits"},
113 {"pkeyopt", OPT_PKEYOPT
, 's', "Public key options as opt:value"},
114 {"sigopt", OPT_SIGOPT
, 's', "Signature parameter in n:v form"},
115 {"batch", OPT_BATCH
, '-',
116 "Do not ask anything during request generation"},
117 {"newhdr", OPT_NEWHDR
, '-', "Output \"NEW\" in the header lines"},
118 {"modulus", OPT_MODULUS
, '-', "RSA modulus"},
119 {"verify", OPT_VERIFY
, '-', "Verify signature on REQ"},
120 {"nodes", OPT_NODES
, '-', "Don't encrypt the output key"},
121 {"noout", OPT_NOOUT
, '-', "Do not output REQ"},
122 {"verbose", OPT_VERBOSE
, '-', "Verbose output"},
123 {"utf8", OPT_UTF8
, '-', "Input characters are UTF8 (default ASCII)"},
124 {"nameopt", OPT_NAMEOPT
, 's', "Various certificate name options"},
125 {"reqopt", OPT_REQOPT
, 's', "Various request text options"},
126 {"text", OPT_TEXT
, '-', "Text form of request"},
127 {"x509", OPT_X509
, '-',
128 "Output a x509 structure instead of a cert request"},
129 {OPT_MORE_STR
, 1, 1, "(Required by some CA's)"},
130 {"subj", OPT_SUBJ
, 's', "Set or modify request subject"},
131 {"subject", OPT_SUBJECT
, '-', "Output the request's subject"},
132 {"multivalue-rdn", OPT_MULTIVALUE_RDN
, '-',
133 "Enable support for multivalued RDNs"},
134 {"days", OPT_DAYS
, 'p', "Number of days cert is valid for"},
135 {"set_serial", OPT_SET_SERIAL
, 's', "Serial number to use"},
136 {"addext", OPT_ADDEXT
, 's',
137 "Additional cert extension key=value pair (may be given more than once)"},
138 {"extensions", OPT_EXTENSIONS
, 's',
139 "Cert extension section (override value in config file)"},
140 {"reqexts", OPT_REQEXTS
, 's',
141 "Request extension section (override value in config file)"},
142 {"precert", OPT_PRECERT
, '-', "Add a poison extension (implies -new)"},
143 {"", OPT_MD
, '-', "Any supported digest"},
144 #ifndef OPENSSL_NO_ENGINE
145 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
146 {"keygen_engine", OPT_KEYGEN_ENGINE
, 's',
147 "Specify engine to be used for key generation operations"},
149 #ifndef OPENSSL_NO_SM2
150 {"sm2-id", OPT_SM2ID
, 's',
151 "Specify an ID string to verify an SM2 certificate request"},
152 {"sm2-hex-id", OPT_SM2HEXID
, 's',
153 "Specify a hex ID string to verify an SM2 certificate request"},
160 * An LHASH of strings, where each string is an extension name.
162 static unsigned long ext_name_hash(const OPENSSL_STRING
*a
)
164 return OPENSSL_LH_strhash((const char *)a
);
167 static int ext_name_cmp(const OPENSSL_STRING
*a
, const OPENSSL_STRING
*b
)
169 return strcmp((const char *)a
, (const char *)b
);
172 static void exts_cleanup(OPENSSL_STRING
*x
)
174 OPENSSL_free((char *)x
);
178 * Is the |kv| key already duplicated? This is remarkably tricky to get
179 * right. Return 0 if unique, -1 on runtime error; 1 if found or a syntax
182 static int duplicated(LHASH_OF(OPENSSL_STRING
) *addexts
, char *kv
)
188 /* Skip leading whitespace, make a copy. */
189 while (*kv
&& isspace(*kv
))
192 if ((p
= strchr(kv
, '=')) == NULL
)
195 if ((kv
= OPENSSL_strdup(kv
)) == NULL
)
198 /* Skip trailing space before the equal sign. */
199 for (p
= kv
+ off
; p
> kv
; --p
)
208 /* Finally have a clean "key"; see if it's there [by attempt to add it]. */
209 if ((p
= (char *)lh_OPENSSL_STRING_insert(addexts
, (OPENSSL_STRING
*)kv
))
210 != NULL
|| lh_OPENSSL_STRING_error(addexts
)) {
211 OPENSSL_free(p
!= NULL
? p
: kv
);
218 int req_main(int argc
, char **argv
)
220 ASN1_INTEGER
*serial
= NULL
;
221 BIO
*in
= NULL
, *out
= NULL
;
222 ENGINE
*e
= NULL
, *gen_eng
= NULL
;
223 EVP_PKEY
*pkey
= NULL
;
224 EVP_PKEY_CTX
*genctx
= NULL
;
225 STACK_OF(OPENSSL_STRING
) *pkeyopts
= NULL
, *sigopts
= NULL
;
226 LHASH_OF(OPENSSL_STRING
) *addexts
= NULL
;
228 X509_REQ
*req
= NULL
;
229 const EVP_CIPHER
*cipher
= NULL
;
230 const EVP_MD
*md_alg
= NULL
, *digest
= NULL
;
231 BIO
*addext_bio
= NULL
;
232 char *extensions
= NULL
, *infile
= NULL
;
233 char *outfile
= NULL
, *keyfile
= NULL
;
234 char *keyalgstr
= NULL
, *p
, *prog
, *passargin
= NULL
, *passargout
= NULL
;
235 char *passin
= NULL
, *passout
= NULL
;
236 char *nofree_passin
= NULL
, *nofree_passout
= NULL
;
237 char *req_exts
= NULL
, *subj
= NULL
;
238 char *template = default_config_file
, *keyout
= NULL
;
239 const char *keyalg
= NULL
;
241 int ret
= 1, x509
= 0, days
= 0, i
= 0, newreq
= 0, verbose
= 0;
242 int pkey_type
= -1, private = 0;
243 int informat
= FORMAT_PEM
, outformat
= FORMAT_PEM
, keyform
= FORMAT_PEM
;
244 int modulus
= 0, multirdn
= 0, verify
= 0, noout
= 0, text
= 0;
245 int nodes
= 0, newhdr
= 0, subject
= 0, pubkey
= 0, precert
= 0;
247 unsigned long chtype
= MBSTRING_ASC
, reqflag
= 0;
248 unsigned char *sm2_id
= NULL
;
249 size_t sm2_idlen
= 0;
252 #ifndef OPENSSL_NO_DES
253 cipher
= EVP_des_ede3_cbc();
256 prog
= opt_init(argc
, argv
, req_options
);
257 while ((o
= opt_next()) != OPT_EOF
) {
262 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
265 opt_help(req_options
);
269 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &informat
))
273 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &outformat
))
277 e
= setup_engine(opt_arg(), 0);
279 case OPT_KEYGEN_ENGINE
:
280 #ifndef OPENSSL_NO_ENGINE
281 gen_eng
= ENGINE_by_id(opt_arg());
282 if (gen_eng
== NULL
) {
283 BIO_printf(bio_err
, "Can't find keygen engine %s\n", *argv
);
298 template = opt_arg();
301 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &keyform
))
314 passargin
= opt_arg();
317 passargout
= opt_arg();
329 pkeyopts
= sk_OPENSSL_STRING_new_null();
330 if (!pkeyopts
|| !sk_OPENSSL_STRING_push(pkeyopts
, opt_arg()))
335 sigopts
= sk_OPENSSL_STRING_new_null();
336 if (!sigopts
|| !sk_OPENSSL_STRING_push(sigopts
, opt_arg()))
361 chtype
= MBSTRING_UTF8
;
364 if (!set_nameopt(opt_arg()))
368 if (!set_cert_ex(&reqflag
, opt_arg()))
378 days
= atoi(opt_arg());
381 if (serial
!= NULL
) {
382 BIO_printf(bio_err
, "Serial number supplied twice\n");
385 serial
= s2i_ASN1_INTEGER(NULL
, opt_arg());
395 case OPT_MULTIVALUE_RDN
:
400 if (addexts
== NULL
) {
401 addexts
= lh_OPENSSL_STRING_new(ext_name_hash
, ext_name_cmp
);
402 addext_bio
= BIO_new(BIO_s_mem());
403 if (addexts
== NULL
|| addext_bio
== NULL
)
406 i
= duplicated(addexts
, p
);
409 if (i
< 0 || BIO_printf(addext_bio
, "%s\n", opt_arg()) < 0)
413 extensions
= opt_arg();
416 req_exts
= opt_arg();
419 newreq
= precert
= 1;
422 if (!opt_md(opt_unknown(), &md_alg
))
427 if (sm2_id
!= NULL
) {
429 "Use one of the options 'sm2-hex-id' or 'sm2-id'\n");
432 sm2_id
= (unsigned char *)opt_arg();
433 sm2_idlen
= strlen((const char *)sm2_id
);
436 if (sm2_id
!= NULL
) {
438 "Use one of the options 'sm2-hex-id' or 'sm2-id'\n");
441 /* try to parse the input as hex string first */
443 sm2_id
= OPENSSL_hexstr2buf(opt_arg(), (long *)&sm2_idlen
);
444 if (sm2_id
== NULL
) {
445 BIO_printf(bio_err
, "Invalid hex string input\n");
451 argc
= opt_num_rest();
456 BIO_printf(bio_err
, "Ignoring -days; not generating a certificate\n");
457 if (x509
&& infile
== NULL
)
460 /* TODO: simplify this as pkey is still always NULL here */
461 private = newreq
&& (pkey
== NULL
) ? 1 : 0;
463 if (!app_passwd(passargin
, passargout
, &passin
, &passout
)) {
464 BIO_printf(bio_err
, "Error getting passwords\n");
469 BIO_printf(bio_err
, "Using configuration from %s\n", template);
470 req_conf
= app_load_config(template);
474 "Using additional configuration from command line\n");
475 addext_conf
= app_load_config_bio(addext_bio
, NULL
);
477 if (template != default_config_file
&& !app_load_modules(req_conf
))
480 if (req_conf
!= NULL
) {
481 p
= NCONF_get_string(req_conf
, NULL
, "oid_file");
487 oid_bio
= BIO_new_file(p
, "r");
488 if (oid_bio
== NULL
) {
490 BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
491 ERR_print_errors(bio_err);
494 OBJ_create_objects(oid_bio
);
499 if (!add_oid_section(req_conf
))
502 if (md_alg
== NULL
) {
503 p
= NCONF_get_string(req_conf
, SECTION
, "default_md");
507 if (!opt_md(p
, &md_alg
))
513 if (extensions
== NULL
) {
514 extensions
= NCONF_get_string(req_conf
, SECTION
, V3_EXTENSIONS
);
515 if (extensions
== NULL
)
518 if (extensions
!= NULL
) {
519 /* Check syntax of file */
521 X509V3_set_ctx_test(&ctx
);
522 X509V3_set_nconf(&ctx
, req_conf
);
523 if (!X509V3_EXT_add_nconf(req_conf
, &ctx
, extensions
, NULL
)) {
525 "Error Loading extension section %s\n", extensions
);
529 if (addext_conf
!= NULL
) {
530 /* Check syntax of command line extensions */
532 X509V3_set_ctx_test(&ctx
);
533 X509V3_set_nconf(&ctx
, addext_conf
);
534 if (!X509V3_EXT_add_nconf(addext_conf
, &ctx
, "default", NULL
)) {
535 BIO_printf(bio_err
, "Error Loading command line extensions\n");
540 if (passin
== NULL
) {
541 passin
= nofree_passin
=
542 NCONF_get_string(req_conf
, SECTION
, "input_password");
547 if (passout
== NULL
) {
548 passout
= nofree_passout
=
549 NCONF_get_string(req_conf
, SECTION
, "output_password");
554 p
= NCONF_get_string(req_conf
, SECTION
, STRING_MASK
);
558 if (p
!= NULL
&& !ASN1_STRING_set_default_mask_asc(p
)) {
559 BIO_printf(bio_err
, "Invalid global string mask setting %s\n", p
);
563 if (chtype
!= MBSTRING_UTF8
) {
564 p
= NCONF_get_string(req_conf
, SECTION
, UTF8_IN
);
567 else if (strcmp(p
, "yes") == 0)
568 chtype
= MBSTRING_UTF8
;
571 if (req_exts
== NULL
) {
572 req_exts
= NCONF_get_string(req_conf
, SECTION
, REQ_EXTENSIONS
);
573 if (req_exts
== NULL
)
576 if (req_exts
!= NULL
) {
577 /* Check syntax of file */
579 X509V3_set_ctx_test(&ctx
);
580 X509V3_set_nconf(&ctx
, req_conf
);
581 if (!X509V3_EXT_add_nconf(req_conf
, &ctx
, req_exts
, NULL
)) {
583 "Error Loading request extension section %s\n",
589 if (keyfile
!= NULL
) {
590 pkey
= load_key(keyfile
, keyform
, 0, passin
, e
, "Private Key");
592 /* load_key() has already printed an appropriate message */
595 app_RAND_load_conf(req_conf
, SECTION
);
599 if (newreq
&& (pkey
== NULL
)) {
600 app_RAND_load_conf(req_conf
, SECTION
);
602 if (!NCONF_get_number(req_conf
, SECTION
, BITS
, &newkey
)) {
603 newkey
= DEFAULT_KEY_LENGTH
;
606 if (keyalg
!= NULL
) {
607 genctx
= set_keygen_ctx(keyalg
, &pkey_type
, &newkey
,
608 &keyalgstr
, gen_eng
);
613 if (newkey
< MIN_KEY_LENGTH
614 && (pkey_type
== EVP_PKEY_RSA
|| pkey_type
== EVP_PKEY_DSA
)) {
615 BIO_printf(bio_err
, "private key length is too short,\n");
616 BIO_printf(bio_err
, "it needs to be at least %d bits, not %ld\n",
617 MIN_KEY_LENGTH
, newkey
);
621 if (pkey_type
== EVP_PKEY_RSA
&& newkey
> OPENSSL_RSA_MAX_MODULUS_BITS
)
623 "Warning: It is not recommended to use more than %d bit for RSA keys.\n"
624 " Your key size is %ld! Larger key size may behave not as expected.\n",
625 OPENSSL_RSA_MAX_MODULUS_BITS
, newkey
);
627 #ifndef OPENSSL_NO_DSA
628 if (pkey_type
== EVP_PKEY_DSA
&& newkey
> OPENSSL_DSA_MAX_MODULUS_BITS
)
630 "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
631 " Your key size is %ld! Larger key size may behave not as expected.\n",
632 OPENSSL_DSA_MAX_MODULUS_BITS
, newkey
);
635 if (genctx
== NULL
) {
636 genctx
= set_keygen_ctx(NULL
, &pkey_type
, &newkey
,
637 &keyalgstr
, gen_eng
);
642 if (pkeyopts
!= NULL
) {
644 for (i
= 0; i
< sk_OPENSSL_STRING_num(pkeyopts
); i
++) {
645 genopt
= sk_OPENSSL_STRING_value(pkeyopts
, i
);
646 if (pkey_ctrl_string(genctx
, genopt
) <= 0) {
647 BIO_printf(bio_err
, "parameter error \"%s\"\n", genopt
);
648 ERR_print_errors(bio_err
);
654 if (pkey_type
== EVP_PKEY_EC
) {
655 BIO_printf(bio_err
, "Generating an EC private key\n");
657 BIO_printf(bio_err
, "Generating a %s private key\n", keyalgstr
);
660 EVP_PKEY_CTX_set_cb(genctx
, genpkey_cb
);
661 EVP_PKEY_CTX_set_app_data(genctx
, bio_err
);
663 if (EVP_PKEY_keygen(genctx
, &pkey
) <= 0) {
664 BIO_puts(bio_err
, "Error Generating Key\n");
668 EVP_PKEY_CTX_free(genctx
);
671 if (keyout
== NULL
) {
672 keyout
= NCONF_get_string(req_conf
, SECTION
, KEYFILE
);
678 BIO_printf(bio_err
, "writing new private key to stdout\n");
680 BIO_printf(bio_err
, "writing new private key to '%s'\n", keyout
);
681 out
= bio_open_owner(keyout
, outformat
, private);
685 p
= NCONF_get_string(req_conf
, SECTION
, "encrypt_rsa_key");
688 p
= NCONF_get_string(req_conf
, SECTION
, "encrypt_key");
692 if ((p
!= NULL
) && (strcmp(p
, "no") == 0))
700 if (!PEM_write_bio_PrivateKey(out
, pkey
, cipher
,
701 NULL
, 0, NULL
, passout
)) {
702 if ((ERR_GET_REASON(ERR_peek_error()) ==
703 PEM_R_PROBLEMS_GETTING_PASSWORD
) && (i
< 3)) {
712 BIO_printf(bio_err
, "-----\n");
716 in
= bio_open_default(infile
, 'r', informat
);
720 if (informat
== FORMAT_ASN1
)
721 req
= d2i_X509_REQ_bio(in
, NULL
);
723 req
= PEM_read_bio_X509_REQ(in
, NULL
, NULL
, NULL
);
725 BIO_printf(bio_err
, "unable to load X509 request\n");
730 if (newreq
|| x509
) {
732 BIO_printf(bio_err
, "you need to specify a private key\n");
737 req
= X509_REQ_new();
742 i
= make_REQ(req
, pkey
, subj
, multirdn
, !x509
, chtype
);
743 subj
= NULL
; /* done processing '-subj' option */
745 BIO_printf(bio_err
, "problems making Certificate Request\n");
752 if ((x509ss
= X509_new()) == NULL
)
755 /* Set version to V3 */
756 if ((extensions
!= NULL
|| addext_conf
!= NULL
)
757 && !X509_set_version(x509ss
, 2))
759 if (serial
!= NULL
) {
760 if (!X509_set_serialNumber(x509ss
, serial
))
763 if (!rand_serial(NULL
, X509_get_serialNumber(x509ss
)))
767 if (!X509_set_issuer_name(x509ss
, X509_REQ_get_subject_name(req
)))
770 /* set default days if it's not specified */
773 if (!set_cert_times(x509ss
, NULL
, NULL
, days
))
775 if (!X509_set_subject_name
776 (x509ss
, X509_REQ_get_subject_name(req
)))
778 tmppkey
= X509_REQ_get0_pubkey(req
);
779 if (!tmppkey
|| !X509_set_pubkey(x509ss
, tmppkey
))
782 /* Set up V3 context struct */
784 X509V3_set_ctx(&ext_ctx
, x509ss
, x509ss
, NULL
, NULL
, 0);
785 X509V3_set_nconf(&ext_ctx
, req_conf
);
788 if (extensions
!= NULL
&& !X509V3_EXT_add_nconf(req_conf
,
789 &ext_ctx
, extensions
,
791 BIO_printf(bio_err
, "Error Loading extension section %s\n",
795 if (addext_conf
!= NULL
796 && !X509V3_EXT_add_nconf(addext_conf
, &ext_ctx
, "default",
798 BIO_printf(bio_err
, "Error Loading command line extensions\n");
802 /* If a pre-cert was requested, we need to add a poison extension */
804 if (X509_add1_ext_i2d(x509ss
, NID_ct_precert_poison
, NULL
, 1, 0)
806 BIO_printf(bio_err
, "Error adding poison extension\n");
811 i
= do_X509_sign(x509ss
, pkey
, digest
, sigopts
);
813 ERR_print_errors(bio_err
);
819 /* Set up V3 context struct */
821 X509V3_set_ctx(&ext_ctx
, NULL
, NULL
, req
, NULL
, 0);
822 X509V3_set_nconf(&ext_ctx
, req_conf
);
826 && !X509V3_EXT_REQ_add_nconf(req_conf
, &ext_ctx
,
828 BIO_printf(bio_err
, "Error Loading extension section %s\n",
832 if (addext_conf
!= NULL
833 && !X509V3_EXT_REQ_add_nconf(addext_conf
, &ext_ctx
, "default",
835 BIO_printf(bio_err
, "Error Loading command line extensions\n");
838 i
= do_X509_REQ_sign(req
, pkey
, digest
, sigopts
);
840 ERR_print_errors(bio_err
);
847 BIO_printf(bio_err
, "Cannot modify certificate subject\n");
853 BIO_printf(bio_err
, "Modifying Request's Subject\n");
854 print_name(bio_err
, "old subject=",
855 X509_REQ_get_subject_name(req
), get_nameopt());
858 if (build_subject(req
, subj
, chtype
, multirdn
) == 0) {
859 BIO_printf(bio_err
, "ERROR: cannot modify subject\n");
865 print_name(bio_err
, "new subject=",
866 X509_REQ_get_subject_name(req
), get_nameopt());
870 if (verify
&& !x509
) {
871 EVP_PKEY
*tpubkey
= pkey
;
873 if (tpubkey
== NULL
) {
874 tpubkey
= X509_REQ_get0_pubkey(req
);
879 if (sm2_id
!= NULL
) {
880 #ifndef OPENSSL_NO_SM2
881 ASN1_OCTET_STRING
*v
;
883 v
= ASN1_OCTET_STRING_new();
885 BIO_printf(bio_err
, "error: SM2 ID allocation failed\n");
889 if (!ASN1_OCTET_STRING_set(v
, sm2_id
, sm2_idlen
)) {
890 BIO_printf(bio_err
, "error: setting SM2 ID failed\n");
891 ASN1_OCTET_STRING_free(v
);
895 X509_REQ_set0_sm2_id(req
, v
);
899 i
= X509_REQ_verify(req
, tpubkey
);
904 BIO_printf(bio_err
, "verify failure\n");
905 ERR_print_errors(bio_err
);
906 } else { /* if (i > 0) */
907 BIO_printf(bio_err
, "verify OK\n");
911 if (noout
&& !text
&& !modulus
&& !subject
&& !pubkey
) {
916 out
= bio_open_default(outfile
,
917 keyout
!= NULL
&& outfile
!= NULL
&&
918 strcmp(keyout
, outfile
) == 0 ? 'a' : 'w',
924 EVP_PKEY
*tpubkey
= X509_REQ_get0_pubkey(req
);
926 if (tpubkey
== NULL
) {
927 BIO_printf(bio_err
, "Error getting public key\n");
928 ERR_print_errors(bio_err
);
931 PEM_write_bio_PUBKEY(out
, tpubkey
);
936 ret
= X509_print_ex(out
, x509ss
, get_nameopt(), reqflag
);
938 ret
= X509_REQ_print_ex(out
, req
, get_nameopt(), reqflag
);
942 BIO_printf(bio_err
, "Error printing certificate\n");
944 BIO_printf(bio_err
, "Error printing certificate request\n");
946 ERR_print_errors(bio_err
);
953 print_name(out
, "subject=", X509_get_subject_name(x509ss
),
956 print_name(out
, "subject=", X509_REQ_get_subject_name(req
),
964 tpubkey
= X509_get0_pubkey(x509ss
);
966 tpubkey
= X509_REQ_get0_pubkey(req
);
967 if (tpubkey
== NULL
) {
968 fprintf(stdout
, "Modulus=unavailable\n");
971 fprintf(stdout
, "Modulus=");
972 #ifndef OPENSSL_NO_RSA
973 if (EVP_PKEY_base_id(tpubkey
) == EVP_PKEY_RSA
) {
975 RSA_get0_key(EVP_PKEY_get0_RSA(tpubkey
), &n
, NULL
, NULL
);
979 fprintf(stdout
, "Wrong Algorithm type");
980 fprintf(stdout
, "\n");
983 if (!noout
&& !x509
) {
984 if (outformat
== FORMAT_ASN1
)
985 i
= i2d_X509_REQ_bio(out
, req
);
987 i
= PEM_write_bio_X509_REQ_NEW(out
, req
);
989 i
= PEM_write_bio_X509_REQ(out
, req
);
991 BIO_printf(bio_err
, "unable to write X509 request\n");
995 if (!noout
&& x509
&& (x509ss
!= NULL
)) {
996 if (outformat
== FORMAT_ASN1
)
997 i
= i2d_X509_bio(out
, x509ss
);
999 i
= PEM_write_bio_X509(out
, x509ss
);
1001 BIO_printf(bio_err
, "unable to write X509 certificate\n");
1008 OPENSSL_free(sm2_id
);
1010 ERR_print_errors(bio_err
);
1012 NCONF_free(req_conf
);
1013 NCONF_free(addext_conf
);
1014 BIO_free(addext_bio
);
1017 EVP_PKEY_free(pkey
);
1018 EVP_PKEY_CTX_free(genctx
);
1019 sk_OPENSSL_STRING_free(pkeyopts
);
1020 sk_OPENSSL_STRING_free(sigopts
);
1021 lh_OPENSSL_STRING_doall(addexts
, exts_cleanup
);
1022 lh_OPENSSL_STRING_free(addexts
);
1023 #ifndef OPENSSL_NO_ENGINE
1024 ENGINE_free(gen_eng
);
1026 OPENSSL_free(keyalgstr
);
1029 ASN1_INTEGER_free(serial
);
1031 if (passin
!= nofree_passin
)
1032 OPENSSL_free(passin
);
1033 if (passout
!= nofree_passout
)
1034 OPENSSL_free(passout
);
1038 static int make_REQ(X509_REQ
*req
, EVP_PKEY
*pkey
, char *subj
, int multirdn
,
1039 int attribs
, unsigned long chtype
)
1043 STACK_OF(CONF_VALUE
) *dn_sk
, *attr_sk
= NULL
;
1044 char *tmp
, *dn_sect
, *attr_sect
;
1046 tmp
= NCONF_get_string(req_conf
, SECTION
, PROMPT
);
1049 if ((tmp
!= NULL
) && strcmp(tmp
, "no") == 0)
1052 dn_sect
= NCONF_get_string(req_conf
, SECTION
, DISTINGUISHED_NAME
);
1053 if (dn_sect
== NULL
) {
1054 BIO_printf(bio_err
, "unable to find '%s' in config\n",
1055 DISTINGUISHED_NAME
);
1058 dn_sk
= NCONF_get_section(req_conf
, dn_sect
);
1059 if (dn_sk
== NULL
) {
1060 BIO_printf(bio_err
, "unable to get '%s' section\n", dn_sect
);
1064 attr_sect
= NCONF_get_string(req_conf
, SECTION
, ATTRIBUTES
);
1065 if (attr_sect
== NULL
) {
1069 attr_sk
= NCONF_get_section(req_conf
, attr_sect
);
1070 if (attr_sk
== NULL
) {
1071 BIO_printf(bio_err
, "unable to get '%s' section\n", attr_sect
);
1076 /* setup version number */
1077 if (!X509_REQ_set_version(req
, 0L))
1078 goto err
; /* version 1 */
1081 i
= build_subject(req
, subj
, chtype
, multirdn
);
1083 i
= auto_info(req
, dn_sk
, attr_sk
, attribs
, chtype
);
1085 i
= prompt_info(req
, dn_sk
, dn_sect
, attr_sk
, attr_sect
, attribs
,
1090 if (!X509_REQ_set_pubkey(req
, pkey
))
1099 * subject is expected to be in the format /type0=value0/type1=value1/type2=...
1100 * where characters may be escaped by \
1102 static int build_subject(X509_REQ
*req
, const char *subject
, unsigned long chtype
,
1107 if ((n
= parse_name(subject
, chtype
, multirdn
)) == NULL
)
1110 if (!X509_REQ_set_subject_name(req
, n
)) {
1118 static int prompt_info(X509_REQ
*req
,
1119 STACK_OF(CONF_VALUE
) *dn_sk
, const char *dn_sect
,
1120 STACK_OF(CONF_VALUE
) *attr_sk
, const char *attr_sect
,
1121 int attribs
, unsigned long chtype
)
1132 subj
= X509_REQ_get_subject_name(req
);
1136 "You are about to be asked to enter information that will be incorporated\n");
1137 BIO_printf(bio_err
, "into your certificate request.\n");
1139 "What you are about to enter is what is called a Distinguished Name or a DN.\n");
1141 "There are quite a few fields but you can leave some blank\n");
1143 "For some fields there will be a default value,\n");
1145 "If you enter '.', the field will be left blank.\n");
1146 BIO_printf(bio_err
, "-----\n");
1149 if (sk_CONF_VALUE_num(dn_sk
)) {
1154 if (sk_CONF_VALUE_num(dn_sk
) <= i
)
1157 v
= sk_CONF_VALUE_value(dn_sk
, i
);
1160 if (!check_end(type
, "_min") || !check_end(type
, "_max") ||
1161 !check_end(type
, "_default") || !check_end(type
, "_value"))
1164 * Skip past any leading X. X: X, etc to allow for multiple
1167 for (p
= v
->name
; *p
; p
++)
1168 if ((*p
== ':') || (*p
== ',') || (*p
== '.')) {
1180 /* If OBJ not recognised ignore it */
1181 if ((nid
= OBJ_txt2nid(type
)) == NID_undef
)
1183 if (!join(buf
, sizeof(buf
), v
->name
, "_default", "Name"))
1185 if ((def
= NCONF_get_string(req_conf
, dn_sect
, buf
)) == NULL
) {
1190 if (!join(buf
, sizeof(buf
), v
->name
, "_value", "Name"))
1192 if ((value
= NCONF_get_string(req_conf
, dn_sect
, buf
)) == NULL
) {
1197 if (!join(buf
, sizeof(buf
), v
->name
, "_min", "Name"))
1199 if (!NCONF_get_number(req_conf
, dn_sect
, buf
, &n_min
)) {
1205 if (!join(buf
, sizeof(buf
), v
->name
, "_max", "Name"))
1207 if (!NCONF_get_number(req_conf
, dn_sect
, buf
, &n_max
)) {
1212 if (!add_DN_object(subj
, v
->value
, def
, value
, nid
,
1213 n_min
, n_max
, chtype
, mval
))
1216 if (X509_NAME_entry_count(subj
) == 0) {
1218 "error, no objects specified in config file\n");
1223 if ((attr_sk
!= NULL
) && (sk_CONF_VALUE_num(attr_sk
) > 0)
1226 "\nPlease enter the following 'extra' attributes\n");
1228 "to be sent with your certificate request\n");
1235 if ((attr_sk
== NULL
) || (sk_CONF_VALUE_num(attr_sk
) <= i
))
1238 v
= sk_CONF_VALUE_value(attr_sk
, i
);
1240 if ((nid
= OBJ_txt2nid(type
)) == NID_undef
)
1243 if (!join(buf
, sizeof(buf
), type
, "_default", "Name"))
1245 if ((def
= NCONF_get_string(req_conf
, attr_sect
, buf
))
1251 if (!join(buf
, sizeof(buf
), type
, "_value", "Name"))
1253 if ((value
= NCONF_get_string(req_conf
, attr_sect
, buf
))
1259 if (!join(buf
, sizeof(buf
), type
,"_min", "Name"))
1261 if (!NCONF_get_number(req_conf
, attr_sect
, buf
, &n_min
)) {
1266 if (!join(buf
, sizeof(buf
), type
, "_max", "Name"))
1268 if (!NCONF_get_number(req_conf
, attr_sect
, buf
, &n_max
)) {
1273 if (!add_attribute_object(req
,
1274 v
->value
, def
, value
, nid
, n_min
,
1280 BIO_printf(bio_err
, "No template, please set one up.\n");
1288 static int auto_info(X509_REQ
*req
, STACK_OF(CONF_VALUE
) *dn_sk
,
1289 STACK_OF(CONF_VALUE
) *attr_sk
, int attribs
,
1290 unsigned long chtype
)
1292 int i
, spec_char
, plus_char
;
1298 subj
= X509_REQ_get_subject_name(req
);
1300 for (i
= 0; i
< sk_CONF_VALUE_num(dn_sk
); i
++) {
1302 v
= sk_CONF_VALUE_value(dn_sk
, i
);
1306 * Skip past any leading X. X: X, etc to allow for multiple instances
1308 for (p
= v
->name
; *p
; p
++) {
1309 #ifndef CHARSET_EBCDIC
1310 spec_char
= ((*p
== ':') || (*p
== ',') || (*p
== '.'));
1312 spec_char
= ((*p
== os_toascii
[':']) || (*p
== os_toascii
[','])
1313 || (*p
== os_toascii
['.']));
1322 #ifndef CHARSET_EBCDIC
1323 plus_char
= (*type
== '+');
1325 plus_char
= (*type
== os_toascii
['+']);
1333 if (!X509_NAME_add_entry_by_txt(subj
, type
, chtype
,
1334 (unsigned char *)v
->value
, -1, -1,
1340 if (!X509_NAME_entry_count(subj
)) {
1341 BIO_printf(bio_err
, "error, no objects specified in config file\n");
1345 for (i
= 0; i
< sk_CONF_VALUE_num(attr_sk
); i
++) {
1346 v
= sk_CONF_VALUE_value(attr_sk
, i
);
1347 if (!X509_REQ_add1_attr_by_txt(req
, v
->name
, chtype
,
1348 (unsigned char *)v
->value
, -1))
1355 static int add_DN_object(X509_NAME
*n
, char *text
, const char *def
,
1356 char *value
, int nid
, int n_min
, int n_max
,
1357 unsigned long chtype
, int mval
)
1362 ret
= build_data(text
, def
, value
, n_min
, n_max
, buf
, sizeof(buf
),
1363 "DN value", "DN default");
1364 if ((ret
== 0) || (ret
== 1))
1368 if (!X509_NAME_add_entry_by_NID(n
, nid
, chtype
,
1369 (unsigned char *)buf
, -1, -1, mval
))
1375 static int add_attribute_object(X509_REQ
*req
, char *text
, const char *def
,
1376 char *value
, int nid
, int n_min
,
1377 int n_max
, unsigned long chtype
)
1382 ret
= build_data(text
, def
, value
, n_min
, n_max
, buf
, sizeof(buf
),
1383 "Attribute value", "Attribute default");
1384 if ((ret
== 0) || (ret
== 1))
1388 if (!X509_REQ_add1_attr_by_NID(req
, nid
, chtype
,
1389 (unsigned char *)buf
, -1)) {
1390 BIO_printf(bio_err
, "Error adding attribute\n");
1391 ERR_print_errors(bio_err
);
1399 static int build_data(char *text
, const char *def
,
1400 char *value
, int n_min
, int n_max
,
1401 char *buf
, const int buf_size
,
1402 const char *desc1
, const char *desc2
1408 BIO_printf(bio_err
, "%s [%s]:", text
, def
);
1409 (void)BIO_flush(bio_err
);
1410 if (value
!= NULL
) {
1411 if (!join(buf
, buf_size
, value
, "\n", desc1
))
1413 BIO_printf(bio_err
, "%s\n", value
);
1417 if (!fgets(buf
, buf_size
, stdin
))
1427 if (buf
[0] == '\n') {
1428 if ((def
== NULL
) || (def
[0] == '\0'))
1430 if (!join(buf
, buf_size
, def
, "\n", desc2
))
1432 } else if ((buf
[0] == '.') && (buf
[1] == '\n')) {
1437 if (buf
[i
- 1] != '\n') {
1438 BIO_printf(bio_err
, "weird input :-(\n");
1442 #ifdef CHARSET_EBCDIC
1443 ebcdic2ascii(buf
, buf
, i
);
1445 if (!req_check_len(i
, n_min
, n_max
)) {
1453 static int req_check_len(int len
, int n_min
, int n_max
)
1455 if ((n_min
> 0) && (len
< n_min
)) {
1457 "string is too short, it needs to be at least %d bytes long\n",
1461 if ((n_max
>= 0) && (len
> n_max
)) {
1463 "string is too long, it needs to be no more than %d bytes long\n",
1470 /* Check if the end of a string matches 'end' */
1471 static int check_end(const char *str
, const char *end
)
1480 tmp
= str
+ slen
- elen
;
1481 return strcmp(tmp
, end
);
1485 * Merge the two strings together into the result buffer checking for
1486 * overflow and producing an error message if there is.
1488 static int join(char buf
[], size_t buf_size
, const char *name
,
1489 const char *tail
, const char *desc
)
1491 const size_t name_len
= strlen(name
), tail_len
= strlen(tail
);
1493 if (name_len
+ tail_len
+ 1 > buf_size
) {
1494 BIO_printf(bio_err
, "%s '%s' too long\n", desc
, name
);
1497 memcpy(buf
, name
, name_len
);
1498 memcpy(buf
+ name_len
, tail
, tail_len
+ 1);
1502 static EVP_PKEY_CTX
*set_keygen_ctx(const char *gstr
,
1503 int *pkey_type
, long *pkeylen
,
1504 char **palgnam
, ENGINE
*keygen_engine
)
1506 EVP_PKEY_CTX
*gctx
= NULL
;
1507 EVP_PKEY
*param
= NULL
;
1510 const char *paramfile
= NULL
;
1513 *pkey_type
= EVP_PKEY_RSA
;
1515 } else if (gstr
[0] >= '0' && gstr
[0] <= '9') {
1516 *pkey_type
= EVP_PKEY_RSA
;
1517 keylen
= atol(gstr
);
1519 } else if (strncmp(gstr
, "param:", 6) == 0) {
1520 paramfile
= gstr
+ 6;
1522 const char *p
= strchr(gstr
, ':');
1525 const EVP_PKEY_ASN1_METHOD
*ameth
;
1532 * The lookup of a the string will cover all engines so keep a note
1533 * of the implementation.
1536 ameth
= EVP_PKEY_asn1_find_str(&tmpeng
, gstr
, len
);
1538 if (ameth
== NULL
) {
1539 BIO_printf(bio_err
, "Unknown algorithm %.*s\n", len
, gstr
);
1543 EVP_PKEY_asn1_get0_info(NULL
, pkey_type
, NULL
, NULL
, NULL
, ameth
);
1544 #ifndef OPENSSL_NO_ENGINE
1545 ENGINE_finish(tmpeng
);
1547 if (*pkey_type
== EVP_PKEY_RSA
) {
1549 keylen
= atol(p
+ 1);
1554 } else if (p
!= NULL
) {
1559 if (paramfile
!= NULL
) {
1560 pbio
= BIO_new_file(paramfile
, "r");
1562 BIO_printf(bio_err
, "Can't open parameter file %s\n", paramfile
);
1565 param
= PEM_read_bio_Parameters(pbio
, NULL
);
1567 if (param
== NULL
) {
1570 (void)BIO_reset(pbio
);
1571 x
= PEM_read_bio_X509(pbio
, NULL
, NULL
, NULL
);
1573 param
= X509_get_pubkey(x
);
1580 if (param
== NULL
) {
1581 BIO_printf(bio_err
, "Error reading parameter file %s\n", paramfile
);
1584 if (*pkey_type
== -1) {
1585 *pkey_type
= EVP_PKEY_id(param
);
1586 } else if (*pkey_type
!= EVP_PKEY_base_id(param
)) {
1587 BIO_printf(bio_err
, "Key Type does not match parameters\n");
1588 EVP_PKEY_free(param
);
1593 if (palgnam
!= NULL
) {
1594 const EVP_PKEY_ASN1_METHOD
*ameth
;
1598 ameth
= EVP_PKEY_asn1_find(&tmpeng
, *pkey_type
);
1599 if (ameth
== NULL
) {
1600 BIO_puts(bio_err
, "Internal error: can't find key algorithm\n");
1603 EVP_PKEY_asn1_get0_info(NULL
, NULL
, NULL
, NULL
, &anam
, ameth
);
1604 *palgnam
= OPENSSL_strdup(anam
);
1605 #ifndef OPENSSL_NO_ENGINE
1606 ENGINE_finish(tmpeng
);
1610 if (param
!= NULL
) {
1611 gctx
= EVP_PKEY_CTX_new(param
, keygen_engine
);
1612 *pkeylen
= EVP_PKEY_bits(param
);
1613 EVP_PKEY_free(param
);
1615 gctx
= EVP_PKEY_CTX_new_id(*pkey_type
, keygen_engine
);
1619 BIO_puts(bio_err
, "Error allocating keygen context\n");
1620 ERR_print_errors(bio_err
);
1624 if (EVP_PKEY_keygen_init(gctx
) <= 0) {
1625 BIO_puts(bio_err
, "Error initializing keygen context\n");
1626 ERR_print_errors(bio_err
);
1627 EVP_PKEY_CTX_free(gctx
);
1630 #ifndef OPENSSL_NO_RSA
1631 if ((*pkey_type
== EVP_PKEY_RSA
) && (keylen
!= -1)) {
1632 if (EVP_PKEY_CTX_set_rsa_keygen_bits(gctx
, keylen
) <= 0) {
1633 BIO_puts(bio_err
, "Error setting RSA keysize\n");
1634 ERR_print_errors(bio_err
);
1635 EVP_PKEY_CTX_free(gctx
);
1644 static int genpkey_cb(EVP_PKEY_CTX
*ctx
)
1647 BIO
*b
= EVP_PKEY_CTX_get_app_data(ctx
);
1649 p
= EVP_PKEY_CTX_get_keygen_info(ctx
, 0);
1658 BIO_write(b
, &c
, 1);
1663 #ifndef OPENSSL_NO_SM2
1664 static int ec_pkey_is_sm2(EVP_PKEY
*pkey
)
1666 EC_KEY
*eckey
= NULL
;
1667 const EC_GROUP
*group
= NULL
;
1669 if (EVP_PKEY_id(pkey
) == EVP_PKEY_SM2
)
1671 if (EVP_PKEY_id(pkey
) == EVP_PKEY_EC
1672 && (eckey
= EVP_PKEY_get0_EC_KEY(pkey
)) != NULL
1673 && (group
= EC_KEY_get0_group(eckey
)) != NULL
1674 && EC_GROUP_get_curve_name(group
) == NID_sm2
)
1680 static int do_sign_init(EVP_MD_CTX
*ctx
, EVP_PKEY
*pkey
,
1681 const EVP_MD
*md
, STACK_OF(OPENSSL_STRING
) *sigopts
)
1683 EVP_PKEY_CTX
*pkctx
= NULL
;
1684 #ifndef OPENSSL_NO_SM2
1685 EVP_PKEY_CTX
*pctx
= NULL
;
1687 int i
, def_nid
, ret
= 0;
1691 #ifndef OPENSSL_NO_SM2
1692 if (ec_pkey_is_sm2(pkey
)) {
1693 /* initialize some SM2-specific code */
1694 if (!EVP_PKEY_set_alias_type(pkey
, EVP_PKEY_SM2
)) {
1695 BIO_printf(bio_err
, "Internal error.\n");
1698 pctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
1700 BIO_printf(bio_err
, "memory allocation failure.\n");
1703 /* set SM2 ID from sig options before calling the real init routine */
1704 for (i
= 0; i
< sk_OPENSSL_STRING_num(sigopts
); i
++) {
1705 char *sigopt
= sk_OPENSSL_STRING_value(sigopts
, i
);
1706 if (pkey_ctrl_string(pctx
, sigopt
) <= 0) {
1707 BIO_printf(bio_err
, "parameter error \"%s\"\n", sigopt
);
1708 ERR_print_errors(bio_err
);
1712 EVP_MD_CTX_set_pkey_ctx(ctx
, pctx
);
1716 * EVP_PKEY_get_default_digest_nid() returns 2 if the digest is mandatory
1717 * for this algorithm.
1719 if (EVP_PKEY_get_default_digest_nid(pkey
, &def_nid
) == 2
1720 && def_nid
== NID_undef
) {
1721 /* The signing algorithm requires there to be no digest */
1724 if (!EVP_DigestSignInit(ctx
, &pkctx
, md
, NULL
, pkey
))
1726 for (i
= 0; i
< sk_OPENSSL_STRING_num(sigopts
); i
++) {
1727 char *sigopt
= sk_OPENSSL_STRING_value(sigopts
, i
);
1728 if (pkey_ctrl_string(pkctx
, sigopt
) <= 0) {
1729 BIO_printf(bio_err
, "parameter error \"%s\"\n", sigopt
);
1730 ERR_print_errors(bio_err
);
1737 #ifndef OPENSSL_NO_SM2
1739 EVP_PKEY_CTX_free(pctx
);
1744 int do_X509_sign(X509
*x
, EVP_PKEY
*pkey
, const EVP_MD
*md
,
1745 STACK_OF(OPENSSL_STRING
) *sigopts
)
1748 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
1749 #ifndef OPENSSL_NO_SM2
1750 EVP_PKEY_CTX
*pctx
= NULL
;
1753 rv
= do_sign_init(mctx
, pkey
, md
, sigopts
);
1755 rv
= X509_sign_ctx(x
, mctx
);
1756 #ifndef OPENSSL_NO_SM2
1757 /* only in SM2 case we need to free the pctx explicitly */
1758 if (ec_pkey_is_sm2(pkey
)) {
1759 pctx
= EVP_MD_CTX_pkey_ctx(mctx
);
1760 EVP_PKEY_CTX_free(pctx
);
1763 EVP_MD_CTX_free(mctx
);
1764 return rv
> 0 ? 1 : 0;
1767 int do_X509_REQ_sign(X509_REQ
*x
, EVP_PKEY
*pkey
, const EVP_MD
*md
,
1768 STACK_OF(OPENSSL_STRING
) *sigopts
)
1771 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
1772 #ifndef OPENSSL_NO_SM2
1773 EVP_PKEY_CTX
*pctx
= NULL
;
1776 rv
= do_sign_init(mctx
, pkey
, md
, sigopts
);
1778 rv
= X509_REQ_sign_ctx(x
, mctx
);
1779 #ifndef OPENSSL_NO_SM2
1780 /* only in SM2 case we need to free the pctx explicitly */
1781 if (ec_pkey_is_sm2(pkey
)) {
1782 pctx
= EVP_MD_CTX_pkey_ctx(mctx
);
1783 EVP_PKEY_CTX_free(pctx
);
1786 EVP_MD_CTX_free(mctx
);
1787 return rv
> 0 ? 1 : 0;
1790 int do_X509_CRL_sign(X509_CRL
*x
, EVP_PKEY
*pkey
, const EVP_MD
*md
,
1791 STACK_OF(OPENSSL_STRING
) *sigopts
)
1794 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
1795 #ifndef OPENSSL_NO_SM2
1796 EVP_PKEY_CTX
*pctx
= NULL
;
1799 rv
= do_sign_init(mctx
, pkey
, md
, sigopts
);
1801 rv
= X509_CRL_sign_ctx(x
, mctx
);
1802 #ifndef OPENSSL_NO_SM2
1803 /* only in SM2 case we need to free the pctx explicitly */
1804 if (ec_pkey_is_sm2(pkey
)) {
1805 pctx
= EVP_MD_CTX_pkey_ctx(mctx
);
1806 EVP_PKEY_CTX_free(pctx
);
1809 EVP_MD_CTX_free(mctx
);
1810 return rv
> 0 ? 1 : 0;