Skip to content

Commit 478f43f

Browse files
committed
Add support for reading authorized_keys files
This also changes the requirements when calling `ecc_find_curve()` that the `cu` argument can be NULL. Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
1 parent c0be0aa commit 478f43f

File tree

10 files changed

+340
-24
lines changed

10 files changed

+340
-24
lines changed

doc/crypt.tex

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7504,6 +7504,39 @@ \subsection{De- and Encoding with Multiple Argument Lists}
75047504

75057505

75067506

7507+
\subsection{OpenSSH authorized\_keys files}
7508+
7509+
\index{authorized\_keys}
7510+
\index{ssh\_read\_authorized\_keys\_filehandle}
7511+
\index{ssh\_read\_authorized\_keys}
7512+
OpenSSH uses a simple storage format for public keys, which stores a public key per line in a regular text file.
7513+
To process such a file the following API can be used.
7514+
7515+
\begin{verbatim}
7516+
int ssh_read_authorized_keys_filehandle(FILE *f,
7517+
ssh_authorized_key_cb cb, void *ctx);
7518+
int ssh_read_authorized_keys(const void *buf, unsigned long len,
7519+
ssh_authorized_key_cb cb, void *ctx);
7520+
\end{verbatim}
7521+
7522+
\index{ssh\_authorized\_key\_cb}
7523+
For each key found in the file the callback as described below will be called.
7524+
7525+
\begin{verbatim}
7526+
/**
7527+
Callback function for each key in an `authorized_keys` file.
7528+
7529+
This function takes ownership of the `k` parameter passed.
7530+
`k` must be free'd by calling `pka_key_destroy(&k)`.
7531+
7532+
@param k Pointer to the PKA key.
7533+
@param comment Pointer to a string with the comment.
7534+
@param ctx The `ctx` pointer as passed to the read function.
7535+
*/
7536+
typedef int (*ssh_authorized_key_cb)(ltc_pka_key *k, const char *comment, void *ctx);
7537+
\end{verbatim}
7538+
7539+
75077540

75087541
\mysection{PEM Files}
75097542
\index{PEM}

src/headers/tomcrypt_misc.h

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,36 @@ 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, const password_ctx *pw_ctx);
163+
/* Buffer-based API */
164164
int pem_decode(const void *buf, unsigned long len, ltc_pka_key *k, const password_ctx *pw_ctx);
165-
166-
int pem_decode_pkcs_filehandle(FILE *f, ltc_pka_key *k, const password_ctx *pw_ctx);
167165
int pem_decode_pkcs(const void *buf, unsigned long len, ltc_pka_key *k, const password_ctx *pw_ctx);
168166

169167
#ifdef LTC_SSH
170-
int pem_decode_openssh_filehandle(FILE *f, ltc_pka_key *k, const password_ctx *pw_ctx);
168+
/**
169+
Callback function for each key in an `authorized_keys` file.
170+
171+
This function takes ownership of the `k` parameter passed.
172+
`k` must be free'd by calling `pka_key_destroy(&k)`.
173+
174+
@param k Pointer to the PKA key.
175+
@param comment Pointer to a string with the comment.
176+
@param ctx The `ctx` pointer as passed to the read function.
177+
*/
178+
typedef int (*ssh_authorized_key_cb)(ltc_pka_key *k, const char *comment, void *ctx);
179+
171180
int pem_decode_openssh(const void *buf, unsigned long len, ltc_pka_key *k, const password_ctx *pw_ctx);
172-
#endif
181+
int ssh_read_authorized_keys(const void *buf, unsigned long len, ssh_authorized_key_cb cb, void *ctx);
182+
#endif /* LTC_SSH */
183+
184+
/* FILE*-based API */
185+
#ifndef LTC_NO_FILE
186+
int pem_decode_filehandle(FILE *f, ltc_pka_key *k, const password_ctx *pw_ctx);
187+
int pem_decode_pkcs_filehandle(FILE *f, ltc_pka_key *k, const password_ctx *pw_ctx);
188+
#ifdef LTC_SSH
189+
int pem_decode_openssh_filehandle(FILE *f, ltc_pka_key *k, const password_ctx *pw_ctx);
190+
int ssh_read_authorized_keys_filehandle(FILE *f, ssh_authorized_key_cb cb, void *ctx);
191+
#endif /* LTC_SSH */
192+
#endif /* LTC_NO_FILE */
173193

174194
#endif /* LTC_PEM */
175195

src/headers/tomcrypt_pk.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ typedef struct {
554554
} ltc_pka_key;
555555

556556
void pka_key_free(ltc_pka_key *key);
557+
void pka_key_destroy(ltc_pka_key **key);
557558

558559
#ifdef LTC_DER
559560
/* DER handling */

0 commit comments

Comments
 (0)