1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-06-30 19:12:48 -05:00

Centralise most details of host-key prompting.

The text of the host key warnings was replicated in three places: the
Windows rc file, the GTK dialog setup function, and the console.c
shared between both platforms' CLI tools. Now it lives in just one
place, namely ssh/common.c where the rest of the centralised host-key
checking is done, so it'll be easier to adjust the wording in future.

This comes with some extra automation. Paragraph wrapping is no longer
done by hand in any version of these prompts. (Previously we let GTK
do the wrapping on GTK, but on Windows the resource file contained a
bunch of pre-wrapped LTEXT lines, and console.c had pre-wrapped
terminal messages.) And the dialog heights in Windows are determined
automatically based on the amount of stuff in the window.

The main idea of all this is that it'll be easier to set up more
elaborate kinds of host key prompt that deal with certificates (if,
e.g., a server sends us a certified host key which we don't trust the
CA for). But there are side benefits of this refactoring too: each
tool now reliably inserts its own appname in the prompts, and also, on
Windows the entire prompt text is copy-pastable.

Details of implementation: there's a new type SeatDialogText which
holds a set of (type, string) pairs describing the contents of a
prompt. Type codes distinguish ordinary text paragraphs, paragraphs to
be displayed prominently (like key fingerprints), the extra-bold scary
title at the top of the 'host key changed' version of the dialog, and
the various information that lives in the subsidiary 'more info' box.
ssh/common.c constructs this, and passes it to the Seat to present the
actual prompt.

In order to deal with the different UI for answering the prompt, I've
added an extra Seat method 'prompt_descriptions' which returns some
snippets of text to interpolate into the messages. ssh/common.c calls
that while it's still constructing the text, and incorporates the
resulting snippets into the SeatDialogText.

For the moment, this refactoring only affects the host key prompts.
The warnings about outmoded crypto are still done the old-fashioned
way; they probably ought to be similarly refactored to use this new
SeatDialogText system, but it's not immediately critical for the
purpose I have right now.
This commit is contained in:
Simon Tatham
2022-07-07 17:25:15 +01:00
parent 46332db26e
commit f1c8298000
25 changed files with 644 additions and 322 deletions

View File

