Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
Signed-off-by: Gael Guegan <[email protected]>
  • Loading branch information
Gael Guegan committed Feb 22, 2019
2 parents b9a8b04 + 933d5d3 commit 515cbaf
Show file tree
Hide file tree
Showing 20 changed files with 275 additions and 68 deletions.
12 changes: 8 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ dist: trusty

env:
matrix:
- OPENSSL_BRANCH=OpenSSL_1_0_2-stable
- OPENSSL_BRANCH=OpenSSL_1_1_0-stable
- OPENSSL_BRANCH=OpenSSL_1_0_2-stable TPM2TSS_BRANCH=2.2.0
- OPENSSL_BRANCH=OpenSSL_1_1_0-stable TPM2TSS_BRANCH=2.1.0
- OPENSSL_BRANCH=OpenSSL_1_1_0-stable TPM2TSS_BRANCH=2.2.0
global:
- TPM2TOOLS_TCTI=mssim
- PATH="${PWD}/installdir/usr/local/bin:${PATH}"
- PKG_CONFIG_PATH="${PWD}/installdir/usr/local/lib/pkgconfig:/usr/lib/pkgconfig"
- LD_LIBRARY_PATH="${PWD}/installdir/usr/local/lib:/usr/lib"
Expand All @@ -29,6 +31,7 @@ addons:
- uthash-dev
- pandoc
- expect
- doxygen

install:
- git clean -xdf
Expand Down Expand Up @@ -82,11 +85,12 @@ install:
- tar xJf autoconf-archive-2017.09.28.tar.xz
- cp autoconf-archive-2017.09.28/m4/ax_code_coverage.m4 m4/
# TPM2-TSS
- git clone --depth=1 -b 2.1.0 https://github.com/tpm2-software/tpm2-tss.git
- git clone --depth=1 -b ${TPM2TSS_BRANCH} https://github.com/tpm2-software/tpm2-tss.git
- pushd tpm2-tss
- cp ../autoconf-archive-2017.09.28/m4/ax_code_coverage.m4 m4/
- cp ../autoconf-archive-2017.09.28/m4/ax_prog_doxygen.m4 m4/
- ./bootstrap
- ./configure CFLAGS=-I${PWD}/../installdir/usr/local/include LDFLAGS=-L${PWD}/../installdir/usr/local/lib
- ./configure CFLAGS=-I${PWD}/../installdir/usr/local/include LDFLAGS=-L${PWD}/../installdir/usr/local/lib --disable-doxygen-doc
- make -j$(nproc)
- make install DESTDIR=${PWD}/../installdir
- rm ${PWD}/../installdir/usr/local/lib/*.la
Expand Down
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ install-exec-local:
# Due to confusions with OpenSSL Naming conventions for engines regarding the
# lib* prefix, we will create a symlink for the engine on install
# see https://github.com/tpm2-software/tpm2-tss-engine/issues/6#issuecomment-422489744
# see https://github.com/openssl/openssl/commit/9ee0ed3de66678a15db126d10b3e4226e835b8f5
# see https://github.com/openssl/openssl/commit/9ee0ed3de66678a15db126d10b3e4226e835b8f5
install-exec-hook:
(cd $(DESTDIR)$(openssl_enginedir) && \
$(LN_S) -f libtpm2tss.so tpm2tss.so)
Expand Down
5 changes: 5 additions & 0 deletions include/tpm2-tss-engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,9 @@ tpm2tss_ecc_getappdata(EC_KEY *key);
int
tpm2tss_ecc_setappdata(EC_KEY *key, TPM2_DATA *data);

int
tpm2tss_sym_genkey(EVP_CIPHER_CTX *cipher, TPMI_ALG_PUBLIC algo,
TPMI_ALG_SYM_MODE mode, int bits,
char *password, TPM2_HANDLE parentHandle);

#endif /* TPM2_TSS_ENGINE_H */
177 changes: 152 additions & 25 deletions src/tpm2-tss-engine-ciphers.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,35 @@ typedef struct {
TPM2B_IV iv; // Initialization Vector
} TPM2_DATA_CIPHER;

