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
|
/* krb5/context.c --- Implementation of Kerberos 5 GSS Context functions.
* Copyright (C) 2003-2022 Simon Josefsson
*
* This file is part of the GNU Generic Security Service Library.
*
* This file is free software: you can redistribute it and/or modify
* it under the terms of either:
*
* * the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* or
*
* * the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* or both in parallel, as here.
*
* This file 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 copies of the GNU General Public License
* and the GNU Lesser General Public License along with this file. If
* not, see <http://www.gnu.org/licenses/>.
*
*/
/* Get specification. */
#include "k5internal.h"
/* Get checksum (un)packers. */
#include "checksum.h"
#define TOK_LEN 2
#define TOK_AP_REQ "\x01\x00"
#define TOK_AP_REP "\x02\x00"
/* Request part of gss_krb5_init_sec_context. Assumes that
context_handle is valid, and has krb5 specific structure, and that
output_token is valid and cleared. */
static OM_uint32
init_request (OM_uint32 * minor_status,
const gss_cred_id_t initiator_cred_handle,
gss_ctx_id_t * context_handle,
const gss_name_t target_name,
const gss_OID mech_type,
OM_uint32 req_flags,
OM_uint32 time_req,
const gss_channel_bindings_t input_chan_bindings,
const gss_buffer_t input_token,
gss_OID * actual_mech_type,
gss_buffer_t output_token,
OM_uint32 * ret_flags, OM_uint32 * time_rec)
{
gss_ctx_id_t ctx = *context_handle;
_gss_krb5_ctx_t k5 = ctx->krb5;
char *cksum, *der;
size_t cksumlen, derlen;
int rc;
OM_uint32 maj_stat;
Shishi_tkts_hint hint;
/* Get service ticket. */
maj_stat = gss_krb5_canonicalize_name (minor_status, target_name,
GSS_C_NO_OID, &k5->peerptr);
if (GSS_ERROR (maj_stat))
return maj_stat;
memset (&hint, 0, sizeof (hint));
hint.server = k5->peerptr->value;
hint.endtime = time_req;
k5->tkt = shishi_tkts_get (shishi_tkts_default (k5->sh), &hint);
if (!k5->tkt)
{
if (minor_status)
*minor_status = GSS_KRB5_S_KG_CCACHE_NOMATCH;
return GSS_S_NO_CRED;
}
/* Create Authenticator checksum field. */
maj_stat = _gss_krb5_checksum_pack (minor_status, initiator_cred_handle,
context_handle,
input_chan_bindings, req_flags,
&cksum, &cksumlen);
if (GSS_ERROR (maj_stat))
return maj_stat;
/* Create AP-REQ in output_token. */
rc = shishi_ap_tktoptionsraw (k5->sh, &k5->ap, k5->tkt,
SHISHI_APOPTIONS_MUTUAL_REQUIRED,
0x8003, cksum, cksumlen);
free (cksum);
if (rc != SHISHI_OK)
return GSS_S_FAILURE;
rc = shishi_authenticator_seqnumber_get (k5->sh,
shishi_ap_authenticator (k5->ap),
&k5->initseqnr);
if (rc != SHISHI_OK)
return GSS_S_FAILURE;
rc = shishi_ap_req_der (k5->ap, &der, &derlen);
if (rc != SHISHI_OK)
return GSS_S_FAILURE;
rc = _gss_encapsulate_token_prefix (TOK_AP_REQ, TOK_LEN,
der, derlen,
GSS_KRB5->elements,
GSS_KRB5->length,
&output_token->value,
&output_token->length);
free (der);
if (rc != 0)
return GSS_S_FAILURE;
if (req_flags & GSS_C_MUTUAL_FLAG)
return GSS_S_CONTINUE_NEEDED;
return GSS_S_COMPLETE;
}
/* Reply part of gss_krb5_init_sec_context. Assumes that
context_handle is valid, and has krb5 specific structure, and that
output_token is valid and cleared. */
static OM_uint32
init_reply (OM_uint32 * minor_status,
const gss_cred_id_t initiator_cred_handle,
gss_ctx_id_t * context_handle,
const gss_name_t target_name,
const gss_OID mech_type,
OM_uint32 req_flags,
OM_uint32 time_req,
const gss_channel_bindings_t input_chan_bindings,
const gss_buffer_t input_token,
gss_OID * actual_mech_type,
gss_buffer_t output_token,
OM_uint32 * ret_flags, OM_uint32 * time_rec)
{
gss_ctx_id_t ctx = *context_handle;
_gss_krb5_ctx_t k5 = ctx->krb5;
OM_uint32 tmp_min_stat;
gss_buffer_desc data;
int rc;
if (gss_decapsulate_token (input_token, GSS_KRB5, &data) != GSS_S_COMPLETE)
return GSS_S_DEFECTIVE_TOKEN;
if (data.length < TOK_LEN)
{
gss_release_buffer (&tmp_min_stat, &data);
return GSS_S_DEFECTIVE_TOKEN;
}
if (memcmp (data.value, TOK_AP_REP, TOK_LEN) != 0)
{
gss_release_buffer (&tmp_min_stat, &data);
return GSS_S_DEFECTIVE_TOKEN;
}
rc = shishi_ap_rep_der_set (k5->ap, (char *) data.value + TOK_LEN,
data.length - TOK_LEN);
gss_release_buffer (&tmp_min_stat, &data);
if (rc != SHISHI_OK)
return GSS_S_DEFECTIVE_TOKEN;
rc = shishi_ap_rep_verify (k5->ap);
if (rc != SHISHI_OK)
return GSS_S_DEFECTIVE_TOKEN;
rc = shishi_encapreppart_seqnumber_get (k5->sh,
shishi_ap_encapreppart (k5->ap),
&k5->acceptseqnr);
if (rc != SHISHI_OK)
{
/* A strict 1964 implementation would return
GSS_S_DEFECTIVE_TOKEN here. gssapi-cfx permit absent
sequence number, though. */
k5->acceptseqnr = 0;
}
return GSS_S_COMPLETE;
}
/* Initiates the establishment of a krb5 security context between the
application and a remote peer. Assumes that context_handle and
output_token are valid and cleared. */
OM_uint32
gss_krb5_init_sec_context (OM_uint32 * minor_status,
const gss_cred_id_t initiator_cred_handle,
gss_ctx_id_t * context_handle,
const gss_name_t target_name,
const gss_OID mech_type,
OM_uint32 req_flags,
OM_uint32 time_req,
const gss_channel_bindings_t input_chan_bindings,
const gss_buffer_t input_token,
gss_OID * actual_mech_type,
gss_buffer_t output_token,
OM_uint32 * ret_flags, OM_uint32 * time_rec)
{
gss_ctx_id_t ctx = *context_handle;
_gss_krb5_ctx_t k5 = ctx->krb5;
OM_uint32 maj_stat;
int rc;
if (minor_status)
*minor_status = 0;
if (initiator_cred_handle)
{
/* We only support the default initiator. See k5internal.h for
adding a Shishi_tkt to the credential structure. I'm not sure
what the use would be -- user-to-user authentication perhaps?
Later: if you have tickets for foo@BAR and bar@FOO, it may be
useful to call gss_acquire_cred first to chose which one to
initiate the context with. Not many applications need this. */
return GSS_S_NO_CRED;
}
if (target_name == NULL)
{
return GSS_S_BAD_NAME;
}
if (k5 == NULL)
{
k5 = ctx->krb5 = calloc (sizeof (*k5), 1);
if (!k5)
{
if (minor_status)
*minor_status = ENOMEM;
return GSS_S_FAILURE;
}
rc = shishi_init (&k5->sh);
if (rc != SHISHI_OK)
return GSS_S_FAILURE;
}
if (!k5->reqdone)
{
maj_stat = init_request (minor_status,
initiator_cred_handle,
context_handle,
target_name,
mech_type,
req_flags,
time_req,
input_chan_bindings,
input_token,
actual_mech_type,
output_token, ret_flags, time_rec);
if (GSS_ERROR (maj_stat))
return maj_stat;
k5->flags = req_flags & ( /* GSS_C_DELEG_FLAG | */
GSS_C_MUTUAL_FLAG |
GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG |
GSS_C_CONF_FLAG | GSS_C_INTEG_FLAG);
/* PROT_READY is not mentioned in 1964/gssapi-cfx but we support
it anyway. */
k5->flags |= GSS_C_PROT_READY_FLAG;
if (ret_flags)
*ret_flags = k5->flags;
k5->key = shishi_ap_key (k5->ap);
k5->reqdone = 1;
}
else if (k5->reqdone && k5->flags & GSS_C_MUTUAL_FLAG && !k5->repdone)
{
maj_stat = init_reply (minor_status,
initiator_cred_handle,
context_handle,
target_name,
mech_type,
req_flags,
time_req,
input_chan_bindings,
input_token,
actual_mech_type,
output_token, ret_flags, time_rec);
if (GSS_ERROR (maj_stat))
return maj_stat;
if (ret_flags)
*ret_flags = k5->flags;
k5->repdone = 1;
}
else
maj_stat = GSS_S_FAILURE;
if (time_rec)
*time_rec = gss_krb5_tktlifetime (k5->tkt);
return maj_stat;
}
/* Allows a remotely initiated security context between the
application and a remote peer to be established, using krb5.
Assumes context_handle is valid. */
OM_uint32
gss_krb5_accept_sec_context (OM_uint32 * minor_status,
gss_ctx_id_t * context_handle,
const gss_cred_id_t acceptor_cred_handle,
const gss_buffer_t input_token_buffer,
const gss_channel_bindings_t input_chan_bindings,
gss_name_t * src_name,
gss_OID * mech_type,
gss_buffer_t output_token,
OM_uint32 * ret_flags,
OM_uint32 * time_rec,
gss_cred_id_t * delegated_cred_handle)
{
gss_buffer_desc in;
gss_ctx_id_t cx;
_gss_krb5_ctx_t cxk5;
_gss_krb5_cred_t crk5;
OM_uint32 tmp_min_stat;
int rc;
if (minor_status)
*minor_status = 0;
if (ret_flags)
*ret_flags = 0;
if (!acceptor_cred_handle)
/* XXX support GSS_C_NO_CREDENTIAL: acquire_cred() default server */
return GSS_S_NO_CRED;
if (*context_handle)
return GSS_S_FAILURE;
crk5 = acceptor_cred_handle->krb5;
cx = calloc (sizeof (*cx), 1);
if (!cx)
{
if (minor_status)
*minor_status = ENOMEM;
return GSS_S_FAILURE;
}
cxk5 = calloc (sizeof (*cxk5), 1);
if (!cxk5)
{
free (cx);
if (minor_status)
*minor_status = ENOMEM;
return GSS_S_FAILURE;
}
cx->mech = GSS_KRB5;
cx->krb5 = cxk5;
/* XXX cx->peer?? */
*context_handle = cx;
cxk5->sh = crk5->sh;
cxk5->key = crk5->key;
cxk5->acceptor = 1;
rc = shishi_ap (cxk5->sh, &cxk5->ap);
if (rc != SHISHI_OK)
return GSS_S_FAILURE;
rc = gss_decapsulate_token (input_token_buffer, GSS_KRB5, &in);
if (rc != GSS_S_COMPLETE)
return GSS_S_BAD_MIC;
if (in.length < TOK_LEN)
{
gss_release_buffer (&tmp_min_stat, &in);
return GSS_S_BAD_MIC;
}
if (memcmp (in.value, TOK_AP_REQ, TOK_LEN) != 0)
{
gss_release_buffer (&tmp_min_stat, &in);
return GSS_S_BAD_MIC;
}
rc = shishi_ap_req_der_set (cxk5->ap, (char *) in.value + TOK_LEN,
in.length - TOK_LEN);
gss_release_buffer (&tmp_min_stat, &in);
if (rc != SHISHI_OK)
return GSS_S_FAILURE;
rc = shishi_ap_req_process (cxk5->ap, crk5->key);
if (rc != SHISHI_OK)
{
if (minor_status)
*minor_status = GSS_KRB5_S_G_VALIDATE_FAILED;
return GSS_S_FAILURE;
}
rc = shishi_authenticator_seqnumber_get (cxk5->sh,
shishi_ap_authenticator (cxk5->ap),
&cxk5->initseqnr);
if (rc != SHISHI_OK)
return GSS_S_FAILURE;
rc = _gss_krb5_checksum_parse (minor_status,
context_handle, input_chan_bindings);
if (rc != GSS_S_COMPLETE)
return GSS_S_FAILURE;
cxk5->tkt = shishi_ap_tkt (cxk5->ap);
cxk5->key = shishi_ap_key (cxk5->ap);
if (shishi_apreq_mutual_required_p (crk5->sh, shishi_ap_req (cxk5->ap)))
{
Shishi_asn1 aprep;
char *der;
size_t len;
rc = shishi_ap_rep_asn1 (cxk5->ap, &aprep);
if (rc != SHISHI_OK)
{
printf ("Error creating AP-REP: %s\n", shishi_strerror (rc));
return GSS_S_FAILURE;
}
rc = shishi_encapreppart_seqnumber_get (cxk5->sh,
shishi_ap_encapreppart
(cxk5->ap), &cxk5->acceptseqnr);
if (rc != SHISHI_OK)
{
/* A strict 1964 implementation would return
GSS_S_DEFECTIVE_TOKEN here. gssapi-cfx permit absent
sequence number, though. */
cxk5->acceptseqnr = 0;
}
rc = shishi_asn1_to_der (crk5->sh, aprep, &der, &len);
if (rc != SHISHI_OK)
{
printf ("Error der encoding aprep: %s\n", shishi_strerror (rc));
return GSS_S_FAILURE;
}
rc = _gss_encapsulate_token_prefix (TOK_AP_REP, TOK_LEN,
der, len,
GSS_KRB5->elements,
GSS_KRB5->length,
&output_token->value,
&output_token->length);
if (rc != 0)
return GSS_S_FAILURE;
if (ret_flags)
*ret_flags = GSS_C_MUTUAL_FLAG;
}
else
{
output_token->value = NULL;
output_token->length = 0;
}
if (src_name)
{
gss_name_t p;
p = malloc (sizeof (*p));
if (!p)
{
if (minor_status)
*minor_status = ENOMEM;
return GSS_S_FAILURE;
}
rc = shishi_encticketpart_client (cxk5->sh,
shishi_tkt_encticketpart (cxk5->tkt),
&p->value, &p->length);
if (rc != SHISHI_OK)
return GSS_S_FAILURE;
p->type = GSS_KRB5_NT_PRINCIPAL_NAME;
*src_name = p;
}
/* PROT_READY is not mentioned in 1964/gssapi-cfx but we support
it anyway. */
if (ret_flags)
*ret_flags |= GSS_C_PROT_READY_FLAG;
if (minor_status)
*minor_status = 0;
return GSS_S_COMPLETE;
}
/* Delete a krb5 security context. Assumes context_handle is valid.
Should only delete krb5 specific part of context. */
OM_uint32
gss_krb5_delete_sec_context (OM_uint32 * minor_status,
gss_ctx_id_t * context_handle,
gss_buffer_t output_token)
{
_gss_krb5_ctx_t k5 = (*context_handle)->krb5;
if (k5->peerptr != GSS_C_NO_NAME)
gss_release_name (NULL, &k5->peerptr);
if (k5->ap)
shishi_ap_done (k5->ap);
if (!k5->acceptor)
shishi_done (k5->sh);
free (k5);
if (minor_status)
*minor_status = 0;
return GSS_S_COMPLETE;
}
/* Determines the number of seconds for which the specified krb5
context will remain valid. Assumes context_handle is valid. */
OM_uint32
gss_krb5_context_time (OM_uint32 * minor_status,
const gss_ctx_id_t context_handle,
OM_uint32 * time_rec)
{
_gss_krb5_ctx_t k5 = context_handle->krb5;
if (time_rec)
{
*time_rec = gss_krb5_tktlifetime (k5->tkt);
if (*time_rec == 0)
{
if (minor_status)
*minor_status = 0;
return GSS_S_CONTEXT_EXPIRED;
}
}
if (minor_status)
*minor_status = 0;
return GSS_S_COMPLETE;
}
|