Home | History | Annotate | Line # | Download | only in apps
      1      1.1  christos /*
      2      1.1  christos  * Copyright 2008-2025 The OpenSSL Project Authors. All Rights Reserved.
      3      1.1  christos  *
      4      1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5      1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      6      1.1  christos  * in the file LICENSE in the source distribution or at
      7      1.1  christos  * https://www.openssl.org/source/license.html
      8      1.1  christos  */
      9      1.1  christos 
     10      1.1  christos /* CMS utility function */
     11      1.1  christos 
     12      1.1  christos #include <stdio.h>
     13      1.1  christos #include <string.h>
     14      1.1  christos #include "apps.h"
     15      1.1  christos #include "progs.h"
     16      1.1  christos 
     17      1.1  christos #include <openssl/crypto.h>
     18      1.1  christos #include <openssl/pem.h>
     19      1.1  christos #include <openssl/err.h>
     20      1.1  christos #include <openssl/x509_vfy.h>
     21      1.1  christos #include <openssl/x509v3.h>
     22      1.1  christos #include <openssl/cms.h>
     23      1.1  christos 
     24      1.1  christos static int save_certs(char *signerfile, STACK_OF(X509) *signers);
     25      1.1  christos static int cms_cb(int ok, X509_STORE_CTX *ctx);
     26      1.1  christos static void receipt_request_print(CMS_ContentInfo *cms);
     27  1.1.1.2  christos static CMS_ReceiptRequest *make_receipt_request(STACK_OF(OPENSSL_STRING) *rr_to, int rr_allorfirst,
     28  1.1.1.2  christos     STACK_OF(OPENSSL_STRING) *rr_from);
     29      1.1  christos static int cms_set_pkey_param(EVP_PKEY_CTX *pctx,
     30  1.1.1.2  christos     STACK_OF(OPENSSL_STRING) *param);
     31      1.1  christos 
     32  1.1.1.2  christos #define SMIME_OP 0x100
     33  1.1.1.2  christos #define SMIME_IP 0x200
     34  1.1.1.2  christos #define SMIME_SIGNERS 0x400
     35  1.1.1.2  christos #define SMIME_ENCRYPT (1 | SMIME_OP)
     36  1.1.1.2  christos #define SMIME_DECRYPT (2 | SMIME_IP)
     37  1.1.1.2  christos #define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
     38  1.1.1.2  christos #define SMIME_VERIFY (4 | SMIME_IP)
     39  1.1.1.2  christos #define SMIME_RESIGN (5 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
     40  1.1.1.2  christos #define SMIME_SIGN_RECEIPT (6 | SMIME_IP | SMIME_OP)
     41  1.1.1.2  christos #define SMIME_VERIFY_RECEIPT (7 | SMIME_IP)
     42  1.1.1.2  christos #define SMIME_DIGEST_CREATE (8 | SMIME_OP)
     43  1.1.1.2  christos #define SMIME_DIGEST_VERIFY (9 | SMIME_IP)
     44  1.1.1.2  christos #define SMIME_COMPRESS (10 | SMIME_OP)
     45  1.1.1.2  christos #define SMIME_UNCOMPRESS (11 | SMIME_IP)
     46      1.1  christos #define SMIME_ENCRYPTED_ENCRYPT (12 | SMIME_OP)
     47      1.1  christos #define SMIME_ENCRYPTED_DECRYPT (13 | SMIME_IP)
     48  1.1.1.2  christos #define SMIME_DATA_CREATE (14 | SMIME_OP)
     49  1.1.1.2  christos #define SMIME_DATA_OUT (15 | SMIME_IP)
     50  1.1.1.2  christos #define SMIME_CMSOUT (16 | SMIME_IP | SMIME_OP)
     51      1.1  christos 
     52      1.1  christos static int verify_err = 0;
     53      1.1  christos 
     54      1.1  christos typedef struct cms_key_param_st cms_key_param;
     55      1.1  christos 
     56      1.1  christos struct cms_key_param_st {
     57      1.1  christos     int idx;
     58      1.1  christos     STACK_OF(OPENSSL_STRING) *param;
     59      1.1  christos     cms_key_param *next;
     60      1.1  christos };
     61      1.1  christos 
     62      1.1  christos typedef enum OPTION_choice {
     63      1.1  christos     OPT_COMMON,
     64  1.1.1.2  christos     OPT_INFORM,
     65  1.1.1.2  christos     OPT_OUTFORM,
     66  1.1.1.2  christos     OPT_IN,
     67  1.1.1.2  christos     OPT_OUT,
     68  1.1.1.2  christos     OPT_ENCRYPT,
     69  1.1.1.2  christos     OPT_DECRYPT,
     70  1.1.1.2  christos     OPT_SIGN,
     71  1.1.1.2  christos     OPT_CADES,
     72  1.1.1.2  christos     OPT_SIGN_RECEIPT,
     73  1.1.1.2  christos     OPT_RESIGN,
     74  1.1.1.2  christos     OPT_VERIFY,
     75  1.1.1.2  christos     OPT_VERIFY_RETCODE,
     76  1.1.1.2  christos     OPT_VERIFY_RECEIPT,
     77  1.1.1.2  christos     OPT_CMSOUT,
     78  1.1.1.2  christos     OPT_DATA_OUT,
     79  1.1.1.2  christos     OPT_DATA_CREATE,
     80  1.1.1.2  christos     OPT_DIGEST_VERIFY,
     81  1.1.1.2  christos     OPT_DIGEST,
     82  1.1.1.2  christos     OPT_DIGEST_CREATE,
     83  1.1.1.2  christos     OPT_COMPRESS,
     84  1.1.1.2  christos     OPT_UNCOMPRESS,
     85  1.1.1.2  christos     OPT_ED_DECRYPT,
     86  1.1.1.2  christos     OPT_ED_ENCRYPT,
     87  1.1.1.2  christos     OPT_DEBUG_DECRYPT,
     88  1.1.1.2  christos     OPT_TEXT,
     89  1.1.1.2  christos     OPT_ASCIICRLF,
     90  1.1.1.2  christos     OPT_NOINTERN,
     91  1.1.1.2  christos     OPT_NOVERIFY,
     92  1.1.1.2  christos     OPT_NOCERTS,
     93  1.1.1.2  christos     OPT_NOATTR,
     94  1.1.1.2  christos     OPT_NODETACH,
     95  1.1.1.2  christos     OPT_NOSMIMECAP,
     96  1.1.1.2  christos     OPT_NO_SIGNING_TIME,
     97  1.1.1.2  christos     OPT_BINARY,
     98  1.1.1.2  christos     OPT_KEYID,
     99  1.1.1.2  christos     OPT_NOSIGS,
    100  1.1.1.2  christos     OPT_NO_CONTENT_VERIFY,
    101  1.1.1.2  christos     OPT_NO_ATTR_VERIFY,
    102  1.1.1.2  christos     OPT_INDEF,
    103  1.1.1.2  christos     OPT_NOINDEF,
    104  1.1.1.2  christos     OPT_CRLFEOL,
    105  1.1.1.2  christos     OPT_NOOUT,
    106  1.1.1.2  christos     OPT_RR_PRINT,
    107  1.1.1.2  christos     OPT_RR_ALL,
    108  1.1.1.2  christos     OPT_RR_FIRST,
    109  1.1.1.2  christos     OPT_RCTFORM,
    110  1.1.1.2  christos     OPT_CERTFILE,
    111  1.1.1.2  christos     OPT_CAFILE,
    112  1.1.1.2  christos     OPT_CAPATH,
    113  1.1.1.2  christos     OPT_CASTORE,
    114  1.1.1.2  christos     OPT_NOCAPATH,
    115  1.1.1.2  christos     OPT_NOCAFILE,
    116  1.1.1.2  christos     OPT_NOCASTORE,
    117  1.1.1.2  christos     OPT_CONTENT,
    118  1.1.1.2  christos     OPT_PRINT,
    119  1.1.1.2  christos     OPT_NAMEOPT,
    120  1.1.1.2  christos     OPT_SECRETKEY,
    121  1.1.1.2  christos     OPT_SECRETKEYID,
    122  1.1.1.2  christos     OPT_PWRI_PASSWORD,
    123  1.1.1.2  christos     OPT_ECONTENT_TYPE,
    124  1.1.1.2  christos     OPT_PASSIN,
    125  1.1.1.2  christos     OPT_TO,
    126  1.1.1.2  christos     OPT_FROM,
    127  1.1.1.2  christos     OPT_SUBJECT,
    128  1.1.1.2  christos     OPT_SIGNER,
    129  1.1.1.2  christos     OPT_RECIP,
    130  1.1.1.2  christos     OPT_CERTSOUT,
    131  1.1.1.2  christos     OPT_MD,
    132  1.1.1.2  christos     OPT_INKEY,
    133  1.1.1.2  christos     OPT_KEYFORM,
    134  1.1.1.2  christos     OPT_KEYOPT,
    135  1.1.1.2  christos     OPT_RR_FROM,
    136  1.1.1.2  christos     OPT_RR_TO,
    137  1.1.1.2  christos     OPT_AES128_WRAP,
    138  1.1.1.2  christos     OPT_AES192_WRAP,
    139  1.1.1.2  christos     OPT_AES256_WRAP,
    140  1.1.1.2  christos     OPT_3DES_WRAP,
    141  1.1.1.2  christos     OPT_WRAP,
    142  1.1.1.2  christos     OPT_ENGINE,
    143      1.1  christos     OPT_R_ENUM,
    144  1.1.1.2  christos     OPT_PROV_ENUM,
    145  1.1.1.2  christos     OPT_CONFIG,
    146      1.1  christos     OPT_V_ENUM,
    147      1.1  christos     OPT_CIPHER,
    148      1.1  christos     OPT_ORIGINATOR
    149      1.1  christos } OPTION_CHOICE;
    150      1.1  christos 
    151      1.1  christos const OPTIONS cms_options[] = {
    152  1.1.1.2  christos     { OPT_HELP_STR, 1, '-', "Usage: %s [options] [cert...]\n" },
    153  1.1.1.2  christos     { "help", OPT_HELP, '-', "Display this summary" },
    154      1.1  christos 
    155      1.1  christos     OPT_SECTION("General"),
    156  1.1.1.2  christos     { "in", OPT_IN, '<', "Input file" },
    157  1.1.1.2  christos     { "out", OPT_OUT, '>', "Output file" },
    158      1.1  christos     OPT_CONFIG_OPTION,
    159      1.1  christos 
    160      1.1  christos     OPT_SECTION("Operation"),
    161  1.1.1.2  christos     { "encrypt", OPT_ENCRYPT, '-', "Encrypt message" },
    162  1.1.1.2  christos     { "decrypt", OPT_DECRYPT, '-', "Decrypt encrypted message" },
    163  1.1.1.2  christos     { "sign", OPT_SIGN, '-', "Sign message" },
    164  1.1.1.2  christos     { "verify", OPT_VERIFY, '-', "Verify signed message" },
    165  1.1.1.2  christos     { "resign", OPT_RESIGN, '-', "Resign a signed message" },
    166  1.1.1.2  christos     { "sign_receipt", OPT_SIGN_RECEIPT, '-',
    167  1.1.1.2  christos         "Generate a signed receipt for a message" },
    168  1.1.1.2  christos     { "verify_receipt", OPT_VERIFY_RECEIPT, '<',
    169  1.1.1.2  christos         "Verify receipts; exit if receipt signatures do not verify" },
    170  1.1.1.2  christos     { "digest", OPT_DIGEST, 's', "Sign a pre-computed digest in hex notation" },
    171  1.1.1.2  christos     { "digest_create", OPT_DIGEST_CREATE, '-',
    172  1.1.1.2  christos         "Create a CMS \"DigestedData\" object" },
    173  1.1.1.2  christos     { "digest_verify", OPT_DIGEST_VERIFY, '-',
    174  1.1.1.2  christos         "Verify a CMS \"DigestedData\" object and output it" },
    175  1.1.1.2  christos     { "compress", OPT_COMPRESS, '-', "Create a CMS \"CompressedData\" object" },
    176  1.1.1.2  christos     { "uncompress", OPT_UNCOMPRESS, '-',
    177  1.1.1.2  christos         "Uncompress a CMS \"CompressedData\" object" },
    178  1.1.1.2  christos     { "EncryptedData_encrypt", OPT_ED_ENCRYPT, '-',
    179  1.1.1.2  christos         "Create CMS \"EncryptedData\" object using symmetric key" },
    180  1.1.1.2  christos     { "EncryptedData_decrypt", OPT_ED_DECRYPT, '-',
    181  1.1.1.2  christos         "Decrypt CMS \"EncryptedData\" object using symmetric key" },
    182  1.1.1.2  christos     { "data_create", OPT_DATA_CREATE, '-', "Create a CMS \"Data\" object" },
    183  1.1.1.2  christos     { "data_out", OPT_DATA_OUT, '-', "Copy CMS \"Data\" object to output" },
    184  1.1.1.2  christos     { "cmsout", OPT_CMSOUT, '-', "Output CMS structure" },
    185      1.1  christos 
    186      1.1  christos     OPT_SECTION("File format"),
    187  1.1.1.2  christos     { "inform", OPT_INFORM, 'c', "Input format SMIME (default), PEM or DER" },
    188  1.1.1.2  christos     { "outform", OPT_OUTFORM, 'c',
    189  1.1.1.2  christos         "Output format SMIME (default), PEM or DER" },
    190  1.1.1.2  christos     { "rctform", OPT_RCTFORM, 'F', "Receipt file format" },
    191  1.1.1.2  christos     { "stream", OPT_INDEF, '-', "Enable CMS streaming" },
    192  1.1.1.2  christos     { "indef", OPT_INDEF, '-', "Same as -stream" },
    193  1.1.1.2  christos     { "noindef", OPT_NOINDEF, '-', "Disable CMS streaming" },
    194  1.1.1.2  christos     { "binary", OPT_BINARY, '-',
    195  1.1.1.2  christos         "Treat input as binary: do not translate to canonical form" },
    196  1.1.1.2  christos     { "crlfeol", OPT_CRLFEOL, '-',
    197  1.1.1.2  christos         "Use CRLF as EOL termination instead of LF only" },
    198  1.1.1.2  christos     { "asciicrlf", OPT_ASCIICRLF, '-',
    199  1.1.1.2  christos         "Perform CRLF canonicalisation when signing" },
    200      1.1  christos 
    201      1.1  christos     OPT_SECTION("Keys and passwords"),
    202  1.1.1.2  christos     { "pwri_password", OPT_PWRI_PASSWORD, 's',
    203  1.1.1.2  christos         "Specific password for recipient" },
    204  1.1.1.2  christos     { "secretkey", OPT_SECRETKEY, 's',
    205  1.1.1.2  christos         "Use specified hex-encoded key to decrypt/encrypt recipients or content" },
    206  1.1.1.2  christos     { "secretkeyid", OPT_SECRETKEYID, 's',
    207  1.1.1.2  christos         "Identity of the -secretkey for CMS \"KEKRecipientInfo\" object" },
    208  1.1.1.2  christos     { "inkey", OPT_INKEY, 's',
    209  1.1.1.2  christos         "Input private key (if not signer or recipient)" },
    210  1.1.1.2  christos     { "passin", OPT_PASSIN, 's', "Input file pass phrase source" },
    211  1.1.1.2  christos     { "keyopt", OPT_KEYOPT, 's', "Set public key parameters as n:v pairs" },
    212  1.1.1.2  christos     { "keyform", OPT_KEYFORM, 'f',
    213  1.1.1.2  christos         "Input private key format (ENGINE, other values ignored)" },
    214      1.1  christos #ifndef OPENSSL_NO_ENGINE
    215  1.1.1.2  christos     { "engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device" },
    216      1.1  christos #endif
    217      1.1  christos     OPT_PROV_OPTIONS,
    218      1.1  christos     OPT_R_OPTIONS,
    219      1.1  christos 
    220      1.1  christos     OPT_SECTION("Encryption and decryption"),
    221  1.1.1.2  christos     { "originator", OPT_ORIGINATOR, 's', "Originator certificate file" },
    222  1.1.1.2  christos     { "recip", OPT_RECIP, '<', "Recipient cert file" },
    223  1.1.1.2  christos     { "cert...", OPT_PARAM, '.',
    224  1.1.1.2  christos         "Recipient certs (optional; used only when encrypting)" },
    225  1.1.1.2  christos     { "", OPT_CIPHER, '-',
    226  1.1.1.2  christos         "The encryption algorithm to use (any supported cipher)" },
    227  1.1.1.2  christos     { "wrap", OPT_WRAP, 's',
    228  1.1.1.2  christos         "Key wrap algorithm to use when encrypting with key agreement" },
    229  1.1.1.2  christos     { "aes128-wrap", OPT_AES128_WRAP, '-', "Use AES128 to wrap key" },
    230  1.1.1.2  christos     { "aes192-wrap", OPT_AES192_WRAP, '-', "Use AES192 to wrap key" },
    231  1.1.1.2  christos     { "aes256-wrap", OPT_AES256_WRAP, '-', "Use AES256 to wrap key" },
    232  1.1.1.2  christos     { "des3-wrap", OPT_3DES_WRAP, '-', "Use 3DES-EDE to wrap key" },
    233  1.1.1.2  christos     { "debug_decrypt", OPT_DEBUG_DECRYPT, '-',
    234  1.1.1.2  christos         "Disable MMA protection, return error if no recipient found (see doc)" },
    235      1.1  christos 
    236      1.1  christos     OPT_SECTION("Signing"),
    237  1.1.1.2  christos     { "md", OPT_MD, 's', "Digest algorithm to use" },
    238  1.1.1.2  christos     { "signer", OPT_SIGNER, 's', "Signer certificate input file" },
    239  1.1.1.2  christos     { "certfile", OPT_CERTFILE, '<',
    240  1.1.1.2  christos         "Extra signer and intermediate CA certificates to include when signing" },
    241  1.1.1.2  christos     { OPT_MORE_STR, 0, 0,
    242  1.1.1.2  christos         "or to use as preferred signer certs and for chain building when verifying" },
    243  1.1.1.2  christos     { "cades", OPT_CADES, '-',
    244  1.1.1.2  christos         "Include signingCertificate attribute (CAdES-BES)" },
    245  1.1.1.2  christos     { "nodetach", OPT_NODETACH, '-', "Use opaque signing" },
    246  1.1.1.2  christos     { "nocerts", OPT_NOCERTS, '-',
    247  1.1.1.2  christos         "Don't include signer's certificate when signing" },
    248  1.1.1.2  christos     { "noattr", OPT_NOATTR, '-', "Don't include any signed attributes" },
    249  1.1.1.2  christos     { "nosmimecap", OPT_NOSMIMECAP, '-', "Omit the SMIMECapabilities attribute" },
    250  1.1.1.2  christos     { "no_signing_time", OPT_NO_SIGNING_TIME, '-',
    251  1.1.1.2  christos         "Omit the signing time attribute" },
    252  1.1.1.2  christos     { "receipt_request_all", OPT_RR_ALL, '-',
    253  1.1.1.2  christos         "When signing, create a receipt request for all recipients" },
    254  1.1.1.2  christos     { "receipt_request_first", OPT_RR_FIRST, '-',
    255  1.1.1.2  christos         "When signing, create a receipt request for first recipient" },
    256  1.1.1.2  christos     { "receipt_request_from", OPT_RR_FROM, 's',
    257  1.1.1.2  christos         "Create signed receipt request with specified email address" },
    258  1.1.1.2  christos     { "receipt_request_to", OPT_RR_TO, 's',
    259  1.1.1.2  christos         "Create signed receipt targeted to specified address" },
    260      1.1  christos 
    261      1.1  christos     OPT_SECTION("Verification"),
    262  1.1.1.2  christos     { "signer", OPT_DUP, 's', "Signer certificate(s) output file" },
    263  1.1.1.2  christos     { "content", OPT_CONTENT, '<',
    264  1.1.1.2  christos         "Supply or override content for detached signature" },
    265  1.1.1.2  christos     { "no_content_verify", OPT_NO_CONTENT_VERIFY, '-',
    266  1.1.1.2  christos         "Do not verify signed content signatures" },
    267  1.1.1.2  christos     { "no_attr_verify", OPT_NO_ATTR_VERIFY, '-',
    268  1.1.1.2  christos         "Do not verify signed attribute signatures" },
    269  1.1.1.2  christos     { "nosigs", OPT_NOSIGS, '-', "Don't verify message signature" },
    270  1.1.1.2  christos     { "noverify", OPT_NOVERIFY, '-', "Don't verify signers certificate" },
    271  1.1.1.2  christos     { "nointern", OPT_NOINTERN, '-',
    272  1.1.1.2  christos         "Don't search certificates in message for signer" },
    273  1.1.1.2  christos     { "cades", OPT_DUP, '-', "Check signingCertificate (CAdES-BES)" },
    274  1.1.1.2  christos     { "verify_retcode", OPT_VERIFY_RETCODE, '-',
    275  1.1.1.2  christos         "Exit non-zero on verification failure" },
    276  1.1.1.2  christos     { "CAfile", OPT_CAFILE, '<', "Trusted certificates file" },
    277  1.1.1.2  christos     { "CApath", OPT_CAPATH, '/', "Trusted certificates directory" },
    278  1.1.1.2  christos     { "CAstore", OPT_CASTORE, ':', "Trusted certificates store URI" },
    279  1.1.1.2  christos     { "no-CAfile", OPT_NOCAFILE, '-',
    280  1.1.1.2  christos         "Do not load the default certificates file" },
    281  1.1.1.2  christos     { "no-CApath", OPT_NOCAPATH, '-',
    282  1.1.1.2  christos         "Do not load certificates from the default certificates directory" },
    283  1.1.1.2  christos     { "no-CAstore", OPT_NOCASTORE, '-',
    284  1.1.1.2  christos         "Do not load certificates from the default certificates store" },
    285      1.1  christos 
    286      1.1  christos     OPT_SECTION("Output"),
    287  1.1.1.2  christos     { "keyid", OPT_KEYID, '-', "Use subject key identifier" },
    288  1.1.1.2  christos     { "econtent_type", OPT_ECONTENT_TYPE, 's', "OID for external content" },
    289  1.1.1.2  christos     { "text", OPT_TEXT, '-', "Include or delete text MIME headers" },
    290  1.1.1.2  christos     { "certsout", OPT_CERTSOUT, '>', "Certificate output file" },
    291  1.1.1.2  christos     { "to", OPT_TO, 's', "To address" },
    292  1.1.1.2  christos     { "from", OPT_FROM, 's', "From address" },
    293  1.1.1.2  christos     { "subject", OPT_SUBJECT, 's', "Subject" },
    294      1.1  christos 
    295      1.1  christos     OPT_SECTION("Printing"),
    296  1.1.1.2  christos     { "noout", OPT_NOOUT, '-',
    297  1.1.1.2  christos         "For the -cmsout operation do not output the parsed CMS structure" },
    298  1.1.1.2  christos     { "print", OPT_PRINT, '-',
    299  1.1.1.2  christos         "For the -cmsout operation print out all fields of the CMS structure" },
    300  1.1.1.2  christos     { "nameopt", OPT_NAMEOPT, 's',
    301  1.1.1.2  christos         "For the -print option specifies various strings printing options" },
    302  1.1.1.2  christos     { "receipt_request_print", OPT_RR_PRINT, '-', "Print CMS Receipt Request" },
    303      1.1  christos 
    304      1.1  christos     OPT_V_OPTIONS,
    305  1.1.1.2  christos     { NULL }
    306      1.1  christos };
    307      1.1  christos 
    308      1.1  christos static CMS_ContentInfo *load_content_info(int informat, BIO *in, int flags,
    309  1.1.1.2  christos     BIO **indata, const char *name)
    310      1.1  christos {
    311      1.1  christos     CMS_ContentInfo *ret, *ci;
    312      1.1  christos 
    313      1.1  christos     ret = CMS_ContentInfo_new_ex(app_get0_libctx(), app_get0_propq());
    314      1.1  christos     if (ret == NULL) {
    315      1.1  christos         BIO_printf(bio_err, "Error allocating CMS_contentinfo\n");
    316      1.1  christos         return NULL;
    317      1.1  christos     }
    318      1.1  christos     switch (informat) {
    319      1.1  christos     case FORMAT_SMIME:
    320      1.1  christos         ci = SMIME_read_CMS_ex(in, flags, indata, &ret);
    321      1.1  christos         break;
    322      1.1  christos     case FORMAT_PEM:
    323      1.1  christos         ci = PEM_read_bio_CMS(in, &ret, NULL, NULL);
    324      1.1  christos         break;
    325      1.1  christos     case FORMAT_ASN1:
    326      1.1  christos         ci = d2i_CMS_bio(in, &ret);
    327      1.1  christos         break;
    328      1.1  christos     default:
    329      1.1  christos         BIO_printf(bio_err, "Bad input format for %s\n", name);
    330      1.1  christos         goto err;
    331      1.1  christos     }
    332      1.1  christos     if (ci == NULL) {
    333      1.1  christos         BIO_printf(bio_err, "Error reading %s Content Info\n", name);
    334      1.1  christos         goto err;
    335      1.1  christos     }
    336      1.1  christos     return ret;
    337  1.1.1.2  christos err:
    338      1.1  christos     CMS_ContentInfo_free(ret);
    339      1.1  christos     return NULL;
    340      1.1  christos }
    341      1.1  christos 
    342      1.1  christos int cms_main(int argc, char **argv)
    343      1.1  christos {
    344      1.1  christos     CONF *conf = NULL;
    345      1.1  christos     ASN1_OBJECT *econtent_type = NULL;
    346      1.1  christos     BIO *in = NULL, *out = NULL, *indata = NULL, *rctin = NULL;
    347      1.1  christos     CMS_ContentInfo *cms = NULL, *rcms = NULL;
    348      1.1  christos     CMS_ReceiptRequest *rr = NULL;
    349      1.1  christos     ENGINE *e = NULL;
    350      1.1  christos     EVP_PKEY *key = NULL;
    351      1.1  christos     EVP_CIPHER *cipher = NULL, *wrap_cipher = NULL;
    352      1.1  christos     EVP_MD *sign_md = NULL;
    353      1.1  christos     STACK_OF(OPENSSL_STRING) *rr_to = NULL, *rr_from = NULL;
    354      1.1  christos     STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;
    355      1.1  christos     STACK_OF(X509) *encerts = sk_X509_new_null(), *other = NULL;
    356      1.1  christos     X509 *cert = NULL, *recip = NULL, *signer = NULL, *originator = NULL;
    357      1.1  christos     X509_STORE *store = NULL;
    358      1.1  christos     X509_VERIFY_PARAM *vpm = X509_VERIFY_PARAM_new();
    359      1.1  christos     char *certfile = NULL, *keyfile = NULL, *contfile = NULL;
    360      1.1  christos     const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL;
    361      1.1  christos     char *certsoutfile = NULL, *digestname = NULL, *wrapname = NULL;
    362      1.1  christos     int noCAfile = 0, noCApath = 0, noCAstore = 0;
    363      1.1  christos     char *digesthex = NULL;
    364      1.1  christos     unsigned char *digestbin = NULL;
    365      1.1  christos     long digestlen = 0;
    366      1.1  christos     char *infile = NULL, *outfile = NULL, *rctfile = NULL;
    367      1.1  christos     char *passinarg = NULL, *passin = NULL, *signerfile = NULL;
    368      1.1  christos     char *originatorfile = NULL, *recipfile = NULL, *ciphername = NULL;
    369      1.1  christos     char *to = NULL, *from = NULL, *subject = NULL, *prog;
    370      1.1  christos     cms_key_param *key_first = NULL, *key_param = NULL;
    371      1.1  christos     int flags = CMS_DETACHED, binary_files = 0;
    372      1.1  christos     int noout = 0, print = 0, keyidx = -1, vpmtouched = 0;
    373      1.1  christos     int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
    374      1.1  christos     int operation = 0, ret = 1, rr_print = 0, rr_allorfirst = -1;
    375      1.1  christos     int verify_retcode = 0, rctformat = FORMAT_SMIME, keyform = FORMAT_UNDEF;
    376      1.1  christos     size_t secret_keylen = 0, secret_keyidlen = 0;
    377      1.1  christos     unsigned char *pwri_pass = NULL, *pwri_tmp = NULL;
    378      1.1  christos     unsigned char *secret_key = NULL, *secret_keyid = NULL;
    379      1.1  christos     long ltmp;
    380      1.1  christos     const char *mime_eol = "\n";
    381      1.1  christos     OPTION_CHOICE o;
    382      1.1  christos     OSSL_LIB_CTX *libctx = app_get0_libctx();
    383      1.1  christos 
    384      1.1  christos     if (encerts == NULL || vpm == NULL)
    385      1.1  christos         goto end;
    386      1.1  christos 
    387      1.1  christos     opt_set_unknown_name("cipher");
    388      1.1  christos     prog = opt_init(argc, argv, cms_options);
    389      1.1  christos     while ((o = opt_next()) != OPT_EOF) {
    390      1.1  christos         switch (o) {
    391      1.1  christos         case OPT_EOF:
    392      1.1  christos         case OPT_ERR:
    393  1.1.1.2  christos         opthelp:
    394      1.1  christos             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
    395      1.1  christos             goto end;
    396      1.1  christos         case OPT_HELP:
    397      1.1  christos             opt_help(cms_options);
    398      1.1  christos             ret = 0;
    399      1.1  christos             goto end;
    400      1.1  christos         case OPT_INFORM:
    401      1.1  christos             if (!opt_format(opt_arg(), OPT_FMT_PDS, &informat))
    402      1.1  christos                 goto opthelp;
    403      1.1  christos             break;
    404      1.1  christos         case OPT_OUTFORM:
    405      1.1  christos             if (!opt_format(opt_arg(), OPT_FMT_PDS, &outformat))
    406      1.1  christos                 goto opthelp;
    407      1.1  christos             break;
    408      1.1  christos         case OPT_OUT:
    409      1.1  christos             outfile = opt_arg();
    410      1.1  christos             break;
    411      1.1  christos 
    412      1.1  christos         case OPT_ENCRYPT:
    413      1.1  christos             operation = SMIME_ENCRYPT;
    414      1.1  christos             break;
    415      1.1  christos         case OPT_DECRYPT:
    416      1.1  christos             operation = SMIME_DECRYPT;
    417      1.1  christos             break;
    418      1.1  christos         case OPT_SIGN:
    419      1.1  christos             operation = SMIME_SIGN;
    420      1.1  christos             break;
    421      1.1  christos         case OPT_VERIFY:
    422      1.1  christos             operation = SMIME_VERIFY;
    423      1.1  christos             break;
    424      1.1  christos         case OPT_RESIGN:
    425      1.1  christos             operation = SMIME_RESIGN;
    426      1.1  christos             break;
    427      1.1  christos         case OPT_SIGN_RECEIPT:
    428      1.1  christos             operation = SMIME_SIGN_RECEIPT;
    429      1.1  christos             break;
    430      1.1  christos         case OPT_VERIFY_RECEIPT:
    431      1.1  christos             operation = SMIME_VERIFY_RECEIPT;
    432      1.1  christos             rctfile = opt_arg();
    433      1.1  christos             break;
    434      1.1  christos         case OPT_VERIFY_RETCODE:
    435      1.1  christos             verify_retcode = 1;
    436      1.1  christos             break;
    437      1.1  christos         case OPT_DIGEST_CREATE:
    438      1.1  christos             operation = SMIME_DIGEST_CREATE;
    439      1.1  christos             break;
    440      1.1  christos         case OPT_DIGEST:
    441      1.1  christos             digesthex = opt_arg();
    442      1.1  christos             break;
    443      1.1  christos         case OPT_DIGEST_VERIFY:
    444      1.1  christos             operation = SMIME_DIGEST_VERIFY;
    445      1.1  christos             break;
    446      1.1  christos         case OPT_COMPRESS:
    447      1.1  christos             operation = SMIME_COMPRESS;
    448      1.1  christos             break;
    449      1.1  christos         case OPT_UNCOMPRESS:
    450      1.1  christos             operation = SMIME_UNCOMPRESS;
    451      1.1  christos             break;
    452      1.1  christos         case OPT_ED_ENCRYPT:
    453      1.1  christos             operation = SMIME_ENCRYPTED_ENCRYPT;
    454      1.1  christos             break;
    455      1.1  christos         case OPT_ED_DECRYPT:
    456      1.1  christos             operation = SMIME_ENCRYPTED_DECRYPT;
    457      1.1  christos             break;
    458      1.1  christos         case OPT_DATA_CREATE:
    459      1.1  christos             operation = SMIME_DATA_CREATE;
    460      1.1  christos             break;
    461      1.1  christos         case OPT_DATA_OUT:
    462      1.1  christos             operation = SMIME_DATA_OUT;
    463      1.1  christos             break;
    464      1.1  christos         case OPT_CMSOUT:
    465      1.1  christos             operation = SMIME_CMSOUT;
    466      1.1  christos             break;
    467      1.1  christos 
    468      1.1  christos         case OPT_DEBUG_DECRYPT:
    469      1.1  christos             flags |= CMS_DEBUG_DECRYPT;
    470      1.1  christos             break;
    471      1.1  christos         case OPT_TEXT:
    472      1.1  christos             flags |= CMS_TEXT;
    473      1.1  christos             break;
    474      1.1  christos         case OPT_ASCIICRLF:
    475      1.1  christos             flags |= CMS_ASCIICRLF;
    476      1.1  christos             break;
    477      1.1  christos         case OPT_NOINTERN:
    478      1.1  christos             flags |= CMS_NOINTERN;
    479      1.1  christos             break;
    480      1.1  christos         case OPT_NOVERIFY:
    481      1.1  christos             flags |= CMS_NO_SIGNER_CERT_VERIFY;
    482      1.1  christos             break;
    483      1.1  christos         case OPT_NOCERTS:
    484      1.1  christos             flags |= CMS_NOCERTS;
    485      1.1  christos             break;
    486      1.1  christos         case OPT_NOATTR:
    487      1.1  christos             flags |= CMS_NOATTR;
    488      1.1  christos             break;
    489      1.1  christos         case OPT_NODETACH:
    490      1.1  christos             flags &= ~CMS_DETACHED;
    491      1.1  christos             break;
    492      1.1  christos         case OPT_NOSMIMECAP:
    493      1.1  christos             flags |= CMS_NOSMIMECAP;
    494      1.1  christos             break;
    495      1.1  christos         case OPT_NO_SIGNING_TIME:
    496      1.1  christos             flags |= CMS_NO_SIGNING_TIME;
    497      1.1  christos             break;
    498      1.1  christos         case OPT_BINARY:
    499      1.1  christos             flags |= CMS_BINARY;
    500      1.1  christos             break;
    501      1.1  christos         case OPT_CADES:
    502      1.1  christos             flags |= CMS_CADES;
    503      1.1  christos             break;
    504      1.1  christos         case OPT_KEYID:
    505      1.1  christos             flags |= CMS_USE_KEYID;
    506      1.1  christos             break;
    507      1.1  christos         case OPT_NOSIGS:
    508      1.1  christos             flags |= CMS_NOSIGS;
    509      1.1  christos             break;
    510      1.1  christos         case OPT_NO_CONTENT_VERIFY:
    511      1.1  christos             flags |= CMS_NO_CONTENT_VERIFY;
    512      1.1  christos             break;
    513      1.1  christos         case OPT_NO_ATTR_VERIFY:
    514      1.1  christos             flags |= CMS_NO_ATTR_VERIFY;
    515      1.1  christos             break;
    516      1.1  christos         case OPT_INDEF:
    517      1.1  christos             flags |= CMS_STREAM;
    518      1.1  christos             break;
    519      1.1  christos         case OPT_NOINDEF:
    520      1.1  christos             flags &= ~CMS_STREAM;
    521      1.1  christos             break;
    522      1.1  christos         case OPT_CRLFEOL:
    523      1.1  christos             mime_eol = "\r\n";
    524      1.1  christos             flags |= CMS_CRLFEOL;
    525      1.1  christos             break;
    526      1.1  christos         case OPT_NOOUT:
    527      1.1  christos             noout = 1;
    528      1.1  christos             break;
    529      1.1  christos         case OPT_RR_PRINT:
    530      1.1  christos             rr_print = 1;
    531      1.1  christos             break;
    532      1.1  christos         case OPT_RR_ALL:
    533      1.1  christos             rr_allorfirst = 0;
    534      1.1  christos             break;
    535      1.1  christos         case OPT_RR_FIRST:
    536      1.1  christos             rr_allorfirst = 1;
    537      1.1  christos             break;
    538      1.1  christos         case OPT_RCTFORM:
    539      1.1  christos             if (!opt_format(opt_arg(),
    540  1.1.1.2  christos                     OPT_FMT_PEMDER | OPT_FMT_SMIME, &rctformat))
    541      1.1  christos                 goto opthelp;
    542      1.1  christos             break;
    543      1.1  christos         case OPT_CERTFILE:
    544      1.1  christos             certfile = opt_arg();
    545      1.1  christos             break;
    546      1.1  christos         case OPT_CAFILE:
    547      1.1  christos             CAfile = opt_arg();
    548      1.1  christos             break;
    549      1.1  christos         case OPT_CAPATH:
    550      1.1  christos             CApath = opt_arg();
    551      1.1  christos             break;
    552      1.1  christos         case OPT_CASTORE:
    553      1.1  christos             CAstore = opt_arg();
    554      1.1  christos             break;
    555      1.1  christos         case OPT_NOCAFILE:
    556      1.1  christos             noCAfile = 1;
    557      1.1  christos             break;
    558      1.1  christos         case OPT_NOCAPATH:
    559      1.1  christos             noCApath = 1;
    560      1.1  christos             break;
    561      1.1  christos         case OPT_NOCASTORE:
    562      1.1  christos             noCAstore = 1;
    563      1.1  christos             break;
    564      1.1  christos         case OPT_IN:
    565      1.1  christos             infile = opt_arg();
    566      1.1  christos             break;
    567      1.1  christos         case OPT_CONTENT:
    568      1.1  christos             contfile = opt_arg();
    569      1.1  christos             break;
    570      1.1  christos         case OPT_RR_FROM:
    571      1.1  christos             if (rr_from == NULL
    572      1.1  christos                 && (rr_from = sk_OPENSSL_STRING_new_null()) == NULL)
    573      1.1  christos                 goto end;
    574      1.1  christos             if (sk_OPENSSL_STRING_push(rr_from, opt_arg()) <= 0)
    575      1.1  christos                 goto end;
    576      1.1  christos             break;
    577      1.1  christos         case OPT_RR_TO:
    578      1.1  christos             if (rr_to == NULL
    579      1.1  christos                 && (rr_to = sk_OPENSSL_STRING_new_null()) == NULL)
    580      1.1  christos                 goto end;
    581      1.1  christos             if (sk_OPENSSL_STRING_push(rr_to, opt_arg()) <= 0)
    582      1.1  christos                 goto end;
    583      1.1  christos             break;
    584      1.1  christos         case OPT_PRINT:
    585      1.1  christos             noout = print = 1;
    586      1.1  christos             break;
    587      1.1  christos         case OPT_NAMEOPT:
    588      1.1  christos             if (!set_nameopt(opt_arg()))
    589      1.1  christos                 goto opthelp;
    590      1.1  christos             break;
    591      1.1  christos         case OPT_SECRETKEY:
    592      1.1  christos             if (secret_key != NULL) {
    593      1.1  christos                 BIO_printf(bio_err, "Invalid key (supplied twice) %s\n",
    594  1.1.1.2  christos                     opt_arg());
    595      1.1  christos                 goto opthelp;
    596      1.1  christos             }
    597      1.1  christos             secret_key = OPENSSL_hexstr2buf(opt_arg(), &ltmp);
    598      1.1  christos             if (secret_key == NULL) {
    599      1.1  christos                 BIO_printf(bio_err, "Invalid key %s\n", opt_arg());
    600      1.1  christos                 goto end;
    601      1.1  christos             }
    602      1.1  christos             secret_keylen = (size_t)ltmp;
    603      1.1  christos             break;
    604      1.1  christos         case OPT_SECRETKEYID:
    605      1.1  christos             if (secret_keyid != NULL) {
    606      1.1  christos                 BIO_printf(bio_err, "Invalid id (supplied twice) %s\n",
    607  1.1.1.2  christos                     opt_arg());
    608      1.1  christos                 goto opthelp;
    609      1.1  christos             }
    610      1.1  christos             secret_keyid = OPENSSL_hexstr2buf(opt_arg(), &ltmp);
    611      1.1  christos             if (secret_keyid == NULL) {
    612      1.1  christos                 BIO_printf(bio_err, "Invalid id %s\n", opt_arg());
    613      1.1  christos                 goto opthelp;
    614      1.1  christos             }
    615      1.1  christos             secret_keyidlen = (size_t)ltmp;
    616      1.1  christos             break;
    617      1.1  christos         case OPT_PWRI_PASSWORD:
    618      1.1  christos             pwri_pass = (unsigned char *)opt_arg();
    619      1.1  christos             break;
    620      1.1  christos         case OPT_ECONTENT_TYPE:
    621      1.1  christos             if (econtent_type != NULL) {
    622      1.1  christos                 BIO_printf(bio_err, "Invalid OID (supplied twice) %s\n",
    623  1.1.1.2  christos                     opt_arg());
    624      1.1  christos                 goto opthelp;
    625      1.1  christos             }
    626      1.1  christos             econtent_type = OBJ_txt2obj(opt_arg(), 0);
    627      1.1  christos             if (econtent_type == NULL) {
    628      1.1  christos                 BIO_printf(bio_err, "Invalid OID %s\n", opt_arg());
    629      1.1  christos                 goto opthelp;
    630      1.1  christos             }
    631      1.1  christos             break;
    632      1.1  christos         case OPT_ENGINE:
    633      1.1  christos             e = setup_engine(opt_arg(), 0);
    634      1.1  christos             break;
    635      1.1  christos         case OPT_PASSIN:
    636      1.1  christos             passinarg = opt_arg();
    637      1.1  christos             break;
    638      1.1  christos         case OPT_TO:
    639      1.1  christos             to = opt_arg();
    640      1.1  christos             break;
    641      1.1  christos         case OPT_FROM:
    642      1.1  christos             from = opt_arg();
    643      1.1  christos             break;
    644      1.1  christos         case OPT_SUBJECT:
    645      1.1  christos             subject = opt_arg();
    646      1.1  christos             break;
    647      1.1  christos         case OPT_CERTSOUT:
    648      1.1  christos             certsoutfile = opt_arg();
    649      1.1  christos             break;
    650      1.1  christos         case OPT_MD:
    651      1.1  christos             digestname = opt_arg();
    652      1.1  christos             break;
    653      1.1  christos         case OPT_SIGNER:
    654      1.1  christos             /* If previous -signer argument add signer to list */
    655      1.1  christos             if (signerfile != NULL) {
    656      1.1  christos                 if (sksigners == NULL
    657      1.1  christos                     && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
    658      1.1  christos                     goto end;
    659      1.1  christos                 if (sk_OPENSSL_STRING_push(sksigners, signerfile) <= 0)
    660      1.1  christos                     goto end;
    661      1.1  christos                 if (keyfile == NULL)
    662      1.1  christos                     keyfile = signerfile;
    663      1.1  christos                 if (skkeys == NULL
    664      1.1  christos                     && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
    665      1.1  christos                     goto end;
    666      1.1  christos                 if (sk_OPENSSL_STRING_push(skkeys, keyfile) <= 0)
    667      1.1  christos                     goto end;
    668      1.1  christos                 keyfile = NULL;
    669      1.1  christos             }
    670      1.1  christos             signerfile = opt_arg();
    671      1.1  christos             break;
    672      1.1  christos         case OPT_ORIGINATOR:
    673      1.1  christos             originatorfile = opt_arg();
    674      1.1  christos             break;
    675      1.1  christos         case OPT_INKEY:
    676      1.1  christos             /* If previous -inkey argument add signer to list */
    677      1.1  christos             if (keyfile != NULL) {
    678      1.1  christos                 if (signerfile == NULL) {
    679      1.1  christos                     BIO_puts(bio_err, "Illegal -inkey without -signer\n");
    680      1.1  christos                     goto end;
    681      1.1  christos                 }
    682      1.1  christos                 if (sksigners == NULL
    683      1.1  christos                     && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
    684      1.1  christos                     goto end;
    685      1.1  christos                 if (sk_OPENSSL_STRING_push(sksigners, signerfile) <= 0)
    686      1.1  christos                     goto end;
    687      1.1  christos                 signerfile = NULL;
    688      1.1  christos                 if (skkeys == NULL
    689      1.1  christos                     && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
    690      1.1  christos                     goto end;
    691      1.1  christos                 if (sk_OPENSSL_STRING_push(skkeys, keyfile) <= 0)
    692      1.1  christos                     goto end;
    693      1.1  christos             }
    694      1.1  christos             keyfile = opt_arg();
    695      1.1  christos             break;
    696      1.1  christos         case OPT_KEYFORM:
    697      1.1  christos             if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
    698      1.1  christos                 goto opthelp;
    699      1.1  christos             break;
    700      1.1  christos         case OPT_RECIP:
    701      1.1  christos             if (operation == SMIME_ENCRYPT) {
    702      1.1  christos                 cert = load_cert(opt_arg(), FORMAT_UNDEF,
    703  1.1.1.2  christos                     "recipient certificate file");
    704      1.1  christos                 if (cert == NULL)
    705      1.1  christos                     goto end;
    706      1.1  christos                 if (!sk_X509_push(encerts, cert))
    707      1.1  christos                     goto end;
    708      1.1  christos                 cert = NULL;
    709      1.1  christos             } else {
    710      1.1  christos                 recipfile = opt_arg();
    711      1.1  christos             }
    712      1.1  christos             break;
    713      1.1  christos         case OPT_CIPHER:
    714      1.1  christos             ciphername = opt_unknown();
    715      1.1  christos             break;
    716      1.1  christos         case OPT_KEYOPT:
    717      1.1  christos             keyidx = -1;
    718      1.1  christos             if (operation == SMIME_ENCRYPT) {
    719      1.1  christos                 if (sk_X509_num(encerts) > 0)
    720      1.1  christos                     keyidx += sk_X509_num(encerts);
    721      1.1  christos             } else {
    722      1.1  christos                 if (keyfile != NULL || signerfile != NULL)
    723      1.1  christos                     keyidx++;
    724      1.1  christos                 if (skkeys != NULL)
    725      1.1  christos                     keyidx += sk_OPENSSL_STRING_num(skkeys);
    726      1.1  christos             }
    727      1.1  christos             if (keyidx < 0) {
    728      1.1  christos                 BIO_printf(bio_err, "No key specified\n");
    729      1.1  christos                 goto opthelp;
    730      1.1  christos             }
    731      1.1  christos             if (key_param == NULL || key_param->idx != keyidx) {
    732      1.1  christos                 cms_key_param *nparam;
    733      1.1  christos                 nparam = app_malloc(sizeof(*nparam), "key param buffer");
    734      1.1  christos                 if ((nparam->param = sk_OPENSSL_STRING_new_null()) == NULL) {
    735      1.1  christos                     OPENSSL_free(nparam);
    736      1.1  christos                     goto end;
    737      1.1  christos                 }
    738      1.1  christos                 nparam->idx = keyidx;
    739      1.1  christos                 nparam->next = NULL;
    740      1.1  christos                 if (key_first == NULL)
    741      1.1  christos                     key_first = nparam;
    742      1.1  christos                 else
    743      1.1  christos                     key_param->next = nparam;
    744      1.1  christos                 key_param = nparam;
    745      1.1  christos             }
    746      1.1  christos             if (sk_OPENSSL_STRING_push(key_param->param, opt_arg()) <= 0)
    747      1.1  christos                 goto end;
    748      1.1  christos             break;
    749      1.1  christos         case OPT_V_CASES:
    750      1.1  christos             if (!opt_verify(o, vpm))
    751      1.1  christos                 goto end;
    752      1.1  christos             vpmtouched++;
    753      1.1  christos             break;
    754      1.1  christos         case OPT_R_CASES:
    755      1.1  christos             if (!opt_rand(o))
    756      1.1  christos                 goto end;
    757      1.1  christos             break;
    758      1.1  christos         case OPT_PROV_CASES:
    759      1.1  christos             if (!opt_provider(o))
    760      1.1  christos                 goto end;
    761      1.1  christos             break;
    762      1.1  christos         case OPT_CONFIG:
    763      1.1  christos             conf = app_load_config_modules(opt_arg());
    764      1.1  christos             if (conf == NULL)
    765      1.1  christos                 goto end;
    766      1.1  christos             break;
    767      1.1  christos         case OPT_WRAP:
    768      1.1  christos             wrapname = opt_arg();
    769      1.1  christos             break;
    770      1.1  christos         case OPT_AES128_WRAP:
    771      1.1  christos         case OPT_AES192_WRAP:
    772      1.1  christos         case OPT_AES256_WRAP:
    773      1.1  christos         case OPT_3DES_WRAP:
    774      1.1  christos             wrapname = opt_flag() + 1;
    775      1.1  christos             break;
    776      1.1  christos         }
    777      1.1  christos     }
    778      1.1  christos     if (!app_RAND_load())
    779      1.1  christos         goto end;
    780      1.1  christos 
    781      1.1  christos     if (digestname != NULL) {
    782      1.1  christos         if (!opt_md(digestname, &sign_md))
    783      1.1  christos             goto end;
    784      1.1  christos     }
    785      1.1  christos     if (!opt_cipher_any(ciphername, &cipher))
    786      1.1  christos         goto end;
    787      1.1  christos     if (wrapname != NULL) {
    788      1.1  christos         if (!opt_cipher_any(wrapname, &wrap_cipher))
    789      1.1  christos             goto end;
    790      1.1  christos     }
    791      1.1  christos 
    792      1.1  christos     /* Remaining args are files to process. */
    793      1.1  christos     argv = opt_rest();
    794      1.1  christos 
    795      1.1  christos     if ((rr_allorfirst != -1 || rr_from != NULL) && rr_to == NULL) {
    796      1.1  christos         BIO_puts(bio_err, "No Signed Receipts Recipients\n");
    797      1.1  christos         goto opthelp;
    798      1.1  christos     }
    799      1.1  christos 
    800      1.1  christos     if (!(operation & SMIME_SIGNERS) && (rr_to != NULL || rr_from != NULL)) {
    801      1.1  christos         BIO_puts(bio_err, "Signed receipts only allowed with -sign\n");
    802      1.1  christos         goto opthelp;
    803      1.1  christos     }
    804      1.1  christos     if (!(operation & SMIME_SIGNERS) && (skkeys != NULL || sksigners != NULL)) {
    805      1.1  christos         BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
    806      1.1  christos         goto opthelp;
    807      1.1  christos     }
    808      1.1  christos 
    809      1.1  christos     if ((flags & CMS_CADES) != 0) {
    810      1.1  christos         if ((flags & CMS_NOATTR) != 0) {
    811      1.1  christos             BIO_puts(bio_err, "Incompatible options: "
    812  1.1.1.2  christos                               "CAdES requires signed attributes\n");
    813      1.1  christos             goto opthelp;
    814      1.1  christos         }
    815      1.1  christos         if (operation == SMIME_VERIFY
    816  1.1.1.2  christos             && (flags & (CMS_NO_SIGNER_CERT_VERIFY | CMS_NO_ATTR_VERIFY)) != 0) {
    817      1.1  christos             BIO_puts(bio_err, "Incompatible options: CAdES validation requires"
    818  1.1.1.2  christos                               " certs and signed attributes validations\n");
    819      1.1  christos             goto opthelp;
    820      1.1  christos         }
    821      1.1  christos     }
    822      1.1  christos 
    823      1.1  christos     if (operation & SMIME_SIGNERS) {
    824      1.1  christos         if (keyfile != NULL && signerfile == NULL) {
    825      1.1  christos             BIO_puts(bio_err, "Illegal -inkey without -signer\n");
    826      1.1  christos             goto opthelp;
    827      1.1  christos         }
    828      1.1  christos         /* Check to see if any final signer needs to be appended */
    829      1.1  christos         if (signerfile != NULL) {
    830      1.1  christos             if (sksigners == NULL
    831      1.1  christos                 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
    832      1.1  christos                 goto end;
    833      1.1  christos             if (sk_OPENSSL_STRING_push(sksigners, signerfile) <= 0)
    834      1.1  christos                 goto end;
    835      1.1  christos             if (skkeys == NULL && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
    836      1.1  christos                 goto end;
    837      1.1  christos             if (keyfile == NULL)
    838      1.1  christos                 keyfile = signerfile;
    839      1.1  christos             if (sk_OPENSSL_STRING_push(skkeys, keyfile) <= 0)
    840      1.1  christos                 goto end;
    841      1.1  christos         }
    842      1.1  christos         if (sksigners == NULL) {
    843      1.1  christos             BIO_printf(bio_err, "No signer certificate specified\n");
    844      1.1  christos             goto opthelp;
    845      1.1  christos         }
    846      1.1  christos         signerfile = NULL;
    847      1.1  christos         keyfile = NULL;
    848      1.1  christos     } else if (operation == SMIME_DECRYPT) {
    849      1.1  christos         if (recipfile == NULL && keyfile == NULL
    850      1.1  christos             && secret_key == NULL && pwri_pass == NULL) {
    851      1.1  christos             BIO_printf(bio_err,
    852  1.1.1.2  christos                 "No recipient certificate or key specified\n");
    853      1.1  christos             goto opthelp;
    854      1.1  christos         }
    855      1.1  christos     } else if (operation == SMIME_ENCRYPT) {
    856      1.1  christos         if (*argv == NULL && secret_key == NULL
    857      1.1  christos             && pwri_pass == NULL && sk_X509_num(encerts) <= 0) {
    858      1.1  christos             BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
    859      1.1  christos             goto opthelp;
    860      1.1  christos         }
    861      1.1  christos     } else if (!operation) {
    862      1.1  christos         BIO_printf(bio_err, "No operation option (-encrypt|-decrypt|-sign|-verify|...) specified.\n");
    863      1.1  christos         goto opthelp;
    864      1.1  christos     }
    865      1.1  christos 
    866      1.1  christos     if (!app_passwd(passinarg, NULL, &passin, NULL)) {
    867      1.1  christos         BIO_printf(bio_err, "Error getting password\n");
    868      1.1  christos         goto end;
    869      1.1  christos     }
    870      1.1  christos 
    871      1.1  christos     ret = 2;
    872      1.1  christos 
    873      1.1  christos     if ((operation & SMIME_SIGNERS) == 0) {
    874      1.1  christos         if ((flags & CMS_DETACHED) == 0)
    875      1.1  christos             BIO_printf(bio_err,
    876  1.1.1.2  christos                 "Warning: -nodetach option is ignored for non-signing operation\n");
    877      1.1  christos 
    878      1.1  christos         flags &= ~CMS_DETACHED;
    879      1.1  christos     }
    880      1.1  christos     if ((operation & SMIME_IP) == 0 && contfile != NULL)
    881      1.1  christos         BIO_printf(bio_err,
    882  1.1.1.2  christos             "Warning: -contfile option is ignored for the given operation\n");
    883      1.1  christos     if (operation != SMIME_ENCRYPT && *argv != NULL)
    884      1.1  christos         BIO_printf(bio_err,
    885  1.1.1.2  christos             "Warning: recipient certificate file parameters ignored for operation other than -encrypt\n");
    886      1.1  christos 
    887      1.1  christos     if ((flags & CMS_BINARY) != 0) {
    888      1.1  christos         if (!(operation & SMIME_OP))
    889      1.1  christos             outformat = FORMAT_BINARY;
    890      1.1  christos         if (!(operation & SMIME_IP))
    891      1.1  christos             informat = FORMAT_BINARY;
    892      1.1  christos         if ((operation & SMIME_SIGNERS) != 0 && (flags & CMS_DETACHED) != 0)
    893      1.1  christos             binary_files = 1;
    894      1.1  christos         if ((operation & SMIME_IP) != 0 && contfile == NULL)
    895      1.1  christos             binary_files = 1;
    896      1.1  christos     }
    897      1.1  christos 
    898      1.1  christos     if (operation == SMIME_ENCRYPT) {
    899      1.1  christos         if (!cipher)
    900      1.1  christos             cipher = (EVP_CIPHER *)EVP_aes_256_cbc();
    901      1.1  christos         if (secret_key && !secret_keyid) {
    902      1.1  christos             BIO_printf(bio_err, "No secret key id\n");
    903      1.1  christos             goto end;
    904      1.1  christos         }
    905      1.1  christos 
    906      1.1  christos         for (; *argv != NULL; argv++) {
    907      1.1  christos             cert = load_cert(*argv, FORMAT_UNDEF,
    908  1.1.1.2  christos                 "recipient certificate file");
    909      1.1  christos             if (cert == NULL)
    910      1.1  christos                 goto end;
    911      1.1  christos             if (!sk_X509_push(encerts, cert))
    912      1.1  christos                 goto end;
    913      1.1  christos             cert = NULL;
    914      1.1  christos         }
    915      1.1  christos     }
    916      1.1  christos 
    917      1.1  christos     if (certfile != NULL) {
    918      1.1  christos         if (!load_certs(certfile, 0, &other, NULL, "certificate file")) {
    919      1.1  christos             ERR_print_errors(bio_err);
    920      1.1  christos             goto end;
    921      1.1  christos         }
    922      1.1  christos     }
    923      1.1  christos 
    924      1.1  christos     if (recipfile != NULL && (operation == SMIME_DECRYPT)) {
    925      1.1  christos         if ((recip = load_cert(recipfile, FORMAT_UNDEF,
    926  1.1.1.2  christos                  "recipient certificate file"))
    927  1.1.1.2  christos             == NULL) {
    928      1.1  christos             ERR_print_errors(bio_err);
    929      1.1  christos             goto end;
    930      1.1  christos         }
    931      1.1  christos     }
    932      1.1  christos 
    933      1.1  christos     if (originatorfile != NULL) {
    934      1.1  christos         if ((originator = load_cert(originatorfile, FORMAT_UNDEF,
    935  1.1.1.2  christos                  "originator certificate file"))
    936  1.1.1.2  christos             == NULL) {
    937      1.1  christos             ERR_print_errors(bio_err);
    938      1.1  christos             goto end;
    939      1.1  christos         }
    940      1.1  christos     }
    941      1.1  christos 
    942      1.1  christos     if (operation == SMIME_SIGN_RECEIPT) {
    943      1.1  christos         if ((signer = load_cert(signerfile, FORMAT_UNDEF,
    944  1.1.1.2  christos                  "receipt signer certificate file"))
    945  1.1.1.2  christos             == NULL) {
    946      1.1  christos             ERR_print_errors(bio_err);
    947      1.1  christos             goto end;
    948      1.1  christos         }
    949      1.1  christos     }
    950      1.1  christos 
    951      1.1  christos     if ((operation == SMIME_DECRYPT) || (operation == SMIME_ENCRYPT)) {
    952      1.1  christos         if (keyfile == NULL)
    953      1.1  christos             keyfile = recipfile;
    954      1.1  christos     } else if ((operation == SMIME_SIGN) || (operation == SMIME_SIGN_RECEIPT)) {
    955      1.1  christos         if (keyfile == NULL)
    956      1.1  christos             keyfile = signerfile;
    957      1.1  christos     } else {
    958      1.1  christos         keyfile = NULL;
    959      1.1  christos     }
    960      1.1  christos 
    961      1.1  christos     if (keyfile != NULL) {
    962      1.1  christos         key = load_key(keyfile, keyform, 0, passin, e, "signing key");
    963      1.1  christos         if (key == NULL)
    964      1.1  christos             goto end;
    965      1.1  christos     }
    966      1.1  christos 
    967      1.1  christos     if (digesthex != NULL) {
    968      1.1  christos         if (operation != SMIME_SIGN) {
    969      1.1  christos             BIO_printf(bio_err,
    970  1.1.1.2  christos                 "Cannot use -digest for non-signing operation\n");
    971      1.1  christos             goto end;
    972      1.1  christos         }
    973      1.1  christos         if (infile != NULL
    974      1.1  christos             || (flags & CMS_DETACHED) == 0
    975      1.1  christos             || (flags & CMS_STREAM) != 0) {
    976      1.1  christos             BIO_printf(bio_err,
    977  1.1.1.2  christos                 "Cannot use -digest when -in, -nodetach or streaming is used\n");
    978      1.1  christos             goto end;
    979      1.1  christos         }
    980      1.1  christos         digestbin = OPENSSL_hexstr2buf(digesthex, &digestlen);
    981      1.1  christos         if (digestbin == NULL) {
    982      1.1  christos             BIO_printf(bio_err,
    983  1.1.1.2  christos                 "Invalid hex value after -digest\n");
    984      1.1  christos             goto end;
    985      1.1  christos         }
    986      1.1  christos     } else {
    987      1.1  christos         in = bio_open_default(infile, 'r',
    988  1.1.1.2  christos             binary_files ? FORMAT_BINARY : informat);
    989      1.1  christos         if (in == NULL)
    990      1.1  christos             goto end;
    991      1.1  christos     }
    992      1.1  christos 
    993      1.1  christos     if (operation & SMIME_IP) {
    994      1.1  christos         cms = load_content_info(informat, in, flags, &indata, "SMIME");
    995      1.1  christos         if (cms == NULL)
    996      1.1  christos             goto end;
    997      1.1  christos         if (contfile != NULL) {
    998      1.1  christos             BIO_free(indata);
    999      1.1  christos             if ((indata = BIO_new_file(contfile, "rb")) == NULL) {
   1000      1.1  christos                 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
   1001      1.1  christos                 goto end;
   1002      1.1  christos             }
   1003      1.1  christos         }
   1004      1.1  christos         if (certsoutfile != NULL) {
   1005      1.1  christos             STACK_OF(X509) *allcerts;
   1006      1.1  christos             allcerts = CMS_get1_certs(cms);
   1007      1.1  christos             if (!save_certs(certsoutfile, allcerts)) {
   1008      1.1  christos                 BIO_printf(bio_err,
   1009  1.1.1.2  christos                     "Error writing certs to %s\n", certsoutfile);
   1010      1.1  christos                 ret = 5;
   1011      1.1  christos                 goto end;
   1012      1.1  christos             }
   1013      1.1  christos             OSSL_STACK_OF_X509_free(allcerts);
   1014      1.1  christos         }
   1015      1.1  christos     }
   1016      1.1  christos 
   1017      1.1  christos     if (rctfile != NULL) {
   1018      1.1  christos         char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";
   1019      1.1  christos 
   1020      1.1  christos         if ((rctin = BIO_new_file(rctfile, rctmode)) == NULL) {
   1021      1.1  christos             BIO_printf(bio_err, "Can't open receipt file %s\n", rctfile);
   1022      1.1  christos             goto end;
   1023      1.1  christos         }
   1024      1.1  christos 
   1025      1.1  christos         rcms = load_content_info(rctformat, rctin, 0, NULL, "receipt");
   1026      1.1  christos         if (rcms == NULL)
   1027      1.1  christos             goto end;
   1028      1.1  christos     }
   1029      1.1  christos 
   1030      1.1  christos     out = bio_open_default(outfile, 'w',
   1031  1.1.1.2  christos         binary_files ? FORMAT_BINARY : outformat);
   1032      1.1  christos     if (out == NULL)
   1033      1.1  christos         goto end;
   1034      1.1  christos 
   1035      1.1  christos     if ((operation == SMIME_VERIFY) || (operation == SMIME_VERIFY_RECEIPT)) {
   1036      1.1  christos         if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
   1037  1.1.1.2  christos                  CAstore, noCAstore))
   1038  1.1.1.2  christos             == NULL)
   1039      1.1  christos             goto end;
   1040      1.1  christos         X509_STORE_set_verify_cb(store, cms_cb);
   1041      1.1  christos         if (vpmtouched)
   1042      1.1  christos             X509_STORE_set1_param(store, vpm);
   1043      1.1  christos     }
   1044      1.1  christos 
   1045      1.1  christos     ret = 3;
   1046      1.1  christos 
   1047      1.1  christos     if (operation == SMIME_DATA_CREATE) {
   1048      1.1  christos         cms = CMS_data_create_ex(in, flags, libctx, app_get0_propq());
   1049      1.1  christos     } else if (operation == SMIME_DIGEST_CREATE) {
   1050      1.1  christos         cms = CMS_digest_create_ex(in, sign_md, flags, libctx, app_get0_propq());
   1051      1.1  christos     } else if (operation == SMIME_COMPRESS) {
   1052      1.1  christos         cms = CMS_compress(in, -1, flags);
   1053      1.1  christos     } else if (operation == SMIME_ENCRYPT) {
   1054      1.1  christos         int i;
   1055      1.1  christos         flags |= CMS_PARTIAL;
   1056      1.1  christos         cms = CMS_encrypt_ex(NULL, in, cipher, flags, libctx, app_get0_propq());
   1057      1.1  christos         if (cms == NULL)
   1058      1.1  christos             goto end;
   1059      1.1  christos         for (i = 0; i < sk_X509_num(encerts); i++) {
   1060      1.1  christos             CMS_RecipientInfo *ri;
   1061      1.1  christos             cms_key_param *kparam;
   1062      1.1  christos             int tflags = flags | CMS_KEY_PARAM;
   1063      1.1  christos             /* This flag enforces allocating the EVP_PKEY_CTX for the recipient here */
   1064      1.1  christos             EVP_PKEY_CTX *pctx;
   1065      1.1  christos             X509 *x = sk_X509_value(encerts, i);
   1066      1.1  christos             int res;
   1067      1.1  christos 
   1068      1.1  christos             for (kparam = key_first; kparam; kparam = kparam->next) {
   1069      1.1  christos                 if (kparam->idx == i) {
   1070      1.1  christos                     break;
   1071      1.1  christos                 }
   1072      1.1  christos             }
   1073      1.1  christos             ri = CMS_add1_recipient(cms, x, key, originator, tflags);
   1074      1.1  christos             if (ri == NULL)
   1075      1.1  christos                 goto end;
   1076      1.1  christos 
   1077      1.1  christos             pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
   1078      1.1  christos             if (pctx != NULL && kparam != NULL) {
   1079      1.1  christos                 if (!cms_set_pkey_param(pctx, kparam->param))
   1080      1.1  christos                     goto end;
   1081      1.1  christos             }
   1082      1.1  christos 
   1083      1.1  christos             res = EVP_PKEY_CTX_ctrl(pctx, -1, -1,
   1084  1.1.1.2  christos                 EVP_PKEY_CTRL_CIPHER,
   1085  1.1.1.2  christos                 EVP_CIPHER_get_nid(cipher), NULL);
   1086      1.1  christos             if (res <= 0 && res != -2)
   1087      1.1  christos                 goto end;
   1088      1.1  christos 
   1089      1.1  christos             if (CMS_RecipientInfo_type(ri) == CMS_RECIPINFO_AGREE
   1090  1.1.1.2  christos                 && wrap_cipher != NULL) {
   1091      1.1  christos                 EVP_CIPHER_CTX *wctx;
   1092      1.1  christos                 wctx = CMS_RecipientInfo_kari_get0_ctx(ri);
   1093      1.1  christos                 if (EVP_EncryptInit_ex(wctx, wrap_cipher, NULL, NULL, NULL) != 1)
   1094      1.1  christos                     goto end;
   1095      1.1  christos             }
   1096      1.1  christos         }
   1097      1.1  christos 
   1098      1.1  christos         if (secret_key != NULL) {
   1099      1.1  christos             if (!CMS_add0_recipient_key(cms, NID_undef,
   1100  1.1.1.2  christos                     secret_key, secret_keylen,
   1101  1.1.1.2  christos                     secret_keyid, secret_keyidlen,
   1102  1.1.1.2  christos                     NULL, NULL, NULL))
   1103      1.1  christos                 goto end;
   1104      1.1  christos             /* NULL these because call absorbs them */
   1105      1.1  christos             secret_key = NULL;
   1106      1.1  christos             secret_keyid = NULL;
   1107      1.1  christos         }
   1108      1.1  christos         if (pwri_pass != NULL) {
   1109      1.1  christos             pwri_tmp = (unsigned char *)OPENSSL_strdup((char *)pwri_pass);
   1110      1.1  christos             if (pwri_tmp == NULL)
   1111      1.1  christos                 goto end;
   1112      1.1  christos             if (CMS_add0_recipient_password(cms,
   1113  1.1.1.2  christos                     -1, NID_undef, NID_undef,
   1114  1.1.1.2  christos                     pwri_tmp, -1, NULL)
   1115  1.1.1.2  christos                 == NULL)
   1116      1.1  christos                 goto end;
   1117      1.1  christos             pwri_tmp = NULL;
   1118      1.1  christos         }
   1119      1.1  christos         if (!(flags & CMS_STREAM)) {
   1120      1.1  christos             if (!CMS_final(cms, in, NULL, flags)) {
   1121      1.1  christos                 if (originator != NULL
   1122      1.1  christos                     && ERR_GET_REASON(ERR_peek_error())
   1123  1.1.1.2  christos                         == CMS_R_ERROR_UNSUPPORTED_STATIC_KEY_AGREEMENT) {
   1124      1.1  christos                     BIO_printf(bio_err, "Cannot use originator for encryption\n");
   1125      1.1  christos                     goto end;
   1126      1.1  christos                 }
   1127      1.1  christos                 goto end;
   1128      1.1  christos             }
   1129      1.1  christos         }
   1130      1.1  christos     } else if (operation == SMIME_ENCRYPTED_ENCRYPT) {
   1131      1.1  christos         cms = CMS_EncryptedData_encrypt_ex(in, cipher, secret_key,
   1132  1.1.1.2  christos             secret_keylen, flags, libctx, app_get0_propq());
   1133      1.1  christos 
   1134      1.1  christos     } else if (operation == SMIME_SIGN_RECEIPT) {
   1135      1.1  christos         CMS_ContentInfo *srcms = NULL;
   1136      1.1  christos         STACK_OF(CMS_SignerInfo) *sis;
   1137      1.1  christos         CMS_SignerInfo *si;
   1138      1.1  christos         sis = CMS_get0_SignerInfos(cms);
   1139      1.1  christos         if (sis == NULL)
   1140      1.1  christos             goto end;
   1141      1.1  christos         si = sk_CMS_SignerInfo_value(sis, 0);
   1142      1.1  christos         srcms = CMS_sign_receipt(si, signer, key, other, flags);
   1143      1.1  christos         if (srcms == NULL)
   1144      1.1  christos             goto end;
   1145      1.1  christos         CMS_ContentInfo_free(cms);
   1146      1.1  christos         cms = srcms;
   1147      1.1  christos     } else if (operation & SMIME_SIGNERS) {
   1148      1.1  christos         int i;
   1149      1.1  christos         /*
   1150      1.1  christos          * If detached data content and not signing pre-computed digest, we
   1151      1.1  christos          * enable streaming if S/MIME output format.
   1152      1.1  christos          */
   1153      1.1  christos         if (operation == SMIME_SIGN) {
   1154      1.1  christos 
   1155      1.1  christos             if ((flags & CMS_DETACHED) != 0 && digestbin == NULL) {
   1156      1.1  christos                 if (outformat == FORMAT_SMIME)
   1157      1.1  christos                     flags |= CMS_STREAM;
   1158      1.1  christos             }
   1159      1.1  christos             flags |= CMS_PARTIAL;
   1160      1.1  christos             cms = CMS_sign_ex(NULL, NULL, other, in, flags, libctx, app_get0_propq());
   1161      1.1  christos             if (cms == NULL)
   1162      1.1  christos                 goto end;
   1163      1.1  christos             if (econtent_type != NULL)
   1164      1.1  christos                 CMS_set1_eContentType(cms, econtent_type);
   1165      1.1  christos 
   1166      1.1  christos             if (rr_to != NULL
   1167      1.1  christos                 && ((rr = make_receipt_request(rr_to, rr_allorfirst, rr_from))
   1168      1.1  christos                     == NULL)) {
   1169      1.1  christos                 BIO_puts(bio_err, "Signed Receipt Request Creation Error\n");
   1170      1.1  christos                 goto end;
   1171      1.1  christos             }
   1172      1.1  christos         } else {
   1173      1.1  christos             flags |= CMS_REUSE_DIGEST;
   1174      1.1  christos         }
   1175      1.1  christos         for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
   1176      1.1  christos             CMS_SignerInfo *si;
   1177      1.1  christos             cms_key_param *kparam;
   1178      1.1  christos             int tflags = flags;
   1179      1.1  christos             signerfile = sk_OPENSSL_STRING_value(sksigners, i);
   1180      1.1  christos             keyfile = sk_OPENSSL_STRING_value(skkeys, i);
   1181      1.1  christos 
   1182      1.1  christos             signer = load_cert(signerfile, FORMAT_UNDEF, "signer certificate");
   1183      1.1  christos             if (signer == NULL) {
   1184      1.1  christos                 ret = 2;
   1185      1.1  christos                 goto end;
   1186      1.1  christos             }
   1187      1.1  christos             key = load_key(keyfile, keyform, 0, passin, e, "signing key");
   1188      1.1  christos             if (key == NULL) {
   1189      1.1  christos                 ret = 2;
   1190      1.1  christos                 goto end;
   1191      1.1  christos             }
   1192      1.1  christos 
   1193      1.1  christos             for (kparam = key_first; kparam; kparam = kparam->next) {
   1194      1.1  christos                 if (kparam->idx == i) {
   1195      1.1  christos                     tflags |= CMS_KEY_PARAM;
   1196      1.1  christos                     break;
   1197      1.1  christos                 }
   1198      1.1  christos             }
   1199      1.1  christos             si = CMS_add1_signer(cms, signer, key, sign_md, tflags);
   1200      1.1  christos             if (si == NULL)
   1201      1.1  christos                 goto end;
   1202      1.1  christos             if (kparam != NULL) {
   1203      1.1  christos                 EVP_PKEY_CTX *pctx;
   1204      1.1  christos                 pctx = CMS_SignerInfo_get0_pkey_ctx(si);
   1205      1.1  christos                 if (!cms_set_pkey_param(pctx, kparam->param))
   1206      1.1  christos                     goto end;
   1207      1.1  christos             }
   1208      1.1  christos             if (rr != NULL && !CMS_add1_ReceiptRequest(si, rr))
   1209      1.1  christos                 goto end;
   1210      1.1  christos             X509_free(signer);
   1211      1.1  christos             signer = NULL;
   1212      1.1  christos             EVP_PKEY_free(key);
   1213      1.1  christos             key = NULL;
   1214      1.1  christos         }
   1215      1.1  christos         /* If not streaming or resigning finalize structure */
   1216      1.1  christos         if (operation == SMIME_SIGN && digestbin != NULL
   1217      1.1  christos             && (flags & CMS_STREAM) == 0) {
   1218      1.1  christos             /* Use pre-computed digest instead of content */
   1219      1.1  christos             if (!CMS_final_digest(cms, digestbin, digestlen, NULL, flags))
   1220      1.1  christos                 goto end;
   1221      1.1  christos         } else if (operation == SMIME_SIGN && (flags & CMS_STREAM) == 0) {
   1222      1.1  christos             if (!CMS_final(cms, in, NULL, flags))
   1223      1.1  christos                 goto end;
   1224      1.1  christos         }
   1225      1.1  christos     }
   1226      1.1  christos 
   1227      1.1  christos     if (cms == NULL) {
   1228      1.1  christos         BIO_printf(bio_err, "Error creating CMS structure\n");
   1229      1.1  christos         goto end;
   1230      1.1  christos     }
   1231      1.1  christos 
   1232      1.1  christos     ret = 4;
   1233      1.1  christos     if (operation == SMIME_DECRYPT) {
   1234      1.1  christos         if (flags & CMS_DEBUG_DECRYPT)
   1235      1.1  christos             CMS_decrypt(cms, NULL, NULL, NULL, NULL, flags);
   1236      1.1  christos 
   1237      1.1  christos         if (secret_key != NULL) {
   1238      1.1  christos             if (!CMS_decrypt_set1_key(cms,
   1239  1.1.1.2  christos                     secret_key, secret_keylen,
   1240  1.1.1.2  christos                     secret_keyid, secret_keyidlen)) {
   1241      1.1  christos                 BIO_puts(bio_err, "Error decrypting CMS using secret key\n");
   1242      1.1  christos                 goto end;
   1243      1.1  christos             }
   1244      1.1  christos         }
   1245      1.1  christos 
   1246      1.1  christos         if (key != NULL) {
   1247      1.1  christos             if (!CMS_decrypt_set1_pkey_and_peer(cms, key, recip, originator)) {
   1248      1.1  christos                 BIO_puts(bio_err, "Error decrypting CMS using private key\n");
   1249      1.1  christos                 goto end;
   1250      1.1  christos             }
   1251      1.1  christos         }
   1252      1.1  christos 
   1253      1.1  christos         if (pwri_pass != NULL) {
   1254      1.1  christos             if (!CMS_decrypt_set1_password(cms, pwri_pass, -1)) {
   1255      1.1  christos                 BIO_puts(bio_err, "Error decrypting CMS using password\n");
   1256      1.1  christos                 goto end;
   1257      1.1  christos             }
   1258      1.1  christos         }
   1259      1.1  christos 
   1260      1.1  christos         if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags)) {
   1261      1.1  christos             BIO_printf(bio_err, "Error decrypting CMS structure\n");
   1262      1.1  christos             goto end;
   1263      1.1  christos         }
   1264      1.1  christos     } else if (operation == SMIME_DATA_OUT) {
   1265      1.1  christos         if (!CMS_data(cms, out, flags))
   1266      1.1  christos             goto end;
   1267      1.1  christos     } else if (operation == SMIME_UNCOMPRESS) {
   1268      1.1  christos         if (!CMS_uncompress(cms, indata, out, flags))
   1269      1.1  christos             goto end;
   1270      1.1  christos     } else if (operation == SMIME_DIGEST_VERIFY) {
   1271      1.1  christos         if (CMS_digest_verify(cms, indata, out, flags) > 0) {
   1272      1.1  christos             BIO_printf(bio_err, "Verification successful\n");
   1273      1.1  christos         } else {
   1274      1.1  christos             BIO_printf(bio_err, "Verification failure\n");
   1275      1.1  christos             goto end;
   1276      1.1  christos         }
   1277      1.1  christos     } else if (operation == SMIME_ENCRYPTED_DECRYPT) {
   1278      1.1  christos         if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,
   1279  1.1.1.2  christos                 indata, out, flags))
   1280      1.1  christos             goto end;
   1281      1.1  christos     } else if (operation == SMIME_VERIFY) {
   1282      1.1  christos         if (CMS_verify(cms, other, store, indata, out, flags) > 0) {
   1283      1.1  christos             BIO_printf(bio_err, "%s Verification successful\n",
   1284  1.1.1.2  christos                 (flags & CMS_CADES) != 0 ? "CAdES" : "CMS");
   1285      1.1  christos         } else {
   1286      1.1  christos             BIO_printf(bio_err, "%s Verification failure\n",
   1287  1.1.1.2  christos                 (flags & CMS_CADES) != 0 ? "CAdES" : "CMS");
   1288      1.1  christos             if (verify_retcode)
   1289      1.1  christos                 ret = verify_err + 32;
   1290      1.1  christos             goto end;
   1291      1.1  christos         }
   1292      1.1  christos         if (signerfile != NULL) {
   1293      1.1  christos             STACK_OF(X509) *signers = CMS_get0_signers(cms);
   1294      1.1  christos 
   1295      1.1  christos             if (!save_certs(signerfile, signers)) {
   1296      1.1  christos                 BIO_printf(bio_err,
   1297  1.1.1.2  christos                     "Error writing signers to %s\n", signerfile);
   1298      1.1  christos                 ret = 5;
   1299      1.1  christos                 goto end;
   1300      1.1  christos             }
   1301      1.1  christos             sk_X509_free(signers);
   1302      1.1  christos         }
   1303      1.1  christos         if (rr_print)
   1304      1.1  christos             receipt_request_print(cms);
   1305      1.1  christos 
   1306      1.1  christos     } else if (operation == SMIME_VERIFY_RECEIPT) {
   1307      1.1  christos         if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0) {
   1308      1.1  christos             BIO_printf(bio_err, "Verification successful\n");
   1309      1.1  christos         } else {
   1310      1.1  christos             BIO_printf(bio_err, "Verification failure\n");
   1311      1.1  christos             goto end;
   1312      1.1  christos         }
   1313      1.1  christos     } else {
   1314      1.1  christos         if (noout) {
   1315      1.1  christos             if (print) {
   1316      1.1  christos                 ASN1_PCTX *pctx = NULL;
   1317      1.1  christos                 if (get_nameopt() != XN_FLAG_ONELINE) {
   1318      1.1  christos                     pctx = ASN1_PCTX_new();
   1319      1.1  christos                     if (pctx != NULL) { /* Print anyway if malloc failed */
   1320      1.1  christos                         ASN1_PCTX_set_flags(pctx, ASN1_PCTX_FLAGS_SHOW_ABSENT);
   1321      1.1  christos                         ASN1_PCTX_set_str_flags(pctx, get_nameopt());
   1322      1.1  christos                         ASN1_PCTX_set_nm_flags(pctx, get_nameopt());
   1323      1.1  christos                     }
   1324      1.1  christos                 }
   1325      1.1  christos                 CMS_ContentInfo_print_ctx(out, cms, 0, pctx);
   1326      1.1  christos                 ASN1_PCTX_free(pctx);
   1327      1.1  christos             }
   1328      1.1  christos         } else if (outformat == FORMAT_SMIME) {
   1329      1.1  christos             if (to)
   1330      1.1  christos                 BIO_printf(out, "To: %s%s", to, mime_eol);
   1331      1.1  christos             if (from)
   1332      1.1  christos                 BIO_printf(out, "From: %s%s", from, mime_eol);
   1333      1.1  christos             if (subject)
   1334      1.1  christos                 BIO_printf(out, "Subject: %s%s", subject, mime_eol);
   1335      1.1  christos             if (operation == SMIME_RESIGN)
   1336      1.1  christos                 ret = SMIME_write_CMS(out, cms, indata, flags);
   1337      1.1  christos             else
   1338      1.1  christos                 ret = SMIME_write_CMS(out, cms, in, flags);
   1339      1.1  christos         } else if (outformat == FORMAT_PEM) {
   1340      1.1  christos             ret = PEM_write_bio_CMS_stream(out, cms, in, flags);
   1341      1.1  christos         } else if (outformat == FORMAT_ASN1) {
   1342      1.1  christos             ret = i2d_CMS_bio_stream(out, cms, in, flags);
   1343      1.1  christos         } else {
   1344      1.1  christos             BIO_printf(bio_err, "Bad output format for CMS file\n");
   1345      1.1  christos             goto end;
   1346      1.1  christos         }
   1347      1.1  christos         if (ret <= 0) {
   1348  1.1.1.2  christos             BIO_printf(bio_err, "Error writing CMS output\n");
   1349      1.1  christos             ret = 6;
   1350      1.1  christos             goto end;
   1351      1.1  christos         }
   1352      1.1  christos     }
   1353      1.1  christos     ret = 0;
   1354  1.1.1.2  christos end:
   1355      1.1  christos     if (ret)
   1356      1.1  christos         ERR_print_errors(bio_err);
   1357      1.1  christos     OSSL_STACK_OF_X509_free(encerts);
   1358      1.1  christos     OSSL_STACK_OF_X509_free(other);
   1359      1.1  christos     X509_VERIFY_PARAM_free(vpm);
   1360      1.1  christos     sk_OPENSSL_STRING_free(sksigners);
   1361      1.1  christos     sk_OPENSSL_STRING_free(skkeys);
   1362      1.1  christos     OPENSSL_free(secret_key);
   1363      1.1  christos     OPENSSL_free(secret_keyid);
   1364      1.1  christos     OPENSSL_free(pwri_tmp);
   1365      1.1  christos     ASN1_OBJECT_free(econtent_type);
   1366      1.1  christos     CMS_ReceiptRequest_free(rr);
   1367      1.1  christos     sk_OPENSSL_STRING_free(rr_to);
   1368      1.1  christos     sk_OPENSSL_STRING_free(rr_from);
   1369      1.1  christos     for (key_param = key_first; key_param;) {
   1370      1.1  christos         cms_key_param *tparam;
   1371      1.1  christos         sk_OPENSSL_STRING_free(key_param->param);
   1372      1.1  christos         tparam = key_param->next;
   1373      1.1  christos         OPENSSL_free(key_param);
   1374      1.1  christos         key_param = tparam;
   1375      1.1  christos     }
   1376      1.1  christos     X509_STORE_free(store);
   1377      1.1  christos     X509_free(cert);
   1378      1.1  christos     X509_free(recip);
   1379      1.1  christos     X509_free(signer);
   1380      1.1  christos     X509_free(originator);
   1381      1.1  christos     EVP_PKEY_free(key);
   1382      1.1  christos     EVP_CIPHER_free(cipher);
   1383      1.1  christos     EVP_CIPHER_free(wrap_cipher);
   1384      1.1  christos     EVP_MD_free(sign_md);
   1385      1.1  christos     CMS_ContentInfo_free(cms);
   1386      1.1  christos     CMS_ContentInfo_free(rcms);
   1387      1.1  christos     release_engine(e);
   1388      1.1  christos     BIO_free(rctin);
   1389      1.1  christos     BIO_free(in);
   1390      1.1  christos     BIO_free(indata);
   1391      1.1  christos     BIO_free_all(out);
   1392      1.1  christos     OPENSSL_free(digestbin);
   1393      1.1  christos     OPENSSL_free(passin);
   1394      1.1  christos     NCONF_free(conf);
   1395      1.1  christos     return ret;
   1396      1.1  christos }
   1397      1.1  christos 
   1398      1.1  christos static int save_certs(char *signerfile, STACK_OF(X509) *signers)
   1399      1.1  christos {
   1400      1.1  christos     int i;
   1401      1.1  christos     BIO *tmp;
   1402      1.1  christos     if (signerfile == NULL)
   1403      1.1  christos         return 1;
   1404      1.1  christos     tmp = BIO_new_file(signerfile, "w");
   1405      1.1  christos     if (tmp == NULL)
   1406      1.1  christos         return 0;
   1407      1.1  christos     for (i = 0; i < sk_X509_num(signers); i++)
   1408      1.1  christos         PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
   1409      1.1  christos     BIO_free(tmp);
   1410      1.1  christos     return 1;
   1411      1.1  christos }
   1412      1.1  christos 
   1413      1.1  christos /* Minimal callback just to output policy info (if any) */
   1414      1.1  christos 
   1415      1.1  christos static int cms_cb(int ok, X509_STORE_CTX *ctx)
   1416      1.1  christos {
   1417      1.1  christos     int error;
   1418      1.1  christos 
   1419      1.1  christos     error = X509_STORE_CTX_get_error(ctx);
   1420      1.1  christos 
   1421      1.1  christos     verify_err = error;
   1422      1.1  christos 
   1423      1.1  christos     if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
   1424      1.1  christos         && ((error != X509_V_OK) || (ok != 2)))
   1425      1.1  christos         return ok;
   1426      1.1  christos 
   1427      1.1  christos     policies_print(ctx);
   1428      1.1  christos 
   1429      1.1  christos     return ok;
   1430      1.1  christos }
   1431      1.1  christos 
   1432      1.1  christos static void gnames_stack_print(STACK_OF(GENERAL_NAMES) *gns)
   1433      1.1  christos {
   1434      1.1  christos     STACK_OF(GENERAL_NAME) *gens;
   1435      1.1  christos     GENERAL_NAME *gen;
   1436      1.1  christos     int i, j;
   1437      1.1  christos 
   1438      1.1  christos     for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++) {
   1439      1.1  christos         gens = sk_GENERAL_NAMES_value(gns, i);
   1440      1.1  christos         for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) {
   1441      1.1  christos             gen = sk_GENERAL_NAME_value(gens, j);
   1442      1.1  christos             BIO_puts(bio_err, "    ");
   1443      1.1  christos             GENERAL_NAME_print(bio_err, gen);
   1444      1.1  christos             BIO_puts(bio_err, "\n");
   1445      1.1  christos         }
   1446      1.1  christos     }
   1447      1.1  christos     return;
   1448      1.1  christos }
   1449      1.1  christos 
   1450      1.1  christos static void receipt_request_print(CMS_ContentInfo *cms)
   1451      1.1  christos {
   1452      1.1  christos     STACK_OF(CMS_SignerInfo) *sis;
   1453      1.1  christos     CMS_SignerInfo *si;
   1454      1.1  christos     CMS_ReceiptRequest *rr;
   1455      1.1  christos     int allorfirst;
   1456      1.1  christos     STACK_OF(GENERAL_NAMES) *rto, *rlist;
   1457      1.1  christos     ASN1_STRING *scid;
   1458      1.1  christos     int i, rv;
   1459      1.1  christos     sis = CMS_get0_SignerInfos(cms);
   1460      1.1  christos     for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++) {
   1461      1.1  christos         si = sk_CMS_SignerInfo_value(sis, i);
   1462      1.1  christos         rv = CMS_get1_ReceiptRequest(si, &rr);
   1463      1.1  christos         BIO_printf(bio_err, "Signer %d:\n", i + 1);
   1464      1.1  christos         if (rv == 0) {
   1465      1.1  christos             BIO_puts(bio_err, "  No Receipt Request\n");
   1466      1.1  christos         } else if (rv < 0) {
   1467      1.1  christos             BIO_puts(bio_err, "  Receipt Request Parse Error\n");
   1468      1.1  christos             ERR_print_errors(bio_err);
   1469      1.1  christos         } else {
   1470      1.1  christos             const char *id;
   1471      1.1  christos             int idlen;
   1472      1.1  christos             CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
   1473  1.1.1.2  christos                 &rlist, &rto);
   1474      1.1  christos             BIO_puts(bio_err, "  Signed Content ID:\n");
   1475      1.1  christos             idlen = ASN1_STRING_length(scid);
   1476      1.1  christos             id = (const char *)ASN1_STRING_get0_data(scid);
   1477      1.1  christos             BIO_dump_indent(bio_err, id, idlen, 4);
   1478      1.1  christos             BIO_puts(bio_err, "  Receipts From");
   1479      1.1  christos             if (rlist != NULL) {
   1480      1.1  christos                 BIO_puts(bio_err, " List:\n");
   1481      1.1  christos                 gnames_stack_print(rlist);
   1482      1.1  christos             } else if (allorfirst == 1) {
   1483      1.1  christos                 BIO_puts(bio_err, ": First Tier\n");
   1484      1.1  christos             } else if (allorfirst == 0) {
   1485      1.1  christos                 BIO_puts(bio_err, ": All\n");
   1486      1.1  christos             } else {
   1487      1.1  christos                 BIO_printf(bio_err, " Unknown (%d)\n", allorfirst);
   1488      1.1  christos             }
   1489      1.1  christos             BIO_puts(bio_err, "  Receipts To:\n");
   1490      1.1  christos             gnames_stack_print(rto);
   1491      1.1  christos         }
   1492      1.1  christos         CMS_ReceiptRequest_free(rr);
   1493      1.1  christos     }
   1494      1.1  christos }
   1495      1.1  christos 
   1496      1.1  christos static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK_OF(OPENSSL_STRING) *ns)
   1497      1.1  christos {
   1498      1.1  christos     int i;
   1499      1.1  christos     STACK_OF(GENERAL_NAMES) *ret;
   1500      1.1  christos     GENERAL_NAMES *gens = NULL;
   1501      1.1  christos     GENERAL_NAME *gen = NULL;
   1502      1.1  christos     ret = sk_GENERAL_NAMES_new_null();
   1503      1.1  christos     if (ret == NULL)
   1504      1.1  christos         goto err;
   1505      1.1  christos     for (i = 0; i < sk_OPENSSL_STRING_num(ns); i++) {
   1506      1.1  christos         char *str = sk_OPENSSL_STRING_value(ns, i);
   1507      1.1  christos         gen = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_EMAIL, str, 0);
   1508      1.1  christos         if (gen == NULL)
   1509      1.1  christos             goto err;
   1510      1.1  christos         gens = GENERAL_NAMES_new();
   1511      1.1  christos         if (gens == NULL)
   1512      1.1  christos             goto err;
   1513      1.1  christos         if (!sk_GENERAL_NAME_push(gens, gen))
   1514      1.1  christos             goto err;
   1515      1.1  christos         gen = NULL;
   1516      1.1  christos         if (!sk_GENERAL_NAMES_push(ret, gens))
   1517      1.1  christos             goto err;
   1518      1.1  christos         gens = NULL;
   1519      1.1  christos     }
   1520      1.1  christos 
   1521      1.1  christos     return ret;
   1522      1.1  christos 
   1523  1.1.1.2  christos err:
   1524      1.1  christos     sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free);
   1525      1.1  christos     GENERAL_NAMES_free(gens);
   1526      1.1  christos     GENERAL_NAME_free(gen);
   1527      1.1  christos     return NULL;
   1528      1.1  christos }
   1529      1.1  christos 
   1530  1.1.1.2  christos static CMS_ReceiptRequest *make_receipt_request(STACK_OF(OPENSSL_STRING) *rr_to, int rr_allorfirst,
   1531  1.1.1.2  christos     STACK_OF(OPENSSL_STRING) *rr_from)
   1532      1.1  christos {
   1533      1.1  christos     STACK_OF(GENERAL_NAMES) *rct_to = NULL, *rct_from = NULL;
   1534      1.1  christos     CMS_ReceiptRequest *rr;
   1535      1.1  christos 
   1536      1.1  christos     rct_to = make_names_stack(rr_to);
   1537      1.1  christos     if (rct_to == NULL)
   1538      1.1  christos         goto err;
   1539      1.1  christos     if (rr_from != NULL) {
   1540      1.1  christos         rct_from = make_names_stack(rr_from);
   1541      1.1  christos         if (rct_from == NULL)
   1542      1.1  christos             goto err;
   1543      1.1  christos     } else {
   1544      1.1  christos         rct_from = NULL;
   1545      1.1  christos     }
   1546      1.1  christos     rr = CMS_ReceiptRequest_create0_ex(NULL, -1, rr_allorfirst, rct_from,
   1547  1.1.1.2  christos         rct_to, app_get0_libctx());
   1548      1.1  christos     if (rr == NULL)
   1549      1.1  christos         goto err;
   1550      1.1  christos     return rr;
   1551  1.1.1.2  christos err:
   1552      1.1  christos     sk_GENERAL_NAMES_pop_free(rct_to, GENERAL_NAMES_free);
   1553      1.1  christos     sk_GENERAL_NAMES_pop_free(rct_from, GENERAL_NAMES_free);
   1554      1.1  christos     return NULL;
   1555      1.1  christos }
   1556      1.1  christos 
   1557      1.1  christos static int cms_set_pkey_param(EVP_PKEY_CTX *pctx,
   1558  1.1.1.2  christos     STACK_OF(OPENSSL_STRING) *param)
   1559      1.1  christos {
   1560      1.1  christos     char *keyopt;
   1561      1.1  christos     int i;
   1562      1.1  christos     if (sk_OPENSSL_STRING_num(param) <= 0)
   1563      1.1  christos         return 1;
   1564      1.1  christos     for (i = 0; i < sk_OPENSSL_STRING_num(param); i++) {
   1565      1.1  christos         keyopt = sk_OPENSSL_STRING_value(param, i);
   1566      1.1  christos         if (pkey_ctrl_string(pctx, keyopt) <= 0) {
   1567      1.1  christos             BIO_printf(bio_err, "parameter error \"%s\"\n", keyopt);
   1568      1.1  christos             ERR_print_errors(bio_err);
   1569      1.1  christos             return 0;
   1570      1.1  christos         }
   1571      1.1  christos     }
   1572      1.1  christos     return 1;
   1573      1.1  christos }
   1574