Skip to content

Commit

Permalink
Better support for AES-CBC-HMAC with other key management algs (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxwolf authored Mar 6, 2018
1 parent 4d63064 commit 602555f
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 137 deletions.
2 changes: 1 addition & 1 deletion src/include/jwe_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ typedef struct _jwe_rec_fntable_int
typedef struct _jwe_fntable_int
{

bool (*set_cek)(cjose_jwe_t *jwe, const cjose_jwk_t *jwk, bool prealloc, cjose_err *err);
bool (*set_cek)(cjose_jwe_t *jwe, const cjose_jwk_t *jwk, bool random, cjose_err *err);

bool (*set_iv)(cjose_jwe_t *jwe, cjose_err *err);

Expand Down
53 changes: 37 additions & 16 deletions src/jwe.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@


////////////////////////////////////////////////////////////////////////////////
static bool _cjose_jwe_set_cek_a256gcm(cjose_jwe_t *jwe, const cjose_jwk_t *jwk, bool prealloc, cjose_err *err);
static bool _cjose_jwe_set_cek_a256gcm(cjose_jwe_t *jwe, const cjose_jwk_t *jwk, bool random, cjose_err *err);

static bool _cjose_jwe_set_cek_aes_cbc(cjose_jwe_t *jwe, const cjose_jwk_t *jwk, bool prealloc, cjose_err *err);
static bool _cjose_jwe_set_cek_aes_cbc(cjose_jwe_t *jwe, const cjose_jwk_t *jwk, bool random, cjose_err *err);

static bool
_cjose_jwe_encrypt_ek_dir(_jwe_int_recipient_t *recipient, cjose_jwe_t *jwe, const cjose_jwk_t *jwk, cjose_err *err);
Expand Down Expand Up @@ -361,7 +361,7 @@ static bool _cjose_jwe_validate_alg(cjose_header_t *protected_header,
}

////////////////////////////////////////////////////////////////////////////////
static bool _cjose_jwe_set_cek_a256gcm(cjose_jwe_t *jwe, const cjose_jwk_t *jwk, bool prealloc, cjose_err *err)
static bool _cjose_jwe_set_cek_a256gcm(cjose_jwe_t *jwe, const cjose_jwk_t *jwk, bool random, cjose_err *err)
{
// 256 bits = 32 bytes
static const size_t keysize = 32;
Expand All @@ -375,7 +375,7 @@ static bool _cjose_jwe_set_cek_a256gcm(cjose_jwe_t *jwe, const cjose_jwk_t *jwk,
if (NULL == jwk)
{
_cjose_release_cek(&jwe->cek, jwe->cek_len);
if (!_cjose_jwe_malloc(keysize, !prealloc, &jwe->cek, err))
if (!_cjose_jwe_malloc(keysize, random, &jwe->cek, err))
{
return false;
}
Expand Down Expand Up @@ -404,7 +404,7 @@ static bool _cjose_jwe_set_cek_a256gcm(cjose_jwe_t *jwe, const cjose_jwk_t *jwk,
}

////////////////////////////////////////////////////////////////////////////////
static bool _cjose_jwe_set_cek_aes_cbc(cjose_jwe_t *jwe, const cjose_jwk_t *cek, bool prealloc, cjose_err *err)
static bool _cjose_jwe_set_cek_aes_cbc(cjose_jwe_t *jwe, const cjose_jwk_t *jwk, bool random, cjose_err *err)
{

if (NULL != jwe->cek)
Expand All @@ -422,20 +422,42 @@ static bool _cjose_jwe_set_cek_aes_cbc(cjose_jwe_t *jwe, const cjose_jwk_t *cek,
const char *enc = json_string_value(enc_obj);

// determine the CEK key size based on the encryption algorithm
size_t keysize = 0;
if (strcmp(enc, CJOSE_HDR_ENC_A128CBC_HS256) == 0)
jwe->cek_len = 32;
keysize = 32;
if (strcmp(enc, CJOSE_HDR_ENC_A192CBC_HS384) == 0)
jwe->cek_len = 48;
keysize = 48;
if (strcmp(enc, CJOSE_HDR_ENC_A256CBC_HS512) == 0)
jwe->cek_len = 64;
keysize = 64;

// allocate memory for the CEK and fill with random bytes or 0's
_cjose_release_cek(&jwe->cek, jwe->cek_len);
if (!_cjose_jwe_malloc(jwe->cek_len, !prealloc, &jwe->cek, err))
if (NULL == jwk)
{
return false;
// allocate memory for the CEK and fill with random bytes or 0's
_cjose_release_cek(&jwe->cek, jwe->cek_len);
if (!_cjose_jwe_malloc(keysize, !random, &jwe->cek, err))
{
return false;
}
jwe->cek_len = keysize;
}
else
{
// if a JWK is provided, it must be a symmetric key of correct size
if (CJOSE_JWK_KTY_OCT != cjose_jwk_get_kty(jwk, err) || jwk->keysize != keysize * 8 || NULL == jwk->keydata)
{
CJOSE_ERROR(err, CJOSE_ERR_INVALID_ARG);
return false;
}

// copy the key material directly from jwk to the jwe->cek
_cjose_release_cek(&jwe->cek, jwe->cek_len);
if (!_cjose_jwe_malloc(keysize, false, &jwe->cek, err))
{
return false;
}
memcpy(jwe->cek, jwk->keydata, keysize);
jwe->cek_len = keysize;
}
return true;
}

Expand Down Expand Up @@ -483,7 +505,7 @@ _cjose_jwe_encrypt_ek_aes_kw(_jwe_int_recipient_t *recipient, cjose_jwe_t *jwe,
}

// generate random CEK
if (!jwe->fns.set_cek(jwe, NULL, false, err))
if (!jwe->fns.set_cek(jwe, NULL, true, err))
{
return false;
}
Expand Down Expand Up @@ -539,8 +561,7 @@ _cjose_jwe_decrypt_ek_aes_kw(_jwe_int_recipient_t *recipient, cjose_jwe_t *jwe,
return false;
}

// generate empty CEK so the the right amount of memory is allocated (abuse JWK parameter to empty)
if (!jwe->fns.set_cek(jwe, NULL, true, err))
if (!jwe->fns.set_cek(jwe, NULL, false, err))
{
return false;
}
Expand Down Expand Up @@ -579,7 +600,7 @@ static bool _cjose_jwe_encrypt_ek_rsa_padding(
}

// generate random cek
if (!jwe->fns.set_cek(jwe, NULL, false, err))
if (!jwe->fns.set_cek(jwe, NULL, true, err))
{
return false;
}
Expand Down
Loading

0 comments on commit 602555f

Please sign in to comment.