2 * Copyright (c) 2015 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (c) 1999 Kungliga Tekniska Högskolan
31 * (Royal Institute of Technology, Stockholm, Sweden).
32 * All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of KTH nor the names of its contributors may be
46 * used to endorse or promote products derived from this software without
47 * specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
50 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
53 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
54 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
55 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
56 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
57 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
58 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
59 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/malloc.h>
67 #include <sys/kpi_mbuf.h>
68 #include <sys/random.h>
69 #include <mach_assert.h>
70 #include <kern/assert.h>
71 #include <libkern/OSAtomic.h>
72 #include "gss_krb5_mech.h"
74 lck_grp_t
*gss_krb5_mech_grp
;
76 typedef struct crypt_walker_ctx
{
78 const struct ccmode_cbc
*ccmode
;
81 } *crypt_walker_ctx_t
;
83 typedef struct hmac_walker_ctx
{
84 const struct ccdigest_info
*di
;
85 struct cchmac_ctx
*hmac_ctx
;
88 typedef size_t (*ccpad_func
)(const struct ccmode_cbc
*, cccbc_ctx
*, cccbc_iv
*,
89 size_t nbytes
, const void *, void *);
91 static int krb5_n_fold(const void *instr
, size_t len
, void *foldstr
, size_t size
);
93 size_t gss_mbuf_len(mbuf_t
, size_t);
94 errno_t
gss_prepend_mbuf(mbuf_t
*, uint8_t *, size_t);
95 errno_t
gss_append_mbuf(mbuf_t
, uint8_t *, size_t);
96 errno_t
gss_strip_mbuf(mbuf_t
, ssize_t
);
97 int mbuf_walk(mbuf_t
, size_t, size_t, size_t, int (*)(void *, uint8_t *, uint32_t), void *);
99 void do_crypt_init(crypt_walker_ctx_t
, int, crypto_ctx_t
, cccbc_ctx
*);
100 int do_crypt(void *, uint8_t *, uint32_t);
101 void do_hmac_init(hmac_walker_ctx_t
, crypto_ctx_t
, void *);
102 int do_hmac(void *, uint8_t *, uint32_t);
104 void krb5_make_usage(uint32_t, uint8_t, uint8_t[KRB5_USAGE_LEN
]);
105 void krb5_key_derivation(crypto_ctx_t
, const void *, size_t, void **, size_t);
106 void cc_key_schedule_create(crypto_ctx_t
);
107 void gss_crypto_ctx_free(crypto_ctx_t
);
108 int gss_crypto_ctx_init(struct crypto_ctx
*, lucid_context_t
);
110 errno_t
krb5_crypt_mbuf(crypto_ctx_t
, mbuf_t
*, uint32_t, int, cccbc_ctx
*);
111 int krb5_mic(crypto_ctx_t
, gss_buffer_t
, gss_buffer_t
, gss_buffer_t
, uint8_t *, int *, int, int);
112 int krb5_mic_mbuf(crypto_ctx_t
, gss_buffer_t
, mbuf_t
, uint32_t, uint32_t, gss_buffer_t
, uint8_t *, int *, int, int);
114 uint32_t gss_krb5_cfx_get_mic(uint32_t *, gss_ctx_id_t
, gss_qop_t
, gss_buffer_t
, gss_buffer_t
);
115 uint32_t gss_krb5_cfx_verify_mic(uint32_t *, gss_ctx_id_t
, gss_buffer_t
, gss_buffer_t
, gss_qop_t
*);
116 uint32_t gss_krb5_cfx_get_mic_mbuf(uint32_t *, gss_ctx_id_t
, gss_qop_t
, mbuf_t
, size_t, size_t, gss_buffer_t
);
117 uint32_t gss_krb5_cfx_verify_mic_mbuf(uint32_t *, gss_ctx_id_t
, mbuf_t
, size_t, size_t, gss_buffer_t
, gss_qop_t
*);
118 errno_t
krb5_cfx_crypt_mbuf(crypto_ctx_t
, mbuf_t
*, size_t *, int, int);
119 uint32_t gss_krb5_cfx_wrap_mbuf(uint32_t *, gss_ctx_id_t
, int, gss_qop_t
, mbuf_t
*, size_t, int *);
120 uint32_t gss_krb5_cfx_unwrap_mbuf(uint32_t *, gss_ctx_id_t
, mbuf_t
*, size_t, int *, gss_qop_t
*);
122 int gss_krb5_mech_is_initialized(void);
123 void gss_krb5_mech_init(void);
125 /* Debugging routines */
127 printmbuf(const char *str
, mbuf_t mb
, uint32_t offset
, uint32_t len
)
132 len
= len
? len
: ~0;
133 printf("%s mbuf = %p offset = %d len = %d:\n", str
? str
: "mbuf", mb
, offset
, len
);
134 for (; mb
&& len
; mb
= mbuf_next(mb
)) {
135 if (offset
>= mbuf_len(mb
)) {
136 offset
-= mbuf_len(mb
);
139 for (i
= offset
; len
&& i
< mbuf_len(mb
); i
++) {
140 const char *s
= (cout
% 8) ? " " : (cout
% 16) ? " " : "\n";
141 printf("%02x%s", ((uint8_t *)mbuf_data(mb
))[i
], s
);
147 if ((cout
- 1) % 16) {
150 printf("Count chars %d\n", cout
- 1);
154 printgbuf(const char *str
, gss_buffer_t buf
)
157 size_t len
= buf
->length
> 128 ? 128 : buf
->length
;
159 printf("%s: len = %d value = %p\n", str
? str
: "buffer", (int)buf
->length
, buf
->value
);
160 for (i
= 0; i
< len
; i
++) {
161 const char *s
= ((i
+ 1) % 8) ? " " : ((i
+ 1) % 16) ? " " : "\n";
162 printf("%02x%s", ((uint8_t *)buf
->value
)[i
], s
);
170 * Initialize the data structures for the gss kerberos mech.
172 #define GSS_KRB5_NOT_INITIALIZED 0
173 #define GSS_KRB5_INITIALIZING 1
174 #define GSS_KRB5_INITIALIZED 2
175 static volatile uint32_t gss_krb5_mech_initted
= GSS_KRB5_NOT_INITIALIZED
;
178 gss_krb5_mech_is_initialized(void)
180 return gss_krb5_mech_initted
== GSS_KRB5_NOT_INITIALIZED
;
184 gss_krb5_mech_init(void)
186 extern void IOSleep(int);
188 /* Once initted always initted */
189 if (gss_krb5_mech_initted
== GSS_KRB5_INITIALIZED
) {
193 /* make sure we init only once */
194 if (!OSCompareAndSwap(GSS_KRB5_NOT_INITIALIZED
, GSS_KRB5_INITIALIZING
, &gss_krb5_mech_initted
)) {
195 /* wait until initialization is complete */
196 while (!gss_krb5_mech_is_initialized()) {
201 gss_krb5_mech_grp
= lck_grp_alloc_init("gss_krb5_mech", LCK_GRP_ATTR_NULL
);
202 gss_krb5_mech_initted
= GSS_KRB5_INITIALIZED
;
206 gss_release_buffer(uint32_t *minor
, gss_buffer_t buf
)
212 FREE(buf
->value
, M_TEMP
);
216 return GSS_S_COMPLETE
;
224 gss_mbuf_len(mbuf_t mb
, size_t offset
)
228 for (len
= 0; mb
; mb
= mbuf_next(mb
)) {
231 return (offset
> len
) ? 0 : len
- offset
;
235 * Split an mbuf in a chain into two mbufs such that the original mbuf
236 * points to the original mbuf and the new mbuf points to the rest of the
237 * chain. The first mbuf length is the first len bytes and the second
238 * mbuf contains the remaining bytes. if len is zero or equals
239 * mbuf_len(mb) the don't create a new mbuf. We are already at an mbuf
240 * boundary. Return the mbuf that starts at the offset.
243 split_one_mbuf(mbuf_t mb
, size_t offset
, mbuf_t
*nmb
, int join
)
248 /* We don't have an mbuf or we're alread on an mbuf boundary */
249 if (mb
== NULL
|| offset
== 0) {
253 /* If the mbuf length is offset then the next mbuf is the one we want */
254 if (mbuf_len(mb
) == offset
) {
255 *nmb
= mbuf_next(mb
);
257 mbuf_setnext(mb
, NULL
);
262 if (offset
> mbuf_len(mb
)) {
266 error
= mbuf_split(mb
, offset
, MBUF_WAITOK
, nmb
);
271 if (mbuf_flags(*nmb
) & MBUF_PKTHDR
) {
272 /* We don't want to copy the pkthdr. mbuf_split does that. */
273 error
= mbuf_setflags_mask(*nmb
, ~MBUF_PKTHDR
, MBUF_PKTHDR
);
277 /* Join the chain again */
278 mbuf_setnext(mb
, *nmb
);
285 * Given an mbuf with an offset and length return the chain such that
286 * offset and offset + *subchain_length are on mbuf boundaries. If
287 * *mbuf_length is less that the length of the chain after offset
288 * return that length in *mbuf_length. The mbuf sub chain starting at
289 * offset is returned in *subchain. If an error occurs return the
290 * corresponding errno. Note if there are less than offset bytes then
291 * subchain will be set to NULL and *subchain_length will be set to
292 * zero. If *subchain_length is 0; then set it to the length of the
293 * chain starting at offset. Join parameter is used to indicate whether
294 * the mbuf chain will be joined again as on chain, just rearranged so
295 * that offset and subchain_length are on mbuf boundaries.
299 gss_normalize_mbuf(mbuf_t chain
, size_t offset
, size_t *subchain_length
, mbuf_t
*subchain
, mbuf_t
*tail
, int join
)
301 size_t length
= *subchain_length
? *subchain_length
: ~0;
312 for (len
= offset
, mb
= chain
; mb
&& len
> mbuf_len(mb
); mb
= mbuf_next(mb
)) {
316 /* if we don't have offset bytes just return */
321 error
= split_one_mbuf(mb
, len
, subchain
, join
);
326 assert(subchain
!= NULL
&& *subchain
!= NULL
);
327 assert(offset
== 0 ? mb
== *subchain
: 1);
329 len
= gss_mbuf_len(*subchain
, 0);
330 length
= (length
> len
) ? len
: length
;
331 *subchain_length
= length
;
333 for (len
= length
, mb
= *subchain
; mb
&& len
> mbuf_len(mb
); mb
= mbuf_next(mb
)) {
337 error
= split_one_mbuf(mb
, len
, tail
, join
);
343 gss_join_mbuf(mbuf_t head
, mbuf_t body
, mbuf_t tail
)
347 for (mb
= head
; mb
&& mbuf_next(mb
); mb
= mbuf_next(mb
)) {
351 mbuf_setnext(mb
, body
);
353 for (mb
= body
; mb
&& mbuf_next(mb
); mb
= mbuf_next(mb
)) {
357 mbuf_setnext(mb
, tail
);
359 mb
= head
? head
: (body
? body
: tail
);
364 * Prepend size bytes to the mbuf chain.
367 gss_prepend_mbuf(mbuf_t
*chain
, uint8_t *bytes
, size_t size
)
369 uint8_t *data
= mbuf_data(*chain
);
370 size_t leading
= mbuf_leadingspace(*chain
);
371 size_t trailing
= mbuf_trailingspace(*chain
);
372 size_t mlen
= mbuf_len(*chain
);
375 if (size
> leading
&& size
<= leading
+ trailing
) {
376 data
= memmove(data
+ size
- leading
, data
, mlen
);
377 mbuf_setdata(*chain
, data
, mlen
);
380 error
= mbuf_prepend(chain
, size
, MBUF_WAITOK
);
384 data
= mbuf_data(*chain
);
385 memcpy(data
, bytes
, size
);
391 gss_append_mbuf(mbuf_t chain
, uint8_t *bytes
, size_t size
)
400 for (mb
= chain
; mb
; mb
= mbuf_next(mb
)) {
404 return mbuf_copyback(chain
, len
, size
, bytes
, MBUF_WAITOK
);
408 gss_strip_mbuf(mbuf_t chain
, ssize_t size
)
414 mbuf_adj(chain
, size
);
421 * Kerberos mech generic crypto support for mbufs
425 * Walk the mbuf after the given offset calling the passed in crypto function
426 * for len bytes. Note the length, len should be a multiple of the blocksize and
427 * there should be at least len bytes available after the offset in the mbuf chain.
428 * padding should be done before calling this routine.
431 mbuf_walk(mbuf_t mbp
, size_t offset
, size_t len
, size_t blocksize
, int (*crypto_fn
)(void *, uint8_t *data
, uint32_t length
), void *ctx
)
434 size_t mlen
, residue
;
438 /* Move to the start of the chain */
439 for (mb
= mbp
; mb
&& len
> 0; mb
= mbuf_next(mb
)) {
442 if (offset
>= mlen
) {
443 /* Offset not yet reached */
447 /* Found starting point in chain */
453 * Handle the data in this mbuf. If the length to
454 * walk is less than the data in the mbuf, set
455 * the mbuf length left to be the length left
457 mlen
= mlen
< len
? mlen
: len
;
458 /* Figure out how much is a multple of blocksize */
459 residue
= mlen
% blocksize
;
460 /* And addjust the mleft length to be the largest multiple of blocksized */
462 /* run our hash/encrypt/decrpyt function */
464 error
= crypto_fn(ctx
, ptr
, mlen
);
472 * If we have a residue then to get a full block for our crypto
473 * function, we need to copy the residue into our block size
474 * block and use the next mbuf to get the rest of the data for
475 * the block. N.B. We generally assume that from the offset
476 * passed in, that the total length, len, is a multple of
477 * blocksize and that there are at least len bytes in the chain
478 * from the offset. We also assume there is at least (blocksize
479 * - residue) size data in any next mbuf for residue > 0. If not
480 * we attemp to pullup bytes from down the chain.
483 mbuf_t nmb
= mbuf_next(mb
);
484 uint8_t *nptr
= NULL
, block
[blocksize
];
488 offset
= blocksize
- residue
;
492 * We don't have enough bytes so zero the block
493 * so that any trailing bytes will be zero.
495 cc_clear(sizeof(block
), block
);
497 memcpy(block
, ptr
, residue
);
499 mlen
= mbuf_len(nmb
);
501 error
= mbuf_pullup(&nmb
, offset
- mlen
);
503 mbuf_setnext(mb
, NULL
);
507 nptr
= mbuf_data(nmb
);
508 memcpy(block
+ residue
, nptr
, offset
);
511 error
= crypto_fn(ctx
, block
, sizeof(block
));
515 memcpy(ptr
, block
, residue
);
517 memcpy(nptr
, block
+ residue
, offset
);
526 do_crypt_init(crypt_walker_ctx_t wctx
, int encrypt
, crypto_ctx_t cctx
, cccbc_ctx
*ks
)
528 wctx
->ccmode
= encrypt
? cctx
->enc_mode
: cctx
->dec_mode
;
530 wctx
->crypt_ctx
= ks
;
531 MALLOC(wctx
->iv
, cccbc_iv
*, wctx
->ccmode
->block_size
, M_TEMP
, M_WAITOK
| M_ZERO
);
532 cccbc_set_iv(wctx
->ccmode
, wctx
->iv
, NULL
);
536 do_crypt(void *walker
, uint8_t *data
, uint32_t len
)
538 struct crypt_walker_ctx
*wctx
= (crypt_walker_ctx_t
)walker
;
541 nblocks
= len
/ wctx
->ccmode
->block_size
;
542 assert(len
% wctx
->ccmode
->block_size
== 0);
543 cccbc_update(wctx
->ccmode
, wctx
->crypt_ctx
, wctx
->iv
, nblocks
, data
, data
);
550 do_hmac_init(hmac_walker_ctx_t wctx
, crypto_ctx_t cctx
, void *key
)
552 size_t alloc_size
= cchmac_di_size(cctx
->di
);
555 MALLOC(wctx
->hmac_ctx
, struct cchmac_ctx
*, alloc_size
, M_TEMP
, M_WAITOK
| M_ZERO
);
556 cchmac_init(cctx
->di
, wctx
->hmac_ctx
, cctx
->keylen
, key
);
560 do_hmac(void *walker
, uint8_t *data
, uint32_t len
)
562 hmac_walker_ctx_t wctx
= (hmac_walker_ctx_t
)walker
;
564 cchmac_update(wctx
->di
, wctx
->hmac_ctx
, len
, data
);
571 krb5_mic(crypto_ctx_t ctx
, gss_buffer_t header
, gss_buffer_t bp
, gss_buffer_t trailer
, uint8_t *mic
, int *verify
, int ikey
, int reverse
)
573 uint8_t digest
[ctx
->di
->output_size
];
574 cchmac_di_decl(ctx
->di
, hmac_ctx
);
575 int kdx
= (verify
== NULL
) ? (reverse
? GSS_RCV
: GSS_SND
) : (reverse
? GSS_SND
: GSS_RCV
);
579 if (!(ctx
->flags
& CRYPTO_KS_ALLOCED
)) {
580 lck_mtx_lock(ctx
->lock
);
581 if (!(ctx
->flags
& CRYPTO_KS_ALLOCED
)) {
582 cc_key_schedule_create(ctx
);
584 ctx
->flags
|= CRYPTO_KS_ALLOCED
;
585 lck_mtx_unlock(ctx
->lock
);
587 key2use
= ctx
->ks
.ikey
[kdx
];
589 key2use
= ctx
->ckey
[kdx
];
592 cchmac_init(ctx
->di
, hmac_ctx
, ctx
->keylen
, key2use
);
595 cchmac_update(ctx
->di
, hmac_ctx
, header
->length
, header
->value
);
598 cchmac_update(ctx
->di
, hmac_ctx
, bp
->length
, bp
->value
);
601 cchmac_update(ctx
->di
, hmac_ctx
, trailer
->length
, trailer
->value
);
604 cchmac_final(ctx
->di
, hmac_ctx
, digest
);
607 *verify
= (memcmp(mic
, digest
, ctx
->digest_size
) == 0);
609 memcpy(mic
, digest
, ctx
->digest_size
);
616 krb5_mic_mbuf(crypto_ctx_t ctx
, gss_buffer_t header
,
617 mbuf_t mbp
, uint32_t offset
, uint32_t len
, gss_buffer_t trailer
, uint8_t *mic
, int *verify
, int ikey
, int reverse
)
619 struct hmac_walker_ctx wctx
;
620 uint8_t digest
[ctx
->di
->output_size
];
622 int kdx
= (verify
== NULL
) ? (reverse
? GSS_RCV
: GSS_SND
) : (reverse
? GSS_SND
: GSS_RCV
);
626 if (!(ctx
->flags
& CRYPTO_KS_ALLOCED
)) {
627 lck_mtx_lock(ctx
->lock
);
628 if (!(ctx
->flags
& CRYPTO_KS_ALLOCED
)) {
629 cc_key_schedule_create(ctx
);
631 ctx
->flags
|= CRYPTO_KS_ALLOCED
;
632 lck_mtx_unlock(ctx
->lock
);
634 key2use
= ctx
->ks
.ikey
[kdx
];
636 key2use
= ctx
->ckey
[kdx
];
639 do_hmac_init(&wctx
, ctx
, key2use
);
642 cchmac_update(ctx
->di
, wctx
.hmac_ctx
, header
->length
, header
->value
);
645 error
= mbuf_walk(mbp
, offset
, len
, 1, do_hmac
, &wctx
);
651 cchmac_update(ctx
->di
, wctx
.hmac_ctx
, trailer
->length
, trailer
->value
);
654 cchmac_final(ctx
->di
, wctx
.hmac_ctx
, digest
);
655 FREE(wctx
.hmac_ctx
, M_TEMP
);
658 *verify
= (memcmp(mic
, digest
, ctx
->digest_size
) == 0);
663 memcpy(mic
, digest
, ctx
->digest_size
);
670 /* __attribute__((optnone)) */
671 krb5_crypt_mbuf(crypto_ctx_t ctx
, mbuf_t
*mbp
, uint32_t len
, int encrypt
, cccbc_ctx
*ks
)
673 struct crypt_walker_ctx wctx
;
674 const struct ccmode_cbc
*ccmode
= encrypt
? ctx
->enc_mode
: ctx
->dec_mode
;
680 if (!(ctx
->flags
& CRYPTO_KS_ALLOCED
)) {
681 lck_mtx_lock(ctx
->lock
);
682 if (!(ctx
->flags
& CRYPTO_KS_ALLOCED
)) {
683 cc_key_schedule_create(ctx
);
685 ctx
->flags
|= CRYPTO_KS_ALLOCED
;
686 lck_mtx_unlock(ctx
->lock
);
689 ks
= encrypt
? ctx
->ks
.enc
: ctx
->ks
.dec
;
692 if ((ctx
->flags
& CRYPTO_CTS_ENABLE
) && ctx
->mpad
== 1) {
693 uint8_t block
[ccmode
->block_size
];
694 /* if the length is less than or equal to a blocksize. We just encrypt the block */
695 if (len
<= ccmode
->block_size
) {
696 if (len
< ccmode
->block_size
) {
697 memset(block
, 0, sizeof(block
));
698 gss_append_mbuf(*mbp
, block
, ccmode
->block_size
);
700 plen
= ccmode
->block_size
;
702 /* determine where the last two blocks are */
703 uint32_t r
= len
% ccmode
->block_size
;
705 cts_len
= r
? r
+ ccmode
->block_size
: 2 * ccmode
->block_size
;
706 plen
= len
- cts_len
;
707 /* If plen is 0 we only have two blocks to crypt with ccpad below */
711 gss_normalize_mbuf(*mbp
, 0, &plen
, &mb
, &lmb
, 0);
713 assert(plen
== len
- cts_len
);
714 assert(gss_mbuf_len(mb
, 0) == plen
);
715 assert(gss_mbuf_len(lmb
, 0) == cts_len
);
718 } else if (len
% ctx
->mpad
) {
719 uint8_t pad_block
[ctx
->mpad
];
720 size_t padlen
= ctx
->mpad
- (len
% ctx
->mpad
);
722 memset(pad_block
, 0, padlen
);
723 error
= gss_append_mbuf(*mbp
, pad_block
, padlen
);
729 do_crypt_init(&wctx
, encrypt
, ctx
, ks
);
731 error
= mbuf_walk(*mbp
, 0, plen
, ccmode
->block_size
, do_crypt
, &wctx
);
737 if ((ctx
->flags
& CRYPTO_CTS_ENABLE
) && cts_len
) {
738 uint8_t cts_pad
[2 * ccmode
->block_size
];
739 ccpad_func do_ccpad
= encrypt
? ccpad_cts3_encrypt
: ccpad_cts3_decrypt
;
741 assert(cts_len
<= 2 * ccmode
->block_size
&& cts_len
> ccmode
->block_size
);
742 memset(cts_pad
, 0, sizeof(cts_pad
));
743 mbuf_copydata(lmb
, 0, cts_len
, cts_pad
);
745 do_ccpad(ccmode
, wctx
.crypt_ctx
, wctx
.iv
, cts_len
, cts_pad
, cts_pad
);
746 gss_append_mbuf(*mbp
, cts_pad
, cts_len
);
748 FREE(wctx
.iv
, M_TEMP
);
754 * Key derivation routines
758 rr13(unsigned char *buf
, size_t len
)
760 size_t bytes
= (len
+ 7) / 8;
761 unsigned char tmp
[bytes
];
769 const int bits
= 13 % len
;
770 const int lbit
= len
% 8;
772 memcpy(tmp
, buf
, bytes
);
774 /* pad final byte with inital bits */
775 tmp
[bytes
- 1] &= 0xff << (8 - lbit
);
776 for (i
= lbit
; i
< 8; i
+= len
) {
777 tmp
[bytes
- 1] |= buf
[0] >> i
;
780 for (i
= 0; i
< bytes
; i
++) {
782 ssize_t b1
, s1
, b2
, s2
;
784 /* calculate first bit position of this byte */
789 /* byte offset and shift count */
792 if ((size_t)bb
+ 8 > bytes
* 8) {
793 /* watch for wraparound */
794 s2
= (len
+ 8 - s1
) % 8;
798 b2
= (b1
+ 1) % bytes
;
799 buf
[i
] = (tmp
[b1
] << s1
) | (tmp
[b2
] >> s2
);
806 /* Add `b' to `a', both being one's complement numbers. */
808 add1(unsigned char *a
, unsigned char *b
, size_t len
)
813 for (i
= len
- 1; i
>= 0; i
--) {
814 int x
= a
[i
] + b
[i
] + carry
;
818 for (i
= len
- 1; carry
&& i
>= 0; i
--) {
819 int x
= a
[i
] + carry
;
827 krb5_n_fold(const void *instr
, size_t len
, void *foldstr
, size_t size
)
829 /* if len < size we need at most N * len bytes, ie < 2 * size;
830 * if len > size we need at most 2 * len */
832 size_t maxlen
= 2 * max(size
, len
);
834 unsigned char tmp
[maxlen
];
835 unsigned char buf
[len
];
837 memcpy(buf
, instr
, len
);
838 memset(foldstr
, 0, size
);
840 memcpy(tmp
+ l
, buf
, len
);
842 ret
= rr13(buf
, len
* 8);
847 add1(foldstr
, tmp
, size
);
852 memmove(tmp
, tmp
+ size
, l
);
861 krb5_make_usage(uint32_t usage_no
, uint8_t suffix
, uint8_t usage_string
[KRB5_USAGE_LEN
])
865 for (i
= 0; i
< 4; i
++) {
866 usage_string
[i
] = ((usage_no
>> 8 * (3 - i
)) & 0xff);
868 usage_string
[i
] = suffix
;
872 krb5_key_derivation(crypto_ctx_t ctx
, const void *cons
, size_t conslen
, void **dkey
, size_t dklen
)
874 size_t blocksize
= ctx
->enc_mode
->block_size
;
875 cccbc_iv_decl(blocksize
, iv
);
876 cccbc_ctx_decl(ctx
->enc_mode
->size
, enc_ctx
);
877 size_t ksize
= 8 * dklen
;
878 size_t nblocks
= (ksize
+ 8 * blocksize
- 1) / (8 * blocksize
);
880 uint8_t block
[blocksize
];
882 MALLOC(*dkey
, void *, nblocks
* blocksize
, M_TEMP
, M_WAITOK
| M_ZERO
);
885 krb5_n_fold(cons
, conslen
, block
, blocksize
);
886 cccbc_init(ctx
->enc_mode
, enc_ctx
, ctx
->keylen
, ctx
->key
);
887 for (size_t i
= 0; i
< nblocks
; i
++) {
888 cccbc_set_iv(ctx
->enc_mode
, iv
, NULL
);
889 cccbc_update(ctx
->enc_mode
, enc_ctx
, iv
, 1, block
, block
);
890 memcpy(dkptr
, block
, blocksize
);
896 des_make_key(const uint8_t rawkey
[7], uint8_t deskey
[8])
900 memcpy(deskey
, rawkey
, 7);
901 for (int i
= 0; i
< 7; i
++) {
902 val
|= ((deskey
[i
] & 1) << (i
+ 1));
905 ccdes_key_set_odd_parity(deskey
, 8);
909 krb5_3des_key_derivation(crypto_ctx_t ctx
, const void *cons
, size_t conslen
, void **des3key
)
911 const struct ccmode_cbc
*cbcmode
= ctx
->enc_mode
;
913 uint8_t *kptr
, *rptr
;
915 MALLOC(*des3key
, void *, 3 * cbcmode
->block_size
, M_TEMP
, M_WAITOK
| M_ZERO
);
916 krb5_key_derivation(ctx
, cons
, conslen
, &rawkey
, 3 * (cbcmode
->block_size
- 1));
917 kptr
= (uint8_t *)*des3key
;
918 rptr
= (uint8_t *)rawkey
;
920 for (int i
= 0; i
< 3; i
++) {
921 des_make_key(rptr
, kptr
);
922 rptr
+= cbcmode
->block_size
- 1;
923 kptr
+= cbcmode
->block_size
;
926 cc_clear(3 * (cbcmode
->block_size
- 1), rawkey
);
927 FREE(rawkey
, M_TEMP
);
931 * Create a key schecule
935 cc_key_schedule_create(crypto_ctx_t ctx
)
937 uint8_t usage_string
[KRB5_USAGE_LEN
];
938 lucid_context_t lctx
= ctx
->gss_ctx
;
941 switch (lctx
->key_data
.proto
) {
943 if (ctx
->ks
.enc
== NULL
) {
944 MALLOC(ctx
->ks
.enc
, cccbc_ctx
*, ctx
->enc_mode
->size
, M_TEMP
, M_WAITOK
| M_ZERO
);
945 cccbc_init(ctx
->enc_mode
, ctx
->ks
.enc
, ctx
->keylen
, ctx
->key
);
947 if (ctx
->ks
.dec
== NULL
) {
948 MALLOC(ctx
->ks
.dec
, cccbc_ctx
*, ctx
->dec_mode
->size
, M_TEMP
, M_WAITOK
| M_ZERO
);
949 cccbc_init(ctx
->dec_mode
, ctx
->ks
.dec
, ctx
->keylen
, ctx
->key
);
953 if (ctx
->ks
.enc
== NULL
) {
954 krb5_make_usage(lctx
->initiate
?
955 KRB5_USAGE_INITIATOR_SEAL
: KRB5_USAGE_ACCEPTOR_SEAL
,
957 krb5_key_derivation(ctx
, usage_string
, KRB5_USAGE_LEN
, &ekey
, ctx
->keylen
);
958 MALLOC(ctx
->ks
.enc
, cccbc_ctx
*, ctx
->enc_mode
->size
, M_TEMP
, M_WAITOK
| M_ZERO
);
959 cccbc_init(ctx
->enc_mode
, ctx
->ks
.enc
, ctx
->keylen
, ekey
);
962 if (ctx
->ks
.dec
== NULL
) {
963 krb5_make_usage(lctx
->initiate
?
964 KRB5_USAGE_ACCEPTOR_SEAL
: KRB5_USAGE_INITIATOR_SEAL
,
966 krb5_key_derivation(ctx
, usage_string
, KRB5_USAGE_LEN
, &ekey
, ctx
->keylen
);
967 MALLOC(ctx
->ks
.dec
, cccbc_ctx
*, ctx
->dec_mode
->size
, M_TEMP
, M_WAITOK
| M_ZERO
);
968 cccbc_init(ctx
->dec_mode
, ctx
->ks
.dec
, ctx
->keylen
, ekey
);
971 if (ctx
->ks
.ikey
[GSS_SND
] == NULL
) {
972 krb5_make_usage(lctx
->initiate
?
973 KRB5_USAGE_INITIATOR_SEAL
: KRB5_USAGE_ACCEPTOR_SEAL
,
975 krb5_key_derivation(ctx
, usage_string
, KRB5_USAGE_LEN
, &ctx
->ks
.ikey
[GSS_SND
], ctx
->keylen
);
977 if (ctx
->ks
.ikey
[GSS_RCV
] == NULL
) {
978 krb5_make_usage(lctx
->initiate
?
979 KRB5_USAGE_ACCEPTOR_SEAL
: KRB5_USAGE_INITIATOR_SEAL
,
981 krb5_key_derivation(ctx
, usage_string
, KRB5_USAGE_LEN
, &ctx
->ks
.ikey
[GSS_RCV
], ctx
->keylen
);
988 gss_crypto_ctx_free(crypto_ctx_t ctx
)
990 ctx
->ks
.ikey
[GSS_SND
] = NULL
;
991 if (ctx
->ks
.ikey
[GSS_RCV
] && ctx
->key
!= ctx
->ks
.ikey
[GSS_RCV
]) {
992 cc_clear(ctx
->keylen
, ctx
->ks
.ikey
[GSS_RCV
]);
993 FREE(ctx
->ks
.ikey
[GSS_RCV
], M_TEMP
);
995 ctx
->ks
.ikey
[GSS_RCV
] = NULL
;
997 cccbc_ctx_clear(ctx
->enc_mode
->size
, ctx
->ks
.enc
);
998 FREE(ctx
->ks
.enc
, M_TEMP
);
1002 cccbc_ctx_clear(ctx
->dec_mode
->size
, ctx
->ks
.dec
);
1003 FREE(ctx
->ks
.dec
, M_TEMP
);
1006 if (ctx
->ckey
[GSS_SND
] && ctx
->ckey
[GSS_SND
] != ctx
->key
) {
1007 cc_clear(ctx
->keylen
, ctx
->ckey
[GSS_SND
]);
1008 FREE(ctx
->ckey
[GSS_SND
], M_TEMP
);
1010 ctx
->ckey
[GSS_SND
] = NULL
;
1011 if (ctx
->ckey
[GSS_RCV
] && ctx
->ckey
[GSS_RCV
] != ctx
->key
) {
1012 cc_clear(ctx
->keylen
, ctx
->ckey
[GSS_RCV
]);
1013 FREE(ctx
->ckey
[GSS_RCV
], M_TEMP
);
1015 ctx
->ckey
[GSS_RCV
] = NULL
;
1021 gss_crypto_ctx_init(struct crypto_ctx
*ctx
, lucid_context_t lucid
)
1023 ctx
->gss_ctx
= lucid
;
1025 uint8_t usage_string
[KRB5_USAGE_LEN
];
1027 ctx
->keylen
= ctx
->gss_ctx
->ctx_key
.key
.key_len
;
1028 key
= ctx
->gss_ctx
->ctx_key
.key
.key_val
;
1029 ctx
->etype
= ctx
->gss_ctx
->ctx_key
.etype
;
1032 switch (ctx
->etype
) {
1033 case AES128_CTS_HMAC_SHA1_96
:
1034 case AES256_CTS_HMAC_SHA1_96
:
1035 ctx
->enc_mode
= ccaes_cbc_encrypt_mode();
1036 assert(ctx
->enc_mode
);
1037 ctx
->dec_mode
= ccaes_cbc_decrypt_mode();
1038 assert(ctx
->dec_mode
);
1041 ctx
->di
= ccsha1_di();
1043 ctx
->flags
= CRYPTO_CTS_ENABLE
;
1045 ctx
->digest_size
= 12; /* 96 bits */
1046 krb5_make_usage(ctx
->gss_ctx
->initiate
?
1047 KRB5_USAGE_INITIATOR_SIGN
: KRB5_USAGE_ACCEPTOR_SIGN
,
1048 0x99, usage_string
);
1049 krb5_key_derivation(ctx
, usage_string
, KRB5_USAGE_LEN
, &ctx
->ckey
[GSS_SND
], ctx
->keylen
);
1050 krb5_make_usage(ctx
->gss_ctx
->initiate
?
1051 KRB5_USAGE_ACCEPTOR_SIGN
: KRB5_USAGE_INITIATOR_SIGN
,
1052 0x99, usage_string
);
1053 krb5_key_derivation(ctx
, usage_string
, KRB5_USAGE_LEN
, &ctx
->ckey
[GSS_RCV
], ctx
->keylen
);
1055 case DES3_CBC_SHA1_KD
:
1056 ctx
->enc_mode
= ccdes3_cbc_encrypt_mode();
1057 assert(ctx
->enc_mode
);
1058 ctx
->dec_mode
= ccdes3_cbc_decrypt_mode();
1059 assert(ctx
->dec_mode
);
1060 ctx
->ks
.ikey
[GSS_SND
] = ctx
->key
;
1061 ctx
->ks
.ikey
[GSS_RCV
] = ctx
->key
;
1062 ctx
->di
= ccsha1_di();
1065 ctx
->mpad
= ctx
->enc_mode
->block_size
;
1066 ctx
->digest_size
= 20; /* 160 bits */
1067 krb5_make_usage(KRB5_USAGE_ACCEPTOR_SIGN
, 0x99, usage_string
);
1068 krb5_3des_key_derivation(ctx
, usage_string
, KRB5_USAGE_LEN
, &ctx
->ckey
[GSS_SND
]);
1069 krb5_3des_key_derivation(ctx
, usage_string
, KRB5_USAGE_LEN
, &ctx
->ckey
[GSS_RCV
]);
1075 ctx
->lock
= lck_mtx_alloc_init(gss_krb5_mech_grp
, LCK_ATTR_NULL
);
1081 * CFX gss support routines
1083 /* From Heimdal cfx.h file RFC 4121 Cryptoo framework extensions */
1084 typedef struct gss_cfx_mic_token_desc_struct
{
1085 uint8_t TOK_ID
[2]; /* 04 04 */
1089 } gss_cfx_mic_token_desc
, *gss_cfx_mic_token
;
1091 typedef struct gss_cfx_wrap_token_desc_struct
{
1092 uint8_t TOK_ID
[2]; /* 05 04 */
1098 } gss_cfx_wrap_token_desc
, *gss_cfx_wrap_token
;
1100 /* End of cfx.h file */
1102 #define CFXSentByAcceptor (1 << 0)
1103 #define CFXSealed (1 << 1)
1104 #define CFXAcceptorSubkey (1 << 2)
1106 const gss_cfx_mic_token_desc mic_cfx_token
= {
1107 .TOK_ID
= "\x04\x04",
1109 .Filler
= "\xff\xff\xff\xff\xff",
1110 .SND_SEQ
= "\x00\x00\x00\x00\x00\x00\x00\x00"
1113 const gss_cfx_wrap_token_desc wrap_cfx_token
= {
1114 .TOK_ID
= "\x05\04",
1119 .SND_SEQ
= "\x00\x00\x00\x00\x00\x00\x00\x00"
1123 gss_krb5_cfx_verify_mic_token(gss_ctx_id_t ctx
, gss_cfx_mic_token token
)
1126 lucid_context_t lctx
= &ctx
->gss_lucid_ctx
;
1129 if (token
->TOK_ID
[0] != mic_cfx_token
.TOK_ID
[0] || token
->TOK_ID
[1] != mic_cfx_token
.TOK_ID
[1]) {
1130 printf("Bad mic TOK_ID %x %x\n", token
->TOK_ID
[0], token
->TOK_ID
[1]);
1133 if (lctx
->initiate
) {
1134 flags
|= CFXSentByAcceptor
;
1136 if (lctx
->key_data
.lucid_protocol_u
.data_4121
.acceptor_subkey
) {
1137 flags
|= CFXAcceptorSubkey
;
1139 if (token
->Flags
!= flags
) {
1140 printf("Bad flags received %x exptect %x\n", token
->Flags
, flags
);
1143 for (i
= 0; i
< 5; i
++) {
1144 if (token
->Filler
[i
] != mic_cfx_token
.Filler
[i
]) {
1150 printf("Bad mic filler %x @ %d\n", token
->Filler
[i
], i
);
1158 gss_krb5_cfx_get_mic(uint32_t *minor
, /* minor_status */
1159 gss_ctx_id_t ctx
, /* context_handle */
1160 gss_qop_t qop __unused
, /* qop_req (ignored) */
1161 gss_buffer_t mbp
, /* message mbuf */
1162 gss_buffer_t mic
/* message_token */)
1164 gss_cfx_mic_token_desc token
;
1165 lucid_context_t lctx
= &ctx
->gss_lucid_ctx
;
1166 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
1167 gss_buffer_desc header
;
1169 uint64_t seq
= htonll(lctx
->send_seq
);
1171 if (minor
== NULL
) {
1175 token
= mic_cfx_token
;
1176 mic
->length
= sizeof(token
) + cctx
->digest_size
;
1177 MALLOC(mic
->value
, void *, mic
->length
, M_TEMP
, M_WAITOK
| M_ZERO
);
1178 if (!lctx
->initiate
) {
1179 token
.Flags
|= CFXSentByAcceptor
;
1181 if (lctx
->key_data
.lucid_protocol_u
.data_4121
.acceptor_subkey
) {
1182 token
.Flags
|= CFXAcceptorSubkey
;
1184 memcpy(&token
.SND_SEQ
, &seq
, sizeof(lctx
->send_seq
));
1185 lctx
->send_seq
++; //XXX should only update this below on success? Heimdal seems to do it this way
1186 header
.value
= &token
;
1187 header
.length
= sizeof(gss_cfx_mic_token_desc
);
1189 *minor
= krb5_mic(cctx
, NULL
, mbp
, &header
, (uint8_t *)mic
->value
+ sizeof(token
), NULL
, 0, 0);
1193 FREE(mic
->value
, M_TEMP
);
1196 memcpy(mic
->value
, &token
, sizeof(token
));
1199 return *minor
? GSS_S_FAILURE
: GSS_S_COMPLETE
;
1203 gss_krb5_cfx_verify_mic(uint32_t *minor
, /* minor_status */
1204 gss_ctx_id_t ctx
, /* context_handle */
1205 gss_buffer_t mbp
, /* message_buffer */
1206 gss_buffer_t mic
, /* message_token */
1207 gss_qop_t
*qop
/* qop_state */)
1209 gss_cfx_mic_token token
= mic
->value
;
1210 lucid_context_t lctx
= &ctx
->gss_lucid_ctx
;
1211 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
1212 uint8_t *digest
= (uint8_t *)mic
->value
+ sizeof(gss_cfx_mic_token_desc
);
1216 gss_buffer_desc header
;
1219 *qop
= GSS_C_QOP_DEFAULT
;
1221 if (minor
== NULL
) {
1225 if (mic
->length
!= sizeof(gss_cfx_mic_token_desc
) + cctx
->digest_size
) {
1226 printf("mic token wrong length\n");
1230 *minor
= gss_krb5_cfx_verify_mic_token(ctx
, token
);
1232 return GSS_S_FAILURE
;
1234 header
.value
= token
;
1235 header
.length
= sizeof(gss_cfx_mic_token_desc
);
1236 *minor
= krb5_mic(cctx
, NULL
, mbp
, &header
, digest
, &verified
, 0, 0);
1239 //XXX errors and such? Sequencing and replay? Not supported in RPCSEC_GSS
1240 memcpy(&seq
, token
->SND_SEQ
, sizeof(uint64_t));
1242 lctx
->recv_seq
= seq
;
1246 return verified
? GSS_S_COMPLETE
: GSS_S_BAD_SIG
;
1250 gss_krb5_cfx_get_mic_mbuf(uint32_t *minor
, /* minor_status */
1251 gss_ctx_id_t ctx
, /* context_handle */
1252 gss_qop_t qop __unused
, /* qop_req (ignored) */
1253 mbuf_t mbp
, /* message mbuf */
1254 size_t offset
, /* offest */
1255 size_t len
, /* length */
1256 gss_buffer_t mic
/* message_token */)
1258 gss_cfx_mic_token_desc token
;
1259 lucid_context_t lctx
= &ctx
->gss_lucid_ctx
;
1260 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
1262 uint64_t seq
= htonll(lctx
->send_seq
);
1263 gss_buffer_desc header
;
1265 if (minor
== NULL
) {
1270 token
= mic_cfx_token
;
1271 mic
->length
= sizeof(token
) + cctx
->digest_size
;
1272 MALLOC(mic
->value
, void *, mic
->length
, M_TEMP
, M_WAITOK
| M_ZERO
);
1273 if (!lctx
->initiate
) {
1274 token
.Flags
|= CFXSentByAcceptor
;
1276 if (lctx
->key_data
.lucid_protocol_u
.data_4121
.acceptor_subkey
) {
1277 token
.Flags
|= CFXAcceptorSubkey
;
1280 memcpy(&token
.SND_SEQ
, &seq
, sizeof(lctx
->send_seq
));
1281 lctx
->send_seq
++; //XXX should only update this below on success? Heimdal seems to do it this way
1283 header
.length
= sizeof(token
);
1284 header
.value
= &token
;
1286 len
= len
? len
: gss_mbuf_len(mbp
, offset
);
1287 *minor
= krb5_mic_mbuf(cctx
, NULL
, mbp
, offset
, len
, &header
, (uint8_t *)mic
->value
+ sizeof(token
), NULL
, 0, 0);
1291 FREE(mic
->value
, M_TEMP
);
1294 memcpy(mic
->value
, &token
, sizeof(token
));
1297 return *minor
? GSS_S_FAILURE
: GSS_S_COMPLETE
;
1302 gss_krb5_cfx_verify_mic_mbuf(uint32_t *minor
, /* minor_status */
1303 gss_ctx_id_t ctx
, /* context_handle */
1304 mbuf_t mbp
, /* message_buffer */
1305 size_t offset
, /* offset */
1306 size_t len
, /* length */
1307 gss_buffer_t mic
, /* message_token */
1308 gss_qop_t
*qop
/* qop_state */)
1310 gss_cfx_mic_token token
= mic
->value
;
1311 lucid_context_t lctx
= &ctx
->gss_lucid_ctx
;
1312 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
1313 uint8_t *digest
= (uint8_t *)mic
->value
+ sizeof(gss_cfx_mic_token_desc
);
1317 gss_buffer_desc header
;
1320 *qop
= GSS_C_QOP_DEFAULT
;
1323 if (minor
== NULL
) {
1327 *minor
= gss_krb5_cfx_verify_mic_token(ctx
, token
);
1329 return GSS_S_FAILURE
;
1332 header
.length
= sizeof(gss_cfx_mic_token_desc
);
1333 header
.value
= mic
->value
;
1335 *minor
= krb5_mic_mbuf(cctx
, NULL
, mbp
, offset
, len
, &header
, digest
, &verified
, 0, 0);
1337 return GSS_S_FAILURE
;
1340 //XXX errors and such? Sequencing and replay? Not Supported RPCSEC_GSS
1341 memcpy(&seq
, token
->SND_SEQ
, sizeof(uint64_t));
1343 lctx
->recv_seq
= seq
;
1345 return verified
? GSS_S_COMPLETE
: GSS_S_BAD_SIG
;
1349 krb5_cfx_crypt_mbuf(crypto_ctx_t ctx
, mbuf_t
*mbp
, size_t *len
, int encrypt
, int reverse
)
1351 const struct ccmode_cbc
*ccmode
= encrypt
? ctx
->enc_mode
: ctx
->dec_mode
;
1352 uint8_t confounder
[ccmode
->block_size
];
1353 uint8_t digest
[ctx
->digest_size
];
1358 read_random(confounder
, ccmode
->block_size
);
1359 error
= gss_prepend_mbuf(mbp
, confounder
, ccmode
->block_size
);
1363 tlen
= *len
+ ccmode
->block_size
;
1364 if (ctx
->mpad
> 1) {
1365 r
= ctx
->mpad
- (tlen
% ctx
->mpad
);
1367 /* We expect that r == 0 from krb5_cfx_wrap */
1371 error
= gss_append_mbuf(*mbp
, mpad
, r
);
1377 error
= krb5_mic_mbuf(ctx
, NULL
, *mbp
, 0, tlen
, NULL
, digest
, NULL
, 1, 0);
1381 error
= krb5_crypt_mbuf(ctx
, mbp
, tlen
, 1, NULL
);
1385 error
= gss_append_mbuf(*mbp
, digest
, ctx
->digest_size
);
1389 *len
= tlen
+ ctx
->digest_size
;
1393 cccbc_ctx
*ks
= NULL
;
1395 if (*len
< ctx
->digest_size
+ sizeof(confounder
)) {
1398 tlen
= *len
- ctx
->digest_size
;
1399 /* get the digest */
1400 error
= mbuf_copydata(*mbp
, tlen
, ctx
->digest_size
, digest
);
1401 /* Remove the digest from the mbuffer */
1402 error
= gss_strip_mbuf(*mbp
, -ctx
->digest_size
);
1409 * Derive a key schedule that the sender can unwrap with. This
1410 * is so that RPCSEC_GSS can restore encrypted arguments for
1411 * resending. We do that because the RPCSEC_GSS sequence number in
1412 * the rpc header is prepended to the body of the message before wrapping.
1415 uint8_t usage_string
[KRB5_USAGE_LEN
];
1416 lucid_context_t lctx
= ctx
->gss_ctx
;
1418 krb5_make_usage(lctx
->initiate
?
1419 KRB5_USAGE_INITIATOR_SEAL
: KRB5_USAGE_ACCEPTOR_SEAL
,
1420 0xAA, usage_string
);
1421 krb5_key_derivation(ctx
, usage_string
, KRB5_USAGE_LEN
, &ekey
, ctx
->keylen
);
1422 MALLOC(ks
, cccbc_ctx
*, ctx
->dec_mode
->size
, M_TEMP
, M_WAITOK
| M_ZERO
);
1423 cccbc_init(ctx
->dec_mode
, ks
, ctx
->keylen
, ekey
);
1426 error
= krb5_crypt_mbuf(ctx
, mbp
, tlen
, 0, ks
);
1431 error
= krb5_mic_mbuf(ctx
, NULL
, *mbp
, 0, tlen
, NULL
, digest
, &verf
, 1, reverse
);
1438 /* strip off the confounder */
1439 error
= gss_strip_mbuf(*mbp
, ccmode
->block_size
);
1443 *len
= tlen
- ccmode
->block_size
;
1449 gss_krb5_cfx_wrap_mbuf(uint32_t *minor
, /* minor_status */
1450 gss_ctx_id_t ctx
, /* context_handle */
1451 int conf_flag
, /* conf_req_flag */
1452 gss_qop_t qop __unused
, /* qop_req */
1453 mbuf_t
*mbp
, /* input/output message_buffer */
1454 size_t len
, /* mbuf chain length */
1455 int *conf
/* conf_state */)
1457 gss_cfx_wrap_token_desc token
;
1458 lucid_context_t lctx
= &ctx
->gss_lucid_ctx
;
1459 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
1462 uint64_t seq
= htonll(lctx
->send_seq
);
1464 if (minor
== NULL
) {
1472 token
= wrap_cfx_token
;
1473 if (!lctx
->initiate
) {
1474 token
.Flags
|= CFXSentByAcceptor
;
1476 if (lctx
->key_data
.lucid_protocol_u
.data_4121
.acceptor_subkey
) {
1477 token
.Flags
|= CFXAcceptorSubkey
;
1479 memcpy(&token
.SND_SEQ
, &seq
, sizeof(uint64_t));
1482 uint8_t pad
[cctx
->mpad
];
1485 token
.Flags
|= CFXSealed
;
1486 memset(pad
, 0, cctx
->mpad
);
1487 if (cctx
->mpad
> 1) {
1488 plen
= htons(cctx
->mpad
- ((len
+ sizeof(gss_cfx_wrap_token_desc
)) % cctx
->mpad
));
1489 token
.EC
[0] = ((plen
>> 8) & 0xff);
1490 token
.EC
[1] = (plen
& 0xff);
1493 error
= gss_append_mbuf(*mbp
, pad
, plen
);
1497 error
= gss_append_mbuf(*mbp
, (uint8_t *)&token
, sizeof(gss_cfx_wrap_token_desc
));
1498 len
+= sizeof(gss_cfx_wrap_token_desc
);
1501 error
= krb5_cfx_crypt_mbuf(cctx
, mbp
, &len
, 1, 0);
1504 error
= gss_prepend_mbuf(mbp
, (uint8_t *)&token
, sizeof(gss_cfx_wrap_token_desc
));
1507 uint8_t digest
[cctx
->digest_size
];
1508 gss_buffer_desc header
;
1510 header
.length
= sizeof(token
);
1511 header
.value
= &token
;
1513 error
= krb5_mic_mbuf(cctx
, NULL
, *mbp
, 0, len
, &header
, digest
, NULL
, 1, 0);
1515 error
= gss_append_mbuf(*mbp
, digest
, cctx
->digest_size
);
1517 uint16_t plen
= htons(cctx
->digest_size
);
1518 memcpy(token
.EC
, &plen
, 2);
1519 error
= gss_prepend_mbuf(mbp
, (uint8_t *)&token
, sizeof(gss_cfx_wrap_token_desc
));
1525 return GSS_S_FAILURE
;
1528 return GSS_S_COMPLETE
;
1532 * Given a wrap token the has a rrc, move the trailer back to the end.
1535 gss_krb5_cfx_unwrap_rrc_mbuf(mbuf_t header
, size_t rrc
)
1537 mbuf_t body
, trailer
;
1539 gss_normalize_mbuf(header
, sizeof(gss_cfx_wrap_token_desc
), &rrc
, &trailer
, &body
, 0);
1540 gss_join_mbuf(header
, body
, trailer
);
1544 gss_krb5_cfx_unwrap_mbuf(uint32_t * minor
, /* minor_status */
1545 gss_ctx_id_t ctx
, /* context_handle */
1546 mbuf_t
*mbp
, /* input/output message_buffer */
1547 size_t len
, /* mbuf chain length */
1548 int *conf_flag
, /* conf_state */
1549 gss_qop_t
*qop
/* qop state */)
1551 gss_cfx_wrap_token_desc token
;
1552 lucid_context_t lctx
= &ctx
->gss_lucid_ctx
;
1553 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
1555 uint16_t ec
= 0, rrc
= 0;
1557 int reverse
= (*qop
== GSS_C_QOP_REVERSE
);
1558 int initiate
= lctx
->initiate
? (reverse
? 0 : 1) : (reverse
? 1 : 0);
1560 error
= mbuf_copydata(*mbp
, 0, sizeof(gss_cfx_wrap_token_desc
), &token
);
1561 gss_strip_mbuf(*mbp
, sizeof(gss_cfx_wrap_token_desc
));
1562 len
-= sizeof(gss_cfx_wrap_token_desc
);
1564 /* Check for valid token */
1565 if (token
.TOK_ID
[0] != wrap_cfx_token
.TOK_ID
[0] ||
1566 token
.TOK_ID
[1] != wrap_cfx_token
.TOK_ID
[1] ||
1567 token
.Filler
!= wrap_cfx_token
.Filler
) {
1568 printf("Token id does not match\n");
1571 if ((initiate
&& !(token
.Flags
& CFXSentByAcceptor
)) ||
1572 (lctx
->key_data
.lucid_protocol_u
.data_4121
.acceptor_subkey
&& !(token
.Flags
& CFXAcceptorSubkey
))) {
1573 printf("Bad flags %x\n", token
.Flags
);
1577 /* XXX Sequence replay detection */
1578 memcpy(&seq
, token
.SND_SEQ
, sizeof(seq
));
1580 lctx
->recv_seq
= seq
;
1582 ec
= (token
.EC
[0] << 8) | token
.EC
[1];
1583 rrc
= (token
.RRC
[0] << 8) | token
.RRC
[1];
1584 *qop
= GSS_C_QOP_DEFAULT
;
1585 conf
= ((token
.Flags
& CFXSealed
) == CFXSealed
);
1590 gss_cfx_wrap_token_desc etoken
;
1592 if (rrc
) { /* Handle Right rotation count */
1593 gss_krb5_cfx_unwrap_rrc_mbuf(*mbp
, rrc
);
1595 error
= krb5_cfx_crypt_mbuf(cctx
, mbp
, &len
, 0, reverse
);
1597 printf("krb5_cfx_crypt_mbuf %d\n", error
);
1599 return GSS_S_FAILURE
;
1601 if (len
>= sizeof(gss_cfx_wrap_token_desc
)) {
1602 len
-= sizeof(gss_cfx_wrap_token_desc
);
1606 mbuf_copydata(*mbp
, len
, sizeof(gss_cfx_wrap_token_desc
), &etoken
);
1607 /* Verify etoken with the token wich should be the same, except the rc field is always zero */
1608 token
.RRC
[0] = token
.RRC
[1] = 0;
1609 if (memcmp(&token
, &etoken
, sizeof(gss_cfx_wrap_token_desc
)) != 0) {
1610 printf("Encrypted token mismach\n");
1613 /* strip the encrypted token and any pad bytes */
1614 gss_strip_mbuf(*mbp
, -(sizeof(gss_cfx_wrap_token_desc
) + ec
));
1615 len
-= (sizeof(gss_cfx_wrap_token_desc
) + ec
);
1617 uint8_t digest
[cctx
->digest_size
];
1619 gss_buffer_desc header
;
1621 if (ec
!= cctx
->digest_size
|| len
>= cctx
->digest_size
) {
1624 len
-= cctx
->digest_size
;
1625 mbuf_copydata(*mbp
, len
, cctx
->digest_size
, digest
);
1626 gss_strip_mbuf(*mbp
, -cctx
->digest_size
);
1627 /* When calculating the mic header fields ec and rcc must be zero */
1628 token
.EC
[0] = token
.EC
[1] = token
.RRC
[0] = token
.RRC
[1] = 0;
1629 header
.value
= &token
;
1630 header
.length
= sizeof(gss_cfx_wrap_token_desc
);
1631 error
= krb5_mic_mbuf(cctx
, NULL
, *mbp
, 0, len
, &header
, digest
, &verf
, 1, reverse
);
1636 return GSS_S_COMPLETE
;
1640 return GSS_S_FAILURE
;
1644 * RFC 1964 3DES support
1647 typedef struct gss_1964_mic_token_desc_struct
{
1648 uint8_t TOK_ID
[2]; /* 01 01 */
1649 uint8_t Sign_Alg
[2];
1650 uint8_t Filler
[4]; /* ff ff ff ff */
1651 } gss_1964_mic_token_desc
, *gss_1964_mic_token
;
1653 typedef struct gss_1964_wrap_token_desc_struct
{
1654 uint8_t TOK_ID
[2]; /* 02 01 */
1655 uint8_t Sign_Alg
[2];
1656 uint8_t Seal_Alg
[2];
1657 uint8_t Filler
[2]; /* ff ff */
1658 } gss_1964_wrap_token_desc
, *gss_1964_wrap_token
;
1660 typedef struct gss_1964_delete_token_desc_struct
{
1661 uint8_t TOK_ID
[2]; /* 01 02 */
1662 uint8_t Sign_Alg
[2];
1663 uint8_t Filler
[4]; /* ff ff ff ff */
1664 } gss_1964_delete_token_desc
, *gss_1964_delete_token
;
1666 typedef struct gss_1964_header_desc_struct
{
1667 uint8_t App0
; /* 0x60 Application 0 constructed */
1668 uint8_t AppLen
[]; /* Variable Der length */
1669 } gss_1964_header_desc
, *gss_1964_header
;
1672 gss_1964_mic_token_desc mic_tok
;
1673 gss_1964_wrap_token_desc wrap_tok
;
1674 gss_1964_delete_token_desc del_tok
;
1675 } gss_1964_tok_type
__attribute__((transparent_union
));
1677 typedef struct gss_1964_token_body_struct
{
1678 uint8_t OIDType
; /* 0x06 */
1679 uint8_t OIDLen
; /* 0x09 */
1680 uint8_t kerb_mech
[9]; /* Der Encode kerberos mech 1.2.840.113554.1.2.2
1681 * 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x01, 0x02, 0x02 */
1682 gss_1964_tok_type body
;
1684 uint8_t Hash
[]; /* Mic */
1685 } gss_1964_token_body_desc
, *gss_1964_token_body
;
1688 gss_1964_header_desc tok_1964_header
= {
1692 gss_1964_mic_token_desc mic_1964_token
= {
1693 .TOK_ID
= "\x01\x01",
1694 .Filler
= "\xff\xff\xff\xff"
1697 gss_1964_wrap_token_desc wrap_1964_token
= {
1698 .TOK_ID
= "\x02\x01",
1699 .Filler
= "\xff\xff"
1702 gss_1964_delete_token_desc del_1964_token
= {
1703 .TOK_ID
= "\x01\x01",
1704 .Filler
= "\xff\xff\xff\xff"
1707 gss_1964_token_body_desc body_1964_token
= {
1710 .kerb_mech
= "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02",
1713 #define GSS_KRB5_3DES_MAXTOKSZ (sizeof(gss_1964_header_desc) + 5 /* max der length supported */ + sizeof(gss_1964_token_body_desc))
1715 uint32_t gss_krb5_3des_get_mic(uint32_t *, gss_ctx_id_t
, gss_qop_t
, gss_buffer_t
, gss_buffer_t
);
1716 uint32_t gss_krb5_3des_verify_mic(uint32_t *, gss_ctx_id_t
, gss_buffer_t
, gss_buffer_t
, gss_qop_t
*);
1717 uint32_t gss_krb5_3des_get_mic_mbuf(uint32_t *, gss_ctx_id_t
, gss_qop_t
, mbuf_t
, size_t, size_t, gss_buffer_t
);
1718 uint32_t gss_krb5_3des_verify_mic_mbuf(uint32_t *, gss_ctx_id_t
, mbuf_t
, size_t, size_t, gss_buffer_t
, gss_qop_t
*);
1719 uint32_t gss_krb5_3des_wrap_mbuf(uint32_t *, gss_ctx_id_t
, int, gss_qop_t
, mbuf_t
*, size_t, int *);
1720 uint32_t gss_krb5_3des_unwrap_mbuf(uint32_t *, gss_ctx_id_t
, mbuf_t
*, size_t, int *, gss_qop_t
*);
1723 * Decode an ASN.1 DER length field
1726 gss_krb5_der_length_get(uint8_t **pp
)
1729 uint32_t flen
, len
= 0;
1734 if (flen
> sizeof(uint32_t)) {
1738 len
= (len
<< 8) + *p
++;
1748 * Determine size of ASN.1 DER length
1751 gss_krb5_der_length_size(int len
)
1754 len
< (1 << 7) ? 1 :
1755 len
< (1 << 8) ? 2 :
1756 len
< (1 << 16) ? 3 :
1757 len
< (1 << 24) ? 4 : 5;
1761 * Encode an ASN.1 DER length field
1764 gss_krb5_der_length_put(uint8_t **pp
, int len
)
1766 int sz
= gss_krb5_der_length_size(len
);
1770 *p
++ = (uint8_t) len
;
1772 *p
++ = (uint8_t) ((sz
- 1) | 0x80);
1775 *p
++ = (uint8_t) ((len
>> (sz
* 8)) & 0xff);
1783 gss_krb5_3des_token_put(gss_ctx_id_t ctx
, gss_1964_tok_type body
, gss_buffer_t hash
, size_t datalen
, gss_buffer_t des3_token
)
1785 gss_1964_header token
;
1786 gss_1964_token_body tokbody
;
1787 lucid_context_t lctx
= &ctx
->gss_lucid_ctx
;
1788 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
1789 uint32_t seq
= (uint32_t) (lctx
->send_seq
++ & 0xffff);
1790 size_t toklen
= sizeof(gss_1964_token_body_desc
) + cctx
->digest_size
;
1791 size_t alloclen
= toklen
+ sizeof(gss_1964_header_desc
) + gss_krb5_der_length_size(toklen
+ datalen
);
1794 MALLOC(token
, gss_1964_header
, alloclen
, M_TEMP
, M_WAITOK
| M_ZERO
);
1795 *token
= tok_1964_header
;
1796 tokptr
= token
->AppLen
;
1797 gss_krb5_der_length_put(&tokptr
, toklen
+ datalen
);
1798 tokbody
= (gss_1964_token_body
)tokptr
;
1799 *tokbody
= body_1964_token
; /* Initalize the token body */
1800 tokbody
->body
= body
; /* and now set the body to the token type passed in */
1802 for (int i
= 0; i
< 4; i
++) {
1803 tokbody
->SND_SEQ
[i
] = (uint8_t)((seq
>> (i
* 8)) & 0xff);
1805 for (int i
= 4; i
< 8; i
++) {
1806 tokbody
->SND_SEQ
[i
] = lctx
->initiate
? 0x00 : 0xff;
1809 size_t blocksize
= cctx
->enc_mode
->block_size
;
1810 cccbc_iv_decl(blocksize
, iv
);
1811 cccbc_ctx_decl(cctx
->enc_mode
->size
, enc_ctx
);
1812 cccbc_set_iv(cctx
->enc_mode
, iv
, hash
->value
);
1813 cccbc_init(cctx
->enc_mode
, enc_ctx
, cctx
->keylen
, cctx
->key
);
1814 cccbc_update(cctx
->enc_mode
, enc_ctx
, iv
, 1, tokbody
->SND_SEQ
, tokbody
->SND_SEQ
);
1816 assert(hash
->length
== cctx
->digest_size
);
1817 memcpy(tokbody
->Hash
, hash
->value
, hash
->length
);
1818 des3_token
->length
= alloclen
;
1819 des3_token
->value
= token
;
1823 gss_krb5_3des_token_get(gss_ctx_id_t ctx
, gss_buffer_t intok
,
1824 gss_1964_tok_type body
, gss_buffer_t hash
, size_t *offset
, size_t *len
, int reverse
)
1826 gss_1964_header token
= intok
->value
;
1827 gss_1964_token_body tokbody
;
1828 lucid_context_t lctx
= &ctx
->gss_lucid_ctx
;
1829 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
1836 if (token
->App0
!= tok_1964_header
.App0
) {
1837 printf("%s: bad framing\n", __func__
);
1838 printgbuf(__func__
, intok
);
1841 tokptr
= token
->AppLen
;
1842 length
= gss_krb5_der_length_get(&tokptr
);
1844 printf("%s: invalid length\n", __func__
);
1845 printgbuf(__func__
, intok
);
1848 toklen
= sizeof(gss_1964_header_desc
) + gss_krb5_der_length_size(length
)
1849 + sizeof(gss_1964_token_body_desc
);
1851 if (intok
->length
< toklen
+ cctx
->digest_size
) {
1852 printf("%s: token to short", __func__
);
1853 printf("toklen = %d, length = %d\n", (int)toklen
, (int)length
);
1854 printgbuf(__func__
, intok
);
1859 *offset
= toklen
+ cctx
->digest_size
;
1863 *len
= length
- sizeof(gss_1964_token_body_desc
) - cctx
->digest_size
;
1866 tokbody
= (gss_1964_token_body
)tokptr
;
1867 if (tokbody
->OIDType
!= body_1964_token
.OIDType
||
1868 tokbody
->OIDLen
!= body_1964_token
.OIDLen
||
1869 memcmp(tokbody
->kerb_mech
, body_1964_token
.kerb_mech
, tokbody
->OIDLen
) != 0) {
1870 printf("%s: Invalid mechanism\n", __func__
);
1871 printgbuf(__func__
, intok
);
1874 if (memcmp(&tokbody
->body
, &body
, sizeof(gss_1964_tok_type
)) != 0) {
1875 printf("%s: Invalid body\n", __func__
);
1876 printgbuf(__func__
, intok
);
1879 size_t blocksize
= cctx
->enc_mode
->block_size
;
1880 uint8_t *block
= tokbody
->SND_SEQ
;
1882 assert(blocksize
== sizeof(tokbody
->SND_SEQ
));
1883 cccbc_iv_decl(blocksize
, iv
);
1884 cccbc_ctx_decl(cctx
->dec_mode
->size
, dec_ctx
);
1885 cccbc_set_iv(cctx
->dec_mode
, iv
, tokbody
->Hash
);
1886 cccbc_init(cctx
->dec_mode
, dec_ctx
, cctx
->keylen
, cctx
->key
);
1887 cccbc_update(cctx
->dec_mode
, dec_ctx
, iv
, 1, block
, block
);
1889 initiate
= lctx
->initiate
? (reverse
? 0 : 1) : (reverse
? 1 : 0);
1890 for (int i
= 4; i
< 8; i
++) {
1891 if (tokbody
->SND_SEQ
[i
] != (initiate
? 0xff : 0x00)) {
1892 printf("%s: Invalid des mac\n", __func__
);
1893 printgbuf(__func__
, intok
);
1898 memcpy(&seq
, tokbody
->SND_SEQ
, sizeof(uint32_t));
1900 lctx
->recv_seq
= ntohl(seq
);
1902 assert(hash
->length
>= cctx
->digest_size
);
1903 memcpy(hash
->value
, tokbody
->Hash
, cctx
->digest_size
);
1909 gss_krb5_3des_get_mic(uint32_t *minor
, /* minor status */
1910 gss_ctx_id_t ctx
, /* krb5 context id */
1911 gss_qop_t qop __unused
, /* qop_req (ignored) */
1912 gss_buffer_t mbp
, /* message buffer in */
1913 gss_buffer_t mic
) /* mic token out */
1915 gss_1964_mic_token_desc tokbody
= mic_1964_token
;
1916 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
1917 gss_buffer_desc hash
;
1918 gss_buffer_desc header
;
1919 uint8_t hashval
[cctx
->digest_size
];
1921 hash
.length
= cctx
->digest_size
;
1922 hash
.value
= hashval
;
1923 tokbody
.Sign_Alg
[0] = 0x04; /* lctx->keydata.lucid_protocol_u.data_1964.sign_alg */
1924 tokbody
.Sign_Alg
[1] = 0x00;
1925 header
.length
= sizeof(gss_1964_mic_token_desc
);
1926 header
.value
= &tokbody
;
1929 *minor
= krb5_mic(cctx
, &header
, mbp
, NULL
, hashval
, NULL
, 0, 0);
1931 return GSS_S_FAILURE
;
1934 /* Make the token */
1935 gss_krb5_3des_token_put(ctx
, tokbody
, &hash
, 0, mic
);
1937 return GSS_S_COMPLETE
;
1941 gss_krb5_3des_verify_mic(uint32_t *minor
,
1947 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
1948 uint8_t hashval
[cctx
->digest_size
];
1949 gss_buffer_desc hash
;
1950 gss_1964_mic_token_desc mtok
= mic_1964_token
;
1951 gss_buffer_desc header
;
1954 mtok
.Sign_Alg
[0] = 0x04; /* lctx->key_data.lucid_protocol_u.data_1964.sign_alg */
1955 mtok
.Sign_Alg
[1] = 0x00;
1956 hash
.length
= cctx
->digest_size
;
1957 hash
.value
= hashval
;
1958 header
.length
= sizeof(gss_1964_mic_token_desc
);
1959 header
.value
= &mtok
;
1962 *qop
= GSS_C_QOP_DEFAULT
;
1965 *minor
= gss_krb5_3des_token_get(ctx
, mic
, mtok
, &hash
, NULL
, NULL
, 0);
1967 return GSS_S_FAILURE
;
1970 *minor
= krb5_mic(cctx
, &header
, mbp
, NULL
, hashval
, &verf
, 0, 0);
1972 return GSS_S_FAILURE
;
1975 return verf
? GSS_S_COMPLETE
: GSS_S_BAD_SIG
;
1979 gss_krb5_3des_get_mic_mbuf(uint32_t *minor
,
1981 gss_qop_t qop __unused
,
1987 gss_1964_mic_token_desc tokbody
= mic_1964_token
;
1988 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
1989 gss_buffer_desc header
;
1990 gss_buffer_desc hash
;
1991 uint8_t hashval
[cctx
->digest_size
];
1993 hash
.length
= cctx
->digest_size
;
1994 hash
.value
= hashval
;
1995 tokbody
.Sign_Alg
[0] = 0x04; /* lctx->key_data.lucid_protocol_u.data_4121.sign_alg */
1996 tokbody
.Sign_Alg
[1] = 0x00;
1997 header
.length
= sizeof(gss_1964_mic_token_desc
);
1998 header
.value
= &tokbody
;
2001 *minor
= krb5_mic_mbuf(cctx
, &header
, mbp
, offset
, len
, NULL
, hashval
, NULL
, 0, 0);
2003 return GSS_S_FAILURE
;
2006 /* Make the token */
2007 gss_krb5_3des_token_put(ctx
, tokbody
, &hash
, 0, mic
);
2009 return GSS_S_COMPLETE
;
2013 gss_krb5_3des_verify_mic_mbuf(uint32_t *minor
,
2021 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
2022 uint8_t hashval
[cctx
->digest_size
];
2023 gss_buffer_desc header
;
2024 gss_buffer_desc hash
;
2025 gss_1964_mic_token_desc mtok
= mic_1964_token
;
2028 mtok
.Sign_Alg
[0] = 0x04; /* lctx->key_data.lucic_protocol_u.data1964.sign_alg */
2029 mtok
.Sign_Alg
[1] = 0x00;
2030 hash
.length
= cctx
->digest_size
;
2031 hash
.value
= hashval
;
2032 header
.length
= sizeof(gss_1964_mic_token_desc
);
2033 header
.value
= &mtok
;
2036 *qop
= GSS_C_QOP_DEFAULT
;
2039 *minor
= gss_krb5_3des_token_get(ctx
, mic
, mtok
, &hash
, NULL
, NULL
, 0);
2041 return GSS_S_FAILURE
;
2044 *minor
= krb5_mic_mbuf(cctx
, &header
, mbp
, offset
, len
, NULL
, hashval
, &verf
, 0, 0);
2046 return GSS_S_FAILURE
;
2049 return verf
? GSS_S_COMPLETE
: GSS_S_BAD_SIG
;
2053 gss_krb5_3des_wrap_mbuf(uint32_t *minor
,
2056 gss_qop_t qop __unused
,
2061 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
2062 const struct ccmode_cbc
*ccmode
= cctx
->enc_mode
;
2065 uint8_t confounder
[ccmode
->block_size
];
2066 gss_1964_wrap_token_desc tokbody
= wrap_1964_token
;
2067 gss_buffer_desc header
;
2068 gss_buffer_desc mic
;
2069 gss_buffer_desc hash
;
2070 uint8_t hashval
[cctx
->digest_size
];
2073 *conf_state
= conf_flag
;
2076 hash
.length
= cctx
->digest_size
;
2077 hash
.value
= hashval
;
2078 tokbody
.Sign_Alg
[0] = 0x04; /* lctx->key_data.lucid_protocol_u.data_1964.sign_alg */
2079 tokbody
.Sign_Alg
[1] = 0x00;
2080 /* conf_flag ? lctx->key_data.lucid_protocol_u.data_1964.seal_alg : 0xffff */
2081 tokbody
.Seal_Alg
[0] = conf_flag
? 0x02 : 0xff;
2082 tokbody
.Seal_Alg
[1] = conf_flag
? 0x00 : 0xff;
2083 header
.length
= sizeof(gss_1964_wrap_token_desc
);
2084 header
.value
= &tokbody
;
2086 /* Prepend confounder */
2087 read_random(confounder
, ccmode
->block_size
);
2088 *minor
= gss_prepend_mbuf(mbp
, confounder
, ccmode
->block_size
);
2090 return GSS_S_FAILURE
;
2093 /* Append trailer of up to 8 bytes and set pad length in each trailer byte */
2094 padlen
= 8 - len
% 8;
2095 for (int i
= 0; i
< padlen
; i
++) {
2098 *minor
= gss_append_mbuf(*mbp
, pad
, padlen
);
2100 return GSS_S_FAILURE
;
2103 len
+= ccmode
->block_size
+ padlen
;
2106 *minor
= krb5_mic_mbuf(cctx
, &header
, *mbp
, 0, len
, NULL
, hashval
, NULL
, 0, 0);
2108 return GSS_S_FAILURE
;
2111 /* Make the token */
2112 gss_krb5_3des_token_put(ctx
, tokbody
, &hash
, len
, &mic
);
2115 *minor
= krb5_crypt_mbuf(cctx
, mbp
, len
, 1, 0);
2117 return GSS_S_FAILURE
;
2121 *minor
= gss_prepend_mbuf(mbp
, mic
.value
, mic
.length
);
2123 return *minor
? GSS_S_FAILURE
: GSS_S_COMPLETE
;
2127 gss_krb5_3des_unwrap_mbuf(uint32_t *minor
,
2134 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
2135 const struct ccmode_cbc
*ccmode
= cctx
->dec_mode
;
2136 size_t length
= 0, offset
;
2137 gss_buffer_desc hash
;
2138 uint8_t hashval
[cctx
->digest_size
];
2139 gss_buffer_desc itoken
;
2140 uint8_t tbuffer
[GSS_KRB5_3DES_MAXTOKSZ
+ cctx
->digest_size
];
2141 itoken
.length
= GSS_KRB5_3DES_MAXTOKSZ
+ cctx
->digest_size
;
2142 itoken
.value
= tbuffer
;
2143 gss_1964_wrap_token_desc wrap
= wrap_1964_token
;
2144 gss_buffer_desc header
;
2147 int cflag
, verified
, reverse
= 0;
2149 if (len
< GSS_KRB5_3DES_MAXTOKSZ
) {
2151 return GSS_S_FAILURE
;
2154 if (*qop
== GSS_C_QOP_REVERSE
) {
2157 *qop
= GSS_C_QOP_DEFAULT
;
2159 *minor
= mbuf_copydata(*mbp
, 0, itoken
.length
, itoken
.value
);
2161 return GSS_S_FAILURE
;
2164 hash
.length
= cctx
->digest_size
;
2165 hash
.value
= hashval
;
2166 wrap
.Sign_Alg
[0] = 0x04;
2167 wrap
.Sign_Alg
[1] = 0x00;
2168 wrap
.Seal_Alg
[0] = 0x02;
2169 wrap
.Seal_Alg
[1] = 0x00;
2171 for (cflag
= 1; cflag
>= 0; cflag
--) {
2172 *minor
= gss_krb5_3des_token_get(ctx
, &itoken
, wrap
, &hash
, &offset
, &length
, reverse
);
2176 wrap
.Seal_Alg
[0] = 0xff;
2177 wrap
.Seal_Alg
[1] = 0xff;
2180 return GSS_S_FAILURE
;
2184 *conf_state
= cflag
;
2188 * Seperate off the header
2190 *minor
= gss_normalize_mbuf(*mbp
, offset
, &length
, &smb
, &tmb
, 0);
2192 return GSS_S_FAILURE
;
2195 assert(tmb
== NULL
);
2197 /* Decrypt the chain if needed */
2199 *minor
= krb5_crypt_mbuf(cctx
, &smb
, length
, 0, NULL
);
2201 return GSS_S_FAILURE
;
2205 /* Verify the mic */
2206 header
.length
= sizeof(gss_1964_wrap_token_desc
);
2207 header
.value
= &wrap
;
2209 *minor
= krb5_mic_mbuf(cctx
, &header
, smb
, 0, length
, NULL
, hashval
, &verified
, 0, 0);
2211 return GSS_S_FAILURE
;
2214 return GSS_S_BAD_SIG
;
2217 /* Get the pad bytes */
2218 *minor
= mbuf_copydata(smb
, length
- 1, 1, &padlen
);
2220 return GSS_S_FAILURE
;
2223 /* Strip the confounder and trailing pad bytes */
2224 gss_strip_mbuf(smb
, -padlen
);
2225 gss_strip_mbuf(smb
, ccmode
->block_size
);
2232 return GSS_S_COMPLETE
;
2236 etype_name(etypes etype
)
2239 case DES3_CBC_SHA1_KD
:
2240 return "des3-cbc-sha1";
2241 case AES128_CTS_HMAC_SHA1_96
:
2242 return "aes128-cts-hmac-sha1-96";
2243 case AES256_CTS_HMAC_SHA1_96
:
2244 return "aes-cts-hmac-sha1-96";
2246 return "unknown enctype";
2251 supported_etype(uint32_t proto
, etypes etype
)
2253 const char *proto_name
;
2258 proto_name
= "RFC 1964 krb5 gss mech";
2260 case DES3_CBC_SHA1_KD
:
2268 proto_name
= "RFC 4121 krb5 gss mech";
2270 case AES256_CTS_HMAC_SHA1_96
:
2271 case AES128_CTS_HMAC_SHA1_96
:
2278 proto_name
= "Unknown krb5 gss mech";
2281 printf("%s: Non supported encryption %s (%d) type for protocol %s (%d)\n",
2282 __func__
, etype_name(etype
), etype
, proto_name
, proto
);
2287 * Kerberos gss mech entry points
2290 gss_krb5_get_mic(uint32_t *minor
, /* minor_status */
2291 gss_ctx_id_t ctx
, /* context_handle */
2292 gss_qop_t qop
, /* qop_req */
2293 gss_buffer_t mbp
, /* message buffer */
2294 gss_buffer_t mic
/* message_token */)
2296 uint32_t minor_stat
= 0;
2298 if (minor
== NULL
) {
2299 minor
= &minor_stat
;
2303 /* Validate context */
2304 if (ctx
== NULL
|| ((lucid_context_version_t
)ctx
)->version
!= 1) {
2305 return GSS_S_NO_CONTEXT
;
2308 if (!supported_etype(ctx
->gss_lucid_ctx
.key_data
.proto
, ctx
->gss_cryptor
.etype
)) {
2310 return GSS_S_FAILURE
;
2313 switch (ctx
->gss_lucid_ctx
.key_data
.proto
) {
2315 /* RFC 1964 DES3 case */
2316 return gss_krb5_3des_get_mic(minor
, ctx
, qop
, mbp
, mic
);
2318 /* RFC 4121 CFX case */
2319 return gss_krb5_cfx_get_mic(minor
, ctx
, qop
, mbp
, mic
);
2322 return GSS_S_COMPLETE
;
2326 gss_krb5_verify_mic(uint32_t *minor
, /* minor_status */
2327 gss_ctx_id_t ctx
, /* context_handle */
2328 gss_buffer_t mbp
, /* message_buffer */
2329 gss_buffer_t mic
, /* message_token */
2330 gss_qop_t
*qop
/* qop_state */)
2332 uint32_t minor_stat
= 0;
2333 gss_qop_t qop_val
= GSS_C_QOP_DEFAULT
;
2335 if (minor
== NULL
) {
2336 minor
= &minor_stat
;
2344 /* Validate context */
2345 if (ctx
== NULL
|| ((lucid_context_version_t
)ctx
)->version
!= 1) {
2346 return GSS_S_NO_CONTEXT
;
2349 if (!supported_etype(ctx
->gss_lucid_ctx
.key_data
.proto
, ctx
->gss_cryptor
.etype
)) {
2351 return GSS_S_FAILURE
;
2354 switch (ctx
->gss_lucid_ctx
.key_data
.proto
) {
2356 /* RFC 1964 DES3 case */
2357 return gss_krb5_3des_verify_mic(minor
, ctx
, mbp
, mic
, qop
);
2359 /* RFC 4121 CFX case */
2360 return gss_krb5_cfx_verify_mic(minor
, ctx
, mbp
, mic
, qop
);
2362 return GSS_S_COMPLETE
;
2366 gss_krb5_get_mic_mbuf(uint32_t *minor
, /* minor_status */
2367 gss_ctx_id_t ctx
, /* context_handle */
2368 gss_qop_t qop
, /* qop_req */
2369 mbuf_t mbp
, /* message mbuf */
2370 size_t offset
, /* offest */
2371 size_t len
, /* length */
2372 gss_buffer_t mic
/* message_token */)
2374 uint32_t minor_stat
= 0;
2376 if (minor
== NULL
) {
2377 minor
= &minor_stat
;
2385 /* Validate context */
2386 if (ctx
== NULL
|| ((lucid_context_version_t
)ctx
)->version
!= 1) {
2387 return GSS_S_NO_CONTEXT
;
2390 if (!supported_etype(ctx
->gss_lucid_ctx
.key_data
.proto
, ctx
->gss_cryptor
.etype
)) {
2392 return GSS_S_FAILURE
;
2395 switch (ctx
->gss_lucid_ctx
.key_data
.proto
) {
2397 /* RFC 1964 DES3 case */
2398 return gss_krb5_3des_get_mic_mbuf(minor
, ctx
, qop
, mbp
, offset
, len
, mic
);
2400 /* RFC 4121 CFX case */
2401 return gss_krb5_cfx_get_mic_mbuf(minor
, ctx
, qop
, mbp
, offset
, len
, mic
);
2404 return GSS_S_COMPLETE
;
2408 gss_krb5_verify_mic_mbuf(uint32_t *minor
, /* minor_status */
2409 gss_ctx_id_t ctx
, /* context_handle */
2410 mbuf_t mbp
, /* message_buffer */
2411 size_t offset
, /* offset */
2412 size_t len
, /* length */
2413 gss_buffer_t mic
, /* message_token */
2414 gss_qop_t
*qop
/* qop_state */)
2416 uint32_t minor_stat
= 0;
2417 gss_qop_t qop_val
= GSS_C_QOP_DEFAULT
;
2419 if (minor
== NULL
) {
2420 minor
= &minor_stat
;
2432 /* Validate context */
2433 if (ctx
== NULL
|| ((lucid_context_version_t
)ctx
)->version
!= 1) {
2434 return GSS_S_NO_CONTEXT
;
2437 if (!supported_etype(ctx
->gss_lucid_ctx
.key_data
.proto
, ctx
->gss_cryptor
.etype
)) {
2439 return GSS_S_FAILURE
;
2442 switch (ctx
->gss_lucid_ctx
.key_data
.proto
) {
2444 /* RFC 1964 DES3 case */
2445 return gss_krb5_3des_verify_mic_mbuf(minor
, ctx
, mbp
, offset
, len
, mic
, qop
);
2447 /* RFC 4121 CFX case */
2448 return gss_krb5_cfx_verify_mic_mbuf(minor
, ctx
, mbp
, offset
, len
, mic
, qop
);
2451 return GSS_S_COMPLETE
;
2455 gss_krb5_wrap_mbuf(uint32_t *minor
, /* minor_status */
2456 gss_ctx_id_t ctx
, /* context_handle */
2457 int conf_flag
, /* conf_req_flag */
2458 gss_qop_t qop
, /* qop_req */
2459 mbuf_t
*mbp
, /* input/output message_buffer */
2460 size_t offset
, /* offset */
2461 size_t len
, /* length */
2462 int *conf_state
/* conf state */)
2464 uint32_t major
, minor_stat
= 0;
2468 if (minor
== NULL
) {
2469 minor
= &minor_stat
;
2471 if (conf_state
== NULL
) {
2472 conf_state
= &conf_val
;
2477 /* Validate context */
2478 if (ctx
== NULL
|| ((lucid_context_version_t
)ctx
)->version
!= 1) {
2479 return GSS_S_NO_CONTEXT
;
2482 if (!supported_etype(ctx
->gss_lucid_ctx
.key_data
.proto
, ctx
->gss_cryptor
.etype
)) {
2484 return GSS_S_FAILURE
;
2487 gss_normalize_mbuf(*mbp
, offset
, &len
, &smb
, &tmb
, 0);
2489 switch (ctx
->gss_lucid_ctx
.key_data
.proto
) {
2491 /* RFC 1964 DES3 case */
2492 major
= gss_krb5_3des_wrap_mbuf(minor
, ctx
, conf_flag
, qop
, &smb
, len
, conf_state
);
2495 /* RFC 4121 CFX case */
2496 major
= gss_krb5_cfx_wrap_mbuf(minor
, ctx
, conf_flag
, qop
, &smb
, len
, conf_state
);
2501 gss_join_mbuf(*mbp
, smb
, tmb
);
2504 gss_join_mbuf(smb
, tmb
, NULL
);
2511 gss_krb5_unwrap_mbuf(uint32_t * minor
, /* minor_status */
2512 gss_ctx_id_t ctx
, /* context_handle */
2513 mbuf_t
*mbp
, /* input/output message_buffer */
2514 size_t offset
, /* offset */
2515 size_t len
, /* length */
2516 int *conf_flag
, /* conf_state */
2517 gss_qop_t
*qop
/* qop state */)
2519 uint32_t major
, minor_stat
= 0;
2520 gss_qop_t qop_val
= GSS_C_QOP_DEFAULT
;
2524 if (minor
== NULL
) {
2525 minor
= &minor_stat
;
2530 if (conf_flag
== NULL
) {
2531 conf_flag
= &conf_val
;
2534 /* Validate context */
2535 if (ctx
== NULL
|| ((lucid_context_version_t
)ctx
)->version
!= 1) {
2536 return GSS_S_NO_CONTEXT
;
2539 if (!supported_etype(ctx
->gss_lucid_ctx
.key_data
.proto
, ctx
->gss_cryptor
.etype
)) {
2541 return GSS_S_FAILURE
;
2544 gss_normalize_mbuf(*mbp
, offset
, &len
, &smb
, &tmb
, 0);
2546 switch (ctx
->gss_lucid_ctx
.key_data
.proto
) {
2548 /* RFC 1964 DES3 case */
2549 major
= gss_krb5_3des_unwrap_mbuf(minor
, ctx
, &smb
, len
, conf_flag
, qop
);
2552 /* RFC 4121 CFX case */
2553 major
= gss_krb5_cfx_unwrap_mbuf(minor
, ctx
, &smb
, len
, conf_flag
, qop
);
2558 gss_join_mbuf(*mbp
, smb
, tmb
);
2561 gss_join_mbuf(smb
, tmb
, NULL
);
2567 #include <nfs/xdr_subs.h>
2570 xdr_lucid_context(void *data
, size_t length
, lucid_context_t lctx
)
2574 uint32_t keylen
= 0;
2576 xb_init_buffer(&xb
, data
, length
);
2577 xb_get_32(error
, &xb
, lctx
->vers
);
2578 if (!error
&& lctx
->vers
!= 1) {
2580 printf("%s: invalid version %d\n", __func__
, (int)lctx
->vers
);
2583 xb_get_32(error
, &xb
, lctx
->initiate
);
2585 printf("%s: Could not decode initiate\n", __func__
);
2588 xb_get_32(error
, &xb
, lctx
->endtime
);
2590 printf("%s: Could not decode endtime\n", __func__
);
2593 xb_get_64(error
, &xb
, lctx
->send_seq
);
2595 printf("%s: Could not decode send_seq\n", __func__
);
2598 xb_get_64(error
, &xb
, lctx
->recv_seq
);
2600 printf("%s: Could not decode recv_seq\n", __func__
);
2603 xb_get_32(error
, &xb
, lctx
->key_data
.proto
);
2605 printf("%s: Could not decode mech protocol\n", __func__
);
2608 switch (lctx
->key_data
.proto
) {
2610 xb_get_32(error
, &xb
, lctx
->key_data
.lucid_protocol_u
.data_1964
.sign_alg
);
2611 xb_get_32(error
, &xb
, lctx
->key_data
.lucid_protocol_u
.data_1964
.seal_alg
);
2613 printf("%s: Could not decode rfc1964 sign and seal\n", __func__
);
2617 xb_get_32(error
, &xb
, lctx
->key_data
.lucid_protocol_u
.data_4121
.acceptor_subkey
);
2619 printf("%s: Could not decode rfc4121 acceptor_subkey", __func__
);
2623 printf("%s: Invalid mech protocol %d\n", __func__
, (int)lctx
->key_data
.proto
);
2629 xb_get_32(error
, &xb
, lctx
->ctx_key
.etype
);
2631 printf("%s: Could not decode key enctype\n", __func__
);
2634 switch (lctx
->ctx_key
.etype
) {
2635 case DES3_CBC_SHA1_KD
:
2638 case AES128_CTS_HMAC_SHA1_96
:
2641 case AES256_CTS_HMAC_SHA1_96
:
2648 xb_get_32(error
, &xb
, lctx
->ctx_key
.key
.key_len
);
2650 printf("%s: could not decode key length\n", __func__
);
2653 if (lctx
->ctx_key
.key
.key_len
!= keylen
) {
2655 printf("%s: etype = %d keylen = %d expected keylen = %d\n", __func__
,
2656 lctx
->ctx_key
.etype
, lctx
->ctx_key
.key
.key_len
, keylen
);
2660 lctx
->ctx_key
.key
.key_val
= xb_malloc(keylen
);
2661 if (lctx
->ctx_key
.key
.key_val
== NULL
) {
2662 printf("%s: could not get memory for key\n", __func__
);
2666 error
= xb_get_bytes(&xb
, (char *)lctx
->ctx_key
.key
.key_val
, keylen
, 1);
2668 printf("%s: could get key value\n", __func__
);
2669 xb_free(lctx
->ctx_key
.key
.key_val
);
2676 gss_krb5_make_context(void *data
, uint32_t datalen
)
2680 if (!corecrypto_available()) {
2684 gss_krb5_mech_init();
2685 MALLOC(ctx
, gss_ctx_id_t
, sizeof(struct gss_ctx_id_desc
), M_TEMP
, M_WAITOK
| M_ZERO
);
2686 if (xdr_lucid_context(data
, datalen
, &ctx
->gss_lucid_ctx
) ||
2687 !supported_etype(ctx
->gss_lucid_ctx
.key_data
.proto
, ctx
->gss_lucid_ctx
.ctx_key
.etype
)) {
2693 /* Set up crypto context */
2694 gss_crypto_ctx_init(&ctx
->gss_cryptor
, &ctx
->gss_lucid_ctx
);
2701 gss_krb5_destroy_context(gss_ctx_id_t ctx
)
2706 gss_crypto_ctx_free(&ctx
->gss_cryptor
);
2707 FREE(ctx
->gss_lucid_ctx
.ctx_key
.key
.key_val
, M_TEMP
);
2708 cc_clear(sizeof(lucid_context_t
), &ctx
->gss_lucid_ctx
);