Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,73 @@ jobs:
path: test-results
if-no-files-found: ignore

# The same PKCS #11 tests, with the token reached over TCP as a network HSM is: the relay client library is
# loaded in place of SoftHSM, and SoftHSM sits behind the relay server in a container (tools/pkcs11-relay).
pkcs11-relay:
name: "PKCS #11 over the network"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/setup-dotnet@0f4a354dd5547a28c70b7853dbd95d5b67886e73 # v6.1.0
with:
dotnet-version: 10.0.x

- name: Build the relay client library
run: |
sudo apt-get update -qq
sudo apt-get install -y -qq clang zlib1g-dev
dotnet publish tools/pkcs11-relay/Client -c Release -r linux-x64 -o "$RUNNER_TEMP/relay-client"
test -s "$RUNNER_TEMP/relay-client/sigyll-pkcs11-relay.so"
echo "SIGYLL_TEST_PKCS11_LIB=$RUNNER_TEMP/relay-client/sigyll-pkcs11-relay.so" >> "$GITHUB_ENV"

- name: Start the relay server with SoftHSM
run: |
docker build -t sigyll-pkcs11-relay tools/pkcs11-relay
docker run -d --name relay -p 127.0.0.1:5696:5696 sigyll-pkcs11-relay

- name: Test
run: dotnet test _tests/Sigyll.Signing.Tests -c Release --filter "FullyQualifiedName~Pkcs11" --logger "trx;LogFileName=pkcs11-relay.trx" --results-directory test-results

# The token tests skip when no library is set, so a green run alone does not show they used the relay.
- name: Refuse a run that never reached the relay
run: |
docker logs relay
docker logs relay 2>&1 | grep -q " connected"

- name: Keep test results
if: always()
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: test-results-pkcs11-relay
path: test-results
if-no-files-found: ignore

# The relay client for Windows. Native AOT builds only for the system it runs on, so the DLL is built here; the
# Windows runners cannot run the Linux container the server needs, so it is built and kept, not tested.
pkcs11-relay-windows:
name: Relay client for Windows
runs-on: windows-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/setup-dotnet@0f4a354dd5547a28c70b7853dbd95d5b67886e73 # v6.1.0
with:
dotnet-version: 10.0.x

- name: Build the relay client library
shell: pwsh
run: |
dotnet publish tools/pkcs11-relay/Client -c Release -r win-x64 -o relay-client
if (-not (Test-Path relay-client/sigyll-pkcs11-relay.dll)) { throw "The relay client DLL was not built." }

- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: sigyll-pkcs11-relay-win-x64
path: relay-client/sigyll-pkcs11-relay.dll

sbom:
name: Software bill of materials
runs-on: ubuntu-latest
Expand Down
3 changes: 3 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ dotnet test _tests/Sigyll.Signing.Tests
# PKCS #11 token tests against SoftHSM in a Linux container (they skip in a plain run without SoftHSM)
./tools/softhsm/run-token-tests.ps1

# The same tests on Windows, with SoftHSM reached over TCP like a network HSM (tools/pkcs11-relay/README.md)
./tools/pkcs11-relay/run-token-tests.ps1

