jwk_sign(const json_t* to_sign, const json_t* sig_keys)
{
    if (!sig_keys || !json_is_array(sig_keys) || !json_is_array(to_sign)) {
        return NULL;
    }

    json_auto_t* to_sign_copy = json_deep_copy(to_sign);
    if (!jose_jwk_pub(NULL, to_sign_copy)) {
        fprintf(stderr, "Error removing private material from data to sign\n");
        return NULL;
    }

    json_auto_t* payload = json_pack("{s:O}", "keys", to_sign_copy);
    json_auto_t* sig_template = json_pack("{s:{s:s}}",
                                          "protected", "cty", "jwk-set+json");

    __attribute__ ((__cleanup__(cleanup_str))) char* data_to_sign = json_dumps(payload, 0);
    json_auto_t* jws = json_pack("{s:o}", "payload",
                                 jose_b64_enc(data_to_sign, strlen(data_to_sign)));

    if (!jose_jws_sig(NULL, jws, sig_template, sig_keys)) {
        fprintf(stderr, "Error trying to jose_jws_sign\n");
        return NULL;
    }
    return json_incref(jws);
}
jwk_sign(const json_t* to_sign, const json_t* sig_keys)
{
    if (!sig_keys || !json_is_array(sig_keys) || !json_is_array(to_sign)) {
        return NULL;
    }

    json_auto_t* to_sign_copy = json_deep_copy(to_sign);
    if (!jose_jwk_pub(NULL, to_sign_copy)) {
        fprintf(stderr, "Error removing private material from data to sign\n");
        return NULL;
    }

    json_auto_t* payload = json_pack("{s:O}", "keys", to_sign_copy);
    json_auto_t* sig_template = json_pack("{s:{s:s}}",
                                          "protected", "cty", "jwk-set+json");

    __attribute__ ((__cleanup__(cleanup_str))) char* data_to_sign = json_dumps(payload, 0);
    json_auto_t* jws = json_pack("{s:o}", "payload",
                                 jose_b64_enc(data_to_sign, strlen(data_to_sign)));

    if (!jose_jws_sig(NULL, jws, sig_template, sig_keys)) {
        fprintf(stderr, "Error trying to jose_jws_sign\n");
        return NULL;
    }
    return json_incref(jws);
}