1 /*
2  * Copyright (c) 2017, [Ribose Inc](https://www.ribose.com).
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26 #ifndef RNP_CFG_H_
27 #define RNP_CFG_H_
28 
29 #include <stdbool.h>
30 #include <stdint.h>
31 #include <string>
32 #include <vector>
33 #include <unordered_map>
34 
35 /* cfg variables known by rnp */
36 #define CFG_OVERWRITE "overwrite" /* overwrite output file if it is already exist or fail */
37 #define CFG_ARMOR "armor"         /* armor output data or not */
38 #define CFG_ARMOR_DATA_TYPE "armor_type" /* armor data type, used with ``enarmor`` option */
39 #define CFG_COMMAND "command"            /* command to execute over input data */
40 #define CFG_DETACHED "detached"          /* produce the detached signature */
41 #define CFG_CLEARTEXT "cleartext"        /* cleartext signing should be used */
42 #define CFG_SIGN_NEEDED "sign_needed"    /* signing is needed during data protection */
43 #define CFG_OUTFILE "outfile"            /* name/path of the output file */
44 #define CFG_NO_OUTPUT "no_output"        /* do not output any data - just verify or process */
45 #define CFG_INFILE "infile"              /* name/path of the input file */
46 #define CFG_RESULTS "results"            /* name/path for results, not used right now */
47 #define CFG_KEYSTOREFMT "keystorefmt"    /* keyring format : GPG, G10, KBX */
48 #define CFG_COREDUMPS "coredumps"        /* enable/disable core dumps. 1 or 0. */
49 #define CFG_NEEDSSECKEY "needsseckey"    /* needs secret key for the ongoing operation */
50 #define CFG_USERID "userid"              /* userid for the ongoing operation */
51 #define CFG_RECIPIENTS "recipients"      /* list of encrypted data recipients */
52 #define CFG_SIGNERS "signers"            /* list of signers */
53 #define CFG_VERBOSE "verbose"            /* verbose logging */
54 #define CFG_HOMEDIR "homedir"            /* home directory - folder with keyrings and so on */
55 #define CFG_KEYFILE "keyfile"     /* path to the file with key(s), used instead of keyring */
56 #define CFG_PASSFD "pass-fd"      /* password file descriptor */
57 #define CFG_PASSWD "password"     /* password as command-line constant */
58 #define CFG_PASSWORDC "passwordc" /* number of passwords for symmetric encryption */
59 #define CFG_USERINPUTFD "user-input-fd" /* user input file descriptor */
60 #define CFG_NUMTRIES "numtries"         /* number of password request tries, or 'unlimited' */
61 #define CFG_EXPIRATION "expiration"     /* signature expiration time */
62 #define CFG_CREATION "creation"         /* signature validity start */
63 #define CFG_CIPHER "cipher"             /* symmetric encryption algorithm as string */
64 #define CFG_HASH "hash"                 /* hash algorithm used, string like 'SHA1'*/
65 #define CFG_S2K_ITER "s2k-iter"         /* number of S2K hash iterations to perform */
66 #define CFG_S2K_MSEC "s2k-msec"         /* number of milliseconds S2K should target */
67 #define CFG_ENCRYPT_PK "encrypt_pk"     /* public key should be used during encryption */
68 #define CFG_ENCRYPT_SK "encrypt_sk"     /* password encryption should be used */
69 #define CFG_IO_RESS "ress"              /* results stream */
70 #define CFG_NUMBITS "numbits"           /* number of bits in generated key */
71 #define CFG_KEYFORMAT "format"          /* key format : "human" for human-readable or ... */
72 #define CFG_EXPERT "expert"             /* expert key generation mode */
73 #define CFG_ZLEVEL "zlevel"             /* compression level: 0..9 (0 for no compression) */
74 #define CFG_ZALG "zalg"                 /* compression algorithm: zip, zlib or bzip2 */
75 #define CFG_AEAD "aead"                 /* if nonzero then AEAD enryption mode, int */
76 #define CFG_AEAD_CHUNK "aead_chunk"     /* AEAD chunk size bits, int from 0 to 56 */
77 #define CFG_KEYSTORE_DISABLED \
78     "disable_keystore"              /* indicates whether keystore must be initialized */
79 #define CFG_FORCE "force"           /* force command to succeed operation */
80 #define CFG_SECRET "secret"         /* indicates operation on secret key */
81 #define CFG_WITH_SIGS "with-sigs"   /* list keys with signatures */
82 #define CFG_JSON "json"             /* list packets with JSON output */
83 #define CFG_GRIPS "grips"           /* dump grips when dumping key packets */
84 #define CFG_MPIS "mpis"             /* dump MPI values when dumping packets */
85 #define CFG_RAW "raw"               /* dump raw packet contents */
86 #define CFG_REV_TYPE "rev-type"     /* revocation reason code */
87 #define CFG_REV_REASON "rev-reason" /* revocation reason human-readable string */
88 #define CFG_PERMISSIVE "permissive" /* ignore bad packets during key import */
89 
90 /* rnp keyring setup variables */
91 #define CFG_KR_PUB_FORMAT "kr-pub-format"
92 #define CFG_KR_SEC_FORMAT "kr-sec-format"
93 #define CFG_KR_PUB_PATH "kr-pub-path"
94 #define CFG_KR_SEC_PATH "kr-sec-path"
95 #define CFG_KR_DEF_KEY "kr-def-key"
96 
97 /* key generation variables */
98 #define CFG_KG_PRIMARY_ALG "kg-primary-alg"
99 #define CFG_KG_PRIMARY_BITS "kg-primary-bits"
100 #define CFG_KG_PRIMARY_CURVE "kg-primary-curve"
101 #define CFG_KG_PRIMARY_EXPIRATION "kg-primary-expiration"
102 #define CFG_KG_SUBKEY_ALG "kg-subkey-alg"
103 #define CFG_KG_SUBKEY_BITS "kg-subkey-bits"
104 #define CFG_KG_SUBKEY_CURVE "kg-subkey-curve"
105 #define CFG_KG_SUBKEY_EXPIRATION "kg-subkey-expiration"
106 #define CFG_KG_HASH "kg-hash"
107 #define CFG_KG_PROT_HASH "kg-prot-hash"
108 #define CFG_KG_PROT_ALG "kg-prot-alg"
109 #define CFG_KG_PROT_ITERATIONS "kg-prot-iterations"
110 
111 /* rnp CLI config : contains all the system-dependent and specified by the user configuration
112  * options */
113 class rnp_cfg_val;
114 
115 class rnp_cfg {
116   private:
117     std::unordered_map<std::string, rnp_cfg_val *> vals_;
118     std::string                                    empty_str_;
119 
120   public:
121     /** @brief load default settings */
122     void load_defaults();
123     /** @brief set string value for the key in config */
124     void set_str(const std::string &key, const std::string &val);
125     void set_str(const std::string &key, const char *val);
126     /** @brief set int value for the key in config */
127     void set_int(const std::string &key, int val);
128     /** @brief set bool value for the key in config */
129     void set_bool(const std::string &key, bool val);
130     /** @brief remove key and corresponding value from the config */
131     void unset(const std::string &key);
132     /** @brief add string item to the list value */
133     void add_str(const std::string &key, const std::string &val);
134     /** @brief check whether config has value for the key */
135     bool has(const std::string &key) const;
136     /** @brief get string value from the config. If it is absent then empty string will be
137      *         returned */
138     const std::string &get_str(const std::string &key) const;
139     /** @brief get C string value from the config. Will return 0 instead of empty string if
140      * value is absent. */
141     const char *get_cstr(const std::string &key) const;
142     /** @brief get int value from the config. If it is absent then def will be returned */
143     int get_int(const std::string &key, int def = 0) const;
144     /** @brief get bool value from the config. If it is absent then false will be returned */
145     bool get_bool(const std::string &key) const;
146     /** @brief get number of items in the string list value. If it is absent then 0 will be
147      *         returned. */
148     size_t get_count(const std::string &key) const;
149     /** @brief get string from the list value at the corresponding position. If there is no
150      *         corresponding value or index too large then empty string will be returned. */
151     const std::string &get_str(const std::string &key, size_t idx) const;
152     /** @brief get all strings from the list value */
153     std::vector<std::string> get_list(const std::string &key) const;
154     /** @brief get number of the password tries */
155     int get_pswdtries() const;
156     /** @brief get hash algorithm */
157     const std::string get_hashalg() const;
158     /** @brief copy or override a configuration.
159      *  @param src vals will be overridden (if key exist) or copied (if not) from this object
160      */
161     void copy(const rnp_cfg &src);
162     void clear();
163     /* delete unneeded operators */
164     rnp_cfg &operator=(const rnp_cfg &src) = delete;
165     rnp_cfg &operator=(const rnp_cfg &&src) = delete;
166     /** @brief destructor */
167     ~rnp_cfg();
168 };
169 
170 /* rnp CLI helper functions */
171 
172 /** @brief Get signature validity expiration time from the user input
173  *
174  *  Signature expiration may be specified in different formats:
175  *  - 10d : 10 days (you can use [h]ours, d[ays], [w]eeks, [m]onthes)
176  *  - 2017-07-12 : as the exact date when signature becomes invalid
177  *  - 60000 : number of seconds
178  *
179  *  @param s [in] NULL-terminated string with the date
180  *  @param t [out] On successfull return result will be placed here
181  *  @return 0 on success
182  *          -1 on parse error
183  *          -2 if a date in the past was specified
184  *          -3 overflow
185  */
186 int get_expiration(const char *s, uint32_t *t);
187 
188 /** @brief Get signature validity start time from the user input
189  *
190  *  Signature validity may be specified in different formats:
191  *  - 2017-07-12 : as the exact date when signature becomes invalid
192  *  - 1499334073 : timestamp
193  *
194  *  @param s [in] NULL-terminated string with the date
195  *  @return timestamp of the validity start
196  */
197 int64_t get_creation(const char *s);
198 
199 #endif
200