Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dropbearkey: save a public key file .pub #267

Merged
merged 3 commits into from
Jan 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 76 additions & 19 deletions src/dropbearkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@

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, int create_pub_file, const char * filename);
/* Print a public key and fingerprint to stdout.
* Used for "dropbearkey -y" command but also after generation of a new key.
* For the new key pair the create_pub_file will be TRUE and the pub key will be saved to a .pub file.
*/
static int printpubfile(const char* filename, const char * comment, int create_pub_file);

/* Print a help message */
static void printhelp(char * progname) {
Expand Down Expand Up @@ -107,6 +110,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 Specify the key comment (email).\n"
#if DEBUG_TRACE
"-v verbose\n"
#endif
Expand Down Expand Up @@ -160,6 +164,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 +193,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 +229,7 @@ int main(int argc, char ** argv) {
}

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

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

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

return EXIT_SUCCESS;
}
#endif

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

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

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

err = DROPBEAR_SUCCESS;

Expand All @@ -330,7 +338,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, int create_pub_file, const char * filename) {

buffer * buf = NULL;
unsigned char base64key[MAX_PUBKEY_SIZE*2];
Expand All @@ -342,6 +350,31 @@ static void printpubkey(sign_key * key, int keytype) {
struct passwd * pw = NULL;
char * username = NULL;
char hostname[100];
char * filename_pub = NULL;
FILE *pubkey_file = NULL;

if (create_pub_file) {
int pubkey_fd = -1;
int filename_pub_len = 0;
filename_pub_len = strlen(filename) + 5;
filename_pub = m_malloc(filename_pub_len);
snprintf(filename_pub, filename_pub_len, "%s.pub", filename);

/* open() to use O_EXCL, then use a FILE* for fprintf().
* dprintf() is only posix2008 onwards */
pubkey_fd = open(filename_pub, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
if (pubkey_fd >= 0) {
/* Convert the fd to a FILE*. The underlying FD is closed
* by later fclose() */
pubkey_file = fdopen(pubkey_fd, "w");
if (!pubkey_file) {
m_close(pubkey_fd);
}
}
if (!pubkey_file) {
dropbear_log(LOG_ERR, "Save public key to %s failed: %s", filename_pub, strerror(errno));
}
}

buf = buf_new(MAX_PUBKEY_SIZE);
buf_put_pub_key(buf, key, keytype);
Expand All @@ -358,21 +391,45 @@ 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");

/* a user@host comment is informative */
username = "";
pw = getpwuid(getuid());
if (pw) {
username = pw->pw_name;
}
if (comment) {
printf("%s %s %s\n",
typestring, base64key, comment);
if (pubkey_file) {
fprintf(pubkey_file, "%s %s %s\n",
typestring, base64key, comment);
}
} else {
/* 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);
if (pubkey_file) {
fprintf(pubkey_file, "%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);
stokito marked this conversation as resolved.
Show resolved Hide resolved

if (pubkey_file) {
if (fsync(fileno(pubkey_file)) != 0) {
dropbear_log(LOG_ERR, "fsync of %s failed: %s", filename_pub, strerror(errno));
}
fclose(pubkey_file);
}
m_free(filename_pub);
}