Fix a double free issue when signing SM2 cert
[openssl.git] / doc / man1 / openssl-req.pod
1 =pod
2
3 =head1 NAME
4
5 openssl-req - PKCS#10 certificate request and certificate generating utility
6
7 =head1 SYNOPSIS
8
9 B<openssl> B<req>
10 [B<-help>]
11 [B<-inform PEM|DER>]
12 [B<-outform PEM|DER>]
13 [B<-in filename>]
14 [B<-passin arg>]
15 [B<-out filename>]
16 [B<-passout arg>]
17 [B<-text>]
18 [B<-pubkey>]
19 [B<-noout>]
20 [B<-verify>]
21 [B<-modulus>]
22 [B<-new>]
23 [B<-rand file...>]
24 [B<-writerand file>]
25 [B<-newkey rsa:bits>]
26 [B<-newkey alg:file>]
27 [B<-nodes>]
28 [B<-key filename>]
29 [B<-keyform PEM|DER>]
30 [B<-keyout filename>]
31 [B<-keygen_engine id>]
32 [B<-I<digest>>]
33 [B<-config filename>]
34 [B<-multivalue-rdn>]
35 [B<-x509>]
36 [B<-days n>]
37 [B<-set_serial n>]
38 [B<-newhdr>]
39 [B<-addext ext>]
40 [B<-extensions section>]
41 [B<-reqexts section>]
42 [B<-precert>]
43 [B<-utf8>]
44 [B<-nameopt>]
45 [B<-reqopt>]
46 [B<-subject>]
47 [B<-subj arg>]
48 [B<-sigopt nm:v>]
49 [B<-batch>]
50 [B<-verbose>]
51 [B<-engine id>]
52 [B<-sm2-id string>]
53 [B<-sm2-hex-id hex-string>]
54
55 =head1 DESCRIPTION
56
57 The B<req> command primarily creates and processes certificate requests
58 in PKCS#10 format. It can additionally create self signed certificates
59 for use as root CAs for example.
60
61 =head1 OPTIONS
62
63 =over 4
64
65 =item B<-help>
66
67 Print out a usage message.
68
69 =item B<-inform DER|PEM>
70
71 This specifies the input format. The B<DER> option uses an ASN1 DER encoded
72 form compatible with the PKCS#10. The B<PEM> form is the default format: it
73 consists of the B<DER> format base64 encoded with additional header and
74 footer lines.
75
76 =item B<-outform DER|PEM>
77
78 This specifies the output format, the options have the same meaning and default
79 as the B<-inform> option.
80
81 =item B<-in filename>
82
83 This specifies the input filename to read a request from or standard input
84 if this option is not specified. A request is only read if the creation
85 options (B<-new> and B<-newkey>) are not specified.
86
87 =item B<-sigopt nm:v>
88
89 Pass options to the signature algorithm during sign or verify operations.
90 Names and values of these options are algorithm-specific.
91
92 =item B<-passin arg>
93
94 The input file password source. For more information about the format of B<arg>
95 see the B<PASS PHRASE ARGUMENTS> section in L<openssl(1)>.
96
97 =item B<-out filename>
98
99 This specifies the output filename to write to or standard output by
100 default.
101
102 =item B<-passout arg>
103
104 The output file password source. For more information about the format of B<arg>
105 see the B<PASS PHRASE ARGUMENTS> section in L<openssl(1)>.
106
107 =item B<-text>
108
109 Prints out the certificate request in text form.
110
111 =item B<-subject>
112
113 Prints out the request subject (or certificate subject if B<-x509> is
114 specified)
115
116 =item B<-pubkey>
117
118 Outputs the public key.
119
120 =item B<-noout>
121
122 This option prevents output of the encoded version of the request.
123
124 =item B<-modulus>
125
126 This option prints out the value of the modulus of the public key
127 contained in the request.
128
129 =item B<-verify>
130
131 Verifies the signature on the request.
132
133 =item B<-new>
134
135 This option generates a new certificate request. It will prompt
136 the user for the relevant field values. The actual fields
137 prompted for and their maximum and minimum sizes are specified
138 in the configuration file and any requested extensions.
139
140 If the B<-key> option is not used it will generate a new RSA private
141 key using information specified in the configuration file.
142
143 =item B<-rand file...>
144
145 A file or files containing random data used to seed the random number
146 generator.
147 Multiple files can be specified separated by an OS-dependent character.
148 The separator is B<;> for MS-Windows, B<,> for OpenVMS, and B<:> for
149 all others.
150
151 =item [B<-writerand file>]
152
153 Writes random data to the specified I<file> upon exit.
154 This can be used with a subsequent B<-rand> flag.
155
156 =item B<-newkey arg>
157
158 This option creates a new certificate request and a new private
159 key. The argument takes one of several forms. B<rsa:nbits>, where
160 B<nbits> is the number of bits, generates an RSA key B<nbits>
161 in size. If B<nbits> is omitted, i.e. B<-newkey rsa> specified,
162 the default key size, specified in the configuration file is used.
163
164 All other algorithms support the B<-newkey alg:file> form, where file may be
165 an algorithm parameter file, created by the B<genpkey -genparam> command
166 or and X.509 certificate for a key with appropriate algorithm.
167
168 B<param:file> generates a key using the parameter file or certificate B<file>,
169 the algorithm is determined by the parameters. B<algname:file> use algorithm
170 B<algname> and parameter file B<file>: the two algorithms must match or an
171 error occurs. B<algname> just uses algorithm B<algname>, and parameters,
172 if necessary should be specified via B<-pkeyopt> parameter.
173
174 B<dsa:filename> generates a DSA key using the parameters
175 in the file B<filename>. B<ec:filename> generates EC key (usable both with
176 ECDSA or ECDH algorithms), B<gost2001:filename> generates GOST R
177 34.10-2001 key (requires B<ccgost> engine configured in the configuration
178 file). If just B<gost2001> is specified a parameter set should be
179 specified by B<-pkeyopt paramset:X>
180
181
182 =item B<-pkeyopt opt:value>
183
184 Set the public key algorithm option B<opt> to B<value>. The precise set of
185 options supported depends on the public key algorithm used and its
186 implementation. See B<KEY GENERATION OPTIONS> in the B<genpkey> manual page
187 for more details.
188
189 =item B<-key filename>
190
191 This specifies the file to read the private key from. It also
192 accepts PKCS#8 format private keys for PEM format files.
193
194 =item B<-keyform PEM|DER>
195
196 The format of the private key file specified in the B<-key>
197 argument. PEM is the default.
198
199 =item B<-keyout filename>
200
201 This gives the filename to write the newly created private key to.
202 If this option is not specified then the filename present in the
203 configuration file is used.
204
205 =item B<-nodes>
206
207 If this option is specified then if a private key is created it
208 will not be encrypted.
209
210 =item B<-I<digest>>
211
212 This specifies the message digest to sign the request.
213 Any digest supported by the OpenSSL B<dgst> command can be used.
214 This overrides the digest algorithm specified in
215 the configuration file.
216
217 Some public key algorithms may override this choice. For instance, DSA
218 signatures always use SHA1, GOST R 34.10 signatures always use
219 GOST R 34.11-94 (B<-md_gost94>), Ed25519 and Ed448 never use any digest.
220
221 =item B<-config filename>
222
223 This allows an alternative configuration file to be specified.
224 Optional; for a description of the default value,
225 see L<openssl(1)/COMMAND SUMMARY>.
226
227 =item B<-subj arg>
228
229 Sets subject name for new request or supersedes the subject name
230 when processing a request.
231 The arg must be formatted as I</type0=value0/type1=value1/type2=...>.
232 Keyword characters may be escaped by \ (backslash), and whitespace is retained.
233 Empty values are permitted, but the corresponding type will not be included
234 in the request.
235
236 =item B<-multivalue-rdn>
237
238 This option causes the -subj argument to be interpreted with full
239 support for multivalued RDNs. Example:
240
241 I</DC=org/DC=OpenSSL/DC=users/UID=123456+CN=John Doe>
242
243 If -multi-rdn is not used then the UID value is I<123456+CN=John Doe>.
244
245 =item B<-x509>
246
247 This option outputs a self signed certificate instead of a certificate
248 request. This is typically used to generate a test certificate or
249 a self signed root CA. The extensions added to the certificate
250 (if any) are specified in the configuration file. Unless specified
251 using the B<set_serial> option, a large random number will be used for
252 the serial number.
253
254 If existing request is specified with the B<-in> option, it is converted
255 to the self signed certificate otherwise new request is created.
256
257 =item B<-days n>
258
259 When the B<-x509> option is being used this specifies the number of
260 days to certify the certificate for, otherwise it is ignored. B<n> should
261 be a positive integer. The default is 30 days.
262
263 =item B<-set_serial n>
264
265 Serial number to use when outputting a self signed certificate. This
266 may be specified as a decimal value or a hex value if preceded by B<0x>.
267
268 =item B<-addext ext>
269
270 Add a specific extension to the certificate (if the B<-x509> option is
271 present) or certificate request.  The argument must have the form of
272 a key=value pair as it would appear in a config file.
273
274 This option can be given multiple times.
275
276 =item B<-extensions section>
277
278 =item B<-reqexts section>
279
280 These options specify alternative sections to include certificate
281 extensions (if the B<-x509> option is present) or certificate
282 request extensions. This allows several different sections to
283 be used in the same configuration file to specify requests for
284 a variety of purposes.
285
286 =item B<-precert>
287
288 A poison extension will be added to the certificate, making it a
289 "pre-certificate" (see RFC6962). This can be submitted to Certificate
290 Transparency logs in order to obtain signed certificate timestamps (SCTs).
291 These SCTs can then be embedded into the pre-certificate as an extension, before
292 removing the poison and signing the certificate.
293
294 This implies the B<-new> flag.
295
296 =item B<-utf8>
297
298 This option causes field values to be interpreted as UTF8 strings, by
299 default they are interpreted as ASCII. This means that the field
300 values, whether prompted from a terminal or obtained from a
301 configuration file, must be valid UTF8 strings.
302
303 =item B<-nameopt option>
304
305 Option which determines how the subject or issuer names are displayed. The
306 B<option> argument can be a single option or multiple options separated by
307 commas.  Alternatively the B<-nameopt> switch may be used more than once to
308 set multiple options. See the L<x509(1)> manual page for details.
309
310 =item B<-reqopt>
311
312 Customise the output format used with B<-text>. The B<option> argument can be
313 a single option or multiple options separated by commas.
314
315 See discussion of the  B<-certopt> parameter in the L<x509(1)>
316 command.
317
318 =item B<-newhdr>
319
320 Adds the word B<NEW> to the PEM file header and footer lines on the outputted
321 request. Some software (Netscape certificate server) and some CAs need this.
322
323 =item B<-batch>
324
325 Non-interactive mode.
326
327 =item B<-verbose>
328
329 Print extra details about the operations being performed.
330
331 =item B<-engine id>
332
333 Specifying an engine (by its unique B<id> string) will cause B<req>
334 to attempt to obtain a functional reference to the specified engine,
335 thus initialising it if needed. The engine will then be set as the default
336 for all available algorithms.
337
338 =item B<-keygen_engine id>
339
340 Specifies an engine (by its unique B<id> string) which would be used
341 for key generation operations.
342
343 =item B<-sm2-id>
344
345 Specify the ID string to use when verifying an SM2 certificate request. The ID
346 string is required by the SM2 signature algorithm for signing and verification.
347
348 =item B<-sm2-hex-id>
349
350 Specify a binary ID string to use when signing or verifying using an SM2
351 certificate. The argument for this option is string of hexadecimal digits.
352
353 =back
354
355 =head1 CONFIGURATION FILE FORMAT
356
357 The configuration options are specified in the B<req> section of
358 the configuration file. As with all configuration files if no
359 value is specified in the specific section (i.e. B<req>) then
360 the initial unnamed or B<default> section is searched too.
361
362 The options available are described in detail below.
363
364 =over 4
365
366 =item B<input_password output_password>
367
368 The passwords for the input private key file (if present) and
369 the output private key file (if one will be created). The
370 command line options B<passin> and B<passout> override the
371 configuration file values.
372
373 =item B<default_bits>
374
375 Specifies the default key size in bits.
376
377 This option is used in conjunction with the B<-new> option to generate
378 a new key. It can be overridden by specifying an explicit key size in
379 the B<-newkey> option. The smallest accepted key size is 512 bits. If
380 no key size is specified then 2048 bits is used.
381
382 =item B<default_keyfile>
383
384 This is the default filename to write a private key to. If not
385 specified the key is written to standard output. This can be
386 overridden by the B<-keyout> option.
387
388 =item B<oid_file>
389
390 This specifies a file containing additional B<OBJECT IDENTIFIERS>.
391 Each line of the file should consist of the numerical form of the
392 object identifier followed by white space then the short name followed
393 by white space and finally the long name.
394
395 =item B<oid_section>
396
397 This specifies a section in the configuration file containing extra
398 object identifiers. Each line should consist of the short name of the
399 object identifier followed by B<=> and the numerical form. The short
400 and long names are the same when this option is used.
401
402 =item B<RANDFILE>
403
404 At startup the specified file is loaded into the random number generator,
405 and at exit 256 bytes will be written to it.
406 It is used for private key generation.
407
408 =item B<encrypt_key>
409
410 If this is set to B<no> then if a private key is generated it is
411 B<not> encrypted. This is equivalent to the B<-nodes> command line
412 option. For compatibility B<encrypt_rsa_key> is an equivalent option.
413
414 =item B<default_md>
415
416 This option specifies the digest algorithm to use. Any digest supported by the
417 OpenSSL B<dgst> command can be used. This option can be overridden on the
418 command line. Certain signing algorithms (i.e. Ed25519 and Ed448) will ignore
419 any digest that has been set.
420
421 =item B<string_mask>
422
423 This option masks out the use of certain string types in certain
424 fields. Most users will not need to change this option.
425
426 It can be set to several values B<default> which is also the default
427 option uses PrintableStrings, T61Strings and BMPStrings if the
428 B<pkix> value is used then only PrintableStrings and BMPStrings will
429 be used. This follows the PKIX recommendation in RFC2459. If the
430 B<utf8only> option is used then only UTF8Strings will be used: this
431 is the PKIX recommendation in RFC2459 after 2003. Finally the B<nombstr>
432 option just uses PrintableStrings and T61Strings: certain software has
433 problems with BMPStrings and UTF8Strings: in particular Netscape.
434
435 =item B<req_extensions>
436
437 This specifies the configuration file section containing a list of
438 extensions to add to the certificate request. It can be overridden
439 by the B<-reqexts> command line switch. See the
440 L<x509v3_config(5)> manual page for details of the
441 extension section format.
442
443 =item B<x509_extensions>
444
445 This specifies the configuration file section containing a list of
446 extensions to add to certificate generated when the B<-x509> switch
447 is used. It can be overridden by the B<-extensions> command line switch.
448
449 =item B<prompt>
450
451 If set to the value B<no> this disables prompting of certificate fields
452 and just takes values from the config file directly. It also changes the
453 expected format of the B<distinguished_name> and B<attributes> sections.
454
455 =item B<utf8>
456
457 If set to the value B<yes> then field values to be interpreted as UTF8
458 strings, by default they are interpreted as ASCII. This means that
459 the field values, whether prompted from a terminal or obtained from a
460 configuration file, must be valid UTF8 strings.
461
462 =item B<attributes>
463
464 This specifies the section containing any request attributes: its format
465 is the same as B<distinguished_name>. Typically these may contain the
466 challengePassword or unstructuredName types. They are currently ignored
467 by OpenSSL's request signing utilities but some CAs might want them.
468
469 =item B<distinguished_name>
470
471 This specifies the section containing the distinguished name fields to
472 prompt for when generating a certificate or certificate request. The format
473 is described in the next section.
474
475 =back
476
477 =head1 DISTINGUISHED NAME AND ATTRIBUTE SECTION FORMAT
478
479 There are two separate formats for the distinguished name and attribute
480 sections. If the B<prompt> option is set to B<no> then these sections
481 just consist of field names and values: for example,
482
483  CN=My Name
484  OU=My Organization
485  emailAddress=someone@somewhere.org
486
487 This allows external programs (e.g. GUI based) to generate a template file
488 with all the field names and values and just pass it to B<req>. An example
489 of this kind of configuration file is contained in the B<EXAMPLES> section.
490
491 Alternatively if the B<prompt> option is absent or not set to B<no> then the
492 file contains field prompting information. It consists of lines of the form:
493
494  fieldName="prompt"
495  fieldName_default="default field value"
496  fieldName_min= 2
497  fieldName_max= 4
498
499 "fieldName" is the field name being used, for example commonName (or CN).
500 The "prompt" string is used to ask the user to enter the relevant
501 details. If the user enters nothing then the default value is used if no
502 default value is present then the field is omitted. A field can
503 still be omitted if a default value is present if the user just
504 enters the '.' character.
505
506 The number of characters entered must be between the fieldName_min and
507 fieldName_max limits: there may be additional restrictions based
508 on the field being used (for example countryName can only ever be
509 two characters long and must fit in a PrintableString).
510
511 Some fields (such as organizationName) can be used more than once
512 in a DN. This presents a problem because configuration files will
513 not recognize the same name occurring twice. To avoid this problem
514 if the fieldName contains some characters followed by a full stop
515 they will be ignored. So for example a second organizationName can
516 be input by calling it "1.organizationName".
517
518 The actual permitted field names are any object identifier short or
519 long names. These are compiled into OpenSSL and include the usual
520 values such as commonName, countryName, localityName, organizationName,
521 organizationalUnitName, stateOrProvinceName. Additionally emailAddress
522 is included as well as name, surname, givenName, initials, and dnQualifier.
523
524 Additional object identifiers can be defined with the B<oid_file> or
525 B<oid_section> options in the configuration file. Any additional fields
526 will be treated as though they were a DirectoryString.
527
528
529 =head1 EXAMPLES
530
531 Examine and verify certificate request:
532
533  openssl req -in req.pem -text -verify -noout
534
535 Create a private key and then generate a certificate request from it:
536
537  openssl genrsa -out key.pem 2048
538  openssl req -new -key key.pem -out req.pem
539
540 The same but just using req:
541
542  openssl req -newkey rsa:2048 -keyout key.pem -out req.pem
543
544 Generate a self signed root certificate:
545
546  openssl req -x509 -newkey rsa:2048 -keyout key.pem -out req.pem
547
548 Create an SM2 private key and then generate a certificate request from it:
549
550  openssl ecparam -genkey -name SM2 -out sm2.key
551  openssl req -new -key sm2.key -out sm2.csr -sm3 -sigopt "sm2_id:1234567812345678"
552
553 Examine and verify an SM2 certificate request:
554
555  openssl req -verify -in sm2.csr -sm3 -sm2-id 1234567812345678
556
557 Example of a file pointed to by the B<oid_file> option:
558
559  1.2.3.4        shortName       A longer Name
560  1.2.3.6        otherName       Other longer Name
561
562 Example of a section pointed to by B<oid_section> making use of variable
563 expansion:
564
565  testoid1=1.2.3.5
566  testoid2=${testoid1}.6
567
568 Sample configuration file prompting for field values:
569
570  [ req ]
571  default_bits           = 2048
572  default_keyfile        = privkey.pem
573  distinguished_name     = req_distinguished_name
574  attributes             = req_attributes
575  req_extensions         = v3_ca
576
577  dirstring_type = nobmp
578
579  [ req_distinguished_name ]
580  countryName                    = Country Name (2 letter code)
581  countryName_default            = AU
582  countryName_min                = 2
583  countryName_max                = 2
584
585  localityName                   = Locality Name (eg, city)
586
587  organizationalUnitName         = Organizational Unit Name (eg, section)
588
589  commonName                     = Common Name (eg, YOUR name)
590  commonName_max                 = 64
591
592  emailAddress                   = Email Address
593  emailAddress_max               = 40
594
595  [ req_attributes ]
596  challengePassword              = A challenge password
597  challengePassword_min          = 4
598  challengePassword_max          = 20
599
600  [ v3_ca ]
601
602  subjectKeyIdentifier=hash
603  authorityKeyIdentifier=keyid:always,issuer:always
604  basicConstraints = critical, CA:true
605
606 Sample configuration containing all field values:
607
608
609  RANDFILE               = $ENV::HOME/.rnd
610
611  [ req ]
612  default_bits           = 2048
613  default_keyfile        = keyfile.pem
614  distinguished_name     = req_distinguished_name
615  attributes             = req_attributes
616  prompt                 = no
617  output_password        = mypass
618
619  [ req_distinguished_name ]
620  C                      = GB
621  ST                     = Test State or Province
622  L                      = Test Locality
623  O                      = Organization Name
624  OU                     = Organizational Unit Name
625  CN                     = Common Name
626  emailAddress           = test@email.address
627
628  [ req_attributes ]
629  challengePassword              = A challenge password
630
631 Example of giving the most common attributes (subject and extensions)
632 on the command line:
633
634  openssl req -new -subj "/C=GB/CN=foo" \
635                   -addext "subjectAltName = DNS:foo.co.uk" \
636                   -addext "certificatePolicies = 1.2.3.4" \
637                   -newkey rsa:2048 -keyout key.pem -out req.pem
638
639
640 =head1 NOTES
641
642 The header and footer lines in the B<PEM> format are normally:
643
644  -----BEGIN CERTIFICATE REQUEST-----
645  -----END CERTIFICATE REQUEST-----
646
647 some software (some versions of Netscape certificate server) instead needs:
648
649  -----BEGIN NEW CERTIFICATE REQUEST-----
650  -----END NEW CERTIFICATE REQUEST-----
651
652 which is produced with the B<-newhdr> option but is otherwise compatible.
653 Either form is accepted transparently on input.
654
655 The certificate requests generated by B<Xenroll> with MSIE have extensions
656 added. It includes the B<keyUsage> extension which determines the type of
657 key (signature only or general purpose) and any additional OIDs entered
658 by the script in an extendedKeyUsage extension.
659
660 =head1 DIAGNOSTICS
661
662 The following messages are frequently asked about:
663
664         Using configuration from /some/path/openssl.cnf
665         Unable to load config info
666
667 This is followed some time later by...
668
669         unable to find 'distinguished_name' in config
670         problems making Certificate Request
671
672 The first error message is the clue: it can't find the configuration
673 file! Certain operations (like examining a certificate request) don't
674 need a configuration file so its use isn't enforced. Generation of
675 certificates or requests however does need a configuration file. This
676 could be regarded as a bug.
677
678 Another puzzling message is this:
679
680         Attributes:
681             a0:00
682
683 this is displayed when no attributes are present and the request includes
684 the correct empty B<SET OF> structure (the DER encoding of which is 0xa0
685 0x00). If you just see:
686
687         Attributes:
688
689 then the B<SET OF> is missing and the encoding is technically invalid (but
690 it is tolerated). See the description of the command line option B<-asn1-kludge>
691 for more information.
692
693 =head1 BUGS
694
695 OpenSSL's handling of T61Strings (aka TeletexStrings) is broken: it effectively
696 treats them as ISO-8859-1 (Latin 1), Netscape and MSIE have similar behaviour.
697 This can cause problems if you need characters that aren't available in
698 PrintableStrings and you don't want to or can't use BMPStrings.
699
700 As a consequence of the T61String handling the only correct way to represent
701 accented characters in OpenSSL is to use a BMPString: unfortunately Netscape
702 currently chokes on these. If you have to use accented characters with Netscape
703 and MSIE then you currently need to use the invalid T61String form.
704
705 The current prompting is not very friendly. It doesn't allow you to confirm what
706 you've just entered. Other things like extensions in certificate requests are
707 statically defined in the configuration file. Some of these: like an email
708 address in subjectAltName should be input by the user.
709
710 =head1 SEE ALSO
711
712 L<openssl(1)>,
713 L<openssl-x509(1)>,
714 L<openssl-ca(1)>,
715 L<openssl-genrsa(1)>,
716 L<openssl-gendsa(1)>,
717 L<config(5)>,
718 L<x509v3_config(5)>
719
720 =head1 COPYRIGHT
721
722 Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
723
724 Licensed under the Apache License 2.0 (the "License").  You may not use
725 this file except in compliance with the License.  You can obtain a copy
726 in the file LICENSE in the source distribution or at
727 L<https://www.openssl.org/source/license.html>.
728
729 =cut