Skip to content

Commit 68ef9b7

Browse files
committed
add support for regular PEM files
This adds support to decode plain and encrypted PEM files. Either in OpenSSL specific format or PKCS#8 Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
1 parent 107b82c commit 68ef9b7

File tree

6 files changed

+329
-14
lines changed

6 files changed

+329
-14
lines changed

src/headers/tomcrypt_custom.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@
114114

115115
#define LTC_NO_MISC
116116
#define LTC_BASE64
117+
#define LTC_BASE16
118+
#define LTC_PEM
117119
#endif /* LTC_EASY */
118120

119121
/* The minimal set of functionality to run the tests */
@@ -565,6 +567,12 @@
565567
#endif
566568

567569
#if defined(LTC_PEM)
570+
/* Size of the line-buffer */
571+
#ifndef LTC_PEM_DECODE_BUFSZ
572+
#define LTC_PEM_DECODE_BUFSZ 72
573+
#elif LTC_PEM_DECODE_BUFSZ < 72
574+
#error "LTC_PEM_DECODE_BUFSZ shall not be < 72 bytes"
575+
#endif
568576
/* Size of the decoded data buffer */
569577
#ifndef LTC_PEM_READ_BUFSIZE
570578
#ifdef LTC_FILE_READ_BUFSIZE

src/headers/tomcrypt_misc.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,10 +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_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);
163165

164166
#ifdef LTC_SSH
165167
int pem_decode_openssh_filehandle(FILE *f, ltc_pka_key *k, password_ctx *pw_ctx);
166-
int pem_decode_openssh(void *buf, unsigned long len, 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);
167169
#endif
168170

169171
#endif /* LTC_PEM */

src/headers/tomcrypt_private.h

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ struct password {
225225
void *pw;
226226
unsigned long l;
227227
};
228+
struct dek_info {
229+
const char *alg;
230+
unsigned long keylen;
231+
/* should use `MAXBLOCKSIZE` here, but all supported
232+
* blockciphers require max 16 bytes IV */
233+
char iv[16 * 2 + 1];
234+
};
228235

229236
struct str {
230237
char *p;
@@ -233,18 +240,29 @@ struct str {
233240

234241
#define SET_STR(n, s) n.p = s, n.len = XSTRLEN(s)
235242
#define SET_CSTR(n, s) n.p = (char*)s, n.len = XSTRLEN(s)
243+
#define COPY_STR(n, s, l) do { XMEMCPY(n.p, s, l); n.len = l; } while(0)
244+
#define FREE_STR(n) do { n.p = NULL; n.len = 0; } while(0)
236245

237246
enum more_headers {
238247
no,
239248
yes,
240249
maybe
241250
};
242251

252+
struct pem_header_id {
253+
struct str start, end;
254+
enum more_headers has_more_headers;
255+
int encrypted;
256+
enum ltc_pka_id pka;
257+
int pkcs8;
258+
int (*decrypt)(void *, unsigned long *, void *);
259+
};
260+
243261
struct pem_headers {
244-
const struct {
245-
struct str start, end;
246-
enum more_headers has_more_headers;
247-
};
262+
const struct pem_header_id *id;
263+
int encrypted;
264+
struct dek_info info;
265+
struct password *pw;
248266
};
249267

250268
struct bufp {
@@ -254,14 +272,16 @@ struct bufp {
254272
char *p, *r, *end;
255273
};
256274

257-
#define SET_BUFP(n, d, l) n.p = d, n.r = d, n.end = (char*)d + l + 1
275+
#define SET_BUFP(n, d, l) n.p = (char*)d, n.r = (char*)d, n.end = (char*)d + l + 1
258276

259277
struct get_char {
260278
int (*get)(struct get_char*);
261279
union {
262280
FILE *f;
263281
struct bufp buf;
264282
};
283+
struct str unget_buf;
284+
char unget_buf_[LTC_PEM_DECODE_BUFSZ];
265285
};
266286

267287
/* others */
@@ -294,6 +314,7 @@ void rsa_shrink_key(rsa_key *key);
294314
int rsa_make_key_bn_e(prng_state *prng, int wprng, int size, void *e,
295315
rsa_key *key); /* used by op-tee */
296316
int rsa_import_pkcs1(const unsigned char *in, unsigned long inlen, rsa_key *key);
317+
int rsa_import_pkcs8_asn1(ltc_asn1_list *alg_id, ltc_asn1_list *priv_key, rsa_key *key);
297318
#endif /* LTC_MRSA */
298319

299320
/* ---- DH Routines ---- */

src/misc/crypt/crypt.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ const char *crypt_build_settings =
463463
#endif
464464
#if defined(LTC_PEM)
465465
" PEM "
466+
" " NAME_VALUE(LTC_PEM_DECODE_BUFSZ) " "
466467
" " NAME_VALUE(LTC_PEM_READ_BUFSIZE) " "
467468
#endif
468469
#if defined(LTC_SSH)

0 commit comments

Comments
 (0)