Skip to content

Commit 94d02c5

Browse files
committed
split-up into multiple C files
... and slightly optimize multiple things, e.g. `DEK-Info` decoding Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
1 parent ac33c3a commit 94d02c5

File tree

7 files changed

+488
-427
lines changed

7 files changed

+488
-427
lines changed

src/headers/tomcrypt_misc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ 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_filehandle(FILE *f, ltc_pka_key *k, password_ctx *pw_ctx);
164-
int pem_decode(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, password_ctx *pw_ctx);
164+
int pem_decode_pkcs(const void *buf, unsigned long len, ltc_pka_key *k, password_ctx *pw_ctx);
165165

166166
#ifdef LTC_SSH
167167
int pem_decode_openssh_filehandle(FILE *f, ltc_pka_key *k, password_ctx *pw_ctx);

src/headers/tomcrypt_private.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ int base64_encode_pem(const unsigned char *in, unsigned long inlen,
219219

220220
/* PEM related */
221221

222+
#ifdef LTC_PEM
222223
struct password {
223224
/* usually a `char*` but could also contain binary data
224225
* so use a `void*` + length to be on the safe side.
@@ -242,7 +243,12 @@ struct str {
242243
#define SET_STR(n, s) n.p = s, n.len = XSTRLEN(s)
243244
#define SET_CSTR(n, s) n.p = (char*)s, n.len = XSTRLEN(s)
244245
#define COPY_STR(n, s, l) do { XMEMCPY(n.p, s, l); n.len = l; } while(0)
245-
#define FREE_STR(n) do { n.p = NULL; n.len = 0; } while(0)
246+
#define RESET_STR(n) do { n.p = NULL; n.len = 0; } while(0)
247+
248+
struct dek_info_from_str {
249+
const struct str id;
250+
struct dek_info info;
251+
};
246252

247253
enum more_headers {
248254
no,
@@ -266,14 +272,21 @@ struct pem_headers {
266272
struct password *pw;
267273
};
268274

275+
extern const struct pem_header_id pem_std_headers[];
276+
extern const unsigned long pem_std_headers_num;
277+
extern const struct str pem_proc_type_encrypted;
278+
extern const struct str pem_dek_info_start;
279+
extern const struct dek_info_from_str pem_dek_infos[];
280+
extern const unsigned long pem_dek_infos_num;
281+
269282
struct bufp {
270283
/* `end` points to one byte after the last
271284
* element of the allocated buffer
272285
*/
273-
char *p, *r, *end;
286+
char *start, *work, *end;
274287
};
275288

276-
#define SET_BUFP(n, d, l) n.p = (char*)d, n.r = (char*)d, n.end = (char*)d + l + 1
289+
#define SET_BUFP(n, d, l) n.start = (char*)d, n.work = (char*)d, n.end = (char*)d + l + 1
277290

278291
struct get_char {
279292
int (*get)(struct get_char*);
@@ -284,6 +297,7 @@ struct get_char {
284297
struct str unget_buf;
285298
char unget_buf_[LTC_PEM_DECODE_BUFSZ];
286299
};
300+
#endif
287301

288302
/* others */
289303

src/misc/pem/pem.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2+
/* SPDX-License-Identifier: Unlicense */
3+
#include "tomcrypt_private.h"
4+
5+
/**
6+
@file pem.c
7+
Const declarations for PEM, Steffen Jaeckel
8+
*/
9+
10+
#ifdef LTC_PEM
11+
12+
const struct pem_header_id pem_std_headers[] = {
13+
{
14+
/* PKCS#8 encrypted */
15+
SET_CSTR(.start, "-----BEGIN ENCRYPTED PRIVATE KEY-----"),
16+
SET_CSTR(.end, "-----END ENCRYPTED PRIVATE KEY-----"),
17+
.has_more_headers = no,
18+
.encrypted = 1,
19+
.pkcs8 = 1,
20+
},
21+
{
22+
/* PKCS#8 plain */
23+
SET_CSTR(.start, "-----BEGIN PRIVATE KEY-----"),
24+
SET_CSTR(.end, "-----END PRIVATE KEY-----"),
25+
.has_more_headers = no,
26+
.pkcs8 = 1,
27+
},
28+
/* Regular plain or encrypted private keys */
29+
{
30+
SET_CSTR(.start, "-----BEGIN RSA PRIVATE KEY-----"),
31+
SET_CSTR(.end, "-----END RSA PRIVATE KEY-----"),
32+
.has_more_headers = maybe,
33+
.pka = LTC_PKA_RSA,
34+
},
35+
{
36+
SET_CSTR(.start, "-----BEGIN EC PRIVATE KEY-----"),
37+
SET_CSTR(.end, "-----END EC PRIVATE KEY-----"),
38+
.has_more_headers = maybe,
39+
.pka = LTC_PKA_EC,
40+
},
41+
{
42+
SET_CSTR(.start, "-----BEGIN DSA PRIVATE KEY-----"),
43+
SET_CSTR(.end, "-----END DSA PRIVATE KEY-----"),
44+
.has_more_headers = maybe,
45+
.pka = LTC_PKA_DSA,
46+
},
47+
};
48+
const unsigned long pem_std_headers_num = sizeof(pem_std_headers)/sizeof(pem_std_headers[0]);
49+
50+
51+
/* Encrypted PEM files */
52+
const struct str pem_proc_type_encrypted = { SET_CSTR(, "Proc-Type: 4,ENCRYPTED") };
53+
const struct str pem_dek_info_start = { SET_CSTR(, "DEK-Info: ") };
54+
const struct dek_info_from_str pem_dek_infos[] =
55+
{
56+
{ SET_CSTR(.id, "AES-128-CBC,"), .info.alg = "aes", .info.keylen = 128 / 8, },
57+
{ SET_CSTR(.id, "AES-192-CBC,"), .info.alg = "aes", .info.keylen = 192 / 8, },
58+
{ SET_CSTR(.id, "AES-256-CBC,"), .info.alg = "aes", .info.keylen = 256 / 8, },
59+
{ SET_CSTR(.id, "CAMELLIA-128-CBC,"), .info.alg = "camellia", .info.keylen = 128 / 8, },
60+
{ SET_CSTR(.id, "CAMELLIA-192-CBC,"), .info.alg = "camellia", .info.keylen = 192 / 8, },
61+
{ SET_CSTR(.id, "CAMELLIA-256-CBC,"), .info.alg = "camellia", .info.keylen = 256 / 8, },
62+
{ SET_CSTR(.id, "DES-EDE3-CBC,"), .info.alg = "3des", .info.keylen = 192 / 8, },
63+
{ SET_CSTR(.id, "DES-CBC,"), .info.alg = "des", .info.keylen = 64 / 8, },
64+
};
65+
const unsigned long pem_dek_infos_num = sizeof(pem_dek_infos)/sizeof(pem_dek_infos[0]);
66+
67+
#endif /* LTC_PEM */

0 commit comments

Comments
 (0)