2 * Copyright (c) 1997 - 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "krb5/gsskrb5_locl.h"
36 RCSID("$Id: wrap.c,v 1.37 2006/10/18 15:59:33 lha Exp $");
39 * Return initiator subkey, or if that doesn't exists, the subkey.
43 _gsskrb5i_get_initiator_subkey(const gsskrb5_ctx ctx, krb5_keyblock **key)
48 if (ctx->more_flags & LOCAL) {
49 ret = krb5_auth_con_getlocalsubkey(_gsskrb5_context,
53 ret = krb5_auth_con_getremotesubkey(_gsskrb5_context,
58 ret = krb5_auth_con_getkey(_gsskrb5_context,
62 _gsskrb5_set_status("No initiator subkey available");
63 return GSS_KRB5_S_KG_NO_SUBKEY;
69 _gsskrb5i_get_acceptor_subkey(const gsskrb5_ctx ctx, krb5_keyblock **key)
74 if (ctx->more_flags & LOCAL) {
75 ret = krb5_auth_con_getremotesubkey(_gsskrb5_context,
79 ret = krb5_auth_con_getlocalsubkey(_gsskrb5_context,
84 _gsskrb5_set_status("No acceptor subkey available");
85 return GSS_KRB5_S_KG_NO_SUBKEY;
91 _gsskrb5i_get_token_key(const gsskrb5_ctx ctx, krb5_keyblock **key)
93 _gsskrb5i_get_acceptor_subkey(ctx, key);
96 * Only use the initiator subkey or ticket session key if an
97 * acceptor subkey was not required.
99 if ((ctx->more_flags & ACCEPTOR_SUBKEY) == 0)
100 _gsskrb5i_get_initiator_subkey(ctx, key);
103 _gsskrb5_set_status("No token key available");
104 return GSS_KRB5_S_KG_NO_SUBKEY;
106 _gsskrb5_clear_status();
112 OM_uint32 req_output_size,
113 OM_uint32 * max_input_size,
118 size_t len, total_len;
120 len = 8 + req_output_size + blocksize + extrasize;
122 _gsskrb5_encap_length(len, &len, &total_len, GSS_KRB5_MECHANISM);
124 total_len -= req_output_size; /* token length */
125 if (total_len < req_output_size) {
126 *max_input_size = (req_output_size - total_len);
127 (*max_input_size) &= (~(OM_uint32)(blocksize - 1));
131 return GSS_S_COMPLETE;
135 _gsskrb5_wrap_size_limit (
136 OM_uint32 * minor_status,
137 const gss_ctx_id_t context_handle,
140 OM_uint32 req_output_size,
141 OM_uint32 * max_input_size
146 krb5_keytype keytype;
147 const gsskrb5_ctx ctx = (const gsskrb5_ctx) context_handle;
149 HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
150 ret = _gsskrb5i_get_token_key(ctx, &key);
151 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
153 _gsskrb5_set_error_string ();
155 return GSS_S_FAILURE;
157 krb5_enctype_to_keytype (_gsskrb5_context, key->keytype, &keytype);
161 ret = sub_wrap_size(req_output_size, max_input_size, 8, 22);
163 case KEYTYPE_ARCFOUR:
164 case KEYTYPE_ARCFOUR_56:
165 ret = _gssapi_wrap_size_arcfour(minor_status, ctx,
166 conf_req_flag, qop_req,
167 req_output_size, max_input_size, key);
170 ret = sub_wrap_size(req_output_size, max_input_size, 8, 34);
173 ret = _gssapi_wrap_size_cfx(minor_status, ctx,
174 conf_req_flag, qop_req,
175 req_output_size, max_input_size, key);
178 krb5_free_keyblock (_gsskrb5_context, key);
185 (OM_uint32 * minor_status,
186 const gsskrb5_ctx ctx,
189 const gss_buffer_t input_message_buffer,
191 gss_buffer_t output_message_buffer,
198 DES_key_schedule schedule;
203 size_t len, total_len, padlength, datalen;
205 padlength = 8 - (input_message_buffer->length % 8);
206 datalen = input_message_buffer->length + padlength + 8;
208 _gsskrb5_encap_length (len, &len, &total_len, GSS_KRB5_MECHANISM);
210 output_message_buffer->length = total_len;
211 output_message_buffer->value = malloc (total_len);
212 if (output_message_buffer->value == NULL) {
213 output_message_buffer->length = 0;
214 *minor_status = ENOMEM;
215 return GSS_S_FAILURE;
218 p = _gsskrb5_make_header(output_message_buffer->value,
220 "\x02\x01", /* TOK_ID */
224 memcpy (p, "\x00\x00", 2);
228 memcpy (p, "\x00\x00", 2);
230 memcpy (p, "\xff\xff", 2);
233 memcpy (p, "\xff\xff", 2);
240 /* confounder + data + pad */
241 krb5_generate_random_block(p, 8);
242 memcpy (p + 8, input_message_buffer->value,
243 input_message_buffer->length);
244 memset (p + 8 + input_message_buffer->length, padlength, padlength);
248 MD5_Update (&md5, p - 24, 8);
249 MD5_Update (&md5, p, datalen);
250 MD5_Final (hash, &md5);
252 memset (&zero, 0, sizeof(zero));
253 memcpy (&deskey, key->keyvalue.data, sizeof(deskey));
254 DES_set_key (&deskey, &schedule);
255 DES_cbc_cksum ((void *)hash, (void *)hash, sizeof(hash),
257 memcpy (p - 8, hash, 8);
259 /* sequence number */
260 HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
261 krb5_auth_con_getlocalseqnumber (_gsskrb5_context,
266 p[0] = (seq_number >> 0) & 0xFF;
267 p[1] = (seq_number >> 8) & 0xFF;
268 p[2] = (seq_number >> 16) & 0xFF;
269 p[3] = (seq_number >> 24) & 0xFF;
271 (ctx->more_flags & LOCAL) ? 0 : 0xFF,
274 DES_set_key (&deskey, &schedule);
275 DES_cbc_encrypt ((void *)p, (void *)p, 8,
276 &schedule, (DES_cblock *)(p + 8), DES_ENCRYPT);
278 krb5_auth_con_setlocalseqnumber (_gsskrb5_context,
281 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
283 /* encrypt the data */
287 memcpy (&deskey, key->keyvalue.data, sizeof(deskey));
289 for (i = 0; i < sizeof(deskey); ++i)
291 DES_set_key (&deskey, &schedule);
292 memset (&zero, 0, sizeof(zero));
293 DES_cbc_encrypt ((void *)p,
300 memset (deskey, 0, sizeof(deskey));
301 memset (&schedule, 0, sizeof(schedule));
303 if(conf_state != NULL)
304 *conf_state = conf_req_flag;
306 return GSS_S_COMPLETE;
311 (OM_uint32 * minor_status,
312 const gsskrb5_ctx ctx,
315 const gss_buffer_t input_message_buffer,
317 gss_buffer_t output_message_buffer,
324 size_t len, total_len, padlength, datalen;
330 padlength = 8 - (input_message_buffer->length % 8);
331 datalen = input_message_buffer->length + padlength + 8;
333 _gsskrb5_encap_length (len, &len, &total_len, GSS_KRB5_MECHANISM);
335 output_message_buffer->length = total_len;
336 output_message_buffer->value = malloc (total_len);
337 if (output_message_buffer->value == NULL) {
338 output_message_buffer->length = 0;
339 *minor_status = ENOMEM;
340 return GSS_S_FAILURE;
343 p = _gsskrb5_make_header(output_message_buffer->value,
345 "\x02\x01", /* TOK_ID */
349 memcpy (p, "\x04\x00", 2); /* HMAC SHA1 DES3-KD */
353 memcpy (p, "\x02\x00", 2); /* DES3-KD */
355 memcpy (p, "\xff\xff", 2);
358 memcpy (p, "\xff\xff", 2);
361 /* calculate checksum (the above + confounder + data + pad) */
363 memcpy (p + 20, p - 8, 8);
364 krb5_generate_random_block(p + 28, 8);
365 memcpy (p + 28 + 8, input_message_buffer->value,
366 input_message_buffer->length);
367 memset (p + 28 + 8 + input_message_buffer->length, padlength, padlength);
369 ret = krb5_crypto_init(_gsskrb5_context, key, 0, &crypto);
371 _gsskrb5_set_error_string ();
372 free (output_message_buffer->value);
373 output_message_buffer->length = 0;
374 output_message_buffer->value = NULL;
376 return GSS_S_FAILURE;
379 ret = krb5_create_checksum (_gsskrb5_context,
386 krb5_crypto_destroy (_gsskrb5_context, crypto);
388 _gsskrb5_set_error_string ();
389 free (output_message_buffer->value);
390 output_message_buffer->length = 0;
391 output_message_buffer->value = NULL;
393 return GSS_S_FAILURE;
396 /* zero out SND_SEQ + SGN_CKSUM in case */
399 memcpy (p + 8, cksum.checksum.data, cksum.checksum.length);
400 free_Checksum (&cksum);
402 HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
403 /* sequence number */
404 krb5_auth_con_getlocalseqnumber (_gsskrb5_context,
408 seq[0] = (seq_number >> 0) & 0xFF;
409 seq[1] = (seq_number >> 8) & 0xFF;
410 seq[2] = (seq_number >> 16) & 0xFF;
411 seq[3] = (seq_number >> 24) & 0xFF;
413 (ctx->more_flags & LOCAL) ? 0 : 0xFF,
417 ret = krb5_crypto_init(_gsskrb5_context, key, ETYPE_DES3_CBC_NONE,
420 free (output_message_buffer->value);
421 output_message_buffer->length = 0;
422 output_message_buffer->value = NULL;
424 return GSS_S_FAILURE;
430 memcpy (&ivec, p + 8, 8);
431 ret = krb5_encrypt_ivec (_gsskrb5_context,
437 krb5_crypto_destroy (_gsskrb5_context, crypto);
439 _gsskrb5_set_error_string ();
440 free (output_message_buffer->value);
441 output_message_buffer->length = 0;
442 output_message_buffer->value = NULL;
444 return GSS_S_FAILURE;
447 assert (encdata.length == 8);
449 memcpy (p, encdata.data, encdata.length);
450 krb5_data_free (&encdata);
452 krb5_auth_con_setlocalseqnumber (_gsskrb5_context,
455 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
457 /* encrypt the data */
463 ret = krb5_crypto_init(_gsskrb5_context, key,
464 ETYPE_DES3_CBC_NONE, &crypto);
466 _gsskrb5_set_error_string ();
467 free (output_message_buffer->value);
468 output_message_buffer->length = 0;
469 output_message_buffer->value = NULL;
471 return GSS_S_FAILURE;
473 ret = krb5_encrypt(_gsskrb5_context, crypto, KRB5_KU_USAGE_SEAL,
475 krb5_crypto_destroy(_gsskrb5_context, crypto);
477 _gsskrb5_set_error_string ();
478 free (output_message_buffer->value);
479 output_message_buffer->length = 0;
480 output_message_buffer->value = NULL;
482 return GSS_S_FAILURE;
484 assert (tmp.length == datalen);
486 memcpy (p, tmp.data, datalen);
487 krb5_data_free(&tmp);
489 if(conf_state != NULL)
490 *conf_state = conf_req_flag;
492 return GSS_S_COMPLETE;
495 OM_uint32 _gsskrb5_wrap
496 (OM_uint32 * minor_status,
497 const gss_ctx_id_t context_handle,
500 const gss_buffer_t input_message_buffer,
502 gss_buffer_t output_message_buffer
507 krb5_keytype keytype;
508 const gsskrb5_ctx ctx = (const gsskrb5_ctx) context_handle;
510 HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
511 ret = _gsskrb5i_get_token_key(ctx, &key);
512 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
514 _gsskrb5_set_error_string ();
516 return GSS_S_FAILURE;
518 krb5_enctype_to_keytype (_gsskrb5_context, key->keytype, &keytype);
522 ret = wrap_des (minor_status, ctx, conf_req_flag,
523 qop_req, input_message_buffer, conf_state,
524 output_message_buffer, key);
527 ret = wrap_des3 (minor_status, ctx, conf_req_flag,
528 qop_req, input_message_buffer, conf_state,
529 output_message_buffer, key);
531 case KEYTYPE_ARCFOUR:
532 case KEYTYPE_ARCFOUR_56:
533 ret = _gssapi_wrap_arcfour (minor_status, ctx, conf_req_flag,
534 qop_req, input_message_buffer, conf_state,
535 output_message_buffer, key);
538 ret = _gssapi_wrap_cfx (minor_status, ctx, conf_req_flag,
539 qop_req, input_message_buffer, conf_state,
540 output_message_buffer, key);
543 krb5_free_keyblock (_gsskrb5_context, key);