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
|
/* aprep.c --- AP-REP 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_APREP_DEFAULT_PVNO "5"
#define SHISHI_APREP_DEFAULT_PVNO_LEN 0
#define SHISHI_APREP_DEFAULT_MSG_TYPE "15" /* KRB_AP_REP */
#define SHISHI_APREP_DEFAULT_MSG_TYPE_LEN 0
#define SHISHI_APREP_DEFAULT_ENC_PART_ETYPE "0"
#define SHISHI_APREP_DEFAULT_ENC_PART_ETYPE_LEN 0
#define SHISHI_APREP_DEFAULT_ENC_PART_KVNO "0"
#define SHISHI_APREP_DEFAULT_ENC_PART_KVNO_LEN 0
#define SHISHI_APREP_DEFAULT_ENC_PART_CIPHER ""
#define SHISHI_APREP_DEFAULT_ENC_PART_CIPHER_LEN 0
/**
* shishi_aprep:
* @handle: shishi handle as allocated by shishi_init().
*
* This function creates a new AP-REP, populated with some default
* values.
*
* Return value: Returns the authenticator or NULL on
* failure.
**/
Shishi_asn1
shishi_aprep (Shishi * handle)
{
Shishi_asn1 node;
int res;
node = shishi_asn1_aprep (handle);
if (!node)
return NULL;
res = shishi_asn1_write (handle, node, "pvno",
SHISHI_APREP_DEFAULT_PVNO,
SHISHI_APREP_DEFAULT_PVNO_LEN);
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_write (handle, node, "msg-type",
SHISHI_APREP_DEFAULT_MSG_TYPE,
SHISHI_APREP_DEFAULT_MSG_TYPE_LEN);
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_write (handle, node, "enc-part.etype",
SHISHI_APREP_DEFAULT_ENC_PART_ETYPE,
SHISHI_APREP_DEFAULT_ENC_PART_ETYPE_LEN);
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_write (handle, node, "enc-part.kvno",
SHISHI_APREP_DEFAULT_ENC_PART_KVNO,
SHISHI_APREP_DEFAULT_ENC_PART_KVNO_LEN);
if (res != SHISHI_OK)
goto error;
res = shishi_asn1_write (handle, node, "enc-part.cipher",
SHISHI_APREP_DEFAULT_ENC_PART_CIPHER,
SHISHI_APREP_DEFAULT_ENC_PART_CIPHER_LEN);
if (res != SHISHI_OK)
goto error;
return node;
error:
shishi_asn1_done (handle, node);
return NULL;
}
/**
* shishi_aprep_print:
* @handle: shishi handle as allocated by shishi_init().
* @fh: file handle open for writing.
* @aprep: AP-REP to print.
*
* Print ASCII armored DER encoding of AP-REP to file.
*
* Return value: Returns SHISHI_OK iff successful.
**/
int
shishi_aprep_print (Shishi * handle, FILE * fh, Shishi_asn1 aprep)
{
return _shishi_print_armored_data (handle, fh, aprep, "AP-REP", NULL);
}
/**
* shishi_aprep_save:
* @handle: shishi handle as allocated by shishi_init().
* @fh: file handle open for writing.
* @aprep: AP-REP to save.
*
* Save DER encoding of AP-REP to file.
*
* Return value: Returns SHISHI_OK iff successful.
**/
int
shishi_aprep_save (Shishi * handle, FILE * fh, Shishi_asn1 aprep)
{
return _shishi_save_data (handle, fh, aprep, "AP-REP");
}
/**
* shishi_aprep_to_file:
* @handle: shishi handle as allocated by shishi_init().
* @aprep: AP-REP 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-REP to file in specified TYPE. The file will be
* truncated if it exists.
*
* Return value: Returns SHISHI_OK iff successful.
**/
int
shishi_aprep_to_file (Shishi * handle, Shishi_asn1 aprep,
int filetype, const char *filename)
{
FILE *fh;
int res;
if (VERBOSE (handle))
printf (_("Writing AP-REP to %s...\n"), filename);
fh = fopen (filename, "w");
if (fh == NULL)
return SHISHI_FOPEN_ERROR;
if (VERBOSE (handle))
printf (_("Writing AP-REP in %s format...\n"),
filetype == SHISHI_FILETYPE_TEXT ? "TEXT" : "DER");
if (filetype == SHISHI_FILETYPE_TEXT)
res = shishi_aprep_print (handle, fh, aprep);
else
res = shishi_aprep_save (handle, fh, aprep);
if (res != SHISHI_OK)
return res;
res = fclose (fh);
if (res != 0)
return SHISHI_IO_ERROR;
if (VERBOSE (handle))
printf (_("Writing AP-REP to %s...done\n"), filename);
return SHISHI_OK;
}
/**
* shishi_aprep_parse:
* @handle: shishi handle as allocated by shishi_init().
* @fh: file handle open for reading.
* @aprep: output variable with newly allocated AP-REP.
*
* Read ASCII armored DER encoded AP-REP from file and populate given
* variable.
*
* Return value: Returns SHISHI_OK iff successful.
**/
int
shishi_aprep_parse (Shishi * handle, FILE * fh, Shishi_asn1 * aprep)
{
return _shishi_aprep_input (handle, fh, aprep, 0);
}
/**
* shishi_aprep_read:
* @handle: shishi handle as allocated by shishi_init().
* @fh: file handle open for reading.
* @aprep: output variable with newly allocated AP-REP.
*
* Read DER encoded AP-REP from file and populate given variable.
*
* Return value: Returns SHISHI_OK iff successful.
**/
int
shishi_aprep_read (Shishi * handle, FILE * fh, Shishi_asn1 * aprep)
{
return _shishi_aprep_input (handle, fh, aprep, 1);
}
/**
* shishi_aprep_from_file:
* @handle: shishi handle as allocated by shishi_init().
* @aprep: output variable with newly allocated AP-REP.
* @filetype: input variable specifying type of file to be read,
* see Shishi_filetype.
* @filename: input variable with filename to read from.
*
* Read AP-REP from file in specified TYPE.
*
* Return value: Returns SHISHI_OK iff successful.
**/
int
shishi_aprep_from_file (Shishi * handle, Shishi_asn1 * aprep,
int filetype, const char *filename)
{
int res;
FILE *fh;
if (VERBOSE (handle))
printf (_("Reading AP-REP from %s...\n"), filename);
fh = fopen (filename, "r");
if (fh == NULL)
return SHISHI_FOPEN_ERROR;
if (VERBOSE (handle))
printf (_("Reading AP-REP in %s format...\n"),
filetype == SHISHI_FILETYPE_TEXT ? "TEXT" : "DER");
if (filetype == SHISHI_FILETYPE_TEXT)
res = shishi_aprep_parse (handle, fh, aprep);
else
res = shishi_aprep_read (handle, fh, aprep);
if (res != SHISHI_OK)
return res;
res = fclose (fh);
if (res != 0)
return SHISHI_IO_ERROR;
if (VERBOSE (handle))
printf (_("Reading AP-REP from %s...done\n"), filename);
return SHISHI_OK;
}
int
shishi_aprep_enc_part_set (Shishi * handle,
Shishi_asn1 aprep,
int etype, const char *buf, size_t buflen)
{
int res;
res = shishi_asn1_write (handle, aprep, "enc-part.cipher", buf, buflen);
if (res != SHISHI_OK)
return res;
res = shishi_asn1_write_integer (handle, aprep, "enc-part.etype", etype);
if (res != SHISHI_OK)
return res;
return SHISHI_OK;
}
int
shishi_aprep_enc_part_add (Shishi * handle,
Shishi_asn1 aprep,
Shishi_asn1 encticketpart,
Shishi_asn1 encapreppart)
{
int res;
char *buf;
size_t buflen;
char *der;
size_t derlen;
Shishi_key *key;
res = shishi_encticketpart_get_key (handle, encticketpart, &key);
if (res != SHISHI_OK)
return res;
res = shishi_asn1_to_der (handle, encapreppart, &der, &derlen);
if (res != SHISHI_OK)
{
shishi_error_printf (handle, "Could not DER encode authenticator: %s\n",
shishi_strerror (res));
return !SHISHI_OK;
}
der = xrealloc (der, derlen + 8);
while ((derlen % 8) != 0)
{
der[derlen] = '\0';
derlen++;
}
res = shishi_encrypt (handle, key, SHISHI_KEYUSAGE_ENCAPREPPART,
der, derlen, &buf, &buflen);
free (der);
if (res != SHISHI_OK)
{
shishi_error_printf (handle, "APRep encryption failed\n");
return res;
}
res = shishi_aprep_enc_part_set (handle, aprep, shishi_key_type (key),
buf, buflen);
free (buf);
return res;
}
int
shishi_aprep_enc_part_make (Shishi * handle,
Shishi_asn1 aprep,
Shishi_asn1 encapreppart,
Shishi_asn1 authenticator,
Shishi_asn1 encticketpart)
{
int res;
res = shishi_encapreppart_time_copy (handle, encapreppart, authenticator);
if (res != SHISHI_OK)
{
shishi_error_printf (handle, "Could not copy time: %s\n",
shishi_error (handle));
return res;
}
res = shishi_aprep_enc_part_add (handle, aprep, encticketpart,
encapreppart);
if (res != SHISHI_OK)
{
shishi_error_printf (handle, "Could not add encapreppart: %s\n",
shishi_error (handle));
return res;
}
return SHISHI_OK;
}
/**
* shishi_aprep_get_enc_part_etype:
* @handle: shishi handle as allocated by shishi_init().
* @aprep: AP-REP variable to get value from.
* @etype: output variable that holds the value.
*
* Extract AP-REP.enc-part.etype.
*
* Return value: Returns SHISHI_OK iff successful.
**/
int
shishi_aprep_get_enc_part_etype (Shishi * handle,
Shishi_asn1 aprep, int32_t * etype)
{
return shishi_asn1_read_int32 (handle, aprep, "enc-part.etype", etype);
}
int
shishi_aprep_decrypt (Shishi * handle,
Shishi_asn1 aprep,
Shishi_key * key,
int keyusage, Shishi_asn1 * encapreppart)
{
int res;
int i;
char *buf;
size_t buflen;
char *cipher;
size_t cipherlen;
int etype;
res = shishi_aprep_get_enc_part_etype (handle, aprep, &etype);
if (res != SHISHI_OK)
return res;
if (etype != shishi_key_type (key))
return SHISHI_APREP_BAD_KEYTYPE;
res = shishi_asn1_read (handle, aprep, "enc-part.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,
"APRep decryption failed, wrong password?\n");
return res;
}
/* 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);
*encapreppart = shishi_der2asn1_encapreppart (handle, &buf[0],
buflen - i);
if (*encapreppart != NULL)
break;
}
if (*encapreppart == NULL)
{
shishi_error_printf (handle, "Could not DER decode EncAPRepPart. "
"Password probably correct (decrypt ok) though\n");
return SHISHI_ASN1_ERROR;
}
return SHISHI_OK;
}
int
shishi_aprep_verify (Shishi * handle,
Shishi_asn1 authenticator, Shishi_asn1 encapreppart)
{
char *authenticatorctime;
char *encapreppartctime;
uint32_t authenticatorcusec, encapreppartcusec;
int res;
int different;
/*
3.2.5. Receipt of KRB_AP_REP message
If a KRB_AP_REP message is returned, the client uses the session key from
the credentials obtained for the server[3.10] to decrypt the message, and
verifies that the timestamp and microsecond fields match those in the
Authenticator it sent to the server. If they match, then the client is
assured that the server is genuine. The sequence number and subkey (if
present) are retained for later use.
*/
res = shishi_authenticator_ctime (handle, authenticator,
&authenticatorctime);
if (res != SHISHI_OK)
return res;
res = shishi_authenticator_cusec_get (handle, authenticator,
&authenticatorcusec);
if (res != SHISHI_OK)
return res;
res = shishi_encapreppart_ctime (handle, encapreppart, &encapreppartctime);
if (res != SHISHI_OK)
return res;
res = shishi_encapreppart_cusec_get (handle, encapreppart,
&encapreppartcusec);
if (res != SHISHI_OK)
return res;
if (VERBOSE (handle))
{
printf ("authenticator cusec %08x ctime %s\n", authenticatorcusec,
authenticatorctime);
printf ("encapreppart cusec %08x ctime %s\n", encapreppartcusec,
encapreppartctime);
}
different = authenticatorcusec != encapreppartcusec ||
strcmp (authenticatorctime, encapreppartctime) != 0;
free (authenticatorctime);
free (encapreppartctime);
if (different)
return SHISHI_APREP_VERIFY_FAILED;
return SHISHI_OK;
}
|