6d3723f4da43b4e2f58503cf0a70d2a0096d0b0e
[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 #include "skipwrap.h"
38
39 /* evil hack */
40 #define SEQUENCE 16
41 #define CONSTRUCTED 32
42 #define APPLICATION 64
43 #define CONTEXT_SPECIFIC 128
44 static int skip_get_number(char **pp, size_t *lp, int *np)
45 {
46     unsigned l;
47     int r, n, i;
48     char *p;
49
50     l = *lp;
51     if (l < 1) {
52 #ifdef DEBUG
53         fprintf(stderr, "skip_bad_number: missing number\n");
54 #endif
55         return -1;
56     }
57     p = *pp;
58     r = (unsigned char)*p;
59     ++p; --l;
60     if (r & 0x80) {
61         n = (r&0x7f);
62         if (l < n) {
63 #ifdef DEBUG
64             fprintf(stderr, "skip_bad_number: truncated number\n");
65 #endif
66             return -1;
67         }
68         r = 0;
69         for (i = n; --i >= 0; ) {
70             r <<= 8;
71             r += (unsigned char)*p;
72             ++p; --l;
73         }
74     }
75     *np = r;
76     *pp = p;
77     *lp = l;
78     return 0;
79 }
80
81 int
82 afs_krb5_skip_ticket_wrapper(char *tix, size_t tixlen, char **enc, size_t *enclen)
83 {
84     char *p = tix;
85     size_t l = tixlen;
86     int code;
87     int num;
88
89     if (l < 1) return -1;
90     if (*p != (char) (CONSTRUCTED+APPLICATION+1)) return -1;
91     ++p; --l;
92     if ((code = skip_get_number(&p, &l, &num))) return code;
93     if (l != num) return -1;
94     if (l < 1) return -1;
95     if (*p != (char)(CONSTRUCTED+SEQUENCE)) return -1;
96     ++p; --l;
97     if ((code = skip_get_number(&p, &l, &num))) return code;
98     if (l != num) return -1;
99     if (l < 1) return -1;
100     if (*p != (char)(CONSTRUCTED+CONTEXT_SPECIFIC+0)) return -1;
101     ++p; --l;
102     if ((code = skip_get_number(&p, &l, &num))) return code;
103     if (l < num) return -1;
104     l -= num; p += num;
105     if (l < 1) return -1;
106     if (*p != (char)(CONSTRUCTED+CONTEXT_SPECIFIC+1)) return -1;
107     ++p; --l;
108     if ((code = skip_get_number(&p, &l, &num))) return code;
109     if (l < num) return -1;
110     l -= num; p += num;
111     if (l < 1) return -1;
112     if (*p != (char)(CONSTRUCTED+CONTEXT_SPECIFIC+2)) return -1;
113     ++p; --l;
114     if ((code = skip_get_number(&p, &l, &num))) return code;
115     if (l < num) return -1;
116     l -= num; p += num;
117     if (l < 1) return -1;
118     if (*p != (char)(CONSTRUCTED+CONTEXT_SPECIFIC+3)) return -1;
119     ++p; --l;
120     if ((code = skip_get_number(&p, &l, &num))) return code;
121     if (l != num) return -1;
122     *enc = p;
123     *enclen = l;
124     return 0;
125 }