Update gnulib files.
[shishi.git] / lib / encticketpart.c
blob07dbb03edebb805166f69ab6bc928aff4a92b666
1 /* encticketpart.c --- Encrypted ticket part handling.
2 * Copyright (C) 2002, 2003, 2004, 2007 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful, but
12 * 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, see http://www.gnu.org/licenses or write
18 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
19 * Floor, Boston, MA 02110-1301, USA
23 #include "internal.h"
25 Shishi_asn1
26 shishi_encticketpart (Shishi * handle)
28 Shishi_asn1 node;
29 int res;
31 node = shishi_asn1_encticketpart (handle);
33 res = shishi_asn1_write (handle, node, "starttime", NULL, 0);
34 if (res != SHISHI_OK)
36 shishi_asn1_done (handle, node);
37 return NULL;
40 res = shishi_asn1_write (handle, node, "renew-till", NULL, 0);
41 if (res != SHISHI_OK)
43 shishi_asn1_done (handle, node);
44 return NULL;
47 res = shishi_asn1_write (handle, node, "caddr", NULL, 0);
48 if (res != SHISHI_OK)
50 shishi_asn1_done (handle, node);
51 return NULL;
54 res = shishi_asn1_write (handle, node, "authorization-data", NULL, 0);
55 if (res != SHISHI_OK)
57 shishi_asn1_done (handle, node);
58 return NULL;
61 res = shishi_encticketpart_flags_set (handle, node, 0);
62 if (res != SHISHI_OK)
64 shishi_asn1_done (handle, node);
65 return NULL;
68 return node;
71 /**
72 * shishi_encticketpart_get_key:
73 * @handle: shishi handle as allocated by shishi_init().
74 * @encticketpart: input EncTicketPart variable.
75 * @key: newly allocated key.
77 * Extract the session key in the Ticket.
79 * Return value: Returns SHISHI_OK iff succesful.
80 **/
81 int
82 shishi_encticketpart_get_key (Shishi * handle,
83 Shishi_asn1 encticketpart, Shishi_key ** key)
85 int res;
86 char *buf;
87 size_t buflen;
88 int32_t keytype;
90 res = shishi_asn1_read_int32 (handle, encticketpart,
91 "key.keytype", &keytype);
92 if (res != SHISHI_OK)
93 return res;
95 res = shishi_asn1_read (handle, encticketpart, "key.keyvalue",
96 &buf, &buflen);
97 if (res != SHISHI_OK)
98 return res;
100 res = shishi_key_from_value (handle, keytype, buf, key);
101 free (buf);
102 if (res != SHISHI_OK)
103 return res;
105 return SHISHI_OK;
109 * shishi_encticketpart_key_set:
110 * @handle: shishi handle as allocated by shishi_init().
111 * @encticketpart: input EncTicketPart variable.
112 * @key: key handle with information to store in encticketpart.
114 * Set the EncTicketPart.key field to key type and value of supplied
115 * key.
117 * Return value: Returns SHISHI_OK iff succesful.
120 shishi_encticketpart_key_set (Shishi * handle,
121 Shishi_asn1 encticketpart, Shishi_key * key)
123 int res;
124 int keytype;
126 keytype = shishi_key_type (key);
127 res = shishi_asn1_write_uint32 (handle, encticketpart,
128 "key.keytype", keytype);
129 if (res != SHISHI_OK)
130 return res;
132 res = shishi_asn1_write (handle, encticketpart, "key.keyvalue",
133 shishi_key_value (key), shishi_key_length (key));
134 if (res != SHISHI_OK)
135 return res;
137 return SHISHI_OK;
141 * shishi_encticketpart_flags_set:
142 * @handle: shishi handle as allocated by shishi_init().
143 * @encticketpart: input EncTicketPart variable.
144 * @flags: flags to set in encticketpart.
146 * Set the EncTicketPart.flags to supplied value.
148 * Return value: Returns SHISHI_OK iff succesful.
151 shishi_encticketpart_flags_set (Shishi * handle,
152 Shishi_asn1 encticketpart, int flags)
154 int res;
156 res = shishi_asn1_write_bitstring (handle, encticketpart, "flags", flags);
157 if (res != SHISHI_OK)
158 return res;
160 return SHISHI_OK;
164 shishi_encticketpart_crealm (Shishi * handle,
165 Shishi_asn1 encticketpart,
166 char **crealm, size_t * crealmlen)
168 return shishi_asn1_read (handle, encticketpart, "crealm",
169 crealm, crealmlen);
173 * shishi_encticketpart_crealm_set:
174 * @handle: shishi handle as allocated by shishi_init().
175 * @encticketpart: input EncTicketPart variable.
176 * @realm: input array with name of realm.
178 * Set the realm field in the KDC-REQ.
180 * Return value: Returns SHISHI_OK iff successful.
183 shishi_encticketpart_crealm_set (Shishi * handle,
184 Shishi_asn1 encticketpart, const char *realm)
186 int res;
188 res = shishi_asn1_write (handle, encticketpart, "crealm", realm, 0);
189 if (res != SHISHI_OK)
190 return res;
192 return SHISHI_OK;
196 * shishi_encticketpart_cname_set:
197 * @handle: shishi handle as allocated by shishi_init().
198 * @encticketpart: input EncTicketPart variable.
199 * @name_type: type of principial, see Shishi_name_type, usually
200 * SHISHI_NT_UNKNOWN.
201 * @principal: input array with principal name.
203 * Set the client name field in the EncTicketPart.
205 * Return value: Returns SHISHI_OK iff successful.
208 shishi_encticketpart_cname_set (Shishi * handle,
209 Shishi_asn1 encticketpart,
210 Shishi_name_type name_type,
211 const char *principal)
213 int res;
215 res = shishi_asn1_write_uint32 (handle, encticketpart,
216 "cname.name-type", name_type);
217 if (res != SHISHI_OK)
218 return res;
220 res = shishi_asn1_write (handle, encticketpart,
221 "cname.name-string", NULL, 0);
222 if (res != SHISHI_OK)
223 return res;
225 res = shishi_asn1_write (handle, encticketpart,
226 "cname.name-string", "NEW", 1);
227 if (res != SHISHI_OK)
228 return res;
230 res = shishi_asn1_write (handle, encticketpart,
231 "cname.name-string.?1",
232 principal, strlen (principal));
233 if (res != SHISHI_OK)
234 return res;
236 return SHISHI_OK;
240 * shishi_encticketpart_transited_set:
241 * @handle: shishi handle as allocated by shishi_init().
242 * @encticketpart: input EncTicketPart variable.
243 * @trtype: transitedencoding type, e.g. SHISHI_TR_DOMAIN_X500_COMPRESS.
244 * @trdata: actual transited realm data.
245 * @trdatalen: length of actual transited realm data.
247 * Set the EncTicketPart.transited field to supplied value.
249 * Return value: Returns SHISHI_OK iff succesful.
252 shishi_encticketpart_transited_set (Shishi * handle,
253 Shishi_asn1 encticketpart,
254 int32_t trtype,
255 const char *trdata, size_t trdatalen)
257 int res;
259 res = shishi_asn1_write_int32 (handle, encticketpart,
260 "transited.tr-type", trtype);
261 if (res != SHISHI_OK)
262 return res;
264 res = shishi_asn1_write (handle, encticketpart,
265 "transited.contents", trdata, trdatalen);
266 if (res != SHISHI_OK)
267 return res;
269 return SHISHI_OK;
273 * shishi_encticketpart_authtime_set:
274 * @handle: shishi handle as allocated by shishi_init().
275 * @encticketpart: input EncTicketPart variable.
276 * @authtime: character buffer containing a generalized time string.
278 * Set the EncTicketPart.authtime to supplied value.
280 * Return value: Returns SHISHI_OK iff succesful.
283 shishi_encticketpart_authtime_set (Shishi * handle,
284 Shishi_asn1 encticketpart,
285 const char *authtime)
287 int res;
289 res = shishi_asn1_write (handle, encticketpart, "authtime",
290 authtime, SHISHI_GENERALIZEDTIME_LENGTH);
291 if (res != SHISHI_OK)
292 return SHISHI_ASN1_ERROR;
294 return SHISHI_OK;
298 * shishi_encticketpart_endtime_set:
299 * @handle: shishi handle as allocated by shishi_init().
300 * @encticketpart: input EncTicketPart variable.
301 * @endtime: character buffer containing a generalized time string.
303 * Set the EncTicketPart.endtime to supplied value.
305 * Return value: Returns SHISHI_OK iff succesful.
308 shishi_encticketpart_endtime_set (Shishi * handle,
309 Shishi_asn1 encticketpart,
310 const char *endtime)
312 int res;
314 res = shishi_asn1_write (handle, encticketpart, "endtime",
315 endtime, SHISHI_GENERALIZEDTIME_LENGTH);
316 if (res != SHISHI_OK)
317 return res;
319 return SHISHI_OK;
323 shishi_encticketpart_authtime (Shishi * handle,
324 Shishi_asn1 encticketpart,
325 char *authtime, size_t * authtimelen)
327 return shishi_asn1_read_inline (handle, encticketpart, "authtime",
328 authtime, authtimelen);
331 time_t
332 shishi_encticketpart_authctime (Shishi * handle, Shishi_asn1 encticketpart)
334 char authtime[SHISHI_GENERALIZEDTIME_LENGTH + 1];
335 size_t authtimelen;
336 time_t t;
337 int res;
339 authtimelen = sizeof (authtime);
340 res = shishi_encticketpart_authtime (handle, encticketpart,
341 authtime, &authtimelen);
342 if (res != SHISHI_OK)
343 return (time_t) - 1;
345 authtime[SHISHI_GENERALIZEDTIME_LENGTH] = '\0';
347 t = shishi_generalize_ctime (handle, authtime);
349 return t;
353 * shishi_encticketpart_client:
354 * @handle: Shishi library handle create by shishi_init().
355 * @encticketpart: EncTicketPart variable to get client name from.
356 * @client: pointer to newly allocated zero terminated string containing
357 * principal name. May be %NULL (to only populate @clientlen).
358 * @clientlen: pointer to length of @client on output, excluding terminating
359 * zero. May be %NULL (to only populate @client).
361 * Represent client principal name in EncTicketPart as zero-terminated
362 * string. The string is allocate by this function, and it is the
363 * responsibility of the caller to deallocate it. Note that the
364 * output length @clientlen does not include the terminating zero.
366 * Return value: Returns SHISHI_OK iff successful.
369 shishi_encticketpart_client (Shishi * handle,
370 Shishi_asn1 encticketpart,
371 char **client, size_t * clientlen)
373 return shishi_principal_name (handle, encticketpart, "cname",
374 client, clientlen);
378 * shishi_encticketpart_clientrealm:
379 * @handle: Shishi library handle create by shishi_init().
380 * @encticketpart: EncTicketPart variable to get client name and realm from.
381 * @client: pointer to newly allocated zero terminated string containing
382 * principal name and realm. May be %NULL (to only populate @clientlen).
383 * @clientlen: pointer to length of @client on output, excluding terminating
384 * zero. May be %NULL (to only populate @client).
386 * Convert cname and realm fields from EncTicketPart to printable
387 * principal name format. The string is allocate by this function,
388 * and it is the responsibility of the caller to deallocate it. Note
389 * that the output length @clientlen does not include the terminating
390 * zero.
392 * Return value: Returns SHISHI_OK iff successful.
395 shishi_encticketpart_clientrealm (Shishi * handle,
396 Shishi_asn1 encticketpart,
397 char **client, size_t * clientlen)
399 return shishi_principal_name_realm (handle,
400 encticketpart, "cname",
401 encticketpart, "crealm",
402 client, clientlen);