k5-klog-20061026
[openafs.git] / src / aklog / skipwrap.c
1
2 /*
3  * Copyright (c) 2006
4  * The Regents of the University of Michigan
5  * ALL RIGHTS RESERVED
6  *
7  * Permission is granted to use, copy, create derivative works
8  * and redistribute this software and such derivative works
9  * for any purpose, so long as the name of the University of
10  * Michigan is not used in any advertising or publicity
11  * pertaining to the use or distribution of this software
12  * without specific, written prior authorization.  If the
13  * above copyright notice or any other identification of the
14  * University of Michigan is included in any copy of any
15  * portion of this software, then the disclaimer below must
16  * also be included.
17  *
18  * This software is provided as is, without representation
19  * from the University of Michigan as to its fitness for any
20  * purpose, and without warranty by the University of
21  * Michigan of any kind, either express or implied, including
22  * without limitation the implied warranties of
23  * merchantability and fitness for a particular purpose.  The
24  * regents of the University of Michigan shall not be liable
25  * for any damages, including special, indirect, incidental, or
26  * consequential damages, with respect to any claim arising
27  * out of or in connection with the use of the software, even
28  * if it has been or is hereafter advised of the possibility of
29  * such damages.
30  */
31
32 #include <afsconfig.h>
33 #include <afs/param.h>
34 #include <stdio.h>
35 #include <aklog.h>
36 #include <krb5.h>
37
38 /* evil hack */
39 #define SEQUENCE 16
40 #define CONSTRUCTED 32
41 #define APPLICATION 64
42 #define CONTEXT_SPECIFIC 128
43 static int skip_get_number(char **pp, size_t *lp, int *np)
44 {
45     unsigned l;
46     int r, n, i;
47     char *p;
48
49     l = *lp;
50     if (l < 1) {
51 #ifdef DEBUG
52         fprintf(stderr, "skip_bad_number: missing number\n");
53 #endif
54         return -1;
55     }
56     p = *pp;
57     r = (unsigned char)*p;
58     ++p; --l;
59     if (r & 0x80) {
60         n = (r&0x7f);
61         if (l < n) {
62 #ifdef DEBUG
63             fprintf(stderr, "skip_bad_number: truncated number\n");
64 #endif
65             return -1;
66         }
67         r = 0;
68         for (i = n; --i >= 0; ) {
69             r <<= 8;
70             r += (unsigned char)*p;
71             ++p; --l;
72         }
73     }
74     *np = r;
75     *pp = p;
76     *lp = l;
77     return 0;
78 }
79
80 int
81 afs_krb5_skip_ticket_wrapper(char *tix, size_t tixlen, char **enc, size_t *enclen)
82 {
83     char *p = tix;
84     size_t l = tixlen;
85     int code;
86     int num;
87
88     if (l < 1) return -1;
89     if (*p != (char) (CONSTRUCTED+APPLICATION+1)) return -1;
90     ++p; --l;
91     if ((code = skip_get_number(&p, &l, &num))) return code;
92     if (l != num) return -1;
93     if (l < 1) return -1;
94     if (*p != (char)(CONSTRUCTED+SEQUENCE)) return -1;
95     ++p; --l;
96     if ((code = skip_get_number(&p, &l, &num))) return code;
97     if (l != num) return -1;
98     if (l < 1) return -1;
99     if (*p != (char)(CONSTRUCTED+CONTEXT_SPECIFIC+0)) return -1;
100     ++p; --l;
101     if ((code = skip_get_number(&p, &l, &num))) return code;
102     if (l < num) return -1;
103     l -= num; p += num;
104     if (l < 1) return -1;
105     if (*p != (char)(CONSTRUCTED+CONTEXT_SPECIFIC+1)) return -1;
106     ++p; --l;
107     if ((code = skip_get_number(&p, &l, &num))) return code;
108     if (l < num) return -1;
109     l -= num; p += num;
110     if (l < 1) return -1;
111     if (*p != (char)(CONSTRUCTED+CONTEXT_SPECIFIC+2)) return -1;
112     ++p; --l;
113     if ((code = skip_get_number(&p, &l, &num))) return code;
114     if (l < num) return -1;
115     l -= num; p += num;
116     if (l < 1) return -1;
117     if (*p != (char)(CONSTRUCTED+CONTEXT_SPECIFIC+3)) return -1;
118     ++p; --l;
119     if ((code = skip_get_number(&p, &l, &num))) return code;
120     if (l != num) return -1;
121     *enc = p;
122     *enclen = l;
123     return 0;
124 }