Fix standalone libtasn1.
[shishi.git] / lib / encapreppart.c
blob98ca93a635ddbbab9150e97037970fdb5d05bda2
1 /* encapreppart.c Key distribution encrypted reply part functions
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 /**
25 * shishi_encapreppart:
26 * @handle: shishi handle as allocated by shishi_init().
28 * This function creates a new EncAPRepPart, populated with some
29 * default values. It uses the current time as returned by the system
30 * for the ctime and cusec fields.
32 * Return value: Returns the encapreppart or NULL on failure.
33 **/
34 Shishi_asn1
35 shishi_encapreppart (Shishi * handle)
37 int res;
38 Shishi_asn1 node = NULL;
39 struct timeval tv;
40 struct timezone tz;
42 node = shishi_asn1_encapreppart (handle);
43 if (!node)
44 return NULL;
46 res = shishi_asn1_write (handle, node, "ctime",
47 shishi_generalize_time (handle, time (NULL)), 0);
48 if (res != SHISHI_OK)
49 goto error;
51 gettimeofday (&tv, &tz);
52 res = shishi_asn1_write_integer (handle, node, "cusec",
53 tv.tv_usec % 1000000);
54 if (res != SHISHI_OK)
55 goto error;
57 res = shishi_asn1_write (handle, node, "subkey", NULL, 0);
58 if (res != SHISHI_OK)
59 goto error;
61 res = shishi_asn1_write (handle, node, "seq-number", NULL, 0);
62 if (res != SHISHI_OK)
63 goto error;
65 return node;
67 error:
68 shishi_asn1_done (handle, node);
69 return NULL;
72 /**
73 * shishi_encapreppart_print:
74 * @handle: shishi handle as allocated by shishi_init().
75 * @fh: file handle open for writing.
76 * @encapreppart: EncAPRepPart to print.
78 * Print ASCII armored DER encoding of EncAPRepPart to file.
80 * Return value: Returns SHISHI_OK iff successful.
81 **/
82 int
83 shishi_encapreppart_print (Shishi * handle, FILE * fh,
84 Shishi_asn1 encapreppart)
86 return _shishi_print_armored_data (handle, fh, encapreppart,
87 "EncAPRepPart", NULL);
90 /**
91 * shishi_encapreppart_save:
92 * @handle: shishi handle as allocated by shishi_init().
93 * @fh: file handle open for writing.
94 * @encapreppart: EncAPRepPart to save.
96 * Save DER encoding of EncAPRepPart to file.
98 * Return value: Returns SHISHI_OK iff successful.
99 **/
101 shishi_encapreppart_save (Shishi * handle, FILE * fh,
102 Shishi_asn1 encapreppart)
104 return _shishi_save_data (handle, fh, encapreppart, "EncAPRepPart");
108 * shishi_encapreppart_to_file:
109 * @handle: shishi handle as allocated by shishi_init().
110 * @encapreppart: EncAPRepPart to save.
111 * @filetype: input variable specifying type of file to be written,
112 * see Shishi_filetype.
113 * @filename: input variable with filename to write to.
115 * Write EncAPRepPart to file in specified TYPE. The file will be
116 * truncated if it exists.
118 * Return value: Returns SHISHI_OK iff successful.
121 shishi_encapreppart_to_file (Shishi * handle, Shishi_asn1 encapreppart,
122 int filetype, char *filename)
124 FILE *fh;
125 int res;
127 if (VERBOSE (handle))
128 printf (_("Writing EncAPRepPart to %s...\n"), filename);
130 fh = fopen (filename, "w");
131 if (fh == NULL)
132 return SHISHI_FOPEN_ERROR;
134 if (VERBOSE (handle))
135 printf (_("Writing EncAPRepPart in %s format...\n"),
136 filetype == SHISHI_FILETYPE_TEXT ? "TEXT" : "DER");
138 if (filetype == SHISHI_FILETYPE_TEXT)
139 res = shishi_encapreppart_print (handle, fh, encapreppart);
140 else
141 res = shishi_encapreppart_save (handle, fh, encapreppart);
142 if (res != SHISHI_OK)
143 return res;
145 res = fclose (fh);
146 if (res != 0)
147 return SHISHI_FCLOSE_ERROR;
149 if (VERBOSE (handle))
150 printf (_("Writing EncAPRepPart to %s...done\n"), filename);
152 return SHISHI_OK;
156 * shishi_encapreppart_parse:
157 * @handle: shishi handle as allocated by shishi_init().
158 * @fh: file handle open for reading.
159 * @encapreppart: output variable with newly allocated EncAPRepPart.
161 * Read ASCII armored DER encoded EncAPRepPart from file and populate given
162 * variable.
164 * Return value: Returns SHISHI_OK iff successful.
167 shishi_encapreppart_parse (Shishi * handle, FILE * fh,
168 Shishi_asn1 * encapreppart)
170 return _shishi_encapreppart_input (handle, fh, encapreppart, 0);
174 * shishi_encapreppart_read:
175 * @handle: shishi handle as allocated by shishi_init().
176 * @fh: file handle open for reading.
177 * @encapreppart: output variable with newly allocated EncAPRepPart.
179 * Read DER encoded EncAPRepPart from file and populate given variable.
181 * Return value: Returns SHISHI_OK iff successful.
184 shishi_encapreppart_read (Shishi * handle, FILE * fh,
185 Shishi_asn1 * encapreppart)
187 return _shishi_encapreppart_input (handle, fh, encapreppart, 1);
191 * shishi_encapreppart_from_file:
192 * @handle: shishi handle as allocated by shishi_init().
193 * @encapreppart: output variable with newly allocated EncAPRepPart.
194 * @filetype: input variable specifying type of file to be read,
195 * see Shishi_filetype.
196 * @filename: input variable with filename to read from.
198 * Read EncAPRepPart from file in specified TYPE.
200 * Return value: Returns SHISHI_OK iff successful.
203 shishi_encapreppart_from_file (Shishi * handle, Shishi_asn1 * encapreppart,
204 int filetype, char *filename)
206 int res;
207 FILE *fh;
209 if (VERBOSE (handle))
210 printf (_("Reading EncAPRepPart from %s...\n"), filename);
212 fh = fopen (filename, "r");
213 if (fh == NULL)
214 return SHISHI_FOPEN_ERROR;
216 if (VERBOSE (handle))
217 printf (_("Reading EncAPRepPart in %s format...\n"),
218 filetype == SHISHI_FILETYPE_TEXT ? "TEXT" : "DER");
220 if (filetype == SHISHI_FILETYPE_TEXT)
221 res = shishi_encapreppart_parse (handle, fh, encapreppart);
222 else
223 res = shishi_encapreppart_read (handle, fh, encapreppart);
224 if (res != SHISHI_OK)
225 return res;
227 res = fclose (fh);
228 if (res != 0)
229 return SHISHI_FCLOSE_ERROR;
231 if (VERBOSE (handle))
232 printf (_("Reading EncAPRepPart from %s...done\n"), filename);
234 return SHISHI_OK;
238 * shishi_encapreppart_get_key:
239 * @handle: shishi handle as allocated by shishi_init().
240 * @encapreppart: input EncAPRepPart variable.
241 * @keytype: output variable that holds key type.
242 * @keyvalue: output array with key.
243 * @keyvalue_len: on input, maximum size of output array with key,
244 * on output, holds the actual size of output array with key.
246 * Extract the subkey from the encrypted AP-REP part.
248 * Return value: Returns SHISHI_OK iff succesful.
251 shishi_encapreppart_get_key (Shishi * handle,
252 Shishi_asn1 encapreppart,
253 int32_t * keytype,
254 char *keyvalue, size_t * keyvalue_len)
256 int res;
258 *keytype = 0;
259 res = shishi_asn1_read_int32 (handle, encapreppart,
260 "subkey.keytype", keytype);
261 if (res != SHISHI_OK)
262 return res;
264 res = shishi_asn1_read (handle, encapreppart,
265 "subkey.keyvalue", keyvalue, keyvalue_len);
266 if (res != SHISHI_OK)
267 return res;
269 return SHISHI_OK;
273 * shishi_encapreppart_ctime:
274 * @handle: shishi handle as allocated by shishi_init().
275 * @encapreppart: EncAPRepPart as allocated by shishi_encapreppart().
276 * @ctime: newly allocated zero-terminated character array with client time.
278 * Extract client time from EncAPRepPart.
280 * Return value: Returns SHISHI_OK iff successful.
283 shishi_encapreppart_ctime (Shishi * handle,
284 Shishi_asn1 encapreppart, char **ctime)
286 return shishi_time (handle, encapreppart, "ctime", ctime);
290 * shishi_encapreppart_ctime_set:
291 * @handle: shishi handle as allocated by shishi_init().
292 * @encapreppart: EncAPRepPart as allocated by shishi_encapreppart().
293 * @ctime: string with generalized time value to store in EncAPRepPart.
295 * Store client time in EncAPRepPart.
297 * Return value: Returns SHISHI_OK iff successful.
300 shishi_encapreppart_ctime_set (Shishi * handle,
301 Shishi_asn1 encapreppart, char *ctime)
303 int res;
305 res = shishi_asn1_write (handle, encapreppart, "ctime",
306 ctime, GENERALIZEDTIME_TIME_LEN);
307 if (res != SHISHI_OK)
308 return res;
310 return SHISHI_OK;
314 * shishi_encapreppart_cusec_get:
315 * @handle: shishi handle as allocated by shishi_init().
316 * @encapreppart: EncAPRepPart as allocated by shishi_encapreppart().
317 * @cusec: output integer with client microseconds field.
319 * Extract client microseconds field from EncAPRepPart.
321 * Return value: Returns SHISHI_OK iff successful.
324 shishi_encapreppart_cusec_get (Shishi * handle,
325 Shishi_asn1 encapreppart, int *cusec)
327 int res;
329 res = shishi_asn1_read_integer (handle, encapreppart, "cusec", cusec);
330 if (res != SHISHI_OK)
331 return res;
333 return res;
337 * shishi_encapreppart_cusec_set:
338 * @handle: shishi handle as allocated by shishi_init().
339 * @encapreppart: EncAPRepPart as allocated by shishi_encapreppart().
340 * @cusec: client microseconds to set in authenticator, 0-999999.
342 * Set the cusec field in the Authenticator.
344 * Return value: Returns SHISHI_OK iff successful.
347 shishi_encapreppart_cusec_set (Shishi * handle,
348 Shishi_asn1 encapreppart, int cusec)
350 int res;
352 res = shishi_asn1_write_integer (handle, encapreppart, "cusec", cusec);
353 if (res != SHISHI_OK)
354 return res;
356 return SHISHI_OK;
360 * shishi_encapreppart_seqnumber_get:
361 * @handle: shishi handle as allocated by shishi_init().
362 * @encapreppart: EncAPRepPart as allocated by shishi_encapreppart().
363 * @seqnumber: output integer with sequence number field.
365 * Extract sequence number field from EncAPRepPart.
367 * Return value: Returns SHISHI_OK iff successful.
370 shishi_encapreppart_seqnumber_get (Shishi * handle,
371 Shishi_asn1 encapreppart,
372 uint32_t * seqnumber)
374 int res;
376 res = shishi_asn1_read_uint32 (handle, encapreppart,
377 "seq-number", seqnumber);
378 if (res != SHISHI_OK)
379 return res;
381 return res;
385 * shishi_encapreppart_time_copy:
386 * @handle: shishi handle as allocated by shishi_init().
387 * @encapreppart: EncAPRepPart as allocated by shishi_encapreppart().
388 * @authenticator: Authenticator to copy time fields from.
390 * Copy time fields from Authenticator into EncAPRepPart.
392 * Return value: Returns SHISHI_OK iff successful.
395 shishi_encapreppart_time_copy (Shishi * handle,
396 Shishi_asn1 encapreppart,
397 Shishi_asn1 authenticator)
399 char *buf;
400 size_t buflen;
401 int res;
403 res = shishi_asn1_read2 (handle, authenticator, "cusec", &buf, &buflen);
404 if (res != SHISHI_OK)
405 return res;
407 res = shishi_asn1_write (handle, encapreppart, "cusec", buf, buflen);
408 free (buf);
409 if (res != SHISHI_OK)
410 return res;
412 res = shishi_asn1_read2 (handle, authenticator, "ctime", &buf, &buflen);
413 if (res != SHISHI_OK)
414 return res;
416 res = shishi_asn1_write (handle, encapreppart, "ctime", buf, buflen);
417 free (buf);
418 if (res != SHISHI_OK)
419 return res;
421 return SHISHI_OK;