static TPM2B_DATA allOutsideInfo = {
.size = 0,
};

static TPML_PCR_SELECTION allCreationPCR = {
.count = 0,
};

static TPM2B_PUBLIC keyTemplate = {
.publicArea = {
.type = TPM2_ALG_SYMCIPHER,
.nameAlg = ENGINE_HASH_ALG,
.objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
TPMA_OBJECT_SIGN_ENCRYPT |
TPMA_OBJECT_DECRYPT |
TPMA_OBJECT_FIXEDTPM |
TPMA_OBJECT_FIXEDPARENT |
TPMA_OBJECT_SENSITIVEDATAORIGIN |
TPMA_OBJECT_NODA),
.authPolicy.size = 0,
.parameters.symDetail.sym = {
.algorithm = TPM2_ALG_NULL,
.keyBits.sym = 0,
.mode.sym = 0,
},
.unique.sym.size = 0
}
};

static int tpm2_cipher_nids[] = {
NID_aes_192_ofb128,
NID_aes_256_cfb1,
Expand All @@ -67,6 +96,97 @@ static int convert_array_hex_to_int(const unsigned char *in, size_t size)
return integer;
}

int
tpm2tss_sym_genkey(EVP_CIPHER_CTX *cipher, TPMI_ALG_PUBLIC algo,
TPMI_ALG_SYM_MODE mode, int bits,
char *password, TPM2_HANDLE parentHandle)
{
//DBG("Generating %s in %s mode for %i bits keysize.\n", algo, mode, bits);
DBG("Generating for %i bits keysize.\n", bits);

TSS2_RC r = TSS2_RC_SUCCESS;
ESYS_AUXCONTEXT eactx = (ESYS_AUXCONTEXT) { 0 };
ESYS_TR parent = ESYS_TR_NONE;
TPM2B_PUBLIC *keyPublic = NULL;
TPM2B_PRIVATE *keyPrivate = NULL;
TPM2_DATA *tpm2Data = NULL;
TPM2B_PUBLIC inPublic = keyTemplate;
TPM2B_SENSITIVE_CREATE inSensitive = {
.sensitive = {
.userAuth = {
.size = 0,
},
.data = {
.size = 0,
}
}
};

tpm2Data = OPENSSL_malloc(sizeof(*tpm2Data));
if (tpm2Data == NULL) {
ERR(tpm2tss_sym_genkey, TPM2TSS_R_GENERAL_FAILURE);
goto error;
}
memset(tpm2Data, 0, sizeof(*tpm2Data));

// Set mode, algo, keysize
inPublic.publicArea.parameters.symDetail.sym.algorithm = algo;
inPublic.publicArea.parameters.symDetail.sym.keyBits.sym = bits;
inPublic.publicArea.parameters.symDetail.sym.mode.sym = mode;

if (password) {
DBG("Setting a password for the created key.\n");
if (strlen(password) > sizeof(tpm2Data->userauth.buffer) - 1) {
goto error;
}
tpm2Data->userauth.size = strlen(password);
memcpy(&tpm2Data->userauth.buffer[0], password,
tpm2Data->userauth.size);

inSensitive.sensitive.userAuth.size = strlen(password);
memcpy(&inSensitive.sensitive.userAuth.buffer[0], password,
strlen(password));
} else
tpm2Data->emptyAuth = 1;

r = init_tpm_parent(&eactx, parentHandle, &parent);
ERRchktss(tpm2tss_sym_genkey, r, goto error);

tpm2Data->parent = parentHandle;

DBG("Generating the Sym key inside the TPM.\n");

r = Esys_Create(eactx.ectx, parent,
ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
&inSensitive, &inPublic, &allOutsideInfo, &allCreationPCR,
&keyPrivate, &keyPublic, NULL, NULL, NULL);
ERRchktss(tpm2tss_sym_genkey, r, goto error);

DBG("Generated the Sym key inside the TPM.\n");

tpm2Data->pub = *keyPublic;
tpm2Data->priv = *keyPrivate;

EVP_CIPHER_CTX_set_app_data(cipher, tpm2Data);

goto end;
error:
r = -1;
if (tpm2Data)
OPENSSL_free(tpm2Data);

end:
free(keyPrivate);
free(keyPublic);

if (parent != ESYS_TR_NONE && !parentHandle)
Esys_FlushContext(eactx.ectx, parent);

esys_auxctx_free(&eactx);

return (r == TSS2_RC_SUCCESS);
}

static int populate_tpm2data(const unsigned char *key, TPM2_DATA **tpm2Data)
{
uint32_t keyHandle = 0;
Expand All @@ -89,45 +209,29 @@ static int populate_tpm2data(const unsigned char *key, TPM2_DATA **tpm2Data)

/* Use blob context */
} else {
if (!tpm2tss_tpm2data_read((char *)key, tpm2Data))
return 0;
//if (!tpm2tss_tpm2data_read((char *)key, tpm2Data))
// return 0;
}

return 1;
}

static TPMI_ALG_SYM_MODE tpm2_get_cipher_mode(EVP_CIPHER_CTX *ctx, TPM2_DATA_CIPHER *tpm2DataCipher)
{
TPMI_ALG_SYM_MODE mode_tpm2;
unsigned long mode_ctx;

mode_ctx = EVP_CIPHER_CTX_mode(ctx);
switch (mode_ctx) {
switch (EVP_CIPHER_CTX_mode(ctx)) {
case EVP_CIPH_CFB_MODE:
mode_tpm2 = TPM2_ALG_CFB;
break;
return TPM2_ALG_CFB;
case EVP_CIPH_OFB_MODE:
mode_tpm2 = TPM2_ALG_OFB;
break;
return TPM2_ALG_OFB;
case EVP_CIPH_CTR_MODE:
mode_tpm2 = TPM2_ALG_CTR;
break;
return TPM2_ALG_CTR;
case EVP_CIPH_ECB_MODE:
mode_tpm2 = TPM2_ALG_ECB;
break;
return TPM2_ALG_ECB;
case EVP_CIPH_CBC_MODE:
mode_tpm2 = TPM2_ALG_CBC;
break;
return TPM2_ALG_CBC;
default:
mode_tpm2 = tpm2DataCipher->tpm2Data->pub.publicArea.parameters.symDetail.sym.mode.sym;
break;
return tpm2DataCipher->tpm2Data->pub.publicArea.parameters.symDetail.sym.mode.sym;
}
if (mode_tpm2 == TPM2_ALG_NULL)
mode_tpm2 = TPM2_ALG_CFB;

mode_tpm2 = tpm2DataCipher->tpm2Data->pub.publicArea.parameters.symDetail.sym.mode.sym;

return mode_tpm2;
}

static int
Expand Down Expand Up @@ -303,6 +407,24 @@ tpm2_cipher_cleanup(EVP_CIPHER_CTX *ctx)
return 1;
}

#if OPENSSL_VERSION_NUMBER < 0x10100000
static EVP_CIPHER tpm2_aes_256_cbc =
{
NID_aes_256_cbc, // ID
TPM2_MAX_SYM_BLOCK_SIZE, // Block size
TPM2_MAX_SYM_KEY_BYTES, // Key length
TPM2_MAX_SYM_BLOCK_SIZE, // IV length
EVP_CIPH_CBC_MODE, // Flags
tpm2_cipher_init_key, // Init key
tpm2_do_cipher, // Encrypt/Decrypt
tpm2_cipher_cleanup, // Cleanup
sizeof(TPM2_DATA_CIPHER), // Context size
NULL, // Set ASN1 parameters
NULL, // Get ASN1 parameters
NULL, // CTRL
NULL // App data
};
#else
static EVP_CIPHER *_tpm2_aes_256_cbc = NULL;
const EVP_CIPHER *tpm2_aes_256_cbc(void)
{
Expand All @@ -323,6 +445,7 @@ const EVP_CIPHER *tpm2_aes_256_cbc(void)
}
return _tpm2_aes_256_cbc;
}
#endif

static int
tpm2_ciphers_selector(ENGINE *e, const EVP_CIPHER **cipher, const int **nids, int nid)
Expand All @@ -337,7 +460,11 @@ tpm2_ciphers_selector(ENGINE *e, const EVP_CIPHER **cipher, const int **nids, in

switch (nid) {
case NID_aes_256_cbc:
#if OPENSSL_VERSION_NUMBER < 0x10100000
*cipher = &tpm2_aes_256_cbc;
#else
*cipher = tpm2_aes_256_cbc();
#endif
break;
/*
case NID_aes_256_ocb:
Expand Down
15 changes: 14 additions & 1 deletion src/tpm2-tss-engine-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include <openssl/engine.h>
#include <openssl/pem.h>
#include <openssl/rand.h>

#include "tpm2-tss-engine.h"
#include "tpm2-tss-engine-common.h"
Expand Down Expand Up @@ -251,6 +252,16 @@ tpm2tss_tpm2data_readtpm(uint32_t handle, TPM2_DATA **tpm2Datap)
.mode = {.aes = TPM2_ALG_CFB}
};

/* Esys_StartAuthSession() and session handling use OpenSSL for random
bytes and thus might end up inside this engine again. This becomes
a problem if we have no resource manager, i.e. the tpm simulator. */
const RAND_METHOD *rand_save = RAND_get_rand_method();
#if OPENSSL_VERSION_NUMBER < 0x10100000
RAND_set_rand_method(RAND_SSLeay());
#else /* OPENSSL_VERSION_NUMBER < 0x10100000 */
RAND_set_rand_method(RAND_OpenSSL());
#endif

/* We do the check by starting a bound audit session and executing a
very cheap command. */
r = Esys_StartAuthSession(eactx.ectx, ESYS_TR_NONE, keyHandle,
Expand All @@ -275,7 +286,9 @@ tpm2tss_tpm2data_readtpm(uint32_t handle, TPM2_DATA **tpm2Datap)

r = Esys_ReadPublic(eactx.ectx, keyHandle,
session, ESYS_TR_NONE, ESYS_TR_NONE,
&outPublic, NULL, NULL);
NULL, NULL, NULL);

RAND_set_rand_method(rand_save);

/* tpm2-tss < 2.2 has some bugs. (1) it may miscalculate the auth from
above leading to a password query in case of empty auth and (2) it
Expand Down
1 change: 1 addition & 0 deletions src/tpm2-tss-engine-err.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ static ERR_STRING_DATA TPM2TSS_f[] = {
/* tpm2-tss-engine-ciphers.c */
ERR_F(tpm2_cipher_init_key),
ERR_F(tpm2_do_cipher),
ERR_F(tpm2tss_sym_genkey),
{0, NULL}
};

Expand Down
1 change: 1 addition & 0 deletions src/tpm2-tss-engine-err.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ void ERR_error(int function, int reason, const char *file, int line);
/* tpm2-tss-engine-ciphers.c */
#define TPM2TSS_F_tpm2_cipher_init_key 157
#define TPM2TSS_F_tpm2_do_cipher 158
#define TPM2TSS_F_tpm2tss_sym_genkey 159

/* Reason codes */
#define TPM2TSS_R_TPM2DATA_READ_FAILED 100
Expand Down
Loading

0 comments on commit 515cbaf

Please sign in to comment.