| 1 |
/* |
| 2 |
* Copyright (c) 2003, PADL Software Pty Ltd. |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* |
| 9 |
* 1. Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* |
| 12 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
* documentation and/or other materials provided with the distribution. |
| 15 |
* |
| 16 |
* 3. Neither the name of PADL Software nor the names of its contributors |
| 17 |
* may be used to endorse or promote products derived from this software |
| 18 |
* without specific prior written permission. |
| 19 |
* |
| 20 |
* THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND |
| 21 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 |
* ARE DISCLAIMED. IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE |
| 24 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 26 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 29 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 30 |
* SUCH DAMAGE. |
| 31 |
*/ |
| 32 |
|
| 33 |
#include "gsskrb5_locl.h" |
| 34 |
|
| 35 |
/* |
| 36 |
* Implementation of RFC 4121 |
| 37 |
*/ |
| 38 |
|
| 39 |
#define CFXSentByAcceptor (1 << 0) |
| 40 |
#define CFXSealed (1 << 1) |
| 41 |
#define CFXAcceptorSubkey (1 << 2) |
| 42 |
|
| 43 |
krb5_error_code |
| 44 |
_gsskrb5cfx_wrap_length_cfx(krb5_context context, |
| 45 |
krb5_crypto crypto, |
| 46 |
int conf_req_flag, |
| 47 |
int dce_style, |
| 48 |
size_t input_length, |
| 49 |
size_t *output_length, |
| 50 |
size_t *cksumsize, |
| 51 |
uint16_t *padlength) |
| 52 |
{ |
| 53 |
krb5_error_code ret; |
| 54 |
krb5_cksumtype type; |
| 55 |
|
| 56 |
/* 16-byte header is always first */ |
| 57 |
*output_length = sizeof(gss_cfx_wrap_token_desc); |
| 58 |
*padlength = 0; |
| 59 |
|
| 60 |
ret = krb5_crypto_get_checksum_type(context, crypto, &type); |
| 61 |
if (ret) |
| 62 |
return ret; |
| 63 |
|
| 64 |
ret = krb5_checksumsize(context, type, cksumsize); |
| 65 |
if (ret) |
| 66 |
return ret; |
| 67 |
|
| 68 |
if (conf_req_flag) { |
| 69 |
size_t padsize; |
| 70 |
|
| 71 |
/* Header is concatenated with data before encryption */ |
| 72 |
input_length += sizeof(gss_cfx_wrap_token_desc); |
| 73 |
|
| 74 |
if (dce_style) { |
| 75 |
ret = krb5_crypto_getblocksize(context, crypto, &padsize); |
| 76 |
} else { |
| 77 |
ret = krb5_crypto_getpadsize(context, crypto, &padsize); |
| 78 |
} |
| 79 |
if (ret) { |
| 80 |
return ret; |
| 81 |
} |
| 82 |
if (padsize > 1) { |
| 83 |
/* XXX check this */ |
| 84 |
*padlength = padsize - (input_length % padsize); |
| 85 |
|
| 86 |
/* We add the pad ourselves (noted here for completeness only) */ |
| 87 |
input_length += *padlength; |
| 88 |
} |
| 89 |
|
| 90 |
*output_length += krb5_get_wrapped_length(context, |
| 91 |
crypto, input_length); |
| 92 |
} else { |
| 93 |
/* Checksum is concatenated with data */ |
| 94 |
*output_length += input_length + *cksumsize; |
| 95 |
} |
| 96 |
|
| 97 |
assert(*output_length > input_length); |
| 98 |
|
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
OM_uint32 |
| 103 |
_gssapi_wrap_size_cfx(OM_uint32 *minor_status, |
| 104 |
const gsskrb5_ctx ctx, |
| 105 |
krb5_context context, |
| 106 |
int conf_req_flag, |
| 107 |
gss_qop_t qop_req, |
| 108 |
OM_uint32 req_output_size, |
| 109 |
OM_uint32 *max_input_size) |
| 110 |
{ |
| 111 |
krb5_error_code ret; |
| 112 |
|
| 113 |
*max_input_size = 0; |
| 114 |
|
| 115 |
/* 16-byte header is always first */ |
| 116 |
if (req_output_size < 16) |
| 117 |
return 0; |
| 118 |
req_output_size -= 16; |
| 119 |
|
| 120 |
if (conf_req_flag) { |
| 121 |
size_t wrapped_size, sz; |
| 122 |
|
| 123 |
wrapped_size = req_output_size + 1; |
| 124 |
do { |
| 125 |
wrapped_size--; |
| 126 |
sz = krb5_get_wrapped_length(context, |
| 127 |
ctx->crypto, wrapped_size); |
| 128 |
} while (wrapped_size && sz > req_output_size); |
| 129 |
if (wrapped_size == 0) |
| 130 |
return 0; |
| 131 |
|
| 132 |
/* inner header */ |
| 133 |
if (wrapped_size < 16) |
| 134 |
return 0; |
| 135 |
|
| 136 |
wrapped_size -= 16; |
| 137 |
|
| 138 |
*max_input_size = wrapped_size; |
| 139 |
} else { |
| 140 |
krb5_cksumtype type; |
| 141 |
size_t cksumsize; |
| 142 |
|
| 143 |
ret = krb5_crypto_get_checksum_type(context, ctx->crypto, &type); |
| 144 |
if (ret) |
| 145 |
return ret; |
| 146 |
|
| 147 |
ret = krb5_checksumsize(context, type, &cksumsize); |
| 148 |
if (ret) |
| 149 |
return ret; |
| 150 |
|
| 151 |
if (req_output_size < cksumsize) |
| 152 |
return 0; |
| 153 |
|
| 154 |
/* Checksum is concatenated with data */ |
| 155 |
*max_input_size = req_output_size - cksumsize; |
| 156 |
} |
| 157 |
|
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
/* |
| 162 |
* Rotate "rrc" bytes to the front or back |
| 163 |
*/ |
| 164 |
|
| 165 |
static krb5_error_code |
| 166 |
rrc_rotate(void *data, size_t len, uint16_t rrc, krb5_boolean unrotate) |
| 167 |
{ |
| 168 |
u_char *tmp, buf[256]; |
| 169 |
size_t left; |
| 170 |
|
| 171 |
if (len == 0) |
| 172 |
return 0; |
| 173 |
|
| 174 |
rrc %= len; |
| 175 |
|
| 176 |
if (rrc == 0) |
| 177 |
return 0; |
| 178 |
|
| 179 |
left = len - rrc; |
| 180 |
|
| 181 |
if (rrc <= sizeof(buf)) { |
| 182 |
tmp = buf; |
| 183 |
} else { |
| 184 |
tmp = malloc(rrc); |
| 185 |
if (tmp == NULL) |
| 186 |
return ENOMEM; |
| 187 |
} |
| 188 |
|
| 189 |
if (unrotate) { |
| 190 |
memcpy(tmp, data, rrc); |
| 191 |
memmove(data, (u_char *)data + rrc, left); |
| 192 |
memcpy((u_char *)data + left, tmp, rrc); |
| 193 |
} else { |
| 194 |
memcpy(tmp, (u_char *)data + left, rrc); |
| 195 |
memmove((u_char *)data + rrc, data, left); |
| 196 |
memcpy(data, tmp, rrc); |
| 197 |
} |
| 198 |
|
| 199 |
if (rrc > sizeof(buf)) |
| 200 |
free(tmp); |
| 201 |
|
| 202 |
return 0; |
| 203 |
} |
| 204 |
|
| 205 |
gss_iov_buffer_desc * |
| 206 |
_gk_find_buffer(gss_iov_buffer_desc *iov, int iov_count, OM_uint32 type) |
| 207 |
{ |
| 208 |
int i; |
| 209 |
gss_iov_buffer_t iovp = GSS_C_NO_IOV_BUFFER; |
| 210 |
|
| 211 |
if (iov == GSS_C_NO_IOV_BUFFER) |
| 212 |
return GSS_C_NO_IOV_BUFFER; |
| 213 |
|
| 214 |
/* |
| 215 |
* This function is used to find header, padding or trailer buffers |
| 216 |
* which are singletons; return NULL if multiple instances are found. |
| 217 |
*/ |
| 218 |
for (i = 0; i < iov_count; i++) { |
| 219 |
if (type == GSS_IOV_BUFFER_TYPE(iov[i].type)) { |
| 220 |
if (iovp == GSS_C_NO_IOV_BUFFER) |
| 221 |
iovp = &iov[i]; |
| 222 |
else |
| 223 |
return GSS_C_NO_IOV_BUFFER; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
/* |
| 228 |
* For compatibility with SSPI, an empty padding buffer is treated |
| 229 |
* equivalent to an absent padding buffer (unless the caller is |
| 230 |
* requesting that a padding buffer be allocated). |
| 231 |
*/ |
| 232 |
if (iovp && |
| 233 |
iovp->buffer.length == 0 && |
| 234 |
type == GSS_IOV_BUFFER_TYPE_PADDING && |
| 235 |
(GSS_IOV_BUFFER_FLAGS(iovp->type) & GSS_IOV_BUFFER_FLAG_ALLOCATE) == 0) |
| 236 |
iovp = NULL; |
| 237 |
|
| 238 |
return iovp; |
| 239 |
} |
| 240 |
|
| 241 |
OM_uint32 |
| 242 |
_gk_allocate_buffer(OM_uint32 *minor_status, gss_iov_buffer_desc *buffer, size_t size) |
| 243 |
{ |
| 244 |
if (buffer->type & GSS_IOV_BUFFER_FLAG_ALLOCATED) { |
| 245 |
if (buffer->buffer.length == size) |
| 246 |
return GSS_S_COMPLETE; |
| 247 |
free(buffer->buffer.value); |
| 248 |
} |
| 249 |
|
| 250 |
buffer->buffer.value = malloc(size); |
| 251 |
buffer->buffer.length = size; |
| 252 |
if (buffer->buffer.value == NULL) { |
| 253 |
*minor_status = ENOMEM; |
| 254 |
return GSS_S_FAILURE; |
| 255 |
} |
| 256 |
buffer->type |= GSS_IOV_BUFFER_FLAG_ALLOCATED; |
| 257 |
|
| 258 |
return GSS_S_COMPLETE; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
OM_uint32 |
| 263 |
_gk_verify_buffers(OM_uint32 *minor_status, |
| 264 |
const gsskrb5_ctx ctx, |
| 265 |
const gss_iov_buffer_desc *header, |
| 266 |
const gss_iov_buffer_desc *padding, |
| 267 |
const gss_iov_buffer_desc *trailer, |
| 268 |
int block_cipher) |
| 269 |
{ |
| 270 |
if (header == NULL) { |
| 271 |
*minor_status = EINVAL; |
| 272 |
return GSS_S_FAILURE; |
| 273 |
} |
| 274 |
|
| 275 |
if (IS_DCE_STYLE(ctx)) { |
| 276 |
/* |
| 277 |
* In DCE style mode we reject having a padding or trailer buffer |
| 278 |
*/ |
| 279 |
if (padding) { |
| 280 |
*minor_status = EINVAL; |
| 281 |
return GSS_S_FAILURE; |
| 282 |
} |
| 283 |
if (trailer) { |
| 284 |
*minor_status = EINVAL; |
| 285 |
return GSS_S_FAILURE; |
| 286 |
} |
| 287 |
} else { |
| 288 |
/* |
| 289 |
* In non-DCE style mode we require having a padding buffer for |
| 290 |
* encryption types that do not behave as stream ciphers. This |
| 291 |
* check is superfluous for now, as only RC4 and RFC4121 enctypes |
| 292 |
* are presently implemented for the IOV APIs; be defensive. |
| 293 |
*/ |
| 294 |
if (block_cipher && padding == NULL) { |
| 295 |
*minor_status = EINVAL; |
| 296 |
return GSS_S_FAILURE; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
*minor_status = 0; |
| 301 |
return GSS_S_COMPLETE; |
| 302 |
} |
| 303 |
|
| 304 |
OM_uint32 |
| 305 |
_gssapi_wrap_cfx_iov(OM_uint32 *minor_status, |
| 306 |
gsskrb5_ctx ctx, |
| 307 |
krb5_context context, |
| 308 |
int conf_req_flag, |
| 309 |
int *conf_state, |
| 310 |
gss_iov_buffer_desc *iov, |
| 311 |
int iov_count) |
| 312 |
{ |
| 313 |
OM_uint32 major_status, junk; |
| 314 |
gss_iov_buffer_desc *header, *trailer, *padding; |
| 315 |
size_t gsshsize, k5hsize; |
| 316 |
size_t gsstsize, k5tsize; |
| 317 |
size_t rrc = 0, ec = 0; |
| 318 |
int i; |
| 319 |
gss_cfx_wrap_token token; |
| 320 |
krb5_error_code ret; |
| 321 |
int32_t seq_number; |
| 322 |
unsigned usage; |
| 323 |
krb5_crypto_iov *data = NULL; |
| 324 |
|
| 325 |
header = _gk_find_buffer(iov, iov_count, GSS_IOV_BUFFER_TYPE_HEADER); |
| 326 |
if (header == NULL) { |
| 327 |
*minor_status = EINVAL; |
| 328 |
return GSS_S_FAILURE; |
| 329 |
} |
| 330 |
|
| 331 |
padding = _gk_find_buffer(iov, iov_count, GSS_IOV_BUFFER_TYPE_PADDING); |
| 332 |
if (padding != NULL) { |
| 333 |
padding->buffer.length = 0; |
| 334 |
} |
| 335 |
|
| 336 |
trailer = _gk_find_buffer(iov, iov_count, GSS_IOV_BUFFER_TYPE_TRAILER); |
| 337 |
|
| 338 |
major_status = _gk_verify_buffers(minor_status, ctx, header, |
| 339 |
padding, trailer, FALSE); |
| 340 |
if (major_status != GSS_S_COMPLETE) { |
| 341 |
return major_status; |
| 342 |
} |
| 343 |
|
| 344 |
if (conf_req_flag) { |
| 345 |
size_t k5psize = 0; |
| 346 |
size_t k5pbase = 0; |
| 347 |
size_t k5bsize = 0; |
| 348 |
size_t size = 0; |
| 349 |
|
| 350 |
for (i = 0; i < iov_count; i++) { |
| 351 |
switch (GSS_IOV_BUFFER_TYPE(iov[i].type)) { |
| 352 |
case GSS_IOV_BUFFER_TYPE_DATA: |
| 353 |
size += iov[i].buffer.length; |
| 354 |
break; |
| 355 |
default: |
| 356 |
break; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
size += sizeof(gss_cfx_wrap_token_desc); |
| 361 |
|
| 362 |
*minor_status = krb5_crypto_length(context, ctx->crypto, |
| 363 |
KRB5_CRYPTO_TYPE_HEADER, |
| 364 |
&k5hsize); |
| 365 |
if (*minor_status) |
| 366 |
return GSS_S_FAILURE; |
| 367 |
|
| 368 |
*minor_status = krb5_crypto_length(context, ctx->crypto, |
| 369 |
KRB5_CRYPTO_TYPE_TRAILER, |
| 370 |
&k5tsize); |
| 371 |
if (*minor_status) |
| 372 |
return GSS_S_FAILURE; |
| 373 |
|
| 374 |
*minor_status = krb5_crypto_length(context, ctx->crypto, |
| 375 |
KRB5_CRYPTO_TYPE_PADDING, |
| 376 |
&k5pbase); |
| 377 |
if (*minor_status) |
| 378 |
return GSS_S_FAILURE; |
| 379 |
|
| 380 |
if (k5pbase > 1) { |
| 381 |
k5psize = k5pbase - (size % k5pbase); |
| 382 |
} else { |
| 383 |
k5psize = 0; |
| 384 |
} |
| 385 |
|
| 386 |
if (k5psize == 0 && IS_DCE_STYLE(ctx)) { |
| 387 |
*minor_status = krb5_crypto_getblocksize(context, ctx->crypto, |
| 388 |
&k5bsize); |
| 389 |
if (*minor_status) |
| 390 |
return GSS_S_FAILURE; |
| 391 |
ec = k5bsize; |
| 392 |
} else { |
| 393 |
ec = k5psize; |
| 394 |
} |
| 395 |
|
| 396 |
gsshsize = sizeof(gss_cfx_wrap_token_desc) + k5hsize; |
| 397 |
gsstsize = sizeof(gss_cfx_wrap_token_desc) + ec + k5tsize; |
| 398 |
} else { |
| 399 |
if (IS_DCE_STYLE(ctx)) { |
| 400 |
*minor_status = EINVAL; |
| 401 |
return GSS_S_FAILURE; |
| 402 |
} |
| 403 |
|
| 404 |
k5hsize = 0; |
| 405 |
*minor_status = krb5_crypto_length(context, ctx->crypto, |
| 406 |
KRB5_CRYPTO_TYPE_CHECKSUM, |
| 407 |
&k5tsize); |
| 408 |
if (*minor_status) |
| 409 |
return GSS_S_FAILURE; |
| 410 |
|
| 411 |
gsshsize = sizeof(gss_cfx_wrap_token_desc); |
| 412 |
gsstsize = k5tsize; |
| 413 |
} |
| 414 |
|
| 415 |
/* |
| 416 |
* |
| 417 |
*/ |
| 418 |
|
| 419 |
if (trailer == NULL) { |
| 420 |
rrc = gsstsize; |
| 421 |
if (IS_DCE_STYLE(ctx)) |
| 422 |
rrc -= ec; |
| 423 |
gsshsize += gsstsize; |
| 424 |
} else if (GSS_IOV_BUFFER_FLAGS(trailer->type) & GSS_IOV_BUFFER_FLAG_ALLOCATE) { |
| 425 |
major_status = _gk_allocate_buffer(minor_status, trailer, gsstsize); |
| 426 |
if (major_status) |
| 427 |
goto failure; |
| 428 |
} else if (trailer->buffer.length < gsstsize) { |
| 429 |
*minor_status = KRB5_BAD_MSIZE; |
| 430 |
major_status = GSS_S_FAILURE; |
| 431 |
goto failure; |
| 432 |
} else |
| 433 |
trailer->buffer.length = gsstsize; |
| 434 |
|
| 435 |
/* |
| 436 |
* |
| 437 |
*/ |
| 438 |
|
| 439 |
if (GSS_IOV_BUFFER_FLAGS(header->type) & GSS_IOV_BUFFER_FLAG_ALLOCATE) { |
| 440 |
major_status = _gk_allocate_buffer(minor_status, header, gsshsize); |
| 441 |
if (major_status != GSS_S_COMPLETE) |
| 442 |
goto failure; |
| 443 |
} else if (header->buffer.length < gsshsize) { |
| 444 |
*minor_status = KRB5_BAD_MSIZE; |
| 445 |
major_status = GSS_S_FAILURE; |
| 446 |
goto failure; |
| 447 |
} else |
| 448 |
header->buffer.length = gsshsize; |
| 449 |
|
| 450 |
token = (gss_cfx_wrap_token)header->buffer.value; |
| 451 |
|
| 452 |
token->TOK_ID[0] = 0x05; |
| 453 |
token->TOK_ID[1] = 0x04; |
| 454 |
token->Flags = 0; |
| 455 |
token->Filler = 0xFF; |
| 456 |
|
| 457 |
if ((ctx->more_flags & LOCAL) == 0) |
| 458 |
token->Flags |= CFXSentByAcceptor; |
| 459 |
|
| 460 |
if (ctx->more_flags & ACCEPTOR_SUBKEY) |
| 461 |
token->Flags |= CFXAcceptorSubkey; |
| 462 |
|
| 463 |
if (ctx->more_flags & LOCAL) |
| 464 |
usage = KRB5_KU_USAGE_INITIATOR_SEAL; |
| 465 |
else |
| 466 |
usage = KRB5_KU_USAGE_ACCEPTOR_SEAL; |
| 467 |
|
| 468 |
if (conf_req_flag) { |
| 469 |
/* |
| 470 |
* In Wrap tokens with confidentiality, the EC field is |
| 471 |
* used to encode the size (in bytes) of the random filler. |
| 472 |
*/ |
| 473 |
token->Flags |= CFXSealed; |
| 474 |
token->EC[0] = (ec >> 8) & 0xFF; |
| 475 |
token->EC[1] = (ec >> 0) & 0xFF; |
| 476 |
|
| 477 |
} else { |
| 478 |
/* |
| 479 |
* In Wrap tokens without confidentiality, the EC field is |
| 480 |
* used to encode the size (in bytes) of the trailing |
| 481 |
* checksum. |
| 482 |
* |
| 483 |
* This is not used in the checksum calcuation itself, |
| 484 |
* because the checksum length could potentially vary |
| 485 |
* depending on the data length. |
| 486 |
*/ |
| 487 |
token->EC[0] = 0; |
| 488 |
token->EC[1] = 0; |
| 489 |
} |
| 490 |
|
| 491 |
/* |
| 492 |
* In Wrap tokens that provide for confidentiality, the RRC |
| 493 |
* field in the header contains the hex value 00 00 before |
| 494 |
* encryption. |
| 495 |
* |
| 496 |
* In Wrap tokens that do not provide for confidentiality, |
| 497 |
* both the EC and RRC fields in the appended checksum |
| 498 |
* contain the hex value 00 00 for the purpose of calculating |
| 499 |
* the checksum. |
| 500 |
*/ |
| 501 |
token->RRC[0] = 0; |
| 502 |
token->RRC[1] = 0; |
| 503 |
|
| 504 |
HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex); |
| 505 |
krb5_auth_con_getlocalseqnumber(context, |
| 506 |
ctx->auth_context, |
| 507 |
&seq_number); |
| 508 |
_gsskrb5_encode_be_om_uint32(0, &token->SND_SEQ[0]); |
| 509 |
_gsskrb5_encode_be_om_uint32(seq_number, &token->SND_SEQ[4]); |
| 510 |
krb5_auth_con_setlocalseqnumber(context, |
| 511 |
ctx->auth_context, |
| 512 |
++seq_number); |
| 513 |
HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex); |
| 514 |
|
| 515 |
data = calloc(iov_count + 3, sizeof(data[0])); |
| 516 |
if (data == NULL) { |
| 517 |
*minor_status = ENOMEM; |
| 518 |
major_status = GSS_S_FAILURE; |
| 519 |
goto failure; |
| 520 |
} |
| 521 |
|
| 522 |
if (conf_req_flag) { |
| 523 |
/* |
| 524 |
plain packet: |
| 525 |
|
| 526 |
{"header" | encrypt(plaintext-data | ec-padding | E"header")} |
| 527 |
|
| 528 |
Expanded, this is with with RRC = 0: |
| 529 |
|
| 530 |
{"header" | krb5-header | plaintext-data | ec-padding | E"header" | krb5-trailer } |
| 531 |
|
| 532 |
In DCE-RPC mode == no trailer: RRC = gss "trailer" == length(ec-padding | E"header" | krb5-trailer) |
| 533 |
|
| 534 |
{"header" | ec-padding | E"header" | krb5-trailer | krb5-header | plaintext-data } |
| 535 |
*/ |
| 536 |
|
| 537 |
i = 0; |
| 538 |
data[i].flags = KRB5_CRYPTO_TYPE_HEADER; |
| 539 |
data[i].data.data = ((uint8_t *)header->buffer.value) + header->buffer.length - k5hsize; |
| 540 |
data[i].data.length = k5hsize; |
| 541 |
|
| 542 |
for (i = 1; i < iov_count + 1; i++) { |
| 543 |
switch (GSS_IOV_BUFFER_TYPE(iov[i - 1].type)) { |
| 544 |
case GSS_IOV_BUFFER_TYPE_DATA: |
| 545 |
data[i].flags = KRB5_CRYPTO_TYPE_DATA; |
| 546 |
break; |
| 547 |
case GSS_IOV_BUFFER_TYPE_SIGN_ONLY: |
| 548 |
data[i].flags = KRB5_CRYPTO_TYPE_SIGN_ONLY; |
| 549 |
break; |
| 550 |
default: |
| 551 |
data[i].flags = KRB5_CRYPTO_TYPE_EMPTY; |
| 552 |
break; |
| 553 |
} |
| 554 |
data[i].data.length = iov[i - 1].buffer.length; |
| 555 |
data[i].data.data = iov[i - 1].buffer.value; |
| 556 |
} |
| 557 |
|
| 558 |
/* |
| 559 |
* Any necessary padding is added here to ensure that the |
| 560 |
* encrypted token header is always at the end of the |
| 561 |
* ciphertext. |
| 562 |
*/ |
| 563 |
|
| 564 |
/* encrypted CFX header in trailer (or after the header if in |
| 565 |
DCE mode). Copy in header into E"header" |
| 566 |
*/ |
| 567 |
data[i].flags = KRB5_CRYPTO_TYPE_DATA; |
| 568 |
if (trailer) |
| 569 |
data[i].data.data = trailer->buffer.value; |
| 570 |
else |
| 571 |
data[i].data.data = ((uint8_t *)header->buffer.value) + sizeof(*token); |
| 572 |
|
| 573 |
data[i].data.length = ec + sizeof(*token); |
| 574 |
memset(data[i].data.data, 0xFF, ec); |
| 575 |
memcpy(((uint8_t *)data[i].data.data) + ec, token, sizeof(*token)); |
| 576 |
i++; |
| 577 |
|
| 578 |
/* Kerberos trailer comes after the gss trailer */ |
| 579 |
data[i].flags = KRB5_CRYPTO_TYPE_TRAILER; |
| 580 |
data[i].data.data = ((uint8_t *)data[i-1].data.data) + ec + sizeof(*token); |
| 581 |
data[i].data.length = k5tsize; |
| 582 |
i++; |
| 583 |
|
| 584 |
ret = krb5_encrypt_iov_ivec(context, ctx->crypto, usage, data, i, NULL); |
| 585 |
if (ret != 0) { |
| 586 |
*minor_status = ret; |
| 587 |
major_status = GSS_S_FAILURE; |
| 588 |
goto failure; |
| 589 |
} |
| 590 |
|
| 591 |
if (rrc) { |
| 592 |
token->RRC[0] = (rrc >> 8) & 0xFF; |
| 593 |
token->RRC[1] = (rrc >> 0) & 0xFF; |
| 594 |
} |
| 595 |
|
| 596 |
} else { |
| 597 |
/* |
| 598 |
plain packet: |
| 599 |
|
| 600 |
{data | "header" | gss-trailer (krb5 checksum) |
| 601 |
|
| 602 |
don't do RRC != 0 |
| 603 |
|
| 604 |
*/ |
| 605 |
|
| 606 |
for (i = 0; i < iov_count; i++) { |
| 607 |
switch (GSS_IOV_BUFFER_TYPE(iov[i].type)) { |
| 608 |
case GSS_IOV_BUFFER_TYPE_DATA: |
| 609 |
data[i].flags = KRB5_CRYPTO_TYPE_DATA; |
| 610 |
break; |
| 611 |
case GSS_IOV_BUFFER_TYPE_SIGN_ONLY: |
| 612 |
data[i].flags = KRB5_CRYPTO_TYPE_SIGN_ONLY; |
| 613 |
break; |
| 614 |
default: |
| 615 |
data[i].flags = KRB5_CRYPTO_TYPE_EMPTY; |
| 616 |
break; |
| 617 |
} |
| 618 |
data[i].data.length = iov[i].buffer.length; |
| 619 |
data[i].data.data = iov[i].buffer.value; |
| 620 |
} |
| 621 |
|
| 622 |
data[i].flags = KRB5_CRYPTO_TYPE_DATA; |
| 623 |
data[i].data.data = header->buffer.value; |
| 624 |
data[i].data.length = sizeof(gss_cfx_wrap_token_desc); |
| 625 |
i++; |
| 626 |
|
| 627 |
data[i].flags = KRB5_CRYPTO_TYPE_CHECKSUM; |
| 628 |
if (trailer) { |
| 629 |
data[i].data.data = trailer->buffer.value; |
| 630 |
} else { |
| 631 |
data[i].data.data = (uint8_t *)header->buffer.value + |
| 632 |
sizeof(gss_cfx_wrap_token_desc); |
| 633 |
} |
| 634 |
data[i].data.length = k5tsize; |
| 635 |
i++; |
| 636 |
|
| 637 |
ret = krb5_create_checksum_iov(context, ctx->crypto, usage, data, i, NULL); |
| 638 |
if (ret) { |
| 639 |
*minor_status = ret; |
| 640 |
major_status = GSS_S_FAILURE; |
| 641 |
goto failure; |
| 642 |
} |
| 643 |
|
| 644 |
if (rrc) { |
| 645 |
token->RRC[0] = (rrc >> 8) & 0xFF; |
| 646 |
token->RRC[1] = (rrc >> 0) & 0xFF; |
| 647 |
} |
| 648 |
|
| 649 |
token->EC[0] = (k5tsize >> 8) & 0xFF; |
| 650 |
token->EC[1] = (k5tsize >> 0) & 0xFF; |
| 651 |
} |
| 652 |
|
| 653 |
if (conf_state != NULL) |
| 654 |
*conf_state = conf_req_flag; |
| 655 |
|
| 656 |
free(data); |
| 657 |
|
| 658 |
*minor_status = 0; |
| 659 |
return GSS_S_COMPLETE; |
| 660 |
|
| 661 |
failure: |
| 662 |
if (data) |
| 663 |
free(data); |
| 664 |
|
| 665 |
gss_release_iov_buffer(&junk, iov, iov_count); |
| 666 |
|
| 667 |
return major_status; |
| 668 |
} |
| 669 |
|
| 670 |
/* This is slowpath */ |
| 671 |
static OM_uint32 |
| 672 |
unrotate_iov(OM_uint32 *minor_status, size_t rrc, gss_iov_buffer_desc *iov, int iov_count) |
| 673 |
{ |
| 674 |
uint8_t *p, *q; |
| 675 |
size_t len = 0, skip; |
| 676 |
int i; |
| 677 |
|
| 678 |
for (i = 0; i < iov_count; i++) |
| 679 |
if (GSS_IOV_BUFFER_TYPE(iov[i].type) == GSS_IOV_BUFFER_TYPE_DATA || |
| 680 |
GSS_IOV_BUFFER_TYPE(iov[i].type) == GSS_IOV_BUFFER_TYPE_PADDING || |
| 681 |
GSS_IOV_BUFFER_TYPE(iov[i].type) == GSS_IOV_BUFFER_TYPE_TRAILER) |
| 682 |
len += iov[i].buffer.length; |
| 683 |
|
| 684 |
p = malloc(len); |
| 685 |
if (p == NULL) { |
| 686 |
*minor_status = ENOMEM; |
| 687 |
return GSS_S_FAILURE; |
| 688 |
} |
| 689 |
q = p; |
| 690 |
|
| 691 |
/* copy up */ |
| 692 |
|
| 693 |
for (i = 0; i < iov_count; i++) { |
| 694 |
if (GSS_IOV_BUFFER_TYPE(iov[i].type) == GSS_IOV_BUFFER_TYPE_DATA || |
| 695 |
GSS_IOV_BUFFER_TYPE(iov[i].type) == GSS_IOV_BUFFER_TYPE_PADDING || |
| 696 |
GSS_IOV_BUFFER_TYPE(iov[i].type) == GSS_IOV_BUFFER_TYPE_TRAILER) |
| 697 |
{ |
| 698 |
memcpy(q, iov[i].buffer.value, iov[i].buffer.length); |
| 699 |
q += iov[i].buffer.length; |
| 700 |
} |
| 701 |
} |
| 702 |
assert((size_t)(q - p) == len); |
| 703 |
|
| 704 |
/* unrotate first part */ |
| 705 |
q = p + rrc; |
| 706 |
skip = rrc; |
| 707 |
for (i = 0; i < iov_count; i++) { |
| 708 |
if (GSS_IOV_BUFFER_TYPE(iov[i].type) == GSS_IOV_BUFFER_TYPE_DATA || |
| 709 |
GSS_IOV_BUFFER_TYPE(iov[i].type) == GSS_IOV_BUFFER_TYPE_PADDING || |
| 710 |
GSS_IOV_BUFFER_TYPE(iov[i].type) == GSS_IOV_BUFFER_TYPE_TRAILER) |
| 711 |
{ |
| 712 |
if (iov[i].buffer.length <= skip) { |
| 713 |
skip -= iov[i].buffer.length; |
| 714 |
} else { |
| 715 |
/* copy back to original buffer */ |
| 716 |
memcpy(((uint8_t *)iov[i].buffer.value) + skip, q, iov[i].buffer.length - skip); |
| 717 |
q += iov[i].buffer.length - skip; |
| 718 |
skip = 0; |
| 719 |
} |
| 720 |
} |
| 721 |
} |
| 722 |
/* copy trailer */ |
| 723 |
q = p; |
| 724 |
skip = rrc; |
| 725 |
for (i = 0; i < iov_count; i++) { |
| 726 |
if (GSS_IOV_BUFFER_TYPE(iov[i].type) == GSS_IOV_BUFFER_TYPE_DATA || |
| 727 |
GSS_IOV_BUFFER_TYPE(iov[i].type) == GSS_IOV_BUFFER_TYPE_PADDING || |
| 728 |
GSS_IOV_BUFFER_TYPE(iov[i].type) == GSS_IOV_BUFFER_TYPE_TRAILER) |
| 729 |
{ |
| 730 |
memcpy(iov[i].buffer.value, q, min(iov[i].buffer.length, skip)); |
| 731 |
if (iov[i].buffer.length > skip) |
| 732 |
break; |
| 733 |
skip -= iov[i].buffer.length; |
| 734 |
q += iov[i].buffer.length; |
| 735 |
} |
| 736 |
} |
| 737 |
free(p); |
| 738 |
return GSS_S_COMPLETE; |
| 739 |
} |
| 740 |
|
| 741 |
|
| 742 |
OM_uint32 |
| 743 |
_gssapi_unwrap_cfx_iov(OM_uint32 *minor_status, |
| 744 |
gsskrb5_ctx ctx, |
| 745 |
krb5_context context, |
| 746 |
int *conf_state, |
| 747 |
gss_qop_t *qop_state, |
| 748 |
gss_iov_buffer_desc *iov, |
| 749 |
int iov_count) |
| 750 |
{ |
| 751 |
OM_uint32 seq_number_lo, seq_number_hi, major_status, junk; |
| 752 |
gss_iov_buffer_desc *header, *trailer, *padding; |
| 753 |
gss_cfx_wrap_token token, ttoken; |
| 754 |
u_char token_flags; |
| 755 |
krb5_error_code ret; |
| 756 |
unsigned usage; |
| 757 |
uint16_t ec, rrc; |
| 758 |
krb5_crypto_iov *data = NULL; |
| 759 |
int i, j; |
| 760 |
|
| 761 |
*minor_status = 0; |
| 762 |
|
| 763 |
header = _gk_find_buffer(iov, iov_count, GSS_IOV_BUFFER_TYPE_HEADER); |
| 764 |
if (header == NULL) { |
| 765 |
*minor_status = EINVAL; |
| 766 |
return GSS_S_FAILURE; |
| 767 |
} |
| 768 |
|
| 769 |
if (header->buffer.length < sizeof(*token)) /* we check exact below */ |
| 770 |
return GSS_S_DEFECTIVE_TOKEN; |
| 771 |
|
| 772 |
padding = _gk_find_buffer(iov, iov_count, GSS_IOV_BUFFER_TYPE_PADDING); |
| 773 |
if (padding != NULL && padding->buffer.length != 0) { |
| 774 |
*minor_status = EINVAL; |
| 775 |
return GSS_S_FAILURE; |
| 776 |
} |
| 777 |
|
| 778 |
trailer = _gk_find_buffer(iov, iov_count, GSS_IOV_BUFFER_TYPE_TRAILER); |
| 779 |
|
| 780 |
major_status = _gk_verify_buffers(minor_status, ctx, header, |
| 781 |
padding, trailer, FALSE); |
| 782 |
if (major_status != GSS_S_COMPLETE) { |
| 783 |
return major_status; |
| 784 |
} |
| 785 |
|
| 786 |
token = (gss_cfx_wrap_token)header->buffer.value; |
| 787 |
|
| 788 |
if (token->TOK_ID[0] != 0x05 || token->TOK_ID[1] != 0x04) |
| 789 |
return GSS_S_DEFECTIVE_TOKEN; |
| 790 |
|
| 791 |
/* Ignore unknown flags */ |
| 792 |
token_flags = token->Flags & |
| 793 |
(CFXSentByAcceptor | CFXSealed | CFXAcceptorSubkey); |
| 794 |
|
| 795 |
if (token_flags & CFXSentByAcceptor) { |
| 796 |
if ((ctx->more_flags & LOCAL) == 0) |
| 797 |
return GSS_S_DEFECTIVE_TOKEN; |
| 798 |
} |
| 799 |
|
| 800 |
if (ctx->more_flags & ACCEPTOR_SUBKEY) { |
| 801 |
if ((token_flags & CFXAcceptorSubkey) == 0) |
| 802 |
return GSS_S_DEFECTIVE_TOKEN; |
| 803 |
} else { |
| 804 |
if (token_flags & CFXAcceptorSubkey) |
| 805 |
return GSS_S_DEFECTIVE_TOKEN; |
| 806 |
} |
| 807 |
|
| 808 |
if (token->Filler != 0xFF) |
| 809 |
return GSS_S_DEFECTIVE_TOKEN; |
| 810 |
|
| 811 |
if (conf_state != NULL) |
| 812 |
*conf_state = (token_flags & CFXSealed) ? 1 : 0; |
| 813 |
|
| 814 |
ec = (token->EC[0] << 8) | token->EC[1]; |
| 815 |
rrc = (token->RRC[0] << 8) | token->RRC[1]; |
| 816 |
|
| 817 |
/* |
| 818 |
* Check sequence number |
| 819 |
*/ |
| 820 |
_gsskrb5_decode_be_om_uint32(&token->SND_SEQ[0], &seq_number_hi); |
| 821 |
_gsskrb5_decode_be_om_uint32(&token->SND_SEQ[4], &seq_number_lo); |
| 822 |
if (seq_number_hi) { |
| 823 |
/* no support for 64-bit sequence numbers */ |
| 824 |
*minor_status = ERANGE; |
| 825 |
return GSS_S_UNSEQ_TOKEN; |
| 826 |
} |
| 827 |
|
| 828 |
HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex); |
| 829 |
ret = _gssapi_msg_order_check(ctx->order, seq_number_lo); |
| 830 |
if (ret != 0) { |
| 831 |
*minor_status = 0; |
| 832 |
HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex); |
| 833 |
return ret; |
| 834 |
} |
| 835 |
HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex); |
| 836 |
|
| 837 |
/* |
| 838 |
* Decrypt and/or verify checksum |
| 839 |
*/ |
| 840 |
|
| 841 |
if (ctx->more_flags & LOCAL) { |
| 842 |
usage = KRB5_KU_USAGE_ACCEPTOR_SEAL; |
| 843 |
} else { |
| 844 |
usage = KRB5_KU_USAGE_INITIATOR_SEAL; |
| 845 |
} |
| 846 |
|
| 847 |
data = calloc(iov_count + 3, sizeof(data[0])); |
| 848 |
if (data == NULL) { |
| 849 |
*minor_status = ENOMEM; |
| 850 |
major_status = GSS_S_FAILURE; |
| 851 |
goto failure; |
| 852 |
} |
| 853 |
|
| 854 |
if (token_flags & CFXSealed) { |
| 855 |
size_t k5tsize, k5hsize; |
| 856 |
|
| 857 |
krb5_crypto_length(context, ctx->crypto, KRB5_CRYPTO_TYPE_HEADER, &k5hsize); |
| 858 |
krb5_crypto_length(context, ctx->crypto, KRB5_CRYPTO_TYPE_TRAILER, &k5tsize); |
| 859 |
|
| 860 |
/* Rotate by RRC; bogus to do this in-place XXX */ |
| 861 |
/* Check RRC */ |
| 862 |
|
| 863 |
if (trailer == NULL) { |
| 864 |
size_t gsstsize = k5tsize + sizeof(*token); |
| 865 |
size_t gsshsize = k5hsize + sizeof(*token); |
| 866 |
|
| 867 |
if (rrc != gsstsize) { |
| 868 |
major_status = GSS_S_DEFECTIVE_TOKEN; |
| 869 |
goto failure; |
| 870 |
} |
| 871 |
|
| 872 |
if (IS_DCE_STYLE(ctx)) |
| 873 |
gsstsize += ec; |
| 874 |
|
| 875 |
gsshsize += gsstsize; |
| 876 |
|
| 877 |
if (header->buffer.length != gsshsize) { |
| 878 |
major_status = GSS_S_DEFECTIVE_TOKEN; |
| 879 |
goto failure; |
| 880 |
} |
| 881 |
} else if (trailer->buffer.length != sizeof(*token) + k5tsize) { |
| 882 |
major_status = GSS_S_DEFECTIVE_TOKEN; |
| 883 |
goto failure; |
| 884 |
} else if (header->buffer.length != sizeof(*token) + k5hsize) { |
| 885 |
major_status = GSS_S_DEFECTIVE_TOKEN; |
| 886 |
goto failure; |
| 887 |
} else if (rrc != 0) { |
| 888 |
/* go though slowpath */ |
| 889 |
major_status = unrotate_iov(minor_status, rrc, iov, iov_count); |
| 890 |
if (major_status) |
| 891 |
goto failure; |
| 892 |
} |
| 893 |
|
| 894 |
i = 0; |
| 895 |
data[i].flags = KRB5_CRYPTO_TYPE_HEADER; |
| 896 |
data[i].data.data = ((uint8_t *)header->buffer.value) + header->buffer.length - k5hsize; |
| 897 |
data[i].data.length = k5hsize; |
| 898 |
i++; |
| 899 |
|
| 900 |
for (j = 0; j < iov_count; i++, j++) { |
| 901 |
switch (GSS_IOV_BUFFER_TYPE(iov[j].type)) { |
| 902 |
case GSS_IOV_BUFFER_TYPE_DATA: |
| 903 |
data[i].flags = KRB5_CRYPTO_TYPE_DATA; |
| 904 |
break; |
| 905 |
case GSS_IOV_BUFFER_TYPE_SIGN_ONLY: |
| 906 |
data[i].flags = KRB5_CRYPTO_TYPE_SIGN_ONLY; |
| 907 |
break; |
| 908 |
default: |
| 909 |
data[i].flags = KRB5_CRYPTO_TYPE_EMPTY; |
| 910 |
break; |
| 911 |
} |
| 912 |
data[i].data.length = iov[j].buffer.length; |
| 913 |
data[i].data.data = iov[j].buffer.value; |
| 914 |
} |
| 915 |
|
| 916 |
/* encrypted CFX header in trailer (or after the header if in |
| 917 |
DCE mode). Copy in header into E"header" |
| 918 |
*/ |
| 919 |
data[i].flags = KRB5_CRYPTO_TYPE_DATA; |
| 920 |
if (trailer) { |
| 921 |
data[i].data.data = trailer->buffer.value; |
| 922 |
} else { |
| 923 |
data[i].data.data = ((uint8_t *)header->buffer.value) + |
| 924 |
header->buffer.length - k5hsize - k5tsize - ec- sizeof(*token); |
| 925 |
} |
| 926 |
|
| 927 |
data[i].data.length = ec + sizeof(*token); |
| 928 |
ttoken = (gss_cfx_wrap_token)(((uint8_t *)data[i].data.data) + ec); |
| 929 |
i++; |
| 930 |
|
| 931 |
/* Kerberos trailer comes after the gss trailer */ |
| 932 |
data[i].flags = KRB5_CRYPTO_TYPE_TRAILER; |
| 933 |
data[i].data.data = ((uint8_t *)data[i-1].data.data) + ec + sizeof(*token); |
| 934 |
data[i].data.length = k5tsize; |
| 935 |
i++; |
| 936 |
|
| 937 |
ret = krb5_decrypt_iov_ivec(context, ctx->crypto, usage, data, i, NULL); |
| 938 |
if (ret != 0) { |
| 939 |
*minor_status = ret; |
| 940 |
major_status = GSS_S_FAILURE; |
| 941 |
goto failure; |
| 942 |
} |
| 943 |
|
| 944 |
ttoken->RRC[0] = token->RRC[0]; |
| 945 |
ttoken->RRC[1] = token->RRC[1]; |
| 946 |
|
| 947 |
/* Check the integrity of the header */ |
| 948 |
if (ct_memcmp(ttoken, token, sizeof(*token)) != 0) { |
| 949 |
major_status = GSS_S_BAD_MIC; |
| 950 |
goto failure; |
| 951 |
} |
| 952 |
} else { |
| 953 |
size_t gsstsize = ec; |
| 954 |
size_t gsshsize = sizeof(*token); |
| 955 |
|
| 956 |
if (trailer == NULL) { |
| 957 |
/* Check RRC */ |
| 958 |
if (rrc != gsstsize) { |
| 959 |
*minor_status = EINVAL; |
| 960 |
major_status = GSS_S_FAILURE; |
| 961 |
goto failure; |
| 962 |
} |
| 963 |
|
| 964 |
gsshsize += gsstsize; |
| 965 |
} else if (trailer->buffer.length != gsstsize) { |
| 966 |
major_status = GSS_S_DEFECTIVE_TOKEN; |
| 967 |
goto failure; |
| 968 |
} else if (rrc != 0) { |
| 969 |
/* Check RRC */ |
| 970 |
*minor_status = EINVAL; |
| 971 |
major_status = GSS_S_FAILURE; |
| 972 |
goto failure; |
| 973 |
} |
| 974 |
|
| 975 |
if (header->buffer.length != gsshsize) { |
| 976 |
major_status = GSS_S_DEFECTIVE_TOKEN; |
| 977 |
goto failure; |
| 978 |
} |
| 979 |
|
| 980 |
for (i = 0; i < iov_count; i++) { |
| 981 |
switch (GSS_IOV_BUFFER_TYPE(iov[i].type)) { |
| 982 |
case GSS_IOV_BUFFER_TYPE_DATA: |
| 983 |
data[i].flags = KRB5_CRYPTO_TYPE_DATA; |
| 984 |
break; |
| 985 |
case GSS_IOV_BUFFER_TYPE_SIGN_ONLY: |
| 986 |
data[i].flags = KRB5_CRYPTO_TYPE_SIGN_ONLY; |
| 987 |
break; |
| 988 |
default: |
| 989 |
data[i].flags = KRB5_CRYPTO_TYPE_EMPTY; |
| 990 |
break; |
| 991 |
} |
| 992 |
data[i].data.length = iov[i].buffer.length; |
| 993 |
data[i].data.data = iov[i].buffer.value; |
| 994 |
} |
| 995 |
|
| 996 |
data[i].flags = KRB5_CRYPTO_TYPE_DATA; |
| 997 |
data[i].data.data = header->buffer.value; |
| 998 |
data[i].data.length = sizeof(*token); |
| 999 |
i++; |
| 1000 |
|
| 1001 |
data[i].flags = KRB5_CRYPTO_TYPE_CHECKSUM; |
| 1002 |
if (trailer) { |
| 1003 |
data[i].data.data = trailer->buffer.value; |
| 1004 |
} else { |
| 1005 |
data[i].data.data = (uint8_t *)header->buffer.value + |
| 1006 |
sizeof(*token); |
| 1007 |
} |
| 1008 |
data[i].data.length = ec; |
| 1009 |
i++; |
| 1010 |
|
| 1011 |
token = (gss_cfx_wrap_token)header->buffer.value; |
| 1012 |
token->EC[0] = 0; |
| 1013 |
token->EC[1] = 0; |
| 1014 |
token->RRC[0] = 0; |
| 1015 |
token->RRC[1] = 0; |
| 1016 |
|
| 1017 |
ret = krb5_verify_checksum_iov(context, ctx->crypto, usage, data, i, NULL); |
| 1018 |
if (ret) { |
| 1019 |
*minor_status = ret; |
| 1020 |
major_status = GSS_S_FAILURE; |
| 1021 |
goto failure; |
| 1022 |
} |
| 1023 |
} |
| 1024 |
|
| 1025 |
if (qop_state != NULL) { |
| 1026 |
*qop_state = GSS_C_QOP_DEFAULT; |
| 1027 |
} |
| 1028 |
|
| 1029 |
free(data); |
| 1030 |
|
| 1031 |
*minor_status = 0; |
| 1032 |
return GSS_S_COMPLETE; |
| 1033 |
|
| 1034 |
failure: |
| 1035 |
if (data) |
| 1036 |
free(data); |
| 1037 |
|
| 1038 |
gss_release_iov_buffer(&junk, iov, iov_count); |
| 1039 |
|
| 1040 |
return major_status; |
| 1041 |
} |
| 1042 |
|
| 1043 |
OM_uint32 |
| 1044 |
_gssapi_wrap_iov_length_cfx(OM_uint32 *minor_status, |
| 1045 |
gsskrb5_ctx ctx, |
| 1046 |
krb5_context context, |
| 1047 |
int conf_req_flag, |
| 1048 |
gss_qop_t qop_req, |
| 1049 |
int *conf_state, |
| 1050 |
gss_iov_buffer_desc *iov, |
| 1051 |
int iov_count) |
| 1052 |
{ |
| 1053 |
OM_uint32 major_status; |
| 1054 |
size_t size; |
| 1055 |
int i; |
| 1056 |
gss_iov_buffer_desc *header = NULL; |
| 1057 |
gss_iov_buffer_desc *padding = NULL; |
| 1058 |
gss_iov_buffer_desc *trailer = NULL; |
| 1059 |
size_t gsshsize = 0; |
| 1060 |
size_t gsstsize = 0; |
| 1061 |
size_t k5hsize = 0; |
| 1062 |
size_t k5tsize = 0; |
| 1063 |
|
| 1064 |
GSSAPI_KRB5_INIT (&context); |
| 1065 |
*minor_status = 0; |
| 1066 |
|
| 1067 |
for (size = 0, i = 0; i < iov_count; i++) { |
| 1068 |
switch(GSS_IOV_BUFFER_TYPE(iov[i].type)) { |
| 1069 |
case GSS_IOV_BUFFER_TYPE_EMPTY: |
| 1070 |
break; |
| 1071 |
case GSS_IOV_BUFFER_TYPE_DATA: |
| 1072 |
size += iov[i].buffer.length; |
| 1073 |
break; |
| 1074 |
case GSS_IOV_BUFFER_TYPE_HEADER: |
| 1075 |
if (header != NULL) { |
| 1076 |
*minor_status = 0; |
| 1077 |
return GSS_S_FAILURE; |
| 1078 |
} |
| 1079 |
header = &iov[i]; |
| 1080 |
break; |
| 1081 |
case GSS_IOV_BUFFER_TYPE_TRAILER: |
| 1082 |
if (trailer != NULL) { |
| 1083 |
*minor_status = 0; |
| 1084 |
return GSS_S_FAILURE; |
| 1085 |
} |
| 1086 |
trailer = &iov[i]; |
| 1087 |
break; |
| 1088 |
case GSS_IOV_BUFFER_TYPE_PADDING: |
| 1089 |
if (padding != NULL) { |
| 1090 |
*minor_status = 0; |
| 1091 |
return GSS_S_FAILURE; |
| 1092 |
} |
| 1093 |
padding = &iov[i]; |
| 1094 |
break; |
| 1095 |
case GSS_IOV_BUFFER_TYPE_SIGN_ONLY: |
| 1096 |
break; |
| 1097 |
default: |
| 1098 |
*minor_status = EINVAL; |
| 1099 |
return GSS_S_FAILURE; |
| 1100 |
} |
| 1101 |
} |
| 1102 |
|
| 1103 |
major_status = _gk_verify_buffers(minor_status, ctx, header, |
| 1104 |
padding, trailer, FALSE); |
| 1105 |
if (major_status != GSS_S_COMPLETE) { |
| 1106 |
return major_status; |
| 1107 |
} |
| 1108 |
|
| 1109 |
if (conf_req_flag) { |
| 1110 |
size_t k5psize = 0; |
| 1111 |
size_t k5pbase = 0; |
| 1112 |
size_t k5bsize = 0; |
| 1113 |
size_t ec = 0; |
| 1114 |
|
| 1115 |
size += sizeof(gss_cfx_wrap_token_desc); |
| 1116 |
|
| 1117 |
*minor_status = krb5_crypto_length(context, ctx->crypto, |
| 1118 |
KRB5_CRYPTO_TYPE_HEADER, |
| 1119 |
&k5hsize); |
| 1120 |
if (*minor_status) |
| 1121 |
return GSS_S_FAILURE; |
| 1122 |
|
| 1123 |
*minor_status = krb5_crypto_length(context, ctx->crypto, |
| 1124 |
KRB5_CRYPTO_TYPE_TRAILER, |
| 1125 |
&k5tsize); |
| 1126 |
if (*minor_status) |
| 1127 |
return GSS_S_FAILURE; |
| 1128 |
|
| 1129 |
*minor_status = krb5_crypto_length(context, ctx->crypto, |
| 1130 |
KRB5_CRYPTO_TYPE_PADDING, |
| 1131 |
&k5pbase); |
| 1132 |
if (*minor_status) |
| 1133 |
return GSS_S_FAILURE; |
| 1134 |
|
| 1135 |
if (k5pbase > 1) { |
| 1136 |
k5psize = k5pbase - (size % k5pbase); |
| 1137 |
} else { |
| 1138 |
k5psize = 0; |
| 1139 |
} |
| 1140 |
|
| 1141 |
if (k5psize == 0 && IS_DCE_STYLE(ctx)) { |
| 1142 |
*minor_status = krb5_crypto_getblocksize(context, ctx->crypto, |
| 1143 |
&k5bsize); |
| 1144 |
if (*minor_status) |
| 1145 |
return GSS_S_FAILURE; |
| 1146 |
|
| 1147 |
ec = k5bsize; |
| 1148 |
} else { |
| 1149 |
ec = k5psize; |
| 1150 |
} |
| 1151 |
|
| 1152 |
gsshsize = sizeof(gss_cfx_wrap_token_desc) + k5hsize; |
| 1153 |
gsstsize = sizeof(gss_cfx_wrap_token_desc) + ec + k5tsize; |
| 1154 |
} else { |
| 1155 |
*minor_status = krb5_crypto_length(context, ctx->crypto, |
| 1156 |
KRB5_CRYPTO_TYPE_CHECKSUM, |
| 1157 |
&k5tsize); |
| 1158 |
if (*minor_status) |
| 1159 |
return GSS_S_FAILURE; |
| 1160 |
|
| 1161 |
gsshsize = sizeof(gss_cfx_wrap_token_desc); |
| 1162 |
gsstsize = k5tsize; |
| 1163 |
} |
| 1164 |
|
| 1165 |
if (trailer != NULL) { |
| 1166 |
trailer->buffer.length = gsstsize; |
| 1167 |
} else { |
| 1168 |
gsshsize += gsstsize; |
| 1169 |
} |
| 1170 |
|
| 1171 |
header->buffer.length = gsshsize; |
| 1172 |
|
| 1173 |
if (padding) { |
| 1174 |
/* padding is done via EC and is contained in the header or trailer */ |
| 1175 |
padding->buffer.length = 0; |
| 1176 |
} |
| 1177 |
|
| 1178 |
if (conf_state) { |
| 1179 |
*conf_state = conf_req_flag; |
| 1180 |
} |
| 1181 |
|
| 1182 |
return GSS_S_COMPLETE; |
| 1183 |
} |
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
OM_uint32 _gssapi_wrap_cfx(OM_uint32 *minor_status, |
| 1189 |
const gsskrb5_ctx ctx, |
| 1190 |
krb5_context context, |
| 1191 |
int conf_req_flag, |
| 1192 |
const gss_buffer_t input_message_buffer, |
| 1193 |
int *conf_state, |
| 1194 |
gss_buffer_t output_message_buffer) |
| 1195 |
{ |
| 1196 |
gss_cfx_wrap_token token; |
| 1197 |
krb5_error_code ret; |
| 1198 |
unsigned usage; |
| 1199 |
krb5_data cipher; |
| 1200 |
size_t wrapped_len, cksumsize; |
| 1201 |
uint16_t padlength, rrc = 0; |
| 1202 |
int32_t seq_number; |
| 1203 |
u_char *p; |
| 1204 |
|
| 1205 |
ret = _gsskrb5cfx_wrap_length_cfx(context, |
| 1206 |
ctx->crypto, conf_req_flag, |
| 1207 |
IS_DCE_STYLE(ctx), |
| 1208 |
input_message_buffer->length, |
| 1209 |
&wrapped_len, &cksumsize, &padlength); |
| 1210 |
if (ret != 0) { |
| 1211 |
*minor_status = ret; |
| 1212 |
return GSS_S_FAILURE; |
| 1213 |
} |
| 1214 |
|
| 1215 |
/* Always rotate encrypted token (if any) and checksum to header */ |
| 1216 |
rrc = (conf_req_flag ? sizeof(*token) : 0) + (uint16_t)cksumsize; |
| 1217 |
|
| 1218 |
output_message_buffer->length = wrapped_len; |
| 1219 |
output_message_buffer->value = malloc(output_message_buffer->length); |
| 1220 |
if (output_message_buffer->value == NULL) { |
| 1221 |
*minor_status = ENOMEM; |
| 1222 |
return GSS_S_FAILURE; |
| 1223 |
} |
| 1224 |
|
| 1225 |
p = output_message_buffer->value; |
| 1226 |
token = (gss_cfx_wrap_token)p; |
| 1227 |
token->TOK_ID[0] = 0x05; |
| 1228 |
token->TOK_ID[1] = 0x04; |
| 1229 |
token->Flags = 0; |
| 1230 |
token->Filler = 0xFF; |
| 1231 |
if ((ctx->more_flags & LOCAL) == 0) |
| 1232 |
token->Flags |= CFXSentByAcceptor; |
| 1233 |
if (ctx->more_flags & ACCEPTOR_SUBKEY) |
| 1234 |
token->Flags |= CFXAcceptorSubkey; |
| 1235 |
if (conf_req_flag) { |
| 1236 |
/* |
| 1237 |
* In Wrap tokens with confidentiality, the EC field is |
| 1238 |
* used to encode the size (in bytes) of the random filler. |
| 1239 |
*/ |
| 1240 |
token->Flags |= CFXSealed; |
| 1241 |
token->EC[0] = (padlength >> 8) & 0xFF; |
| 1242 |
token->EC[1] = (padlength >> 0) & 0xFF; |
| 1243 |
} else { |
| 1244 |
/* |
| 1245 |
* In Wrap tokens without confidentiality, the EC field is |
| 1246 |
* used to encode the size (in bytes) of the trailing |
| 1247 |
* checksum. |
| 1248 |
* |
| 1249 |
* This is not used in the checksum calcuation itself, |
| 1250 |
* because the checksum length could potentially vary |
| 1251 |
* depending on the data length. |
| 1252 |
*/ |
| 1253 |
token->EC[0] = 0; |
| 1254 |
token->EC[1] = 0; |
| 1255 |
} |
| 1256 |
|
| 1257 |
/* |
| 1258 |
* In Wrap tokens that provide for confidentiality, the RRC |
| 1259 |
* field in the header contains the hex value 00 00 before |
| 1260 |
* encryption. |
| 1261 |
* |
| 1262 |
* In Wrap tokens that do not provide for confidentiality, |
| 1263 |
* both the EC and RRC fields in the appended checksum |
| 1264 |
* contain the hex value 00 00 for the purpose of calculating |
| 1265 |
* the checksum. |
| 1266 |
*/ |
| 1267 |
token->RRC[0] = 0; |
| 1268 |
token->RRC[1] = 0; |
| 1269 |
|
| 1270 |
HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex); |
| 1271 |
krb5_auth_con_getlocalseqnumber(context, |
| 1272 |
ctx->auth_context, |
| 1273 |
&seq_number); |
| 1274 |
_gsskrb5_encode_be_om_uint32(0, &token->SND_SEQ[0]); |
| 1275 |
_gsskrb5_encode_be_om_uint32(seq_number, &token->SND_SEQ[4]); |
| 1276 |
krb5_auth_con_setlocalseqnumber(context, |
| 1277 |
ctx->auth_context, |
| 1278 |
++seq_number); |
| 1279 |
HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex); |
| 1280 |
|
| 1281 |
/* |
| 1282 |
* If confidentiality is requested, the token header is |
| 1283 |
* appended to the plaintext before encryption; the resulting |
| 1284 |
* token is {"header" | encrypt(plaintext | pad | "header")}. |
| 1285 |
* |
| 1286 |
* If no confidentiality is requested, the checksum is |
| 1287 |
* calculated over the plaintext concatenated with the |
| 1288 |
* token header. |
| 1289 |
*/ |
| 1290 |
if (ctx->more_flags & LOCAL) { |
| 1291 |
usage = KRB5_KU_USAGE_INITIATOR_SEAL; |
| 1292 |
} else { |
| 1293 |
usage = KRB5_KU_USAGE_ACCEPTOR_SEAL; |
| 1294 |
} |
| 1295 |
|
| 1296 |
if (conf_req_flag) { |
| 1297 |
/* |
| 1298 |
* Any necessary padding is added here to ensure that the |
| 1299 |
* encrypted token header is always at the end of the |
| 1300 |
* ciphertext. |
| 1301 |
* |
| 1302 |
* The specification does not require that the padding |
| 1303 |
* bytes are initialized. |
| 1304 |
*/ |
| 1305 |
p += sizeof(*token); |
| 1306 |
memcpy(p, input_message_buffer->value, input_message_buffer->length); |
| 1307 |
memset(p + input_message_buffer->length, 0xFF, padlength); |
| 1308 |
memcpy(p + input_message_buffer->length + padlength, |
| 1309 |
token, sizeof(*token)); |
| 1310 |
|
| 1311 |
ret = krb5_encrypt(context, ctx->crypto, |
| 1312 |
usage, p, |
| 1313 |
input_message_buffer->length + padlength + |
| 1314 |
sizeof(*token), |
| 1315 |
&cipher); |
| 1316 |
if (ret != 0) { |
| 1317 |
*minor_status = ret; |
| 1318 |
_gsskrb5_release_buffer(minor_status, output_message_buffer); |
| 1319 |
return GSS_S_FAILURE; |
| 1320 |
} |
| 1321 |
assert(sizeof(*token) + cipher.length == wrapped_len); |
| 1322 |
token->RRC[0] = (rrc >> 8) & 0xFF; |
| 1323 |
token->RRC[1] = (rrc >> 0) & 0xFF; |
| 1324 |
|
| 1325 |
/* |
| 1326 |
* this is really ugly, but needed against windows |
| 1327 |
* for DCERPC, as windows rotates by EC+RRC. |
| 1328 |
*/ |
| 1329 |
if (IS_DCE_STYLE(ctx)) { |
| 1330 |
ret = rrc_rotate(cipher.data, cipher.length, rrc+padlength, FALSE); |
| 1331 |
} else { |
| 1332 |
ret = rrc_rotate(cipher.data, cipher.length, rrc, FALSE); |
| 1333 |
} |
| 1334 |
if (ret != 0) { |
| 1335 |
*minor_status = ret; |
| 1336 |
_gsskrb5_release_buffer(minor_status, output_message_buffer); |
| 1337 |
return GSS_S_FAILURE; |
| 1338 |
} |
| 1339 |
memcpy(p, cipher.data, cipher.length); |
| 1340 |
krb5_data_free(&cipher); |
| 1341 |
} else { |
| 1342 |
char *buf; |
| 1343 |
Checksum cksum; |
| 1344 |
|
| 1345 |
buf = malloc(input_message_buffer->length + sizeof(*token)); |
| 1346 |
if (buf == NULL) { |
| 1347 |
*minor_status = ENOMEM; |
| 1348 |
_gsskrb5_release_buffer(minor_status, output_message_buffer); |
| 1349 |
return GSS_S_FAILURE; |
| 1350 |
} |
| 1351 |
memcpy(buf, input_message_buffer->value, input_message_buffer->length); |
| 1352 |
memcpy(buf + input_message_buffer->length, token, sizeof(*token)); |
| 1353 |
|
| 1354 |
ret = krb5_create_checksum(context, ctx->crypto, |
| 1355 |
usage, 0, buf, |
| 1356 |
input_message_buffer->length + |
| 1357 |
sizeof(*token), |
| 1358 |
&cksum); |
| 1359 |
if (ret != 0) { |
| 1360 |
*minor_status = ret; |
| 1361 |
_gsskrb5_release_buffer(minor_status, output_message_buffer); |
| 1362 |
free(buf); |
| 1363 |
return GSS_S_FAILURE; |
| 1364 |
} |
| 1365 |
|
| 1366 |
free(buf); |
| 1367 |
|
| 1368 |
assert(cksum.checksum.length == cksumsize); |
| 1369 |
token->EC[0] = (cksum.checksum.length >> 8) & 0xFF; |
| 1370 |
token->EC[1] = (cksum.checksum.length >> 0) & 0xFF; |
| 1371 |
token->RRC[0] = (rrc >> 8) & 0xFF; |
| 1372 |
token->RRC[1] = (rrc >> 0) & 0xFF; |
| 1373 |
|
| 1374 |
p += sizeof(*token); |
| 1375 |
memcpy(p, input_message_buffer->value, input_message_buffer->length); |
| 1376 |
memcpy(p + input_message_buffer->length, |
| 1377 |
cksum.checksum.data, cksum.checksum.length); |
| 1378 |
|
| 1379 |
ret = rrc_rotate(p, |
| 1380 |
input_message_buffer->length + cksum.checksum.length, rrc, FALSE); |
| 1381 |
if (ret != 0) { |
| 1382 |
*minor_status = ret; |
| 1383 |
_gsskrb5_release_buffer(minor_status, output_message_buffer); |
| 1384 |
free_Checksum(&cksum); |
| 1385 |
return GSS_S_FAILURE; |
| 1386 |
} |
| 1387 |
free_Checksum(&cksum); |
| 1388 |
} |
| 1389 |
|
| 1390 |
if (conf_state != NULL) { |
| 1391 |
*conf_state = conf_req_flag; |
| 1392 |
} |
| 1393 |
|
| 1394 |
*minor_status = 0; |
| 1395 |
return GSS_S_COMPLETE; |
| 1396 |
} |
| 1397 |
|
| 1398 |
OM_uint32 _gssapi_unwrap_cfx(OM_uint32 *minor_status, |
| 1399 |
const gsskrb5_ctx ctx, |
| 1400 |
krb5_context context, |
| 1401 |
const gss_buffer_t input_message_buffer, |
| 1402 |
gss_buffer_t output_message_buffer, |
| 1403 |
int *conf_state, |
| 1404 |
gss_qop_t *qop_state) |
| 1405 |
{ |
| 1406 |
gss_cfx_wrap_token token; |
| 1407 |
u_char token_flags; |
| 1408 |
krb5_error_code ret; |
| 1409 |
unsigned usage; |
| 1410 |
krb5_data data; |
| 1411 |
uint16_t ec, rrc; |
| 1412 |
OM_uint32 seq_number_lo, seq_number_hi; |
| 1413 |
size_t len; |
| 1414 |
u_char *p; |
| 1415 |
|
| 1416 |
*minor_status = 0; |
| 1417 |
|
| 1418 |
if (input_message_buffer->length < sizeof(*token)) { |
| 1419 |
return GSS_S_DEFECTIVE_TOKEN; |
| 1420 |
} |
| 1421 |
|
| 1422 |
p = input_message_buffer->value; |
| 1423 |
|
| 1424 |
token = (gss_cfx_wrap_token)p; |
| 1425 |
|
| 1426 |
if (token->TOK_ID[0] != 0x05 || token->TOK_ID[1] != 0x04) { |
| 1427 |
return GSS_S_DEFECTIVE_TOKEN; |
| 1428 |
} |
| 1429 |
|
| 1430 |
/* Ignore unknown flags */ |
| 1431 |
token_flags = token->Flags & |
| 1432 |
(CFXSentByAcceptor | CFXSealed | CFXAcceptorSubkey); |
| 1433 |
|
| 1434 |
if (token_flags & CFXSentByAcceptor) { |
| 1435 |
if ((ctx->more_flags & LOCAL) == 0) |
| 1436 |
return GSS_S_DEFECTIVE_TOKEN; |
| 1437 |
} |
| 1438 |
|
| 1439 |
if (ctx->more_flags & ACCEPTOR_SUBKEY) { |
| 1440 |
if ((token_flags & CFXAcceptorSubkey) == 0) |
| 1441 |
return GSS_S_DEFECTIVE_TOKEN; |
| 1442 |
} else { |
| 1443 |
if (token_flags & CFXAcceptorSubkey) |
| 1444 |
return GSS_S_DEFECTIVE_TOKEN; |
| 1445 |
} |
| 1446 |
|
| 1447 |
if (token->Filler != 0xFF) { |
| 1448 |
return GSS_S_DEFECTIVE_TOKEN; |
| 1449 |
} |
| 1450 |
|
| 1451 |
if (conf_state != NULL) { |
| 1452 |
*conf_state = (token_flags & CFXSealed) ? 1 : 0; |
| 1453 |
} |
| 1454 |
|
| 1455 |
ec = (token->EC[0] << 8) | token->EC[1]; |
| 1456 |
rrc = (token->RRC[0] << 8) | token->RRC[1]; |
| 1457 |
|
| 1458 |
/* |
| 1459 |
* Check sequence number |
| 1460 |
*/ |
| 1461 |
_gsskrb5_decode_be_om_uint32(&token->SND_SEQ[0], &seq_number_hi); |
| 1462 |
_gsskrb5_decode_be_om_uint32(&token->SND_SEQ[4], &seq_number_lo); |
| 1463 |
if (seq_number_hi) { |
| 1464 |
/* no support for 64-bit sequence numbers */ |
| 1465 |
*minor_status = ERANGE; |
| 1466 |
return GSS_S_UNSEQ_TOKEN; |
| 1467 |
} |
| 1468 |
|
| 1469 |
HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex); |
| 1470 |
ret = _gssapi_msg_order_check(ctx->order, seq_number_lo); |
| 1471 |
if (ret != 0) { |
| 1472 |
*minor_status = 0; |
| 1473 |
HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex); |
| 1474 |
_gsskrb5_release_buffer(minor_status, output_message_buffer); |
| 1475 |
return ret; |
| 1476 |
} |
| 1477 |
HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex); |
| 1478 |
|
| 1479 |
/* |
| 1480 |
* Decrypt and/or verify checksum |
| 1481 |
*/ |
| 1482 |
|
| 1483 |
if (ctx->more_flags & LOCAL) { |
| 1484 |
usage = KRB5_KU_USAGE_ACCEPTOR_SEAL; |
| 1485 |
} else { |
| 1486 |
usage = KRB5_KU_USAGE_INITIATOR_SEAL; |
| 1487 |
} |
| 1488 |
|
| 1489 |
p += sizeof(*token); |
| 1490 |
len = input_message_buffer->length; |
| 1491 |
len -= (p - (u_char *)input_message_buffer->value); |
| 1492 |
|
| 1493 |
if (token_flags & CFXSealed) { |
| 1494 |
/* |
| 1495 |
* this is really ugly, but needed against windows |
| 1496 |
* for DCERPC, as windows rotates by EC+RRC. |
| 1497 |
*/ |
| 1498 |
if (IS_DCE_STYLE(ctx)) { |
| 1499 |
*minor_status = rrc_rotate(p, len, rrc+ec, TRUE); |
| 1500 |
} else { |
| 1501 |
*minor_status = rrc_rotate(p, len, rrc, TRUE); |
| 1502 |
} |
| 1503 |
if (*minor_status != 0) { |
| 1504 |
return GSS_S_FAILURE; |
| 1505 |
} |
| 1506 |
|
| 1507 |
ret = krb5_decrypt(context, ctx->crypto, usage, |
| 1508 |
p, len, &data); |
| 1509 |
if (ret != 0) { |
| 1510 |
*minor_status = ret; |
| 1511 |
return GSS_S_BAD_MIC; |
| 1512 |
} |
| 1513 |
|
| 1514 |
/* Check that there is room for the pad and token header */ |
| 1515 |
if (data.length < ec + sizeof(*token)) { |
| 1516 |
krb5_data_free(&data); |
| 1517 |
return GSS_S_DEFECTIVE_TOKEN; |
| 1518 |
} |
| 1519 |
p = data.data; |
| 1520 |
p += data.length - sizeof(*token); |
| 1521 |
|
| 1522 |
/* RRC is unprotected; don't modify input buffer */ |
| 1523 |
((gss_cfx_wrap_token)p)->RRC[0] = token->RRC[0]; |
| 1524 |
((gss_cfx_wrap_token)p)->RRC[1] = token->RRC[1]; |
| 1525 |
|
| 1526 |
/* Check the integrity of the header */ |
| 1527 |
if (ct_memcmp(p, token, sizeof(*token)) != 0) { |
| 1528 |
krb5_data_free(&data); |
| 1529 |
return GSS_S_BAD_MIC; |
| 1530 |
} |
| 1531 |
|
| 1532 |
output_message_buffer->value = data.data; |
| 1533 |
output_message_buffer->length = data.length - ec - sizeof(*token); |
| 1534 |
} else { |
| 1535 |
Checksum cksum; |
| 1536 |
|
| 1537 |
/* Rotate by RRC; bogus to do this in-place XXX */ |
| 1538 |
*minor_status = rrc_rotate(p, len, rrc, TRUE); |
| 1539 |
if (*minor_status != 0) { |
| 1540 |
return GSS_S_FAILURE; |
| 1541 |
} |
| 1542 |
|
| 1543 |
/* Determine checksum type */ |
| 1544 |
ret = krb5_crypto_get_checksum_type(context, |
| 1545 |
ctx->crypto, |
| 1546 |
&cksum.cksumtype); |
| 1547 |
if (ret != 0) { |
| 1548 |
*minor_status = ret; |
| 1549 |
return GSS_S_FAILURE; |
| 1550 |
} |
| 1551 |
|
| 1552 |
cksum.checksum.length = ec; |
| 1553 |
|
| 1554 |
/* Check we have at least as much data as the checksum */ |
| 1555 |
if (len < cksum.checksum.length) { |
| 1556 |
*minor_status = ERANGE; |
| 1557 |
return GSS_S_BAD_MIC; |
| 1558 |
} |
| 1559 |
|
| 1560 |
/* Length now is of the plaintext only, no checksum */ |
| 1561 |
len -= cksum.checksum.length; |
| 1562 |
cksum.checksum.data = p + len; |
| 1563 |
|
| 1564 |
output_message_buffer->length = len; /* for later */ |
| 1565 |
output_message_buffer->value = malloc(len + sizeof(*token)); |
| 1566 |
if (output_message_buffer->value == NULL) { |
| 1567 |
*minor_status = ENOMEM; |
| 1568 |
return GSS_S_FAILURE; |
| 1569 |
} |
| 1570 |
|
| 1571 |
/* Checksum is over (plaintext-data | "header") */ |
| 1572 |
memcpy(output_message_buffer->value, p, len); |
| 1573 |
memcpy((u_char *)output_message_buffer->value + len, |
| 1574 |
token, sizeof(*token)); |
| 1575 |
|
| 1576 |
/* EC is not included in checksum calculation */ |
| 1577 |
token = (gss_cfx_wrap_token)((u_char *)output_message_buffer->value + |
| 1578 |
len); |
| 1579 |
token->EC[0] = 0; |
| 1580 |
token->EC[1] = 0; |
| 1581 |
token->RRC[0] = 0; |
| 1582 |
token->RRC[1] = 0; |
| 1583 |
|
| 1584 |
ret = krb5_verify_checksum(context, ctx->crypto, |
| 1585 |
usage, |
| 1586 |
output_message_buffer->value, |
| 1587 |
len + sizeof(*token), |
| 1588 |
&cksum); |
| 1589 |
if (ret != 0) { |
| 1590 |
*minor_status = ret; |
| 1591 |
_gsskrb5_release_buffer(minor_status, output_message_buffer); |
| 1592 |
return GSS_S_BAD_MIC; |
| 1593 |
} |
| 1594 |
} |
| 1595 |
|
| 1596 |
if (qop_state != NULL) { |
| 1597 |
*qop_state = GSS_C_QOP_DEFAULT; |
| 1598 |
} |
| 1599 |
|
| 1600 |
*minor_status = 0; |
| 1601 |
return GSS_S_COMPLETE; |
| 1602 |
} |
| 1603 |
|
| 1604 |
OM_uint32 _gssapi_mic_cfx(OM_uint32 *minor_status, |
| 1605 |
const gsskrb5_ctx ctx, |
| 1606 |
krb5_context context, |
| 1607 |
gss_qop_t qop_req, |
| 1608 |
const gss_buffer_t message_buffer, |
| 1609 |
gss_buffer_t message_token) |
| 1610 |
{ |
| 1611 |
gss_cfx_mic_token token; |
| 1612 |
krb5_error_code ret; |
| 1613 |
unsigned usage; |
| 1614 |
Checksum cksum; |
| 1615 |
u_char *buf; |
| 1616 |
size_t len; |
| 1617 |
int32_t seq_number; |
| 1618 |
|
| 1619 |
len = message_buffer->length + sizeof(*token); |
| 1620 |
buf = malloc(len); |
| 1621 |
if (buf == NULL) { |
| 1622 |
*minor_status = ENOMEM; |
| 1623 |
return GSS_S_FAILURE; |
| 1624 |
} |
| 1625 |
|
| 1626 |
memcpy(buf, message_buffer->value, message_buffer->length); |
| 1627 |
|
| 1628 |
token = (gss_cfx_mic_token)(buf + message_buffer->length); |
| 1629 |
token->TOK_ID[0] = 0x04; |
| 1630 |
token->TOK_ID[1] = 0x04; |
| 1631 |
token->Flags = 0; |
| 1632 |
if ((ctx->more_flags & LOCAL) == 0) |
| 1633 |
token->Flags |= CFXSentByAcceptor; |
| 1634 |
if (ctx->more_flags & ACCEPTOR_SUBKEY) |
| 1635 |
token->Flags |= CFXAcceptorSubkey; |
| 1636 |
memset(token->Filler, 0xFF, 5); |
| 1637 |
|
| 1638 |
HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex); |
| 1639 |
krb5_auth_con_getlocalseqnumber(context, |
| 1640 |
ctx->auth_context, |
| 1641 |
&seq_number); |
| 1642 |
_gsskrb5_encode_be_om_uint32(0, &token->SND_SEQ[0]); |
| 1643 |
_gsskrb5_encode_be_om_uint32(seq_number, &token->SND_SEQ[4]); |
| 1644 |
krb5_auth_con_setlocalseqnumber(context, |
| 1645 |
ctx->auth_context, |
| 1646 |
++seq_number); |
| 1647 |
HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex); |
| 1648 |
|
| 1649 |
if (ctx->more_flags & LOCAL) { |
| 1650 |
usage = KRB5_KU_USAGE_INITIATOR_SIGN; |
| 1651 |
} else { |
| 1652 |
usage = KRB5_KU_USAGE_ACCEPTOR_SIGN; |
| 1653 |
} |
| 1654 |
|
| 1655 |
ret = krb5_create_checksum(context, ctx->crypto, |
| 1656 |
usage, 0, buf, len, &cksum); |
| 1657 |
if (ret != 0) { |
| 1658 |
*minor_status = ret; |
| 1659 |
free(buf); |
| 1660 |
return GSS_S_FAILURE; |
| 1661 |
} |
| 1662 |
|
| 1663 |
/* Determine MIC length */ |
| 1664 |
message_token->length = sizeof(*token) + cksum.checksum.length; |
| 1665 |
message_token->value = malloc(message_token->length); |
| 1666 |
if (message_token->value == NULL) { |
| 1667 |
*minor_status = ENOMEM; |
| 1668 |
free_Checksum(&cksum); |
| 1669 |
free(buf); |
| 1670 |
return GSS_S_FAILURE; |
| 1671 |
} |
| 1672 |
|
| 1673 |
/* Token is { "header" | get_mic("header" | plaintext-data) } */ |
| 1674 |
memcpy(message_token->value, token, sizeof(*token)); |
| 1675 |
memcpy((u_char *)message_token->value + sizeof(*token), |
| 1676 |
cksum.checksum.data, cksum.checksum.length); |
| 1677 |
|
| 1678 |
free_Checksum(&cksum); |
| 1679 |
free(buf); |
| 1680 |
|
| 1681 |
*minor_status = 0; |
| 1682 |
return GSS_S_COMPLETE; |
| 1683 |
} |
| 1684 |
|
| 1685 |
OM_uint32 _gssapi_verify_mic_cfx(OM_uint32 *minor_status, |
| 1686 |
const gsskrb5_ctx ctx, |
| 1687 |
krb5_context context, |
| 1688 |
const gss_buffer_t message_buffer, |
| 1689 |
const gss_buffer_t token_buffer, |
| 1690 |
gss_qop_t *qop_state) |
| 1691 |
{ |
| 1692 |
gss_cfx_mic_token token; |
| 1693 |
u_char token_flags; |
| 1694 |
krb5_error_code ret; |
| 1695 |
unsigned usage; |
| 1696 |
OM_uint32 seq_number_lo, seq_number_hi; |
| 1697 |
u_char *buf, *p; |
| 1698 |
Checksum cksum; |
| 1699 |
|
| 1700 |
*minor_status = 0; |
| 1701 |
|
| 1702 |
if (token_buffer->length < sizeof(*token)) { |
| 1703 |
return GSS_S_DEFECTIVE_TOKEN; |
| 1704 |
} |
| 1705 |
|
| 1706 |
p = token_buffer->value; |
| 1707 |
|
| 1708 |
token = (gss_cfx_mic_token)p; |
| 1709 |
|
| 1710 |
if (token->TOK_ID[0] != 0x04 || token->TOK_ID[1] != 0x04) { |
| 1711 |
return GSS_S_DEFECTIVE_TOKEN; |
| 1712 |
} |
| 1713 |
|
| 1714 |
/* Ignore unknown flags */ |
| 1715 |
token_flags = token->Flags & (CFXSentByAcceptor | CFXAcceptorSubkey); |
| 1716 |
|
| 1717 |
if (token_flags & CFXSentByAcceptor) { |
| 1718 |
if ((ctx->more_flags & LOCAL) == 0) |
| 1719 |
return GSS_S_DEFECTIVE_TOKEN; |
| 1720 |
} |
| 1721 |
if (ctx->more_flags & ACCEPTOR_SUBKEY) { |
| 1722 |
if ((token_flags & CFXAcceptorSubkey) == 0) |
| 1723 |
return GSS_S_DEFECTIVE_TOKEN; |
| 1724 |
} else { |
| 1725 |
if (token_flags & CFXAcceptorSubkey) |
| 1726 |
return GSS_S_DEFECTIVE_TOKEN; |
| 1727 |
} |
| 1728 |
|
| 1729 |
if (ct_memcmp(token->Filler, "\xff\xff\xff\xff\xff", 5) != 0) { |
| 1730 |
return GSS_S_DEFECTIVE_TOKEN; |
| 1731 |
} |
| 1732 |
|
| 1733 |
/* |
| 1734 |
* Check sequence number |
| 1735 |
*/ |
| 1736 |
_gsskrb5_decode_be_om_uint32(&token->SND_SEQ[0], &seq_number_hi); |
| 1737 |
_gsskrb5_decode_be_om_uint32(&token->SND_SEQ[4], &seq_number_lo); |
| 1738 |
if (seq_number_hi) { |
| 1739 |
*minor_status = ERANGE; |
| 1740 |
return GSS_S_UNSEQ_TOKEN; |
| 1741 |
} |
| 1742 |
|
| 1743 |
HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex); |
| 1744 |
ret = _gssapi_msg_order_check(ctx->order, seq_number_lo); |
| 1745 |
if (ret != 0) { |
| 1746 |
*minor_status = 0; |
| 1747 |
HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex); |
| 1748 |
return ret; |
| 1749 |
} |
| 1750 |
HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex); |
| 1751 |
|
| 1752 |
/* |
| 1753 |
* Verify checksum |
| 1754 |
*/ |
| 1755 |
ret = krb5_crypto_get_checksum_type(context, ctx->crypto, |
| 1756 |
&cksum.cksumtype); |
| 1757 |
if (ret != 0) { |
| 1758 |
*minor_status = ret; |
| 1759 |
return GSS_S_FAILURE; |
| 1760 |
} |
| 1761 |
|
| 1762 |
cksum.checksum.data = p + sizeof(*token); |
| 1763 |
cksum.checksum.length = token_buffer->length - sizeof(*token); |
| 1764 |
|
| 1765 |
if (ctx->more_flags & LOCAL) { |
| 1766 |
usage = KRB5_KU_USAGE_ACCEPTOR_SIGN; |
| 1767 |
} else { |
| 1768 |
usage = KRB5_KU_USAGE_INITIATOR_SIGN; |
| 1769 |
} |
| 1770 |
|
| 1771 |
buf = malloc(message_buffer->length + sizeof(*token)); |
| 1772 |
if (buf == NULL) { |
| 1773 |
*minor_status = ENOMEM; |
| 1774 |
return GSS_S_FAILURE; |
| 1775 |
} |
| 1776 |
memcpy(buf, message_buffer->value, message_buffer->length); |
| 1777 |
memcpy(buf + message_buffer->length, token, sizeof(*token)); |
| 1778 |
|
| 1779 |
ret = krb5_verify_checksum(context, ctx->crypto, |
| 1780 |
usage, |
| 1781 |
buf, |
| 1782 |
sizeof(*token) + message_buffer->length, |
| 1783 |
&cksum); |
| 1784 |
if (ret != 0) { |
| 1785 |
*minor_status = ret; |
| 1786 |
free(buf); |
| 1787 |
return GSS_S_BAD_MIC; |
| 1788 |
} |
| 1789 |
|
| 1790 |
free(buf); |
| 1791 |
|
| 1792 |
if (qop_state != NULL) { |
| 1793 |
*qop_state = GSS_C_QOP_DEFAULT; |
| 1794 |
} |
| 1795 |
|
| 1796 |
return GSS_S_COMPLETE; |
| 1797 |
} |