Bump versions.
[shishi.git] / lib / enckdcreppart.c
blob9280cdf5337e18e4ec9aa825f4c4fe0036b6bbf9
1 /* enckdcreppart.c --- Key distribution encrypted reply part functions
2 * Copyright (C) 2002, 2003, 2004, 2006 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"
25 Shishi_asn1
26 shishi_enckdcreppart (Shishi * handle)
28 int res;
29 Shishi_asn1 node;
31 node = shishi_asn1_enckdcreppart (handle);
32 if (!node)
33 return NULL;
35 /* XXX remove these two: */
36 res = shishi_asn1_write (handle, node, "key-expiration", NULL, 0);
37 if (res != SHISHI_OK)
38 return NULL;
40 res = shishi_asn1_write (handle, node, "caddr", NULL, 0);
41 if (res != SHISHI_OK)
42 return NULL;
44 res = shishi_enckdcreppart_flags_set (handle, node, 0);
45 if (res != SHISHI_OK)
46 return NULL;
48 return node;
51 Shishi_asn1
52 shishi_encasreppart (Shishi * handle)
54 int res;
55 Shishi_asn1 node;
57 node = shishi_asn1_encasreppart (handle);
58 if (!node)
59 return NULL;
61 /* XXX remove these two: */
62 res = shishi_asn1_write (handle, node, "key-expiration", NULL, 0);
63 if (res != SHISHI_OK)
64 puts ("urk");
65 res = shishi_asn1_write (handle, node, "caddr", NULL, 0);
66 if (res != SHISHI_OK)
67 puts ("urk2");
69 return node;
72 /**
73 * shishi_enckdcreppart_get_key:
74 * @handle: shishi handle as allocated by shishi_init().
75 * @enckdcreppart: input EncKDCRepPart variable.
76 * @key: newly allocated encryption key handle.
78 * Extract the key to use with the ticket sent in the KDC-REP
79 * associated with the EncKDCRepPart input variable.
81 * Return value: Returns SHISHI_OK iff succesful.
82 **/
83 int
84 shishi_enckdcreppart_get_key (Shishi * handle,
85 Shishi_asn1 enckdcreppart, Shishi_key ** key)
87 int res;
88 char *buf;
89 size_t buflen;
90 int32_t keytype;
92 res = shishi_asn1_read_int32 (handle, enckdcreppart,
93 "key.keytype", &keytype);
94 if (res != SHISHI_OK)
95 return res;
97 res = shishi_asn1_read (handle, enckdcreppart, "key.keyvalue",
98 &buf, &buflen);
99 if (res != SHISHI_OK)
100 return res;
102 res = shishi_key_from_value (handle, keytype, buf, key);
103 free (buf);
104 if (res != SHISHI_OK)
105 return res;
107 return SHISHI_OK;
111 * shishi_enckdcreppart_key_set:
112 * @handle: shishi handle as allocated by shishi_init().
113 * @enckdcreppart: input EncKDCRepPart variable.
114 * @key: key handle with information to store in enckdcreppart.
116 * Set the EncKDCRepPart.key field to key type and value of supplied
117 * key.
119 * Return value: Returns SHISHI_OK iff succesful.
122 shishi_enckdcreppart_key_set (Shishi * handle,
123 Shishi_asn1 enckdcreppart, Shishi_key * key)
125 int res;
127 res = shishi_asn1_write_integer (handle, enckdcreppart, "key.keytype",
128 shishi_key_type (key));
129 if (res != SHISHI_OK)
130 return SHISHI_ASN1_ERROR;
132 res = shishi_asn1_write (handle, enckdcreppart, "key.keyvalue",
133 shishi_key_value (key), shishi_key_length (key));
134 if (res != SHISHI_OK)
135 return SHISHI_ASN1_ERROR;
137 return SHISHI_OK;
141 * shishi_enckdcreppart_nonce_set:
142 * @handle: shishi handle as allocated by shishi_init().
143 * @enckdcreppart: input EncKDCRepPart variable.
144 * @nonce: nonce to set in EncKDCRepPart.
146 * Set the EncKDCRepPart.nonce field.
148 * Return value: Returns SHISHI_OK iff succesful.
151 shishi_enckdcreppart_nonce_set (Shishi * handle,
152 Shishi_asn1 enckdcreppart, uint32_t nonce)
154 int res;
156 res = shishi_asn1_write_uint32 (handle, enckdcreppart, "nonce", nonce);
157 if (res != SHISHI_OK)
158 return res;
160 return SHISHI_OK;
164 * shishi_enckdcreppart_flags_set:
165 * @handle: shishi handle as allocated by shishi_init().
166 * @enckdcreppart: input EncKDCRepPart variable.
167 * @flags: flags to set in EncKDCRepPart.
169 * Set the EncKDCRepPart.flags field.
171 * Return value: Returns SHISHI_OK iff succesful.
174 shishi_enckdcreppart_flags_set (Shishi * handle,
175 Shishi_asn1 enckdcreppart, int flags)
177 int res;
179 res = shishi_asn1_write_bitstring (handle, enckdcreppart, "flags", flags);
180 if (res != SHISHI_OK)
181 return res;
183 return SHISHI_OK;
187 * shishi_enckdcreppart_authtime_set:
188 * @handle: shishi handle as allocated by shishi_init().
189 * @enckdcreppart: input EncKDCRepPart variable.
190 * @authtime: character buffer containing a generalized time string.
192 * Set the EncTicketPart.authtime to supplied value.
194 * Return value: Returns SHISHI_OK iff succesful.
197 shishi_enckdcreppart_authtime_set (Shishi * handle,
198 Shishi_asn1 enckdcreppart,
199 const char *authtime)
201 int res;
203 res = shishi_asn1_write (handle, enckdcreppart, "authtime",
204 authtime, SHISHI_GENERALIZEDTIME_LENGTH);
205 if (res != SHISHI_OK)
206 return res;
208 return SHISHI_OK;
212 * shishi_enckdcreppart_starttime_set:
213 * @handle: shishi handle as allocated by shishi_init().
214 * @enckdcreppart: input EncKDCRepPart variable.
215 * @starttime: character buffer containing a generalized time string.
217 * Set the EncTicketPart.starttime to supplied value. Use a NULL
218 * value for @starttime to remove the field.
220 * Return value: Returns SHISHI_OK iff succesful.
223 shishi_enckdcreppart_starttime_set (Shishi * handle,
224 Shishi_asn1 enckdcreppart,
225 const char *starttime)
227 int res;
229 if (!starttime)
230 res = shishi_asn1_write (handle, enckdcreppart, "starttime", NULL, 0);
231 else
232 res = shishi_asn1_write (handle, enckdcreppart, "starttime",
233 starttime, SHISHI_GENERALIZEDTIME_LENGTH);
234 if (res != SHISHI_OK)
235 return res;
237 return SHISHI_OK;
241 * shishi_enckdcreppart_endtime_set:
242 * @handle: shishi handle as allocated by shishi_init().
243 * @enckdcreppart: input EncKDCRepPart variable.
244 * @endtime: character buffer containing a generalized time string.
246 * Set the EncTicketPart.endtime to supplied value.
248 * Return value: Returns SHISHI_OK iff succesful.
251 shishi_enckdcreppart_endtime_set (Shishi * handle,
252 Shishi_asn1 enckdcreppart,
253 const char *endtime)
255 int res;
257 res = shishi_asn1_write (handle, enckdcreppart, "endtime",
258 endtime, SHISHI_GENERALIZEDTIME_LENGTH);
259 if (res != SHISHI_OK)
260 return res;
262 return SHISHI_OK;
266 * shishi_enckdcreppart_renew_till_set:
267 * @handle: shishi handle as allocated by shishi_init().
268 * @enckdcreppart: input EncKDCRepPart variable.
269 * @renew_till: character buffer containing a generalized time string.
271 * Set the EncTicketPart.renew-till to supplied value. Use a NULL
272 * value for @renew_till to remove the field.
274 * Return value: Returns SHISHI_OK iff succesful.
277 shishi_enckdcreppart_renew_till_set (Shishi * handle,
278 Shishi_asn1 enckdcreppart,
279 const char *renew_till)
281 int res;
283 if (!renew_till)
284 res = shishi_asn1_write (handle, enckdcreppart, "renew-till", NULL, 0);
285 else
286 res = shishi_asn1_write (handle, enckdcreppart, "renew-till",
287 renew_till, SHISHI_GENERALIZEDTIME_LENGTH);
288 if (res != SHISHI_OK)
289 return res;
291 return SHISHI_OK;
295 * shishi_enckdcreppart_srealm_set:
296 * @handle: shishi handle as allocated by shishi_init().
297 * @enckdcreppart: EncKDCRepPart variable to set realm field in.
298 * @srealm: input array with name of realm.
300 * Set the server realm field in the EncKDCRepPart.
302 * Return value: Returns SHISHI_OK iff successful.
305 shishi_enckdcreppart_srealm_set (Shishi * handle,
306 Shishi_asn1 enckdcreppart,
307 const char *srealm)
309 int res = SHISHI_OK;
311 res = shishi_asn1_write (handle, enckdcreppart, "srealm", srealm, 0);
312 if (res != SHISHI_OK)
313 return res;
315 return SHISHI_OK;
320 * shishi_enckdcreppart_sname_set:
321 * @handle: shishi handle as allocated by shishi_init().
322 * @enckdcreppart: EncKDCRepPart variable to set server name field in.
323 * @name_type: type of principial, see Shishi_name_type, usually
324 * SHISHI_NT_UNKNOWN.
325 * @sname: input array with principal name.
327 * Set the server name field in the EncKDCRepPart.
329 * Return value: Returns SHISHI_OK iff successful.
332 shishi_enckdcreppart_sname_set (Shishi * handle,
333 Shishi_asn1 enckdcreppart,
334 Shishi_name_type name_type, char *sname[])
336 int res = SHISHI_OK;
337 int i;
338 char *buf;
340 res = shishi_asn1_write_integer (handle, enckdcreppart,
341 "sname.name-type", name_type);
342 if (res != SHISHI_OK)
343 return res;
345 res = shishi_asn1_write (handle, enckdcreppart,
346 "sname.name-string", NULL, 0);
347 if (res != SHISHI_OK)
348 return res;
350 i = 1;
351 while (sname[i - 1])
353 res = shishi_asn1_write (handle, enckdcreppart, "sname.name-string",
354 "NEW", 1);
355 if (res != SHISHI_OK)
356 return res;
358 asprintf (&buf, "sname.name-string.?%d", i);
359 res = shishi_asn1_write (handle, enckdcreppart, buf, sname[i - 1], 0);
360 free (buf);
361 if (res != SHISHI_OK)
362 return res;
364 i++;
367 return SHISHI_OK;
371 shishi_enckdcreppart_server_set (Shishi * handle,
372 Shishi_asn1 enckdcreppart,
373 const char *server)
375 char *tmpserver;
376 char **serverbuf;
377 char *tokptr;
378 int res;
379 int i;
381 tmpserver = xstrdup (server);
383 serverbuf = xmalloc (sizeof (*serverbuf));
384 for (i = 0;
385 (serverbuf[i] = strtok_r (i == 0 ? tmpserver : NULL, "/", &tokptr));
386 i++)
388 serverbuf = xrealloc (serverbuf, (i + 2) * sizeof (*serverbuf));
391 res = shishi_enckdcreppart_sname_set (handle, enckdcreppart,
392 SHISHI_NT_PRINCIPAL, serverbuf);
393 if (res != SHISHI_OK)
394 return res;
396 free (serverbuf);
397 free (tmpserver);
399 return SHISHI_OK;
403 shishi_enckdcreppart_srealmserver_set (Shishi * handle,
404 Shishi_asn1 enckdcreppart,
405 const char *srealm, const char *server)
407 int res;
409 res = shishi_enckdcreppart_srealm_set (handle, enckdcreppart, srealm);
410 if (res != SHISHI_OK)
411 return res;
413 res = shishi_enckdcreppart_server_set (handle, enckdcreppart, server);
414 if (res != SHISHI_OK)
415 return res;
417 return SHISHI_OK;
421 * shishi_enckdcreppart_populate_encticketpart:
422 * @handle: shishi handle as allocated by shishi_init().
423 * @enckdcreppart: input EncKDCRepPart variable.
424 * @encticketpart: input EncTicketPart variable.
426 * Set the flags, authtime, starttime, endtime, renew-till and caddr
427 * fields of the EncKDCRepPart to the corresponding values in the
428 * EncTicketPart.
430 * Return value: Returns SHISHI_OK iff succesful.
433 shishi_enckdcreppart_populate_encticketpart (Shishi * handle,
434 Shishi_asn1 enckdcreppart,
435 Shishi_asn1 encticketpart)
437 char *buf;
438 size_t buflen;
439 int res;
441 res = shishi_asn1_read (handle, encticketpart, "flags", &buf, &buflen);
442 if (res != SHISHI_OK)
443 return SHISHI_ASN1_ERROR;
445 res = shishi_asn1_write (handle, enckdcreppart, "flags", buf, buflen);
446 free (buf);
447 if (res != SHISHI_OK)
448 return SHISHI_ASN1_ERROR;
450 res = shishi_asn1_read (handle, encticketpart, "authtime", &buf, &buflen);
451 if (res != SHISHI_OK)
452 return SHISHI_ASN1_ERROR;
454 res = shishi_asn1_write (handle, enckdcreppart, "authtime", buf, buflen);
455 free (buf);
456 if (res != SHISHI_OK)
457 return SHISHI_ASN1_ERROR;
459 res = shishi_asn1_read (handle, encticketpart, "starttime", &buf, &buflen);
460 if (res != SHISHI_OK && res != SHISHI_ASN1_NO_ELEMENT)
461 return SHISHI_ASN1_ERROR;
463 if (res == SHISHI_ASN1_NO_ELEMENT)
464 res = shishi_asn1_write (handle, enckdcreppart, "starttime", NULL, 0);
465 else
467 res = shishi_asn1_write (handle, enckdcreppart, "starttime",
468 buf, buflen);
469 free (buf);
471 if (res != SHISHI_OK)
472 return SHISHI_ASN1_ERROR;
474 res = shishi_asn1_read (handle, encticketpart, "endtime", &buf, &buflen);
475 if (res != SHISHI_OK)
476 return SHISHI_ASN1_ERROR;
478 res = shishi_asn1_write (handle, enckdcreppart, "endtime", buf, buflen);
479 free (buf);
480 if (res != SHISHI_OK)
481 return SHISHI_ASN1_ERROR;
483 res = shishi_asn1_read (handle, encticketpart, "renew-till", &buf, &buflen);
484 if (res != SHISHI_OK && res != SHISHI_ASN1_NO_ELEMENT)
485 return SHISHI_ASN1_ERROR;
487 if (res == SHISHI_ASN1_NO_ELEMENT)
488 res = shishi_asn1_write (handle, enckdcreppart, "renew-till", NULL, 0);
489 else
491 res = shishi_asn1_write (handle, enckdcreppart,
492 "renew-till", buf, buflen);
493 free (buf);
495 if (res != SHISHI_OK)
496 return SHISHI_ASN1_ERROR;
498 /* XXX copy caddr too */
500 return SHISHI_OK;