Skip to content

Commit 056d0ba

Browse files
committed
Revert "Move password buffer into the library"
This reverts commit 3db6e798830ca51b179909e8c8cff7e77c2f494e.
1 parent 459e21e commit 056d0ba

File tree

14 files changed

+69
-76
lines changed

14 files changed

+69
-76
lines changed

demos/openssh-privkey.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ static void die_(int err, int line)
2929
#define die(i) do { die_(i, __LINE__); } while(0)
3030
#define DIE(s, ...) do { print_err("%3d: " s "\n", __LINE__, ##__VA_ARGS__); exit(EXIT_FAILURE); } while(0)
3131

32-
static int getpassword(const char *prompt, char *pass, unsigned long *len)
32+
static char* getpassword(const char *prompt, size_t maxlen)
3333
{
34+
char *wr, *end, *pass = XCALLOC(1, maxlen + 1);
3435
struct termios tio;
3536
tcflag_t c_lflag;
36-
unsigned long maxlen = *len, wr = 0;
37+
if (pass == NULL)
38+
return NULL;
39+
wr = pass;
40+
end = pass + maxlen;
3741

3842
tcgetattr(0, &tio);
3943
c_lflag = tio.c_lflag;
@@ -42,25 +46,24 @@ static int getpassword(const char *prompt, char *pass, unsigned long *len)
4246

4347
printf("%s", prompt);
4448
fflush(stdout);
45-
while (1) {
49+
while (pass < end) {
4650
int c = getchar();
4751
if (c == '\r' || c == '\n' || c == -1)
4852
break;
49-
if (wr < maxlen)
50-
pass[wr] = c;
51-
wr++;
53+
*wr++ = c;
5254
}
53-
*len = wr;
5455
tio.c_lflag = c_lflag;
5556
tcsetattr(0, TCSAFLUSH, &tio);
5657
printf("\n");
57-
return wr <= maxlen;
58+
return pass;
5859
}
5960

60-
static int password_get(void *p, unsigned long *l, void *u)
61+
static int password_get(void **p, unsigned long *l, void *u)
6162
{
6263
(void)u;
63-
return getpassword("Enter passphrase: ", p, l);
64+
*p = getpassword("Enter passphrase: ", 256);
65+
*l = strlen(*p);
66+
return 0;
6467
}
6568

6669
static void print(ltc_pka_key *k)

src/headers/tomcrypt_custom.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -627,10 +627,6 @@
627627
#define LTC_PBES
628628
#endif
629629

630-
#if defined(LTC_PEM) || defined(LTC_PKCS_8) && !defined(LTC_MAX_PASSWORD_LEN)
631-
#define LTC_MAX_PASSWORD_LEN 256
632-
#endif
633-
634630
#if defined(LTC_CLEAN_STACK)
635631
/* if you're sure that you want to use it, remove the line below */
636632
#error LTC_CLEAN_STACK is considered as broken

src/headers/tomcrypt_pk.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@ typedef struct {
55
/**
66
Callback function that is called when a password is required.
77
8-
@param str Pointer to where the password shall be stored.
9-
@param len [in/out] The max length resp. resulting length of the password.
8+
Please be aware that the library takes ownership of the pointer that is
9+
returned to the library via `str`.
10+
`str` shall be allocated via the same function as `XMALLOC` points to.
11+
The data will be zeroed and `XFREE`'d as soon as it isn't required anymore.
12+
13+
@param str Pointer to pointer where the password will be stored.
14+
@param len Pointer to the length of the password.
1015
@param userdata `userdata` that was passed in the `password_ctx` struct.
1116
@return CRYPT_OK on success
1217
*/
13-
int (*callback)(void *str, unsigned long *len, void *userdata);
18+
int (*callback)(void **str, unsigned long *len, void *userdata);
1419
/** Opaque `userdata` pointer passed when the callback is called */
1520
void *userdata;
1621
} password_ctx;

src/headers/tomcrypt_private.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,11 @@ typedef struct {
8383
unsigned long blocklen;
8484
} pbes_properties;
8585

86-
struct password {
87-
/* usually a `char*` but could also contain binary data
88-
* so use a `void*` + length to be on the safe side.
89-
*/
90-
unsigned char pw[LTC_MAX_PASSWORD_LEN];
91-
unsigned long l;
92-
};
93-
9486
typedef struct
9587
{
9688
pbes_properties type;
97-
struct password pwd;
89+
void *pwd;
90+
unsigned long pwdlen;
9891
ltc_asn1_list *enc_data;
9992
ltc_asn1_list *salt;
10093
ltc_asn1_list *iv;
@@ -266,6 +259,14 @@ enum cipher_mode {
266259
cm_none, cm_cbc, cm_cfb, cm_ctr, cm_ofb, cm_stream, cm_gcm
267260
};
268261

262+
struct password {
263+
/* usually a `char*` but could also contain binary data
264+
* so use a `void*` + length to be on the safe side.
265+
*/
266+
void *pw;
267+
unsigned long l;
268+
};
269+
269270
struct blockcipher_info {
270271
const char *name;
271272
const char *algo;

src/misc/crypt/crypt.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,6 @@ const char *crypt_build_settings =
470470
" PBES1 "
471471
" PBES2 "
472472
#endif
473-
#if defined(LTC_MAX_PASSWORD_LEN)
474-
" " NAME_VALUE(LTC_MAX_PASSWORD_LEN) " "
475-
#endif
476473
#if defined(LTC_PEM)
477474
" PEM "
478475
" " NAME_VALUE(LTC_PEM_DECODE_BUFSZ) " "

src/misc/pbes/pbes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ int pbes_decrypt(const pbes_arg *arg, unsigned char *dec_data, unsigned long *d
5050

5151
if (klen > sizeof(k)) return CRYPT_INVALID_ARG;
5252

53-
if ((err = arg->type.kdf(arg->pwd.pw, arg->pwd.l, arg->salt->data, arg->salt->size, arg->iterations, hid, k, &klen)) != CRYPT_OK) goto LBL_ERROR;
53+
if ((err = arg->type.kdf(arg->pwd, arg->pwdlen, arg->salt->data, arg->salt->size, arg->iterations, hid, k, &klen)) != CRYPT_OK) goto LBL_ERROR;
5454
if ((err = cbc_start(cid, iv, k, keylen, 0, &cbc)) != CRYPT_OK) goto LBL_ERROR;
5555
if ((err = cbc_decrypt(arg->enc_data->data, dec_data, arg->enc_data->size, &cbc)) != CRYPT_OK) goto LBL_ERROR;
5656
if ((err = cbc_done(&cbc)) != CRYPT_OK) goto LBL_ERROR;

src/misc/pem/pem_pkcs.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ static int s_decrypt_pem(unsigned char *pem, unsigned long *l, const struct pem_
2525
if (hdr->info.keylen > sizeof(key)) {
2626
return CRYPT_BUFFER_OVERFLOW;
2727
}
28+
if (!hdr->pw->pw) {
29+
return CRYPT_INVALID_ARG;
30+
}
2831

2932
ivlen = sizeof(iv);
3033
if ((err = base16_decode(hdr->info.iv, XSTRLEN(hdr->info.iv), iv, &ivlen)) != CRYPT_OK) {
@@ -199,7 +202,7 @@ static int s_decode(struct get_char *g, ltc_pka_key *k, const password_ctx *pw_c
199202
unsigned long w, l, n;
200203
int err = CRYPT_ERROR;
201204
struct pem_headers hdr = { 0 };
202-
struct password pw = { 0 };
205+
struct password pw;
203206
enum ltc_pka_id pka;
204207
XMEMSET(k, 0, sizeof(*k));
205208
w = LTC_PEM_READ_BUFSIZE * 2;
@@ -238,8 +241,7 @@ static int s_decode(struct get_char *g, ltc_pka_key *k, const password_ctx *pw_c
238241
}
239242

240243
hdr.pw = &pw;
241-
hdr.pw->l = LTC_MAX_PASSWORD_LEN;
242-
if (pw_ctx->callback(hdr.pw->pw, &hdr.pw->l, pw_ctx->userdata)) {
244+
if (pw_ctx->callback(&hdr.pw->pw, &hdr.pw->l, pw_ctx->userdata)) {
243245
err = CRYPT_ERROR;
244246
goto cleanup;
245247
}
@@ -264,7 +266,8 @@ static int s_decode(struct get_char *g, ltc_pka_key *k, const password_ctx *pw_c
264266

265267
cleanup:
266268
if (hdr.pw) {
267-
zeromem(hdr.pw->pw, sizeof(hdr.pw->pw));
269+
zeromem(hdr.pw->pw, hdr.pw->l);
270+
XFREE(hdr.pw->pw);
268271
}
269272
XFREE(pem);
270273
return err;

src/misc/pem/pem_ssh.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -570,8 +570,7 @@ static int s_decode_openssh(struct get_char *g, ltc_pka_key *k, const password_c
570570
err = CRYPT_PW_CTX_MISSING;
571571
goto cleanup;
572572
}
573-
opts.pw.l = LTC_MAX_PASSWORD_LEN;
574-
if (pw_ctx->callback(opts.pw.pw, &opts.pw.l, pw_ctx->userdata)) {
573+
if (pw_ctx->callback(&opts.pw.pw, &opts.pw.l, pw_ctx->userdata)) {
575574
err = CRYPT_ERROR;
576575
goto cleanup;
577576
}
@@ -590,8 +589,8 @@ static int s_decode_openssh(struct get_char *g, ltc_pka_key *k, const password_c
590589
}
591590

592591
cleanup:
593-
if (opts.pw.l) {
594-
zeromem(&opts.pw, sizeof(opts.pw));
592+
if (opts.pw.pw) {
593+
XFREE(opts.pw.pw);
595594
}
596595
if (privkey) {
597596
zeromem(privkey, privkey_len);

src/pk/asn1/pkcs8/pkcs8_decode_flexi.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ int pkcs8_decode_flexi(const unsigned char *in, unsigned long inlen,
2323
unsigned char *dec_data = NULL;
2424
ltc_asn1_list *l = NULL;
2525
int err;
26-
pbes_arg pbes = { 0 };
26+
pbes_arg pbes;
2727

2828
LTC_ARGCHK(in != NULL);
2929
LTC_ARGCHK(decoded_list != NULL);
3030

31+
XMEMSET(&pbes, 0, sizeof(pbes));
32+
3133
*decoded_list = NULL;
3234
if ((err = der_decode_sequence_flexi(in, &len, &l)) == CRYPT_OK) {
3335
/* the following "if" detects whether it is encrypted or not */
@@ -61,8 +63,7 @@ int pkcs8_decode_flexi(const unsigned char *in, unsigned long inlen,
6163
goto LBL_DONE;
6264
}
6365

64-
pbes.pwd.l = LTC_MAX_PASSWORD_LEN;
65-
if (pw_ctx->callback(pbes.pwd.pw, &pbes.pwd.l, pw_ctx->userdata)) {
66+
if (pw_ctx->callback(&pbes.pwd, &pbes.pwdlen, pw_ctx->userdata)) {
6667
err = CRYPT_ERROR;
6768
goto LBL_DONE;
6869
}
@@ -94,8 +95,9 @@ int pkcs8_decode_flexi(const unsigned char *in, unsigned long inlen,
9495

9596
LBL_DONE:
9697
if (l) der_free_sequence_flexi(l);
97-
if (pbes.pwd.l) {
98-
zeromem(&pbes.pwd, sizeof(pbes.pwd));
98+
if (pbes.pwd) {
99+
zeromem(pbes.pwd, pbes.pwdlen);
100+
XFREE(pbes.pwd);
99101
}
100102
if (dec_data) {
101103
zeromem(dec_data, dec_size);

tests/ecc_test.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -660,14 +660,12 @@ static int s_ecc_new_api(void)
660660
}
661661

662662

663-
static int password_get(void *p, unsigned long *l, void *u)
663+
static int password_get(void **p, unsigned long *l, void *u)
664664
{
665-
int ret = *l < 6;
666665
LTC_UNUSED_PARAM(u);
667-
if (!ret)
668-
XMEMCPY(p, "secret", 6);
666+
*p = strdup("secret");
669667
*l = 6;
670-
return ret;
668+
return 0;
671669
}
672670

673671
static int s_ecc_import_export(void) {

0 commit comments

Comments
 (0)