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.50 2005/06/17 04:36:33 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))
433 p->name.name_type = type;
434 p->name.name_string.len = ncomp;
435 ret = krb5_ret_string(sp, &p->realm);
437 p->name.name_string.val = calloc(ncomp, sizeof(*p->name.name_string.val));
438 if(p->name.name_string.val == NULL){
442 for(i = 0; i < ncomp; i++){
443 ret = krb5_ret_string(sp, &p->name.name_string.val[i]);
444 if(ret) return ret; /* XXX */
450 krb5_error_code KRB5_LIB_FUNCTION
451 krb5_store_keyblock(krb5_storage *sp, krb5_keyblock p)
454 ret = krb5_store_int16(sp, p.keytype);
457 if(krb5_storage_is_flags(sp, KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE)){
458 /* this should really be enctype, but it is the same as
460 ret = krb5_store_int16(sp, p.keytype);
464 ret = krb5_store_data(sp, p.keyvalue);
468 krb5_error_code KRB5_LIB_FUNCTION
469 krb5_ret_keyblock(krb5_storage *sp, krb5_keyblock *p)
474 ret = krb5_ret_int16(sp, &tmp);
478 if(krb5_storage_is_flags(sp, KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE)){
479 ret = krb5_ret_int16(sp, &tmp);
483 ret = krb5_ret_data(sp, &p->keyvalue);
487 krb5_error_code KRB5_LIB_FUNCTION
488 krb5_store_times(krb5_storage *sp, krb5_times times)
491 ret = krb5_store_int32(sp, times.authtime);
493 ret = krb5_store_int32(sp, times.starttime);
495 ret = krb5_store_int32(sp, times.endtime);
497 ret = krb5_store_int32(sp, times.renew_till);
501 krb5_error_code KRB5_LIB_FUNCTION
502 krb5_ret_times(krb5_storage *sp, krb5_times *times)
506 ret = krb5_ret_int32(sp, &tmp);
507 times->authtime = tmp;
509 ret = krb5_ret_int32(sp, &tmp);
510 times->starttime = tmp;
512 ret = krb5_ret_int32(sp, &tmp);
513 times->endtime = tmp;
515 ret = krb5_ret_int32(sp, &tmp);
516 times->renew_till = tmp;
520 krb5_error_code KRB5_LIB_FUNCTION
521 krb5_store_address(krb5_storage *sp, krb5_address p)
524 ret = krb5_store_int16(sp, p.addr_type);
526 ret = krb5_store_data(sp, p.address);
530 krb5_error_code KRB5_LIB_FUNCTION
531 krb5_ret_address(krb5_storage *sp, krb5_address *adr)
535 ret = krb5_ret_int16(sp, &t);
538 ret = krb5_ret_data(sp, &adr->address);
542 krb5_error_code KRB5_LIB_FUNCTION
543 krb5_store_addrs(krb5_storage *sp, krb5_addresses p)
547 ret = krb5_store_int32(sp, p.len);
549 for(i = 0; i<p.len; i++){
550 ret = krb5_store_address(sp, p.val[i]);
556 krb5_error_code KRB5_LIB_FUNCTION
557 krb5_ret_addrs(krb5_storage *sp, krb5_addresses *adr)
563 ret = krb5_ret_int32(sp, &tmp);
566 ALLOC(adr->val, adr->len);
567 if (adr->val == NULL && adr->len != 0)
569 for(i = 0; i < adr->len; i++){
570 ret = krb5_ret_address(sp, &adr->val[i]);
576 krb5_error_code KRB5_LIB_FUNCTION
577 krb5_store_authdata(krb5_storage *sp, krb5_authdata auth)
581 ret = krb5_store_int32(sp, auth.len);
583 for(i = 0; i < auth.len; i++){
584 ret = krb5_store_int16(sp, auth.val[i].ad_type);
586 ret = krb5_store_data(sp, auth.val[i].ad_data);
592 krb5_error_code KRB5_LIB_FUNCTION
593 krb5_ret_authdata(krb5_storage *sp, krb5_authdata *auth)
599 ret = krb5_ret_int32(sp, &tmp);
601 ALLOC_SEQ(auth, tmp);
602 if (auth->val == NULL && tmp != 0)
604 for(i = 0; i < tmp; i++){
605 ret = krb5_ret_int16(sp, &tmp2);
607 auth->val[i].ad_type = tmp2;
608 ret = krb5_ret_data(sp, &auth->val[i].ad_data);
619 for (i = 0; i < 32; i++) {
620 r = r << 1 | (b & 1);
631 krb5_error_code KRB5_LIB_FUNCTION
632 krb5_store_creds(krb5_storage *sp, krb5_creds *creds)
636 ret = krb5_store_principal(sp, creds->client);
639 ret = krb5_store_principal(sp, creds->server);
642 ret = krb5_store_keyblock(sp, creds->session);
645 ret = krb5_store_times(sp, creds->times);
648 ret = krb5_store_int8(sp, creds->second_ticket.length != 0); /* is_skey */
652 if(krb5_storage_is_flags(sp, KRB5_STORAGE_CREDS_FLAGS_WRONG_BITORDER))
653 ret = krb5_store_int32(sp, creds->flags.i);
655 ret = krb5_store_int32(sp, bitswap32(TicketFlags2int(creds->flags.b)));
659 ret = krb5_store_addrs(sp, creds->addresses);
662 ret = krb5_store_authdata(sp, creds->authdata);
665 ret = krb5_store_data(sp, creds->ticket);
668 ret = krb5_store_data(sp, creds->second_ticket);
672 krb5_error_code KRB5_LIB_FUNCTION
673 krb5_ret_creds(krb5_storage *sp, krb5_creds *creds)
679 memset(creds, 0, sizeof(*creds));
680 ret = krb5_ret_principal (sp, &creds->client);
681 if(ret) goto cleanup;
682 ret = krb5_ret_principal (sp, &creds->server);
683 if(ret) goto cleanup;
684 ret = krb5_ret_keyblock (sp, &creds->session);
685 if(ret) goto cleanup;
686 ret = krb5_ret_times (sp, &creds->times);
687 if(ret) goto cleanup;
688 ret = krb5_ret_int8 (sp, &dummy8);
689 if(ret) goto cleanup;
690 ret = krb5_ret_int32 (sp, &dummy32);
691 if(ret) goto cleanup;
693 * Runtime detect the what is the higher bits of the bitfield. If
694 * any of the higher bits are set in the input data, its either a
695 * new ticket flag (and this code need to be removed), or its a
696 * MIT cache (or new Heimdal cache), lets change it to our current
700 u_int32_t mask = 0xffff0000;
702 creds->flags.b.anonymous = 1;
703 if (creds->flags.i & mask)
706 dummy32 = bitswap32(dummy32);
708 creds->flags.i = dummy32;
709 ret = krb5_ret_addrs (sp, &creds->addresses);
710 if(ret) goto cleanup;
711 ret = krb5_ret_authdata (sp, &creds->authdata);
712 if(ret) goto cleanup;
713 ret = krb5_ret_data (sp, &creds->ticket);
714 if(ret) goto cleanup;
715 ret = krb5_ret_data (sp, &creds->second_ticket);
719 krb5_free_cred_contents(context, creds); /* XXX */
725 #define SC_CLIENT_PRINCIPAL 0x0001
726 #define SC_SERVER_PRINCIPAL 0x0002
727 #define SC_SESSION_KEY 0x0004
728 #define SC_TICKET 0x0008
729 #define SC_SECOND_TICKET 0x0010
730 #define SC_AUTHDATA 0x0020
731 #define SC_ADDRESSES 0x0040
737 krb5_error_code KRB5_LIB_FUNCTION
738 krb5_store_creds_tag(krb5_storage *sp, krb5_creds *creds)
744 header |= SC_CLIENT_PRINCIPAL;
746 header |= SC_SERVER_PRINCIPAL;
747 if (creds->session.keyvalue.data)
748 header |= SC_SESSION_KEY;
749 if (creds->ticket.data)
751 if (creds->second_ticket.length)
752 header |= SC_SECOND_TICKET;
753 if (creds->authdata.len)
754 header |= SC_AUTHDATA;
755 if (creds->addresses.len)
756 header |= SC_ADDRESSES;
758 ret = krb5_store_int32(sp, header);
761 ret = krb5_store_principal(sp, creds->client);
767 ret = krb5_store_principal(sp, creds->server);
772 if (creds->session.keyvalue.data) {
773 ret = krb5_store_keyblock(sp, creds->session);
778 ret = krb5_store_times(sp, creds->times);
781 ret = krb5_store_int8(sp, creds->second_ticket.length != 0); /* is_skey */
785 ret = krb5_store_int32(sp, bitswap32(TicketFlags2int(creds->flags.b)));
789 if (creds->addresses.len) {
790 ret = krb5_store_addrs(sp, creds->addresses);
795 if (creds->authdata.len) {
796 ret = krb5_store_authdata(sp, creds->authdata);
801 if (creds->ticket.data) {
802 ret = krb5_store_data(sp, creds->ticket);
807 if (creds->second_ticket.data) {
808 ret = krb5_store_data(sp, creds->second_ticket);
816 krb5_error_code KRB5_LIB_FUNCTION
817 krb5_ret_creds_tag(krb5_storage *sp,
822 int32_t dummy32, header;
824 memset(creds, 0, sizeof(*creds));
826 ret = krb5_ret_int32 (sp, &header);
827 if (ret) goto cleanup;
829 if (header & SC_CLIENT_PRINCIPAL) {
830 ret = krb5_ret_principal (sp, &creds->client);
831 if(ret) goto cleanup;
833 if (header & SC_SERVER_PRINCIPAL) {
834 ret = krb5_ret_principal (sp, &creds->server);
835 if(ret) goto cleanup;
837 if (header & SC_SESSION_KEY) {
838 ret = krb5_ret_keyblock (sp, &creds->session);
839 if(ret) goto cleanup;
841 ret = krb5_ret_times (sp, &creds->times);
842 if(ret) goto cleanup;
843 ret = krb5_ret_int8 (sp, &dummy8);
844 if(ret) goto cleanup;
845 ret = krb5_ret_int32 (sp, &dummy32);
846 if(ret) goto cleanup;
848 * Runtime detect the what is the higher bits of the bitfield. If
849 * any of the higher bits are set in the input data, its either a
850 * new ticket flag (and this code need to be removed), or its a
851 * MIT cache (or new Heimdal cache), lets change it to our current
855 u_int32_t mask = 0xffff0000;
857 creds->flags.b.anonymous = 1;
858 if (creds->flags.i & mask)
861 dummy32 = bitswap32(dummy32);
863 creds->flags.i = dummy32;
864 if (header & SC_ADDRESSES) {
865 ret = krb5_ret_addrs (sp, &creds->addresses);
866 if(ret) goto cleanup;
868 if (header & SC_AUTHDATA) {
869 ret = krb5_ret_authdata (sp, &creds->authdata);
870 if(ret) goto cleanup;
872 if (header & SC_TICKET) {
873 ret = krb5_ret_data (sp, &creds->ticket);
874 if(ret) goto cleanup;
876 if (header & SC_SECOND_TICKET) {
877 ret = krb5_ret_data (sp, &creds->second_ticket);
878 if(ret) goto cleanup;
884 krb5_free_cred_contents(context, creds); /* XXX */