sign-ed25519: Convert functions to new style

The "new style" code generally avoids `goto err` because it conflicts
with `__attribute__((cleanup))`.  This fixes a compiler warning.

Signed-off-by: Denis Pynkin <denis.pynkin@collabora.com>
This commit is contained in:
Denis Pynkin 2020-02-18 00:11:38 +03:00
parent 09d5b475af
commit aaf73f6afc
1 changed files with 34 additions and 84 deletions

View File

@ -111,13 +111,9 @@ _ostree_sign_ed25519_is_initialized (OstreeSignEd25519 *self, GError **error)
case ED25519_OK: case ED25519_OK:
break; break;
case ED25519_NOT_SUPPORTED: case ED25519_NOT_SUPPORTED:
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, return glnx_throw(error, "ed25519: engine is not supported");
"ed25519: engine is not supported");
return FALSE;
case ED25519_FAILED_INITIALIZATION: case ED25519_FAILED_INITIALIZATION:
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, return glnx_throw(error, "ed25519: libsodium library isn't initialized properly");
"ed25519: libsodium library isn't initialized properly");
return FALSE;
} }
return TRUE; return TRUE;
@ -138,14 +134,11 @@ gboolean ostree_sign_ed25519_data (OstreeSign *self,
#endif #endif
if (!_ostree_sign_ed25519_is_initialized (sign, error)) if (!_ostree_sign_ed25519_is_initialized (sign, error))
goto err; return FALSE;
if (sign->secret_key == NULL) if (sign->secret_key == NULL)
{ return glnx_throw (error, "Not able to sign: secret key is not set");
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"secret key is not set");
goto err;
}
#ifdef HAVE_LIBSODIUM #ifdef HAVE_LIBSODIUM
unsigned long long sig_size = 0; unsigned long long sig_size = 0;
@ -157,16 +150,12 @@ gboolean ostree_sign_ed25519_data (OstreeSign *self,
g_bytes_get_size (data), g_bytes_get_size (data),
sign->secret_key)) sign->secret_key))
{ {
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, return glnx_throw (error, "Not able to sign: fail to sign the object");
"fail to sign the object");
goto err;
} }
*signature = g_bytes_new_take (sig, sig_size); *signature = g_bytes_new_take (sig, sig_size);
return TRUE; return TRUE;
#endif /* HAVE_LIBSODIUM */ #endif /* HAVE_LIBSODIUM */
err:
g_prefix_error (error, "Not able to sign: ");
return FALSE; return FALSE;
} }
@ -184,28 +173,17 @@ gboolean ostree_sign_ed25519_data_verify (OstreeSign *self,
{ {
g_return_val_if_fail (OSTREE_IS_SIGN (self), FALSE); g_return_val_if_fail (OSTREE_IS_SIGN (self), FALSE);
g_return_val_if_fail (data != NULL, FALSE); g_return_val_if_fail (data != NULL, FALSE);
gboolean ret = FALSE;
OstreeSignEd25519 *sign = _ostree_sign_ed25519_get_instance_private(OSTREE_SIGN_ED25519(self)); OstreeSignEd25519 *sign = _ostree_sign_ed25519_get_instance_private(OSTREE_SIGN_ED25519(self));
if (!_ostree_sign_ed25519_is_initialized (sign, error)) if (!_ostree_sign_ed25519_is_initialized (sign, error))
goto out; return FALSE;
if (signatures == NULL) if (signatures == NULL)
{ return glnx_throw (error, "ed25519: commit have no signatures of my type");
g_set_error_literal (error,
G_IO_ERROR, G_IO_ERROR_FAILED,
"ed25519: commit have no signatures of my type");
goto out;
}
if (!g_variant_is_of_type (signatures, (GVariantType *) OSTREE_SIGN_METADATA_ED25519_TYPE)) if (!g_variant_is_of_type (signatures, (GVariantType *) OSTREE_SIGN_METADATA_ED25519_TYPE))
{ return glnx_throw (error, "ed25519: wrong type passed for verification");
g_set_error_literal (error,
G_IO_ERROR, G_IO_ERROR_FAILED,
"ed25519: wrong type passed for verification");
goto out;
}
#ifdef HAVE_LIBSODIUM #ifdef HAVE_LIBSODIUM
/* If no keys pre-loaded then, /* If no keys pre-loaded then,
@ -219,7 +197,7 @@ gboolean ostree_sign_ed25519_data_verify (OstreeSign *self,
options = g_variant_builder_end (builder); options = g_variant_builder_end (builder);
if (!ostree_sign_ed25519_load_pk (self, options, error)) if (!ostree_sign_ed25519_load_pk (self, options, error))
goto out; return FALSE;
} }
g_debug ("verify: data hash = 0x%x", g_bytes_hash(data)); g_debug ("verify: data hash = 0x%x", g_bytes_hash(data));
@ -257,23 +235,17 @@ gboolean ostree_sign_ed25519_data_verify (OstreeSign *self,
} }
else else
{ {
ret = TRUE;
g_debug ("Signature verified successfully with key '%s'", g_debug ("Signature verified successfully with key '%s'",
sodium_bin2hex (hex, crypto_sign_PUBLICKEYBYTES*2+1, public_key->data, crypto_sign_PUBLICKEYBYTES)); sodium_bin2hex (hex, crypto_sign_PUBLICKEYBYTES*2+1, public_key->data, crypto_sign_PUBLICKEYBYTES));
break; return TRUE;
} }
} }
} }
if (ret != TRUE) return glnx_throw (error, "Not able to verify: no valid signatures found");
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"no valid signatures found");
#endif /* HAVE_LIBSODIUM */ #endif /* HAVE_LIBSODIUM */
out: return FALSE;
if (ret != TRUE)
g_prefix_error (error, "Not able to verify: ");
return ret;
} }
const gchar * ostree_sign_ed25519_get_name (OstreeSign *self) const gchar * ostree_sign_ed25519_get_name (OstreeSign *self)
@ -303,7 +275,7 @@ gboolean ostree_sign_ed25519_clear_keys (OstreeSign *self,
OstreeSignEd25519 *sign = _ostree_sign_ed25519_get_instance_private(OSTREE_SIGN_ED25519(self)); OstreeSignEd25519 *sign = _ostree_sign_ed25519_get_instance_private(OSTREE_SIGN_ED25519(self));
if (!_ostree_sign_ed25519_is_initialized (sign, error)) if (!_ostree_sign_ed25519_is_initialized (sign, error))
goto err; return FALSE;
#ifdef HAVE_LIBSODIUM #ifdef HAVE_LIBSODIUM
/* Clear secret key */ /* Clear secret key */
@ -331,7 +303,6 @@ gboolean ostree_sign_ed25519_clear_keys (OstreeSign *self,
return TRUE; return TRUE;
#endif /* HAVE_LIBSODIUM */ #endif /* HAVE_LIBSODIUM */
err:
return FALSE; return FALSE;
} }
@ -347,7 +318,7 @@ gboolean ostree_sign_ed25519_set_sk (OstreeSign *self,
if (!ostree_sign_ed25519_clear_keys (self, error)) if (!ostree_sign_ed25519_clear_keys (self, error))
goto err; return FALSE;
#ifdef HAVE_LIBSODIUM #ifdef HAVE_LIBSODIUM
OstreeSignEd25519 *sign = _ostree_sign_ed25519_get_instance_private(OSTREE_SIGN_ED25519(self)); OstreeSignEd25519 *sign = _ostree_sign_ed25519_get_instance_private(OSTREE_SIGN_ED25519(self));
@ -365,23 +336,15 @@ gboolean ostree_sign_ed25519_set_sk (OstreeSign *self,
} }
else else
{ {
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, return glnx_throw (error, "Unknown ed25519 secret key type");
"Unknown ed25519 secret key type");
goto err;
} }
if (n_elements != crypto_sign_SECRETKEYBYTES) if (n_elements != crypto_sign_SECRETKEYBYTES)
{ return glnx_throw (error, "Incorrect ed25519 secret key");
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Incorrect ed25519 secret key");
goto err;
}
return TRUE; return TRUE;
#endif /* HAVE_LIBSODIUM */ #endif /* HAVE_LIBSODIUM */
err:
return FALSE; return FALSE;
} }
@ -506,10 +469,10 @@ _load_pk_from_stream (OstreeSign *self,
gboolean added = FALSE; gboolean added = FALSE;
if (*error != NULL) if (*error != NULL)
goto err; return FALSE;
if (line == NULL) if (line == NULL)
goto out; return ret;
/* Read the key itself */ /* Read the key itself */
/* base64 encoded key */ /* base64 encoded key */
@ -529,11 +492,6 @@ _load_pk_from_stream (OstreeSign *self,
if (added) if (added)
ret = TRUE; ret = TRUE;
} }
out:
return ret;
err:
#endif /* HAVE_LIBSODIUM */ #endif /* HAVE_LIBSODIUM */
return FALSE; return FALSE;
} }
@ -553,15 +511,13 @@ _load_pk_from_file (OstreeSign *self,
if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR)) if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR))
{ {
g_debug ("Can't open file '%s' with public keys", filename); g_debug ("Can't open file '%s' with public keys", filename);
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, return glnx_throw (error, "File object '%s' is not a regular file", filename);
"File object '%s' is not a regular file", filename);
goto err;
} }
keyfile = g_file_new_for_path (filename); keyfile = g_file_new_for_path (filename);
key_stream_in = g_file_read (keyfile, NULL, error); key_stream_in = g_file_read (keyfile, NULL, error);
if (key_stream_in == NULL) if (key_stream_in == NULL)
goto err; return FALSE;
key_data_in = g_data_input_stream_new (G_INPUT_STREAM(key_stream_in)); key_data_in = g_data_input_stream_new (G_INPUT_STREAM(key_stream_in));
g_assert (key_data_in != NULL); g_assert (key_data_in != NULL);
@ -569,16 +525,12 @@ _load_pk_from_file (OstreeSign *self,
if (!_load_pk_from_stream (self, key_data_in, trusted, error)) if (!_load_pk_from_stream (self, key_data_in, trusted, error))
{ {
if (error == NULL || *error == NULL) if (error == NULL || *error == NULL)
g_set_error (error, return glnx_throw (error,
G_IO_ERROR, G_IO_ERROR_FAILED, "signature: ed25519: no valid keys in file '%s'",
"signature: ed25519: no valid keys in file '%s'", filename);
filename);
goto err;
} }
return TRUE; return TRUE;
err:
return FALSE;
} }
static gboolean static gboolean
@ -639,21 +591,19 @@ _ed25519_load_pk (OstreeSign *self,
/* Scan all well-known files */ /* Scan all well-known files */
for (gint i=0; i < ed25519_files->len; i++) for (gint i=0; i < ed25519_files->len; i++)
{ {
if (!_load_pk_from_file (self, (gchar *)g_ptr_array_index (ed25519_files, i), trusted, error)) if (!_load_pk_from_file (self, (gchar *)g_ptr_array_index (ed25519_files, i), trusted, error))
{ {
g_debug ("Problem with loading ed25519 %s keys from `%s`", g_debug ("Problem with loading ed25519 %s keys from `%s`",
trusted ? "public" : "revoked", trusted ? "public" : "revoked",
(gchar *)g_ptr_array_index (ed25519_files, i)); (gchar *)g_ptr_array_index (ed25519_files, i));
g_clear_error(error); g_clear_error(error);
} }
else else
ret = TRUE; ret = TRUE;
} }
if (!ret && (error == NULL || *error == NULL)) if (!ret && (error == NULL || *error == NULL))
g_set_error_literal (error, return glnx_throw (error, "signature: ed25519: no keys loaded");
G_IO_ERROR, G_IO_ERROR_FAILED,
"signature: ed25519: no keys loaded");
return ret; return ret;
} }