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
25 shishi_encapreppart (Shishi
* handle
)
28 Shishi_asn1 node
= NULL
;
32 node
= shishi_asn1_encapreppart (handle
);
36 res
= shishi_asn1_write (handle
, node
, "ctime",
37 shishi_generalize_time (handle
, time (NULL
)), 0);
41 gettimeofday (&tv
, &tz
);
42 res
= shishi_asn1_write_integer (handle
, node
, "cusec",
43 tv
.tv_usec
% 1000000);
47 res
= shishi_asn1_write (handle
, node
, "subkey", NULL
, 0);
51 res
= shishi_asn1_write (handle
, node
, "seq-number", NULL
, 0);
58 shishi_asn1_done (handle
, node
);
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.
73 shishi_encapreppart_print (Shishi
* handle
, FILE * fh
,
74 Shishi_asn1 encapreppart
)
76 return _shishi_print_armored_data (handle
, fh
, encapreppart
,
77 "EncAPRepPart", NULL
);
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.
91 shishi_encapreppart_save (Shishi
* handle
, FILE * fh
,
92 Shishi_asn1 encapreppart
)
94 return _shishi_save_data (handle
, fh
, encapreppart
, "EncAPRepPart");
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
)
117 if (VERBOSE (handle
))
118 printf (_("Writing EncAPRepPart to %s...\n"), filename
);
120 fh
= fopen (filename
, "w");
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
);
131 res
= shishi_encapreppart_save (handle
, fh
, encapreppart
);
132 if (res
!= SHISHI_OK
)
137 return SHISHI_FCLOSE_ERROR
;
139 if (VERBOSE (handle
))
140 printf (_("Writing EncAPRepPart to %s...done\n"), filename
);
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
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
)
199 if (VERBOSE (handle
))
200 printf (_("Reading EncAPRepPart from %s...\n"), filename
);
202 fh
= fopen (filename
, "r");
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
);
213 res
= shishi_encapreppart_read (handle
, fh
, encapreppart
);
214 if (res
!= SHISHI_OK
)
219 return SHISHI_FCLOSE_ERROR
;
221 if (VERBOSE (handle
))
222 printf (_("Reading EncAPRepPart from %s...done\n"), filename
);
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
,
244 char *keyvalue
, size_t * keyvalue_len
)
249 res
= shishi_asn1_read_int32 (handle
, encapreppart
,
250 "subkey.keytype", keytype
);
251 if (res
!= SHISHI_OK
)
254 res
= shishi_asn1_read (handle
, encapreppart
,
255 "subkey.keyvalue", keyvalue
, keyvalue_len
);
256 if (res
!= SHISHI_OK
)
263 shishi_encapreppart_ctime_get (Shishi
* handle
,
264 Shishi_asn1 encapreppart
, char *ctime
)
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
)
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
)
293 res
= shishi_asn1_write (handle
, encapreppart
, "ctime",
294 ctime
, GENERALIZEDTIME_TIME_LEN
);
295 if (res
!= 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
)
317 res
= shishi_asn1_read_integer (handle
, encapreppart
, "cusec", cusec
);
318 if (res
!= SHISHI_OK
)
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
)
340 res
= shishi_asn1_write_integer (handle
, encapreppart
, "cusec", cusec
);
341 if (res
!= 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
,
364 res
= shishi_asn1_read_uint32 (handle
, encapreppart
,
365 "seq-number", seqnumber
);
366 if (res
!= SHISHI_OK
)
373 shishi_encapreppart_time_copy (Shishi
* handle
,
374 Shishi_asn1 encapreppart
,
375 Shishi_asn1 authenticator
)
382 res
= shishi_asn1_read (handle
, authenticator
, "cusec", buf
, &buflen
);
383 if (res
!= SHISHI_OK
)
386 res
= shishi_asn1_write (handle
, encapreppart
, "cusec", buf
, buflen
);
387 if (res
!= SHISHI_OK
)
391 res
= shishi_asn1_read (handle
, authenticator
, "ctime", buf
, &buflen
);
392 if (res
!= SHISHI_OK
)
395 res
= shishi_asn1_write (handle
, encapreppart
, "ctime", buf
, buflen
);
396 if (res
!= SHISHI_OK
)