xref: /minix3/crypto/external/bsd/heimdal/dist/lib/gssapi/krb5/wrap.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: wrap.c,v 1.1.1.2 2014/04/24 12:45:29 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 1997 - 2003 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc  *
19ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc  *    without specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34ebfedea0SLionel Sambuc  */
35ebfedea0SLionel Sambuc 
36ebfedea0SLionel Sambuc #include "gsskrb5_locl.h"
37ebfedea0SLionel Sambuc 
38ebfedea0SLionel Sambuc /*
39ebfedea0SLionel Sambuc  * Return initiator subkey, or if that doesn't exists, the subkey.
40ebfedea0SLionel Sambuc  */
41ebfedea0SLionel Sambuc 
42ebfedea0SLionel Sambuc krb5_error_code
_gsskrb5i_get_initiator_subkey(const gsskrb5_ctx ctx,krb5_context context,krb5_keyblock ** key)43ebfedea0SLionel Sambuc _gsskrb5i_get_initiator_subkey(const gsskrb5_ctx ctx,
44ebfedea0SLionel Sambuc 			       krb5_context context,
45ebfedea0SLionel Sambuc 			       krb5_keyblock **key)
46ebfedea0SLionel Sambuc {
47ebfedea0SLionel Sambuc     krb5_error_code ret;
48ebfedea0SLionel Sambuc     *key = NULL;
49ebfedea0SLionel Sambuc 
50ebfedea0SLionel Sambuc     if (ctx->more_flags & LOCAL) {
51ebfedea0SLionel Sambuc 	ret = krb5_auth_con_getlocalsubkey(context,
52ebfedea0SLionel Sambuc 				     ctx->auth_context,
53ebfedea0SLionel Sambuc 				     key);
54ebfedea0SLionel Sambuc     } else {
55ebfedea0SLionel Sambuc 	ret = krb5_auth_con_getremotesubkey(context,
56ebfedea0SLionel Sambuc 				      ctx->auth_context,
57ebfedea0SLionel Sambuc 				      key);
58ebfedea0SLionel Sambuc     }
59ebfedea0SLionel Sambuc     if (ret == 0 && *key == NULL)
60ebfedea0SLionel Sambuc 	ret = krb5_auth_con_getkey(context,
61ebfedea0SLionel Sambuc 				   ctx->auth_context,
62ebfedea0SLionel Sambuc 				   key);
63ebfedea0SLionel Sambuc     if (ret == 0 && *key == NULL) {
64ebfedea0SLionel Sambuc 	krb5_set_error_message(context, 0, "No initiator subkey available");
65ebfedea0SLionel Sambuc 	return GSS_KRB5_S_KG_NO_SUBKEY;
66ebfedea0SLionel Sambuc     }
67ebfedea0SLionel Sambuc     return ret;
68ebfedea0SLionel Sambuc }
69ebfedea0SLionel Sambuc 
70ebfedea0SLionel Sambuc krb5_error_code
_gsskrb5i_get_acceptor_subkey(const gsskrb5_ctx ctx,krb5_context context,krb5_keyblock ** key)71ebfedea0SLionel Sambuc _gsskrb5i_get_acceptor_subkey(const gsskrb5_ctx ctx,
72ebfedea0SLionel Sambuc 			      krb5_context context,
73ebfedea0SLionel Sambuc 			      krb5_keyblock **key)
74ebfedea0SLionel Sambuc {
75ebfedea0SLionel Sambuc     krb5_error_code ret;
76ebfedea0SLionel Sambuc     *key = NULL;
77ebfedea0SLionel Sambuc 
78ebfedea0SLionel Sambuc     if (ctx->more_flags & LOCAL) {
79ebfedea0SLionel Sambuc 	ret = krb5_auth_con_getremotesubkey(context,
80ebfedea0SLionel Sambuc 				      ctx->auth_context,
81ebfedea0SLionel Sambuc 				      key);
82ebfedea0SLionel Sambuc     } else {
83ebfedea0SLionel Sambuc 	ret = krb5_auth_con_getlocalsubkey(context,
84ebfedea0SLionel Sambuc 				     ctx->auth_context,
85ebfedea0SLionel Sambuc 				     key);
86ebfedea0SLionel Sambuc     }
87ebfedea0SLionel Sambuc     if (ret == 0 && *key == NULL) {
88ebfedea0SLionel Sambuc 	krb5_set_error_message(context, 0, "No acceptor subkey available");
89ebfedea0SLionel Sambuc 	return GSS_KRB5_S_KG_NO_SUBKEY;
90ebfedea0SLionel Sambuc     }
91ebfedea0SLionel Sambuc     return ret;
92ebfedea0SLionel Sambuc }
93ebfedea0SLionel Sambuc 
94ebfedea0SLionel Sambuc OM_uint32
_gsskrb5i_get_token_key(const gsskrb5_ctx ctx,krb5_context context,krb5_keyblock ** key)95ebfedea0SLionel Sambuc _gsskrb5i_get_token_key(const gsskrb5_ctx ctx,
96ebfedea0SLionel Sambuc 			krb5_context context,
97ebfedea0SLionel Sambuc 			krb5_keyblock **key)
98ebfedea0SLionel Sambuc {
99ebfedea0SLionel Sambuc     _gsskrb5i_get_acceptor_subkey(ctx, context, key);
100ebfedea0SLionel Sambuc     if(*key == NULL) {
101ebfedea0SLionel Sambuc 	/*
102ebfedea0SLionel Sambuc 	 * Only use the initiator subkey or ticket session key if an
103ebfedea0SLionel Sambuc 	 * acceptor subkey was not required.
104ebfedea0SLionel Sambuc 	 */
105ebfedea0SLionel Sambuc 	if ((ctx->more_flags & ACCEPTOR_SUBKEY) == 0)
106ebfedea0SLionel Sambuc 	    _gsskrb5i_get_initiator_subkey(ctx, context, key);
107ebfedea0SLionel Sambuc     }
108ebfedea0SLionel Sambuc     if (*key == NULL) {
109ebfedea0SLionel Sambuc 	krb5_set_error_message(context, 0, "No token key available");
110ebfedea0SLionel Sambuc 	return GSS_KRB5_S_KG_NO_SUBKEY;
111ebfedea0SLionel Sambuc     }
112ebfedea0SLionel Sambuc     return 0;
113ebfedea0SLionel Sambuc }
114ebfedea0SLionel Sambuc 
115ebfedea0SLionel Sambuc static OM_uint32
sub_wrap_size(OM_uint32 req_output_size,OM_uint32 * max_input_size,int blocksize,int extrasize)116ebfedea0SLionel Sambuc sub_wrap_size (
117ebfedea0SLionel Sambuc             OM_uint32 req_output_size,
118ebfedea0SLionel Sambuc             OM_uint32 * max_input_size,
119ebfedea0SLionel Sambuc 	    int blocksize,
120ebfedea0SLionel Sambuc 	    int extrasize
121ebfedea0SLionel Sambuc            )
122ebfedea0SLionel Sambuc {
123ebfedea0SLionel Sambuc     size_t len, total_len;
124ebfedea0SLionel Sambuc 
125ebfedea0SLionel Sambuc     len = 8 + req_output_size + blocksize + extrasize;
126ebfedea0SLionel Sambuc 
127ebfedea0SLionel Sambuc     _gsskrb5_encap_length(len, &len, &total_len, GSS_KRB5_MECHANISM);
128ebfedea0SLionel Sambuc 
129ebfedea0SLionel Sambuc     total_len -= req_output_size; /* token length */
130ebfedea0SLionel Sambuc     if (total_len < req_output_size) {
131ebfedea0SLionel Sambuc         *max_input_size = (req_output_size - total_len);
132ebfedea0SLionel Sambuc         (*max_input_size) &= (~(OM_uint32)(blocksize - 1));
133ebfedea0SLionel Sambuc     } else {
134ebfedea0SLionel Sambuc         *max_input_size = 0;
135ebfedea0SLionel Sambuc     }
136ebfedea0SLionel Sambuc     return GSS_S_COMPLETE;
137ebfedea0SLionel Sambuc }
138ebfedea0SLionel Sambuc 
139ebfedea0SLionel Sambuc OM_uint32 GSSAPI_CALLCONV
_gsskrb5_wrap_size_limit(OM_uint32 * minor_status,const gss_ctx_id_t context_handle,int conf_req_flag,gss_qop_t qop_req,OM_uint32 req_output_size,OM_uint32 * max_input_size)140ebfedea0SLionel Sambuc _gsskrb5_wrap_size_limit (
141ebfedea0SLionel Sambuc             OM_uint32 * minor_status,
142ebfedea0SLionel Sambuc             const gss_ctx_id_t context_handle,
143ebfedea0SLionel Sambuc             int conf_req_flag,
144ebfedea0SLionel Sambuc             gss_qop_t qop_req,
145ebfedea0SLionel Sambuc             OM_uint32 req_output_size,
146ebfedea0SLionel Sambuc             OM_uint32 * max_input_size
147ebfedea0SLionel Sambuc            )
148ebfedea0SLionel Sambuc {
149ebfedea0SLionel Sambuc   krb5_context context;
150ebfedea0SLionel Sambuc   krb5_keyblock *key;
151ebfedea0SLionel Sambuc   OM_uint32 ret;
152ebfedea0SLionel Sambuc   krb5_keytype keytype;
153ebfedea0SLionel Sambuc   const gsskrb5_ctx ctx = (const gsskrb5_ctx) context_handle;
154ebfedea0SLionel Sambuc 
155ebfedea0SLionel Sambuc   GSSAPI_KRB5_INIT (&context);
156ebfedea0SLionel Sambuc 
157ebfedea0SLionel Sambuc   if (ctx->more_flags & IS_CFX)
158ebfedea0SLionel Sambuc       return _gssapi_wrap_size_cfx(minor_status, ctx, context,
159ebfedea0SLionel Sambuc 				   conf_req_flag, qop_req,
160ebfedea0SLionel Sambuc 				   req_output_size, max_input_size);
161ebfedea0SLionel Sambuc 
162ebfedea0SLionel Sambuc   HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
163ebfedea0SLionel Sambuc   ret = _gsskrb5i_get_token_key(ctx, context, &key);
164ebfedea0SLionel Sambuc   HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
165ebfedea0SLionel Sambuc   if (ret) {
166ebfedea0SLionel Sambuc       *minor_status = ret;
167ebfedea0SLionel Sambuc       return GSS_S_FAILURE;
168ebfedea0SLionel Sambuc   }
169ebfedea0SLionel Sambuc   krb5_enctype_to_keytype (context, key->keytype, &keytype);
170ebfedea0SLionel Sambuc 
171ebfedea0SLionel Sambuc   switch (keytype) {
172ebfedea0SLionel Sambuc   case KEYTYPE_DES :
173ebfedea0SLionel Sambuc #ifdef HEIM_WEAK_CRYPTO
174ebfedea0SLionel Sambuc       ret = sub_wrap_size(req_output_size, max_input_size, 8, 22);
175ebfedea0SLionel Sambuc #else
176ebfedea0SLionel Sambuc       ret = GSS_S_FAILURE;
177ebfedea0SLionel Sambuc #endif
178ebfedea0SLionel Sambuc       break;
179*0a6a1f1dSLionel Sambuc   case ENCTYPE_ARCFOUR_HMAC_MD5:
180*0a6a1f1dSLionel Sambuc   case ENCTYPE_ARCFOUR_HMAC_MD5_56:
181ebfedea0SLionel Sambuc       ret = _gssapi_wrap_size_arcfour(minor_status, ctx, context,
182ebfedea0SLionel Sambuc 				      conf_req_flag, qop_req,
183ebfedea0SLionel Sambuc 				      req_output_size, max_input_size, key);
184ebfedea0SLionel Sambuc       break;
185ebfedea0SLionel Sambuc   case KEYTYPE_DES3 :
186ebfedea0SLionel Sambuc       ret = sub_wrap_size(req_output_size, max_input_size, 8, 34);
187ebfedea0SLionel Sambuc       break;
188ebfedea0SLionel Sambuc   default :
189ebfedea0SLionel Sambuc       abort();
190ebfedea0SLionel Sambuc       break;
191ebfedea0SLionel Sambuc   }
192ebfedea0SLionel Sambuc   krb5_free_keyblock (context, key);
193ebfedea0SLionel Sambuc   *minor_status = 0;
194ebfedea0SLionel Sambuc   return ret;
195ebfedea0SLionel Sambuc }
196ebfedea0SLionel Sambuc 
197ebfedea0SLionel Sambuc #ifdef HEIM_WEAK_CRYPTO
198ebfedea0SLionel Sambuc 
199ebfedea0SLionel Sambuc static OM_uint32
wrap_des(OM_uint32 * minor_status,const gsskrb5_ctx ctx,krb5_context context,int conf_req_flag,gss_qop_t qop_req,const gss_buffer_t input_message_buffer,int * conf_state,gss_buffer_t output_message_buffer,krb5_keyblock * key)200ebfedea0SLionel Sambuc wrap_des
201ebfedea0SLionel Sambuc            (OM_uint32 * minor_status,
202ebfedea0SLionel Sambuc             const gsskrb5_ctx ctx,
203ebfedea0SLionel Sambuc 	    krb5_context context,
204ebfedea0SLionel Sambuc             int conf_req_flag,
205ebfedea0SLionel Sambuc             gss_qop_t qop_req,
206ebfedea0SLionel Sambuc             const gss_buffer_t input_message_buffer,
207ebfedea0SLionel Sambuc             int * conf_state,
208ebfedea0SLionel Sambuc             gss_buffer_t output_message_buffer,
209ebfedea0SLionel Sambuc 	    krb5_keyblock *key
210ebfedea0SLionel Sambuc            )
211ebfedea0SLionel Sambuc {
212ebfedea0SLionel Sambuc   u_char *p;
213ebfedea0SLionel Sambuc   EVP_MD_CTX *md5;
214ebfedea0SLionel Sambuc   u_char hash[16];
215ebfedea0SLionel Sambuc   DES_key_schedule schedule;
216ebfedea0SLionel Sambuc   EVP_CIPHER_CTX des_ctx;
217ebfedea0SLionel Sambuc   DES_cblock deskey;
218ebfedea0SLionel Sambuc   DES_cblock zero;
219*0a6a1f1dSLionel Sambuc   size_t i;
220ebfedea0SLionel Sambuc   int32_t seq_number;
221ebfedea0SLionel Sambuc   size_t len, total_len, padlength, datalen;
222ebfedea0SLionel Sambuc 
223ebfedea0SLionel Sambuc   if (IS_DCE_STYLE(ctx)) {
224ebfedea0SLionel Sambuc     padlength = 0;
225ebfedea0SLionel Sambuc     datalen = input_message_buffer->length;
226ebfedea0SLionel Sambuc     len = 22 + 8;
227ebfedea0SLionel Sambuc     _gsskrb5_encap_length (len, &len, &total_len, GSS_KRB5_MECHANISM);
228ebfedea0SLionel Sambuc     total_len += datalen;
229ebfedea0SLionel Sambuc     datalen += 8;
230ebfedea0SLionel Sambuc   } else {
231ebfedea0SLionel Sambuc     padlength = 8 - (input_message_buffer->length % 8);
232ebfedea0SLionel Sambuc     datalen = input_message_buffer->length + padlength + 8;
233ebfedea0SLionel Sambuc     len = datalen + 22;
234ebfedea0SLionel Sambuc     _gsskrb5_encap_length (len, &len, &total_len, GSS_KRB5_MECHANISM);
235ebfedea0SLionel Sambuc   }
236ebfedea0SLionel Sambuc 
237ebfedea0SLionel Sambuc   output_message_buffer->length = total_len;
238ebfedea0SLionel Sambuc   output_message_buffer->value  = malloc (total_len);
239ebfedea0SLionel Sambuc   if (output_message_buffer->value == NULL) {
240ebfedea0SLionel Sambuc     output_message_buffer->length = 0;
241ebfedea0SLionel Sambuc     *minor_status = ENOMEM;
242ebfedea0SLionel Sambuc     return GSS_S_FAILURE;
243ebfedea0SLionel Sambuc   }
244ebfedea0SLionel Sambuc 
245ebfedea0SLionel Sambuc   p = _gsskrb5_make_header(output_message_buffer->value,
246ebfedea0SLionel Sambuc 			      len,
247ebfedea0SLionel Sambuc 			      "\x02\x01", /* TOK_ID */
248ebfedea0SLionel Sambuc 			      GSS_KRB5_MECHANISM);
249ebfedea0SLionel Sambuc 
250ebfedea0SLionel Sambuc   /* SGN_ALG */
251ebfedea0SLionel Sambuc   memcpy (p, "\x00\x00", 2);
252ebfedea0SLionel Sambuc   p += 2;
253ebfedea0SLionel Sambuc   /* SEAL_ALG */
254ebfedea0SLionel Sambuc   if(conf_req_flag)
255ebfedea0SLionel Sambuc       memcpy (p, "\x00\x00", 2);
256ebfedea0SLionel Sambuc   else
257ebfedea0SLionel Sambuc       memcpy (p, "\xff\xff", 2);
258ebfedea0SLionel Sambuc   p += 2;
259ebfedea0SLionel Sambuc   /* Filler */
260ebfedea0SLionel Sambuc   memcpy (p, "\xff\xff", 2);
261ebfedea0SLionel Sambuc   p += 2;
262ebfedea0SLionel Sambuc 
263ebfedea0SLionel Sambuc   /* fill in later */
264ebfedea0SLionel Sambuc   memset (p, 0, 16);
265ebfedea0SLionel Sambuc   p += 16;
266ebfedea0SLionel Sambuc 
267ebfedea0SLionel Sambuc   /* confounder + data + pad */
268ebfedea0SLionel Sambuc   krb5_generate_random_block(p, 8);
269ebfedea0SLionel Sambuc   memcpy (p + 8, input_message_buffer->value,
270ebfedea0SLionel Sambuc 	  input_message_buffer->length);
271ebfedea0SLionel Sambuc   memset (p + 8 + input_message_buffer->length, padlength, padlength);
272ebfedea0SLionel Sambuc 
273ebfedea0SLionel Sambuc   /* checksum */
274ebfedea0SLionel Sambuc   md5 = EVP_MD_CTX_create();
275ebfedea0SLionel Sambuc   EVP_DigestInit_ex(md5, EVP_md5(), NULL);
276ebfedea0SLionel Sambuc   EVP_DigestUpdate(md5, p - 24, 8);
277ebfedea0SLionel Sambuc   EVP_DigestUpdate(md5, p, datalen);
278ebfedea0SLionel Sambuc   EVP_DigestFinal_ex(md5, hash, NULL);
279ebfedea0SLionel Sambuc   EVP_MD_CTX_destroy(md5);
280ebfedea0SLionel Sambuc 
281ebfedea0SLionel Sambuc   memset (&zero, 0, sizeof(zero));
282ebfedea0SLionel Sambuc   memcpy (&deskey, key->keyvalue.data, sizeof(deskey));
283ebfedea0SLionel Sambuc   DES_set_key_unchecked (&deskey, &schedule);
284ebfedea0SLionel Sambuc   DES_cbc_cksum ((void *)hash, (void *)hash, sizeof(hash),
285ebfedea0SLionel Sambuc 		 &schedule, &zero);
286ebfedea0SLionel Sambuc   memcpy (p - 8, hash, 8);
287ebfedea0SLionel Sambuc 
288ebfedea0SLionel Sambuc   /* sequence number */
289ebfedea0SLionel Sambuc   HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
290ebfedea0SLionel Sambuc   krb5_auth_con_getlocalseqnumber (context,
291ebfedea0SLionel Sambuc 				   ctx->auth_context,
292ebfedea0SLionel Sambuc 				   &seq_number);
293ebfedea0SLionel Sambuc 
294ebfedea0SLionel Sambuc   p -= 16;
295ebfedea0SLionel Sambuc   p[0] = (seq_number >> 0)  & 0xFF;
296ebfedea0SLionel Sambuc   p[1] = (seq_number >> 8)  & 0xFF;
297ebfedea0SLionel Sambuc   p[2] = (seq_number >> 16) & 0xFF;
298ebfedea0SLionel Sambuc   p[3] = (seq_number >> 24) & 0xFF;
299ebfedea0SLionel Sambuc   memset (p + 4,
300ebfedea0SLionel Sambuc 	  (ctx->more_flags & LOCAL) ? 0 : 0xFF,
301ebfedea0SLionel Sambuc 	  4);
302ebfedea0SLionel Sambuc 
303ebfedea0SLionel Sambuc   EVP_CIPHER_CTX_init(&des_ctx);
304ebfedea0SLionel Sambuc   EVP_CipherInit_ex(&des_ctx, EVP_des_cbc(), NULL, key->keyvalue.data, p + 8, 1);
305ebfedea0SLionel Sambuc   EVP_Cipher(&des_ctx, p, p, 8);
306ebfedea0SLionel Sambuc   EVP_CIPHER_CTX_cleanup(&des_ctx);
307ebfedea0SLionel Sambuc 
308ebfedea0SLionel Sambuc   krb5_auth_con_setlocalseqnumber (context,
309ebfedea0SLionel Sambuc 			       ctx->auth_context,
310ebfedea0SLionel Sambuc 			       ++seq_number);
311ebfedea0SLionel Sambuc   HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
312ebfedea0SLionel Sambuc 
313ebfedea0SLionel Sambuc   /* encrypt the data */
314ebfedea0SLionel Sambuc   p += 16;
315ebfedea0SLionel Sambuc 
316ebfedea0SLionel Sambuc   if(conf_req_flag) {
317ebfedea0SLionel Sambuc       memcpy (&deskey, key->keyvalue.data, sizeof(deskey));
318ebfedea0SLionel Sambuc 
319ebfedea0SLionel Sambuc       for (i = 0; i < sizeof(deskey); ++i)
320ebfedea0SLionel Sambuc 	  deskey[i] ^= 0xf0;
321ebfedea0SLionel Sambuc 
322ebfedea0SLionel Sambuc       EVP_CIPHER_CTX_init(&des_ctx);
323ebfedea0SLionel Sambuc       EVP_CipherInit_ex(&des_ctx, EVP_des_cbc(), NULL, deskey, zero, 1);
324ebfedea0SLionel Sambuc       EVP_Cipher(&des_ctx, p, p, datalen);
325ebfedea0SLionel Sambuc       EVP_CIPHER_CTX_cleanup(&des_ctx);
326ebfedea0SLionel Sambuc   }
327ebfedea0SLionel Sambuc   memset (deskey, 0, sizeof(deskey));
328ebfedea0SLionel Sambuc   memset (&schedule, 0, sizeof(schedule));
329ebfedea0SLionel Sambuc 
330ebfedea0SLionel Sambuc   if(conf_state != NULL)
331ebfedea0SLionel Sambuc       *conf_state = conf_req_flag;
332ebfedea0SLionel Sambuc   *minor_status = 0;
333ebfedea0SLionel Sambuc   return GSS_S_COMPLETE;
334ebfedea0SLionel Sambuc }
335ebfedea0SLionel Sambuc 
336ebfedea0SLionel Sambuc #endif
337ebfedea0SLionel Sambuc 
338ebfedea0SLionel Sambuc static OM_uint32
wrap_des3(OM_uint32 * minor_status,const gsskrb5_ctx ctx,krb5_context context,int conf_req_flag,gss_qop_t qop_req,const gss_buffer_t input_message_buffer,int * conf_state,gss_buffer_t output_message_buffer,krb5_keyblock * key)339ebfedea0SLionel Sambuc wrap_des3
340ebfedea0SLionel Sambuc            (OM_uint32 * minor_status,
341ebfedea0SLionel Sambuc             const gsskrb5_ctx ctx,
342ebfedea0SLionel Sambuc 	    krb5_context context,
343ebfedea0SLionel Sambuc             int conf_req_flag,
344ebfedea0SLionel Sambuc             gss_qop_t qop_req,
345ebfedea0SLionel Sambuc             const gss_buffer_t input_message_buffer,
346ebfedea0SLionel Sambuc             int * conf_state,
347ebfedea0SLionel Sambuc             gss_buffer_t output_message_buffer,
348ebfedea0SLionel Sambuc 	    krb5_keyblock *key
349ebfedea0SLionel Sambuc            )
350ebfedea0SLionel Sambuc {
351ebfedea0SLionel Sambuc   u_char *p;
352ebfedea0SLionel Sambuc   u_char seq[8];
353ebfedea0SLionel Sambuc   int32_t seq_number;
354ebfedea0SLionel Sambuc   size_t len, total_len, padlength, datalen;
355ebfedea0SLionel Sambuc   uint32_t ret;
356ebfedea0SLionel Sambuc   krb5_crypto crypto;
357ebfedea0SLionel Sambuc   Checksum cksum;
358ebfedea0SLionel Sambuc   krb5_data encdata;
359ebfedea0SLionel Sambuc 
360ebfedea0SLionel Sambuc   if (IS_DCE_STYLE(ctx)) {
361ebfedea0SLionel Sambuc     padlength = 0;
362ebfedea0SLionel Sambuc     datalen = input_message_buffer->length;
363ebfedea0SLionel Sambuc     len = 34 + 8;
364ebfedea0SLionel Sambuc     _gsskrb5_encap_length (len, &len, &total_len, GSS_KRB5_MECHANISM);
365ebfedea0SLionel Sambuc     total_len += datalen;
366ebfedea0SLionel Sambuc     datalen += 8;
367ebfedea0SLionel Sambuc   } else {
368ebfedea0SLionel Sambuc     padlength = 8 - (input_message_buffer->length % 8);
369ebfedea0SLionel Sambuc     datalen = input_message_buffer->length + padlength + 8;
370ebfedea0SLionel Sambuc     len = datalen + 34;
371ebfedea0SLionel Sambuc     _gsskrb5_encap_length (len, &len, &total_len, GSS_KRB5_MECHANISM);
372ebfedea0SLionel Sambuc   }
373ebfedea0SLionel Sambuc 
374ebfedea0SLionel Sambuc   output_message_buffer->length = total_len;
375ebfedea0SLionel Sambuc   output_message_buffer->value  = malloc (total_len);
376ebfedea0SLionel Sambuc   if (output_message_buffer->value == NULL) {
377ebfedea0SLionel Sambuc     output_message_buffer->length = 0;
378ebfedea0SLionel Sambuc     *minor_status = ENOMEM;
379ebfedea0SLionel Sambuc     return GSS_S_FAILURE;
380ebfedea0SLionel Sambuc   }
381ebfedea0SLionel Sambuc 
382ebfedea0SLionel Sambuc   p = _gsskrb5_make_header(output_message_buffer->value,
383ebfedea0SLionel Sambuc 			      len,
384ebfedea0SLionel Sambuc 			      "\x02\x01", /* TOK_ID */
385ebfedea0SLionel Sambuc 			      GSS_KRB5_MECHANISM);
386ebfedea0SLionel Sambuc 
387ebfedea0SLionel Sambuc   /* SGN_ALG */
388ebfedea0SLionel Sambuc   memcpy (p, "\x04\x00", 2);	/* HMAC SHA1 DES3-KD */
389ebfedea0SLionel Sambuc   p += 2;
390ebfedea0SLionel Sambuc   /* SEAL_ALG */
391ebfedea0SLionel Sambuc   if(conf_req_flag)
392ebfedea0SLionel Sambuc       memcpy (p, "\x02\x00", 2); /* DES3-KD */
393ebfedea0SLionel Sambuc   else
394ebfedea0SLionel Sambuc       memcpy (p, "\xff\xff", 2);
395ebfedea0SLionel Sambuc   p += 2;
396ebfedea0SLionel Sambuc   /* Filler */
397ebfedea0SLionel Sambuc   memcpy (p, "\xff\xff", 2);
398ebfedea0SLionel Sambuc   p += 2;
399ebfedea0SLionel Sambuc 
400ebfedea0SLionel Sambuc   /* calculate checksum (the above + confounder + data + pad) */
401ebfedea0SLionel Sambuc 
402ebfedea0SLionel Sambuc   memcpy (p + 20, p - 8, 8);
403ebfedea0SLionel Sambuc   krb5_generate_random_block(p + 28, 8);
404ebfedea0SLionel Sambuc   memcpy (p + 28 + 8, input_message_buffer->value,
405ebfedea0SLionel Sambuc 	  input_message_buffer->length);
406ebfedea0SLionel Sambuc   memset (p + 28 + 8 + input_message_buffer->length, padlength, padlength);
407ebfedea0SLionel Sambuc 
408ebfedea0SLionel Sambuc   ret = krb5_crypto_init(context, key, 0, &crypto);
409ebfedea0SLionel Sambuc   if (ret) {
410ebfedea0SLionel Sambuc       free (output_message_buffer->value);
411ebfedea0SLionel Sambuc       output_message_buffer->length = 0;
412ebfedea0SLionel Sambuc       output_message_buffer->value = NULL;
413ebfedea0SLionel Sambuc       *minor_status = ret;
414ebfedea0SLionel Sambuc       return GSS_S_FAILURE;
415ebfedea0SLionel Sambuc   }
416ebfedea0SLionel Sambuc 
417ebfedea0SLionel Sambuc   ret = krb5_create_checksum (context,
418ebfedea0SLionel Sambuc 			      crypto,
419ebfedea0SLionel Sambuc 			      KRB5_KU_USAGE_SIGN,
420ebfedea0SLionel Sambuc 			      0,
421ebfedea0SLionel Sambuc 			      p + 20,
422ebfedea0SLionel Sambuc 			      datalen + 8,
423ebfedea0SLionel Sambuc 			      &cksum);
424ebfedea0SLionel Sambuc   krb5_crypto_destroy (context, crypto);
425ebfedea0SLionel Sambuc   if (ret) {
426ebfedea0SLionel Sambuc       free (output_message_buffer->value);
427ebfedea0SLionel Sambuc       output_message_buffer->length = 0;
428ebfedea0SLionel Sambuc       output_message_buffer->value = NULL;
429ebfedea0SLionel Sambuc       *minor_status = ret;
430ebfedea0SLionel Sambuc       return GSS_S_FAILURE;
431ebfedea0SLionel Sambuc   }
432ebfedea0SLionel Sambuc 
433ebfedea0SLionel Sambuc   /* zero out SND_SEQ + SGN_CKSUM in case */
434ebfedea0SLionel Sambuc   memset (p, 0, 28);
435ebfedea0SLionel Sambuc 
436ebfedea0SLionel Sambuc   memcpy (p + 8, cksum.checksum.data, cksum.checksum.length);
437ebfedea0SLionel Sambuc   free_Checksum (&cksum);
438ebfedea0SLionel Sambuc 
439ebfedea0SLionel Sambuc   HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
440ebfedea0SLionel Sambuc   /* sequence number */
441ebfedea0SLionel Sambuc   krb5_auth_con_getlocalseqnumber (context,
442ebfedea0SLionel Sambuc 			       ctx->auth_context,
443ebfedea0SLionel Sambuc 			       &seq_number);
444ebfedea0SLionel Sambuc 
445ebfedea0SLionel Sambuc   seq[0] = (seq_number >> 0)  & 0xFF;
446ebfedea0SLionel Sambuc   seq[1] = (seq_number >> 8)  & 0xFF;
447ebfedea0SLionel Sambuc   seq[2] = (seq_number >> 16) & 0xFF;
448ebfedea0SLionel Sambuc   seq[3] = (seq_number >> 24) & 0xFF;
449ebfedea0SLionel Sambuc   memset (seq + 4,
450ebfedea0SLionel Sambuc 	  (ctx->more_flags & LOCAL) ? 0 : 0xFF,
451ebfedea0SLionel Sambuc 	  4);
452ebfedea0SLionel Sambuc 
453ebfedea0SLionel Sambuc 
454ebfedea0SLionel Sambuc   ret = krb5_crypto_init(context, key, ETYPE_DES3_CBC_NONE,
455ebfedea0SLionel Sambuc 			 &crypto);
456ebfedea0SLionel Sambuc   if (ret) {
457ebfedea0SLionel Sambuc       free (output_message_buffer->value);
458ebfedea0SLionel Sambuc       output_message_buffer->length = 0;
459ebfedea0SLionel Sambuc       output_message_buffer->value = NULL;
460ebfedea0SLionel Sambuc       *minor_status = ret;
461ebfedea0SLionel Sambuc       return GSS_S_FAILURE;
462ebfedea0SLionel Sambuc   }
463ebfedea0SLionel Sambuc 
464ebfedea0SLionel Sambuc   {
465ebfedea0SLionel Sambuc       DES_cblock ivec;
466ebfedea0SLionel Sambuc 
467ebfedea0SLionel Sambuc       memcpy (&ivec, p + 8, 8);
468ebfedea0SLionel Sambuc       ret = krb5_encrypt_ivec (context,
469ebfedea0SLionel Sambuc 			       crypto,
470ebfedea0SLionel Sambuc 			       KRB5_KU_USAGE_SEQ,
471ebfedea0SLionel Sambuc 			       seq, 8, &encdata,
472ebfedea0SLionel Sambuc 			       &ivec);
473ebfedea0SLionel Sambuc   }
474ebfedea0SLionel Sambuc   krb5_crypto_destroy (context, crypto);
475ebfedea0SLionel Sambuc   if (ret) {
476ebfedea0SLionel Sambuc       free (output_message_buffer->value);
477ebfedea0SLionel Sambuc       output_message_buffer->length = 0;
478ebfedea0SLionel Sambuc       output_message_buffer->value = NULL;
479ebfedea0SLionel Sambuc       *minor_status = ret;
480ebfedea0SLionel Sambuc       return GSS_S_FAILURE;
481ebfedea0SLionel Sambuc   }
482ebfedea0SLionel Sambuc 
483ebfedea0SLionel Sambuc   assert (encdata.length == 8);
484ebfedea0SLionel Sambuc 
485ebfedea0SLionel Sambuc   memcpy (p, encdata.data, encdata.length);
486ebfedea0SLionel Sambuc   krb5_data_free (&encdata);
487ebfedea0SLionel Sambuc 
488ebfedea0SLionel Sambuc   krb5_auth_con_setlocalseqnumber (context,
489ebfedea0SLionel Sambuc 			       ctx->auth_context,
490ebfedea0SLionel Sambuc 			       ++seq_number);
491ebfedea0SLionel Sambuc   HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
492ebfedea0SLionel Sambuc 
493ebfedea0SLionel Sambuc   /* encrypt the data */
494ebfedea0SLionel Sambuc   p += 28;
495ebfedea0SLionel Sambuc 
496ebfedea0SLionel Sambuc   if(conf_req_flag) {
497ebfedea0SLionel Sambuc       krb5_data tmp;
498ebfedea0SLionel Sambuc 
499ebfedea0SLionel Sambuc       ret = krb5_crypto_init(context, key,
500ebfedea0SLionel Sambuc 			     ETYPE_DES3_CBC_NONE, &crypto);
501ebfedea0SLionel Sambuc       if (ret) {
502ebfedea0SLionel Sambuc 	  free (output_message_buffer->value);
503ebfedea0SLionel Sambuc 	  output_message_buffer->length = 0;
504ebfedea0SLionel Sambuc 	  output_message_buffer->value = NULL;
505ebfedea0SLionel Sambuc 	  *minor_status = ret;
506ebfedea0SLionel Sambuc 	  return GSS_S_FAILURE;
507ebfedea0SLionel Sambuc       }
508ebfedea0SLionel Sambuc       ret = krb5_encrypt(context, crypto, KRB5_KU_USAGE_SEAL,
509ebfedea0SLionel Sambuc 			 p, datalen, &tmp);
510ebfedea0SLionel Sambuc       krb5_crypto_destroy(context, crypto);
511ebfedea0SLionel Sambuc       if (ret) {
512ebfedea0SLionel Sambuc 	  free (output_message_buffer->value);
513ebfedea0SLionel Sambuc 	  output_message_buffer->length = 0;
514ebfedea0SLionel Sambuc 	  output_message_buffer->value = NULL;
515ebfedea0SLionel Sambuc 	  *minor_status = ret;
516ebfedea0SLionel Sambuc 	  return GSS_S_FAILURE;
517ebfedea0SLionel Sambuc       }
518ebfedea0SLionel Sambuc       assert (tmp.length == datalen);
519ebfedea0SLionel Sambuc 
520ebfedea0SLionel Sambuc       memcpy (p, tmp.data, datalen);
521ebfedea0SLionel Sambuc       krb5_data_free(&tmp);
522ebfedea0SLionel Sambuc   }
523ebfedea0SLionel Sambuc   if(conf_state != NULL)
524ebfedea0SLionel Sambuc       *conf_state = conf_req_flag;
525ebfedea0SLionel Sambuc   *minor_status = 0;
526ebfedea0SLionel Sambuc   return GSS_S_COMPLETE;
527ebfedea0SLionel Sambuc }
528ebfedea0SLionel Sambuc 
529ebfedea0SLionel Sambuc OM_uint32 GSSAPI_CALLCONV
_gsskrb5_wrap(OM_uint32 * minor_status,const gss_ctx_id_t context_handle,int conf_req_flag,gss_qop_t qop_req,const gss_buffer_t input_message_buffer,int * conf_state,gss_buffer_t output_message_buffer)530ebfedea0SLionel Sambuc _gsskrb5_wrap
531ebfedea0SLionel Sambuc            (OM_uint32 * minor_status,
532ebfedea0SLionel Sambuc             const gss_ctx_id_t context_handle,
533ebfedea0SLionel Sambuc             int conf_req_flag,
534ebfedea0SLionel Sambuc             gss_qop_t qop_req,
535ebfedea0SLionel Sambuc             const gss_buffer_t input_message_buffer,
536ebfedea0SLionel Sambuc             int * conf_state,
537ebfedea0SLionel Sambuc             gss_buffer_t output_message_buffer
538ebfedea0SLionel Sambuc            )
539ebfedea0SLionel Sambuc {
540ebfedea0SLionel Sambuc   krb5_context context;
541ebfedea0SLionel Sambuc   krb5_keyblock *key;
542ebfedea0SLionel Sambuc   OM_uint32 ret;
543ebfedea0SLionel Sambuc   krb5_keytype keytype;
544ebfedea0SLionel Sambuc   const gsskrb5_ctx ctx = (const gsskrb5_ctx) context_handle;
545ebfedea0SLionel Sambuc 
546ebfedea0SLionel Sambuc   output_message_buffer->value = NULL;
547ebfedea0SLionel Sambuc   output_message_buffer->length = 0;
548ebfedea0SLionel Sambuc 
549ebfedea0SLionel Sambuc   GSSAPI_KRB5_INIT (&context);
550ebfedea0SLionel Sambuc 
551ebfedea0SLionel Sambuc   if (ctx->more_flags & IS_CFX)
552ebfedea0SLionel Sambuc       return _gssapi_wrap_cfx (minor_status, ctx, context, conf_req_flag,
553ebfedea0SLionel Sambuc 			       input_message_buffer, conf_state,
554ebfedea0SLionel Sambuc 			       output_message_buffer);
555ebfedea0SLionel Sambuc 
556ebfedea0SLionel Sambuc   HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
557ebfedea0SLionel Sambuc   ret = _gsskrb5i_get_token_key(ctx, context, &key);
558ebfedea0SLionel Sambuc   HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
559ebfedea0SLionel Sambuc   if (ret) {
560ebfedea0SLionel Sambuc       *minor_status = ret;
561ebfedea0SLionel Sambuc       return GSS_S_FAILURE;
562ebfedea0SLionel Sambuc   }
563ebfedea0SLionel Sambuc   krb5_enctype_to_keytype (context, key->keytype, &keytype);
564ebfedea0SLionel Sambuc 
565ebfedea0SLionel Sambuc   switch (keytype) {
566ebfedea0SLionel Sambuc   case KEYTYPE_DES :
567ebfedea0SLionel Sambuc #ifdef HEIM_WEAK_CRYPTO
568ebfedea0SLionel Sambuc       ret = wrap_des (minor_status, ctx, context, conf_req_flag,
569ebfedea0SLionel Sambuc 		      qop_req, input_message_buffer, conf_state,
570ebfedea0SLionel Sambuc 		      output_message_buffer, key);
571ebfedea0SLionel Sambuc #else
572ebfedea0SLionel Sambuc       ret = GSS_S_FAILURE;
573ebfedea0SLionel Sambuc #endif
574ebfedea0SLionel Sambuc       break;
575ebfedea0SLionel Sambuc   case KEYTYPE_DES3 :
576ebfedea0SLionel Sambuc       ret = wrap_des3 (minor_status, ctx, context, conf_req_flag,
577ebfedea0SLionel Sambuc 		       qop_req, input_message_buffer, conf_state,
578ebfedea0SLionel Sambuc 		       output_message_buffer, key);
579ebfedea0SLionel Sambuc       break;
580ebfedea0SLionel Sambuc   case KEYTYPE_ARCFOUR:
581ebfedea0SLionel Sambuc   case KEYTYPE_ARCFOUR_56:
582ebfedea0SLionel Sambuc       ret = _gssapi_wrap_arcfour (minor_status, ctx, context, conf_req_flag,
583ebfedea0SLionel Sambuc 				  qop_req, input_message_buffer, conf_state,
584ebfedea0SLionel Sambuc 				  output_message_buffer, key);
585ebfedea0SLionel Sambuc       break;
586ebfedea0SLionel Sambuc   default :
587ebfedea0SLionel Sambuc       abort();
588ebfedea0SLionel Sambuc       break;
589ebfedea0SLionel Sambuc   }
590ebfedea0SLionel Sambuc   krb5_free_keyblock (context, key);
591ebfedea0SLionel Sambuc   return ret;
592ebfedea0SLionel Sambuc }
593