1 /* $NetBSD: crypto.c,v 1.1.1.2 2014/04/24 12:45:29 pettai Exp $ */
4 * Copyright (c) 2006 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 _krb5_crc_update (const char *p
, size_t len
, uint32_t res
);
41 _krb5_crc_init_table(void);
48 encode_le_uint32(uint32_t n
, unsigned char *p
)
50 p
[0] = (n
>> 0) & 0xFF;
51 p
[1] = (n
>> 8) & 0xFF;
52 p
[2] = (n
>> 16) & 0xFF;
53 p
[3] = (n
>> 24) & 0xFF;
58 decode_le_uint32(const void *ptr
, uint32_t *n
)
60 const unsigned char *p
= ptr
;
61 *n
= (p
[0] << 0) | (p
[1] << 8) | (p
[2] << 16) | (p
[3] << 24);
68 const char a2i_signmagic
[] =
69 "session key to server-to-client signing key magic constant";
70 const char a2i_sealmagic
[] =
71 "session key to server-to-client sealing key magic constant";
72 const char i2a_signmagic
[] =
73 "session key to client-to-server signing key magic constant";
74 const char i2a_sealmagic
[] =
75 "session key to client-to-server sealing key magic constant";
79 _gss_ntlm_set_key(struct ntlmv2_key
*key
, int acceptor
, int sealsign
,
80 unsigned char *data
, size_t len
)
82 unsigned char out
[16];
84 const char *signmagic
;
85 const char *sealmagic
;
88 signmagic
= a2i_signmagic
;
89 sealmagic
= a2i_sealmagic
;
91 signmagic
= i2a_signmagic
;
92 sealmagic
= i2a_sealmagic
;
97 ctx
= EVP_MD_CTX_create();
98 EVP_DigestInit_ex(ctx
, EVP_md5(), NULL
);
99 EVP_DigestUpdate(ctx
, data
, len
);
100 EVP_DigestUpdate(ctx
, signmagic
, strlen(signmagic
) + 1);
101 EVP_DigestFinal_ex(ctx
, key
->signkey
, NULL
);
103 EVP_DigestInit_ex(ctx
, EVP_md5(), NULL
);
104 EVP_DigestUpdate(ctx
, data
, len
);
105 EVP_DigestUpdate(ctx
, sealmagic
, strlen(sealmagic
) + 1);
106 EVP_DigestFinal_ex(ctx
, out
, NULL
);
107 EVP_MD_CTX_destroy(ctx
);
109 RC4_set_key(&key
->sealkey
, 16, out
);
111 key
->signsealkey
= &key
->sealkey
;
119 v1_sign_message(gss_buffer_t in
,
122 unsigned char out
[16])
124 unsigned char sigature
[12];
127 _krb5_crc_init_table();
128 crc
= _krb5_crc_update(in
->value
, in
->length
, 0);
130 encode_le_uint32(0, &sigature
[0]);
131 encode_le_uint32(crc
, &sigature
[4]);
132 encode_le_uint32(seq
, &sigature
[8]);
134 encode_le_uint32(1, out
); /* version */
135 RC4(signkey
, sizeof(sigature
), sigature
, out
+ 4);
137 if (RAND_bytes(out
+ 4, 4) != 1)
138 return GSS_S_UNAVAILABLE
;
145 v2_sign_message(gss_buffer_t in
,
146 unsigned char signkey
[16],
149 unsigned char out
[16])
151 unsigned char hmac
[16];
152 unsigned int hmaclen
;
156 HMAC_Init_ex(&c
, signkey
, 16, EVP_md5(), NULL
);
158 encode_le_uint32(seq
, hmac
);
159 HMAC_Update(&c
, hmac
, 4);
160 HMAC_Update(&c
, in
->value
, in
->length
);
161 HMAC_Final(&c
, hmac
, &hmaclen
);
162 HMAC_CTX_cleanup(&c
);
164 encode_le_uint32(1, &out
[0]);
166 RC4(sealkey
, 8, hmac
, &out
[4]);
168 memcpy(&out
[4], hmac
, 8);
170 memset(&out
[12], 0, 4);
172 return GSS_S_COMPLETE
;
176 v2_verify_message(gss_buffer_t in
,
177 unsigned char signkey
[16],
180 const unsigned char checksum
[16])
183 unsigned char out
[16];
185 ret
= v2_sign_message(in
, signkey
, sealkey
, seq
, out
);
189 if (memcmp(checksum
, out
, 16) != 0)
190 return GSS_S_BAD_MIC
;
192 return GSS_S_COMPLETE
;
196 v2_seal_message(const gss_buffer_t in
,
197 unsigned char signkey
[16],
205 if (in
->length
+ 16 < in
->length
)
208 p
= malloc(in
->length
+ 16);
212 RC4(sealkey
, in
->length
, in
->value
, p
);
214 ret
= v2_sign_message(in
, signkey
, sealkey
, seq
, &p
[in
->length
]);
221 out
->length
= in
->length
+ 16;
227 v2_unseal_message(gss_buffer_t in
,
228 unsigned char signkey
[16],
236 return GSS_S_BAD_MIC
;
238 out
->length
= in
->length
- 16;
239 out
->value
= malloc(out
->length
);
240 if (out
->value
== NULL
)
241 return GSS_S_BAD_MIC
;
243 RC4(sealkey
, out
->length
, in
->value
, out
->value
);
245 ret
= v2_verify_message(out
, signkey
, sealkey
, seq
,
246 ((const unsigned char *)in
->value
) + out
->length
);
249 gss_release_buffer(&junk
, out
);
258 #define CTX_FLAGS_ISSET(_ctx,_flags) \
259 (((_ctx)->flags & (_flags)) == (_flags))
265 OM_uint32 GSSAPI_CALLCONV
267 (OM_uint32
* minor_status
,
268 const gss_ctx_id_t context_handle
,
270 const gss_buffer_t message_buffer
,
271 gss_buffer_t message_token
274 ntlm_ctx ctx
= (ntlm_ctx
)context_handle
;
279 message_token
->value
= malloc(16);
280 message_token
->length
= 16;
281 if (message_token
->value
== NULL
) {
282 *minor_status
= ENOMEM
;
283 return GSS_S_FAILURE
;
286 if (CTX_FLAGS_ISSET(ctx
, NTLM_NEG_SIGN
|NTLM_NEG_NTLM2_SESSION
)) {
289 if ((ctx
->status
& STATUS_SESSIONKEY
) == 0) {
290 gss_release_buffer(&junk
, message_token
);
291 return GSS_S_UNAVAILABLE
;
294 ret
= v2_sign_message(message_buffer
,
295 ctx
->u
.v2
.send
.signkey
,
296 ctx
->u
.v2
.send
.signsealkey
,
297 ctx
->u
.v2
.send
.seq
++,
298 message_token
->value
);
300 gss_release_buffer(&junk
, message_token
);
303 } else if (CTX_FLAGS_ISSET(ctx
, NTLM_NEG_SIGN
)) {
306 if ((ctx
->status
& STATUS_SESSIONKEY
) == 0) {
307 gss_release_buffer(&junk
, message_token
);
308 return GSS_S_UNAVAILABLE
;
311 ret
= v1_sign_message(message_buffer
,
312 &ctx
->u
.v1
.crypto_send
.key
,
313 ctx
->u
.v1
.crypto_send
.seq
++,
314 message_token
->value
);
316 gss_release_buffer(&junk
, message_token
);
319 } else if (CTX_FLAGS_ISSET(ctx
, NTLM_NEG_ALWAYS_SIGN
)) {
320 unsigned char *sigature
;
322 sigature
= message_token
->value
;
324 encode_le_uint32(1, &sigature
[0]); /* version */
325 encode_le_uint32(0, &sigature
[4]);
326 encode_le_uint32(0, &sigature
[8]);
327 encode_le_uint32(0, &sigature
[12]);
329 return GSS_S_COMPLETE
;
331 gss_release_buffer(&junk
, message_token
);
333 return GSS_S_UNAVAILABLE
;
340 OM_uint32 GSSAPI_CALLCONV
342 (OM_uint32
* minor_status
,
343 const gss_ctx_id_t context_handle
,
344 const gss_buffer_t message_buffer
,
345 const gss_buffer_t token_buffer
,
346 gss_qop_t
* qop_state
349 ntlm_ctx ctx
= (ntlm_ctx
)context_handle
;
351 if (qop_state
!= NULL
)
352 *qop_state
= GSS_C_QOP_DEFAULT
;
355 if (token_buffer
->length
!= 16)
356 return GSS_S_BAD_MIC
;
358 if (CTX_FLAGS_ISSET(ctx
, NTLM_NEG_SIGN
|NTLM_NEG_NTLM2_SESSION
)) {
361 if ((ctx
->status
& STATUS_SESSIONKEY
) == 0)
362 return GSS_S_UNAVAILABLE
;
364 ret
= v2_verify_message(message_buffer
,
365 ctx
->u
.v2
.recv
.signkey
,
366 ctx
->u
.v2
.recv
.signsealkey
,
367 ctx
->u
.v2
.recv
.seq
++,
368 token_buffer
->value
);
372 return GSS_S_COMPLETE
;
373 } else if (CTX_FLAGS_ISSET(ctx
, NTLM_NEG_SIGN
)) {
375 unsigned char sigature
[12];
378 if ((ctx
->status
& STATUS_SESSIONKEY
) == 0)
379 return GSS_S_UNAVAILABLE
;
381 decode_le_uint32(token_buffer
->value
, &num
);
383 return GSS_S_BAD_MIC
;
385 RC4(&ctx
->u
.v1
.crypto_recv
.key
, sizeof(sigature
),
386 ((unsigned char *)token_buffer
->value
) + 4, sigature
);
388 _krb5_crc_init_table();
389 crc
= _krb5_crc_update(message_buffer
->value
,
390 message_buffer
->length
, 0);
391 /* skip first 4 bytes in the encrypted checksum */
392 decode_le_uint32(&sigature
[4], &num
);
394 return GSS_S_BAD_MIC
;
395 decode_le_uint32(&sigature
[8], &num
);
396 if (ctx
->u
.v1
.crypto_recv
.seq
!= num
)
397 return GSS_S_BAD_MIC
;
398 ctx
->u
.v1
.crypto_recv
.seq
++;
400 return GSS_S_COMPLETE
;
401 } else if (ctx
->flags
& NTLM_NEG_ALWAYS_SIGN
) {
405 p
= (unsigned char*)(token_buffer
->value
);
407 decode_le_uint32(&p
[0], &num
); /* version */
408 if (num
!= 1) return GSS_S_BAD_MIC
;
409 decode_le_uint32(&p
[4], &num
);
410 if (num
!= 0) return GSS_S_BAD_MIC
;
411 decode_le_uint32(&p
[8], &num
);
412 if (num
!= 0) return GSS_S_BAD_MIC
;
413 decode_le_uint32(&p
[12], &num
);
414 if (num
!= 0) return GSS_S_BAD_MIC
;
416 return GSS_S_COMPLETE
;
419 return GSS_S_UNAVAILABLE
;
426 OM_uint32 GSSAPI_CALLCONV
427 _gss_ntlm_wrap_size_limit (
428 OM_uint32
* minor_status
,
429 const gss_ctx_id_t context_handle
,
432 OM_uint32 req_output_size
,
433 OM_uint32
* max_input_size
436 ntlm_ctx ctx
= (ntlm_ctx
)context_handle
;
440 if(ctx
->flags
& NTLM_NEG_SEAL
) {
442 if (req_output_size
< 16)
445 *max_input_size
= req_output_size
- 16;
447 return GSS_S_COMPLETE
;
450 return GSS_S_UNAVAILABLE
;
457 OM_uint32 GSSAPI_CALLCONV
459 (OM_uint32
* minor_status
,
460 const gss_ctx_id_t context_handle
,
463 const gss_buffer_t input_message_buffer
,
465 gss_buffer_t output_message_buffer
468 ntlm_ctx ctx
= (ntlm_ctx
)context_handle
;
474 if (output_message_buffer
== GSS_C_NO_BUFFER
)
475 return GSS_S_FAILURE
;
478 if (CTX_FLAGS_ISSET(ctx
, NTLM_NEG_SEAL
|NTLM_NEG_NTLM2_SESSION
)) {
480 return v2_seal_message(input_message_buffer
,
481 ctx
->u
.v2
.send
.signkey
,
482 ctx
->u
.v2
.send
.seq
++,
483 &ctx
->u
.v2
.send
.sealkey
,
484 output_message_buffer
);
486 } else if (CTX_FLAGS_ISSET(ctx
, NTLM_NEG_SEAL
)) {
487 gss_buffer_desc trailer
;
490 output_message_buffer
->length
= input_message_buffer
->length
+ 16;
491 output_message_buffer
->value
= malloc(output_message_buffer
->length
);
492 if (output_message_buffer
->value
== NULL
) {
493 output_message_buffer
->length
= 0;
494 return GSS_S_FAILURE
;
498 RC4(&ctx
->u
.v1
.crypto_send
.key
, input_message_buffer
->length
,
499 input_message_buffer
->value
, output_message_buffer
->value
);
501 ret
= _gss_ntlm_get_mic(minor_status
, context_handle
,
502 0, input_message_buffer
,
505 gss_release_buffer(&junk
, output_message_buffer
);
508 if (trailer
.length
!= 16) {
509 gss_release_buffer(&junk
, output_message_buffer
);
510 gss_release_buffer(&junk
, &trailer
);
511 return GSS_S_FAILURE
;
513 memcpy(((unsigned char *)output_message_buffer
->value
) +
514 input_message_buffer
->length
,
515 trailer
.value
, trailer
.length
);
516 gss_release_buffer(&junk
, &trailer
);
518 return GSS_S_COMPLETE
;
521 return GSS_S_UNAVAILABLE
;
528 OM_uint32 GSSAPI_CALLCONV
530 (OM_uint32
* minor_status
,
531 const gss_ctx_id_t context_handle
,
532 const gss_buffer_t input_message_buffer
,
533 gss_buffer_t output_message_buffer
,
535 gss_qop_t
* qop_state
538 ntlm_ctx ctx
= (ntlm_ctx
)context_handle
;
542 output_message_buffer
->value
= NULL
;
543 output_message_buffer
->length
= 0;
550 if (CTX_FLAGS_ISSET(ctx
, NTLM_NEG_SEAL
|NTLM_NEG_NTLM2_SESSION
)) {
552 return v2_unseal_message(input_message_buffer
,
553 ctx
->u
.v2
.recv
.signkey
,
554 ctx
->u
.v2
.recv
.seq
++,
555 &ctx
->u
.v2
.recv
.sealkey
,
556 output_message_buffer
);
558 } else if (CTX_FLAGS_ISSET(ctx
, NTLM_NEG_SEAL
)) {
560 gss_buffer_desc trailer
;
563 if (input_message_buffer
->length
< 16)
564 return GSS_S_BAD_MIC
;
566 output_message_buffer
->length
= input_message_buffer
->length
- 16;
567 output_message_buffer
->value
= malloc(output_message_buffer
->length
);
568 if (output_message_buffer
->value
== NULL
) {
569 output_message_buffer
->length
= 0;
570 return GSS_S_FAILURE
;
573 RC4(&ctx
->u
.v1
.crypto_recv
.key
, output_message_buffer
->length
,
574 input_message_buffer
->value
, output_message_buffer
->value
);
576 trailer
.value
= ((unsigned char *)input_message_buffer
->value
) +
577 output_message_buffer
->length
;
580 ret
= _gss_ntlm_verify_mic(minor_status
, context_handle
,
581 output_message_buffer
,
584 gss_release_buffer(&junk
, output_message_buffer
);
588 return GSS_S_COMPLETE
;
591 return GSS_S_UNAVAILABLE
;