2 * Copyright (c) 2006 - 2017 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "krb5_locl.h"
37 struct PAC_INFO_BUFFER
{
47 struct PAC_INFO_BUFFER buffers
[1];
50 struct krb5_pac_data
{
53 struct PAC_INFO_BUFFER
*server_checksum
;
54 struct PAC_INFO_BUFFER
*privsvr_checksum
;
55 struct PAC_INFO_BUFFER
*logon_name
;
58 #define PAC_ALIGNMENT 8
60 #define PACTYPE_SIZE 8
61 #define PAC_INFO_BUFFER_SIZE 16
63 #define PAC_SERVER_CHECKSUM 6
64 #define PAC_PRIVSVR_CHECKSUM 7
65 #define PAC_LOGON_NAME 10
66 #define PAC_CONSTRAINED_DELEGATION 11
68 #define CHECK(r,f,l) \
70 if (((r) = f ) != 0) { \
71 krb5_clear_error_message(context); \
76 static const char zeros
[PAC_ALIGNMENT
] = { 0 };
79 * HMAC-MD5 checksum over any key (needed for the PAC routines)
82 static krb5_error_code
83 HMAC_MD5_any_checksum(krb5_context context
,
84 const krb5_keyblock
*key
,
90 struct _krb5_key_data local_key
;
91 struct krb5_crypto_iov iov
;
94 memset(&local_key
, 0, sizeof(local_key
));
96 ret
= krb5_copy_keyblock(context
, key
, &local_key
.key
);
100 ret
= krb5_data_alloc (&result
->checksum
, 16);
102 krb5_free_keyblock(context
, local_key
.key
);
106 result
->cksumtype
= CKSUMTYPE_HMAC_MD5
;
107 iov
.data
.data
= (void *)data
;
108 iov
.data
.length
= len
;
109 iov
.flags
= KRB5_CRYPTO_TYPE_DATA
;
111 ret
= _krb5_HMAC_MD5_checksum(context
, NULL
, &local_key
, usage
, &iov
, 1,
114 krb5_data_free(&result
->checksum
);
116 krb5_free_keyblock(context
, local_key
.key
);
125 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
126 krb5_pac_parse(krb5_context context
, const void *ptr
, size_t len
,
131 krb5_storage
*sp
= NULL
;
132 uint32_t i
, tmp
, tmp2
, header_end
;
134 p
= calloc(1, sizeof(*p
));
136 ret
= krb5_enomem(context
);
140 sp
= krb5_storage_from_readonly_mem(ptr
, len
);
142 ret
= krb5_enomem(context
);
145 krb5_storage_set_flags(sp
, KRB5_STORAGE_BYTEORDER_LE
);
147 CHECK(ret
, krb5_ret_uint32(sp
, &tmp
), out
);
148 CHECK(ret
, krb5_ret_uint32(sp
, &tmp2
), out
);
150 ret
= EINVAL
; /* Too few buffers */
151 krb5_set_error_message(context
, ret
, N_("PAC have too few buffer", ""));
155 ret
= EINVAL
; /* Wrong version */
156 krb5_set_error_message(context
, ret
,
157 N_("PAC have wrong version %d", ""),
163 sizeof(*p
->pac
) + (sizeof(p
->pac
->buffers
[0]) * (tmp
- 1)));
164 if (p
->pac
== NULL
) {
165 ret
= krb5_enomem(context
);
169 p
->pac
->numbuffers
= tmp
;
170 p
->pac
->version
= tmp2
;
172 header_end
= PACTYPE_SIZE
+ (PAC_INFO_BUFFER_SIZE
* p
->pac
->numbuffers
);
173 if (header_end
> len
) {
178 for (i
= 0; i
< p
->pac
->numbuffers
; i
++) {
179 CHECK(ret
, krb5_ret_uint32(sp
, &p
->pac
->buffers
[i
].type
), out
);
180 CHECK(ret
, krb5_ret_uint32(sp
, &p
->pac
->buffers
[i
].buffersize
), out
);
181 CHECK(ret
, krb5_ret_uint32(sp
, &p
->pac
->buffers
[i
].offset_lo
), out
);
182 CHECK(ret
, krb5_ret_uint32(sp
, &p
->pac
->buffers
[i
].offset_hi
), out
);
184 /* consistency checks */
185 if (p
->pac
->buffers
[i
].offset_lo
& (PAC_ALIGNMENT
- 1)) {
187 krb5_set_error_message(context
, ret
,
188 N_("PAC out of alignment", ""));
191 if (p
->pac
->buffers
[i
].offset_hi
) {
193 krb5_set_error_message(context
, ret
,
194 N_("PAC high offset set", ""));
197 if (p
->pac
->buffers
[i
].offset_lo
> len
) {
199 krb5_set_error_message(context
, ret
,
200 N_("PAC offset off end", ""));
203 if (p
->pac
->buffers
[i
].offset_lo
< header_end
) {
205 krb5_set_error_message(context
, ret
,
206 N_("PAC offset inside header: %lu %lu", ""),
207 (unsigned long)p
->pac
->buffers
[i
].offset_lo
,
208 (unsigned long)header_end
);
211 if (p
->pac
->buffers
[i
].buffersize
> len
- p
->pac
->buffers
[i
].offset_lo
){
213 krb5_set_error_message(context
, ret
, N_("PAC length off end", ""));
217 /* let save pointer to data we need later */
218 if (p
->pac
->buffers
[i
].type
== PAC_SERVER_CHECKSUM
) {
219 if (p
->server_checksum
) {
221 krb5_set_error_message(context
, ret
,
222 N_("PAC have two server checksums", ""));
225 p
->server_checksum
= &p
->pac
->buffers
[i
];
226 } else if (p
->pac
->buffers
[i
].type
== PAC_PRIVSVR_CHECKSUM
) {
227 if (p
->privsvr_checksum
) {
229 krb5_set_error_message(context
, ret
,
230 N_("PAC have two KDC checksums", ""));
233 p
->privsvr_checksum
= &p
->pac
->buffers
[i
];
234 } else if (p
->pac
->buffers
[i
].type
== PAC_LOGON_NAME
) {
237 krb5_set_error_message(context
, ret
,
238 N_("PAC have two logon names", ""));
241 p
->logon_name
= &p
->pac
->buffers
[i
];
245 ret
= krb5_data_copy(&p
->data
, ptr
, len
);
249 krb5_storage_free(sp
);
256 krb5_storage_free(sp
);
267 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
268 krb5_pac_init(krb5_context context
, krb5_pac
*pac
)
273 p
= calloc(1, sizeof(*p
));
275 return krb5_enomem(context
);
278 p
->pac
= calloc(1, sizeof(*p
->pac
));
279 if (p
->pac
== NULL
) {
281 return krb5_enomem(context
);
284 ret
= krb5_data_alloc(&p
->data
, PACTYPE_SIZE
);
288 return krb5_enomem(context
);
295 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
296 krb5_pac_add_buffer(krb5_context context
, krb5_pac p
,
297 uint32_t type
, const krb5_data
*data
)
301 size_t len
, offset
, header_end
, old_end
;
304 len
= p
->pac
->numbuffers
;
306 ptr
= realloc(p
->pac
,
307 sizeof(*p
->pac
) + (sizeof(p
->pac
->buffers
[0]) * len
));
309 return krb5_enomem(context
);
313 for (i
= 0; i
< len
; i
++)
314 p
->pac
->buffers
[i
].offset_lo
+= PAC_INFO_BUFFER_SIZE
;
316 offset
= p
->data
.length
+ PAC_INFO_BUFFER_SIZE
;
318 p
->pac
->buffers
[len
].type
= type
;
319 p
->pac
->buffers
[len
].buffersize
= data
->length
;
320 p
->pac
->buffers
[len
].offset_lo
= offset
;
321 p
->pac
->buffers
[len
].offset_hi
= 0;
323 old_end
= p
->data
.length
;
324 len
= p
->data
.length
+ data
->length
+ PAC_INFO_BUFFER_SIZE
;
325 if (len
< p
->data
.length
) {
326 krb5_set_error_message(context
, EINVAL
, "integer overrun");
330 /* align to PAC_ALIGNMENT */
331 len
= ((len
+ PAC_ALIGNMENT
- 1) / PAC_ALIGNMENT
) * PAC_ALIGNMENT
;
333 ret
= krb5_data_realloc(&p
->data
, len
);
335 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
340 * make place for new PAC INFO BUFFER header
342 header_end
= PACTYPE_SIZE
+ (PAC_INFO_BUFFER_SIZE
* p
->pac
->numbuffers
);
343 memmove((unsigned char *)p
->data
.data
+ header_end
+ PAC_INFO_BUFFER_SIZE
,
344 (unsigned char *)p
->data
.data
+ header_end
,
345 old_end
- header_end
);
346 memset((unsigned char *)p
->data
.data
+ header_end
, 0, PAC_INFO_BUFFER_SIZE
);
349 * copy in new data part
352 memcpy((unsigned char *)p
->data
.data
+ offset
,
353 data
->data
, data
->length
);
354 memset((unsigned char *)p
->data
.data
+ offset
+ data
->length
,
355 0, p
->data
.length
- offset
- data
->length
);
357 p
->pac
->numbuffers
+= 1;
363 * Get the PAC buffer of specific type from the pac.
365 * @param context Kerberos 5 context.
366 * @param p the pac structure returned by krb5_pac_parse().
367 * @param type type of buffer to get
368 * @param data return data, free with krb5_data_free().
370 * @return Returns 0 to indicate success. Otherwise an kerberos et
371 * error code is returned, see krb5_get_error_message().
376 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
377 krb5_pac_get_buffer(krb5_context context
, krb5_pac p
,
378 uint32_t type
, krb5_data
*data
)
383 for (i
= 0; i
< p
->pac
->numbuffers
; i
++) {
384 const size_t len
= p
->pac
->buffers
[i
].buffersize
;
385 const size_t offset
= p
->pac
->buffers
[i
].offset_lo
;
387 if (p
->pac
->buffers
[i
].type
!= type
)
390 ret
= krb5_data_copy(data
, (unsigned char *)p
->data
.data
+ offset
, len
);
392 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
397 krb5_set_error_message(context
, ENOENT
, "No PAC buffer of type %lu was found",
398 (unsigned long)type
);
406 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
407 krb5_pac_get_types(krb5_context context
,
414 *types
= calloc(p
->pac
->numbuffers
, sizeof(**types
));
415 if (*types
== NULL
) {
417 return krb5_enomem(context
);
419 for (i
= 0; i
< p
->pac
->numbuffers
; i
++)
420 (*types
)[i
] = p
->pac
->buffers
[i
].type
;
421 *len
= p
->pac
->numbuffers
;
430 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
431 krb5_pac_free(krb5_context context
, krb5_pac pac
)
433 krb5_data_free(&pac
->data
);
442 static krb5_error_code
443 verify_checksum(krb5_context context
,
444 const struct PAC_INFO_BUFFER
*sig
,
445 const krb5_data
*data
,
446 void *ptr
, size_t len
,
447 const krb5_keyblock
*key
)
449 krb5_storage
*sp
= NULL
;
454 memset(&cksum
, 0, sizeof(cksum
));
456 sp
= krb5_storage_from_mem((char *)data
->data
+ sig
->offset_lo
,
459 return krb5_enomem(context
);
461 krb5_storage_set_flags(sp
, KRB5_STORAGE_BYTEORDER_LE
);
463 CHECK(ret
, krb5_ret_uint32(sp
, &type
), out
);
464 cksum
.cksumtype
= type
;
465 cksum
.checksum
.length
=
466 sig
->buffersize
- krb5_storage_seek(sp
, 0, SEEK_CUR
);
467 cksum
.checksum
.data
= malloc(cksum
.checksum
.length
);
468 if (cksum
.checksum
.data
== NULL
) {
469 ret
= krb5_enomem(context
);
472 ret
= krb5_storage_read(sp
, cksum
.checksum
.data
, cksum
.checksum
.length
);
473 if (ret
!= (int)cksum
.checksum
.length
) {
475 krb5_set_error_message(context
, ret
, "PAC checksum missing checksum");
479 if (!krb5_checksum_is_keyed(context
, cksum
.cksumtype
)) {
481 krb5_set_error_message(context
, ret
, "Checksum type %d not keyed",
486 /* If the checksum is HMAC-MD5, the checksum type is not tied to
487 * the key type, instead the HMAC-MD5 checksum is applied blindly
488 * on whatever key is used for this connection, avoiding issues
489 * with unkeyed checksums on des-cbc-md5 and des-cbc-crc. See
490 * http://comments.gmane.org/gmane.comp.encryption.kerberos.devel/8743
491 * for the same issue in MIT, and
492 * http://blogs.msdn.com/b/openspecification/archive/2010/01/01/verifying-the-server-signature-in-kerberos-privilege-account-certificate.aspx
493 * for Microsoft's explaination */
495 if (cksum
.cksumtype
== CKSUMTYPE_HMAC_MD5
) {
496 Checksum local_checksum
;
498 memset(&local_checksum
, 0, sizeof(local_checksum
));
500 ret
= HMAC_MD5_any_checksum(context
, key
, ptr
, len
,
501 KRB5_KU_OTHER_CKSUM
, &local_checksum
);
503 if (ret
!= 0 || krb5_data_ct_cmp(&local_checksum
.checksum
, &cksum
.checksum
) != 0) {
504 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
505 krb5_set_error_message(context
, ret
,
506 N_("PAC integrity check failed for "
507 "hmac-md5 checksum", ""));
509 krb5_data_free(&local_checksum
.checksum
);
512 krb5_crypto crypto
= NULL
;
514 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
518 ret
= krb5_verify_checksum(context
, crypto
, KRB5_KU_OTHER_CKSUM
,
520 krb5_crypto_destroy(context
, crypto
);
522 free(cksum
.checksum
.data
);
523 krb5_storage_free(sp
);
528 if (cksum
.checksum
.data
)
529 free(cksum
.checksum
.data
);
531 krb5_storage_free(sp
);
535 static krb5_error_code
536 create_checksum(krb5_context context
,
537 const krb5_keyblock
*key
,
539 void *data
, size_t datalen
,
540 void *sig
, size_t siglen
)
542 krb5_crypto crypto
= NULL
;
546 /* If the checksum is HMAC-MD5, the checksum type is not tied to
547 * the key type, instead the HMAC-MD5 checksum is applied blindly
548 * on whatever key is used for this connection, avoiding issues
549 * with unkeyed checksums on des-cbc-md5 and des-cbc-crc. See
550 * http://comments.gmane.org/gmane.comp.encryption.kerberos.devel/8743
551 * for the same issue in MIT, and
552 * http://blogs.msdn.com/b/openspecification/archive/2010/01/01/verifying-the-server-signature-in-kerberos-privilege-account-certificate.aspx
553 * for Microsoft's explaination */
555 if (cksumtype
== (uint32_t)CKSUMTYPE_HMAC_MD5
) {
556 ret
= HMAC_MD5_any_checksum(context
, key
, data
, datalen
,
557 KRB5_KU_OTHER_CKSUM
, &cksum
);
561 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
565 ret
= krb5_create_checksum(context
, crypto
, KRB5_KU_OTHER_CKSUM
, 0,
566 data
, datalen
, &cksum
);
567 krb5_crypto_destroy(context
, crypto
);
571 if (cksum
.checksum
.length
!= siglen
) {
572 krb5_set_error_message(context
, EINVAL
, "pac checksum wrong length");
573 free_Checksum(&cksum
);
577 memcpy(sig
, cksum
.checksum
.data
, siglen
);
578 free_Checksum(&cksum
);
588 #define NTTIME_EPOCH 0x019DB1DED53E8000LL
591 unix2nttime(time_t unix_time
)
594 wt
= unix_time
* (uint64_t)10000000 + (uint64_t)NTTIME_EPOCH
;
598 static krb5_error_code
599 verify_logonname(krb5_context context
,
600 const struct PAC_INFO_BUFFER
*logon_name
,
601 const krb5_data
*data
,
603 krb5_const_principal principal
)
606 uint32_t time1
, time2
;
610 char *principal_string
= NULL
;
611 char *logon_string
= NULL
;
613 sp
= krb5_storage_from_readonly_mem((const char *)data
->data
+ logon_name
->offset_lo
,
614 logon_name
->buffersize
);
616 return krb5_enomem(context
);
618 krb5_storage_set_flags(sp
, KRB5_STORAGE_BYTEORDER_LE
);
620 CHECK(ret
, krb5_ret_uint32(sp
, &time1
), out
);
621 CHECK(ret
, krb5_ret_uint32(sp
, &time2
), out
);
625 t1
= unix2nttime(authtime
);
626 t2
= ((uint64_t)time2
<< 32) | time1
;
628 * When neither the ticket nor the PAC set an explicit authtime,
629 * both times are zero, but relative to different time scales.
630 * So we must compare "not set" values without converting to a
631 * common time reference.
633 if (t1
!= t2
&& (t2
!= 0 && authtime
!= 0)) {
634 krb5_storage_free(sp
);
635 krb5_set_error_message(context
, EINVAL
, "PAC timestamp mismatch");
639 CHECK(ret
, krb5_ret_uint16(sp
, &len
), out
);
641 krb5_storage_free(sp
);
642 krb5_set_error_message(context
, EINVAL
, "PAC logon name length missing");
648 krb5_storage_free(sp
);
649 return krb5_enomem(context
);
651 ret
= krb5_storage_read(sp
, s
, len
);
653 krb5_storage_free(sp
);
654 krb5_set_error_message(context
, EINVAL
, "Failed to read PAC logon name");
657 krb5_storage_free(sp
);
659 size_t ucs2len
= len
/ 2;
662 unsigned int flags
= WIND_RW_LE
;
664 ucs2
= malloc(sizeof(ucs2
[0]) * ucs2len
);
666 return krb5_enomem(context
);
668 ret
= wind_ucs2read(s
, len
, &flags
, ucs2
, &ucs2len
);
672 krb5_set_error_message(context
, ret
, "Failed to convert string to UCS-2");
675 ret
= wind_ucs2utf8_length(ucs2
, ucs2len
, &u8len
);
678 krb5_set_error_message(context
, ret
, "Failed to count length of UCS-2 string");
681 u8len
+= 1; /* Add space for NUL */
682 logon_string
= malloc(u8len
);
683 if (logon_string
== NULL
) {
685 return krb5_enomem(context
);
687 ret
= wind_ucs2utf8(ucs2
, ucs2len
, logon_string
, &u8len
);
691 krb5_set_error_message(context
, ret
, "Failed to convert to UTF-8");
695 ret
= krb5_unparse_name_flags(context
, principal
,
696 KRB5_PRINCIPAL_UNPARSE_NO_REALM
|
697 KRB5_PRINCIPAL_UNPARSE_DISPLAY
,
704 ret
= strcmp(logon_string
, principal_string
);
707 krb5_set_error_message(context
, ret
, "PAC logon name [%s] mismatch principal name [%s]",
708 logon_string
, principal_string
);
711 free(principal_string
);
721 static krb5_error_code
722 build_logon_name(krb5_context context
,
724 krb5_const_principal principal
,
733 t
= unix2nttime(authtime
);
735 krb5_data_zero(logon
);
737 sp
= krb5_storage_emem();
739 return krb5_enomem(context
);
741 krb5_storage_set_flags(sp
, KRB5_STORAGE_BYTEORDER_LE
);
743 CHECK(ret
, krb5_store_uint32(sp
, t
& 0xffffffff), out
);
744 CHECK(ret
, krb5_store_uint32(sp
, t
>> 32), out
);
746 ret
= krb5_unparse_name_flags(context
, principal
,
747 KRB5_PRINCIPAL_UNPARSE_NO_REALM
|
748 KRB5_PRINCIPAL_UNPARSE_DISPLAY
,
758 ret
= wind_utf8ucs2_length(s
, &ucs2_len
);
760 krb5_set_error_message(context
, ret
, "Principal %s is not valid UTF-8", s
);
765 ucs2
= malloc(sizeof(ucs2
[0]) * ucs2_len
);
768 return krb5_enomem(context
);
771 ret
= wind_utf8ucs2(s
, ucs2
, &ucs2_len
);
774 krb5_set_error_message(context
, ret
, "Principal %s is not valid UTF-8", s
);
780 s2_len
= (ucs2_len
+ 1) * 2;
784 return krb5_enomem(context
);
788 ret
= wind_ucs2write(ucs2
, ucs2_len
,
789 &flags
, s2
, &s2_len
);
793 krb5_set_error_message(context
, ret
, "Failed to write to UCS-2 buffer");
798 * we do not want zero termination
800 s2_len
= ucs2_len
* 2;
803 CHECK(ret
, krb5_store_uint16(sp
, s2_len
), out
);
805 ret
= krb5_storage_write(sp
, s2
, s2_len
);
807 if (ret
!= (int)s2_len
) {
808 ret
= krb5_enomem(context
);
811 ret
= krb5_storage_to_data(sp
, logon
);
814 krb5_storage_free(sp
);
818 krb5_storage_free(sp
);
826 * @param context Kerberos 5 context.
827 * @param pac the pac structure returned by krb5_pac_parse().
828 * @param authtime The time of the ticket the PAC belongs to.
829 * @param principal the principal to verify.
830 * @param server The service key, most always be given.
831 * @param privsvr The KDC key, may be given.
833 * @return Returns 0 to indicate success. Otherwise an kerberos et
834 * error code is returned, see krb5_get_error_message().
839 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
840 krb5_pac_verify(krb5_context context
,
843 krb5_const_principal principal
,
844 const krb5_keyblock
*server
,
845 const krb5_keyblock
*privsvr
)
849 if (pac
->server_checksum
== NULL
) {
850 krb5_set_error_message(context
, EINVAL
, "PAC missing server checksum");
853 if (pac
->privsvr_checksum
== NULL
) {
854 krb5_set_error_message(context
, EINVAL
, "PAC missing kdc checksum");
857 if (pac
->logon_name
== NULL
) {
858 krb5_set_error_message(context
, EINVAL
, "PAC missing logon name");
862 ret
= verify_logonname(context
,
871 * in the service case, clean out data option of the privsvr and
872 * server checksum before checking the checksum.
877 if (pac
->server_checksum
->buffersize
< 4 ||
878 pac
->privsvr_checksum
->buffersize
< 4)
881 ret
= krb5_copy_data(context
, &pac
->data
, ©
);
885 memset((char *)copy
->data
+ pac
->server_checksum
->offset_lo
+ 4,
887 pac
->server_checksum
->buffersize
- 4);
889 memset((char *)copy
->data
+ pac
->privsvr_checksum
->offset_lo
+ 4,
891 pac
->privsvr_checksum
->buffersize
- 4);
893 ret
= verify_checksum(context
,
894 pac
->server_checksum
,
899 krb5_free_data(context
, copy
);
904 /* The priv checksum covers the server checksum */
905 ret
= verify_checksum(context
,
906 pac
->privsvr_checksum
,
908 (char *)pac
->data
.data
909 + pac
->server_checksum
->offset_lo
+ 4,
910 pac
->server_checksum
->buffersize
- 4,
923 static krb5_error_code
924 fill_zeros(krb5_context context
, krb5_storage
*sp
, size_t len
)
931 if (l
> sizeof(zeros
))
933 sret
= krb5_storage_write(sp
, zeros
, l
);
935 return krb5_enomem(context
);
942 static krb5_error_code
943 pac_checksum(krb5_context context
,
944 const krb5_keyblock
*key
,
948 krb5_cksumtype cktype
;
950 krb5_crypto crypto
= NULL
;
952 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
956 ret
= krb5_crypto_get_checksum_type(context
, crypto
, &cktype
);
957 krb5_crypto_destroy(context
, crypto
);
961 if (krb5_checksum_is_keyed(context
, cktype
) == FALSE
) {
962 *cksumtype
= CKSUMTYPE_HMAC_MD5
;
966 ret
= krb5_checksumsize(context
, cktype
, cksumsize
);
970 *cksumtype
= (uint32_t)cktype
;
975 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
976 _krb5_pac_sign(krb5_context context
,
979 krb5_principal principal
,
980 const krb5_keyblock
*server_key
,
981 const krb5_keyblock
*priv_key
,
985 krb5_storage
*sp
= NULL
, *spdata
= NULL
;
987 size_t server_size
, priv_size
;
988 uint32_t server_offset
= 0, priv_offset
= 0;
989 uint32_t server_cksumtype
= 0, priv_cksumtype
= 0;
994 krb5_data_zero(&logon
);
996 for (i
= 0; i
< p
->pac
->numbuffers
; i
++) {
997 if (p
->pac
->buffers
[i
].type
== PAC_SERVER_CHECKSUM
) {
998 if (p
->server_checksum
== NULL
) {
999 p
->server_checksum
= &p
->pac
->buffers
[i
];
1001 if (p
->server_checksum
!= &p
->pac
->buffers
[i
]) {
1003 krb5_set_error_message(context
, ret
,
1004 N_("PAC have two server checksums", ""));
1007 } else if (p
->pac
->buffers
[i
].type
== PAC_PRIVSVR_CHECKSUM
) {
1008 if (p
->privsvr_checksum
== NULL
) {
1009 p
->privsvr_checksum
= &p
->pac
->buffers
[i
];
1011 if (p
->privsvr_checksum
!= &p
->pac
->buffers
[i
]) {
1013 krb5_set_error_message(context
, ret
,
1014 N_("PAC have two KDC checksums", ""));
1017 } else if (p
->pac
->buffers
[i
].type
== PAC_LOGON_NAME
) {
1018 if (p
->logon_name
== NULL
) {
1019 p
->logon_name
= &p
->pac
->buffers
[i
];
1021 if (p
->logon_name
!= &p
->pac
->buffers
[i
]) {
1023 krb5_set_error_message(context
, ret
,
1024 N_("PAC have two logon names", ""));
1030 if (p
->logon_name
== NULL
)
1032 if (p
->server_checksum
== NULL
)
1034 if (p
->privsvr_checksum
== NULL
)
1040 ptr
= realloc(p
->pac
, sizeof(*p
->pac
) + (sizeof(p
->pac
->buffers
[0]) * (p
->pac
->numbuffers
+ num
- 1)));
1042 return krb5_enomem(context
);
1046 if (p
->logon_name
== NULL
) {
1047 p
->logon_name
= &p
->pac
->buffers
[p
->pac
->numbuffers
++];
1048 memset(p
->logon_name
, 0, sizeof(*p
->logon_name
));
1049 p
->logon_name
->type
= PAC_LOGON_NAME
;
1051 if (p
->server_checksum
== NULL
) {
1052 p
->server_checksum
= &p
->pac
->buffers
[p
->pac
->numbuffers
++];
1053 memset(p
->server_checksum
, 0, sizeof(*p
->server_checksum
));
1054 p
->server_checksum
->type
= PAC_SERVER_CHECKSUM
;
1056 if (p
->privsvr_checksum
== NULL
) {
1057 p
->privsvr_checksum
= &p
->pac
->buffers
[p
->pac
->numbuffers
++];
1058 memset(p
->privsvr_checksum
, 0, sizeof(*p
->privsvr_checksum
));
1059 p
->privsvr_checksum
->type
= PAC_PRIVSVR_CHECKSUM
;
1063 /* Calculate LOGON NAME */
1064 ret
= build_logon_name(context
, authtime
, principal
, &logon
);
1068 /* Set lengths for checksum */
1069 ret
= pac_checksum(context
, server_key
, &server_cksumtype
, &server_size
);
1072 ret
= pac_checksum(context
, priv_key
, &priv_cksumtype
, &priv_size
);
1077 sp
= krb5_storage_emem();
1079 return krb5_enomem(context
);
1081 krb5_storage_set_flags(sp
, KRB5_STORAGE_BYTEORDER_LE
);
1083 spdata
= krb5_storage_emem();
1084 if (spdata
== NULL
) {
1085 krb5_storage_free(sp
);
1086 return krb5_enomem(context
);
1088 krb5_storage_set_flags(spdata
, KRB5_STORAGE_BYTEORDER_LE
);
1090 CHECK(ret
, krb5_store_uint32(sp
, p
->pac
->numbuffers
), out
);
1091 CHECK(ret
, krb5_store_uint32(sp
, p
->pac
->version
), out
);
1093 end
= PACTYPE_SIZE
+ (PAC_INFO_BUFFER_SIZE
* p
->pac
->numbuffers
);
1095 for (i
= 0; i
< p
->pac
->numbuffers
; i
++) {
1102 if (p
->pac
->buffers
[i
].type
== PAC_SERVER_CHECKSUM
) {
1103 len
= server_size
+ 4;
1104 server_offset
= end
+ 4;
1105 CHECK(ret
, krb5_store_uint32(spdata
, server_cksumtype
), out
);
1106 CHECK(ret
, fill_zeros(context
, spdata
, server_size
), out
);
1107 } else if (p
->pac
->buffers
[i
].type
== PAC_PRIVSVR_CHECKSUM
) {
1108 len
= priv_size
+ 4;
1109 priv_offset
= end
+ 4;
1110 CHECK(ret
, krb5_store_uint32(spdata
, priv_cksumtype
), out
);
1111 CHECK(ret
, fill_zeros(context
, spdata
, priv_size
), out
);
1112 } else if (p
->pac
->buffers
[i
].type
== PAC_LOGON_NAME
) {
1113 len
= krb5_storage_write(spdata
, logon
.data
, logon
.length
);
1114 if (logon
.length
!= len
) {
1119 len
= p
->pac
->buffers
[i
].buffersize
;
1120 ptr
= (char *)p
->data
.data
+ p
->pac
->buffers
[i
].offset_lo
;
1122 sret
= krb5_storage_write(spdata
, ptr
, len
);
1124 ret
= krb5_enomem(context
);
1127 /* XXX if not aligned, fill_zeros */
1131 CHECK(ret
, krb5_store_uint32(sp
, p
->pac
->buffers
[i
].type
), out
);
1132 CHECK(ret
, krb5_store_uint32(sp
, len
), out
);
1133 CHECK(ret
, krb5_store_uint32(sp
, end
), out
);
1134 CHECK(ret
, krb5_store_uint32(sp
, 0), out
);
1136 /* advance data endpointer and align */
1141 e
= ((end
+ PAC_ALIGNMENT
- 1) / PAC_ALIGNMENT
) * PAC_ALIGNMENT
;
1142 if ((int32_t)end
!= e
) {
1143 CHECK(ret
, fill_zeros(context
, spdata
, e
- end
), out
);
1150 /* assert (server_offset != 0 && priv_offset != 0); */
1153 ret
= krb5_storage_to_data(spdata
, &d
);
1155 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
1158 ret
= krb5_storage_write(sp
, d
.data
, d
.length
);
1159 if (ret
!= (int)d
.length
) {
1161 ret
= krb5_enomem(context
);
1166 ret
= krb5_storage_to_data(sp
, &d
);
1168 ret
= krb5_enomem(context
);
1173 ret
= create_checksum(context
, server_key
, server_cksumtype
,
1175 (char *)d
.data
+ server_offset
, server_size
);
1180 ret
= create_checksum(context
, priv_key
, priv_cksumtype
,
1181 (char *)d
.data
+ server_offset
, server_size
,
1182 (char *)d
.data
+ priv_offset
, priv_size
);
1191 krb5_data_free(&logon
);
1192 krb5_storage_free(sp
);
1193 krb5_storage_free(spdata
);
1197 krb5_data_free(&logon
);
1199 krb5_storage_free(sp
);
1201 krb5_storage_free(spdata
);