Fix.
[shishi.git] / lib / encapreppart.c
blob9df69ac57c70e7b1d384f31223e3b22e58b47c4b
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 Shishi_asn1
25 shishi_encapreppart (Shishi * handle)
27 int res;
28 Shishi_asn1 node = NULL;
29 struct timeval tv;
30 struct timezone tz;
32 node = shishi_asn1_encapreppart (handle);
33 if (!node)
34 return NULL;
36 res = shishi_asn1_write (handle, node, "ctime",
37 shishi_generalize_time (handle, time (NULL)), 0);
38 if (res != SHISHI_OK)
39 goto error;
41 gettimeofday (&tv, &tz);
42 res = shishi_asn1_write_integer (handle, node, "cusec",
43 tv.tv_usec % 1000000);
44 if (res != SHISHI_OK)
45 goto error;
47 res = shishi_asn1_write (handle, node, "subkey", NULL, 0);
48 if (res != SHISHI_OK)
49 goto error;
51 res = shishi_asn1_write (handle, node, "seq-number", NULL, 0);
52 if (res != SHISHI_OK)
53 goto error;
55 return node;
57 error:
58 shishi_asn1_done (handle, node);
59 return NULL;
62 /**
63 * shishi_encapreppart_print:
64 * @handle: shishi handle as allocated by shishi_init().
65 * @fh: file handle open for writing.
66 * @encapreppart: EncAPRepPart to print.
68 * Print ASCII armored DER encoding of EncAPRepPart to file.
70 * Return value: Returns SHISHI_OK iff successful.
71 **/
72 int
73 shishi_encapreppart_print (Shishi * handle, FILE * fh,
74 Shishi_asn1 encapreppart)
76 return _shishi_print_armored_data (handle, fh, encapreppart,
77 "EncAPRepPart", NULL);
80 /**
81 * shishi_encapreppart_save:
82 * @handle: shishi handle as allocated by shishi_init().
83 * @fh: file handle open for writing.
84 * @encapreppart: EncAPRepPart to save.
86 * Save DER encoding of EncAPRepPart to file.
88 * Return value: Returns SHISHI_OK iff successful.
89 **/
90 int
91 shishi_encapreppart_save (Shishi * handle, FILE * fh,
92 Shishi_asn1 encapreppart)
94 return _shishi_save_data (handle, fh, encapreppart, "EncAPRepPart");
97 /**
98 * shishi_encapreppart_to_file:
99 * @handle: shishi handle as allocated by shishi_init().
100 * @encapreppart: EncAPRepPart to save.
101 * @filetype: input variable specifying type of file to be written,
102 * see Shishi_filetype.
103 * @filename: input variable with filename to write to.
105 * Write EncAPRepPart to file in specified TYPE. The file will be
106 * truncated if it exists.
108 * Return value: Returns SHISHI_OK iff successful.
111 shishi_encapreppart_to_file (Shishi * handle, Shishi_asn1 encapreppart,
112 int filetype, char *filename)
114 FILE *fh;
115 int res;
117 if (VERBOSE (handle))
118 printf (_("Writing EncAPRepPart to %s...\n"), filename);
120 fh = fopen (filename, "w");
121 if (fh == NULL)
122 return SHISHI_FOPEN_ERROR;
124 if (VERBOSE (handle))
125 printf (_("Writing EncAPRepPart in %s format...\n"),
126 filetype == SHISHI_FILETYPE_TEXT ? "TEXT" : "DER");
128 if (filetype == SHISHI_FILETYPE_TEXT)
129 res = shishi_encapreppart_print (handle, fh, encapreppart);
130 else
131 res = shishi_encapreppart_save (handle, fh, encapreppart);
132 if (res != SHISHI_OK)
133 return res;
135 res = fclose (fh);
136 if (res != 0)
137 return SHISHI_FCLOSE_ERROR;
139 if (VERBOSE (handle))
140 printf (_("Writing EncAPRepPart to %s...done\n"), filename);
142 return SHISHI_OK;
146 * shishi_encapreppart_parse:
147 * @handle: shishi handle as allocated by shishi_init().
148 * @fh: file handle open for reading.
149 * @encapreppart: output variable with newly allocated EncAPRepPart.
151 * Read ASCII armored DER encoded EncAPRepPart from file and populate given
152 * variable.
154 * Return value: Returns SHISHI_OK iff successful.
157 shishi_encapreppart_parse (Shishi * handle, FILE * fh,
158 Shishi_asn1 * encapreppart)
160 return _shishi_encapreppart_input (handle, fh, encapreppart, 0);
164 * shishi_encapreppart_read:
165 * @handle: shishi handle as allocated by shishi_init().
166 * @fh: file handle open for reading.
167 * @encapreppart: output variable with newly allocated EncAPRepPart.
169 * Read DER encoded EncAPRepPart from file and populate given variable.
171 * Return value: Returns SHISHI_OK iff successful.
174 shishi_encapreppart_read (Shishi * handle, FILE * fh,
175 Shishi_asn1 * encapreppart)
177 return _shishi_encapreppart_input (handle, fh, encapreppart, 1);
181 * shishi_encapreppart_from_file:
182 * @handle: shishi handle as allocated by shishi_init().
183 * @encapreppart: output variable with newly allocated EncAPRepPart.
184 * @filetype: input variable specifying type of file to be read,
185 * see Shishi_filetype.
186 * @filename: input variable with filename to read from.
188 * Read EncAPRepPart from file in specified TYPE.
190 * Return value: Returns SHISHI_OK iff successful.
193 shishi_encapreppart_from_file (Shishi * handle, Shishi_asn1 * encapreppart,
194 int filetype, char *filename)
196 int res;
197 FILE *fh;
199 if (VERBOSE (handle))
200 printf (_("Reading EncAPRepPart from %s...\n"), filename);
202 fh = fopen (filename, "r");
203 if (fh == NULL)
204 return SHISHI_FOPEN_ERROR;
206 if (VERBOSE (handle))
207 printf (_("Reading EncAPRepPart in %s format...\n"),
208 filetype == SHISHI_FILETYPE_TEXT ? "TEXT" : "DER");
210 if (filetype == SHISHI_FILETYPE_TEXT)
211 res = shishi_encapreppart_parse (handle, fh, encapreppart);
212 else
213 res = shishi_encapreppart_read (handle, fh, encapreppart);
214 if (res != SHISHI_OK)
215 return res;
217 res = fclose (fh);
218 if (res != 0)
219 return SHISHI_FCLOSE_ERROR;
221 if (VERBOSE (handle))
222 printf (_("Reading EncAPRepPart from %s...done\n"), filename);
224 return SHISHI_OK;
228 * shishi_encapreppart_get_key:
229 * @handle: shishi handle as allocated by shishi_init().
230 * @encapreppart: input EncAPRepPart variable.
231 * @keytype: output variable that holds key type.
232 * @keyvalue: output array with key.
233 * @keyvalue_len: on input, maximum size of output array with key,
234 * on output, holds the actual size of output array with key.
236 * Extract the subkey from the encrypted AP-REP part.
238 * Return value: Returns SHISHI_OK iff succesful.
241 shishi_encapreppart_get_key (Shishi * handle,
242 Shishi_asn1 encapreppart,
243 int32_t * keytype,
244 char *keyvalue, size_t * keyvalue_len)
246 int res;
248 *keytype = 0;
249 res = shishi_asn1_read_int32 (handle, encapreppart,
250 "subkey.keytype", keytype);
251 if (res != SHISHI_OK)
252 return res;
254 res = shishi_asn1_read (handle, encapreppart,
255 "subkey.keyvalue", keyvalue, keyvalue_len);
256 if (res != SHISHI_OK)
257 return res;
259 return SHISHI_OK;
263 shishi_encapreppart_ctime_get (Shishi * handle,
264 Shishi_asn1 encapreppart, char *ctime)
266 int len;
267 int res;
269 len = GENERALIZEDTIME_TIME_LEN + 1;
270 res = shishi_asn1_field (handle, encapreppart, ctime, &len, "ctime");
271 if (res == SHISHI_OK && len == GENERALIZEDTIME_TIME_LEN)
272 ctime[len] = '\0';
274 return res;
278 * shishi_encapreppart_ctime_set:
279 * @handle: shishi handle as allocated by shishi_init().
280 * @encapreppart: EncAPRepPart as allocated by shishi_encapreppart().
281 * @ctime: string with generalized time value to store in EncAPRepPart.
283 * Store client time in EncAPRepPart.
285 * Return value: Returns SHISHI_OK iff successful.
288 shishi_encapreppart_ctime_set (Shishi * handle,
289 Shishi_asn1 encapreppart, char *ctime)
291 int res;
293 res = shishi_asn1_write (handle, encapreppart, "ctime",
294 ctime, GENERALIZEDTIME_TIME_LEN);
295 if (res != SHISHI_OK)
296 return res;
298 return SHISHI_OK;
302 * shishi_encapreppart_cusec_get:
303 * @handle: shishi handle as allocated by shishi_init().
304 * @encapreppart: EncAPRepPart as allocated by shishi_encapreppart().
305 * @cusec: output integer with client microseconds field.
307 * Extract client microseconds field from EncAPRepPart.
309 * Return value: Returns SHISHI_OK iff successful.
312 shishi_encapreppart_cusec_get (Shishi * handle,
313 Shishi_asn1 encapreppart, int *cusec)
315 int res;
317 res = shishi_asn1_read_integer (handle, encapreppart, "cusec", cusec);
318 if (res != SHISHI_OK)
319 return res;
321 return res;
325 * shishi_encapreppart_cusec_set:
326 * @handle: shishi handle as allocated by shishi_init().
327 * @encapreppart: EncAPRepPart as allocated by shishi_encapreppart().
328 * @cusec: client microseconds to set in authenticator, 0-999999.
330 * Set the cusec field in the Authenticator.
332 * Return value: Returns SHISHI_OK iff successful.
335 shishi_encapreppart_cusec_set (Shishi * handle,
336 Shishi_asn1 encapreppart, int cusec)
338 int res;
340 res = shishi_asn1_write_integer (handle, encapreppart, "cusec", cusec);
341 if (res != SHISHI_OK)
342 return res;
344 return SHISHI_OK;
348 * shishi_encapreppart_seqnumber_get:
349 * @handle: shishi handle as allocated by shishi_init().
350 * @encapreppart: EncAPRepPart as allocated by shishi_encapreppart().
351 * @seqnumber: output integer with sequence number field.
353 * Extract sequence number field from EncAPRepPart.
355 * Return value: Returns SHISHI_OK iff successful.
358 shishi_encapreppart_seqnumber_get (Shishi * handle,
359 Shishi_asn1 encapreppart,
360 uint32_t *seqnumber)
362 int res;
364 res = shishi_asn1_read_uint32 (handle, encapreppart,
365 "seq-number", seqnumber);
366 if (res != SHISHI_OK)
367 return res;
369 return res;
373 shishi_encapreppart_time_copy (Shishi * handle,
374 Shishi_asn1 encapreppart,
375 Shishi_asn1 authenticator)
377 char buf[BUFSIZ];
378 int buflen;
379 int res;
381 buflen = BUFSIZ;
382 res = shishi_asn1_read (handle, authenticator, "cusec", buf, &buflen);
383 if (res != SHISHI_OK)
384 return res;
386 res = shishi_asn1_write (handle, encapreppart, "cusec", buf, buflen);
387 if (res != SHISHI_OK)
388 return res;
390 buflen = BUFSIZ;
391 res = shishi_asn1_read (handle, authenticator, "ctime", buf, &buflen);
392 if (res != SHISHI_OK)
393 return res;
395 res = shishi_asn1_write (handle, encapreppart, "ctime", buf, buflen);
396 if (res != SHISHI_OK)
397 return res;
399 return SHISHI_OK;