60 #ifndef OPENSSL_NO_CMS
71 static int save_certs(
char *signerfile,
STACK_OF(
X509) *signers);
80 #define SMIME_SIGNERS 0x40
81 #define SMIME_ENCRYPT (1 | SMIME_OP)
82 #define SMIME_DECRYPT (2 | SMIME_IP)
83 #define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
84 #define SMIME_VERIFY (4 | SMIME_IP)
85 #define SMIME_CMSOUT (5 | SMIME_IP | SMIME_OP)
86 #define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
87 #define SMIME_DATAOUT (7 | SMIME_IP)
88 #define SMIME_DATA_CREATE (8 | SMIME_OP)
89 #define SMIME_DIGEST_VERIFY (9 | SMIME_IP)
90 #define SMIME_DIGEST_CREATE (10 | SMIME_OP)
91 #define SMIME_UNCOMPRESS (11 | SMIME_IP)
92 #define SMIME_COMPRESS (12 | SMIME_OP)
93 #define SMIME_ENCRYPTED_DECRYPT (13 | SMIME_IP)
94 #define SMIME_ENCRYPTED_ENCRYPT (14 | SMIME_OP)
95 #define SMIME_SIGN_RECEIPT (15 | SMIME_IP | SMIME_OP)
96 #define SMIME_VERIFY_RECEIPT (16 | SMIME_IP)
100 int MAIN(
int,
char **);
102 int MAIN(
int argc,
char **argv)
108 const char *inmode =
"r", *outmode =
"w";
109 char *infile = NULL, *outfile = NULL, *rctfile = NULL;
110 char *signerfile = NULL, *recipfile = NULL;
112 char *certfile = NULL, *keyfile = NULL, *contfile=NULL;
113 char *certsoutfile = NULL;
117 X509 *cert = NULL, *recip = NULL, *signer = NULL;
120 BIO *
in = NULL, *
out = NULL, *indata = NULL, *rctin = NULL;
123 int verify_retcode = 0;
124 int rr_print = 0, rr_allorfirst = -1;
127 char *to = NULL, *from = NULL, *subject = NULL;
128 char *CAfile = NULL, *CApath = NULL;
129 char *passargin = NULL, *passin = NULL;
132 const EVP_MD *sign_md = NULL;
135 #ifndef OPENSSL_NO_ENGINE
138 unsigned char *secret_key = NULL, *secret_keyid = NULL;
139 unsigned char *pwri_pass = NULL, *pwri_tmp = NULL;
140 size_t secret_keylen = 0, secret_keyidlen = 0;
160 while (!badarg && *args && *args[0] ==
'-')
162 if (!strcmp (*args,
"-encrypt"))
164 else if (!strcmp (*args,
"-decrypt"))
166 else if (!strcmp (*args,
"-sign"))
168 else if (!strcmp (*args,
"-sign_receipt"))
170 else if (!strcmp (*args,
"-resign"))
172 else if (!strcmp (*args,
"-verify"))
174 else if (!strcmp (*args,
"-verify_retcode"))
176 else if (!strcmp(*args,
"-verify_receipt"))
184 else if (!strcmp (*args,
"-cmsout"))
186 else if (!strcmp (*args,
"-data_out"))
188 else if (!strcmp (*args,
"-data_create"))
190 else if (!strcmp (*args,
"-digest_verify"))
192 else if (!strcmp (*args,
"-digest_create"))
194 else if (!strcmp (*args,
"-compress"))
196 else if (!strcmp (*args,
"-uncompress"))
198 else if (!strcmp (*args,
"-EncryptedData_decrypt"))
200 else if (!strcmp (*args,
"-EncryptedData_encrypt"))
202 #ifndef OPENSSL_NO_DES
203 else if (!strcmp (*args,
"-des3"))
205 else if (!strcmp (*args,
"-des"))
208 #ifndef OPENSSL_NO_SEED
209 else if (!strcmp (*args,
"-seed"))
212 #ifndef OPENSSL_NO_RC2
213 else if (!strcmp (*args,
"-rc2-40"))
215 else if (!strcmp (*args,
"-rc2-128"))
217 else if (!strcmp (*args,
"-rc2-64"))
220 #ifndef OPENSSL_NO_AES
221 else if (!strcmp(*args,
"-aes128"))
223 else if (!strcmp(*args,
"-aes192"))
225 else if (!strcmp(*args,
"-aes256"))
228 #ifndef OPENSSL_NO_CAMELLIA
229 else if (!strcmp(*args,
"-camellia128"))
231 else if (!strcmp(*args,
"-camellia192"))
233 else if (!strcmp(*args,
"-camellia256"))
236 else if (!strcmp (*args,
"-text"))
238 else if (!strcmp (*args,
"-nointern"))
240 else if (!strcmp (*args,
"-noverify")
241 || !strcmp (*args,
"-no_signer_cert_verify"))
243 else if (!strcmp (*args,
"-nocerts"))
245 else if (!strcmp (*args,
"-noattr"))
247 else if (!strcmp (*args,
"-nodetach"))
249 else if (!strcmp (*args,
"-nosmimecap"))
251 else if (!strcmp (*args,
"-binary"))
253 else if (!strcmp (*args,
"-keyid"))
255 else if (!strcmp (*args,
"-nosigs"))
257 else if (!strcmp (*args,
"-no_content_verify"))
259 else if (!strcmp (*args,
"-no_attr_verify"))
261 else if (!strcmp (*args,
"-stream"))
263 else if (!strcmp (*args,
"-indef"))
265 else if (!strcmp (*args,
"-noindef"))
267 else if (!strcmp (*args,
"-nooldmime"))
269 else if (!strcmp (*args,
"-crlfeol"))
271 else if (!strcmp (*args,
"-noout"))
273 else if (!strcmp (*args,
"-receipt_request_print"))
275 else if (!strcmp (*args,
"-receipt_request_all"))
277 else if (!strcmp (*args,
"-receipt_request_first"))
279 else if (!strcmp(*args,
"-receipt_request_from"))
288 else if (!strcmp(*args,
"-receipt_request_to"))
297 else if (!strcmp (*args,
"-print"))
302 else if (!strcmp(*args,
"-secretkey"))
314 secret_keylen = (size_t)ltmp;
316 else if (!strcmp(*args,
"-secretkeyid"))
328 secret_keyidlen = (size_t)ltmp;
330 else if (!strcmp(*args,
"-pwri_password"))
335 pwri_pass = (
unsigned char *)*args;
337 else if (!strcmp(*args,
"-econtent_type"))
349 else if (!strcmp(*args,
"-rand"))
357 #ifndef OPENSSL_NO_ENGINE
358 else if (!strcmp(*args,
"-engine"))
365 else if (!strcmp(*args,
"-passin"))
371 else if (!strcmp (*args,
"-to"))
377 else if (!strcmp (*args,
"-from"))
383 else if (!strcmp (*args,
"-subject"))
389 else if (!strcmp (*args,
"-signer"))
401 keyfile = signerfile;
407 signerfile = *++args;
409 else if (!strcmp (*args,
"-recip"))
415 else if (!strcmp (*args,
"-certsout"))
419 certsoutfile = *++args;
421 else if (!strcmp (*args,
"-md"))
433 else if (!strcmp (*args,
"-inkey"))
455 else if (!strcmp (*args,
"-keyform"))
461 else if (!strcmp (*args,
"-rctform"))
467 else if (!strcmp (*args,
"-certfile"))
473 else if (!strcmp (*args,
"-CAfile"))
479 else if (!strcmp (*args,
"-CApath"))
485 else if (!strcmp (*args,
"-in"))
491 else if (!strcmp (*args,
"-inform"))
497 else if (!strcmp (*args,
"-outform"))
503 else if (!strcmp (*args,
"-out"))
509 else if (!strcmp (*args,
"-content"))
522 if (((rr_allorfirst != -1) || rr_from) && !rr_to)
533 if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners))
539 if (operation & SMIME_SIGNERS)
541 if (keyfile && !signerfile)
555 keyfile = signerfile;
570 if (!recipfile && !keyfile && !secret_key && !pwri_pass)
578 if (!*args && !secret_key && !pwri_pass)
598 #ifndef OPENSSL_NO_DES
602 #ifndef OPENSSL_NO_SEED
605 #ifndef OPENSSL_NO_RC2
610 #ifndef OPENSSL_NO_AES
614 #ifndef OPENSSL_NO_CAMELLIA
618 BIO_printf (
bio_err,
"-nointern don't search certificates in message for signer\n");
621 BIO_printf (
bio_err,
"-nocerts don't include signers certificate when signing\n");
632 BIO_printf (
bio_err,
"-inkey file input private key (if not signer or recipient)\n");
635 BIO_printf (
bio_err,
"-outform arg output format SMIME (default), PEM or DER\n");
636 BIO_printf (
bio_err,
"-content file supply or override content for detached signature\n");
643 BIO_printf (
bio_err,
"-crl_check check revocation status of signer's certificate using CRLs\n");
644 BIO_printf (
bio_err,
"-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
645 #ifndef OPENSSL_NO_ENGINE
656 #ifndef OPENSSL_NO_ENGINE
676 if (!(operation & SMIME_SIGNERS))
705 #ifndef OPENSSL_NO_DES
713 if (secret_key && !secret_keyid)
724 NULL, e,
"recipient certificate file")))
735 e,
"certificate file")))
745 e,
"recipient certificate file")))
755 e,
"receipt signer certificate file")))
770 keyfile = signerfile;
787 "Can't open input file %s\n", infile);
794 if (operation & SMIME_IP)
799 cms = PEM_read_bio_CMS(in, NULL, NULL, NULL);
825 allcerts = CMS_get1_certs(cms);
826 if (!save_certs(certsoutfile, allcerts))
829 "Error writing certs to %s\n",
840 char *rctmode = (rctformat ==
FORMAT_ASN1) ?
"rb" :
"r";
844 "Can't open receipt file %s\n", rctfile);
851 rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);
872 "Can't open output file %s\n", outfile);
879 #ifdef OPENSSL_SYS_VMS
920 secret_key, secret_keylen,
921 secret_keyid, secret_keyidlen,
930 pwri_tmp = (
unsigned char *)
BUF_strdup((
char *)pwri_pass);
948 secret_key, secret_keylen,
957 sis = CMS_get0_SignerInfos(cms);
964 CMS_ContentInfo_free(cms);
967 else if (operation & SMIME_SIGNERS)
982 cms =
CMS_sign(NULL, NULL, other, in, flags);
990 rr = make_receipt_request(rr_to, rr_allorfirst,
995 "Signed Receipt Request Creation Error\n");
1008 e,
"signer certificate");
1012 "signing key file");
1026 if ((operation ==
SMIME_SIGN) && !(flags & CMS_STREAM))
1046 secret_key, secret_keylen,
1047 secret_keyid, secret_keyidlen))
1050 "Error decrypting CMS using secret key\n");
1060 "Error decrypting CMS using private key\n");
1070 "Error decrypting CMS using password\n");
1075 if (!
CMS_decrypt(cms, NULL, NULL, indata, out, flags))
1104 indata, out, flags))
1109 if (
CMS_verify(cms, other, store, indata, out, flags) > 0)
1115 ret = verify_err + 32;
1121 signers = CMS_get0_signers(cms);
1122 if (!save_certs(signerfile, signers))
1125 "Error writing signers to %s\n",
1133 receipt_request_print(
bio_err, cms);
1151 CMS_ContentInfo_print_ctx(out, cms, 0, NULL);
1204 CMS_ReceiptRequest_free(rr);
1214 CMS_ContentInfo_free(cms);
1215 CMS_ContentInfo_free(rcms);
1224 static int save_certs(
char *signerfile,
STACK_OF(
X509) *signers)
1259 static void gnames_stack_print(
BIO *out,
STACK_OF(GENERAL_NAMES) *gns)
1284 STACK_OF(GENERAL_NAMES) *rto, *rlist;
1287 sis = CMS_get0_SignerInfos(cms);
1306 BIO_puts(out,
" Signed Content ID:\n");
1314 gnames_stack_print(out, rlist);
1316 else if (allorfirst == 1)
1318 else if (allorfirst == 0)
1321 BIO_printf(out,
" Unknown (%d)\n", allorfirst);
1323 gnames_stack_print(out, rto);
1326 CMS_ReceiptRequest_free(rr);
1334 GENERAL_NAMES *gens = NULL;
1345 gens = GENERAL_NAMES_new();
1362 GENERAL_NAMES_free(gens);
1364 GENERAL_NAME_free(gen);
1373 STACK_OF(GENERAL_NAMES) *rct_to, *rct_from;
1375 rct_to = make_names_stack(rr_to);
1380 rct_from = make_names_stack(rr_from);