2 * Copyright (c) 2006 - 2007 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
;
93 memset(&local_key
, 0, sizeof(local_key
));
95 ret
= krb5_copy_keyblock(context
, key
, &local_key
.key
);
99 ret
= krb5_data_alloc (&result
->checksum
, 16);
101 krb5_free_keyblock(context
, local_key
.key
);
105 result
->cksumtype
= CKSUMTYPE_HMAC_MD5
;
106 ret
= _krb5_HMAC_MD5_checksum(context
, &local_key
, data
, len
, usage
, result
);
108 krb5_data_free(&result
->checksum
);
110 krb5_free_keyblock(context
, local_key
.key
);
119 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
120 krb5_pac_parse(krb5_context context
, const void *ptr
, size_t len
,
125 krb5_storage
*sp
= NULL
;
126 uint32_t i
, tmp
, tmp2
, header_end
;
128 p
= calloc(1, sizeof(*p
));
130 ret
= krb5_enomem(context
);
134 sp
= krb5_storage_from_readonly_mem(ptr
, len
);
136 ret
= krb5_enomem(context
);
139 krb5_storage_set_flags(sp
, KRB5_STORAGE_BYTEORDER_LE
);
141 CHECK(ret
, krb5_ret_uint32(sp
, &tmp
), out
);
142 CHECK(ret
, krb5_ret_uint32(sp
, &tmp2
), out
);
144 ret
= EINVAL
; /* Too few buffers */
145 krb5_set_error_message(context
, ret
, N_("PAC have too few buffer", ""));
149 ret
= EINVAL
; /* Wrong version */
150 krb5_set_error_message(context
, ret
,
151 N_("PAC have wrong version %d", ""),
157 sizeof(*p
->pac
) + (sizeof(p
->pac
->buffers
[0]) * (tmp
- 1)));
158 if (p
->pac
== NULL
) {
159 ret
= krb5_enomem(context
);
163 p
->pac
->numbuffers
= tmp
;
164 p
->pac
->version
= tmp2
;
166 header_end
= PACTYPE_SIZE
+ (PAC_INFO_BUFFER_SIZE
* p
->pac
->numbuffers
);
167 if (header_end
> len
) {
172 for (i
= 0; i
< p
->pac
->numbuffers
; i
++) {
173 CHECK(ret
, krb5_ret_uint32(sp
, &p
->pac
->buffers
[i
].type
), out
);
174 CHECK(ret
, krb5_ret_uint32(sp
, &p
->pac
->buffers
[i
].buffersize
), out
);
175 CHECK(ret
, krb5_ret_uint32(sp
, &p
->pac
->buffers
[i
].offset_lo
), out
);
176 CHECK(ret
, krb5_ret_uint32(sp
, &p
->pac
->buffers
[i
].offset_hi
), out
);
178 /* consistency checks */
179 if (p
->pac
->buffers
[i
].offset_lo
& (PAC_ALIGNMENT
- 1)) {
181 krb5_set_error_message(context
, ret
,
182 N_("PAC out of allignment", ""));
185 if (p
->pac
->buffers
[i
].offset_hi
) {
187 krb5_set_error_message(context
, ret
,
188 N_("PAC high offset set", ""));
191 if (p
->pac
->buffers
[i
].offset_lo
> len
) {
193 krb5_set_error_message(context
, ret
,
194 N_("PAC offset off end", ""));
197 if (p
->pac
->buffers
[i
].offset_lo
< header_end
) {
199 krb5_set_error_message(context
, ret
,
200 N_("PAC offset inside header: %lu %lu", ""),
201 (unsigned long)p
->pac
->buffers
[i
].offset_lo
,
202 (unsigned long)header_end
);
205 if (p
->pac
->buffers
[i
].buffersize
> len
- p
->pac
->buffers
[i
].offset_lo
){
207 krb5_set_error_message(context
, ret
, N_("PAC length off end", ""));
211 /* let save pointer to data we need later */
212 if (p
->pac
->buffers
[i
].type
== PAC_SERVER_CHECKSUM
) {
213 if (p
->server_checksum
) {
215 krb5_set_error_message(context
, ret
,
216 N_("PAC have two server checksums", ""));
219 p
->server_checksum
= &p
->pac
->buffers
[i
];
220 } else if (p
->pac
->buffers
[i
].type
== PAC_PRIVSVR_CHECKSUM
) {
221 if (p
->privsvr_checksum
) {
223 krb5_set_error_message(context
, ret
,
224 N_("PAC have two KDC checksums", ""));
227 p
->privsvr_checksum
= &p
->pac
->buffers
[i
];
228 } else if (p
->pac
->buffers
[i
].type
== PAC_LOGON_NAME
) {
231 krb5_set_error_message(context
, ret
,
232 N_("PAC have two logon names", ""));
235 p
->logon_name
= &p
->pac
->buffers
[i
];
239 ret
= krb5_data_copy(&p
->data
, ptr
, len
);
243 krb5_storage_free(sp
);
250 krb5_storage_free(sp
);
261 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
262 krb5_pac_init(krb5_context context
, krb5_pac
*pac
)
267 p
= calloc(1, sizeof(*p
));
269 return krb5_enomem(context
);
272 p
->pac
= calloc(1, sizeof(*p
->pac
));
273 if (p
->pac
== NULL
) {
275 return krb5_enomem(context
);
278 ret
= krb5_data_alloc(&p
->data
, PACTYPE_SIZE
);
282 return krb5_enomem(context
);
289 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
290 krb5_pac_add_buffer(krb5_context context
, krb5_pac p
,
291 uint32_t type
, const krb5_data
*data
)
295 size_t len
, offset
, header_end
, old_end
;
298 len
= p
->pac
->numbuffers
;
300 ptr
= realloc(p
->pac
,
301 sizeof(*p
->pac
) + (sizeof(p
->pac
->buffers
[0]) * len
));
303 return krb5_enomem(context
);
307 for (i
= 0; i
< len
; i
++)
308 p
->pac
->buffers
[i
].offset_lo
+= PAC_INFO_BUFFER_SIZE
;
310 offset
= p
->data
.length
+ PAC_INFO_BUFFER_SIZE
;
312 p
->pac
->buffers
[len
].type
= type
;
313 p
->pac
->buffers
[len
].buffersize
= data
->length
;
314 p
->pac
->buffers
[len
].offset_lo
= offset
;
315 p
->pac
->buffers
[len
].offset_hi
= 0;
317 old_end
= p
->data
.length
;
318 len
= p
->data
.length
+ data
->length
+ PAC_INFO_BUFFER_SIZE
;
319 if (len
< p
->data
.length
) {
320 krb5_set_error_message(context
, EINVAL
, "integer overrun");
324 /* align to PAC_ALIGNMENT */
325 len
= ((len
+ PAC_ALIGNMENT
- 1) / PAC_ALIGNMENT
) * PAC_ALIGNMENT
;
327 ret
= krb5_data_realloc(&p
->data
, len
);
329 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
334 * make place for new PAC INFO BUFFER header
336 header_end
= PACTYPE_SIZE
+ (PAC_INFO_BUFFER_SIZE
* p
->pac
->numbuffers
);
337 memmove((unsigned char *)p
->data
.data
+ header_end
+ PAC_INFO_BUFFER_SIZE
,
338 (unsigned char *)p
->data
.data
+ header_end
,
339 old_end
- header_end
);
340 memset((unsigned char *)p
->data
.data
+ header_end
, 0, PAC_INFO_BUFFER_SIZE
);
343 * copy in new data part
346 memcpy((unsigned char *)p
->data
.data
+ offset
,
347 data
->data
, data
->length
);
348 memset((unsigned char *)p
->data
.data
+ offset
+ data
->length
,
349 0, p
->data
.length
- offset
- data
->length
);
351 p
->pac
->numbuffers
+= 1;
357 * Get the PAC buffer of specific type from the pac.
359 * @param context Kerberos 5 context.
360 * @param p the pac structure returned by krb5_pac_parse().
361 * @param type type of buffer to get
362 * @param data return data, free with krb5_data_free().
364 * @return Returns 0 to indicate success. Otherwise an kerberos et
365 * error code is returned, see krb5_get_error_message().
370 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
371 krb5_pac_get_buffer(krb5_context context
, krb5_pac p
,
372 uint32_t type
, krb5_data
*data
)
377 for (i
= 0; i
< p
->pac
->numbuffers
; i
++) {
378 const size_t len
= p
->pac
->buffers
[i
].buffersize
;
379 const size_t offset
= p
->pac
->buffers
[i
].offset_lo
;
381 if (p
->pac
->buffers
[i
].type
!= type
)
384 ret
= krb5_data_copy(data
, (unsigned char *)p
->data
.data
+ offset
, len
);
386 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
391 krb5_set_error_message(context
, ENOENT
, "No PAC buffer of type %lu was found",
392 (unsigned long)type
);
400 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
401 krb5_pac_get_types(krb5_context context
,
408 *types
= calloc(p
->pac
->numbuffers
, sizeof(*types
));
409 if (*types
== NULL
) {
411 return krb5_enomem(context
);
413 for (i
= 0; i
< p
->pac
->numbuffers
; i
++)
414 (*types
)[i
] = p
->pac
->buffers
[i
].type
;
415 *len
= p
->pac
->numbuffers
;
424 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
425 krb5_pac_free(krb5_context context
, krb5_pac pac
)
427 krb5_data_free(&pac
->data
);
436 static krb5_error_code
437 verify_checksum(krb5_context context
,
438 const struct PAC_INFO_BUFFER
*sig
,
439 const krb5_data
*data
,
440 void *ptr
, size_t len
,
441 const krb5_keyblock
*key
)
443 krb5_storage
*sp
= NULL
;
448 memset(&cksum
, 0, sizeof(cksum
));
450 sp
= krb5_storage_from_mem((char *)data
->data
+ sig
->offset_lo
,
453 return krb5_enomem(context
);
455 krb5_storage_set_flags(sp
, KRB5_STORAGE_BYTEORDER_LE
);
457 CHECK(ret
, krb5_ret_uint32(sp
, &type
), out
);
458 cksum
.cksumtype
= type
;
459 cksum
.checksum
.length
=
460 sig
->buffersize
- krb5_storage_seek(sp
, 0, SEEK_CUR
);
461 cksum
.checksum
.data
= malloc(cksum
.checksum
.length
);
462 if (cksum
.checksum
.data
== NULL
) {
463 ret
= krb5_enomem(context
);
466 ret
= krb5_storage_read(sp
, cksum
.checksum
.data
, cksum
.checksum
.length
);
467 if (ret
!= (int)cksum
.checksum
.length
) {
469 krb5_set_error_message(context
, ret
, "PAC checksum missing checksum");
473 if (!krb5_checksum_is_keyed(context
, cksum
.cksumtype
)) {
475 krb5_set_error_message(context
, ret
, "Checksum type %d not keyed",
480 /* If the checksum is HMAC-MD5, the checksum type is not tied to
481 * the key type, instead the HMAC-MD5 checksum is applied blindly
482 * on whatever key is used for this connection, avoiding issues
483 * with unkeyed checksums on des-cbc-md5 and des-cbc-crc. See
484 * http://comments.gmane.org/gmane.comp.encryption.kerberos.devel/8743
485 * for the same issue in MIT, and
486 * http://blogs.msdn.com/b/openspecification/archive/2010/01/01/verifying-the-server-signature-in-kerberos-privilege-account-certificate.aspx
487 * for Microsoft's explaination */
489 if (cksum
.cksumtype
== CKSUMTYPE_HMAC_MD5
) {
490 Checksum local_checksum
;
492 memset(&local_checksum
, 0, sizeof(local_checksum
));
494 ret
= HMAC_MD5_any_checksum(context
, key
, ptr
, len
,
495 KRB5_KU_OTHER_CKSUM
, &local_checksum
);
497 if (ret
!= 0 || krb5_data_ct_cmp(&local_checksum
.checksum
, &cksum
.checksum
) != 0) {
498 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
499 krb5_set_error_message(context
, ret
,
500 N_("PAC integrity check failed for "
501 "hmac-md5 checksum", ""));
503 krb5_data_free(&local_checksum
.checksum
);
506 krb5_crypto crypto
= NULL
;
508 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
512 ret
= krb5_verify_checksum(context
, crypto
, KRB5_KU_OTHER_CKSUM
,
514 krb5_crypto_destroy(context
, crypto
);
516 free(cksum
.checksum
.data
);
517 krb5_storage_free(sp
);
522 if (cksum
.checksum
.data
)
523 free(cksum
.checksum
.data
);
525 krb5_storage_free(sp
);
529 static krb5_error_code
530 create_checksum(krb5_context context
,
531 const krb5_keyblock
*key
,
533 void *data
, size_t datalen
,
534 void *sig
, size_t siglen
)
536 krb5_crypto crypto
= NULL
;
540 /* If the checksum is HMAC-MD5, the checksum type is not tied to
541 * the key type, instead the HMAC-MD5 checksum is applied blindly
542 * on whatever key is used for this connection, avoiding issues
543 * with unkeyed checksums on des-cbc-md5 and des-cbc-crc. See
544 * http://comments.gmane.org/gmane.comp.encryption.kerberos.devel/8743
545 * for the same issue in MIT, and
546 * http://blogs.msdn.com/b/openspecification/archive/2010/01/01/verifying-the-server-signature-in-kerberos-privilege-account-certificate.aspx
547 * for Microsoft's explaination */
549 if (cksumtype
== (uint32_t)CKSUMTYPE_HMAC_MD5
) {
550 ret
= HMAC_MD5_any_checksum(context
, key
, data
, datalen
,
551 KRB5_KU_OTHER_CKSUM
, &cksum
);
553 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
557 ret
= krb5_create_checksum(context
, crypto
, KRB5_KU_OTHER_CKSUM
, 0,
558 data
, datalen
, &cksum
);
559 krb5_crypto_destroy(context
, crypto
);
563 if (cksum
.checksum
.length
!= siglen
) {
564 krb5_set_error_message(context
, EINVAL
, "pac checksum wrong length");
565 free_Checksum(&cksum
);
569 memcpy(sig
, cksum
.checksum
.data
, siglen
);
570 free_Checksum(&cksum
);
580 #define NTTIME_EPOCH 0x019DB1DED53E8000LL
583 unix2nttime(time_t unix_time
)
586 wt
= unix_time
* (uint64_t)10000000 + (uint64_t)NTTIME_EPOCH
;
590 static krb5_error_code
591 verify_logonname(krb5_context context
,
592 const struct PAC_INFO_BUFFER
*logon_name
,
593 const krb5_data
*data
,
595 krb5_const_principal principal
)
599 uint32_t time1
, time2
;
604 sp
= krb5_storage_from_readonly_mem((const char *)data
->data
+ logon_name
->offset_lo
,
605 logon_name
->buffersize
);
607 return krb5_enomem(context
);
609 krb5_storage_set_flags(sp
, KRB5_STORAGE_BYTEORDER_LE
);
611 CHECK(ret
, krb5_ret_uint32(sp
, &time1
), out
);
612 CHECK(ret
, krb5_ret_uint32(sp
, &time2
), out
);
616 t1
= unix2nttime(authtime
);
617 t2
= ((uint64_t)time2
<< 32) | time1
;
619 * When neither the ticket nor the PAC set an explicit authtime,
620 * both times are zero, but relative to different time scales.
621 * So we must compare "not set" values without converting to a
622 * common time reference.
624 if (t1
!= t2
&& (t2
!= 0 && authtime
!= 0)) {
625 krb5_storage_free(sp
);
626 krb5_set_error_message(context
, EINVAL
, "PAC timestamp mismatch");
630 CHECK(ret
, krb5_ret_uint16(sp
, &len
), out
);
632 krb5_storage_free(sp
);
633 krb5_set_error_message(context
, EINVAL
, "PAC logon name length missing");
639 krb5_storage_free(sp
);
640 return krb5_enomem(context
);
642 ret
= krb5_storage_read(sp
, s
, len
);
644 krb5_storage_free(sp
);
645 krb5_set_error_message(context
, EINVAL
, "Failed to read PAC logon name");
648 krb5_storage_free(sp
);
650 size_t ucs2len
= len
/ 2;
653 unsigned int flags
= WIND_RW_LE
;
655 ucs2
= malloc(sizeof(ucs2
[0]) * ucs2len
);
657 return krb5_enomem(context
);
659 ret
= wind_ucs2read(s
, len
, &flags
, ucs2
, &ucs2len
);
663 krb5_set_error_message(context
, ret
, "Failed to convert string to UCS-2");
666 ret
= wind_ucs2utf8_length(ucs2
, ucs2len
, &u8len
);
669 krb5_set_error_message(context
, ret
, "Failed to count length of UCS-2 string");
672 u8len
+= 1; /* Add space for NUL */
676 return krb5_enomem(context
);
678 ret
= wind_ucs2utf8(ucs2
, ucs2len
, s
, &u8len
);
682 krb5_set_error_message(context
, ret
, "Failed to convert to UTF-8");
686 ret
= krb5_parse_name_flags(context
, s
, KRB5_PRINCIPAL_PARSE_NO_REALM
, &p2
);
691 if (krb5_principal_compare_any_realm(context
, principal
, p2
) != TRUE
) {
693 krb5_set_error_message(context
, ret
, "PAC logon name mismatch");
695 krb5_free_principal(context
, p2
);
705 static krb5_error_code
706 build_logon_name(krb5_context context
,
708 krb5_const_principal principal
,
717 t
= unix2nttime(authtime
);
719 krb5_data_zero(logon
);
721 sp
= krb5_storage_emem();
723 return krb5_enomem(context
);
725 krb5_storage_set_flags(sp
, KRB5_STORAGE_BYTEORDER_LE
);
727 CHECK(ret
, krb5_store_uint32(sp
, t
& 0xffffffff), out
);
728 CHECK(ret
, krb5_store_uint32(sp
, t
>> 32), out
);
730 ret
= krb5_unparse_name_flags(context
, principal
,
731 KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &s
);
740 ret
= wind_utf8ucs2_length(s
, &ucs2_len
);
742 krb5_set_error_message(context
, ret
, "Principal %s is not valid UTF-8", s
);
747 ucs2
= malloc(sizeof(ucs2
[0]) * ucs2_len
);
750 return krb5_enomem(context
);
753 ret
= wind_utf8ucs2(s
, ucs2
, &ucs2_len
);
756 krb5_set_error_message(context
, ret
, "Principal %s is not valid UTF-8", s
);
762 s2_len
= (ucs2_len
+ 1) * 2;
766 return krb5_enomem(context
);
770 ret
= wind_ucs2write(ucs2
, ucs2_len
,
771 &flags
, s2
, &s2_len
);
775 krb5_set_error_message(context
, ret
, "Failed to write to UCS-2 buffer");
780 * we do not want zero termination
782 s2_len
= ucs2_len
* 2;
785 CHECK(ret
, krb5_store_uint16(sp
, s2_len
), out
);
787 ret
= krb5_storage_write(sp
, s2
, s2_len
);
789 if (ret
!= (int)s2_len
) {
790 ret
= krb5_enomem(context
);
793 ret
= krb5_storage_to_data(sp
, logon
);
796 krb5_storage_free(sp
);
800 krb5_storage_free(sp
);
808 * @param context Kerberos 5 context.
809 * @param pac the pac structure returned by krb5_pac_parse().
810 * @param authtime The time of the ticket the PAC belongs to.
811 * @param principal the principal to verify.
812 * @param server The service key, most always be given.
813 * @param privsvr The KDC key, may be given.
815 * @return Returns 0 to indicate success. Otherwise an kerberos et
816 * error code is returned, see krb5_get_error_message().
821 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
822 krb5_pac_verify(krb5_context context
,
825 krb5_const_principal principal
,
826 const krb5_keyblock
*server
,
827 const krb5_keyblock
*privsvr
)
831 if (pac
->server_checksum
== NULL
) {
832 krb5_set_error_message(context
, EINVAL
, "PAC missing server checksum");
835 if (pac
->privsvr_checksum
== NULL
) {
836 krb5_set_error_message(context
, EINVAL
, "PAC missing kdc checksum");
839 if (pac
->logon_name
== NULL
) {
840 krb5_set_error_message(context
, EINVAL
, "PAC missing logon name");
844 ret
= verify_logonname(context
,
853 * in the service case, clean out data option of the privsvr and
854 * server checksum before checking the checksum.
859 ret
= krb5_copy_data(context
, &pac
->data
, ©
);
863 if (pac
->server_checksum
->buffersize
< 4)
865 if (pac
->privsvr_checksum
->buffersize
< 4)
868 memset((char *)copy
->data
+ pac
->server_checksum
->offset_lo
+ 4,
870 pac
->server_checksum
->buffersize
- 4);
872 memset((char *)copy
->data
+ pac
->privsvr_checksum
->offset_lo
+ 4,
874 pac
->privsvr_checksum
->buffersize
- 4);
876 ret
= verify_checksum(context
,
877 pac
->server_checksum
,
882 krb5_free_data(context
, copy
);
887 /* The priv checksum covers the server checksum */
888 ret
= verify_checksum(context
,
889 pac
->privsvr_checksum
,
891 (char *)pac
->data
.data
892 + pac
->server_checksum
->offset_lo
+ 4,
893 pac
->server_checksum
->buffersize
- 4,
906 static krb5_error_code
907 fill_zeros(krb5_context context
, krb5_storage
*sp
, size_t len
)
914 if (l
> sizeof(zeros
))
916 sret
= krb5_storage_write(sp
, zeros
, l
);
918 return krb5_enomem(context
);
925 static krb5_error_code
926 pac_checksum(krb5_context context
,
927 const krb5_keyblock
*key
,
931 krb5_cksumtype cktype
;
933 krb5_crypto crypto
= NULL
;
935 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
939 ret
= krb5_crypto_get_checksum_type(context
, crypto
, &cktype
);
940 krb5_crypto_destroy(context
, crypto
);
944 if (krb5_checksum_is_keyed(context
, cktype
) == FALSE
) {
945 *cksumtype
= CKSUMTYPE_HMAC_MD5
;
949 ret
= krb5_checksumsize(context
, cktype
, cksumsize
);
953 *cksumtype
= (uint32_t)cktype
;
958 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
959 _krb5_pac_sign(krb5_context context
,
962 krb5_principal principal
,
963 const krb5_keyblock
*server_key
,
964 const krb5_keyblock
*priv_key
,
968 krb5_storage
*sp
= NULL
, *spdata
= NULL
;
970 size_t server_size
, priv_size
;
971 uint32_t server_offset
= 0, priv_offset
= 0;
972 uint32_t server_cksumtype
= 0, priv_cksumtype
= 0;
977 krb5_data_zero(&logon
);
979 if (p
->logon_name
== NULL
)
981 if (p
->server_checksum
== NULL
)
983 if (p
->privsvr_checksum
== NULL
)
989 ptr
= realloc(p
->pac
, sizeof(*p
->pac
) + (sizeof(p
->pac
->buffers
[0]) * (p
->pac
->numbuffers
+ num
- 1)));
991 return krb5_enomem(context
);
995 if (p
->logon_name
== NULL
) {
996 p
->logon_name
= &p
->pac
->buffers
[p
->pac
->numbuffers
++];
997 memset(p
->logon_name
, 0, sizeof(*p
->logon_name
));
998 p
->logon_name
->type
= PAC_LOGON_NAME
;
1000 if (p
->server_checksum
== NULL
) {
1001 p
->server_checksum
= &p
->pac
->buffers
[p
->pac
->numbuffers
++];
1002 memset(p
->server_checksum
, 0, sizeof(*p
->server_checksum
));
1003 p
->server_checksum
->type
= PAC_SERVER_CHECKSUM
;
1005 if (p
->privsvr_checksum
== NULL
) {
1006 p
->privsvr_checksum
= &p
->pac
->buffers
[p
->pac
->numbuffers
++];
1007 memset(p
->privsvr_checksum
, 0, sizeof(*p
->privsvr_checksum
));
1008 p
->privsvr_checksum
->type
= PAC_PRIVSVR_CHECKSUM
;
1012 /* Calculate LOGON NAME */
1013 ret
= build_logon_name(context
, authtime
, principal
, &logon
);
1017 /* Set lengths for checksum */
1018 ret
= pac_checksum(context
, server_key
, &server_cksumtype
, &server_size
);
1021 ret
= pac_checksum(context
, priv_key
, &priv_cksumtype
, &priv_size
);
1026 sp
= krb5_storage_emem();
1028 return krb5_enomem(context
);
1030 krb5_storage_set_flags(sp
, KRB5_STORAGE_BYTEORDER_LE
);
1032 spdata
= krb5_storage_emem();
1033 if (spdata
== NULL
) {
1034 krb5_storage_free(sp
);
1035 return krb5_enomem(context
);
1037 krb5_storage_set_flags(spdata
, KRB5_STORAGE_BYTEORDER_LE
);
1039 CHECK(ret
, krb5_store_uint32(sp
, p
->pac
->numbuffers
), out
);
1040 CHECK(ret
, krb5_store_uint32(sp
, p
->pac
->version
), out
);
1042 end
= PACTYPE_SIZE
+ (PAC_INFO_BUFFER_SIZE
* p
->pac
->numbuffers
);
1044 for (i
= 0; i
< p
->pac
->numbuffers
; i
++) {
1051 if (p
->pac
->buffers
[i
].type
== PAC_SERVER_CHECKSUM
) {
1052 len
= server_size
+ 4;
1053 server_offset
= end
+ 4;
1054 CHECK(ret
, krb5_store_uint32(spdata
, server_cksumtype
), out
);
1055 CHECK(ret
, fill_zeros(context
, spdata
, server_size
), out
);
1056 } else if (p
->pac
->buffers
[i
].type
== PAC_PRIVSVR_CHECKSUM
) {
1057 len
= priv_size
+ 4;
1058 priv_offset
= end
+ 4;
1059 CHECK(ret
, krb5_store_uint32(spdata
, priv_cksumtype
), out
);
1060 CHECK(ret
, fill_zeros(context
, spdata
, priv_size
), out
);
1061 } else if (p
->pac
->buffers
[i
].type
== PAC_LOGON_NAME
) {
1062 len
= krb5_storage_write(spdata
, logon
.data
, logon
.length
);
1063 if (logon
.length
!= len
) {
1068 len
= p
->pac
->buffers
[i
].buffersize
;
1069 ptr
= (char *)p
->data
.data
+ p
->pac
->buffers
[i
].offset_lo
;
1071 sret
= krb5_storage_write(spdata
, ptr
, len
);
1073 ret
= krb5_enomem(context
);
1076 /* XXX if not aligned, fill_zeros */
1080 CHECK(ret
, krb5_store_uint32(sp
, p
->pac
->buffers
[i
].type
), out
);
1081 CHECK(ret
, krb5_store_uint32(sp
, len
), out
);
1082 CHECK(ret
, krb5_store_uint32(sp
, end
), out
);
1083 CHECK(ret
, krb5_store_uint32(sp
, 0), out
);
1085 /* advance data endpointer and align */
1090 e
= ((end
+ PAC_ALIGNMENT
- 1) / PAC_ALIGNMENT
) * PAC_ALIGNMENT
;
1091 if ((int32_t)end
!= e
) {
1092 CHECK(ret
, fill_zeros(context
, spdata
, e
- end
), out
);
1099 /* assert (server_offset != 0 && priv_offset != 0); */
1102 ret
= krb5_storage_to_data(spdata
, &d
);
1104 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
1107 ret
= krb5_storage_write(sp
, d
.data
, d
.length
);
1108 if (ret
!= (int)d
.length
) {
1110 ret
= krb5_enomem(context
);
1115 ret
= krb5_storage_to_data(sp
, &d
);
1117 ret
= krb5_enomem(context
);
1122 ret
= create_checksum(context
, server_key
, server_cksumtype
,
1124 (char *)d
.data
+ server_offset
, server_size
);
1129 ret
= create_checksum(context
, priv_key
, priv_cksumtype
,
1130 (char *)d
.data
+ server_offset
, server_size
,
1131 (char *)d
.data
+ priv_offset
, priv_size
);
1140 krb5_data_free(&logon
);
1141 krb5_storage_free(sp
);
1142 krb5_storage_free(spdata
);
1146 krb5_data_free(&logon
);
1148 krb5_storage_free(sp
);
1150 krb5_storage_free(spdata
);