Skip to content

Commit

Permalink
decoder: return certificates when decoding pem
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Wernli <[email protected]>
  • Loading branch information
Florian Wernli committed Feb 22, 2024
1 parent 846b8da commit 49d0fb5
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 24 deletions.
72 changes: 49 additions & 23 deletions src/decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,13 @@ static int filter_for_desired_data_type(const OSSL_PARAM params[], void *arg)
return RET_OSSL_CARRY_ON_DECODING;
}

static int p11prov_der_decoder_p11prov_obj_decode(
const char *desired_data_type, void *inctx, OSSL_CORE_BIO *cin,
int selection, OSSL_CALLBACK *object_cb, void *object_cbarg,
OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
static int load_obj(const P11PROV_DECODER_CTX *ctx, const unsigned char *der,
long der_len, struct desired_data_type_cbdata *cbdata,
OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
{
P11PROV_PK11_URI *obj = NULL;
BIO *bin;
char *uri = NULL;
P11PROV_DECODER_CTX *ctx = inctx;

bin = BIO_new_from_core_bio(p11prov_ctx_get_libctx(ctx->provctx), cin);
if (!bin) {
P11PROV_debug("P11 DECODER BIO_new_from_core_bio failed");
goto done;
}

const unsigned char *der;
long der_len = BIO_get_mem_data(bin, &der);
if (der_len <= 0) {
P11PROV_debug("P11 DECODER BIO_get_mem_data failed");
goto done;
}
obj = d2i_P11PROV_PK11_URI(NULL, &der, der_len);
if (!obj) {
P11PROV_debug("P11 KEY DECODER d2i_P11PROV_PK11_URI failed");
Expand All @@ -119,18 +104,49 @@ static int p11prov_der_decoder_p11prov_obj_decode(
goto done;
}

p11prov_store_direct_fetch(ctx->provctx, uri, filter_for_desired_data_type,
cbdata, pw_cb, pw_cbarg);
done:
OPENSSL_free(uri);
P11PROV_PK11_URI_free(obj);
return RET_OSSL_CARRY_ON_DECODING;
}

static int p11prov_der_decoder_p11prov_obj_decode(
const char *desired_data_type, void *inctx, OSSL_CORE_BIO *cin,
int selection, OSSL_CALLBACK *object_cb, void *object_cbarg,
OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
{
const P11PROV_DECODER_CTX *ctx = inctx;
BIO *bin;
unsigned char *der = NULL;
long der_len;
int ret = RET_OSSL_CARRY_ON_DECODING;

bin = BIO_new_from_core_bio(p11prov_ctx_get_libctx(ctx->provctx), cin);
if (!bin) {
P11PROV_debug("P11 DECODER BIO_new_from_core_bio failed");
goto done;
}

der_len = BIO_get_mem_data(bin, &der);
if (der_len <= 0) {
P11PROV_debug("P11 DECODER BIO_get_mem_data failed");
goto done;
}

struct desired_data_type_cbdata cbdata = {
.desired_data_type = desired_data_type,
.cb = object_cb,
.cbarg = object_cbarg,
};

p11prov_store_direct_fetch(ctx->provctx, uri, filter_for_desired_data_type,
&cbdata, pw_cb, pw_cbarg);
ret = load_obj(ctx, der, der_len, &cbdata, pw_cb, pw_cbarg);

done:
OPENSSL_free(uri);
P11PROV_PK11_URI_free(obj);
return RET_OSSL_CARRY_ON_DECODING;
BIO_free(bin);
P11PROV_debug("der decoder (carry on:%d)", ret);
return ret;
}

static int p11prov_der_decoder_p11prov_rsa_decode(
Expand Down Expand Up @@ -189,6 +205,16 @@ static int p11prov_pem_decoder_p11prov_der_decode(

if (PEM_read_bio(bin, &pem_label, &pem_header, &der_data, &der_len) > 0
&& strcmp(pem_label, P11PROV_PEM_LABEL) == 0) {

/* special case certificates */
struct desired_data_type_cbdata cbdata = {
.desired_data_type = P11PROV_NAME_CERTIFICATE,
.cb = object_cb,
.cbarg = object_cbarg,
};
load_obj(ctx, der_data, der_len, &cbdata, pw_cb, pw_cbarg);
/* */

params[0] = OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
der_data, der_len);
params[1] = OSSL_PARAM_construct_utf8_string(
Expand Down
1 change: 1 addition & 0 deletions src/provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#define P11PROV_DESCS_RAND "PKCS11 Random Generator"
#define P11PROV_NAMES_RSAKEY "RSA"
#define P11PROV_DESCS_RSAKEY "PKCS11 RSA Key"
#define P11PROV_NAME_CERTIFICATE "CERTIFICATE"

#define P11PROV_PARAM_URI "pkcs11_uri"
#define P11PROV_PARAM_KEY_USAGE "pkcs11_key_usage"
Expand Down
2 changes: 1 addition & 1 deletion src/store.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ static int p11prov_store_load(void *pctx, OSSL_CALLBACK *object_cb,
break;
case CKO_CERTIFICATE:
object_type = OSSL_OBJECT_CERT;
data_type = (char *)"CERTIFICATE";
data_type = (char *)P11PROV_NAME_CERTIFICATE;
cert = p11prov_obj_get_attr(obj, CKA_VALUE);
if (cert == NULL) {
return RET_OSSL_ERR;
Expand Down
13 changes: 13 additions & 0 deletions tests/tpem_encoder
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,17 @@ if [[ ! ${DATA} =~ "Total found: 1" ]]; then
exit 1
fi


title PARA "Test certificate is usable"
make-uri-pem "${ECCRTURI}" "${TMPPDIR}/eccrturi-cert.pem"
ossl '
storeutl
-out "${TMPPDIR}/storeutl-eccrturi-cert.txt"
"${TMPPDIR}/eccrturi-cert.pem"'
DATA=$(cat "${TMPPDIR}/storeutl-eccrturi-cert.txt")
if [[ ! ${DATA} =~ "Total found: 1" ]]; then
echo "Should return certificate"
exit 1
fi

exit 0

0 comments on commit 49d0fb5

Please sign in to comment.