Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

# FIXME run cppcheck

SUBDIRS = lvm
SUBDIRS = lib
SUBDIRS += lvm
SUBDIRS += vhd
SUBDIRS += cpumond
SUBDIRS += control
Expand Down
2 changes: 1 addition & 1 deletion cbt/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ sbin_PROGRAMS = cbt-util
noinst_LTLIBRARIES = libcbtutil.la

libcbtutil_la_SOURCES = cbt-util.c
libcbtutil_la_LIBADD = -luuid
libcbtutil_la_LIBADD = -luuid $(top_builddir)/lib/libblktaputil.la

cbt_util_SOURCES = main.c
cbt_util_LDADD = libcbtutil.la
Expand Down
23 changes: 20 additions & 3 deletions cbt/cbt-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

#include "cbt-util.h"
#include "cbt-util-priv.h"
#include "util.h"

int cbt_util_create(int , char **);
int cbt_util_set(int , char **);
Expand Down Expand Up @@ -148,7 +149,7 @@ cbt_util_get(int argc, char **argv)
{
char *name, uuid_str[37], *buf;
int err, c, ret;
int parent, child, flag, size, bitmap;
int parent, child, flag, size, bitmap, use_base64;
FILE *f = NULL;

err = 0;
Expand All @@ -159,14 +160,15 @@ cbt_util_get(int argc, char **argv)
size = 0;
buf = NULL;
bitmap = 0;
use_base64 = 0;

if (!argc || !argv)
goto usage;

/* Make sure we start from the start of the args */
optind = 1;

while ((c = getopt(argc, argv, "n:pcfsbh")) != -1) {
while ((c = getopt(argc, argv, "n:pcfsbEh")) != -1) {
switch (c) {
case 'n':
name = optarg;
Expand All @@ -186,6 +188,9 @@ cbt_util_get(int argc, char **argv)
case 'b':
bitmap = 1;
break;
case 'E':
use_base64 = 1;
break;
case 'h':
default:
goto usage;
Expand Down Expand Up @@ -252,7 +257,18 @@ cbt_util_get(int argc, char **argv)
bmsize, name);
}

fwrite(buf, bmsize, 1, stdout);
if (use_base64) {
char *encoded_buf;
if (base64_encode_data((uint8_t*)buf, bmsize, &encoded_buf) != 0) {
fprintf(stderr, "Failed to encode bitmap to base64\n");
err = -EIO;
goto error;
}
printf("%s", encoded_buf);
free(encoded_buf);
} else {
fwrite(buf, bmsize, 1, stdout);
}
}

error:
Expand All @@ -273,6 +289,7 @@ cbt_util_get(int argc, char **argv)
printf("[-f]\t\tPrint consistency flag\n");
printf("[-s]\t\tPrint size of disk in bytes\n");
printf("[-b]\t\tPrint bitmap contents\n");
printf("[-E]\t\tEncode bitmap output as base64\n");
printf("[-h]\t\thelp\n");

return -EINVAL;
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ AC_DEFINE(_BLKTAP, 1,
Indicates whether this is an internal or external compilation.)
AC_CONFIG_FILES([
Makefile
lib/Makefile
lvm/Makefile
cpumond/Makefile
cbt/Makefile
Expand Down
3 changes: 2 additions & 1 deletion control/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ AM_CPPFLAGS += -DTAPDISK_EXECDIR='"$(libexecdir)"'
AM_CPPFLAGS += -DTAPDISK_BUILDDIR='"$(top_builddir)/drivers"'

sbin_PROGRAMS = tap-ctl
tap_ctl_LDADD = libblktapctl.la
tap_ctl_SOURCES = tap-ctl.c
tap_ctl_LDADD = libblktapctl.la $(top_builddir)/lib/libblktaputil.la

lib_LTLIBRARIES = libblktapctl.la

Expand Down
31 changes: 25 additions & 6 deletions control/tap-ctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
#include <sys/time.h>

#include "tap-ctl.h"

#define MAX_AES_XTS_PLAIN_KEYSIZE 1024
#include "util.h"

typedef int (*tap_ctl_func_t) (int, char **);

Expand Down Expand Up @@ -708,7 +707,7 @@ tap_cli_open_usage(FILE *stream)
"fail over to the secondary image on ENOSPC] "
"[-t request timeout in seconds] [-D no O_DIRECT] "
"[-C </path/to/logfile> insert log layer to track changed blocks] "
"[-E read encryption key from stdin]\n");
"[-E read base64-encoded encryption key from stdin]\n");
}

static int
Expand Down Expand Up @@ -773,13 +772,33 @@ tap_cli_open(int argc, char **argv)
fprintf(stderr, "Only supply -E once\n");
exit(1);
}
/* Allocate the space for the key, */

char base64_key[512];
ssize_t read_len = read(STDIN_FILENO, base64_key, sizeof(base64_key) - 1);
if (read_len <= 0) {
fprintf(stderr, "Failed to read base64 key from stdin\n");
exit(1);
}
base64_key[read_len] = '\0';