# SQL style check for Flyway migrations
python tools/sql-lint/check_sql.py
```
Expand Down
123 changes: 123 additions & 0 deletions tools/pkcs11-relay/Client/Cryptoki.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#if CRYPTOKI_WINDOWS
global using CK_ULONG = uint;
#else
global using CK_ULONG = nuint;
#endif

using System.Runtime.InteropServices;

namespace Sigyll.Pkcs11Relay;

// The Cryptoki structures the caller passes by pointer, laid out as the platform's header lays them out: packed to
// one byte on Windows, naturally aligned elsewhere.

#if CRYPTOKI_WINDOWS
[StructLayout(LayoutKind.Sequential, Pack = 1)]
#else
[StructLayout(LayoutKind.Sequential)]
#endif
internal unsafe struct CK_ATTRIBUTE
{
public CK_ULONG type;
public void* pValue;
public CK_ULONG ulValueLen;
}

#if CRYPTOKI_WINDOWS
[StructLayout(LayoutKind.Sequential, Pack = 1)]
#else
[StructLayout(LayoutKind.Sequential)]
#endif
internal unsafe struct CK_MECHANISM
{
public CK_ULONG mechanism;
public void* pParameter;
public CK_ULONG ulParameterLen;
}

/// <summary>
/// Writes a structure of byte arrays and CK_ULONGs into the caller's memory, field by field, with the platform's
/// layout.
/// </summary>
internal unsafe ref struct StructWriter(void* target)
{
private readonly byte* _start = (byte*)target;
private int _offset;

public void Bytes(ReadOnlySpan<byte> value)
{
value.CopyTo(new Span<byte>(_start + _offset, value.Length));
_offset += value.Length;
}

public void ULong(ulong value)
{
#if !CRYPTOKI_WINDOWS
_offset = (_offset + sizeof(CK_ULONG) - 1) / sizeof(CK_ULONG) * sizeof(CK_ULONG);
#endif
*(CK_ULONG*)(_start + _offset) = Cryptoki.Narrow(value);
_offset += sizeof(CK_ULONG);
}
}

internal static unsafe class Cryptoki
{
/// <summary>The 64-bit wire value as this platform's CK_ULONG; "all ones" (unavailable information) stays all ones.</summary>
public static CK_ULONG Narrow(ulong value) => value == Wire.Unavailable ? CK_ULONG.MaxValue : (CK_ULONG)value;

public static ulong Widen(CK_ULONG value) => value == CK_ULONG.MaxValue ? Wire.Unavailable : value;

/// <summary>
/// Attributes whose value is a single CK_ULONG, so its size differs between platforms. Everything else is a
/// byte string, a one-byte boolean or a date, and is the same everywhere.
/// </summary>
public static bool IsULongAttribute(ulong type) => type is
0x000 // CKA_CLASS
or 0x080 // CKA_CERTIFICATE_TYPE
or 0x087 // CKA_CERTIFICATE_CATEGORY
or 0x088 // CKA_JAVA_MIDP_SECURITY_DOMAIN
or 0x08C // CKA_NAME_HASH_ALGORITHM
or 0x100 // CKA_KEY_TYPE
or 0x121 // CKA_MODULUS_BITS
or 0x133 // CKA_PRIME_BITS
or 0x134 // CKA_SUBPRIME_BITS
or 0x160 // CKA_VALUE_BITS
or 0x161 // CKA_VALUE_LEN
or 0x166 // CKA_KEY_GEN_MECHANISM
or 0x300 // CKA_HW_FEATURE_TYPE
or 0x500; // CKA_MECHANISM_TYPE

/// <summary>Writes a template's attributes (type and value) to a request.</summary>
public static void WriteTemplate(BinaryWriter request, CK_ATTRIBUTE* template, CK_ULONG count)
{
request.Write((int)count);
for (var i = 0; i < (int)count; i++)
{
var attribute = template[i];
request.Write(Widen(attribute.type));
if (IsULongAttribute(attribute.type) && attribute.ulValueLen == (CK_ULONG)sizeof(CK_ULONG) && attribute.pValue != null)
{
request.Write(sizeof(ulong));
request.Write(Widen(*(CK_ULONG*)attribute.pValue));
}
else
{
request.WriteBlob(attribute.pValue == null
? default
: new ReadOnlySpan<byte>(attribute.pValue, (int)attribute.ulValueLen));
}
}
}

/// <summary>
/// Writes a mechanism to a request. Its parameter travels as plain bytes, which is right for mechanisms without
/// a parameter and for byte-string parameters; parameter structures holding CK_ULONGs are not translated.
/// </summary>
public static void WriteMechanism(BinaryWriter request, CK_MECHANISM* mechanism)
{
request.Write(Widen(mechanism->mechanism));
request.WriteBlob(mechanism->pParameter == null
? default
: new ReadOnlySpan<byte>(mechanism->pParameter, (int)mechanism->ulParameterLen));
}
}
Loading
Loading