Use new libtasn1 API.
[shishi.git] / lib / encticketpart.c
blobf6b1f531e5dc5651d0c30bd13208a6b996cfe353
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",
54 NULL, 0);
55 if (res != SHISHI_OK)
57 shishi_asn1_done (handle, node);
58 return NULL;
61 return node;
64 /**
65 * shishi_asn1ticket_get_enc_part_etype:
66 * @handle: shishi handle as allocated by shishi_init().
67 * @kdcrep: Ticket variable to get value from.
68 * @etype: output variable that holds the value.
70 * Extract Ticket.enc-part.etype.
72 * Return value: Returns SHISHI_OK iff successful.
73 **/
74 int
75 shishi_encticketpart_get_enc_part_etype (Shishi * handle,
76 Shishi_asn1 encticketpart,
77 int *etype)
79 int buflen;
80 int res;
82 *etype = 0;
83 buflen = sizeof (*etype);
84 res = shishi_asn1_field (handle, encticketpart, (char *) etype, &buflen,
85 "enc-part.etype");
87 return res;
90 /**
91 * shishi_encticketpart_get_key:
92 * @handle: shishi handle as allocated by shishi_init().
93 * @encticketpart: input EncTicketPart variable.
94 * @keytype: output variable that holds key type.
95 * @keyvalue: output array with key.
96 * @keyvalue_len: on input, maximum size of output array with key,
97 * on output, holds the actual size of output array with key.
99 * Extract the session key in the Ticket.
101 * Return value: Returns SHISHI_OK iff succesful.
104 shishi_encticketpart_get_key (Shishi * handle,
105 Shishi_asn1 encticketpart, Shishi_key ** key)
107 int res;
108 char buf[BUFSIZ];
109 int buflen;
110 int keytype;
112 res = shishi_asn1_integer_field (handle, encticketpart, &keytype,
113 "key.keytype");
114 if (res != SHISHI_OK)
115 return res;
117 buflen = BUFSIZ;
118 res = shishi_asn1_field (handle, encticketpart, buf, &buflen,
119 "key.keyvalue");
120 if (res != SHISHI_OK)
121 return res;
123 res = shishi_key_from_value (handle, keytype, buf, key);
124 if (res != SHISHI_OK)
125 return res;
127 return SHISHI_OK;
131 * shishi_encticketpart_key_set:
132 * @handle: shishi handle as allocated by shishi_init().
133 * @encticketpart: input EncTicketPart variable.
134 * @key: key handle with information to store in encticketpart.
136 * Set the EncTicketPart.key field to key type and value of supplied
137 * key.
139 * Return value: Returns SHISHI_OK iff succesful.
142 shishi_encticketpart_key_set (Shishi * handle,
143 Shishi_asn1 encticketpart, Shishi_key * key)
145 int res;
146 char buf[BUFSIZ];
147 int keytype;
149 keytype = shishi_key_type (key);
150 sprintf (buf, "%d", keytype);
151 res = shishi_asn1_write (handle, encticketpart, "key.keytype",
152 buf, 0);
153 if (res != SHISHI_OK)
154 return res;
156 res = shishi_asn1_write (handle, encticketpart,
157 "key.keyvalue",
158 shishi_key_value (key), shishi_key_length (key));
159 if (res != SHISHI_OK)
160 return res;
162 return SHISHI_OK;
166 * shishi_encticketpart_flags_set:
167 * @handle: shishi handle as allocated by shishi_init().
168 * @encticketpart: input EncTicketPart variable.
169 * @flags: flags to set in encticketpart.
171 * Set the EncTicketPart.flags to supplied value.
173 * Return value: Returns SHISHI_OK iff succesful.
176 shishi_encticketpart_flags_set (Shishi * handle,
177 Shishi_asn1 encticketpart, int flags)
179 int res;
180 char buf[BUFSIZ];
182 sprintf (buf, "%d", flags);
183 res = shishi_asn1_write (handle, encticketpart, "flags",
184 buf, 0);
185 if (res != SHISHI_OK)
186 return res;
188 return SHISHI_OK;
192 * shishi_encticketpart_crealm_set:
193 * @handle: shishi handle as allocated by shishi_init().
194 * @encticketpart: input EncTicketPart variable.
195 * @realm: input array with name of realm.
197 * Set the realm field in the KDC-REQ.
199 * Return value: Returns SHISHI_OK iff successful.
202 shishi_encticketpart_crealm_set (Shishi * handle,
203 Shishi_asn1 encticketpart, const char *realm)
205 int res;
207 res = shishi_asn1_write (handle, encticketpart, "crealm",
208 realm, 0);
209 if (res != SHISHI_OK)
210 return res;
212 return SHISHI_OK;
216 * shishi_encticketpart_cname_set:
217 * @handle: shishi handle as allocated by shishi_init().
218 * @encticketpart: input EncTicketPart variable.
219 * @name_type: type of principial, see Shishi_name_type, usually
220 * SHISHI_NT_UNKNOWN.
221 * @principal: input array with principal name.
223 * Set the client name field in the EncTicketPart.
225 * Return value: Returns SHISHI_OK iff successful.
228 shishi_encticketpart_cname_set (Shishi * handle,
229 Shishi_asn1 encticketpart,
230 Shishi_name_type name_type,
231 const char *principal)
233 int res;
234 char buf[BUFSIZ];
236 sprintf (buf, "%d", name_type);
238 res = shishi_asn1_write (handle, encticketpart,
239 "cname.name-type", buf, 0);
240 if (res != SHISHI_OK)
241 return res;
243 res = shishi_asn1_write (handle, encticketpart,
244 "cname.name-string", NULL, 0);
245 if (res != SHISHI_OK)
246 return res;
248 res = shishi_asn1_write (handle, encticketpart,
249 "cname.name-string", "NEW", 1);
250 if (res != SHISHI_OK)
251 return res;
253 res = shishi_asn1_write (handle, encticketpart,
254 "cname.name-string.?1",
255 principal, strlen (principal));
256 if (res != SHISHI_OK)
257 return res;
259 return SHISHI_OK;
263 * shishi_encticketpart_transited_set:
264 * @handle: shishi handle as allocated by shishi_init().
265 * @encticketpart: input EncTicketPart variable.
266 * @trtype: transitedencoding type, e.g. SHISHI_TR_DOMAIN_X500_COMPRESS.
267 * @trdata: actual transited realm data.
268 * @trdatalen: length of actual transited realm data.
270 * Set the EncTicketPart.transited field to supplied value.
272 * Return value: Returns SHISHI_OK iff succesful.
275 shishi_encticketpart_transited_set (Shishi * handle,
276 Shishi_asn1 encticketpart,
277 int trtype,
278 char *trdata, size_t trdatalen)
280 int res;
281 char buf[BUFSIZ];
283 sprintf (buf, "%d", trtype);
284 res = shishi_asn1_write (handle, encticketpart,
285 "transited.tr-type", buf, 0);
286 if (res != SHISHI_OK)
287 return res;
289 res = shishi_asn1_write (handle, encticketpart,
290 "transited.contents",
291 trdata, trdatalen);
292 if (res != SHISHI_OK)
293 return res;
295 return SHISHI_OK;
299 * shishi_encticketpart_authtime_set:
300 * @handle: shishi handle as allocated by shishi_init().
301 * @encticketpart: input EncTicketPart variable.
302 * @authtime: character buffer containing a generalized time string.
304 * Set the EncTicketPart.authtime to supplied value.
306 * Return value: Returns SHISHI_OK iff succesful.
309 shishi_encticketpart_authtime_set (Shishi * handle,
310 Shishi_asn1 encticketpart,
311 const char *authtime)
313 int res;
315 res = shishi_asn1_write (handle, encticketpart, "authtime",
316 authtime, GENERALIZEDTIME_TIME_LEN);
317 if (res != SHISHI_OK)
318 return SHISHI_ASN1_ERROR;
320 return SHISHI_OK;
324 * shishi_encticketpart_endtime_set:
325 * @handle: shishi handle as allocated by shishi_init().
326 * @encticketpart: input EncTicketPart variable.
327 * @endtime: character buffer containing a generalized time string.
329 * Set the EncTicketPart.endtime to supplied value.
331 * Return value: Returns SHISHI_OK iff succesful.
334 shishi_encticketpart_endtime_set (Shishi * handle,
335 Shishi_asn1 encticketpart,
336 const char *endtime)
338 int res;
340 res = shishi_asn1_write (handle, encticketpart, "endtime",
341 endtime, GENERALIZEDTIME_TIME_LEN);
342 if (res != SHISHI_OK)
343 return res;
345 return SHISHI_OK;
349 shishi_encticketpart_authtime (Shishi * handle,
350 Shishi_asn1 encticketpart,
351 char *authtime, int *authtimelen)
353 return shishi_asn1_field (handle, encticketpart, authtime, authtimelen,
354 "authtime");
357 time_t
358 shishi_encticketpart_authctime (Shishi * handle, Shishi_asn1 encticketpart)
360 char authtime[GENERALIZEDTIME_TIME_LEN + 1];
361 int authtimelen;
362 time_t t;
363 int res;
365 authtimelen = sizeof (authtime);
366 res = shishi_encticketpart_authtime (handle, encticketpart,
367 authtime, &authtimelen);
368 if (res != SHISHI_OK)
369 return (time_t) - 1;
371 authtime[GENERALIZEDTIME_TIME_LEN] = '\0';
373 t = shishi_generalize_ctime (handle, authtime);
375 return t;
379 shishi_encticketpart_cname_get (Shishi * handle,
380 Shishi_asn1 encticketpart,
381 char *cname, int *cnamelen)
383 return shishi_principal_name_get (handle, encticketpart,
384 "cname", cname, cnamelen);
388 shishi_encticketpart_cnamerealm_get (Shishi * handle,
389 Shishi_asn1 encticketpart,
390 char *cnamerealm, int *cnamerealmlen)
392 return shishi_principal_name_realm_get (handle, encticketpart,
393 "cname",
394 encticketpart,
395 "crealm",
396 cnamerealm, cnamerealmlen);