Skip to content

Commit

Permalink
Merge pull request #215 from ANSSI-FR/mlakey-parser
Browse files Browse the repository at this point in the history
Introduce "mlakey-parser" for ASN.1 MLA key import & export
  • Loading branch information
commial authored Aug 2, 2024
2 parents e56ffa7 + 27c0c52 commit 7403923
Show file tree
Hide file tree
Showing 45 changed files with 1,592 additions and 692 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/sanitize.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ jobs:
# Ensure CHANGELOGs respect the KeepAChangelog format
strategy:
matrix:
changelog: [curve25519-parser, mla, mlar]
changelog: [mlakey-parser, mla, mlar]
include:
- changelog: curve25519-parser
path: curve25519-parser
- changelog: mlakey-parser
path: mlakey-parser
- changelog: mla
path: mla
- changelog: mlar
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

members = [
"mla",
"curve25519-parser",
"mlakey-parser",
"mlar",
"mla-fuzz-afl",
"bindings/C",
Expand Down
3 changes: 2 additions & 1 deletion bindings/C/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ crate-type = ["cdylib", "staticlib"]

[dependencies]
mla = { path = "../../mla", version = "1" }
curve25519-parser = { path = "../../curve25519-parser", version = "0.4" }
mlakey-parser = { path = "../../mlakey-parser", version = "0", default-features = false }

4 changes: 3 additions & 1 deletion bindings/C/mla.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ enum MLAStatus {
MLA_STATUS_IO_ERROR = 65536,
MLA_STATUS_WRONG_MAGIC = 131072,
MLA_STATUS_UNSUPPORTED_VERSION = 196608,
MLA_STATUS_INVALID_ECC_KEY_FORMAT = 262144,
MLA_STATUS_INVALID_KEY_FORMAT = 262144,
MLA_STATUS_WRONG_BLOCK_SUB_FILE_TYPE = 327680,
MLA_STATUS_UTF8_CONVERSION_ERROR = 393216,
MLA_STATUS_FILENAME_TOO_LONG = 458752,
Expand All @@ -36,6 +36,8 @@ enum MLAStatus {
MLA_STATUS_CONFIG_ERROR_KEY_COMMITMENT_COMPUTATION_ERROR = 1310727,
MLA_STATUS_CONFIG_ERROR_KEY_COMMITMENT_CHECKING_ERROR = 1310728,
MLA_STATUS_CONFIG_ERROR_NO_RECIPIENTS = 1310729,
MLA_STATUS_CONFIG_ERROR_MLKEM_COMPUTATION_ERROR = 1310730,
MLA_STATUS_CONFIG_ERROR_KEY_WRAPPING_COMPUTATION_ERROR = 1310731,
MLA_STATUS_DUPLICATE_FILENAME = 1376256,
MLA_STATUS_AUTHENTICATED_DECRYPTION_WRONG_TAG = 1441792,
MLA_STATUS_HKDF_INVALID_KEY_LENGTH = 1507328,
Expand Down
4 changes: 3 additions & 1 deletion bindings/C/mla.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ enum class MLAStatus : uint64_t {
MLA_STATUS_IO_ERROR = 65536,
MLA_STATUS_WRONG_MAGIC = 131072,
MLA_STATUS_UNSUPPORTED_VERSION = 196608,
MLA_STATUS_INVALID_ECC_KEY_FORMAT = 262144,
MLA_STATUS_INVALID_KEY_FORMAT = 262144,
MLA_STATUS_WRONG_BLOCK_SUB_FILE_TYPE = 327680,
MLA_STATUS_UTF8_CONVERSION_ERROR = 393216,
MLA_STATUS_FILENAME_TOO_LONG = 458752,
Expand All @@ -37,6 +37,8 @@ enum class MLAStatus : uint64_t {
MLA_STATUS_CONFIG_ERROR_KEY_COMMITMENT_COMPUTATION_ERROR = 1310727,
MLA_STATUS_CONFIG_ERROR_KEY_COMMITMENT_CHECKING_ERROR = 1310728,
MLA_STATUS_CONFIG_ERROR_NO_RECIPIENTS = 1310729,
MLA_STATUS_CONFIG_ERROR_MLKEM_COMPUTATION_ERROR = 1310730,
MLA_STATUS_CONFIG_ERROR_KEY_WRAPPING_COMPUTATION_ERROR = 1310731,
MLA_STATUS_DUPLICATE_FILENAME = 1376256,
MLA_STATUS_AUTHENTICATED_DECRYPTION_WRONG_TAG = 1441792,
MLA_STATUS_HKDF_INVALID_KEY_LENGTH = 1507328,
Expand Down
26 changes: 16 additions & 10 deletions bindings/C/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#![allow(clippy::not_unsafe_ptr_arg_deref)]
use curve25519_parser::parse_openssl_25519_privkey;
use curve25519_parser::parse_openssl_25519_pubkeys_pem_many;
use mla::config::ArchiveReaderConfig;
use mla::config::ArchiveWriterConfig;
use mla::errors::ConfigError;
Expand All @@ -10,6 +8,8 @@ use mla::ArchiveHeader;
use mla::ArchiveReader;
use mla::ArchiveWriter;
use mla::{ArchiveFileID, Layers};
use mlakey_parser::parse_mlakey_privkey;
use mlakey_parser::parse_mlakey_pubkeys_pem_many;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::ffi::{c_void, CStr};
Expand All @@ -26,7 +26,7 @@ pub enum MLAStatus {
IOError = 0x010000,
WrongMagic = 0x020000,
UnsupportedVersion = 0x030000,
InvalidECCKeyFormat = 0x040000,
InvalidKeyFormat = 0x040000,
WrongBlockSubFileType = 0x050000,
UTF8ConversionError = 0x060000,
FilenameTooLong = 0x070000,
Expand All @@ -52,6 +52,8 @@ pub enum MLAStatus {
ConfigErrorKeyCommitmentComputationError = 0x140007,
ConfigErrorKeyCommitmentCheckingError = 0x140008,
ConfigErrorNoRecipients = 0x140009,
ConfigErrorMLKEMComputationError = 0x14000A,
ConfigErrorKeyWrappingComputationError = 0x14000B,
DuplicateFilename = 0x150000,
AuthenticatedDecryptionWrongTag = 0x160000,
HKDFInvalidKeyLength = 0x170000,
Expand Down Expand Up @@ -139,7 +141,7 @@ impl From<MLAError> for MLAStatus {
MLAError::IOError(_) => MLAStatus::IOError,
MLAError::WrongMagic => MLAStatus::WrongMagic,
MLAError::UnsupportedVersion => MLAStatus::UnsupportedVersion,
MLAError::InvalidECCKeyFormat => MLAStatus::InvalidECCKeyFormat,
MLAError::InvalidKeyFormat => MLAStatus::InvalidKeyFormat,
MLAError::WrongBlockSubFileType => MLAStatus::WrongBlockSubFileType,
MLAError::UTF8ConversionError(_) => MLAStatus::UTF8ConversionError,
MLAError::FilenameTooLong => MLAStatus::FilenameTooLong,
Expand All @@ -163,9 +165,7 @@ impl From<MLAError> for MLAStatus {
MLAError::ConfigError(ConfigError::CompressionLevelOutOfRange) => {
MLAStatus::ConfigErrorCompressionLevelOutOfRange
}
MLAError::ConfigError(ConfigError::NoRecipients) => {
MLAStatus::ConfigErrorNoRecipients
}
MLAError::ConfigError(ConfigError::NoRecipients) => MLAStatus::ConfigErrorNoRecipients,
MLAError::ConfigError(ConfigError::EncryptionKeyIsMissing) => {
MLAStatus::ConfigErrorEncryptionKeyIsMissing
}
Expand All @@ -184,6 +184,12 @@ impl From<MLAError> for MLAStatus {
MLAError::ConfigError(ConfigError::KeyCommitmentCheckingError) => {
MLAStatus::ConfigErrorKeyCommitmentCheckingError
}
MLAError::ConfigError(ConfigError::MLKEMComputationError) => {
MLAStatus::ConfigErrorMLKEMComputationError
}
MLAError::ConfigError(ConfigError::KeyWrappingComputationError) => {
MLAStatus::ConfigErrorKeyWrappingComputationError
}
MLAError::DuplicateFilename => MLAStatus::DuplicateFilename,
MLAError::AuthenticatedDecryptionWrongTag => MLAStatus::AuthenticatedDecryptionWrongTag,
MLAError::HKDFInvalidKeyLength => MLAStatus::HKDFInvalidKeyLength,
Expand Down Expand Up @@ -267,7 +273,7 @@ pub extern "C" fn mla_config_add_public_keys(
// Create a slice from the NULL-terminated string
let public_keys = unsafe { CStr::from_ptr(public_keys) }.to_bytes();
// Parse as OpenSSL Ed25519 public key(s)
let res = match parse_openssl_25519_pubkeys_pem_many(public_keys) {
let res = match parse_mlakey_pubkeys_pem_many(public_keys) {
Ok(v) if !v.is_empty() => {
config.add_public_keys(&v);
MLAStatus::Success
Expand Down Expand Up @@ -335,8 +341,8 @@ pub extern "C" fn mla_reader_config_add_private_key(

// Create a slice from the NULL-terminated string
let private_key = unsafe { CStr::from_ptr(private_key) }.to_bytes();
// Parse as OpenSSL Ed25519 private key(s)
let res = match parse_openssl_25519_privkey(private_key) {
// Parse as MLA private key(s)
let res = match parse_mlakey_privkey(private_key) {
Ok(v) => {
private_keys.push(v);
config.add_private_keys(&private_keys);
Expand Down
27 changes: 16 additions & 11 deletions bindings/C/tests/linux-gcc-g++/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CFLAGS ?= -Wall -Wextra -pedantic -Werror -O0 -g -I../../
CFLAGS ?= -Wall -Wextra -pedantic -Werror -Wno-overlength-strings -O0 -g -I../../
LDFLAGS ?= -O1 -lpthread -ldl -lm
BUILDDIR ?= ../../../../target
TARGET ?= x86_64-unknown-linux-gnu
Expand All @@ -8,12 +8,12 @@ tests: test_create test_create_manykeys test_create_set_compression_level test_c

test_create: $(MLAR) create.elf
./create.elf
$(MLAR) cat test.txt --input ./test.mla -k ../../../../samples/test_ed25519.pem | grep -E '^Hello, World!$$'
$(MLAR) cat test.txt --input ./test.mla -k ../../../../samples/test_mlakey.pem | grep -E '^Hello, World!$$'

test_create_manykeys: $(MLAR) create_manykeys.elf
./create_manykeys.elf
$(MLAR) cat test.txt --input ./test.mla -k ../../../../samples/test_ed25519.pem | grep -E '^Hello, World!$$'
$(MLAR) cat test.txt --input ./test.mla -k ../../../../samples/test_x25519.pem | grep -E '^Hello, World!$$'
$(MLAR) cat test.txt --input ./test.mla -k ../../../../samples/test_mlakey.pem | grep -E '^Hello, World!$$'
$(MLAR) cat test.txt --input ./test.mla -k ../../../../samples/test_mlakey_rev.der | grep -E '^Hello, World!$$'

test_create_set_compression_level: $(MLAR) create_set_compression_level.elf
./create_set_compression_level.elf
Expand All @@ -26,25 +26,25 @@ test_create_fail_no_key: $(MLAR) create_fail_no_key.elf

test_create_interleaved: $(MLAR) create_interleaved.elf
./create_interleaved.elf
$(MLAR) cat test1.txt --input ./test.mla -k ../../../../samples/test_ed25519.pem | grep -E '^Hello, World!$$'
$(MLAR) cat test2.txt --input ./test.mla -k ../../../../samples/test_ed25519.pem | grep -E '^Hello, World!$$'
$(MLAR) cat test1.txt --input ./test.mla -k ../../../../samples/test_mlakey.pem | grep -E '^Hello, World!$$'
$(MLAR) cat test2.txt --input ./test.mla -k ../../../../samples/test_mlakey.pem | grep -E '^Hello, World!$$'

test_create_no_early_close: $(MLAR) create_no_early_close.elf
./create_no_early_close.elf

test_create_unicode: $(MLAR) create_unicode.elf
./create_unicode.elf
$(MLAR) cat $(shell printf 'test\xE2\x98\xA0.txt') --input ./test.mla -k ../../../../samples/test_ed25519.pem | grep -E '^Hello, World!$$'
$(MLAR) cat $(shell printf 'test\xE2\x98\xA0.txt') --input ./test.mla -k ../../../../samples/test_mlakey.pem | grep -E '^Hello, World!$$'

test_create_stream: $(MLAR) create_stream.elf
yes "All work and no play makes Jack a dull boy" | dd bs=1M count=2 iflag=fullblock | ./create_stream.elf
bash -c "$(MLAR) cat test.txt --input ./test.mla -k ../../../../samples/test_ed25519.pem | sha256sum -c <(echo \"132f5f1a403697b63eec77930cb1ab38414c82ca4c7d415d8dc6e3d3eb6a8b98 -\")"
bash -c "$(MLAR) cat test.txt --input ./test.mla -k ../../../../samples/test_mlakey.pem | sha256sum -c <(echo \"132f5f1a403697b63eec77930cb1ab38414c82ca4c7d415d8dc6e3d3eb6a8b98 -\")"
yes "All work and no play makes Jack a dull boy" | dd bs=1M count=20 iflag=fullblock | ./create_stream.elf
bash -c "$(MLAR) cat test.txt --input ./test.mla -k ../../../../samples/test_ed25519.pem | sha256sum -c <(echo \"e667ca14e52cbea16f6e0413e5fdfb7e789a3f1af80c2a795c36e2523195a20d -\")"
bash -c "$(MLAR) cat test.txt --input ./test.mla -k ../../../../samples/test_mlakey.pem | sha256sum -c <(echo \"e667ca14e52cbea16f6e0413e5fdfb7e789a3f1af80c2a795c36e2523195a20d -\")"

test_create_write_byte_by_byte: $(MLAR) create_write_byte_by_byte.elf
./create_write_byte_by_byte.elf
$(MLAR) cat test.txt --input ./test.mla -k ../../../../samples/test_ed25519.pem | grep -E '^Hello, World!$$'
$(MLAR) cat test.txt --input ./test.mla -k ../../../../samples/test_mlakey.pem | grep -E '^Hello, World!$$'

test_open: $(MLAR) open.elf
./open.elf
Expand All @@ -53,7 +53,10 @@ test_open: $(MLAR) open.elf
test_info: $(MLAR) info.elf
./info.elf

%.elf: %.c $(BUILDDIR)/$(TARGET)/debug/libmla.a
%.expanded.c: %.c
awk -v file="../../../../samples/test_mlakey_pub.pem" 'BEGIN{c = ""; while ((getline line < file) > 0){c = c line "\\n";}}/REPLACE WITH PUBLIC KEY FROM SAMPLE/{gsub("REPLACE WITH PUBLIC KEY FROM SAMPLE",c)}1' $< | awk -v file="../../../../samples/test_mlakey_many_pub.pem" 'BEGIN{c = ""; while ((getline line < file) > 0){c = c line "\\n";}}/REPLACE WITH MANY PUBLIC KEY FROM SAMPLE/{gsub("REPLACE WITH MANY PUBLIC KEY FROM SAMPLE",c)}1' > $@

%.elf: %.expanded.c $(BUILDDIR)/$(TARGET)/debug/libmla.a
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)

$(MLAR): FORCE
Expand All @@ -64,3 +67,5 @@ $(BUILDDIR)/$(TARGET)/debug/libmla.a: FORCE

FORCE:

clean:
rm *.elf; rm *.expanded.c; rm -rf ./extracted; rm ./test.mla
6 changes: 2 additions & 4 deletions bindings/C/tests/linux-gcc-g++/create.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
#define MLA_STATUS(x) (x)
#endif

// from samples/test_ed25519.pem
const char *szPubkey = "-----BEGIN PUBLIC KEY-----\n"
"MCowBQYDK2VwAyEA9md4yIIFx+ftwe0c1p2YsJFrobXWKxan54Bs+/jFagE=\n"
"-----END PUBLIC KEY-----\n";
// from samples/test_mlakey_pub.pem
const char *szPubkey = "REPLACE WITH PUBLIC KEY FROM SAMPLE";

static int32_t callback_write(const uint8_t* pBuffer, uint32_t length, void *context, uint32_t *pBytesWritten)
{
Expand Down
2 changes: 1 addition & 1 deletion bindings/C/tests/linux-gcc-g++/create_fail_no_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int main()

MLAArchiveHandle hArchive = NULL;
status = mla_archive_new(&hConfig, &callback_write, &callback_flush, f, &hArchive);
if (status != MLA_STATUS(MLA_STATUS_CONFIG_ERROR_ENCRYPTION_KEY_IS_MISSING))
if (status != MLA_STATUS(MLA_STATUS_CONFIG_ERROR_NO_RECIPIENTS))
{
fprintf(stderr, " [!] Archive creation did not fail, status %" PRIX64 "\n", (uint64_t)status);
return (int)status;
Expand Down
6 changes: 2 additions & 4 deletions bindings/C/tests/linux-gcc-g++/create_interleaved.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
#define MLA_STATUS(x) (x)
#endif

// From samples/test_ed25519.pem
const char *szPubkey = "-----BEGIN PUBLIC KEY-----\n"
"MCowBQYDK2VwAyEA9md4yIIFx+ftwe0c1p2YsJFrobXWKxan54Bs+/jFagE=\n"
"-----END PUBLIC KEY-----\n";
// from samples/test_mlakey_pub.pem
const char *szPubkey = "REPLACE WITH PUBLIC KEY FROM SAMPLE";

static int32_t callback_write(const uint8_t* pBuffer, uint32_t length, void *context, uint32_t *pBytesWritten)
{
Expand Down
9 changes: 2 additions & 7 deletions bindings/C/tests/linux-gcc-g++/create_manykeys.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@
#include <string.h>
#include "mla.h"

// From samples/test_ed25519.pem and samples/test_x25519.pem
const char *szPubkey = "-----BEGIN PUBLIC KEY-----\n"
"MCowBQYDK2VwAyEA9md4yIIFx+ftwe0c1p2YsJFrobXWKxan54Bs+/jFagE=\n"
"-----END PUBLIC KEY-----\n"
"-----BEGIN PUBLIC KEY-----\n"
"MCowBQYDK2VuAyEA/6CISoJRU5cqURCDRXpPUUF4nHGNo8jzQm+KUKhmc1c=\n"
"-----END PUBLIC KEY-----\n";
// From samples/test_mlakey_many_pub.pem
const char *szPubkey = "REPLACE WITH MANY PUBLIC KEY FROM SAMPLE";

static int32_t callback_write(const uint8_t* pBuffer, uint32_t length, void *context, uint32_t *pBytesWritten)
{
Expand Down
6 changes: 2 additions & 4 deletions bindings/C/tests/linux-gcc-g++/create_no_early_close.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
#define MLA_STATUS(x) (x)
#endif

// From samples/test_ed25519_pub.pem
const char *szPubkey = "-----BEGIN PUBLIC KEY-----\n"
"MCowBQYDK2VwAyEA9md4yIIFx+ftwe0c1p2YsJFrobXWKxan54Bs+/jFagE=\n"
"-----END PUBLIC KEY-----\n";
// from samples/test_mlakey_pub.pem
const char *szPubkey = "REPLACE WITH PUBLIC KEY FROM SAMPLE";

static int32_t callback_write(const uint8_t* pBuffer, uint32_t length, void *context, uint32_t *pBytesWritten)
{
Expand Down
6 changes: 2 additions & 4 deletions bindings/C/tests/linux-gcc-g++/create_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
#define MLA_STATUS(x) (x)
#endif

// From samples/test_ed25519.pem
const char *szPubkey = "-----BEGIN PUBLIC KEY-----\n"
"MCowBQYDK2VwAyEA9md4yIIFx+ftwe0c1p2YsJFrobXWKxan54Bs+/jFagE=\n"
"-----END PUBLIC KEY-----\n";
// from samples/test_mlakey_pub.pem
const char *szPubkey = "REPLACE WITH PUBLIC KEY FROM SAMPLE";

static int32_t callback_write(const uint8_t* pBuffer, uint32_t length, void *context, uint32_t *pBytesWritten)
{
Expand Down
6 changes: 2 additions & 4 deletions bindings/C/tests/linux-gcc-g++/create_unicode.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
#define MLA_STATUS(x) (x)
#endif

// From samples/test_ed25519.pem
const char *szPubkey = "-----BEGIN PUBLIC KEY-----\n"
"MCowBQYDK2VwAyEA9md4yIIFx+ftwe0c1p2YsJFrobXWKxan54Bs+/jFagE=\n"
"-----END PUBLIC KEY-----\n";
// from samples/test_mlakey_pub.pem
const char *szPubkey = "REPLACE WITH PUBLIC KEY FROM SAMPLE";

static int32_t callback_write(const uint8_t* pBuffer, uint32_t length, void *context, uint32_t *pBytesWritten)
{
Expand Down
6 changes: 2 additions & 4 deletions bindings/C/tests/linux-gcc-g++/create_write_byte_by_byte.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
#define MLA_STATUS(x) (x)
#endif

// from samples/test_ed25519.pem
const char *szPubkey = "-----BEGIN PUBLIC KEY-----\n"
"MCowBQYDK2VwAyEA9md4yIIFx+ftwe0c1p2YsJFrobXWKxan54Bs+/jFagE=\n"
"-----END PUBLIC KEY-----\n";
// from samples/test_mlakey_pub.pem
const char *szPubkey = "REPLACE WITH PUBLIC KEY FROM SAMPLE";

static int32_t callback_write(const uint8_t* pBuffer, uint32_t length, void *context, uint32_t *pBytesWritten)
{
Expand Down
2 changes: 1 addition & 1 deletion bindings/C/tests/linux-gcc-g++/open.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static int32_t file_cb(void *context, const uint8_t *filename, uintptr_t filenam

int main()
{
FILE *kf = fopen("../../../../samples/test_ed25519.pem", "r");
FILE *kf = fopen("../../../../samples/test_mlakey.pem", "r");
if (kf == NULL)
{
fprintf(stderr, " [!] Could not open private key file\n");
Expand Down
2 changes: 1 addition & 1 deletion bindings/C/tests/windows-msvc/src/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ int test_reader_extract()
{
FILE *kf;

if (fopen_s(&kf, "../../../../samples/test_ed25519.pem", "r") != 0)
if (fopen_s(&kf, "../../../../samples/test_mlakey.pem", "r") != 0)
{
fprintf(stderr, " [!] Could not open private key file\n");
return errno;
Expand Down
31 changes: 26 additions & 5 deletions bindings/C/tests/windows-msvc/src/write.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
#define MLA_STATUS(x) (x)
#endif

// From samples/test_ed25519_pub.pem
PCSTR szPubkey = "-----BEGIN PUBLIC KEY-----\n"
"MCowBQYDK2VwAyEA9md4yIIFx+ftwe0c1p2YsJFrobXWKxan54Bs+/jFagE=\n"
"-----END PUBLIC KEY-----\n";

static int32_t callback_write(const uint8_t* pBuffer, uint32_t length, void* context, uint32_t* pBytesWritten)
{
HANDLE hOutFile = (HANDLE)context;
Expand Down Expand Up @@ -41,6 +36,28 @@ static int32_t callback_flush(void* context)

int test_writer()
{
FILE *kf;

if (fopen_s(&kf, "../../../../samples/test_mlakey_pub.pem", "r") != 0)
{
fprintf(stderr, " [!] Could not open public key file\n");
return errno;
}
if (fseek(kf, 0, SEEK_END))
{
fprintf(stderr, " [!] Could not open public key file\n");
return errno;
}

long keySize = ftell(kf);
char *szPubkey = (char *)malloc((size_t)keySize);
rewind(kf);
if (keySize != (long)fread(szPubkey, sizeof *szPubkey, keySize, kf))
{
fprintf(stderr, " [!] Could not read public key file\n");
return ferror(kf);
}

HANDLE hOutFile = INVALID_HANDLE_VALUE;

hOutFile = CreateFileA("test.mla", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
Expand Down Expand Up @@ -104,5 +121,9 @@ int test_writer()
}

CloseHandle(hOutFile);

free(szPubkey);
fclose(kf);

return 0;
}
Loading

0 comments on commit 7403923

Please sign in to comment.