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 };
83 krb5_pac_parse(krb5_context context
, const void *ptr
, size_t len
,
88 krb5_storage
*sp
= NULL
;
89 uint32_t i
, tmp
, tmp2
, header_end
;
91 p
= calloc(1, sizeof(*p
));
94 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
98 sp
= krb5_storage_from_readonly_mem(ptr
, len
);
101 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
104 krb5_storage_set_flags(sp
, KRB5_STORAGE_BYTEORDER_LE
);
106 CHECK(ret
, krb5_ret_uint32(sp
, &tmp
), out
);
107 CHECK(ret
, krb5_ret_uint32(sp
, &tmp2
), out
);
109 ret
= EINVAL
; /* Too few buffers */
110 krb5_set_error_message(context
, ret
, N_("PAC have too few buffer", ""));
114 ret
= EINVAL
; /* Wrong version */
115 krb5_set_error_message(context
, ret
,
116 N_("PAC have wrong version %d", ""),
122 sizeof(*p
->pac
) + (sizeof(p
->pac
->buffers
[0]) * (tmp
- 1)));
123 if (p
->pac
== NULL
) {
125 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
129 p
->pac
->numbuffers
= tmp
;
130 p
->pac
->version
= tmp2
;
132 header_end
= PACTYPE_SIZE
+ (PAC_INFO_BUFFER_SIZE
* p
->pac
->numbuffers
);
133 if (header_end
> len
) {
138 for (i
= 0; i
< p
->pac
->numbuffers
; i
++) {
139 CHECK(ret
, krb5_ret_uint32(sp
, &p
->pac
->buffers
[i
].type
), out
);
140 CHECK(ret
, krb5_ret_uint32(sp
, &p
->pac
->buffers
[i
].buffersize
), out
);
141 CHECK(ret
, krb5_ret_uint32(sp
, &p
->pac
->buffers
[i
].offset_lo
), out
);
142 CHECK(ret
, krb5_ret_uint32(sp
, &p
->pac
->buffers
[i
].offset_hi
), out
);
144 /* consistency checks */
145 if (p
->pac
->buffers
[i
].offset_lo
& (PAC_ALIGNMENT
- 1)) {
147 krb5_set_error_message(context
, ret
,
148 N_("PAC out of allignment", ""));
151 if (p
->pac
->buffers
[i
].offset_hi
) {
153 krb5_set_error_message(context
, ret
,
154 N_("PAC high offset set", ""));
157 if (p
->pac
->buffers
[i
].offset_lo
> len
) {
159 krb5_set_error_message(context
, ret
,
160 N_("PAC offset off end", ""));
163 if (p
->pac
->buffers
[i
].offset_lo
< header_end
) {
165 krb5_set_error_message(context
, ret
,
166 N_("PAC offset inside header: %lu %lu", ""),
167 (unsigned long)p
->pac
->buffers
[i
].offset_lo
,
168 (unsigned long)header_end
);
171 if (p
->pac
->buffers
[i
].buffersize
> len
- p
->pac
->buffers
[i
].offset_lo
){
173 krb5_set_error_message(context
, ret
, N_("PAC length off end", ""));
177 /* let save pointer to data we need later */
178 if (p
->pac
->buffers
[i
].type
== PAC_SERVER_CHECKSUM
) {
179 if (p
->server_checksum
) {
181 krb5_set_error_message(context
, ret
,
182 N_("PAC have two server checksums", ""));
185 p
->server_checksum
= &p
->pac
->buffers
[i
];
186 } else if (p
->pac
->buffers
[i
].type
== PAC_PRIVSVR_CHECKSUM
) {
187 if (p
->privsvr_checksum
) {
189 krb5_set_error_message(context
, ret
,
190 N_("PAC have two KDC checksums", ""));
193 p
->privsvr_checksum
= &p
->pac
->buffers
[i
];
194 } else if (p
->pac
->buffers
[i
].type
== PAC_LOGON_NAME
) {
197 krb5_set_error_message(context
, ret
,
198 N_("PAC have two logon names", ""));
201 p
->logon_name
= &p
->pac
->buffers
[i
];
205 ret
= krb5_data_copy(&p
->data
, ptr
, len
);
209 krb5_storage_free(sp
);
216 krb5_storage_free(sp
);
228 krb5_pac_init(krb5_context context
, krb5_pac
*pac
)
233 p
= calloc(1, sizeof(*p
));
235 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
239 p
->pac
= calloc(1, sizeof(*p
->pac
));
240 if (p
->pac
== NULL
) {
242 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
246 ret
= krb5_data_alloc(&p
->data
, PACTYPE_SIZE
);
250 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
260 krb5_pac_add_buffer(krb5_context context
, krb5_pac p
,
261 uint32_t type
, const krb5_data
*data
)
265 size_t len
, offset
, header_end
, old_end
;
268 len
= p
->pac
->numbuffers
;
270 ptr
= realloc(p
->pac
,
271 sizeof(*p
->pac
) + (sizeof(p
->pac
->buffers
[0]) * len
));
273 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
278 for (i
= 0; i
< len
; i
++)
279 p
->pac
->buffers
[i
].offset_lo
+= PAC_INFO_BUFFER_SIZE
;
281 offset
= p
->data
.length
+ PAC_INFO_BUFFER_SIZE
;
283 p
->pac
->buffers
[len
].type
= type
;
284 p
->pac
->buffers
[len
].buffersize
= data
->length
;
285 p
->pac
->buffers
[len
].offset_lo
= offset
;
286 p
->pac
->buffers
[len
].offset_hi
= 0;
288 old_end
= p
->data
.length
;
289 len
= p
->data
.length
+ data
->length
+ PAC_INFO_BUFFER_SIZE
;
290 if (len
< p
->data
.length
) {
291 krb5_set_error_message(context
, EINVAL
, "integer overrun");
295 /* align to PAC_ALIGNMENT */
296 len
= ((len
+ PAC_ALIGNMENT
- 1) / PAC_ALIGNMENT
) * PAC_ALIGNMENT
;
298 ret
= krb5_data_realloc(&p
->data
, len
);
300 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
305 * make place for new PAC INFO BUFFER header
307 header_end
= PACTYPE_SIZE
+ (PAC_INFO_BUFFER_SIZE
* p
->pac
->numbuffers
);
308 memmove((unsigned char *)p
->data
.data
+ header_end
+ PAC_INFO_BUFFER_SIZE
,
309 (unsigned char *)p
->data
.data
+ header_end
,
310 old_end
- header_end
);
311 memset((unsigned char *)p
->data
.data
+ header_end
, 0, PAC_INFO_BUFFER_SIZE
);
314 * copy in new data part
317 memcpy((unsigned char *)p
->data
.data
+ offset
,
318 data
->data
, data
->length
);
319 memset((unsigned char *)p
->data
.data
+ offset
+ data
->length
,
320 0, p
->data
.length
- offset
- data
->length
);
322 p
->pac
->numbuffers
+= 1;
328 * Get the PAC buffer of specific type from the pac.
330 * @param context Kerberos 5 context.
331 * @param p the pac structure returned by krb5_pac_parse().
332 * @param type type of buffer to get
333 * @param data return data, free with krb5_data_free().
335 * @return Returns 0 to indicate success. Otherwise an kerberos et
336 * error code is returned, see krb5_get_error_message().
342 krb5_pac_get_buffer(krb5_context context
, krb5_pac p
,
343 uint32_t type
, krb5_data
*data
)
348 for (i
= 0; i
< p
->pac
->numbuffers
; i
++) {
349 const size_t len
= p
->pac
->buffers
[i
].buffersize
;
350 const size_t offset
= p
->pac
->buffers
[i
].offset_lo
;
352 if (p
->pac
->buffers
[i
].type
!= type
)
355 ret
= krb5_data_copy(data
, (unsigned char *)p
->data
.data
+ offset
, len
);
357 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
362 krb5_set_error_message(context
, ENOENT
, "No PAC buffer of type %lu was found",
363 (unsigned long)type
);
372 krb5_pac_get_types(krb5_context context
,
379 *types
= calloc(p
->pac
->numbuffers
, sizeof(*types
));
380 if (*types
== NULL
) {
382 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
385 for (i
= 0; i
< p
->pac
->numbuffers
; i
++)
386 (*types
)[i
] = p
->pac
->buffers
[i
].type
;
387 *len
= p
->pac
->numbuffers
;
397 krb5_pac_free(krb5_context context
, krb5_pac pac
)
399 krb5_data_free(&pac
->data
);
408 static krb5_error_code
409 verify_checksum(krb5_context context
,
410 const struct PAC_INFO_BUFFER
*sig
,
411 const krb5_data
*data
,
412 void *ptr
, size_t len
,
413 const krb5_keyblock
*key
)
415 krb5_crypto crypto
= NULL
;
416 krb5_storage
*sp
= NULL
;
421 memset(&cksum
, 0, sizeof(cksum
));
423 sp
= krb5_storage_from_mem((char *)data
->data
+ sig
->offset_lo
,
426 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
429 krb5_storage_set_flags(sp
, KRB5_STORAGE_BYTEORDER_LE
);
431 CHECK(ret
, krb5_ret_uint32(sp
, &type
), out
);
432 cksum
.cksumtype
= type
;
433 cksum
.checksum
.length
=
434 sig
->buffersize
- krb5_storage_seek(sp
, 0, SEEK_CUR
);
435 cksum
.checksum
.data
= malloc(cksum
.checksum
.length
);
436 if (cksum
.checksum
.data
== NULL
) {
438 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
441 ret
= krb5_storage_read(sp
, cksum
.checksum
.data
, cksum
.checksum
.length
);
442 if (ret
!= cksum
.checksum
.length
) {
444 krb5_set_error_message(context
, ret
, "PAC checksum missing checksum");
448 if (!krb5_checksum_is_keyed(context
, cksum
.cksumtype
)) {
450 krb5_set_error_message(context
, ret
, "Checksum type %d not keyed",
455 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
459 ret
= krb5_verify_checksum(context
, crypto
, KRB5_KU_OTHER_CKSUM
,
461 free(cksum
.checksum
.data
);
462 krb5_crypto_destroy(context
, crypto
);
463 krb5_storage_free(sp
);
468 if (cksum
.checksum
.data
)
469 free(cksum
.checksum
.data
);
471 krb5_storage_free(sp
);
473 krb5_crypto_destroy(context
, crypto
);
477 static krb5_error_code
478 create_checksum(krb5_context context
,
479 const krb5_keyblock
*key
,
480 void *data
, size_t datalen
,
481 void *sig
, size_t siglen
)
483 krb5_crypto crypto
= NULL
;
487 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
491 ret
= krb5_create_checksum(context
, crypto
, KRB5_KU_OTHER_CKSUM
, 0,
492 data
, datalen
, &cksum
);
493 krb5_crypto_destroy(context
, crypto
);
497 if (cksum
.checksum
.length
!= siglen
) {
498 krb5_set_error_message(context
, EINVAL
, "pac checksum wrong length");
499 free_Checksum(&cksum
);
503 memcpy(sig
, cksum
.checksum
.data
, siglen
);
504 free_Checksum(&cksum
);
514 #define NTTIME_EPOCH 0x019DB1DED53E8000LL
517 unix2nttime(time_t unix_time
)
520 wt
= unix_time
* (uint64_t)10000000 + (uint64_t)NTTIME_EPOCH
;
524 static krb5_error_code
525 verify_logonname(krb5_context context
,
526 const struct PAC_INFO_BUFFER
*logon_name
,
527 const krb5_data
*data
,
529 krb5_const_principal principal
)
533 uint32_t time1
, time2
;
538 sp
= krb5_storage_from_readonly_mem((const char *)data
->data
+ logon_name
->offset_lo
,
539 logon_name
->buffersize
);
541 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
545 krb5_storage_set_flags(sp
, KRB5_STORAGE_BYTEORDER_LE
);
547 CHECK(ret
, krb5_ret_uint32(sp
, &time1
), out
);
548 CHECK(ret
, krb5_ret_uint32(sp
, &time2
), out
);
552 t1
= unix2nttime(authtime
);
553 t2
= ((uint64_t)time2
<< 32) | time1
;
555 krb5_storage_free(sp
);
556 krb5_set_error_message(context
, EINVAL
, "PAC timestamp mismatch");
560 CHECK(ret
, krb5_ret_uint16(sp
, &len
), out
);
562 krb5_storage_free(sp
);
563 krb5_set_error_message(context
, EINVAL
, "PAC logon name length missing");
569 krb5_storage_free(sp
);
570 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
573 ret
= krb5_storage_read(sp
, s
, len
);
575 krb5_storage_free(sp
);
576 krb5_set_error_message(context
, EINVAL
, "Failed to read PAC logon name");
579 krb5_storage_free(sp
);
581 size_t ucs2len
= len
/ 2;
584 unsigned int flags
= WIND_RW_LE
;
586 ucs2
= malloc(sizeof(ucs2
[0]) * ucs2len
);
588 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
591 ret
= wind_ucs2read(s
, len
, &flags
, ucs2
, &ucs2len
);
595 krb5_set_error_message(context
, ret
, "Failed to convert string to UCS-2");
598 ret
= wind_ucs2utf8_length(ucs2
, ucs2len
, &u8len
);
601 krb5_set_error_message(context
, ret
, "Failed to count length of UCS-2 string");
604 u8len
+= 1; /* Add space for NUL */
608 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
611 ret
= wind_ucs2utf8(ucs2
, ucs2len
, s
, &u8len
);
615 krb5_set_error_message(context
, ret
, "Failed to convert to UTF-8");
619 ret
= krb5_parse_name_flags(context
, s
, KRB5_PRINCIPAL_PARSE_NO_REALM
, &p2
);
624 if (krb5_principal_compare_any_realm(context
, principal
, p2
) != TRUE
) {
626 krb5_set_error_message(context
, ret
, "PAC logon name mismatch");
628 krb5_free_principal(context
, p2
);
638 static krb5_error_code
639 build_logon_name(krb5_context context
,
641 krb5_const_principal principal
,
650 t
= unix2nttime(authtime
);
652 krb5_data_zero(logon
);
654 sp
= krb5_storage_emem();
656 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
659 krb5_storage_set_flags(sp
, KRB5_STORAGE_BYTEORDER_LE
);
661 CHECK(ret
, krb5_store_uint32(sp
, t
& 0xffffffff), out
);
662 CHECK(ret
, krb5_store_uint32(sp
, t
>> 32), out
);
664 ret
= krb5_unparse_name_flags(context
, principal
,
665 KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &s
);
671 CHECK(ret
, krb5_store_uint16(sp
, len
* 2), out
);
673 #if 1 /* cheat for now */
674 s2
= malloc(len
* 2);
680 for (i
= 0; i
< len
; i
++) {
686 /* write libwind code here */
689 ret
= krb5_storage_write(sp
, s2
, len
* 2);
691 if (ret
!= len
* 2) {
695 ret
= krb5_storage_to_data(sp
, logon
);
698 krb5_storage_free(sp
);
702 krb5_storage_free(sp
);
710 * @param context Kerberos 5 context.
711 * @param pac the pac structure returned by krb5_pac_parse().
712 * @param authtime The time of the ticket the PAC belongs to.
713 * @param principal the principal to verify.
714 * @param server The service key, most always be given.
715 * @param privsvr The KDC key, may be given.
717 * @return Returns 0 to indicate success. Otherwise an kerberos et
718 * error code is returned, see krb5_get_error_message().
724 krb5_pac_verify(krb5_context context
,
727 krb5_const_principal principal
,
728 const krb5_keyblock
*server
,
729 const krb5_keyblock
*privsvr
)
733 if (pac
->server_checksum
== NULL
) {
734 krb5_set_error_message(context
, EINVAL
, "PAC missing server checksum");
737 if (pac
->privsvr_checksum
== NULL
) {
738 krb5_set_error_message(context
, EINVAL
, "PAC missing kdc checksum");
741 if (pac
->logon_name
== NULL
) {
742 krb5_set_error_message(context
, EINVAL
, "PAC missing logon name");
746 ret
= verify_logonname(context
,
755 * in the service case, clean out data option of the privsvr and
756 * server checksum before checking the checksum.
761 ret
= krb5_copy_data(context
, &pac
->data
, ©
);
765 if (pac
->server_checksum
->buffersize
< 4)
767 if (pac
->privsvr_checksum
->buffersize
< 4)
770 memset((char *)copy
->data
+ pac
->server_checksum
->offset_lo
+ 4,
772 pac
->server_checksum
->buffersize
- 4);
774 memset((char *)copy
->data
+ pac
->privsvr_checksum
->offset_lo
+ 4,
776 pac
->privsvr_checksum
->buffersize
- 4);
778 ret
= verify_checksum(context
,
779 pac
->server_checksum
,
784 krb5_free_data(context
, copy
);
789 /* The priv checksum covers the server checksum */
790 ret
= verify_checksum(context
,
791 pac
->privsvr_checksum
,
793 (char *)pac
->data
.data
794 + pac
->server_checksum
->offset_lo
+ 4,
795 pac
->server_checksum
->buffersize
- 4,
808 static krb5_error_code
809 fill_zeros(krb5_context context
, krb5_storage
*sp
, size_t len
)
816 if (l
> sizeof(zeros
))
818 sret
= krb5_storage_write(sp
, zeros
, l
);
820 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
828 static krb5_error_code
829 pac_checksum(krb5_context context
,
830 const krb5_keyblock
*key
,
834 krb5_cksumtype cktype
;
836 krb5_crypto crypto
= NULL
;
838 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
842 ret
= krb5_crypto_get_checksum_type(context
, crypto
, &cktype
);
843 krb5_crypto_destroy(context
, crypto
);
847 if (krb5_checksum_is_keyed(context
, cktype
) == FALSE
) {
848 krb5_set_error_message(context
, EINVAL
, "PAC checksum type is not keyed");
852 ret
= krb5_checksumsize(context
, cktype
, cksumsize
);
856 *cksumtype
= (uint32_t)cktype
;
862 _krb5_pac_sign(krb5_context context
,
865 krb5_principal principal
,
866 const krb5_keyblock
*server_key
,
867 const krb5_keyblock
*priv_key
,
871 krb5_storage
*sp
= NULL
, *spdata
= NULL
;
873 size_t server_size
, priv_size
;
874 uint32_t server_offset
= 0, priv_offset
= 0;
875 uint32_t server_cksumtype
= 0, priv_cksumtype
= 0;
879 krb5_data_zero(&logon
);
881 if (p
->logon_name
== NULL
)
883 if (p
->server_checksum
== NULL
)
885 if (p
->privsvr_checksum
== NULL
)
891 ptr
= realloc(p
->pac
, sizeof(*p
->pac
) + (sizeof(p
->pac
->buffers
[0]) * (p
->pac
->numbuffers
+ num
- 1)));
893 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
898 if (p
->logon_name
== NULL
) {
899 p
->logon_name
= &p
->pac
->buffers
[p
->pac
->numbuffers
++];
900 memset(p
->logon_name
, 0, sizeof(*p
->logon_name
));
901 p
->logon_name
->type
= PAC_LOGON_NAME
;
903 if (p
->server_checksum
== NULL
) {
904 p
->server_checksum
= &p
->pac
->buffers
[p
->pac
->numbuffers
++];
905 memset(p
->server_checksum
, 0, sizeof(*p
->server_checksum
));
906 p
->server_checksum
->type
= PAC_SERVER_CHECKSUM
;
908 if (p
->privsvr_checksum
== NULL
) {
909 p
->privsvr_checksum
= &p
->pac
->buffers
[p
->pac
->numbuffers
++];
910 memset(p
->privsvr_checksum
, 0, sizeof(*p
->privsvr_checksum
));
911 p
->privsvr_checksum
->type
= PAC_PRIVSVR_CHECKSUM
;
915 /* Calculate LOGON NAME */
916 ret
= build_logon_name(context
, authtime
, principal
, &logon
);
920 /* Set lengths for checksum */
921 ret
= pac_checksum(context
, server_key
, &server_cksumtype
, &server_size
);
924 ret
= pac_checksum(context
, priv_key
, &priv_cksumtype
, &priv_size
);
929 sp
= krb5_storage_emem();
931 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
934 krb5_storage_set_flags(sp
, KRB5_STORAGE_BYTEORDER_LE
);
936 spdata
= krb5_storage_emem();
937 if (spdata
== NULL
) {
938 krb5_storage_free(sp
);
939 krb5_set_error_message(context
, ENOMEM
, N_("malloc: out of memory", ""));
942 krb5_storage_set_flags(spdata
, KRB5_STORAGE_BYTEORDER_LE
);
944 CHECK(ret
, krb5_store_uint32(sp
, p
->pac
->numbuffers
), out
);
945 CHECK(ret
, krb5_store_uint32(sp
, p
->pac
->version
), out
);
947 end
= PACTYPE_SIZE
+ (PAC_INFO_BUFFER_SIZE
* p
->pac
->numbuffers
);
949 for (i
= 0; i
< p
->pac
->numbuffers
; i
++) {
956 if (p
->pac
->buffers
[i
].type
== PAC_SERVER_CHECKSUM
) {
957 len
= server_size
+ 4;
958 server_offset
= end
+ 4;
959 CHECK(ret
, krb5_store_uint32(spdata
, server_cksumtype
), out
);
960 CHECK(ret
, fill_zeros(context
, spdata
, server_size
), out
);
961 } else if (p
->pac
->buffers
[i
].type
== PAC_PRIVSVR_CHECKSUM
) {
963 priv_offset
= end
+ 4;
964 CHECK(ret
, krb5_store_uint32(spdata
, priv_cksumtype
), out
);
965 CHECK(ret
, fill_zeros(context
, spdata
, priv_size
), out
);
966 } else if (p
->pac
->buffers
[i
].type
== PAC_LOGON_NAME
) {
967 len
= krb5_storage_write(spdata
, logon
.data
, logon
.length
);
968 if (logon
.length
!= len
) {
973 len
= p
->pac
->buffers
[i
].buffersize
;
974 ptr
= (char *)p
->data
.data
+ p
->pac
->buffers
[i
].offset_lo
;
976 sret
= krb5_storage_write(spdata
, ptr
, len
);
979 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
982 /* XXX if not aligned, fill_zeros */
986 CHECK(ret
, krb5_store_uint32(sp
, p
->pac
->buffers
[i
].type
), out
);
987 CHECK(ret
, krb5_store_uint32(sp
, len
), out
);
988 CHECK(ret
, krb5_store_uint32(sp
, end
), out
);
989 CHECK(ret
, krb5_store_uint32(sp
, 0), out
);
991 /* advance data endpointer and align */
996 e
= ((end
+ PAC_ALIGNMENT
- 1) / PAC_ALIGNMENT
) * PAC_ALIGNMENT
;
998 CHECK(ret
, fill_zeros(context
, spdata
, e
- end
), out
);
1005 /* assert (server_offset != 0 && priv_offset != 0); */
1008 ret
= krb5_storage_to_data(spdata
, &d
);
1010 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
1013 ret
= krb5_storage_write(sp
, d
.data
, d
.length
);
1014 if (ret
!= d
.length
) {
1017 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
1022 ret
= krb5_storage_to_data(sp
, &d
);
1024 krb5_set_error_message(context
, ret
, N_("malloc: out of memory", ""));
1030 ret
= create_checksum(context
, server_key
,
1032 (char *)d
.data
+ server_offset
, server_size
);
1038 ret
= create_checksum(context
, priv_key
,
1039 (char *)d
.data
+ server_offset
, server_size
,
1040 (char *)d
.data
+ priv_offset
, priv_size
);
1049 krb5_data_free(&logon
);
1050 krb5_storage_free(sp
);
1051 krb5_storage_free(spdata
);
1055 krb5_data_free(&logon
);
1057 krb5_storage_free(sp
);
1059 krb5_storage_free(spdata
);