Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ result-*
.project
.cproject
.settings/
.cache/

# System files
.DS_Store
Expand Down
2 changes: 1 addition & 1 deletion include/apg/package.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ bool install_package(const struct package *pkg);

bool install_package_in_root(const struct package *pkg, const char *root_path);

struct package *parse_package(const char *path);
struct package *parse_package(const char *path, const char *root_path);


#endif
4 changes: 2 additions & 2 deletions include/apg/sign.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#pragma once

#include <stdbool.h>
#include <stdio.h>

bool sign_file(FILE *);
bool sign_file(const char *pkg_path);
bool sign_file_by_key(const char *path, const char *sig_path, const unsigned char *public_key);

2 changes: 2 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ libapg_dep = declare_dependency(
link_with: libapg,
include_directories: libapg_inc,
)

subdir('test')
21 changes: 15 additions & 6 deletions src/archive.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extract_to_dir(const char *archive_path, const char *path_dest)
{
struct archive_entry *entry;
char full_path[PATH_MAX];
const FILE *log_file = fopen(log_file_path, "a");
FILE *log_file = fopen(log_file_path, "a");

struct archive *a = archive_read_new();
archive_read_support_filter_xz(a);
Expand All @@ -26,8 +26,10 @@ extract_to_dir(const char *archive_path, const char *path_dest)
struct archive *ext = archive_write_disk_new();
archive_write_disk_set_options(ext, ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM);

if (archive_read_open_filename(a, archive_path, 10240) != ARCHIVE_OK)
if (archive_read_open_filename(a, archive_path, 10240) != ARCHIVE_OK) {
if (log_file) fclose(log_file);
return false;
}

while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
snprintf(full_path, sizeof(full_path), "%s/%s", path_dest, archive_entry_pathname(entry));
Expand Down Expand Up @@ -55,17 +57,24 @@ extract_to_dir(const char *archive_path, const char *path_dest)
archive_write_close(ext);
archive_write_free(ext);

if (log_file) fclose(log_file);
return true;
}

bool
unarchive_package(const struct package *pkg, const char *path)
unarchive_package_in_root(const struct package *pkg, const char *root)
{
if (!extract_to_dir(pkg->pkg_path, path)) {
log_two(ERR, "Failed to extract package into: ", (char*)path, stdout);
if (!extract_to_dir(pkg->pkg_path, root)) {
log_two(ERR, "Failed to extract package into: ", root, stdout);
return false;
}
log_two(WRN, "Package extracted successfully into: ", (char*)path, stdout);
log_two(INF, "Package extracted successfully into: ", root, stdout);
return true;
}

bool
unarchive_package(const struct package *pkg)
{
return unarchive_package_in_root(pkg, "/tmp/apg/");
}

8 changes: 7 additions & 1 deletion src/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ yyjson_val
return NULL; // is a stub, type actually incorrect
}

char
char
*json_to_string(yyjson_val *)
{
return NULL;
}

struct package_metadata *
package_metadata_from_file(const char *path)
{
return NULL; // stub
}
34 changes: 29 additions & 5 deletions src/package.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,42 @@ install_package_in_root(const struct package *pkg, const char *root_path)
struct package *
parse_package(const char *path, const char *root_path)
{
// ReSharper disable once CppDFAMemoryLeak
struct package *pkg = package_new();
if (!pkg) return NULL;

pkg->pkg_path = realpath(path, NULL);
if (!pkg->pkg_path) {
package_free(pkg);
return NULL;
}

char *real_tmp = concat_dirs(root_path, path);
if (!unarchive_package_in_root(pkg, real_tmp)) return NULL;
free(real_tmp);
char *real_tmp = concat_dirs(root_path, tmp_path);
if (!real_tmp) {
package_free(pkg);
return NULL;
}
create_dir(real_tmp);

package_metadata_from_file()
if (!unarchive_package_in_root(pkg, real_tmp)) {
free(real_tmp);
package_free(pkg);
return NULL;
}

char *meta_path = concat_dirs(real_tmp, "metadata.json");
free(real_tmp);
if (!meta_path) {
package_free(pkg);
return NULL;
}

package_metadata_free(pkg->meta);
pkg->meta = package_metadata_from_file(meta_path);
free(meta_path);
if (!pkg->meta) {
package_free(pkg);
return NULL;
}

return pkg;
}
54 changes: 39 additions & 15 deletions src/sign.c
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
// NurOS Ruzen42 2026 apg/sign.c
// Last change: Feb 2

#include <soduim.h>
#include <sodium.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#include "../include/util.h"

const char *key_path = "/etc/apg/keys/";

bool
sign_file_by_key(const char *path, const char *sig_path, const unsigned char *public_key[crypto_sign_PUBLICKEYBYTES])
bool
sign_file_by_key(const char *path, const char *sig_path, const unsigned char *public_key)
{
if (sodium_init() < 0) return false;

unsigned char signature[crypto_sign_BYTES];

FILE *sig_f = fopen(sig_path, "rb"); // open file

FILE *sig_f = fopen(sig_path, "rb");
if (!sig_f) return false;

int sig_read = fread(signature, 1, crypto_sign_BYTES, sig_f);
size_t sig_read = fread(signature, 1, crypto_sign_BYTES, sig_f);
fclose(sig_f);
if (sig_read != crypto_sign_BYTES) return false;

fclose(sig_f);

crypto_sign_state state; // Sodium signature state now
crypto_sign_state state;
crypto_sign_init(&state);

FILE *pkg_f = fopen(path, "rb");

if (!pkg_f) return false;

unsigned char buffer[4096]; // 4KB magic number
int bytes_read;
unsigned char buffer[4096];
size_t bytes_read;

while ((bytes_read = fread(buffer, 1, sizeof(buffer), pkg_f)) > 0) {
crypto_sign_update(&state, buffer, bytes_read);
Expand All @@ -47,8 +48,31 @@ sign_file_by_key(const char *path, const char *sig_path, const unsigned char *pu
bool
sign_file(const char *pkg_path)
{

char *sig_path = concat(pkg_path, ".sig");
if (!sig_path) return false;

char *key_file = concat(key_path, "trusted.pub");
if (!key_file) {
free(sig_path);
return false;
}

unsigned char public_key[crypto_sign_PUBLICKEYBYTES];
FILE *key_f = fopen(key_file, "rb");
free(key_file);
if (!key_f) {
free(sig_path);
return false;
}

size_t key_read = fread(public_key, 1, crypto_sign_PUBLICKEYBYTES, key_f);
fclose(key_f);
if (key_read != crypto_sign_PUBLICKEYBYTES) {
free(sig_path);
return false;
}

return sign_file_by_key(pkg_path

bool result = sign_file_by_key(pkg_path, sig_path, public_key);
free(sig_path);
return result;
}
10 changes: 0 additions & 10 deletions test/Makefile

This file was deleted.

6 changes: 6 additions & 0 deletions test/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apg_test = executable('apg-test',
files('src/main.c'),
dependencies: [libapg_dep],
)

test('apg-test', apg_test)