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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
|
/* keys.c --- Functions for reading /etc/krb5.keytab style key files.
* Copyright (C) 2002, 2003, 2004, 2006, 2007, 2008, 2010 Simon Josefsson
*
* This file is part of Shishi.
*
* Shishi is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Shishi 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Shishi; if not, see http://www.gnu.org/licenses or write
* to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
* Floor, Boston, MA 02110-1301, USA
*
*/
#include "internal.h"
/* Get _shishi_hexprint, etc. */
#include "utils.h"
/**
* shishi_keys_add_keytab_mem:
* @handle: shishi handle as allocated by shishi_init().
* @data: constant memory buffer with keytab of @len size.
* @len: size of memory buffer with keytab data.
* @keys: allocated key set to store keys in.
*
* Read keys from a MIT keytab data structure, and add them to the key
* set.
*
* The format of keytab's is proprietary, and this function support
* the 0x0501 and 0x0502 formats. See the section The MIT Kerberos
* Keytab Binary File Format in the Shishi manual for a description of
* the reverse-engineered format.
*
* Returns: Returns %SHISHI_KEYTAB_ERROR if the data does not
* represent a valid keytab structure, and %SHISHI_OK on success.
**/
int
shishi_keys_add_keytab_mem (Shishi * handle,
const char *data, size_t len, Shishi_keys * keys)
{
int rc;
uint16_t file_format_version;
size_t entrystartpos;
uint16_t num_components; /* sub 1 if version 0x501 */
size_t i;
Shishi_key *key;
if (VERBOSENOISE (handle))
{
printf ("keytab len %d (0x%x)\n", len, len);
_shishi_hexprint (data, len);
}
/* Check file format. */
file_format_version = (data[0] << 8) | data[1];
if (VERBOSENOISE (handle))
printf ("keytab file_format_version %04X\n", file_format_version);
if (file_format_version != 0x0501 && file_format_version != 0x0502)
return SHISHI_KEYTAB_ERROR;
/* Check file integrity first, to avoid error-checking below. */
entrystartpos = 2;
while (entrystartpos < len)
{
int32_t size = data[entrystartpos] << 24 | data[entrystartpos + 1] << 16
| data[entrystartpos + 2] << 8 | data[entrystartpos + 3];
entrystartpos += 4;
if (VERBOSENOISE (handle))
{
printf ("keytab size %d (%x)\n", size, size);
printf ("keytab pos %d < %d\n", entrystartpos + size, len);
}
if (entrystartpos + size > len)
return SHISHI_KEYTAB_ERROR;
/* Go to next entry... */
entrystartpos += size;
}
if (entrystartpos != len)
return SHISHI_KEYTAB_ERROR;
rc = shishi_key (handle, &key);
if (rc != SHISHI_OK)
return rc;
entrystartpos = 2;
while (entrystartpos < len)
{
size_t pos = entrystartpos;
uint16_t size = data[pos] << 24 | data[pos + 1] << 16
| data[pos + 2] << 8 | data[pos + 3];
pos += 4;
if (VERBOSENOISE (handle))
printf ("keytab size %d (%x)\n", size, size);
/* Num_components */
num_components = data[pos] << 8 | data[pos + 1];
pos += 2;
if (file_format_version == 0x0501)
num_components--;
/* Realm */
{
uint16_t realmlen = data[pos] << 8 | data[pos + 1];
char *realm = xstrndup (&data[pos + 2], realmlen);;
pos += 2 + realmlen;
shishi_key_realm_set (key, realm);
free (realm);
}
/* Principal components. */
{
char *name = NULL;
size_t namelen = 0;
for (i = 0; i < num_components; i++)
{
size_t l;
l = data[pos] << 8 | data[pos + 1];
pos += 2;
name = xrealloc (name, namelen + l + 1);
memcpy (name + namelen, &data[pos], l);
name[namelen + l] = '/';
namelen += l + 1;
pos += l;
}
name[namelen - 1] = '\0';
shishi_key_principal_set (key, name);
free (name);
}
/* Name_type */
{
uint32_t name_type /* not present if version 0x501 */
= data[pos] << 24 | data[pos + 1] << 16
| data[pos + 2] << 8 | data[pos + 3];
pos += 4;
if (VERBOSENOISE (handle))
printf ("keytab nametype %d (0x%08x)\n", name_type, name_type);
}
/* Timestamp */
{
uint32_t timestamp =
((data[pos] << 24) & 0xFF000000)
| ((data[pos + 1] << 16) & 0xFF0000)
| ((data[pos + 2] << 8) & 0xFF00) | ((data[pos + 3] & 0xFF));
time_t t = timestamp;
pos += 4;
if (VERBOSENOISE (handle))
printf ("keytab timestamp %u (0x%08ux)\n", timestamp, timestamp);
shishi_key_timestamp_set (key, t);
}
/* keyvno8 */
{
uint8_t vno8 = data[pos++];
if (VERBOSENOISE (handle))
printf ("keytab kvno8 %d (0x%02x)\n", vno8, vno8);
shishi_key_version_set (key, vno8);
}
/* key, keytype */
{
uint32_t keytype = data[pos] << 8 | data[pos + 1];
pos += 2;
if (VERBOSENOISE (handle))
printf ("keytab keytype %d (0x%x)\n", keytype, keytype);
shishi_key_type_set (key, keytype);
}
/* key, length and data */
{
uint16_t keylen = data[pos] << 8 | data[pos + 1];
pos += 2;
if (VERBOSENOISE (handle))
printf ("keytab keylen %d (0x%x) eq? %d\n", keylen, keylen,
shishi_key_length (key));
if (VERBOSENOISE (handle))
_shishi_hexprint (data + pos, keylen);
shishi_key_value_set (key, data + pos);
pos += keylen;
}
if (pos - entrystartpos < (size_t) size + 4)
{
uint32_t vno /* only present if >= 4 bytes left in entry */
= data[pos] << 24 | data[pos + 1] << 16
| data[pos + 2] << 8 | data[pos + 3];
pos += 4;
if (VERBOSENOISE (handle))
printf ("keytab kvno %d (0x%08x)\n", vno, vno);
shishi_key_version_set (key, vno);
}
if (VERBOSECRYPTONOISE (handle))
shishi_key_print (handle, stdout, key);
rc = shishi_keys_add (keys, key);
if (rc != SHISHI_OK)
goto done;
/* Go to next entry... */
entrystartpos += size + 4;
}
rc = SHISHI_OK;
done:
shishi_key_done (key);
return rc;
}
/**
* shishi_keys_add_keytab_file:
* @handle: shishi handle as allocated by shishi_init().
* @filename: name of file to read.
* @keys: allocated key set to store keys in.
*
* Read keys from a MIT keytab data structure from a file, and add the
* keys to the key set.
*
* The format of keytab's is proprietary, and this function support
* the 0x0501 and 0x0502 formats. See the section The MIT Kerberos
* Keytab Binary File Format in the Shishi manual for a description of
* the reverse-engineered format.
*
* Returns: Returns %SHISHI_IO_ERROR if the file cannot be read,
* %SHISHI_KEYTAB_ERROR if the data cannot be parsed as a valid keytab
* structure, and %SHISHI_OK on success.
**/
int
shishi_keys_add_keytab_file (Shishi * handle,
const char *filename, Shishi_keys * keys)
{
size_t len;
char *keytab = read_binary_file (filename, &len);
int rc;
if (!keytab)
return SHISHI_IO_ERROR;
rc = shishi_keys_add_keytab_mem (handle, keytab, len, keys);
free (keytab);
return rc;
}
/**
* shishi_keys_from_keytab_mem:
* @handle: shishi handle as allocated by shishi_init().
* @data: constant memory buffer with keytab of @len size.
* @len: size of memory buffer with keytab data.
* @outkeys: pointer to key set that will be allocated and populated,
* must be deallocated by caller on succes.
*
* Create a new key set populated with keys from a MIT keytab data
* structure read from a memory block.
*
* The format of keytab's is proprietary, and this function support
* the 0x0501 and 0x0502 formats. See the section The MIT Kerberos
* Keytab Binary File Format in the Shishi manual for a description of
* the reverse-engineered format.
*
* Returns: Returns %SHISHI_KEYTAB_ERROR if the data does not
* represent a valid keytab structure, and %SHISHI_OK on success.
**/
int
shishi_keys_from_keytab_mem (Shishi * handle,
const char *data, size_t len,
Shishi_keys ** outkeys)
{
int rc;
rc = shishi_keys (handle, outkeys);
if (rc != SHISHI_OK)
return rc;
rc = shishi_keys_add_keytab_mem (handle, data, len, *outkeys);
if (rc != SHISHI_OK)
{
shishi_keys_done (outkeys);
return rc;
}
return SHISHI_OK;
}
/**
* shishi_keys_from_keytab_file:
* @handle: shishi handle as allocated by shishi_init().
* @filename: name of file to read.
* @outkeys: pointer to key set that will be allocated and populated,
* must be deallocated by caller on succes.
*
* Create a new key set populated with keys from a MIT keytab data
* structure read from a file.
*
* The format of keytab's is proprietary, and this function support
* the 0x0501 and 0x0502 formats. See the section The MIT Kerberos
* Keytab Binary File Format in the Shishi manual for a description of
* the reverse-engineered format.
*
* Returns: Returns %SHISHI_IO_ERROR if the file cannot be read,
* %SHISHI_KEYTAB_ERROR if the data cannot be parsed as a valid keytab
* structure, and %SHISHI_OK on success.
**/
int
shishi_keys_from_keytab_file (Shishi * handle,
const char *filename, Shishi_keys ** outkeys)
{
int rc;
rc = shishi_keys (handle, outkeys);
if (rc != SHISHI_OK)
return rc;
rc = shishi_keys_add_keytab_file (handle, filename, *outkeys);
if (rc != SHISHI_OK)
{
shishi_keys_done (outkeys);
return rc;
}
return SHISHI_OK;
}
static int
key_to_keytab_entry (Shishi * handle,
const Shishi_key * key, char **out, size_t * len)
{
uint16_t num_components = 0;
const char *realm = shishi_key_realm (key);
size_t realmlen = strlen (realm);
const char *principal = shishi_key_principal (key);
uint32_t name_type = SHISHI_NT_PRINCIPAL;
time_t timestamp = shishi_key_timestamp (key);
uint32_t version = shishi_key_version (key);
uint16_t key_type = shishi_key_type (key);
size_t key_length = shishi_key_length (key);
const char *key_value = shishi_key_value (key);
char *tmpname;
const char **namebuf;
char *tokptr = NULL;
char *p;
size_t i;
if (realmlen > UINT16_MAX)
return SHISHI_KEYTAB_ERROR;
if (key_length > UINT16_MAX)
return SHISHI_KEYTAB_ERROR;
/* Reserve room for size, num_components, realm.length, realm,
name_type, timestamp, vno8, keyblock.type, keyblock.data.length,
keyblock.data, and version. */
*len = 4 + 2 + 2 + realmlen + 4 + 4 + 1 + 2 + 2 + key_length + 4;
tmpname = xstrdup (principal);
namebuf = xmalloc (sizeof (*namebuf));
for (num_components = 0; (namebuf[num_components] =
strtok_r (num_components == 0 ? tmpname
: NULL, "/", &tokptr));
num_components++)
{
size_t length = strlen (namebuf[num_components]);
if (length > UINT16_MAX)
return SHISHI_KEYTAB_ERROR;
*len += 2 + length;
namebuf = xrealloc (namebuf, (num_components + 2) * sizeof (*namebuf));
}
*out = xmalloc (*len);
p = *out;
/* Write size. */
p[0] = ((*len - 4) >> 24) & 0xFF;
p[1] = ((*len - 4) >> 16) & 0xFF;
p[2] = ((*len - 4) >> 8) & 0xFF;
p[3] = (*len - 4) & 0xFF;
p += 4;
/* Write num_components. */
p[0] = (num_components >> 8) & 0xFF;
p[1] = num_components & 0xFF;
p += 2;
/* Write realm.length and realm.data. */
p[0] = (realmlen >> 8) & 0xFF;
p[1] = realmlen & 0xFF;
p += 2;
memcpy (p, realm, realmlen);
p += realmlen;
for (i = 0; i < num_components; i++)
{
uint16_t length = strlen (namebuf[i]);
p[0] = (length >> 8) & 0xFF;
p[1] = length & 0xFF;
p += 2;
memcpy (p, namebuf[i], length);
p += length;
}
/* Name type */
p[0] = (name_type >> 24) & 0xFF;
p[1] = (name_type >> 16) & 0xFF;
p[2] = (name_type >> 8) & 0xFF;
p[3] = name_type & 0xFF;
p += 4;
/* Timestamp */
p[0] = (timestamp >> 24) & 0xFF;
p[1] = (timestamp >> 16) & 0xFF;
p[2] = (timestamp >> 8) & 0xFF;
p[3] = timestamp & 0xFF;
p += 4;
/* Version */
if (version < 256)
p[0] = version & 0xFF;
else
p[0] = 0; /* use vno */
p += 1;
/* Key */
p[0] = (key_type >> 8) & 0xFF;
p[1] = key_type & 0xFF;
p += 2;
p[0] = (key_length >> 8) & 0xFF;
p[1] = key_length & 0xFF;
p += 2;
memcpy (p, key_value, key_length);
p += key_length;
/* Version */
p[0] = (version >> 24) & 0xFF;
p[1] = (version >> 16) & 0xFF;
p[2] = (version >> 8) & 0xFF;
p[3] = version & 0xFF;
free (tmpname);
free (namebuf);
return SHISHI_OK;
}
/**
* shishi_keys_to_keytab_mem:
* @handle: shishi handle as allocated by shishi_init().
* @keys: key set to convert to keytab format.
* @out: constant memory buffer with keytab of @len size.
* @len: size of memory buffer with keytab data.
*
* Write keys to a MIT keytab data structure.
*
* The format of keytab's is proprietary, and this function writes the
* 0x0502 format. See the section The MIT Kerberos Keytab Binary File
* Format in the Shishi manual for a description of the
* reverse-engineered format.
*
* Returns: On success %SHISHI_OK is returned, otherwise an error
* code.
*
* Since: 0.0.42
**/
int
shishi_keys_to_keytab_mem (Shishi * handle,
Shishi_keys * keys, char **out, size_t * len)
{
int rc;
const Shishi_key *key;
int keyno = 0;
*out = xmalloc (2);
*len = 2;
/* Write file format version. */
(*out)[0] = '\x05';
(*out)[1] = '\x02';
while ((key = shishi_keys_nth (keys, keyno++)) != NULL)
{
char *tmp = NULL;
size_t tmplen = 0;
rc = key_to_keytab_entry (handle, key, &tmp, &tmplen);
if (rc != SHISHI_OK)
{
free (*out);
return rc;
}
*out = xrealloc (*out, *len + tmplen);
memcpy (*out + *len, tmp, tmplen);
*len += tmplen;
}
if (VERBOSENOISE (handle))
{
printf ("keys_to_keytab len %d (0x%x)\n", *len, *len);
_shishi_hexprint (*out, *len);
}
return rc;
}
static int
write_binary_file (const char *filename, const char *data, size_t length)
{
FILE *fh;
size_t written;
fh = fopen (filename, "wb");
if (!fh)
return SHISHI_FOPEN_ERROR;
written = fwrite (data, 1, length, fh);
if (written != length)
return SHISHI_IO_ERROR;
return SHISHI_OK;
}
/**
* shishi_keys_to_keytab_file:
* @handle: shishi handle as allocated by shishi_init().
* @keys: keyset to write.
* @filename: name of file to write.
*
* Write keys to a MIT keytab data structure.
*
* The format of keytab's is proprietary, and this function writes the
* 0x0502 format. See the section The MIT Kerberos Keytab Binary File
* Format in the Shishi manual for a description of the
* reverse-engineered format.
*
* Returns: %SHISHI_FOPEN_ERROR if there is a problem opening
* @filename for writing, %SHISHI_IO_ERROR if there is problem
* writing the file, and %SHISHI_OK on success.
*
* Since: 0.0.42
**/
int
shishi_keys_to_keytab_file (Shishi * handle,
Shishi_keys * keys, const char *filename)
{
int rc;
char *data;
size_t len;
rc = shishi_keys_to_keytab_mem (handle, keys, &data, &len);
if (rc != SHISHI_OK)
return rc;
rc = write_binary_file (filename, data, len);
if (rc != SHISHI_OK)
return rc;
return SHISHI_OK;
}
|