-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This will contain logic shared between ostree-prepare-root and libostree-1.so. It will just link to libgio.so, so as to avoid pulling in e.g. libcurl and other things. In other words, `ostree-prepare-root` will not link to `libostree-1.so`, but will pull in just what it needs from this library.
- Loading branch information
Showing
7 changed files
with
198 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# SPDX-License-Identifier: LGPL-2.0+ | ||
# | ||
# This library is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License as published by the Free Software Foundation; either | ||
# version 2 of the License, or (at your option) any later version. | ||
# | ||
# This library is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public | ||
# License along with this library. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
noinst_LTLIBRARIES += libotcore.la | ||
|
||
libotcore_la_SOURCES = \ | ||
src/libotcore/otcore.h \ | ||
src/libotcore/otcore-ed25519-verify.c \ | ||
$(NULL) | ||
|
||
libotcore_la_CFLAGS = $(AM_CFLAGS) -I$(srcdir)/libglnx -I$(srcdir)/src/libotutil -DLOCALEDIR=\"$(datadir)/locale\" $(OT_INTERNAL_GIO_UNIX_CFLAGS) $(OT_INTERNAL_GPGME_CFLAGS) $(OT_DEP_CRYPTO_LIBS) $(LIBSYSTEMD_CFLAGS) | ||
libotcore_la_LIBADD = $(OT_INTERNAL_GIO_UNIX_LIBS) $(OT_INTERNAL_GPGME_LIBS) $(LIBSYSTEMD_LIBS) $(OT_DEP_CRYPTO_LIBS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This library is (will be) shared between `libostree-1.so` and `ostree-prepare-root`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* SPDX-License-Identifier: LGPL-2.0+ | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "config.h" | ||
|
||
#include "otcore.h" | ||
|
||
/* Initialize global state; may be called multiple times and is idempotent. */ | ||
bool | ||
otcore_ed25519_init (void) | ||
{ | ||
#if defined(HAVE_LIBSODIUM) | ||
static gssize initstate; | ||
if (g_once_init_enter (&initstate)) | ||
{ | ||
int val = sodium_init () >= 0 ? 1 : -1; | ||
g_once_init_leave (&initstate, val); | ||
} | ||
switch (initstate) | ||
{ | ||
case 1: | ||
return true; | ||
case -1: | ||
return false; | ||
default: | ||
g_assert_not_reached (); | ||
} | ||
#else | ||
return true; | ||
#endif | ||
} | ||
|
||
/* Validate a single ed25519 signature. If there is an unexpected state, such | ||
* as an ill-forumed public key or signature, a hard error will be returned. | ||
* | ||
* If the signature is not correct, this function will return successfully, but | ||
* `out_valid` will be set to `false`. | ||
* | ||
* If the signature is correct, `out_valid` will be `true`. | ||
* */ | ||
gboolean | ||
otcore_validate_ed25519_signature (GBytes *data, GBytes *public_key, GBytes *signature, | ||
bool *out_valid, GError **error) | ||
{ | ||
// Since this is signature verification code, let's verify preconditions. | ||
g_assert (data); | ||
g_assert (public_key); | ||
g_assert (signature); | ||
g_assert (out_valid); | ||
// It is OK for error to be NULL, though according to GError rules. | ||
|
||
#if defined(HAVE_LIBSODIUM) || defined(HAVE_OPENSSL) | ||
// And strictly verify pubkey and signature lengths | ||
if (g_bytes_get_size (public_key) != OSTREE_SIGN_ED25519_PUBKEY_SIZE) | ||
return glnx_throw (error, "Invalid public key of %" G_GSIZE_FORMAT " expected %" G_GSIZE_FORMAT, | ||
(gsize)g_bytes_get_size (public_key), | ||
(gsize)OSTREE_SIGN_ED25519_PUBKEY_SIZE); | ||
const guint8 *public_key_buf = g_bytes_get_data (public_key, NULL); | ||
if (g_bytes_get_size (signature) != OSTREE_SIGN_ED25519_SIG_SIZE) | ||
return glnx_throw ( | ||
error, "Invalid signature length of %" G_GSIZE_FORMAT " bytes, expected %" G_GSIZE_FORMAT, | ||
(gsize)g_bytes_get_size (signature), (gsize)OSTREE_SIGN_ED25519_SIG_SIZE); | ||
const guint8 *signature_buf = g_bytes_get_data (signature, NULL); | ||
|
||
#endif | ||
|
||
#if defined(HAVE_LIBSODIUM) | ||
// Note that libsodium assumes the passed byte arrays for the signature and public key | ||
// have at least the expected length, but we checked that above. | ||
if (crypto_sign_verify_detached (signature_buf, g_bytes_get_data (data, NULL), | ||
g_bytes_get_size (data), public_key_buf) | ||
== 0) | ||
{ | ||
*out_valid = true; | ||
} | ||
return TRUE; | ||
#elif defined(HAVE_OPENSSL) | ||
EVP_MD_CTX *ctx = EVP_MD_CTX_new (); | ||
if (!ctx) | ||
return glnx_throw (error, "openssl: failed to allocate context"); | ||
EVP_PKEY *pkey = EVP_PKEY_new_raw_public_key (EVP_PKEY_ED25519, NULL, public_key_buf, | ||
OSTREE_SIGN_ED25519_PUBKEY_SIZE); | ||
if (!pkey) | ||
{ | ||
EVP_MD_CTX_free (ctx); | ||
return glnx_throw (error, "openssl: Failed to initialize ed5519 key"); | ||
} | ||
if (EVP_DigestVerifyInit (ctx, NULL, NULL, NULL, pkey) != 0 | ||
&& EVP_DigestVerify (ctx, signature_buf, OSTREE_SIGN_ED25519_SIG_SIZE, | ||
g_bytes_get_data (data, NULL), g_bytes_get_size (data)) | ||
!= 0) | ||
{ | ||
*out_valid = true; | ||
} | ||
EVP_PKEY_free (pkey); | ||
EVP_MD_CTX_free (ctx); | ||
return TRUE; | ||
#else | ||
return glnx_throw (error, "ed25519 signature validation requested, but support not compiled in"); | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* SPDX-License-Identifier: LGPL-2.0+ | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "config.h" | ||
|
||
#include "otutil.h" | ||
#include <stdbool.h> | ||
|
||
#ifdef HAVE_LIBSODIUM | ||
#include <sodium.h> | ||
#define USE_LIBSODIUM | ||
#elif defined(HAVE_OPENSSL) | ||
#include <openssl/evp.h> | ||
#define USE_OPENSSL | ||
#endif | ||
|
||
// Length of a signature in bytes | ||
#define OSTREE_SIGN_ED25519_SIG_SIZE 64U | ||
// Length of a public key in bytes | ||
#define OSTREE_SIGN_ED25519_PUBKEY_SIZE 32U | ||
// This key is stored inside commit metadata. | ||
#define OSTREE_SIGN_METADATA_ED25519_KEY "ostree.sign.ed25519" | ||
// The variant type | ||
#define OSTREE_SIGN_METADATA_ED25519_TYPE "aay" | ||
|
||
bool otcore_ed25519_init (void); | ||
gboolean otcore_validate_ed25519_signature (GBytes *data, GBytes *pubkey, GBytes *signature, | ||
bool *out_valid, GError **error); |