if (read_len > 0 && base64_key[read_len - 1] == '\n') {
base64_key[read_len - 1] = '\0';
}

encryption_key = malloc(MAX_AES_XTS_PLAIN_KEYSIZE / sizeof(uint8_t));
if (!encryption_key) {
fprintf(stderr, "Failed to allocate space for encrpytion key\n");
fprintf(stderr, "Failed to allocate space for encryption key\n");
exit(1);
}

/* Decode base64 to binary key */
size_t decoded_len;
if (base64_decode_key(base64_key, encryption_key, &decoded_len) != 0) {
fprintf(stderr, "Failed to decode base64 encryption key\n");
free(encryption_key);
exit(1);
}
key_size = read(STDIN_FILENO, (void*)encryption_key, MAX_AES_XTS_PLAIN_KEYSIZE / sizeof(uint8_t));
key_size = decoded_len;
if (key_size != 32 && key_size != 64){
fprintf(stderr, "Unsupported keysize, use either 256 bit or 512 bit key\n");
free(encryption_key);
Expand Down
12 changes: 12 additions & 0 deletions include/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include <stddef.h>
#include <string.h>
#include <stdint.h>

#define ARRAY_SIZE(_a) (sizeof(_a)/sizeof((_a)[0]))

Expand All @@ -49,4 +50,15 @@ safe_strncpy(char *dest, const char *src, size_t n)
return pdest;
}

/*
* Constants for cryptographic operations
*/
#define MAX_AES_XTS_PLAIN_KEYSIZE 1024

/*
* Base64 encoding/decoding utilities using OpenSSL
*/
int base64_encode_data(const uint8_t *input, size_t input_len, char **output);
int base64_decode_key(const char *input, uint8_t *output, size_t *output_len);

#endif /* __TAPDISK_UTIL_H__ */
14 changes: 14 additions & 0 deletions lib/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
AM_CFLAGS = -Wall
AM_CFLAGS += -Werror
AM_CFLAGS += $(if $(GCOV),-fprofile-dir=/tmp/coverage/blktap/lib -fprofile-arcs -ftest-coverage)

AM_CPPFLAGS = -I$(top_srcdir)/include

lib_LTLIBRARIES = libblktaputil.la

libblktaputil_la_SOURCES = util.c
libblktaputil_la_LDFLAGS = -version-info 1:0:0
libblktaputil_la_LIBADD = -lssl -lcrypto

clean-local:
-rm -rf *.gc??
122 changes: 122 additions & 0 deletions lib/util.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright (c) Cloud Software Group, Inc.
*
* 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.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT OWNER
* 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.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <string.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/buffer.h>

#include "util.h"

int
base64_encode_data(const uint8_t *input, size_t input_len, char **output)
{
BIO *bio, *b64;
BUF_MEM *bufferPtr;

if (!input || input_len == 0 || !output) {
return -1;
}

*output = NULL;

b64 = BIO_new(BIO_f_base64());
bio = BIO_new(BIO_s_mem());
if (!b64 || !bio) {
if (b64) BIO_free(b64);
if (bio) BIO_free(bio);
return -1;
}

bio = BIO_push(b64, bio);
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);

if (BIO_write(bio, input, input_len) != (int)input_len) {
BIO_free_all(bio);
return -1;
}

if (BIO_flush(bio) != 1) {
BIO_free_all(bio);
return -1;
}

BIO_get_mem_ptr(bio, &bufferPtr);
*output = malloc(bufferPtr->length + 1);
if (!*output) {
BIO_free_all(bio);
return -1;
}

memcpy(*output, bufferPtr->data, bufferPtr->length);
(*output)[bufferPtr->length] = '\0';

BIO_free_all(bio);
return 0;
}

int
base64_decode_key(const char *input, uint8_t *output, size_t *output_len)
{
BIO *bio, *b64;
int decoded_len;

if (!input || !output || !output_len || strlen(input) == 0) {
return -1;
}

*output_len = 0;

b64 = BIO_new(BIO_f_base64());
bio = BIO_new_mem_buf((void*)input, strlen(input));
if (!b64 || !bio) {
if (b64) BIO_free(b64);
if (bio) BIO_free(bio);
return -1;
}

bio = BIO_push(b64, bio);
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);

decoded_len = BIO_read(bio, output, MAX_AES_XTS_PLAIN_KEYSIZE);
BIO_free_all(bio);

if (decoded_len < 0) {
return -1;
}

*output_len = decoded_len;
return 0;
}
2 changes: 1 addition & 1 deletion vhd/lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ libvhd_la_SOURCES += xattr.h

libvhd_la_LDFLAGS = -version-info 1:1:1

libvhd_la_LIBADD = -luuid -ldl $(LIBICONV) $(top_srcdir)/lvm/liblvmutil.la
libvhd_la_LIBADD = -luuid -ldl $(LIBICONV) $(top_builddir)/lib/libblktaputil.la $(top_srcdir)/lvm/liblvmutil.la

if ENABLE_TESTS
MAYBE_test = test
Expand Down
Loading