json: Refactor signature and ei code

* src/gpgme-json.c (add_summary_to_object): Changed to:
sigsum_to_json.
(add_signature_to_object): Changed to signature_to_json.
(add_signatures_to_object): Changed to verify_result_to_json.
(add_ei_to_object): Changed to engine_info_to_json.
(op_decrypt, op_verify, op_version): Use new functions.

--
This pattern of gpgme_type to json object conversion
is much cleaner then the error returning "add to object"
functions.
This commit is contained in:
Andre Heinecke 2018-05-25 12:17:46 +02:00
parent 546e3295ea
commit aa59f4970a
No known key found for this signature in database
GPG Key ID: 2978E9D40CBABA5C

View File

@ -646,46 +646,47 @@ data_from_base64_string (gpgme_data_t *r_data, cjson_t json)
} }
/* Helper for summary formatting */ /* Create sigsum json array */
static void static cjson_t
add_summary_to_object (cjson_t result, gpgme_sigsum_t summary) sigsum_to_json (gpgme_sigsum_t summary)
{ {
cjson_t response = xjson_CreateArray (); cjson_t result = xjson_CreateArray ();
if ( (summary & GPGME_SIGSUM_VALID )) if ( (summary & GPGME_SIGSUM_VALID ))
cJSON_AddItemToArray (response, cJSON_AddItemToArray (result,
cJSON_CreateString ("valid")); cJSON_CreateString ("valid"));
if ( (summary & GPGME_SIGSUM_GREEN )) if ( (summary & GPGME_SIGSUM_GREEN ))
cJSON_AddItemToArray (response, cJSON_AddItemToArray (result,
cJSON_CreateString ("green")); cJSON_CreateString ("green"));
if ( (summary & GPGME_SIGSUM_RED )) if ( (summary & GPGME_SIGSUM_RED ))
cJSON_AddItemToArray (response, cJSON_AddItemToArray (result,
cJSON_CreateString ("red")); cJSON_CreateString ("red"));
if ( (summary & GPGME_SIGSUM_KEY_REVOKED)) if ( (summary & GPGME_SIGSUM_KEY_REVOKED))
cJSON_AddItemToArray (response, cJSON_AddItemToArray (result,
cJSON_CreateString ("revoked")); cJSON_CreateString ("revoked"));
if ( (summary & GPGME_SIGSUM_KEY_EXPIRED)) if ( (summary & GPGME_SIGSUM_KEY_EXPIRED))
cJSON_AddItemToArray (response, cJSON_AddItemToArray (result,
cJSON_CreateString ("key-expired")); cJSON_CreateString ("key-expired"));
if ( (summary & GPGME_SIGSUM_SIG_EXPIRED)) if ( (summary & GPGME_SIGSUM_SIG_EXPIRED))
cJSON_AddItemToArray (response, cJSON_AddItemToArray (result,
cJSON_CreateString ("sig-expired")); cJSON_CreateString ("sig-expired"));
if ( (summary & GPGME_SIGSUM_KEY_MISSING)) if ( (summary & GPGME_SIGSUM_KEY_MISSING))
cJSON_AddItemToArray (response, cJSON_AddItemToArray (result,
cJSON_CreateString ("key-missing")); cJSON_CreateString ("key-missing"));
if ( (summary & GPGME_SIGSUM_CRL_MISSING)) if ( (summary & GPGME_SIGSUM_CRL_MISSING))
cJSON_AddItemToArray (response, cJSON_AddItemToArray (result,
cJSON_CreateString ("crl-missing")); cJSON_CreateString ("crl-missing"));
if ( (summary & GPGME_SIGSUM_CRL_TOO_OLD)) if ( (summary & GPGME_SIGSUM_CRL_TOO_OLD))
cJSON_AddItemToArray (response, cJSON_AddItemToArray (result,
cJSON_CreateString ("crl-too-old")); cJSON_CreateString ("crl-too-old"));
if ( (summary & GPGME_SIGSUM_BAD_POLICY )) if ( (summary & GPGME_SIGSUM_BAD_POLICY ))
cJSON_AddItemToArray (response, cJSON_AddItemToArray (result,
cJSON_CreateString ("bad-policy")); cJSON_CreateString ("bad-policy"));
if ( (summary & GPGME_SIGSUM_SYS_ERROR )) if ( (summary & GPGME_SIGSUM_SYS_ERROR ))
cJSON_AddItemToArray (response, cJSON_AddItemToArray (result,
cJSON_CreateString ("sys-error")); cJSON_CreateString ("sys-error"));
cJSON_AddItemToObject (result, "summary", response); return result;
} }
@ -930,145 +931,62 @@ key_to_json (gpgme_key_t key)
return result; return result;
} }
/* Add a single signature to a json object */ /* Create a signature json object */
static gpg_error_t static cjson_t
add_signature_to_object (cjson_t result, gpgme_signature_t sig) signature_to_json (gpgme_signature_t sig)
{ {
gpg_error_t err = 0; cjson_t result = xjson_CreateObject ();
if (!cJSON_AddStringToObject (result, "status", xjson_AddStringToObject0 (result, "status",
gpgme_strerror (sig->status))) gpgme_strerror (sig->status));
{
err = gpg_error_from_syserror ();
goto leave;
}
if (!cJSON_AddNumberToObject (result, "code", sig->status)) xjson_AddStringToObject0 (result, "validity",
{ validity_to_string (sig->validity));
err = gpg_error_from_syserror (); xjson_AddStringToObject0 (result, "fingerprint", sig->fpr);
goto leave;
}
add_summary_to_object (result, sig->summary); xjson_AddItemToObject (result, "summary", sigsum_to_json (sig->summary));
if (!cJSON_AddStringToObject (result, "fingerprint", sig->fpr)) xjson_AddNumberToObject (result, "created", sig->timestamp);
{ xjson_AddNumberToObject (result, "expired", sig->exp_timestamp);
err = gpg_error_from_syserror (); xjson_AddNumberToObject (result, "code", sig->status);
goto leave;
}
if (!cJSON_AddNumberToObject (result, "created", sig->timestamp)) return result;
{
err = gpg_error_from_syserror ();
goto leave;
}
if (!cJSON_AddNumberToObject (result, "expired", sig->exp_timestamp))
{
err = gpg_error_from_syserror ();
goto leave;
}
if (!cJSON_AddStringToObject (result, "validity",
validity_to_string (sig->validity)))
{
err = gpg_error_from_syserror ();
goto leave;
}
leave:
return err;
} }
/* Create a JSON object from a gpgme_verify result */
/* Add multiple signatures as an array to a result */ static cjson_t
static gpg_error_t verify_result_to_json (gpgme_verify_result_t verify_result)
add_signatures_to_object (cjson_t result, gpgme_signature_t signatures)
{
cjson_t response = xjson_CreateArray ();
gpg_error_t err = 0;
gpgme_signature_t sig;
for (sig = signatures; sig; sig = sig->next)
{
cjson_t sig_obj = xjson_CreateObject ();
err = add_signature_to_object (sig_obj, sig);
if (err)
{
cJSON_Delete (sig_obj);
sig_obj = NULL;
goto leave;
}
cJSON_AddItemToArray (response, sig_obj);
}
if (!cJSON_AddItemToObject (result, "signatures", response))
{
err = gpg_error_from_syserror ();
cJSON_Delete (response);
response = NULL;
return err;
}
response = NULL;
leave:
if (err && response)
{
cJSON_Delete (response);
response = NULL;
}
return err;
}
/* Add an array of signature informations under the name "name". */
static gpg_error_t
add_signatures_object (cjson_t result, const char *name,
gpgme_verify_result_t verify_result)
{ {
cjson_t response = xjson_CreateObject (); cjson_t response = xjson_CreateObject ();
gpg_error_t err = 0;
err = add_signatures_to_object (response, verify_result->signatures); if (verify_result->signatures)
if (err)
{ {
goto leave; cjson_t array = xjson_CreateArray ();
gpgme_signature_t sig;
for (sig = verify_result->signatures; sig; sig = sig->next)
cJSON_AddItemToArray (array, signature_to_json (sig));
xjson_AddItemToObject (response, "signatures", array);
} }
if (!cJSON_AddItemToObject (result, name, response)) return response;
{
err = gpg_error_from_syserror ();
goto leave;
}
leave:
if (err)
{
cJSON_Delete (response);
response = NULL;
}
return err;
} }
static gpg_error_t /* Create a JSON object from an engine_info */
add_ei_to_object (cjson_t result, gpgme_engine_info_t info) static cjson_t
engine_info_to_json (gpgme_engine_info_t info)
{ {
if (!cJSON_AddStringToObject (result, "protocol", cjson_t result = xjson_CreateObject ();
protocol_to_string (info->protocol)))
return gpg_error_from_syserror (); xjson_AddStringToObject0 (result, "protocol",
if (!cJSON_AddStringToObject (result, "fname", info->file_name)) protocol_to_string (info->protocol));
return gpg_error_from_syserror (); xjson_AddStringToObject0 (result, "fname", info->file_name);
if (!cJSON_AddStringToObject (result, "version", info->version)) xjson_AddStringToObject0 (result, "version", info->version);
return gpg_error_from_syserror (); xjson_AddStringToObject0 (result, "req_version", info->req_version);
if (!cJSON_AddStringToObject (result, "req_version", info->req_version)) xjson_AddStringToObject0 (result, "homedir", info->home_dir ?
return gpg_error_from_syserror (); info->home_dir :
if (!cJSON_AddStringToObject (result, "homedir", "default");
info->home_dir ? return result;
info->home_dir :
"default"))
return gpg_error_from_syserror ();
return 0;
} }
/* Create a gpgme_data from json string data named "name" /* Create a gpgme_data from json string data named "name"
@ -1371,6 +1289,17 @@ static const char hlp_decrypt[] =
"base64: Boolean indicating whether data is base64 encoded.\n" "base64: Boolean indicating whether data is base64 encoded.\n"
"mime: A Boolean indicating whether the data is a MIME object.\n" "mime: A Boolean indicating whether the data is a MIME object.\n"
"info: An optional object with extra information.\n" "info: An optional object with extra information.\n"
"info: An object with optional signature information.\n"
" Array values:\n"
" signatures\n"
" String values:\n"
" status: The status of the signature.\n"
" fingerprint: The fingerprint of the signing key.\n"
" validity: The validity as string.\n"
" Number values:\n"
" code: The status as a number.\n"
" Array values:\n"
" summary: A string array of the sig summary.\n"
"more: Optional boolean indicating that \"getmore\" is required."; "more: Optional boolean indicating that \"getmore\" is required.";
static gpg_error_t static gpg_error_t
op_decrypt (cjson_t request, cjson_t result) op_decrypt (cjson_t request, cjson_t result)
@ -1422,14 +1351,8 @@ op_decrypt (cjson_t request, cjson_t result)
verify_result = gpgme_op_verify_result (ctx); verify_result = gpgme_op_verify_result (ctx);
if (verify_result && verify_result->signatures) if (verify_result && verify_result->signatures)
{ {
err = add_signatures_object (result, "info", verify_result); xjson_AddItemToObject (result, "info",
} verify_result_to_json (verify_result));
if (err)
{
gpg_error_object (result, err, "Info output failed: %s",
gpg_strerror (err));
goto leave;
} }
err = make_data_object (result, output, chunksize, "plaintext", -1); err = make_data_object (result, output, chunksize, "plaintext", -1);
@ -1611,6 +1534,16 @@ static const char hlp_verify[] =
"data: The verified data. This may be base64 encoded.\n" "data: The verified data. This may be base64 encoded.\n"
"base64: Boolean indicating whether data is base64 encoded.\n" "base64: Boolean indicating whether data is base64 encoded.\n"
"info: An object with signature information.\n" "info: An object with signature information.\n"
" Array values:\n"
" signatures\n"
" String values:\n"
" status: The status of the signature.\n"
" fingerprint: The fingerprint of the signing key.\n"
" validity: The validity as string.\n"
" Number values:\n"
" code: The status as a number.\n"
" Array values:\n"
" summary: A string array of the sig summary.\n"
"more: Optional boolean indicating that \"getmore\" is required."; "more: Optional boolean indicating that \"getmore\" is required.";
static gpg_error_t static gpg_error_t
op_verify (cjson_t request, cjson_t result) op_verify (cjson_t request, cjson_t result)
@ -1669,14 +1602,8 @@ op_verify (cjson_t request, cjson_t result)
verify_result = gpgme_op_verify_result (ctx); verify_result = gpgme_op_verify_result (ctx);
if (verify_result && verify_result->signatures) if (verify_result && verify_result->signatures)
{ {
err = add_signatures_object (result, "info", verify_result); xjson_AddItemToObject (result, "info",
} verify_result_to_json (verify_result));
if (err)
{
gpg_error_object (result, err, "Info output failed: %s",
gpg_strerror (err));
goto leave;
} }
err = make_data_object (result, output, chunksize, "plaintext", -1); err = make_data_object (result, output, chunksize, "plaintext", -1);
@ -1728,15 +1655,7 @@ op_version (cjson_t request, cjson_t result)
} }
for (; ei; ei = ei->next) for (; ei; ei = ei->next)
{ cJSON_AddItemToArray (infos, engine_info_to_json (ei));
cjson_t obj = xjson_CreateObject ();
if ((err = add_ei_to_object (obj, ei)))
{
cJSON_Delete (infos);
return err;
}
cJSON_AddItemToArray (infos, obj);
}
if (!cJSON_AddItemToObject (result, "info", infos)) if (!cJSON_AddItemToObject (result, "info", infos))
{ {