*** empty log message ***
[heimdal.git] / lib / gssapi / krb5 / unwrap.c
blobf36bce52b4dcda06d661abc2d7c526cc0fb78be1
1 /*
2 * Copyright (c) 1997, 1998 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "gssapi_locl.h"
36 RCSID("$Id$");
38 OM_uint32
39 gss_krb5_getsomekey(const gss_ctx_id_t context_handle,
40 des_cblock *key)
42 /* XXX this is ugly, and probably incorrect... */
43 krb5_keyblock *skey;
44 krb5_auth_con_getlocalsubkey(gssapi_krb5_context,
45 context_handle->auth_context,
46 &skey);
47 if(skey == NULL)
48 krb5_auth_con_getremotesubkey(gssapi_krb5_context,
49 context_handle->auth_context,
50 &skey);
51 if(skey == NULL)
52 krb5_auth_con_getkey(gssapi_krb5_context,
53 context_handle->auth_context,
54 &skey);
55 if(skey == NULL)
56 return GSS_S_FAILURE;
57 memcpy(key, skey->keyvalue.data, sizeof(*key));
58 krb5_free_keyblock(gssapi_krb5_context, skey);
59 return 0;
62 OM_uint32 gss_unwrap
63 (OM_uint32 * minor_status,
64 const gss_ctx_id_t context_handle,
65 const gss_buffer_t input_message_buffer,
66 gss_buffer_t output_message_buffer,
67 int * conf_state,
68 gss_qop_t * qop_state
71 u_char *p, *pad;
72 size_t len;
73 struct md5 md5;
74 u_char hash[16], seq_data[8];
75 des_key_schedule schedule;
76 des_cblock key;
77 des_cblock zero;
78 int i;
79 int32_t seq_number;
80 size_t padlength;
81 OM_uint32 ret;
82 int cstate;
84 p = input_message_buffer->value;
85 ret = gssapi_krb5_verify_header (&p,
86 input_message_buffer->length,
87 "\x02\x01");
88 if (ret)
89 return ret;
91 if (memcmp (p, "\x00\x00", 2) != 0)
92 return GSS_S_BAD_SIG;
93 p += 2;
94 if (memcmp (p, "\x00\x00", 2) == 0) {
95 cstate = 1;
96 } else if (memcmp (p, "\xFF\xFF", 2) == 0) {
97 cstate = 0;
98 } else
99 return GSS_S_BAD_MIC;
100 p += 2;
101 if(conf_state != NULL)
102 *conf_state = cstate;
103 if (memcmp (p, "\xff\xff", 2) != 0)
104 return GSS_S_DEFECTIVE_TOKEN;
105 p += 2;
106 p += 16;
108 len = p - (u_char *)input_message_buffer->value;
110 if(cstate) {
111 /* decrypt data */
112 gss_krb5_getsomekey(context_handle, &key);
113 for (i = 0; i < sizeof(key); ++i)
114 key[i] ^= 0xf0;
115 des_set_key (&key, schedule);
116 memset (&zero, 0, sizeof(zero));
117 des_cbc_encrypt ((des_cblock *)p,
118 (des_cblock *)p,
119 input_message_buffer->length - len,
120 schedule,
121 &zero,
122 DES_DECRYPT);
124 memset (key, 0, sizeof(key));
125 memset (schedule, 0, sizeof(schedule));
127 /* check pad */
129 pad = (u_char *)input_message_buffer->value + input_message_buffer->length - 1;
130 padlength = *pad;
132 for (i = padlength; i > 0 && *pad == padlength; i--, pad--)
134 if (i != 0)
135 return GSS_S_BAD_MIC;
137 md5_init (&md5);
138 md5_update (&md5, p - 24, 8);
139 md5_update (&md5, p, input_message_buffer->length - len);
140 md5_finito (&md5, hash);
142 memset (&zero, 0, sizeof(zero));
143 gss_krb5_getsomekey(context_handle, &key);
144 des_set_key (&key, schedule);
145 des_cbc_cksum ((des_cblock *)hash,
146 (des_cblock *)hash, sizeof(hash), schedule, &zero);
147 if (memcmp (p - 8, hash, 8) != 0)
148 return GSS_S_BAD_MIC;
150 /* verify sequence number */
152 krb5_auth_getremoteseqnumber (gssapi_krb5_context,
153 context_handle->auth_context,
154 &seq_number);
155 seq_data[0] = (seq_number >> 0) & 0xFF;
156 seq_data[1] = (seq_number >> 8) & 0xFF;
157 seq_data[2] = (seq_number >> 16) & 0xFF;
158 seq_data[3] = (seq_number >> 24) & 0xFF;
159 memset (seq_data + 4,
160 (context_handle->more_flags & LOCAL) ? 0xFF : 0,
163 p -= 16;
164 des_set_key (&key, schedule);
165 des_cbc_encrypt ((des_cblock *)p, (des_cblock *)p, 8,
166 schedule, (des_cblock *)hash, DES_DECRYPT);
168 memset (key, 0, sizeof(key));
169 memset (schedule, 0, sizeof(schedule));
171 if (memcmp (p, seq_data, 8) != 0) {
172 return GSS_S_BAD_MIC;
175 krb5_auth_setremoteseqnumber (gssapi_krb5_context,
176 context_handle->auth_context,
177 ++seq_number);
179 /* copy out data */
181 output_message_buffer->length = input_message_buffer->length
182 - len - 8 - padlength;
183 output_message_buffer->value = malloc(output_message_buffer->length);
184 if(output_message_buffer->length != 0 && output_message_buffer->value == NULL)
185 return GSS_S_FAILURE;
186 memcpy (output_message_buffer->value,
187 p + 24,
188 output_message_buffer->length);
189 return GSS_S_COMPLETE;