Skip to content

Commit

Permalink
dropbearkey: allow to specify a comment
Browse files Browse the repository at this point in the history
  • Loading branch information
stokito committed Dec 16, 2023
1 parent bba3053 commit e4228dd
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions src/dropbearkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@
static void printhelp(char * progname);


static void printpubkey(sign_key * key, int keytype);
static int printpubfile(const char* filename);
static void printpubkey(sign_key * key, int keytype, const char * comment);
static int printpubfile(const char* filename, const char * comment);

/* Print a help message */
static void printhelp(char * progname) {
Expand Down Expand Up @@ -107,6 +107,7 @@ static void printhelp(char * progname) {
" Ed25519 has a fixed size of 256 bits\n"
#endif
"-y Just print the publickey and fingerprint for the\n private key in <filename>.\n"
"-C Set the public key comment.\n"
#if DEBUG_TRACE
"-v verbose\n"
#endif
Expand Down Expand Up @@ -160,6 +161,7 @@ int main(int argc, char ** argv) {
char * typetext = NULL;
char * sizetext = NULL;
char * passphrase = NULL;
char * comment = NULL;
unsigned int bits = 0, genbits;
int printpub = 0;

Expand Down Expand Up @@ -188,6 +190,9 @@ int main(int argc, char ** argv) {
case 's':
next = &sizetext;
break;
case 'C':
next = &comment;
break;
case 'y':
printpub = 1;
break;
Expand Down Expand Up @@ -221,7 +226,7 @@ int main(int argc, char ** argv) {
}

if (printpub) {
int ret = printpubfile(filename);
int ret = printpubfile(filename, NULL);
exit(ret);
}

Expand Down Expand Up @@ -284,13 +289,13 @@ int main(int argc, char ** argv) {
dropbear_exit("Failed to generate key.\n");
}

printpubfile(filename);
printpubfile(filename, comment);

return EXIT_SUCCESS;
}
#endif

static int printpubfile(const char* filename) {
static int printpubfile(const char* filename, const char* comment) {

buffer *buf = NULL;
sign_key *key = NULL;
Expand All @@ -316,7 +321,7 @@ static int printpubfile(const char* filename) {
goto out;
}

printpubkey(key, keytype);
printpubkey(key, keytype, comment);

err = DROPBEAR_SUCCESS;

Expand All @@ -330,7 +335,7 @@ static int printpubfile(const char* filename) {
return err;
}

static void printpubkey(sign_key * key, int keytype) {
static void printpubkey(sign_key * key, int keytype, const char * comment) {

buffer * buf = NULL;
unsigned char base64key[MAX_PUBKEY_SIZE*2];
Expand Down Expand Up @@ -358,20 +363,28 @@ static void printpubkey(sign_key * key, int keytype) {

typestring = signkey_name_from_type(keytype, NULL);

fp = sign_key_fingerprint(buf_getptr(buf, len), len);
printf("Public key portion is:\n");

if (comment) {
printf("%s %s %s\n",
typestring, base64key, comment);
} else {
/* a user@host comment is informative */
username = "";
pw = getpwuid(getuid());
if (pw) {
username = pw->pw_name;
}

/* a user@host comment is informative */
username = "";
pw = getpwuid(getuid());
if (pw) {
username = pw->pw_name;
}
gethostname(hostname, sizeof(hostname));
hostname[sizeof(hostname) - 1] = '\0';

gethostname(hostname, sizeof(hostname));
hostname[sizeof(hostname)-1] = '\0';
printf("%s %s %s@%s\n",
typestring, base64key, username, hostname);
}

printf("Public key portion is:\n%s %s %s@%s\nFingerprint: %s\n",
typestring, base64key, username, hostname, fp);
fp = sign_key_fingerprint(buf_getptr(buf, len), len);
printf("Fingerprint: %s\n", fp);

m_free(fp);
buf_free(buf);
Expand Down

0 comments on commit e4228dd

Please sign in to comment.