tinyblocks-socket, -ssl, -http e -archive: internet e arquivos compactados em blocos
socket: conexao TCP como TbStream (fluxo com classe read/write/close) e blocos para conectar, enviar, receber e resolver endereco. ssl: embrulha um TbStream TCP em TLS (OpenSSL), com SNI e certificado conferido pelo sistema e pelo nome do host. http: cliente HTTP/1.1 so sobre TbStream; https:// quando a ssl esta carregada; Content-Length, chunked e redirecionamentos; GET e POST de texto e download em segundo plano (.parte ate terminar) com progresso. archive: lista e extrai .tar e .tar.gz (zlib), nomes longos GNU/pax; recusa entradas com "../" ou caminho absoluto. As camadas se acham pelas tabelas no lib_userdata, sem linkar umas com as outras. test-http usa um servidor local (e a internet, se houver); test-archive cria os arquivos com tar e Python. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
05fdb094ed
commit
066bb295ed
20 files changed
+1689
-3
No files matched your search
@@ -0,0 +1,30 @@
|
||||
# Makefile da library ssl (categoria "SSL")
|
||||
# Constroi build/libtinyblocks-ssl.a e build/libtinyblocks-ssl.so
|
||||
CC := gcc
|
||||
BUILD := build
|
||||
|
||||
CFLAGS := -std=c11 -O2 -Wall -Wextra -pedantic -fvisibility=hidden \
|
||||
-D_POSIX_C_SOURCE=200809L -I../../include -Iinclude -I../tinyblocks-socket/include
|
||||
|
||||
SRCS := src/tb_ssl.c
|
||||
OBJS := $(BUILD)/tb_ssl.o
|
||||
LIB_A := $(BUILD)/libtinyblocks-ssl.a
|
||||
LIB_SO := $(BUILD)/libtinyblocks-ssl.so
|
||||
|
||||
.PHONY: all static shared clean
|
||||
all: static shared
|
||||
static: $(LIB_A)
|
||||
shared: $(LIB_SO)
|
||||
|
||||
$(BUILD)/%.o: src/%.c $(wildcard include/*.h) $(wildcard src/*.h)
|
||||
@mkdir -p $(BUILD)
|
||||
$(CC) $(CFLAGS) -fPIC -c $< -o $@
|
||||
|
||||
$(LIB_A): $(OBJS)
|
||||
ar rcs $@ $^
|
||||
|
||||
$(LIB_SO): $(OBJS)
|
||||
$(CC) -shared -o $@ $^ -lssl -lcrypto
|
||||
|
||||
clean:
|
||||
$(RM) -r $(BUILD)
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* TinyBlocks - tb_ssl.h
|
||||
* Library tinyblocks-ssl: TLS (dependencia de terceiros: OpenSSL). Registra
|
||||
* a categoria "SSL".
|
||||
*
|
||||
* Embrulha um TbStream TCP (tinyblocks-socket) num TbStream TLS: ler e
|
||||
* escrever passam a ser cifrados, e quem usa o fluxo (ex.: a
|
||||
* tinyblocks-http, para o https://) nao muda nada. O certificado do
|
||||
* servidor e conferido com os certificados do sistema e com o nome do host
|
||||
* (SNI + verificacao do nome).
|
||||
*
|
||||
* Tabela TbSslApi no lib_userdata da library (tb_ssl_api(rt)).
|
||||
*/
|
||||
#ifndef TINYBLOCKS_SSL_H
|
||||
#define TINYBLOCKS_SSL_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include "tb_socket.h"
|
||||
|
||||
#define TB_SSL_LIB "tinyblocks-ssl"
|
||||
#define TB_SSL_API_VERSION 1
|
||||
|
||||
typedef struct TbSslApi {
|
||||
int version; /* TB_SSL_API_VERSION */
|
||||
/* Faz o aperto de mao TLS sobre tcp (fica dono dele: fecha junto).
|
||||
NULL em erro (tcp ja fechado; mensagem em err). */
|
||||
TbStream* (*wrap)(TbStream* tcp, const char* host, char* err, size_t cap);
|
||||
const char* (*version_text)(void); /* "OpenSSL 3.0.13 ..." */
|
||||
} TbSslApi;
|
||||
|
||||
TB_API const TinyBlocks_LibraryDescriptor* tb_ssl_get_library(void);
|
||||
|
||||
static inline const TbSslApi* tb_ssl_api(TinyBlocks* rt) {
|
||||
TinyBlocks_LibraryBlock* lib = tb_rt_find_library(rt, TB_SSL_LIB);
|
||||
const TbSslApi* api = lib ? (const TbSslApi*)lib->lib_userdata : NULL;
|
||||
return api && api->version == TB_SSL_API_VERSION ? api : NULL;
|
||||
}
|
||||
|
||||
#endif /* TINYBLOCKS_SSL_H */
|
||||
@@ -0,0 +1,15 @@
|
||||
# Metadados da library ssl. Lido pela UI/build sem recompilar o .so.
|
||||
[library]
|
||||
name = "tinyblocks-ssl"
|
||||
version = "1.0.0"
|
||||
category = "SSL"
|
||||
description = "TLS (OpenSSL): embrulha uma conexao TCP num fluxo cifrado, com certificado conferido"
|
||||
color = [102, 140, 77, 255]
|
||||
dark = [74, 107, 54, 255]
|
||||
|
||||
[build]
|
||||
sources = ["src/tb_ssl.c"]
|
||||
includes = ["include"]
|
||||
shared = "build/libtinyblocks-ssl.so"
|
||||
static = "build/libtinyblocks-ssl.a"
|
||||
depends = ["socket"]
|
||||
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* TinyBlocks - tb_ssl.c
|
||||
* TLS sobre TbStream com OpenSSL: o fluxo "tls" le e escreve pelo SSL*
|
||||
* ligado ao socket do fluxo TCP que ele embrulha.
|
||||
*/
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/ssl.h>
|
||||
#include <openssl/x509v3.h>
|
||||
|
||||
#include "tb_ssl.h"
|
||||
|
||||
static SSL_CTX* s_ctx;
|
||||
|
||||
typedef struct { SSL* ssl; TbStream* tcp; } Tls;
|
||||
|
||||
static void ssl_err(char* out, size_t cap, const char* what) {
|
||||
unsigned long e = ERR_get_error();
|
||||
char buf[200] = "";
|
||||
if (e) ERR_error_string_n(e, buf, sizeof(buf));
|
||||
snprintf(out, cap, "%s%s%s", what, buf[0] ? ": " : "", buf);
|
||||
}
|
||||
|
||||
static long tls_read(TbStream* s, void* buf, size_t cap) {
|
||||
Tls* t = s->impl;
|
||||
int n = SSL_read(t->ssl, buf, (int)(cap > 1 << 30 ? 1 << 30 : cap));
|
||||
if (n > 0) return n;
|
||||
int e = SSL_get_error(t->ssl, n);
|
||||
if (e == SSL_ERROR_ZERO_RETURN) return 0; /* o servidor fechou direito */
|
||||
if (e == SSL_ERROR_SYSCALL && ERR_peek_error() == 0) return 0; /* fechou sem close_notify */
|
||||
ssl_err(s->err, sizeof(s->err), "falha ao ler (TLS)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
static long tls_write(TbStream* s, const void* buf, size_t n) {
|
||||
Tls* t = s->impl;
|
||||
int w = SSL_write(t->ssl, buf, (int)(n > 1 << 30 ? 1 << 30 : n));
|
||||
if (w > 0) return w;
|
||||
ssl_err(s->err, sizeof(s->err), "falha ao escrever (TLS)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void tls_close(TbStream* s) {
|
||||
Tls* t = s->impl;
|
||||
if (t) {
|
||||
if (t->ssl) { SSL_shutdown(t->ssl); SSL_free(t->ssl); }
|
||||
tb_stream_close(t->tcp);
|
||||
free(t);
|
||||
}
|
||||
free(s);
|
||||
}
|
||||
|
||||
static const TbStreamClass s_tls = { "tls", tls_read, tls_write, tls_close };
|
||||
|
||||
static TbStream* tls_wrap(TbStream* tcp, const char* host, char* err, size_t cap) {
|
||||
if (!s_ctx) { snprintf(err, cap, "TLS não inicializado"); tb_stream_close(tcp); return NULL; }
|
||||
SSL* ssl = SSL_new(s_ctx);
|
||||
if (!ssl) { ssl_err(err, cap, "SSL_new"); tb_stream_close(tcp); return NULL; }
|
||||
SSL_set_fd(ssl, tcp->fd);
|
||||
SSL_set_tlsext_host_name(ssl, host); /* SNI */
|
||||
SSL_set1_host(ssl, host); /* o certificado tem de ser deste host */
|
||||
if (SSL_connect(ssl) != 1) {
|
||||
long v = SSL_get_verify_result(ssl);
|
||||
if (v != X509_V_OK) snprintf(err, cap, "certificado de %s recusado: %s", host, X509_verify_cert_error_string(v));
|
||||
else ssl_err(err, cap, "aperto de mão TLS falhou");
|
||||
SSL_free(ssl);
|
||||
tb_stream_close(tcp);
|
||||
return NULL;
|
||||
}
|
||||
TbStream* s = calloc(1, sizeof(TbStream));
|
||||
Tls* t = calloc(1, sizeof(Tls));
|
||||
if (!s || !t) { free(s); free(t); SSL_free(ssl); tb_stream_close(tcp); snprintf(err, cap, "sem memória"); return NULL; }
|
||||
t->ssl = ssl; t->tcp = tcp;
|
||||
s->cls = &s_tls; s->fd = tcp->fd; s->impl = t;
|
||||
return s;
|
||||
}
|
||||
|
||||
static const char* version_text(void) { return OpenSSL_version(OPENSSL_VERSION); }
|
||||
|
||||
static const TbSslApi s_api = { TB_SSL_API_VERSION, tls_wrap, version_text };
|
||||
|
||||
static void eval_version(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)rt;
|
||||
tb_block_set_output(self, 0, tb_value_text(version_text()));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static bool ssl_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
static const TinyBlocks_Category cat = { "SSL", {0.40f, 0.55f, 0.30f, 1.f}, {0.29f, 0.42f, 0.21f, 1.f} };
|
||||
tb_library_set_category(lib, &cat);
|
||||
OPENSSL_init_ssl(0, NULL);
|
||||
s_ctx = SSL_CTX_new(TLS_client_method());
|
||||
if (!s_ctx) return false;
|
||||
SSL_CTX_set_min_proto_version(s_ctx, TLS1_2_VERSION);
|
||||
SSL_CTX_set_verify(s_ctx, SSL_VERIFY_PEER, NULL);
|
||||
SSL_CTX_set_default_verify_paths(s_ctx); /* certificados do sistema */
|
||||
lib->lib_userdata = (void*)&s_api;
|
||||
static const TinyBlocks_SlotDesc out[] = {{"versão", TB_VALUE_TEXT}};
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, "SSL.Version", NULL, 0, out, 1, eval_version);
|
||||
if (!t) return false;
|
||||
tb_type_block_set_label(t, "versão do SSL", NULL);
|
||||
tb_meta_declare("SSL.Version", "eval_version", __FILE__, "Versão da biblioteca de TLS");
|
||||
return tb_library_add(lib, &t->base);
|
||||
}
|
||||
|
||||
static void ssl_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
(void)rt;
|
||||
lib->lib_userdata = NULL;
|
||||
SSL_CTX_free(s_ctx);
|
||||
s_ctx = NULL;
|
||||
}
|
||||
|
||||
TINYBLOCKS_DEFINE_LIBRARY(ssl, "tinyblocks-ssl", "1.0.0", ssl_init, ssl_deinit)
|
||||
|
||||
const TinyBlocks_LibraryDescriptor* tb_ssl_get_library(void) {
|
||||
return &_tb_lib_desc_ssl;
|
||||
}
|
||||
Reference in new issue
Block a user