1*0a6a1f1dSLionel Sambuc /* $NetBSD: crypto.c,v 1.1.1.2 2014/04/24 12:45:29 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 2006 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 "ntlm.h"
37ebfedea0SLionel Sambuc
38ebfedea0SLionel Sambuc uint32_t
39ebfedea0SLionel Sambuc _krb5_crc_update (const char *p, size_t len, uint32_t res);
40ebfedea0SLionel Sambuc void
41ebfedea0SLionel Sambuc _krb5_crc_init_table(void);
42ebfedea0SLionel Sambuc
43ebfedea0SLionel Sambuc /*
44ebfedea0SLionel Sambuc *
45ebfedea0SLionel Sambuc */
46ebfedea0SLionel Sambuc
47ebfedea0SLionel Sambuc static void
encode_le_uint32(uint32_t n,unsigned char * p)48ebfedea0SLionel Sambuc encode_le_uint32(uint32_t n, unsigned char *p)
49ebfedea0SLionel Sambuc {
50ebfedea0SLionel Sambuc p[0] = (n >> 0) & 0xFF;
51ebfedea0SLionel Sambuc p[1] = (n >> 8) & 0xFF;
52ebfedea0SLionel Sambuc p[2] = (n >> 16) & 0xFF;
53ebfedea0SLionel Sambuc p[3] = (n >> 24) & 0xFF;
54ebfedea0SLionel Sambuc }
55ebfedea0SLionel Sambuc
56ebfedea0SLionel Sambuc
57ebfedea0SLionel Sambuc static void
decode_le_uint32(const void * ptr,uint32_t * n)58ebfedea0SLionel Sambuc decode_le_uint32(const void *ptr, uint32_t *n)
59ebfedea0SLionel Sambuc {
60ebfedea0SLionel Sambuc const unsigned char *p = ptr;
61ebfedea0SLionel Sambuc *n = (p[0] << 0) | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
62ebfedea0SLionel Sambuc }
63ebfedea0SLionel Sambuc
64ebfedea0SLionel Sambuc /*
65ebfedea0SLionel Sambuc *
66ebfedea0SLionel Sambuc */
67ebfedea0SLionel Sambuc
68ebfedea0SLionel Sambuc const char a2i_signmagic[] =
69ebfedea0SLionel Sambuc "session key to server-to-client signing key magic constant";
70ebfedea0SLionel Sambuc const char a2i_sealmagic[] =
71ebfedea0SLionel Sambuc "session key to server-to-client sealing key magic constant";
72ebfedea0SLionel Sambuc const char i2a_signmagic[] =
73ebfedea0SLionel Sambuc "session key to client-to-server signing key magic constant";
74ebfedea0SLionel Sambuc const char i2a_sealmagic[] =
75ebfedea0SLionel Sambuc "session key to client-to-server sealing key magic constant";
76ebfedea0SLionel Sambuc
77ebfedea0SLionel Sambuc
78ebfedea0SLionel Sambuc void
_gss_ntlm_set_key(struct ntlmv2_key * key,int acceptor,int sealsign,unsigned char * data,size_t len)79ebfedea0SLionel Sambuc _gss_ntlm_set_key(struct ntlmv2_key *key, int acceptor, int sealsign,
80ebfedea0SLionel Sambuc unsigned char *data, size_t len)
81ebfedea0SLionel Sambuc {
82ebfedea0SLionel Sambuc unsigned char out[16];
83ebfedea0SLionel Sambuc EVP_MD_CTX *ctx;
84ebfedea0SLionel Sambuc const char *signmagic;
85ebfedea0SLionel Sambuc const char *sealmagic;
86ebfedea0SLionel Sambuc
87ebfedea0SLionel Sambuc if (acceptor) {
88ebfedea0SLionel Sambuc signmagic = a2i_signmagic;
89ebfedea0SLionel Sambuc sealmagic = a2i_sealmagic;
90ebfedea0SLionel Sambuc } else {
91ebfedea0SLionel Sambuc signmagic = i2a_signmagic;
92ebfedea0SLionel Sambuc sealmagic = i2a_sealmagic;
93ebfedea0SLionel Sambuc }
94ebfedea0SLionel Sambuc
95ebfedea0SLionel Sambuc key->seq = 0;
96ebfedea0SLionel Sambuc
97ebfedea0SLionel Sambuc ctx = EVP_MD_CTX_create();
98ebfedea0SLionel Sambuc EVP_DigestInit_ex(ctx, EVP_md5(), NULL);
99ebfedea0SLionel Sambuc EVP_DigestUpdate(ctx, data, len);
100ebfedea0SLionel Sambuc EVP_DigestUpdate(ctx, signmagic, strlen(signmagic) + 1);
101ebfedea0SLionel Sambuc EVP_DigestFinal_ex(ctx, key->signkey, NULL);
102ebfedea0SLionel Sambuc
103ebfedea0SLionel Sambuc EVP_DigestInit_ex(ctx, EVP_md5(), NULL);
104ebfedea0SLionel Sambuc EVP_DigestUpdate(ctx, data, len);
105ebfedea0SLionel Sambuc EVP_DigestUpdate(ctx, sealmagic, strlen(sealmagic) + 1);
106ebfedea0SLionel Sambuc EVP_DigestFinal_ex(ctx, out, NULL);
107ebfedea0SLionel Sambuc EVP_MD_CTX_destroy(ctx);
108ebfedea0SLionel Sambuc
109ebfedea0SLionel Sambuc RC4_set_key(&key->sealkey, 16, out);
110ebfedea0SLionel Sambuc if (sealsign)
111ebfedea0SLionel Sambuc key->signsealkey = &key->sealkey;
112ebfedea0SLionel Sambuc }
113ebfedea0SLionel Sambuc
114ebfedea0SLionel Sambuc /*
115ebfedea0SLionel Sambuc *
116ebfedea0SLionel Sambuc */
117ebfedea0SLionel Sambuc
118ebfedea0SLionel Sambuc static OM_uint32
v1_sign_message(gss_buffer_t in,RC4_KEY * signkey,uint32_t seq,unsigned char out[16])119ebfedea0SLionel Sambuc v1_sign_message(gss_buffer_t in,
120ebfedea0SLionel Sambuc RC4_KEY *signkey,
121ebfedea0SLionel Sambuc uint32_t seq,
122ebfedea0SLionel Sambuc unsigned char out[16])
123ebfedea0SLionel Sambuc {
124ebfedea0SLionel Sambuc unsigned char sigature[12];
125ebfedea0SLionel Sambuc uint32_t crc;
126ebfedea0SLionel Sambuc
127ebfedea0SLionel Sambuc _krb5_crc_init_table();
128ebfedea0SLionel Sambuc crc = _krb5_crc_update(in->value, in->length, 0);
129ebfedea0SLionel Sambuc
130ebfedea0SLionel Sambuc encode_le_uint32(0, &sigature[0]);
131ebfedea0SLionel Sambuc encode_le_uint32(crc, &sigature[4]);
132ebfedea0SLionel Sambuc encode_le_uint32(seq, &sigature[8]);
133ebfedea0SLionel Sambuc
134ebfedea0SLionel Sambuc encode_le_uint32(1, out); /* version */
135ebfedea0SLionel Sambuc RC4(signkey, sizeof(sigature), sigature, out + 4);
136ebfedea0SLionel Sambuc
137ebfedea0SLionel Sambuc if (RAND_bytes(out + 4, 4) != 1)
138ebfedea0SLionel Sambuc return GSS_S_UNAVAILABLE;
139ebfedea0SLionel Sambuc
140ebfedea0SLionel Sambuc return 0;
141ebfedea0SLionel Sambuc }
142ebfedea0SLionel Sambuc
143ebfedea0SLionel Sambuc
144ebfedea0SLionel Sambuc static OM_uint32
v2_sign_message(gss_buffer_t in,unsigned char signkey[16],RC4_KEY * sealkey,uint32_t seq,unsigned char out[16])145ebfedea0SLionel Sambuc v2_sign_message(gss_buffer_t in,
146ebfedea0SLionel Sambuc unsigned char signkey[16],
147ebfedea0SLionel Sambuc RC4_KEY *sealkey,
148ebfedea0SLionel Sambuc uint32_t seq,
149ebfedea0SLionel Sambuc unsigned char out[16])
150ebfedea0SLionel Sambuc {
151ebfedea0SLionel Sambuc unsigned char hmac[16];
152ebfedea0SLionel Sambuc unsigned int hmaclen;
153ebfedea0SLionel Sambuc HMAC_CTX c;
154ebfedea0SLionel Sambuc
155ebfedea0SLionel Sambuc HMAC_CTX_init(&c);
156ebfedea0SLionel Sambuc HMAC_Init_ex(&c, signkey, 16, EVP_md5(), NULL);
157ebfedea0SLionel Sambuc
158ebfedea0SLionel Sambuc encode_le_uint32(seq, hmac);
159ebfedea0SLionel Sambuc HMAC_Update(&c, hmac, 4);
160ebfedea0SLionel Sambuc HMAC_Update(&c, in->value, in->length);
161ebfedea0SLionel Sambuc HMAC_Final(&c, hmac, &hmaclen);
162ebfedea0SLionel Sambuc HMAC_CTX_cleanup(&c);
163ebfedea0SLionel Sambuc
164ebfedea0SLionel Sambuc encode_le_uint32(1, &out[0]);
165ebfedea0SLionel Sambuc if (sealkey)
166ebfedea0SLionel Sambuc RC4(sealkey, 8, hmac, &out[4]);
167ebfedea0SLionel Sambuc else
168ebfedea0SLionel Sambuc memcpy(&out[4], hmac, 8);
169ebfedea0SLionel Sambuc
170ebfedea0SLionel Sambuc memset(&out[12], 0, 4);
171ebfedea0SLionel Sambuc
172ebfedea0SLionel Sambuc return GSS_S_COMPLETE;
173ebfedea0SLionel Sambuc }
174ebfedea0SLionel Sambuc
175ebfedea0SLionel Sambuc static OM_uint32
v2_verify_message(gss_buffer_t in,unsigned char signkey[16],RC4_KEY * sealkey,uint32_t seq,const unsigned char checksum[16])176ebfedea0SLionel Sambuc v2_verify_message(gss_buffer_t in,
177ebfedea0SLionel Sambuc unsigned char signkey[16],
178ebfedea0SLionel Sambuc RC4_KEY *sealkey,
179ebfedea0SLionel Sambuc uint32_t seq,
180ebfedea0SLionel Sambuc const unsigned char checksum[16])
181ebfedea0SLionel Sambuc {
182ebfedea0SLionel Sambuc OM_uint32 ret;
183ebfedea0SLionel Sambuc unsigned char out[16];
184ebfedea0SLionel Sambuc
185ebfedea0SLionel Sambuc ret = v2_sign_message(in, signkey, sealkey, seq, out);
186ebfedea0SLionel Sambuc if (ret)
187ebfedea0SLionel Sambuc return ret;
188ebfedea0SLionel Sambuc
189ebfedea0SLionel Sambuc if (memcmp(checksum, out, 16) != 0)
190ebfedea0SLionel Sambuc return GSS_S_BAD_MIC;
191ebfedea0SLionel Sambuc
192ebfedea0SLionel Sambuc return GSS_S_COMPLETE;
193ebfedea0SLionel Sambuc }
194ebfedea0SLionel Sambuc
195ebfedea0SLionel Sambuc static OM_uint32
v2_seal_message(const gss_buffer_t in,unsigned char signkey[16],uint32_t seq,RC4_KEY * sealkey,gss_buffer_t out)196ebfedea0SLionel Sambuc v2_seal_message(const gss_buffer_t in,
197ebfedea0SLionel Sambuc unsigned char signkey[16],
198ebfedea0SLionel Sambuc uint32_t seq,
199ebfedea0SLionel Sambuc RC4_KEY *sealkey,
200ebfedea0SLionel Sambuc gss_buffer_t out)
201ebfedea0SLionel Sambuc {
202ebfedea0SLionel Sambuc unsigned char *p;
203ebfedea0SLionel Sambuc OM_uint32 ret;
204ebfedea0SLionel Sambuc
205ebfedea0SLionel Sambuc if (in->length + 16 < in->length)
206ebfedea0SLionel Sambuc return EINVAL;
207ebfedea0SLionel Sambuc
208ebfedea0SLionel Sambuc p = malloc(in->length + 16);
209ebfedea0SLionel Sambuc if (p == NULL)
210ebfedea0SLionel Sambuc return ENOMEM;
211ebfedea0SLionel Sambuc
212ebfedea0SLionel Sambuc RC4(sealkey, in->length, in->value, p);
213ebfedea0SLionel Sambuc
214ebfedea0SLionel Sambuc ret = v2_sign_message(in, signkey, sealkey, seq, &p[in->length]);
215ebfedea0SLionel Sambuc if (ret) {
216ebfedea0SLionel Sambuc free(p);
217ebfedea0SLionel Sambuc return ret;
218ebfedea0SLionel Sambuc }
219ebfedea0SLionel Sambuc
220ebfedea0SLionel Sambuc out->value = p;
221ebfedea0SLionel Sambuc out->length = in->length + 16;
222ebfedea0SLionel Sambuc
223ebfedea0SLionel Sambuc return 0;
224ebfedea0SLionel Sambuc }
225ebfedea0SLionel Sambuc
226ebfedea0SLionel Sambuc static OM_uint32
v2_unseal_message(gss_buffer_t in,unsigned char signkey[16],uint32_t seq,RC4_KEY * sealkey,gss_buffer_t out)227ebfedea0SLionel Sambuc v2_unseal_message(gss_buffer_t in,
228ebfedea0SLionel Sambuc unsigned char signkey[16],
229ebfedea0SLionel Sambuc uint32_t seq,
230ebfedea0SLionel Sambuc RC4_KEY *sealkey,
231ebfedea0SLionel Sambuc gss_buffer_t out)
232ebfedea0SLionel Sambuc {
233ebfedea0SLionel Sambuc OM_uint32 ret;
234ebfedea0SLionel Sambuc
235ebfedea0SLionel Sambuc if (in->length < 16)
236ebfedea0SLionel Sambuc return GSS_S_BAD_MIC;
237ebfedea0SLionel Sambuc
238ebfedea0SLionel Sambuc out->length = in->length - 16;
239ebfedea0SLionel Sambuc out->value = malloc(out->length);
240ebfedea0SLionel Sambuc if (out->value == NULL)
241ebfedea0SLionel Sambuc return GSS_S_BAD_MIC;
242ebfedea0SLionel Sambuc
243ebfedea0SLionel Sambuc RC4(sealkey, out->length, in->value, out->value);
244ebfedea0SLionel Sambuc
245ebfedea0SLionel Sambuc ret = v2_verify_message(out, signkey, sealkey, seq,
246ebfedea0SLionel Sambuc ((const unsigned char *)in->value) + out->length);
247ebfedea0SLionel Sambuc if (ret) {
248ebfedea0SLionel Sambuc OM_uint32 junk;
249ebfedea0SLionel Sambuc gss_release_buffer(&junk, out);
250ebfedea0SLionel Sambuc }
251ebfedea0SLionel Sambuc return ret;
252ebfedea0SLionel Sambuc }
253ebfedea0SLionel Sambuc
254ebfedea0SLionel Sambuc /*
255ebfedea0SLionel Sambuc *
256ebfedea0SLionel Sambuc */
257ebfedea0SLionel Sambuc
258ebfedea0SLionel Sambuc #define CTX_FLAGS_ISSET(_ctx,_flags) \
259ebfedea0SLionel Sambuc (((_ctx)->flags & (_flags)) == (_flags))
260ebfedea0SLionel Sambuc
261ebfedea0SLionel Sambuc /*
262ebfedea0SLionel Sambuc *
263ebfedea0SLionel Sambuc */
264ebfedea0SLionel Sambuc
265ebfedea0SLionel Sambuc OM_uint32 GSSAPI_CALLCONV
_gss_ntlm_get_mic(OM_uint32 * minor_status,const gss_ctx_id_t context_handle,gss_qop_t qop_req,const gss_buffer_t message_buffer,gss_buffer_t message_token)266ebfedea0SLionel Sambuc _gss_ntlm_get_mic
267ebfedea0SLionel Sambuc (OM_uint32 * minor_status,
268ebfedea0SLionel Sambuc const gss_ctx_id_t context_handle,
269ebfedea0SLionel Sambuc gss_qop_t qop_req,
270ebfedea0SLionel Sambuc const gss_buffer_t message_buffer,
271ebfedea0SLionel Sambuc gss_buffer_t message_token
272ebfedea0SLionel Sambuc )
273ebfedea0SLionel Sambuc {
274ebfedea0SLionel Sambuc ntlm_ctx ctx = (ntlm_ctx)context_handle;
275ebfedea0SLionel Sambuc OM_uint32 junk;
276ebfedea0SLionel Sambuc
277ebfedea0SLionel Sambuc *minor_status = 0;
278ebfedea0SLionel Sambuc
279ebfedea0SLionel Sambuc message_token->value = malloc(16);
280ebfedea0SLionel Sambuc message_token->length = 16;
281ebfedea0SLionel Sambuc if (message_token->value == NULL) {
282ebfedea0SLionel Sambuc *minor_status = ENOMEM;
283ebfedea0SLionel Sambuc return GSS_S_FAILURE;
284ebfedea0SLionel Sambuc }
285ebfedea0SLionel Sambuc
286ebfedea0SLionel Sambuc if (CTX_FLAGS_ISSET(ctx, NTLM_NEG_SIGN|NTLM_NEG_NTLM2_SESSION)) {
287ebfedea0SLionel Sambuc OM_uint32 ret;
288ebfedea0SLionel Sambuc
289ebfedea0SLionel Sambuc if ((ctx->status & STATUS_SESSIONKEY) == 0) {
290ebfedea0SLionel Sambuc gss_release_buffer(&junk, message_token);
291ebfedea0SLionel Sambuc return GSS_S_UNAVAILABLE;
292ebfedea0SLionel Sambuc }
293ebfedea0SLionel Sambuc
294ebfedea0SLionel Sambuc ret = v2_sign_message(message_buffer,
295ebfedea0SLionel Sambuc ctx->u.v2.send.signkey,
296ebfedea0SLionel Sambuc ctx->u.v2.send.signsealkey,
297ebfedea0SLionel Sambuc ctx->u.v2.send.seq++,
298ebfedea0SLionel Sambuc message_token->value);
299ebfedea0SLionel Sambuc if (ret)
300ebfedea0SLionel Sambuc gss_release_buffer(&junk, message_token);
301ebfedea0SLionel Sambuc return ret;
302ebfedea0SLionel Sambuc
303ebfedea0SLionel Sambuc } else if (CTX_FLAGS_ISSET(ctx, NTLM_NEG_SIGN)) {
304ebfedea0SLionel Sambuc OM_uint32 ret;
305ebfedea0SLionel Sambuc
306ebfedea0SLionel Sambuc if ((ctx->status & STATUS_SESSIONKEY) == 0) {
307ebfedea0SLionel Sambuc gss_release_buffer(&junk, message_token);
308ebfedea0SLionel Sambuc return GSS_S_UNAVAILABLE;
309ebfedea0SLionel Sambuc }
310ebfedea0SLionel Sambuc
311ebfedea0SLionel Sambuc ret = v1_sign_message(message_buffer,
312ebfedea0SLionel Sambuc &ctx->u.v1.crypto_send.key,
313ebfedea0SLionel Sambuc ctx->u.v1.crypto_send.seq++,
314ebfedea0SLionel Sambuc message_token->value);
315ebfedea0SLionel Sambuc if (ret)
316ebfedea0SLionel Sambuc gss_release_buffer(&junk, message_token);
317ebfedea0SLionel Sambuc return ret;
318ebfedea0SLionel Sambuc
319ebfedea0SLionel Sambuc } else if (CTX_FLAGS_ISSET(ctx, NTLM_NEG_ALWAYS_SIGN)) {
320ebfedea0SLionel Sambuc unsigned char *sigature;
321ebfedea0SLionel Sambuc
322ebfedea0SLionel Sambuc sigature = message_token->value;
323ebfedea0SLionel Sambuc
324ebfedea0SLionel Sambuc encode_le_uint32(1, &sigature[0]); /* version */
325ebfedea0SLionel Sambuc encode_le_uint32(0, &sigature[4]);
326ebfedea0SLionel Sambuc encode_le_uint32(0, &sigature[8]);
327ebfedea0SLionel Sambuc encode_le_uint32(0, &sigature[12]);
328ebfedea0SLionel Sambuc
329ebfedea0SLionel Sambuc return GSS_S_COMPLETE;
330ebfedea0SLionel Sambuc }
331ebfedea0SLionel Sambuc gss_release_buffer(&junk, message_token);
332ebfedea0SLionel Sambuc
333ebfedea0SLionel Sambuc return GSS_S_UNAVAILABLE;
334ebfedea0SLionel Sambuc }
335ebfedea0SLionel Sambuc
336ebfedea0SLionel Sambuc /*
337ebfedea0SLionel Sambuc *
338ebfedea0SLionel Sambuc */
339ebfedea0SLionel Sambuc
340ebfedea0SLionel Sambuc OM_uint32 GSSAPI_CALLCONV
_gss_ntlm_verify_mic(OM_uint32 * minor_status,const gss_ctx_id_t context_handle,const gss_buffer_t message_buffer,const gss_buffer_t token_buffer,gss_qop_t * qop_state)341ebfedea0SLionel Sambuc _gss_ntlm_verify_mic
342ebfedea0SLionel Sambuc (OM_uint32 * minor_status,
343ebfedea0SLionel Sambuc const gss_ctx_id_t context_handle,
344ebfedea0SLionel Sambuc const gss_buffer_t message_buffer,
345ebfedea0SLionel Sambuc const gss_buffer_t token_buffer,
346ebfedea0SLionel Sambuc gss_qop_t * qop_state
347ebfedea0SLionel Sambuc )
348ebfedea0SLionel Sambuc {
349ebfedea0SLionel Sambuc ntlm_ctx ctx = (ntlm_ctx)context_handle;
350ebfedea0SLionel Sambuc
351ebfedea0SLionel Sambuc if (qop_state != NULL)
352ebfedea0SLionel Sambuc *qop_state = GSS_C_QOP_DEFAULT;
353ebfedea0SLionel Sambuc *minor_status = 0;
354ebfedea0SLionel Sambuc
355ebfedea0SLionel Sambuc if (token_buffer->length != 16)
356ebfedea0SLionel Sambuc return GSS_S_BAD_MIC;
357ebfedea0SLionel Sambuc
358ebfedea0SLionel Sambuc if (CTX_FLAGS_ISSET(ctx, NTLM_NEG_SIGN|NTLM_NEG_NTLM2_SESSION)) {
359ebfedea0SLionel Sambuc OM_uint32 ret;
360ebfedea0SLionel Sambuc
361ebfedea0SLionel Sambuc if ((ctx->status & STATUS_SESSIONKEY) == 0)
362ebfedea0SLionel Sambuc return GSS_S_UNAVAILABLE;
363ebfedea0SLionel Sambuc
364ebfedea0SLionel Sambuc ret = v2_verify_message(message_buffer,
365ebfedea0SLionel Sambuc ctx->u.v2.recv.signkey,
366ebfedea0SLionel Sambuc ctx->u.v2.recv.signsealkey,
367ebfedea0SLionel Sambuc ctx->u.v2.recv.seq++,
368ebfedea0SLionel Sambuc token_buffer->value);
369ebfedea0SLionel Sambuc if (ret)
370ebfedea0SLionel Sambuc return ret;
371ebfedea0SLionel Sambuc
372ebfedea0SLionel Sambuc return GSS_S_COMPLETE;
373ebfedea0SLionel Sambuc } else if (CTX_FLAGS_ISSET(ctx, NTLM_NEG_SIGN)) {
374ebfedea0SLionel Sambuc
375ebfedea0SLionel Sambuc unsigned char sigature[12];
376ebfedea0SLionel Sambuc uint32_t crc, num;
377ebfedea0SLionel Sambuc
378ebfedea0SLionel Sambuc if ((ctx->status & STATUS_SESSIONKEY) == 0)
379ebfedea0SLionel Sambuc return GSS_S_UNAVAILABLE;
380ebfedea0SLionel Sambuc
381ebfedea0SLionel Sambuc decode_le_uint32(token_buffer->value, &num);
382ebfedea0SLionel Sambuc if (num != 1)
383ebfedea0SLionel Sambuc return GSS_S_BAD_MIC;
384ebfedea0SLionel Sambuc
385ebfedea0SLionel Sambuc RC4(&ctx->u.v1.crypto_recv.key, sizeof(sigature),
386ebfedea0SLionel Sambuc ((unsigned char *)token_buffer->value) + 4, sigature);
387ebfedea0SLionel Sambuc
388ebfedea0SLionel Sambuc _krb5_crc_init_table();
389ebfedea0SLionel Sambuc crc = _krb5_crc_update(message_buffer->value,
390ebfedea0SLionel Sambuc message_buffer->length, 0);
391ebfedea0SLionel Sambuc /* skip first 4 bytes in the encrypted checksum */
392ebfedea0SLionel Sambuc decode_le_uint32(&sigature[4], &num);
393ebfedea0SLionel Sambuc if (num != crc)
394ebfedea0SLionel Sambuc return GSS_S_BAD_MIC;
395ebfedea0SLionel Sambuc decode_le_uint32(&sigature[8], &num);
396ebfedea0SLionel Sambuc if (ctx->u.v1.crypto_recv.seq != num)
397ebfedea0SLionel Sambuc return GSS_S_BAD_MIC;
398ebfedea0SLionel Sambuc ctx->u.v1.crypto_recv.seq++;
399ebfedea0SLionel Sambuc
400ebfedea0SLionel Sambuc return GSS_S_COMPLETE;
401ebfedea0SLionel Sambuc } else if (ctx->flags & NTLM_NEG_ALWAYS_SIGN) {
402ebfedea0SLionel Sambuc uint32_t num;
403ebfedea0SLionel Sambuc unsigned char *p;
404ebfedea0SLionel Sambuc
405ebfedea0SLionel Sambuc p = (unsigned char*)(token_buffer->value);
406ebfedea0SLionel Sambuc
407ebfedea0SLionel Sambuc decode_le_uint32(&p[0], &num); /* version */
408ebfedea0SLionel Sambuc if (num != 1) return GSS_S_BAD_MIC;
409ebfedea0SLionel Sambuc decode_le_uint32(&p[4], &num);
410ebfedea0SLionel Sambuc if (num != 0) return GSS_S_BAD_MIC;
411ebfedea0SLionel Sambuc decode_le_uint32(&p[8], &num);
412ebfedea0SLionel Sambuc if (num != 0) return GSS_S_BAD_MIC;
413ebfedea0SLionel Sambuc decode_le_uint32(&p[12], &num);
414ebfedea0SLionel Sambuc if (num != 0) return GSS_S_BAD_MIC;
415ebfedea0SLionel Sambuc
416ebfedea0SLionel Sambuc return GSS_S_COMPLETE;
417ebfedea0SLionel Sambuc }
418ebfedea0SLionel Sambuc
419ebfedea0SLionel Sambuc return GSS_S_UNAVAILABLE;
420ebfedea0SLionel Sambuc }
421ebfedea0SLionel Sambuc
422ebfedea0SLionel Sambuc /*
423ebfedea0SLionel Sambuc *
424ebfedea0SLionel Sambuc */
425ebfedea0SLionel Sambuc
426ebfedea0SLionel Sambuc OM_uint32 GSSAPI_CALLCONV
_gss_ntlm_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)427ebfedea0SLionel Sambuc _gss_ntlm_wrap_size_limit (
428ebfedea0SLionel Sambuc OM_uint32 * minor_status,
429ebfedea0SLionel Sambuc const gss_ctx_id_t context_handle,
430ebfedea0SLionel Sambuc int conf_req_flag,
431ebfedea0SLionel Sambuc gss_qop_t qop_req,
432ebfedea0SLionel Sambuc OM_uint32 req_output_size,
433ebfedea0SLionel Sambuc OM_uint32 * max_input_size
434ebfedea0SLionel Sambuc )
435ebfedea0SLionel Sambuc {
436ebfedea0SLionel Sambuc ntlm_ctx ctx = (ntlm_ctx)context_handle;
437ebfedea0SLionel Sambuc
438ebfedea0SLionel Sambuc *minor_status = 0;
439ebfedea0SLionel Sambuc
440ebfedea0SLionel Sambuc if(ctx->flags & NTLM_NEG_SEAL) {
441ebfedea0SLionel Sambuc
442ebfedea0SLionel Sambuc if (req_output_size < 16)
443ebfedea0SLionel Sambuc *max_input_size = 0;
444ebfedea0SLionel Sambuc else
445ebfedea0SLionel Sambuc *max_input_size = req_output_size - 16;
446ebfedea0SLionel Sambuc
447ebfedea0SLionel Sambuc return GSS_S_COMPLETE;
448ebfedea0SLionel Sambuc }
449ebfedea0SLionel Sambuc
450ebfedea0SLionel Sambuc return GSS_S_UNAVAILABLE;
451ebfedea0SLionel Sambuc }
452ebfedea0SLionel Sambuc
453ebfedea0SLionel Sambuc /*
454ebfedea0SLionel Sambuc *
455ebfedea0SLionel Sambuc */
456ebfedea0SLionel Sambuc
457ebfedea0SLionel Sambuc OM_uint32 GSSAPI_CALLCONV
_gss_ntlm_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)458ebfedea0SLionel Sambuc _gss_ntlm_wrap
459ebfedea0SLionel Sambuc (OM_uint32 * minor_status,
460ebfedea0SLionel Sambuc const gss_ctx_id_t context_handle,
461ebfedea0SLionel Sambuc int conf_req_flag,
462ebfedea0SLionel Sambuc gss_qop_t qop_req,
463ebfedea0SLionel Sambuc const gss_buffer_t input_message_buffer,
464ebfedea0SLionel Sambuc int * conf_state,
465ebfedea0SLionel Sambuc gss_buffer_t output_message_buffer
466ebfedea0SLionel Sambuc )
467ebfedea0SLionel Sambuc {
468ebfedea0SLionel Sambuc ntlm_ctx ctx = (ntlm_ctx)context_handle;
469ebfedea0SLionel Sambuc OM_uint32 ret;
470ebfedea0SLionel Sambuc
471ebfedea0SLionel Sambuc *minor_status = 0;
472ebfedea0SLionel Sambuc if (conf_state)
473ebfedea0SLionel Sambuc *conf_state = 0;
474ebfedea0SLionel Sambuc if (output_message_buffer == GSS_C_NO_BUFFER)
475ebfedea0SLionel Sambuc return GSS_S_FAILURE;
476ebfedea0SLionel Sambuc
477ebfedea0SLionel Sambuc
478ebfedea0SLionel Sambuc if (CTX_FLAGS_ISSET(ctx, NTLM_NEG_SEAL|NTLM_NEG_NTLM2_SESSION)) {
479ebfedea0SLionel Sambuc
480ebfedea0SLionel Sambuc return v2_seal_message(input_message_buffer,
481ebfedea0SLionel Sambuc ctx->u.v2.send.signkey,
482ebfedea0SLionel Sambuc ctx->u.v2.send.seq++,
483ebfedea0SLionel Sambuc &ctx->u.v2.send.sealkey,
484ebfedea0SLionel Sambuc output_message_buffer);
485ebfedea0SLionel Sambuc
486ebfedea0SLionel Sambuc } else if (CTX_FLAGS_ISSET(ctx, NTLM_NEG_SEAL)) {
487ebfedea0SLionel Sambuc gss_buffer_desc trailer;
488ebfedea0SLionel Sambuc OM_uint32 junk;
489ebfedea0SLionel Sambuc
490ebfedea0SLionel Sambuc output_message_buffer->length = input_message_buffer->length + 16;
491ebfedea0SLionel Sambuc output_message_buffer->value = malloc(output_message_buffer->length);
492ebfedea0SLionel Sambuc if (output_message_buffer->value == NULL) {
493ebfedea0SLionel Sambuc output_message_buffer->length = 0;
494ebfedea0SLionel Sambuc return GSS_S_FAILURE;
495ebfedea0SLionel Sambuc }
496ebfedea0SLionel Sambuc
497ebfedea0SLionel Sambuc
498ebfedea0SLionel Sambuc RC4(&ctx->u.v1.crypto_send.key, input_message_buffer->length,
499ebfedea0SLionel Sambuc input_message_buffer->value, output_message_buffer->value);
500ebfedea0SLionel Sambuc
501ebfedea0SLionel Sambuc ret = _gss_ntlm_get_mic(minor_status, context_handle,
502ebfedea0SLionel Sambuc 0, input_message_buffer,
503ebfedea0SLionel Sambuc &trailer);
504ebfedea0SLionel Sambuc if (ret) {
505ebfedea0SLionel Sambuc gss_release_buffer(&junk, output_message_buffer);
506ebfedea0SLionel Sambuc return ret;
507ebfedea0SLionel Sambuc }
508ebfedea0SLionel Sambuc if (trailer.length != 16) {
509ebfedea0SLionel Sambuc gss_release_buffer(&junk, output_message_buffer);
510ebfedea0SLionel Sambuc gss_release_buffer(&junk, &trailer);
511ebfedea0SLionel Sambuc return GSS_S_FAILURE;
512ebfedea0SLionel Sambuc }
513ebfedea0SLionel Sambuc memcpy(((unsigned char *)output_message_buffer->value) +
514ebfedea0SLionel Sambuc input_message_buffer->length,
515ebfedea0SLionel Sambuc trailer.value, trailer.length);
516ebfedea0SLionel Sambuc gss_release_buffer(&junk, &trailer);
517ebfedea0SLionel Sambuc
518ebfedea0SLionel Sambuc return GSS_S_COMPLETE;
519ebfedea0SLionel Sambuc }
520ebfedea0SLionel Sambuc
521ebfedea0SLionel Sambuc return GSS_S_UNAVAILABLE;
522ebfedea0SLionel Sambuc }
523ebfedea0SLionel Sambuc
524ebfedea0SLionel Sambuc /*
525ebfedea0SLionel Sambuc *
526ebfedea0SLionel Sambuc */
527ebfedea0SLionel Sambuc
528ebfedea0SLionel Sambuc OM_uint32 GSSAPI_CALLCONV
_gss_ntlm_unwrap(OM_uint32 * minor_status,const gss_ctx_id_t context_handle,const gss_buffer_t input_message_buffer,gss_buffer_t output_message_buffer,int * conf_state,gss_qop_t * qop_state)529ebfedea0SLionel Sambuc _gss_ntlm_unwrap
530ebfedea0SLionel Sambuc (OM_uint32 * minor_status,
531ebfedea0SLionel Sambuc const gss_ctx_id_t context_handle,
532ebfedea0SLionel Sambuc const gss_buffer_t input_message_buffer,
533ebfedea0SLionel Sambuc gss_buffer_t output_message_buffer,
534ebfedea0SLionel Sambuc int * conf_state,
535ebfedea0SLionel Sambuc gss_qop_t * qop_state
536ebfedea0SLionel Sambuc )
537ebfedea0SLionel Sambuc {
538ebfedea0SLionel Sambuc ntlm_ctx ctx = (ntlm_ctx)context_handle;
539ebfedea0SLionel Sambuc OM_uint32 ret;
540ebfedea0SLionel Sambuc
541ebfedea0SLionel Sambuc *minor_status = 0;
542ebfedea0SLionel Sambuc output_message_buffer->value = NULL;
543ebfedea0SLionel Sambuc output_message_buffer->length = 0;
544ebfedea0SLionel Sambuc
545ebfedea0SLionel Sambuc if (conf_state)
546ebfedea0SLionel Sambuc *conf_state = 0;
547ebfedea0SLionel Sambuc if (qop_state)
548ebfedea0SLionel Sambuc *qop_state = 0;
549ebfedea0SLionel Sambuc
550ebfedea0SLionel Sambuc if (CTX_FLAGS_ISSET(ctx, NTLM_NEG_SEAL|NTLM_NEG_NTLM2_SESSION)) {
551ebfedea0SLionel Sambuc
552ebfedea0SLionel Sambuc return v2_unseal_message(input_message_buffer,
553ebfedea0SLionel Sambuc ctx->u.v2.recv.signkey,
554ebfedea0SLionel Sambuc ctx->u.v2.recv.seq++,
555ebfedea0SLionel Sambuc &ctx->u.v2.recv.sealkey,
556ebfedea0SLionel Sambuc output_message_buffer);
557ebfedea0SLionel Sambuc
558ebfedea0SLionel Sambuc } else if (CTX_FLAGS_ISSET(ctx, NTLM_NEG_SEAL)) {
559ebfedea0SLionel Sambuc
560ebfedea0SLionel Sambuc gss_buffer_desc trailer;
561ebfedea0SLionel Sambuc OM_uint32 junk;
562ebfedea0SLionel Sambuc
563ebfedea0SLionel Sambuc if (input_message_buffer->length < 16)
564ebfedea0SLionel Sambuc return GSS_S_BAD_MIC;
565ebfedea0SLionel Sambuc
566ebfedea0SLionel Sambuc output_message_buffer->length = input_message_buffer->length - 16;
567ebfedea0SLionel Sambuc output_message_buffer->value = malloc(output_message_buffer->length);
568ebfedea0SLionel Sambuc if (output_message_buffer->value == NULL) {
569ebfedea0SLionel Sambuc output_message_buffer->length = 0;
570ebfedea0SLionel Sambuc return GSS_S_FAILURE;
571ebfedea0SLionel Sambuc }
572ebfedea0SLionel Sambuc
573ebfedea0SLionel Sambuc RC4(&ctx->u.v1.crypto_recv.key, output_message_buffer->length,
574ebfedea0SLionel Sambuc input_message_buffer->value, output_message_buffer->value);
575ebfedea0SLionel Sambuc
576ebfedea0SLionel Sambuc trailer.value = ((unsigned char *)input_message_buffer->value) +
577ebfedea0SLionel Sambuc output_message_buffer->length;
578ebfedea0SLionel Sambuc trailer.length = 16;
579ebfedea0SLionel Sambuc
580ebfedea0SLionel Sambuc ret = _gss_ntlm_verify_mic(minor_status, context_handle,
581ebfedea0SLionel Sambuc output_message_buffer,
582ebfedea0SLionel Sambuc &trailer, NULL);
583ebfedea0SLionel Sambuc if (ret) {
584ebfedea0SLionel Sambuc gss_release_buffer(&junk, output_message_buffer);
585ebfedea0SLionel Sambuc return ret;
586ebfedea0SLionel Sambuc }
587ebfedea0SLionel Sambuc
588ebfedea0SLionel Sambuc return GSS_S_COMPLETE;
589ebfedea0SLionel Sambuc }
590ebfedea0SLionel Sambuc
591ebfedea0SLionel Sambuc return GSS_S_UNAVAILABLE;
592ebfedea0SLionel Sambuc }
593