Skip to content

Commit 9111235

Browse files
committed
Add "Enter passphrase" support to openssh-privkey
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
1 parent 175d797 commit 9111235

File tree

1 file changed

+66
-7
lines changed

1 file changed

+66
-7
lines changed

demos/openssh-privkey.c

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,96 @@
88

99
#include <tomcrypt.h>
1010
#include <stdarg.h>
11-
12-
static int verbose = 0;
11+
#include <termios.h>
1312

1413
#if defined(LTC_PEM_SSH)
1514
static void print_err(const char *fmt, ...)
1615
{
1716
va_list args;
1817

19-
if (!verbose) return;
20-
2118
va_start(args, fmt);
2219
vfprintf(stderr, fmt, args);
20+
va_end(args);
2321
}
2422

2523
static void die_(int err, int line)
2624
{
27-
verbose = 1;
2825
print_err("%3d: LTC sez %s\n", line, error_to_string(err));
2926
exit(EXIT_FAILURE);
3027
}
3128

3229
#define die(i) do { die_(i, __LINE__); } while(0)
33-
#define DIE(s, ...) do { verbose = 1; print_err("%3d: " s "\n", __LINE__, ##__VA_ARGS__); exit(EXIT_FAILURE); } while(0)
30+
#define DIE(s, ...) do { print_err("%3d: " s "\n", __LINE__, ##__VA_ARGS__); exit(EXIT_FAILURE); } while(0)
31+
32+
static char* getpassword(const char *prompt, size_t maxlen)
33+
{
34+
char *wr, *end, *pass = XCALLOC(1, maxlen + 1);
35+
struct termios tio;
36+
tcflag_t c_lflag;
37+
if (pass == NULL)
38+
return NULL;
39+
wr = pass;
40+
end = pass + maxlen;
41+
42+
tcgetattr(0, &tio);
43+
c_lflag = tio.c_lflag;
44+
tio.c_lflag &= ~ECHO;
45+
tcsetattr(0, TCSANOW, &tio);
46+
47+
printf("%s", prompt);
48+
fflush(stdout);
49+
while (pass < end) {
50+
int c = getchar();
51+
if (c == '\r' || c == '\n' || c == -1)
52+
break;
53+
*wr++ = c;
54+
}
55+
tio.c_lflag = c_lflag;
56+
tcsetattr(0, TCSAFLUSH, &tio);
57+
printf("\n");
58+
return pass;
59+
}
3460

3561
static int password_get(void **p, unsigned long *l, void *u)
3662
{
3763
(void)u;
38-
*p = strdup("abc123");
64+
*p = getpassword("Enter passphrase: ", 256);
3965
*l = strlen(*p);
4066
return 0;
4167
}
4268

69+
static void print(ltc_pka_key *k)
70+
{
71+
int err = CRYPT_OK;
72+
unsigned char buf[256];
73+
unsigned long lbuf = sizeof(buf);
74+
char pubkey[256*4/3];
75+
unsigned long lpubkey = sizeof(pubkey);
76+
void *mpint = NULL;
77+
switch (k->id) {
78+
case LTC_PKA_ED25519:
79+
ltc_mp.init(&mpint);
80+
ltc_mp.unsigned_read(mpint, k->u.ed25519.pub, sizeof(k->u.ed25519.pub));
81+
if ((err = ssh_encode_sequence_multi(buf, &lbuf,
82+
LTC_SSHDATA_STRING, "ssh-ed25519", strlen("ssh-ed25519"),
83+
LTC_SSHDATA_MPINT, mpint,
84+
0, NULL)) != CRYPT_OK)
85+
goto errout;
86+
if ((err = base64_encode(buf, lbuf, pubkey, &lpubkey)) != CRYPT_OK)
87+
goto errout;
88+
printf("\rssh-ed25519 %s\n", pubkey);
89+
break;
90+
default:
91+
print_err("Unsupported key type: %d\n", k->id);
92+
break;
93+
}
94+
errout:
95+
if (mpint != NULL)
96+
ltc_mp.deinit(mpint);
97+
if (err != CRYPT_OK)
98+
die(err);
99+
}
100+
43101
int main(int argc, char **argv)
44102
{
45103
int err;
@@ -65,6 +123,7 @@ int main(int argc, char **argv)
65123
if ((err = pem_decode_openssh_filehandle(f, &k, &pw_ctx))) {
66124
die(err);
67125
}
126+
print(&k);
68127
return EXIT_SUCCESS;
69128
}
70129
#else

0 commit comments

Comments
 (0)