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 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
|
/* apreq.c --- AP-REQ functions.
* Copyright (C) 2002-2022 Simon Josefsson
*
* This file is part of Shishi.
*
* Shishi is free software; you can redistribute it and/or modify it 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 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_print_armored_data, etc. */
#include "diskio.h"
#define SHISHI_APREQ_DEFAULT_PVNO "5"
#define SHISHI_APREQ_DEFAULT_PVNO_LEN 0
#define SHISHI_APREQ_DEFAULT_MSG_TYPE "14" /* KRB_AP_REQ */
#define SHISHI_APREQ_DEFAULT_MSG_TYPE_LEN 0
#define SHISHI_APREQ_DEFAULT_AP_OPTIONS "\x00\x00\x00\x00"
#define SHISHI_APREQ_DEFAULT_AP_OPTIONS_LEN 32
#define SHISHI_APREQ_DEFAULT_TICKET_TKT_VNO "5"
#define SHISHI_APREQ_DEFAULT_TICKET_TKT_VNO_LEN 0
#define SHISHI_APREQ_DEFAULT_TICKET_REALM ""
#define SHISHI_APREQ_DEFAULT_TICKET_REALM_LEN 0
#define SHISHI_APREQ_DEFAULT_TICKET_SNAME_NAME_TYPE "1" /* SHISHI_NT_PRINCIPAL */
#define SHISHI_APREQ_DEFAULT_TICKET_SNAME_NAME_TYPE_LEN 0
#define SHISHI_APREQ_DEFAULT_TICKET_ENC_PART_ETYPE "0"
#define SHISHI_APREQ_DEFAULT_TICKET_ENC_PART_ETYPE_LEN 0
#define SHISHI_APREQ_DEFAULT_TICKET_ENC_PART_KVNO "0"
#define SHISHI_APREQ_DEFAULT_TICKET_ENC_PART_KVNO_LEN 0
#define SHISHI_APREQ_DEFAULT_TICKET_ENC_PART_CIPHER ""
#define SHISHI_APREQ_DEFAULT_TICKET_ENC_PART_CIPHER_LEN 0
#define SHISHI_APREQ_DEFAULT_AUTHENTICATOR_ETYPE "0"
#define SHISHI_APREQ_DEFAULT_AUTHENTICATOR_ETYPE_LEN 0
#define SHISHI_APREQ_DEFAULT_AUTHENTICATOR_KVNO "1"
#define SHISHI_APREQ_DEFAULT_AUTHENTICATOR_KVNO_LEN 0
#define SHISHI_APREQ_DEFAULT_AUTHENTICATOR_CIPHER ""
#define SHISHI_APREQ_DEFAULT_AUTHENTICATOR_CIPHER_LEN 0
/**
* shishi_apreq:
* @handle: shishi handle as allocated by shishi_init().
*
* This function creates a new AP-REQ, populated with some default
* values.
*
* Return value: Returns the AP-REQ or NULL on failure.
**/
Shishi_asn1
shishi_apreq (Shishi * handle)
{
Shishi_asn1 node;
int res;
node = shishi_asn1_apreq (handle);
if (!node)
goto error;
res = shishi_asn1_write (handle, node, "pvno",
SHISHI_APREQ_DEFAULT_PVNO,
SHISHI_APREQ_DEFAULT_PVNO_LEN);
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_write (handle, node, "msg-type",
SHISHI_APREQ_DEFAULT_MSG_TYPE,
SHISHI_APREQ_DEFAULT_MSG_TYPE_LEN);
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_write (handle, node, "ap-options",
SHISHI_APREQ_DEFAULT_AP_OPTIONS,
SHISHI_APREQ_DEFAULT_AP_OPTIONS_LEN);
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_write (handle, node, "ticket.tkt-vno",
SHISHI_APREQ_DEFAULT_TICKET_TKT_VNO,
SHISHI_APREQ_DEFAULT_TICKET_TKT_VNO_LEN);
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_write (handle, node, "ticket.realm",
SHISHI_APREQ_DEFAULT_TICKET_REALM,
SHISHI_APREQ_DEFAULT_TICKET_REALM_LEN);
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_write (handle, node, "ticket.realm",
SHISHI_APREQ_DEFAULT_TICKET_REALM,
SHISHI_APREQ_DEFAULT_TICKET_REALM_LEN);
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_write (handle, node, "ticket.sname.name-type",
SHISHI_APREQ_DEFAULT_TICKET_SNAME_NAME_TYPE,
SHISHI_APREQ_DEFAULT_TICKET_SNAME_NAME_TYPE_LEN);
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_write (handle, node, "ticket.enc-part.etype",
SHISHI_APREQ_DEFAULT_TICKET_ENC_PART_ETYPE,
SHISHI_APREQ_DEFAULT_TICKET_ENC_PART_ETYPE_LEN);
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_write (handle, node, "ticket.enc-part.kvno",
SHISHI_APREQ_DEFAULT_TICKET_ENC_PART_KVNO,
SHISHI_APREQ_DEFAULT_TICKET_ENC_PART_KVNO_LEN);
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_write (handle, node, "ticket.enc-part.cipher",
SHISHI_APREQ_DEFAULT_TICKET_ENC_PART_CIPHER,
SHISHI_APREQ_DEFAULT_TICKET_ENC_PART_CIPHER_LEN);
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_write (handle, node, "authenticator.etype",
SHISHI_APREQ_DEFAULT_AUTHENTICATOR_ETYPE,
SHISHI_APREQ_DEFAULT_AUTHENTICATOR_ETYPE_LEN);
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_write (handle, node, "authenticator.kvno",
SHISHI_APREQ_DEFAULT_AUTHENTICATOR_KVNO,
SHISHI_APREQ_DEFAULT_AUTHENTICATOR_KVNO_LEN);
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_write (handle, node, "authenticator.cipher",
SHISHI_APREQ_DEFAULT_AUTHENTICATOR_CIPHER,
SHISHI_APREQ_DEFAULT_AUTHENTICATOR_CIPHER_LEN);
if (res != SHISHI_OK)
goto error;
return node;
error:
if (node)
shishi_asn1_done (handle, node);
return NULL;
}
/**
* shishi_apreq_print:
* @handle: shishi handle as allocated by shishi_init().
* @fh: file handle open for writing.
* @apreq: AP-REQ to print.
*
* Print ASCII armored DER encoding of AP-REQ to file.
*
* Return value: Returns SHISHI_OK iff successful.
**/
int
shishi_apreq_print (Shishi * handle, FILE * fh, Shishi_asn1 apreq)
{
return _shishi_print_armored_data (handle, fh, apreq, "AP-REQ", NULL);
}
/**
* shishi_apreq_save:
* @handle: shishi handle as allocated by shishi_init().
* @fh: file handle open for writing.
* @apreq: AP-REQ to save.
*
* Save DER encoding of AP-REQ to file.
*
* Return value: Returns SHISHI_OK iff successful.
**/
int
shishi_apreq_save (Shishi * handle, FILE * fh, Shishi_asn1 apreq)
{
return _shishi_save_data (handle, fh, apreq, "AP-REQ");
}
/**
* shishi_apreq_to_file:
* @handle: shishi handle as allocated by shishi_init().
* @apreq: AP-REQ to save.
* @filetype: input variable specifying type of file to be written,
* see Shishi_filetype.
* @filename: input variable with filename to write to.
*
* Write AP-REQ to file in specified TYPE. The file will be
* truncated if it exists.
*
* Return value: Returns SHISHI_OK iff successful.
**/
int
shishi_apreq_to_file (Shishi * handle, Shishi_asn1 apreq,
int filetype, const char *filename)
{
FILE *fh;
int res;
if (VERBOSE (handle))
printf (_("Writing AP-REQ to %s...\n"), filename);
fh = fopen (filename, "w");
if (fh == NULL)
return SHISHI_FOPEN_ERROR;
if (VERBOSE (handle))
printf (_("Writing AP-REQ in %s format...\n"),
filetype == SHISHI_FILETYPE_TEXT ? "TEXT" : "DER");
if (filetype == SHISHI_FILETYPE_TEXT)
res = shishi_apreq_print (handle, fh, apreq);
else
res = shishi_apreq_save (handle, fh, apreq);
if (res != SHISHI_OK)
return res;
res = fclose (fh);
if (res != 0)
return SHISHI_IO_ERROR;
if (VERBOSE (handle))
printf (_("Writing AP-REQ to %s...done\n"), filename);
return SHISHI_OK;
}
/**
* shishi_apreq_parse:
* @handle: shishi handle as allocated by shishi_init().
* @fh: file handle open for reading.
* @apreq: output variable with newly allocated AP-REQ.
*
* Read ASCII armored DER encoded AP-REQ from file and populate given
* variable.
*
* Return value: Returns SHISHI_OK iff successful.
**/
int
shishi_apreq_parse (Shishi * handle, FILE * fh, Shishi_asn1 * apreq)
{
return _shishi_apreq_input (handle, fh, apreq, 0);
}
/**
* shishi_apreq_read:
* @handle: shishi handle as allocated by shishi_init().
* @fh: file handle open for reading.
* @apreq: output variable with newly allocated AP-REQ.
*
* Read DER encoded AP-REQ from file and populate given variable.
*
* Return value: Returns SHISHI_OK iff successful.
**/
int
shishi_apreq_read (Shishi * handle, FILE * fh, Shishi_asn1 * apreq)
{
return _shishi_apreq_input (handle, fh, apreq, 1);
}
/**
* shishi_apreq_from_file:
* @handle: shishi handle as allocated by shishi_init().
* @apreq: output variable with newly allocated AP-REQ.
* @filetype: input variable specifying type of file to be read,
* see Shishi_filetype.
* @filename: input variable with filename to read from.
*
* Read AP-REQ from file in specified TYPE.
*
* Return value: Returns SHISHI_OK iff successful.
**/
int
shishi_apreq_from_file (Shishi * handle, Shishi_asn1 * apreq,
int filetype, const char *filename)
{
int res;
FILE *fh;
if (VERBOSE (handle))
printf (_("Reading AP-REQ from %s...\n"), filename);
fh = fopen (filename, "r");
if (fh == NULL)
return SHISHI_FOPEN_ERROR;
if (VERBOSE (handle))
printf (_("Reading AP-REQ in %s format...\n"),
filetype == SHISHI_FILETYPE_TEXT ? "TEXT" : "DER");
if (filetype == SHISHI_FILETYPE_TEXT)
res = shishi_apreq_parse (handle, fh, apreq);
else
res = shishi_apreq_read (handle, fh, apreq);
if (res != SHISHI_OK)
return res;
res = fclose (fh);
if (res != 0)
return SHISHI_IO_ERROR;
if (VERBOSE (handle))
printf (_("Reading AP-REQ from %s...done\n"), filename);
return SHISHI_OK;
}
/**
* shishi_apreq_set_authenticator:
* @handle: shishi handle as allocated by shishi_init().
* @apreq: AP-REQ to add authenticator field to.
* @etype: encryption type used to encrypt authenticator.
* @kvno: version of the key used to encrypt authenticator.
* @buf: input array with encrypted authenticator.
* @buflen: size of input array with encrypted authenticator.
*
* Set the encrypted authenticator field in the AP-REP. The encrypted
* data is usually created by calling shishi_encrypt() on the DER
* encoded authenticator. To save time, you may want to use
* shishi_apreq_add_authenticator() instead, which calculates the
* encrypted data and calls this function in one step.
*
* Return value: Returns SHISHI_OK on success.
**/
int
shishi_apreq_set_authenticator (Shishi * handle,
Shishi_asn1 apreq,
int32_t etype, uint32_t kvno,
const char *buf, size_t buflen)
{
int res;
res = shishi_asn1_write (handle, apreq, "authenticator.cipher",
buf, buflen);
if (res != SHISHI_OK)
return res;
if (kvno == UINT32_MAX)
res = shishi_asn1_write (handle, apreq, "authenticator.kvno", NULL, 0);
else
res = shishi_asn1_write_int32 (handle, apreq, "authenticator.kvno", kvno);
if (res != SHISHI_OK)
return res;
res = shishi_asn1_write_int32 (handle, apreq, "authenticator.etype", etype);
if (res != SHISHI_OK)
return res;
return SHISHI_OK;
}
/**
* shishi_apreq_add_authenticator:
* @handle: shishi handle as allocated by shishi_init().
* @apreq: AP-REQ to add authenticator field to.
* @key: key to to use for encryption.
* @keyusage: cryptographic key usage value to use in encryption.
* @authenticator: authenticator as allocated by shishi_authenticator().
*
* Encrypts DER encoded authenticator using key and store it in the
* AP-REQ.
*
* Return value: Returns SHISHI_OK iff successful.
**/
int
shishi_apreq_add_authenticator (Shishi * handle,
Shishi_asn1 apreq,
Shishi_key * key,
int keyusage, Shishi_asn1 authenticator)
{
int res;
char *buf;
size_t buflen;
char *der;
size_t derlen;
res = shishi_asn1_to_der (handle, authenticator, &der, &derlen);
if (res != SHISHI_OK)
{
shishi_error_printf (handle, "Could not DER encode authenticator: %s\n",
shishi_strerror (res));
return res;
}
res = shishi_encrypt (handle, key, keyusage, der, derlen, &buf, &buflen);
free (der);
if (res != SHISHI_OK)
{
shishi_error_printf (handle, "Cannot encrypt authenticator.\n");
return res;
}
res = shishi_apreq_set_authenticator (handle, apreq, shishi_key_type (key),
shishi_key_version (key),
buf, buflen);
return res;
}
/**
* shishi_apreq_set_ticket:
* @handle: shishi handle as allocated by shishi_init().
* @apreq: AP-REQ to add ticket field to.
* @ticket: input ticket to copy into AP-REQ ticket field.
*
* Copy ticket into AP-REQ.
*
* Return value: Returns SHISHI_OK iff successful.
**/
int
shishi_apreq_set_ticket (Shishi * handle, Shishi_asn1 apreq,
Shishi_asn1 ticket)
{
int res;
char *format;
char *buf;
size_t buflen, i, n;
res = shishi_asn1_read (handle, ticket, "tkt-vno", &buf, &buflen);
if (res != SHISHI_OK)
return res;
res = shishi_asn1_write (handle, apreq, "ticket.tkt-vno", buf, buflen);
free (buf);
if (res != SHISHI_OK)
return res;
res = shishi_asn1_read (handle, ticket, "realm", &buf, &buflen);
if (res != SHISHI_OK)
return res;
res = shishi_asn1_write (handle, apreq, "ticket.realm", buf, buflen);
free (buf);
if (res != SHISHI_OK)
return res;
res = shishi_asn1_read (handle, ticket, "sname.name-type", &buf, &buflen);
if (res != SHISHI_OK)
return res;
res = shishi_asn1_write (handle, apreq, "ticket.sname.name-type",
buf, buflen);
free (buf);
if (res != SHISHI_OK)
return res;
res = shishi_asn1_number_of_elements (handle, ticket,
"sname.name-string", &n);
if (res != SHISHI_OK)
return res;
for (i = 1; i <= n; i++)
{
res = shishi_asn1_write (handle, apreq,
"ticket.sname.name-string", "NEW", 1);
if (res != SHISHI_OK)
return res;
asprintf (&format, "sname.name-string.?%zu", i);
res = shishi_asn1_read (handle, ticket, format, &buf, &buflen);
free (format);
if (res != SHISHI_OK)
return res;
asprintf (&format, "ticket.sname.name-string.?%zu", i);
res = shishi_asn1_write (handle, apreq, format, buf, buflen);
free (format);
free (buf);
if (res != SHISHI_OK)
return res;
}
res = shishi_asn1_read (handle, ticket, "enc-part.etype", &buf, &buflen);
if (res != SHISHI_OK)
return res;
res = shishi_asn1_write (handle, apreq, "ticket.enc-part.etype",
buf, buflen);
free (buf);
if (res != SHISHI_OK)
return res;
res = shishi_asn1_read (handle, ticket, "enc-part.kvno", &buf, &buflen);
if (res != SHISHI_OK && res != SHISHI_ASN1_NO_ELEMENT)
return res;
if (res == SHISHI_ASN1_NO_ELEMENT)
res = shishi_asn1_write (handle, apreq, "ticket.enc-part.kvno", NULL, 0);
else
{
res = shishi_asn1_write (handle, apreq, "ticket.enc-part.kvno",
buf, buflen);
free (buf);
}
if (res != SHISHI_OK)
return res;
res = shishi_asn1_read (handle, ticket, "enc-part.cipher", &buf, &buflen);
if (res != SHISHI_OK)
return res;
res = shishi_asn1_write (handle, apreq, "ticket.enc-part.cipher",
buf, buflen);
free (buf);
if (res != SHISHI_OK)
return res;
return SHISHI_OK;
}
/**
* shishi_apreq_options:
* @handle: shishi handle as allocated by shishi_init().
* @apreq: AP-REQ to get options from.
* @flags: Output integer containing options from AP-REQ.
*
* Extract the AP-Options from AP-REQ into output integer.
*
* Return value: Returns SHISHI_OK iff successful.
**/
int
shishi_apreq_options (Shishi * handle, Shishi_asn1 apreq, uint32_t * flags)
{
return shishi_asn1_read_bitstring (handle, apreq, "ap-options", flags);
}
/**
* shishi_apreq_use_session_key_p:
* @handle: shishi handle as allocated by shishi_init().
* @apreq: AP-REQ as allocated by shishi_apreq().
*
* Return non-0 iff the "Use session key" option is set in the AP-REQ.
*
* Return value: Returns SHISHI_OK iff successful.
**/
int
shishi_apreq_use_session_key_p (Shishi * handle, Shishi_asn1 apreq)
{
uint32_t options = 0;
shishi_apreq_options (handle, apreq, &options);
return options & SHISHI_APOPTIONS_USE_SESSION_KEY;
}
/**
* shishi_apreq_mutual_required_p:
* @handle: shishi handle as allocated by shishi_init().
* @apreq: AP-REQ as allocated by shishi_apreq().
*
* Return non-0 iff the "Mutual required" option is set in the AP-REQ.
*
* Return value: Returns SHISHI_OK iff successful.
**/
int
shishi_apreq_mutual_required_p (Shishi * handle, Shishi_asn1 apreq)
{
uint32_t options = 0;
shishi_apreq_options (handle, apreq, &options);
return options & SHISHI_APOPTIONS_MUTUAL_REQUIRED;
}
/**
* shishi_apreq_options_set:
* @handle: shishi handle as allocated by shishi_init().
* @apreq: AP-REQ as allocated by shishi_apreq().
* @options: Options to set in AP-REQ.
*
* Set the AP-Options in AP-REQ to indicate integer.
*
* Return value: Returns SHISHI_OK iff successful.
**/
int
shishi_apreq_options_set (Shishi * handle, Shishi_asn1 apreq,
uint32_t options)
{
int res;
res = shishi_asn1_write_bitstring (handle, apreq, "ap-options", options);
if (res != SHISHI_OK)
return res;
return SHISHI_OK;
}
/**
* shishi_apreq_options_add:
* @handle: shishi handle as allocated by shishi_init().
* @apreq: AP-REQ as allocated by shishi_apreq().
* @option: Options to add in AP-REQ.
*
* Add the AP-Options in AP-REQ. Options not set in input parameter
* @option are preserved in the AP-REQ.
*
* Return value: Returns SHISHI_OK iff successful.
**/
int
shishi_apreq_options_add (Shishi * handle, Shishi_asn1 apreq, uint32_t option)
{
uint32_t options;
int res;
res = shishi_apreq_options (handle, apreq, &options);
if (res != SHISHI_OK)
return res;
options |= option;
res = shishi_apreq_options_set (handle, apreq, options);
if (res != SHISHI_OK)
return res;
return SHISHI_OK;
}
/**
* shishi_apreq_options_remove:
* @handle: shishi handle as allocated by shishi_init().
* @apreq: AP-REQ as allocated by shishi_apreq().
* @option: Options to remove from AP-REQ.
*
* Remove the AP-Options from AP-REQ. Options not set in input
* parameter @option are preserved in the AP-REQ.
*
* Return value: Returns SHISHI_OK iff successful.
**/
int
shishi_apreq_options_remove (Shishi * handle,
Shishi_asn1 apreq, uint32_t option)
{
uint32_t options;
int res;
res = shishi_apreq_options (handle, apreq, &options);
if (res != SHISHI_OK)
return res;
options &= ~(options & option);
res = shishi_apreq_options_set (handle, apreq, options);
if (res != SHISHI_OK)
return res;
return SHISHI_OK;
}
/**
* shishi_apreq_get_authenticator_etype:
* @handle: shishi handle as allocated by shishi_init().
* @apreq: AP-REQ variable to get value from.
* @etype: output variable that holds the value.
*
* Extract AP-REQ.authenticator.etype.
*
* Return value: Returns SHISHI_OK iff successful.
**/
int
shishi_apreq_get_authenticator_etype (Shishi * handle,
Shishi_asn1 apreq, int32_t * etype)
{
return shishi_asn1_read_int32 (handle, apreq, "authenticator.etype", etype);
}
/**
* shishi_apreq_get_ticket:
* @handle: shishi handle as allocated by shishi_init().
* @apreq: AP-REQ variable to get ticket from.
* @ticket: output variable to hold extracted ticket.
*
* Extract ticket from AP-REQ.
*
* Return value: Returns SHISHI_OK iff successful.
**/
int
shishi_apreq_get_ticket (Shishi * handle,
Shishi_asn1 apreq, Shishi_asn1 * ticket)
{
char *buf;
char *format;
size_t buflen, i, n;
int res;
/* there's GOT to be an easier way to do this */
*ticket = shishi_ticket (handle);
if (!*ticket)
return SHISHI_ASN1_ERROR;
res = shishi_asn1_read (handle, apreq, "ticket.tkt-vno", &buf, &buflen);
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_write (handle, *ticket, "tkt-vno", buf, buflen);
free (buf);
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_read (handle, apreq, "ticket.realm", &buf, &buflen);
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_write (handle, *ticket, "realm", buf, buflen);
free (buf);
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_read (handle, apreq, "ticket.sname.name-type",
&buf, &buflen);
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_write (handle, *ticket, "sname.name-type", buf, buflen);
free (buf);
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_number_of_elements (handle, apreq,
"ticket.sname.name-string", &n);
if (res != SHISHI_OK)
goto error;
for (i = 1; i <= n; i++)
{
res = shishi_asn1_write (handle, *ticket, "sname.name-string",
"NEW", 1);
if (res != SHISHI_OK)
goto error;
asprintf (&format, "ticket.sname.name-string.?%zu", i);
res = shishi_asn1_read (handle, apreq, format, &buf, &buflen);
free (format);
if (res != SHISHI_OK)
goto error;
asprintf (&format, "sname.name-string.?%zu", i);
res = shishi_asn1_write (handle, *ticket, format, buf, buflen);
free (format);
free (buf);
if (res != SHISHI_OK)
goto error;
}
res = shishi_asn1_read (handle, apreq, "ticket.enc-part.etype",
&buf, &buflen);
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_write (handle, *ticket, "enc-part.etype", buf, buflen);
free (buf);
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_read (handle, apreq, "ticket.enc-part.kvno",
&buf, &buflen);
if (res != SHISHI_OK && res != SHISHI_ASN1_NO_ELEMENT)
goto error;
if (res == SHISHI_ASN1_NO_ELEMENT)
res = shishi_asn1_write (handle, *ticket, "enc-part.kvno", NULL, 0);
else
{
res = shishi_asn1_write (handle, *ticket, "enc-part.kvno", buf, buflen);
free (buf);
}
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_read (handle, apreq, "ticket.enc-part.cipher",
&buf, &buflen);
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_write (handle, *ticket, "enc-part.cipher", buf, buflen);
free (buf);
if (res != SHISHI_OK)
goto error;
return SHISHI_OK;
error:
shishi_asn1_done (handle, *ticket);
return res;
}
int
shishi_apreq_decrypt (Shishi * handle,
Shishi_asn1 apreq,
Shishi_key * key,
int keyusage, Shishi_asn1 * authenticator)
{
int res;
int i;
char *buf;
size_t buflen;
char *cipher;
size_t cipherlen;
int etype;
res = shishi_apreq_get_authenticator_etype (handle, apreq, &etype);
if (res != SHISHI_OK)
return res;
if (etype != shishi_key_type (key))
return SHISHI_APREQ_BAD_KEYTYPE;
res = shishi_asn1_read (handle, apreq, "authenticator.cipher",
&cipher, &cipherlen);
if (res != SHISHI_OK)
return res;
res = shishi_decrypt (handle, key, keyusage,
cipher, cipherlen, &buf, &buflen);
free (cipher);
if (res != SHISHI_OK)
{
shishi_error_printf (handle,
"decrypt fail, most likely wrong password\n");
return SHISHI_APREQ_DECRYPT_FAILED;
}
/* The crypto is so 1980; no length indicator. Trim off pad bytes
until we can parse it. */
for (i = 0; i < 8; i++)
{
if (VERBOSEASN1 (handle))
printf ("Trying with %d pad in enckdcrep...\n", i);
*authenticator = shishi_der2asn1_authenticator (handle, &buf[0],
buflen - i);
if (*authenticator != NULL)
break;
}
if (*authenticator == NULL)
{
shishi_error_printf (handle, "Could not DER decode Authenticator. "
"Password probably correct (decrypt ok) though\n");
return SHISHI_ASN1_ERROR;
}
return SHISHI_OK;
}
|