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 cchmac_ctx_t 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
);
149 printf("Count chars %d\n", cout
- 1);
153 printgbuf(const char *str
, gss_buffer_t buf
)
156 size_t len
= buf
->length
> 128 ? 128 : buf
->length
;
158 printf("%s: len = %d value = %p\n", str
? str
: "buffer", (int)buf
->length
, buf
->value
);
159 for (i
= 0; i
< len
; i
++) {
160 const char *s
= ((i
+ 1) % 8) ? " " : ((i
+ 1) % 16) ? " " : "\n";
161 printf("%02x%s", ((uint8_t *)buf
->value
)[i
], s
);
168 * Initialize the data structures for the gss kerberos mech.
170 #define GSS_KRB5_NOT_INITIALIZED 0
171 #define GSS_KRB5_INITIALIZING 1
172 #define GSS_KRB5_INITIALIZED 2
173 static volatile uint32_t gss_krb5_mech_initted
= GSS_KRB5_NOT_INITIALIZED
;
176 gss_krb5_mech_is_initialized(void)
178 return (gss_krb5_mech_initted
== GSS_KRB5_NOT_INITIALIZED
);
182 gss_krb5_mech_init(void)
184 extern void IOSleep(int);
186 /* Once initted always initted */
187 if (gss_krb5_mech_initted
== GSS_KRB5_INITIALIZED
)
190 /* make sure we init only once */
191 if (!OSCompareAndSwap(GSS_KRB5_NOT_INITIALIZED
, GSS_KRB5_INITIALIZING
, &gss_krb5_mech_initted
)) {
192 /* wait until initialization is complete */
193 while (!gss_krb5_mech_is_initialized())
197 gss_krb5_mech_grp
= lck_grp_alloc_init("gss_krb5_mech", LCK_GRP_ATTR_NULL
);
198 gss_krb5_mech_initted
= GSS_KRB5_INITIALIZED
;
202 gss_release_buffer(uint32_t *minor
, gss_buffer_t buf
)
207 FREE(buf
->value
, M_TEMP
);
210 return (GSS_S_COMPLETE
);
218 gss_mbuf_len(mbuf_t mb
, size_t offset
)
222 for (len
= 0; mb
; mb
= mbuf_next(mb
))
224 return ((offset
> len
) ? 0 : len
- offset
);
228 * Split an mbuf in a chain into two mbufs such that the original mbuf
229 * points to the original mbuf and the new mbuf points to the rest of the
230 * chain. The first mbuf length is the first len bytes and the second
231 * mbuf contains the remaining bytes. if len is zero or equals
232 * mbuf_len(mb) the don't create a new mbuf. We are already at an mbuf
233 * boundary. Return the mbuf that starts at the offset.
236 split_one_mbuf(mbuf_t mb
, size_t offset
, mbuf_t
*nmb
, int join
)
241 /* We don't have an mbuf or we're alread on an mbuf boundary */
242 if (mb
== NULL
|| offset
== 0)
245 /* If the mbuf length is offset then the next mbuf is the one we want */
246 if (mbuf_len(mb
) == offset
) {
247 *nmb
= mbuf_next(mb
);
249 mbuf_setnext(mb
, NULL
);
253 if (offset
> mbuf_len(mb
))
256 error
= mbuf_split(mb
, offset
, MBUF_WAITOK
, nmb
);
260 if (mbuf_flags(*nmb
) & MBUF_PKTHDR
) {
261 /* We don't want to copy the pkthdr. mbuf_split does that. */
262 error
= mbuf_setflags_mask(*nmb
, ~MBUF_PKTHDR
, MBUF_PKTHDR
);
266 /* Join the chain again */
267 mbuf_setnext(mb
, *nmb
);
273 * Given an mbuf with an offset and length return the chain such that
274 * offset and offset + *subchain_length are on mbuf boundaries. If
275 * *mbuf_length is less that the length of the chain after offset
276 * return that length in *mbuf_length. The mbuf sub chain starting at
277 * offset is returned in *subchain. If an error occurs return the
278 * corresponding errno. Note if there are less than offset bytes then
279 * subchain will be set to NULL and *subchain_length will be set to
280 * zero. If *subchain_length is 0; then set it to the length of the
281 * chain starting at offset. Join parameter is used to indicate whether
282 * the mbuf chain will be joined again as on chain, just rearranged so
283 * that offset and subchain_length are on mbuf boundaries.
287 gss_normalize_mbuf(mbuf_t chain
, size_t offset
, size_t *subchain_length
, mbuf_t
*subchain
, mbuf_t
*tail
, int join
)
289 size_t length
= *subchain_length
? *subchain_length
: ~0;
299 for (len
= offset
, mb
= chain
; mb
&& len
> mbuf_len(mb
); mb
= mbuf_next(mb
))
302 /* if we don't have offset bytes just return */
306 error
= split_one_mbuf(mb
, len
, subchain
, join
);
310 assert(subchain
!= NULL
&& *subchain
!= NULL
);
311 assert(offset
== 0 ? mb
== *subchain
: 1);
313 len
= gss_mbuf_len(*subchain
, 0);
314 length
= (length
> len
) ? len
: length
;
315 *subchain_length
= length
;
317 for (len
= length
, mb
= *subchain
; mb
&& len
> mbuf_len(mb
); mb
= mbuf_next(mb
))
320 error
= split_one_mbuf(mb
, len
, tail
, join
);
326 gss_join_mbuf(mbuf_t head
, mbuf_t body
, mbuf_t tail
)
330 for (mb
= head
; mb
&& mbuf_next(mb
); mb
= mbuf_next(mb
))
333 mbuf_setnext(mb
, body
);
334 for (mb
= body
; mb
&& mbuf_next(mb
); mb
= mbuf_next(mb
))
337 mbuf_setnext(mb
, tail
);
338 mb
= head
? head
: (body
? body
: tail
);
343 * Prepend size bytes to the mbuf chain.
346 gss_prepend_mbuf(mbuf_t
*chain
, uint8_t *bytes
, size_t size
)
348 uint8_t *data
= mbuf_data(*chain
);
349 size_t leading
= mbuf_leadingspace(*chain
);
350 size_t trailing
= mbuf_trailingspace(*chain
);
351 size_t mlen
= mbuf_len(*chain
);
354 if (size
> leading
&& size
<= leading
+ trailing
) {
355 data
= memmove(data
+ size
- leading
, data
, mlen
);
356 mbuf_setdata(*chain
, data
, mlen
);
359 error
= mbuf_prepend(chain
, size
, MBUF_WAITOK
);
362 data
= mbuf_data(*chain
);
363 memcpy(data
, bytes
, size
);
369 gss_append_mbuf(mbuf_t chain
, uint8_t *bytes
, size_t size
)
377 for (mb
= chain
; mb
; mb
= mbuf_next(mb
))
380 return (mbuf_copyback(chain
, len
, size
, bytes
, MBUF_WAITOK
));
384 gss_strip_mbuf(mbuf_t chain
, ssize_t size
)
389 mbuf_adj(chain
, size
);
396 * Kerberos mech generic crypto support for mbufs
400 * Walk the mbuf after the given offset calling the passed in crypto function
401 * for len bytes. Note the length, len should be a multiple of the blocksize and
402 * there should be at least len bytes available after the offset in the mbuf chain.
403 * padding should be done before calling this routine.
406 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
)
409 size_t mlen
, residue
;
413 /* Move to the start of the chain */
414 for (mb
= mbp
; mb
&& len
> 0; mb
= mbuf_next(mb
)) {
417 if (offset
>= mlen
) {
418 /* Offset not yet reached */
422 /* Found starting point in chain */
428 * Handle the data in this mbuf. If the length to
429 * walk is less than the data in the mbuf, set
430 * the mbuf length left to be the length left
432 mlen
= mlen
< len
? mlen
: len
;
433 /* Figure out how much is a multple of blocksize */
434 residue
= mlen
% blocksize
;
435 /* And addjust the mleft length to be the largest multiple of blocksized */
437 /* run our hash/encrypt/decrpyt function */
439 error
= crypto_fn(ctx
, ptr
, mlen
);
446 * If we have a residue then to get a full block for our crypto
447 * function, we need to copy the residue into our block size
448 * block and use the next mbuf to get the rest of the data for
449 * the block. N.B. We generally assume that from the offset
450 * passed in, that the total length, len, is a multple of
451 * blocksize and that there are at least len bytes in the chain
452 * from the offset. We also assume there is at least (blocksize
453 * - residue) size data in any next mbuf for residue > 0. If not
454 * we attemp to pullup bytes from down the chain.
457 mbuf_t nmb
= mbuf_next(mb
);
458 uint8_t *nptr
= NULL
, block
[blocksize
];
462 offset
= blocksize
- residue
;
466 * We don't have enough bytes so zero the block
467 * so that any trailing bytes will be zero.
469 cc_clear(sizeof(block
), block
);
471 memcpy(block
, ptr
, residue
);
473 mlen
= mbuf_len(nmb
);
475 error
= mbuf_pullup(&nmb
, offset
- mlen
);
477 mbuf_setnext(mb
, NULL
);
481 nptr
= mbuf_data(nmb
);
482 memcpy(block
+ residue
, nptr
, offset
);
485 error
= crypto_fn(ctx
, block
, sizeof(block
));
488 memcpy(ptr
, block
, residue
);
490 memcpy(nptr
, block
+ residue
, offset
);
498 do_crypt_init(crypt_walker_ctx_t wctx
, int encrypt
, crypto_ctx_t cctx
, cccbc_ctx
*ks
)
500 wctx
->ccmode
= encrypt
? cctx
->enc_mode
: cctx
->dec_mode
;
502 wctx
->crypt_ctx
= ks
;
503 MALLOC(wctx
->iv
, cccbc_iv
*, wctx
->ccmode
->block_size
, M_TEMP
, M_WAITOK
|M_ZERO
);
504 cccbc_set_iv(wctx
->ccmode
, wctx
->iv
, NULL
);
508 do_crypt(void *walker
, uint8_t *data
, uint32_t len
)
510 struct crypt_walker_ctx
*wctx
= (crypt_walker_ctx_t
)walker
;
513 nblocks
= len
/ wctx
->ccmode
->block_size
;
514 assert(len
% wctx
->ccmode
->block_size
== 0);
515 cccbc_update(wctx
->ccmode
, wctx
->crypt_ctx
, wctx
->iv
, nblocks
, data
, data
);
522 do_hmac_init(hmac_walker_ctx_t wctx
, crypto_ctx_t cctx
, void *key
)
524 size_t alloc_size
= cc_ctx_n(struct cchmac_ctx
, cchmac_di_size(cctx
->di
)) * sizeof(struct cchmac_ctx
);
527 MALLOC(wctx
->hmac_ctx
.hdr
, struct cchmac_ctx
*, alloc_size
, M_TEMP
, M_WAITOK
|M_ZERO
);
528 cchmac_init(cctx
->di
, wctx
->hmac_ctx
, cctx
->keylen
, key
);
532 do_hmac(void *walker
, uint8_t *data
, uint32_t len
)
534 hmac_walker_ctx_t wctx
= (hmac_walker_ctx_t
)walker
;
536 cchmac_update(wctx
->di
, wctx
->hmac_ctx
, len
, data
);
543 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
)
545 uint8_t digest
[ctx
->di
->output_size
];
546 cchmac_di_decl(ctx
->di
, hmac_ctx
);
547 int kdx
= (verify
== NULL
) ? (reverse
? GSS_RCV
: GSS_SND
) : (reverse
? GSS_SND
: GSS_RCV
);
551 if (!(ctx
->flags
& CRYPTO_KS_ALLOCED
)) {
552 lck_mtx_lock(ctx
->lock
);
553 if (!(ctx
->flags
& CRYPTO_KS_ALLOCED
)) {
554 cc_key_schedule_create(ctx
);
556 ctx
->flags
|= CRYPTO_KS_ALLOCED
;
557 lck_mtx_unlock(ctx
->lock
);
559 key2use
= ctx
->ks
.ikey
[kdx
];
561 key2use
= ctx
->ckey
[kdx
];
564 cchmac_init(ctx
->di
, hmac_ctx
, ctx
->keylen
, key2use
);
567 cchmac_update(ctx
->di
, hmac_ctx
, header
->length
, header
->value
);
570 cchmac_update(ctx
->di
, hmac_ctx
, bp
->length
, bp
->value
);
573 cchmac_update(ctx
->di
, hmac_ctx
, trailer
->length
, trailer
->value
);
576 cchmac_final(ctx
->di
, hmac_ctx
, digest
);
579 *verify
= (memcmp(mic
, digest
, ctx
->digest_size
) == 0);
582 memcpy(mic
, digest
, ctx
->digest_size
);
588 krb5_mic_mbuf(crypto_ctx_t ctx
, gss_buffer_t header
,
589 mbuf_t mbp
, uint32_t offset
, uint32_t len
, gss_buffer_t trailer
, uint8_t *mic
, int *verify
, int ikey
, int reverse
)
591 struct hmac_walker_ctx wctx
;
592 uint8_t digest
[ctx
->di
->output_size
];
594 int kdx
= (verify
== NULL
) ? (reverse
? GSS_RCV
: GSS_SND
) : (reverse
? GSS_SND
: GSS_RCV
);
598 if (!(ctx
->flags
& CRYPTO_KS_ALLOCED
)) {
599 lck_mtx_lock(ctx
->lock
);
600 if (!(ctx
->flags
& CRYPTO_KS_ALLOCED
)) {
601 cc_key_schedule_create(ctx
);
603 ctx
->flags
|= CRYPTO_KS_ALLOCED
;
604 lck_mtx_unlock(ctx
->lock
);
606 key2use
= ctx
->ks
.ikey
[kdx
];
608 key2use
= ctx
->ckey
[kdx
];
611 do_hmac_init(&wctx
, ctx
, key2use
);
614 cchmac_update(ctx
->di
, wctx
.hmac_ctx
, header
->length
, header
->value
);
617 error
= mbuf_walk(mbp
, offset
, len
, 1, do_hmac
, &wctx
);
622 cchmac_update(ctx
->di
, wctx
.hmac_ctx
, trailer
->length
, trailer
->value
);
624 cchmac_final(ctx
->di
, wctx
.hmac_ctx
, digest
);
625 FREE(wctx
.hmac_ctx
.hdr
, M_TEMP
);
628 *verify
= (memcmp(mic
, digest
, ctx
->digest_size
) == 0);
632 memcpy(mic
, digest
, ctx
->digest_size
);
637 errno_t
/* __attribute__((optnone)) */
638 krb5_crypt_mbuf(crypto_ctx_t ctx
, mbuf_t
*mbp
, uint32_t len
, int encrypt
, cccbc_ctx
*ks
)
640 struct crypt_walker_ctx wctx
;
641 const struct ccmode_cbc
*ccmode
= encrypt
? ctx
->enc_mode
: ctx
->dec_mode
;
647 if (!(ctx
->flags
& CRYPTO_KS_ALLOCED
)) {
648 lck_mtx_lock(ctx
->lock
);
649 if (!(ctx
->flags
& CRYPTO_KS_ALLOCED
)) {
650 cc_key_schedule_create(ctx
);
652 ctx
->flags
|= CRYPTO_KS_ALLOCED
;
653 lck_mtx_unlock(ctx
->lock
);
656 ks
= encrypt
? ctx
->ks
.enc
: ctx
->ks
.dec
;
658 if ((ctx
->flags
& CRYPTO_CTS_ENABLE
) && ctx
->mpad
== 1) {
659 uint8_t block
[ccmode
->block_size
];
660 /* if the length is less than or equal to a blocksize. We just encrypt the block */
661 if (len
<= ccmode
->block_size
) {
662 if (len
< ccmode
->block_size
) {
663 memset(block
, 0, sizeof(block
));
664 gss_append_mbuf(*mbp
, block
, ccmode
->block_size
);
666 plen
= ccmode
->block_size
;
668 /* determine where the last two blocks are */
669 uint32_t r
= len
% ccmode
->block_size
;
671 cts_len
= r
? r
+ ccmode
->block_size
: 2 * ccmode
->block_size
;
672 plen
= len
- cts_len
;
673 /* If plen is 0 we only have two blocks to crypt with ccpad below */
677 gss_normalize_mbuf(*mbp
, 0, &plen
, &mb
, &lmb
, 0);
679 assert(plen
== len
- cts_len
);
680 assert(gss_mbuf_len(mb
, 0) == plen
);
681 assert(gss_mbuf_len(lmb
, 0) == cts_len
);
684 } else if (len
% ctx
->mpad
) {
685 uint8_t pad_block
[ctx
->mpad
];
686 size_t padlen
= ctx
->mpad
- (len
% ctx
->mpad
);
688 memset(pad_block
, 0, padlen
);
689 error
= gss_append_mbuf(*mbp
, pad_block
, padlen
);
694 do_crypt_init(&wctx
, encrypt
, ctx
, ks
);
696 error
= mbuf_walk(*mbp
, 0, plen
, ccmode
->block_size
, do_crypt
, &wctx
);
701 if ((ctx
->flags
& CRYPTO_CTS_ENABLE
) && cts_len
) {
702 uint8_t cts_pad
[2*ccmode
->block_size
];
703 ccpad_func do_ccpad
= encrypt
? ccpad_cts3_encrypt
: ccpad_cts3_decrypt
;
705 assert(cts_len
<= 2*ccmode
->block_size
&& cts_len
> ccmode
->block_size
);
706 memset(cts_pad
, 0, sizeof(cts_pad
));
707 mbuf_copydata(lmb
, 0, cts_len
, cts_pad
);
709 do_ccpad(ccmode
, wctx
.crypt_ctx
, wctx
.iv
, cts_len
, cts_pad
, cts_pad
);
710 gss_append_mbuf(*mbp
, cts_pad
, cts_len
);
712 FREE(wctx
.iv
, M_TEMP
);
718 * Key derivation routines
722 rr13(unsigned char *buf
, size_t len
)
724 size_t bytes
= (len
+ 7) / 8;
725 unsigned char tmp
[bytes
];
732 const int bits
= 13 % len
;
733 const int lbit
= len
% 8;
735 memcpy(tmp
, buf
, bytes
);
737 /* pad final byte with inital bits */
738 tmp
[bytes
- 1] &= 0xff << (8 - lbit
);
739 for(i
= lbit
; i
< 8; i
+= len
)
740 tmp
[bytes
- 1] |= buf
[0] >> i
;
742 for(i
= 0; i
< bytes
; i
++) {
744 ssize_t b1
, s1
, b2
, s2
;
746 /* calculate first bit position of this byte */
750 /* byte offset and shift count */
753 if((size_t)bb
+ 8 > bytes
* 8)
754 /* watch for wraparound */
755 s2
= (len
+ 8 - s1
) % 8;
758 b2
= (b1
+ 1) % bytes
;
759 buf
[i
] = (tmp
[b1
] << s1
) | (tmp
[b2
] >> s2
);
766 /* Add `b' to `a', both being one's complement numbers. */
768 add1(unsigned char *a
, unsigned char *b
, size_t len
)
773 for(i
= len
- 1; i
>= 0; i
--){
774 int x
= a
[i
] + b
[i
] + carry
;
778 for(i
= len
- 1; carry
&& i
>= 0; i
--){
779 int x
= a
[i
] + carry
;
787 krb5_n_fold(const void *instr
, size_t len
, void *foldstr
, size_t size
)
789 /* if len < size we need at most N * len bytes, ie < 2 * size;
790 if len > size we need at most 2 * len */
792 size_t maxlen
= 2 * max(size
, len
);
794 unsigned char tmp
[maxlen
];
795 unsigned char buf
[len
];
797 memcpy(buf
, instr
, len
);
798 memset(foldstr
, 0, size
);
800 memcpy(tmp
+ l
, buf
, len
);
802 ret
= rr13(buf
, len
* 8);
806 add1(foldstr
, tmp
, size
);
810 memmove(tmp
, tmp
+ size
, l
);
819 krb5_make_usage(uint32_t usage_no
, uint8_t suffix
, uint8_t usage_string
[KRB5_USAGE_LEN
])
823 for (i
= 0; i
< 4; i
++)
824 usage_string
[i
] = ((usage_no
>> 8*(3-i
)) & 0xff);
825 usage_string
[i
] = suffix
;
829 krb5_key_derivation(crypto_ctx_t ctx
, const void *cons
, size_t conslen
, void **dkey
, size_t dklen
)
831 size_t blocksize
= ctx
->enc_mode
->block_size
;
832 cccbc_iv_decl(blocksize
, iv
);
833 cccbc_ctx_decl(ctx
->enc_mode
->size
, enc_ctx
);
834 size_t ksize
= 8*dklen
;
835 size_t nblocks
= (ksize
+ 8*blocksize
- 1) / (8*blocksize
);
837 uint8_t block
[blocksize
];
839 MALLOC(*dkey
, void *, nblocks
* blocksize
, M_TEMP
, M_WAITOK
| M_ZERO
);
842 krb5_n_fold(cons
, conslen
, block
, blocksize
);
843 cccbc_init(ctx
->enc_mode
, enc_ctx
, ctx
->keylen
, ctx
->key
);
844 for (size_t i
= 0; i
< nblocks
; i
++) {
845 cccbc_set_iv(ctx
->enc_mode
, iv
, NULL
);
846 cccbc_update(ctx
->enc_mode
, enc_ctx
, iv
, 1, block
, block
);
847 memcpy(dkptr
, block
, blocksize
);
853 des_make_key(const uint8_t rawkey
[7], uint8_t deskey
[8])
857 memcpy(deskey
, rawkey
, 7);
858 for (int i
= 0; i
< 7; i
++)
859 val
|= ((deskey
[i
] & 1) << (i
+1));
861 ccdes_key_set_odd_parity(deskey
, 8);
865 krb5_3des_key_derivation(crypto_ctx_t ctx
, const void *cons
, size_t conslen
, void **des3key
)
867 const struct ccmode_cbc
*cbcmode
= ctx
->enc_mode
;
869 uint8_t *kptr
, *rptr
;
871 MALLOC(*des3key
, void *, 3*cbcmode
->block_size
, M_TEMP
, M_WAITOK
| M_ZERO
);
872 krb5_key_derivation(ctx
, cons
, conslen
, &rawkey
, 3*(cbcmode
->block_size
- 1));
873 kptr
= (uint8_t *)*des3key
;
874 rptr
= (uint8_t *)rawkey
;
876 for (int i
= 0; i
< 3; i
++) {
877 des_make_key(rptr
, kptr
);
878 rptr
+= cbcmode
->block_size
- 1;
879 kptr
+= cbcmode
->block_size
;
882 cc_clear(3*(cbcmode
->block_size
- 1), rawkey
);
883 FREE(rawkey
, M_TEMP
);
887 * Create a key schecule
891 cc_key_schedule_create(crypto_ctx_t ctx
)
893 uint8_t usage_string
[KRB5_USAGE_LEN
];
894 lucid_context_t lctx
= ctx
->gss_ctx
;
897 switch (lctx
->key_data
.proto
) {
899 if (ctx
->ks
.enc
== NULL
) {
900 MALLOC(ctx
->ks
.enc
, cccbc_ctx
*, ctx
->enc_mode
->size
, M_TEMP
, M_WAITOK
| M_ZERO
);
901 cccbc_init(ctx
->enc_mode
, ctx
->ks
.enc
, ctx
->keylen
, ctx
->key
);
903 if (ctx
->ks
.dec
== NULL
) {
904 MALLOC(ctx
->ks
.dec
, cccbc_ctx
*, ctx
->dec_mode
->size
, M_TEMP
, M_WAITOK
| M_ZERO
);
905 cccbc_init(ctx
->dec_mode
, ctx
->ks
.dec
, ctx
->keylen
, ctx
->key
);
909 if (ctx
->ks
.enc
== NULL
) {
910 krb5_make_usage(lctx
->initiate
?
911 KRB5_USAGE_INITIATOR_SEAL
: KRB5_USAGE_ACCEPTOR_SEAL
,
913 krb5_key_derivation(ctx
, usage_string
, KRB5_USAGE_LEN
, &ekey
, ctx
->keylen
);
914 MALLOC(ctx
->ks
.enc
, cccbc_ctx
*, ctx
->enc_mode
->size
, M_TEMP
, M_WAITOK
| M_ZERO
);
915 cccbc_init(ctx
->enc_mode
, ctx
->ks
.enc
, ctx
->keylen
, ekey
);
918 if (ctx
->ks
.dec
== NULL
) {
919 krb5_make_usage(lctx
->initiate
?
920 KRB5_USAGE_ACCEPTOR_SEAL
: KRB5_USAGE_INITIATOR_SEAL
,
922 krb5_key_derivation(ctx
, usage_string
, KRB5_USAGE_LEN
, &ekey
, ctx
->keylen
);
923 MALLOC(ctx
->ks
.dec
, cccbc_ctx
*, ctx
->dec_mode
->size
, M_TEMP
, M_WAITOK
| M_ZERO
);
924 cccbc_init(ctx
->dec_mode
, ctx
->ks
.dec
, ctx
->keylen
, ekey
);
927 if (ctx
->ks
.ikey
[GSS_SND
] == NULL
) {
928 krb5_make_usage(lctx
->initiate
?
929 KRB5_USAGE_INITIATOR_SEAL
: KRB5_USAGE_ACCEPTOR_SEAL
,
931 krb5_key_derivation(ctx
, usage_string
, KRB5_USAGE_LEN
, &ctx
->ks
.ikey
[GSS_SND
], ctx
->keylen
);
933 if (ctx
->ks
.ikey
[GSS_RCV
] == NULL
) {
934 krb5_make_usage(lctx
->initiate
?
935 KRB5_USAGE_ACCEPTOR_SEAL
: KRB5_USAGE_INITIATOR_SEAL
,
937 krb5_key_derivation(ctx
, usage_string
, KRB5_USAGE_LEN
, &ctx
->ks
.ikey
[GSS_RCV
], ctx
->keylen
);
944 gss_crypto_ctx_free(crypto_ctx_t ctx
)
946 ctx
->ks
.ikey
[GSS_SND
] = NULL
;
947 if (ctx
->ks
.ikey
[GSS_RCV
] && ctx
->key
!= ctx
->ks
.ikey
[GSS_RCV
]) {
948 cc_clear(ctx
->keylen
, ctx
->ks
.ikey
[GSS_RCV
]);
949 FREE(ctx
->ks
.ikey
[GSS_RCV
], M_TEMP
);
951 ctx
->ks
.ikey
[GSS_RCV
] = NULL
;
953 cccbc_ctx_clear(ctx
->enc_mode
->size
, ctx
->ks
.enc
);
954 FREE(ctx
->ks
.enc
, M_TEMP
);
958 cccbc_ctx_clear(ctx
->dec_mode
->size
, ctx
->ks
.dec
);
959 FREE(ctx
->ks
.dec
, M_TEMP
);
962 if (ctx
->ckey
[GSS_SND
] && ctx
->ckey
[GSS_SND
] != ctx
->key
) {
963 cc_clear(ctx
->keylen
, ctx
->ckey
[GSS_SND
]);
964 FREE(ctx
->ckey
[GSS_SND
], M_TEMP
);
966 ctx
->ckey
[GSS_SND
] = NULL
;
967 if (ctx
->ckey
[GSS_RCV
] && ctx
->ckey
[GSS_RCV
] != ctx
->key
) {
968 cc_clear(ctx
->keylen
, ctx
->ckey
[GSS_RCV
]);
969 FREE(ctx
->ckey
[GSS_RCV
], M_TEMP
);
971 ctx
->ckey
[GSS_RCV
] = NULL
;
977 gss_crypto_ctx_init(struct crypto_ctx
*ctx
, lucid_context_t lucid
)
979 ctx
->gss_ctx
= lucid
;
981 uint8_t usage_string
[KRB5_USAGE_LEN
];
983 ctx
->keylen
= ctx
->gss_ctx
->ctx_key
.key
.key_len
;
984 key
= ctx
->gss_ctx
->ctx_key
.key
.key_val
;
985 ctx
->etype
= ctx
->gss_ctx
->ctx_key
.etype
;
989 case AES128_CTS_HMAC_SHA1_96
:
990 case AES256_CTS_HMAC_SHA1_96
:
991 ctx
->enc_mode
= ccaes_cbc_encrypt_mode();
992 assert(ctx
->enc_mode
);
993 ctx
->dec_mode
= ccaes_cbc_decrypt_mode();
994 assert(ctx
->dec_mode
);
997 ctx
->di
= ccsha1_di();
999 ctx
->flags
= CRYPTO_CTS_ENABLE
;
1001 ctx
->digest_size
= 12; /* 96 bits */
1002 krb5_make_usage(ctx
->gss_ctx
->initiate
?
1003 KRB5_USAGE_INITIATOR_SIGN
: KRB5_USAGE_ACCEPTOR_SIGN
,
1004 0x99, usage_string
);
1005 krb5_key_derivation(ctx
, usage_string
, KRB5_USAGE_LEN
, &ctx
->ckey
[GSS_SND
], ctx
->keylen
);
1006 krb5_make_usage(ctx
->gss_ctx
->initiate
?
1007 KRB5_USAGE_ACCEPTOR_SIGN
: KRB5_USAGE_INITIATOR_SIGN
,
1008 0x99, usage_string
);
1009 krb5_key_derivation(ctx
, usage_string
, KRB5_USAGE_LEN
, &ctx
->ckey
[GSS_RCV
], ctx
->keylen
);
1011 case DES3_CBC_SHA1_KD
:
1012 ctx
->enc_mode
= ccdes3_cbc_encrypt_mode();
1013 assert(ctx
->enc_mode
);
1014 ctx
->dec_mode
= ccdes3_cbc_decrypt_mode();
1015 assert(ctx
->dec_mode
);
1016 ctx
->ks
.ikey
[GSS_SND
] = ctx
->key
;
1017 ctx
->ks
.ikey
[GSS_RCV
] = ctx
->key
;
1018 ctx
->di
= ccsha1_di();
1021 ctx
->mpad
= ctx
->enc_mode
->block_size
;
1022 ctx
->digest_size
= 20; /* 160 bits */
1023 krb5_make_usage(KRB5_USAGE_ACCEPTOR_SIGN
, 0x99, usage_string
);
1024 krb5_3des_key_derivation(ctx
, usage_string
, KRB5_USAGE_LEN
, &ctx
->ckey
[GSS_SND
]);
1025 krb5_3des_key_derivation(ctx
, usage_string
, KRB5_USAGE_LEN
, &ctx
->ckey
[GSS_RCV
]);
1031 ctx
->lock
= lck_mtx_alloc_init(gss_krb5_mech_grp
, LCK_ATTR_NULL
);
1037 * CFX gss support routines
1039 /* From Heimdal cfx.h file RFC 4121 Cryptoo framework extensions */
1040 typedef struct gss_cfx_mic_token_desc_struct
1042 uint8_t TOK_ID
[2]; /* 04 04 */
1046 } gss_cfx_mic_token_desc
, *gss_cfx_mic_token
;
1048 typedef struct gss_cfx_wrap_token_desc_struct
1050 uint8_t TOK_ID
[2]; /* 05 04 */
1056 } gss_cfx_wrap_token_desc
, *gss_cfx_wrap_token
;
1058 /* End of cfx.h file */
1060 #define CFXSentByAcceptor (1 << 0)
1061 #define CFXSealed (1 << 1)
1062 #define CFXAcceptorSubkey (1 << 2)
1064 const gss_cfx_mic_token_desc mic_cfx_token
= {
1065 .TOK_ID
= "\x04\x04",
1067 .Filler
= "\xff\xff\xff\xff\xff",
1068 .SND_SEQ
= "\x00\x00\x00\x00\x00\x00\x00\x00"
1071 const gss_cfx_wrap_token_desc wrap_cfx_token
= {
1072 .TOK_ID
= "\x05\04",
1077 .SND_SEQ
= "\x00\x00\x00\x00\x00\x00\x00\x00"
1081 gss_krb5_cfx_verify_mic_token(gss_ctx_id_t ctx
, gss_cfx_mic_token token
)
1084 lucid_context_t lctx
= &ctx
->gss_lucid_ctx
;
1087 if (token
->TOK_ID
[0] != mic_cfx_token
.TOK_ID
[0] || token
->TOK_ID
[1] != mic_cfx_token
.TOK_ID
[1]) {
1088 printf("Bad mic TOK_ID %x %x\n", token
->TOK_ID
[0], token
->TOK_ID
[1]);
1092 flags
|= CFXSentByAcceptor
;
1093 if (lctx
->key_data
.lucid_protocol_u
.data_4121
.acceptor_subkey
)
1094 flags
|= CFXAcceptorSubkey
;
1095 if (token
->Flags
!= flags
) {
1096 printf("Bad flags received %x exptect %x\n", token
->Flags
, flags
);
1099 for (i
= 0; i
< 5; i
++) {
1100 if (token
->Filler
[i
] != mic_cfx_token
.Filler
[i
])
1105 printf("Bad mic filler %x @ %d\n", token
->Filler
[i
], i
);
1113 gss_krb5_cfx_get_mic(uint32_t *minor
, /* minor_status */
1114 gss_ctx_id_t ctx
, /* context_handle */
1115 gss_qop_t qop __unused
, /* qop_req (ignored) */
1116 gss_buffer_t mbp
, /* message mbuf */
1117 gss_buffer_t mic
/* message_token */)
1119 gss_cfx_mic_token_desc token
;
1120 lucid_context_t lctx
= &ctx
->gss_lucid_ctx
;
1121 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
1122 gss_buffer_desc header
;
1124 uint64_t seq
= htonll(lctx
->send_seq
);
1129 token
= mic_cfx_token
;
1130 mic
->length
= sizeof (token
) + cctx
->digest_size
;
1131 MALLOC(mic
->value
, void *, mic
->length
, M_TEMP
, M_WAITOK
| M_ZERO
);
1132 if (!lctx
->initiate
)
1133 token
.Flags
|= CFXSentByAcceptor
;
1134 if (lctx
->key_data
.lucid_protocol_u
.data_4121
.acceptor_subkey
)
1135 token
.Flags
|= CFXAcceptorSubkey
;
1136 memcpy(&token
.SND_SEQ
, &seq
, sizeof(lctx
->send_seq
));
1137 lctx
->send_seq
++; //XXX should only update this below on success? Heimdal seems to do it this way
1138 header
.value
= &token
;
1139 header
.length
= sizeof (gss_cfx_mic_token_desc
);
1141 *minor
= krb5_mic(cctx
, NULL
, mbp
, &header
, (uint8_t *)mic
->value
+ sizeof(token
), NULL
, 0, 0);
1145 FREE(mic
->value
, M_TEMP
);
1148 memcpy(mic
->value
, &token
, sizeof(token
));
1151 return (*minor
? GSS_S_FAILURE
: GSS_S_COMPLETE
);
1155 gss_krb5_cfx_verify_mic(uint32_t *minor
, /* minor_status */
1156 gss_ctx_id_t ctx
, /* context_handle */
1157 gss_buffer_t mbp
, /* message_buffer */
1158 gss_buffer_t mic
, /* message_token */
1159 gss_qop_t
*qop
/* qop_state */)
1161 gss_cfx_mic_token token
= mic
->value
;
1162 lucid_context_t lctx
= &ctx
->gss_lucid_ctx
;
1163 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
1164 uint8_t *digest
= (uint8_t *)mic
->value
+ sizeof (gss_cfx_mic_token_desc
);
1168 gss_buffer_desc header
;
1171 *qop
= GSS_C_QOP_DEFAULT
;
1175 if (mic
->length
!= sizeof(gss_cfx_mic_token_desc
) + cctx
->digest_size
) {
1176 printf("mic token wrong length\n");
1180 *minor
= gss_krb5_cfx_verify_mic_token(ctx
, token
);
1182 return (GSS_S_FAILURE
);
1183 header
.value
= token
;
1184 header
.length
= sizeof (gss_cfx_mic_token_desc
);
1185 *minor
= krb5_mic(cctx
, NULL
, mbp
, &header
, digest
, &verified
, 0, 0);
1188 //XXX errors and such? Sequencing and replay? Not supported in RPCSEC_GSS
1189 memcpy(&seq
, token
->SND_SEQ
, sizeof (uint64_t));
1191 lctx
->recv_seq
= seq
;
1195 return (verified
? GSS_S_COMPLETE
: GSS_S_BAD_SIG
);
1199 gss_krb5_cfx_get_mic_mbuf(uint32_t *minor
, /* minor_status */
1200 gss_ctx_id_t ctx
, /* context_handle */
1201 gss_qop_t qop __unused
,/* qop_req (ignored) */
1202 mbuf_t mbp
, /* message mbuf */
1203 size_t offset
, /* offest */
1204 size_t len
, /* length */
1205 gss_buffer_t mic
/* message_token */)
1207 gss_cfx_mic_token_desc token
;
1208 lucid_context_t lctx
= &ctx
->gss_lucid_ctx
;
1209 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
1211 uint64_t seq
= htonll(lctx
->send_seq
);
1212 gss_buffer_desc header
;
1218 token
= mic_cfx_token
;
1219 mic
->length
= sizeof (token
) + cctx
->digest_size
;
1220 MALLOC(mic
->value
, void *, mic
->length
, M_TEMP
, M_WAITOK
| M_ZERO
);
1221 if (!lctx
->initiate
)
1222 token
.Flags
|= CFXSentByAcceptor
;
1223 if (lctx
->key_data
.lucid_protocol_u
.data_4121
.acceptor_subkey
)
1224 token
.Flags
|= CFXAcceptorSubkey
;
1226 memcpy(&token
.SND_SEQ
, &seq
, sizeof(lctx
->send_seq
));
1227 lctx
->send_seq
++; //XXX should only update this below on success? Heimdal seems to do it this way
1229 header
.length
= sizeof(token
);
1230 header
.value
= &token
;
1232 len
= len
? len
: gss_mbuf_len(mbp
, offset
);
1233 *minor
= krb5_mic_mbuf(cctx
, NULL
, mbp
, offset
, len
, &header
, (uint8_t *)mic
->value
+ sizeof(token
), NULL
, 0, 0);
1237 FREE(mic
->value
, M_TEMP
);
1240 memcpy(mic
->value
, &token
, sizeof(token
));
1243 return (*minor
? GSS_S_FAILURE
: GSS_S_COMPLETE
);
1248 gss_krb5_cfx_verify_mic_mbuf(uint32_t *minor
, /* minor_status */
1249 gss_ctx_id_t ctx
, /* context_handle */
1250 mbuf_t mbp
, /* message_buffer */
1251 size_t offset
, /* offset */
1252 size_t len
, /* length */
1253 gss_buffer_t mic
, /* message_token */
1254 gss_qop_t
*qop
/* qop_state */)
1256 gss_cfx_mic_token token
= mic
->value
;
1257 lucid_context_t lctx
= &ctx
->gss_lucid_ctx
;
1258 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
1259 uint8_t *digest
= (uint8_t *)mic
->value
+ sizeof (gss_cfx_mic_token_desc
);
1263 gss_buffer_desc header
;
1266 *qop
= GSS_C_QOP_DEFAULT
;
1271 *minor
= gss_krb5_cfx_verify_mic_token(ctx
, token
);
1273 return (GSS_S_FAILURE
);
1275 header
.length
= sizeof(gss_cfx_mic_token_desc
);
1276 header
.value
= mic
->value
;
1278 *minor
= krb5_mic_mbuf(cctx
, NULL
, mbp
, offset
, len
, &header
, digest
, &verified
, 0, 0);
1280 //XXX errors and such? Sequencing and replay? Not Supported RPCSEC_GSS
1281 memcpy(&seq
, token
->SND_SEQ
, sizeof (uint64_t));
1283 lctx
->recv_seq
= seq
;
1285 return (verified
? GSS_S_COMPLETE
: GSS_S_BAD_SIG
);
1289 krb5_cfx_crypt_mbuf(crypto_ctx_t ctx
, mbuf_t
*mbp
, size_t *len
, int encrypt
, int reverse
)
1291 const struct ccmode_cbc
*ccmode
= encrypt
? ctx
->enc_mode
: ctx
->dec_mode
;
1292 uint8_t confounder
[ccmode
->block_size
];
1293 uint8_t digest
[ctx
->digest_size
];
1298 read_random(confounder
, ccmode
->block_size
);
1299 error
= gss_prepend_mbuf(mbp
, confounder
, ccmode
->block_size
);
1302 tlen
= *len
+ ccmode
->block_size
;
1304 r
= ctx
->mpad
- (tlen
% ctx
->mpad
);
1305 /* We expect that r == 0 from krb5_cfx_wrap */
1309 error
= gss_append_mbuf(*mbp
, mpad
, r
);
1314 error
= krb5_mic_mbuf(ctx
, NULL
, *mbp
, 0, tlen
, NULL
, digest
, NULL
, 1, 0);
1317 error
= krb5_crypt_mbuf(ctx
, mbp
, tlen
, 1, NULL
);
1320 error
= gss_append_mbuf(*mbp
, digest
, ctx
->digest_size
);
1323 *len
= tlen
+ ctx
->digest_size
;
1327 cccbc_ctx
*ks
= NULL
;
1329 if (*len
< ctx
->digest_size
+ sizeof(confounder
))
1331 tlen
= *len
- ctx
->digest_size
;
1332 /* get the digest */
1333 error
= mbuf_copydata(*mbp
, tlen
, ctx
->digest_size
, digest
);
1334 /* Remove the digest from the mbuffer */
1335 error
= gss_strip_mbuf(*mbp
, -ctx
->digest_size
);
1341 * Derive a key schedule that the sender can unwrap with. This
1342 * is so that RPCSEC_GSS can restore encrypted arguments for
1343 * resending. We do that because the RPCSEC_GSS sequence number in
1344 * the rpc header is prepended to the body of the message before wrapping.
1347 uint8_t usage_string
[KRB5_USAGE_LEN
];
1348 lucid_context_t lctx
= ctx
->gss_ctx
;
1350 krb5_make_usage(lctx
->initiate
?
1351 KRB5_USAGE_INITIATOR_SEAL
: KRB5_USAGE_ACCEPTOR_SEAL
,
1352 0xAA, usage_string
);
1353 krb5_key_derivation(ctx
, usage_string
, KRB5_USAGE_LEN
, &ekey
, ctx
->keylen
);
1354 MALLOC(ks
, cccbc_ctx
*, ctx
->dec_mode
->size
, M_TEMP
, M_WAITOK
| M_ZERO
);
1355 cccbc_init(ctx
->dec_mode
, ks
, ctx
->keylen
, ekey
);
1358 error
= krb5_crypt_mbuf(ctx
, mbp
, tlen
, 0, ks
);
1362 error
= krb5_mic_mbuf(ctx
, NULL
, *mbp
, 0, tlen
, NULL
, digest
, &verf
, 1, reverse
);
1367 /* strip off the confounder */
1368 error
= gss_strip_mbuf(*mbp
, ccmode
->block_size
);
1371 *len
= tlen
- ccmode
->block_size
;
1377 gss_krb5_cfx_wrap_mbuf(uint32_t *minor
, /* minor_status */
1378 gss_ctx_id_t ctx
, /* context_handle */
1379 int conf_flag
, /* conf_req_flag */
1380 gss_qop_t qop __unused
, /* qop_req */
1381 mbuf_t
*mbp
, /* input/output message_buffer */
1382 size_t len
, /* mbuf chain length */
1383 int *conf
/* conf_state */)
1385 gss_cfx_wrap_token_desc token
;
1386 lucid_context_t lctx
= &ctx
->gss_lucid_ctx
;
1387 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
1390 uint64_t seq
= htonll(lctx
->send_seq
);
1398 token
= wrap_cfx_token
;
1399 if (!lctx
->initiate
)
1400 token
.Flags
|= CFXSentByAcceptor
;
1401 if (lctx
->key_data
.lucid_protocol_u
.data_4121
.acceptor_subkey
)
1402 token
.Flags
|= CFXAcceptorSubkey
;
1403 memcpy(&token
.SND_SEQ
, &seq
, sizeof(uint64_t));
1406 uint8_t pad
[cctx
->mpad
];
1409 token
.Flags
|= CFXSealed
;
1410 memset(pad
, 0, cctx
->mpad
);
1411 if (cctx
->mpad
> 1) {
1412 plen
= htons(cctx
->mpad
- ((len
+ sizeof (gss_cfx_wrap_token_desc
)) % cctx
->mpad
));
1413 token
.EC
[0] = ((plen
>> 8) & 0xff);
1414 token
.EC
[1] = (plen
& 0xff);
1417 error
= gss_append_mbuf(*mbp
, pad
, plen
);
1421 error
= gss_append_mbuf(*mbp
, (uint8_t *)&token
, sizeof(gss_cfx_wrap_token_desc
));
1422 len
+= sizeof (gss_cfx_wrap_token_desc
);
1425 error
= krb5_cfx_crypt_mbuf(cctx
, mbp
, &len
, 1, 0);
1427 error
= gss_prepend_mbuf(mbp
, (uint8_t *)&token
, sizeof(gss_cfx_wrap_token_desc
));
1429 uint8_t digest
[cctx
->digest_size
];
1430 gss_buffer_desc header
;
1432 header
.length
= sizeof(token
);
1433 header
.value
= &token
;
1435 error
= krb5_mic_mbuf(cctx
, NULL
, *mbp
, 0, len
, &header
, digest
, NULL
, 1, 0);
1437 error
= gss_append_mbuf(*mbp
, digest
, cctx
->digest_size
);
1439 uint16_t plen
= htons(cctx
->digest_size
);
1440 memcpy(token
.EC
, &plen
, 2);
1441 error
= gss_prepend_mbuf(mbp
, (uint8_t *)&token
, sizeof (gss_cfx_wrap_token_desc
));
1447 return (GSS_S_FAILURE
);
1450 return (GSS_S_COMPLETE
);
1454 * Given a wrap token the has a rrc, move the trailer back to the end.
1457 gss_krb5_cfx_unwrap_rrc_mbuf(mbuf_t header
, size_t rrc
)
1459 mbuf_t body
, trailer
;
1461 gss_normalize_mbuf(header
, sizeof(gss_cfx_wrap_token_desc
), &rrc
, &trailer
, &body
, 0);
1462 gss_join_mbuf(header
, body
, trailer
);
1466 gss_krb5_cfx_unwrap_mbuf(uint32_t * minor
, /* minor_status */
1467 gss_ctx_id_t ctx
, /* context_handle */
1468 mbuf_t
*mbp
, /* input/output message_buffer */
1469 size_t len
, /* mbuf chain length */
1470 int *conf_flag
, /* conf_state */
1471 gss_qop_t
*qop
/* qop state */)
1473 gss_cfx_wrap_token_desc token
;
1474 lucid_context_t lctx
= &ctx
->gss_lucid_ctx
;
1475 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
1477 uint16_t ec
= 0 , rrc
= 0;
1479 int reverse
= (*qop
== GSS_C_QOP_REVERSE
);
1480 int initiate
= lctx
->initiate
? (reverse
? 0 : 1) : (reverse
? 1 : 0);
1482 error
= mbuf_copydata(*mbp
, 0, sizeof (gss_cfx_wrap_token_desc
), &token
);
1483 gss_strip_mbuf(*mbp
, sizeof (gss_cfx_wrap_token_desc
));
1484 len
-= sizeof (gss_cfx_wrap_token_desc
);
1486 /* Check for valid token */
1487 if (token
.TOK_ID
[0] != wrap_cfx_token
.TOK_ID
[0] ||
1488 token
.TOK_ID
[1] != wrap_cfx_token
.TOK_ID
[1] ||
1489 token
.Filler
!= wrap_cfx_token
.Filler
) {
1490 printf("Token id does not match\n");
1493 if ((initiate
&& !(token
.Flags
& CFXSentByAcceptor
)) ||
1494 (lctx
->key_data
.lucid_protocol_u
.data_4121
.acceptor_subkey
&& !(token
.Flags
& CFXAcceptorSubkey
))) {
1495 printf("Bad flags %x\n", token
.Flags
);
1499 /* XXX Sequence replay detection */
1500 memcpy(&seq
, token
.SND_SEQ
, sizeof (seq
));
1502 lctx
->recv_seq
= seq
;
1504 ec
= (token
.EC
[0] << 8) | token
.EC
[1];
1505 rrc
= (token
.RRC
[0] << 8) | token
.RRC
[1];
1506 *qop
= GSS_C_QOP_DEFAULT
;
1507 conf
= ((token
.Flags
& CFXSealed
) == CFXSealed
);
1511 gss_cfx_wrap_token_desc etoken
;
1513 if (rrc
) /* Handle Right rotation count */
1514 gss_krb5_cfx_unwrap_rrc_mbuf(*mbp
, rrc
);
1515 error
= krb5_cfx_crypt_mbuf(cctx
, mbp
, &len
, 0, reverse
);
1517 printf("krb5_cfx_crypt_mbuf %d\n", error
);
1519 return (GSS_S_FAILURE
);
1521 if (len
>= sizeof(gss_cfx_wrap_token_desc
))
1522 len
-= sizeof(gss_cfx_wrap_token_desc
);
1525 mbuf_copydata(*mbp
, len
, sizeof(gss_cfx_wrap_token_desc
), &etoken
);
1526 /* Verify etoken with the token wich should be the same, except the rc field is always zero */
1527 token
.RRC
[0] = token
.RRC
[1] = 0;
1528 if (memcmp(&token
, &etoken
, sizeof (gss_cfx_wrap_token_desc
)) != 0) {
1529 printf("Encrypted token mismach\n");
1532 /* strip the encrypted token and any pad bytes */
1533 gss_strip_mbuf(*mbp
, -(sizeof(gss_cfx_wrap_token_desc
) + ec
));
1534 len
-= (sizeof(gss_cfx_wrap_token_desc
) + ec
);
1536 uint8_t digest
[cctx
->digest_size
];
1538 gss_buffer_desc header
;
1540 if (ec
!= cctx
->digest_size
|| len
>= cctx
->digest_size
)
1542 len
-= cctx
->digest_size
;
1543 mbuf_copydata(*mbp
, len
, cctx
->digest_size
, digest
);
1544 gss_strip_mbuf(*mbp
, -cctx
->digest_size
);
1545 /* When calculating the mic header fields ec and rcc must be zero */
1546 token
.EC
[0] = token
.EC
[1] = token
.RRC
[0] = token
.RRC
[1] = 0;
1547 header
.value
= &token
;
1548 header
.length
= sizeof(gss_cfx_wrap_token_desc
);
1549 error
= krb5_mic_mbuf(cctx
, NULL
, *mbp
, 0, len
, &header
, digest
, &verf
, 1, reverse
);
1553 return (GSS_S_COMPLETE
);
1557 return (GSS_S_FAILURE
);
1561 * RFC 1964 3DES support
1564 typedef struct gss_1964_mic_token_desc_struct
{
1565 uint8_t TOK_ID
[2]; /* 01 01 */
1566 uint8_t Sign_Alg
[2];
1567 uint8_t Filler
[4]; /* ff ff ff ff */
1568 } gss_1964_mic_token_desc
, *gss_1964_mic_token
;
1570 typedef struct gss_1964_wrap_token_desc_struct
{
1571 uint8_t TOK_ID
[2]; /* 02 01 */
1572 uint8_t Sign_Alg
[2];
1573 uint8_t Seal_Alg
[2];
1574 uint8_t Filler
[2]; /* ff ff */
1575 } gss_1964_wrap_token_desc
, *gss_1964_wrap_token
;
1577 typedef struct gss_1964_delete_token_desc_struct
{
1578 uint8_t TOK_ID
[2]; /* 01 02 */
1579 uint8_t Sign_Alg
[2];
1580 uint8_t Filler
[4]; /* ff ff ff ff */
1581 } gss_1964_delete_token_desc
, *gss_1964_delete_token
;
1583 typedef struct gss_1964_header_desc_struct
{
1584 uint8_t App0
; /* 0x60 Application 0 constructed */
1585 uint8_t AppLen
[]; /* Variable Der length */
1586 } gss_1964_header_desc
, *gss_1964_header
;
1589 gss_1964_mic_token_desc mic_tok
;
1590 gss_1964_wrap_token_desc wrap_tok
;
1591 gss_1964_delete_token_desc del_tok
;
1592 } gss_1964_tok_type
__attribute__((transparent_union
));
1594 typedef struct gss_1964_token_body_struct
1596 uint8_t OIDType
; /* 0x06 */
1597 uint8_t OIDLen
; /* 0x09 */
1598 uint8_t kerb_mech
[9]; /* Der Encode kerberos mech 1.2.840.113554.1.2.2
1599 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x01, 0x02, 0x02 */
1600 gss_1964_tok_type body
;
1602 uint8_t Hash
[]; /* Mic */
1603 } gss_1964_token_body_desc
, *gss_1964_token_body
;
1606 gss_1964_header_desc tok_1964_header
= {
1610 gss_1964_mic_token_desc mic_1964_token
= {
1611 .TOK_ID
= "\x01\x01",
1612 .Filler
= "\xff\xff\xff\xff"
1615 gss_1964_wrap_token_desc wrap_1964_token
= {
1616 .TOK_ID
= "\x02\x01",
1617 .Filler
= "\xff\xff"
1620 gss_1964_delete_token_desc del_1964_token
= {
1621 .TOK_ID
= "\x01\x01",
1622 .Filler
= "\xff\xff\xff\xff"
1625 gss_1964_token_body_desc body_1964_token
= {
1628 .kerb_mech
= "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02",
1631 #define GSS_KRB5_3DES_MAXTOKSZ (sizeof(gss_1964_header_desc) + 5 /* max der length supported */ + sizeof(gss_1964_token_body_desc))
1633 uint32_t gss_krb5_3des_get_mic(uint32_t *, gss_ctx_id_t
, gss_qop_t
, gss_buffer_t
, gss_buffer_t
);
1634 uint32_t gss_krb5_3des_verify_mic(uint32_t *, gss_ctx_id_t
, gss_buffer_t
, gss_buffer_t
, gss_qop_t
*);
1635 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
);
1636 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
*);
1637 uint32_t gss_krb5_3des_wrap_mbuf(uint32_t *, gss_ctx_id_t
, int, gss_qop_t
, mbuf_t
*, size_t, int *);
1638 uint32_t gss_krb5_3des_unwrap_mbuf(uint32_t *, gss_ctx_id_t
, mbuf_t
*, size_t, int *, gss_qop_t
*);
1641 * Decode an ASN.1 DER length field
1644 gss_krb5_der_length_get(uint8_t **pp
)
1647 uint32_t flen
, len
= 0;
1652 if (flen
> sizeof(uint32_t))
1655 len
= (len
<< 8) + *p
++;
1664 * Determine size of ASN.1 DER length
1667 gss_krb5_der_length_size(int len
)
1670 len
< (1 << 7) ? 1 :
1671 len
< (1 << 8) ? 2 :
1672 len
< (1 << 16) ? 3 :
1673 len
< (1 << 24) ? 4 : 5;
1677 * Encode an ASN.1 DER length field
1680 gss_krb5_der_length_put(uint8_t **pp
, int len
)
1682 int sz
= gss_krb5_der_length_size(len
);
1686 *p
++ = (uint8_t) len
;
1688 *p
++ = (uint8_t) ((sz
-1) | 0x80);
1691 *p
++ = (uint8_t) ((len
>> (sz
* 8)) & 0xff);
1698 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
)
1700 gss_1964_header token
;
1701 gss_1964_token_body tokbody
;
1702 lucid_context_t lctx
= &ctx
->gss_lucid_ctx
;
1703 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
1704 uint32_t seq
= (uint32_t) (lctx
->send_seq
++ & 0xffff);
1705 size_t toklen
= sizeof(gss_1964_token_body_desc
) + cctx
->digest_size
;
1706 size_t alloclen
= toklen
+ sizeof (gss_1964_header_desc
) + gss_krb5_der_length_size(toklen
+ datalen
);
1709 MALLOC(token
, gss_1964_header
, alloclen
, M_TEMP
, M_WAITOK
|M_ZERO
);
1710 *token
= tok_1964_header
;
1711 tokptr
= token
->AppLen
;
1712 gss_krb5_der_length_put(&tokptr
, toklen
+ datalen
);
1713 tokbody
= (gss_1964_token_body
)tokptr
;
1714 *tokbody
= body_1964_token
; /* Initalize the token body */
1715 tokbody
->body
= body
; /* and now set the body to the token type passed in */
1717 for (int i
= 0; i
< 4; i
++)
1718 tokbody
->SND_SEQ
[i
] = (uint8_t)((seq
>> (i
* 8)) & 0xff);
1719 for (int i
= 4; i
< 8; i
++)
1720 tokbody
->SND_SEQ
[i
] = lctx
->initiate
? 0x00 : 0xff;
1722 size_t blocksize
= cctx
->enc_mode
->block_size
;
1723 cccbc_iv_decl(blocksize
, iv
);
1724 cccbc_ctx_decl(cctx
->enc_mode
->size
, enc_ctx
);
1725 cccbc_set_iv(cctx
->enc_mode
, iv
, hash
->value
);
1726 cccbc_init(cctx
->enc_mode
, enc_ctx
, cctx
->keylen
, cctx
->key
);
1727 cccbc_update(cctx
->enc_mode
, enc_ctx
, iv
, 1, tokbody
->SND_SEQ
, tokbody
->SND_SEQ
);
1729 assert(hash
->length
== cctx
->digest_size
);
1730 memcpy(tokbody
->Hash
, hash
->value
, hash
->length
);
1731 des3_token
->length
= alloclen
;
1732 des3_token
->value
= token
;
1736 gss_krb5_3des_token_get(gss_ctx_id_t ctx
, gss_buffer_t intok
,
1737 gss_1964_tok_type body
, gss_buffer_t hash
, size_t *offset
, size_t *len
, int reverse
)
1739 gss_1964_header token
= intok
->value
;
1740 gss_1964_token_body tokbody
;
1741 lucid_context_t lctx
= &ctx
->gss_lucid_ctx
;
1742 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
1749 if (token
->App0
!= tok_1964_header
.App0
) {
1750 printf("%s: bad framing\n", __func__
);
1751 printgbuf(__func__
, intok
);
1754 tokptr
= token
->AppLen
;
1755 length
= gss_krb5_der_length_get(&tokptr
);
1757 printf("%s: invalid length\n", __func__
);
1758 printgbuf(__func__
, intok
);
1761 toklen
= sizeof (gss_1964_header_desc
) + gss_krb5_der_length_size(length
)
1762 + sizeof (gss_1964_token_body_desc
);
1764 if (intok
->length
< toklen
+ cctx
->digest_size
) {
1765 printf("%s: token to short", __func__
);
1766 printf("toklen = %d, length = %d\n", (int)toklen
, (int)length
);
1767 printgbuf(__func__
, intok
);
1772 *offset
= toklen
+ cctx
->digest_size
;
1775 *len
= length
- sizeof (gss_1964_token_body_desc
) - cctx
->digest_size
;
1777 tokbody
= (gss_1964_token_body
)tokptr
;
1778 if (tokbody
->OIDType
!= body_1964_token
.OIDType
||
1779 tokbody
->OIDLen
!= body_1964_token
.OIDLen
||
1780 memcmp(tokbody
->kerb_mech
, body_1964_token
.kerb_mech
, tokbody
->OIDLen
) != 0) {
1781 printf("%s: Invalid mechanism\n", __func__
);
1782 printgbuf(__func__
, intok
);
1785 if (memcmp(&tokbody
->body
, &body
, sizeof(gss_1964_tok_type
)) != 0) {
1786 printf("%s: Invalid body\n", __func__
);
1787 printgbuf(__func__
, intok
);
1790 size_t blocksize
= cctx
->enc_mode
->block_size
;
1791 uint8_t *block
= tokbody
->SND_SEQ
;
1793 assert(blocksize
== sizeof(tokbody
->SND_SEQ
));
1794 cccbc_iv_decl(blocksize
, iv
);
1795 cccbc_ctx_decl(cctx
->dec_mode
->size
, dec_ctx
);
1796 cccbc_set_iv(cctx
->dec_mode
, iv
, tokbody
->Hash
);
1797 cccbc_init(cctx
->dec_mode
, dec_ctx
, cctx
->keylen
, cctx
->key
);
1798 cccbc_update(cctx
->dec_mode
, dec_ctx
, iv
, 1, block
, block
);
1800 initiate
= lctx
->initiate
? (reverse
? 0 : 1) : (reverse
? 1 : 0);
1801 for(int i
= 4; i
< 8; i
++) {
1802 if (tokbody
->SND_SEQ
[i
] != (initiate
? 0xff : 0x00)) {
1803 printf("%s: Invalid des mac\n", __func__
);
1804 printgbuf(__func__
, intok
);
1809 memcpy(&seq
, tokbody
->SND_SEQ
, sizeof (uint32_t));
1811 lctx
->recv_seq
= ntohl(seq
);
1813 assert(hash
->length
>= cctx
->digest_size
);
1814 memcpy(hash
->value
, tokbody
->Hash
, cctx
->digest_size
);
1820 gss_krb5_3des_get_mic(uint32_t *minor
, /* minor status */
1821 gss_ctx_id_t ctx
, /* krb5 context id */
1822 gss_qop_t qop __unused
, /* qop_req (ignored) */
1823 gss_buffer_t mbp
, /* message buffer in */
1824 gss_buffer_t mic
) /* mic token out */
1826 gss_1964_mic_token_desc tokbody
= mic_1964_token
;
1827 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
1828 gss_buffer_desc hash
;
1829 gss_buffer_desc header
;
1830 uint8_t hashval
[cctx
->digest_size
];
1832 hash
.length
= cctx
->digest_size
;
1833 hash
.value
= hashval
;
1834 tokbody
.Sign_Alg
[0] = 0x04; /* lctx->keydata.lucid_protocol_u.data_1964.sign_alg */
1835 tokbody
.Sign_Alg
[1] = 0x00;
1836 header
.length
= sizeof (gss_1964_mic_token_desc
);
1837 header
.value
= & tokbody
;
1840 *minor
= krb5_mic(cctx
, &header
, mbp
, NULL
, hashval
, NULL
, 0, 0);
1842 return (GSS_S_FAILURE
);
1844 /* Make the token */
1845 gss_krb5_3des_token_put(ctx
, tokbody
, &hash
, 0, mic
);
1847 return (GSS_S_COMPLETE
);
1851 gss_krb5_3des_verify_mic(uint32_t *minor
,
1857 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
1858 uint8_t hashval
[cctx
->digest_size
];
1859 gss_buffer_desc hash
;
1860 gss_1964_mic_token_desc mtok
= mic_1964_token
;
1861 gss_buffer_desc header
;
1864 mtok
.Sign_Alg
[0] = 0x04; /* lctx->key_data.lucid_protocol_u.data_1964.sign_alg */
1865 mtok
.Sign_Alg
[1] = 0x00;
1866 hash
.length
= cctx
->digest_size
;
1867 hash
.value
= hashval
;
1868 header
.length
= sizeof(gss_1964_mic_token_desc
);
1869 header
.value
= &mtok
;
1872 *qop
= GSS_C_QOP_DEFAULT
;
1874 *minor
= gss_krb5_3des_token_get(ctx
, mic
, mtok
, &hash
, NULL
, NULL
, 0);
1876 return (GSS_S_FAILURE
);
1878 *minor
= krb5_mic(cctx
, &header
, mbp
, NULL
, hashval
, &verf
, 0, 0);
1880 return (GSS_S_FAILURE
);
1882 return (verf
? GSS_S_COMPLETE
: GSS_S_BAD_SIG
);
1886 gss_krb5_3des_get_mic_mbuf(uint32_t *minor
,
1888 gss_qop_t qop __unused
,
1894 gss_1964_mic_token_desc tokbody
= mic_1964_token
;
1895 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
1896 gss_buffer_desc header
;
1897 gss_buffer_desc hash
;
1898 uint8_t hashval
[cctx
->digest_size
];
1900 hash
.length
= cctx
->digest_size
;
1901 hash
.value
= hashval
;
1902 tokbody
.Sign_Alg
[0] = 0x04; /* lctx->key_data.lucid_protocol_u.data_4121.sign_alg */
1903 tokbody
.Sign_Alg
[1] = 0x00;
1904 header
.length
= sizeof (gss_1964_mic_token_desc
);
1905 header
.value
= &tokbody
;
1908 *minor
= krb5_mic_mbuf(cctx
, &header
, mbp
, offset
, len
, NULL
, hashval
, NULL
, 0, 0);
1910 return (GSS_S_FAILURE
);
1912 /* Make the token */
1913 gss_krb5_3des_token_put(ctx
, tokbody
, &hash
, 0, mic
);
1915 return (GSS_S_COMPLETE
);
1919 gss_krb5_3des_verify_mic_mbuf(uint32_t *minor
,
1927 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
1928 uint8_t hashval
[cctx
->digest_size
];
1929 gss_buffer_desc header
;
1930 gss_buffer_desc hash
;
1931 gss_1964_mic_token_desc mtok
= mic_1964_token
;
1934 mtok
.Sign_Alg
[0] = 0x04; /* lctx->key_data.lucic_protocol_u.data1964.sign_alg */
1935 mtok
.Sign_Alg
[1] = 0x00;
1936 hash
.length
= cctx
->digest_size
;
1937 hash
.value
= hashval
;
1938 header
.length
= sizeof(gss_1964_mic_token_desc
);
1939 header
.value
= &mtok
;
1942 *qop
= GSS_C_QOP_DEFAULT
;
1944 *minor
= gss_krb5_3des_token_get(ctx
, mic
, mtok
, &hash
, NULL
, NULL
, 0);
1946 return (GSS_S_FAILURE
);
1948 *minor
= krb5_mic_mbuf(cctx
, &header
, mbp
, offset
, len
, NULL
, hashval
, &verf
, 0, 0);
1950 return (GSS_S_FAILURE
);
1952 return (verf
? GSS_S_COMPLETE
: GSS_S_BAD_SIG
);
1956 gss_krb5_3des_wrap_mbuf(uint32_t *minor
,
1959 gss_qop_t qop __unused
,
1964 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
1965 const struct ccmode_cbc
*ccmode
= cctx
->enc_mode
;
1968 uint8_t confounder
[ccmode
->block_size
];
1969 gss_1964_wrap_token_desc tokbody
= wrap_1964_token
;
1970 gss_buffer_desc header
;
1971 gss_buffer_desc mic
;
1972 gss_buffer_desc hash
;
1973 uint8_t hashval
[cctx
->digest_size
];
1976 *conf_state
= conf_flag
;
1978 hash
.length
= cctx
->digest_size
;
1979 hash
.value
= hashval
;
1980 tokbody
.Sign_Alg
[0] = 0x04; /* lctx->key_data.lucid_protocol_u.data_1964.sign_alg */
1981 tokbody
.Sign_Alg
[1] = 0x00;
1982 /* conf_flag ? lctx->key_data.lucid_protocol_u.data_1964.seal_alg : 0xffff */
1983 tokbody
.Seal_Alg
[0] = conf_flag
? 0x02 : 0xff;
1984 tokbody
.Seal_Alg
[1] = conf_flag
? 0x00 : 0xff;
1985 header
.length
= sizeof (gss_1964_wrap_token_desc
);
1986 header
.value
= &tokbody
;
1988 /* Prepend confounder */
1989 read_random(confounder
, ccmode
->block_size
);
1990 *minor
= gss_prepend_mbuf(mbp
, confounder
, ccmode
->block_size
);
1992 return (GSS_S_FAILURE
);
1994 /* Append trailer of up to 8 bytes and set pad length in each trailer byte */
1995 padlen
= 8 - len
% 8;
1996 for (int i
= 0; i
< padlen
; i
++)
1998 *minor
= gss_append_mbuf(*mbp
, pad
, padlen
);
2000 return (GSS_S_FAILURE
);
2002 len
+= ccmode
->block_size
+ padlen
;
2005 *minor
= krb5_mic_mbuf(cctx
, &header
, *mbp
, 0, len
, NULL
, hashval
, NULL
, 0, 0);
2007 return (GSS_S_FAILURE
);
2009 /* Make the token */
2010 gss_krb5_3des_token_put(ctx
, tokbody
, &hash
, len
, &mic
);
2013 *minor
= krb5_crypt_mbuf(cctx
, mbp
, len
, 1, 0);
2015 return (GSS_S_FAILURE
);
2018 *minor
= gss_prepend_mbuf(mbp
, mic
.value
, mic
.length
);
2020 return (*minor
? GSS_S_FAILURE
: GSS_S_COMPLETE
);
2024 gss_krb5_3des_unwrap_mbuf(uint32_t *minor
,
2031 crypto_ctx_t cctx
= &ctx
->gss_cryptor
;
2032 const struct ccmode_cbc
*ccmode
= cctx
->dec_mode
;
2033 size_t length
= 0, offset
;
2034 gss_buffer_desc hash
;
2035 uint8_t hashval
[cctx
->digest_size
];
2036 gss_buffer_desc itoken
;
2037 uint8_t tbuffer
[GSS_KRB5_3DES_MAXTOKSZ
+ cctx
->digest_size
];
2038 itoken
.length
= GSS_KRB5_3DES_MAXTOKSZ
+ cctx
->digest_size
;
2039 itoken
.value
= tbuffer
;
2040 gss_1964_wrap_token_desc wrap
= wrap_1964_token
;
2041 gss_buffer_desc header
;
2044 int cflag
, verified
, reverse
= 0;
2046 if (len
< GSS_KRB5_3DES_MAXTOKSZ
) {
2048 return (GSS_S_FAILURE
);
2051 if (*qop
== GSS_C_QOP_REVERSE
)
2053 *qop
= GSS_C_QOP_DEFAULT
;
2055 *minor
= mbuf_copydata(*mbp
, 0, itoken
.length
, itoken
.value
);
2057 return (GSS_S_FAILURE
);
2059 hash
.length
= cctx
->digest_size
;
2060 hash
.value
= hashval
;
2061 wrap
.Sign_Alg
[0] = 0x04;
2062 wrap
.Sign_Alg
[1] = 0x00;
2063 wrap
.Seal_Alg
[0] = 0x02;
2064 wrap
.Seal_Alg
[1] = 0x00;
2066 for (cflag
= 1; cflag
>= 0; cflag
--) {
2067 *minor
= gss_krb5_3des_token_get(ctx
, &itoken
, wrap
, &hash
, &offset
, &length
, reverse
);
2070 wrap
.Seal_Alg
[0] = 0xff;
2071 wrap
.Seal_Alg
[0] = 0xff;
2074 return (GSS_S_FAILURE
);
2077 *conf_state
= cflag
;
2080 * Seperate off the header
2082 *minor
= gss_normalize_mbuf(*mbp
, offset
, &length
, &smb
, &tmb
, 0);
2084 return (GSS_S_FAILURE
);
2086 assert(tmb
== NULL
);
2088 /* Decrypt the chain if needed */
2090 *minor
= krb5_crypt_mbuf(cctx
, &smb
, length
, 0, NULL
);
2092 return (GSS_S_FAILURE
);
2095 /* Verify the mic */
2096 header
.length
= sizeof(gss_1964_wrap_token_desc
);
2097 header
.value
= &wrap
;
2099 *minor
= krb5_mic_mbuf(cctx
, &header
, smb
, 0, length
, NULL
, hashval
, &verified
, 0, 0);
2101 return (GSS_S_BAD_SIG
);
2103 return (GSS_S_FAILURE
);
2105 /* Get the pad bytes */
2106 *minor
= mbuf_copydata(smb
, length
- 1, 1, &padlen
);
2108 return (GSS_S_FAILURE
);
2110 /* Strip the confounder and trailing pad bytes */
2111 gss_strip_mbuf(smb
, -padlen
);
2112 gss_strip_mbuf(smb
, ccmode
->block_size
);
2119 return (GSS_S_COMPLETE
);
2123 etype_name(etypes etype
)
2126 case DES3_CBC_SHA1_KD
:
2127 return ("des3-cbc-sha1");
2128 case AES128_CTS_HMAC_SHA1_96
:
2129 return ("aes128-cts-hmac-sha1-96");
2130 case AES256_CTS_HMAC_SHA1_96
:
2131 return ("aes-cts-hmac-sha1-96");
2133 return ("unknown enctype");
2138 supported_etype(uint32_t proto
, etypes etype
)
2140 const char *proto_name
;
2145 proto_name
= "RFC 1964 krb5 gss mech";
2147 case DES3_CBC_SHA1_KD
:
2155 proto_name
= "RFC 4121 krb5 gss mech";
2157 case AES256_CTS_HMAC_SHA1_96
:
2158 case AES128_CTS_HMAC_SHA1_96
:
2165 proto_name
= "Unknown krb5 gss mech";
2168 printf("%s: Non supported encryption %s (%d) type for protocol %s (%d)\n",
2169 __func__
, etype_name(etype
), etype
, proto_name
, proto
);
2174 * Kerberos gss mech entry points
2177 gss_krb5_get_mic(uint32_t *minor
, /* minor_status */
2178 gss_ctx_id_t ctx
, /* context_handle */
2179 gss_qop_t qop
, /* qop_req */
2180 gss_buffer_t mbp
, /* message buffer */
2181 gss_buffer_t mic
/* message_token */)
2183 uint32_t minor_stat
= 0;
2186 minor
= &minor_stat
;
2189 /* Validate context */
2190 if (ctx
== NULL
|| ((lucid_context_version_t
)ctx
)->version
!= 1)
2191 return (GSS_S_NO_CONTEXT
);
2193 if (!supported_etype(ctx
->gss_lucid_ctx
.key_data
.proto
, ctx
->gss_cryptor
.etype
)) {
2195 return (GSS_S_FAILURE
);
2198 switch(ctx
->gss_lucid_ctx
.key_data
.proto
) {
2200 /* RFC 1964 DES3 case */
2201 return (gss_krb5_3des_get_mic(minor
, ctx
, qop
, mbp
, mic
));
2203 /* RFC 4121 CFX case */
2204 return (gss_krb5_cfx_get_mic(minor
, ctx
, qop
, mbp
, mic
));
2207 return (GSS_S_COMPLETE
);
2211 gss_krb5_verify_mic(uint32_t *minor
, /* minor_status */
2212 gss_ctx_id_t ctx
, /* context_handle */
2213 gss_buffer_t mbp
, /* message_buffer */
2214 gss_buffer_t mic
, /* message_token */
2215 gss_qop_t
*qop
/* qop_state */)
2217 uint32_t minor_stat
= 0;
2218 gss_qop_t qop_val
= GSS_C_QOP_DEFAULT
;
2221 minor
= &minor_stat
;
2227 /* Validate context */
2228 if (ctx
== NULL
|| ((lucid_context_version_t
)ctx
)->version
!= 1)
2229 return (GSS_S_NO_CONTEXT
);
2231 if (!supported_etype(ctx
->gss_lucid_ctx
.key_data
.proto
, ctx
->gss_cryptor
.etype
)) {
2233 return (GSS_S_FAILURE
);
2236 switch(ctx
->gss_lucid_ctx
.key_data
.proto
) {
2238 /* RFC 1964 DES3 case */
2239 return (gss_krb5_3des_verify_mic(minor
, ctx
, mbp
, mic
, qop
));
2241 /* RFC 4121 CFX case */
2242 return (gss_krb5_cfx_verify_mic(minor
, ctx
, mbp
, mic
, qop
));
2244 return (GSS_S_COMPLETE
);
2248 gss_krb5_get_mic_mbuf(uint32_t *minor
, /* minor_status */
2249 gss_ctx_id_t ctx
, /* context_handle */
2250 gss_qop_t qop
, /* qop_req */
2251 mbuf_t mbp
, /* message mbuf */
2252 size_t offset
, /* offest */
2253 size_t len
, /* length */
2254 gss_buffer_t mic
/* message_token */)
2256 uint32_t minor_stat
= 0;
2259 minor
= &minor_stat
;
2265 /* Validate context */
2266 if (ctx
== NULL
|| ((lucid_context_version_t
)ctx
)->version
!= 1)
2267 return (GSS_S_NO_CONTEXT
);
2269 if (!supported_etype(ctx
->gss_lucid_ctx
.key_data
.proto
, ctx
->gss_cryptor
.etype
)) {
2271 return (GSS_S_FAILURE
);
2274 switch(ctx
->gss_lucid_ctx
.key_data
.proto
) {
2276 /* RFC 1964 DES3 case */
2277 return (gss_krb5_3des_get_mic_mbuf(minor
, ctx
, qop
, mbp
, offset
, len
, mic
));
2279 /* RFC 4121 CFX case */
2280 return (gss_krb5_cfx_get_mic_mbuf(minor
, ctx
, qop
, mbp
, offset
, len
, mic
));
2283 return (GSS_S_COMPLETE
);
2287 gss_krb5_verify_mic_mbuf(uint32_t *minor
, /* minor_status */
2288 gss_ctx_id_t ctx
, /* context_handle */
2289 mbuf_t mbp
, /* message_buffer */
2290 size_t offset
, /* offset */
2291 size_t len
, /* length */
2292 gss_buffer_t mic
, /* message_token */
2293 gss_qop_t
*qop
/* qop_state */)
2295 uint32_t minor_stat
= 0;
2296 gss_qop_t qop_val
= GSS_C_QOP_DEFAULT
;
2299 minor
= &minor_stat
;
2308 /* Validate context */
2309 if (ctx
== NULL
|| ((lucid_context_version_t
)ctx
)->version
!= 1)
2310 return (GSS_S_NO_CONTEXT
);
2312 if (!supported_etype(ctx
->gss_lucid_ctx
.key_data
.proto
, ctx
->gss_cryptor
.etype
)) {
2314 return (GSS_S_FAILURE
);
2317 switch(ctx
->gss_lucid_ctx
.key_data
.proto
) {
2319 /* RFC 1964 DES3 case */
2320 return (gss_krb5_3des_verify_mic_mbuf(minor
, ctx
, mbp
, offset
, len
, mic
, qop
));
2322 /* RFC 4121 CFX case */
2323 return (gss_krb5_cfx_verify_mic_mbuf(minor
, ctx
, mbp
, offset
, len
, mic
, qop
));
2326 return (GSS_S_COMPLETE
);
2330 gss_krb5_wrap_mbuf(uint32_t *minor
, /* minor_status */
2331 gss_ctx_id_t ctx
, /* context_handle */
2332 int conf_flag
, /* conf_req_flag */
2333 gss_qop_t qop
, /* qop_req */
2334 mbuf_t
*mbp
, /* input/output message_buffer */
2335 size_t offset
, /* offset */
2336 size_t len
, /* length */
2337 int *conf_state
/* conf state */)
2339 uint32_t major
, minor_stat
= 0;
2344 minor
= &minor_stat
;
2345 if (conf_state
== NULL
)
2346 conf_state
= &conf_val
;
2350 /* Validate context */
2351 if (ctx
== NULL
|| ((lucid_context_version_t
)ctx
)->version
!= 1)
2352 return (GSS_S_NO_CONTEXT
);
2354 if (!supported_etype(ctx
->gss_lucid_ctx
.key_data
.proto
, ctx
->gss_cryptor
.etype
)) {
2356 return (GSS_S_FAILURE
);
2359 gss_normalize_mbuf(*mbp
, offset
, &len
, &smb
, &tmb
, 0);
2361 switch(ctx
->gss_lucid_ctx
.key_data
.proto
) {
2363 /* RFC 1964 DES3 case */
2364 major
= gss_krb5_3des_wrap_mbuf(minor
, ctx
, conf_flag
, qop
, &smb
, len
, conf_state
);
2367 /* RFC 4121 CFX case */
2368 major
= gss_krb5_cfx_wrap_mbuf(minor
, ctx
, conf_flag
, qop
, &smb
, len
, conf_state
);
2373 gss_join_mbuf(*mbp
, smb
, tmb
);
2376 gss_join_mbuf(smb
, tmb
, NULL
);
2383 gss_krb5_unwrap_mbuf(uint32_t * minor
, /* minor_status */
2384 gss_ctx_id_t ctx
, /* context_handle */
2385 mbuf_t
*mbp
, /* input/output message_buffer */
2386 size_t offset
, /* offset */
2387 size_t len
, /* length */
2388 int *conf_flag
, /* conf_state */
2389 gss_qop_t
*qop
/* qop state */)
2391 uint32_t major
, minor_stat
= 0;
2392 gss_qop_t qop_val
= GSS_C_QOP_DEFAULT
;
2397 minor
= &minor_stat
;
2400 if (conf_flag
== NULL
)
2401 conf_flag
= &conf_val
;
2403 /* Validate context */
2404 if (ctx
== NULL
|| ((lucid_context_version_t
)ctx
)->version
!= 1)
2405 return (GSS_S_NO_CONTEXT
);
2407 if (!supported_etype(ctx
->gss_lucid_ctx
.key_data
.proto
, ctx
->gss_cryptor
.etype
)) {
2409 return (GSS_S_FAILURE
);
2412 gss_normalize_mbuf(*mbp
, offset
, &len
, &smb
, &tmb
, 0);
2414 switch(ctx
->gss_lucid_ctx
.key_data
.proto
) {
2416 /* RFC 1964 DES3 case */
2417 major
= gss_krb5_3des_unwrap_mbuf(minor
, ctx
, &smb
, len
, conf_flag
, qop
);
2420 /* RFC 4121 CFX case */
2421 major
= gss_krb5_cfx_unwrap_mbuf(minor
, ctx
, &smb
, len
, conf_flag
, qop
);
2426 gss_join_mbuf(*mbp
, smb
, tmb
);
2429 gss_join_mbuf(smb
, tmb
, NULL
);
2435 #include <nfs/xdr_subs.h>
2438 xdr_lucid_context(void *data
, size_t length
, lucid_context_t lctx
)
2442 uint32_t keylen
= 0;
2444 xb_init_buffer(&xb
, data
, length
);
2445 xb_get_32(error
, &xb
, lctx
->vers
);
2446 if (!error
&& lctx
->vers
!= 1) {
2448 printf("%s: invalid version %d\n", __func__
, (int)lctx
->vers
);
2451 xb_get_32(error
, &xb
, lctx
->initiate
);
2453 printf("%s: Could not decode initiate\n", __func__
);
2456 xb_get_32(error
, &xb
, lctx
->endtime
);
2458 printf("%s: Could not decode endtime\n", __func__
);
2461 xb_get_64(error
, &xb
, lctx
->send_seq
);
2463 printf("%s: Could not decode send_seq\n", __func__
);
2466 xb_get_64(error
, &xb
, lctx
->recv_seq
);
2468 printf("%s: Could not decode recv_seq\n", __func__
);
2471 xb_get_32(error
, &xb
, lctx
->key_data
.proto
);
2473 printf("%s: Could not decode mech protocol\n", __func__
);
2476 switch(lctx
->key_data
.proto
) {
2478 xb_get_32(error
, &xb
, lctx
->key_data
.lucid_protocol_u
.data_1964
.sign_alg
);
2479 xb_get_32(error
, &xb
, lctx
->key_data
.lucid_protocol_u
.data_1964
.seal_alg
);
2481 printf("%s: Could not decode rfc1964 sign and seal\n", __func__
);
2484 xb_get_32(error
, &xb
, lctx
->key_data
.lucid_protocol_u
.data_4121
.acceptor_subkey
);
2486 printf("%s: Could not decode rfc4121 acceptor_subkey", __func__
);
2489 printf("%s: Invalid mech protocol %d\n", __func__
, (int)lctx
->key_data
.proto
);
2494 xb_get_32(error
, &xb
, lctx
->ctx_key
.etype
);
2496 printf("%s: Could not decode key enctype\n", __func__
);
2499 switch(lctx
->ctx_key
.etype
) {
2500 case DES3_CBC_SHA1_KD
:
2503 case AES128_CTS_HMAC_SHA1_96
:
2506 case AES256_CTS_HMAC_SHA1_96
:
2513 xb_get_32(error
, &xb
, lctx
->ctx_key
.key
.key_len
);
2515 printf("%s: could not decode key length\n", __func__
);
2518 if (lctx
->ctx_key
.key
.key_len
!= keylen
) {
2520 printf("%s: etype = %d keylen = %d expected keylen = %d\n", __func__
,
2521 lctx
->ctx_key
.etype
, lctx
->ctx_key
.key
.key_len
, keylen
);
2525 lctx
->ctx_key
.key
.key_val
= xb_malloc(keylen
);
2526 if (lctx
->ctx_key
.key
.key_val
== NULL
) {
2527 printf("%s: could not get memory for key\n", __func__
);
2531 error
= xb_get_bytes(&xb
, (char *)lctx
->ctx_key
.key
.key_val
, keylen
, 1);
2533 printf("%s: could get key value\n", __func__
);
2534 xb_free(lctx
->ctx_key
.key
.key_val
);
2541 gss_krb5_make_context(void *data
, uint32_t datalen
)
2545 if (!corecrypto_available())
2548 gss_krb5_mech_init();
2549 MALLOC(ctx
, gss_ctx_id_t
, sizeof (struct gss_ctx_id_desc
), M_TEMP
, M_WAITOK
| M_ZERO
);
2550 if (xdr_lucid_context(data
, datalen
, &ctx
->gss_lucid_ctx
) ||
2551 !supported_etype(ctx
->gss_lucid_ctx
.key_data
.proto
, ctx
->gss_lucid_ctx
.ctx_key
.etype
)) {
2557 /* Set up crypto context */
2558 gss_crypto_ctx_init(&ctx
->gss_cryptor
, &ctx
->gss_lucid_ctx
);
2565 gss_krb5_destroy_context(gss_ctx_id_t ctx
)
2569 gss_crypto_ctx_free(&ctx
->gss_cryptor
);
2570 FREE(ctx
->gss_lucid_ctx
.ctx_key
.key
.key_val
, M_TEMP
);
2571 cc_clear(sizeof (lucid_context_t
), &ctx
->gss_lucid_ctx
);