summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile.am8
-rw-r--r--libarchive/CMakeLists.txt4
-rw-r--r--libarchive/archive.h20
-rw-r--r--libarchive/archive_read.c18
-rw-r--r--libarchive/archive_read_add_passphrase.374
-rw-r--r--libarchive/archive_read_add_passphrase.c157
-rw-r--r--libarchive/archive_read_private.h24
-rw-r--r--libarchive/archive_write.c11
-rw-r--r--libarchive/archive_write_private.h13
-rw-r--r--libarchive/archive_write_set_passphrase.359
-rw-r--r--libarchive/archive_write_set_passphrase.c59
-rw-r--r--libarchive/test/CMakeLists.txt2
-rw-r--r--libarchive/test/test_archive_read_add_passphrase.c204
-rw-r--r--libarchive/test/test_archive_write_set_passphrase.c62
14 files changed, 712 insertions, 3 deletions
diff --git a/Makefile.am b/Makefile.am
index ae826c8d..14113d83 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -24,7 +24,7 @@ DISTCHECK_CONFIGURE_FLAGS = --enable-bsdtar --enable-bsdcpio
COMMON_CFLAGS=-Wall -Wformat -Wformat-security
# The next line is commented out by default in shipping libarchive releases.
# It is uncommented by default in trunk.
-DEV_CFLAGS=-Werror -Wextra -Wunused -Wshadow -Wmissing-prototypes -Wcast-qual
+DEV_CFLAGS=-Werror -Wextra -Wunused -Wshadow -Wmissing-prototypes -Wcast-qual -g
AM_CFLAGS=$(COMMON_CFLAGS) $(DEV_CFLAGS)
PLATFORMCPPFLAGS = @PLATFORMCPPFLAGS@
AM_CPPFLAGS=$(PLATFORMCPPFLAGS)
@@ -133,6 +133,7 @@ libarchive_la_SOURCES= \
libarchive/archive_rb.c \
libarchive/archive_rb.h \
libarchive/archive_read.c \
+ libarchive/archive_read_add_passphrase.c \
libarchive/archive_read_append_filter.c \
libarchive/archive_read_data_into_fd.c \
libarchive/archive_read_disk_entry_from_file.c \
@@ -225,6 +226,7 @@ libarchive_la_SOURCES= \
libarchive/archive_write_set_format_xar.c \
libarchive/archive_write_set_format_zip.c \
libarchive/archive_write_set_options.c \
+ libarchive/archive_write_set_passphrase.c \
libarchive/archive_xxhash.h \
libarchive/config_freebsd.h \
libarchive/filter_fork_posix.c \
@@ -256,6 +258,7 @@ libarchive_man_MANS= \
libarchive/archive_entry_stat.3 \
libarchive/archive_entry_time.3 \
libarchive/archive_read.3 \
+ libarchive/archive_read_add_passphrase.3 \
libarchive/archive_read_data.3 \
libarchive/archive_read_disk.3 \
libarchive/archive_read_extract.3 \
@@ -279,6 +282,7 @@ libarchive_man_MANS= \
libarchive/archive_write_new.3 \
libarchive/archive_write_open.3 \
libarchive/archive_write_set_options.3 \
+ libarchive/archive_write_set_passphrase.3 \
libarchive/cpio.5 \
libarchive/libarchive.3 \
libarchive/libarchive_changes.3 \
@@ -329,6 +333,7 @@ libarchive_test_SOURCES= \
libarchive/test/test_archive_match_path.c \
libarchive/test/test_archive_match_time.c \
libarchive/test/test_archive_pathmatch.c \
+ libarchive/test/test_archive_read_add_passphrase.c \
libarchive/test/test_archive_read_close_twice.c \
libarchive/test/test_archive_read_close_twice_open_fd.c \
libarchive/test/test_archive_read_close_twice_open_filename.c \
@@ -350,6 +355,7 @@ libarchive_test_SOURCES= \
libarchive/test/test_archive_write_set_format_option.c \
libarchive/test/test_archive_write_set_option.c \
libarchive/test/test_archive_write_set_options.c \
+ libarchive/test/test_archive_write_set_passphrase.c \
libarchive/test/test_bad_fd.c \
libarchive/test/test_compat_bzip2.c \
libarchive/test/test_compat_cpio.c \
diff --git a/libarchive/CMakeLists.txt b/libarchive/CMakeLists.txt
index 6a8771e3..f1be5fb0 100644
--- a/libarchive/CMakeLists.txt
+++ b/libarchive/CMakeLists.txt
@@ -51,6 +51,7 @@ SET(libarchive_SOURCES
archive_rb.c
archive_rb.h
archive_read.c
+ archive_read_add_passphrase.c
archive_read_append_filter.c
archive_read_data_into_fd.c
archive_read_disk_entry_from_file.c
@@ -143,6 +144,7 @@ SET(libarchive_SOURCES
archive_write_set_format_xar.c
archive_write_set_format_zip.c
archive_write_set_options.c
+ archive_write_set_passphrase.c
archive_xxhash.h
filter_fork_posix.c
filter_fork.h
@@ -160,12 +162,14 @@ SET(libarchive_MANS
archive_entry_stat.3
archive_entry_time.3
archive_read.3
+ archive_read_add_passphrase.3
archive_read_disk.3
archive_read_set_options.3
archive_util.3
archive_write.3
archive_write_disk.3
archive_write_set_options.3
+ archive_write_set_passphrase.3
cpio.5
libarchive.3
libarchive_internals.3
diff --git a/libarchive/archive.h b/libarchive/archive.h
index 2fb78b8b..aa1f8a14 100644
--- a/libarchive/archive.h
+++ b/libarchive/archive.h
@@ -218,6 +218,13 @@ typedef int archive_switch_callback(struct archive *, void *_client_data1,
void *_client_data2);
/*
+ * Returns a passphrase used for encryption or decryption, NULL on nothing
+ * to do and give it up.
+ */
+typedef const char *archive_passphrase_callback(struct archive *,
+ void *_client_data);
+
+/*
* Codes to identify various stream filters.
*/
#define ARCHIVE_FILTER_NONE 0
@@ -582,6 +589,14 @@ __LA_DECL int archive_read_set_option(struct archive *_a,
__LA_DECL int archive_read_set_options(struct archive *_a,
const char *opts);
+/*
+ * Add a decryption passphrase.
+ */
+__LA_DECL int archive_read_add_passphrase(struct archive *, const char *);
+__LA_DECL int archive_read_set_passphrase_callback(struct archive *,
+ void *client_data, archive_passphrase_callback *);
+
+
/*-
* Convenience function to recreate the current entry (whose header
* has just been read) on disk.
@@ -815,6 +830,11 @@ __LA_DECL int archive_write_set_option(struct archive *_a,
__LA_DECL int archive_write_set_options(struct archive *_a,
const char *opts);
+/*
+ * Set a encryption passphrase.
+ */
+__LA_DECL int archive_write_set_passphrase(struct archive *_a, const char *p);
+
/*-
* ARCHIVE_WRITE_DISK API
*
diff --git a/libarchive/archive_read.c b/libarchive/archive_read.c
index 0882485f..02bf8d3a 100644
--- a/libarchive/archive_read.c
+++ b/libarchive/archive_read.c
@@ -101,16 +101,17 @@ archive_read_new(void)
{
struct archive_read *a;
- a = (struct archive_read *)malloc(sizeof(*a));
+ a = (struct archive_read *)calloc(1, sizeof(*a));
if (a == NULL)
return (NULL);
- memset(a, 0, sizeof(*a));
a->archive.magic = ARCHIVE_READ_MAGIC;
a->archive.state = ARCHIVE_STATE_NEW;
a->entry = archive_entry_new2(&a->archive);
a->archive.vtable = archive_read_vtable();
+ a->passphrases.last = &a->passphrases.first;
+
return (&a->archive);
}
@@ -1043,6 +1044,7 @@ static int
_archive_read_free(struct archive *_a)
{
struct archive_read *a = (struct archive_read *)_a;
+ struct archive_read_passphrase *p;
int i, n;
int slots;
int r = ARCHIVE_OK;
@@ -1080,6 +1082,18 @@ _archive_read_free(struct archive *_a)
}
}
+ /* Release passphrase list. */
+ p = a->passphrases.first;
+ while (p != NULL) {
+ struct archive_read_passphrase *np = p->next;
+
+ /* A passphrase should be cleaned. */
+ memset(p->passphrase, 0, strlen(p->passphrase));
+ free(p->passphrase);
+ free(p);
+ p = np;
+ }
+
archive_string_free(&a->archive.error_string);
if (a->entry)
archive_entry_free(a->entry);
diff --git a/libarchive/archive_read_add_passphrase.3 b/libarchive/archive_read_add_passphrase.3
new file mode 100644
index 00000000..8b242ea7
--- /dev/null
+++ b/libarchive/archive_read_add_passphrase.3
@@ -0,0 +1,74 @@
+.\" Copyright (c) 2014 Michihiro NAKAJIMA
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd September 14, 2014
+.Dt ARCHIVE_READ_ADD_PASSPHRASE 3
+.Os
+.Sh NAME
+.Nm archive_read_add_passphrase ,
+.Nm archive_read_set_passphrase_callback
+.Nd functions for reading encrypted archives
+.Sh LIBRARY
+Streaming Archive Library (libarchive, -larchive)
+.Sh SYNOPSIS
+.In archive.h
+.Ft int
+.Fo archive_read_add_passphrase
+.Fa "struct archive *"
+.Fa "const char *passphrase"
+.Fc
+.Ft int
+.Fo archive_read_set_passphrase_callback
+.Fa "struct archive *"
+.Fa "void *client_data"
+.Fa "archive_passphrase_callback *"
+.Fc
+.Sh DESCRIPTION
+.Bl -tag -width indent
+.It Fn archive_read_add_passphrase
+Register passphrases for reading an encryption archive.
+If
+.Ar passphrase
+is
+.Dv NULL
+or empty, this function will do nothing and
+.Cm ARCHIVE_FAILED
+will be returned.
+Otherwise,
+.Cm ARCHIVE_OK
+will be returned.
+.It Fn archive_read_set_passphrase_callback
+Register callback function that will be invoked to get a passphrase
+for decrption after trying all passphrases registered by the
+.Fn archive_read_add_passphrase
+function failed.
+.El
+.\" .Sh ERRORS
+.Sh SEE ALSO
+.Xr tar 1 ,
+.Xr libarchive 3 ,
+.Xr archive_read 3 ,
+.Xr archive_read_set_options 3
diff --git a/libarchive/archive_read_add_passphrase.c b/libarchive/archive_read_add_passphrase.c
new file mode 100644
index 00000000..2fe005a1
--- /dev/null
+++ b/libarchive/archive_read_add_passphrase.c
@@ -0,0 +1,157 @@
+/*-
+ * Copyright (c) 2014 Michihiro NAKAJIMA
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "archive_platform.h"
+__FBSDID("$FreeBSD$");
+
+#include "archive_read_private.h"
+
+static void
+add_passphrase_to_tail(struct archive_read *a,
+ struct archive_read_passphrase *p)
+{
+ *a->passphrases.last = p;
+ a->passphrases.last = &p->next;
+ p->next = NULL;
+}
+
+static struct archive_read_passphrase *
+remove_passphrases_from_head(struct archive_read *a)
+{
+ struct archive_read_passphrase *p;
+
+ p = a->passphrases.first;
+ if (p != NULL)
+ a->passphrases.first = p->next;
+ return (p);
+}
+
+int
+archive_read_add_passphrase(struct archive *_a, const char *passphrase)
+{
+ struct archive_read *a = (struct archive_read *)_a;
+ struct archive_read_passphrase *p;
+
+ archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
+ "archive_read_add_passphrase");
+
+ if (passphrase == NULL || passphrase[0] == '\0') {
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
+ "Empty passphrase is unacceptable");
+ return (ARCHIVE_FAILED);
+ }
+
+ p = malloc(sizeof(*p));
+ if (p == NULL) {
+ archive_set_error(&a->archive, ENOMEM,
+ "Can't allocate memory");
+ return (ARCHIVE_FATAL);
+ }
+ p->passphrase = strdup(passphrase);
+ if (p->passphrase == NULL) {
+ free(p);
+ archive_set_error(&a->archive, ENOMEM,
+ "Can't allocate memory");
+ return (ARCHIVE_FATAL);
+ }
+ add_passphrase_to_tail(a, p);
+
+ return (ARCHIVE_OK);
+}
+
+int
+archive_read_set_passphrase_callback(struct archive *_a, void *client_data,
+ archive_passphrase_callback *cb)
+{
+ struct archive_read *a = (struct archive_read *)_a;
+
+ archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
+ "archive_read_set_passphrase_callback");
+
+ a->passphrases.callback = cb;
+ a->passphrases.client_data = client_data;
+ return (ARCHIVE_OK);
+}
+
+/*
+ * Call this in advance when you start to get a passphrase for decryption
+ * for a entry.
+ */
+void
+__archive_read_reset_passphrase(struct archive_read *a)
+{
+
+ a->passphrases.candiate = -1;
+}
+
+/*
+ * Get a passphrase for decryption.
+ */
+const char *
+__archive_read_next_passphrase(struct archive_read *a)
+{
+ struct archive_read_passphrase *p;
+ const char *passphrase;
+
+ if (a->passphrases.candiate < 0) {
+ /* Count out how many passphrases we have. */
+ int cnt = 0;
+
+ for (p = a->passphrases.first; p != NULL; p = p->next)
+ cnt++;
+ a->passphrases.candiate = cnt;
+ p = a->passphrases.first;
+ } else if (a->passphrases.candiate > 1) {
+ /* Rotate a passphrase list. */
+ a->passphrases.candiate--;
+ p = remove_passphrases_from_head(a);
+ add_passphrase_to_tail(a, p);
+ /* Pick a new passphrase candiate up. */
+ p = a->passphrases.first;
+ } else if (a->passphrases.candiate == 1) {
+ /* This case is that all cadiates failed to decryption. */
+ a->passphrases.candiate = 0;
+ if (a->passphrases.first->next != NULL) {
+ /* Rotate a passphrase list. */
+ p = remove_passphrases_from_head(a);
+ add_passphrase_to_tail(a, p);
+ }
+ p = NULL;
+ } else /* There is no passphrase candaite. */
+ p = NULL;
+
+ if (p != NULL)
+ passphrase = p->passphrase;
+ else if (a->passphrases.callback != NULL) {
+ /* Get a passphrase through a call-back function
+ * since we tried all passphrases out or we don't
+ * have it. */
+ passphrase = a->passphrases.callback(&a->archive,
+ a->passphrases.client_data);
+ } else
+ passphrase = NULL;
+
+ return (passphrase);
+}
diff --git a/libarchive/archive_read_private.h b/libarchive/archive_read_private.h
index b65f03ab..774db82b 100644
--- a/libarchive/archive_read_private.h
+++ b/libarchive/archive_read_private.h
@@ -26,8 +26,10 @@
*/
#ifndef __LIBARCHIVE_BUILD
+#ifndef __LIBARCHIVE_TEST
#error This header is only to be used internally to libarchive.
#endif
+#endif
#ifndef ARCHIVE_READ_PRIVATE_H_INCLUDED
#define ARCHIVE_READ_PRIVATE_H_INCLUDED
@@ -141,6 +143,10 @@ struct archive_read_client {
int64_t position;
struct archive_read_data_node *dataset;
};
+struct archive_read_passphrase {
+ char *passphrase;
+ struct archive_read_passphrase *next;
+};
struct archive_read_extract {
struct archive *ad; /* archive_write_disk object */
@@ -225,6 +231,17 @@ struct archive_read {
*/
struct archive_read_extract *extract;
int (*cleanup_archive_extract)(struct archive_read *);
+
+ /*
+ * Decryption passphrase.
+ */
+ struct {
+ struct archive_read_passphrase *first;
+ struct archive_read_passphrase **last;
+ int candiate;
+ archive_passphrase_callback *callback;
+ void *client_data;
+ } passphrases;
};
int __archive_read_register_format(struct archive_read *a,
@@ -254,4 +271,11 @@ int __archive_read_program(struct archive_read_filter *, const char *);
void __archive_read_free_filters(struct archive_read *);
int __archive_read_close_filters(struct archive_read *);
struct archive_read_extract *__archive_read_get_extract(struct archive_read *);
+
+
+/*
+ * Get a decryption passphrase.
+ */
+void __archive_read_reset_passphrase(struct archive_read *a);
+const char * __archive_read_next_passphrase(struct archive_read *a);
#endif
diff --git a/libarchive/archive_write.c b/libarchive/archive_write.c
index 90212bcb..9f707f07 100644
--- a/libarchive/archive_write.c
+++ b/libarchive/archive_write.c
@@ -444,6 +444,12 @@ archive_write_client_close(struct archive_write_filter *f)
/* Clear the close handler myself not to be called again. */
f->close = NULL;
a->client_data = NULL;
+ /* Clear passphrase. */
+ if (a->passphrase != NULL) {
+ memset(a->passphrase, 0, strlen(a->passphrase));
+ free(a->passphrase);
+ a->passphrase = NULL;
+ }
return (ret);
}
@@ -592,6 +598,11 @@ _archive_write_free(struct archive *_a)
/* Release various dynamic buffers. */
free((void *)(uintptr_t)(const void *)a->nulls);
archive_string_free(&a->archive.error_string);
+ if (a->passphrase != NULL) {
+ /* A passphrase should be cleaned. */
+ memset(a->passphrase, 0, strlen(a->passphrase));
+ free(a->passphrase);
+ }
a->archive.magic = 0;
__archive_clean(&a->archive);
free(a);
diff --git a/libarchive/archive_write_private.h b/libarchive/archive_write_private.h
index e600d547..a2b5c0f8 100644
--- a/libarchive/archive_write_private.h
+++ b/libarchive/archive_write_private.h
@@ -26,8 +26,10 @@
*/
#ifndef __LIBARCHIVE_BUILD
+#ifndef __LIBARCHIVE_TEST
#error This header is only to be used internally to libarchive.
#endif
+#endif
#ifndef ARCHIVE_WRITE_PRIVATE_H_INCLUDED
#define ARCHIVE_WRITE_PRIVATE_H_INCLUDED
@@ -116,6 +118,12 @@ struct archive_write {
const void *buff, size_t);
int (*format_close)(struct archive_write *);
int (*format_free)(struct archive_write *);
+
+
+ /*
+ * Encryption passphrase.
+ */
+ char *passphrase;
};
/*
@@ -142,4 +150,9 @@ int __archive_write_program_close(struct archive_write_filter *,
struct archive_write_program_data *);
int __archive_write_program_write(struct archive_write_filter *,
struct archive_write_program_data *, const void *, size_t);
+
+/*
+ * Get a encryption passphrase.
+ */
+const char * __archive_write_get_passphrase(struct archive_write *a);
#endif
diff --git a/libarchive/archive_write_set_passphrase.3 b/libarchive/archive_write_set_passphrase.3
new file mode 100644
index 00000000..b2e47d4a
--- /dev/null
+++ b/libarchive/archive_write_set_passphrase.3
@@ -0,0 +1,59 @@
+.\" Copyright (c) 2014 Michihiro NAKAJIMA
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd September 13, 2014
+.Dt ARCHIVE_WRITE_SET_PASSPHRASE 3
+.Os
+.Sh NAME
+.Nm archive_write_set_passphrase
+.Nd functions for writing encrypted archives
+.Sh LIBRARY
+Streaming Archive Library (libarchive, -larchive)
+.Sh SYNOPSIS
+.In archive.h
+.Ft int
+.Fo archive_write_set_passphrase
+.Fa "struct archive *"
+.Fa "const char *passphrase"
+.Fc
+.Sh DESCRIPTION
+Set a passphrase for writing an encryption archive.
+If
+.Ar passphrase
+is
+.Dv NULL
+or empty, this function will do nothing and
+.Cm ARCHIVE_FAILED
+will be returned.
+Otherwise,
+.Cm ARCHIVE_OK
+will be returned.
+.\" .Sh ERRORS
+.Sh SEE ALSO
+.Xr tar 1 ,
+.Xr libarchive 3 ,
+.Xr archive_write 3 ,
+.Xr archive_write_set_options 3
diff --git a/libarchive/archive_write_set_passphrase.c b/libarchive/archive_write_set_passphrase.c
new file mode 100644
index 00000000..56989228
--- /dev/null
+++ b/libarchive/archive_write_set_passphrase.c
@@ -0,0 +1,59 @@
+/*-
+ * Copyright (c) 2014 Michihiro NAKAJIMA
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "archive_platform.h"
+__FBSDID("$FreeBSD$");
+
+#include "archive_write_private.h"
+
+int
+archive_write_set_passphrase(struct archive *_a, const char *p)
+{
+ struct archive_write *a = (struct archive_write *)_a;
+
+ archive_check_magic(_a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW,
+ "archive_write_set_passphrase");
+
+ if (p == NULL || p[0] == '\0') {
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
+ "Empty passphrase is unacceptable");
+ return (ARCHIVE_FAILED);
+ }
+ free(a->passphrase);
+ a->passphrase = strdup(p);
+ if (a->passphrase == NULL) {
+ archive_set_error(&a->archive, ENOMEM,
+ "Can't allocate data for passphrase");
+ return (ARCHIVE_FATAL);
+ }
+ return (ARCHIVE_OK);
+}
+
+const char *
+__archive_write_get_passphrase(struct archive_write *a)
+{
+
+ return (a->passphrase);
+}
diff --git a/libarchive/test/CMakeLists.txt b/libarchive/test/CMakeLists.txt
index 5246ecd3..57a561f2 100644
--- a/libarchive/test/CMakeLists.txt
+++ b/libarchive/test/CMakeLists.txt
@@ -23,6 +23,7 @@ IF(ENABLE_TEST)
test_archive_match_path.c
test_archive_match_time.c
test_archive_pathmatch.c
+ test_archive_read_add_passphrase.c
test_archive_read_close_twice.c
test_archive_read_close_twice_open_fd.c
test_archive_read_close_twice_open_filename.c
@@ -44,6 +45,7 @@ IF(ENABLE_TEST)
test_archive_write_set_format_option.c
test_archive_write_set_option.c
test_archive_write_set_options.c
+ test_archive_write_set_passphrase.c
test_bad_fd.c
test_compat_bzip2.c
test_compat_cpio.c
diff --git a/libarchive/test/test_archive_read_add_passphrase.c b/libarchive/test/test_archive_read_add_passphrase.c
new file mode 100644
index 00000000..6c2a4c01
--- /dev/null
+++ b/libarchive/test/test_archive_read_add_passphrase.c
@@ -0,0 +1,204 @@
+/*-
+ * Copyright (c) 2011 Tim Kientzle
+ * Copyright (c) 2014 Michihiro NAKAJIMA
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "test.h"
+__FBSDID("$FreeBSD$");
+
+struct archive_read;
+extern void __archive_read_reset_passphrase(struct archive_read *);
+extern const char * __archive_read_next_passphrase(struct archive_read *);
+
+static void
+test(int pristine)
+{
+ struct archive* a = archive_read_new();
+
+ if (!pristine) {
+ archive_read_support_filter_all(a);
+ archive_read_support_format_all(a);
+ }
+
+ assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass1"));
+ /* An empty passphrase cannot be accepted. */
+ assertEqualInt(ARCHIVE_FAILED, archive_read_add_passphrase(a, ""));
+ /* NULL passphrases cannot be accepted. */
+ assertEqualInt(ARCHIVE_FAILED, archive_read_add_passphrase(a, NULL));
+
+ archive_read_free(a);
+}
+
+DEFINE_TEST(test_archive_read_add_passphrase)
+{
+ test(1);
+ test(0);
+}
+
+DEFINE_TEST(test_archive_read_add_passphrase_incorrect_sequance)
+{
+ struct archive* a = archive_read_new();
+ struct archive_read *ar = (struct archive_read *)a;
+
+ assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass1"));
+
+ /* No call of __archive_read_reset_passphrase() leads to
+ * get NULL even if a user has passed a passphrases. */
+ assertEqualString(NULL, __archive_read_next_passphrase(ar));
+
+ archive_read_free(a);
+}
+
+DEFINE_TEST(test_archive_read_add_passphrase_single)
+{
+ struct archive* a = archive_read_new();
+ struct archive_read *ar = (struct archive_read *)a;
+
+ assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass1"));
+
+ __archive_read_reset_passphrase(ar);
+ /* Fist call, we should get "pass1" as a passphrase. */
+ assertEqualString("pass1", __archive_read_next_passphrase(ar));
+ /* Second call, we should get NULL which means all the pssphrases
+ * are passed already. */
+ assertEqualString(NULL, __archive_read_next_passphrase(ar));
+
+ archive_read_free(a);
+}
+
+DEFINE_TEST(test_archive_read_add_passphrase_multiple)
+{
+ struct archive* a = archive_read_new();
+ struct archive_read *ar = (struct archive_read *)a;
+
+ assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass1"));
+ assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass2"));
+
+ __archive_read_reset_passphrase(ar);
+ /* Fist call, we should get "pass1" as a passphrase. */
+ assertEqualString("pass1", __archive_read_next_passphrase(ar));
+ /* Second call, we should get "pass2" as a passphrase. */
+ assertEqualString("pass2", __archive_read_next_passphrase(ar));
+ /* Third call, we should get NULL which means all the pssphrases
+ * are passed already. */
+ assertEqualString(NULL, __archive_read_next_passphrase(ar));
+
+ archive_read_free(a);
+}
+
+static const char *
+callback1(struct archive *a, void *_client_data)
+{
+ (void)a; /* UNUSED */
+ (void)_client_data; /* UNUSED */
+ return ("passCallBack");
+}
+
+DEFINE_TEST(test_archive_read_add_passphrase_set_callback1)
+{
+ struct archive* a = archive_read_new();
+ struct archive_read *ar = (struct archive_read *)a;
+
+ assertEqualInt(ARCHIVE_OK,
+ archive_read_set_passphrase_callback(a, NULL, callback1));
+
+ __archive_read_reset_passphrase(ar);
+ /* Fist call, we should get "passCallBack" as a passphrase. */
+ assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
+ /* Second call, we still get "passCallBack" as a passphrase. */
+ assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
+
+ archive_read_free(a);
+
+ /* Without __archive_read_reset_passphrase call, the callback
+ * should work fine. */
+ a = archive_read_new();
+ assertEqualInt(ARCHIVE_OK,
+ archive_read_set_passphrase_callback(a, NULL, callback1));
+ /* Fist call, we should get "passCallBack" as a passphrase. */
+ assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
+ /* Second call, we still get "passCallBack" as a passphrase. */
+ assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
+
+ archive_read_free(a);
+}
+
+static const char *
+callback2(struct archive *a, void *_client_data)
+{
+ int *cd = (int *)_client_data;
+
+ (void)a; /* UNUSED */
+
+ if (*cd == 0) {
+ *cd = 1;
+ return ("passCallBack");
+ }
+ return (NULL);
+}
+
+DEFINE_TEST(test_archive_read_add_passphrase_set_callback2)
+{
+ struct archive* a = archive_read_new();
+ struct archive_read *ar = (struct archive_read *)a;
+ int client_data = 0;
+
+ assertEqualInt(ARCHIVE_OK,
+ archive_read_set_passphrase_callback(a, &client_data, callback2));
+
+ __archive_read_reset_passphrase(ar);
+ /* Fist call, we should get "passCallBack" as a passphrase. */
+ assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
+ /* Second call, we should get NULL which means all the pssphrases
+ * are passed already. */
+ assertEqualString(NULL, __archive_read_next_passphrase(ar));
+
+ archive_read_free(a);
+}
+
+DEFINE_TEST(test_archive_read_add_passphrase_multiple_with_callback)
+{
+ struct archive* a = archive_read_new();
+ struct archive_read *ar = (struct archive_read *)a;
+ int client_data = 0;
+
+ assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass1"));
+ assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass2"));
+ assertEqualInt(ARCHIVE_OK,
+ archive_read_set_passphrase_callback(a, &client_data, callback2));
+
+ __archive_read_reset_passphrase(ar);
+ /* Fist call, we should get "pass1" as a passphrase. */
+ assertEqualString("pass1", __archive_read_next_passphrase(ar));
+ /* Second call, we should get "pass2" as a passphrase. */
+ assertEqualString("pass2", __archive_read_next_passphrase(ar));
+ /* Third call, we should get "passCallBack" as a passphrase. */
+ assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
+ /* Fourth call, we should get NULL which means all the pssphrases
+ * are passed already. */
+ assertEqualString(NULL, __archive_read_next_passphrase(ar));
+
+ archive_read_free(a);
+}
+
diff --git a/libarchive/test/test_archive_write_set_passphrase.c b/libarchive/test/test_archive_write_set_passphrase.c
new file mode 100644
index 00000000..20171e2d
--- /dev/null
+++ b/libarchive/test/test_archive_write_set_passphrase.c
@@ -0,0 +1,62 @@
+/*-
+ * Copyright (c) 2011 Tim Kientzle
+ * Copyright (c) 2014 Michihiro NAKAJIMA
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "test.h"
+__FBSDID("$FreeBSD$");
+
+struct archive_write;
+extern const char * __archive_write_get_passphrase(struct archive_write *);
+
+static void
+test(int pristine)
+{
+ struct archive* a = archive_write_new();
+ struct archive_write* aw = (struct archive_write *)a;
+
+ if (!pristine) {
+ archive_write_add_filter_gzip(a);
+ archive_write_set_format_iso9660(a);
+ }
+
+ assertEqualInt(ARCHIVE_OK, archive_write_set_passphrase(a, "pass1"));
+ /* An empty passphrase cannot be accepted. */
+ assertEqualInt(ARCHIVE_FAILED, archive_write_set_passphrase(a, ""));
+ /* NULL passphrases cannot be accepted. */
+ assertEqualInt(ARCHIVE_FAILED, archive_write_set_passphrase(a, NULL));
+ /* Check a passphrase. */
+ assertEqualString("pass1", __archive_write_get_passphrase(aw));
+ /* Change the passphrase. */
+ assertEqualInt(ARCHIVE_OK, archive_write_set_passphrase(a, "pass2"));
+ assertEqualString("pass2", __archive_write_get_passphrase(aw));
+
+ archive_write_free(a);
+}
+
+DEFINE_TEST(test_archive_write_set_passphrase)
+{
+ test(1);
+ test(0);
+}