2 * Copyright (c) 1997-2008 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"
35 #include "store-int.h"
37 #define BYTEORDER_IS(SP, V) (((SP)->flags & KRB5_STORAGE_BYTEORDER_MASK) == (V))
38 #define BYTEORDER_IS_LE(SP) BYTEORDER_IS((SP), KRB5_STORAGE_BYTEORDER_LE)
39 #define BYTEORDER_IS_BE(SP) BYTEORDER_IS((SP), KRB5_STORAGE_BYTEORDER_BE)
40 #define BYTEORDER_IS_HOST(SP) (BYTEORDER_IS((SP), KRB5_STORAGE_BYTEORDER_HOST) || \
41 krb5_storage_is_flags((SP), KRB5_STORAGE_HOST_BYTEORDER))
44 * Add the flags on a storage buffer by or-ing in the flags to the buffer.
46 * @param sp the storage buffer to set the flags on
47 * @param flags the flags to set
49 * @ingroup krb5_storage
52 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
53 krb5_storage_set_flags(krb5_storage
*sp
, krb5_flags flags
)
59 * Clear the flags on a storage buffer
61 * @param sp the storage buffer to clear the flags on
62 * @param flags the flags to clear
64 * @ingroup krb5_storage
67 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
68 krb5_storage_clear_flags(krb5_storage
*sp
, krb5_flags flags
)
74 * Return true or false depending on if the storage flags is set or
75 * not. NB testing for the flag 0 always return true.
77 * @param sp the storage buffer to check flags on
78 * @param flags The flags to test for
80 * @return true if all the flags are set, false if not.
82 * @ingroup krb5_storage
85 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
86 krb5_storage_is_flags(krb5_storage
*sp
, krb5_flags flags
)
88 return (sp
->flags
& flags
) == flags
;
92 * Set the new byte order of the storage buffer.
94 * @param sp the storage buffer to set the byte order for.
95 * @param byteorder the new byte order.
97 * The byte order are: KRB5_STORAGE_BYTEORDER_BE,
98 * KRB5_STORAGE_BYTEORDER_LE and KRB5_STORAGE_BYTEORDER_HOST.
100 * @ingroup krb5_storage
103 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
104 krb5_storage_set_byteorder(krb5_storage
*sp
, krb5_flags byteorder
)
106 sp
->flags
&= ~KRB5_STORAGE_BYTEORDER_MASK
;
107 sp
->flags
|= byteorder
;
111 * Return the current byteorder for the buffer. See krb5_storage_set_byteorder() for the list or byte order contants.
113 * @ingroup krb5_storage
116 KRB5_LIB_FUNCTION krb5_flags KRB5_LIB_CALL
117 krb5_storage_get_byteorder(krb5_storage
*sp
)
119 return sp
->flags
& KRB5_STORAGE_BYTEORDER_MASK
;
123 * Set the max alloc value
125 * @param sp the storage buffer set the max allow for
126 * @param size maximum size to allocate, use 0 to remove limit
128 * @ingroup krb5_storage
131 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
132 krb5_storage_set_max_alloc(krb5_storage
*sp
, size_t size
)
134 sp
->max_alloc
= size
;
137 /* don't allocate unresonable amount of memory */
138 static krb5_error_code
139 size_too_large(krb5_storage
*sp
, size_t size
)
141 if (sp
->max_alloc
&& sp
->max_alloc
< size
)
142 return HEIM_ERR_TOO_BIG
;
146 static krb5_error_code
147 size_too_large_num(krb5_storage
*sp
, size_t count
, size_t size
)
149 if (sp
->max_alloc
== 0 || size
== 0)
151 size
= sp
->max_alloc
/ size
;
153 return HEIM_ERR_TOO_BIG
;
158 * Seek to a new offset.
160 * @param sp the storage buffer to seek in.
161 * @param offset the offset to seek
162 * @param whence relateive searching, SEEK_CUR from the current
163 * position, SEEK_END from the end, SEEK_SET absolute from the start.
165 * @return The new current offset
167 * @ingroup krb5_storage
170 KRB5_LIB_FUNCTION off_t KRB5_LIB_CALL
171 krb5_storage_seek(krb5_storage
*sp
, off_t offset
, int whence
)
173 return (*sp
->seek
)(sp
, offset
, whence
);
177 * Truncate the storage buffer in sp to offset.
179 * @param sp the storage buffer to truncate.
180 * @param offset the offset to truncate too.
182 * @return An Kerberos 5 error code.
184 * @ingroup krb5_storage
187 KRB5_LIB_FUNCTION
int KRB5_LIB_CALL
188 krb5_storage_truncate(krb5_storage
*sp
, off_t offset
)
190 return (*sp
->trunc
)(sp
, offset
);
194 * Sync the storage buffer to its backing store. If there is no
195 * backing store this function will return success.
197 * @param sp the storage buffer to sync
199 * @return A Kerberos 5 error code
201 * @ingroup krb5_storage
204 KRB5_LIB_FUNCTION
int KRB5_LIB_CALL
205 krb5_storage_fsync(krb5_storage
*sp
)
207 if (sp
->fsync
!= NULL
)
208 return sp
->fsync(sp
);
213 * Read to the storage buffer.
215 * @param sp the storage buffer to read from
216 * @param buf the buffer to store the data in
217 * @param len the length to read
219 * @return The length of data read (can be shorter then len), or negative on error.
221 * @ingroup krb5_storage
224 KRB5_LIB_FUNCTION krb5_ssize_t KRB5_LIB_CALL
225 krb5_storage_read(krb5_storage
*sp
, void *buf
, size_t len
)
227 return sp
->fetch(sp
, buf
, len
);
231 * Write to the storage buffer.
233 * @param sp the storage buffer to write to
234 * @param buf the buffer to write to the storage buffer
235 * @param len the length to write
237 * @return The length of data written (can be shorter then len), or negative on error.
239 * @ingroup krb5_storage
242 KRB5_LIB_FUNCTION krb5_ssize_t KRB5_LIB_CALL
243 krb5_storage_write(krb5_storage
*sp
, const void *buf
, size_t len
)
245 return sp
->store(sp
, buf
, len
);
249 * Set the return code that will be used when end of storage is reached.
251 * @param sp the storage
252 * @param code the error code to return on end of storage
254 * @ingroup krb5_storage
257 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
258 krb5_storage_set_eof_code(krb5_storage
*sp
, int code
)
264 * Get the return code that will be used when end of storage is reached.
266 * @param sp the storage
268 * @return storage error code
270 * @ingroup krb5_storage
273 KRB5_LIB_FUNCTION
int KRB5_LIB_CALL
274 krb5_storage_get_eof_code(krb5_storage
*sp
)
280 * Free a krb5 storage.
282 * @param sp the storage to free.
284 * @return An Kerberos 5 error code.
286 * @ingroup krb5_storage
289 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
290 krb5_storage_free(krb5_storage
*sp
)
300 * Copy the contnent of storage
302 * @param sp the storage to copy to a data
303 * @param data the copied data, free with krb5_data_free()
305 * @return 0 for success, or a Kerberos 5 error code on failure.
307 * @ingroup krb5_storage
310 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
311 krb5_storage_to_data(krb5_storage
*sp
, krb5_data
*data
)
316 pos
= sp
->seek(sp
, 0, SEEK_CUR
);
318 return HEIM_ERR_NOT_SEEKABLE
;
319 size
= sp
->seek(sp
, 0, SEEK_END
);
320 ret
= size_too_large(sp
, size
);
323 ret
= krb5_data_alloc(data
, size
);
325 sp
->seek(sp
, pos
, SEEK_SET
);
329 sp
->seek(sp
, 0, SEEK_SET
);
330 sp
->fetch(sp
, data
->data
, data
->length
);
331 sp
->seek(sp
, pos
, SEEK_SET
);
336 static krb5_error_code
337 krb5_store_int(krb5_storage
*sp
,
346 _krb5_put_int(v
, value
, len
);
347 ret
= sp
->store(sp
, v
, len
);
350 if ((size_t)ret
!= len
)
356 * Store a int32 to storage, byte order is controlled by the settings
357 * on the storage, see krb5_storage_set_byteorder().
359 * @param sp the storage to write too
360 * @param value the value to store
362 * @return 0 for success, or a Kerberos 5 error code on failure.
364 * @ingroup krb5_storage
367 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
368 krb5_store_int32(krb5_storage
*sp
,
371 if(BYTEORDER_IS_HOST(sp
))
372 value
= htonl(value
);
373 else if(BYTEORDER_IS_LE(sp
))
374 value
= bswap32(value
);
375 return krb5_store_int(sp
, value
, 4);
379 * Store a uint32 to storage, byte order is controlled by the settings
380 * on the storage, see krb5_storage_set_byteorder().
382 * @param sp the storage to write too
383 * @param value the value to store
385 * @return 0 for success, or a Kerberos 5 error code on failure.
387 * @ingroup krb5_storage
390 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
391 krb5_store_uint32(krb5_storage
*sp
,
394 return krb5_store_int32(sp
, (int32_t)value
);
397 static krb5_error_code
398 krb5_ret_int(krb5_storage
*sp
,
405 ret
= sp
->fetch(sp
, v
, len
);
408 if ((size_t)ret
!= len
)
410 _krb5_get_int(v
, &w
, len
);
416 * Read a int32 from storage, byte order is controlled by the settings
417 * on the storage, see krb5_storage_set_byteorder().
419 * @param sp the storage to write too
420 * @param value the value read from the buffer
422 * @return 0 for success, or a Kerberos 5 error code on failure.
424 * @ingroup krb5_storage
427 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
428 krb5_ret_int32(krb5_storage
*sp
,
431 krb5_error_code ret
= krb5_ret_int(sp
, value
, 4);
434 if(BYTEORDER_IS_HOST(sp
))
435 *value
= htonl(*value
);
436 else if(BYTEORDER_IS_LE(sp
))
437 *value
= bswap32(*value
);
442 * Read a uint32 from storage, byte order is controlled by the settings
443 * on the storage, see krb5_storage_set_byteorder().
445 * @param sp the storage to write too
446 * @param value the value read from the buffer
448 * @return 0 for success, or a Kerberos 5 error code on failure.
450 * @ingroup krb5_storage
453 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
454 krb5_ret_uint32(krb5_storage
*sp
,
460 ret
= krb5_ret_int32(sp
, &v
);
462 *value
= (uint32_t)v
;
468 * Store a int16 to storage, byte order is controlled by the settings
469 * on the storage, see krb5_storage_set_byteorder().
471 * @param sp the storage to write too
472 * @param value the value to store
474 * @return 0 for success, or a Kerberos 5 error code on failure.
476 * @ingroup krb5_storage
479 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
480 krb5_store_int16(krb5_storage
*sp
,
483 if(BYTEORDER_IS_HOST(sp
))
484 value
= htons(value
);
485 else if(BYTEORDER_IS_LE(sp
))
486 value
= bswap16(value
);
487 return krb5_store_int(sp
, value
, 2);
491 * Store a uint16 to storage, byte order is controlled by the settings
492 * on the storage, see krb5_storage_set_byteorder().
494 * @param sp the storage to write too
495 * @param value the value to store
497 * @return 0 for success, or a Kerberos 5 error code on failure.
499 * @ingroup krb5_storage
502 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
503 krb5_store_uint16(krb5_storage
*sp
,
506 return krb5_store_int16(sp
, (int16_t)value
);
510 * Read a int16 from storage, byte order is controlled by the settings
511 * on the storage, see krb5_storage_set_byteorder().
513 * @param sp the storage to write too
514 * @param value the value read from the buffer
516 * @return 0 for success, or a Kerberos 5 error code on failure.
518 * @ingroup krb5_storage
521 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
522 krb5_ret_int16(krb5_storage
*sp
,
527 ret
= krb5_ret_int(sp
, &v
, 2);
531 if(BYTEORDER_IS_HOST(sp
))
532 *value
= htons(*value
);
533 else if(BYTEORDER_IS_LE(sp
))
534 *value
= bswap16(*value
);
539 * Read a int16 from storage, byte order is controlled by the settings
540 * on the storage, see krb5_storage_set_byteorder().
542 * @param sp the storage to write too
543 * @param value the value read from the buffer
545 * @return 0 for success, or a Kerberos 5 error code on failure.
547 * @ingroup krb5_storage
550 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
551 krb5_ret_uint16(krb5_storage
*sp
,
557 ret
= krb5_ret_int16(sp
, &v
);
559 *value
= (uint16_t)v
;
565 * Store a int8 to storage.
567 * @param sp the storage to write too
568 * @param value the value to store
570 * @return 0 for success, or a Kerberos 5 error code on failure.
572 * @ingroup krb5_storage
575 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
576 krb5_store_int8(krb5_storage
*sp
,
581 ret
= sp
->store(sp
, &value
, sizeof(value
));
582 if (ret
!= sizeof(value
))
583 return (ret
<0)?errno
:sp
->eof_code
;
588 * Store a uint8 to storage.
590 * @param sp the storage to write too
591 * @param value the value to store
593 * @return 0 for success, or a Kerberos 5 error code on failure.
595 * @ingroup krb5_storage
598 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
599 krb5_store_uint8(krb5_storage
*sp
,
602 return krb5_store_int8(sp
, (int8_t)value
);
606 * Read a int8 from storage
608 * @param sp the storage to write too
609 * @param value the value read from the buffer
611 * @return 0 for success, or a Kerberos 5 error code on failure.
613 * @ingroup krb5_storage
616 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
617 krb5_ret_int8(krb5_storage
*sp
,
622 ret
= sp
->fetch(sp
, value
, sizeof(*value
));
623 if (ret
!= sizeof(*value
))
624 return (ret
<0)?errno
:sp
->eof_code
;
629 * Read a uint8 from storage
631 * @param sp the storage to write too
632 * @param value the value read from the buffer
634 * @return 0 for success, or a Kerberos 5 error code on failure.
636 * @ingroup krb5_storage
639 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
640 krb5_ret_uint8(krb5_storage
*sp
,
646 ret
= krb5_ret_int8(sp
, &v
);
654 * Store a data to the storage. The data is stored with an int32 as
655 * lenght plus the data (not padded).
657 * @param sp the storage buffer to write to
658 * @param data the buffer to store.
660 * @return 0 on success, a Kerberos 5 error code on failure.
662 * @ingroup krb5_storage
665 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
666 krb5_store_data(krb5_storage
*sp
,
670 ret
= krb5_store_int32(sp
, data
.length
);
673 ret
= sp
->store(sp
, data
.data
, data
.length
);
676 if((size_t)ret
!= data
.length
)
682 * Parse a data from the storage.
684 * @param sp the storage buffer to read from
685 * @param data the parsed data
687 * @return 0 on success, a Kerberos 5 error code on failure.
689 * @ingroup krb5_storage
692 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
693 krb5_ret_data(krb5_storage
*sp
,
699 ret
= krb5_ret_int32(sp
, &size
);
702 ret
= size_too_large(sp
, size
);
705 ret
= krb5_data_alloc (data
, size
);
709 ret
= sp
->fetch(sp
, data
->data
, size
);
711 krb5_data_free(data
);
712 return (ret
< 0)? errno
: sp
->eof_code
;
719 * Store a string to the buffer. The data is formated as an len:uint32
720 * plus the string itself (not padded).
722 * @param sp the storage buffer to write to
723 * @param s the string to store.
725 * @return 0 on success, a Kerberos 5 error code on failure.
727 * @ingroup krb5_storage
730 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
731 krb5_store_string(krb5_storage
*sp
, const char *s
)
734 data
.length
= strlen(s
);
735 data
.data
= rk_UNCONST(s
);
736 return krb5_store_data(sp
, data
);
740 * Parse a string from the storage.
742 * @param sp the storage buffer to read from
743 * @param string the parsed string
745 * @return 0 on success, a Kerberos 5 error code on failure.
747 * @ingroup krb5_storage
751 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
752 krb5_ret_string(krb5_storage
*sp
,
757 ret
= krb5_ret_data(sp
, &data
);
760 *string
= realloc(data
.data
, data
.length
+ 1);
765 (*string
)[data
.length
] = 0;
770 * Store a zero terminated string to the buffer. The data is stored
771 * one character at a time until a NUL is stored.
773 * @param sp the storage buffer to write to
774 * @param s the string to store.
776 * @return 0 on success, a Kerberos 5 error code on failure.
778 * @ingroup krb5_storage
781 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
782 krb5_store_stringz(krb5_storage
*sp
, const char *s
)
784 size_t len
= strlen(s
) + 1;
787 ret
= sp
->store(sp
, s
, len
);
790 if((size_t)ret
!= len
)
796 * Parse zero terminated string from the storage.
798 * @param sp the storage buffer to read from
799 * @param string the parsed string
801 * @return 0 on success, a Kerberos 5 error code on failure.
803 * @ingroup krb5_storage
806 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
807 krb5_ret_stringz(krb5_storage
*sp
,
815 while((ret
= sp
->fetch(sp
, &c
, 1)) == 1){
816 krb5_error_code eret
;
820 eret
= size_too_large(sp
, len
);
825 tmp
= realloc (s
, len
);
845 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
846 krb5_store_stringnl(krb5_storage
*sp
, const char *s
)
848 size_t len
= strlen(s
);
851 ret
= sp
->store(sp
, s
, len
);
854 if((size_t)ret
!= len
)
856 ret
= sp
->store(sp
, "\n", 1);
868 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
869 krb5_ret_stringnl(krb5_storage
*sp
,
878 while((ret
= sp
->fetch(sp
, &c
, 1)) == 1){
879 krb5_error_code eret
;
886 if (expect_nl
&& c
!= '\n') {
888 return KRB5_BADMSGTYPE
;
892 eret
= size_too_large(sp
, len
);
897 tmp
= realloc (s
, len
);
920 * Write a principal block to storage.
922 * @param sp the storage buffer to write to
923 * @param p the principal block to write.
925 * @return 0 on success, a Kerberos 5 error code on failure.
927 * @ingroup krb5_storage
930 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
931 krb5_store_principal(krb5_storage
*sp
,
932 krb5_const_principal p
)
937 if(!krb5_storage_is_flags(sp
, KRB5_STORAGE_PRINCIPAL_NO_NAME_TYPE
)) {
938 ret
= krb5_store_int32(sp
, p
->name
.name_type
);
941 if(krb5_storage_is_flags(sp
, KRB5_STORAGE_PRINCIPAL_WRONG_NUM_COMPONENTS
))
942 ret
= krb5_store_int32(sp
, p
->name
.name_string
.len
+ 1);
944 ret
= krb5_store_int32(sp
, p
->name
.name_string
.len
);
947 ret
= krb5_store_string(sp
, p
->realm
);
949 for(i
= 0; i
< p
->name
.name_string
.len
; i
++){
950 ret
= krb5_store_string(sp
, p
->name
.name_string
.val
[i
]);
957 * Parse principal from the storage.
959 * @param sp the storage buffer to read from
960 * @param princ the parsed principal
962 * @return 0 on success, a Kerberos 5 error code on failure.
964 * @ingroup krb5_storage
967 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
968 krb5_ret_principal(krb5_storage
*sp
,
969 krb5_principal
*princ
)
977 p
= calloc(1, sizeof(*p
));
981 if(krb5_storage_is_flags(sp
, KRB5_STORAGE_PRINCIPAL_NO_NAME_TYPE
))
982 type
= KRB5_NT_UNKNOWN
;
983 else if((ret
= krb5_ret_int32(sp
, &type
))){
987 if((ret
= krb5_ret_int32(sp
, &ncomp
))){
991 if(krb5_storage_is_flags(sp
, KRB5_STORAGE_PRINCIPAL_WRONG_NUM_COMPONENTS
))
997 ret
= size_too_large_num(sp
, ncomp
, sizeof(p
->name
.name_string
.val
[0]));
1002 p
->name
.name_type
= type
;
1003 p
->name
.name_string
.len
= ncomp
;
1004 ret
= krb5_ret_string(sp
, &p
->realm
);
1009 p
->name
.name_string
.val
= calloc(ncomp
, sizeof(p
->name
.name_string
.val
[0]));
1010 if(p
->name
.name_string
.val
== NULL
&& ncomp
!= 0){
1015 for(i
= 0; i
< ncomp
; i
++){
1016 ret
= krb5_ret_string(sp
, &p
->name
.name_string
.val
[i
]);
1019 free(p
->name
.name_string
.val
[i
--]);
1030 * Store a keyblock to the storage.
1032 * @param sp the storage buffer to write to
1033 * @param p the keyblock to write
1035 * @return 0 on success, a Kerberos 5 error code on failure.
1037 * @ingroup krb5_storage
1040 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1041 krb5_store_keyblock(krb5_storage
*sp
, krb5_keyblock p
)
1044 ret
= krb5_store_int16(sp
, p
.keytype
);
1047 if(krb5_storage_is_flags(sp
, KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE
)){
1048 /* this should really be enctype, but it is the same as
1050 ret
= krb5_store_int16(sp
, p
.keytype
);
1054 ret
= krb5_store_data(sp
, p
.keyvalue
);
1059 * Read a keyblock from the storage.
1061 * @param sp the storage buffer to write to
1062 * @param p the keyblock read from storage, free using krb5_free_keyblock()
1064 * @return 0 on success, a Kerberos 5 error code on failure.
1066 * @ingroup krb5_storage
1069 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1070 krb5_ret_keyblock(krb5_storage
*sp
, krb5_keyblock
*p
)
1075 ret
= krb5_ret_int16(sp
, &tmp
);
1079 if(krb5_storage_is_flags(sp
, KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE
)){
1080 ret
= krb5_ret_int16(sp
, &tmp
);
1084 ret
= krb5_ret_data(sp
, &p
->keyvalue
);
1089 * Write a times block to storage.
1091 * @param sp the storage buffer to write to
1092 * @param times the times block to write.
1094 * @return 0 on success, a Kerberos 5 error code on failure.
1096 * @ingroup krb5_storage
1099 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1100 krb5_store_times(krb5_storage
*sp
, krb5_times times
)
1103 ret
= krb5_store_int32(sp
, times
.authtime
);
1105 ret
= krb5_store_int32(sp
, times
.starttime
);
1107 ret
= krb5_store_int32(sp
, times
.endtime
);
1109 ret
= krb5_store_int32(sp
, times
.renew_till
);
1114 * Read a times block from the storage.
1116 * @param sp the storage buffer to write to
1117 * @param times the times block read from storage
1119 * @return 0 on success, a Kerberos 5 error code on failure.
1121 * @ingroup krb5_storage
1124 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1125 krb5_ret_times(krb5_storage
*sp
, krb5_times
*times
)
1129 ret
= krb5_ret_int32(sp
, &tmp
);
1130 times
->authtime
= tmp
;
1132 ret
= krb5_ret_int32(sp
, &tmp
);
1133 times
->starttime
= tmp
;
1135 ret
= krb5_ret_int32(sp
, &tmp
);
1136 times
->endtime
= tmp
;
1138 ret
= krb5_ret_int32(sp
, &tmp
);
1139 times
->renew_till
= tmp
;
1144 * Write a address block to storage.
1146 * @param sp the storage buffer to write to
1147 * @param p the address block to write.
1149 * @return 0 on success, a Kerberos 5 error code on failure.
1151 * @ingroup krb5_storage
1154 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1155 krb5_store_address(krb5_storage
*sp
, krb5_address p
)
1158 ret
= krb5_store_int16(sp
, p
.addr_type
);
1160 ret
= krb5_store_data(sp
, p
.address
);
1165 * Read a address block from the storage.
1167 * @param sp the storage buffer to write to
1168 * @param adr the address block read from storage
1170 * @return 0 on success, a Kerberos 5 error code on failure.
1172 * @ingroup krb5_storage
1175 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1176 krb5_ret_address(krb5_storage
*sp
, krb5_address
*adr
)
1180 ret
= krb5_ret_int16(sp
, &t
);
1183 ret
= krb5_ret_data(sp
, &adr
->address
);
1188 * Write a addresses block to storage.
1190 * @param sp the storage buffer to write to
1191 * @param p the addresses block to write.
1193 * @return 0 on success, a Kerberos 5 error code on failure.
1195 * @ingroup krb5_storage
1198 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1199 krb5_store_addrs(krb5_storage
*sp
, krb5_addresses p
)
1203 ret
= krb5_store_int32(sp
, p
.len
);
1205 for(i
= 0; i
<p
.len
; i
++){
1206 ret
= krb5_store_address(sp
, p
.val
[i
]);
1213 * Read a addresses block from the storage.
1215 * @param sp the storage buffer to write to
1216 * @param adr the addresses block read from storage
1218 * @return 0 on success, a Kerberos 5 error code on failure.
1220 * @ingroup krb5_storage
1223 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1224 krb5_ret_addrs(krb5_storage
*sp
, krb5_addresses
*adr
)
1230 ret
= krb5_ret_int32(sp
, &tmp
);
1232 ret
= size_too_large_num(sp
, tmp
, sizeof(adr
->val
[0]));
1233 if (ret
) return ret
;
1235 ALLOC(adr
->val
, adr
->len
);
1236 if (adr
->val
== NULL
&& adr
->len
!= 0)
1238 for(i
= 0; i
< adr
->len
; i
++){
1239 ret
= krb5_ret_address(sp
, &adr
->val
[i
]);
1246 * Write a auth data block to storage.
1248 * @param sp the storage buffer to write to
1249 * @param auth the auth data block to write.
1251 * @return 0 on success, a Kerberos 5 error code on failure.
1253 * @ingroup krb5_storage
1256 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1257 krb5_store_authdata(krb5_storage
*sp
, krb5_authdata auth
)
1259 krb5_error_code ret
;
1261 ret
= krb5_store_int32(sp
, auth
.len
);
1263 for(i
= 0; i
< auth
.len
; i
++){
1264 ret
= krb5_store_int16(sp
, auth
.val
[i
].ad_type
);
1266 ret
= krb5_store_data(sp
, auth
.val
[i
].ad_data
);
1273 * Read a auth data from the storage.
1275 * @param sp the storage buffer to write to
1276 * @param auth the auth data block read from storage
1278 * @return 0 on success, a Kerberos 5 error code on failure.
1280 * @ingroup krb5_storage
1283 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1284 krb5_ret_authdata(krb5_storage
*sp
, krb5_authdata
*auth
)
1286 krb5_error_code ret
;
1290 ret
= krb5_ret_int32(sp
, &tmp
);
1292 ret
= size_too_large_num(sp
, tmp
, sizeof(auth
->val
[0]));
1293 if (ret
) return ret
;
1294 ALLOC_SEQ(auth
, tmp
);
1295 if (auth
->val
== NULL
&& tmp
!= 0)
1297 for(i
= 0; i
< tmp
; i
++){
1298 ret
= krb5_ret_int16(sp
, &tmp2
);
1300 auth
->val
[i
].ad_type
= tmp2
;
1301 ret
= krb5_ret_data(sp
, &auth
->val
[i
].ad_data
);
1308 bitswap32(int32_t b
)
1312 for (i
= 0; i
< 32; i
++) {
1313 r
= r
<< 1 | (b
& 1);
1320 * Write a credentials block to storage.
1322 * @param sp the storage buffer to write to
1323 * @param creds the creds block to write.
1325 * @return 0 on success, a Kerberos 5 error code on failure.
1327 * @ingroup krb5_storage
1330 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1331 krb5_store_creds(krb5_storage
*sp
, krb5_creds
*creds
)
1335 ret
= krb5_store_principal(sp
, creds
->client
);
1338 ret
= krb5_store_principal(sp
, creds
->server
);
1341 ret
= krb5_store_keyblock(sp
, creds
->session
);
1344 ret
= krb5_store_times(sp
, creds
->times
);
1347 ret
= krb5_store_int8(sp
, creds
->second_ticket
.length
!= 0); /* is_skey */
1351 if(krb5_storage_is_flags(sp
, KRB5_STORAGE_CREDS_FLAGS_WRONG_BITORDER
))
1352 ret
= krb5_store_int32(sp
, creds
->flags
.i
);
1354 ret
= krb5_store_int32(sp
, bitswap32(TicketFlags2int(creds
->flags
.b
)));
1358 ret
= krb5_store_addrs(sp
, creds
->addresses
);
1361 ret
= krb5_store_authdata(sp
, creds
->authdata
);
1364 ret
= krb5_store_data(sp
, creds
->ticket
);
1367 ret
= krb5_store_data(sp
, creds
->second_ticket
);
1372 * Read a credentials block from the storage.
1374 * @param sp the storage buffer to write to
1375 * @param creds the credentials block read from storage
1377 * @return 0 on success, a Kerberos 5 error code on failure.
1379 * @ingroup krb5_storage
1382 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1383 krb5_ret_creds(krb5_storage
*sp
, krb5_creds
*creds
)
1385 krb5_error_code ret
;
1389 memset(creds
, 0, sizeof(*creds
));
1390 ret
= krb5_ret_principal (sp
, &creds
->client
);
1391 if(ret
) goto cleanup
;
1392 ret
= krb5_ret_principal (sp
, &creds
->server
);
1393 if(ret
) goto cleanup
;
1394 ret
= krb5_ret_keyblock (sp
, &creds
->session
);
1395 if(ret
) goto cleanup
;
1396 ret
= krb5_ret_times (sp
, &creds
->times
);
1397 if(ret
) goto cleanup
;
1398 ret
= krb5_ret_int8 (sp
, &dummy8
);
1399 if(ret
) goto cleanup
;
1400 ret
= krb5_ret_int32 (sp
, &dummy32
);
1401 if(ret
) goto cleanup
;
1403 * Runtime detect the what is the higher bits of the bitfield. If
1404 * any of the higher bits are set in the input data, it's either a
1405 * new ticket flag (and this code need to be removed), or it's a
1406 * MIT cache (or new Heimdal cache), lets change it to our current
1410 uint32_t mask
= 0xffff0000;
1412 creds
->flags
.b
.anonymous
= 1;
1413 if (creds
->flags
.i
& mask
)
1416 dummy32
= bitswap32(dummy32
);
1418 creds
->flags
.i
= dummy32
;
1419 ret
= krb5_ret_addrs (sp
, &creds
->addresses
);
1420 if(ret
) goto cleanup
;
1421 ret
= krb5_ret_authdata (sp
, &creds
->authdata
);
1422 if(ret
) goto cleanup
;
1423 ret
= krb5_ret_data (sp
, &creds
->ticket
);
1424 if(ret
) goto cleanup
;
1425 ret
= krb5_ret_data (sp
, &creds
->second_ticket
);
1429 krb5_free_cred_contents(context
, creds
); /* XXX */
1435 #define SC_CLIENT_PRINCIPAL 0x0001
1436 #define SC_SERVER_PRINCIPAL 0x0002
1437 #define SC_SESSION_KEY 0x0004
1438 #define SC_TICKET 0x0008
1439 #define SC_SECOND_TICKET 0x0010
1440 #define SC_AUTHDATA 0x0020
1441 #define SC_ADDRESSES 0x0040
1444 * Write a tagged credentials block to storage.
1446 * @param sp the storage buffer to write to
1447 * @param creds the creds block to write.
1449 * @return 0 on success, a Kerberos 5 error code on failure.
1451 * @ingroup krb5_storage
1454 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1455 krb5_store_creds_tag(krb5_storage
*sp
, krb5_creds
*creds
)
1461 header
|= SC_CLIENT_PRINCIPAL
;
1463 header
|= SC_SERVER_PRINCIPAL
;
1464 if (creds
->session
.keytype
!= ETYPE_NULL
)
1465 header
|= SC_SESSION_KEY
;
1466 if (creds
->ticket
.data
)
1467 header
|= SC_TICKET
;
1468 if (creds
->second_ticket
.length
)
1469 header
|= SC_SECOND_TICKET
;
1470 if (creds
->authdata
.len
)
1471 header
|= SC_AUTHDATA
;
1472 if (creds
->addresses
.len
)
1473 header
|= SC_ADDRESSES
;
1475 ret
= krb5_store_int32(sp
, header
);
1479 if (creds
->client
) {
1480 ret
= krb5_store_principal(sp
, creds
->client
);
1485 if (creds
->server
) {
1486 ret
= krb5_store_principal(sp
, creds
->server
);
1491 if (creds
->session
.keytype
!= ETYPE_NULL
) {
1492 ret
= krb5_store_keyblock(sp
, creds
->session
);
1497 ret
= krb5_store_times(sp
, creds
->times
);
1500 ret
= krb5_store_int8(sp
, creds
->second_ticket
.length
!= 0); /* is_skey */
1504 ret
= krb5_store_int32(sp
, bitswap32(TicketFlags2int(creds
->flags
.b
)));
1508 if (creds
->addresses
.len
) {
1509 ret
= krb5_store_addrs(sp
, creds
->addresses
);
1514 if (creds
->authdata
.len
) {
1515 ret
= krb5_store_authdata(sp
, creds
->authdata
);
1520 if (creds
->ticket
.data
) {
1521 ret
= krb5_store_data(sp
, creds
->ticket
);
1526 if (creds
->second_ticket
.data
) {
1527 ret
= krb5_store_data(sp
, creds
->second_ticket
);
1536 * Read a tagged credentials block from the storage.
1538 * @param sp the storage buffer to write to
1539 * @param creds the credentials block read from storage
1541 * @return 0 on success, a Kerberos 5 error code on failure.
1543 * @ingroup krb5_storage
1546 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1547 krb5_ret_creds_tag(krb5_storage
*sp
,
1550 krb5_error_code ret
;
1552 int32_t dummy32
, header
;
1554 memset(creds
, 0, sizeof(*creds
));
1556 ret
= krb5_ret_int32 (sp
, &header
);
1557 if (ret
) goto cleanup
;
1559 if (header
& SC_CLIENT_PRINCIPAL
) {
1560 ret
= krb5_ret_principal (sp
, &creds
->client
);
1561 if(ret
) goto cleanup
;
1563 if (header
& SC_SERVER_PRINCIPAL
) {
1564 ret
= krb5_ret_principal (sp
, &creds
->server
);
1565 if(ret
) goto cleanup
;
1567 if (header
& SC_SESSION_KEY
) {
1568 ret
= krb5_ret_keyblock (sp
, &creds
->session
);
1569 if(ret
) goto cleanup
;
1571 ret
= krb5_ret_times (sp
, &creds
->times
);
1572 if(ret
) goto cleanup
;
1573 ret
= krb5_ret_int8 (sp
, &dummy8
);
1574 if(ret
) goto cleanup
;
1575 ret
= krb5_ret_int32 (sp
, &dummy32
);
1576 if(ret
) goto cleanup
;
1578 * Runtime detect the what is the higher bits of the bitfield. If
1579 * any of the higher bits are set in the input data, it's either a
1580 * new ticket flag (and this code need to be removed), or it's a
1581 * MIT cache (or new Heimdal cache), lets change it to our current
1585 uint32_t mask
= 0xffff0000;
1587 creds
->flags
.b
.anonymous
= 1;
1588 if (creds
->flags
.i
& mask
)
1591 dummy32
= bitswap32(dummy32
);
1593 creds
->flags
.i
= dummy32
;
1594 if (header
& SC_ADDRESSES
) {
1595 ret
= krb5_ret_addrs (sp
, &creds
->addresses
);
1596 if(ret
) goto cleanup
;
1598 if (header
& SC_AUTHDATA
) {
1599 ret
= krb5_ret_authdata (sp
, &creds
->authdata
);
1600 if(ret
) goto cleanup
;
1602 if (header
& SC_TICKET
) {
1603 ret
= krb5_ret_data (sp
, &creds
->ticket
);
1604 if(ret
) goto cleanup
;
1606 if (header
& SC_SECOND_TICKET
) {
1607 ret
= krb5_ret_data (sp
, &creds
->second_ticket
);
1608 if(ret
) goto cleanup
;
1614 krb5_free_cred_contents(context
, creds
); /* XXX */