Fix -Wall warnings.
[shishi.git] / lib / ticket.c
blobe6f0cbc81bbdbd765943e2c08daf03fcf180143a
1 /* ticket.c low-level ASN.1 Ticket 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 int
25 shishi_ticket_realm_get (Shishi * handle,
26 Shishi_asn1 ticket, char *realm, int *realmlen)
28 return shishi_asn1_read (handle, ticket, "realm", realm, realmlen);
31 /**
32 * shishi_ticket_realm_set:
33 * @handle: shishi handle as allocated by shishi_init().
34 * @ticket: input variable with ticket info.
35 * @realm: input array with name of realm.
37 * Set the realm field in the Ticket.
39 * Return value: Returns SHISHI_OK iff successful.
40 **/
41 int
42 shishi_ticket_realm_set (Shishi * handle, Shishi_asn1 ticket,
43 const char *realm)
45 int res;
47 res = shishi_asn1_write (handle, ticket, "realm", realm, 0);
48 if (res != SHISHI_OK)
49 return res;
51 return SHISHI_OK;
54 int
55 shishi_ticket_sname_get (Shishi * handle,
56 Shishi_asn1 ticket, char *server, size_t * serverlen)
58 return shishi_principal_name_get (handle, ticket, "sname",
59 server, serverlen);
62 /**
63 * shishi_ticket_sname_set:
64 * @handle: shishi handle as allocated by shishi_init().
65 * @ticket: Ticket variable to set server name field in.
66 * @name_type: type of principial, see Shishi_name_type, usually
67 * SHISHI_NT_UNKNOWN.
68 * @sname: input array with principal name.
70 * Set the server name field in the Ticket.
72 * Return value: Returns SHISHI_OK iff successful.
73 **/
74 int
75 shishi_ticket_sname_set (Shishi * handle,
76 Shishi_asn1 ticket,
77 Shishi_name_type name_type, char *sname[])
79 int res = SHISHI_OK;
80 char *buf;
81 int i;
83 asprintf (&buf, "%d", name_type);
84 res = shishi_asn1_write (handle, ticket, "sname.name-type", buf, 0);
85 free (buf);
86 if (res != SHISHI_OK)
87 return res;
89 res = shishi_asn1_write (handle, ticket, "sname.name-string", NULL, 0);
90 if (res != SHISHI_OK)
91 return res;
93 i = 1;
94 while (sname[i - 1])
96 res = shishi_asn1_write (handle, ticket, "sname.name-string", "NEW", 1);
97 if (res != SHISHI_OK)
98 return res;
100 asprintf (&buf, "sname.name-string.?%d", i);
101 res = shishi_asn1_write (handle, ticket, buf, sname[i - 1], 0);
102 free (buf);
103 if (res != SHISHI_OK)
104 return res;
106 i++;
109 return SHISHI_OK;
113 shishi_ticket_set_server (Shishi * handle,
114 Shishi_asn1 ticket, const char *server)
116 char *tmpserver;
117 char **serverbuf;
118 char *tokptr;
119 int res;
120 int i;
122 tmpserver = strdup (server);
123 if (tmpserver == NULL)
124 return SHISHI_MALLOC_ERROR;
126 serverbuf = malloc (sizeof (*serverbuf));
127 for (i = 0;
128 (serverbuf[i] = strtok_r (i == 0 ? tmpserver : NULL, "/", &tokptr));
129 i++)
131 serverbuf = realloc (serverbuf, (i + 2) * sizeof (*serverbuf));
132 if (serverbuf == NULL)
133 return SHISHI_MALLOC_ERROR;
135 res = shishi_ticket_sname_set (handle, ticket,
136 SHISHI_NT_PRINCIPAL, serverbuf);
137 if (res != SHISHI_OK)
139 fprintf (stderr, _("Could not set sname: %s\n"),
140 shishi_strerror_details (handle));
141 return res;
143 free (serverbuf);
144 free (tmpserver);
146 return SHISHI_OK;
150 shishi_ticket_snamerealm_get (Shishi * handle,
151 Shishi_asn1 ticket,
152 char *serverrealm, int *serverrealmlen)
154 return shishi_principal_name_realm_get (handle, ticket, "sname",
155 ticket, "realm",
156 serverrealm, serverrealmlen);
160 shishi_ticket_srealmserver_set (Shishi * handle,
161 Shishi_asn1 ticket, char *realm, char *server)
163 int res;
165 res = shishi_ticket_realm_set (handle, ticket, realm);
166 if (res != SHISHI_OK)
167 return res;
169 res = shishi_ticket_set_server (handle, ticket, server);
170 if (res != SHISHI_OK)
171 return res;
173 return SHISHI_OK;
177 * shishi_ticket_get_enc_part_etype:
178 * @handle: shishi handle as allocated by shishi_init().
179 * @ticket: Ticket variable to get value from.
180 * @etype: output variable that holds the value.
182 * Extract Ticket.enc-part.etype.
184 * Return value: Returns SHISHI_OK iff successful.
187 shishi_ticket_get_enc_part_etype (Shishi * handle,
188 Shishi_asn1 ticket, int32_t * etype)
190 int res;
192 res = shishi_asn1_read_int32 (handle, ticket, "enc-part.etype", etype);
194 return res;
198 shishi_ticket_decrypt (Shishi * handle,
199 Shishi_asn1 ticket,
200 Shishi_key * key, Shishi_asn1 * encticketpart)
202 int res;
203 int i;
204 char *buf;
205 size_t buflen;
206 char *cipher;
207 size_t cipherlen;
208 int etype;
210 res = shishi_ticket_get_enc_part_etype (handle, ticket, &etype);
211 if (res != SHISHI_OK)
212 return res;
214 if (etype != shishi_key_type (key))
215 return SHISHI_TICKET_BAD_KEYTYPE;
217 res = shishi_asn1_read2 (handle, ticket, "enc-part.cipher",
218 &cipher, &cipherlen);
219 if (res != SHISHI_OK)
220 return res;
222 res = shishi_decrypt (handle, key, SHISHI_KEYUSAGE_ENCTICKETPART,
223 cipher, cipherlen, &buf, &buflen);
224 free (cipher);
225 if (res != SHISHI_OK)
227 shishi_error_printf (handle,
228 "Ticket decrypt failed, wrong password?\n");
229 return SHISHI_TICKET_DECRYPT_FAILED;
232 /* The crypto is so 1980; no length indicator. Trim off pad bytes
233 until we can parse it. */
234 for (i = 0; i < 8; i++)
236 if (VERBOSEASN1 (handle))
237 printf ("Trying with %d pad in enckdcrep...\n", i);
239 *encticketpart = shishi_der2asn1_encticketpart (handle, &buf[0],
240 buflen - i);
241 if (*encticketpart != NULL)
242 break;
245 if (*encticketpart == NULL)
247 shishi_error_printf (handle, "Could not DER decode EncTicketPart. "
248 "Password probably correct (decrypt ok) though\n");
249 return SHISHI_ASN1_ERROR;
252 return SHISHI_OK;
256 * shishi_ticket_set_enc_part:
257 * @handle: shishi handle as allocated by shishi_init().
258 * @ticket: Ticket to add enc-part field to.
259 * @etype: encryption type used to encrypt enc-part.
260 * @kvno: key version number.
261 * @buf: input array with encrypted enc-part.
262 * @buflen: size of input array with encrypted enc-part.
264 * Set the encrypted enc-part field in the Ticket. The encrypted data
265 * is usually created by calling shishi_encrypt() on the DER encoded
266 * enc-part. To save time, you may want to use
267 * shishi_ticket_add_enc_part() instead, which calculates the
268 * encrypted data and calls this function in one step.
270 * Return value: Returns SHISHI_OK iff successful.
273 shishi_ticket_set_enc_part (Shishi * handle,
274 Shishi_asn1 ticket,
275 int etype, int kvno, char *buf, size_t buflen)
277 int res = SHISHI_OK;
279 res = shishi_asn1_write (handle, ticket, "enc-part.cipher", buf, buflen);
280 if (res != SHISHI_OK)
281 return res;
283 res = shishi_asn1_write_int32 (handle, ticket, "enc-part.etype", etype);
284 if (res != SHISHI_OK)
285 return res;
287 if (kvno == 0)
288 res = shishi_asn1_write (handle, ticket, "enc-part.kvno", NULL, 0);
289 else
290 res = shishi_asn1_write_uint32 (handle, ticket, "enc-part.kvno", kvno);
291 if (res != SHISHI_OK)
292 return res;
294 return SHISHI_OK;
298 * shishi_ticket_add_enc_part:
299 * @handle: shishi handle as allocated by shishi_init().
300 * @ticket: Ticket to add enc-part field to.
301 * @key: key used to encrypt enc-part.
302 * @encticketpart: EncTicketPart to add.
304 * Encrypts DER encoded EncTicketPart using key and stores it in the
305 * Ticket.
307 * Return value: Returns SHISHI_OK iff successful.
310 shishi_ticket_add_enc_part (Shishi * handle,
311 Shishi_asn1 ticket,
312 Shishi_key * key, Shishi_asn1 encticketpart)
314 int res = SHISHI_OK;
315 char *buf;
316 size_t buflen;
317 char *der;
318 size_t derlen;
320 res = shishi_new_a2d (handle, encticketpart, &der, &derlen);
321 if (res != SHISHI_OK)
323 shishi_error_printf (handle, "Could not DER encode encticketpart: %s\n",
324 shishi_strerror (res));
325 return res;
328 res = shishi_encrypt (handle, key, SHISHI_KEYUSAGE_ENCTICKETPART,
329 der, derlen, &buf, &buflen);
331 free (der);
333 if (res != SHISHI_OK)
335 shishi_error_printf (handle,
336 "Cannot encrypt encrypted part of ticket\n");
337 return res;
340 res = shishi_ticket_set_enc_part (handle, ticket, shishi_key_type (key),
341 shishi_key_version (key), buf, buflen);
343 free (buf);
345 return res;