@ -104,43 +104,53 @@ static int block_and_read(int fd, void *buf, size_t len)
SeatPromptResult console_confirm_ssh_host_key(
Seat *seat, const char *host, int port, const char *keytype,
char *keystr, const char *keydisp, char **fingerprints, bool mismatch,
char *keystr, SeatDialogText *text, HelpCtx helpctx,
void (*callback)(void *ctx, SeatPromptResult result), void *ctx)
{
char line[32];
struct termios cf;
char *common;
const char *intro, *prompt;
const char *prompt;
FingerprintType fptype_default =
ssh2_pick_default_fingerprint(fingerprints);
if (mismatch) { /* key was different */
common = hk_wrongmsg_common(host, port, keytype,
fingerprints[fptype_default]);
intro = hk_wrongmsg_interactive_intro;
prompt = hk_wrongmsg_interactive_prompt;
} else { /* key was absent */
common = hk_absentmsg_common(host, port, keytype,
fingerprints[fptype_default]);
intro = hk_absentmsg_interactive_intro;
prompt = hk_absentmsg_interactive_prompt;
}
stdio_sink errsink[1];
stdio_sink_init(errsink, stderr);
premsg(&cf);
fputs(common, stderr);
sfree(common);
if (console_batch_mode) {
fputs(console_abandoned_msg, stderr);
postmsg(&cf);
return SPR_SW_ABORT("Cannot confirm a host key in batch mode");
for (SeatDialogTextItem *item = text->items,
*end = item+text->nitems; item < end; item++) {
switch (item->type) {
case SDT_PARA:
wordwrap(BinarySink_UPCAST(errsink),
ptrlen_from_asciz(item->text), 60);
fputc('\n', stderr);
break;
case SDT_DISPLAY:
fprintf(stderr, " %s\n", item->text);
break;
case SDT_SCARY_HEADING:
/* Can't change font size or weight in this context */
fprintf(stderr, "%s\n", item->text);
break;
case SDT_BATCH_ABORT:
if (console_batch_mode) {
fprintf(stderr, "%s\n", item->text);
fflush(stderr);
postmsg(&cf);
return SPR_SW_ABORT("Cannot confirm a host key in batch mode");
}
break;
case SDT_PROMPT:
prompt = item->text;
break;
default:
break;
}
}
fputs(intro, stderr);
fflush(stderr);
while (true) {
fputs(prompt, stderr);
fprintf(stderr,
"%s (y/n, Return cancels connection, i for more info) ",
prompt);
fflush(stderr);
struct termios oldmode, newmode;
@ -154,13 +164,22 @@ SeatPromptResult console_confirm_ssh_host_key(
tcsetattr(0, TCSANOW, &oldmode);
if (line[0] == 'i' || line[0] == 'I') {
fprintf(stderr, "Full public key:\n%s\n", keydisp);
if (fingerprints[SSH_FPTYPE_SHA256])
fprintf(stderr, "SHA256 key fingerprint:\n%s\n",
fingerprints[SSH_FPTYPE_SHA256]);
if (fingerprints[SSH_FPTYPE_MD5])
fprintf(stderr, "MD5 key fingerprint:\n%s\n",
fingerprints[SSH_FPTYPE_MD5]);
for (SeatDialogTextItem *item = text->items,
*end = item+text->nitems; item < end; item++) {
switch (item->type) {
case SDT_MORE_INFO_KEY:
fprintf(stderr, "%s", item->text);
break;
case SDT_MORE_INFO_VALUE_SHORT:
fprintf(stderr, ": %s\n", item->text);
break;
case SDT_MORE_INFO_VALUE_BLOB:
fprintf(stderr, ":\n%s\n", item->text);
break;
default:
break;
}
}
} else {
break;
}

View File

@ -3592,41 +3592,22 @@ static void more_info_button_clicked(GtkButton *button, gpointer vctx)
&buttons_ok, more_info_closed, ctx);
}
const SeatDialogPromptDescriptions *gtk_seat_prompt_descriptions(Seat *seat)
{
static const SeatDialogPromptDescriptions descs = {
.hk_accept_action = "press \"Accept\"",
.hk_connect_once_action = "press \"Connect Once\"",
.hk_cancel_action = "press \"Cancel\"",
.hk_cancel_action_Participle = "Pressing \"Cancel\"",
};
return &descs;
}
SeatPromptResult gtk_seat_confirm_ssh_host_key(
Seat *seat, const char *host, int port, const char *keytype,
char *keystr, const char *keydisp, char **fingerprints, bool mismatch,
char *keystr, SeatDialogText *text, HelpCtx helpctx,
void (*callback)(void *ctx, SeatPromptResult result), void *ctx)
{
static const char absenttxt[] =
"The host key is not cached for this server:\n\n"
"%s (port %d)\n\n"
"You have no guarantee that the server is the computer "
"you think it is.\n"
"The server's %s key fingerprint is:\n\n"
"%s\n\n"
"If you trust this host, press \"Accept\" to add the key to "
"PuTTY's cache and carry on connecting.\n"
"If you want to carry on connecting just once, without "
"adding the key to the cache, press \"Connect Once\".\n"
"If you do not trust this host, press \"Cancel\" to abandon the "
"connection.";
static const char wrongtxt[] =
"WARNING - POTENTIAL SECURITY BREACH!\n"
"The host key does not match the one PuTTY has cached "
"for this server:\n\n"
"%s (port %d)\n\n"
"This means that either the server administrator has "
"changed the host key, or you have actually connected "
"to another computer pretending to be the server.\n"
"The new %s key fingerprint is:\n\n"
"%s\n\n"
"If you were expecting this change and trust the new key, "
"press \"Accept\" to update PuTTY's cache and continue connecting.\n"
"If you want to carry on connecting but without updating "
"the cache, press \"Connect Once\".\n"
"If you want to abandon the connection completely, press "
"\"Cancel\" to cancel. Pressing \"Cancel\" is the ONLY guaranteed "
"safe choice.";
static const struct message_box_button button_array_hostkey[] = {
{"Accept", 'a', 0, 2},
{"Connect Once", 'o', 0, 1},
@ -3636,17 +3617,40 @@ SeatPromptResult gtk_seat_confirm_ssh_host_key(
button_array_hostkey, lenof(button_array_hostkey),
};
char *text;
struct confirm_ssh_host_key_dialog_ctx *result_ctx;
const char *dlg_title = NULL;
strbuf *dlg_text = strbuf_new();
int width = string_width("default dialog width determination string");
for (SeatDialogTextItem *item = text->items,
*end = item + text->nitems; item < end; item++) {
switch (item->type) {
case SDT_PARA:
put_fmt(dlg_text, "%s\n\n", item->text);
break;
case SDT_DISPLAY: {
put_fmt(dlg_text, "%s\n\n", item->text);
int thiswidth = string_width(item->text);
if (width < thiswidth)
width = thiswidth;
break;
}
case SDT_SCARY_HEADING:
/* Can't change font size or weight in this context */
put_fmt(dlg_text, "%s\n\n", item->text);
break;
case SDT_TITLE:
dlg_title = item->text;
break;
default:
break;
}
}
while (strbuf_chomp(dlg_text, '\n'));
GtkWidget *mainwin, *msgbox;
FingerprintType fptype_default =
ssh2_pick_default_fingerprint(fingerprints);
text = dupprintf((mismatch ? wrongtxt : absenttxt), host, port,
keytype, fingerprints[fptype_default]);
result_ctx = snew(struct confirm_ssh_host_key_dialog_ctx);
struct confirm_ssh_host_key_dialog_ctx *result_ctx =
snew(struct confirm_ssh_host_key_dialog_ctx);
result_ctx->callback = callback;
result_ctx->callback_ctx = ctx;
result_ctx->host = dupstr(host);
@ -3658,41 +3662,48 @@ SeatPromptResult gtk_seat_confirm_ssh_host_key(
mainwin = GTK_WIDGET(gtk_seat_get_window(seat));
GtkWidget *more_info_button = NULL;
msgbox = create_message_box_general(
mainwin, "PuTTY Security Alert", text,
string_width(fingerprints[fptype_default]), true,
mainwin, dlg_title, dlg_text->s, width, true,
&buttons_hostkey, confirm_ssh_host_key_result_callback, result_ctx,
add_more_info_button, &more_info_button);
result_ctx->main_dialog = msgbox;
result_ctx->more_info_dialog = NULL;
strbuf *sb = strbuf_new();
if (fingerprints[SSH_FPTYPE_SHA256])
put_fmt(sb, "SHA256 fingerprint: %s\n",
fingerprints[SSH_FPTYPE_SHA256]);
if (fingerprints[SSH_FPTYPE_MD5])
put_fmt(sb, "MD5 fingerprint: %s\n",
fingerprints[SSH_FPTYPE_MD5]);
put_fmt(sb, "Full text of host's public key:");
/* We have to manually wrap the public key, or else the GtkLabel
* will resize itself to accommodate the longest word, which will
* lead to a hilariously wide message box. */
for (const char *p = keydisp, *q = p + strlen(p); p < q ;) {
size_t linelen = q-p;
if (linelen > 72)
linelen = 72;
put_byte(sb, '\n');
put_data(sb, p, linelen);
p += linelen;
strbuf *moreinfo = strbuf_new();
for (SeatDialogTextItem *item = text->items,
*end = item + text->nitems; item < end; item++) {
switch (item->type) {
case SDT_MORE_INFO_KEY:
put_fmt(moreinfo, "%s", item->text);
break;
case SDT_MORE_INFO_VALUE_SHORT:
put_fmt(moreinfo, ": %s\n", item->text);
break;
case SDT_MORE_INFO_VALUE_BLOB:
/* We have to manually wrap the public key, or else the GtkLabel
* will resize itself to accommodate the longest word, which will
* lead to a hilariously wide message box. */
for (const char *p = item->text, *q = p + strlen(p); p < q ;) {
size_t linelen = q-p;
if (linelen > 72)
linelen = 72;
put_byte(moreinfo, '\n');
put_data(moreinfo, p, linelen);
p += linelen;
}
break;
default:
break;
}
}
result_ctx->more_info = strbuf_to_str(sb);
result_ctx->more_info = strbuf_to_str(moreinfo);
g_signal_connect(G_OBJECT(more_info_button), "clicked",
G_CALLBACK(more_info_button_clicked), result_ctx);
register_dialog(seat, DIALOG_SLOT_NETWORK_PROMPT, msgbox);
sfree(text);
strbuf_free(dlg_text);
return SPR_INCOMPLETE; /* dialog still in progress */
}

View File

@ -222,7 +222,7 @@ int gtkdlg_askappend(Seat *seat, Filename *filename,
void (*callback)(void *ctx, int result), void *ctx);
SeatPromptResult gtk_seat_confirm_ssh_host_key(
Seat *seat, const char *host, int port, const char *keytype,
char *keystr, const char *keydisp, char **fingerprints, bool mismatch,
char *keystr, SeatDialogText *text, HelpCtx helpctx,
void (*callback)(void *ctx, SeatPromptResult result), void *ctx);
SeatPromptResult gtk_seat_confirm_weak_crypto_primitive(
Seat *seat, const char *algtype, const char *algname,
@ -230,6 +230,7 @@ SeatPromptResult gtk_seat_confirm_weak_crypto_primitive(
SeatPromptResult gtk_seat_confirm_weak_cached_hostkey(
Seat *seat, const char *algname, const char *betteralgs,
void (*callback)(void *ctx, SeatPromptResult result), void *ctx);
const SeatDialogPromptDescriptions *gtk_seat_prompt_descriptions(Seat *seat);
#ifdef MAY_REFER_TO_GTK_IN_HEADERS
struct message_box_button {
const char *title;

View File

@ -408,6 +408,7 @@ static const SeatVtable plink_seat_vt = {
.confirm_ssh_host_key = console_confirm_ssh_host_key,
.confirm_weak_crypto_primitive = console_confirm_weak_crypto_primitive,
.confirm_weak_cached_hostkey = console_confirm_weak_cached_hostkey,
.prompt_descriptions = console_prompt_descriptions,
.is_utf8 = nullseat_is_never_utf8,
.echoedit_update = plink_echoedit_update,
.get_x_display = nullseat_get_x_display,

View File

@ -429,6 +429,7 @@ static const SeatVtable gtk_seat_vt = {
.confirm_ssh_host_key = gtk_seat_confirm_ssh_host_key,
.confirm_weak_crypto_primitive = gtk_seat_confirm_weak_crypto_primitive,
.confirm_weak_cached_hostkey = gtk_seat_confirm_weak_cached_hostkey,
.prompt_descriptions = gtk_seat_prompt_descriptions,
.is_utf8 = gtk_seat_is_utf8,
.echoedit_update = nullseat_echoedit_update,
.get_x_display = gtk_seat_get_x_display,