Look for SRV RRs if KDC address not known.
[shishi.git] / lib / encticketpart.c
blobecccde6f0579159a3a07211f2df7bc273a0f1e0f
1 /* encticketpart.c encrypted ticket part handling
2 * Copyright (C) 2002, 2003 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Shishi; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "internal.h"
24 Shishi_asn1
25 shishi_encticketpart (Shishi * handle)
27 Shishi_asn1 node;
28 int res;
30 node = shishi_asn1_encticketpart (handle);
32 res = shishi_asn1_write (handle, node, "starttime", NULL, 0);
33 if (res != SHISHI_OK)
35 shishi_asn1_done (handle, node);
36 return NULL;
39 res = shishi_asn1_write (handle, node, "renew-till", NULL, 0);
40 if (res != SHISHI_OK)
42 shishi_asn1_done (handle, node);
43 return NULL;
46 res = shishi_asn1_write (handle, node, "caddr", NULL, 0);
47 if (res != SHISHI_OK)
49 shishi_asn1_done (handle, node);
50 return NULL;
53 res = shishi_asn1_write (handle, node, "authorization-data", NULL, 0);
54 if (res != SHISHI_OK)
56 shishi_asn1_done (handle, node);
57 return NULL;
60 return node;
63 /**
64 * shishi_asn1ticket_get_enc_part_etype:
65 * @handle: shishi handle as allocated by shishi_init().
66 * @kdcrep: Ticket variable to get value from.
67 * @etype: output variable that holds the value.
69 * Extract Ticket.enc-part.etype.
71 * Return value: Returns SHISHI_OK iff successful.
72 **/
73 int
74 shishi_encticketpart_get_enc_part_etype (Shishi * handle,
75 Shishi_asn1 encticketpart,
76 int32_t * etype)
78 int res;
80 res = shishi_asn1_read_int32 (handle, encticketpart,
81 "enc-part.etype", etype);
83 return res;
86 /**
87 * shishi_encticketpart_get_key:
88 * @handle: shishi handle as allocated by shishi_init().
89 * @encticketpart: input EncTicketPart variable.
90 * @keytype: output variable that holds key type.
91 * @keyvalue: output array with key.
92 * @keyvalue_len: on input, maximum size of output array with key,
93 * on output, holds the actual size of output array with key.
95 * Extract the session key in the Ticket.
97 * Return value: Returns SHISHI_OK iff succesful.
98 **/
99 int
100 shishi_encticketpart_get_key (Shishi * handle,
101 Shishi_asn1 encticketpart, Shishi_key ** key)
103 int res;
104 char buf[BUFSIZ];
105 int buflen;
106 int32_t keytype;
108 res = shishi_asn1_read_int32 (handle, encticketpart,
109 "key.keytype", &keytype);
110 if (res != SHISHI_OK)
111 return res;
113 buflen = BUFSIZ;
114 res = shishi_asn1_field (handle, encticketpart, buf, &buflen,
115 "key.keyvalue");
116 if (res != SHISHI_OK)
117 return res;
119 res = shishi_key_from_value (handle, keytype, buf, key);
120 if (res != SHISHI_OK)
121 return res;
123 return SHISHI_OK;
127 * shishi_encticketpart_key_set:
128 * @handle: shishi handle as allocated by shishi_init().
129 * @encticketpart: input EncTicketPart variable.
130 * @key: key handle with information to store in encticketpart.
132 * Set the EncTicketPart.key field to key type and value of supplied
133 * key.
135 * Return value: Returns SHISHI_OK iff succesful.
138 shishi_encticketpart_key_set (Shishi * handle,
139 Shishi_asn1 encticketpart, Shishi_key * key)
141 int res;
142 char buf[BUFSIZ];
143 int keytype;
145 keytype = shishi_key_type (key);
146 sprintf (buf, "%d", keytype);
147 res = shishi_asn1_write (handle, encticketpart, "key.keytype", buf, 0);
148 if (res != SHISHI_OK)
149 return res;
151 res = shishi_asn1_write (handle, encticketpart,
152 "key.keyvalue",
153 shishi_key_value (key), shishi_key_length (key));
154 if (res != SHISHI_OK)
155 return res;
157 return SHISHI_OK;
161 * shishi_encticketpart_flags_set:
162 * @handle: shishi handle as allocated by shishi_init().
163 * @encticketpart: input EncTicketPart variable.
164 * @flags: flags to set in encticketpart.
166 * Set the EncTicketPart.flags to supplied value.
168 * Return value: Returns SHISHI_OK iff succesful.
171 shishi_encticketpart_flags_set (Shishi * handle,
172 Shishi_asn1 encticketpart, int flags)
174 int res;
175 char buf[BUFSIZ];
177 sprintf (buf, "%d", flags);
178 res = shishi_asn1_write (handle, encticketpart, "flags", buf, 0);
179 if (res != SHISHI_OK)
180 return res;
182 return SHISHI_OK;
186 * shishi_encticketpart_crealm_set:
187 * @handle: shishi handle as allocated by shishi_init().
188 * @encticketpart: input EncTicketPart variable.
189 * @realm: input array with name of realm.
191 * Set the realm field in the KDC-REQ.
193 * Return value: Returns SHISHI_OK iff successful.
196 shishi_encticketpart_crealm_set (Shishi * handle,
197 Shishi_asn1 encticketpart, const char *realm)
199 int res;
201 res = shishi_asn1_write (handle, encticketpart, "crealm", realm, 0);
202 if (res != SHISHI_OK)
203 return res;
205 return SHISHI_OK;
209 * shishi_encticketpart_cname_set:
210 * @handle: shishi handle as allocated by shishi_init().
211 * @encticketpart: input EncTicketPart variable.
212 * @name_type: type of principial, see Shishi_name_type, usually
213 * SHISHI_NT_UNKNOWN.
214 * @principal: input array with principal name.
216 * Set the client name field in the EncTicketPart.
218 * Return value: Returns SHISHI_OK iff successful.
221 shishi_encticketpart_cname_set (Shishi * handle,
222 Shishi_asn1 encticketpart,
223 Shishi_name_type name_type,
224 const char *principal)
226 int res;
227 char buf[BUFSIZ];
229 sprintf (buf, "%d", name_type);
231 res = shishi_asn1_write (handle, encticketpart, "cname.name-type", buf, 0);
232 if (res != SHISHI_OK)
233 return res;
235 res = shishi_asn1_write (handle, encticketpart,
236 "cname.name-string", NULL, 0);
237 if (res != SHISHI_OK)
238 return res;
240 res = shishi_asn1_write (handle, encticketpart,
241 "cname.name-string", "NEW", 1);
242 if (res != SHISHI_OK)
243 return res;
245 res = shishi_asn1_write (handle, encticketpart,
246 "cname.name-string.?1",
247 principal, strlen (principal));
248 if (res != SHISHI_OK)
249 return res;
251 return SHISHI_OK;
255 * shishi_encticketpart_transited_set:
256 * @handle: shishi handle as allocated by shishi_init().
257 * @encticketpart: input EncTicketPart variable.
258 * @trtype: transitedencoding type, e.g. SHISHI_TR_DOMAIN_X500_COMPRESS.
259 * @trdata: actual transited realm data.
260 * @trdatalen: length of actual transited realm data.
262 * Set the EncTicketPart.transited field to supplied value.
264 * Return value: Returns SHISHI_OK iff succesful.
267 shishi_encticketpart_transited_set (Shishi * handle,
268 Shishi_asn1 encticketpart,
269 int trtype,
270 char *trdata, size_t trdatalen)
272 int res;
273 char buf[BUFSIZ];
275 sprintf (buf, "%d", trtype);
276 res = shishi_asn1_write (handle, encticketpart,
277 "transited.tr-type", buf, 0);
278 if (res != SHISHI_OK)
279 return res;
281 res = shishi_asn1_write (handle, encticketpart,
282 "transited.contents", trdata, trdatalen);
283 if (res != SHISHI_OK)
284 return res;
286 return SHISHI_OK;
290 * shishi_encticketpart_authtime_set:
291 * @handle: shishi handle as allocated by shishi_init().
292 * @encticketpart: input EncTicketPart variable.
293 * @authtime: character buffer containing a generalized time string.
295 * Set the EncTicketPart.authtime to supplied value.
297 * Return value: Returns SHISHI_OK iff succesful.
300 shishi_encticketpart_authtime_set (Shishi * handle,
301 Shishi_asn1 encticketpart,
302 const char *authtime)
304 int res;
306 res = shishi_asn1_write (handle, encticketpart, "authtime",
307 authtime, GENERALIZEDTIME_TIME_LEN);
308 if (res != SHISHI_OK)
309 return SHISHI_ASN1_ERROR;
311 return SHISHI_OK;
315 * shishi_encticketpart_endtime_set:
316 * @handle: shishi handle as allocated by shishi_init().
317 * @encticketpart: input EncTicketPart variable.
318 * @endtime: character buffer containing a generalized time string.
320 * Set the EncTicketPart.endtime to supplied value.
322 * Return value: Returns SHISHI_OK iff succesful.
325 shishi_encticketpart_endtime_set (Shishi * handle,
326 Shishi_asn1 encticketpart,
327 const char *endtime)
329 int res;
331 res = shishi_asn1_write (handle, encticketpart, "endtime",
332 endtime, GENERALIZEDTIME_TIME_LEN);
333 if (res != SHISHI_OK)
334 return res;
336 return SHISHI_OK;
340 shishi_encticketpart_authtime (Shishi * handle,
341 Shishi_asn1 encticketpart,
342 char *authtime, int *authtimelen)
344 return shishi_asn1_field (handle, encticketpart, authtime, authtimelen,
345 "authtime");
348 time_t
349 shishi_encticketpart_authctime (Shishi * handle, Shishi_asn1 encticketpart)
351 char authtime[GENERALIZEDTIME_TIME_LEN + 1];
352 int authtimelen;
353 time_t t;
354 int res;
356 authtimelen = sizeof (authtime);
357 res = shishi_encticketpart_authtime (handle, encticketpart,
358 authtime, &authtimelen);
359 if (res != SHISHI_OK)
360 return (time_t) - 1;
362 authtime[GENERALIZEDTIME_TIME_LEN] = '\0';
364 t = shishi_generalize_ctime (handle, authtime);
366 return t;
370 shishi_encticketpart_cname_get (Shishi * handle,
371 Shishi_asn1 encticketpart,
372 char *cname, int *cnamelen)
374 return shishi_principal_name_get (handle, encticketpart,
375 "cname", cname, cnamelen);
379 shishi_encticketpart_cnamerealm_get (Shishi * handle,
380 Shishi_asn1 encticketpart,
381 char *cnamerealm, int *cnamerealmlen)
383 return shishi_principal_name_realm_get (handle, encticketpart,
384 "cname",
385 encticketpart,
386 "crealm",
387 cnamerealm, cnamerealmlen);