Disable dependencies (to speed things up).
[shishi.git] / lib / diskio.c
blob1875e083c220407f505c85de81363ebee8ffc6f7
1 /* diskio.c read and write data structures from disk
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 <libtasn1.h>
23 #define _SHISHI_HAS_LIBTASN1_H 1
24 #include "internal.h"
26 #define HEADERBEG "-----BEGIN SHISHI %s-----"
27 #define HEADEREND "-----END SHISHI %s-----"
30 * Note to self: if you change any *print* function, remember to change
31 * the corresponding *parse* function too.
35 int
36 _shishi_print_armored_data (Shishi * handle,
37 FILE * fh,
38 Shishi_asn1 asn1, char *asn1type, char *headers)
40 char der[BUFSIZ];
41 int derlen = BUFSIZ;
42 char b64der[BUFSIZ];
43 int res;
44 size_t i;
46 if (asn1 == NULL)
47 return !SHISHI_OK;
49 asn1_print_structure (fh, asn1, "", ASN1_PRINT_NAME_TYPE_VALUE);
51 res = shishi_a2d_field (handle, asn1, "", der, &derlen);
52 if (res != ASN1_SUCCESS)
54 shishi_error_printf (handle, "Could not DER encode %s: %s\n",
55 asn1type, shishi_strerror (res));
56 return !SHISHI_OK;
59 shishi_to_base64 (b64der, der, derlen, sizeof (b64der));
61 fprintf (fh, HEADERBEG "\n", asn1type);
63 if (headers)
65 fprintf (fh, headers);
66 fprintf (fh, "\n");
69 for (i = 0; i < strlen (b64der); i++)
71 fprintf (fh, "%c", b64der[i]);
72 if ((i + 1) % 64 == 0)
73 fprintf (fh, "\n");
75 if ((i + 1) % 64 != 0)
76 fprintf (fh, "\n");
78 fprintf (fh, HEADEREND "\n", asn1type);
80 return SHISHI_OK;
83 int
84 _shishi_save_data (Shishi * handle, FILE * fh, Shishi_asn1 asn1,
85 char *asn1type)
87 char der[BUFSIZ];
88 int derlen;
89 int res;
90 int i;
92 derlen = sizeof (der);
93 res = shishi_a2d_field (handle, asn1, asn1type, der, &derlen);
94 if (res != SHISHI_OK)
96 shishi_error_printf (handle, "Could not DER encode %s: %s\n",
97 asn1type, shishi_strerror (res));
98 return !SHISHI_OK;
101 i = fwrite (der, sizeof (der[0]), derlen, fh);
102 if (i != derlen)
104 shishi_error_printf (handle, "Short write to file (wrote %d of %d)\n",
105 i, derlen);
106 return !SHISHI_OK;
109 return SHISHI_OK;
113 shishi_enckdcreppart_print (Shishi * handle,
114 FILE * fh, Shishi_asn1 enckdcreppart)
116 return _shishi_print_armored_data (handle, fh, enckdcreppart,
117 "EncKDCRepPart", NULL);
121 shishi_enckdcreppart_save (Shishi * handle,
122 FILE * fh, Shishi_asn1 enckdcreppart)
124 return _shishi_save_data (handle, fh, enckdcreppart, "EncKDCRepPart");
128 shishi_ticket_save (Shishi * handle, FILE * fh, Shishi_asn1 ticket)
130 return _shishi_save_data (handle, fh, ticket, "Ticket");
134 shishi_ticket_print (Shishi * handle, FILE * fh, Shishi_asn1 ticket)
136 return _shishi_print_armored_data (handle, fh, ticket, "Ticket", NULL);
140 shishi_encticketpart_print (Shishi * handle, FILE * fh,
141 Shishi_asn1 encticketpart)
143 return _shishi_print_armored_data (handle, fh, encticketpart,
144 "EncTicketPart", NULL);
147 static int
148 _shishi_read_armored_data (Shishi * handle,
149 FILE * fh, char *buffer, size_t len, char *tag)
151 int lno = 0;
152 size_t maxsize = len;
153 char line[BUFSIZ];
154 char armorbegin[BUFSIZ];
155 char armorend[BUFSIZ];
156 int in_data = 0;
158 sprintf (armorbegin, HEADERBEG, tag);
159 sprintf (armorend, HEADEREND, tag);
161 len = 0;
162 while (fgets (line, sizeof (line), fh))
164 lno++;
165 line[sizeof (line) - 1] = '\0';
166 if (!*line || line[strlen (line) - 1] != '\n')
168 fprintf (stderr, "input line %u too long or missing LF\n", lno);
169 continue;
171 line[strlen (line) - 1] = '\0';
172 if (VERBOSENOICE (handle))
173 printf ("line %d read %d bytes: %s\n", lno, strlen (line), line);
175 /* XXX check if all chars in line are b64 data, otherwise bail out */
177 if (in_data)
179 if (strncmp (line, armorend, strlen (armorend)) == 0)
180 break;
182 else
184 in_data = strncmp (line, armorbegin, strlen (armorbegin)) == 0;
185 continue;
188 if (len + strlen (line) >= maxsize)
190 shishi_error_printf (handle, "too large input size on line %d\n",
191 lno);
192 return !SHISHI_OK;
195 memcpy (buffer + len, line, strlen (line));
196 len += strlen (line);
199 if (len <= 0)
200 return !SHISHI_OK;
202 buffer[len] = '\0';
204 return SHISHI_OK;
207 static int
208 _shishi_ticket_input (Shishi * handle,
209 FILE * fh, Shishi_asn1 * ticket, int type)
211 char der[BUFSIZ];
212 size_t derlen;
213 char b64der[BUFSIZ];
214 size_t b64len = 0;
215 int res;
217 if (type == 0)
219 b64len = sizeof (b64der);
220 res = _shishi_read_armored_data (handle, fh, b64der, b64len, "Ticket");
221 if (res != SHISHI_OK)
223 shishi_error_printf (handle, "armor data read fail\n");
224 return res;
227 derlen = shishi_from_base64 (&der[0], b64der);
229 else
231 derlen =
232 fread (der, sizeof (der[0]), sizeof (der) / sizeof (der[0]), fh);
233 if (derlen <= 0 || !feof (fh) || ferror (fh))
235 shishi_error_printf (handle,
236 "Error reading from file (got %d bytes)...",
237 derlen);
238 return !SHISHI_OK;
242 *ticket = shishi_der2asn1_ticket (handle, der, derlen);
243 if (*ticket == NULL)
244 return SHISHI_ASN1_ERROR;
246 return SHISHI_OK;
250 shishi_ticket_parse (Shishi * handle, FILE * fh, Shishi_asn1 * ticket)
252 return _shishi_ticket_input (handle, fh, ticket, 0);
256 shishi_ticket_read (Shishi * handle, FILE * fh, Shishi_asn1 * ticket)
258 return _shishi_ticket_input (handle, fh, ticket, 1);
261 static int
262 _shishi_enckdcreppart_input (Shishi * handle,
263 FILE * fh, Shishi_asn1 * enckdcreppart, int type)
265 char der[BUFSIZ];
266 size_t derlen;
267 char b64der[BUFSIZ];
268 size_t b64len = 0;
269 int res;
271 if (type == 0)
273 b64len = sizeof (b64der);
274 res = _shishi_read_armored_data (handle, fh,
275 b64der, b64len, "EncKDCRepPart");
276 if (res != SHISHI_OK)
278 shishi_error_printf (handle, "armor data read fail\n");
279 return res;
282 derlen = shishi_from_base64 (&der[0], b64der);
284 else
286 derlen =
287 fread (der, sizeof (der[0]), sizeof (der) / sizeof (der[0]), fh);
288 if (derlen <= 0 || !feof (fh) || ferror (fh))
290 shishi_error_printf (handle,
291 "Error reading from file (got %d bytes)...",
292 derlen);
293 return !SHISHI_OK;
297 *enckdcreppart = shishi_der2asn1_encasreppart (handle, der, derlen);
298 if (*enckdcreppart == NULL)
300 shishi_error_printf (handle, "Could not DER decode Encasreppart: %s",
301 shishi_strerror_details (handle));
303 *enckdcreppart = shishi_der2asn1_enctgsreppart (handle, der, derlen);
304 if (*enckdcreppart == NULL)
306 shishi_error_printf (handle,
307 "Could not DER decode Enctgsreppart: %s",
308 shishi_strerror_details (handle));
310 *enckdcreppart =
311 shishi_der2asn1_enckdcreppart (handle, der, derlen);
312 if (*enckdcreppart == NULL)
314 shishi_error_printf (handle,
315 "Could not DER decode Enckdcreppart: %s",
316 shishi_strerror_details (handle));
317 return !SHISHI_OK;
322 return SHISHI_OK;
326 shishi_enckdcreppart_parse (Shishi * handle,
327 FILE * fh, Shishi_asn1 * enckdcreppart)
329 return _shishi_enckdcreppart_input (handle, fh, enckdcreppart, 0);
333 shishi_enckdcreppart_read (Shishi * handle,
334 FILE * fh, Shishi_asn1 * enckdcreppart)
336 return _shishi_enckdcreppart_input (handle, fh, enckdcreppart, 1);
340 _shishi_kdcreq_input (Shishi * handle, FILE * fh, Shishi_asn1 * asreq,
341 int type)
343 char der[BUFSIZ];
344 size_t derlen;
345 char b64der[BUFSIZ];
346 size_t b64len = 0;
347 int res;
349 if (type == 0)
351 b64len = sizeof (b64der);
352 res = _shishi_read_armored_data (handle, fh, b64der, b64len, "KDC-REQ");
353 if (res != SHISHI_OK)
355 shishi_error_printf (handle, "armor data read fail\n");
356 return res;
359 derlen = shishi_from_base64 (&der[0], b64der);
361 else
363 derlen =
364 fread (der, sizeof (der[0]), sizeof (der) / sizeof (der[0]), fh);
365 if (derlen <= 0 || !feof (fh) || ferror (fh))
367 shishi_error_printf (handle,
368 "Error reading from file (got %d bytes)...",
369 derlen);
370 return !SHISHI_OK;
374 *asreq = shishi_der2asn1_asreq (handle, der, derlen);
375 if (*asreq == NULL)
377 printf ("bad asreq magic\n");
378 shishi_error_printf (handle, "Could not DER decode AS-REQ\n");
380 *asreq = shishi_der2asn1_tgsreq (handle, der, derlen);
381 if (*asreq == NULL)
383 printf ("bad tgsreq magic\n");
384 shishi_error_printf (handle, "Could not DER decode TGS-REQ\n");
386 *asreq = shishi_der2asn1_kdcreq (handle, der, derlen);
387 if (*asreq == NULL)
389 printf ("bad kdcreq magic\n");
390 shishi_error_printf (handle, "Could not DER decode KDC-REQ\n");
392 return !SHISHI_OK;
397 return SHISHI_OK;
401 _shishi_kdcrep_input (Shishi * handle, FILE * fh, Shishi_asn1 * asrep,
402 int type)
404 char der[BUFSIZ];
405 size_t derlen;
406 char b64der[BUFSIZ];
407 size_t b64len = 0;
408 int res;
410 if (type == 0)
412 b64len = sizeof (b64der);
413 res = _shishi_read_armored_data (handle, fh, b64der, b64len, "KDC-REP");
414 if (res != SHISHI_OK)
416 shishi_error_printf (handle, "armor data read fail\n");
417 return res;
420 derlen = shishi_from_base64 (&der[0], b64der);
422 else
424 derlen =
425 fread (der, sizeof (der[0]), sizeof (der) / sizeof (der[0]), fh);
426 if (derlen <= 0 || !feof (fh) || ferror (fh))
428 shishi_error_printf (handle,
429 "Error reading from file (got %d bytes)...",
430 derlen);
431 return !SHISHI_OK;
435 *asrep = shishi_der2asn1_asrep (handle, der, derlen);
436 if (*asrep == NULL)
438 *asrep = shishi_der2asn1_tgsrep (handle, der, derlen);
439 if (*asrep == NULL)
441 printf ("Could not DER decode KDC-REP: %s\n",
442 shishi_strerror_details (handle));
443 printf ("Parsing AS/TGS-REP as KDC-REP (bug work around)\n");
445 *asrep = shishi_der2asn1_kdcrep (handle, der, derlen);
446 if (*asrep == NULL)
448 fprintf (stderr, "Could not DER decode KDC-REP: %s\n",
449 shishi_strerror_details (handle));
450 return !SHISHI_OK;
453 fprintf (stderr, "Bug workaround code successful...\n");
457 return SHISHI_OK;
461 _shishi_apreq_input (Shishi * handle, FILE * fh, Shishi_asn1 * apreq,
462 int type)
464 char der[BUFSIZ];
465 size_t derlen;
466 char b64der[BUFSIZ];
467 size_t b64len = 0;
468 int res;
470 if (type == 0)
472 b64len = sizeof (b64der);
473 res = _shishi_read_armored_data (handle, fh, b64der, b64len, "AP-REQ");
474 if (res != SHISHI_OK)
476 shishi_error_printf (handle, "armor data read fail\n");
477 return res;
480 derlen = shishi_from_base64 (&der[0], b64der);
482 else
484 derlen =
485 fread (der, sizeof (der[0]), sizeof (der) / sizeof (der[0]), fh);
486 if (derlen <= 0 || !feof (fh) || ferror (fh))
488 shishi_error_printf (handle,
489 "Error reading from file (got %d bytes)...",
490 derlen);
491 return !SHISHI_OK;
495 *apreq = shishi_der2asn1_apreq (handle, der, derlen);
496 if (*apreq == NULL)
498 printf ("bad magic %s\n", shishi_strerror_details (handle));
499 shishi_error_printf (handle, "Could not DER decode AP-REQ\n");
501 return !SHISHI_OK;
504 return SHISHI_OK;
508 _shishi_aprep_input (Shishi * handle, FILE * fh, Shishi_asn1 * aprep,
509 int type)
511 char der[BUFSIZ];
512 size_t derlen;
513 char b64der[BUFSIZ];
514 size_t b64len = 0;
515 int res;
517 if (type == 0)
519 b64len = sizeof (b64der);
520 res = _shishi_read_armored_data (handle, fh, b64der, b64len, "AP-REP");
521 if (res != SHISHI_OK)
523 shishi_error_printf (handle, "armor data read fail\n");
524 return res;
527 derlen = shishi_from_base64 (&der[0], b64der);
529 else
531 derlen =
532 fread (der, sizeof (der[0]), sizeof (der) / sizeof (der[0]), fh);
533 if (derlen <= 0 || !feof (fh) || ferror (fh))
535 shishi_error_printf (handle,
536 "Error reading from file (got %d bytes)...",
537 derlen);
538 return !SHISHI_OK;
542 *aprep = shishi_der2asn1_aprep (handle, der, derlen);
543 if (*aprep == NULL)
545 printf ("bad magic %s\n", shishi_strerror_details (handle));
546 shishi_error_printf (handle, "Could not DER decode AP-REP\n");
548 return !SHISHI_OK;
551 return SHISHI_OK;
555 _shishi_encapreppart_input (Shishi * handle, FILE * fh,
556 Shishi_asn1 * encapreppart, int type)
558 char der[BUFSIZ];
559 size_t derlen;
560 char b64der[BUFSIZ];
561 size_t b64len = 0;
562 int res;
564 if (type == 0)
566 b64len = sizeof (b64der);
567 res =
568 _shishi_read_armored_data (handle, fh, b64der, b64len,
569 "EncAPRepPart");
570 if (res != SHISHI_OK)
572 shishi_error_printf (handle, "armor data read fail\n");
573 return res;
576 derlen = shishi_from_base64 (&der[0], b64der);
578 else
580 derlen =
581 fread (der, sizeof (der[0]), sizeof (der) / sizeof (der[0]), fh);
582 if (derlen <= 0 || !feof (fh) || ferror (fh))
584 shishi_error_printf (handle,
585 "Error reading from file (got %d bytes)...",
586 derlen);
587 return !SHISHI_OK;
591 *encapreppart = shishi_der2asn1_encapreppart (handle, der, derlen);
592 if (*encapreppart == NULL)
594 printf ("bad magic %s\n", shishi_strerror_details (handle));
595 shishi_error_printf (handle, "Could not DER decode EncAPRepPart\n");
597 return !SHISHI_OK;
600 return SHISHI_OK;
604 _shishi_authenticator_input (Shishi * handle,
605 FILE * fh, Shishi_asn1 * authenticator, int type)
607 char der[BUFSIZ];
608 size_t derlen;
609 char b64der[BUFSIZ];
610 size_t b64len = 0;
611 int res;
613 if (type == 0)
615 b64len = sizeof (b64der);
616 res = _shishi_read_armored_data (handle, fh, b64der, b64len,
617 "Authenticator");
618 if (res != SHISHI_OK)
620 shishi_error_printf (handle, "armor data read fail\n");
621 return res;
624 derlen = shishi_from_base64 (&der[0], b64der);
626 else
628 derlen =
629 fread (der, sizeof (der[0]), sizeof (der) / sizeof (der[0]), fh);
630 if (derlen <= 0 || !feof (fh) || ferror (fh))
632 shishi_error_printf (handle,
633 "Error reading from file (got %d bytes)...",
634 derlen);
635 return !SHISHI_OK;
639 *authenticator = shishi_der2asn1_authenticator (handle, der, derlen);
640 if (*authenticator == NULL)
642 printf ("bad magic %s\n", shishi_strerror_details (handle));
643 shishi_error_printf (handle, "Could not DER decode AP-REQ\n");
645 return !SHISHI_OK;
648 return SHISHI_OK;
652 _shishi_krberror_input (Shishi * handle,
653 FILE * fh, Shishi_asn1 * krberror, int type)
655 char der[BUFSIZ];
656 size_t derlen;
657 char b64der[BUFSIZ];
658 size_t b64len = 0;
659 int res;
661 if (type == 0)
663 b64len = sizeof (b64der);
664 res = _shishi_read_armored_data (handle, fh, b64der, b64len,
665 "KRB-ERROR");
666 if (res != SHISHI_OK)
668 shishi_error_printf (handle, "armor data read fail\n");
669 return res;
672 derlen = shishi_from_base64 (&der[0], b64der);
674 else
676 derlen = fread (der, sizeof (der[0]),
677 sizeof (der) / sizeof (der[0]), fh);
678 if (derlen <= 0 || !feof (fh) || ferror (fh))
680 shishi_error_printf (handle,
681 "Error reading from file (got %d bytes)...",
682 derlen);
683 return !SHISHI_OK;
687 *krberror = shishi_der2asn1_krberror (handle, der, derlen);
688 if (*krberror == NULL)
690 printf ("bad magic %s\n", shishi_strerror_details (handle));
691 shishi_error_printf (handle, "Could not DER decode AP-REQ\n");
693 return !SHISHI_OK;
696 return SHISHI_OK;
700 _shishi_safe_input (Shishi * handle, FILE * fh, Shishi_asn1 * safe, int type)
702 char der[BUFSIZ];
703 size_t derlen;
704 char b64der[BUFSIZ];
705 size_t b64len = 0;
706 int res;
708 if (type == 0)
710 b64len = sizeof (b64der);
711 res = _shishi_read_armored_data (handle, fh, b64der, b64len,
712 "KRB-SAFE");
713 if (res != SHISHI_OK)
715 shishi_error_printf (handle, "armor data read fail\n");
716 return res;
719 derlen = shishi_from_base64 (&der[0], b64der);
721 else
723 derlen = fread (der, sizeof (der[0]),
724 sizeof (der) / sizeof (der[0]), fh);
725 if (derlen <= 0 || !feof (fh) || ferror (fh))
727 shishi_error_printf (handle,
728 "Error reading from file (got %d bytes)...",
729 derlen);
730 return !SHISHI_OK;
734 *safe = shishi_der2asn1_krbsafe (handle, der, derlen);
735 if (*safe == NULL)
737 printf ("bad magic %s\n", shishi_strerror_details (handle));
738 shishi_error_printf (handle, "Could not DER decode KRB-SAFE\n");
740 return !SHISHI_OK;
743 return SHISHI_OK;
747 shishi_key_parse (Shishi * handle, FILE * fh, Shishi_key ** key)
749 int lno = 0;
750 char line[BUFSIZ];
751 char buffer[BUFSIZ];
752 char armorbegin[BUFSIZ];
753 char armorend[BUFSIZ];
754 int in_key = 0, in_body = 0;
755 int res;
756 size_t len, buflen;
757 Shishi_key *lkey = NULL;
759 sprintf (armorbegin, HEADERBEG, "KEY");
760 sprintf (armorend, HEADEREND, "KEY");
762 len = 0;
763 while (fgets (line, sizeof (line), fh))
765 lno++;
766 line[sizeof (line) - 1] = '\0';
767 if (!*line || line[strlen (line) - 1] != '\n')
769 fprintf (stderr, "input line %u too long or missing LF\n", lno);
770 continue;
772 line[strlen (line) - 1] = '\0';
773 if (VERBOSENOICE (handle))
774 printf ("line %d read %d bytes: %s\n", lno, strlen (line), line);
776 if (!in_key)
778 in_key = strncmp (line, armorbegin, strlen (armorbegin)) == 0;
779 if (in_key)
781 res = shishi_key (handle, &lkey);
782 if (res != SHISHI_OK)
783 return res;
786 continue;
789 if (strcmp (line, armorend) == 0)
790 break;
792 if (in_body)
794 buflen = shishi_from_base64 (buffer, line);
795 shishi_key_value_set (lkey, buffer);
797 else
799 if (in_body == 0 && strcmp (line, "") == 0)
800 in_body = 1;
802 if (strncmp (line, "Keytype: ", strlen ("Keytype: ")) == 0)
804 int type;
805 if (sscanf (line, "Keytype: %d (", &type) == 1)
806 shishi_key_type_set (lkey, type);
808 else if (strncmp (line, "Key-Version-Number: ",
809 strlen ("Key-Version-Number: ")) == 0)
811 int type;
812 if (sscanf (line, "Key-Version-Number: %d", &type) == 1)
813 shishi_key_version_set (lkey, type);
815 else if (strncmp (line, "Realm: ", strlen ("Realm: ")) == 0)
817 shishi_key_realm_set (lkey, line + strlen ("Realm: "));
819 else if (strncmp (line, "Principal: ", strlen ("Principal: ")) == 0)
821 shishi_key_principal_set (lkey, line + strlen ("Principal: "));
826 if (!lkey)
827 return SHISHI_OK;
829 *key = lkey;
831 return SHISHI_OK;
835 * shishi_key_print:
836 * @handle: shishi handle as allocated by shishi_init().
837 * @fh: file handle opened for writing.
838 * @key: key to print.
840 * Print an ASCII representation of a key structure to file
841 * descriptor. Example output:
843 * -----BEGIN SHISHI KEY-----
844 * Keytype: 18 (aes256-cts-hmac-sha1-96)
845 * Principal: host/latte.josefsson.org
846 * Realm: JOSEFSSON.ORG
847 * Key-Version-Number: 1
849 * P1QdeW/oSiag/bTyVEBAY2msiGSTmgLXlopuCKoppDs=
850 * -----END SHISHI KEY-----
852 * Return value: Returns SHISHI_OK iff successful.
855 shishi_key_print (Shishi * handle, FILE * fh, Shishi_key * key)
857 char b64key[BUFSIZ];
858 size_t i;
860 shishi_to_base64 (b64key, shishi_key_value (key),
861 shishi_key_length (key), sizeof (b64key));
863 fprintf (fh, HEADERBEG "\n", "KEY");
865 fprintf (fh, "Keytype: %d (%s)\n", shishi_key_type (key),
866 shishi_cipher_name (shishi_key_type (key)));
867 if (shishi_key_principal (key))
868 fprintf (fh, "Principal: %s\n", shishi_key_principal (key));
869 if (shishi_key_realm (key))
870 fprintf (fh, "Realm: %s\n", shishi_key_realm (key));
871 if (shishi_key_version (key))
872 fprintf (fh, "Key-Version-Number: %d\n", shishi_key_version (key));
873 fprintf (fh, "\n");
875 for (i = 0; i < strlen (b64key); i++)
877 fprintf (fh, "%c", b64key[i]);
878 if ((i + 1) % 64 == 0)
879 fprintf (fh, "\n");
881 if ((i + 1) % 64 != 0)
882 fprintf (fh, "\n");
884 if (VERBOSENOICE (handle))
886 for (i = 0; i < shishi_key_length (key); i++)
887 fprintf (stdout, "%02x", shishi_key_value (key)[i] & 0xFF);
888 fprintf (stdout, "\n");
891 fprintf (fh, HEADEREND "\n", "KEY");
893 return SHISHI_OK;
897 * shishi_key_to_file:
898 * @handle: shishi handle as allocated by shishi_init().
899 * @filename: filename to append key to.
900 * @key: key to print.
902 * Print an ASCII representation of a key structure to a file. The
903 * file is appended to if it exists. See shishi_key_print() for
904 * format of output.
906 * Return value: Returns SHISHI_OK iff successful.
909 shishi_key_to_file (Shishi * handle, const char *filename, Shishi_key * key)
911 FILE *fh;
912 int res;
914 if (VERBOSE (handle))
915 printf (_("Writing KEY to %s...\n"), filename);
917 fh = fopen (filename, "a");
918 if (fh == NULL)
919 return SHISHI_FOPEN_ERROR;
921 res = shishi_key_print (handle, fh, key);
922 if (res != SHISHI_OK)
923 return res;
925 res = fclose (fh);
926 if (res != 0)
927 return SHISHI_FCLOSE_ERROR;
929 if (VERBOSE (handle))
930 printf (_("Writing KEY to %s...done\n"), filename);
932 return SHISHI_OK;