More OpenSSL and documenation fun. No more words to say. (0.38.13)
This commit is contained in:
parent
53698478d9
commit
38968a9a51
33 changed files with 498 additions and 88 deletions
|
|
@ -170,6 +170,8 @@ static const char *opcode_name(OpCode op) {
|
|||
case OP_PCRE2_MATCH: return "PCRE2_MATCH";
|
||||
case OP_PCRE2_FINDALL: return "PCRE2_FINDALL";
|
||||
case OP_OPENSSL_MD5: return "OPENSSL_MD5";
|
||||
case OP_OPENSSL_SHA256: return "OPENSSL_SHA256";
|
||||
case OP_OPENSSL_SHA512: return "OPENSSL_SHA512";
|
||||
case OP_INI_LOAD: return "INI_LOAD";
|
||||
case OP_INI_FREE: return "INI_FREE";
|
||||
case OP_INI_GET_STRING: return "INI_GET_STRING";
|
||||
|
|
|
|||
|
|
@ -183,6 +183,8 @@ typedef enum {
|
|||
|
||||
// OpenSSL (optional)
|
||||
OP_OPENSSL_MD5, // pops data string; pushes md5 hex string
|
||||
OP_OPENSSL_SHA256, // pops data string; pushes sha256 hex string
|
||||
OP_OPENSSL_SHA512, // pops data string; pushes sha512 hex string
|
||||
|
||||
// INI (iniparser 4.2.6) optional
|
||||
OP_INI_LOAD, // pops path; pushes handle (>0) or 0
|
||||
|
|
|
|||
130
src/external/openssl.c
vendored
Normal file
130
src/external/openssl.c
vendored
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-02-19
|
||||
*/
|
||||
|
||||
/*
|
||||
* OpenSSL integration helpers (MD5, SHA-256, SHA-512)
|
||||
*/
|
||||
|
||||
#ifdef FUN_WITH_OPENSSL
|
||||
# include <openssl/evp.h>
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
|
||||
/* Compute MD5 hex of input buffer; returns malloc'ed C string (lowercase hex).
|
||||
* Caller must free(). Returns NULL on failure. When OpenSSL is disabled,
|
||||
* returns an allocated empty string ("") to keep behavior consistent with
|
||||
* other optional extensions. */
|
||||
static char *fun_openssl_md5_hex(const unsigned char *data, size_t len) {
|
||||
static const char hexdig[] = "0123456789abcdef";
|
||||
if (!data && len != 0) return NULL;
|
||||
#ifdef FUN_WITH_OPENSSL
|
||||
const EVP_MD *md = EVP_md5();
|
||||
if (!md) return NULL;
|
||||
int dlen = EVP_MD_get_size(md);
|
||||
if (dlen <= 0) return NULL;
|
||||
unsigned char *digest = (unsigned char*)malloc((size_t)dlen);
|
||||
if (!digest) return NULL;
|
||||
unsigned int out_len = 0;
|
||||
int ok;
|
||||
if (len == 0) {
|
||||
// EVP_Digest handles zero-length fine as well
|
||||
ok = EVP_Digest(NULL, 0, digest, &out_len, md, NULL);
|
||||
} else {
|
||||
ok = EVP_Digest(data, len, digest, &out_len, md, NULL);
|
||||
}
|
||||
if (ok != 1 || (int)out_len != dlen) { free(digest); return NULL; }
|
||||
char *hex = (char*)malloc((size_t)dlen * 2 + 1);
|
||||
if (!hex) { free(digest); return NULL; }
|
||||
for (int i = 0; i < dlen; ++i) {
|
||||
hex[2*i] = hexdig[(digest[i] >> 4) & 0xF];
|
||||
hex[2*i+1] = hexdig[digest[i] & 0xF];
|
||||
}
|
||||
hex[dlen * 2] = '\0';
|
||||
free(digest);
|
||||
return hex;
|
||||
#else
|
||||
char *hex = (char*)malloc(1);
|
||||
if (hex) hex[0] = '\0';
|
||||
return hex;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Compute SHA-256 hex of input buffer; returns malloc'ed C string (lowercase hex).
|
||||
* Fallback when OpenSSL disabled: empty string. */
|
||||
static char *fun_openssl_sha256_hex(const unsigned char *data, size_t len) {
|
||||
static const char hexdig[] = "0123456789abcdef";
|
||||
if (!data && len != 0) return NULL;
|
||||
#ifdef FUN_WITH_OPENSSL
|
||||
const EVP_MD *md = EVP_sha256();
|
||||
if (!md) return NULL;
|
||||
int dlen = EVP_MD_get_size(md);
|
||||
if (dlen <= 0) return NULL;
|
||||
unsigned char *digest = (unsigned char*)malloc((size_t)dlen);
|
||||
if (!digest) return NULL;
|
||||
unsigned int out_len = 0;
|
||||
int ok;
|
||||
if (len == 0) {
|
||||
ok = EVP_Digest(NULL, 0, digest, &out_len, md, NULL);
|
||||
} else {
|
||||
ok = EVP_Digest(data, len, digest, &out_len, md, NULL);
|
||||
}
|
||||
if (ok != 1 || (int)out_len != dlen) { free(digest); return NULL; }
|
||||
char *hex = (char*)malloc((size_t)dlen * 2 + 1);
|
||||
if (!hex) { free(digest); return NULL; }
|
||||
for (int i = 0; i < dlen; ++i) {
|
||||
hex[2*i] = hexdig[(digest[i] >> 4) & 0xF];
|
||||
hex[2*i+1] = hexdig[digest[i] & 0xF];
|
||||
}
|
||||
hex[dlen * 2] = '\0';
|
||||
free(digest);
|
||||
return hex;
|
||||
#else
|
||||
char *hex = (char*)malloc(1);
|
||||
if (hex) hex[0] = '\0';
|
||||
return hex;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Compute SHA-512 hex of input buffer; returns malloc'ed C string (lowercase hex).
|
||||
* Fallback when OpenSSL disabled: empty string. */
|
||||
static char *fun_openssl_sha512_hex(const unsigned char *data, size_t len) {
|
||||
static const char hexdig[] = "0123456789abcdef";
|
||||
if (!data && len != 0) return NULL;
|
||||
#ifdef FUN_WITH_OPENSSL
|
||||
const EVP_MD *md = EVP_sha512();
|
||||
if (!md) return NULL;
|
||||
int dlen = EVP_MD_get_size(md);
|
||||
if (dlen <= 0) return NULL;
|
||||
unsigned char *digest = (unsigned char*)malloc((size_t)dlen);
|
||||
if (!digest) return NULL;
|
||||
unsigned int out_len = 0;
|
||||
int ok;
|
||||
if (len == 0) {
|
||||
ok = EVP_Digest(NULL, 0, digest, &out_len, md, NULL);
|
||||
} else {
|
||||
ok = EVP_Digest(data, len, digest, &out_len, md, NULL);
|
||||
}
|
||||
if (ok != 1 || (int)out_len != dlen) { free(digest); return NULL; }
|
||||
char *hex = (char*)malloc((size_t)dlen * 2 + 1);
|
||||
if (!hex) { free(digest); return NULL; }
|
||||
for (int i = 0; i < dlen; ++i) {
|
||||
hex[2*i] = hexdig[(digest[i] >> 4) & 0xF];
|
||||
hex[2*i+1] = hexdig[digest[i] & 0xF];
|
||||
}
|
||||
hex[dlen * 2] = '\0';
|
||||
free(digest);
|
||||
return hex;
|
||||
#else
|
||||
char *hex = (char*)malloc(1);
|
||||
if (hex) hex[0] = '\0';
|
||||
return hex;
|
||||
#endif
|
||||
}
|
||||
47
src/external/opensssl.c
vendored
47
src/external/opensssl.c
vendored
|
|
@ -1,47 +0,0 @@
|
|||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-02-19
|
||||
*/
|
||||
|
||||
/*
|
||||
* OpenSSL integration helpers (currently MD5 only)
|
||||
*/
|
||||
|
||||
#ifdef FUN_WITH_OPENSSL
|
||||
# include <openssl/md5.h>
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
|
||||
/* Compute MD5 hex of input buffer; returns malloc'ed C string (lowercase hex).
|
||||
* Caller must free(). Returns NULL on failure. When OpenSSL is disabled,
|
||||
* returns an allocated empty string ("") to keep behavior consistent with
|
||||
* other optional extensions. */
|
||||
static char *fun_openssl_md5_hex(const unsigned char *data, size_t len) {
|
||||
static const char hexdig[] = "0123456789abcdef";
|
||||
if (!data && len != 0) return NULL;
|
||||
#ifdef FUN_WITH_OPENSSL
|
||||
unsigned char digest[MD5_DIGEST_LENGTH];
|
||||
MD5_CTX ctx;
|
||||
if (MD5_Init(&ctx) != 1) return NULL;
|
||||
if (len && MD5_Update(&ctx, data, len) != 1) return NULL;
|
||||
if (MD5_Final(digest, &ctx) != 1) return NULL;
|
||||
char *hex = (char*)malloc(MD5_DIGEST_LENGTH * 2 + 1);
|
||||
if (!hex) return NULL;
|
||||
for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
|
||||
hex[2*i] = hexdig[(digest[i] >> 4) & 0xF];
|
||||
hex[2*i+1] = hexdig[digest[i] & 0xF];
|
||||
}
|
||||
hex[MD5_DIGEST_LENGTH * 2] = '\0';
|
||||
return hex;
|
||||
#else
|
||||
char *hex = (char*)malloc(1);
|
||||
if (hex) hex[0] = '\0';
|
||||
return hex;
|
||||
#endif
|
||||
}
|
||||
18
src/parser.c
18
src/parser.c
|
|
@ -1197,7 +1197,7 @@ static int emit_primary(Bytecode *bc, const char *src, size_t len, size_t *pos)
|
|||
free(name);
|
||||
return 1;
|
||||
}
|
||||
/* OpenSSL (md5) */
|
||||
/* OpenSSL (md5/sha256/sha512) */
|
||||
if (strcmp(name, "openssl_md5") == 0) {
|
||||
(*pos)++; /* '(' */
|
||||
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "openssl_md5 expects (data)"); free(name); return 0; }
|
||||
|
|
@ -1206,6 +1206,22 @@ static int emit_primary(Bytecode *bc, const char *src, size_t len, size_t *pos)
|
|||
free(name);
|
||||
return 1;
|
||||
}
|
||||
if (strcmp(name, "openssl_sha256") == 0) {
|
||||
(*pos)++; /* '(' */
|
||||
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "openssl_sha256 expects (data)"); free(name); return 0; }
|
||||
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after openssl_sha256 arg"); free(name); return 0; }
|
||||
bytecode_add_instruction(bc, OP_OPENSSL_SHA256, 0);
|
||||
free(name);
|
||||
return 1;
|
||||
}
|
||||
if (strcmp(name, "openssl_sha512") == 0) {
|
||||
(*pos)++; /* '(' */
|
||||
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "openssl_sha512 expects (data)"); free(name); return 0; }
|
||||
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after openssl_sha512 arg"); free(name); return 0; }
|
||||
bytecode_add_instruction(bc, OP_OPENSSL_SHA512, 0);
|
||||
free(name);
|
||||
return 1;
|
||||
}
|
||||
/* PCSC builtins */
|
||||
if (strcmp(name, "pcsc_establish") == 0) {
|
||||
(*pos)++; /* '(' */
|
||||
|
|
|
|||
6
src/vm.c
6
src/vm.c
|
|
@ -73,7 +73,7 @@
|
|||
#include "external/sqlite.c"
|
||||
#include "external/tcltk.c"
|
||||
#include "external/xml2.c"
|
||||
#include "external/opensssl.c"
|
||||
#include "external/openssl.c"
|
||||
|
||||
/* forward declarations for include mapping used in error reporting */
|
||||
extern char *preprocess_includes(const char *src);
|
||||
|
|
@ -869,8 +869,10 @@ void vm_run(VM *vm, Bytecode *entry) {
|
|||
#include "vm/curl/download.c"
|
||||
#endif
|
||||
|
||||
/* OpenSSL ops (md5) */
|
||||
/* OpenSSL ops (md5/sha256/sha512) */
|
||||
#include "vm/openssl/md5.c"
|
||||
#include "vm/openssl/sha256.c"
|
||||
#include "vm/openssl/sha512.c"
|
||||
|
||||
/* Tk (Tcl/Tk) ops */
|
||||
#ifdef FUN_WITH_TCLTK
|
||||
|
|
|
|||
16
src/vm/openssl/sha256.c
Normal file
16
src/vm/openssl/sha256.c
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
/**
|
||||
* OpenSSL SHA-256 builtin
|
||||
*/
|
||||
case OP_OPENSSL_SHA256: {
|
||||
Value vdata = pop_value(vm);
|
||||
char *s = value_to_string_alloc(&vdata);
|
||||
free_value(vdata);
|
||||
if (!s) { push_value(vm, make_string("")); break; }
|
||||
char *hex = fun_openssl_sha256_hex((const unsigned char*)s, strlen(s));
|
||||
free(s);
|
||||
if (!hex) { push_value(vm, make_string("")); break; }
|
||||
Value out = make_string(hex);
|
||||
free(hex);
|
||||
push_value(vm, out);
|
||||
break;
|
||||
}
|
||||
16
src/vm/openssl/sha512.c
Normal file
16
src/vm/openssl/sha512.c
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
/**
|
||||
* OpenSSL SHA-512 builtin
|
||||
*/
|
||||
case OP_OPENSSL_SHA512: {
|
||||
Value vdata = pop_value(vm);
|
||||
char *s = value_to_string_alloc(&vdata);
|
||||
free_value(vdata);
|
||||
if (!s) { push_value(vm, make_string("")); break; }
|
||||
char *hex = fun_openssl_sha512_hex((const unsigned char*)s, strlen(s));
|
||||
free(s);
|
||||
if (!hex) { push_value(vm, make_string("")); break; }
|
||||
Value out = make_string(hex);
|
||||
free(hex);
|
||||
push_value(vm, out);
|
||||
break;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue