8
8
9
9
#include <tomcrypt.h>
10
10
#include <stdarg.h>
11
-
12
- static int verbose = 0 ;
11
+ #include <termios.h>
13
12
14
13
static void print_err (const char * fmt , ...)
15
14
{
16
15
va_list args ;
17
16
18
- if (!verbose ) return ;
19
-
20
17
va_start (args , fmt );
21
18
vfprintf (stderr , fmt , args );
19
+ va_end (args );
22
20
}
23
21
24
22
static void die_ (int err , int line )
25
23
{
26
- verbose = 1 ;
27
24
print_err ("%3d: LTC sez %s\n" , line , error_to_string (err ));
28
25
exit (EXIT_FAILURE );
29
26
}
30
27
31
28
#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
+ }
33
59
34
60
static int password_get (void * * p , unsigned long * l , void * u )
35
61
{
36
62
(void )u ;
37
- * p = strdup ( "abc123" );
63
+ * p = getpassword ( "Enter passphrase: " , 256 );
38
64
* l = strlen (* p );
39
65
return 0 ;
40
66
}
41
67
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
+
42
100
int main (int argc , char * * argv )
43
101
{
44
102
int err ;
@@ -64,6 +122,7 @@ int main(int argc, char **argv)
64
122
if ((err = pem_decode_openssh_filehandle (f , & k , & pw_ctx ))) {
65
123
die (err );
66
124
}
125
+ print (& k );
67
126
return EXIT_SUCCESS ;
68
127
}
69
128
0 commit comments