summaryrefslogtreecommitdiffstats
path: root/src/rnpkeys/tui.cpp
blob: 73f26dc2d1ff94a478e3b2a5217cddc03a2c33ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
 * Copyright (c) 2017-2020, [Ribose Inc](https://www.ribose.com).
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1.  Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *
 * 2.  Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <algorithm>
#ifdef _MSC_VER
#include "uniwin.h"
#else
#include <unistd.h>
#endif
#include <errno.h>
#include <iterator>
#include "rnp/rnpcfg.h"
#include "rnpkeys.h"
#include "defaults.h"
#include "file-utils.h"
#include "logging.h"

/* -----------------------------------------------------------------------------
 * @brief   Reads input from file pointer and converts it securelly to ints
 *          Partially based on ERR34-C from SEI CERT C Coding Standard
 *
 * @param   fp          pointer to opened pipe
 * @param   result[out] result read from file pointer and converted to int
 *
 * @returns true and value in result if integer was parsed correctly,
 *          otherwise false
 *
-------------------------------------------------------------------------------- */
static bool
rnp_secure_get_long_from_fd(FILE *fp, long &result, bool allow_empty = true)
{
    char buff[BUFSIZ];
    if (!fgets(buff, sizeof(buff), fp)) {
        RNP_LOG("EOF or read error");
        return false;
    }

    errno = 0;
    char *end_ptr = NULL;
    long  num_long = strtol(buff, &end_ptr, 10);
    if (ERANGE == errno) {
        RNP_LOG("Number out of range");
        return false;
    }
    if (end_ptr == buff) {
        return allow_empty;
    }
    if ('\n' != *end_ptr && '\0' != *end_ptr) {
        RNP_LOG("Unexpected end of line");
        return false;
    }

    result = num_long;
    return true;
}

static bool
is_rsa_keysize_supported(uint32_t keysize)
{
    return ((keysize >= 1024) && (keysize <= 4096) && !(keysize % 8));
}

static const char *
ask_curve_name(FILE *input_fp)
{
    std::vector<const char *> curves;
    static const char *const  known_curves[] = {
      "NIST P-256",
      "NIST P-384",
      "NIST P-521",
      "brainpoolP256r1",
      "brainpoolP384r1",
      "brainpoolP512r1",
      "secp256k1",
    };
    const size_t curvenum = sizeof(known_curves) / sizeof(*known_curves);

    try {
        std::copy_if(known_curves,
                     known_curves + curvenum,
                     std::back_inserter(curves),
                     [](const char *curve) {
                         bool supported = false;
                         return !rnp_supports_feature(RNP_FEATURE_CURVE, curve, &supported) &&
                                supported;
                     });
    } catch (const std::exception &e) {
        RNP_LOG("%s", e.what());
        return NULL;
    }
    const size_t ccount = curves.size();
    if (!ccount) {
        return NULL;
    }
    bool        ok = false;
    const char *result = NULL;
    int         attempts = 0;
    do {
        if (attempts >= 10) {
            printf("Too many attempts. Aborting.\n");
            return NULL;
        }
        printf("Please select which elliptic curve you want:\n");
        for (size_t i = 0; i < ccount; i++) {
            printf("\t(%zu) %s\n", i + 1, curves[i]);
        }
        printf("(default %s)> ", DEFAULT_CURVE);
        long val = 0;
        ok = rnp_secure_get_long_from_fd(input_fp, val) && (val > 0) && (val <= (long) ccount);
        if (ok) {
            result = curves[val - 1];
        }
        attempts++;
    } while (!ok);

    return result;
}

static long
ask_rsa_bitlen(FILE *input_fp)
{
    long result = 0;
    do {
        result = DEFAULT_RSA_NUMBITS;
        printf("Please provide bit length of the key (between 1024 and 4096):\n(default %d)> ",
               DEFAULT_RSA_NUMBITS);
    } while (!rnp_secure_get_long_from_fd(input_fp, result) ||
             !is_rsa_keysize_supported(result));
    return result;
}

static long
ask_elgamal_bitlen(FILE *input_fp)
{
    do {
        printf(
          "Please provide bit length of the ElGamal key (between %d and %d):\n(default %d) > ",
          ELGAMAL_MIN_P_BITLEN,
          ELGAMAL_MAX_P_BITLEN,
          DEFAULT_ELGAMAL_NUMBITS);
        long result = DEFAULT_ELGAMAL_NUMBITS;
        if (!rnp_secure_get_long_from_fd(input_fp, result)) {
            continue;
        }
        if ((result >= ELGAMAL_MIN_P_BITLEN) && (result <= ELGAMAL_MAX_P_BITLEN)) {
            // round up to multiple of 32
            result = ((result + 31) / 32) * 32;
            printf("Bitlen of the key will be %lu\n", result);
            return result;
        }
    } while (1);
}

static long
ask_dsa_bitlen(FILE *input_fp)
{
    do {
        printf(
          "Please provide bit length of the DSA key (between %d and %d):\n(default %d) > ",
          DSA_MIN_P_BITLEN,
          DSA_MAX_P_BITLEN,
          DSA_DEFAULT_P_BITLEN);
        long result = DSA_DEFAULT_P_BITLEN;
        if (!rnp_secure_get_long_from_fd(input_fp, result)) {
            continue;
        }
        if ((result >= DSA_MIN_P_BITLEN) && (result <= DSA_MAX_P_BITLEN)) {
            // round up to multiple of 64
            result = ((result + 63) / 64) * 64;
            printf("Bitlen of the key will be %lu\n", result);
            return result;
        }
    } while (1);
}

static bool
rnpkeys_ask_generate_params(rnp_cfg &cfg, FILE *input_fp)
{
    long option = 0;
    do {
        printf("Please select what kind of key you want:\n"
               "\t(1)  RSA (Encrypt or Sign)\n"
               "\t(16) DSA + ElGamal\n"
               "\t(17) DSA + RSA\n" // TODO: See #584
               "\t(19) ECDSA + ECDH\n"
               "\t(22) EDDSA + X25519\n"
               "\t(99) SM2\n"
               "> ");
        if (!rnp_secure_get_long_from_fd(input_fp, option, false)) {
            option = 0;
            continue;
        }
        switch (option) {
        case 1: {
            int bits = ask_rsa_bitlen(input_fp);
            cfg.set_str(CFG_KG_PRIMARY_ALG, RNP_ALGNAME_RSA);
            cfg.set_str(CFG_KG_SUBKEY_ALG, RNP_ALGNAME_RSA);
            cfg.set_int(CFG_KG_PRIMARY_BITS, bits);
            cfg.set_int(CFG_KG_SUBKEY_BITS, bits);
            break;
        }
        case 16: {
            int bits = ask_dsa_bitlen(input_fp);
            cfg.set_str(CFG_KG_PRIMARY_ALG, RNP_ALGNAME_DSA);
            cfg.set_str(CFG_KG_SUBKEY_ALG, RNP_ALGNAME_ELGAMAL);
            cfg.set_int(CFG_KG_PRIMARY_BITS, bits);
            cfg.set_int(CFG_KG_SUBKEY_BITS, bits);
            break;
        }
        case 17: {
            int bits = ask_dsa_bitlen(input_fp);
            cfg.set_str(CFG_KG_PRIMARY_ALG, RNP_ALGNAME_DSA);
            cfg.set_str(CFG_KG_SUBKEY_ALG, RNP_ALGNAME_RSA);
            cfg.set_int(CFG_KG_PRIMARY_BITS, bits);
            cfg.set_int(CFG_KG_SUBKEY_BITS, bits);
            break;
        }
        case 19: {
            const char *curve = ask_curve_name(input_fp);
            if (!curve) {
                return false;
            }
            cfg.set_str(CFG_KG_PRIMARY_ALG, RNP_ALGNAME_ECDSA);
            cfg.set_str(CFG_KG_SUBKEY_ALG, RNP_ALGNAME_ECDH);
            cfg.set_str(CFG_KG_PRIMARY_CURVE, curve);
            cfg.set_str(CFG_KG_SUBKEY_CURVE, curve);
            break;
        }
        case 22: {
            cfg.set_str(CFG_KG_PRIMARY_ALG, RNP_ALGNAME_EDDSA);
            cfg.set_str(CFG_KG_SUBKEY_ALG, RNP_ALGNAME_ECDH);
            cfg.set_str(CFG_KG_SUBKEY_CURVE, "Curve25519");
            break;
        }
        case 99: {
            cfg.set_str(CFG_KG_PRIMARY_ALG, RNP_ALGNAME_SM2);
            cfg.set_str(CFG_KG_SUBKEY_ALG, RNP_ALGNAME_SM2);
            if (!cfg.has(CFG_KG_HASH)) {
                cfg.set_str(CFG_KG_HASH, RNP_ALGNAME_SM3);
            }
            break;
        }
        default:
            option = 0;
            break;
        }
    } while (!option);

    return true;
}

static bool
rnpkeys_ask_generate_params_subkey(rnp_cfg &cfg, FILE *input_fp)
{
    long option = 0;
    do {
        printf("Please select subkey algorithm you want:\n"
               "\t(1)  RSA\n"
               "\t(16) ElGamal\n"
               "\t(17) DSA\n"
               "\t(18) ECDH\n"
               "\t(19) ECDSA\n"
               "\t(22) EDDSA\n"
               "\t(99) SM2"
               "> ");
        if (!rnp_secure_get_long_from_fd(input_fp, option, false)) {
            option = 0;
            continue;
        }
        switch (option) {
        case 1: {
            int bits = ask_rsa_bitlen(input_fp);
            cfg.set_str(CFG_KG_SUBKEY_ALG, RNP_ALGNAME_RSA);
            cfg.set_int(CFG_KG_SUBKEY_BITS, bits);
            break;
        }
        case 16: {
            int bits = ask_elgamal_bitlen(input_fp);
            cfg.set_str(CFG_KG_SUBKEY_ALG, RNP_ALGNAME_ELGAMAL);
            cfg.set_int(CFG_KG_SUBKEY_BITS, bits);
            break;
        }
        case 17: {
            int bits = ask_dsa_bitlen(input_fp);
            cfg.set_str(CFG_KG_SUBKEY_ALG, RNP_ALGNAME_DSA);
            cfg.set_int(CFG_KG_SUBKEY_BITS, bits);
            break;
        }
        case 18: {
            const char *curve = ask_curve_name(input_fp);
            if (!curve) {
                return false;
            }
            cfg.set_str(CFG_KG_SUBKEY_ALG, RNP_ALGNAME_ECDH);
            cfg.set_str(CFG_KG_SUBKEY_CURVE, curve);
            break;
        }
        case 19: {
            const char *curve = ask_curve_name(input_fp);
            if (!curve) {
                return false;
            }
            cfg.set_str(CFG_KG_SUBKEY_ALG, RNP_ALGNAME_ECDSA);
            cfg.set_str(CFG_KG_SUBKEY_CURVE, curve);
            break;
        }
        case 22: {
            cfg.set_str(CFG_KG_SUBKEY_ALG, RNP_ALGNAME_EDDSA);
            break;
        }
        case 99: {
            cfg.set_str(CFG_KG_SUBKEY_ALG, RNP_ALGNAME_SM2);
            if (!cfg.has(CFG_KG_HASH)) {
                cfg.set_str(CFG_KG_HASH, RNP_ALGNAME_SM3);
            }
            break;
        }
        default:
            option = 0;
            break;
        }
    } while (!option);

    return true;
}

bool
cli_rnp_set_generate_params(rnp_cfg &cfg, bool subkey)
{
    bool res = true;
    // hash algorithms for signing and protection
    if (cfg.has(CFG_HASH)) {
        cfg.set_str(CFG_KG_HASH, cfg.get_str(CFG_HASH));
        cfg.set_str(CFG_KG_PROT_HASH, cfg.get_str(CFG_HASH));
    }

    // key and subkey algorithms, bit length/curve
    if (!cfg.get_bool(CFG_EXPERT)) {
        cfg.set_str(CFG_KG_PRIMARY_ALG, RNP_ALGNAME_RSA);
        cfg.set_int(CFG_KG_PRIMARY_BITS, cfg.get_int(CFG_NUMBITS));
        cfg.set_str(CFG_KG_SUBKEY_ALG, RNP_ALGNAME_RSA);
        cfg.set_int(CFG_KG_SUBKEY_BITS, cfg.get_int(CFG_NUMBITS));
    } else {
        FILE *input = stdin;
        if (cfg.has(CFG_USERINPUTFD)) {
            int inputfd = dup(cfg.get_int(CFG_USERINPUTFD));
            if (inputfd != -1) {
                input = rnp_fdopen(inputfd, "r");
                if (!input) {
                    close(inputfd);
                }
            }
        }
        if (!input) {
            return false;
        }
        if (subkey) {
            res = rnpkeys_ask_generate_params_subkey(cfg, input);
        } else {
            res = rnpkeys_ask_generate_params(cfg, input);
        }
        if (input != stdin) {
            fclose(input);
        }
        if (!res) {
            return false;
        }
    }

    // make sure hash algorithms are set
    if (!cfg.has(CFG_KG_HASH)) {
        cfg.set_str(CFG_KG_HASH, DEFAULT_HASH_ALG);
    }
    if (!cfg.has(CFG_KG_PROT_HASH)) {
        cfg.set_str(CFG_KG_PROT_HASH, DEFAULT_HASH_ALG);
    }

    // protection symmetric algorithm
    cfg.set_str(CFG_KG_PROT_ALG,
                cfg.has(CFG_CIPHER) ? cfg.get_str(CFG_CIPHER) : DEFAULT_SYMM_ALG);
    // protection iterations count
    size_t iterations = cfg.get_int(CFG_S2K_ITER);
    if (!iterations) {
        res = res && !rnp_calculate_iterations(cfg.get_str(CFG_KG_PROT_HASH).c_str(),
                                               cfg.get_int(CFG_S2K_MSEC),
                                               &iterations);
    }
    cfg.set_int(CFG_KG_PROT_ITERATIONS, iterations);
    return res;
}