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
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
);
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.
42 shishi_ticket_realm_set (Shishi
* handle
, Shishi_asn1 ticket
,
47 res
= shishi_asn1_write (handle
, ticket
, "realm", realm
, 0);
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",
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
68 * @sname: input array with principal name.
70 * Set the server name field in the Ticket.
72 * Return value: Returns SHISHI_OK iff successful.
75 shishi_ticket_sname_set (Shishi
* handle
,
77 Shishi_name_type name_type
, char *sname
[])
83 asprintf (&buf
, "%d", name_type
);
84 res
= shishi_asn1_write (handle
, ticket
, "sname.name-type", buf
, 0);
89 res
= shishi_asn1_write (handle
, ticket
, "sname.name-string", NULL
, 0);
96 res
= shishi_asn1_write (handle
, ticket
, "sname.name-string", "NEW", 1);
100 asprintf (&buf
, "sname.name-string.?%d", i
);
101 res
= shishi_asn1_write (handle
, ticket
, buf
, sname
[i
- 1], 0);
103 if (res
!= SHISHI_OK
)
113 shishi_ticket_set_server (Shishi
* handle
,
114 Shishi_asn1 ticket
, const char *server
)
122 tmpserver
= strdup (server
);
123 if (tmpserver
== NULL
)
124 return SHISHI_MALLOC_ERROR
;
126 serverbuf
= malloc (sizeof (*serverbuf
));
128 (serverbuf
[i
] = strtok_r (i
== 0 ? tmpserver
: NULL
, "/", &tokptr
));
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
));
150 shishi_ticket_snamerealm_get (Shishi
* handle
,
152 char *serverrealm
, int *serverrealmlen
)
154 return shishi_principal_name_realm_get (handle
, ticket
, "sname",
156 serverrealm
, serverrealmlen
);
160 shishi_ticket_srealmserver_set (Shishi
* handle
,
161 Shishi_asn1 ticket
, char *realm
, char *server
)
165 res
= shishi_ticket_realm_set (handle
, ticket
, realm
);
166 if (res
!= SHISHI_OK
)
169 res
= shishi_ticket_set_server (handle
, ticket
, server
);
170 if (res
!= 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
)
192 res
= shishi_asn1_read_int32 (handle
, ticket
, "enc-part.etype", etype
);
198 shishi_ticket_decrypt (Shishi
* handle
,
200 Shishi_key
* key
, Shishi_asn1
* encticketpart
)
210 res
= shishi_ticket_get_enc_part_etype (handle
, ticket
, &etype
);
211 if (res
!= SHISHI_OK
)
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
)
222 res
= shishi_decrypt (handle
, key
, SHISHI_KEYUSAGE_ENCTICKETPART
,
223 cipher
, cipherlen
, &buf
, &buflen
);
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],
241 if (*encticketpart
!= NULL
)
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
;
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
,
275 int etype
, int kvno
, char *buf
, size_t buflen
)
279 res
= shishi_asn1_write (handle
, ticket
, "enc-part.cipher", buf
, buflen
);
280 if (res
!= SHISHI_OK
)
283 res
= shishi_asn1_write_int32 (handle
, ticket
, "enc-part.etype", etype
);
284 if (res
!= SHISHI_OK
)
288 res
= shishi_asn1_write (handle
, ticket
, "enc-part.kvno", NULL
, 0);
290 res
= shishi_asn1_write_uint32 (handle
, ticket
, "enc-part.kvno", kvno
);
291 if (res
!= 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
307 * Return value: Returns SHISHI_OK iff successful.
310 shishi_ticket_add_enc_part (Shishi
* handle
,
312 Shishi_key
* key
, Shishi_asn1 encticketpart
)
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
));
328 res
= shishi_encrypt (handle
, key
, SHISHI_KEYUSAGE_ENCTICKETPART
,
329 der
, derlen
, &buf
, &buflen
);
333 if (res
!= SHISHI_OK
)
335 shishi_error_printf (handle
,
336 "Cannot encrypt encrypted part of ticket\n");
340 res
= shishi_ticket_set_enc_part (handle
, ticket
, shishi_key_type (key
),
341 shishi_key_version (key
), buf
, buflen
);