summaryrefslogtreecommitdiff
path: root/gcr/gcr-ssh-agent-util.c
blob: 0ac91ebd9e7b47f04b1f9d0feb41885b6d3d15ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * gnome-keyring
 *
 * Copyright (C) 2014 Stef Walter
 * Copyright (C) 2018 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Author: Stef Walter <stef@thewalter.net>, Daiki Ueno
 */

#include "config.h"

#include <string.h>

#include "gcr-ssh-agent-util.h"

gboolean
_gcr_ssh_agent_read_packet (GSocketConnection *connection,
			    EggBuffer *buffer,
			    GCancellable *cancellable,
			    GError **error)
{
	GInputStream *stream;
	guint32 packet_size;
	gsize bytes_read;

	stream = g_io_stream_get_input_stream (G_IO_STREAM (connection));

	egg_buffer_reset (buffer);
	egg_buffer_resize (buffer, 4);

	if (!g_input_stream_read_all (stream, buffer->buf, 4, &bytes_read, cancellable, error))
		return FALSE;

	if (bytes_read < 4) {
		g_set_error (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED,
			     "connection closed by peer");
		return FALSE;
	}

	if (!egg_buffer_get_uint32 (buffer, 0, NULL, &packet_size) ||
	    packet_size < 1) {
		g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
			     "invalid packet size %u",
			     packet_size);
		return FALSE;
	}

	egg_buffer_resize (buffer, packet_size + 4);
	if (!g_input_stream_read_all (stream, buffer->buf + 4, packet_size, &bytes_read, cancellable, error))
		return FALSE;

	return TRUE;
}

gboolean
_gcr_ssh_agent_write_packet (GSocketConnection *connection,
			     EggBuffer *buffer,
			     GCancellable *cancellable,
			     GError **error)
{
	GOutputStream *stream;
	gsize bytes_written;

	stream = g_io_stream_get_output_stream (G_IO_STREAM (connection));
	if (!egg_buffer_set_uint32 (buffer, 0, buffer->len - 4)) {
		g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
			     "cannot read packet length");
		return FALSE;
	}
	return g_output_stream_write_all (stream, buffer->buf, buffer->len, &bytes_written, cancellable, error);
}

gboolean
_gcr_ssh_agent_call (GSocketConnection *connection,
		     EggBuffer*req,
		     EggBuffer *resp,
		     GCancellable *cancellable,
		     GError **error)
{
	return _gcr_ssh_agent_write_packet (connection, req, cancellable, error) &&
		_gcr_ssh_agent_read_packet (connection, resp, cancellable, error);
}

GBytes *
_gcr_ssh_agent_parse_public_key (GBytes *input,
				 gchar **comment)
{
	const guchar *at;
	guchar *decoded;
	gsize n_decoded;
	gint state;
	guint save;
	const guchar *data;
	gsize n_data;
	const guchar *keytype;
	gsize n_keytype;

	g_return_val_if_fail (input, NULL);

	data = g_bytes_get_data (input, &n_data);

	/* Look for a key line */
	for (;;) {
		/* Eat space at the front */
		while (n_data > 0 && g_ascii_isspace (data[0])) {
			++data;
			--n_data;
		}

		/* Not a comment or blank line? Then parse... */
		if (data[0] != '#')
			break;

		/* Skip to the next line */
		at = memchr (data, '\n', n_data);
		if (!at)
			return NULL;
		at += 1;
		n_data -= (at - data);
		data = at;
	}

	/* Limit to use only the first line */
	at = memchr (data, '\n', n_data);
	if (at != NULL)
		n_data = at - data;

	keytype = data;

	/* Find the first space */
	at = memchr (data, ' ', n_data);
	if (!at) {
		g_message ("SSH public key missing space");
		return NULL;
	}

	n_keytype = at - data;

	/* Skip more whitespace */
	n_data -= (at - data);
	data = at;
	while (n_data > 0 && (data[0] == ' ' || data[0] == '\t')) {
		++data;
		--n_data;
	}

	/* Find the next whitespace, or the end */
	at = memchr (data, ' ', n_data);
	if (at == NULL)
		at = data + n_data;

	/* Check if the chunk is the base64 key */
	if ((at - data) % 4 != 0) {
		g_message ("SSH public key missing key data");
		return NULL;
	}

	/* Decode the base64 key */
	save = state = 0;
	decoded = g_malloc (n_data * 3 / 4);
	n_decoded = g_base64_decode_step ((gchar*)data, at - data, decoded, &state, &save);

	if (!n_decoded) {
		g_free (decoded);
		return NULL;
	}

	/* Check if the key type is prefixed to the decoded blob */
	if (!(n_decoded > n_keytype + 4 &&
	      egg_buffer_decode_uint32 (decoded) == n_keytype &&
	      memcmp (keytype, decoded + 4, n_keytype) == 0)) {
		g_message ("SSH public key missing key type");
		g_free (decoded);
		return NULL;
	}

	/* Skip more whitespace */
	n_data -= (at - data);
	data = at;
	while (n_data > 0 && (data[0] == ' ' || data[0] == '\t')) {
		++data;
		--n_data;
	}

	/* If there's data left, its the comment */
	if (comment)
		*comment = n_data ? g_strndup ((gchar*)data, n_data) : g_strdup ("");

	return g_bytes_new_take (decoded, n_decoded);
}

gchar *
_gcr_ssh_agent_canon_error (gchar *str)
{
	gchar *start = str;
	gchar *end = str + strlen (str) + 1;

	for (;;) {
		start = strchr (start, '\r');
		if (!start)
			break;
		memmove (start, start + 1, end - (start + 1));
	}

	return str;
}