mirror of
https://github.com/vacp2p/nim-ngtcp2.git
synced 2026-01-08 22:38:01 -05:00
feat: wrap picotls backend (#10)
This commit is contained in:
133
.github/actions/install_nim/action.yml
vendored
Normal file
133
.github/actions/install_nim/action.yml
vendored
Normal file
@@ -0,0 +1,133 @@
|
||||
name: Install Nim
|
||||
inputs:
|
||||
os:
|
||||
description: "Operating system to build for"
|
||||
required: true
|
||||
cpu:
|
||||
description: "CPU to build for"
|
||||
default: "amd64"
|
||||
nim_ref:
|
||||
description: "Nim version"
|
||||
default: "version-1-6"
|
||||
shell:
|
||||
description: "Shell to run commands in"
|
||||
default: "bash --noprofile --norc -e -o pipefail"
|
||||
|
||||
runs:
|
||||
using: "composite"
|
||||
steps:
|
||||
- name: Install build dependencies (Linux i386)
|
||||
shell: ${{ inputs.shell }}
|
||||
if: inputs.os == 'Linux' && inputs.cpu == 'i386'
|
||||
run: |
|
||||
sudo dpkg --add-architecture i386
|
||||
sudo apt-get update -qq
|
||||
sudo DEBIAN_FRONTEND='noninteractive' apt-get install \
|
||||
--no-install-recommends -yq gcc-multilib g++-multilib \
|
||||
libssl-dev:i386
|
||||
mkdir -p external/bin
|
||||
cat << EOF > external/bin/gcc
|
||||
#!/bin/bash
|
||||
exec $(which gcc) -m32 "\$@"
|
||||
EOF
|
||||
cat << EOF > external/bin/g++
|
||||
#!/bin/bash
|
||||
exec $(which g++) -m32 "\$@"
|
||||
EOF
|
||||
chmod 755 external/bin/gcc external/bin/g++
|
||||
echo '${{ github.workspace }}/external/bin' >> $GITHUB_PATH
|
||||
|
||||
- name: MSYS2 (Windows i386)
|
||||
if: inputs.os == 'Windows' && inputs.cpu == 'i386'
|
||||
uses: msys2/setup-msys2@v2
|
||||
with:
|
||||
path-type: inherit
|
||||
msystem: MINGW32
|
||||
install: >-
|
||||
base-devel
|
||||
git
|
||||
mingw-w64-i686-toolchain
|
||||
|
||||
- name: MSYS2 (Windows amd64)
|
||||
if: inputs.os == 'Windows' && inputs.cpu == 'amd64'
|
||||
uses: msys2/setup-msys2@v2
|
||||
with:
|
||||
path-type: inherit
|
||||
install: >-
|
||||
base-devel
|
||||
git
|
||||
mingw-w64-x86_64-toolchain
|
||||
|
||||
- name: Restore Nim DLLs dependencies (Windows) from cache
|
||||
if: inputs.os == 'Windows'
|
||||
id: windows-dlls-cache
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: external/dlls
|
||||
key: 'dlls'
|
||||
|
||||
- name: Install DLL dependencies (Windows)
|
||||
shell: ${{ inputs.shell }}
|
||||
if: >
|
||||
steps.windows-dlls-cache.outputs.cache-hit != 'true' &&
|
||||
inputs.os == 'Windows'
|
||||
run: |
|
||||
mkdir external
|
||||
curl -L "https://nim-lang.org/download/windeps.zip" -o external/windeps.zip
|
||||
7z x external/windeps.zip -oexternal/dlls
|
||||
|
||||
- name: Path to cached dependencies (Windows)
|
||||
shell: ${{ inputs.shell }}
|
||||
if: >
|
||||
inputs.os == 'Windows'
|
||||
run: |
|
||||
echo '${{ github.workspace }}'"/external/dlls" >> $GITHUB_PATH
|
||||
|
||||
- name: Derive environment variables
|
||||
shell: ${{ inputs.shell }}
|
||||
run: |
|
||||
if [[ '${{ inputs.cpu }}' == 'amd64' ]]; then
|
||||
PLATFORM=x64
|
||||
elif [[ '${{ inputs.cpu }}' == 'arm64' ]]; then
|
||||
PLATFORM=arm64
|
||||
else
|
||||
PLATFORM=x86
|
||||
fi
|
||||
echo "PLATFORM=$PLATFORM" >> $GITHUB_ENV
|
||||
|
||||
ncpu=
|
||||
MAKE_CMD="make"
|
||||
case '${{ inputs.os }}' in
|
||||
'Linux')
|
||||
ncpu=$(nproc)
|
||||
;;
|
||||
'macOS')
|
||||
ncpu=$(sysctl -n hw.ncpu)
|
||||
;;
|
||||
'Windows')
|
||||
ncpu=$NUMBER_OF_PROCESSORS
|
||||
MAKE_CMD="mingw32-make"
|
||||
;;
|
||||
esac
|
||||
[[ -z "$ncpu" || $ncpu -le 0 ]] && ncpu=1
|
||||
echo "ncpu=$ncpu" >> $GITHUB_ENV
|
||||
echo "MAKE_CMD=${MAKE_CMD}" >> $GITHUB_ENV
|
||||
echo '${{ github.workspace }}/nim/bin' >> $GITHUB_PATH
|
||||
|
||||
- name: Restore Nim from cache
|
||||
id: nim-cache
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: '${{ github.workspace }}/nim'
|
||||
key: ${{ inputs.os }}-${{ inputs.cpu }}-nim-${{ inputs.nim_ref }}-cache-${{ env.cache_nonce }}
|
||||
|
||||
- name: Build Nim and Nimble
|
||||
shell: ${{ inputs.shell }}
|
||||
if: ${{ steps.nim-cache.outputs.cache-hit != 'true' }}
|
||||
run: |
|
||||
# We don't want partial matches of the cache restored
|
||||
rm -rf nim
|
||||
curl -O -L -s -S https://raw.githubusercontent.com/status-im/nimbus-build-system/master/scripts/build_nim.sh
|
||||
env MAKE="${MAKE_CMD} -j${ncpu}" ARCH_OVERRIDE=${PLATFORM} NIM_COMMIT=${{ inputs.nim_ref }} \
|
||||
QUICK_AND_DIRTY_COMPILER=1 QUICK_AND_DIRTY_NIMBLE=1 CC=gcc \
|
||||
bash build_nim.sh nim csources dist/nimble NimBinaries
|
||||
114
.github/workflows/test.yml
vendored
114
.github/workflows/test.yml
vendored
@@ -1,14 +1,116 @@
|
||||
name: CI
|
||||
name: Continuous Integration
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
pull_request:
|
||||
merge_group:
|
||||
workflow_dispatch:
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
build:
|
||||
uses: status-im/nimbus-common-workflow/.github/workflows/common.yml@main
|
||||
with:
|
||||
test-command: |
|
||||
nimble test --styleCheck:off
|
||||
test:
|
||||
timeout-minutes: 90
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
platform:
|
||||
- os: linux
|
||||
cpu: amd64
|
||||
- os: linux
|
||||
cpu: i386
|
||||
- os: linux-gcc-14
|
||||
cpu: amd64
|
||||
- os: macos
|
||||
cpu: amd64
|
||||
- os: macos-14
|
||||
cpu: arm64
|
||||
- os: windows
|
||||
cpu: amd64
|
||||
nim:
|
||||
- ref: version-1-6
|
||||
memory_management: refc
|
||||
- ref: version-2-0
|
||||
memory_management: refc
|
||||
include:
|
||||
- platform:
|
||||
os: linux
|
||||
builder: ubuntu-22.04
|
||||
shell: bash
|
||||
- platform:
|
||||
os: linux-gcc-14
|
||||
builder: ubuntu-24.04
|
||||
shell: bash
|
||||
- platform:
|
||||
os: macos
|
||||
builder: macos-13
|
||||
shell: bash
|
||||
- platform:
|
||||
os: macos-14
|
||||
builder: macos-14
|
||||
shell: bash
|
||||
- platform:
|
||||
os: windows
|
||||
builder: windows-2022
|
||||
shell: msys2 {0}
|
||||
|
||||
defaults:
|
||||
run:
|
||||
shell: ${{ matrix.shell }}
|
||||
|
||||
name: '${{ matrix.platform.os }}-${{ matrix.platform.cpu }} (Nim ${{ matrix.nim.ref }})'
|
||||
runs-on: ${{ matrix.builder }}
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: true
|
||||
|
||||
- name: Setup Nim
|
||||
uses: "./.github/actions/install_nim"
|
||||
with:
|
||||
os: ${{ matrix.platform.os }}
|
||||
cpu: ${{ matrix.platform.cpu }}
|
||||
shell: ${{ matrix.shell }}
|
||||
nim_ref: ${{ matrix.nim.ref }}
|
||||
|
||||
- name: Restore deps from cache
|
||||
id: deps-cache
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: nimbledeps
|
||||
# Using nim.ref as a simple way to differentiate between nimble using the "pkgs" or "pkgs2" directories.
|
||||
# The change happened on Nimble v0.14.0. Also forcing the deps to be reinstalled on each os and cpu.
|
||||
key: nimbledeps-${{ matrix.nim.ref }}-${{ matrix.builder }}-${{ matrix.platform.cpu }}-${{ hashFiles('.pinned') }} # hashFiles returns a different value on windows
|
||||
|
||||
- name: Install deps
|
||||
if: ${{ steps.deps-cache.outputs.cache-hit != 'true' }}
|
||||
run: |
|
||||
nimble install
|
||||
|
||||
- name: Use gcc 14
|
||||
if : ${{ matrix.platform.os == 'linux-gcc-14'}}
|
||||
run: |
|
||||
# Add GCC-14 to alternatives
|
||||
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-14 14
|
||||
|
||||
# Set GCC-14 as the default
|
||||
sudo update-alternatives --set gcc /usr/bin/gcc-14
|
||||
|
||||
- name: Install deps (windows)
|
||||
if : ${{ matrix.platform.os == 'windows'}}
|
||||
run: |
|
||||
pacman -S --noconfirm base-devel gcc
|
||||
|
||||
- name: Run tests
|
||||
run: |
|
||||
nim --version
|
||||
nimble --version
|
||||
gcc --version
|
||||
|
||||
NIMFLAGS="${NIMFLAGS} --mm:${{ matrix.nim.memory_management }}"
|
||||
nimble test --styleCheck:off --verbose --debug
|
||||
|
||||
18
.gitignore
vendored
18
.gitignore
vendored
@@ -1,16 +1,2 @@
|
||||
*
|
||||
!*/
|
||||
!*.*
|
||||
|
||||
# ignore all cmake build files, except version.h
|
||||
build/*
|
||||
!build/lib/
|
||||
build/lib/*
|
||||
!build/lib/includes/
|
||||
build/lib/includes/*
|
||||
!build/lib/includes/ngtcp2/
|
||||
build/lib/includes/ngtcp2/*
|
||||
!build/lib/includes/ngtcp2/version.h
|
||||
|
||||
!LICENSE-MIT
|
||||
!LICENSE-APACHEv2
|
||||
!libs/
|
||||
!libs/*
|
||||
9
.gitmodules
vendored
9
.gitmodules
vendored
@@ -1,3 +1,6 @@
|
||||
[submodule "sources"]
|
||||
path = sources
|
||||
url = https://github.com/ngtcp2/ngtcp2.git
|
||||
[submodule "libs/ngtcp2"]
|
||||
path = libs/ngtcp2
|
||||
url = https://github.com/ngtcp2/ngtcp2
|
||||
[submodule "libs/picotls"]
|
||||
path = libs/picotls
|
||||
url = https://github.com/h2o/picotls
|
||||
|
||||
58
build.sh
58
build.sh
@@ -1,35 +1,39 @@
|
||||
#!/bin/bash
|
||||
root=$(dirname "$0")
|
||||
sources=${root}/libs
|
||||
|
||||
# install nimterop, if not already installed
|
||||
if ! [ -x "$(command -v toast)" ]; then
|
||||
nimble install -y nimterop@2532ce0
|
||||
fi
|
||||
|
||||
# run cmake on ngtcp2 sources
|
||||
cmake -S "${root}/sources" -B "${root}/build"
|
||||
|
||||
# add prelude
|
||||
cat "${root}/prelude.nim" > "${root}/ngtcp2.nim"
|
||||
|
||||
# dividing line
|
||||
echo >> "${root}/ngtcp2.nim"
|
||||
rm -f ngtcp2.nim
|
||||
|
||||
# assemble list of C files to be compiled
|
||||
for file in `ls "${root}/sources/lib"/*.c`; do
|
||||
compile="${compile} --compile=${file}"
|
||||
toCompile=(
|
||||
"${sources}/picotls/picotlsvs/picotls/wintimeofday.c"
|
||||
"${sources}/picotls/lib/pembase64.c"
|
||||
"${sources}/picotls/lib/hpke.c"
|
||||
"${sources}/picotls/lib/picotls.c"
|
||||
"${sources}/picotls/lib/openssl.c"
|
||||
)
|
||||
|
||||
for file in `ls "${sources}/ngtcp2/crypto"/*.c`; do
|
||||
toCompile+=("$file")
|
||||
done
|
||||
for file in `ls "${sources}/ngtcp2/crypto/picotls"/*.c`; do
|
||||
toCompile+=("$file")
|
||||
done
|
||||
for file in `ls "${sources}/ngtcp2/lib"/*.c`; do
|
||||
toCompile+=("$file")
|
||||
done
|
||||
for file in `ls "${root}/build/lib"/*.c`; do
|
||||
toCompile+=("$file")
|
||||
done
|
||||
|
||||
# generate nim wrapper with nimterop
|
||||
toast \
|
||||
$compile \
|
||||
--pnim \
|
||||
--preprocess \
|
||||
--noHeader \
|
||||
--defines=NGTCP2_STATICLIB \
|
||||
--replace=sockaddr=SockAddr,SockAddr_storage=Sockaddr_storage,socklen_t=SockLen \
|
||||
--includeDirs="${root}/sources/lib/includes" \
|
||||
--includeDirs="${root}/build/lib/includes" \
|
||||
"${root}/sources/lib/includes/ngtcp2/ngtcp2.h" >> "${root}/ngtcp2.nim"
|
||||
nim c --maxLoopIterationsVM:100000000 generate_ngtcp2.nim
|
||||
|
||||
sed -i 's/\bpassC\b/passc/g' ngtcp2.nim
|
||||
# add prelude
|
||||
cat "${root}/prelude.nim" > ngtcp2.nim
|
||||
|
||||
for file in "${toCompile[@]}"; do
|
||||
echo "{.compile: \"$file\".}" >> ngtcp2.nim
|
||||
done
|
||||
|
||||
cat tmp_ngtcp2.nim >> ngtcp2.nim
|
||||
rm -f tmp_ngtcp2.nim
|
||||
|
||||
140
build/lib/cred_buffer.c
Normal file
140
build/lib/cred_buffer.c
Normal file
@@ -0,0 +1,140 @@
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "utils/cred_buffer.h"
|
||||
|
||||
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||
|
||||
static int cred_buffer_getc(ptls_cred_buffer_t *buf)
|
||||
{
|
||||
return PTLS_CRED_BUFFER_LEFT(buf) > 0 ? buf->base[buf->off++] : -1;
|
||||
}
|
||||
|
||||
static ssize_t fsize(FILE *fp)
|
||||
{
|
||||
long sz;
|
||||
|
||||
if (fseek(fp, 0, SEEK_END) == -1 || (sz = ftell(fp)) == -1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
rewind(fp);
|
||||
|
||||
return (ssize_t) sz;
|
||||
}
|
||||
|
||||
/* The caller owns 'mem' and must have called ptls_buffer_init prior to
|
||||
* invoking this function */
|
||||
int ptls_cred_buffer_set_from_file(ptls_cred_buffer_t *buf, const char *fname)
|
||||
{
|
||||
FILE *fp = NULL;
|
||||
ssize_t sz;
|
||||
char *m = NULL;
|
||||
|
||||
#ifdef _WINDOWS
|
||||
errno_t err = fopen_s(&fp, fname, "r");
|
||||
if (err != 0) {
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
fp = fopen(fname, "r");
|
||||
if (fp == NULL) {
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
if ((sz = fsize(fp)) == -1 ||
|
||||
(m = malloc(sz)) == NULL ||
|
||||
fread(m, sz, 1, fp) != 1) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
(void) fclose(fp);
|
||||
|
||||
buf->base = m;
|
||||
buf->len = sz;
|
||||
buf->off = 0;
|
||||
buf->owns_base = 1;
|
||||
|
||||
return 0;
|
||||
err:
|
||||
if (m)
|
||||
free(m);
|
||||
if (fp != NULL)
|
||||
(void) fclose(fp);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ptls_cred_buffer_set_from_string(ptls_cred_buffer_t *buf, char *s)
|
||||
{
|
||||
buf->base = s;
|
||||
buf->len = strlen(s);
|
||||
buf->off = 0;
|
||||
buf->owns_base = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ptls_cred_buffer_dispose(ptls_cred_buffer_t *buf)
|
||||
{
|
||||
if (buf->owns_base) {
|
||||
if (buf->base) {
|
||||
free(buf->base);
|
||||
buf->base = NULL;
|
||||
}
|
||||
buf->len = buf->off = 0;
|
||||
buf->owns_base = 0;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void ptls_cred_buffer_rewind(ptls_cred_buffer_t *buf)
|
||||
{
|
||||
buf->off = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
/* z -> nlptr */
|
||||
char *ptls_cred_buffer_gets(char *s, int n, ptls_cred_buffer_t *buf)
|
||||
{
|
||||
char *p = s;
|
||||
char *z;
|
||||
size_t k;
|
||||
int c;
|
||||
|
||||
if (n-- <= 1) {
|
||||
if (n) return NULL;
|
||||
*s = '\0';
|
||||
return s;
|
||||
}
|
||||
|
||||
while (n) {
|
||||
if (PTLS_CRED_BUFFER_RPOS(buf) != PTLS_CRED_BUFFER_REND(buf)) {
|
||||
z = memchr(PTLS_CRED_BUFFER_RPOS(buf), '\n', PTLS_CRED_BUFFER_LEFT(buf));
|
||||
k = z ? z - PTLS_CRED_BUFFER_RPOS(buf) + 1 : PTLS_CRED_BUFFER_LEFT(buf);
|
||||
k = MIN(k, n);
|
||||
memcpy(p, PTLS_CRED_BUFFER_RPOS(buf), k);
|
||||
buf->off += k;
|
||||
p += k;
|
||||
n -= k;
|
||||
if (z || !n) break;
|
||||
}
|
||||
|
||||
if ((c = cred_buffer_getc(buf)) < 0) {
|
||||
if (p == s || PTLS_CRED_BUFFER_LEFT(buf) > 0) s = NULL;
|
||||
break;
|
||||
}
|
||||
|
||||
n--;
|
||||
|
||||
if ((*p++ = c) == '\n') break;
|
||||
}
|
||||
|
||||
if (s) *p = '\0';
|
||||
|
||||
return s;
|
||||
}
|
||||
24
build/lib/includes/utils/cred_buffer.h
Normal file
24
build/lib/includes/utils/cred_buffer.h
Normal file
@@ -0,0 +1,24 @@
|
||||
|
||||
#ifndef PTLS_CRED_BUFFER_H
|
||||
#define PTLS_CRED_BUFFER_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include "picotls.h"
|
||||
|
||||
typedef struct ptls_cred_buffer_s {
|
||||
char *base;
|
||||
size_t len;
|
||||
size_t off;
|
||||
int owns_base;
|
||||
#define PTLS_CRED_BUFFER_RPOS(buf) ((buf)->base + (buf)->off)
|
||||
#define PTLS_CRED_BUFFER_REND(buf) ((buf)->base + (buf)->len)
|
||||
#define PTLS_CRED_BUFFER_LEFT(buf) ((buf)->len - (buf)->off)
|
||||
} ptls_cred_buffer_t;
|
||||
|
||||
int ptls_cred_buffer_set_from_file(ptls_cred_buffer_t *buf, const char *fname);
|
||||
int ptls_cred_buffer_set_from_string(ptls_cred_buffer_t *buf, char *s);
|
||||
void ptls_cred_buffer_dispose(ptls_cred_buffer_t *buf);
|
||||
void ptls_cred_buffer_rewind(ptls_cred_buffer_t *buf);
|
||||
char *ptls_cred_buffer_gets(char *s, int n, ptls_cred_buffer_t *buf);
|
||||
|
||||
#endif /* !PTLS_CRED_BUFFER_H */
|
||||
28
build/lib/includes/utils/pem_utils.h
Normal file
28
build/lib/includes/utils/pem_utils.h
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright (c) 2017 Christian Huitema <huitema@huitema.net>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef PTLS_PEM_UTILS
|
||||
#define PTLS_PEM_UTILS
|
||||
|
||||
#include <picotls.h>
|
||||
#include <picotls/openssl.h>
|
||||
#include "cred_buffer.h"
|
||||
|
||||
int ptls_load_certificates_from_memory(ptls_context_t *ctx, ptls_cred_buffer_t *mem);
|
||||
|
||||
int ptls_openssl_init_sign_certificate_with_mem_key(ptls_openssl_sign_certificate_t *self, const void *buf, int len);
|
||||
|
||||
#endif /* PTLS_PEM_UTILS */
|
||||
37
build/lib/includes/wincompat.h
Normal file
37
build/lib/includes/wincompat.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef WINCOMPAT_H
|
||||
#define WINCOMPAT_H
|
||||
|
||||
#include <stdint.h>
|
||||
#define ssize_t int
|
||||
#include <Winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#include <malloc.h>
|
||||
|
||||
#ifndef gettimeofday
|
||||
#define gettimeofday wintimeofday
|
||||
|
||||
#ifndef __attribute__
|
||||
#define __attribute__(X)
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
struct tzone {
|
||||
int tz_minuteswest; /* minutes west of Greenwich */
|
||||
int tz_dsttime; /* type of DST correction */
|
||||
};
|
||||
|
||||
int wintimeofday(struct timeval *tv, struct tzone *tz);
|
||||
|
||||
#ifndef strcasecmp
|
||||
#define strcasecmp _stricmp
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* WINCOMPAT_H */
|
||||
152
build/lib/pem_utils.c
Normal file
152
build/lib/pem_utils.c
Normal file
@@ -0,0 +1,152 @@
|
||||
#include <stdlib.h>
|
||||
#include <picotls.h>
|
||||
#include <picotls/pembase64.h>
|
||||
#include "utils/pem_utils.h"
|
||||
#include <openssl/pem.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/ec.h>
|
||||
#include <openssl/err.h>
|
||||
|
||||
static int ptls_compare_separator_line(const char *line, const char *begin_or_end, const char *label)
|
||||
{
|
||||
int ret = strncmp(line, "-----", 5);
|
||||
size_t text_index = 5;
|
||||
|
||||
if (ret == 0) {
|
||||
size_t begin_or_end_length = strlen(begin_or_end);
|
||||
ret = strncmp(line + text_index, begin_or_end, begin_or_end_length);
|
||||
text_index += begin_or_end_length;
|
||||
}
|
||||
|
||||
if (ret == 0) {
|
||||
ret = line[text_index] - ' ';
|
||||
text_index++;
|
||||
}
|
||||
|
||||
if (ret == 0) {
|
||||
size_t label_length = strlen(label);
|
||||
ret = strncmp(line + text_index, label, label_length);
|
||||
text_index += label_length;
|
||||
}
|
||||
|
||||
if (ret == 0) {
|
||||
ret = strncmp(line + text_index, "-----", 5);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Extracted from https://github.com/h2o/picotls/pull/284/
|
||||
// Remove both pem.c/h and cred_buffer.c/h once that PR gets merged
|
||||
static int ptls_get_pem_object_from_memory(ptls_cred_buffer_t *mem, const char *label, ptls_buffer_t *buf)
|
||||
{
|
||||
int ret = PTLS_ERROR_PEM_LABEL_NOT_FOUND;
|
||||
char line[256];
|
||||
ptls_base64_decode_state_t state;
|
||||
|
||||
/* Get the label on a line by itself */
|
||||
while (ptls_cred_buffer_gets(line, 256, mem)) {
|
||||
if (ptls_compare_separator_line(line, "BEGIN", label) == 0) {
|
||||
ret = 0;
|
||||
ptls_base64_decode_init(&state);
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* Get the data in the buffer */
|
||||
while (ret == 0 && ptls_cred_buffer_gets(line, 256, mem)) {
|
||||
if (ptls_compare_separator_line(line, "END", label) == 0) {
|
||||
if (state.status == PTLS_BASE64_DECODE_DONE || (state.status == PTLS_BASE64_DECODE_IN_PROGRESS && state.nbc == 0)) {
|
||||
ret = 0;
|
||||
} else {
|
||||
ret = PTLS_ERROR_INCORRECT_BASE64;
|
||||
}
|
||||
break;
|
||||
} else {
|
||||
ret = ptls_base64_decode(line, &state, buf);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int ptls_load_pem_objects_from_memory(ptls_cred_buffer_t *mem, const char *label, ptls_iovec_t *list, size_t list_max, size_t *nb_objects)
|
||||
{
|
||||
int ret = 0;
|
||||
size_t count = 0;
|
||||
|
||||
*nb_objects = 0;
|
||||
|
||||
if (ret == 0) {
|
||||
while (count < list_max) {
|
||||
ptls_buffer_t buf;
|
||||
|
||||
ptls_buffer_init(&buf, "", 0);
|
||||
|
||||
ret = ptls_get_pem_object_from_memory(mem, label, &buf);
|
||||
|
||||
if (ret == 0) {
|
||||
if (buf.off > 0 && buf.is_allocated) {
|
||||
list[count].base = buf.base;
|
||||
list[count].len = buf.off;
|
||||
count++;
|
||||
} else {
|
||||
ptls_buffer_dispose(&buf);
|
||||
}
|
||||
} else {
|
||||
ptls_buffer_dispose(&buf);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (ret == PTLS_ERROR_PEM_LABEL_NOT_FOUND && count > 0) {
|
||||
ret = 0;
|
||||
}
|
||||
|
||||
*nb_objects = count;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#define PTLS_MAX_CERTS_IN_CONTEXT 16
|
||||
|
||||
int ptls_load_certificates_from_memory(ptls_context_t *ctx, ptls_cred_buffer_t *mem)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
ctx->certificates.list = (ptls_iovec_t *)malloc(PTLS_MAX_CERTS_IN_CONTEXT * sizeof(ptls_iovec_t));
|
||||
|
||||
if (ctx->certificates.list == NULL) {
|
||||
ret = PTLS_ERROR_NO_MEMORY;
|
||||
} else {
|
||||
ret = ptls_load_pem_objects_from_memory(mem, "CERTIFICATE", ctx->certificates.list, PTLS_MAX_CERTS_IN_CONTEXT,
|
||||
&ctx->certificates.count);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int ptls_openssl_init_sign_certificate_with_mem_key(ptls_openssl_sign_certificate_t *self, const void *buf, int len) {
|
||||
BIO *bio = BIO_new_mem_buf(buf, len);
|
||||
if (bio == NULL) {
|
||||
return 8880;
|
||||
}
|
||||
|
||||
EVP_PKEY *evp_key = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
|
||||
BIO_free(bio);
|
||||
if (evp_key == NULL) {
|
||||
return 8881;
|
||||
}
|
||||
|
||||
// Initialize the certificate signing structure
|
||||
int ret = ptls_openssl_init_sign_certificate(self, evp_key);
|
||||
|
||||
EVP_PKEY_free(evp_key);
|
||||
|
||||
return ret;
|
||||
}
|
||||
16
generate_ngtcp2.nim
Normal file
16
generate_ngtcp2.nim
Normal file
@@ -0,0 +1,16 @@
|
||||
import futhark, strformat
|
||||
from os import parentDir, `/`
|
||||
|
||||
importc:
|
||||
outputPath currentSourcePath.parentDir / "tmp_ngtcp2.nim"
|
||||
path currentSourcePath.parentDir/"libs/ngtcp2/lib/includes"
|
||||
path currentSourcePath.parentDir/"build/lib/includes"
|
||||
path currentSourcePath.parentDir/"libs/ngtcp2/crypto/includes/"
|
||||
path currentSourcePath.parentDir/"libs"/"picotls"/"include"
|
||||
"ngtcp2/ngtcp2.h"
|
||||
"ngtcp2/ngtcp2_crypto.h"
|
||||
"picotls.h"
|
||||
"picotls/openssl.h"
|
||||
"ngtcp2/ngtcp2_crypto_picotls.h"
|
||||
"utils/cred_buffer.h"
|
||||
"utils/pem_utils.h"
|
||||
1
libs/ngtcp2
Submodule
1
libs/ngtcp2
Submodule
Submodule libs/ngtcp2 added at 048cdfd0c2
1
libs/picotls
Submodule
1
libs/picotls
Submodule
Submodule libs/picotls added at bbcdbe6dc3
14189
ngtcp2.nim
14189
ngtcp2.nim
File diff suppressed because it is too large
Load Diff
@@ -3,7 +3,7 @@ version = "0.34.0"
|
||||
author = "Status Research & Development GmbH"
|
||||
description = "Nim wrapper around the ngtcp2 library"
|
||||
license = "MIT"
|
||||
installDirs = @["sources", "build"]
|
||||
installDirs = @["libs", "build"]
|
||||
installFiles = @["ngtcp2.nim"]
|
||||
|
||||
requires "nim >= 1.6.0"
|
||||
|
||||
26
prelude.nim
26
prelude.nim
@@ -6,12 +6,28 @@ import nativesockets
|
||||
|
||||
when defined(windows):
|
||||
{.passl: "-lws2_32".}
|
||||
{.passc: "-D_WINDOWS".}
|
||||
else:
|
||||
{.passC: "-DHAVE_UNISTD_H".}
|
||||
{.passc: "-DHAVE_UNISTD_H".}
|
||||
|
||||
when defined(macosx):
|
||||
{.passl: "-L/opt/homebrew/opt/openssl@3/lib -lcrypto".}
|
||||
{.passc: "-I/opt/homebrew/opt/openssl@3/include".}
|
||||
else:
|
||||
{.passl: "-lcrypto".}
|
||||
|
||||
# C include directories
|
||||
const root = currentSourcePath.parentDir
|
||||
const sourceInclude = root/"sources"/"lib"/"includes"
|
||||
const buildInclude = root/"build"/"lib"/"includes"
|
||||
const libIncludes = root/"build"/"lib"/"includes"
|
||||
const ngtcp2Crypto = root/"libs"/"ngtcp2"/"crypto"
|
||||
const ngtcp2CryptoIncludes = root/"libs"/"ngtcp2"/"crypto"/"includes"
|
||||
const ngtcp2Lib = root/"libs"/"ngtcp2"/"lib"
|
||||
const ngtcp2LibIncludes = root/"libs"/"ngtcp2"/"lib"/"includes"
|
||||
const picotlsInclude = root/"libs"/"picotls"/"include"
|
||||
|
||||
{.passc: fmt"-I{libIncludes}".}
|
||||
{.passc: fmt"-I{ngtcp2Crypto}".}
|
||||
{.passc: fmt"-I{ngtcp2CryptoIncludes}".}
|
||||
{.passc: fmt"-I{ngtcp2Lib}".}
|
||||
{.passc: fmt"-I{ngtcp2LibIncludes}".}
|
||||
{.passc: fmt"-I{picotlsInclude}".}
|
||||
|
||||
{.passc: fmt"-I{sourceInclude} -I{buildInclude}".}
|
||||
|
||||
1
sources
1
sources
Submodule sources deleted from 5f8bd54ad7
@@ -9,3 +9,13 @@ test "default settings":
|
||||
var transport_params: ngtcp2_transport_params
|
||||
ngtcp2_transport_params_default_versioned(NGTCP2_TRANSPORT_PARAMS_V1, addr transport_params)
|
||||
check transport_params.active_connection_id_limit > 0
|
||||
|
||||
test "ptls_instantiation":
|
||||
var ctx: ptls_context_t
|
||||
ctx.random_bytes = ptls_openssl_random_bytes
|
||||
ctx.get_time = addr ptls_get_time
|
||||
ctx.key_exchanges = cast[ptr ptr ptls_key_exchange_algorithm_t](addr ptls_openssl_key_exchanges[0])
|
||||
ctx.cipher_suites = cast[ptr ptr ptls_cipher_suite_t](ptls_openssl_cipher_suites[0])
|
||||
|
||||
var tls: ptr ptls_t = ptls_client_new(addr ctx);
|
||||
check tls != nil
|
||||
|
||||
Reference in New Issue
Block a user