| 1 |
|
| 2 |
#include "gkm-ssh-openssh.h" |
| 3 |
|
| 4 |
#include "gkm/gkm-data-asn1.h" |
| 5 |
#include "gkm/gkm-data-der.h" |
| 6 |
#include "gkm/gkm-data-types.h" |
| 7 |
|
| 8 |
#include "egg/egg-armor.h" |
| 9 |
#include "egg/egg-asn1x.h" |
| 10 |
#include "egg/egg-buffer.h" |
| 11 |
#include "egg/egg-openssl.h" |
| 12 |
#include "egg/egg-secure-memory.h" |
| 13 |
|
| 14 |
typedef struct _ParsePrivate { |
| 15 |
gcry_sexp_t sexp; |
| 16 |
gboolean seen; |
| 17 |
GkmDataResult result; |
| 18 |
const gchar *password; |
| 19 |
gssize n_password; |
| 20 |
} ParsePrivate; |
| 21 |
|
| 22 |
/* ------------------------------------------------------------------------------ |
| 23 |
* INTERNAL |
| 24 |
*/ |
| 25 |
|
| 26 |
static int |
| 27 |
keytype_to_algo (const gchar *salgo) |
| 28 |
{ |
| 29 |
g_return_val_if_fail (salgo, 0); |
| 30 |
if (strcmp (salgo, "ssh-rsa") == 0) |
| 31 |
return GCRY_PK_RSA; |
| 32 |
else if (strcmp (salgo, "ssh-dss") == 0) |
| 33 |
return GCRY_PK_DSA; |
| 34 |
else if ((strcmp (salgo, "ecdsa-sha2-nistp256") == 0) |
| 35 |
|| (strcmp (salgo, "ecdsa-sha2-nistp384") == 0) |
| 36 |
|| (strcmp (salgo, "ecdsa-sha2-nistp521") == 0)) |
| 37 |
return GCRY_PK_ECC; |
| 38 |
return 0; |
| 39 |
} |
| 40 |
|
| 41 |
static const gchar * |
| 42 |
curve_to_gcry (const gchar *salgo) |
| 43 |
{ |
| 44 |
g_return_val_if_fail (salgo, 0); |
| 45 |
if (strcmp (salgo, "nistp256") == 0) |
| 46 |
return "NIST P-256"; |
| 47 |
else if (strcmp (salgo, "nistp384") == 0) |
| 48 |
return "NIST P-384"; |
| 49 |
else if (strcmp (salgo, "nistp521") == 0) |
| 50 |
return "NIST P-521"; |
| 51 |
return NULL; |
| 52 |
} |
| 53 |
|
| 54 |
static gboolean |
| 55 |
read_mpi (EggBuffer *req, gsize *offset, gcry_mpi_t *mpi) |
| 56 |
{ |
| 57 |
const guchar *data; |
| 58 |
gsize len; |
| 59 |
gcry_error_t gcry; |
| 60 |
|
| 61 |
if (!egg_buffer_get_byte_array (req, *offset, offset, &data, &len)) |
| 62 |
return FALSE; |
| 63 |
|
| 64 |
gcry = gcry_mpi_scan (mpi, GCRYMPI_FMT_USG, data, len, NULL); |
| 65 |
if (gcry) |
| 66 |
return FALSE; |
| 67 |
|
| 68 |
return TRUE; |
| 69 |
} |
| 70 |
|
| 71 |
#define SEXP_PUBLIC_DSA \ |
| 72 |
"(public-key" \ |
| 73 |
" (dsa" \ |
| 74 |
" (p %m)" \ |
| 75 |
" (q %m)" \ |
| 76 |
" (g %m)" \ |
| 77 |
" (y %m)))" |
| 78 |
|
| 79 |
static gboolean |
| 80 |
read_public_dsa (EggBuffer *req, gsize *offset, gcry_sexp_t *sexp) |
| 81 |
{ |
| 82 |
gcry_mpi_t p, q, g, y; |
| 83 |
int gcry; |
| 84 |
|
| 85 |
if (!read_mpi (req, offset, &p) || |
| 86 |
!read_mpi (req, offset, &q) || |
| 87 |
!read_mpi (req, offset, &g) || |
| 88 |
!read_mpi (req, offset, &y)) |
| 89 |
return FALSE; |
| 90 |
|
| 91 |
gcry = gcry_sexp_build (sexp, NULL, SEXP_PUBLIC_DSA, p, q, g, y); |
| 92 |
if (gcry) { |
| 93 |
g_warning ("couldn't parse incoming public DSA key: %s", gcry_strerror (gcry)); |
| 94 |
return FALSE; |
| 95 |
} |
| 96 |
|
| 97 |
gcry_mpi_release (p); |
| 98 |
gcry_mpi_release (q); |
| 99 |
gcry_mpi_release (g); |
| 100 |
gcry_mpi_release (y); |
| 101 |
|
| 102 |
return TRUE; |
| 103 |
} |
| 104 |
|
| 105 |
#define SEXP_PUBLIC_ECDSA \ |
| 106 |
"(public-key" \ |
| 107 |
" (ecdsa" \ |
| 108 |
" (curve %s)" \ |
| 109 |
" (q %b)))" |
| 110 |
|
| 111 |
static gboolean |
| 112 |
read_public_ecdsa (EggBuffer *req, gsize *offset, gcry_sexp_t *sexp) |
| 113 |
{ |
| 114 |
int gcry; |
| 115 |
const gchar *curve_name = NULL; |
| 116 |
const guchar *q = NULL; |
| 117 |
size_t q_len; |
| 118 |
g_autofree char *curve = NULL; |
| 119 |
|
| 120 |
if (!egg_buffer_get_string (req, *offset, offset, &curve, (EggBufferAllocator)g_realloc)) |
| 121 |
return FALSE; |
| 122 |
|
| 123 |
if (!egg_buffer_get_byte_array (req, *offset, offset, &q, &q_len)) |
| 124 |
return FALSE; |
| 125 |
|
| 126 |
curve_name = curve_to_gcry (curve); |
| 127 |
g_return_val_if_fail (curve_name, FALSE); |
| 128 |
|
| 129 |
gcry = gcry_sexp_build (sexp, NULL, SEXP_PUBLIC_ECDSA, |
| 130 |
curve_name, q_len, q); |
| 131 |
if (gcry) { |
| 132 |
g_warning ("couldn't parse incoming public ECDSA key: %s", gcry_strerror (gcry)); |
| 133 |
return FALSE; |
| 134 |
} |
| 135 |
|
| 136 |
return TRUE; |
| 137 |
} |
| 138 |
|
| 139 |
#define SEXP_PUBLIC_RSA \ |
| 140 |
"(public-key" \ |
| 141 |
" (rsa" \ |
| 142 |
" (n %m)" \ |
| 143 |
" (e %m)))" |
| 144 |
|
| 145 |
static gboolean |
| 146 |
read_public_rsa (EggBuffer *req, gsize *offset, gcry_sexp_t *sexp) |
| 147 |
{ |
| 148 |
gcry_mpi_t n, e; |
| 149 |
int gcry; |
| 150 |
|
| 151 |
if (!read_mpi (req, offset, &e) || |
| 152 |
!read_mpi (req, offset, &n)) |
| 153 |
return FALSE; |
| 154 |
|
| 155 |
gcry = gcry_sexp_build (sexp, NULL, SEXP_PUBLIC_RSA, n, e); |
| 156 |
if (gcry) { |
| 157 |
g_warning ("couldn't parse incoming public RSA key: %s", gcry_strerror (gcry)); |
| 158 |
return FALSE; |
| 159 |
} |
| 160 |
|
| 161 |
gcry_mpi_release (n); |
| 162 |
gcry_mpi_release (e); |
| 163 |
|
| 164 |
return TRUE; |
| 165 |
} |
| 166 |
|
| 167 |
static gboolean |
| 168 |
read_public (EggBuffer *req, gsize *offset, gcry_sexp_t *key, int *algo) |
| 169 |
{ |
| 170 |
gboolean ret; |
| 171 |
gchar *stype; |
| 172 |
int alg; |
| 173 |
|
| 174 |
/* The string algorithm */ |
| 175 |
if (!egg_buffer_get_string (req, *offset, offset, &stype, (EggBufferAllocator)g_realloc)) |
| 176 |
return FALSE; |
| 177 |
|
| 178 |
alg = keytype_to_algo (stype); |
| 179 |
g_free (stype); |
| 180 |
|
| 181 |
if (!alg) { |
| 182 |
g_warning ("unsupported algorithm from SSH: %s", stype); |
| 183 |
return FALSE; |
| 184 |
} |
| 185 |
|
| 186 |
switch (alg) { |
| 187 |
case GCRY_PK_RSA: |
| 188 |
ret = read_public_rsa (req, offset, key); |
| 189 |
break; |
| 190 |
case GCRY_PK_DSA: |
| 191 |
ret = read_public_dsa (req, offset, key); |
| 192 |
break; |
| 193 |
case GCRY_PK_ECC: |
| 194 |
ret = read_public_ecdsa (req, offset, key); |
| 195 |
break; |
| 196 |
default: |
| 197 |
g_assert_not_reached (); |
| 198 |
return FALSE; |
| 199 |
} |
| 200 |
|
| 201 |
if (!ret) { |
| 202 |
g_warning ("couldn't read incoming SSH private key"); |
| 203 |
return FALSE; |
| 204 |
} |
| 205 |
|
| 206 |
if (algo) |
| 207 |
*algo = alg; |
| 208 |
return TRUE; |
| 209 |
} |
| 210 |
|
| 211 |
static GkmDataResult |
| 212 |
load_encrypted_key (GBytes *data, |
| 213 |
const gchar *dekinfo, |
| 214 |
const gchar *password, |
| 215 |
gssize n_password, |
| 216 |
gcry_sexp_t *skey) |
| 217 |
{ |
| 218 |
guchar *decrypted = NULL; |
| 219 |
gsize n_decrypted = 0; |
| 220 |
GBytes *bytes; |
| 221 |
GkmDataResult ret; |
| 222 |
gint length; |
| 223 |
|
| 224 |
/* Decrypt, this will result in garble if invalid password */ |
| 225 |
decrypted = egg_openssl_decrypt_block (dekinfo, password, n_password, |
| 226 |
data, &n_decrypted); |
| 227 |
if (!decrypted) |
| 228 |
return FALSE; |
| 229 |
|
| 230 |
/* Unpad the DER data */ |
| 231 |
length = egg_asn1x_element_length (decrypted, n_decrypted); |
| 232 |
if (length > 0) |
| 233 |
n_decrypted = length; |
| 234 |
|
| 235 |
bytes = g_bytes_new_with_free_func (decrypted, n_decrypted, egg_secure_free, decrypted); |
| 236 |
|
| 237 |
/* Try to parse */ |
| 238 |
ret = gkm_data_der_read_private_key (bytes, skey); |
| 239 |
g_bytes_unref (bytes); |
| 240 |
|
| 241 |
if (ret != GKM_DATA_UNRECOGNIZED) |
| 242 |
return ret; |
| 243 |
|
| 244 |
return GKM_DATA_LOCKED; |
| 245 |
} |
| 246 |
|
| 247 |
static gboolean |
| 248 |
is_private_key_type (GQuark type) |
| 249 |
{ |
| 250 |
static GQuark PEM_RSA_PRIVATE_KEY; |
| 251 |
static GQuark PEM_DSA_PRIVATE_KEY; |
| 252 |
static GQuark PEM_ECDSA_PRIVATE_KEY; |
| 253 |
static gsize quarks_inited = 0; |
| 254 |
|
| 255 |
/* Initialize the first time through */ |
| 256 |
if (g_once_init_enter (&quarks_inited)) { |
| 257 |
PEM_RSA_PRIVATE_KEY = g_quark_from_static_string ("RSA PRIVATE KEY"); |
| 258 |
PEM_DSA_PRIVATE_KEY = g_quark_from_static_string ("DSA PRIVATE KEY"); |
| 259 |
PEM_ECDSA_PRIVATE_KEY = g_quark_from_static_string ("EC PRIVATE KEY"); |
| 260 |
g_once_init_leave (&quarks_inited, 1); |
| 261 |
} |
| 262 |
|
| 263 |
/* Only handle SSHv2 private keys */ |
| 264 |
return (type == PEM_RSA_PRIVATE_KEY || |
| 265 |
type == PEM_DSA_PRIVATE_KEY || |
| 266 |
type == PEM_ECDSA_PRIVATE_KEY); |
| 267 |
} |
| 268 |
|
| 269 |
static void |
| 270 |
parsed_pem_block (GQuark type, |
| 271 |
GBytes *data, |
| 272 |
GBytes *outer, |
| 273 |
GHashTable *headers, |
| 274 |
gpointer user_data) |
| 275 |
{ |
| 276 |
ParsePrivate *ctx = (ParsePrivate*)user_data; |
| 277 |
const gchar *dekinfo; |
| 278 |
|
| 279 |
if (!is_private_key_type (type)) |
| 280 |
return; |
| 281 |
|
| 282 |
ctx->seen = TRUE; |
| 283 |
|
| 284 |
/* Only parse first key in the file */ |
| 285 |
if (ctx->sexp) |
| 286 |
return; |
| 287 |
|
| 288 |
/* If it's encrypted ... */ |
| 289 |
dekinfo = egg_openssl_get_dekinfo (headers); |
| 290 |
if (dekinfo) { |
| 291 |
ctx->result = load_encrypted_key (data, dekinfo, ctx->password, |
| 292 |
ctx->n_password, &ctx->sexp); |
| 293 |
|
| 294 |
/* not encryted, just load the data */ |
| 295 |
} else { |
| 296 |
ctx->result = gkm_data_der_read_private_key (data, &ctx->sexp); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
static void |
| 301 |
digest_pem_block (GQuark type, |
| 302 |
GBytes *data, |
| 303 |
GBytes *outer, |
| 304 |
GHashTable *headers, |
| 305 |
gpointer user_data) |
| 306 |
{ |
| 307 |
gchar **result = (gchar**)user_data; |
| 308 |
|
| 309 |
g_assert (result); |
| 310 |
|
| 311 |
if (!is_private_key_type (type)) |
| 312 |
return; |
| 313 |
|
| 314 |
/* Only digest the first key in the file */ |
| 315 |
if (*result != NULL) |
| 316 |
return; |
| 317 |
|
| 318 |
*result = g_compute_checksum_for_data (G_CHECKSUM_SHA1, |
| 319 |
g_bytes_get_data (data, NULL), |
| 320 |
g_bytes_get_size (data)); |
| 321 |
} |
| 322 |
|
| 323 |
/* ------------------------------------------------------------------------------ |
| 324 |
* PUBLIC |
| 325 |
*/ |
| 326 |
|
| 327 |
GkmDataResult |
| 328 |
gkm_ssh_openssh_parse_public_key (gconstpointer input, gsize n_data, |
| 329 |
gcry_sexp_t *sexp, gchar **comment) |
| 330 |
{ |
| 331 |
EggBuffer buf; |
| 332 |
const guchar *at; |
| 333 |
guchar *decoded; |
| 334 |
gsize n_decoded; |
| 335 |
gsize offset; |
| 336 |
gchar *val; |
| 337 |
gboolean ret; |
| 338 |
gint state, algo; |
| 339 |
guint save; |
| 340 |
const guchar *data = input; |
| 341 |
|
| 342 |
g_return_val_if_fail (data, FALSE); |
| 343 |
g_return_val_if_fail (sexp, FALSE); |
| 344 |
|
| 345 |
/* Look for a key line */ |
| 346 |
for (;;) { |
| 347 |
/* Eat space at the front */ |
| 348 |
while (n_data > 0 && g_ascii_isspace (data[0])) { |
| 349 |
++data; |
| 350 |
--n_data; |
| 351 |
} |
| 352 |
|
| 353 |
/* Not a comment or blank line? Then parse... */ |
| 354 |
if (data[0] != '#') |
| 355 |
break; |
| 356 |
|
| 357 |
/* Skip to the next line */ |
| 358 |
at = memchr (data, '\n', n_data); |
| 359 |
if (!at) |
| 360 |
return GKM_DATA_UNRECOGNIZED; |
| 361 |
at += 1; |
| 362 |
n_data -= (at - data); |
| 363 |
data = at; |
| 364 |
} |
| 365 |
|
| 366 |
/* Limit to use only the first line */ |
| 367 |
at = memchr (data, '\n', n_data); |
| 368 |
if (at != NULL) |
| 369 |
n_data = at - data; |
| 370 |
|
| 371 |
/* Find the first space */ |
| 372 |
at = memchr (data, ' ', n_data); |
| 373 |
if (!at) { |
| 374 |
g_message ("SSH public key missing space"); |
| 375 |
return GKM_DATA_UNRECOGNIZED; |
| 376 |
} |
| 377 |
|
| 378 |
/* Parse the key type */ |
| 379 |
val = g_strndup ((gchar*)data, at - data); |
| 380 |
algo = keytype_to_algo (val); |
| 381 |
if (!algo) { |
| 382 |
/* A number usually means an SSH1 key, just quietly ignore */ |
| 383 |
if (atoi (val) == 0) |
| 384 |
g_message ("Unsupported or unknown SSH key algorithm: %s", val); |
| 385 |
} |
| 386 |
g_free (val); |
| 387 |
if (!algo) |
| 388 |
return GKM_DATA_UNRECOGNIZED; |
| 389 |
|
| 390 |
/* Skip more whitespace */ |
| 391 |
n_data -= (at - data); |
| 392 |
data = at; |
| 393 |
while (n_data > 0 && (data[0] == ' ' || data[0] == '\t')) { |
| 394 |
++data; |
| 395 |
--n_data; |
| 396 |
} |
| 397 |
|
| 398 |
/* Find the next whitespace, or the end */ |
| 399 |
at = memchr (data, ' ', n_data); |
| 400 |
if (at == NULL) |
| 401 |
at = data + n_data; |
| 402 |
|
| 403 |
/* Decode the base64 key */ |
| 404 |
save = state = 0; |
| 405 |
decoded = g_malloc (n_data * 3 / 4); |
| 406 |
n_decoded = g_base64_decode_step ((gchar*)data, n_data, decoded, &state, &save); |
| 407 |
|
| 408 |
/* Parse the actual key */ |
| 409 |
egg_buffer_init_static (&buf, decoded, n_decoded); |
| 410 |
offset = 0; |
| 411 |
ret = read_public (&buf, &offset, sexp, NULL); |
| 412 |
g_free (decoded); |
| 413 |
if (!ret) { |
| 414 |
g_message ("failed to parse base64 part of SSH key"); |
| 415 |
return GKM_DATA_FAILURE; |
| 416 |
} |
| 417 |
|
| 418 |
/* Skip more whitespace */ |
| 419 |
n_data -= (at - data); |
| 420 |
data = at; |
| 421 |
while (n_data > 0 && (data[0] == ' ' || data[0] == '\t')) { |
| 422 |
++data; |
| 423 |
--n_data; |
| 424 |
} |
| 425 |
|
| 426 |
/* If there's data left, its the comment */ |
| 427 |
if (comment) |
| 428 |
*comment = n_data ? g_strndup ((gchar*)data, n_data) : NULL; |
| 429 |
|
| 430 |
return GKM_DATA_SUCCESS; |
| 431 |
} |
| 432 |
|
| 433 |
GkmDataResult |
| 434 |
gkm_ssh_openssh_parse_private_key (GBytes *data, |
| 435 |
const gchar *password, |
| 436 |
gssize n_password, |
| 437 |
gcry_sexp_t *sexp) |
| 438 |
{ |
| 439 |
ParsePrivate ctx; |
| 440 |
guint num; |
| 441 |
|
| 442 |
memset (&ctx, 0, sizeof (ctx)); |
| 443 |
ctx.result = FALSE; |
| 444 |
ctx.seen = FALSE; |
| 445 |
ctx.sexp = NULL; |
| 446 |
ctx.password = password; |
| 447 |
ctx.n_password = n_password; |
| 448 |
|
| 449 |
num = egg_armor_parse (data, parsed_pem_block, &ctx); |
| 450 |
|
| 451 |
/* Didn't find any private key there */ |
| 452 |
if (num == 0 || !ctx.seen) { |
| 453 |
g_message ("no private keys found in file"); |
| 454 |
return GKM_DATA_UNRECOGNIZED; |
| 455 |
} |
| 456 |
|
| 457 |
*sexp = ctx.sexp; |
| 458 |
return ctx.result; |
| 459 |
} |
| 460 |
|
| 461 |
gchar * |
| 462 |
gkm_ssh_openssh_digest_private_key (GBytes *data) |
| 463 |
{ |
| 464 |
gchar *result = NULL; |
| 465 |
egg_armor_parse (data, digest_pem_block, &result); |
| 466 |
return result; |
| 467 |
} |