#include "asn1/asn1c/SigningTime.h"
static json_t *
-attr2json(asn_TYPE_descriptor_t const *td, struct CMSAttribute const *cattr)
+attr2json(asn_TYPE_descriptor_t const *td, CMSAttributeValue_t const *ber)
{
- json_t *array, *node;
- CMSAttributeValue_t *ber;
void *attr;
- int i;
asn_dec_rval_t rval;
+ json_t *json;
- array = json_array();
- if (array == NULL)
- return NULL;
-
- for (i = 0; i < cattr->attrValues.list.count; i++) {
- ber = cattr->attrValues.list.array[i];
- attr = NULL;
-
- rval = ber_decode(NULL, td, &attr, ber->buf, ber->size);
- if (rval.code != RC_OK)
- goto fail;
-
- node = td->op->json_encoder(td, attr);
-
- ASN_STRUCT_FREE(*td, attr);
-
- if (json_array_append_new(array, node) < 0)
- goto fail;
- }
+ attr = NULL;
+ rval = ber_decode(NULL, td, &attr, ber->buf, ber->size);
+ if (rval.code != RC_OK)
+ return NULL; /* TODO release attr? */
- return array;
+ json = td->op->json_encoder(td, attr);
-fail: json_decref(array);
- return NULL;
+ ASN_STRUCT_FREE(*td, attr);
+ return json;
}
json_t *
CMSAttribute_encode_json(const asn_TYPE_descriptor_t *td, const void *sptr)
{
struct CMSAttribute const *cattr = sptr;
+ json_t *root;
+ json_t *attrValues;
+ int a;
if (!cattr)
return json_null();
+
+ root = json_object();
+ if (root == NULL)
+ return NULL;
+
+ if (json_object_set_new(root, "attrType", OBJECT_IDENTIFIER_encode_json(NULL, &cattr->attrType)))
+ goto fail;
+ if (json_object_set_new(root, "attrValues", attrValues = json_array()))
+ goto fail;
+
if (OBJECT_IDENTIFIER_is_ContentType(&cattr->attrType))
- return attr2json(&asn_DEF_ContentType, cattr);
- if (OBJECT_IDENTIFIER_is_MessageDigest(&cattr->attrType))
- return attr2json(&asn_DEF_MessageDigest, cattr);
- if (OBJECT_IDENTIFIER_is_SigningTime(&cattr->attrType))
- return attr2json(&asn_DEF_SigningTime, cattr);
+ td = &asn_DEF_ContentType;
+ else if (OBJECT_IDENTIFIER_is_MessageDigest(&cattr->attrType))
+ td = &asn_DEF_MessageDigest;
+ else if (OBJECT_IDENTIFIER_is_SigningTime(&cattr->attrType))
+ td = &asn_DEF_SigningTime;
+ else
+ td = &asn_DEF_ANY;
+
+ for (a = 0; a < cattr->attrValues.list.count; a++)
+ if (json_array_append_new(attrValues, attr2json(td, cattr->attrValues.list.array[a])))
+ goto fail;
- return SEQUENCE_encode_json(td, sptr);
+ return root;
+
+fail: json_decref(root);
+ return NULL;
}
asn_TYPE_operation_t asn_OP_CMSAttribute = {
td = &asn_DEF_ContentType;
content_type = td->op->json_encoder(td, &ci->contentType);
- if (content_type == NULL)
- goto fail;
- if (json_object_set_new(parent, td->name, content_type))
+ if (json_object_set_new(parent, "contentType", content_type))
goto fail;
if (OBJECT_IDENTIFIER_is_SignedData(&ci->contentType)) {
if (content == NULL)
goto fail;
- if (json_object_set_new(parent, td->name, content))
+ if (json_object_set_new(parent, "content", content))
goto fail;
return parent;
content_type = td->op->json_encoder(td, &eci->eContentType);
if (content_type == NULL)
goto fail;
- if (json_object_set_new(parent, td->name, content_type))
+ if (json_object_set_new(parent, "eContentType", content_type))
goto fail;
if (OBJECT_IDENTIFIER_is_mft(&eci->eContentType)) {
if (content == NULL)
goto fail;
- if (json_object_set_new(parent, td->name, content))
+ if (json_object_set_new(parent, "eContent", content))
goto fail;
return parent;
#include "asn1/asn1c/SignedAttributes.h"
-static json_t *
-SignedAttributes_encode_json(const struct asn_TYPE_descriptor_s *td,
- const void *sptr)
-{
- json_t *result;
- const asn_anonymous_set_ *list;
- int i;
-
- if (!sptr)
- return json_null();
-
- result = json_object();
- if (result == NULL)
- return NULL;
-
- list = _A_CSET_FROM_VOID(sptr);
- td = &asn_DEF_CMSAttribute;
-
- for (i = 0; i < list->count; i++) {
- CMSAttribute_t *attr;
- json_t *node;
- char buf[OID_STR_MAXLEN];
- char const *key;
-
- attr = list->array[i];
- node = td->op->json_encoder(td, attr);
- if (node == NULL)
- goto fail;
-
- key = OBJECT_IDENTIFIER_to_string(&attr->attrType, buf);
- if (json_object_set_new(result, key, node) < 0)
- goto fail;
- }
-
- return result;
-
-fail: json_decref(result);
- return NULL;
-}
-
-asn_TYPE_operation_t asn_OP_SignedAttributes = {
- SET_OF_free,
- SET_OF_print,
- SET_OF_compare,
- SET_OF_decode_ber,
- SET_OF_encode_der,
- SignedAttributes_encode_json,
- SET_OF_encode_xer,
- 0 /* Use generic outmost tag fetcher */
-};
-
asn_TYPE_member_t asn_MBR_SignedAttributes_1[] = {
{ ATF_POINTER, 0, 0,
(ASN_TAG_CLASS_UNIVERSAL | (16 << 2)),
asn_TYPE_descriptor_t asn_DEF_SignedAttributes = {
"SignedAttributes",
"SignedAttributes",
- &asn_OP_SignedAttributes,
+ &asn_OP_SET_OF,
asn_DEF_SignedAttributes_tags_1,
sizeof(asn_DEF_SignedAttributes_tags_1)
/sizeof(asn_DEF_SignedAttributes_tags_1[0]), /* 1 */
ku_destroy,
};
+static json_t *
+rdn2json(STACK_OF(X509_NAME_ENTRY) *rdn)
+{
+ json_t *root;
+ json_t *child;
+ X509_NAME_ENTRY *name;
+ int n;
+
+ root = json_array();
+ if (root == NULL)
+ return NULL;
+
+ for (n = 0; n < sk_X509_NAME_ENTRY_num(rdn); n++) {
+ if (json_array_append_new(root, child = json_object()))
+ goto fail;
+
+ name = sk_X509_NAME_ENTRY_value(rdn, n);
+ if (json_object_set_new(child, "type", oid2json(X509_NAME_ENTRY_get_object(name))))
+ goto fail;
+ if (json_object_set_new(child, "value", asn1str2json(X509_NAME_ENTRY_get_data(name))))
+ goto fail;
+ }
+
+ return root;
+
+fail: json_decref(root);
+ return NULL;
+}
+
static json_t *
dpname2json(DIST_POINT_NAME const *dpn)
{
if (dpn == NULL)
return json_null();
- if (dpn->type) /* if relativename */
- return name2json(dpn->dpname);
- return gns2json(dpn->name.fullname);
+ return (dpn->type)
+ ? rdn2json(dpn->name.relativename)
+ : gns2json(dpn->name.fullname);
}
static json_t *
return NULL;
for (i = 0; i < sk_POLICYINFO_num(cp); i++)
- if (json_array_append_new(root, pi2json(sk_POLICYINFO_value(cp, i))) < 0)
+ if (json_array_append_new(root, pi2json(sk_POLICYINFO_value(cp, i))))
goto fail;
return root;
unsigned char bin[16];
char str[INET6_ADDRSTRLEN];
unsigned int length;
- json_t *root;
+ char full[INET6_ADDRSTRLEN + 4];
+ int written;
if (ap == NULL)
return json_null();
if (ap->flags & ASN1_STRING_FLAG_BITS_LEFT)
length -= ap->flags & 7;
- root = json_object();
- if (root == NULL)
- return NULL;
- if (json_object_set_new(root, "address", json_string(str)) < 0)
- goto fail;
- if (json_object_set_new(root, "length", json_integer(length)) < 0)
- goto fail;
-
- return root;
-
-fail: json_decref(root);
- return NULL;
+ written = snprintf(full, INET6_ADDRSTRLEN + 4, "%s/%u", str, length);
+ return json_stringn(full, written);
}
static json_t *
if (root == NULL)
goto fail;
for (i = 0; i < sk_IPAddressOrRange_num(iaor); i++)
- if (json_array_append_new(root, iaor2json(sk_IPAddressOrRange_value(iaor, i), af)) < 0)
+ if (json_array_append_new(root, iaor2json(sk_IPAddressOrRange_value(iaor, i), af)))
goto fail;
return root;
}
return NULL;
for (i = 0; i < sk_IPAddressFamily_num(iafs); i++)
- if (json_array_append_new(root, iaf2json(sk_IPAddressFamily_value(iafs, i))) < 0)
+ if (json_array_append_new(root, iaf2json(sk_IPAddressFamily_value(iafs, i))))
goto fail;
return root;
name2json(X509_NAME const *name)
{
json_t *root;
- json_t *child;
+ json_t *rdnSequence;
+ json_t *typeval;
int i;
if (name == NULL)
root = json_object();
if (root == NULL)
return NULL;
+ if (json_object_set_new(root, "rdnSequence", rdnSequence = json_array()))
+ goto fail;
for (i = 0; i < X509_NAME_entry_count(name); i++) {
- X509_NAME_ENTRY *entry;
+ X509_NAME_ENTRY *entry = X509_NAME_get_entry(name, i);
int nid;
const ASN1_STRING *data;
- entry = X509_NAME_get_entry(name, i);
- nid = OBJ_obj2nid(X509_NAME_ENTRY_get_object(entry));
+ if (json_array_append_new(rdnSequence, typeval = json_object()))
+ goto fail;
- data = X509_NAME_ENTRY_get_data(entry);
- if (data == NULL)
+ nid = OBJ_obj2nid(X509_NAME_ENTRY_get_object(entry));
+ if (json_object_set_new(typeval, "type", json_string(OBJ_nid2ln(nid))))
goto fail;
- child = json_stringn((char *)data->data, data->length);
- if (json_object_set_new(root, OBJ_nid2sn(nid), child) < 0)
+ data = X509_NAME_ENTRY_get_data(entry);
+ if (json_object_set_new(typeval, "value", json_stringn((char *)data->data, data->length)))
goto fail;
}
return NULL;
for (n = 0; n < sk_GENERAL_NAME_num(gns); n++)
- if (json_array_append_new(root, gn2json(sk_GENERAL_NAME_value(gns, n))) < 0)
+ if (json_array_append_new(root, gn2json(sk_GENERAL_NAME_value(gns, n))))
goto fail;
return root;
exts2json(const STACK_OF(X509_EXTENSION) *exts)
{
json_t *root;
+ json_t *child;
BIO *bio;
char *name;
int i;
if (sk_X509_EXTENSION_num(exts) <= 0)
return json_null();
- root = json_object();
+ root = json_array();
if (root == NULL)
return NULL;
for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
- json_t *node;
X509_EXTENSION *ex;
+ if (json_array_append_new(root, child = json_object()))
+ goto fail;
+
ex = sk_X509_EXTENSION_value(exts, i);
/* Get the extension name */
BIO_free_all(bio);
goto fail;
}
- name = bio2str(bio);
- /* Create node, add to parent */
- ret = json_object_set_new(root, name, node = json_object());
+ name = bio2str(bio);
+ ret = json_object_set_new(child, "extnID", json_string(name));
free(name);
- if (ret < 0)
+ if (ret)
goto fail;
- /* Child 1: Critical */
- if (json_object_set_new(node, "critical", X509_EXTENSION_get_critical(ex) ? json_true() : json_false()) < 0)
+ if (json_object_set_new(child, "critical", json_boolean(X509_EXTENSION_get_critical(ex))))
goto fail;
- /* Child 2: Value */
- if (json_object_set_new(node, "value", ext2json(ex)))
+ if (json_object_set_new(child, "extnValue", ext2json(ex)))
goto fail;
}