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