2 * Copyright (c) 1997-2004 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 RCSID("$Id: store.c,v 1.51 2006/04/07 22:23:20 lha Exp $");
39 #define BYTEORDER_IS(SP, V) (((SP)->flags & KRB5_STORAGE_BYTEORDER_MASK) == (V))
40 #define BYTEORDER_IS_LE(SP) BYTEORDER_IS((SP), KRB5_STORAGE_BYTEORDER_LE)
41 #define BYTEORDER_IS_BE(SP) BYTEORDER_IS((SP), KRB5_STORAGE_BYTEORDER_BE)
42 #define BYTEORDER_IS_HOST(SP) (BYTEORDER_IS((SP), KRB5_STORAGE_BYTEORDER_HOST) || \
43 krb5_storage_is_flags((SP), KRB5_STORAGE_HOST_BYTEORDER))
45 void KRB5_LIB_FUNCTION
46 krb5_storage_set_flags(krb5_storage *sp, krb5_flags flags)
51 void KRB5_LIB_FUNCTION
52 krb5_storage_clear_flags(krb5_storage *sp, krb5_flags flags)
57 krb5_boolean KRB5_LIB_FUNCTION
58 krb5_storage_is_flags(krb5_storage *sp, krb5_flags flags)
60 return (sp->flags & flags) == flags;
63 void KRB5_LIB_FUNCTION
64 krb5_storage_set_byteorder(krb5_storage *sp, krb5_flags byteorder)
66 sp->flags &= ~KRB5_STORAGE_BYTEORDER_MASK;
67 sp->flags |= byteorder;
70 krb5_flags KRB5_LIB_FUNCTION
71 krb5_storage_get_byteorder(krb5_storage *sp, krb5_flags byteorder)
73 return sp->flags & KRB5_STORAGE_BYTEORDER_MASK;
76 off_t KRB5_LIB_FUNCTION
77 krb5_storage_seek(krb5_storage *sp, off_t offset, int whence)
79 return (*sp->seek)(sp, offset, whence);
82 krb5_ssize_t KRB5_LIB_FUNCTION
83 krb5_storage_read(krb5_storage *sp, void *buf, size_t len)
85 return sp->fetch(sp, buf, len);
88 krb5_ssize_t KRB5_LIB_FUNCTION
89 krb5_storage_write(krb5_storage *sp, const void *buf, size_t len)
91 return sp->store(sp, buf, len);
94 void KRB5_LIB_FUNCTION
95 krb5_storage_set_eof_code(krb5_storage *sp, int code)
100 krb5_ssize_t KRB5_LIB_FUNCTION
101 _krb5_put_int(void *buffer, unsigned long value, size_t size)
103 unsigned char *p = buffer;
105 for (i = size - 1; i >= 0; i--) {
112 krb5_ssize_t KRB5_LIB_FUNCTION
113 _krb5_get_int(void *buffer, unsigned long *value, size_t size)
115 unsigned char *p = buffer;
118 for (i = 0; i < size; i++)
124 krb5_error_code KRB5_LIB_FUNCTION
125 krb5_storage_free(krb5_storage *sp)
134 krb5_error_code KRB5_LIB_FUNCTION
135 krb5_storage_to_data(krb5_storage *sp, krb5_data *data)
141 pos = sp->seek(sp, 0, SEEK_CUR);
142 size = (size_t)sp->seek(sp, 0, SEEK_END);
143 ret = krb5_data_alloc (data, size);
145 sp->seek(sp, pos, SEEK_SET);
149 sp->seek(sp, 0, SEEK_SET);
150 sp->fetch(sp, data->data, data->length);
151 sp->seek(sp, pos, SEEK_SET);
156 static krb5_error_code
157 krb5_store_int(krb5_storage *sp,
166 _krb5_put_int(v, value, len);
167 ret = sp->store(sp, v, len);
169 return (ret<0)?errno:sp->eof_code;
173 krb5_error_code KRB5_LIB_FUNCTION
174 krb5_store_int32(krb5_storage *sp,
177 if(BYTEORDER_IS_HOST(sp))
178 value = htonl(value);
179 else if(BYTEORDER_IS_LE(sp))
180 value = bswap32(value);
181 return krb5_store_int(sp, value, 4);
184 static krb5_error_code
185 krb5_ret_int(krb5_storage *sp,
192 ret = sp->fetch(sp, v, len);
194 return (ret<0)?errno:sp->eof_code;
195 _krb5_get_int(v, &w, len);
200 krb5_error_code KRB5_LIB_FUNCTION
201 krb5_ret_int32(krb5_storage *sp,
204 krb5_error_code ret = krb5_ret_int(sp, value, 4);
207 if(BYTEORDER_IS_HOST(sp))
208 *value = htonl(*value);
209 else if(BYTEORDER_IS_LE(sp))
210 *value = bswap32(*value);
214 krb5_error_code KRB5_LIB_FUNCTION
215 krb5_store_int16(krb5_storage *sp,
218 if(BYTEORDER_IS_HOST(sp))
219 value = htons(value);
220 else if(BYTEORDER_IS_LE(sp))
221 value = bswap16(value);
222 return krb5_store_int(sp, value, 2);
225 krb5_error_code KRB5_LIB_FUNCTION
226 krb5_ret_int16(krb5_storage *sp,
231 ret = krb5_ret_int(sp, &v, 2);
235 if(BYTEORDER_IS_HOST(sp))
236 *value = htons(*value);
237 else if(BYTEORDER_IS_LE(sp))
238 *value = bswap16(*value);
242 krb5_error_code KRB5_LIB_FUNCTION
243 krb5_store_int8(krb5_storage *sp,
248 ret = sp->store(sp, &value, sizeof(value));
249 if (ret != sizeof(value))
250 return (ret<0)?errno:sp->eof_code;
254 krb5_error_code KRB5_LIB_FUNCTION
255 krb5_ret_int8(krb5_storage *sp,
260 ret = sp->fetch(sp, value, sizeof(*value));
261 if (ret != sizeof(*value))
262 return (ret<0)?errno:sp->eof_code;
266 krb5_error_code KRB5_LIB_FUNCTION
267 krb5_store_data(krb5_storage *sp,
271 ret = krb5_store_int32(sp, data.length);
274 ret = sp->store(sp, data.data, data.length);
275 if(ret != data.length){
283 krb5_error_code KRB5_LIB_FUNCTION
284 krb5_ret_data(krb5_storage *sp,
290 ret = krb5_ret_int32(sp, &size);
293 ret = krb5_data_alloc (data, size);
297 ret = sp->fetch(sp, data->data, size);
299 return (ret < 0)? errno : sp->eof_code;
304 krb5_error_code KRB5_LIB_FUNCTION
305 krb5_store_string(krb5_storage *sp, const char *s)
308 data.length = strlen(s);
309 data.data = rk_UNCONST(s);
310 return krb5_store_data(sp, data);
313 krb5_error_code KRB5_LIB_FUNCTION
314 krb5_ret_string(krb5_storage *sp,
319 ret = krb5_ret_data(sp, &data);
322 *string = realloc(data.data, data.length + 1);
327 (*string)[data.length] = 0;
331 krb5_error_code KRB5_LIB_FUNCTION
332 krb5_store_stringz(krb5_storage *sp, const char *s)
334 size_t len = strlen(s) + 1;
337 ret = sp->store(sp, s, len);
347 krb5_error_code KRB5_LIB_FUNCTION
348 krb5_ret_stringz(krb5_storage *sp,
356 while((ret = sp->fetch(sp, &c, 1)) == 1){
360 tmp = realloc (s, len);
381 krb5_error_code KRB5_LIB_FUNCTION
382 krb5_store_principal(krb5_storage *sp,
388 if(!krb5_storage_is_flags(sp, KRB5_STORAGE_PRINCIPAL_NO_NAME_TYPE)) {
389 ret = krb5_store_int32(sp, p->name.name_type);
392 if(krb5_storage_is_flags(sp, KRB5_STORAGE_PRINCIPAL_WRONG_NUM_COMPONENTS))
393 ret = krb5_store_int32(sp, p->name.name_string.len + 1);
395 ret = krb5_store_int32(sp, p->name.name_string.len);
398 ret = krb5_store_string(sp, p->realm);
400 for(i = 0; i < p->name.name_string.len; i++){
401 ret = krb5_store_string(sp, p->name.name_string.val[i]);
407 krb5_error_code KRB5_LIB_FUNCTION
408 krb5_ret_principal(krb5_storage *sp,
409 krb5_principal *princ)
417 p = calloc(1, sizeof(*p));
421 if(krb5_storage_is_flags(sp, KRB5_STORAGE_PRINCIPAL_NO_NAME_TYPE))
422 type = KRB5_NT_UNKNOWN;
423 else if((ret = krb5_ret_int32(sp, &type))){
427 if((ret = krb5_ret_int32(sp, &ncomp))){
431 if(krb5_storage_is_flags(sp, KRB5_STORAGE_PRINCIPAL_WRONG_NUM_COMPONENTS))
437 p->name.name_type = type;
438 p->name.name_string.len = ncomp;
439 ret = krb5_ret_string(sp, &p->realm);
444 p->name.name_string.val = calloc(ncomp, sizeof(*p->name.name_string.val));
445 if(p->name.name_string.val == NULL && ncomp != 0){
449 for(i = 0; i < ncomp; i++){
450 ret = krb5_ret_string(sp, &p->name.name_string.val[i]);
453 free(p->name.name_string.val[i--]);
463 krb5_error_code KRB5_LIB_FUNCTION
464 krb5_store_keyblock(krb5_storage *sp, krb5_keyblock p)
467 ret = krb5_store_int16(sp, p.keytype);
470 if(krb5_storage_is_flags(sp, KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE)){
471 /* this should really be enctype, but it is the same as
473 ret = krb5_store_int16(sp, p.keytype);
477 ret = krb5_store_data(sp, p.keyvalue);
481 krb5_error_code KRB5_LIB_FUNCTION
482 krb5_ret_keyblock(krb5_storage *sp, krb5_keyblock *p)
487 ret = krb5_ret_int16(sp, &tmp);
491 if(krb5_storage_is_flags(sp, KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE)){
492 ret = krb5_ret_int16(sp, &tmp);
496 ret = krb5_ret_data(sp, &p->keyvalue);
500 krb5_error_code KRB5_LIB_FUNCTION
501 krb5_store_times(krb5_storage *sp, krb5_times times)
504 ret = krb5_store_int32(sp, times.authtime);
506 ret = krb5_store_int32(sp, times.starttime);
508 ret = krb5_store_int32(sp, times.endtime);
510 ret = krb5_store_int32(sp, times.renew_till);
514 krb5_error_code KRB5_LIB_FUNCTION
515 krb5_ret_times(krb5_storage *sp, krb5_times *times)
519 ret = krb5_ret_int32(sp, &tmp);
520 times->authtime = tmp;
522 ret = krb5_ret_int32(sp, &tmp);
523 times->starttime = tmp;
525 ret = krb5_ret_int32(sp, &tmp);
526 times->endtime = tmp;
528 ret = krb5_ret_int32(sp, &tmp);
529 times->renew_till = tmp;
533 krb5_error_code KRB5_LIB_FUNCTION
534 krb5_store_address(krb5_storage *sp, krb5_address p)
537 ret = krb5_store_int16(sp, p.addr_type);
539 ret = krb5_store_data(sp, p.address);
543 krb5_error_code KRB5_LIB_FUNCTION
544 krb5_ret_address(krb5_storage *sp, krb5_address *adr)
548 ret = krb5_ret_int16(sp, &t);
551 ret = krb5_ret_data(sp, &adr->address);
555 krb5_error_code KRB5_LIB_FUNCTION
556 krb5_store_addrs(krb5_storage *sp, krb5_addresses p)
560 ret = krb5_store_int32(sp, p.len);
562 for(i = 0; i<p.len; i++){
563 ret = krb5_store_address(sp, p.val[i]);
569 krb5_error_code KRB5_LIB_FUNCTION
570 krb5_ret_addrs(krb5_storage *sp, krb5_addresses *adr)
576 ret = krb5_ret_int32(sp, &tmp);
579 ALLOC(adr->val, adr->len);
580 if (adr->val == NULL && adr->len != 0)
582 for(i = 0; i < adr->len; i++){
583 ret = krb5_ret_address(sp, &adr->val[i]);
589 krb5_error_code KRB5_LIB_FUNCTION
590 krb5_store_authdata(krb5_storage *sp, krb5_authdata auth)
594 ret = krb5_store_int32(sp, auth.len);
596 for(i = 0; i < auth.len; i++){
597 ret = krb5_store_int16(sp, auth.val[i].ad_type);
599 ret = krb5_store_data(sp, auth.val[i].ad_data);
605 krb5_error_code KRB5_LIB_FUNCTION
606 krb5_ret_authdata(krb5_storage *sp, krb5_authdata *auth)
612 ret = krb5_ret_int32(sp, &tmp);
614 ALLOC_SEQ(auth, tmp);
615 if (auth->val == NULL && tmp != 0)
617 for(i = 0; i < tmp; i++){
618 ret = krb5_ret_int16(sp, &tmp2);
620 auth->val[i].ad_type = tmp2;
621 ret = krb5_ret_data(sp, &auth->val[i].ad_data);
632 for (i = 0; i < 32; i++) {
633 r = r << 1 | (b & 1);
644 krb5_error_code KRB5_LIB_FUNCTION
645 krb5_store_creds(krb5_storage *sp, krb5_creds *creds)
649 ret = krb5_store_principal(sp, creds->client);
652 ret = krb5_store_principal(sp, creds->server);
655 ret = krb5_store_keyblock(sp, creds->session);
658 ret = krb5_store_times(sp, creds->times);
661 ret = krb5_store_int8(sp, creds->second_ticket.length != 0); /* is_skey */
665 if(krb5_storage_is_flags(sp, KRB5_STORAGE_CREDS_FLAGS_WRONG_BITORDER))
666 ret = krb5_store_int32(sp, creds->flags.i);
668 ret = krb5_store_int32(sp, bitswap32(TicketFlags2int(creds->flags.b)));
672 ret = krb5_store_addrs(sp, creds->addresses);
675 ret = krb5_store_authdata(sp, creds->authdata);
678 ret = krb5_store_data(sp, creds->ticket);
681 ret = krb5_store_data(sp, creds->second_ticket);
685 krb5_error_code KRB5_LIB_FUNCTION
686 krb5_ret_creds(krb5_storage *sp, krb5_creds *creds)
692 memset(creds, 0, sizeof(*creds));
693 ret = krb5_ret_principal (sp, &creds->client);
694 if(ret) goto cleanup;
695 ret = krb5_ret_principal (sp, &creds->server);
696 if(ret) goto cleanup;
697 ret = krb5_ret_keyblock (sp, &creds->session);
698 if(ret) goto cleanup;
699 ret = krb5_ret_times (sp, &creds->times);
700 if(ret) goto cleanup;
701 ret = krb5_ret_int8 (sp, &dummy8);
702 if(ret) goto cleanup;
703 ret = krb5_ret_int32 (sp, &dummy32);
704 if(ret) goto cleanup;
706 * Runtime detect the what is the higher bits of the bitfield. If
707 * any of the higher bits are set in the input data, its either a
708 * new ticket flag (and this code need to be removed), or its a
709 * MIT cache (or new Heimdal cache), lets change it to our current
713 u_int32_t mask = 0xffff0000;
715 creds->flags.b.anonymous = 1;
716 if (creds->flags.i & mask)
719 dummy32 = bitswap32(dummy32);
721 creds->flags.i = dummy32;
722 ret = krb5_ret_addrs (sp, &creds->addresses);
723 if(ret) goto cleanup;
724 ret = krb5_ret_authdata (sp, &creds->authdata);
725 if(ret) goto cleanup;
726 ret = krb5_ret_data (sp, &creds->ticket);
727 if(ret) goto cleanup;
728 ret = krb5_ret_data (sp, &creds->second_ticket);
732 krb5_free_cred_contents(context, creds); /* XXX */
738 #define SC_CLIENT_PRINCIPAL 0x0001
739 #define SC_SERVER_PRINCIPAL 0x0002
740 #define SC_SESSION_KEY 0x0004
741 #define SC_TICKET 0x0008
742 #define SC_SECOND_TICKET 0x0010
743 #define SC_AUTHDATA 0x0020
744 #define SC_ADDRESSES 0x0040
750 krb5_error_code KRB5_LIB_FUNCTION
751 krb5_store_creds_tag(krb5_storage *sp, krb5_creds *creds)
757 header |= SC_CLIENT_PRINCIPAL;
759 header |= SC_SERVER_PRINCIPAL;
760 if (creds->session.keyvalue.data)
761 header |= SC_SESSION_KEY;
762 if (creds->ticket.data)
764 if (creds->second_ticket.length)
765 header |= SC_SECOND_TICKET;
766 if (creds->authdata.len)
767 header |= SC_AUTHDATA;
768 if (creds->addresses.len)
769 header |= SC_ADDRESSES;
771 ret = krb5_store_int32(sp, header);
774 ret = krb5_store_principal(sp, creds->client);
780 ret = krb5_store_principal(sp, creds->server);
785 if (creds->session.keyvalue.data) {
786 ret = krb5_store_keyblock(sp, creds->session);
791 ret = krb5_store_times(sp, creds->times);
794 ret = krb5_store_int8(sp, creds->second_ticket.length != 0); /* is_skey */
798 ret = krb5_store_int32(sp, bitswap32(TicketFlags2int(creds->flags.b)));
802 if (creds->addresses.len) {
803 ret = krb5_store_addrs(sp, creds->addresses);
808 if (creds->authdata.len) {
809 ret = krb5_store_authdata(sp, creds->authdata);
814 if (creds->ticket.data) {
815 ret = krb5_store_data(sp, creds->ticket);
820 if (creds->second_ticket.data) {
821 ret = krb5_store_data(sp, creds->second_ticket);
829 krb5_error_code KRB5_LIB_FUNCTION
830 krb5_ret_creds_tag(krb5_storage *sp,
835 int32_t dummy32, header;
837 memset(creds, 0, sizeof(*creds));
839 ret = krb5_ret_int32 (sp, &header);
840 if (ret) goto cleanup;
842 if (header & SC_CLIENT_PRINCIPAL) {
843 ret = krb5_ret_principal (sp, &creds->client);
844 if(ret) goto cleanup;
846 if (header & SC_SERVER_PRINCIPAL) {
847 ret = krb5_ret_principal (sp, &creds->server);
848 if(ret) goto cleanup;
850 if (header & SC_SESSION_KEY) {
851 ret = krb5_ret_keyblock (sp, &creds->session);
852 if(ret) goto cleanup;
854 ret = krb5_ret_times (sp, &creds->times);
855 if(ret) goto cleanup;
856 ret = krb5_ret_int8 (sp, &dummy8);
857 if(ret) goto cleanup;
858 ret = krb5_ret_int32 (sp, &dummy32);
859 if(ret) goto cleanup;
861 * Runtime detect the what is the higher bits of the bitfield. If
862 * any of the higher bits are set in the input data, its either a
863 * new ticket flag (and this code need to be removed), or its a
864 * MIT cache (or new Heimdal cache), lets change it to our current
868 u_int32_t mask = 0xffff0000;
870 creds->flags.b.anonymous = 1;
871 if (creds->flags.i & mask)
874 dummy32 = bitswap32(dummy32);
876 creds->flags.i = dummy32;
877 if (header & SC_ADDRESSES) {
878 ret = krb5_ret_addrs (sp, &creds->addresses);
879 if(ret) goto cleanup;
881 if (header & SC_AUTHDATA) {
882 ret = krb5_ret_authdata (sp, &creds->authdata);
883 if(ret) goto cleanup;
885 if (header & SC_TICKET) {
886 ret = krb5_ret_data (sp, &creds->ticket);
887 if(ret) goto cleanup;
889 if (header & SC_SECOND_TICKET) {
890 ret = krb5_ret_data (sp, &creds->second_ticket);
891 if(ret) goto cleanup;
897 krb5_free_cred_contents(context, creds); /* XXX */