Skip to content

Commit

Permalink
Merge pull request #99 from openwrtdiy/openwrt-23.05
Browse files Browse the repository at this point in the history
Openwrt 23.05
  • Loading branch information
openwrtdiy authored Nov 9, 2023
2 parents b5740b7 + 5106f55 commit dd7a128
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package/utils/px5g-mbedtls/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
include $(TOPDIR)/rules.mk

PKG_NAME:=px5g-mbedtls
PKG_RELEASE:=9
PKG_RELEASE:=10
PKG_LICENSE:=LGPL-2.1

PKG_BUILD_FLAGS:=no-mips16
Expand Down
35 changes: 26 additions & 9 deletions package/utils/px5g-mbedtls/px5g-mbedtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <unistd.h>
#include <fcntl.h>
#include <stdbool.h>
#include <errno.h>

#include <mbedtls/bignum.h>
#include <mbedtls/entropy.h>
Expand All @@ -55,10 +56,13 @@ static int _urandom(void *ctx, unsigned char *out, size_t len)
return 0;
}

static void write_file(const char *path, int len, bool pem)
static void write_file(const char *path, size_t len, bool pem, bool cert)
{
FILE *f = stdout;
mode_t mode = S_IRUSR | S_IWUSR;
const char *buf_start = buf;
int fd = STDERR_FILENO;
ssize_t written;
int err;

if (!pem)
buf_start += sizeof(buf) - len;
Expand All @@ -67,17 +71,30 @@ static void write_file(const char *path, int len, bool pem)
fprintf(stderr, "No data to write\n");
exit(1);
}

if (cert)
mode |= S_IRGRP | S_IROTH;

if (!f) {
if (path)
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);

if (fd < 0) {
fprintf(stderr, "error: I/O error\n");
exit(1);
}

written = write(fd, buf_start, len);
if (written != len) {
fprintf(stderr, "writing key failed with: %s\n", strerror(errno));
exit(1);
}
err = fsync(fd);
if (err < 0) {
fprintf(stderr, "syncing key failed with: %s\n", strerror(errno));
exit(1);
}
if (path)
f = fopen(path, "w");

fwrite(buf_start, 1, len, f);
fclose(f);
close(fd);
}

static mbedtls_ecp_group_id ecp_curve(const char *name)
Expand Down Expand Up @@ -110,7 +127,7 @@ static void write_key(mbedtls_pk_context *key, const char *path, bool pem)
len = 0;
}

write_file(path, len, pem);
write_file(path, len, pem, false);
}

static void gen_key(mbedtls_pk_context *key, bool rsa, int ksize, int exp,
Expand Down Expand Up @@ -301,7 +318,7 @@ int selfsigned(char **arg)
return 1;
}
}
write_file(certpath, len, pem);
write_file(certpath, len, pem, true);

mbedtls_x509write_crt_free(&cert);
mbedtls_mpi_free(&serial);
Expand Down
2 changes: 1 addition & 1 deletion package/utils/px5g-wolfssl/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
include $(TOPDIR)/rules.mk

PKG_NAME:=px5g-wolfssl
PKG_RELEASE:=8.2
PKG_RELEASE:=9
PKG_LICENSE:=GPL-2.0-or-later

PKG_BUILD_FLAGS:=no-mips16
Expand Down
45 changes: 29 additions & 16 deletions package/utils/px5g-wolfssl/px5g-wolfssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <wolfssl/options.h>
#include <wolfssl/wolfcrypt/asn.h>
#include <wolfssl/wolfcrypt/asn_public.h>
Expand All @@ -24,27 +26,38 @@ enum {
RSA_KEY_TYPE = 1,
};

int write_file(byte *buf, int bufSz, char *path) {
int ret;
FILE *file;
int write_file(byte *buf, int bufSz, char *path, bool cert) {
mode_t mode = S_IRUSR | S_IWUSR;
ssize_t written;
int err;
int fd;

if (cert)
mode |= S_IRGRP | S_IROTH;

if (path) {
file = fopen(path, "wb");
if (file == NULL) {
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
if (fd < 0) {
perror("Error opening file");
exit(1);
}
} else {
file = stdout;
fd = STDERR_FILENO;
}
ret = (int)fwrite(buf, 1, bufSz, file);
if (path) {
fclose(file);
written = write(fd, buf, bufSz);
if (written != bufSz) {
perror("Error write file");
exit(1);
}
if (ret > 0) {
/* ret > 0 indicates a successful file write, set to zero for return */
ret = 0;
err = fsync(fd);
if (err < 0) {
perror("Error fsync file");
exit(1);
}
return ret;
if (path) {
close(fd);
}
return 0;
}

int write_key(ecc_key *ecKey, RsaKey *rsaKey, int type, int keySz, char *fName,
Expand Down Expand Up @@ -73,9 +86,9 @@ int write_key(ecc_key *ecKey, RsaKey *rsaKey, int type, int keySz, char *fName,
fprintf(stderr, "DER to PEM failed: %d\n", ret);
}
pemSz = ret;
ret = write_file(pem, pemSz, fName);
ret = write_file(pem, pemSz, fName, false);
} else {
ret = write_file(der, derSz, fName);
ret = write_file(der, derSz, fName, false);
}
return ret;
}
Expand Down Expand Up @@ -281,7 +294,7 @@ int selfsigned(WC_RNG *rng, char **arg) {
}
pemSz = ret;

ret = write_file(pemBuf, pemSz, certpath);
ret = write_file(pemBuf, pemSz, certpath, true);
if (ret != 0) {
fprintf(stderr, "Write Cert failed: %d\n", ret);
return ret;
Expand Down

0 comments on commit dd7a128

Please sign in to comment.