document krb5_ret_{u,}int{32,16,8}
[heimdal.git] / lib / krb5 / store.c
blob4927c947395a1083edb618dbd5c39dbd95eb7348
1 /*
2 * Copyright (c) 1997-2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
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))
43 /**
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 void KRB5_LIB_FUNCTION
53 krb5_storage_set_flags(krb5_storage *sp, krb5_flags flags)
55 sp->flags |= flags;
58 /**
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 void KRB5_LIB_FUNCTION
68 krb5_storage_clear_flags(krb5_storage *sp, krb5_flags flags)
70 sp->flags &= ~flags;
73 /**
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_boolean KRB5_LIB_FUNCTION
86 krb5_storage_is_flags(krb5_storage *sp, krb5_flags flags)
88 return (sp->flags & flags) == flags;
91 /**
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 void KRB5_LIB_FUNCTION
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_flags KRB5_LIB_FUNCTION
117 krb5_storage_get_byteorder(krb5_storage *sp)
119 return sp->flags & KRB5_STORAGE_BYTEORDER_MASK;
123 * Seek to a new offset.
125 * @param sp the storage buffer to seek in.
126 * @param offset the offset to seek
127 * @param whence relateive searching, SEEK_CUR from the current
128 * position, SEEK_END from the end, SEEK_SET absolute from the start.
130 * @return The new current offset
132 * @ingroup krb5_storage
135 off_t KRB5_LIB_FUNCTION
136 krb5_storage_seek(krb5_storage *sp, off_t offset, int whence)
138 return (*sp->seek)(sp, offset, whence);
142 * Truncate the storage buffer in sp to offset.
144 * @param sp the storage buffer to truncate.
145 * @param offset the offset to truncate too.
147 * @return An Kerberos 5 error code.
149 * @ingroup krb5_storage
152 int KRB5_LIB_FUNCTION
153 krb5_storage_truncate(krb5_storage *sp, off_t offset)
155 return (*sp->trunc)(sp, offset);
159 * Read to the storage buffer.
161 * @param sp the storage buffer to read from
162 * @param buf the buffer to store the data in
163 * @param len the length to read
165 * @return The length of data read (can be shorter then len), or negative on error.
167 * @ingroup krb5_storage
170 krb5_ssize_t KRB5_LIB_FUNCTION
171 krb5_storage_read(krb5_storage *sp, void *buf, size_t len)
173 return sp->fetch(sp, buf, len);
177 * Write to the storage buffer.
179 * @param sp the storage buffer to write to
180 * @param buf the buffer to write to the storage buffer
181 * @param len the length to write
183 * @return The length of data written (can be shorter then len), or negative on error.
185 * @ingroup krb5_storage
188 krb5_ssize_t KRB5_LIB_FUNCTION
189 krb5_storage_write(krb5_storage *sp, const void *buf, size_t len)
191 return sp->store(sp, buf, len);
195 * Set the return code that will be used when end of storage is reached.
197 * @param sp the storage
198 * @param code the error code to return on end of storage
200 * @ingroup krb5_storage
203 void KRB5_LIB_FUNCTION
204 krb5_storage_set_eof_code(krb5_storage *sp, int code)
206 sp->eof_code = code;
210 * Get the return code that will be used when end of storage is reached.
212 * @param sp the storage
214 * @return storage error code
216 * @ingroup krb5_storage
219 int KRB5_LIB_FUNCTION
220 krb5_storage_get_eof_code(krb5_storage *sp)
222 return sp->eof_code;
225 krb5_ssize_t KRB5_LIB_FUNCTION
226 _krb5_put_int(void *buffer, unsigned long value, size_t size)
228 unsigned char *p = buffer;
229 int i;
230 for (i = size - 1; i >= 0; i--) {
231 p[i] = value & 0xff;
232 value >>= 8;
234 return size;
237 krb5_ssize_t KRB5_LIB_FUNCTION
238 _krb5_get_int(void *buffer, unsigned long *value, size_t size)
240 unsigned char *p = buffer;
241 unsigned long v = 0;
242 int i;
243 for (i = 0; i < size; i++)
244 v = (v << 8) + p[i];
245 *value = v;
246 return size;
250 * Free a krb5 storage.
252 * @param sp the storage to free.
254 * @return An Kerberos 5 error code.
256 * @ingroup krb5_storage
259 krb5_error_code KRB5_LIB_FUNCTION
260 krb5_storage_free(krb5_storage *sp)
262 if(sp->free)
263 (*sp->free)(sp);
264 free(sp->data);
265 free(sp);
266 return 0;
270 * Copy the contnent of storage
272 * @param sp the storage to copy to a data
273 * @param data the copied data, free with krb5_data_free()
275 * @return 0 for success, or a Kerberos 5 error code on failure.
277 * @ingroup krb5_storage
280 krb5_error_code KRB5_LIB_FUNCTION
281 krb5_storage_to_data(krb5_storage *sp, krb5_data *data)
283 off_t pos, size;
284 krb5_error_code ret;
286 pos = sp->seek(sp, 0, SEEK_CUR);
287 if (pos < 0)
288 return HEIM_ERR_NOT_SEEKABLE;
289 size = (size_t)sp->seek(sp, 0, SEEK_END);
290 if (size > (size_t)-1)
291 return HEIM_ERR_TOO_BIG;
292 ret = krb5_data_alloc (data, size);
293 if (ret) {
294 sp->seek(sp, pos, SEEK_SET);
295 return ret;
297 if (size) {
298 sp->seek(sp, 0, SEEK_SET);
299 sp->fetch(sp, data->data, data->length);
300 sp->seek(sp, pos, SEEK_SET);
302 return 0;
305 static krb5_error_code
306 krb5_store_int(krb5_storage *sp,
307 int32_t value,
308 size_t len)
310 int ret;
311 unsigned char v[16];
313 if(len > sizeof(v))
314 return EINVAL;
315 _krb5_put_int(v, value, len);
316 ret = sp->store(sp, v, len);
317 if (ret != len)
318 return (ret<0)?errno:sp->eof_code;
319 return 0;
323 * Store a int32 to storage, byte order is controlled by the settings
324 * on the storage, see krb5_storage_set_byteorder().
326 * @param sp the storage to write too
327 * @param value the value to store
329 * @return 0 for success, or a Kerberos 5 error code on failure.
331 * @ingroup krb5_storage
334 krb5_error_code KRB5_LIB_FUNCTION
335 krb5_store_int32(krb5_storage *sp,
336 int32_t value)
338 if(BYTEORDER_IS_HOST(sp))
339 value = htonl(value);
340 else if(BYTEORDER_IS_LE(sp))
341 value = bswap32(value);
342 return krb5_store_int(sp, value, 4);
346 * Store a uint32 to storage, byte order is controlled by the settings
347 * on the storage, see krb5_storage_set_byteorder().
349 * @param sp the storage to write too
350 * @param value the value to store
352 * @return 0 for success, or a Kerberos 5 error code on failure.
354 * @ingroup krb5_storage
357 krb5_error_code KRB5_LIB_FUNCTION
358 krb5_store_uint32(krb5_storage *sp,
359 uint32_t value)
361 return krb5_store_int32(sp, (int32_t)value);
364 static krb5_error_code
365 krb5_ret_int(krb5_storage *sp,
366 int32_t *value,
367 size_t len)
369 int ret;
370 unsigned char v[4];
371 unsigned long w;
372 ret = sp->fetch(sp, v, len);
373 if(ret != len)
374 return (ret<0)?errno:sp->eof_code;
375 _krb5_get_int(v, &w, len);
376 *value = w;
377 return 0;
381 * Read a int32 from storage, byte order is controlled by the settings
382 * on the storage, see krb5_storage_set_byteorder().
384 * @param sp the storage to write too
385 * @param value the value read from the buffer
387 * @return 0 for success, or a Kerberos 5 error code on failure.
389 * @ingroup krb5_storage
392 krb5_error_code KRB5_LIB_FUNCTION
393 krb5_ret_int32(krb5_storage *sp,
394 int32_t *value)
396 krb5_error_code ret = krb5_ret_int(sp, value, 4);
397 if(ret)
398 return ret;
399 if(BYTEORDER_IS_HOST(sp))
400 *value = htonl(*value);
401 else if(BYTEORDER_IS_LE(sp))
402 *value = bswap32(*value);
403 return 0;
407 * Read a uint32 from storage, byte order is controlled by the settings
408 * on the storage, see krb5_storage_set_byteorder().
410 * @param sp the storage to write too
411 * @param value the value read from the buffer
413 * @return 0 for success, or a Kerberos 5 error code on failure.
415 * @ingroup krb5_storage
418 krb5_error_code KRB5_LIB_FUNCTION
419 krb5_ret_uint32(krb5_storage *sp,
420 uint32_t *value)
422 krb5_error_code ret;
423 int32_t v;
425 ret = krb5_ret_int32(sp, &v);
426 if (ret == 0)
427 *value = (uint32_t)v;
429 return ret;
433 * Store a int16 to storage, byte order is controlled by the settings
434 * on the storage, see krb5_storage_set_byteorder().
436 * @param sp the storage to write too
437 * @param value the value to store
439 * @return 0 for success, or a Kerberos 5 error code on failure.
441 * @ingroup krb5_storage
444 krb5_error_code KRB5_LIB_FUNCTION
445 krb5_store_int16(krb5_storage *sp,
446 int16_t value)
448 if(BYTEORDER_IS_HOST(sp))
449 value = htons(value);
450 else if(BYTEORDER_IS_LE(sp))
451 value = bswap16(value);
452 return krb5_store_int(sp, value, 2);
456 * Store a uint16 to storage, byte order is controlled by the settings
457 * on the storage, see krb5_storage_set_byteorder().
459 * @param sp the storage to write too
460 * @param value the value to store
462 * @return 0 for success, or a Kerberos 5 error code on failure.
464 * @ingroup krb5_storage
467 krb5_error_code KRB5_LIB_FUNCTION
468 krb5_store_uint16(krb5_storage *sp,
469 uint16_t value)
471 return krb5_store_int16(sp, (int16_t)value);
475 * Read a int16 from storage, byte order is controlled by the settings
476 * on the storage, see krb5_storage_set_byteorder().
478 * @param sp the storage to write too
479 * @param value the value read from the buffer
481 * @return 0 for success, or a Kerberos 5 error code on failure.
483 * @ingroup krb5_storage
485 krb5_error_code KRB5_LIB_FUNCTION
486 krb5_ret_int16(krb5_storage *sp,
487 int16_t *value)
489 int32_t v;
490 int ret;
491 ret = krb5_ret_int(sp, &v, 2);
492 if(ret)
493 return ret;
494 *value = v;
495 if(BYTEORDER_IS_HOST(sp))
496 *value = htons(*value);
497 else if(BYTEORDER_IS_LE(sp))
498 *value = bswap16(*value);
499 return 0;
503 * Read a int16 from storage, byte order is controlled by the settings
504 * on the storage, see krb5_storage_set_byteorder().
506 * @param sp the storage to write too
507 * @param value the value read from the buffer
509 * @return 0 for success, or a Kerberos 5 error code on failure.
511 * @ingroup krb5_storage
514 krb5_error_code KRB5_LIB_FUNCTION
515 krb5_ret_uint16(krb5_storage *sp,
516 uint16_t *value)
518 krb5_error_code ret;
519 int16_t v;
521 ret = krb5_ret_int16(sp, &v);
522 if (ret == 0)
523 *value = (uint16_t)v;
525 return ret;
529 * Store a int8 to storage.
531 * @param sp the storage to write too
532 * @param value the value to store
534 * @return 0 for success, or a Kerberos 5 error code on failure.
536 * @ingroup krb5_storage
539 krb5_error_code KRB5_LIB_FUNCTION
540 krb5_store_int8(krb5_storage *sp,
541 int8_t value)
543 int ret;
545 ret = sp->store(sp, &value, sizeof(value));
546 if (ret != sizeof(value))
547 return (ret<0)?errno:sp->eof_code;
548 return 0;
552 * Store a uint8 to storage.
554 * @param sp the storage to write too
555 * @param value the value to store
557 * @return 0 for success, or a Kerberos 5 error code on failure.
559 * @ingroup krb5_storage
562 krb5_error_code KRB5_LIB_FUNCTION
563 krb5_store_uint8(krb5_storage *sp,
564 uint8_t value)
566 return krb5_store_int8(sp, (int8_t)value);
570 * Read a int8 from storage
572 * @param sp the storage to write too
573 * @param value the value read from the buffer
575 * @return 0 for success, or a Kerberos 5 error code on failure.
577 * @ingroup krb5_storage
580 krb5_error_code KRB5_LIB_FUNCTION
581 krb5_ret_int8(krb5_storage *sp,
582 int8_t *value)
584 int ret;
586 ret = sp->fetch(sp, value, sizeof(*value));
587 if (ret != sizeof(*value))
588 return (ret<0)?errno:sp->eof_code;
589 return 0;
593 * Read a uint8 from storage
595 * @param sp the storage to write too
596 * @param value the value read from the buffer
598 * @return 0 for success, or a Kerberos 5 error code on failure.
600 * @ingroup krb5_storage
603 krb5_error_code KRB5_LIB_FUNCTION
604 krb5_ret_uint8(krb5_storage *sp,
605 uint8_t *value)
607 krb5_error_code ret;
608 int8_t v;
610 ret = krb5_ret_int8(sp, &v);
611 if (ret == 0)
612 *value = (uint8_t)v;
614 return ret;
618 * Store a data to the storage.
620 * @param sp the storage buffer to write to
621 * @param data the buffer to store.
623 * @return 0 on success, a Kerberos 5 error code on failure.
625 * @ingroup krb5_storage
628 krb5_error_code KRB5_LIB_FUNCTION
629 krb5_store_data(krb5_storage *sp,
630 krb5_data data)
632 int ret;
633 ret = krb5_store_int32(sp, data.length);
634 if(ret < 0)
635 return ret;
636 ret = sp->store(sp, data.data, data.length);
637 if(ret != data.length){
638 if(ret < 0)
639 return errno;
640 return sp->eof_code;
642 return 0;
646 * Parse a data from the storage.
648 * @param sp the storage buffer to read from
649 * @param data the parsed data
651 * @return 0 on success, a Kerberos 5 error code on failure.
653 * @ingroup krb5_storage
656 krb5_error_code KRB5_LIB_FUNCTION
657 krb5_ret_data(krb5_storage *sp,
658 krb5_data *data)
660 int ret;
661 int32_t size;
663 ret = krb5_ret_int32(sp, &size);
664 if(ret)
665 return ret;
666 ret = krb5_data_alloc (data, size);
667 if (ret)
668 return ret;
669 if (size) {
670 ret = sp->fetch(sp, data->data, size);
671 if(ret != size)
672 return (ret < 0)? errno : sp->eof_code;
674 return 0;
677 krb5_error_code KRB5_LIB_FUNCTION
678 krb5_store_string(krb5_storage *sp, const char *s)
680 krb5_data data;
681 data.length = strlen(s);
682 data.data = rk_UNCONST(s);
683 return krb5_store_data(sp, data);
686 krb5_error_code KRB5_LIB_FUNCTION
687 krb5_ret_string(krb5_storage *sp,
688 char **string)
690 int ret;
691 krb5_data data;
692 ret = krb5_ret_data(sp, &data);
693 if(ret)
694 return ret;
695 *string = realloc(data.data, data.length + 1);
696 if(*string == NULL){
697 free(data.data);
698 return ENOMEM;
700 (*string)[data.length] = 0;
701 return 0;
704 krb5_error_code KRB5_LIB_FUNCTION
705 krb5_store_stringz(krb5_storage *sp, const char *s)
707 size_t len = strlen(s) + 1;
708 ssize_t ret;
710 ret = sp->store(sp, s, len);
711 if(ret != len) {
712 if(ret < 0)
713 return ret;
714 else
715 return sp->eof_code;
717 return 0;
720 krb5_error_code KRB5_LIB_FUNCTION
721 krb5_ret_stringz(krb5_storage *sp,
722 char **string)
724 char c;
725 char *s = NULL;
726 size_t len = 0;
727 ssize_t ret;
729 while((ret = sp->fetch(sp, &c, 1)) == 1){
730 char *tmp;
732 len++;
733 tmp = realloc (s, len);
734 if (tmp == NULL) {
735 free (s);
736 return ENOMEM;
738 s = tmp;
739 s[len - 1] = c;
740 if(c == 0)
741 break;
743 if(ret != 1){
744 free(s);
745 if(ret == 0)
746 return sp->eof_code;
747 return ret;
749 *string = s;
750 return 0;
753 krb5_error_code KRB5_LIB_FUNCTION
754 krb5_store_stringnl(krb5_storage *sp, const char *s)
756 size_t len = strlen(s);
757 ssize_t ret;
759 ret = sp->store(sp, s, len);
760 if(ret != len) {
761 if(ret < 0)
762 return ret;
763 else
764 return sp->eof_code;
766 ret = sp->store(sp, "\n", 1);
767 if(ret != 1) {
768 if(ret < 0)
769 return ret;
770 else
771 return sp->eof_code;
774 return 0;
778 krb5_error_code KRB5_LIB_FUNCTION
779 krb5_ret_stringnl(krb5_storage *sp,
780 char **string)
782 int expect_nl = 0;
783 char c;
784 char *s = NULL;
785 size_t len = 0;
786 ssize_t ret;
788 while((ret = sp->fetch(sp, &c, 1)) == 1){
789 char *tmp;
791 if (c == '\r') {
792 expect_nl = 1;
793 continue;
795 if (expect_nl && c != '\n') {
796 free(s);
797 return KRB5_BADMSGTYPE;
800 len++;
801 tmp = realloc (s, len);
802 if (tmp == NULL) {
803 free (s);
804 return ENOMEM;
806 s = tmp;
807 if(c == '\n') {
808 s[len - 1] = '\0';
809 break;
811 s[len - 1] = c;
813 if(ret != 1){
814 free(s);
815 if(ret == 0)
816 return sp->eof_code;
817 return ret;
819 *string = s;
820 return 0;
824 krb5_error_code KRB5_LIB_FUNCTION
825 krb5_store_principal(krb5_storage *sp,
826 krb5_const_principal p)
828 int i;
829 int ret;
831 if(!krb5_storage_is_flags(sp, KRB5_STORAGE_PRINCIPAL_NO_NAME_TYPE)) {
832 ret = krb5_store_int32(sp, p->name.name_type);
833 if(ret) return ret;
835 if(krb5_storage_is_flags(sp, KRB5_STORAGE_PRINCIPAL_WRONG_NUM_COMPONENTS))
836 ret = krb5_store_int32(sp, p->name.name_string.len + 1);
837 else
838 ret = krb5_store_int32(sp, p->name.name_string.len);
840 if(ret) return ret;
841 ret = krb5_store_string(sp, p->realm);
842 if(ret) return ret;
843 for(i = 0; i < p->name.name_string.len; i++){
844 ret = krb5_store_string(sp, p->name.name_string.val[i]);
845 if(ret) return ret;
847 return 0;
850 krb5_error_code KRB5_LIB_FUNCTION
851 krb5_ret_principal(krb5_storage *sp,
852 krb5_principal *princ)
854 int i;
855 int ret;
856 krb5_principal p;
857 int32_t type;
858 int32_t ncomp;
860 p = calloc(1, sizeof(*p));
861 if(p == NULL)
862 return ENOMEM;
864 if(krb5_storage_is_flags(sp, KRB5_STORAGE_PRINCIPAL_NO_NAME_TYPE))
865 type = KRB5_NT_UNKNOWN;
866 else if((ret = krb5_ret_int32(sp, &type))){
867 free(p);
868 return ret;
870 if((ret = krb5_ret_int32(sp, &ncomp))){
871 free(p);
872 return ret;
874 if(krb5_storage_is_flags(sp, KRB5_STORAGE_PRINCIPAL_WRONG_NUM_COMPONENTS))
875 ncomp--;
876 if (ncomp < 0) {
877 free(p);
878 return EINVAL;
880 p->name.name_type = type;
881 p->name.name_string.len = ncomp;
882 ret = krb5_ret_string(sp, &p->realm);
883 if(ret) {
884 free(p);
885 return ret;
887 p->name.name_string.val = calloc(ncomp, sizeof(*p->name.name_string.val));
888 if(p->name.name_string.val == NULL && ncomp != 0){
889 free(p->realm);
890 free(p);
891 return ENOMEM;
893 for(i = 0; i < ncomp; i++){
894 ret = krb5_ret_string(sp, &p->name.name_string.val[i]);
895 if(ret) {
896 while (i >= 0)
897 free(p->name.name_string.val[i--]);
898 free(p->realm);
899 free(p);
900 return ret;
903 *princ = p;
904 return 0;
908 * Store a keyblock to the storage.
910 * @param sp the storage buffer to write to
911 * @param p the keyblock to write
913 * @return 0 on success, a Kerberos 5 error code on failure.
915 * @ingroup krb5_storage
918 krb5_error_code KRB5_LIB_FUNCTION
919 krb5_store_keyblock(krb5_storage *sp, krb5_keyblock p)
921 int ret;
922 ret = krb5_store_int16(sp, p.keytype);
923 if(ret) return ret;
925 if(krb5_storage_is_flags(sp, KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE)){
926 /* this should really be enctype, but it is the same as
927 keytype nowadays */
928 ret = krb5_store_int16(sp, p.keytype);
929 if(ret) return ret;
932 ret = krb5_store_data(sp, p.keyvalue);
933 return ret;
937 * Read a keyblock from the storage.
939 * @param sp the storage buffer to write to
940 * @param p the keyblock read from storage, free using krb5_free_keyblock()
942 * @return 0 on success, a Kerberos 5 error code on failure.
944 * @ingroup krb5_storage
947 krb5_error_code KRB5_LIB_FUNCTION
948 krb5_ret_keyblock(krb5_storage *sp, krb5_keyblock *p)
950 int ret;
951 int16_t tmp;
953 ret = krb5_ret_int16(sp, &tmp);
954 if(ret) return ret;
955 p->keytype = tmp;
957 if(krb5_storage_is_flags(sp, KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE)){
958 ret = krb5_ret_int16(sp, &tmp);
959 if(ret) return ret;
962 ret = krb5_ret_data(sp, &p->keyvalue);
963 return ret;
967 * Write a times block to storage.
969 * @param sp the storage buffer to write to
970 * @param times the times block to write.
972 * @return 0 on success, a Kerberos 5 error code on failure.
974 * @ingroup krb5_storage
977 krb5_error_code KRB5_LIB_FUNCTION
978 krb5_store_times(krb5_storage *sp, krb5_times times)
980 int ret;
981 ret = krb5_store_int32(sp, times.authtime);
982 if(ret) return ret;
983 ret = krb5_store_int32(sp, times.starttime);
984 if(ret) return ret;
985 ret = krb5_store_int32(sp, times.endtime);
986 if(ret) return ret;
987 ret = krb5_store_int32(sp, times.renew_till);
988 return ret;
992 * Read a times block from the storage.
994 * @param sp the storage buffer to write to
995 * @param times the times block read from storage
997 * @return 0 on success, a Kerberos 5 error code on failure.
999 * @ingroup krb5_storage
1002 krb5_error_code KRB5_LIB_FUNCTION
1003 krb5_ret_times(krb5_storage *sp, krb5_times *times)
1005 int ret;
1006 int32_t tmp;
1007 ret = krb5_ret_int32(sp, &tmp);
1008 times->authtime = tmp;
1009 if(ret) return ret;
1010 ret = krb5_ret_int32(sp, &tmp);
1011 times->starttime = tmp;
1012 if(ret) return ret;
1013 ret = krb5_ret_int32(sp, &tmp);
1014 times->endtime = tmp;
1015 if(ret) return ret;
1016 ret = krb5_ret_int32(sp, &tmp);
1017 times->renew_till = tmp;
1018 return ret;
1021 krb5_error_code KRB5_LIB_FUNCTION
1022 krb5_store_address(krb5_storage *sp, krb5_address p)
1024 int ret;
1025 ret = krb5_store_int16(sp, p.addr_type);
1026 if(ret) return ret;
1027 ret = krb5_store_data(sp, p.address);
1028 return ret;
1031 krb5_error_code KRB5_LIB_FUNCTION
1032 krb5_ret_address(krb5_storage *sp, krb5_address *adr)
1034 int16_t t;
1035 int ret;
1036 ret = krb5_ret_int16(sp, &t);
1037 if(ret) return ret;
1038 adr->addr_type = t;
1039 ret = krb5_ret_data(sp, &adr->address);
1040 return ret;
1043 krb5_error_code KRB5_LIB_FUNCTION
1044 krb5_store_addrs(krb5_storage *sp, krb5_addresses p)
1046 int i;
1047 int ret;
1048 ret = krb5_store_int32(sp, p.len);
1049 if(ret) return ret;
1050 for(i = 0; i<p.len; i++){
1051 ret = krb5_store_address(sp, p.val[i]);
1052 if(ret) break;
1054 return ret;
1057 krb5_error_code KRB5_LIB_FUNCTION
1058 krb5_ret_addrs(krb5_storage *sp, krb5_addresses *adr)
1060 int i;
1061 int ret;
1062 int32_t tmp;
1064 ret = krb5_ret_int32(sp, &tmp);
1065 if(ret) return ret;
1066 adr->len = tmp;
1067 ALLOC(adr->val, adr->len);
1068 if (adr->val == NULL && adr->len != 0)
1069 return ENOMEM;
1070 for(i = 0; i < adr->len; i++){
1071 ret = krb5_ret_address(sp, &adr->val[i]);
1072 if(ret) break;
1074 return ret;
1077 krb5_error_code KRB5_LIB_FUNCTION
1078 krb5_store_authdata(krb5_storage *sp, krb5_authdata auth)
1080 krb5_error_code ret;
1081 int i;
1082 ret = krb5_store_int32(sp, auth.len);
1083 if(ret) return ret;
1084 for(i = 0; i < auth.len; i++){
1085 ret = krb5_store_int16(sp, auth.val[i].ad_type);
1086 if(ret) break;
1087 ret = krb5_store_data(sp, auth.val[i].ad_data);
1088 if(ret) break;
1090 return 0;
1093 krb5_error_code KRB5_LIB_FUNCTION
1094 krb5_ret_authdata(krb5_storage *sp, krb5_authdata *auth)
1096 krb5_error_code ret;
1097 int32_t tmp;
1098 int16_t tmp2;
1099 int i;
1100 ret = krb5_ret_int32(sp, &tmp);
1101 if(ret) return ret;
1102 ALLOC_SEQ(auth, tmp);
1103 if (auth->val == NULL && tmp != 0)
1104 return ENOMEM;
1105 for(i = 0; i < tmp; i++){
1106 ret = krb5_ret_int16(sp, &tmp2);
1107 if(ret) break;
1108 auth->val[i].ad_type = tmp2;
1109 ret = krb5_ret_data(sp, &auth->val[i].ad_data);
1110 if(ret) break;
1112 return ret;
1115 static int32_t
1116 bitswap32(int32_t b)
1118 int32_t r = 0;
1119 int i;
1120 for (i = 0; i < 32; i++) {
1121 r = r << 1 | (b & 1);
1122 b = b >> 1;
1124 return r;
1132 krb5_error_code KRB5_LIB_FUNCTION
1133 krb5_store_creds(krb5_storage *sp, krb5_creds *creds)
1135 int ret;
1137 ret = krb5_store_principal(sp, creds->client);
1138 if(ret)
1139 return ret;
1140 ret = krb5_store_principal(sp, creds->server);
1141 if(ret)
1142 return ret;
1143 ret = krb5_store_keyblock(sp, creds->session);
1144 if(ret)
1145 return ret;
1146 ret = krb5_store_times(sp, creds->times);
1147 if(ret)
1148 return ret;
1149 ret = krb5_store_int8(sp, creds->second_ticket.length != 0); /* is_skey */
1150 if(ret)
1151 return ret;
1153 if(krb5_storage_is_flags(sp, KRB5_STORAGE_CREDS_FLAGS_WRONG_BITORDER))
1154 ret = krb5_store_int32(sp, creds->flags.i);
1155 else
1156 ret = krb5_store_int32(sp, bitswap32(TicketFlags2int(creds->flags.b)));
1157 if(ret)
1158 return ret;
1160 ret = krb5_store_addrs(sp, creds->addresses);
1161 if(ret)
1162 return ret;
1163 ret = krb5_store_authdata(sp, creds->authdata);
1164 if(ret)
1165 return ret;
1166 ret = krb5_store_data(sp, creds->ticket);
1167 if(ret)
1168 return ret;
1169 ret = krb5_store_data(sp, creds->second_ticket);
1170 return ret;
1173 krb5_error_code KRB5_LIB_FUNCTION
1174 krb5_ret_creds(krb5_storage *sp, krb5_creds *creds)
1176 krb5_error_code ret;
1177 int8_t dummy8;
1178 int32_t dummy32;
1180 memset(creds, 0, sizeof(*creds));
1181 ret = krb5_ret_principal (sp, &creds->client);
1182 if(ret) goto cleanup;
1183 ret = krb5_ret_principal (sp, &creds->server);
1184 if(ret) goto cleanup;
1185 ret = krb5_ret_keyblock (sp, &creds->session);
1186 if(ret) goto cleanup;
1187 ret = krb5_ret_times (sp, &creds->times);
1188 if(ret) goto cleanup;
1189 ret = krb5_ret_int8 (sp, &dummy8);
1190 if(ret) goto cleanup;
1191 ret = krb5_ret_int32 (sp, &dummy32);
1192 if(ret) goto cleanup;
1194 * Runtime detect the what is the higher bits of the bitfield. If
1195 * any of the higher bits are set in the input data, it's either a
1196 * new ticket flag (and this code need to be removed), or it's a
1197 * MIT cache (or new Heimdal cache), lets change it to our current
1198 * format.
1201 uint32_t mask = 0xffff0000;
1202 creds->flags.i = 0;
1203 creds->flags.b.anonymous = 1;
1204 if (creds->flags.i & mask)
1205 mask = ~mask;
1206 if (dummy32 & mask)
1207 dummy32 = bitswap32(dummy32);
1209 creds->flags.i = dummy32;
1210 ret = krb5_ret_addrs (sp, &creds->addresses);
1211 if(ret) goto cleanup;
1212 ret = krb5_ret_authdata (sp, &creds->authdata);
1213 if(ret) goto cleanup;
1214 ret = krb5_ret_data (sp, &creds->ticket);
1215 if(ret) goto cleanup;
1216 ret = krb5_ret_data (sp, &creds->second_ticket);
1217 cleanup:
1218 if(ret) {
1219 #if 0
1220 krb5_free_cred_contents(context, creds); /* XXX */
1221 #endif
1223 return ret;
1226 #define SC_CLIENT_PRINCIPAL 0x0001
1227 #define SC_SERVER_PRINCIPAL 0x0002
1228 #define SC_SESSION_KEY 0x0004
1229 #define SC_TICKET 0x0008
1230 #define SC_SECOND_TICKET 0x0010
1231 #define SC_AUTHDATA 0x0020
1232 #define SC_ADDRESSES 0x0040
1238 krb5_error_code KRB5_LIB_FUNCTION
1239 krb5_store_creds_tag(krb5_storage *sp, krb5_creds *creds)
1241 int ret;
1242 int32_t header = 0;
1244 if (creds->client)
1245 header |= SC_CLIENT_PRINCIPAL;
1246 if (creds->server)
1247 header |= SC_SERVER_PRINCIPAL;
1248 if (creds->session.keytype != ETYPE_NULL)
1249 header |= SC_SESSION_KEY;
1250 if (creds->ticket.data)
1251 header |= SC_TICKET;
1252 if (creds->second_ticket.length)
1253 header |= SC_SECOND_TICKET;
1254 if (creds->authdata.len)
1255 header |= SC_AUTHDATA;
1256 if (creds->addresses.len)
1257 header |= SC_ADDRESSES;
1259 ret = krb5_store_int32(sp, header);
1260 if (ret)
1261 return ret;
1263 if (creds->client) {
1264 ret = krb5_store_principal(sp, creds->client);
1265 if(ret)
1266 return ret;
1269 if (creds->server) {
1270 ret = krb5_store_principal(sp, creds->server);
1271 if(ret)
1272 return ret;
1275 if (creds->session.keytype != ETYPE_NULL) {
1276 ret = krb5_store_keyblock(sp, creds->session);
1277 if(ret)
1278 return ret;
1281 ret = krb5_store_times(sp, creds->times);
1282 if(ret)
1283 return ret;
1284 ret = krb5_store_int8(sp, creds->second_ticket.length != 0); /* is_skey */
1285 if(ret)
1286 return ret;
1288 ret = krb5_store_int32(sp, bitswap32(TicketFlags2int(creds->flags.b)));
1289 if(ret)
1290 return ret;
1292 if (creds->addresses.len) {
1293 ret = krb5_store_addrs(sp, creds->addresses);
1294 if(ret)
1295 return ret;
1298 if (creds->authdata.len) {
1299 ret = krb5_store_authdata(sp, creds->authdata);
1300 if(ret)
1301 return ret;
1304 if (creds->ticket.data) {
1305 ret = krb5_store_data(sp, creds->ticket);
1306 if(ret)
1307 return ret;
1310 if (creds->second_ticket.data) {
1311 ret = krb5_store_data(sp, creds->second_ticket);
1312 if (ret)
1313 return ret;
1316 return ret;
1319 krb5_error_code KRB5_LIB_FUNCTION
1320 krb5_ret_creds_tag(krb5_storage *sp,
1321 krb5_creds *creds)
1323 krb5_error_code ret;
1324 int8_t dummy8;
1325 int32_t dummy32, header;
1327 memset(creds, 0, sizeof(*creds));
1329 ret = krb5_ret_int32 (sp, &header);
1330 if (ret) goto cleanup;
1332 if (header & SC_CLIENT_PRINCIPAL) {
1333 ret = krb5_ret_principal (sp, &creds->client);
1334 if(ret) goto cleanup;
1336 if (header & SC_SERVER_PRINCIPAL) {
1337 ret = krb5_ret_principal (sp, &creds->server);
1338 if(ret) goto cleanup;
1340 if (header & SC_SESSION_KEY) {
1341 ret = krb5_ret_keyblock (sp, &creds->session);
1342 if(ret) goto cleanup;
1344 ret = krb5_ret_times (sp, &creds->times);
1345 if(ret) goto cleanup;
1346 ret = krb5_ret_int8 (sp, &dummy8);
1347 if(ret) goto cleanup;
1348 ret = krb5_ret_int32 (sp, &dummy32);
1349 if(ret) goto cleanup;
1351 * Runtime detect the what is the higher bits of the bitfield. If
1352 * any of the higher bits are set in the input data, it's either a
1353 * new ticket flag (and this code need to be removed), or it's a
1354 * MIT cache (or new Heimdal cache), lets change it to our current
1355 * format.
1358 uint32_t mask = 0xffff0000;
1359 creds->flags.i = 0;
1360 creds->flags.b.anonymous = 1;
1361 if (creds->flags.i & mask)
1362 mask = ~mask;
1363 if (dummy32 & mask)
1364 dummy32 = bitswap32(dummy32);
1366 creds->flags.i = dummy32;
1367 if (header & SC_ADDRESSES) {
1368 ret = krb5_ret_addrs (sp, &creds->addresses);
1369 if(ret) goto cleanup;
1371 if (header & SC_AUTHDATA) {
1372 ret = krb5_ret_authdata (sp, &creds->authdata);
1373 if(ret) goto cleanup;
1375 if (header & SC_TICKET) {
1376 ret = krb5_ret_data (sp, &creds->ticket);
1377 if(ret) goto cleanup;
1379 if (header & SC_SECOND_TICKET) {
1380 ret = krb5_ret_data (sp, &creds->second_ticket);
1381 if(ret) goto cleanup;
1384 cleanup:
1385 if(ret) {
1386 #if 0
1387 krb5_free_cred_contents(context, creds); /* XXX */
1388 #endif
1390 return ret;