static bool _oct_private_fields( const cjose_jwk_t *jwk, json_t *json, cjose_err *err) { json_t *field = NULL; char *k = NULL; size_t klen = 0; uint8_t *keydata = (uint8_t *)jwk->keydata; size_t keysize = jwk->keysize / 8; if (!cjose_base64url_encode(keydata, keysize, &k, &klen, err)) { return false; } field = json_stringn(k, klen); cjose_get_dealloc()(k); k = NULL; if (!field) { return false; } json_object_set(json, "k", field); json_decref(field); return true; }
json_t * clevis_buf_encode(const clevis_buf_t *buf) { json_t *out = NULL; BIO *mem = NULL; BIO *b64 = NULL; char *c = NULL; int r = 0; mem = BIO_new(BIO_s_mem()); if (!mem) goto egress; b64 = BIO_new(BIO_f_base64()); if (!b64) goto egress; BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); if (!BIO_push(b64, mem)) goto egress; r = BIO_write(b64, buf->buf, buf->len); if (r != (int) buf->len) goto egress; BIO_flush(b64); r = BIO_get_mem_data(mem, &c); out = json_stringn(c, r); egress: BIO_free(b64); BIO_free(mem); return out; }
static bool _EC_private_fields( const cjose_jwk_t *jwk, json_t *json, cjose_err *err) { ec_keydata *keydata = (ec_keydata *)jwk->keydata; const BIGNUM *bnD = EC_KEY_get0_private_key(keydata->key); uint8_t *buffer = NULL; char *b64u = NULL; size_t len = 0, offset = 0; json_t *field = NULL; bool result = false; // track expected binary data size uint8_t numsize = _ec_size_for_curve(keydata->crv, err); // short circuit if 'd' is NULL or 0 if (!bnD || BN_is_zero(bnD)) { return true; } buffer = cjose_get_alloc()(numsize); if (!buffer) { CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY); goto _ec_to_string_cleanup; } offset = numsize - BN_num_bytes(bnD); memset(buffer, 0, numsize); BN_bn2bin(bnD, (buffer + offset)); if (!cjose_base64url_encode(buffer, numsize, &b64u, &len, err)) { goto _ec_to_string_cleanup; } field = json_stringn(b64u, len); if (!field) { CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY); goto _ec_to_string_cleanup; } json_object_set(json, "d", field); json_decref(field); field = NULL; cjose_get_dealloc()(b64u); b64u = NULL; result = true; _ec_to_string_cleanup: if (buffer) { cjose_get_dealloc()(buffer); } return result; }
static inline bool _RSA_json_field( BIGNUM *param, const char *name, json_t *json, cjose_err *err) { json_t *field = NULL; uint8_t *data = NULL; char *b64u = NULL; size_t datalen = 0, b64ulen = 0; bool result = false; if (!param) { return true; } datalen = BN_num_bytes(param); data = cjose_get_alloc()(sizeof(uint8_t) * datalen); if (!data) { CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY); goto RSA_json_field_cleanup; } BN_bn2bin(param, data); if (!cjose_base64url_encode(data, datalen, &b64u, &b64ulen, err)) { goto RSA_json_field_cleanup; } field = json_stringn(b64u, b64ulen); if (!field) { CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY); goto RSA_json_field_cleanup; } json_object_set(json, name, field); json_decref(field); field = NULL; result = true; RSA_json_field_cleanup: if (b64u) { cjose_get_dealloc()(b64u); b64u = NULL; } if (data) { cjose_get_dealloc()(data); data = NULL; } return result; }
static void encode_nul_byte() { json_t *json; char *result; json = json_stringn("nul byte \0 in string", 20); result = json_dumps(json, JSON_ENCODE_ANY); if(!result || memcmp(result, "\"nul byte \\u0000 in string\"", 27)) fail("json_dumps failed to dump an embedded NUL byte"); free(result); json_decref(json); }
static json_t* convert(object_t* object) { switch (object->type) { case type_bool: return json_boolean(object->b); case type_nil: return json_null(); case type_int: return json_integer(object->i); case type_double: return json_real(object->d); case type_uint: // Note: The generator limits unsigned int to INT64_MAX. Jansson // doesn't allow integers outside the range of signed int64. return json_integer((int64_t)object->u); case type_str: return json_stringn((const char*)object->str, object->l); case type_array: { json_t* array = json_array(); for (size_t i = 0; i < object->l; ++i) { json_t* child = convert(object->children + i); if (!child || json_array_append_new(array, child) != 0) { json_decref(array); return NULL; } } return array; } case type_map: { json_t* map = json_object(); for (size_t i = 0; i < object->l; ++i) { const char* key = object->children[i * 2].str; json_t* child = convert(object->children + i * 2 + 1); if (!child || json_object_set_new(map, key, child) != 0) { json_decref(map); return NULL; } } return map; } default: assert(0); break; } return false; }
static bool _EC_public_fields( const cjose_jwk_t *jwk, json_t *json, cjose_err *err) { ec_keydata *keydata = (ec_keydata *)jwk->keydata; const EC_GROUP *params = NULL; const EC_POINT *pub = NULL; BIGNUM *bnX = NULL, *bnY = NULL; uint8_t *buffer = NULL; char *b64u = NULL; size_t len = 0, offset = 0; json_t *field = NULL; bool result = false; // track expected binary data size uint8_t numsize = _ec_size_for_curve(keydata->crv, err); // output the curve field = json_string(_ec_name_for_curve(keydata->crv, err)); if (!field) { CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY); goto _ec_to_string_cleanup; } json_object_set(json, "crv", field); json_decref(field); field = NULL; // obtain the public key pub = EC_KEY_get0_public_key(keydata->key); params = EC_KEY_get0_group(keydata->key); if (!pub || !params) { CJOSE_ERROR(err, CJOSE_ERR_INVALID_ARG); goto _ec_to_string_cleanup; } buffer = cjose_get_alloc()(numsize); bnX = BN_new(); bnY = BN_new(); if (!buffer || !bnX || !bnY) { CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY); goto _ec_to_string_cleanup; } if (1 != EC_POINT_get_affine_coordinates_GFp(params, pub, bnX, bnY, NULL)) { CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY); goto _ec_to_string_cleanup; } // output the x coordinate offset = numsize - BN_num_bytes(bnX); memset(buffer, 0, numsize); BN_bn2bin(bnX, (buffer + offset)); if (!cjose_base64url_encode(buffer, numsize, &b64u, &len, err)) { goto _ec_to_string_cleanup; } field = json_stringn(b64u, len); if (!field) { CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY); goto _ec_to_string_cleanup; } json_object_set(json, "x", field); json_decref(field); field = NULL; cjose_get_dealloc()(b64u); b64u = NULL; // output the y coordinate offset = numsize - BN_num_bytes(bnY); memset(buffer, 0, numsize); BN_bn2bin(bnY, (buffer + offset)); if (!cjose_base64url_encode(buffer, numsize, &b64u, &len, err)) { goto _ec_to_string_cleanup; } field = json_stringn(b64u, len); if (!field) { CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY); goto _ec_to_string_cleanup; } json_object_set(json, "y", field); json_decref(field); field = NULL; cjose_get_dealloc()(b64u); b64u = NULL; result = true; _ec_to_string_cleanup: if (field) { json_decref(field); } if (bnX) { BN_free(bnX); } if (bnY) { BN_free(bnY); } if (buffer) { cjose_get_dealloc()(buffer); } if (b64u) { cjose_get_dealloc()(b64u); } return result; }
/* -------------------------------------------------------------------------------------------- * String constructor. */ JValue(CSStr val, std::size_t len, bool check = true) : m_Ptr(check ? json_stringn(val, len) : json_stringn_nocheck(val,len)) { /* ... */ }