Add.
[shishi.git] / lib / encticketpart.c
blob6bde4c0706447cac33658e412b147e75a33f2429
1 /* encticketpart.c --- Encrypted ticket part handling.
2 * Copyright (C) 2002, 2003, 2004 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 res = shishi_encticketpart_flags_set (handle, node, 0);
61 if (res != SHISHI_OK)
63 shishi_asn1_done (handle, node);
64 return NULL;
67 return node;
70 /**
71 * shishi_encticketpart_get_key:
72 * @handle: shishi handle as allocated by shishi_init().
73 * @encticketpart: input EncTicketPart variable.
74 * @key: newly allocated key.
76 * Extract the session key in the Ticket.
78 * Return value: Returns SHISHI_OK iff succesful.
79 **/
80 int
81 shishi_encticketpart_get_key (Shishi * handle,
82 Shishi_asn1 encticketpart, Shishi_key ** key)
84 int res;
85 char *buf;
86 size_t buflen;
87 int32_t keytype;
89 res = shishi_asn1_read_int32 (handle, encticketpart,
90 "key.keytype", &keytype);
91 if (res != SHISHI_OK)
92 return res;
94 res = shishi_asn1_read (handle, encticketpart, "key.keyvalue",
95 &buf, &buflen);
96 if (res != SHISHI_OK)
97 return res;
99 res = shishi_key_from_value (handle, keytype, buf, key);
100 free (buf);
101 if (res != SHISHI_OK)
102 return res;
104 return SHISHI_OK;
108 * shishi_encticketpart_key_set:
109 * @handle: shishi handle as allocated by shishi_init().
110 * @encticketpart: input EncTicketPart variable.
111 * @key: key handle with information to store in encticketpart.
113 * Set the EncTicketPart.key field to key type and value of supplied
114 * key.
116 * Return value: Returns SHISHI_OK iff succesful.
119 shishi_encticketpart_key_set (Shishi * handle,
120 Shishi_asn1 encticketpart, Shishi_key * key)
122 int res;
123 int keytype;
125 keytype = shishi_key_type (key);
126 res = shishi_asn1_write_uint32 (handle, encticketpart,
127 "key.keytype", keytype);
128 if (res != SHISHI_OK)
129 return res;
131 res = shishi_asn1_write (handle, encticketpart, "key.keyvalue",
132 shishi_key_value (key), shishi_key_length (key));
133 if (res != SHISHI_OK)
134 return res;
136 return SHISHI_OK;
140 * shishi_encticketpart_flags_set:
141 * @handle: shishi handle as allocated by shishi_init().
142 * @encticketpart: input EncTicketPart variable.
143 * @flags: flags to set in encticketpart.
145 * Set the EncTicketPart.flags to supplied value.
147 * Return value: Returns SHISHI_OK iff succesful.
150 shishi_encticketpart_flags_set (Shishi * handle,
151 Shishi_asn1 encticketpart, int flags)
153 int res;
155 res = shishi_asn1_write_bitstring (handle, encticketpart, "flags", flags);
156 if (res != SHISHI_OK)
157 return res;
159 return SHISHI_OK;
163 shishi_encticketpart_crealm (Shishi * handle,
164 Shishi_asn1 encticketpart,
165 char **crealm, size_t * crealmlen)
167 return shishi_asn1_read (handle, encticketpart, "crealm",
168 crealm, crealmlen);
172 * shishi_encticketpart_crealm_set:
173 * @handle: shishi handle as allocated by shishi_init().
174 * @encticketpart: input EncTicketPart variable.
175 * @realm: input array with name of realm.
177 * Set the realm field in the KDC-REQ.
179 * Return value: Returns SHISHI_OK iff successful.
182 shishi_encticketpart_crealm_set (Shishi * handle,
183 Shishi_asn1 encticketpart, const char *realm)
185 int res;
187 res = shishi_asn1_write (handle, encticketpart, "crealm", realm, 0);
188 if (res != SHISHI_OK)
189 return res;
191 return SHISHI_OK;
195 * shishi_encticketpart_cname_set:
196 * @handle: shishi handle as allocated by shishi_init().
197 * @encticketpart: input EncTicketPart variable.
198 * @name_type: type of principial, see Shishi_name_type, usually
199 * SHISHI_NT_UNKNOWN.
200 * @principal: input array with principal name.
202 * Set the client name field in the EncTicketPart.
204 * Return value: Returns SHISHI_OK iff successful.
207 shishi_encticketpart_cname_set (Shishi * handle,
208 Shishi_asn1 encticketpart,
209 Shishi_name_type name_type,
210 const char *principal)
212 int res;
214 res = shishi_asn1_write_uint32 (handle, encticketpart,
215 "cname.name-type", name_type);
216 if (res != SHISHI_OK)
217 return res;
219 res = shishi_asn1_write (handle, encticketpart,
220 "cname.name-string", NULL, 0);
221 if (res != SHISHI_OK)
222 return res;
224 res = shishi_asn1_write (handle, encticketpart,
225 "cname.name-string", "NEW", 1);
226 if (res != SHISHI_OK)
227 return res;
229 res = shishi_asn1_write (handle, encticketpart,
230 "cname.name-string.?1",
231 principal, strlen (principal));
232 if (res != SHISHI_OK)
233 return res;
235 return SHISHI_OK;
239 * shishi_encticketpart_transited_set:
240 * @handle: shishi handle as allocated by shishi_init().
241 * @encticketpart: input EncTicketPart variable.
242 * @trtype: transitedencoding type, e.g. SHISHI_TR_DOMAIN_X500_COMPRESS.
243 * @trdata: actual transited realm data.
244 * @trdatalen: length of actual transited realm data.
246 * Set the EncTicketPart.transited field to supplied value.
248 * Return value: Returns SHISHI_OK iff succesful.
251 shishi_encticketpart_transited_set (Shishi * handle,
252 Shishi_asn1 encticketpart,
253 int32_t trtype,
254 const char *trdata, size_t trdatalen)
256 int res;
258 res = shishi_asn1_write_int32 (handle, encticketpart,
259 "transited.tr-type", trtype);
260 if (res != SHISHI_OK)
261 return res;
263 res = shishi_asn1_write (handle, encticketpart,
264 "transited.contents", trdata, trdatalen);
265 if (res != SHISHI_OK)
266 return res;
268 return SHISHI_OK;
272 * shishi_encticketpart_authtime_set:
273 * @handle: shishi handle as allocated by shishi_init().
274 * @encticketpart: input EncTicketPart variable.
275 * @authtime: character buffer containing a generalized time string.
277 * Set the EncTicketPart.authtime to supplied value.
279 * Return value: Returns SHISHI_OK iff succesful.
282 shishi_encticketpart_authtime_set (Shishi * handle,
283 Shishi_asn1 encticketpart,
284 const char *authtime)
286 int res;
288 res = shishi_asn1_write (handle, encticketpart, "authtime",
289 authtime, SHISHI_GENERALIZEDTIME_LENGTH);
290 if (res != SHISHI_OK)
291 return SHISHI_ASN1_ERROR;
293 return SHISHI_OK;
297 * shishi_encticketpart_endtime_set:
298 * @handle: shishi handle as allocated by shishi_init().
299 * @encticketpart: input EncTicketPart variable.
300 * @endtime: character buffer containing a generalized time string.
302 * Set the EncTicketPart.endtime to supplied value.
304 * Return value: Returns SHISHI_OK iff succesful.
307 shishi_encticketpart_endtime_set (Shishi * handle,
308 Shishi_asn1 encticketpart,
309 const char *endtime)
311 int res;
313 res = shishi_asn1_write (handle, encticketpart, "endtime",
314 endtime, SHISHI_GENERALIZEDTIME_LENGTH);
315 if (res != SHISHI_OK)
316 return res;
318 return SHISHI_OK;
322 shishi_encticketpart_authtime (Shishi * handle,
323 Shishi_asn1 encticketpart,
324 char *authtime, size_t * authtimelen)
326 return shishi_asn1_read_inline (handle, encticketpart, "authtime",
327 authtime, authtimelen);
330 time_t
331 shishi_encticketpart_authctime (Shishi * handle, Shishi_asn1 encticketpart)
333 char authtime[SHISHI_GENERALIZEDTIME_LENGTH + 1];
334 size_t authtimelen;
335 time_t t;
336 int res;
338 authtimelen = sizeof (authtime);
339 res = shishi_encticketpart_authtime (handle, encticketpart,
340 authtime, &authtimelen);
341 if (res != SHISHI_OK)
342 return (time_t) - 1;
344 authtime[SHISHI_GENERALIZEDTIME_LENGTH] = '\0';
346 t = shishi_generalize_ctime (handle, authtime);
348 return t;
352 * shishi_encticketpart_client:
353 * @handle: Shishi library handle create by shishi_init().
354 * @encticketpart: EncTicketPart variable to get client name from.
355 * @client: pointer to newly allocated zero terminated string containing
356 * principal name. May be %NULL (to only populate @clientlen).
357 * @clientlen: pointer to length of @client on output, excluding terminating
358 * zero. May be %NULL (to only populate @client).
360 * Represent client principal name in EncTicketPart as zero-terminated
361 * string. The string is allocate by this function, and it is the
362 * responsibility of the caller to deallocate it. Note that the
363 * output length @clientlen does not include the terminating zero.
365 * Return value: Returns SHISHI_OK iff successful.
368 shishi_encticketpart_client (Shishi * handle,
369 Shishi_asn1 encticketpart,
370 char **client, size_t * clientlen)
372 return shishi_principal_name (handle, encticketpart, "cname",
373 client, clientlen);
377 * shishi_encticketpart_clientrealm:
378 * @handle: Shishi library handle create by shishi_init().
379 * @encticketpart: EncTicketPart variable to get client name and realm from.
380 * @client: pointer to newly allocated zero terminated string containing
381 * principal name and realm. May be %NULL (to only populate @clientlen).
382 * @clientlen: pointer to length of @client on output, excluding terminating
383 * zero. May be %NULL (to only populate @client).
385 * Convert cname and realm fields from EncTicketPart to printable
386 * principal name format. The string is allocate by this function,
387 * and it is the responsibility of the caller to deallocate it. Note
388 * that the output length @clientlen does not include the terminating
389 * zero.
391 * Return value: Returns SHISHI_OK iff successful.
394 shishi_encticketpart_clientrealm (Shishi * handle,
395 Shishi_asn1 encticketpart,
396 char **client, size_t * clientlen)
398 return shishi_principal_name_realm (handle,
399 encticketpart, "cname",
400 encticketpart, "crealm",
401 client, clientlen);