16b384f39SPeter Avalos /*-
26b384f39SPeter Avalos * Copyright (c) 2014 Michihiro NAKAJIMA
36b384f39SPeter Avalos * All rights reserved.
46b384f39SPeter Avalos *
56b384f39SPeter Avalos * Redistribution and use in source and binary forms, with or without
66b384f39SPeter Avalos * modification, are permitted provided that the following conditions
76b384f39SPeter Avalos * are met:
86b384f39SPeter Avalos * 1. Redistributions of source code must retain the above copyright
96b384f39SPeter Avalos * notice, this list of conditions and the following disclaimer.
106b384f39SPeter Avalos * 2. Redistributions in binary form must reproduce the above copyright
116b384f39SPeter Avalos * notice, this list of conditions and the following disclaimer in the
126b384f39SPeter Avalos * documentation and/or other materials provided with the distribution.
136b384f39SPeter Avalos *
146b384f39SPeter Avalos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
156b384f39SPeter Avalos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
166b384f39SPeter Avalos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
176b384f39SPeter Avalos * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
186b384f39SPeter Avalos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
196b384f39SPeter Avalos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
206b384f39SPeter Avalos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
216b384f39SPeter Avalos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
226b384f39SPeter Avalos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
236b384f39SPeter Avalos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
246b384f39SPeter Avalos */
256b384f39SPeter Avalos
266b384f39SPeter Avalos #include "archive_platform.h"
276b384f39SPeter Avalos __FBSDID("$FreeBSD$");
286b384f39SPeter Avalos
296b384f39SPeter Avalos #ifdef HAVE_ERRNO_H
306b384f39SPeter Avalos #include <errno.h>
316b384f39SPeter Avalos #endif
326b384f39SPeter Avalos #include "archive_read_private.h"
336b384f39SPeter Avalos
346b384f39SPeter Avalos static void
add_passphrase_to_tail(struct archive_read * a,struct archive_read_passphrase * p)356b384f39SPeter Avalos add_passphrase_to_tail(struct archive_read *a,
366b384f39SPeter Avalos struct archive_read_passphrase *p)
376b384f39SPeter Avalos {
386b384f39SPeter Avalos *a->passphrases.last = p;
396b384f39SPeter Avalos a->passphrases.last = &p->next;
406b384f39SPeter Avalos p->next = NULL;
416b384f39SPeter Avalos }
426b384f39SPeter Avalos
436b384f39SPeter Avalos static struct archive_read_passphrase *
remove_passphrases_from_head(struct archive_read * a)446b384f39SPeter Avalos remove_passphrases_from_head(struct archive_read *a)
456b384f39SPeter Avalos {
466b384f39SPeter Avalos struct archive_read_passphrase *p;
476b384f39SPeter Avalos
486b384f39SPeter Avalos p = a->passphrases.first;
496b384f39SPeter Avalos if (p != NULL)
506b384f39SPeter Avalos a->passphrases.first = p->next;
516b384f39SPeter Avalos return (p);
526b384f39SPeter Avalos }
536b384f39SPeter Avalos
546b384f39SPeter Avalos static void
insert_passphrase_to_head(struct archive_read * a,struct archive_read_passphrase * p)556b384f39SPeter Avalos insert_passphrase_to_head(struct archive_read *a,
566b384f39SPeter Avalos struct archive_read_passphrase *p)
576b384f39SPeter Avalos {
586b384f39SPeter Avalos p->next = a->passphrases.first;
596b384f39SPeter Avalos a->passphrases.first = p;
60*085658deSDaniel Fojt if (&a->passphrases.first == a->passphrases.last) {
61*085658deSDaniel Fojt a->passphrases.last = &p->next;
62*085658deSDaniel Fojt p->next = NULL;
63*085658deSDaniel Fojt }
646b384f39SPeter Avalos }
656b384f39SPeter Avalos
666b384f39SPeter Avalos static struct archive_read_passphrase *
new_read_passphrase(struct archive_read * a,const char * passphrase)676b384f39SPeter Avalos new_read_passphrase(struct archive_read *a, const char *passphrase)
686b384f39SPeter Avalos {
696b384f39SPeter Avalos struct archive_read_passphrase *p;
706b384f39SPeter Avalos
716b384f39SPeter Avalos p = malloc(sizeof(*p));
726b384f39SPeter Avalos if (p == NULL) {
736b384f39SPeter Avalos archive_set_error(&a->archive, ENOMEM,
746b384f39SPeter Avalos "Can't allocate memory");
756b384f39SPeter Avalos return (NULL);
766b384f39SPeter Avalos }
776b384f39SPeter Avalos p->passphrase = strdup(passphrase);
786b384f39SPeter Avalos if (p->passphrase == NULL) {
796b384f39SPeter Avalos free(p);
806b384f39SPeter Avalos archive_set_error(&a->archive, ENOMEM,
816b384f39SPeter Avalos "Can't allocate memory");
826b384f39SPeter Avalos return (NULL);
836b384f39SPeter Avalos }
846b384f39SPeter Avalos return (p);
856b384f39SPeter Avalos }
866b384f39SPeter Avalos
876b384f39SPeter Avalos int
archive_read_add_passphrase(struct archive * _a,const char * passphrase)886b384f39SPeter Avalos archive_read_add_passphrase(struct archive *_a, const char *passphrase)
896b384f39SPeter Avalos {
906b384f39SPeter Avalos struct archive_read *a = (struct archive_read *)_a;
916b384f39SPeter Avalos struct archive_read_passphrase *p;
926b384f39SPeter Avalos
936b384f39SPeter Avalos archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
946b384f39SPeter Avalos "archive_read_add_passphrase");
956b384f39SPeter Avalos
966b384f39SPeter Avalos if (passphrase == NULL || passphrase[0] == '\0') {
976b384f39SPeter Avalos archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
986b384f39SPeter Avalos "Empty passphrase is unacceptable");
996b384f39SPeter Avalos return (ARCHIVE_FAILED);
1006b384f39SPeter Avalos }
1016b384f39SPeter Avalos
1026b384f39SPeter Avalos p = new_read_passphrase(a, passphrase);
1036b384f39SPeter Avalos if (p == NULL)
1046b384f39SPeter Avalos return (ARCHIVE_FATAL);
1056b384f39SPeter Avalos add_passphrase_to_tail(a, p);
1066b384f39SPeter Avalos
1076b384f39SPeter Avalos return (ARCHIVE_OK);
1086b384f39SPeter Avalos }
1096b384f39SPeter Avalos
1106b384f39SPeter Avalos int
archive_read_set_passphrase_callback(struct archive * _a,void * client_data,archive_passphrase_callback * cb)1116b384f39SPeter Avalos archive_read_set_passphrase_callback(struct archive *_a, void *client_data,
1126b384f39SPeter Avalos archive_passphrase_callback *cb)
1136b384f39SPeter Avalos {
1146b384f39SPeter Avalos struct archive_read *a = (struct archive_read *)_a;
1156b384f39SPeter Avalos
1166b384f39SPeter Avalos archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
1176b384f39SPeter Avalos "archive_read_set_passphrase_callback");
1186b384f39SPeter Avalos
1196b384f39SPeter Avalos a->passphrases.callback = cb;
1206b384f39SPeter Avalos a->passphrases.client_data = client_data;
1216b384f39SPeter Avalos return (ARCHIVE_OK);
1226b384f39SPeter Avalos }
1236b384f39SPeter Avalos
1246b384f39SPeter Avalos /*
1256b384f39SPeter Avalos * Call this in advance when you start to get a passphrase for decryption
1266b384f39SPeter Avalos * for a entry.
1276b384f39SPeter Avalos */
1286b384f39SPeter Avalos void
__archive_read_reset_passphrase(struct archive_read * a)1296b384f39SPeter Avalos __archive_read_reset_passphrase(struct archive_read *a)
1306b384f39SPeter Avalos {
1316b384f39SPeter Avalos
132e95abc47Szrj a->passphrases.candidate = -1;
1336b384f39SPeter Avalos }
1346b384f39SPeter Avalos
1356b384f39SPeter Avalos /*
1366b384f39SPeter Avalos * Get a passphrase for decryption.
1376b384f39SPeter Avalos */
1386b384f39SPeter Avalos const char *
__archive_read_next_passphrase(struct archive_read * a)1396b384f39SPeter Avalos __archive_read_next_passphrase(struct archive_read *a)
1406b384f39SPeter Avalos {
1416b384f39SPeter Avalos struct archive_read_passphrase *p;
1426b384f39SPeter Avalos const char *passphrase;
1436b384f39SPeter Avalos
144e95abc47Szrj if (a->passphrases.candidate < 0) {
1456b384f39SPeter Avalos /* Count out how many passphrases we have. */
1466b384f39SPeter Avalos int cnt = 0;
1476b384f39SPeter Avalos
1486b384f39SPeter Avalos for (p = a->passphrases.first; p != NULL; p = p->next)
1496b384f39SPeter Avalos cnt++;
150e95abc47Szrj a->passphrases.candidate = cnt;
1516b384f39SPeter Avalos p = a->passphrases.first;
152e95abc47Szrj } else if (a->passphrases.candidate > 1) {
1536b384f39SPeter Avalos /* Rotate a passphrase list. */
154e95abc47Szrj a->passphrases.candidate--;
1556b384f39SPeter Avalos p = remove_passphrases_from_head(a);
1566b384f39SPeter Avalos add_passphrase_to_tail(a, p);
157e95abc47Szrj /* Pick a new passphrase candidate up. */
1586b384f39SPeter Avalos p = a->passphrases.first;
159e95abc47Szrj } else if (a->passphrases.candidate == 1) {
160e95abc47Szrj /* This case is that all candidates failed to decrypt. */
161e95abc47Szrj a->passphrases.candidate = 0;
1626b384f39SPeter Avalos if (a->passphrases.first->next != NULL) {
1636b384f39SPeter Avalos /* Rotate a passphrase list. */
1646b384f39SPeter Avalos p = remove_passphrases_from_head(a);
1656b384f39SPeter Avalos add_passphrase_to_tail(a, p);
1666b384f39SPeter Avalos }
1676b384f39SPeter Avalos p = NULL;
168e95abc47Szrj } else /* There is no passphrase candidate. */
1696b384f39SPeter Avalos p = NULL;
1706b384f39SPeter Avalos
1716b384f39SPeter Avalos if (p != NULL)
1726b384f39SPeter Avalos passphrase = p->passphrase;
1736b384f39SPeter Avalos else if (a->passphrases.callback != NULL) {
1746b384f39SPeter Avalos /* Get a passphrase through a call-back function
1756b384f39SPeter Avalos * since we tried all passphrases out or we don't
1766b384f39SPeter Avalos * have it. */
1776b384f39SPeter Avalos passphrase = a->passphrases.callback(&a->archive,
1786b384f39SPeter Avalos a->passphrases.client_data);
1796b384f39SPeter Avalos if (passphrase != NULL) {
1806b384f39SPeter Avalos p = new_read_passphrase(a, passphrase);
1816b384f39SPeter Avalos if (p == NULL)
1826b384f39SPeter Avalos return (NULL);
1836b384f39SPeter Avalos insert_passphrase_to_head(a, p);
184e95abc47Szrj a->passphrases.candidate = 1;
1856b384f39SPeter Avalos }
1866b384f39SPeter Avalos } else
1876b384f39SPeter Avalos passphrase = NULL;
1886b384f39SPeter Avalos
1896b384f39SPeter Avalos return (passphrase);
1906b384f39SPeter Avalos }
191