cms.pod revision 325337
1=pod
2
3=head1 NAME
4
5openssl-cms,
6cms - CMS utility
7
8=head1 SYNOPSIS
9
10B<openssl> B<cms>
11[B<-encrypt>]
12[B<-decrypt>]
13[B<-sign>]
14[B<-verify>]
15[B<-cmsout>]
16[B<-resign>]
17[B<-data_create>]
18[B<-data_out>]
19[B<-digest_create>]
20[B<-digest_verify>]
21[B<-compress>]
22[B<-uncompress>]
23[B<-EncryptedData_encrypt>]
24[B<-sign_receipt>]
25[B<-verify_receipt receipt>]
26[B<-in filename>]
27[B<-inform SMIME|PEM|DER>]
28[B<-rctform SMIME|PEM|DER>]
29[B<-out filename>]
30[B<-outform SMIME|PEM|DER>]
31[B<-stream -indef -noindef>]
32[B<-noindef>]
33[B<-content filename>]
34[B<-text>]
35[B<-noout>]
36[B<-print>]
37[B<-CAfile file>]
38[B<-CApath dir>]
39[B<-no_alt_chains>]
40[B<-md digest>]
41[B<-[cipher]>]
42[B<-nointern>]
43[B<-no_signer_cert_verify>]
44[B<-nocerts>]
45[B<-noattr>]
46[B<-nosmimecap>]
47[B<-binary>]
48[B<-nodetach>]
49[B<-certfile file>]
50[B<-certsout file>]
51[B<-signer file>]
52[B<-recip file>]
53[B<-keyid>]
54[B<-receipt_request_all -receipt_request_first>]
55[B<-receipt_request_from emailaddress>]
56[B<-receipt_request_to emailaddress>]
57[B<-receipt_request_print>]
58[B<-secretkey key>]
59[B<-secretkeyid id>]
60[B<-econtent_type type>]
61[B<-inkey file>]
62[B<-keyopt name:parameter>]
63[B<-passin arg>]
64[B<-rand file(s)>]
65[B<cert.pem...>]
66[B<-to addr>]
67[B<-from addr>]
68[B<-subject subj>]
69[cert.pem]...
70
71=head1 DESCRIPTION
72
73The B<cms> command handles S/MIME v3.1 mail. It can encrypt, decrypt, sign and
74verify, compress and uncompress S/MIME messages.
75
76=head1 COMMAND OPTIONS
77
78There are fourteen operation options that set the type of operation to be
79performed. The meaning of the other options varies according to the operation
80type.
81
82=over 4
83
84=item B<-encrypt>
85
86encrypt mail for the given recipient certificates. Input file is the message
87to be encrypted. The output file is the encrypted mail in MIME format. The
88actual CMS type is <B>EnvelopedData<B>.
89
90Note that no revocation check is done for the recipient cert, so if that
91key has been compromised, others may be able to decrypt the text.
92
93=item B<-decrypt>
94
95decrypt mail using the supplied certificate and private key. Expects an
96encrypted mail message in MIME format for the input file. The decrypted mail
97is written to the output file.
98
99=item B<-debug_decrypt>
100
101this option sets the B<CMS_DEBUG_DECRYPT> flag. This option should be used
102with caution: see the notes section below.
103
104=item B<-sign>
105
106sign mail using the supplied certificate and private key. Input file is
107the message to be signed. The signed message in MIME format is written
108to the output file.
109
110=item B<-verify>
111
112verify signed mail. Expects a signed mail message on input and outputs
113the signed data. Both clear text and opaque signing is supported.
114
115=item B<-cmsout>
116
117takes an input message and writes out a PEM encoded CMS structure.
118
119=item B<-resign>
120
121resign a message: take an existing message and one or more new signers.
122
123=item B<-data_create>
124
125Create a CMS B<Data> type.
126
127=item B<-data_out>
128
129B<Data> type and output the content.
130
131=item B<-digest_create>
132
133Create a CMS B<DigestedData> type.
134
135=item B<-digest_verify>
136
137Verify a CMS B<DigestedData> type and output the content.
138
139=item B<-compress>
140
141Create a CMS B<CompressedData> type. OpenSSL must be compiled with B<zlib>
142support for this option to work, otherwise it will output an error.
143
144=item B<-uncompress>
145
146Uncompress a CMS B<CompressedData> type and output the content. OpenSSL must be
147compiled with B<zlib> support for this option to work, otherwise it will
148output an error.
149
150=item B<-EncryptedData_encrypt>
151
152Encrypt content using supplied symmetric key and algorithm using a CMS
153B<EncrytedData> type and output the content.
154
155=item B<-sign_receipt>
156
157Generate and output a signed receipt for the supplied message. The input 
158message B<must> contain a signed receipt request. Functionality is otherwise
159similar to the B<-sign> operation.
160
161=item B<-verify_receipt receipt>
162
163Verify a signed receipt in filename B<receipt>. The input message B<must> 
164contain the original receipt request. Functionality is otherwise similar
165to the B<-verify> operation.
166
167=item B<-in filename>
168
169the input message to be encrypted or signed or the message to be decrypted
170or verified.
171
172=item B<-inform SMIME|PEM|DER>
173
174this specifies the input format for the CMS structure. The default
175is B<SMIME> which reads an S/MIME format message. B<PEM> and B<DER>
176format change this to expect PEM and DER format CMS structures
177instead. This currently only affects the input format of the CMS
178structure, if no CMS structure is being input (for example with
179B<-encrypt> or B<-sign>) this option has no effect.
180
181=item B<-rctform SMIME|PEM|DER>
182
183specify the format for a signed receipt for use with the B<-receipt_verify>
184operation.
185
186=item B<-out filename>
187
188the message text that has been decrypted or verified or the output MIME
189format message that has been signed or verified.
190
191=item B<-outform SMIME|PEM|DER>
192
193this specifies the output format for the CMS structure. The default
194is B<SMIME> which writes an S/MIME format message. B<PEM> and B<DER>
195format change this to write PEM and DER format CMS structures
196instead. This currently only affects the output format of the CMS
197structure, if no CMS structure is being output (for example with
198B<-verify> or B<-decrypt>) this option has no effect.
199
200=item B<-stream -indef -noindef>
201
202the B<-stream> and B<-indef> options are equivalent and enable streaming I/O
203for encoding operations. This permits single pass processing of data without
204the need to hold the entire contents in memory, potentially supporting very
205large files. Streaming is automatically set for S/MIME signing with detached
206data if the output format is B<SMIME> it is currently off by default for all
207other operations.
208
209=item B<-noindef>
210
211disable streaming I/O where it would produce and indefinite length constructed
212encoding. This option currently has no effect. In future streaming will be
213enabled by default on all relevant operations and this option will disable it.
214
215=item B<-content filename>
216
217This specifies a file containing the detached content, this is only
218useful with the B<-verify> command. This is only usable if the CMS
219structure is using the detached signature form where the content is
220not included. This option will override any content if the input format
221is S/MIME and it uses the multipart/signed MIME content type.
222
223=item B<-text>
224
225this option adds plain text (text/plain) MIME headers to the supplied
226message if encrypting or signing. If decrypting or verifying it strips
227off text headers: if the decrypted or verified message is not of MIME 
228type text/plain then an error occurs.
229
230=item B<-noout>
231
232for the B<-cmsout> operation do not output the parsed CMS structure. This
233is useful when combined with the B<-print> option or if the syntax of the CMS
234structure is being checked.
235
236=item B<-print>
237
238for the B<-cmsout> operation print out all fields of the CMS structure. This
239is mainly useful for testing purposes.
240
241=item B<-CAfile file>
242
243a file containing trusted CA certificates, only used with B<-verify>.
244
245=item B<-CApath dir>
246
247a directory containing trusted CA certificates, only used with
248B<-verify>. This directory must be a standard certificate directory: that
249is a hash of each subject name (using B<x509 -hash>) should be linked
250to each certificate.
251
252=item B<-md digest>
253
254digest algorithm to use when signing or resigning. If not present then the
255default digest algorithm for the signing key will be used (usually SHA1).
256
257=item B<-[cipher]>
258
259the encryption algorithm to use. For example triple DES (168 bits) - B<-des3>
260or 256 bit AES - B<-aes256>. Any standard algorithm name (as used by the
261EVP_get_cipherbyname() function) can also be used preceded by a dash, for 
262example B<-aes_128_cbc>. See L<B<enc>|enc(1)> for a list of ciphers
263supported by your version of OpenSSL.
264
265If not specified triple DES is used. Only used with B<-encrypt> and 
266B<-EncryptedData_create> commands.
267
268=item B<-nointern>
269
270when verifying a message normally certificates (if any) included in
271the message are searched for the signing certificate. With this option
272only the certificates specified in the B<-certfile> option are used.
273The supplied certificates can still be used as untrusted CAs however.
274
275=item B<-no_signer_cert_verify>
276
277do not verify the signers certificate of a signed message.
278
279=item B<-nocerts>
280
281when signing a message the signer's certificate is normally included
282with this option it is excluded. This will reduce the size of the
283signed message but the verifier must have a copy of the signers certificate
284available locally (passed using the B<-certfile> option for example).
285
286=item B<-noattr>
287
288normally when a message is signed a set of attributes are included which
289include the signing time and supported symmetric algorithms. With this
290option they are not included.
291
292=item B<-nosmimecap>
293
294exclude the list of supported algorithms from signed attributes, other options
295such as signing time and content type are still included.
296
297=item B<-binary>
298
299normally the input message is converted to "canonical" format which is
300effectively using CR and LF as end of line: as required by the S/MIME
301specification. When this option is present no translation occurs. This
302is useful when handling binary data which may not be in MIME format.
303
304=item B<-nodetach>
305
306when signing a message use opaque signing: this form is more resistant
307to translation by mail relays but it cannot be read by mail agents that
308do not support S/MIME.  Without this option cleartext signing with
309the MIME type multipart/signed is used.
310
311=item B<-certfile file>
312
313allows additional certificates to be specified. When signing these will
314be included with the message. When verifying these will be searched for
315the signers certificates. The certificates should be in PEM format.
316
317=item B<-certsout file>
318
319any certificates contained in the message are written to B<file>.
320
321=item B<-signer file>
322
323a signing certificate when signing or resigning a message, this option can be
324used multiple times if more than one signer is required. If a message is being
325verified then the signers certificates will be written to this file if the
326verification was successful.
327
328=item B<-recip file>
329
330when decrypting a message this specifies the recipients certificate. The
331certificate must match one of the recipients of the message or an error
332occurs.
333
334When encrypting a message this option may be used multiple times to specify
335each recipient. This form B<must> be used if customised parameters are
336required (for example to specify RSA-OAEP).
337
338=item B<-keyid>
339
340use subject key identifier to identify certificates instead of issuer name and
341serial number. The supplied certificate B<must> include a subject key
342identifier extension. Supported by B<-sign> and B<-encrypt> options.
343
344=item B<-receipt_request_all -receipt_request_first>
345
346for B<-sign> option include a signed receipt request. Indicate requests should
347be provided by all receipient or first tier recipients (those mailed directly
348and not from a mailing list). Ignored it B<-receipt_request_from> is included.
349
350=item B<-receipt_request_from emailaddress>
351
352for B<-sign> option include a signed receipt request. Add an explicit email
353address where receipts should be supplied.
354
355=item B<-receipt_request_to emailaddress>
356
357Add an explicit email address where signed receipts should be sent to. This 
358option B<must> but supplied if a signed receipt it requested.
359
360=item B<-receipt_request_print>
361
362For the B<-verify> operation print out the contents of any signed receipt
363requests.
364
365=item B<-secretkey key>
366
367specify symmetric key to use. The key must be supplied in hex format and be
368consistent with the algorithm used. Supported by the B<-EncryptedData_encrypt>
369B<-EncrryptedData_decrypt>, B<-encrypt> and B<-decrypt> options. When used
370with B<-encrypt> or B<-decrypt> the supplied key is used to wrap or unwrap the
371content encryption key using an AES key in the B<KEKRecipientInfo> type.
372
373=item B<-secretkeyid id>
374
375the key identifier for the supplied symmetric key for B<KEKRecipientInfo> type.
376This option B<must> be present if the B<-secretkey> option is used with
377B<-encrypt>. With B<-decrypt> operations the B<id> is used to locate the
378relevant key if it is not supplied then an attempt is used to decrypt any
379B<KEKRecipientInfo> structures.
380
381=item B<-econtent_type type>
382
383set the encapsulated content type to B<type> if not supplied the B<Data> type
384is used. The B<type> argument can be any valid OID name in either text or
385numerical format. 
386
387=item B<-inkey file>
388
389the private key to use when signing or decrypting. This must match the
390corresponding certificate. If this option is not specified then the
391private key must be included in the certificate file specified with
392the B<-recip> or B<-signer> file. When signing this option can be used
393multiple times to specify successive keys.
394
395=item B<-keyopt name:opt>
396
397for signing and encryption this option can be used multiple times to
398set customised parameters for the preceding key or certificate. It can
399currently be used to set RSA-PSS for signing, RSA-OAEP for encryption
400or to modify default parameters for ECDH.
401
402=item B<-passin arg>
403
404the private key password source. For more information about the format of B<arg>
405see the B<PASS PHRASE ARGUMENTS> section in L<openssl(1)|openssl(1)>.
406
407=item B<-rand file(s)>
408
409a file or files containing random data used to seed the random number
410generator, or an EGD socket (see L<RAND_egd(3)|RAND_egd(3)>).
411Multiple files can be specified separated by a OS-dependent character.
412The separator is B<;> for MS-Windows, B<,> for OpenVMS, and B<:> for
413all others.
414
415=item B<cert.pem...>
416
417one or more certificates of message recipients: used when encrypting
418a message. 
419
420=item B<-to, -from, -subject>
421
422the relevant mail headers. These are included outside the signed
423portion of a message so they may be included manually. If signing
424then many S/MIME mail clients check the signers certificate's email
425address matches that specified in the From: address.
426
427=item B<-purpose, -ignore_critical, -issuer_checks, -crl_check, -crl_check_all, -policy_check, -extended_crl, -x509_strict, -policy -check_ss_sig -no_alt_chains>
428
429Set various certificate chain valiadition option. See the
430L<B<verify>|verify(1)> manual page for details.
431
432=back
433
434=head1 NOTES
435
436The MIME message must be sent without any blank lines between the
437headers and the output. Some mail programs will automatically add
438a blank line. Piping the mail directly to sendmail is one way to
439achieve the correct format.
440
441The supplied message to be signed or encrypted must include the
442necessary MIME headers or many S/MIME clients wont display it
443properly (if at all). You can use the B<-text> option to automatically
444add plain text headers.
445
446A "signed and encrypted" message is one where a signed message is
447then encrypted. This can be produced by encrypting an already signed
448message: see the examples section.
449
450This version of the program only allows one signer per message but it
451will verify multiple signers on received messages. Some S/MIME clients
452choke if a message contains multiple signers. It is possible to sign
453messages "in parallel" by signing an already signed message.
454
455The options B<-encrypt> and B<-decrypt> reflect common usage in S/MIME
456clients. Strictly speaking these process CMS enveloped data: CMS
457encrypted data is used for other purposes.
458
459The B<-resign> option uses an existing message digest when adding a new
460signer. This means that attributes must be present in at least one existing
461signer using the same message digest or this operation will fail.
462
463The B<-stream> and B<-indef> options enable experimental streaming I/O support.
464As a result the encoding is BER using indefinite length constructed encoding
465and no longer DER. Streaming is supported for the B<-encrypt> operation and the
466B<-sign> operation if the content is not detached.
467
468Streaming is always used for the B<-sign> operation with detached data but
469since the content is no longer part of the CMS structure the encoding
470remains DER.
471
472If the B<-decrypt> option is used without a recipient certificate then an
473attempt is made to locate the recipient by trying each potential recipient
474in turn using the supplied private key. To thwart the MMA attack
475(Bleichenbacher's attack on PKCS #1 v1.5 RSA padding) all recipients are
476tried whether they succeed or not and if no recipients match the message
477is "decrypted" using a random key which will typically output garbage. 
478The B<-debug_decrypt> option can be used to disable the MMA attack protection
479and return an error if no recipient can be found: this option should be used
480with caution. For a fuller description see L<CMS_decrypt(3)|CMS_decrypt(3)>).
481
482=head1 EXIT CODES
483
484=over 4
485
486=item Z<>0
487
488the operation was completely successfully.
489
490=item Z<>1
491
492an error occurred parsing the command options.
493
494=item Z<>2
495
496one of the input files could not be read.
497
498=item Z<>3
499
500an error occurred creating the CMS file or when reading the MIME
501message.
502
503=item Z<>4
504
505an error occurred decrypting or verifying the message.
506
507=item Z<>5
508
509the message was verified correctly but an error occurred writing out
510the signers certificates.
511
512=back
513
514=head1 COMPATIBILITY WITH PKCS#7 format.
515
516The B<smime> utility can only process the older B<PKCS#7> format. The B<cms>
517utility supports Cryptographic Message Syntax format. Use of some features
518will result in messages which cannot be processed by applications which only
519support the older format. These are detailed below.
520
521The use of the B<-keyid> option with B<-sign> or B<-encrypt>.
522
523The B<-outform PEM> option uses different headers.
524
525The B<-compress> option.
526
527The B<-secretkey> option when used with B<-encrypt>.
528
529The use of PSS with B<-sign>.
530
531The use of OAEP or non-RSA keys with B<-encrypt>.
532
533Additionally the B<-EncryptedData_create> and B<-data_create> type cannot
534be processed by the older B<smime> command.
535
536=head1 EXAMPLES
537
538Create a cleartext signed message:
539
540 openssl cms -sign -in message.txt -text -out mail.msg \
541	-signer mycert.pem
542
543Create an opaque signed message
544
545 openssl cms -sign -in message.txt -text -out mail.msg -nodetach \
546	-signer mycert.pem
547
548Create a signed message, include some additional certificates and
549read the private key from another file:
550
551 openssl cms -sign -in in.txt -text -out mail.msg \
552	-signer mycert.pem -inkey mykey.pem -certfile mycerts.pem
553
554Create a signed message with two signers, use key identifier:
555
556 openssl cms -sign -in message.txt -text -out mail.msg \
557	-signer mycert.pem -signer othercert.pem -keyid
558
559Send a signed message under Unix directly to sendmail, including headers:
560
561 openssl cms -sign -in in.txt -text -signer mycert.pem \
562	-from steve@openssl.org -to someone@somewhere \
563	-subject "Signed message" | sendmail someone@somewhere
564
565Verify a message and extract the signer's certificate if successful:
566
567 openssl cms -verify -in mail.msg -signer user.pem -out signedtext.txt
568
569Send encrypted mail using triple DES:
570
571 openssl cms -encrypt -in in.txt -from steve@openssl.org \
572	-to someone@somewhere -subject "Encrypted message" \
573	-des3 user.pem -out mail.msg
574
575Sign and encrypt mail:
576
577 openssl cms -sign -in ml.txt -signer my.pem -text \
578	| openssl cms -encrypt -out mail.msg \
579	-from steve@openssl.org -to someone@somewhere \
580	-subject "Signed and Encrypted message" -des3 user.pem
581
582Note: the encryption command does not include the B<-text> option because the
583message being encrypted already has MIME headers.
584
585Decrypt mail:
586
587 openssl cms -decrypt -in mail.msg -recip mycert.pem -inkey key.pem
588
589The output from Netscape form signing is a PKCS#7 structure with the
590detached signature format. You can use this program to verify the
591signature by line wrapping the base64 encoded structure and surrounding
592it with:
593
594 -----BEGIN PKCS7-----
595 -----END PKCS7-----
596
597and using the command, 
598
599 openssl cms -verify -inform PEM -in signature.pem -content content.txt
600
601alternatively you can base64 decode the signature and use
602
603 openssl cms -verify -inform DER -in signature.der -content content.txt
604
605Create an encrypted message using 128 bit Camellia:
606
607 openssl cms -encrypt -in plain.txt -camellia128 -out mail.msg cert.pem
608
609Add a signer to an existing message:
610
611 openssl cms -resign -in mail.msg -signer newsign.pem -out mail2.msg
612
613Sign mail using RSA-PSS:
614
615 openssl cms -sign -in message.txt -text -out mail.msg \
616	-signer mycert.pem -keyopt rsa_padding_mode:pss
617
618Create encrypted mail using RSA-OAEP:
619
620 openssl cms -encrypt -in plain.txt -out mail.msg \
621	-recip cert.pem -keyopt rsa_padding_mode:oaep
622
623Use SHA256 KDF with an ECDH certificate:
624
625 openssl cms -encrypt -in plain.txt -out mail.msg \
626	-recip ecdhcert.pem -keyopt ecdh_kdf_md:sha256
627
628=head1 BUGS
629
630The MIME parser isn't very clever: it seems to handle most messages that I've
631thrown at it but it may choke on others.
632
633The code currently will only write out the signer's certificate to a file: if
634the signer has a separate encryption certificate this must be manually
635extracted. There should be some heuristic that determines the correct
636encryption certificate.
637
638Ideally a database should be maintained of a certificates for each email
639address.
640
641The code doesn't currently take note of the permitted symmetric encryption
642algorithms as supplied in the SMIMECapabilities signed attribute. this means the
643user has to manually include the correct encryption algorithm. It should store
644the list of permitted ciphers in a database and only use those.
645
646No revocation checking is done on the signer's certificate.
647
648=head1 HISTORY
649
650The use of multiple B<-signer> options and the B<-resign> command were first
651added in OpenSSL 1.0.0
652
653The B<keyopt> option was first added in OpenSSL 1.1.0
654
655The use of B<-recip> to specify the recipient when encrypting mail was first
656added to OpenSSL 1.1.0
657
658Support for RSA-OAEP and RSA-PSS was first added to OpenSSL 1.1.0. 
659
660The use of non-RSA keys with B<-encrypt> and B<-decrypt> was first added
661to OpenSSL 1.1.0.
662
663The -no_alt_chains options was first added to OpenSSL 1.0.2b.
664
665=cut
666