Skip to content

Commit 8fa3795

Browse files
committed
clean-up a bit
* more `const` correctness * take `LTC_NO_FILE` into account * only declare `extern` variables where they're required * ensure keys don't contain stale data * ensure input arguments are valid * add `CRYPT_PW_CTX_MISSING` error code * fix documentation Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
1 parent 49fb08a commit 8fa3795

File tree

9 files changed

+59
-30
lines changed

9 files changed

+59
-30
lines changed

src/headers/tomcrypt.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ enum {
7272
CRYPT_INVALID_PRIME_SIZE,/* Invalid size of prime requested */
7373
CRYPT_PK_INVALID_PADDING, /* Invalid padding on input */
7474

75-
CRYPT_HASH_OVERFLOW /* Hash applied to too many bits */
75+
CRYPT_HASH_OVERFLOW, /* Hash applied to too many bits */
76+
CRYPT_PW_CTX_MISSING, /* Password context to decrypt key file is missing */
7677
};
7778

7879
#include "tomcrypt_cfg.h"

src/headers/tomcrypt_misc.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,12 @@ int padding_depad(const unsigned char *data, unsigned long *length, unsigned lon
160160
#endif /* LTC_PADDING */
161161

162162
#ifdef LTC_PEM
163-
int pem_decode_pkcs_filehandle(FILE *f, ltc_pka_key *k, password_ctx *pw_ctx);
164-
int pem_decode_pkcs(const void *buf, unsigned long len, ltc_pka_key *k, password_ctx *pw_ctx);
163+
int pem_decode_pkcs_filehandle(FILE *f, ltc_pka_key *k, const password_ctx *pw_ctx);
164+
int pem_decode_pkcs(const void *buf, unsigned long len, ltc_pka_key *k, const password_ctx *pw_ctx);
165165

166166
#ifdef LTC_SSH
167-
int pem_decode_openssh_filehandle(FILE *f, ltc_pka_key *k, password_ctx *pw_ctx);
168-
int pem_decode_openssh(const void *buf, unsigned long len, ltc_pka_key *k, password_ctx *pw_ctx);
167+
int pem_decode_openssh_filehandle(FILE *f, ltc_pka_key *k, const password_ctx *pw_ctx);
168+
int pem_decode_openssh(const void *buf, unsigned long len, ltc_pka_key *k, const password_ctx *pw_ctx);
169169
#endif
170170

171171
#endif /* LTC_PEM */

src/headers/tomcrypt_private.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,6 @@ struct pem_headers {
281281
struct password *pw;
282282
};
283283

284-
extern const struct pem_header_id pem_std_headers[];
285-
extern const unsigned long pem_std_headers_num;
286-
extern const struct str pem_proc_type_encrypted;
287-
extern const struct str pem_dek_info_start;
288-
extern const struct dek_info_from_str pem_dek_infos[];
289-
extern const unsigned long pem_dek_infos_num;
290-
291284
struct bufp {
292285
/* `end` points to one byte after the last
293286
* element of the allocated buffer
@@ -300,7 +293,9 @@ struct bufp {
300293
struct get_char {
301294
int (*get)(struct get_char*);
302295
union {
296+
#ifndef LTC_NO_FILE
303297
FILE *f;
298+
#endif /* LTC_NO_FILE */
304299
struct bufp buf;
305300
};
306301
struct str unget_buf;
@@ -317,7 +312,9 @@ int pbes_decrypt(const pbes_arg *arg, unsigned char *dec_data, unsigned long *d
317312
int pbes1_extract(const ltc_asn1_list *s, pbes_arg *res);
318313
int pbes2_extract(const ltc_asn1_list *s, pbes_arg *res);
319314

315+
#ifndef LTC_NO_FILE
320316
int pem_get_char_from_file(struct get_char *g);
317+
#endif /* LTC_NO_FILE */
321318
int pem_get_char_from_buf(struct get_char *g);
322319
int pem_read(void *pem, unsigned long *w, struct pem_headers *hdr, struct get_char *g);
323320

src/misc/error_to_string.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ static const char * const err_2_str[] =
5151
"Invalid padding.",
5252

5353
"Hash applied to too many bits.",
54+
55+
"Password context to decrypt key file is missing.",
5456
};
5557

5658
/**

src/misc/pem/pem_pkcs.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
#ifdef LTC_PEM
1111

12+
extern const struct pem_header_id pem_std_headers[];
13+
extern const unsigned long pem_std_headers_num;
14+
1215
static int s_decrypt_pem(unsigned char *pem, unsigned long *l, const struct pem_headers *hdr)
1316
{
1417
unsigned char iv[MAXBLOCKSIZE], key[MAXBLOCKSIZE];
@@ -58,14 +61,15 @@ typedef struct {
5861
pkcs8_import fn;
5962
} p8_import_st;
6063

61-
static int s_decode(struct get_char *g, ltc_pka_key *k, password_ctx *pw_ctx)
64+
static int s_decode(struct get_char *g, ltc_pka_key *k, const password_ctx *pw_ctx)
6265
{
6366
unsigned char *pem = NULL;
6467
unsigned long w, l, n;
6568
int err = CRYPT_ERROR;
6669
struct pem_headers hdr = { 0 };
6770
struct password pw;
6871
ltc_asn1_list *p8_asn1 = NULL;
72+
XMEMSET(k, 0, sizeof(*k));
6973
w = LTC_PEM_READ_BUFSIZE * 2;
7074
retry:
7175
pem = XREALLOC(pem, w);
@@ -132,8 +136,10 @@ static int s_decode(struct get_char *g, ltc_pka_key *k, password_ctx *pw_ctx)
132136
}
133137
goto cleanup;
134138
} else if (hdr.encrypted) {
135-
LTC_ARGCHK(pw_ctx != NULL);
136-
LTC_ARGCHK(pw_ctx->callback != NULL);
139+
if ((pw_ctx == NULL) || (pw_ctx->callback == NULL)) {
140+
err = CRYPT_PW_CTX_MISSING;
141+
goto cleanup;
142+
}
137143

138144
hdr.pw = &pw;
139145
if (pw_ctx->callback(&hdr.pw->pw, &hdr.pw->l, pw_ctx->userdata)) {
@@ -181,7 +187,8 @@ static int s_decode(struct get_char *g, ltc_pka_key *k, password_ctx *pw_ctx)
181187
return err;
182188
}
183189

184-
int pem_decode_pkcs_filehandle(FILE *f, ltc_pka_key *k, password_ctx *pw_ctx)
190+
#ifndef LTC_NO_FILE
191+
int pem_decode_pkcs_filehandle(FILE *f, ltc_pka_key *k, const password_ctx *pw_ctx)
185192
{
186193
LTC_ARGCHK(f != NULL);
187194
LTC_ARGCHK(k != NULL);
@@ -190,10 +197,12 @@ int pem_decode_pkcs_filehandle(FILE *f, ltc_pka_key *k, password_ctx *pw_ctx)
190197
return s_decode(&g, k, pw_ctx);
191198
}
192199
}
200+
#endif /* LTC_NO_FILE */
193201

194-
int pem_decode_pkcs(const void *buf, unsigned long len, ltc_pka_key *k, password_ctx *pw_ctx)
202+
int pem_decode_pkcs(const void *buf, unsigned long len, ltc_pka_key *k, const password_ctx *pw_ctx)
195203
{
196204
LTC_ARGCHK(buf != NULL);
205+
LTC_ARGCHK(len != 0);
197206
LTC_ARGCHK(k != NULL);
198207
{
199208
struct get_char g = { .get = pem_get_char_from_buf, SET_BUFP(.buf, buf, len) };

src/misc/pem/pem_read.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,17 @@
99

1010
#ifdef LTC_PEM
1111

12+
extern const struct str pem_proc_type_encrypted;
13+
extern const struct str pem_dek_info_start;
14+
extern const struct dek_info_from_str pem_dek_infos[];
15+
extern const unsigned long pem_dek_infos_num;
16+
17+
#ifndef LTC_NO_FILE
1218
int pem_get_char_from_file(struct get_char *g)
1319
{
1420
return getc(g->f);
1521
}
22+
#endif /* LTC_NO_FILE */
1623

1724
int pem_get_char_from_buf(struct get_char *g)
1825
{

src/misc/pem/pem_ssh.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -344,13 +344,14 @@ static const struct pem_header_id pem_openssh =
344344
.has_more_headers = 0
345345
};
346346

347-
static int s_decode_openssh(struct get_char *g, ltc_pka_key *k, password_ctx *pw_ctx)
347+
static int s_decode_openssh(struct get_char *g, ltc_pka_key *k, const password_ctx *pw_ctx)
348348
{
349349
unsigned char *pem = NULL, *p, *privkey = NULL;
350350
unsigned long w, l, privkey_len;
351351
int err;
352352
struct pem_headers hdr = { .id = &pem_openssh };
353353
struct kdf_options opts = { 0 };
354+
XMEMSET(k, 0, sizeof(*k));
354355
w = LTC_PEM_READ_BUFSIZE * 2;
355356
retry:
356357
pem = XREALLOC(pem, w);
@@ -378,9 +379,8 @@ static int s_decode_openssh(struct get_char *g, ltc_pka_key *k, password_ctx *pw
378379
}
379380

380381
if (XSTRCMP(opts.name, "none") != 0) {
381-
/* hard-coded pass for demo keys */
382-
if (!pw_ctx || !pw_ctx->callback) {
383-
err = CRYPT_INVALID_ARG;
382+
if ((pw_ctx == NULL) || (pw_ctx->callback == NULL)) {
383+
err = CRYPT_PW_CTX_MISSING;
384384
goto cleanup;
385385
}
386386
if (pw_ctx->callback(&opts.pw.pw, &opts.pw.l, pw_ctx->userdata)) {
@@ -411,16 +411,27 @@ static int s_decode_openssh(struct get_char *g, ltc_pka_key *k, password_ctx *pw
411411
return err;
412412
}
413413

414-
int pem_decode_openssh_filehandle(FILE *f, ltc_pka_key *k, password_ctx *pw_ctx)
414+
#ifndef LTC_NO_FILE
415+
int pem_decode_openssh_filehandle(FILE *f, ltc_pka_key *k, const password_ctx *pw_ctx)
415416
{
416-
struct get_char g = { .get = pem_get_char_from_file, .f = f };
417-
return s_decode_openssh(&g, k, pw_ctx);
417+
LTC_ARGCHK(f != NULL);
418+
LTC_ARGCHK(k != NULL);
419+
{
420+
struct get_char g = { .get = pem_get_char_from_file, .f = f };
421+
return s_decode_openssh(&g, k, pw_ctx);
422+
}
418423
}
424+
#endif /* LTC_NO_FILE */
419425

420-
int pem_decode_openssh(const void *buf, unsigned long len, ltc_pka_key *k, password_ctx *pw_ctx)
426+
int pem_decode_openssh(const void *buf, unsigned long len, ltc_pka_key *k, const password_ctx *pw_ctx)
421427
{
422-
struct get_char g = { .get = pem_get_char_from_buf, SET_BUFP(.buf, buf, len) };
423-
return s_decode_openssh(&g, k, pw_ctx);
428+
LTC_ARGCHK(buf != NULL);
429+
LTC_ARGCHK(len != 0);
430+
LTC_ARGCHK(k != NULL);
431+
{
432+
struct get_char g = { .get = pem_get_char_from_buf, SET_BUFP(.buf, buf, len) };
433+
return s_decode_openssh(&g, k, pw_ctx);
434+
}
424435
}
425436

426437
#endif /* defined(LTC_PEM) && defined(LTC_SSH) */

src/pk/asn1/pkcs8/pkcs8_decode_flexi.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ int pkcs8_decode_flexi(const unsigned char *in, unsigned long inlen,
4848
LTC_ASN1_IS_TYPE(l->child->next, LTC_ASN1_OCTET_STRING)) {
4949
ltc_asn1_list *lalgoid = l->child->child;
5050

51-
LTC_ARGCHK(pw_ctx != NULL);
52-
LTC_ARGCHK(pw_ctx->callback != NULL);
51+
if ((pw_ctx == NULL) || (pw_ctx->callback == NULL)) {
52+
err = CRYPT_PW_CTX_MISSING;
53+
goto LBL_DONE;
54+
}
5355

5456
if (pbes1_extract(lalgoid, &pbes) == CRYPT_OK) {
5557
/* Successfully extracted PBES1 parameters */

src/pk/rsa/rsa_key.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void rsa_shrink_key(rsa_key *key)
8181

8282
/**
8383
Init an RSA key
84-
@param key The RSA key to free
84+
@param key The RSA key to initialize
8585
@return CRYPT_OK if successful
8686
*/
8787
int rsa_init(rsa_key *key)

0 commit comments

Comments
 (0)