Skip to content

Commit e707197

Browse files
committed
Add "Enter passphrase" support to openssh-privkey
Signed-off-by: Steffen Jaeckel <[email protected]>
1 parent df0c34f commit e707197

File tree

1 file changed

+66
-7
lines changed

1 file changed

+66
-7
lines changed

demos/openssh-privkey.c

+66-7
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,95 @@
88

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

1413
static void print_err(const char *fmt, ...)
1514
{
1615
va_list args;
1716

18-
if (!verbose) return;
19-
2017
va_start(args, fmt);
2118
vfprintf(stderr, fmt, args);
19+
va_end(args);
2220
}
2321

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

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

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

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

0 commit comments

Comments
 (0)